C64 Kernal Disassembly - Bank $E000 Please check out the Notable Development's Javascript minimiser at www.ndl-jsdevtools.com

View Bank at $A000
 
Funct_e000 STA $56
$E002 JSR Funct_bc0f
$E005 LDA $61
$E007 CMP #$88
$E009 BCC Loc_e00e
Loc_e00b JSR Funct_bad4
Loc_e00e JSR Funct_bccc
$E011 LDA $07
$E013 CLC
$E014 ADC #$81
$E016 BEQ Loc_e00b
$E018 SEC
$E019 SBC #$1
$E01B PHA
$E01C LDX #$5
Loc_e01e LDA $69,X
$E020 LDY $61,X
$E022 STA $61,X
$E024 STY $69,X
$E026 DEX
$E027 BPL Loc_e01e
$E029 LDA $56
$E02B STA $70
$E02D JSR Funct_b853
$E030 JSR Funct_bfb4
$E033 LDA #$c4
$E035 LDY #$bf
$E037 JSR Funct_e059
$E03A LDA #$0
$E03C STA $6F
$E03E PLA
$E03F JSR Funct_bab9
$E042 RTS

Loc_e043 STA $71
$E045 STY $72
$E047 JSR Funct_bbca
$E04A LDA #$57
$E04C JSR Funct_ba28
$E04F JSR Funct_e05d
$E052 LDA #$57
$E054 LDY #$0
$E056 JMP Funct_ba28

Funct_e059 STA $71
$E05B STY $72
Funct_e05d JSR Funct_bbc7
$E060 LDA ($71),Y
$E062 STA $67
$E064 LDY $71
$E066 INY
$E067 TYA
$E068 BNE Loc_e06c
$E06A INC $72
Loc_e06c STA $71
$E06E LDY $72
Loc_e070 JSR Funct_ba28
$E073 LDA $71
$E075 LDY $72
$E077 CLC
$E078 ADC #$5
$E07A BCC Loc_e07d
$E07C INY
Loc_e07d STA $71
$E07F STY $72
$E081 JSR Funct_b867
$E084 LDA #$5c
$E086 LDY #$0
$E088 DEC $67
$E08A BNE Loc_e070
$E08C RTS

$E08D .DB $98
$E08E .DB $35
$E08F .DB $44
$E090 .DB $7a
$E091 .DB $0
$E092 .DB $68
$E093 .DB $28
$E094 .DB $b1
$E095 .DB $46
$E096 .DB $0
$E097 JSR Funct_bc2b
$E09A BMI Loc_e0d3
$E09C BNE Loc_e0be
$E09E JSR Funct_fff3
$E0A1 STX StringPtr?_0x22
$E0A3 STY StringPtr?_0x22+1
$E0A5 LDY #$4
$E0A7 LDA (StringPtr?_0x22),Y
$E0A9 STA $62
$E0AB INY
$E0AC LDA (StringPtr?_0x22),Y
$E0AE STA $64
$E0B0 LDY #$8
$E0B2 LDA (StringPtr?_0x22),Y
$E0B4 STA $63
$E0B6 INY
$E0B7 LDA (StringPtr?_0x22),Y
$E0B9 STA $65
$E0BB JMP Loc_e0e3
Loc_e0be LDA #$8b
$E0C0 LDY #$0
$E0C2 JSR Funct_bba2
$E0C5 LDA #$8d
$E0C7 LDY #$e0
$E0C9 JSR Funct_ba28
$E0CC LDA #$92
$E0CE LDY #$e0
$E0D0 JSR Funct_b867
Loc_e0d3 LDX $65
$E0D5 LDA $62
$E0D7 STA $65
$E0D9 STX $62
$E0DB LDX $63
$E0DD LDA $64
$E0DF STA $63
$E0E1 STX $64
Loc_e0e3 LDA #$0
$E0E5 STA $66
$E0E7 LDA $61
$E0E9 STA $70
$E0EB LDA #$80
$E0ED STA $61
$E0EF JSR Funct_b8d7
$E0F2 LDX #$8b
$E0F4 LDY #$0
Funct_e0f6 JMP Loc_bbd4

Loc_e0f9 CMP #$f0
$E0FB BNE Loc_e104
$E0FD STY $38
$E0FF STX $37
$E101 JMP Loc_a663

Loc_e104 TAX
$E105 BNE Loc_e109
$E107 LDX #$1e
Loc_e109 JMP Loc_a437
BASIC_OurChar_e10c JSR OutChar
$E10F BCS Loc_e0f9
$E111 RTS

BASIC_ReadCmdLine? JSR Funct_ffcf
$E115 BCS Loc_e0f9
$E117 RTS

Funct_e118 JSR Funct_e4ad
$E11B BCS Loc_e0f9
$E11D RTS

Funct_e11e JSR Funct_ffc6
$E121 BCS Loc_e0f9
$E123 RTS

Funct_e124 JSR Funct_ffe4
$E127 BCS Loc_e0f9
$E129 RTS

$E12A JSR Funct_ad8a
$E12D JSR BASIC_ReadInt?_b7f7
$E130 LDA #>$E144+2
$E132 PHA
$E133 LDA #>$E144+2
$E135 PHA
$E136 LDA $030F
$E139 PHA
$E13A LDA $030C
$E13D LDX $030D
$E140 LDY $030E
$E143 PLP
$E144 JMP (BASIC_CurrentInt)

$E147 PHP
$E148 STA $030C
$E14B STX $030D
$E14E STY $030E
$E151 PLA
$E152 STA $030F
$E155 RTS

$E156 JSR Funct_e1d4
$E159 LDX LoadAddressLo
$E15B LDY LoadAddressHi
$E15D LDA #$2b
$E15F JSR Kernal_Save
$E162 BCS Loc_e0f9
$E164 RTS

$E165 LDA #$1
$E167 BIT $00A9
$E16A STA $0A
$E16C JSR Funct_e1d4
$E16F LDA $0A
$E171 LDX BasicCodeStartAddress
$E173 LDY BasicCodeStartAddress+1
$E175 JSR Kernal_Load
$E178 BCS Loc_e1d1
$E17A LDA $0A
$E17C BEQ Loc_e195
$E17E LDX #$1c
$E180 JSR Funct_ffb7
$E183 AND #$10
$E185 BNE Loc_e19e
$E187 LDA BASIC_CMDPtrMinus1
$E189 CMP #$2
$E18B BEQ Loc_e194
$E18D LDA #<OKText
$E18F LDY #>OKText
$E191 JMP PrintStingYA_ab1e
Loc_e194 RTS

Loc_e195 JSR Funct_ffb7
$E198 AND #$bf
$E19A BEQ Loc_e1a1
$E19C LDX #$1d
Loc_e19e JMP Loc_a437
Loc_e1a1 LDA BASIC_CMDPtrMinus1+1
$E1A3 CMP #$2
$E1A5 BNE Loc_e1b5
$E1A7 STX LoadAddressLo
$E1A9 STY LoadAddressHi
$E1AB LDA #<ReadyText
$E1AD LDY #>ReadyText
$E1AF JSR PrintStingYA_ab1e
$E1B2 JMP Loc_a52a

Loc_e1b5 JSR Funct_a68e
$E1B8 JSR Funct_a533
$E1BB JMP Loc_a677
BASIC_Exec_OPEN JSR Funct_e219
$E1C1 JSR Funct_ffc0
$E1C4 BCS Loc_e1d1
$E1C6 RTS

$E1C7 JSR Funct_e219
$E1CA LDA $49
$E1CC JSR Funct_ffc3
$E1CF BCC Loc_e194
Loc_e1d1 JMP Loc_e0f9

Funct_e1d4 LDA #$0
$E1D6 JSR Call_SetFileName
$E1D9 LDX #$1
$E1DB LDY #$0
$E1DD JSR Call_SetLFS
$E1E0 JSR Funct_e206
$E1E3 JSR Funct_e257
$E1E6 JSR Funct_e206
$E1E9 JSR Funct_e200
$E1EC LDY #$0
$E1EE STX $49
$E1F0 JSR Call_SetLFS
$E1F3 JSR Funct_e206
$E1F6 JSR Funct_e200
$E1F9 TXA
$E1FA TAY
$E1FB LDX $49
$E1FD JMP Call_SetLFS

Funct_e200 JSR Funct_e20e
$E203 JMP Loc_b79e

Funct_e206 JSR BASIC_GetCrntCMDLineChar
$E209 BNE Exit_e20d
$E20B PLA
$E20C PLA
Exit_e20d RTS

Funct_e20e JSR Funct_aefd
Funct_e211 JSR BASIC_GetCrntCMDLineChar
$E214 BNE Exit_e20d
$E216 JMP Loc_af08

Funct_e219 LDA #$0
$E21B JSR Call_SetFileName
$E21E JSR Funct_e211
$E221 JSR Loc_b79e
$E224 STX $49
$E226 TXA
$E227 LDX #$1
$E229 LDY #$0
$E22B JSR Call_SetLFS
$E22E JSR Funct_e206
$E231 JSR Funct_e200
$E234 STX $4A
$E236 LDY #$0
$E238 LDA $49
$E23A CPX #$3
$E23C BCC Loc_e23f
$E23E DEY
Loc_e23f JSR Call_SetLFS
$E242 JSR Funct_e206
$E245 JSR Funct_e200
$E248 TXA
$E249 TAY
$E24A LDX $4A
$E24C LDA $49
$E24E JSR Call_SetLFS
$E251 JSR Funct_e206
$E254 JSR Funct_e20e
Funct_e257 JSR Funct_ad9e
$E25A JSR BASIC_GetString?_b6a3
$E25D LDX StringPtr?_0x22
$E25F LDY StringPtr?_0x22+1
$E261 JMP Call_SetFileName

$E264 LDA #$e0
$E266 LDY #$e2
$E268 JSR Funct_b867
Funct_e26b JSR Funct_bc0c
$E26E LDA #$e5
$E270 LDY #$e2
$E272 LDX $6E
$E274 JSR Funct_bb07
$E277 JSR Funct_bc0c
$E27A JSR Funct_bccc
$E27D LDA #$0
$E27F STA $6F
$E281 JSR Funct_b853
$E284 LDA #$ea
$E286 LDY #$e2
$E288 JSR Funct_b850
$E28B LDA $66
$E28D PHA
$E28E BPL Loc_e29d
$E290 JSR Funct_b849
$E293 LDA $66
$E295 BMI Loc_e2a0
$E297 LDA $12
$E299 EOR #$ff
$E29B STA $12
Loc_e29d JSR Funct_bfb4
Loc_e2a0 LDA #$ea
$E2A2 LDY #$e2
$E2A4 JSR Funct_b867
$E2A7 PLA
$E2A8 BPL Loc_e2ad
$E2AA JSR Funct_bfb4
Loc_e2ad LDA #$ef
$E2AF LDY #$e2
$E2B1 JMP Loc_e043

$E2B4 JSR Funct_bbca
$E2B7 LDA #$0
$E2B9 STA $12
$E2BB JSR Funct_e26b
$E2BE LDX #$4e
$E2C0 LDY #$0
$E2C2 JSR Funct_e0f6
$E2C5 LDA #$57
$E2C7 LDY #$0
$E2C9 JSR Funct_bba2
$E2CC LDA #$0
$E2CE STA $66
$E2D0 LDA $12
$E2D2 JSR Funct_e2dc
$E2D5 LDA #$4e
$E2D7 LDY #$0
$E2D9 JMP Loc_bb0f

Funct_e2dc PHA
$E2DD JMP Loc_e29d
$E2E0 .DB $81
$E2E1 .DB $49
$E2E2 .DB $f
$E2E3 .DB $da
$E2E4 .DB $a2
$E2E5 .DB $83
$E2E6 .DB $49
$E2E7 .DB $f
$E2E8 .DB $da
$E2E9 .DB $a2
$E2EA .DB $7f
$E2EB .DB $0
$E2EC .DB $0
$E2ED .DB $0
$E2EE .DB $0
$E2EF .DB $5
$E2F0 .DB $84
$E2F1 .DB $e6
$E2F2 .DB $1a
$E2F3 .DB $2d
$E2F4 .DB $1b
$E2F5 .DB $86
$E2F6 .DB $28
$E2F7 .DB $7
$E2F8 .DB $fb
$E2F9 .DB $f8
$E2FA .DB $87
$E2FB .DB $99
$E2FC .DB $68
$E2FD .DB $89
$E2FE .DB $1
$E2FF .DB $87
$E300 .DB $23
$E301 .DB $35
$E302 .DB $df
$E303 .DB $e1
$E304 .DB $86
$E305 .DB $a5
$E306 .DB $5d
$E307 .DB $e7
$E308 .DB $28
$E309 .DB $83
$E30A .DB $49
$E30B .DB $f
$E30C .DB $da
$E30D .DB $a2
$E30E .DB $a5
$E30F .DB $66
$E310 .DB $48
$E311 .DB $10
$E312 .DB $3
$E313 JSR Funct_bfb4
$E316 LDA $61
$E318 PHA
$E319 CMP #$81
$E31B BCC Loc_e324
$E31D LDA #$bc
$E31F LDY #$b9
$E321 JSR Loc_bb0f
Loc_e324 LDA #$3e
$E326 LDY #$e3
$E328 JSR Loc_e043
$E32B PLA
$E32C CMP #$81
$E32E BCC Loc_e337
$E330 LDA #$e0
$E332 LDY #$e2
$E334 JSR Funct_b850
Loc_e337 PLA
$E338 BPL Loc_e33d
$E33A JMP Funct_bfb4
Loc_e33d RTS
$E33E .DB $b
$E33F .DB $76
$E340 .DB $b3
$E341 .DB $83
$E342 .DB $bd
$E343 .DB $d3
$E344 .DB $79
$E345 .DB $1e
$E346 .DB $f4
$E347 .DB $a6
$E348 .DB $f5
$E349 .DB $7b
$E34A .DB $83
$E34B .DB $fc
$E34C .DB $b0
$E34D .DB $10
$E34E .DB $7c
$E34F .DB $c
$E350 .DB $1f
$E351 .DB $67
$E352 .DB $ca
$E353 .DB $7c
$E354 .DB $de
$E355 .DB $53
$E356 .DB $cb
$E357 .DB $c1
$E358 .DB $7d
$E359 .DB $14
$E35A .DB $64
$E35B .DB $70
$E35C .DB $4c
$E35D .DB $7d
$E35E .DB $b7
$E35F .DB $ea
$E360 .DB $51
$E361 .DB $7a
$E362 .DB $7d
$E363 .DB $63
$E364 .DB $30
$E365 .DB $88
$E366 .DB $7e
$E367 .DB $7e
$E368 .DB $92
$E369 .DB $44
$E36A .DB $99
$E36B .DB $3a
$E36C .DB $7e
$E36D .DB $4c
$E36E .DB $cc
$E36F .DB $91
$E370 .DB $c7
$E371 .DB $7f
$E372 .DB $aa
$E373 .DB $aa
$E374 .DB $aa
$E375 .DB $13
$E376 .DB $81
$E377 .DB $0
$E378 .DB $0
$E379 .DB $0
$E37A .DB $0
BASIC_WarmStart JSR CallRestoreIOVct
$E37E LDA #$0
$E380 STA $13
$E382 JSR Funct_a67a
$E385 CLI
Loc_e386 LDX #$80
$E388 JMP (BASIC_ErrorVector)

BASIC_GetCmdLine TXA
$E38C BMI Loc_e391
$E38E JMP Basic_DisplayError
Loc_e391 JMP Loc_a474

; *************************************************
; Function : BASIC_ColdStart
;
; Parameters
;
;
; Return Value :
;
; *************************************************

BASIC_ColdStart JSR
ResetBasicVectors
$E397 JSR Funct_e3bf
$E39A JSR Funct_e422
$E39D LDX #$fb
$E39F TXS
$E3A0 BNE Loc_e386

; *************************************************
; Function : BASIC_Funct73Code
;
; Parameters
;
;
; Return Value :
;
; *************************************************

BASIC_Funct73Code INC
BASIC_CMDPtrMinus1
$E3A4 BNE Loc_e3a8
$E3A6 INC BASIC_CMDPtrMinus1+1
Loc_e3a8 LDA $EA5E+2 ; This address gets replaced with command pointer -1
$E3AB CMP #$3a
$E3AD BCS Exit_e3b9
$E3AF CMP #$20
$E3B1 BEQ BASIC_Funct73Code
$E3B3 SEC
$E3B4 SBC #$30
$E3B6 SEC
$E3B7 SBC #$d0
Exit_e3b9 RTS

$E3BA .DB $80
$E3BB .DB $4f
$E3BC .DB $c7
$E3BD .DB $52
$E3BE .DB $58
Funct_e3bf LDA #$4c
$E3C1 STA Funct_54
$E3C3 STA $0310
$E3C6 LDA #$48
$E3C8 LDY #$b2
$E3CA STA $0311
$E3CD STY $0312
$E3D0 LDA #$91
$E3D2 LDY #$b3
$E3D4 STA $05
$E3D6 STY $06
$E3D8 LDA #$aa
$E3DA LDY #$b1
$E3DC STA $03
$E3DE STY $04
$E3E0 LDX #$1c
Loc_e3e2 LDA BASIC_Funct73Code,X
$E3E5 STA BASIC_GetNextCMDLineChar,X
$E3E7 DEX
$E3E8 BPL Loc_e3e2
$E3EA LDA #$3
$E3EC STA $53
$E3EE LDA #$0
$E3F0 STA $68
$E3F2 STA $13
$E3F4 STA $18
$E3F6 LDX #$1
$E3F8 STX $01FD
$E3FB STX $01FC
$E3FE LDX #$19
$E400 STX $16
$E402 SEC
$E403 JSR Funct_ff9c
$E406 STX BasicCodeStartAddress
$E408 STY BasicCodeStartAddress+1
$E40A SEC
$E40B JSR Funct_ff99
$E40E STX $37
$E410 STY $38
$E412 STX $33
$E414 STY $34
$E416 LDY #$0
$E418 TYA
$E419 STA (BasicCodeStartAddress),Y
$E41B INC BasicCodeStartAddress
$E41D BNE Loc_e421
$E41F INC BasicCodeStartAddress+1
Loc_e421 RTS

