; Target assembler: cc65 v2.17.0
; 6502bench SourceGen v1.1.0-dev1
         .SETCPU "6502"
DISP1    =       $A3
DISP2    =       $A4
DISP3    =       $A5
DISP4    =       $A6
DISP5    =       $A7
Hours    =       $A9
Start_Ram =      $0200
LEDS_Port =      $A000
Keys_Port =      $A001
DDRA     =       $A003      ;Data direction register A
PIA_IER  =       $A00E

         .ORG    $F800
         JMP     KEYIN      ;Query the keyboard with wait

         JMP     Keyin_No_Wait ;Query the keyboard without waiting

         JMP     Mode_Display_On ;Switch on mode display Ext

         JMP     DISPY      ;Turn on the mode display system

         JMP     X_Y_on_Stack ;Put A X Y on the stack

         JMP     X_Y_From_Stack ;Get A X Y from stack

         JMP     LFC7C      ;Enter a 2-digit hex number (wait)

         JMP     LFC4A      ;Enter a 4-digit hex number (wait)

         JMP     Reset_Start_Routine ;Jump back to the monitor directly

Monitor_65:
         JMP     Monitor65  ;Return to the monitor after Enter

         JMP     Timer_Delay ;Time loop 0.1 sec X (Accu)

         JMP     INC_16     ;Increment 16 bit adresss in X reg

         JMP     DEC_16     ;Decrement 16 bit adresss in X reg

         JMP     ERR_Message ;Error message with error code (Accu)

         JMP     LF917      ;Output Value in ACC to Port B (Darling)  

         JMP     $FBE0      ;Display Port B ???

         JMP     Wait_for_64

         JMP     Sub2

         JMP     Sub3

         .byte   $26
ERR_Message:
         .byte   $20
         CPX     #$FB
         AND     #$0F
         TAY
         LDX     #$05
         LDA     Message3,y
@Inner_Loop4:
         STA     DISP1,x
         LDA     Message2,x
         DEX
         BPL     @Inner_Loop4
         JSR     Wait_for_68
         JSR     DISPY
         RTS

LF854:   LDX     #$A0
         JSR     DEC_16
         LDX     #$00
         LDA     ($A0,x)
         LDX     #$A0
         JSR     INC_16
         LDX     #$07
@LF864:  CMP     @LF8D1,x
         BEQ     @LF874
         DEX
         BPL     @LF864
         LDA     #$04
         JSR     ERR_Message
         JMP     LF99C

@LF874:  LDA     $A0
         PHA
         LDA     $A1
         PHA
         JSR     Disp_On_Port_B
         LDA     #$2F
         STA     DISP5
         LDA     #$09
         STA     DISP5+1
         JSR     LFC4A
         PLA
         STA     $E9
         PLA
         STA     $E8
         LDA     $A0
         SEC
         SBC     $E8
         STA     $EA
         LDA     $A1
         SBC     $E9
         BMI     @LF8AC
         BNE     @LF8C5
         DEC     $EA
         LDA     $EA
         CMP     #$80
         BCS     @LF8C9
         LDX     #$00
         STA     ($E8,x)
         JMP     @LF8BA

@LF8AC:  CMP     #$FF
         BNE     @LF8C9
         DEC     $EA
         LDA     $EA
         BPL     @LF8C9
         LDX     #$00
         STA     ($E8,x)
@LF8BA:  LDA     $E8
         STA     $A0
         LDA     $E9
         STA     $A1
         JMP     LF99C

@LF8C5:  LDA     #$02
         BNE     @LF8CB

@LF8C9:  LDA     #$03
@LF8CB:  JSR     ERR_Message
         JMP     @LF8BA

@LF8D1:  .BYTE   $90
         .BYTE   $B0
         .BYTE   $F0
         .BYTE   $30
         .BYTE   $D0
         .BYTE   $10
         .BYTE   $50
         .BYTE   $70

LF8D9:   LDX     #$FF
@LF8DB:  DEX
         LDA     Start_Ram,x
         STA     Start_Ram+1,x
         LDA     #$EA
         STA     Start_Ram,x
         CPX     $A0
         BNE     @LF8DB
         INC     $A0
         JMP     LF99C

X_Y_on_Stack:
         CLC
         BCC     LF8F4

X_Y_From_Stack:
         SEC
LF8F4:   STA     $FF
         PLA
         STA     $FD
         PLA
         STA     $FE
         BCS     @LF909
         LDA     $FF
         PHA
         TXA
         PHA
         TYA
         PHA
         LDA     $FF
         BCC     @LF90E

