Embedded Space
by Ed Nisley

Listing One

Wait1ms:
           PUSH   ACC          ; 2 cycles
           MOV    A,#200       ; 1 cycle
           NOP                 ; 1 cycle

W1_1:      DJNZ   ACC,W1_1     ; 400 cycles
W1_2:      DJNZ   ACC,W1_2     ; 512 cycles

           POP    ACC          ; 2 cycles
           RET                 ; 2 + 2 for CALL
Listing Two 

WaitBar:
           CLR     BusyOut     ; show we're at a new bar

           SETB    BarBitIn    ; ensure it's still an input
           MOV     C,BarBitIn  ; save current state
           MOV     F0,C 

WB_Stall:
           CPL    SpinOut      ; show we're spinning
           JB     TF0,WB_Fail  ; reset on timeout (64k ms)
           MOV    C,F0         ; XOR old & new state
           JNB    BarBitIn,WB_0
           CPL    C            ; change => 1
WB_0:      JNC    WB_Stall     ; 0 => no change

           CLR    TR0          ; stop Timer0
           MOV    R3,TH0       ; save bar time
           MOV    R2,TL0

           MOV   TH0,#HIGH 10  ; fudge for stop/start overhead
           MOV   TL0,#LOW 10
           SETB  TR0           ; start Timer0

           CLR   SpinOut       ; down until next spin
           SETB  BusyOut

           RET

WB_Fail:   JMP   CrashReset    ; restart on timeout

Listing Three 

           MOV   A,R7
           CLR   C
           RRC   A
           MOV   B,ACC         ; save high byte
           MOV   A,R6
           RRC   A             ; ACC = low byte

           XCH   A,R2          ; compute low byte
           CLR   C
           SUBB  A,R2
           XCH   A,R2          ; R2 gets low result

           MOV   A,B           ; compute high byte
           XCH   A,R3
           SUBB  A,R3
           XCH   A,R3          ; R3 get high result

          JC     FW_fail       ; C means < 0, error




2


