forked from dhansel/smon6502
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmon.asm
1942 lines (1830 loc) · 56.5 KB
/
smon.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
;;; The SMON machine language monitor was originally published
;;; in the November/December/January 1984/85 issues of German magazine "64er":
;;; https://archive.org/details/64er_1984_11/page/n59/mode/2up
;;; https://archive.org/details/64er_1984_12/page/n59/mode/2up
;;; https://archive.org/details/64er_1985_01/page/n68/mode/2up
;;; SMON was written for the Commodore 64 by Norfried Mann and Dietrich Weineck
;;;
;;; For an English description of SMON capabilities see:
;;; https://www.c64-wiki.com/wiki/SMON
;;; The following original SMON commands are NOT included in this version:
;;; B (BASIC data), L (disk load), S (disk save), P (printer), I (set I/O device)
;;; The following commands were added in this version:
;;; H - print help screen
;;; L - load Intel HEX data through terminal
;;; MS - check and print memory (RAM) size
;;; MT xxxx yyyy nn - test memory (RAM) xxxx-yyyy nn times (default 1)
;;;
;;; This code is an adaptation of SMON to a minimal 6502 system by David Hansel (2023).
;;; Minimum system requirements:
;;; - MOS 6502 CPU
;;; - MOS 6522 VIA (necessary only if "trace" functions are used)
;;; The VIA interrupt output must be attached to the 6502 IRQ input
;;; - 8K of ROM at address E000-F000 (for SMON)
;;; - 4K of RAM at address 0000-1000
;;; - UART for communication. As presented here, a MC6850 UART
;;; at address $8200 is expected. However, this can easily
;;; be adapted by modifying the code in file "uart.asm"
;;;
;;; This code is based on the SMON disassembly found at:
;;; https://github.com/cbmuser/smon-reassembly
.include "config.asm"
PCHSAVE = $02A8 ; PC hi
PCLSAVE = $02A9 ; PC lo
SRSAVE = $02AA ; SR
AKSAVE = $02AB ; A
XRSAVE = $02AC ; XR
YSAVE = $02AD ; YR
SPSAVE = $02AE ; SP
IRQ_LO = $0314 ; Vector: Hardware IRQ Interrupt Address Lo
IRQ_HI = $0315 ; Vector: Hardware IRQ Interrupt Address Hi
BRK_LO = $0316 ; Vector: BRK Lo
BRK_HI = $0317 ; Vector: BRK Hi
CHRIN = $FECF ; Kernal input routine
CHROUT = $FED2 ; Kernal output routine
STOP = $FEE1 ; Kernal test STOP routine
GETIN = $FEE4 ; Kernal get input routine
.org $8000
.org $e000
ENTRY: lda #<SMON ; set break-vector to program start
sta BRK_LO
lda #>SMON
sta BRK_HI
brk
;; help message
HLPMSG: .byte "A xxxx - Assemble starting at x (end assembly with 'f', use Mxx for label)",0
.byte "C xxxx yyyy zzzz aaaa bbbb - Convert (execute V followed by W)",0
.byte "D xxxx (yyyy) - Disassemble from x (to y)",0
.byte "F aa bb cc ..., xxxxx yyyyy - Find byte sequence a b c in x-y",0
.byte "FAaaaa, xxxx yyyy - Find absolute address used in opcode",0
.byte "FRaaaa, xxxx yyyy - Find relative address used in opcode",0
.byte "FTxxxx yyyy - Find table (non-opcode bytes) in x-y",0
.byte "FZaa, xxxx yyyy - Find zero-page address used in opcode",0
.byte "FIaa, xxxx yyyy - Find immediate argument used in opcode",0
.byte "G (xxxx) - Run from x (or current PC)",0
.byte "K xxxx (yyyy) - Dump memory from x (to y) as ASCII",0
.byte "L - Load Intel HEX data from terminal",0
.byte "M xxxx (yyyy) - Dump memory from x (to y) as HEX",0
.byte "MS - Check and print memory size",0
.byte "MT xxxx yyyy (nn) - Test memory x-y (repeat n times)",0
.byte "O xxxx yyyy aa - Fill memory x-y with a",0
.if VIA > 0
.byte "TW xxxx - Trace walk (single step)",0
.byte "TB xxxx nn - Trace break (set break point at x, stop when hit n times)",0
.byte "TQ xxxx - Trace quick (run to break point)",0
.byte "TS xxxx - Trace stop (run to x)",0
.endif
.byte "V xxxx yyyy zzzz aaaa bbbb - Within a-b, convert addresses referencing x-y to z",0
.byte "W xxxx yyyy zzzz - Copy memory x-y to z",0
.byte "= xxxx yyyy - compare memory starting at x to memory starting at y",0
.byte "#ddd - convert DEC to HEX and BIN",0
.byte "$xx - convert HEX to DEC and BIN",0
.byte "%bbbbbbbb - convert BIN to DEC and HEX",0
.byte 0
;; commands
ICMD: .byte "'#$%,:;=?ACDFGHKLMORTVW"
ICMDE: .byte $00,$00,$00,$00,$00
;; command entry point addresses
IOFS: .byte <(TICK-1),>(TICK-1) ; '
.byte <(BEFDEC-1),>(BEFDEC-1) ; #
.byte <(BEFHEX-1),>(BEFHEX-1) ; $
.byte <(BEFBIN-1),>(BEFBIN-1) ; %
.byte <(COMMA-1),>(COMMA-1) ; ,
.byte <(COLON-1),>(COLON-1) ; :
.byte <(SEMI-1),>(SEMI-1) ; ;
.byte <(EQUALS-1),>(EQUALS-1) ; =
.byte <(ADDSUB-1),>(ADDSUB-1) ; ?
.byte <(ASSEMBLER-1),>(ASSEMBLER-1) ; A
.byte <(CONVERT-1),>(CONVERT-1) ; C
.byte <(DISASS-1),>(DISASS-1) ; D
.byte <(FIND-1),>(FIND-1) ; F
.byte <(GO-1),>(GO-1) ; G
.byte <(HELP-1),>(HELP-1) ; H
.byte <(KONTROLLE-1),>(KONTROLLE-1) ; K
.byte <(LOAD-1),>(LOAD-1) ; L
.byte <(MEMDUMP-1),>(MEMDUMP-1) ; M
.byte <(OCCUPY-1),>(OCCUPY-1) ; O
.byte <(REGISTER-1),>(REGISTER-1) ; R
.byte <(TRACE-1),>(TRACE-1) ; T
.byte <(MOVE-1),>(MOVE-1) ; V
.byte <(WRITE-1),>(WRITE-1) ; W
;; output line start characters
LC061: .byte "':;,()!"
.byte $00,$00,$00
LC06B: .byte $FF,$FF,$01,$00
;; sub-commands for "find" (F)
FSCMD: .byte "AZIRT"
;; "find" sub-command
LC074: .byte $80,$20,$40,$10,$00
;; "find" sub-command data length (2=word,1=byte,0=none)
LC079: .byte $02,$01,$01,$02,$00
REGHDR: .byte $0D,$0D," PC SR AC XR YR SP NV-BDIZC",$00
LC0AD: .byte $02,$04,$01
LC0B0: .byte $2C,$00,$2C
LC0B3: .byte $59,$29,$58
LC0B6: .byte $9D,$1F,$FF,$1C,$1C,$1F,$1F
.byte $1F,$1C,$DF,$1C,$1F,$DF,$FF,$FF
.byte $03,$1F
LC0C7: .byte $80,$09,$20,$0C,$04,$10,$01
.byte $11,$14,$96,$1C,$19,$94,$BE,$6C
.byte $03,$13,$01
LC0D9: .byte $02,$02,$03,$03,$02,$02,$02
.byte $02,$02,$02,$03,$03,$02,$03,$03
.byte $03,$02,$00
LC0EB: .byte $40,$40,$80,$80,$20,$10,$25
.byte $26,$21,$22,$81,$82,$21,$82,$84
.byte $08,$08
LC0FC: .byte $E7,$E7,$E7,$E7,$E3,$E3,$E3
.byte $E3,$E3,$E3,$E3,$E3,$E3,$E3,$E7
.byte $A7,$E7,$E7,$F3,$F3,$F7,$DF
;; opcodes (in same order as mnemonics below)
OPC: .byte $26,$46,$06,$66,$41,$81,$E1
.byte $01,$A0,$A2,$A1,$C1,$21,$61,$84
.byte $86,$E6,$C6,$E0,$C0,$24,$4C,$20
.byte $90,$B0,$F0,$30,$D0,$10,$50,$70
.byte $78,$00,$18,$D8,$58,$B8,$CA,$88
.byte $E8,$C8,$EA,$48
LC13D: .byte $08,$68,$28,$40,$60,$AA,$A8
.byte $BA,$8A,$9A,$98,$38,$F8
LC14A: .byte $89,$9C,$9E,$B2
LC14E: .byte $2A,$4A,$0A,$6A,$4F,$23,$93
.byte $B3,$F3,$33,$D3,$13,$53,$73
;; first, second and third characters of opcode mnemonics
OPMN1: .byte "RLARESSOLLLCAASSIDCCBJJBBBBBBBBSBCCCCDDIINPPPPRRTTTTTTSS"
OPMN2: .byte "OSSOOTBRDDDMNDTTNEPPIMSCCEMNPVVERLLLLEENNOHHLLTTAASXXYEE"
OPMN3: .byte "LRLRRACAYXAPDCYXCCXYTPRCSQIELCSIKCDIVXYXYPAPAPISXYXASACD"
LC204: .byte $08,$84,$81,$22,$21,$26,$20,$80
LC20C: .byte $03,$20,$1C,$14,$14,$10,$04,$0C
; SMON START
SMON: cld
ldx #$05
LC22E: pla
sta PCHSAVE,x ; save stack
dex
bpl LC22E
lda PCLSAVE
bne LC23D
dec PCHSAVE ; PC high
LC23D: dec PCLSAVE ; PC low
tsx
stx SPSAVE
lda #'R' ; execute 'R' command
jmp LC2FF ; jump to main loop
LC249: jsr PEEKCH
beq LC259
LC24E: jsr GETWRD
sta PCLSAVE
lda $FC
sta PCHSAVE
LC259: rts
;; get 3 words into $A4-$A9
LC25A: ldx #$A4
jsr GETWRDX
jsr GETWRDX
bne GETWRDX
;; get start (FB/FC) and end (FD/FE) address from command line
;; end address is optional, defaults to $FFFE
GETSE: jsr GETWRD ; get word from command line
lda #$FE
sta $FD
lda #$FF
sta $FE
jsr PEEKCH ; is there more command line input?
bne GETWRDX ; yes, get another word
sta $0277 ; put NUL into keyboard buffer
inc $C6
rts
;; get two words from command line, store in $FB/$FC and $FD/$FE
GETDW: jsr GETWRD
.byte $2C ; skip next (2-byte) opcode
;; get word from command line, store in $FB/$FC
GETWRD: ldx #$FB
;; get word from command line, store in (X)/(X+1)
GETWRDX:jsr LC28D
sta $01,x
jsr GETBYT
sta $00,x
inx
inx
rts
;; get byte from command line, ignore leading " " and ","
LC28D: jsr GETCHR
cmp #$20
beq LC28D
cmp #$2C
beq LC28D
bne LC29D
;; get byte from command line, return in A
GETBYT: jsr GETCHR ; get character
LC29D: jsr LC2AF ; convert to 0-15
asl
asl
asl
asl
sta $B4
jsr GETCHR ; get character
jsr LC2AF ; convert to 0-15
ora $B4
rts
;; convert character in A from ASCII HEX to 0-15
LC2AF: cmp #$3A
bcc LC2B5
adc #$08
LC2B5: and #$0F
rts
;; skip spaces from command line
LC2B8: jsr GETCHR
cmp #$20
beq LC2B8
dec $D3
rts
;; peek whether next character on command line is CR (Z set if so)
PEEKCH: jsr CHRIN
dec $D3
cmp #$0D
LC2C9: rts
;; convert character in A to uppercase
UCASE: cmp #'a'
bcc UCASE1
cmp #'z'+1
bcs UCASE1
and #$DF
UCASE1: rts
;; get next character from command line, error if CR (end of line)
GETCHR: jsr CHRIN
jsr UCASE
GETCL1: cmp #$0D
bne LC2C9
; invalid input
ERROR: lda #$3F ; print "?"
jsr CHROUT
;; main loop
LC2D6: ldx SPSAVE
txs
ldx #$00 ; clear keyboard buffer
stx $C6
lda $D3 ; get cursor column
beq SKIPCR ; jump if zero
jsr LC351 ; print CR
SKIPCR: lda ($D1,x) ; get first character of next line
ldx #$06 ; compare to known line start characters: ':;,()
LC2E5: cmp LC061,x
beq LC2F2 ; skip prompt if found
dex
bpl LC2E5
lda #$2E ; print prompt (".")
jsr CHROUT
LC2F2: jsr GETCHR ; await next input
cmp #$2E
beq LC2F2 ; ignore leading "."
jmp LC2FF
LC2FC: jmp ERROR
;; find user command in A
LC2FF: sta $AC
and #$7F
ldx #ICMDE-ICMD
FNDCMD: cmp ICMD-1,x ; compare command char
beq LC30F ; found valid command
dex ; next command char
bne FNDCMD ; loop until we checked all command chars
beq LC2FC ; no match => error
LC30F: jsr LC315 ; execute command
jmp LC2D6 ; back to main loop
;; execute command specified by index in X
LC315: txa ; X = X*2+1
asl
tax
inx
lda IOFS-2,x ; low address
pha ; on stack
dex
lda IOFS-2,x ; high address
pha ; on stack
rts ; execute command
;; output data word in FB/FC
LC323: lda $FC
jsr LC32A
lda $FB
;; output data byte in A as HEX
LC32A: pha
lsr
lsr
lsr
lsr
jsr LC335
pla
and #$0F
LC335: cmp #$0A
bcc LC33B
adc #$06
LC33B: adc #$30
jmp CHROUT
;; output CR, followed by character in X
LC340: lda #$0D
LC342: jsr CHROUT
txa
jmp CHROUT
;; output two SPACE
LC349: jsr LC34C
;; output SPACE
LC34C: lda #$20
jmp CHROUT
;; output CR
LC351: lda #$0D
jmp CHROUT
;; print 0-terminated string pointed to by A/Y
STROUT: sta $BB
sty $BC
ldy #$00
STROUT1:lda ($BB),y
beq LC366
jsr CHROUT
inc $BB
bne STROUT1
inc $BC
bne STROUT1
LC366: rts
;; increment address in $FB/$FC
LC367: inc $FB
bne LC36D
inc $FC
LC36D: rts
;; HELP (H)
HELP: lda #<HLPMSG ; get help message start addr
sta $BB ; into $BB/$BC
lda #>HLPMSG
sta $BC
ldy #$00
HLPL1: lda #$0d ; output CR
jsr CHROUT
jsr STROUT1 ; output string until 0
iny ; next byte
cpy #20 ; are we at line 20?
bne HLPL2 ; jump if not
lda #' ' ; put SPACE in keyboard buffer
sta $0277 ; (to pause output)
inc $C6
HLPL2: jsr LC472 ; check for PAUSE,STOP
lda ($BB),y ; get first byte of next string
bne HLPL1 ; loop if not 0
rts
;; REGISTER (R)
REGISTER:
ldy #>REGHDR
lda #<REGHDR
jsr STROUT
ldx #$3B
jsr LC340
lda PCHSAVE
sta $FC
lda PCLSAVE
sta $FB
jsr LC323
jsr LC34C
ldx #$FB
LC3A4: lda $01AF,x
jsr LC32A
jsr LC34C
inx
bne LC3A4
lda SRSAVE
jmp LC3D0
;; Semicolon (; - edit register)
SEMI: jsr LC24E
ldx #$FB
LC3BB: jsr GETCHR
jsr GETBYT
sta $01AF,x
inx
bne LC3BB
jsr LC34C
lda SRSAVE,x
jmp LC3D0
LC3D0: sta $AA
lda #$20
ldy #$09
LC3D6: jsr CHROUT
asl $AA
lda #$30
adc #$00
dey
bne LC3D6
rts
;; GO (G)
GO: jsr LC249
ldx SPSAVE
txs
ldx #$FA
LC3EC: lda $01AE,x
pha
inx
bne LC3EC
pla
tay
pla
tax
pla
rti
;; LOAD (L)
LOAD: lda #13
jsr CHROUT
LDNXT: jsr UAGETW ; get character from UART
cmp #' '
beq LDNXT ; ignore space at beginning of line
cmp #13
beq LDNXT ; ignore CR at beginning of line
cmp #10
beq LDNXT ; ignore LF at beginning of line
cmp #27
beq LDBRK ; stop when receiving BREAK
cmp #3
beq LDBRK ; stop when receiving CTRL-C
cmp #':' ; expect ":" at beginning of line
bne LDEIC
jsr LDBYT ; get record byte count
tax
jsr LDBYT ; get address high byte
sta $FC
jsr LDBYT ; get address low byte
sta $FB
jsr LDBYT ; get record type
beq LDDR ; jump if data record (record type 0)
cmp #1 ; end-of-file record (record type 1)
bne LDERI ; neither a data nor eof record => error
;; read Intel HEX end-of-file record
jsr LDBYT ; get next byte (should be checksum)
cmp #$FF ; checksum of EOF record is FF
bne LDECS ; error if not
LDEOF: rts
;; read Intel HEX data record
LDDR: clc ; prepare checksum
txa ; byte count
adc $FB ; address low
clc
adc $FC ; address high
sta $FD ; store checksum
ldy #0 ; offset
inx
LDDR1: dex ; decrement number of bytes
beq LDDR2 ; done if 0
jsr LDBYT ; get next data byte
sta ($FB),y ; store data byte
cmp ($FB),y ; check data byte
bne LDEM ; memory error if no match
clc
adc $FD ; add to checksum
sta $FD ; store checksum
iny
bne LDDR1
LDDR2: jsr LDBYT ; get checksum byte
clc
adc $FD ; add to computed checkum
bne LDECS ; if sum is 0 then checksum is ok
lda #'+'
jsr UAPUTW
inc $D3
cpy #0 ; did we have 0 bytes in this record?
bne LDNXT ; if not then expect another record
beq LDEOF ; end of file
LDBRK: lda #'B' ; received BREAK (ESC)
.byte $2C
LDERI: lda #'R' ; unknown record identifier error
.byte $2C
LDECS: lda #'C' ; checksum error
.byte $2C
LDEIC: lda #'I' ; input character error
.byte $2C
LDEM: lda #'M' ; memory error
jsr CHROUT
LDERR: jmp ERROR
;; get HEX byte from UART
LDBYT: jsr LDNIB ; get high nibble
asl
asl
asl
asl
sta $B4
jsr LDNIB ; get low libble
ora $B4 ; combine
rts
;; get HEX character from UART, convert to 0-15
LDNIB: jsr UAGETW ; get character from UART
jsr UCASE ; convert to uppercase
cmp #'0'
bcc LDEIC
cmp #'F'+1
bcs LDEIC
cmp #'9'+1
bcc LDBYT2
cmp #'A'
bcc LDEIC
adc #$08
LDBYT2: and #$0F
rts
;; MEMDUMP (M)
MEMDUMP:jsr GETCHR
beq MDERR ; error if CR
cmp #'T' ; is it 'T'
bne MD1 ; go to memory dump
jmp MEMTST
MD1: cmp #'S'
bne MD2
jmp MEMSIZ
MD2: cmp #' '
bne MDERR
MD3: jsr GETSE ; get start (FB/FC) and end address (FD/FE)
LC3FC: ldx #$3A ; ':'
jsr LC340 ; print NEWLINE followed by ':'
jsr LC323 ; print address in FB/FC
ldy #80-17
ldx #0
LC408: jsr LC34C ; output space
cpy #80-9
bne LC409
jsr LC34C ; output space
iny
LC409: lda ($FB,x)
jsr LC32A ; output byte as HEX
lda ($FB,x)
jsr LC439 ; write ASCII char of byte directly to screen
bne LC408 ; repeat until end-of-line
jsr PREOL ; print to end of line
jsr LC45D ; check for PAUSE/STOP or end condition
bcc LC3FC ; repeat until end
rts
;; Colon (: - edit memory dump)
COLON: jsr GETWRD ; get start addressz
ldy #80-17
ldx #$00
LC424: cpy #80-9
bne LC434
iny
LC434: jsr CHRIN ; get next character
cmp #$20 ; was it space?
beq LC424 ; skip space
cmp #$0d ; was it CR?
beq COLOD ; done if so
dec $D3 ; go back one input char
jsr GETBYT ; get hex byte
sta ($FB,x) ; store byte
cmp ($FB,x) ; compare (make sure it's not ROM)
beq LC433 ; if match then skip
MDERR: jmp ERROR ; print error
LC433: jsr LC439 ; write ASCII to screen
bne LC424 ; repeat until end
lda #80-17
jsr PRLINE
COLOD: rts
;; put character into screen buffer at column Y
;; (make sure it is printable first)
LC439: cmp #$20 ; character code < 32 (space)
bcc LC449 ; if so, print "."
cmp #$7F ; character code >= 127
bcs LC449 ; if so, print "."
bcc LC44F ; print character
LC449: lda #$2E
LC44F: sta ($D1),y
lda $0286
sta ($F3),y
LC456: jsr LC367
iny
cpy #80
rts
;; check stop/pause condition, return with carry set
;; if end address reached
LC45D: jsr LC472 ; end-of-line wait handling
jmp LC466 ; check for end address and return
;; increment address in $FB/$FC and check whether end address
;; ($FD/$FE) has been reached, if not, C is clear on return
LC463: jsr LC367 ; increment address in $FB/$FC
;; check whether end address has been reached, C is clear if not
LC466: lda $FB
cmp $FD
lda $FC
sbc $FE
rts
;; end-of-line wait handling:
;; - stop if STOP key pressed
;; - wait if any key pressed
;; - if SPACE pressed, immediately stop again after next line
LC472: jsr LC486 ; check for STOP or keypress
beq LC485 ; no key => done
LC477: jsr LC486 ; check for STOP or keypress
beq LC477 ; no key => wait
cmp #$20 ; is SPACE?
bne LC485 ; no => done
sta $0277 ; put SPACE in keyboard buffer
inc $C6 ; (i.e. advance just one line)
LC485: rts
;; check for STOP or other keypress
LC486: jsr GETIN ; get input character
pha ; save char
jsr STOP ; check stop key
beq LC491 ; jump if pressed
pla ; restore char to A
LC490: rts
LC491: jmp LC2D6 ; back to main loop (resets stack)
;;
LC4CB: ldy #$00
lda ($FB),y ; get opcode at $FB/FC
bit $AA
bmi LC4D5
bvc LC4E1
LC4D5: ldx #$1F
LC4D7: cmp LC13D-1,x
beq LC50B
dex
cpx #$15
bne LC4D7
LC4E1: ldx #$04
LC4E3: cmp LC14A-1,x
beq LC509
cmp LC14E-1,x
beq LC50B
dex
bne LC4E3
ldx #$38
LC4F2: cmp OPC-1,x
beq LC50B
dex
cpx #$16
bne LC4F2
LC4FC: lda ($FB),y
and LC0FC-1,x
eor OPC-1,x
beq LC50B
dex
bne LC4FC
LC509: ldx #$00
LC50B: stx $AD
txa
beq LC51F
ldx #$11
LC512: lda ($FB),y
and LC0B6-1,x
eor LC0C7-1,x
beq LC51F
dex
bne LC512
LC51F: lda LC0EB-1,x
sta $AB
lda LC0D9-1,x
sta $B6
ldx $AD
rts
LC52C: ldy #$01
lda ($FB),y
tax
iny
lda ($FB),y
ldy #$10
cpy $AB
bne LC541
jsr LC54A
ldy #$03
bne LC543
LC541: ldy $B6
LC543: stx $AE
nop
sta $AF
nop
rts
LC54A: ldy #$01
lda ($FB),y
bpl LC551
dey
LC551: sec
adc $FB
tax
inx
beq LC559
dey
LC559: tya
adc $FC
LC55C: rts
; DISASS (D)
DISASS: ldx #$00
stx $AA
jsr GETSE ; get start (FB/FC) and end address (FD/FE)
LC564: jsr LC58C
lda $AD
cmp #$16
beq LC576
cmp #$30
beq LC576
cmp #$21
bne LC586
nop
LC576: jsr LC351
ldx #$23
lda #$2D
LC580: jsr CHROUT
dex
bne LC580
LC586: jsr LC45D
bcc LC564
rts
LC58C: ldx #$2C ; output NEWLINE followed by ","
jsr LC340
jsr LC323 ; output FB/FC (address)
jsr LC34C ; output SPACE
LC597: jsr LC675 ; erase to end of line
jsr LC4CB
jsr LC34C ; output SPACE
LC5A0: lda ($FB),y ; get data byte
jsr LC32A ; output byte in A
jsr LC34C ; output SPACE
iny
cpy $B6
bne LC5A0
lda #$03
sec
sbc $B6
tax
beq LC5BE
LC5B5: jsr LC349
jsr LC34C
dex
bne LC5B5
LC5BE: lda #$20
jsr CHROUT
ldy #$00
ldx $AD
LC5C7: bne LC5DA
ldx #$03
LC5CB: lda #$2A
jsr CHROUT
dex
bne LC5CB
bit $AA
bmi LC55C
jmp LC66A
LC5DA: bit $AA
bvc LC607
lda #$08
bit $AB
beq LC607
lda ($FB),y
and #$FC
sta $AD
iny
lda ($FB),y
asl
tay
lda $033C,y
sta $AE
nop
iny
lda $033C,y
sta $AF
nop
jsr LC6BE
ldy $B6
jsr LC693
jsr LC4CB
LC607: lda OPMN1-1,x
jsr CHROUT
lda OPMN2-1,x
jsr CHROUT
lda OPMN3-1,x
jsr CHROUT
lda #$20
bit $AB
beq LC622
jsr LC349
LC622: ldx #$20
lda #$04
bit $AB
beq LC62C
ldx #$28
LC62C: txa
jsr CHROUT
bit $AB
bvc LC639
lda #$23
jsr CHROUT
LC639: jsr LC52C
dey
beq LC655
lda #$08
bit $AB
beq LC64C
lda #$4D
jsr CHROUT
ldy #$01
LC64C: lda $AD,y
jsr LC32A
dey
bne LC64C
LC655: ldy #$03
LC657: lda LC0AD-1,y
bit $AB
beq LC667
lda LC0B0-1,y
ldx LC0B3-1,y
jsr LC342
LC667: dey
bne LC657
LC66A: lda $B6
LC66C: jsr LC367
sec
sbc #$01
bne LC66C
rts
;; erase screen buffer to end of line
LC675: ldy $D3
lda #' '
LC679: sta ($D1),y
iny
cpy #40
bcc LC679
rts
LC681: cpx $AB
bne LC689
ora $AD
sta $AD
LC689: rts
;; copy $AD through $AD+y to ($FB)
LC68A: lda $AD,y
sta ($FB),y
cmp ($FB),y
bne LC697
LC693: dey
bpl LC68A
rts
LC697: pla
pla
rts
LC69A: bne LC6B8
txa
ora $AB
sta $AB
;; get first character that is not " $(," (max 4)
LC6A1: lda #$04
sta $B5
LC6A5: jsr CHRIN ; get character
cmp #$20 ; is it space?
beq LC6B9 ;
cmp #$24 ; is it "$"?
beq LC6B9
cmp #$28 ; is it "("?
beq LC6B9
cmp #$2C ; is it ","?
beq LC6B9
jsr UCASE ; convert to uppercase
LC6B8: rts
;; character was either " ", "$", "(" or ","
LC6B9: dec $B5
bne LC6A5 ; get next character
rts
LC6BE: cpx #$18
bmi LC6D0
lda $AE
nop
sec
sbc #$02
sec
sbc $FB
sta $AE
nop
ldy #$40
LC6D0: rts
; Assembler (A)
ASSEMBLER:
jsr GETWRD ; get start address
sta $FD
lda $FC
sta $FE
LC6DA: jsr LC351 ; print CR
LC6DD: jsr LC6E4 ; get and assemble line
bmi LC6DD
bpl LC6DA
LC6E4: lda #$00
sta $D3
jsr LC34C ; output ' '
jsr LC323 ; output address
jsr LC34C ; output ' '
jsr CHRIN ; get character
lda #$01 ; set input start column within line
sta $D3
ldx #$80
bne LC701
;; entry point from "," command (assemble single line)
COMMA: ldx #$80 ; set "comma" flag
stx $02B1
LC701: stx $AA
jsr GETWRD ; get word (address) from command line
lda #$25 ; set last input char (37)
sta LASTCOL
bit $02B1 ; skip the following if "comma" flag NOT set
bpl LC717
ldx #$0A ; skip 10 characters (for "," command)
LC711: jsr CHRIN
dex
bne LC711
LC717: lda #$00
sta $02B1