@LF909:  .BYTE   $68
         .BYTE   $A8
         .BYTE   $68
         .BYTE   $AA
         .BYTE   $68

@LF90E:  INC     $FD
         BNE     @LF914
         INC     $FE
@LF914:  JMP     ($00FD)

LF917:   PHA
         JSR     @LF925
         TAX
         PLA
         ROR     A
         ROR     A
         ROR     A
         ROR     A
         JSR     @LF925
         RTS

@LF925:  AND     #$0F
         CMP     #$0A
         BCS     @LF92E
         ORA     #$30
         RTS

@LF92E:  ADC     #$36
         RTS

Timer_Delay:
         JSR     X_Y_on_Stack ;Start of Timer Delay
@Outer_Loop:
         LDY     #$4B       ;0.1 Sec times Val in Acc.
@Middle_Loop:
         LDX     #$FF
@Inner_Loop:
         DEX
         BNE     @Inner_Loop
         DEY
         BNE     @Middle_Loop
         SEC                ;Set Carry Flag
         SBC     #$01
         BNE     @Outer_Loop
         JSR     X_Y_From_Stack
         RTS                ;Return from Timer

Monitor65:
         JSR     Wait_for_68
Reset_Start_Routine:
         LDX     #$FF       ;NORMAL Entry Point
         TXS
         JSR     Init_PIA
         LDA     #$33
         CMP     $D3
         BNE     @LF95A
         CMP     $D4
         BEQ     LF987
@LF95A:  STA     $D3
         STA     $D4
         JSR     Clear_Block_C
         JSR     Clear_RAM
         JSR     Mode_Display_On
         LDX     #$05
@Inner_Loop5:
         LDA     Message5,x
         STA     DISP1,x
         DEX
         BPL     @Inner_Loop5
         JSR     Wait_for_68
         JSR     DISPY
         DEC     $E003
         BNE     LF987
         INC     $E004
         BNE     LF987
         JSR     Disp_On_Port_B
         JMP     $E005

LF987:   JSR     LFA92
         LDA     $C0
         BNE     @LF991
         JMP     ($00C1)

@LF991:  JSR     DISPY
         LDA     #$02
         STA     $A1        ;Display 2 Var
         LDA     #$00
         STA     $A0        ;Display 1 Var
LF99C:   LDX     #$00
         LDA     ($A0,x)
         STA     $A2
Wait_For_Enter:
         JSR     KEYIN
         CMP     #$58
         BEQ     @Key_was_58
         CMP     #$68
         BEQ     @Key_was_68
         CMP     #$64
         BNE     @Key_was_64
         JMP     @LFA06

@Key_was_64:
         CMP     #$62
         BNE     @Key_was_62
         JMP     @LFA28

@Key_was_62:
         CMP     #$51
         BEQ     @LF9EA
         BNE     @LFA0E

@Key_was_58:
         LDA     #$00
         STA     $E6
         JSR     Disp_On_Port_B
         LDA     #$7D
         STA     DISP5
         LDA     #$1F
         STA     DISP5+1
         JSR     LFC4A
         JMP     @LF9E1

@Key_was_68:
         LDA     $A2
         LDX     #$00
         STA     ($A0,x)
         LDX     #$A0
         JSR     INC_16
@LF9E1:  LDX     #$00
         LDA     ($A0,x)
         STA     $A2
         JMP     Wait_For_Enter

@LF9EA:  LDA     $A0
         STA     $E8
         LDA     $A1
         STA     $E9
         LDA     #$00
         STA     $A0
         STA     $A1
         STA     $A2
         JSR     Disp_On_Port_B
         LDA     $AD
         LDX     $AE
         LDY     $AF
         JMP     ($00E8)

@LFA06:  LDX     #$A0
         JSR     DEC_16
         JMP     @LF9E1

@LFA0E:  LDX     #$FF
         STX     $E6
@LFA12:  INX
         CMP     Message6,x
         BNE     @LFA12
         TXA
         ASL     $A2
         ASL     $A2
         ASL     $A2
         ASL     $A2
         ORA     $A2
         STA     $A2
         JMP     Wait_For_Enter

@LFA28:  JSR     Disp_On_Port_B
         LDA     #$14
         STA     DISP5
         LDA     #$0D
         STA     DISP5+1
         JSR     KEYIN
         JSR     DISPY
         CMP     #$41
         BNE     @LFA40
         JMP     Clock