Funct_e422 LDA BasicCodeStartAddress
$E424 LDY BasicCodeStartAddress+1
$E426 JSR Funct_a408
$E429 LDA #<C64BasicStartupText
$E42B LDY #>C64BasicStartupText
$E42D JSR PrintStingYA_ab1e
$E430 LDA $37
$E432 SEC
$E433 SBC BasicCodeStartAddress
$E435 TAX
$E436 LDA $38
$E438 SBC BasicCodeStartAddress+1
$E43A JSR BASIC_PrintInt
$E43D LDA #<BytesFreeText
$E43F LDY #>BytesFreeText
$E441 JSR PrintStingYA_ab1e
$E444 JMP Loc_a644


Loc_E447 .DW BASIC_GetCmdLine
$E449 .DW $A483
$E44B .DW BASIC_TokeniseCmdLine
$E44D .DW BASIC_OutputKeyword
$E44F .DW BASIC_ExecuteCommand
$E451 .DW $AE86

ResetBasicVectors LDX #$b
Loc_e455 LDA Loc_E447,X
$E458 STA BASIC_ErrorVector,X
$E45B DEX
$E45C BPL Loc_e455
$E45E RTS

$E45F .DB $0
BytesFreeText .DB " BASIC BYTES FREE",$0d,$00
C64BasicStartupText .DB $93,$0d," **** COMMODORE 64 BASIC V"
.DB "2 ****",$0d
$E499 .DB $0d
$E49A .DB " 64K RAM SYSTEM ",$00

$E4AC .DB $81
Funct_e4ad PHA
$E4AE JSR Funct_ffc9
$E4B1 TAX
$E4B2 PLA
$E4B3 BCC Exit_e4b6
$E4B5 TXA
Exit_e4b6 RTS

$E4B7 .DB $aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa
.DB $aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa,$aa
.DB $aa,$aa


Loc_e4d3 STA
$A9
$E4D5 LDA #$1
$E4D7 STA $AB
$E4D9 RTS

Funct_e4da LDA $0286
$E4DD STA ($F3),Y
$E4DF RTS

Funct_e4e0 ADC #$2
Loc_e4e2 LDY $91
$E4E4 INY
$E4E5 BNE Exit_e4eb
$E4E7 CMP $A1
$E4E9 BNE Loc_e4e2
Exit_e4eb RTS

$E4EC .DB $19
$E4ED .DB $26
$E4EE .DB $44
$E4EF .DB $19
$E4F0 .DB $1a
$E4F1 .DB $11
$E4F2 .DB $e8
$E4F3 .DB $d
$E4F4 .DB $70
$E4F5 .DB $c
$E4F6 .DB $6
$E4F7 .DB $6
$E4F8 .DB $d1
$E4F9 .DB $2
$E4FA .DB $37
$E4FB .DB $1
$E4FC .DB $ae
$E4FD .DB $0
$E4FE .DB $69
$E4FF .DB $0

Loc_e500 LDX #$0
$E502 LDY #$dc
$E504 RTS

Loc_e505 LDX #$28
$E507 LDY #$19
$E509 RTS

Loc_e50a BCS Loc_e513
$E50C STX $D6
$E50E STY $D3
$E510 JSR Loc_e56c
Loc_e513 LDX $D6
$E515 LDY $D3
$E517 RTS

Funct_e518 JSR SetupVICChip
$E51B LDA #$0
$E51D STA $0291
$E520 STA $CF
$E522 LDA #$48
$E524 STA $028F
$E527 LDA #$eb
$E529 STA $0290
$E52C LDA #$a
$E52E STA $0289
$E531 STA $028C
$E534 LDA #$e
$E536 STA $0286
$E539 LDA #$4
$E53B STA $028B
$E53E LDA #$c
$E540 STA $CD
$E542 STA $CC
Funct_e544 LDA $0288
$E547 ORA #$80
$E549 TAY
$E54A LDA #$0
$E54C TAX
Loc_e54d STY $D9,X
$E54F CLC
$E550 ADC #$28
$E552 BCC Loc_e555
$E554 INY
Loc_e555 INX
$E556 CPX #$1a
$E558 BNE Loc_e54d
$E55A LDA #$ff
$E55C STA $D9,X
$E55E LDX #$18
Loc_e560 JSR Funct_e9ff
$E563 DEX
$E564 BPL Loc_e560
Funct_e566 LDY #$0
$E568 STY $D3
$E56A STY $D6
Loc_e56c LDX $D6
$E56E LDA $D3
Loc_e570 LDY $D9,X
$E572 BMI Loc_e57c
$E574 CLC
$E575 ADC #$28
$E577 STA $D3
$E579 DEX
$E57A BPL Loc_e570
Loc_e57c JSR Loc_e9f0
$E57F LDA #$27
$E581 INX
Loc_e582 LDY $D9,X
$E584 BMI Loc_e58c
$E586 CLC
$E587 ADC #$28
$E589 INX
$E58A BPL Loc_e582
Loc_e58c STA $D5
$E58E JMP Loc_ea24

Funct_e591 CPX $C9
$E593 BEQ Exit_e598
$E595 JMP Loc_e6ed
Exit_e598 RTS

$E599 NOP
$E59A JSR SetupVICChip
$E59D JMP Funct_e566


; *************************************************
; Function : SetupVICChip
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SetupVICChip LDA #$3
$E5A2 STA $9A
$E5A4 LDA #$0
$E5A6 STA $99
$E5A8 LDX #$2f
Loc_e5aa LDA $EC78+$40,X
$E5AD STA $CFFF,X
$E5B0 DEX
$E5B1 BNE Loc_e5aa
$E5B3 RTS

Loc_e5b4 LDY $0277
$E5B7 LDX #$0
Loc_e5b9 LDA $0278,X
$E5BC STA $0277,X
$E5BF INX
$E5C0 CPX NDX
$E5C2 BNE Loc_e5b9
$E5C4 DEC NDX
$E5C6 TYA
$E5C7 CLI
$E5C8 CLC
$E5C9 RTS

Loc_e5ca JSR Loc_e716
Loc_e5cd LDA NDX
$E5CF STA $CC
$E5D1 STA $0292
$E5D4 BEQ Loc_e5cd
$E5D6 SEI
$E5D7 LDA $CF
$E5D9 BEQ Loc_e5e7
$E5DB LDA $CE
$E5DD LDX $0287
$E5E0 LDY #$0
$E5E2 STY $CF
$E5E4 JSR Funct_ea13
Loc_e5e7 JSR Loc_e5b4
$E5EA CMP #$83
$E5EC BNE Loc_e5fe

; Setup Load/RUN in keyboard buffer
; This code is called when shift/run-stop pressed

$E5EE LDX #$9
$E5F0 SEI
$E5F1 STX NDX
Loc_e5f3 LDA VICChipSettings+$2D,X ; Points to Load RUN command -1
$E5F6 STA $0276,X
$E5F9 DEX
$E5FA BNE Loc_e5f3
$E5FC BEQ Loc_e5cd

Loc_e5fe CMP #$d
$E600 BNE Loc_e5ca
$E602 LDY $D5
$E604 STY $D0
Loc_e606 LDA ($D1),Y
$E608 CMP #$20
$E60A BNE Loc_e60f
$E60C DEY
$E60D BNE Loc_e606
Loc_e60f INY
$E610 STY $C8
$E612 LDY #$0
$E614 STY $0292
$E617 STY $D3
$E619 STY $D4
$E61B LDA $C9
$E61D BMI Loc_e63a
$E61F LDX $D6
$E621 JSR Funct_e591
$E624 CPX $C9
$E626 BNE Loc_e63a
$E628 LDA $CA
$E62A STA $D3
$E62C CMP $C8
$E62E BCC Loc_e63a
$E630 BCS Loc_e65d
Loc_e632 TYA
$E633 PHA
$E634 TXA
$E635 PHA
$E636 LDA $D0
$E638 BEQ Loc_e5cd
Loc_e63a LDY $D3
$E63C LDA ($D1),Y
$E63E STA FileSaveChkSum
$E640 AND #$3f
$E642 ASL FileSaveChkSum
$E644 BIT FileSaveChkSum
$E646 BPL Loc_e64a
$E648 ORA #$80
Loc_e64a BCC Loc_e650
$E64C LDX $D4
$E64E BNE Loc_e654
Loc_e650 BVS Loc_e654
$E652 ORA #$40
Loc_e654 INC $D3
$E656 JSR Funct_e684
$E659 CPY $C8
$E65B BNE Loc_e674
Loc_e65d LDA #$0
$E65F STA $D0
$E661 LDA #$d
$E663 LDX $99
$E665 CPX #$3
$E667 BEQ Loc_e66f
$E669 LDX $9A
$E66B CPX #$3
$E66D BEQ Loc_e672
Loc_e66f JSR Loc_e716
Loc_e672 LDA #$d
Loc_e674 STA FileSaveChkSum
$E676 PLA
$E677 TAX
$E678 PLA
$E679 TAY
$E67A LDA FileSaveChkSum
$E67C CMP #$de
$E67E BNE Loc_e682
$E680 LDA #$ff
Loc_e682 CLC
$E683 RTS

Funct_e684 CMP #$22
$E686 BNE Loc_e690
$E688 LDA $D4
$E68A EOR #$1
$E68C STA $D4
$E68E LDA #$22
Loc_e690 RTS

Loc_e691 ORA #$40
Loc_e693 LDX $C7
$E695 BEQ Loc_e699
Loc_e697 ORA #$80
Loc_e699 LDX $D8
$E69B BEQ Loc_e69f
$E69D DEC $D8
Loc_e69f LDX $0286
$E6A2 JSR Funct_ea13
$E6A5 JSR Funct_e6b6
Loc_e6a8 PLA
$E6A9 TAY
$E6AA LDA $D8
$E6AC BEQ Loc_e6b0
$E6AE LSR $D4
Loc_e6b0 PLA
$E6B1 TAX
$E6B2 PLA
$E6B3 CLC
$E6B4 CLI
$E6B5 RTS

Funct_e6b6 JSR Funct_e8b3
$E6B9 INC $D3
$E6BB LDA $D5
$E6BD CMP $D3
$E6BF BCS Loc_e700
$E6C1 CMP #$4f
$E6C3 BEQ Loc_e6f7
$E6C5 LDA $0292
$E6C8 BEQ Loc_e6cd
$E6CA JMP Loc_e967

Loc_e6cd LDX $D6
$E6CF CPX #$19
$E6D1 BCC Loc_e6da
$E6D3 JSR Funct_e8ea
$E6D6 DEC $D6
$E6D8 LDX $D6
Loc_e6da ASL $D9,X
$E6DC LSR $D9,X
$E6DE INX
$E6DF LDA $D9,X
$E6E1 ORA #$80
$E6E3 STA $D9,X
$E6E5 DEX
$E6E6 LDA $D5
$E6E8 CLC
$E6E9 ADC #$28
$E6EB STA $D5
Loc_e6ed LDA $D9,X
$E6EF BMI Loc_e6f4
$E6F1 DEX
$E6F2 BNE Loc_e6ed
Loc_e6f4 JMP Loc_e9f0

Loc_e6f7 DEC $D6
$E6F9 JSR Funct_e87c
$E6FC LDA #$0
$E6FE STA $D3
Loc_e700 RTS

Funct_e701 LDX $D6
$E703 BNE Loc_e70b
$E705 STX $D3
$E707 PLA
$E708 PLA
$E709 BNE Loc_e6a8
Loc_e70b DEX
$E70C STX $D6
$E70E JSR Loc_e56c
$E711 LDY $D5
$E713 STY $D3
$E715 RTS

Loc_e716 PHA
$E717 STA FileSaveChkSum
$E719 TXA
$E71A PHA
$E71B TYA
$E71C PHA
$E71D LDA #$0
$E71F STA $D0
$E721 LDY $D3
$E723 LDA FileSaveChkSum
$E725 BPL Loc_e72a
$E727 JMP Loc_e7d4

Loc_e72a CMP #$d
$E72C BNE Loc_e731
$E72E JMP Loc_e891
Loc_e731 CMP #$20
$E733 BCC Loc_e745
$E735 CMP #$60
$E737 BCC Loc_e73d
$E739 AND #$df
$E73B BNE Loc_e73f
Loc_e73d AND #$3f
Loc_e73f JSR Funct_e684
$E742 JMP Loc_e693

Loc_e745 LDX $D8
$E747 BEQ Loc_e74c
$E749 JMP Loc_e697

Loc_e74c CMP #$14
$E74E BNE Loc_e77e
$E750 TYA
$E751 BNE Loc_e759
$E753 JSR Funct_e701
$E756 JMP Loc_e773

Loc_e759 JSR Funct_e8a1
$E75C DEY
$E75D STY $D3
$E75F JSR Loc_ea24
Loc_e762 INY
$E763 LDA ($D1),Y
$E765 DEY
$E766 STA ($D1),Y
$E768 INY
$E769 LDA ($F3),Y
$E76B DEY
$E76C STA ($F3),Y
$E76E INY
$E76F CPY $D5
$E771 BNE Loc_e762
Loc_e773 LDA #$20
$E775 STA ($D1),Y
$E777 LDA $0286
$E77A STA ($F3),Y
$E77C BPL Loc_e7cb
Loc_e77e LDX $D4
$E780 BEQ Loc_e785
$E782 JMP Loc_e697

Loc_e785 CMP #$12
$E787 BNE Loc_e78b
$E789 STA $C7
Loc_e78b CMP #$13
$E78D BNE Loc_e792
$E78F JSR Funct_e566
Loc_e792 CMP #$1d
$E794 BNE Loc_e7ad
$E796 INY
$E797 JSR Funct_e8b3
$E79A STY $D3
$E79C DEY
$E79D CPY $D5
$E79F BCC Loc_e7aa
$E7A1 DEC $D6
$E7A3 JSR Funct_e87c
$E7A6 LDY #$0
Loc_e7a8 STY $D3
Loc_e7aa JMP Loc_e6a8

Loc_e7ad CMP #$11
$E7AF BNE Loc_e7ce
$E7B1 CLC
$E7B2 TYA
$E7B3 ADC #$28
$E7B5 TAY
$E7B6 INC $D6
$E7B8 CMP $D5
$E7BA BCC Loc_e7a8
$E7BC BEQ Loc_e7a8
$E7BE DEC $D6
Loc_e7c0 SBC #$28
$E7C2 BCC Loc_e7c8
$E7C4 STA $D3
$E7C6 BNE Loc_e7c0
Loc_e7c8 JSR Funct_e87c
Loc_e7cb JMP Loc_e6a8
Loc_e7ce JSR Funct_e8cb
$E7D1 JMP Loc_ec44

Loc_e7d4 AND #$7f
$E7D6 CMP #$7f
$E7D8 BNE Loc_e7dc
$E7DA LDA #$5e
Loc_e7dc CMP #$20
$E7DE BCC Loc_e7e3
$E7E0 JMP Loc_e691

Loc_e7e3 CMP #$d
$E7E5 BNE Loc_e7ea
$E7E7 JMP Loc_e891

Loc_e7ea LDX $D4
$E7EC BNE Loc_e82d
$E7EE CMP #$14
$E7F0 BNE Loc_e829
$E7F2 LDY $D5
$E7F4 LDA ($D1),Y
$E7F6 CMP #$20
$E7F8 BNE Loc_e7fe
$E7FA CPY $D3
$E7FC BNE Loc_e805
Loc_e7fe CPY #$4f
$E800 BEQ Loc_e826
$E802 JSR Funct_e965
Loc_e805 LDY $D5
$E807 JSR Loc_ea24
Loc_e80a DEY
$E80B LDA ($D1),Y
$E80D INY
$E80E STA ($D1),Y
$E810 DEY
$E811 LDA ($F3),Y
$E813 INY
$E814 STA ($F3),Y
$E816 DEY
$E817 CPY $D3
$E819 BNE Loc_e80a
$E81B LDA #$20
$E81D STA ($D1),Y
$E81F LDA $0286
$E822 STA ($F3),Y
$E824 INC $D8
Loc_e826 JMP Loc_e6a8

Loc_e829 LDX $D8
$E82B BEQ Loc_e832
Loc_e82d ORA #$40
$E82F JMP Loc_e697

Loc_e832 CMP #$11
$E834 BNE Loc_e84c
$E836 LDX $D6
$E838 BEQ Loc_e871
$E83A DEC $D6
$E83C LDA $D3
$E83E SEC
$E83F SBC #$28
$E841 BCC Loc_e847
$E843 STA $D3
$E845 BPL Loc_e871
Loc_e847 JSR Loc_e56c
$E84A BNE Loc_e871
Loc_e84c CMP #$12
$E84E BNE Loc_e854
$E850 LDA #$0
$E852 STA $C7
Loc_e854 CMP #$1d
$E856 BNE Loc_e86a
$E858 TYA
$E859 BEQ Loc_e864
$E85B JSR Funct_e8a1
$E85E DEY
$E85F STY $D3
$E861 JMP Loc_e6a8

Loc_e864 JSR Funct_e701
$E867 JMP Loc_e6a8

Loc_e86a CMP #$13
$E86C BNE Loc_e874
$E86E JSR Funct_e544
Loc_e871 JMP Loc_e6a8

Loc_e874 ORA #$80
$E876 JSR Funct_e8cb
$E879 JMP Loc_ec4f
Funct_e87c LSR $C9
$E87E LDX $D6
Loc_e880 INX
$E881 CPX #$19
$E883 BNE Loc_e888
$E885 JSR Funct_e8ea
Loc_e888 LDA $D9,X
$E88A BPL Loc_e880
$E88C STX $D6
$E88E JMP Loc_e56c

