#include "crash82.inc"

        .db "Link port access 1.0",0

    
        ROM_CALL(CLEARLCD)              ; clear the screen
        ld      hl,Str_Screen           ; display basic screen
        ROM_CALL(D_ZT_STR)

        ld      a, $C0                  ; set output
        jp      SetAndRet
        
MainLoop:
        in      a, (0)                  ; read out port
        push    af                      ; save a copy

        and     $01                     ; get red wire input only
        add     a, '0'                  ; convert it to a character
        ld      bc, $0D04               ; print it
        ld      (CURSOR_ROW), bc
        ROM_CALL(TX_CHARPUT)

        pop     af                      ; get white wire input only
        srl     a
        push    af
        and     $01
        add     a, '0'                  ; convert it to a character
        ld      bc, $0D05               ; print it
        ld      (CURSOR_ROW), bc
        ROM_CALL(TX_CHARPUT)

        pop     af                      ; get red wire output only
        cpl
        srl     a
        push    af
        and     $01
        add     a, '0'                  ; convert it to a character
        ld      bc, $0D01               ; print it
        ld      (CURSOR_ROW), bc
        ROM_CALL(TX_CHARPUT)

        pop     af                      ; get white wire output only
        srl     a
        and     $01
        add     a, '0'                  ; convert it to a character
        ld      bc, $0D02               ; print it
        ld      (CURSOR_ROW), bc
        ROM_CALL(TX_CHARPUT)

        halt                            ; save power

        call    GET_KEY                 ; get keypad data
        
        cp      $37                     ; "MODE" pressed?
        ret     z                       ; yes, exit
        
        cp      $36                     ; "2nd" pressed?
        jr      z, SwitchRed            ; yes, switch red wire output state
        
        cp      $30                     ; "ALPHA" pressed?
        jr      z, SwitchWhite          ; yes, switch white wire output state

JrMainLoop:
        jr      MainLoop                ; loop again


#ifdef NonStandardValues

SwitchRed:
        ld      a, (Output)             ; is red wire set?
        cp      $C4
        jr      z, SwitchRed_Unset
        cp      $CC                     ; maybe, is it definitely set?
        jr      z, SwitchRed_Unset
        add     a, $04                  ; yes, unset it
        jr      SetAndRet
SwitchRed_Unset
        sub     $04                     ; no(t set), set it
        jr      SetAndRet               ; set status and return to main loop

SwitchWhite:
        ld      a, (Output)             ; is white wire set?
        cp      $C6
        jr      nc, SwitchWhite_Unset
        add     a, $08                  ; yes, unset it
        jr      SetAndRet
SwitchWhite_Unset:
        sub     $08                     ; no, set it
       ;jr      SetAndRet               ; set status and return to main loop

#else

SwitchRed:
        ld      a, (Output)             ; is red wire set?
        cp      $D4
        jr      z, SwitchRed_Unset
        cp      $FC                     ; maybe, is it definitely set?
        jr      z, SwitchRed_Unset
        add     a, $14                  ; yes, unset it
        jr      SetAndRet
SwitchRed_Unset
        sub     $14                     ; no(t set), set it
        jr      SetAndRet               ; set status and return to main loop

SwitchWhite:
        ld      a, (Output)             ; is white wire set?
        cp      $E0
        jr      nc, SwitchWhite_Unset
        add     a, $28                  ; yes, unset it
        jr      SetAndRet
SwitchWhite_Unset:
        sub     $28                     ; no, set it
       ;jr      SetAndRet               ; set status and return to main loop

#endif
       
SetAndRet:
        ld      (Output), a             ; store new value
        out     (0), a                  ; set new status

#ifndef DontShowOutputHex
        ld      hl, $0007               ; display (Output) as hex
        ld      (CURSOR_ROW), hl
        ld      hl, Output
        ld      b,2
        xor     a
HexLoop:
        rld
        push    af
        add     a,'0'
        cp      '9'+1
        jr      c,Number
        add     a,'A'-'0'-10
Number:
        ROM_CALL(TX_CHARPUT)
        pop     af
        djnz    HexLoop
        rld
#endif

        jr      JrMainLoop              ; return to main loop


Output = TEXT_MEM2

Str_Screen:
        .db "+- Output -----+"
        .db "| Red wire   x |"
        .db "| White wire x |"
        .db "+- Input ------+"
        .db "| Red wire   x |"
        .db "| White wire x |"
        .db "+--------------+",0