@LFA40:  CMP     #$21
         BNE     @LFA47
         JMP     LFC14

@LFA47:  CMP     #$42
         BNE     @LFA4E
         JMP     LF854

@LFA4E:  CMP     #$31
         BNE     @LFA58
         JSR     Disp_On_Port_B
         JMP     $E000

@LFA58:  CMP     #$11
         BNE     @LFA5F
         JMP     LF8D9

@LFA5F:  JMP     Reset_Start_Routine

Wait_for_68:
         JSR     KEYIN      ;Wait for a Key
         CMP     #$68       ;Compare something
         BNE     Wait_for_68 ;Wait till it happens
         RTS                ;Return from Sub

KEYIN:   LDA     $E3        ;Start of Key In Routine
         BEQ     KEYIN      ;Wait for Key Press ?
Keyin_No_Wait:
         LDA     $E3
         PHA
         LDA     #$00
         STA     $E3
         PLA
         RTS

LFA77:   JSR     Mode_Display_On
         LDX     #$05
@LFA7C:  LDA     Message4,x
         STA     DISP1,x
         DEX
         BPL     @LFA7C
         RTS

LFA85:   JSR     Mode_Display_On
         LDX     #$05
@LFA8A:  LDA     #$00
         STA     DISP1,x
         DEX
         BPL     @LFA8A
         RTS

LFA92:   LDY     #$0A
@LFA94:  JSR     LFA77
         LDA     #$02
         JSR     Timer_Delay
         JSR     LFA85
         LDA     #$02
         JSR     Timer_Delay
         DEY
         BNE     @LFA94
         RTS

IRQ_Routine:
         PHA
         TXA
         PHA
         TYA
         PHA
         TSX
         LDA     $0104,x
         AND     #$10
         BEQ     @LFAB8
         JMP     LFC2D

@LFAB8:  BIT     $A00D      ;PIA Interrupt Enable Register
         BVS     @LFAC4
         LDA     $C3
         BNE     @LFAC4
         JMP     ($00C4)

@LFAC4:  LDA     $C9
         BNE     @LFAD1
         JSR     @LFACE
         JMP     @LFAD1

@LFACE:  JMP     ($00CA)

@LFAD1:  LDA     DDRA+1     ;T1 low-byte latch or T1 low-byte Counter
         CLI
         LDA     $E5
         BNE     @LFADF
         JSR     Display_Something
         JSR     LFBA7
@LFADF:  JSR     Decode_Key
         CLC
         ADC     #$10
         CMP     #$70
         BCS     @LFAEE
         STA     Keys_Port
         BCC     @LFAF3

@LFAEE:  AND     #$90
         STA     Keys_Port
@LFAF3:  LDX     $E4
         BNE     @LFB03
         AND     #$70
         LSR     A
         LSR     A
         LSR     A
         LSR     A
         TAX
         LDA     $A2,x
         STA     LEDS_Port
@LFB03:  JSR     Decode_Key
         LDX     $EC
         DEX
         BEQ     @LFB18
         BPL     @LFB31
         TAX
         AND     #$0F
         BEQ     @LFB45
         STX     $EB
         INC     $EC
         BNE     @LFB45
@LFB18:  TAX
         AND     #$70
         STA     $ED
         LDA     $EB
         AND     #$70
         CMP     $ED
         BNE     @LFB45
         TXA
         CMP     $EB
         BNE     @LFB45
         STA     $E3
         INC     $EC
         JMP     @LFB45

@LFB31:  CMP     $EB
         BEQ     @LFB45
         AND     #$70
         STA     $ED
         LDA     $EB
         AND     #$70
         CMP     $ED
         BNE     @LFB45
         LDA     #$00
         STA     $EC
@LFB45:  SED
         DEC     $D5
         BNE     @LFB76
         DEC     $D6
         BPL     @LFB76
         LDA     #$90
         STA     $D5
         LDA     #$01
         STA     $D6
         LDY     #$00
         LDX     #$02
@LFB5A:  LDA     Hours,x    ;00A9 is Hours Var
         CLC
         ADC     #$01
         CPX     #$00
         BNE     @LFB69
         CMP     #$24       ;24 hours?
         BCS     @LFB6D
         BCC     @LFB74

@LFB69:  CMP     #$60       ;60 mins?
         BCC     @LFB74
@LFB6D:  STY     Hours,x
         DEX
         BPL     @LFB5A
         BMI     @LFB76

@LFB74:  STA     Hours,x
@LFB76:  CLD
         PLA
         TAY
         PLA
         TAX
         PLA
         RTI