Loc_e891 LDX #$0
$E893 STX $D8
$E895 STX $C7
$E897 STX $D4
$E899 STX $D3
$E89B JSR Funct_e87c
$E89E JMP Loc_e6a8

Funct_e8a1 LDX #$2
$E8A3 LDA #$0
Loc_e8a5 CMP $D3
$E8A7 BEQ Loc_e8b0
$E8A9 CLC
$E8AA ADC #$28
$E8AC DEX
$E8AD BNE Loc_e8a5
$E8AF RTS

Loc_e8b0 DEC $D6
$E8B2 RTS

Funct_e8b3 LDX #$2
$E8B5 LDA #$27
Loc_e8b7 CMP $D3
$E8B9 BEQ Loc_e8c2
$E8BB CLC
$E8BC ADC #$28
$E8BE DEX
$E8BF BNE Loc_e8b7
$E8C1 RTS

Loc_e8c2 LDX $D6
$E8C4 CPX #$19
$E8C6 BEQ Loc_e8ca
$E8C8 INC $D6
Loc_e8ca RTS

Funct_e8cb LDX #$f
Loc_e8cd CMP $E8DA,X
$E8D0 BEQ Exit_e8d6
$E8D2 DEX
$E8D3 BPL Loc_e8cd
$E8D5 RTS

Exit_e8d6 STX $0286
$E8D9 RTS

$E8DA .DB $90
$E8DB .DB $5
$E8DC .DB $1c
$E8DD .DB $9f
$E8DE .DB $9c
$E8DF .DB $1e
$E8E0 .DB $1f
$E8E1 .DB $9e
$E8E2 .DB $81
$E8E3 .DB $95
$E8E4 .DB $96
$E8E5 .DB $97
$E8E6 .DB $98
$E8E7 .DB $99
$E8E8 .DB $9a
$E8E9 .DB $9b

Funct_e8ea LDA $AC
$E8EC PHA
$E8ED LDA $AC+1
$E8EF PHA
$E8F0 LDA $AE
$E8F2 PHA
$E8F3 LDA $AE+1
$E8F5 PHA
Loc_e8f6 LDX #$ff
$E8F8 DEC $D6
$E8FA DEC $C9
$E8FC DEC $02A5
Loc_e8ff INX
$E900 JSR Loc_e9f0
$E903 CPX #$18
$E905 BCS Loc_e913
$E907 LDA $ECF1,X
$E90A STA $AC
$E90C LDA $DA,X
$E90E JSR Funct_e9c8
$E911 BMI Loc_e8ff
Loc_e913 JSR Funct_e9ff
$E916 LDX #$0
Loc_e918 LDA $D9,X
$E91A AND #$7f
$E91C LDY $DA,X
$E91E BPL Loc_e922
$E920 ORA #$80
Loc_e922 STA $D9,X
$E924 INX
$E925 CPX #$18
$E927 BNE Loc_e918
$E929 LDA $F1
$E92B ORA #$80
$E92D STA $F1
$E92F LDA $D9
$E931 BPL Loc_e8f6
$E933 INC $D6
$E935 INC $02A5
$E938 LDA #$7f
$E93A STA $DC00
$E93D LDA $DC01
$E940 CMP #$fb
$E942 PHP
$E943 LDA #$7f
$E945 STA $DC00
$E948 PLP
$E949 BNE Loc_e956
$E94B LDY #$0
Loc_e94d NOP
$E94E DEX
$E94F BNE Loc_e94d
$E951 DEY
$E952 BNE Loc_e94d
$E954 STY NDX
Loc_e956 LDX $D6
Loc_e958 PLA
$E959 STA $AE+1
$E95B PLA
$E95C STA $AE
$E95E PLA
$E95F STA $AC+1
$E961 PLA
$E962 STA $AC
$E964 RTS

Funct_e965 LDX $D6
Loc_e967 INX
$E968 LDA $D9,X
$E96A BPL Loc_e967
$E96C STX $02A5
$E96F CPX #$18
$E971 BEQ Loc_e981
$E973 BCC Loc_e981
$E975 JSR Funct_e8ea
$E978 LDX $02A5
$E97B DEX
$E97C DEC $D6
$E97E JMP Loc_e6da

Loc_e981 LDA $AC
$E983 PHA
$E984 LDA $AC+1
$E986 PHA
$E987 LDA $AE
$E989 PHA
$E98A LDA $AE+1
$E98C PHA
$E98D LDX #$19
Loc_e98f DEX
$E990 JSR Loc_e9f0
$E993 CPX $02A5
$E996 BCC Loc_e9a6
$E998 BEQ Loc_e9a6
$E99A LDA $ECE7+8,X
$E99D STA $AC
$E99F LDA $D8,X
$E9A1 JSR Funct_e9c8
$E9A4 BMI Loc_e98f
Loc_e9a6 JSR Funct_e9ff
$E9A9 LDX #$17
Loc_e9ab CPX $02A5
$E9AE BCC Loc_e9bf
$E9B0 LDA $DA,X
$E9B2 AND #$7f
$E9B4 LDY $D9,X
$E9B6 BPL Loc_e9ba
$E9B8 ORA #$80
Loc_e9ba STA $DA,X
$E9BC DEX
$E9BD BNE Loc_e9ab
Loc_e9bf LDX $02A5
$E9C2 JSR Loc_e6da
$E9C5 JMP Loc_e958

Funct_e9c8 AND #$3
$E9CA ORA $0288
$E9CD STA $AC+1
$E9CF JSR Funct_e9e0
$E9D2 LDY #$27
Loc_e9d4 LDA ($AC),Y
$E9D6 STA ($D1),Y
$E9D8 LDA ($AE),Y
$E9DA STA ($F3),Y
$E9DC DEY
$E9DD BPL Loc_e9d4
$E9DF RTS

Funct_e9e0 JSR Loc_ea24
$E9E3 LDA $AC
$E9E5 STA $AE
$E9E7 LDA $AC+1
$E9E9 AND #$3
$E9EB ORA #$d8
$E9ED STA $AE+1
$E9EF RTS

Loc_e9f0 LDA $ECE7+9,X
$E9F3 STA $D1
$E9F5 LDA $D9,X
$E9F7 AND #$3
$E9F9 ORA $0288
$E9FC STA $D2
$E9FE RTS

Funct_e9ff LDY #$27
$EA01 JSR Loc_e9f0
$EA04 JSR Loc_ea24
Loc_ea07 JSR Funct_e4da
$EA0A LDA #$20
$EA0C STA ($D1),Y
$EA0E DEY
$EA0F BPL Loc_ea07
$EA11 RTS

$EA12 .DB $ea ; This is padding as it isn't called from anywhere

Funct_ea13 TAY
$EA14 LDA #$2
$EA16 STA $CD
$EA18 JSR Loc_ea24
$EA1B TYA
Funct_ea1c LDY $D3
$EA1E STA ($D1),Y
$EA20 TXA
$EA21 STA ($F3),Y
$EA23 RTS

Loc_ea24 LDA $D1
$EA26 STA $F3
$EA28 LDA $D2
$EA2A AND #$3
$EA2C ORA #$d8
$EA2E STA $F4
$EA30 RTS

NormalIRQ JSR Funct_ffea
$EA34 LDA $CC
$EA36 BNE Loc_ea61
$EA38 DEC $CD
$EA3A BNE Loc_ea61
$EA3C LDA #$14
$EA3E STA $CD
$EA40 LDY $D3
$EA42 LSR $CF
$EA44 LDX $0287
$EA47 LDA ($D1),Y
$EA49 BCS Loc_ea5c
$EA4B INC $CF
$EA4D STA $CE
$EA4F JSR Loc_ea24
$EA52 LDA ($F3),Y
$EA54 STA $0287
$EA57 LDX $0286
$EA5A LDA $CE
Loc_ea5c EOR #$80
$EA5E JSR Funct_ea1c
Loc_ea61 LDA $01
$EA63 AND #$10
$EA65 BEQ Loc_ea71
$EA67 LDY #$0
$EA69 STY Adr1Backup
$EA6B LDA $01
$EA6D ORA #$20
$EA6F BNE Loc_ea79
Loc_ea71 LDA Adr1Backup
$EA73 BNE Loc_ea7b
$EA75 LDA $01
$EA77 AND #$1f
Loc_ea79 STA $01
Loc_ea7b JSR ScanKey
$EA7E LDA CIA1_InterruptFlags
$EA81 PLA
$EA82 TAY
$EA83 PLA
$EA84 TAX
$EA85 PLA
$EA86 RTI

ScanKey LDA #$0
$EA89 STA $028D
$EA8C LDY #$40
$EA8E STY $CB
$EA90 STA $DC00

; Check if keyboard status is available

$EA93 LDX
$DC01
$EA96 CPX #$ff
$EA98 BEQ Loc_eafb
$EA9A TAY

; Set up pointer to StdKeyCodes

$EA9B LDA #$81
$EA9D STA KeyCodePtr
$EA9F LDA #$eb
$EAA1 STA KeyCodePtr+1
$EAA3 LDA #$fe
$EAA5 STA $DC00
Loop_eaa8 LDX #$8
$EAAA PHA

; Wait for stable value

Loop_eaab LDA
$DC01
$EAAE CMP $DC01
$EAB1 BNE Loop_eaab

Loc_eab3 LSR
$EAB4 BCS Loc_eacc

; A keypress has been detected

$EAB6 PHA
$EAB7 LDA (KeyCodePtr),Y
$EAB9 CMP #$5 ; Codes under 5 are special keys such as Shift, Commodore or CTRL
$EABB BCS Loc_eac9
$EABD CMP #$3
$EABF BEQ Loc_eac9
$EAC1 ORA $028D
$EAC4 STA $028D
$EAC7 BPL Loc_eacb
Loc_eac9 STY $CB
Loc_eacb PLA
Loc_eacc INY
$EACD CPY #$41
$EACF BCS Loc_eadc
$EAD1 DEX
$EAD2 BNE Loc_eab3
$EAD4 SEC
$EAD5 PLA
$EAD6 ROL
$EAD7 STA $DC00
$EADA BNE Loop_eaa8
Loc_eadc PLA
$EADD JMP ($028F)

Loc_eae0 LDY $CB
$EAE2 LDA (KeyCodePtr),Y
$EAE4 TAX
$EAE5 CPY LSTX
$EAE7 BEQ Loc_eaf0
$EAE9 LDY #$10
$EAEB STY $028C
$EAEE BNE Loc_eb26
Loc_eaf0 AND #$7f
$EAF2 BIT $028A
$EAF5 BMI Loc_eb0d
$EAF7 BVS Loc_eb42
$EAF9 CMP #$7f
Loc_eafb BEQ Loc_eb26
$EAFD CMP #$14
$EAFF BEQ Loc_eb0d
$EB01 CMP #$20
$EB03 BEQ Loc_eb0d
$EB05 CMP #$1d
$EB07 BEQ Loc_eb0d
$EB09 CMP #$11
$EB0B BNE Loc_eb42
Loc_eb0d LDY $028C
$EB10 BEQ Loc_eb17
$EB12 DEC $028C
$EB15 BNE Loc_eb42
Loc_eb17 DEC $028B
$EB1A BNE Loc_eb42
$EB1C LDY #$4
$EB1E STY $028B
$EB21 LDY NDX
$EB23 DEY
$EB24 BPL Loc_eb42
Loc_eb26 LDY $CB
$EB28 STY LSTX
$EB2A LDY $028D
$EB2D STY $028E
$EB30 CPX #$ff
$EB32 BEQ Loc_eb42
$EB34 TXA
$EB35 LDX NDX
$EB37 CPX $0289
$EB3A BCS Loc_eb42
$EB3C STA $0277,X
$EB3F INX
$EB40 STX NDX
Loc_eb42 LDA #$7f
$EB44 STA $DC00
$EB47 RTS

$EB48 LDA $028D
$EB4B CMP #$3
$EB4D BNE Loc_eb64
$EB4F CMP $028E
$EB52 BEQ Loc_eb42
$EB54 LDA $0291
$EB57 BMI Loc_eb76
$EB59 LDA $D018
$EB5C EOR #$2
$EB5E STA $D018
$EB61 JMP Loc_eb76

Loc_eb64 ASL
$EB65 CMP #$8
$EB67 BCC Loc_eb6b
$EB69 LDA #$6
Loc_eb6b TAX
$EB6C LDA KeyCodePtrs,X
$EB6F STA KeyCodePtr
$EB71 LDA KeyCodePtrs+1,X
$EB74 STA KeyCodePtr+1
Loc_eb76 JMP Loc_eae0

KeyCodePtrs .DW StdKeyCodes,$EBC2,$EC03,$EC78

StdKeyCodes .DB $14,$0d,$1d,$88,$85,$86,$87,$11,"3WA4ZSE",$01,"5RD6CFTX7YG8BHU"
.DB "V9IJ0MKON+PL-.:@,\*;",$13,$01,"=^/1_",$04,"2 ",$02
.DB "Q",$03,$ff

