;=======INDIGO4.ASM=========================================================
;
        radix   hex      
;
        list p=16c54
;
c      equ     0       
z      equ     2       
dc     equ     1       
pd     equ     3       
to     equ     4       
;rp0    equ     5       ;No banking on the 'c54
;rp1    equ     6       ;
;
w       equ     0
f       equ     1       
;
;------------------------------------------------------------------------
indf    equ     00      
tmr0    equ     01      
pcl     equ     02      
status  equ     03      
fsr     equ     04      
porta   equ     05      
portb   equ     06      
;eedata  equ     08      
;eeadr   equ     09      
;pclath  equ     0a      
;intcon  equ     0b      
;
;optreg  equ     01      
;trisa   equ     05      
;trisb   equ     06      
;eecon1  equ     08      
;
;-------------------------------------------------------------------------
comflg  equ     0008            
dlyctr  equ     0009            
recreg  equ     000A            
recuse  equ     000B            
rloop   equ     000C            
rbit    equ     000D            
xmtreg  equ     000E            
xmtuse  equ     000F            
xloop   equ     0010            
xbit    equ     0011            
;
useflg  equ     0012            
;
;---------------------------------------------------------------------------
recflg  equ     00      
lbflg   equ     01      
feflg   equ     02      
ndrflg  equ     03      
roflg   equ     04      
xmtflg  equ     05      
xdrflg  equ     06      
xcflg   equ     07      
;
outport equ     porta   
outpin  equ     01
inport  equ     porta   
inpin   equ     00
;----------------------------------------------------------------------------
		  org		 0x1ff
		  goto	 start
        org     0x00            
;
start   clrf    porta
        bsf     outport,outpin  
        clrf    portb
;        bsf     status,rp0     ; Not needed for 16c54
        movlw   0xFD            ; Was 1010.1001 (E9) Now 1111.1101 (FD)
;        movwf   trisa			  ; Not needed for 16c54
;        bcf     status,rp0     ; Not needed for 16c54
		  tris	 porta			  ; Needed for 16c54
		  movlw   0x00				  ; Set PORTB as outputs
		  tris    portb
		  movlw   0xaa				  ; Put alternating pattern on portb (verify)
		  movwf   portb
        clrf    comflg          
        bsf     comflg,xcflg    
        goto    frec            ; Start RECV/XMIT/USER cycle (21/19/12 cycles)
;---------------------------------------------------------------------------
;        include taskfull.grp
;====== TASKFULL.GRP =======================================================
;------RECEIVE ROUTINE 21 cycles--------------------------------------------
rdeld   set     rdele           
;
frec    btfss   comflg,recflg   
        goto    frec2           
        decfsz  rloop,f         
        goto    rdelb           
        btfsc   comflg,lbflg    
        goto    frec3           
        bsf     status,c       
        btfss   inport,inpin    
        bcf     status,c       
        rrf     recreg,f        
        movlw   04              
        movwf   rloop
        decfsz  rbit,f          
        goto    rdele           
        btfss   comflg,ndrflg   
        bsf     comflg,roflg    
        bsf     comflg,lbflg    
        movf    recreg,w        
        movwf   recuse
        goto    rdelf           
;
frec2   btfsc   inport,inpin    
        goto    rdela           
        bsf     comflg,recflg   
        movlw   08              
        movwf   rbit
        movlw   05              
        movwf   rloop
        goto    rdelc           
;
frec3   btfsc   inport,inpin    
        goto    frec4           
        bsf     comflg,feflg    
frec4   bcf     comflg,recflg   
        bcf     comflg,lbflg    
        bsf     comflg,ndrflg   
        goto    rdeld           
;
rdelb   nop                     
;rdela   goto    rda1            
;rda1    goto    rda2            
;rda2    goto    rdelc
rdela	  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  goto	  fxmt

rdelc   nop                     
        goto    rdele
rdele   goto    rda3            
rda3    goto    rda4
rda4    goto    rdelf
;
rdelf

;----TRANSMIT ROUTINE 19 cycles-----------------------------------------------------
fxmt    btfss   comflg,xmtflg   
        goto    fxmt2           
        decfsz  xloop,f         
        goto    xdlyb           
        movlw   04              
        movwf   xloop
        decfsz  xbit,f          
        goto    fxmt4           
        bcf     comflg,xmtflg   
        bsf     comflg,xcflg    
        goto    xdlyc           

;
fxmt2   btfss   comflg,xdrflg   
        goto    xdlya           
        bsf     comflg,xmtflg   
        movlw   0a              
        movwf   xbit
        movlw   04              
        movwf   xloop
        movf    xmtuse,w        
        movwf   xmtreg
        bcf     comflg,xdrflg   
        goto    fxd1            
fxd1    bcf     outport,outpin  
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
        goto    fxmt8           
;
;
fxmt4   bsf     status,c       
        rrf     xmtreg,f        
;
fxmt5   btfsc   status,c       
        goto    fxmt6           
        bcf     outport,outpin  
        goto    fxmt7           
fxmt6   bsf     outport,outpin  
        nop                     
fxmt7   nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  goto    fxmt8           
;
;

xdlyc   goto    fxd4		; 6 5
fxd4    goto    fxd5		; 4 3
fxd5    nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  nop
		  goto    fxmt8	; 2 1

xdlyb   nop					; 20
xdlya   nop					; 19
		  nop					; 18
		  nop					; 17
		  nop					; 16
		  nop					; 15
		  nop					; 14
		  nop					; 13
		  nop					; 12
		  nop					; 11
		  nop					; 10
		  nop					; 9
		  nop					; 8
		  nop					; 7
		  nop					; 6
		  nop					; 5
		  nop					; 4
		  nop					; 3
		  nop					; 2
		  nop					; 1
;
fxmt8
;
;-----------------------------------------------------------------------------
;dloop  decfsz  dlyctr,f
;        goto    dloop
;        retlw   0
;-----------------------------------------------------------------------------

;
;------USER ROUTINE 12 cycles-------------------------------------------------
echo    btfss   comflg,ndrflg   ; New data received?  If yes, skip next
        goto    udlya           ; No.  Delay and exit
        movf    recuse,w        ; Yes, Get received data
		  movwf   portb
        bcf     comflg,ndrflg   ; Clear new-data received flag
uda3    goto    frec            ;Last line of user

udlya   nop
		  nop
alldun  goto frec

dloop  decfsz  dlyctr,f			  ;3n+5 (includes call statement and return)
        goto    dloop
        retlw   0
;-----------------------------------------------------------------------
       end                      
;-----------------------------------------------------------------------