Decode_Key:
         LDA     #$7F       ;Decode Keypress
         AND     Keys_Port
         EOR     #$0F
         RTS

Mode_Display_On:
         PHA
         LDA     #$FF
         BNE     LFB8D

DISPY:   PHA                ;Start of Display Routine
         LDA     #$00
LFB8D:   STA     $E5
         JSR     Init_PIA
         PLA
         RTS

Display_Something:
         LDA     $A1
         JSR     LFBB1
         STX     DISP1
         STY     DISP2
         LDA     $A0
         JSR     LFBB1
         STX     DISP3
         STY     DISP4
         RTS

LFBA7:   LDA     $A2
         JSR     LFBB1
         STX     DISP5
         STY     DISP5+1
         RTS

LFBB1:   TAY
         LSR     A
         LSR     A
         LSR     A
         LSR     A
         TAX
         LDA     Message3,x
         TAX
         TYA
         AND     #$0F
         TAY
         LDA     Message3,y
         TAY
         RTS

Wait_for_64:
         JSR     KEYIN      ;Get a Key
         CMP     #$64       ;Compare with something
         BEQ     @Leave_Sub ;Set Carry Flag and RTS
         LDX     #$00
@LFBCD:  CMP     Message6,x
         BEQ     @LFBD9     ;Jump out of sub
         INX
         CPX     #$10
         BEQ     Wait_for_64
         BNE     @LFBCD

@LFBD9:  TXA
         AND     #$0F
         CLC
         RTS

@Leave_Sub:
         SEC
         RTS

Disp_On_Port_B:
         JSR     X_Y_on_Stack
         JSR     LFCF8
         JSR     Mode_Display_On
         LDX     #$05
         LDA     #$08
@Loop1:  STA     DISP1,x
         DEX
         BPL     @Loop1     ;Keep Going until all shown
         JSR     X_Y_From_Stack
         RTS

Clock:   JSR     Disp_On_Port_B
         LDA     #$FD
         STA     DISP5+1
         LDA     #$3D
         STA     DISP5
         JSR     LFC4A
         LDA     $A1
         STA     Hours      ;Store in Hours Var
         LDA     $A0
         STA     Hours+1
         LDA     #$00
         STA     $D5
         STA     $D6
         STA     Hours+2
LFC14:   JSR     DISPY
@LFC17:  LDA     Hours
         STA     $A1
         LDA     Hours+1
         STA     $A0
         LDA     Hours+2
         STA     $A2
         JSR     Keyin_No_Wait
         CMP     #$68
         BNE     @LFC17
         JMP     LF987

LFC2D:   LDA     $CC
         BNE     @LFC34
         JMP     ($00CD)

@LFC34:  JSR     DISPY
         TSX
         LDA     $0103,x
         STA     $A2
         LDA     $0101,x
         STA     $A0
         LDA     $0102,x
         STA     $A1
         JMP     Monitor65

LFC4A:   JSR     Wait_for_64
         BCS     LFC4A
         ASL     A
         ASL     A
         ASL     A
         ASL     A
         STA     $E8
         LDA     Message3,x
         STA     DISP1
         LDA     $A1
         AND     #$0F
         ORA     $E8
         STA     $A1
LFC62:   JSR     Wait_for_64
         BCC     @LFC6D
         LDA     #$08
         STA     DISP1
         BNE     LFC4A

@LFC6D:  STA     $E8
         LDA     Message3,x
         STA     DISP2
         LDA     $A1
         AND     #$F0
         ORA     $E8
         STA     $A1
LFC7C:   JSR     Wait_for_64
         BCC     @LFC87
         LDA     #$08
         STA     DISP2
         BNE     LFC62

@LFC87:  ASL     A
         ASL     A
         ASL     A
         ASL     A
         STA     $E8
         LDA     Message3,x
         STA     DISP3
         LDA     $A0
         AND     #$0F
         ORA     $E8
         STA     $A0
         JSR     Wait_for_64
         BCC     @LFCA5
         LDA     #$08
         STA     DISP3
         BNE     LFC7C

@LFCA5:  STA     $E8
         LDA     Message3,x
         STA     DISP4
         LDA     $A0
         AND     #$F0
         ORA     $E8
         STA     $A0
         JSR     DISPY
         RTS

INC_16:  INC     $00,x
         BNE     @LFCBE
         INC     $01,x
@LFCBE:  RTS

DEC_16:  PHA
         DEC     $00,x
         LDA     $00,x
         CMP     #$FF
         BNE     @LFCCA
         DEC     $01,x
