-
Notifications
You must be signed in to change notification settings - Fork 8
/
sxb-hacker.asm
1902 lines (1652 loc) · 65.5 KB
/
sxb-hacker.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;===============================================================================
; ______ ______ _ _ _
; / ___\ \/ / __ ) | | | | __ _ ___| | _____ _ __
; \___ \\ /| _ \ _____| |_| |/ _` |/ __| |/ / _ \ '__|
; ___) / \| |_) |_____| _ | (_| | (__| < __/ |
; |____/_/\_\____/ |_| |_|\__,_|\___|_|\_\___|_|
;
; A program for Hacking your W65C265SXB or W65C816SXB
;-------------------------------------------------------------------------------
; Copyright (C),2015-2018 Andrew Jacobs
; All rights reserved.
;
; This work is made available under the terms of the Creative Commons
; Attribution-NonCommercial-ShareAlike 4.0 International license. Open the
; following URL to see the details.
;
; http://creativecommons.org/licenses/by-nc-sa/4.0/
;
;===============================================================================
; Notes:
;
; This program provides a simple monitor that you can use to inspect the memory
; in your SXB and reprogram parts of the flash ROM.
;
;-------------------------------------------------------------------------------
pw 132
inclist on
chip 65816
ifdef W65C265SXB
include "w65c265.inc"
else
include "w65c816.inc"
endif
;===============================================================================
;-------------------------------------------------------------------------------
MNEM macro P,Q,R
dw ((((P-'@')<<5)|(Q-'@'))<<5)|(R-'@')
endm
;===============================================================================
; ASCII Character Codes
;-------------------------------------------------------------------------------
SOH equ $01
EOT equ $04
ACK equ $06
BEL equ $07
BS equ $08
LF equ $0a
CR equ $0d
NAK equ $15
CAN equ $18
ESC equ $1b
DEL equ $7f
;===============================================================================
;-------------------------------------------------------------------------------
OP_ADC equ 0<<1
OP_AND equ 1<<1
OP_ASL equ 2<<1
OP_BCC equ 3<<1
OP_BCS equ 4<<1
OP_BEQ equ 5<<1
OP_BIT equ 6<<1
OP_BMI equ 7<<1
OP_BNE equ 8<<1
OP_BPL equ 9<<1
OP_BRA equ 10<<1
OP_BRK equ 11<<1
OP_BRL equ 12<<1
OP_BVC equ 13<<1
OP_BVS equ 14<<1
OP_CLC equ 15<<1
OP_CLD equ 16<<1
OP_CLI equ 17<<1
OP_CLV equ 18<<1
OP_CMP equ 19<<1
OP_COP equ 20<<1
OP_CPX equ 21<<1
OP_CPY equ 22<<1
OP_DEC equ 23<<1
OP_DEX equ 24<<1
OP_DEY equ 25<<1
OP_EOR equ 26<<1
OP_INC equ 27<<1
OP_INX equ 28<<1
OP_INY equ 29<<1
OP_JML equ 30<<1
OP_JMP equ 31<<1
OP_JSL equ 32<<1
OP_JSR equ 33<<1
OP_LDA equ 34<<1
OP_LDX equ 35<<1
OP_LDY equ 36<<1
OP_LSR equ 37<<1
OP_MVN equ 38<<1
OP_MVP equ 39<<1
OP_NOP equ 40<<1
OP_ORA equ 41<<1
OP_PEA equ 42<<1
OP_PEI equ 43<<1
OP_PER equ 44<<1
OP_PHA equ 45<<1
OP_PHB equ 46<<1
OP_PHD equ 47<<1
OP_PHK equ 48<<1
OP_PHP equ 49<<1
OP_PHX equ 50<<1
OP_PHY equ 51<<1
OP_PLA equ 52<<1
OP_PLB equ 53<<1
OP_PLD equ 54<<1
OP_PLP equ 55<<1
OP_PLX equ 56<<1
OP_PLY equ 57<<1
OP_REP equ 58<<1
OP_ROL equ 59<<1
OP_ROR equ 60<<1
OP_RTI equ 61<<1
OP_RTL equ 62<<1
OP_RTS equ 63<<1
OP_SBC equ 64<<1
OP_SEC equ 65<<1
OP_SED equ 66<<1
OP_SEI equ 67<<1
OP_SEP equ 68<<1
OP_STA equ 69<<1
OP_STP equ 70<<1
OP_STX equ 71<<1
OP_STY equ 72<<1
OP_STZ equ 73<<1
OP_TAX equ 74<<1
OP_TAY equ 75<<1
OP_TCD equ 76<<1
OP_TCS equ 77<<1
OP_TDC equ 78<<1
OP_TRB equ 79<<1
OP_TSB equ 80<<1
OP_TSC equ 81<<1
OP_TSX equ 82<<1
OP_TXA equ 83<<1
OP_TXS equ 84<<1
OP_TXY equ 85<<1
OP_TYA equ 86<<1
OP_TYX equ 87<<1
OP_WAI equ 88<<1
OP_WDM equ 89<<1
OP_XBA equ 90<<1
OP_XCE equ 91<<1
MD_ABS equ 0<<1 ; a
MD_ACC equ 1<<1 ; A
MD_ABX equ 2<<1 ; a,x
MD_ABY equ 3<<1 ; a,y
MD_ALG equ 4<<1 ; al
MD_ALX equ 5<<1 ; al,x
MD_AIN equ 6<<1 ; (a)
MD_AIX equ 7<<1 ; (a,x)
MD_DPG equ 8<<1 ; d
MD_STK equ 9<<1 ; d,s
MD_DPX equ 10<<1 ; d,x
MD_DPY equ 11<<1 ; d,x
MD_DIN equ 12<<1 ; (d)
MD_DLI equ 13<<1 ; [d]
MD_SKY equ 14<<1 ; (d,s),y
MD_DIX equ 15<<1 ; (d,x)
MD_DIY equ 16<<1 ; (d),y
MD_DLY equ 17<<1 ; [d],y
MD_IMP equ 18<<1 ;
MD_REL equ 19<<1 ; r
MD_RLG equ 20<<1 ; rl
MD_MOV equ 21<<1 ; xyc
MD_IMM equ 22<<1 ; # (A or M)
MD_INT equ 23<<1 ; # (BRK/COP/WDM)
MD_IMX equ 24<<1 ; # (X or Y)
;===============================================================================
; Data Areas
;-------------------------------------------------------------------------------
page0
org $20
FLAGS ds 1 ; Emulated processor flags
BUFLEN ds 1 ; Command buffer length
BANK ds 1 ; Memory bank
ADDR_S ds 3 ; Start address
ADDR_E ds 3 ; End address
BLOCK ds 1 ; XMODEM block number
RETRIES ds 1 ; Retry count
SUM ds 1 ; Checksum
TEMP ds 4 ; Scratch workspace
data
org $200
BUFFER ds 128 ; Command buffer
;===============================================================================
; Initialisation
;-------------------------------------------------------------------------------
code
public Start
extern UartRx
extern UartTx
extern UartRxTest
extern RomSelect
extern RomCheck
Start:
short_a ; Configure register sizes
long_i
jsr TxCRLF
ldx #TITLE ; Display application title
jsr TxStr
stz BANK ; Reset default bank
;===============================================================================
; Command Processor
;-------------------------------------------------------------------------------
NewCommand:
stz BUFLEN ; Clear the buffer
ShowCommand:
short_i
jsr TxCRLF ; Move to a new line
lda #'.' ; Output the prompt
jsr UartTx
ldx #0
DisplayCmd: cpx BUFLEN ; Any saved characters
beq ReadCommand
lda BUFFER,x ; Yes, display them
jsr UartTx
inx
bra DisplayCmd
RingBell:
lda #BEL ; Make a beep
jsr UartTx
ReadCommand:
jsr UartRx ; Wait for character
cmp #ESC ; Cancel input?
beq NewCommand ; Yes, clear and restart
cmp #CR ; End of command?
beq ProcessCommand ; Yes, start processing
cmp #BS ; Back space?
beq BackSpace
cmp #DEL ; Delete?
beq BackSpace
cmp #' ' ; Printable character
bcc RingBell ; No.
cmp #DEL
bcs RingBell ; No.
sta BUFFER,x ; Save the character
inx
jsr UartTx ; Echo it and repeat
bra ReadCommand
BackSpace:
cpx #0 ; Buffer empty?
beq RingBell ; Yes, beep and continue
dex ; No, remove last character
lda #BS
jsr UartTx
jsr TxSpace
lda #BS
jsr UartTx
bra ReadCommand ; And retry
ProcessCommand:
stx BUFLEN ; Save final length
ldy #0 ; Load index for start
jsr SkipSpaces ; Fetch command character
bcs NewCommand ; None, empty command
;===============================================================================
; B - Select Memory Bank
;-------------------------------------------------------------------------------
cmp #'B' ; Select memory bank?
bne NotMemoryBank
ldx #BANK ; Parse bank
jsr GetByte
bcc $+5
jmp ShowError
jmp NewCommand
NotMemoryBank:
;===============================================================================
; D - Disassemble Memory
;-------------------------------------------------------------------------------
cmp #'D' ; Memory display?
bne NotDisassemble
ldx #ADDR_S ; Parse start address
jsr GetAddr
bcc $+5
jmp ShowError
ldx #ADDR_E ; Parse end address
jsr GetAddr
bcc $+5
jmp ShowError
php
pla
sta FLAGS
Disassemble:
jsr TxCRLF
lda ADDR_S+2 ; Show memory address
jsr TxHex2
lda #':'
jsr UartTx
lda ADDR_S+1
jsr TxHex2
lda ADDR_S+0
jsr TxHex2
jsr TxSpace
jsr TxCodeBytes ; Show code bytes
jsr TxSymbolic ; And instruction
lda [ADDR_S] ; Fetch opcode again
pha
ldy #1
cmp #$18 ; CLC?
bne NotCLC
lda #C_FLAG
bra DoREP
NotCLC:
cmp #$38 ; SEC?
bne NotSEC
lda #C_FLAG
bra DoSEP
NotSEC:
cmp #$c2 ; REP?
bne NotREP
lda [ADDR_S],Y
DoREP: trb FLAGS
bra NextOpcode
NotREP:
cmp #$e2 ; SEP?
bne NextOpcode
lda [ADDR_S],Y
DoSEP: tsb FLAGS
NextOpcode:
pla
jsr OpcodeSize
clc
adc ADDR_S+0 ; And move start address on
sta ADDR_S+0
bcc $+4
inc ADDR_S+1
sec ; Exceeded the end address?
sbc ADDR_E+0
lda ADDR_S+1
sbc ADDR_E+1
bmi Disassemble ; No, show more
jmp NewCommand ; Done
NotDisassemble:
;===============================================================================
; E - Erase ROM bank
;-------------------------------------------------------------------------------
cmp #'E' ; Erase bank?
bne NotEraseBank
jsr CheckSafe
ifdef W65C265SXB
lda BCR ; Save mask rom state
pha
lda #$80 ; Then ensure disabled
tsb BCR
endif
lda #$00 ; Set start address
sta ADDR_S+0
lda #$80
sta ADDR_S+1
EraseLoop:
lda #$aa ; Unlock flash
sta $8000+$5555
lda #$55
sta $8000+$2aaa
lda #$80 ; Signal erase
sta $8000+$5555
lda #$aa
sta $8000+$5555
lda #$55
sta $8000+$2aaa
lda #$30 ; Sector erase
sta (ADDR_S)
EraseWait:
lda (ADDR_S) ; Wait for erase to finish
cmp #$FF
bne EraseWait
clc ; Move to next sector
lda ADDR_S+1
adc #$10
sta ADDR_S+1
bcc EraseLoop ; Repeat until end of memory
ifdef W65C265SXB
pla ; Restore mask ROM state
sta BCR
endif
jmp NewCommand ; And start over
EraseFailed:
long_i ; Warn that erase failed
ldx #ERASE_FAILED
jsr TxStr
longi off
jmp NewCommand ; And start over
NotEraseBank:
;===============================================================================
; F - WDC Mask ROM Enable/Disable
;-------------------------------------------------------------------------------
ifdef W65C265SXB
cmp #'F'
bne NotMaskROM
jsr SkipSpaces ; Find first argument
bcs MaskFail ; Success?
cmp #'0' ; Check bank is 0..3
beq MaskOff
cmp #'1'
beq MaskOn
MaskFail:
jmp ShowError
MaskOn:
lda #$80 ; Enable mask ROM
trb BCR
jmp NewCommand
MaskOff:
lda #$80 ; Disable mask ROM
tsb BCR
jmp NewCommand
NotMaskROM:
endif
;===============================================================================
; G - Goto
;-------------------------------------------------------------------------------
cmp #'G' ; Invoke code
bne NotGoto
ldx #ADDR_S ; Parse execution address
jsr GetAddr
bcs $+5
jmp [ADDR_S] ; Run from address
jmp ($FFFC) ; Otherwise reset
NotGoto:
;===============================================================================
; H - Hunt for RAM
;-------------------------------------------------------------------------------
cmp #'H' ; Hunt for RAM
beq $+5
jmp NotHunt
stz ADDR_S+0 ; Start at $00:0000
stz ADDR_S+1
stz ADDR_S+2
HuntStart:
lda [ADDR_S] ; Is byte is writeable?
pha
eor #$ff
sta [ADDR_S]
cmp [ADDR_S]
beq HuntFound ; Yes
pla
clc ; Try the next block
lda ADDR_S+1
adc #$10
sta ADDR_S+1
bcc HuntStart
inc ADDR_S+2
bne HuntStart
jmp NewCommand ; Reached end of RAM
HuntFound:
jsr TxCRLF
lda ADDR_S+2 ; Print start address
jsr TxHex2
lda #':'
jsr UartTx
lda ADDR_S+1
jsr TxHex2
lda ADDR_S+0
jsr TxHex2
lda #'-'
jsr UartTx
HuntEnd:
pla ; Restore memory bytes
sta [ADDR_S]
clc ; Try the next block
lda ADDR_S+1
adc #$10
sta ADDR_S+1
bcc HuntNext
inc ADDR_S+2
beq HuntDone
HuntNext
lda [ADDR_S] ; Is byte is writeable?
pha
eor #$ff
sta [ADDR_S]
cmp [ADDR_S]
beq HuntEnd ; Yes, keep looking
pla
sec ; Print end address
lda ADDR_S+0
sbc #1
pha
lda ADDR_S+1
sbc #0
pha
lda ADDR_S+2
sbc #0
jsr TxHex2
lda #':'
jsr UartTx
pla
jsr TxHex2
pla
jsr TxHex2
bra HuntStart
HuntDone:
lda #$ff ; Pring FF:FFFF
pha
pha
jsr TxHex2
lda #':'
jsr UartTx
pla
jsr TxHex2
pla
jsr TxHex2
jmp NewCommand
NotHunt:
;===============================================================================
; M - Display Memory
;-------------------------------------------------------------------------------
cmp #'M' ; Memory display?
bne NotMemoryDisplay
ldx #ADDR_S ; Parse start address
jsr GetAddr
bcc $+5
jmp ShowError
ldx #ADDR_E ; Parse end address
jsr GetAddr
bcc $+5
jmp ShowError
DisplayMemory:
jsr TxCRLF
lda ADDR_S+2 ; Show memory address
jsr TxHex2
lda #':'
jsr UartTx
lda ADDR_S+1
jsr TxHex2
lda ADDR_S+0
jsr TxHex2
ldy #0 ; Show sixteen bytes of data
ByteLoop: jsr TxSpace
lda [ADDR_S],y
jsr TxHex2
iny
cpy #16
bne ByteLoop
jsr TxSpace
lda #'|'
jsr UartTx
ldy #0 ; Show sixteen characters
CharLoop: lda [ADDR_S],Y
jsr IsPrintable
bcs $+4
lda #'.'
jsr UartTx
iny
cpy #16
bne CharLoop
lda #'|'
jsr UartTx
clc ; Bump the display address
tya
adc ADDR_S+0
sta ADDR_S+0
bcc $+4
inc ADDR_S+1
sec ; Exceeded the end address?
sbc ADDR_E+0
lda ADDR_S+1
sbc ADDR_E+1
bmi DisplayMemory ; No, show more
jmp NewCommand
NotMemoryDisplay:
;===============================================================================
; R - Select ROM Bank
;-------------------------------------------------------------------------------
cmp #'R' ; ROM Bank?
bne NotROMBank ; No
jsr SkipSpaces ; Find first argument
bcc $+5 ; Success?
BankFail: jmp ShowError ; No
cmp #'0' ; Check bank is 0..3
bcc BankFail
cmp #'3'+1
bcs BankFail
jsr RomSelect ; Switch ROM banks
jmp NewCommand ; Done
NotROMBank:
;===============================================================================
; S - S19 Record
;-------------------------------------------------------------------------------
cmp #'S' ; S19?
beq $+5
jmp NotS19
jsr NextChar ; Get record type
bcs S19Fail
cmp #'1' ; Only process type 1
bne S19Done
ldx #ADDR_E ; Get byte count
jsr GetByte
bcs S19Fail
lda ADDR_E ; Use as initial checksum
sta SUM
; Byte count not included in the S19 byte count field
beq S19Fail
ldx #ADDR_S ; Get address
jsr GetAddr
bcs S19Fail
lda ADDR_S+0 ; Add to checksum
adc ADDR_S+1
clc
adc SUM
sta SUM
dec ADDR_E
beq S19Fail
dec ADDR_E
beq S19Fail
S19Load:
ldx #TEMP ; Fetch a data byte
jsr GetByte
bcs S19Fail
lda TEMP
adc SUM
sta SUM
dec ADDR_E
beq S19Fail
lda ADDR_S+2 ; Writing to ROM?
bne WriteS19 ; No
lda ADDR_S+1
bpl WriteS19 ; No
ifdef W65C265SXB
cmp #$df ; Register page?
beq NoWrite
endif
lda #$aa ; Yes, unlock flash
sta $8000+$5555
lda #$55
sta $8000+$2aaa
lda #$a0 ; Start byte write
sta $8000+$5555
WriteS19:
lda TEMP ; Write the value
sta [ADDR_S]
NoWrite:
inc ADDR_S+0 ; Bump address by one
bne $+4
inc ADDR_S+1
lda ADDR_E ; Reached checksum?
cmp #1
bne S19Load
ldx #TEMP ; Yes, read it
jsr GetByte
bcs S19Fail
lda TEMP
adc SUM
cmp #$ff ; Checksum correct?
bne S19Fail
S19Done: jmp NewCommand ; Get
S19Fail:
long_i ; Display error message
ldx #INVALID_S19
jsr TxStr
longi off
jmp NewCommand ; And start over
NotS19:
;===============================================================================
; W - Write memory
;-------------------------------------------------------------------------------
cmp #'W' ; Write memory?
bne NotWrite
ldx #ADDR_S ; Parse start address
jsr GetAddr
bcc $+5
jmp ShowError
bit ADDR_S+1 ; Load into ROM area?
bpl $+5
jsr CheckSafe ; Yes, check selection
ldx #ADDR_E ; Parse value byte
jsr GetByte ; Is there a value?
bcc $+5
jmp NewCommand ; No.
lda ADDR_S+2 ; Writing to ROM?
bne WriteMemory ; No
bit ADDR_S+1
bpl WriteMemory ; No
lda #$aa ; Yes, unlock flash
sta $8000+$5555
lda #$55
sta $8000+$2aaa
lda #$a0 ; Start byte write
sta $8000+$5555
WriteMemory:
lda ADDR_E ; Write the value
sta [ADDR_S]
inc ADDR_S+0 ; Bump address by one
bne $+4
inc ADDR_S+1
lda #'W' ; Build command for next byte
jsr StartCommand
lda #' '
jsr BuildCommand
lda ADDR_S+1 ; Add the next address
jsr BuildByte
lda ADDR_S+0
jsr BuildByte
lda #' '
jsr BuildCommand
jmp ShowCommand ; And prompt for data
NotWrite:
;===============================================================================
; X - XMODEM Receive
;-------------------------------------------------------------------------------
cmp #'X' ; XModem upload?
beq $+5 ; Yes.
jmp NotXModem
ldx #ADDR_S ; Parse start address
jsr GetAddr
bcc $+5
jmp ShowError
bit ADDR_S+1 ; Load into ROM area?
bpl $+5
jsr CheckSafe ; Yes, check selection
long_i ; Display waiting message
ldx #WAITING
jsr TxStr
jsr TxCRLF
short_i
stz BLOCK ; Reset the block number
inc BLOCK
ResetRetries:
lda #10 ; Reset the retry counter
sta RETRIES
TransferWait:
stz TEMP+0 ; Clear timeout counter
stz TEMP+1
lda #-20
sta TEMP+2
TransferPoll:
jsr UartRxTest ; Any data yet?
bcs TransferScan
inc TEMP+0
bne TransferPoll
inc TEMP+1
bne TransferPoll
inc TEMP+2
bne TransferPoll
dec RETRIES
beq TimedOut
jsr SendNAK ; Send a NAK
bra TransferWait
TimedOut:
long_i
ldx #TIMEOUT
jsr TxStr
longi off
jmp NewCommand
TransferScan:
jsr UartRx ; Wait for SOH or EOT
cmp #EOT
beq TransferDone
cmp #SOH
bne TransferWait
jsr UartRx ; Check the block number
cmp BLOCK
bne TransferError
jsr UartRx ; Check inverted block
eor #$ff
cmp BLOCK
bne TransferError
ldy #0
sty SUM ; Clear the check sum
TransferBlock:
jsr UartRx
pha
lda ADDR_S+2 ; Writing to ROM?
bne WriteByte ; No
lda ADDR_S+1
bpl WriteByte ; No
ifdef W65C265SXB
cmp #$df ; Register page?
beq WriteSkip
endif
lda #$aa ; Yes, unlock flash
sta $8000+$5555
lda #$55
sta $8000+$2aaa
lda #$a0 ; Start byte write
sta $8000+$5555
WriteByte:
pla
sta [ADDR_S],Y
WriteWait:
cmp [ADDR_S],Y ; Wait for write
bne WriteWait
bra $+3
WriteSkip:
pla
clc ; Add to check sum
adc SUM
sta SUM
iny
cpy #128
bne TransferBlock
jsr UartRx ; Check the check sum
cmp SUM
bne TransferError ; Failed
clc
tya
adc ADDR_S+0 ; Bump address one block
sta ADDR_S+0
bcc $+4
inc ADDR_S+1
jsr SendACK ; Acknowledge block
inc BLOCK ; Bump block number
jmp TransferWait
TransferError;
jsr SendNAK ; Send a NAK
jmp TransferWait ; And try again
TransferDone:
jsr SendACK ; Acknowledge transmission
jmp NewCommand ; Done
SendACK:
lda #ACK
jmp UartTx
SendNAK:
lda #NAK
jmp UartTx
NotXModem:
;===============================================================================
; ? - Help
;-------------------------------------------------------------------------------
cmp #'?' ; Help command?
bne NotHelp
long_i
ldx #HELP ; Output help string
jsr TxStr
longi off
jmp NewCommand
NotHelp:
;-------------------------------------------------------------------------------
ShowError:
long_i
ldx #ERROR ; Output error message
jsr TxStr
longi off
jmp NewCommand
;===============================================================================
;-------------------------------------------------------------------------------
; Checks if an expendable ROM bank is currently selected. If the bank with the
; WDC firmware is selected then warn and accept a new command.
CheckSafe:
jsr RomCheck ; WDC ROM selected?
beq $+3
rts ; No, save to change
pla ; Discard return address
pla
long_i ; Complain about bank
ldx #NOT_SAFE
jsr TxStr
longi off
jmp NewCommand ; And start over