
        ORG 077#000
MOTR000:
        LCI 220		; delay 3 seconds
MOTR002:
        LAI 300		; start motor
        OUT 013
        JMP TIME132	; delay

WADR010:		; write address
	LBI 100
        LCH		; move H to C
        CAL WDAT026
        LBI 040
        LCL		; move L to C
        JMP WDAT026

WDAT024:		; write data entry point
        LBI 000
WDAT026:
        LAC
        CAL WMSN046	; write MS nibble
        LAC
        JMP WLSN071	; write LS nibble

WEND036:
        LBI 020
        CAL WDAT026
        XRA
        OUT 013
        RET

WMSN046:		; write MSN entry
        RRC
        RRC
        RRC
        RRC
        NDI 017
        ADB
WRIT055:
        JTP WRIT062
        ADI 200		; fix parity
WRIT062:
        LBA
        CAL WDAT102	; write DATA nibble
        JMP WPAR076	; write PARITY/STATUS nibble

WLSN071:		; write LSN entry
        NDI 017
        JMP WRIT055

WPAR076:		; write PAR nibble
        RRC
        RRC
        RRC
        RRC
WDAT102:		; enter here to write LS nibble
        NDI 017		; write nibble
        ADI 100
        OUT 013		; write it
        CAL WCMP114	; wait write complete
        LAB
        RET

WCMP114:
        INP 003		;wait write complete
        RAL
        NDA
        JFS WCMP114
        LAI 340		; then wait some more
WCMP124:
        ADI 001
        JFZ WCMP124
        RET

; Timer
TIME132:
        CAL TIME142
        DCC
        JFZ TIME132
        RET

TIME142:
        DCB
        JFZ TIME142
        RET

WRIT147:			; main write function
        CAL MOTR000
        CAL WADR010
WNXT155:
        LCM
        CAL WDAT024		; write data
        LAH
        CPD			; done?
        JFZ WNXT173
        LAL			; now finish
        CPE
        JTZ WEND201
WNXT173:
        CAL INHL204		; next byte
        JMP WNXT155

WEND201:
        CAL WEND036		; goto to normal write end routine

INHL204:			 ; Increment HL
        INL
        RFZ
        INH
        RET



READ210:			; main read routine
        LCI 060
        CAL MOTR002
RNXT215:
        CAL RBYT312		; read MS nibble
        JFP RPER305		; check parity
        LDA
        CAL RBYT312		; read LS nibble
        JFP RPER305		; check parity
        NDI 017			; save data
        LEA
        LBD
        LAD
        RLC			; shift
        RLC
        RLC
        RLC
        NDI 360			; remove SL nibble
        ADE
        LDA
        LAB
        NDI 160
        JTZ READ276		; save to memory and continue
        RLC
        RLC
        JFC READ266		; page or low address
        LHD			; page address
        JMP RNXT215

READ266:			; low address
        RLC
        JFC REND307
        LLD
        JMP RNXT215

READ276:			; save to memory
        LMD
        CAL INHL204		; next address
        JMP RNXT215		; read next

RPER305:			; parity error
        LCI 377
REND307:			; end - stop motor
        XRA
        OUT 013
        RET

RBYT312:
        LEI 000
        CAL RNIB326		; read 4 LS bits
        CAL RNIB326		; read 4 MS bits
        LAE
        RAL
        NDA
        RET			; return flags and data in ACC

RNIB326:			; read a nibble
        LCI 004
RNIB330:
        INP 003			; wait for high bit
        NDA
        JFS RNIB330
        INP 003			; double check
        NDA
        JFS RNIB330
        LBI 037			; 1.5 bit delay
        CAL TIME142
RNIB347:
        INP 003			; check bit
        NDI 200
        ADE			; add to previous bits
        RAR
        LEA
        LBI 024			; 1 bit delay
        CAL TIME142
        DCC			; all 4 bits present?
        RTZ			; yes, return
        JMP RNIB347

        HLT


        CAL READ210		; Call main read function
        HLT

        CAL WRIT147		; Call main write function
        HLT


