; ctrl_rom.psm - control FPGA controller code
;
; Port Declarations
;
; Output Bits
;
CONSTANT  pGLBConf,     00      ;output     bit	 non-latching
CONSTANT  pNewSeqTable, 01      ;output     bit  non-latching
CONSTANT  pGLBDisable,  02      ;output     bit	 non-latching
CONSTANT  pClrOverload, 03      ;output     bit	 non-latching
;
; Global configReg (4 x 8-bit)
;
CONSTANT  pGlobalOut0,  10      ;output     byte
CONSTANT  pGlobalOut1,  11      ;output     byte
CONSTANT  pGlobalOut2,  12      ;output     byte
CONSTANT  pGlobalOut3,  13      ;output     byte
;
; Status Reg (4 x 8-bit)
;
CONSTANT  pStatus0,     20      ;output     byte
CONSTANT  pStatus1,     21      ;output     byte
CONSTANT  pStatus2,     32      ;output     byte
CONSTANT  pStatus3,     33      ;output     byte
;
; Address Latch
;
CONSTANT  pAddress0,    30      ;output     byte
CONSTANT  pAddress1,    31      ;output     byte
CONSTANT  pAddress2,    32      ;output     byte
CONSTANT  pAddress3,    33      ;output     byte
CONSTANT  pAddressInc,  34      ;output     null
;
; Sequence Table
CONSTANT  pSeqTable,    38      ;output     byte
;
; Serial Transmitter
;
CONSTANT  pTxData,      40      ;output     byte
CONSTANT  pTxBFull,     41      ;input      bit
CONSTANT  pTxBHFull,    42      ;input      bit
;
; Serial Receiver
;
CONSTANT  pRxData,      50      ;input      byte
CONSTANT  pRxDPresent,  51      ;input      bit
CONSTANT  pRxBFull,     52      ;input      bit
CONSTANT  pRxBHFull,    53      ;input      bit
;
;
CONSTANT  SendGlb,      80      ;serial command to send global configuration
CONSTANT  SendSeqTab,   40      ;serial command to send sequence table
CONSTANT  ClrOverload,  20      ;serial command to clear overload lockout
CONSTANT  RcvStatus0,	08		;serial command to receive status 0
CONSTANT  RcvStatus1,	04		;serial command to receive status 1
;
NAMEREG     s3, cmd
NAMEREG     s4, tmp1
NAMEREG     s5, tmp2
NAMEREG     s6, arg1
NAMEREG     s7, arg2
NAMEREG     s8, adr
NAMEREG     s9, lenl
NAMEREG     sa, lenh
NAMEREG     sb, status0
NAMEREG     sc, status1
NAMEREG     sd, unused
NAMEREG     se, zero
NAMEREG     sf, one
;
;
;
Initialize:
    LOAD    zero,00
    LOAD    one,01
    LOAD    status0,00
    LOAD    status1,00
WaitCommand:
    INPUT   tmp1,pRxDPresent     ; Rx Data Present bit    
    AND     tmp1,one             ; is it set ?
    JUMP    Z,ChkStatus
    INPUT   cmd,pRxData
    LOAD    tmp1,SendGlb         ; "Send Global configuration" command 
    SUB     tmp1,cmd
	JUMP    Z,RxGlbConf
    LOAD    tmp1,SendSeqTab      ; "Send Sequence table" command 
    SUB     tmp1,cmd
	JUMP    Z,RxSeqTab
    LOAD    tmp1,ClrOverload     ; "clear overload" command 
    SUB     tmp1,cmd
	JUMP    Z,ClrOvl
ChkStatus:
    INPUT   tmp1,pTxBHFull       ; Tx FIFO half full    
    AND     tmp1,one             ; is it set ?
    JUMP    NZ,WaitCommand
ChkStatus0:
    LOAD	tmp1,status0
	INPUT	status0,pStatus0
	SUB		tmp1,status0		 ; has status 0 changed?
	JUMP	Z,ChkStatus1
	LOAD	tmp1,RcvStatus0
	OUTPUT	tmp1,pTxData
	OUTPUT	status0,pTxData
ChkStatus1:
    LOAD	tmp1,status1
	INPUT	status1,pStatus1
	SUB		tmp1,status1		 ; has status 1 changed?
	JUMP	Z,WaitCommand
	LOAD	tmp1,RcvStatus1
	OUTPUT	tmp1,pTxData
	OUTPUT	status1,pTxData
	JUMP	WaitCommand
ClrOvl:
	OUTPUT  one,pClrOverload	 ; pulse clear overload flag
	JUMP    WaitCommand
RxGlbConf:
	OUTPUT  one,pGLBDisable		 ; pulse disable flag
	CALL    WaitRx
	OUTPUT  tmp1,pGlobalOut0
	CALL    WaitRx
	OUTPUT  tmp1,pGlobalOut1
	CALL    WaitRx
	OUTPUT  tmp1,pGlobalOut2
	CALL    WaitRx
	OUTPUT  tmp1,pGlobalOut3
	OUTPUT  one,pGLBConf		 ; pulse Global Config done flag
	JUMP    WaitCommand
RxSeqTab:
	OUTPUT  zero,pAddress0       ; clear address pointer
	OUTPUT  zero,pAddress1
	LOAD    lenh,20
NextTable:
	LOAD    lenl,20
NextRx:
    INPUT   tmp1,pRxDPresent     ; Rx Data Present bit    
    AND     tmp1,one             ; is it set ?
    JUMP    Z,NextRx
    INPUT   tmp1,pRxData
	OUTPUT  tmp1,pSeqTable
	OUTPUT  zero,pAddressInc     ; increment address
	SUB     lenl,01
    JUMP    NZ,NextRx            ; loop for next byte
	SUB     lenh,01
    JUMP    NZ,NextTable         ; loop for next table
	OUTPUT  one,pNewSeqTable	 ; pulse Global Config done flag
	JUMP    WaitCommand
;
WaitRx:
    INPUT   tmp1,pRxDPresent     ; Rx Data Present bit    
    AND     tmp1,one             ; is it set ?
    JUMP    Z,WaitRx
    INPUT   tmp1,pRxData
	RETURN