@LFCCA:  PLA
         RTS

Sub2:    JSR     DISPY
@LFCCF:  LDA     #$01
         JSR     Timer_Delay
         INC     $A2
         BNE     @LFCCF
         INC     $A0
         BNE     @LFCCF
         INC     $A1
         JMP     @LFCCF

Sub3:    LDA     #$00
         STA     $D3
         JMP     Reset_Start_Routine

Init_PIA:
         LDX     #$7F       ;Initialise PIA
         STX     PIA_IER    ;PIA Interrupt Enable Register
         LDX     #$0E
@Inner_Loop3:
         LDA     Start_Message,x ;Move Chars to LEDS
         STA     LEDS_Port,x ;PIA Output Reg B..Leds
         DEX
         BPL     @Inner_Loop3 ;Fourteen Times
LFCF8:   LDX     #$00
         STX     $E4
         CLD
         CLI
         RTS

         .BYTE   $20
         .BYTE   $07
         .BYTE   $FD
         .BYTE   $A9
         .BYTE   $06
         .BYTE   $4C
         .BYTE   $3A
         .BYTE   $F8
Clear_Block_C:
         LDX     #$02
@inner_loop2:
         LDA     Jump_Forward+3,x ;FF to ACC
         STA     $C0,x      ;Store it in RAM
         STA     $C3,x
         STA     $C6,x
         STA     $C9,x
         STA     $CC,x
         DEX
         BPL     @inner_loop2
         RTS

Clear_RAM:
         LDA     #$00       ;Looks like this is a sub to clear all the RAM to 0
         STA     $AD        ;and these Vars
         STA     $AE
         STA     $AF
         LDX     #$09
@Loop2:  STA     $E3,x
         DEX
         BPL     @Loop2
         TAX
@Loop_Back:
         STA     Start_Ram,x
         STA     $0300,x
         STA     $0400,x
         STA     $0500,x
         STA     $0600,x
         STA     $0700,x
         INX
         BNE     @Loop_Back
         STA     Hours      ;and the Var for the Clock digits
         STA     Hours+1
         STA     Hours+2
         RTS

NMI_Start_Routine:
         JSR     X_Y_on_Stack ;Start Here for Non Maskable Int
         LDA     $C6
         BNE     Jump_Forward ;Reset
         JMP     ($00C7)    ;otherwise go to Sub pointed at by 00C7

Jump_Forward:
         JMP     (Reset_Vector) ;Reset Board

         .BYTE   $FF
         .BYTE   $FF
         .BYTE   $FC
Start_Message:
         .BYTE   $00        ;Boot up Message to LEDS (14 Chars)
         .BYTE   $10        ;Ian Day I guess....
         .BYTE   $7F
         .BYTE   $70
         .BYTE   $6B
         .BYTE   $04
         .RES    5,$00      ;Empty ROM address
         .BYTE   $40
         .BYTE   $00
         .BYTE   $00
Message2:
         .BYTE   $C0
         .BYTE   $6B
         .BYTE   $09
         .BYTE   $09
         .BYTE   $0F
         .BYTE   $09
         .RES    618,$FF
Message3:
         .BYTE   $77
         .BYTE   $14
         .BYTE   $5B
         .BYTE   $5E
         .BYTE   $3C
         .BYTE   $6E
         .BYTE   $6F
         .BYTE   $54
         .BYTE   $7F
         .BYTE   $7E
         .BYTE   $7D
         .BYTE   $2F
         .BYTE   $63
         .BYTE   $1F
         .BYTE   $6B
         .BYTE   $69
Message4:
         .BYTE   $21
         .BYTE   $7D
         .BYTE   $F5
         .BYTE   $F7
         .BYTE   $7D
         .BYTE   $BE
Message5:
         .BYTE   $39
         .BYTE   $79
         .BYTE   $6E
         .BYTE   $08
         .BYTE   $6F
         .BYTE   $6E
Message6:
         .BYTE   $18
         .BYTE   $28
         .BYTE   $38
         .BYTE   $48
         .BYTE   $14
         .BYTE   $24
         .BYTE   $34
         .BYTE   $44
         .BYTE   $12
         .BYTE   $22
NMI_Vector:
         .BYTE   $46        ;NMI
         .BYTE   $FD
Reset_Vector:
         .BYTE   $4A        ;Reset
         .BYTE   $F9
IRQ_Vector:
         .BYTE   $A8        ;IRQ
         .BYTE   $FA