$EBC2 .DB $94,$8d,$9d,$8c,$89,$8a,$8b,$91,"#",$d7,$c1,"$",$da,$d3,$c5,$01,"%",$d2,$c4,"&",$c3,$c6,$d4,$d8,"'",$d9,$c7,"(",$c2,$c8,$d5
.DB $d6,")",$c9,$ca,"0",$cd,$cb,$cf,$ce,$db,$d0,$cc,$dd,">[",$ba,"<",$a9,$c0,"]",$93,$01,"=",$de,"?!_",$04,""",$a0,$02
.DB $d1,$83,$ff

$EC03 .DB $94,$8d,$9d,$8c,$89,$8a,$8b,$91,$96,$b3,$b0,$97,$ad,$ae,$b1,$01,$98,$b2,$ac,$99,$bc,$bb,$a3,$bd,$9a,$b7,$a5,$9b,$bf,$b4,$b8
.DB $be,")",$a2,$b5,"0",$a7,$a1,$b9,$aa,$a6,$af,$b6,$dc,">[",$a4,"<",$a8,$df,"]",$93,$01,"=",$de,"?",$81,"_",$04,$95,$a0,$02
.DB $ab,$83,$ff

Loc_ec44 CMP #$e
$EC46 BNE Loc_ec4f
$EC48 LDA $D018
$EC4B ORA #$2
$EC4D BNE Loc_ec58
Loc_ec4f CMP #$8e
$EC51 BNE Loc_ec5e
$EC53 LDA $D018
$EC56 AND #$fd
Loc_ec58 STA $D018
Loc_ec5b JMP Loc_e6a8

Loc_ec5e CMP #$8
$EC60 BNE Loc_ec69
$EC62 LDA #$80
$EC64 ORA $0291
$EC67 BMI Loc_ec72
Loc_ec69 CMP #$9
$EC6B BNE Loc_ec5b
$EC6D LDA #$7f
$EC6F AND $0291
Loc_ec72 STA $0291
$EC75 JMP Loc_e6a8

$EC78 .DB $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff,$1c,$17,$01,$9f,$1a,$13,$05,$ff,$9c,$12,$04,$1e,$03,$06,$14,$18,$1f,$19,$07,$9e,$02,$08,$15
.DB $16,$12,$09,$0a,$92,$0d,$0b,$0f,$0e,$ff,$10,$0c,$ff,$ff,$1b,$00,$ff,$1c,$ff,$1d,$ff,$ff,$1f,$1e,$ff,$90,$06,$ff,$05,$ff,$ff
.DB $11,$ff,$ff

VICChipSettings .DB $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0
.DB $0,$0,$0,$0,$9b,$37,$0,$0,$0,$8,$0,$14,$f
.DB $0,$0,$0,$0,$0,$0,$e,$6,$1,$2,$3,$4,$0
.DB $1,$2,$3,$4,$5,$6,$7
$ECE7 .DB "LOAD",$0d,"RUN",$0d,$00
$ECF1 .DB $28
$ECF2 .DB $50
$ECF3 .DB $78
$ECF4 .DB $a0
$ECF5 .DB $c8
$ECF6 .DB $f0
$ECF7 .DB $18
$ECF8 .DB $40
$ECF9 .DB $68
$ECFA .DB $90
$ECFB .DB $b8
$ECFC .DB $e0
$ECFD .DB $8
$ECFE .DB $30
$ECFF .DB $58
$ED00 .DB $80
$ED01 .DB $a8
$ED02 .DB $d0
$ED03 .DB $f8
$ED04 .DB $20
$ED05 .DB $48
$ED06 .DB $70
$ED07 .DB $98
$ED08 .DB $c0

; *************************************************
; Function : SendTalkCmd
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SendTalkCmd ORA #$40
$ED0B .DB $2c ; Optimisation : Opcode for "BIT". Has no effect

; *************************************************
; Function : SendListenCmd
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SendListenCmd ORA #$20
$ED0E JSR Funct_f0a4
SerialSendByte? PHA
$ED12 BIT $94
$ED14 BPL Loc_ed20


; This looks like a retry mechanism

$ED16 SEC
$ED17 ROR TapeSaveBitCount
$ED19 JSR SendSerialDataByte
$ED1C LSR $94
$ED1E LSR TapeSaveBitCount

Loc_ed20 PLA
$ED21 STA $95
$ED23 SEI
$ED24 JSR SerialDatOutLo
$ED27 CMP #$3f
$ED29 BNE Loc_ed2e
$ED2B JSR SerialClkOutLo
Loc_ed2e LDA $DD00 ; Set ATN Out High
$ED31 ORA #$8
$ED33 STA $DD00
Funct_ed36 SEI
$ED37 JSR SerialClkOutHi
$ED3A JSR SerialDatOutLo
$ED3D JSR Delay0xB8Loop
SendSerialDataByte SEI
$ED41 JSR SerialDatOutLo
$ED44 JSR WaitForSerialInChange
$ED47 BCS SetStatusto80__edad

; Wait for data in to go Low
$ED49 JSR SerialClkOutLo
$ED4C BIT TapeSaveBitCount
$ED4E BPL Loc_ed5a
Loc_ed50 JSR WaitForSerialInChange
$ED53 BCC Loc_ed50

; Wait for data in to go High
Loc_ed55 JSR WaitForSerialInChange
$ED58 BCS Loc_ed55

; Wait for data in to go Low
Loc_ed5a JSR WaitForSerialInChange
$ED5D BCC Loc_ed5a

; Wait for data in to go High
$ED5F JSR SerialClkOutHi
$ED62 LDA #$8 ; Number of bits to transmit
$ED64 STA TapeSaveSyncDataCount


; Send next data byte

Loc_ed66 LDA
$DD00
$ED69 CMP $DD00
$ED6C BNE Loc_ed66
$ED6E ASL
$ED6F BCC SetStatusto3__edb0 ; If data low then error


; Serial Data In is High

$ED71 ROR
$95
$ED73 BCS Loc_ed7a
$ED75 JSR SerialDatOutHi ; Low Bit
$ED78 BNE Loc_ed7d
Loc_ed7a JSR SerialDatOutLo ; High Bit
Loc_ed7d JSR SerialClkOutLo ; Clock out Low
$ED80 NOP
$ED81 NOP
$ED82 NOP
$ED83 NOP
$ED84 LDA $DD00 ; Data out cleared and Clock out set high
$ED87 AND #$df
$ED89 ORA #$10
$ED8B STA $DD00

$ED8E DEC TapeSaveSyncDataCount
$ED90 BNE Loc_ed66
$ED92 LDA #$4
$ED94 STA CIA1_TimerB+1
$ED97 LDA #$19
$ED99 STA CIA1_TimerB_Ctrl
$ED9C LDA CIA1_InterruptFlags
Loc_ed9f LDA CIA1_InterruptFlags
$EDA2 AND #$2
$EDA4 BNE SetStatusto3__edb0
$EDA6 JSR WaitForSerialInChange
$EDA9 BCS Loc_ed9f ; Wait for Data to go high
$EDAB CLI
$EDAC RTS

SetStatusto80__edad LDA #$80
$EDAF .DB $2c ; Optimisation : Opcode for "BIT". Has no effect
SetStatusto3__edb0 LDA #$3
SetStatusExit??Loc_edb2 JSR SetStatus
$EDB5 CLI
$EDB6 CLC
$EDB7 BCC Loc_ee03


; *************************************************
; Function : SerialSendSecondaryAdrAfterListen
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SerialSendSecondaryAdrAfterListen
STA
$95
$EDBB JSR Funct_ed36


; *************************************************
; Function : ClearSerialATN
;
; Parameters
;
;
; Return Value :
;
; *************************************************

ClearSerialATN LDA
$DD00
$EDC1 AND #$f7
$EDC3 STA $DD00
$EDC6 RTS


SerialSendSecondaryAdrAfterTalk
STA
$95
$EDC9 JSR Funct_ed36
Funct_edcc SEI
$EDCD JSR SerialDatOutHi
$EDD0 JSR ClearSerialATN
$EDD3 JSR SerialClkOutLo
Loc_edd6 JSR WaitForSerialInChange
$EDD9 BMI Loc_edd6 ; Wait for clock in to go low
$EDDB CLI
$EDDC RTS

Funct_eddd BIT $94
$EDDF BMI Loc_ede6
$EDE1 SEC
$EDE2 ROR $94
$EDE4 BNE Loc_edeb
Loc_ede6 PHA
$EDE7 JSR SendSerialDataByte
$EDEA PLA
Loc_edeb STA $95
$EDED CLC
$EDEE RTS



; *************************************************
; Function : UnTalk
;
; Parameters
;
;
; Return Value :
;
; *************************************************

UnTalk SEI
$EDF0 JSR SerialClkOutHi
$EDF3 LDA $DD00
$EDF6 ORA #$8
$EDF8 STA $DD00
$EDFB LDA #$5f
$EDFD BIT $3FA9 ; Optimisation : Opcode for "BIT". Has no effect
$EE00 JSR SerialSendByte?
Loc_ee03 JSR ClearSerialATN
Funct_ee06 TXA
$EE07 LDX #$a
Loc_ee09 DEX
$EE0A BNE Loc_ee09
$EE0C TAX
$EE0D JSR SerialClkOutLo
$EE10 JMP SerialDatOutLo


; *************************************************
; Function : GetSerialByte
;
; Parameters
;
;
; Return Value :
;
; *************************************************

GetSerialByte SEI
$EE14 LDA #$0
$EE16 STA TapeSaveSyncDataCount
$EE18 JSR SerialClkOutLo
Loc_ee1b JSR WaitForSerialInChange
$EE1E BPL Loc_ee1b ; Wait for clock to go high
Loc_ee20 LDA #$1
$EE22 STA CIA1_TimerB+1
$EE25 LDA #$19
$EE27 STA CIA1_TimerB_Ctrl
$EE2A JSR SerialDatOutLo ; Acknowledge we can receive data
$EE2D LDA CIA1_InterruptFlags
Loc_ee30 LDA CIA1_InterruptFlags
$EE33 AND #$2
$EE35 BNE Loc_ee3e ; If TimerB expired, goto ee3e
$EE37 JSR WaitForSerialInChange
$EE3A BMI Loc_ee30
$EE3C BPL Loc_ee56 ; wait for clock to go low
Loc_ee3e LDA TapeSaveSyncDataCount
$EE40 BEQ Loc_ee47
$EE42 LDA #$2
$EE44 JMP SetStatusExit??Loc_edb2

Loc_ee47 JSR SerialDatOutHi
$EE4A JSR SerialClkOutLo
$EE4D LDA #$40
$EE4F JSR SetStatus
$EE52 INC TapeSaveSyncDataCount
$EE54 BNE Loc_ee20


; Receive the data byte

Loc_ee56 LDA #$8
$EE58 STA TapeSaveSyncDataCount

Loc_ee5a LDA $DD00
$EE5D CMP $DD00
$EE60 BNE Loc_ee5a
$EE62 ASL ; Shift received data bit into the Carry Flag
$EE63 BPL Loc_ee5a ; Wait for clock to go High
$EE65 ROR $A4

; Rotate Data bit into the Data byte register
Loc_ee67 LDA $DD00
$EE6A CMP $DD00
$EE6D BNE Loc_ee67
$EE6F ASL
$EE70 BMI Loc_ee67 ; Wait until Clock Low
$EE72 DEC TapeSaveSyncDataCount
$EE74 BNE Loc_ee5a


; Received a whole data byte

$EE76 JSR
SerialDatOutHi
$EE79 BIT $90
$EE7B BVC Exit_ee80
$EE7D JSR Funct_ee06
Exit_ee80 LDA $A4
$EE82 CLI
$EE83 CLC
$EE84 RTS


; *************************************************
; Function : SerialClkOutLo
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SerialClkOutLo LDA
$DD00
$EE88 AND #$ef
$EE8A STA $DD00
$EE8D RTS


; *************************************************
; Function : SerialClkOutHi
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SerialClkOutHi LDA
$DD00
$EE91 ORA #$10
$EE93 STA $DD00
$EE96 RTS


; *************************************************
; Function : SerialDatOutLo
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SerialDatOutLo LDA
$DD00
$EE9A AND #$df
$EE9C STA $DD00
$EE9F RTS


; *************************************************
; Function : SerialClkOutHi
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SerialDatOutHi LDA
$DD00
$EEA3 ORA #$20
$EEA5 STA $DD00
$EEA8 RTS


; *************************************************
; Function : WaitForSerialInChange
;
; Parameters
;
;
; Return Value :
; Carry contains data
; plus / minus = clock state
; *************************************************

WaitForSerialInChange LDA
$DD00
$EEAC CMP $DD00
$EEAF BNE WaitForSerialInChange
$EEB1 ASL
$EEB2 RTS

Delay0xB8Loop TXA
$EEB4 LDX #$b8
Loc_eeb6 DEX
$EEB7 BNE Loc_eeb6
$EEB9 TAX
$EEBA RTS

Funct_eebb LDA $B4
$EEBD BEQ Funct_ef06
$EEBF BMI Loc_ef00
$EEC1 LSR $B6
$EEC3 LDX #$0
$EEC5 BCC Loc_eec8
$EEC7 DEX
Loc_eec8 TXA
$EEC9 EOR DataByte
$EECB STA DataByte
$EECD DEC $B4
$EECF BEQ Loc_eed7
Loc_eed1 TXA
$EED2 AND #$4
$EED4 STA $B5
$EED6 RTS

Loc_eed7 LDA #$20
$EED9 BIT $0294
$EEDC BEQ Loc_eef2
$EEDE BMI Loc_eefc
$EEE0 BVS Loc_eef6
$EEE2 LDA DataByte
$EEE4 BNE Loc_eee7
Loc_eee6 DEX
Loc_eee7 DEC $B4
$EEE9 LDA $0293
$EEEC BPL Loc_eed1
$EEEE DEC $B4
$EEF0 BNE Loc_eed1
Loc_eef2 INC $B4
$EEF4 BNE Loc_eee6
Loc_eef6 LDA DataByte
$EEF8 BEQ Loc_eee7
$EEFA BNE Loc_eee6
Loc_eefc BVS Loc_eee7
$EEFE BVC Loc_eee6

Loc_ef00 INC $B4
$EF02 LDX #$ff
$EF04 BNE Loc_eed1
Funct_ef06 LDA $0294
$EF09 LSR
$EF0A BCC Loc_ef13
$EF0C BIT $DD01
$EF0F BPL Loc_ef2e
$EF11 BVC Loc_ef31
Loc_ef13 LDA #$0
$EF15 STA DataByte
$EF17 STA $B5
$EF19 LDX $0298
$EF1C STX $B4
$EF1E LDY $029D
$EF21 CPY $029E
$EF24 BEQ Loc_ef39
$EF26 LDA ($F9),Y
$EF28 STA $B6
$EF2A INC $029D
$EF2D RTS

Loc_ef2e LDA #$40
$EF30 BIT $10A9 ; Optimisation : Opcode for "BIT". Has no effect
$EF33 ORA $0297
$EF36 STA $0297
Loc_ef39 LDA #$1
Funct_ef3b STA CIA2_InterruptFlags
$EF3E EOR $02A1
$EF41 ORA #$80
$EF43 STA $02A1
$EF46 STA CIA2_InterruptFlags
$EF49 RTS

Loc_ef4a LDX #$9
$EF4C LDA #$20
$EF4E BIT $0293
$EF51 BEQ Loc_ef54
$EF53 DEX
Loc_ef54 BVC Exit_ef58
$EF56 DEX
$EF57 DEX
Exit_ef58 RTS

Loc_ef59 LDX $A9
$EF5B BNE Loc_ef90
$EF5D DEC TapeSaveDataByteStartFlag
$EF5F BEQ Loc_ef97
$EF61 BMI Loc_ef70
$EF63 LDA $A7
$EF65 EOR $AB
$EF67 STA $AB
$EF69 LSR $A7
$EF6B ROR $AA
Exit_ef6d RTS

Loc_ef6e DEC TapeSaveDataByteStartFlag
Loc_ef70 LDA $A7
$EF72 BEQ Loc_efdb
$EF74 LDA $0293
$EF77 ASL
$EF78 LDA #$1
$EF7A ADC TapeSaveDataByteStartFlag
$EF7C BNE Exit_ef6d
Loc_ef7e LDA #$90
$EF80 STA CIA2_InterruptFlags
$EF83 ORA $02A1
$EF86 STA $02A1
$EF89 STA $A9
$EF8B LDA #$2
$EF8D JMP Funct_ef3b

Loc_ef90 LDA $A7
$EF92 BNE Loc_ef7e
$EF94 JMP Loc_e4d3
Loc_ef97 LDY $029B
$EF9A INY
$EF9B CPY $029C
$EF9E BEQ Loc_efca
$EFA0 STY $029B
$EFA3 DEY
$EFA4 LDA $AA
$EFA6 LDX $0298
Loc_efa9 CPX #$9
$EFAB BEQ Loc_efb1
$EFAD LSR
$EFAE INX
$EFAF BNE Loc_efa9
Loc_efb1 STA ($F7),Y
$EFB3 LDA #$20
$EFB5 BIT $0294
$EFB8 BEQ Loc_ef6e
$EFBA BMI Exit_ef6d
$EFBC LDA $A7
$EFBE EOR $AB
$EFC0 BEQ Loc_efc5
$EFC2 BVS Exit_ef6d
$EFC4 .DB $2c ; Optimisation : Opcode for "BIT". Has no effect
Loc_efc5 BVC Exit_ef6d
$EFC7 LDA #$1

$EFC9 .DB $2c ; Optimisation : Opcode for "BIT". Has no effect
Loc_efca LDA #$4

$EFCC .DB $2c ; Optimisation : Opcode for "BIT". Has no effect
Loc_efcd LDA #$80

$EFCF .DB $2c ; Optimisation : Opcode for "BIT". Has no effect
Loc_efd0 LDA #$2

$EFD2 ORA $0297
$EFD5 STA $0297
$EFD8 JMP Loc_ef7e

Loc_efdb LDA $AA
$EFDD BNE Loc_efd0
$EFDF BEQ Loc_efcd
Loc_efe1 STA $9A
$EFE3 LDA $0294
$EFE6 LSR
$EFE7 BCC Exit_f012
$EFE9 LDA #$2
$EFEB BIT $DD01
$EFEE BPL Loc_f00d
$EFF0 BNE Exit_f012
Loc_eff2 LDA $02A1
$EFF5 AND #$2
$EFF7 BNE Loc_eff2
Loc_eff9 BIT $DD01
$EFFC BVS Loc_eff9
$EFFE LDA $DD01
$F001 ORA #$2
$F003 STA $DD01
Loc_f006 BIT $DD01
$F009 BVS Exit_f012
$F00B BMI Loc_f006
Loc_f00d LDA #$40
$F00F STA $0297
Exit_f012 CLC
$F013 RTS

Loc_f014 JSR Funct_f028
Funct_f017 LDY $029E
$F01A INY
$F01B CPY $029D
$F01E BEQ Loc_f014
$F020 STY $029E
$F023 DEY
$F024 LDA $9E
$F026 STA ($F9),Y
Funct_f028 LDA $02A1
$F02B LSR
$F02C BCS Exit_f04c
$F02E LDA #$10
$F030 STA $DD0E
$F033 LDA $0299
$F036 STA CIA2_TimerA
$F039 LDA $029A
$F03C STA CIA2_TimerA+1
$F03F LDA #$81
$F041 JSR Funct_ef3b
$F044 JSR Funct_ef06
$F047 LDA #$11
$F049 STA $DD0E
Exit_f04c RTS

Loc_f04d STA $99
$F04F LDA $0294
$F052 LSR
$F053 BCC Loc_f07d
$F055 AND #$8
$F057 BEQ Loc_f07d
$F059 LDA #$2
$F05B BIT $DD01
$F05E BPL Loc_f00d
$F060 BEQ Exit_f084
Loc_f062 LDA $02A1
$F065 LSR
$F066 BCS Loc_f062
$F068 LDA $DD01
$F06B AND #$fd
$F06D STA $DD01
Loc_f070 LDA $DD01
$F073 AND #$4
$F075 BEQ Loc_f070
Loc_f077 LDA #$90
$F079 CLC
$F07A JMP Funct_ef3b

Loc_f07d LDA $02A1
$F080 AND #$12
$F082 BEQ Loc_f077
Exit_f084 CLC
$F085 RTS

Funct_f086 LDA $0297
$F089 LDY $029C
$F08C CPY $029B
$F08F BEQ Loc_f09c
$F091 AND #$f7
$F093 STA $0297
$F096 LDA ($F7),Y
$F098 INC $029C
$F09B RTS

Loc_f09c ORA #$8
$F09E STA $0297
$F0A1 LDA #$0
$F0A3 RTS

Funct_f0a4 PHA
$F0A5 LDA $02A1
$F0A8 BEQ Exit_f0bb
Loc_f0aa LDA $02A1
$F0AD AND #$3
$F0AF BNE Loc_f0aa
$F0B1 LDA #$10 ; Disable RS232 Interrupts
$F0B3 STA CIA2_InterruptFlags
$F0B6 LDA #$0
$F0B8 STA $02A1
Exit_f0bb PLA
$F0BC RTS

$F0BD .DB $0d,"I/O ERROR ",$a3
$F0C9 .DB $0d,"SEARCHING",$a0
$F0D4 .DB "FOR",$a0
$F0D8 .DB $0d,"PRESS PLAY ON TAP",$c5
$F0EB .DB "PRESS RECORD & PLAY ON TAP",$c5
$F106 .DB $0d,"LOADIN",$c7
$F10E .DB $0d,"SAVING",$a0
$F116 .DB $0d,"VERIFYIN",$c7
$F120 .DB $0d,"FOUND",$a0,$0d
$F128 .DB $4f
$F129 .DB $4b
$F12A .DB $8d

Loc_f12b BIT $9D
$F12D BPL Exit_f13c


; *************************************************
; Function : PrintKernalString_f12f
;
; Parameters
;
;
; Return Value :
;
; *************************************************

PrintKernalString_f12f LDA
$F0BD,Y
$F132 PHP
$F133 AND #$7f
$F135 JSR OutChar
$F138 INY
$F139 PLP
$F13A BPL PrintKernalString_f12f
Exit_f13c CLC
$F13D RTS


$F13E LDA $99
$F140 BNE Loc_f14a
$F142 LDA NDX
$F144 BEQ Exit_f155
$F146 SEI
$F147 JMP Loc_e5b4

Loc_f14a CMP #$2
$F14C BNE Loc_f166
Funct_f14e STY $97
$F150 JSR Funct_f086
$F153 LDY $97
Exit_f155 CLC
$F156 RTS

$F157 LDA $99
$F159 BNE Loc_f166
$F15B LDA $D3
$F15D STA $CA
$F15F LDA $D6
$F161 STA $C9
$F163 JMP Loc_e632

Loc_f166 CMP #$3
$F168 BNE Loc_f173
$F16A STA $D0
$F16C LDA $D5
$F16E STA $C8
$F170 JMP Loc_e632

Loc_f173 BCS Loc_f1ad
$F175 CMP #$2
$F177 BEQ Loc_f1b8
$F179 STX $97
$F17B JSR Funct_f199
$F17E BCS Loc_f196
$F180 PHA
$F181 JSR Funct_f199
$F184 BCS Loc_f193
$F186 BNE Loc_f18d
$F188 LDA #$40
$F18A JSR SetStatus
Loc_f18d DEC $A6
$F18F LDX $97
$F191 PLA
$F192 RTS

Loc_f193 TAX
$F194 PLA
$F195 TXA
Loc_f196 LDX $97
$F198 RTS


Funct_f199 JSR Funct_f80d
$F19C BNE Loc_f1a9
$F19E JSR TapeLoadHeaderSection
$F1A1 BCS Exit_f1b4
$F1A3 LDA #$0
$F1A5 STA $A6
$F1A7 BEQ Funct_f199
Loc_f1a9 LDA (StartofTapeBufPtr),Y
$F1AB CLC
$F1AC RTS


Loc_f1ad LDA $90
$F1AF BEQ Loc_f1b5
Loc_f1b1 LDA #$d
Loc_f1b3 CLC
Exit_f1b4 RTS


Loc_f1b5 JMP GetSerialByte

Loc_f1b8 JSR Funct_f14e
$F1BB BCS Exit_f1b4
$F1BD CMP #$0
$F1BF BNE Loc_f1b3
$F1C1 LDA $0297
$F1C4 AND #$60
$F1C6 BNE Loc_f1b1
$F1C8 BEQ Loc_f1b8

$F1CA PHA
$F1CB LDA $9A
$F1CD CMP #$3
$F1CF BNE Loc_f1d5
$F1D1 PLA
$F1D2 JMP Loc_e716

Loc_f1d5 BCC Loc_f1db
$F1D7 PLA
$F1D8 JMP Funct_eddd

Loc_f1db LSR
$F1DC PLA
Funct_f1dd STA $9E
$F1DF TXA
$F1E0 PHA
$F1E1 TYA
$F1E2 PHA
$F1E3 BCC Loc_f208
$F1E5 JSR Funct_f80d
$F1E8 BNE Loc_f1f8
$F1EA JSR TapeSaveHeaderSection
$F1ED BCS Loc_f1fd
$F1EF LDA #$2
$F1F1 LDY #$0
$F1F3 STA (StartofTapeBufPtr),Y
$F1F5 INY
$F1F6 STY $A6
Loc_f1f8 LDA $9E
$F1FA STA (StartofTapeBufPtr),Y
Loc_f1fc CLC
Loc_f1fd PLA
$F1FE TAY
$F1FF PLA
$F200 TAX
$F201 LDA $9E
$F203 BCC Exit_f207
$F205 LDA #$0
Exit_f207 RTS


Loc_f208 JSR Funct_f017
$F20B JMP Loc_f1fc
$F20E JSR Funct_f30f
$F211 BEQ Loc_f216
$F213 JMP Loc_f701

Loc_f216 JSR Funct_f31f
$F219 LDA CurrentDevice
$F21B BEQ Loc_f233
$F21D CMP #$3
$F21F BEQ Loc_f233
$F221 BCS Loc_f237
$F223 CMP #$2
$F225 BNE Loc_f22a
$F227 JMP Loc_f04d

Loc_f22a LDX SecondaryAdr
$F22C CPX #$60
$F22E BEQ Loc_f233
$F230 JMP Loc_f70a

Loc_f233 STA $99
$F235 CLC
$F236 RTS

Loc_f237 TAX
$F238 JSR SendTalkCmd
$F23B LDA SecondaryAdr
$F23D BPL Loc_f245
$F23F JSR Funct_edcc
$F242 JMP Loc_f248

Loc_f245 JSR SerialSendSecondaryAdrAfterTalk
Loc_f248 TXA
$F249 BIT $90
$F24B BPL Loc_f233
$F24D JMP IOExit??Loc_f707

$F250 JSR Funct_f30f
$F253 BEQ Loc_f258
$F255 JMP Loc_f701

Loc_f258 JSR Funct_f31f
$F25B LDA CurrentDevice
$F25D BNE Loc_f262
Loc_f25f JMP Loc_f70d

Loc_f262 CMP #$3
$F264 BEQ Loc_f275
$F266 BCS Loc_f279
$F268 CMP #$2
$F26A BNE Loc_f26f
$F26C JMP Loc_efe1

Loc_f26f LDX SecondaryAdr
$F271 CPX #$60
$F273 BEQ Loc_f25f
Loc_f275 STA $9A
$F277 CLC
$F278 RTS

Loc_f279 TAX
$F27A JSR SendListenCmd
$F27D LDA SecondaryAdr
$F27F BPL Loc_f286
$F281 JSR ClearSerialATN
$F284 BNE Loc_f289
Loc_f286 JSR SerialSendSecondaryAdrAfterListen
Loc_f289 TXA
$F28A BIT $90
$F28C BPL Loc_f275
$F28E JMP IOExit??Loc_f707
$F291 JSR Funct_f314
$F294 BEQ Loc_f298
$F296 CLC
$F297 RTS

Loc_f298 JSR Funct_f31f
$F29B TXA
$F29C PHA
$F29D LDA CurrentDevice
$F29F BEQ Loc_f2f1
$F2A1 CMP #$3
$F2A3 BEQ Loc_f2f1
$F2A5 BCS Loc_f2ee
$F2A7 CMP #$2
$F2A9 BNE Loc_f2c8
$F2AB PLA
$F2AC JSR Funct_f2f2
$F2AF JSR Funct_f483
$F2B2 JSR Funct_fe27
$F2B5 LDA $F8
$F2B7 BEQ Loc_f2ba
$F2B9 INY
Loc_f2ba LDA $FA
$F2BC BEQ Loc_f2bf
$F2BE INY
Loc_f2bf LDA #$0
$F2C1 STA $F8
$F2C3 STA $FA
$F2C5 JMP Loc_f47d

Loc_f2c8 LDA SecondaryAdr
$F2CA AND #$f
$F2CC BEQ Loc_f2f1
$F2CE JSR GetTapeBufferAddress
$F2D1 LDA #$0
$F2D3 SEC
$F2D4 JSR Funct_f1dd
$F2D7 JSR TapeSaveHeaderSection
$F2DA BCC Loc_f2e0
$F2DC PLA
$F2DD LDA #$0
$F2DF RTS

Loc_f2e0 LDA SecondaryAdr
$F2E2 CMP #$62
$F2E4 BNE Loc_f2f1
$F2E6 LDA #$5
$F2E8 JSR CreateWriteTapeHeader
$F2EB JMP Loc_f2f1
Loc_f2ee JSR SendSerialAbortCMD??_f642
Loc_f2f1 PLA
Funct_f2f2 TAX
$F2F3 DEC $98
$F2F5 CPX $98
$F2F7 BEQ Exit_f30d
$F2F9 LDY $98
$F2FB LDA $0259,Y
$F2FE STA $0259,X
$F301 LDA $0263,Y
$F304 STA $0263,X
$F307 LDA $026D,Y
$F30A STA $026D,X
Exit_f30d CLC
$F30E RTS

Funct_f30f LDA #$0
$F311 STA $90
$F313 TXA
Funct_f314 LDX $98
Loc_f316 DEX
$F317 BMI Exit_f32e
$F319 CMP $0259,X
$F31C BNE Loc_f316
$F31E RTS

Funct_f31f LDA $0259,X
$F322 STA LogicalFile
$F324 LDA $0263,X
$F327 STA CurrentDevice
$F329 LDA $026D,X
$F32C STA SecondaryAdr
Exit_f32e RTS

$F32F LDA #$0
$F331 STA $98
$F333 LDX #$3
$F335 CPX $9A
$F337 BCS Loc_f33c
$F339 JSR UnListen
Loc_f33c CPX $99
$F33E BCS Loc_f343
$F340 JSR UnTalk
Loc_f343 STX $9A
$F345 LDA #$0
$F347 STA $99
$F349 RTS

$F34A LDX LogicalFile
$F34C BNE Loc_f351
$F34E JMP Loc_f70a

Loc_f351 JSR Funct_f30f
$F354 BNE Loc_f359
$F356 JMP Loc_f6fe

Loc_f359 LDX $98
$F35B CPX #$a
$F35D BCC Loc_f362
$F35F JMP Loc_f6fb

Loc_f362 INC $98
$F364 LDA LogicalFile
$F366 STA $0259,X
$F369 LDA SecondaryAdr
$F36B ORA #$60
$F36D STA SecondaryAdr
$F36F STA $026D,X
$F372 LDA CurrentDevice
$F374 STA $0263,X
$F377 BEQ Exit_f3d3
$F379 CMP #$3
$F37B BEQ Exit_f3d3
$F37D BCC Loc_f384
$F37F JSR SendSerSecAdrFnam
$F382 BCC Exit_f3d3
Loc_f384 CMP #$2
$F386 BNE Loc_f38b
$F388 JMP Loc_f409

Loc_f38b JSR GetTapeBufferAddress
$F38E BCS Loc_f393
$F390 JMP HandleIOError2

Loc_f393 LDA SecondaryAdr
$F395 AND #$f
$F397 BNE Loc_f3b8
$F399 JSR PressPlayOnTape
$F39C BCS Loc_f3d4
$F39E JSR DispSearchingFor
$F3A1 LDA NumCharsInFname
$F3A3 BEQ Loc_f3af
$F3A5 JSR FindTapeFile
$F3A8 BCC Loc_f3c2
$F3AA BEQ Loc_f3d4
Loc_f3ac JMP RaiseIOError??Loc_f704

Loc_f3af JSR FindFileTapeHeader
$F3B2 BEQ Loc_f3d4
$F3B4 BCC Loc_f3c2
$F3B6 BCS Loc_f3ac
Loc_f3b8 JSR ConfirmRecPlayIsPressed
$F3BB BCS Loc_f3d4
$F3BD LDA #$4
$F3BF JSR CreateWriteTapeHeader
Loc_f3c2 LDA #$bf
$F3C4 LDY SecondaryAdr
$F3C6 CPY #$60
$F3C8 BEQ Loc_f3d1
$F3CA LDY #$0
$F3CC LDA #$2
$F3CE STA (StartofTapeBufPtr),Y
$F3D0 TYA
Loc_f3d1 STA $A6
Exit_f3d3 CLC
Loc_f3d4 RTS

SendSerSecAdrFnam LDA SecondaryAdr
$F3D7 BMI Exit_f3d3
$F3D9 LDY NumCharsInFname
$F3DB BEQ Exit_f3d3
$F3DD LDA #$0
$F3DF STA $90
$F3E1 LDA CurrentDevice
$F3E3 JSR SendListenCmd
$F3E6 LDA SecondaryAdr
$F3E8 ORA #$f0
$F3EA JSR SerialSendSecondaryAdrAfterListen
$F3ED LDA $90
$F3EF BPL Loc_f3f6

; Some kind of major exit. Remove calling address from stack

$F3F1 PLA
$F3F2 PLA
$F3F3 JMP IOExit??Loc_f707

Loc_f3f6 LDA NumCharsInFname
$F3F8 BEQ Loc_f406
$F3FA LDY #$0
Loc_f3fc LDA (FilenamePointerLo),Y
$F3FE JSR Funct_eddd
$F401 INY
$F402 CPY NumCharsInFname
$F404 BNE Loc_f3fc
Loc_f406 JMP ExitUnlisten_f654

Loc_f409 JSR Funct_f483
$F40C STY $0297
Loc_f40f CPY NumCharsInFname
$F411 BEQ Loc_f41d
$F413 LDA (FilenamePointerLo),Y
$F415 STA $0293,Y
$F418 INY
$F419 CPY #$4
$F41B BNE Loc_f40f
Loc_f41d JSR Loc_ef4a
$F420 STX $0298
$F423 LDA $0293
$F426 AND #$f
$F428 BEQ Loc_f446
$F42A ASL
$F42B TAX
$F42C LDA $02A6
$F42F BNE Loc_f43a
$F431 LDY $FEC1,X
$F434 LDA $FEC0,X
$F437 JMP Loc_f440
Loc_f43a LDY Exit_e4eb,X
$F43D LDA $E4E9+1,X
Loc_f440 STY $0296
$F443 STA $0295
Loc_f446 LDA $0295
$F449 ASL
$F44A JSR Funct_ff2e
$F44D LDA $0294
$F450 LSR
$F451 BCC Loc_f45c
$F453 LDA $DD01
$F456 ASL
$F457 BCS Loc_f45c
$F459 JSR Loc_f00d
Loc_f45c LDA $029B
$F45F STA $029C
$F462 LDA $029E
$F465 STA $029D
$F468 JSR Funct_fe27
$F46B LDA $F8
$F46D BNE Loc_f474
$F46F DEY
$F470 STY $F8
$F472 STX $F7
Loc_f474 LDA $FA
$F476 BNE Loc_f47d
$F478 DEY
$F479 STY $FA
$F47B STX $F9
Loc_f47d SEC
$F47E LDA #$f0
$F480 JMP Funct_fe2d
Funct_f483 LDA #$7f
$F485 STA CIA2_InterruptFlags
$F488 LDA #$6
$F48A STA $DD03
$F48D STA $DD01
$F490 LDA #$4
$F492 ORA $DD00
$F495 STA $DD00
$F498 LDY #$0
$F49A STY $02A1
$F49D RTS

Loc_f49e STX $C3
$F4A0 STY $C3+1
$F4A2 JMP (LoadVector)


; *************************************************
; Function : LoadProgram - Called by Vector
;
; Parameters
; A - 0 = Loading / anyother = Verifying
;
;
; Return Value :
;
; *************************************************

LoadProgram STA
$93
$F4A7 LDA #$0
$F4A9 STA $90
$F4AB LDA CurrentDevice
$F4AD BNE Loc_f4b2
Loc_f4af JMP HandleIOError2

Loc_f4b2 CMP #$3
$F4B4 BEQ Loc_f4af
$F4B6 BCC Loc_f533

; This handles loading a program from a disk drive

$F4B8 LDY
NumCharsInFname
$F4BA BNE Loc_f4bf
$F4BC JMP HandleIOError

Loc_f4bf LDX SecondaryAdr
$F4C1 JSR DispSearchingFor
$F4C4 LDA #$60
$F4C6 STA SecondaryAdr
$F4C8 JSR SendSerSecAdrFnam
$F4CB LDA CurrentDevice
$F4CD JSR SendTalkCmd
$F4D0 LDA SecondaryAdr
$F4D2 JSR SerialSendSecondaryAdrAfterTalk

; Get the requested load address

$F4D5 JSR
GetSerialByte
$F4D8 STA $AE
$F4DA LDA $90
$F4DC LSR
$F4DD LSR
$F4DE BCS Loc_f530
$F4E0 JSR GetSerialByte
$F4E3 STA $AE+1

; X contains the true secondary address as supplied by user

$F4E5 TXA
$F4E6 BNE Loc_f4f0


; Secondary Address = 0 so load in where we want

$F4E8 LDA
$C3
$F4EA STA $AE
$F4EC LDA $C3+1
$F4EE STA $AE+1
Loc_f4f0 JSR DispLoadingVerifying


DiskLoadGetNextByte LDA #$fd
$F4F5 AND $90
$F4F7 STA $90
$F4F9 JSR CallTestStopVector
$F4FC BNE Loc_f501
$F4FE JMP Loc_f633

Loc_f501 JSR GetSerialByte
$F504 TAX
$F505 LDA $90
$F507 LSR
$F508 LSR
$F509 BCS DiskLoadGetNextByte
$F50B TXA
$F50C LDY $93
$F50E BEQ StoreData_f51c


; Verify Data

$F510 LDY #$0
$F512 CMP ($AE),Y
$F514 BEQ IncFPtr_f51e
$F516 LDA #$10
$F518 JSR SetStatus
$F51B .DB $2c ; Optimisation : Opcode for "BIT". Has no effect
StoreData_f51c STA ($AE),Y
IncFPtr_f51e INC $AE
$F520 BNE Loc_f524
$F522 INC $AE+1
Loc_f524 BIT $90
$F526 BVC DiskLoadGetNextByte
$F528 JSR UnTalk
$F52B JSR SendSerialAbortCMD??_f642
$F52E BCC Loc_f5a9
Loc_f530 JMP RaiseIOError??Loc_f704

Loc_f533 LSR
$F534 BCS LoadProgramFromTape
$F536 JMP HandleIOError2


LoadProgramFromTape JSR
GetTapeBufferAddress
$F53C BCS Loc_f541
$F53E JMP HandleIOError2

Loc_f541 JSR PressPlayOnTape
$F544 BCS Loc_f5ae
$F546 JSR DispSearchingFor
Loc_f549 LDA NumCharsInFname
$F54B BEQ Loc_f556
$F54D JSR FindTapeFile
$F550 BCC Loc_f55d
$F552 BEQ Loc_f5ae
$F554 BCS Loc_f530
Loc_f556 JSR FindFileTapeHeader
$F559 BEQ Loc_f5ae
$F55B BCS Loc_f530
Loc_f55d LDA $90
$F55F AND #$10
$F561 SEC
$F562 BNE Loc_f5ae
$F564 CPX #$1 ; File Type 1 can be relocated
$F566 BEQ Loc_f579
$F568 CPX #$3 ; File Type 3 always loads at it's requested Address
$F56A BNE Loc_f549


; Relocate the file

Loc_f56c LDY #$1
$F56E LDA (StartofTapeBufPtr),Y
$F570 STA $C3
$F572 INY
$F573 LDA (StartofTapeBufPtr),Y
$F575 STA $C3+1
$F577 BCS Loc_f57d

Loc_f579 LDA SecondaryAdr ; If secondary address is non zero, load file at it's requested address
$F57B BNE Loc_f56c ; otherwise relocate the file


; Relocate the file

Loc_f57d LDY #$3
$F57F LDA (StartofTapeBufPtr),Y
$F581 LDY #$1
$F583 SBC (StartofTapeBufPtr),Y
$F585 TAX
$F586 LDY #$4
$F588 LDA (StartofTapeBufPtr),Y
$F58A LDY #$2
$F58C SBC (StartofTapeBufPtr),Y
$F58E TAY
$F58F CLC
$F590 TXA
$F591 ADC $C3
$F593 STA $AE
$F595 TYA
$F596 ADC $C3+1
$F598 STA $AE+1
$F59A LDA $C3
$F59C STA IOStartAddressLo
$F59E LDA $C3+1
$F5A0 STA IOStartAddressHi
$F5A2 JSR DispLoadingVerifying
$F5A5 JSR Funct_f84a
$F5A8 BIT $18
$F5AA LDX $AE
$F5AC LDY $AE+1
Loc_f5ae RTS


DispSearchingFor LDA $9D
$F5B1 BPL Exit_f5d1
$F5B3 LDY #$c ; Display "Searching"
$F5B5 JSR PrintKernalString_f12f
$F5B8 LDA NumCharsInFname
$F5BA BEQ Exit_f5d1
$F5BC LDY #$17 ; Displays "FOR "
$F5BE JSR PrintKernalString_f12f
Loc_f5c1 LDY NumCharsInFname
$F5C3 BEQ Exit_f5d1
$F5C5 LDY #$0 ; Display Filename
Loc_f5c7 LDA (FilenamePointerLo),Y
$F5C9 JSR OutChar
$F5CC INY
$F5CD CPY NumCharsInFname
$F5CF BNE Loc_f5c7
Exit_f5d1 RTS

DispLoadingVerifying LDY #$49 ; Display "Loading"
$F5D4 LDA $93
$F5D6 BEQ Loc_f5da
$F5D8 LDY #$59 ; Display "Verifying"
Loc_f5da JMP Loc_f12b

Loc_f5dd STX $AE
$F5DF STY $AE+1
$F5E1 TAX
$F5E2 LDA $00,X
$F5E4 STA IOStartAddressLo
$F5E6 LDA $01,X
$F5E8 STA IOStartAddressHi
$F5EA JMP (SaveVector)

SaveProgram LDA CurrentDevice
$F5EF BNE Loc_f5f4
Loc_f5f1 JMP HandleIOError2

Loc_f5f4 CMP #$3
$F5F6 BEQ Loc_f5f1
$F5F8 BCC Loc_f659


; Save File to Disk

SaveFileToDisk LDA #$61
$F5FC STA SecondaryAdr
$F5FE LDY NumCharsInFname
$F600 BNE Loc_f605
$F602 JMP HandleIOError

Loc_f605 JSR SendSerSecAdrFnam
$F608 JSR DisplaySaving
$F60B LDA CurrentDevice
$F60D JSR SendListenCmd
$F610 LDA SecondaryAdr
$F612 JSR SerialSendSecondaryAdrAfterListen
$F615 LDY #$0
$F617 JSR LoadupIOStartAdr
$F61A LDA $AC
$F61C JSR Funct_eddd
$F61F LDA $AC+1
$F621 JSR Funct_eddd


; Save next disk file byte

Loc_f624 JSR
CheckForFileEnd
$F627 BCS Loc_f63f
$F629 LDA ($AC),Y
$F62B JSR Funct_eddd
$F62E JSR CallTestStopVector
$F631 BNE Loc_f63a
Loc_f633 JSR SendSerialAbortCMD??_f642
$F636 LDA #$0
$F638 SEC
$F639 RTS

Loc_f63a JSR IncrementLdSvPtr
$F63D BNE Loc_f624
Loc_f63f JSR UnListen
SendSerialAbortCMD??_f642
BIT
SecondaryAdr
$F644 BMI Loc_f657
$F646 LDA CurrentDevice
$F648 JSR SendListenCmd
$F64B LDA SecondaryAdr
$F64D AND #$ef
$F64F ORA #$e0
$F651 JSR SerialSendSecondaryAdrAfterListen
ExitUnlisten_f654 JSR UnListen
Loc_f657 CLC
$F658 RTS

Loc_f659 LSR
$F65A BCS SaveProgramToTape
$F65C JMP HandleIOError2


; *************************************************
; Function : SaveProgramToTape
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SaveProgramToTape JSR
GetTapeBufferAddress
$F662 BCC Loc_f5f1
$F664 JSR ConfirmRecPlayIsPressed
$F667 BCS Exit_f68e
$F669 JSR DisplaySaving
$F66C LDX #$3
$F66E LDA SecondaryAdr
$F670 AND #$1
$F672 BNE Loc_f676
$F674 LDX #$1
Loc_f676 TXA
$F677 JSR CreateWriteTapeHeader
$F67A BCS Exit_f68e
$F67C JSR TapeSaveDataSection
$F67F BCS Exit_f68e
$F681 LDA SecondaryAdr
$F683 AND #$2
$F685 BEQ Loc_f68d
$F687 LDA #$5
$F689 JSR CreateWriteTapeHeader
$F68C BIT $18
Exit_f68e RTS

DisplaySaving LDA $9D
$F691 BPL Exit_f68e
$F693 LDY #$51
$F695 JSR PrintKernalString_f12f
$F698 JMP Loc_f5c1

Loc_f69b LDX #$0
$F69D INC $A2
$F69F BNE Loc_f6a7
$F6A1 INC $A1
$F6A3 BNE Loc_f6a7
$F6A5 INC $A0
Loc_f6a7 SEC
$F6A8 LDA $A2
$F6AA SBC #$1
$F6AC LDA $A1
$F6AE SBC #$1a
$F6B0 LDA $A0
$F6B2 SBC #$4f
$F6B4 BCC LogPIAKeyReading
$F6B6 STX $A0
$F6B8 STX $A1
$F6BA STX $A2
LogPIAKeyReading LDA $DC01
$F6BF CMP $DC01
$F6C2 BNE LogPIAKeyReading
$F6C4 TAX
$F6C5 BMI Loc_f6da
$F6C7 LDX #$bd
$F6C9 STX $DC00
Loc_f6cc LDX $DC01
$F6CF CPX $DC01
$F6D2 BNE Loc_f6cc
$F6D4 STA $DC00
$F6D7 INX
$F6D8 BNE Exit_f6dc
Loc_f6da STA $91
Exit_f6dc RTS


Loc_f6dd SEI
$F6DE LDA $A2
$F6E0 LDX $A1
$F6E2 LDY $A0
Loc_f6e4 SEI
$F6E5 STA $A2
$F6E7 STX $A1
$F6E9 STY $A0
$F6EB CLI
$F6EC RTS

$F6ED LDA $91
$F6EF CMP #$7f
$F6F1 BNE Loc_f6fa
$F6F3 PHP
$F6F4 JSR CallRestoreIOVct
$F6F7 STA NDX
$F6F9 PLP
Loc_f6fa RTS

Loc_f6fb LDA #$1
$F6FD .DB $2c ; Optimisation : Opcode for "BIT". Has no effect

Loc_f6fe LDA #$2
$F700 .DB $2c ; Optimisation : Opcode for "BIT". Has no effect

Loc_f701 LDA #$3
$F703 .DB $2c ; Optimisation : Opcode for "BIT". Has no effect

RaiseIOError??Loc_f704 LDA #$4
$F706 .DB $2c ; Optimisation : Opcode for "BIT". Has no effect

IOExit??Loc_f707 LDA #$5
$F709 .DB $2c ; Optimisation : Opcode for "BIT". Has no effect

Loc_f70a LDA #$6
$F70C .DB $2c ; Optimisation : Opcode for "BIT". Has no effect

Loc_f70d LDA #$7
$F70F .DB $2c ; Optimisation : Opcode for "BIT". Has no effect

HandleIOError LDA #$8
$F712 .DB $2c ; Optimisation : Opcode for "BIT". Has no effect

HandleIOError2 LDA #$9
$F715 PHA
$F716 JSR CallRestoreIOVct
$F719 LDY #$0
$F71B BIT $9D
$F71D BVC Loc_f729
$F71F JSR PrintKernalString_f12f ; Display "I/O ERROR"
$F722 PLA
$F723 PHA
$F724 ORA #$30
$F726 JSR OutChar
Loc_f729 PLA
$F72A SEC
$F72B RTS


; *************************************************
; Function : FindFileTapeHeader
;
; Parameters
;
;
; Return Value :
;
; *************************************************

FindFileTapeHeader LDA
$93
$F72E PHA
$F72F JSR TapeLoadHeaderSection
$F732 PLA
$F733 STA $93
$F735 BCS Exit_f769
$F737 LDY #$0
$F739 LDA (StartofTapeBufPtr),Y ; Read in the file type
$F73B CMP #$5
$F73D BEQ Exit_f769
$F73F CMP #$1
$F741 BEQ Loc_f74b
$F743 CMP #$3
$F745 BEQ Loc_f74b
$F747 CMP #$4
$F749 BNE FindFileTapeHeader
Loc_f74b TAX
$F74C BIT $9D
$F74E BPL Loc_f767
$F750 LDY #$63 ; Print "Found"
$F752 JSR PrintKernalString_f12f


; Display Filename

$F755 LDY #$5
Loc_f757 LDA (StartofTapeBufPtr),Y
$F759 JSR OutChar
$F75C INY
$F75D CPY #$15
$F75F BNE Loc_f757
$F761 LDA $A1
$F763 JSR Funct_e4e0
$F766 NOP
Loc_f767 CLC
$F768 DEY
Exit_f769 RTS


; *************************************************
; Function : WriteTapeHeaderSection
;
; Parameters
; A = FileType
;
; Return Value :
;
; *************************************************

CreateWriteTapeHeader STA
$9E
$F76C JSR GetTapeBufferAddress
$F76F BCC Loc_f7cf
$F771 LDA IOStartAddressHi
$F773 PHA
$F774 LDA IOStartAddressLo
$F776 PHA
$F777 LDA $AE+1
$F779 PHA
$F77A LDA $AE
$F77C PHA


; Clear Tape Buffer with spaces

Funct_f77d LDY #$bf
$F77F LDA #$20
Loc_f781 STA (StartofTapeBufPtr),Y
$F783 DEY
$F784 BNE Loc_f781

$F786 LDA $9E ; Set the save type
$F788 STA (StartofTapeBufPtr),Y
$F78A INY
$F78B LDA IOStartAddressLo ; Set Header Load Address Lo
$F78D STA (StartofTapeBufPtr),Y
$F78F INY
$F790 LDA IOStartAddressHi ; Set Header Load Address Hi
$F792 STA (StartofTapeBufPtr),Y
$F794 INY
$F795 LDA $AE ; Set Header Load End Address Lo
$F797 STA (StartofTapeBufPtr),Y
$F799 INY
$F79A LDA $AE+1 ; Set Header Load End Address Hi
$F79C STA (StartofTapeBufPtr),Y
$F79E INY
$F79F STY $9F
$F7A1 LDY #$0
$F7A3 STY $9E
Loc_f7a5 LDY $9E
$F7A7 CPY NumCharsInFname
$F7A9 BEQ Loc_f7b7
$F7AB LDA (FilenamePointerLo),Y

; Store Filename to the Header
$F7AD LDY $9F
$F7AF STA (StartofTapeBufPtr),Y
$F7B1 INC $9E
$F7B3 INC $9F
$F7B5 BNE Loc_f7a5
Loc_f7b7 JSR SetLoadEnd2EndTapBuf

; Set Lead in to 0x6A00 pulses long

$F7BA LDA #$69
$F7BC STA $AB
$F7BE JSR WriteTapeSection
$F7C1 TAY
$F7C2 PLA
$F7C3 STA $AE
$F7C5 PLA
$F7C6 STA $AE+1
$F7C8 PLA
$F7C9 STA IOStartAddressLo
$F7CB PLA
$F7CC STA IOStartAddressHi
$F7CE TYA
Loc_f7cf RTS



; *************************************************
; Function : GetTapeBufferAddress
;
; Parameters
;
;
; Return Value :
;
; *************************************************

GetTapeBufferAddress LDX
StartofTapeBufPtr
$F7D2 LDY StartofTapeBufPtr+1
$F7D4 CPY #$2
$F7D6 RTS


; *************************************************
; Function : SetLoadEnd2EndTapBuf
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SetLoadEnd2EndTapBuf JSR
GetTapeBufferAddress
$F7DA TXA
$F7DB STA IOStartAddressLo
$F7DD CLC
$F7DE ADC #$c0

; Tape buffer size of $c0 bytes
$F7E0 STA $AE
$F7E2 TYA
$F7E3 STA IOStartAddressHi
$F7E5 ADC #$0
$F7E7 STA $AE+1
$F7E9 RTS



; *************************************************
; Function : FindTapeFile
;
; Parameters
;
;
; Return Value :
;
; *************************************************

FindTapeFile JSR
FindFileTapeHeader
$F7ED BCS Exit_f80c
$F7EF LDY #$5
$F7F1 STY $9F
$F7F3 LDY #$0
$F7F5 STY $9E
Loc_f7f7 CPY NumCharsInFname
$F7F9 BEQ Loc_f80b
$F7FB LDA (FilenamePointerLo),Y

; Check the filename, if it doesn't match get next
$F7FD LDY $9F
$F7FF CMP (StartofTapeBufPtr),Y
$F801 BNE FindTapeFile
$F803 INC $9E
$F805 INC $9F
$F807 LDY $9E
$F809 BNE Loc_f7f7
Loc_f80b CLC
Exit_f80c RTS

Funct_f80d JSR GetTapeBufferAddress
$F810 INC $A6
$F812 LDY $A6
$F814 CPY #$c0
$F816 RTS


; *************************************************
; Function : PressPlayOnTape
;
; Parameters
;
;
; Return Value :
;
; *************************************************

PressPlayOnTape JSR
CheckTapeStop
$F81A BEQ Exit_f836
$F81C LDY #$1b ; Display "Press Play on Tape"
Loc_f81e JSR PrintKernalString_f12f


; Wait for play to be pressed

Loc_f821 JSR
Funct_f8d0
$F824 JSR CheckTapeStop
$F827 BNE Loc_f821

$F829 LDY #$6a ; Display "OK"
$F82B JMP PrintKernalString_f12f


CheckTapeStop LDA #$10
$F830 BIT $01
$F832 BNE Exit_f836
$F834 BIT $01
Exit_f836 CLC
$F837 RTS


; *************************************************
; Function : ConfirmPlayIsPressed
;
; Parameters
;
;
; Return Value :
;
; *************************************************

ConfirmRecPlayIsPressed JSR
CheckTapeStop
$F83B BEQ Exit_f836
$F83D LDY #$2e
$F83F BNE Loc_f81e

TapeLoadHeaderSection LDA #$0
$F843 STA $90
$F845 STA $93
$F847 JSR SetLoadEnd2EndTapBuf
Funct_f84a JSR PressPlayOnTape
$F84D BCS Loc_f86e
$F84F SEI
$F850 LDA #$0
$F852 STA $AA
$F854 STA $B4
$F856 STA $B0
$F858 STA $9E
$F85A STA $9F
$F85C STA $9C
$F85E LDA #$90 ; Interrupt on Tape Pulse
$F860 LDX #$e ; Select the load IRQ routine - F92C
$F862 BNE StartTapeLoadSave


; *************************************************
; Function : TapeSaveDataSection
;
; Parameters
;
;
; Return Value :
;
; *************************************************

TapeSaveHeaderSection JSR
SetLoadEnd2EndTapBuf

; Set Lead in to 0x1500 pulses long

TapeSaveDataSection LDA #$14
$F869 STA $AB


; *************************************************
; Function : WriteTapeSection
;
; Parameters
;
;
; Return Value :
;
; *************************************************

WriteTapeSection JSR
ConfirmRecPlayIsPressed
Loc_f86e BCS ExitFunct_f8dc
$F870 SEI
$F871 LDA #$82
$F873 LDX #$8 ; Select the save IRQ routine


; *************************************************
; Function : StartTapeLoadSave
;
; Parameters
;
;
; Return Value :
;
; *************************************************

StartTapeLoadSave LDY #$7f
$F877 STY CIA1_InterruptFlags
$F87A STA CIA1_InterruptFlags
$F87D LDA CIA1_TimerA_Ctrl ; Enable Timer B oneshot
$F880 ORA #$19
$F882 STA CIA1_TimerB_Ctrl
$F885 AND #$91
$F887 STA $02A2
$F88A JSR Funct_f0a4
$F88D LDA $D011 ; Turn off the Display
$F890 AND #$ef
$F892 STA $D011
$F895 LDA IRQVectorLo
$F898 STA IRQSaveLo
$F89B LDA IRQVectorHi
$F89E STA IRQSaveHi
$F8A1 JSR LoadupIRQVector ; Load up the tape save irq routine - fc6a / Load IRQ routine - f92c
$F8A4 LDA #$2
$F8A6 STA TapeSaveStage
$F8A8 JSR ResetTapeSaveByteVars


; Start Tape Motor

$F8AB LDA
$01
$F8AD AND #$1f
$F8AF STA $01
$F8B1 STA Adr1Backup


; Give tape motor time to get up to speed

$F8B3 LDX #$ff
Loc_f8b5 LDY #$ff
Loc_f8b7 DEY
$F8B8 BNE Loc_f8b7
$F8BA DEX
$F8BB BNE Loc_f8b5


; Enable interrupts to start Load / Save

$F8BD CLI
WaitTapeLoadSaveLoop LDA IRQSaveHi
$F8C1 CMP IRQVectorHi
$F8C4 CLC
$F8C5 BEQ ExitFunct_f8dc
$F8C7 JSR Funct_f8d0
$F8CA JSR LogPIAKeyReading
$F8CD JMP WaitTapeLoadSaveLoop


; *************************************************
; Function : Funct_f8d0
;
; Parameters
;
;
; Return Value :
;
; *************************************************

Funct_f8d0 JSR
CallTestStopVector
$F8D3 CLC
$F8D4 BNE Exit_f8e1
$F8D6 JSR StopTapeLoadSave
$F8D9 SEC
$F8DA PLA
$F8DB PLA
ExitFunct_f8dc LDA #$0
$F8DE STA IRQSaveHi
Exit_f8e1 RTS


; *************************************************
; Function : Funct_f8e2
;
; Parameters
;
;
; Return Value :
;
; *************************************************

Funct_f8e2 STX
$B1
$F8E4 LDA $B0
$F8E6 ASL
$F8E7 ASL
$F8E8 CLC
$F8E9 ADC $B0
$F8EB CLC
$F8EC ADC $B1
$F8EE STA $B1
$F8F0 LDA #$0
$F8F2 BIT $B0
$F8F4 BMI Loc_f8f7
$F8F6 ROL
Loc_f8f7 ASL $B1
$F8F9 ROL
$F8FA ASL $B1
$F8FC ROL
$F8FD TAX
Loc_f8fe LDA CIA1_TimerB
$F901 CMP #$16
$F903 BCC Loc_f8fe
$F905 ADC $B1
$F907 STA CIA1_TimerA
$F90A TXA
$F90B ADC CIA1_TimerB+1
$F90E STA CIA1_TimerA+1
$F911 LDA $02A2
$F914 STA CIA1_TimerA_Ctrl
$F917 STA $02A4
$F91A LDA CIA1_InterruptFlags ; Check for Tape Pulse
$F91D AND #$10
$F91F BEQ Exit_f92a
$F921 LDA #>Exit_f92a
$F923 PHA
$F924 LDA #<Exit_f92a
$F926 PHA
$F927 JMP Loc_ff43
Exit_f92a CLI
$F92B RTS


; *************************************************
; Function : TapeLoadInterrupt
;
; Parameters
;
;
; Return Value :
;
; *************************************************

TapeLoadInterrupt LDX
CIA1_TimerB+1
$F92F LDY #$ff
$F931 TYA
$F932 SBC CIA1_TimerB ; Invert Timer Low reading
$F935 CPX CIA1_TimerB+1
$F938 BNE TapeLoadInterrupt

$F93A STX $B1 ; Store Hi timer reading in B1
$F93C TAX


; Reset and restart Timer with value 0xffff

$F93D STY
CIA1_TimerB
$F940 STY CIA1_TimerB+1
$F943 LDA #$19
$F945 STA CIA1_TimerB_Ctrl
$F948 LDA CIA1_InterruptFlags
$F94B STA $02A3
$F94E TYA
$F94F SBC $B1 ; Invert Timer Hi reading
$F951 STX $B1


; Divide Timer Reading by 4 and store in B1

$F953 LSR
$F954 ROR $B1
$F956 LSR
$F957 ROR $B1

$F959 LDA $B0
$F95B CLC
$F95C ADC #$3c
$F95E CMP $B1
$F960 BCS Loc_f9ac
$F962 LDX $9C
$F964 BEQ Loc_f969
$F966 JMP Loc_fa60

Loc_f969 LDX TapeSaveBitCount
$F96B BMI Loc_f988
$F96D LDX #$0
$F96F ADC #$30
$F971 ADC $B0
$F973 CMP $B1
$F975 BCS Loc_f993
$F977 INX
$F978 ADC #$26
$F97A ADC $B0
$F97C CMP $B1
$F97E BCS Loc_f997
$F980 ADC #$2c
$F982 ADC $B0
$F984 CMP $B1
$F986 BCC Loc_f98b
Loc_f988 JMP Loc_fa10

Loc_f98b LDA $B4
$F98D BEQ Loc_f9ac
$F98F STA TapeSaveDataByteStartFlag
$F991 BNE Loc_f9ac
Loc_f993 INC $A9
$F995 BCS Loc_f999
Loc_f997 DEC $A9
Loc_f999 SEC
$F99A SBC #$13
$F99C SBC $B1
$F99E ADC $92
$F9A0 STA $92
$F9A2 LDA $A4
$F9A4 EOR #$1
$F9A6 STA $A4
$F9A8 BEQ Loc_f9d5
$F9AA STX FileSaveChkSum
Loc_f9ac LDA $B4
$F9AE BEQ Exit_f9d2
$F9B0 LDA $02A3
$F9B3 AND #$1
$F9B5 BNE Loc_f9bc
$F9B7 LDA $02A4
$F9BA BNE Exit_f9d2
Loc_f9bc LDA #$0
$F9BE STA $A4
$F9C0 STA $02A4
$F9C3 LDA TapeSaveBitCount
$F9C5 BPL Loc_f9f7
$F9C7 BMI Loc_f988
Loc_f9c9 LDX #$a6
$F9CB JSR Funct_f8e2
$F9CE LDA TapeSaveChksumBit
$F9D0 BNE Loc_f98b
Exit_f9d2 JMP ExitInterrupt?

Loc_f9d5 LDA $92
$F9D7 BEQ Loc_f9e0
$F9D9 BMI Loc_f9de
$F9DB DEC $B0
$F9DD .DB $2c ; Optimisation : Opcode for "BIT". Has no effect
Loc_f9de INC $B0
Loc_f9e0 LDA #$0
$F9E2 STA $92
$F9E4 CPX FileSaveChkSum
$F9E6 BNE Loc_f9f7
$F9E8 TXA
$F9E9 BNE Loc_f98b
$F9EB LDA $A9
$F9ED BMI Loc_f9ac
$F9EF CMP #$10
$F9F1 BCC Loc_f9ac
$F9F3 STA $96
$F9F5 BCS Loc_f9ac
Loc_f9f7 TXA
$F9F8 EOR TapeSaveChksumBit
$F9FA STA TapeSaveChksumBit
$F9FC LDA $B4
$F9FE BEQ Exit_f9d2
$FA00 DEC TapeSaveBitCount
$FA02 BMI Loc_f9c9
$FA04 LSR FileSaveChkSum
$FA06 ROR $BF
$FA08 LDX #$da
$FA0A JSR Funct_f8e2
$FA0D JMP ExitInterrupt?

Loc_fa10 LDA $96
$FA12 BEQ Loc_fa18
$FA14 LDA $B4
$FA16 BEQ Loc_fa1f
Loc_fa18 LDA TapeSaveBitCount
$FA1A BMI Loc_fa1f
$FA1C JMP Loc_f997

Loc_fa1f LSR $B1
$FA21 LDA #$93
$FA23 SEC
$FA24 SBC $B1
$FA26 ADC $B0
$FA28 ASL
$FA29 TAX
$FA2A JSR Funct_f8e2
$FA2D INC $9C
$FA2F LDA $B4
$FA31 BNE Loc_fa44
$FA33 LDA $96
$FA35 BEQ Exit_fa5d
$FA37 STA TapeSaveDataByteStartFlag
$FA39 LDA #$0
$FA3B STA $96
$FA3D LDA #$81
$FA3F STA CIA1_InterruptFlags
$FA42 STA $B4
Loc_fa44 LDA $96
$FA46 STA $B5
$FA48 BEQ Loc_fa53
$FA4A LDA #$0
$FA4C STA $B4
$FA4E LDA #$1
$FA50 STA CIA1_InterruptFlags
Loc_fa53 LDA $BF
$FA55 STA DataByte
$FA57 LDA TapeSaveDataByteStartFlag
$FA59 ORA $A9
$FA5B STA $B6
Exit_fa5d JMP ExitInterrupt?

Loc_fa60 JSR ResetTapeSaveByteVars
$FA63 STA $9C
$FA65 LDX #$da
$FA67 JSR Funct_f8e2
$FA6A LDA TapeSaveStage
$FA6C BEQ Loc_fa70
$FA6E STA $A7
Loc_fa70 LDA #$f
$FA72 BIT $AA
$FA74 BPL Loc_fa8d
$FA76 LDA $B5
$FA78 BNE Loc_fa86
$FA7A LDX TapeSaveStage
$FA7C DEX
$FA7D BNE Exit_fa8a
$FA7F LDA #$8
$FA81 JSR SetStatus
$FA84 BNE Exit_fa8a
Loc_fa86 LDA #$0
$FA88 STA $AA
Exit_fa8a JMP ExitInterrupt?

Loc_fa8d BVS Loc_fac0
$FA8F BNE Loc_faa9
$FA91 LDA $B5
$FA93 BNE Exit_fa8a
$FA95 LDA $B6
$FA97 BNE Exit_fa8a
$FA99 LDA $A7
$FA9B LSR
$FA9C LDA DataByte
$FA9E BMI Loc_faa3
$FAA0 BCC Loc_faba
$FAA2 CLC
Loc_faa3 BCS Loc_faba
$FAA5 AND #$f
$FAA7 STA $AA
Loc_faa9 DEC $AA
$FAAB BNE Exit_fa8a
$FAAD LDA #$40
$FAAF STA $AA
$FAB1 JSR LoadupIOStartAdr
$FAB4 LDA #$0
$FAB6 STA $AB
$FAB8 BEQ Exit_fa8a
Loc_faba LDA #$80
$FABC STA $AA
$FABE BNE Exit_fa8a
Loc_fac0 LDA $B5
$FAC2 BEQ Loc_face
$FAC4 LDA #$4
$FAC6 JSR SetStatus
$FAC9 LDA #$0
$FACB JMP Loc_fb4a

Loc_face JSR CheckForFileEnd
$FAD1 BCC Loc_fad6
$FAD3 JMP Loc_fb48
Loc_fad6 LDX $A7
$FAD8 DEX
$FAD9 BEQ Loc_fb08
$FADB LDA $93
$FADD BEQ Loc_faeb
$FADF LDY #$0
$FAE1 LDA DataByte
$FAE3 CMP ($AC),Y
$FAE5 BEQ Loc_faeb
$FAE7 LDA #$1
$FAE9 STA $B6
Loc_faeb LDA $B6
$FAED BEQ Loc_fb3a
$FAEF LDX #$3d
$FAF1 CPX $9E
$FAF3 BCC Loc_fb33
$FAF5 LDX $9E
$FAF7 LDA $AC+1
$FAF9 STA $0101,X
$FAFC LDA $AC
$FAFE STA $0100,X
$FB01 INX
$FB02 INX
$FB03 STX $9E
$FB05 JMP Loc_fb3a

Loc_fb08 LDX $9F
$FB0A CPX $9E
$FB0C BEQ Loc_fb43
$FB0E LDA $AC
$FB10 CMP $0100,X
$FB13 BNE Loc_fb43
$FB15 LDA $AC+1
$FB17 CMP $0101,X
$FB1A BNE Loc_fb43
$FB1C INC $9F
$FB1E INC $9F
$FB20 LDA $93
$FB22 BEQ Loc_fb2f
$FB24 LDA DataByte
$FB26 LDY #$0
$FB28 CMP ($AC),Y
$FB2A BEQ Loc_fb43
$FB2C INY
$FB2D STY $B6
Loc_fb2f LDA $B6
$FB31 BEQ Loc_fb3a
Loc_fb33 LDA #$10
$FB35 JSR SetStatus
$FB38 BNE Loc_fb43
Loc_fb3a LDA $93
$FB3C BNE Loc_fb43
$FB3E TAY
$FB3F LDA DataByte
$FB41 STA ($AC),Y
Loc_fb43 JSR IncrementLdSvPtr
$FB46 BNE Exit_fb8b
Loc_fb48 LDA #$80
Loc_fb4a STA $AA
$FB4C SEI
$FB4D LDX #$1
$FB4F STX CIA1_InterruptFlags
$FB52 LDX CIA1_InterruptFlags
$FB55 LDX TapeSaveStage
$FB57 DEX
$FB58 BMI Loc_fb5c
$FB5A STX TapeSaveStage
Loc_fb5c DEC $A7
$FB5E BEQ Loc_fb68
$FB60 LDA $9E
$FB62 BNE Exit_fb8b
$FB64 STA TapeSaveStage
$FB66 BEQ Exit_fb8b
Loc_fb68 JSR StopTapeLoadSave
$FB6B JSR LoadupIOStartAdr
$FB6E LDY #$0
$FB70 STY $AB
Loc_fb72 LDA ($AC),Y
$FB74 EOR $AB
$FB76 STA $AB
$FB78 JSR IncrementLdSvPtr
$FB7B JSR CheckForFileEnd
$FB7E BCC Loc_fb72
$FB80 LDA $AB
$FB82 EOR DataByte
$FB84 BEQ Exit_fb8b
$FB86 LDA #$20
$FB88 JSR SetStatus
Exit_fb8b JMP ExitInterrupt?


; *************************************************
; Function : LoadupIOStartAdr
;
; Parameters
;
;
; Return Value :
;
; *************************************************

LoadupIOStartAdr LDA
IOStartAddressHi
$FB90 STA $AC+1
$FB92 LDA IOStartAddressLo
$FB94 STA $AC
$FB96 RTS


; *************************************************
; Function : ResetTapeSaveByteVars
;
; Parameters
;
;
; Return Value :
;
; *************************************************

ResetTapeSaveByteVars LDA #$8
$FB99 STA TapeSaveBitCount
$FB9B LDA #$0
$FB9D STA $A4
$FB9F STA TapeSaveDataByteStartFlag
$FBA1 STA TapeSaveChksumBit
$FBA3 STA $A9
$FBA5 RTS




; *************************************************
; Function : TapeDataPulseWrite
;
; Parameters
;
;
; Return Value :
;
; *************************************************

TapeDataPulseWrite LDA
DataByte
$FBA8 LSR
$FBA9 LDA #$60 ; Write a short pulse
$FBAB BCC WriteLowTapePulse
Write0xb0TapePulse LDA #$b0 ; Write a medium pulse
WriteLowTapePulse LDX #$0
WriteTapePulse STA CIA1_TimerB
$FBB4 STX CIA1_TimerB+1
$FBB7 LDA CIA1_InterruptFlags
$FBBA LDA #$19
$FBBC STA CIA1_TimerB_Ctrl
$FBBF LDA $01
$FBC1 EOR #$8
$FBC3 STA $01
$FBC5 AND #$8
$FBC7 RTS


; *************************************************
; Function : WriteDataToTape
;
; Parameters
;
;
; Return Value :
;
; *************************************************

WriteDataToTape SEC
$FBC9 ROR $B6
$FBCB BMI Exit_fc09

; At the begining of each byte to be sent, both
; TapeSaveDataByteStartFlag and address $a9 are
; set to 0. This causes a long pulse, followed
; by a medium pulse.

SaveTapeDataIRQ LDA
TapeSaveDataByteStartFlag
$FBCF BNE Loc_fbe3
$FBD1 LDA #$10 ; Write a Long Pulse
$FBD3 LDX #$1
$FBD5 JSR WriteTapePulse
$FBD8 BNE Exit_fc09
$FBDA INC TapeSaveDataByteStartFlag
$FBDC LDA $B6
$FBDE BPL Exit_fc09
$FBE0 JMP TapeSaveNextStage

; --------------
Loc_fbe3 LDA $A9
$FBE5 BNE Loc_fbf0
$FBE7 JSR Write0xb0TapePulse ; Write a medium pulse
$FBEA BNE Exit_fc09
$FBEC INC $A9
$FBEE BNE Exit_fc09

Loc_fbf0 JSR TapeDataPulseWrite
$FBF3 BNE Exit_fc09

; Switch between data bit write and Inverse data bit write

$FBF5 LDA
$A4
$FBF7 EOR #$1
$FBF9 STA $A4
$FBFB BEQ SaveTapeNextDataBit
$FBFD LDA DataByte

; Invert data bit
$FBFF EOR #$1
$FC01 STA DataByte
$FC03 AND #$1
$FC05 EOR TapeSaveChksumBit
$FC07 STA TapeSaveChksumBit
Exit_fc09 JMP ExitInterrupt?

SaveTapeNextDataBit LSR DataByte
$FC0E DEC TapeSaveBitCount
$FC10 LDA TapeSaveBitCount
$FC12 BEQ SaveTapeChkSumBit
$FC14 BPL Exit_fc09


; Data byte written

Loc_fc16 JSR
ResetTapeSaveByteVars
$FC19 CLI
$FC1A LDA TapeSaveSyncDataCount
$FC1C BEQ SkipTapeSyncSave


; Write a Data Sync Byte

$FC1E LDX #$0
$FC20 STX FileSaveChkSum
$FC22 DEC TapeSaveSyncDataCount
$FC24 LDX TapeSaveStage
$FC26 CPX #$2
$FC28 BNE Loc_fc2c
$FC2A ORA #$80
Loc_fc2c STA DataByte
$FC2E BNE Exit_fc09


SkipTapeSyncSave JSR
CheckForFileEnd
$FC33 BCC TapeSaveGetDataByte
$FC35 BNE WriteDataToTape
$FC37 INC $AC+1


; End of Data, Write out the Checksum

$FC39 LDA
FileSaveChkSum
$FC3B STA DataByte
$FC3D BCS Exit_fc09


; Get a byte of file data to write out

TapeSaveGetDataByte LDY #$0
$FC41 LDA ($AC),Y ; Load up a byte of data to be saved
$FC43 STA DataByte
$FC45 EOR FileSaveChkSum
$FC47 STA FileSaveChkSum
$FC49 JSR IncrementLdSvPtr
$FC4C BNE Exit_fc09
SaveTapeChkSumBit LDA TapeSaveChksumBit
$FC50 EOR #$1
$FC52 STA DataByte
ExitInt??Loc_fc54 JMP ExitInterrupt?


TapeSaveNextStage DEC
TapeSaveStage
$FC59 BNE Loc_fc5e
$FC5B JSR StopTapeMotor
Loc_fc5e LDA #$50 ; Setup Lead size as 50 pulses
$FC60 STA $A7
$FC62 LDX #$8 ; Setup Save tape leadin irq - fc6a
$FC64 SEI
$FC65 JSR LoadupIRQVector
$FC68 BNE ExitInt??Loc_fc54
SaveTapeLeadinIRQ LDA #$78 ; Write a short pulse
$FC6C JSR WriteLowTapePulse
$FC6F BNE ExitInt??Loc_fc54
$FC71 DEC $A7
$FC73 BNE ExitInt??Loc_fc54
$FC75 JSR ResetTapeSaveByteVars
$FC78 DEC $AB
$FC7A BPL ExitInt??Loc_fc54

; Lead-in section written

$FC7C LDX #$a
$FC7E JSR LoadupIRQVector ; Load up the tape save data irq routine - fbcd
$FC81 CLI
$FC82 INC $AB
$FC84 LDA TapeSaveStage
$FC86 BEQ Loc_fcb8
$FC88 JSR LoadupIOStartAdr
$FC8B LDX #$9 ; Setup 9 bytes of Sync Data
$FC8D STX TapeSaveSyncDataCount
$FC8F STX $B6
$FC91 BNE Loc_fc16
StopTapeLoadSave PHP
$FC94 SEI
$FC95 LDA $D011 ; Enable the Display
$FC98 ORA #$10
$FC9A STA $D011
$FC9D JSR StopTapeMotor
$FCA0 LDA #$7f
$FCA2 STA CIA1_InterruptFlags
$FCA5 JSR SetupStandardCia1Time
$FCA8 LDA IRQSaveHi
$FCAB BEQ Loc_fcb6
$FCAD STA IRQVectorHi
$FCB0 LDA IRQSaveLo
$FCB3 STA IRQVectorLo
Loc_fcb6 PLP
$FCB7 RTS

Loc_fcb8 JSR StopTapeLoadSave
$FCBB BEQ ExitInt??Loc_fc54
LoadupIRQVector LDA $FD92+1,X
$FCC0 STA IRQVectorLo
$FCC3 LDA $FD92+2,X
$FCC6 STA IRQVectorHi
$FCC9 RTS

; *************************************************
; Function : StopTapeMotor
;
; Parameters
;
;
; Return Value :
;
; *************************************************

StopTapeMotor LDA
$01
$FCCC ORA #$20
$FCCE STA $01
$FCD0 RTS

; *************************************************
; Function : CheckForFileEnd
;
; Parameters
;
;
; Return Value :
;
; *************************************************

CheckForFileEnd SEC
$FCD2 LDA $AC
$FCD4 SBC $AE
$FCD6 LDA $AC+1
$FCD8 SBC $AE+1
$FCDA RTS

; *************************************************
; Function : IncrementLdSvPtr
;
; Parameters
;
;
; Return Value :
;
; *************************************************

IncrementLdSvPtr INC
$AC
$FCDD BNE Exit_fce1
$FCDF INC $AC+1
Exit_fce1 RTS

; *************************************************
; Function : ColdStart
;
; Parameters
;
;
; Return Value :
;
; *************************************************

ColdStart LDX #$ff
$FCE4 SEI
$FCE5 TXS
$FCE6 CLD
$FCE7 JSR CheckForROMat8000
$FCEA BNE Loc_fcef
$FCEC JMP ($8000)

Loc_fcef STX $D016
$FCF2 JSR ResetHardware
$FCF5 JSR Funct_fd50
$FCF8 JSR ResetAllVectors
$FCFB JSR Funct_ff5b
$FCFE CLI
$FCFF JMP ($A000)


; *************************************************
; Function : CheckForROMat8000
;
; Parameters
;
;
; Return Value :
;
; *************************************************

CheckForROMat8000 LDX #$5
Loc_fd04 LDA Loc_fd0f,X
$FD07 CMP $8003,X
$FD0A BNE Loc_fd0f
$FD0C DEX
$FD0D BNE Loc_fd04
Loc_fd0f RTS

; Cartridge $8000 identifier

$FD10 .DB $c3
$FD11 .DB $c2
$FD12 .DB $cd
$FD13 .DB $38
$FD14 .DB $30


; *************************************************
; Function : ResetAllVectors
;
; Parameters
;
;
; Return Value :
;
; *************************************************

ResetAllVectors LDX #<
DefaultVectorTable
$FD17 LDY #>DefaultVectorTable
$FD19 CLC
ReadSetVectoredIO STX $C3
$FD1C STY $C3+1
$FD1E LDY #$1f
Loc_fd20 LDA IRQVectorLo,Y
$FD23 BCS Loc_fd27
$FD25 LDA ($C3),Y
Loc_fd27 STA ($C3),Y
$FD29 STA IRQVectorLo,Y
$FD2C DEY
$FD2D BPL Loc_fd20
$FD2F RTS

DefaultVectorTable .DW NormalIRQ
$FD32 .DW WarmStart
$FD34 .DW NMIHandler
$FD36 .DW $F34A,$F291,$F20E,$F250,$F333,$F157,$F1CA
.DW $F6ED,$F13E,$F32F,WarmStart,LoadProgram,SaveProgram

Funct_fd50 LDA #$0
$FD52 TAY
Loc_fd53 STA $0002,Y
$FD56 STA BASIC_CMDLineBuffer,Y
$FD59 STA BASIC_ErrorVector,Y
$FD5C INY
$FD5D BNE Loc_fd53
$FD5F LDX #<TapeBuffer
$FD61 LDY #>TapeBuffer
$FD63 STX StartofTapeBufPtr
$FD65 STY StartofTapeBufPtr+1
$FD67 TAY
$FD68 LDA #$3
$FD6A STA IOStartAddressHi
Loc_fd6c INC IOStartAddressHi
Loc_fd6e LDA (IOStartAddressLo),Y
$FD70 TAX
$FD71 LDA #$55
$FD73 STA (IOStartAddressLo),Y
$FD75 CMP (IOStartAddressLo),Y
$FD77 BNE Loc_fd88
$FD79 ROL
$FD7A STA (IOStartAddressLo),Y
$FD7C CMP (IOStartAddressLo),Y
$FD7E BNE Loc_fd88
$FD80 TXA
$FD81 STA (IOStartAddressLo),Y
$FD83 INY
$FD84 BNE Loc_fd6e
$FD86 BEQ Loc_fd6c
Loc_fd88 TYA
$FD89 TAX
$FD8A LDY IOStartAddressHi
$FD8C CLC
$FD8D JSR Funct_fe2d
$FD90 LDA #$8
$FD92 STA $0282
$FD95 LDA #$4
$FD97 STA $0288
$FD9A RTS


$FD9B .DW SaveTapeLeadinIRQ
$FD9D .DW SaveTapeDataIRQ
$FD9F .DW NormalIRQ
$FDA1 .DW TapeLoadInterrupt


; *************************************************
; Function : ResetHardware
;
; Parameters
;
;
; Return Value :
;
; *************************************************

ResetHardware LDA #$7f
$FDA5 STA CIA1_InterruptFlags
$FDA8 STA CIA2_InterruptFlags
$FDAB STA $DC00
$FDAE LDA #$8
$FDB0 STA CIA1_TimerA_Ctrl
$FDB3 STA $DD0E
$FDB6 STA CIA1_TimerB_Ctrl
$FDB9 STA CIA2_TimerB_Control
$FDBC LDX #$0
$FDBE STX $DC03
$FDC1 STX $DD03
$FDC4 STX SIDFilterVolumeSel
$FDC7 DEX
$FDC8 STX $DC02
$FDCB LDA #$7
$FDCD STA $DD00
$FDD0 LDA #$3f
$FDD2 STA $DD02
$FDD5 LDA #$e7
$FDD7 STA $01
$FDD9 LDA #$2f
$FDDB STA $00
SetupStandardCia1Time LDA $02A6
$FDE0 BEQ Loc_fdec
$FDE2 LDA #$25
$FDE4 STA CIA1_TimerA
$FDE7 LDA #$40
$FDE9 JMP Loc_fdf3
Loc_fdec LDA #$95
$FDEE STA CIA1_TimerA
$FDF1 LDA #$42
Loc_fdf3 STA CIA1_TimerA+1
$FDF6 JMP Loc_ff6e


; *************************************************
; Function : SetFileName
;
; Parameters
;
;
; Return Value :
;
; *************************************************

SetFileName STA
NumCharsInFname
$FDFB STX FilenamePointerLo
$FDFD STY FilenamePointerHi
$FDFF RTS


; *************************************************
; Function : SetLFS
; Set Logical, first and second addresses
; Parameters
;
;
; Return Value :
;
; *************************************************

SetLFS STA
LogicalFile
$FE02 STX CurrentDevice
$FE04 STY SecondaryAdr
$FE06 RTS

ReadIOStatusWord LDA CurrentDevice
$FE09 CMP #$2
$FE0B BNE Loc_fe1a
$FE0D LDA $0297
$FE10 PHA
$FE11 LDA #$0
$FE13 STA $0297
$FE16 PLA
$FE17 RTS

ControlKernalMsgs STA $9D
Loc_fe1a LDA $90
SetStatus ORA $90
$FE1E STA $90
$FE20 RTS

SetBusTimeout STA $0285
$FE24 RTS

ReadSetMemTop BCC Funct_fe2d
Funct_fe27 LDX $0283
$FE2A LDY $0284
Funct_fe2d STX $0283
$FE30 STY $0284
$FE33 RTS

Loc_fe34 BCC Loc_fe3c
$FE36 LDX $0281
$FE39 LDY $0282
Loc_fe3c STX $0281
$FE3F STY $0282
$FE42 RTS

; *************************************************
; Function : NMI
;
; Parameters
;
;
; Return Value :
;
; *************************************************

NMI SEI
$FE44 JMP (NMIInterruptVector)

; *************************************************
; Function : NMIHandler
;
; Parameters
;
;
; Return Value :
;
; *************************************************

NMIHandler PHA
$FE48 TXA
$FE49 PHA
$FE4A TYA
$FE4B PHA
$FE4C LDA #$7f
$FE4E STA CIA2_InterruptFlags
$FE51 LDY CIA2_InterruptFlags
$FE54 BMI Loc_fe72
$FE56 JSR CheckForROMat8000
$FE59 BNE Loc_fe5e
$FE5B JMP ($8002)
Loc_fe5e JSR LogPIAKeyReading
$FE61 JSR CallTestStopVector
$FE64 BNE Loc_fe72

; *************************************************
; Function : WarmStart
;
; Parameters
;
;
; Return Value :
;
; *************************************************

WarmStart JSR
ResetAllVectors
$FE69 JSR ResetHardware
$FE6C JSR Funct_e518
$FE6F JMP ($A002)

Loc_fe72 TYA
$FE73 AND $02A1
$FE76 TAX
$FE77 AND #$1
$FE79 BEQ Loc_fea3
$FE7B LDA $DD00
$FE7E AND #$fb
$FE80 ORA $B5
$FE82 STA $DD00
$FE85 LDA $02A1
$FE88 STA CIA2_InterruptFlags
$FE8B TXA
$FE8C AND #$12
$FE8E BEQ Loc_fe9d
$FE90 AND #$2
$FE92 BEQ Loc_fe9a
$FE94 JSR Funct_fed6
$FE97 JMP Loc_fe9d
Loc_fe9a JSR Funct_ff07
Loc_fe9d JSR Funct_eebb
$FEA0 JMP Loc_feb6
Loc_fea3 TXA
$FEA4 AND #$2
$FEA6 BEQ Loc_feae
$FEA8 JSR Funct_fed6
$FEAB JMP Loc_feb6
Loc_feae TXA
$FEAF AND #$10
$FEB1 BEQ Loc_feb6
$FEB3 JSR Funct_ff07
Loc_feb6 LDA $02A1
$FEB9 STA CIA2_InterruptFlags
ExitInterrupt? PLA
$FEBD TAY
$FEBE PLA
$FEBF TAX
$FEC0 PLA
$FEC1 RTI

$FEC2 .DB $c1
$FEC3 .DB $27
$FEC4 .DB $3e
$FEC5 .DB $1a
$FEC6 .DB $c5
$FEC7 .DB $11
$FEC8 .DB $74
$FEC9 .DB $e
$FECA .DB $ed
$FECB .DB $c
$FECC .DB $45
$FECD .DB $6
$FECE .DB $f0
$FECF .DB $2
$FED0 .DB $46
$FED1 .DB $1
$FED2 .DB $b8
$FED3 .DB $0
$FED4 .DB $71
$FED5 .DB $0

Funct_fed6 LDA $DD01
$FED9 AND #$1
$FEDB STA $A7
$FEDD LDA CIA2_TimerB
$FEE0 SBC #$1c
$FEE2 ADC $0299
$FEE5 STA CIA2_TimerB
$FEE8 LDA CIA2_TimerB+1
$FEEB ADC $029A
$FEEE STA CIA2_TimerB+1
$FEF1 LDA #$11
$FEF3 STA CIA2_TimerB_Control
$FEF6 LDA $02A1
$FEF9 STA CIA2_InterruptFlags
$FEFC LDA #$ff
$FEFE STA CIA2_TimerB
$FF01 STA CIA2_TimerB+1
$FF04 JMP Loc_ef59

Funct_ff07 LDA $0295
$FF0A STA CIA2_TimerB
$FF0D LDA $0296
$FF10 STA CIA2_TimerB+1
$FF13 LDA #$11
$FF15 STA CIA2_TimerB_Control
$FF18 LDA #$12
$FF1A EOR $02A1
$FF1D STA $02A1
$FF20 LDA #$ff
$FF22 STA CIA2_TimerB
$FF25 STA CIA2_TimerB+1
$FF28 LDX $0298
$FF2B STX TapeSaveDataByteStartFlag
$FF2D RTS

Funct_ff2e TAX
$FF2F LDA $0296
$FF32 ROL
$FF33 TAY
$FF34 TXA
$FF35 ADC #$c8
$FF37 STA $0299
$FF3A TYA
$FF3B ADC #$0
$FF3D STA $029A
$FF40 RTS

$FF41 .DB $ea
$FF42 .DB $ea
Loc_ff43 PHP
$FF44 PLA
$FF45 AND #$ef
$FF47 PHA

; *************************************************
; Function : IRQ
;
; Parameters
;
;
; Return Value :
;
; *************************************************

IRQ PHA
$FF49 TXA
$FF4A PHA
$FF4B TYA
$FF4C PHA
$FF4D TSX
$FF4E LDA $0104,X
$FF51 AND #$10
$FF53 BEQ Loc_ff58
$FF55 JMP ($0316)
Loc_ff58 JMP (IRQVectorLo)

Funct_ff5b JSR Funct_e518
Loc_ff5e LDA $D012
$FF61 BNE Loc_ff5e
$FF63 LDA $D019
$FF66 AND #$1
$FF68 STA $02A6
$FF6B JMP SetupStandardCia1Time

Loc_ff6e LDA #$81
$FF70 STA CIA1_InterruptFlags
$FF73 LDA CIA1_TimerA_Ctrl
$FF76 AND #$80
$FF78 ORA #$11
$FF7A STA CIA1_TimerA_Ctrl
$FF7D JMP SerialClkOutHi

$FF80 .DB $3 ; Looks like this is just padding

$FF81 JMP Funct_ff5b
$FF84 JMP ResetHardware
$FF87 JMP Funct_fd50
$FF8A JMP ResetAllVectors
$FF8D JMP ReadSetVectoredIO
Funct_ff90 JMP ControlKernalMsgs
$FF93 JMP SerialSendSecondaryAdrAfterListen
$FF96 JMP SerialSendSecondaryAdrAfterTalk
Funct_ff99 JMP ReadSetMemTop
Funct_ff9c JMP Loc_fe34
$FF9F JMP ScanKey
$FFA2 JMP SetBusTimeout
$FFA5 JMP GetSerialByte
$FFA8 JMP Funct_eddd
$FFAB JMP UnTalk
$FFAE JMP UnListen
$FFB1 JMP SendListenCmd
$FFB4 JMP SendTalkCmd
Funct_ffb7 JMP ReadIOStatusWord
Call_SetLFS JMP SetLFS
Call_SetFileName JMP SetFileName
Funct_ffc0 JMP ($031A)
Funct_ffc3 JMP ($031C)
Funct_ffc6 JMP ($031E)
Funct_ffc9 JMP ($0320)
CallRestoreIOVct JMP (RestoreIOVector)
Funct_ffcf JMP ($0324)

; *************************************************
; Function : OutChar
;
; Parameters
;
;
; Return Value :
;
; *************************************************

OutChar JMP (
$0326)
Kernal_Load JMP Loc_f49e
Kernal_Save JMP Loc_f5dd
Loc_ffdb JMP Loc_f6e4
Funct_ffde JMP Loc_f6dd
CallTestStopVector JMP ($0328)
Funct_ffe4 JMP ($032A)
Funct_ffe7 JMP ($032C)
Funct_ffea JMP Loc_f69b
$FFED JMP Loc_e505
Funct_fff0 JMP Loc_e50a
Funct_fff3 JMP Loc_e500

$FFF6 .DB "RRBY"

$FFFA .DW NMI
$FFFC .DW ColdStart
$FFFE .DW IRQ


 Created with the Notable Developments Disassembler.