; prog_rom.psm - System FPGA controller code
;
; Port Declarations
; 
; Output Bits
;

CONSTANT  pEERead,      00      ;output     bit
CONSTANT  pEEWrite,     01      ;output     bit
CONSTANT  pOverloadClr, 04      ;output     bit -- no latching
CONSTANT  pEEPROMClr,   05      ;output     bit -- no latching
CONSTANT  pSeqTableClr, 06      ;output     bit -- no latching
;
; Input Bits
;
CONSTANT  pEEBusy,      01      ;input      bit
;
; 32 Bit Data Out (4 x 8-bit)
;
CONSTANT  pDataOut0,    10      ;output     byte
CONSTANT  pDataOut1,    11      ;output     byte
CONSTANT  pDataOut2,    12      ;output     byte
CONSTANT  pDataOut3,    13      ;output     byte
CONSTANT  pDOutMask0,   14      ;output     byte
;
; 32 Bit Data In 
;
; Control Register
CONSTANT  pDataIn0,     20      ;input      byte
CONSTANT  pDataIn1,     21      ;input      byte
CONSTANT  pDataIn2,     22      ;input      byte
CONSTANT  pDataIn3,     23      ;input      byte
;
; Global Configuration Register
CONSTANT  pGlobalIn0,   28      ;input      byte
CONSTANT  pGlobalIn1,   29      ;input      byte
CONSTANT  pGlobalIn2,   2a      ;input      byte
CONSTANT  pGlobalIn3,   2b      ;input      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      ;input      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, word2
NAMEREG     sd, word3
NAMEREG     se, zero
NAMEREG     sf, one
;
;
;
Initialize:
    LOAD    zero,00
    LOAD    one,01
    LOAD    status0,00
WaitCommand:
    INPUT   cmd,pDataIn0        ; read a byte from the command register
    OR      cmd,zero            ; are any bits set ?
    JUMP    NZ,GetCommand       ; if yes then act on them
    INPUT   tmp1,pRxDPresent    ; Rx Data Present bit    
    AND     tmp1,one            ; is it set ?
    JUMP    Z,WaitCommand
    INPUT   cmd,pRxData
    LOAD    tmp1,RcvStatus0     ; "Receive status0" command 
    SUB     tmp1,cmd
	JUMP    NZ,RxStatus1
	CALL    WaitRx
    output  tmp1,pDOutMask0
	LOAD	status0,tmp1
    JUMP    WaitCommand
;
RxStatus1:
    LOAD    tmp1,RcvStatus1     ; "Receive status1" command 
    SUB     tmp1,cmd
	JUMP    NZ,WaitCommand
	CALL    WaitRx
    output  tmp1,pDataOut1
    JUMP    WaitCommand
;
GetCommand:
    LOAD    tmp1,02             ; "Write EEPROM" command
    AND     tmp1,cmd            ; is this command bit set ?
    CALL    NZ,WriteEEPROM
    LOAD    tmp1,04             ; "Read EEPROM" command 
    AND     tmp1,cmd            ; is this command bit set ?
    CALL    NZ,ReadEEPROM
    LOAD    tmp1,08             ; "Tx SeqTable" command 
    AND     tmp1,cmd            ; is this command bit set ?
    CALL    NZ,TxSeqTable
    LOAD    tmp1,10             ; "Clear Overload" command 
    AND     tmp1,cmd            ; is this command bit set ?
    CALL    NZ,ClrOvl
    JUMP    WaitCommand
;
ReadEEPROM:
    OUTPUT  one,pEEPROMClr       ; clear command
    OUTPUT  one,pEERead          ; strobe the eeprom read line
WaitReadFinish:
    INPUT   tmp1,pEEBusy         ; read the eeprom busy line    
    AND     tmp1,one             ; is it set ?
    JUMP    NZ,WaitReadFinish    ; if so, loop
	OUTPUT  status0,pDataOut0    ; clear busy flag
    RETURN
;
WriteEEPROM:
    OUTPUT  one,pEEPROMClr       ; clear command
    OUTPUT  one,pEEWrite         ; strobe the eeprom read line
WaitWriteFinish:
    INPUT   tmp1,pEEBusy         ; read the eeprom busy line
    AND     tmp1,one             ; is it set ?
    JUMP    NZ,WaitWriteFinish   ; if so, loop
	OUTPUT  status0,pDataOut0    ; clear busy flag
    RETURN
;
TxSeqTable:
    OUTPUT  one,pSeqTableClr     ; clear command
	LOAD    tmp1,SendGlb		 ; send global configuration
    OUTPUT  tmp1,pTxData
	INPUT   tmp1,pGlobalIn0
    OUTPUT  tmp1,pTxData
	INPUT   tmp1,pGlobalIn1
    OUTPUT  tmp1,pTxData
	INPUT   tmp1,pGlobalIn2
    OUTPUT  tmp1,pTxData
	INPUT   tmp1,pGlobalIn3
    OUTPUT  tmp1,pTxData
	OUTPUT  zero,pAddress0       ; clear address pointer
	OUTPUT  zero,pAddress1
	LOAD    lenh,20
NextTable:
	LOAD    lenl,20
	LOAD    tmp1,SendSeqTab		 ; send global configuration
    OUTPUT  tmp1,pTxData
NextTx:
	INPUT   tmp2,pSeqTable
WaitTxSpace:
    INPUT   tmp1,pTxBFull        ; check for Tx fifo full
    AND     tmp1,one             ; 
    JUMP    NZ,WaitTxSpace       ; if so, loop
	OUTPUT  tmp2,pTxData
	OUTPUT  zero,pAddressInc     ; increment address
	SUB     lenl,01
    JUMP    NZ,NextTx            ; loop for next byte
	SUB     lenh,01
    JUMP    NZ,NextTable         ; loop for next table
	OUTPUT  status0,pDataOut0    ; clear busy flag
    RETURN
;
ClrOvl:
    INPUT   tmp1,pTxBFull        ; check for Tx fifo full
    AND     tmp1,one             ; 
    JUMP    NZ,ClrOvl            ; if so, loop
    LOAD    tmp1,ClrOverload	 ; clear overload
	OUTPUT  tmp1,pTxData
	OUTPUT  one,pOverloadClr     ; clear command
    RETURN
;
WaitRx:
    INPUT   tmp1,pRxDPresent     ; Rx Data Present bit    
    AND     tmp1,one             ; is it set ?
    JUMP    Z,WaitRx
    INPUT   tmp1,pRxData
	RETURN
