forked from jblang/supermon64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supermon64.asm
1539 lines (1439 loc) · 68.4 KB
/
supermon64.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
; ********************************
; * SUPERMON+ 64 JIM BUTTERFIELD *
; * V1.2 AUGUST 20 1985 *
; ********************************
; Reformatted and annotated in late 2016/early 2017 by J.B. Langston.
;
; I've made the minimum necessary changes to this code to get it to assemble
; with 64tass. Specifically, I changed the following directives from PAL
; that 64tass doesn't support:
; - .ASC => .TEXT
; - *=*+X => .FILL X
;
; Aside from this, I have adopted a strict whitespace and comments only
; policy so that I preserve code exactly as Jim Butterfield wrote it.
;
; I think my comments are correct but I don't guarantee I haven't made
; any errors. Sadly Jim isn't around to ask anymore. If you spot any
; misunderstanings or errors in my comments, please report them.
; -----------------------------------------------------------------------------
; temporary pointers
TMP0 = $C1 ; used to return input, often holds end address
TMP2 = $C3 ; usually holds start address
; -----------------------------------------------------------------------------
; kernal variables
SATUS = $90 ; kernal i/o status word
FNLEN = $B7 ; length of current filename
SADD = $B9 ; current secondary address (official name SA)
FA = $BA ; current device number
FNADR = $BB ; pointer to current filename
NDX = $C6 ; number of characters in keyboard buffer
KEYD = $0277 ; keyboard buffer
BKVEC = $0316 ; BRK instruction vector (official name CBINV)
*= $0100 ; store variables in tape error buffer
; -----------------------------------------------------------------------------
; variables
ACMD .FILL 1 ; addressing command
LENGTH .FILL 1 ; length of operand
MNEMW .FILL 3 ; 3 letter mnemonic buffer
SAVX .FILL 1 ; 1 byte temp storage, often to save X register
OPCODE .FILL 1 ; current opcode for assembler/disassembler
UPFLG .FILL 1 ; flag: count up (bit 7 clear) or down (bit 7 set)
DIGCNT .FILL 1 ; digit count
INDIG .FILL 1 ; numeric value of single digit
NUMBIT .FILL 1 ; numeric base of input
STASH .FILL 2 ; 2-byte temp storage
U0AA0 .FILL 10 ; work buffer
U0AAE =* ; end of work buffer
STAGE .FILL 30 ; staging buffer for filename, search, etc.
ESTAGE =* ; end of staging buffer
*= $0200 ; store more variables in basic line editor buffer
INBUFF .FILL 40 ; 40-character input buffer
ENDIN =* ; end of input buffer
; the next 7 locations are used to store the registers when
; entering the monitor and restore them when exiting.
PCH .FILL 1 ; program counter high byte
PCL .FILL 1 ; program counter low byte
SR .FILL 1 ; status register
ACC .FILL 1 ; accumulator
XR .FILL 1 ; X register
YR .FILL 1 ; Y register
SP .FILL 1 ; stack pointer
STORE .FILL 2 ; 2-byte temp storage
CHRPNT .FILL 1 ; current position in input buffer
SAVY .FILL 1 ; temp storage, often to save Y register
U9F .FILL 1 ; index into assembler work buffer
; -----------------------------------------------------------------------------
; kernal entry points
SETMSG = $FF90 ; set kernel message control flag
SECOND = $FF93 ; set secondary address after LISTEN
TKSA = $FF96 ; send secondary address after TALK
LISTEN = $FFB1 ; command serial bus device to LISTEN
TALK = $FFB4 ; command serial bus device to TALK
SETLFS = $FFBA ; set logical file parameters
SETNAM = $FFBD ; set filename
ACPTR = $FFA5 ; input byte from serial bus
CIOUT = $FFA8 ; output byte to serial bus
UNTLK = $FFAB ; command serial bus device to UNTALK
UNLSN = $FFAE ; command serial bus device to UNLISTEN
CHKIN = $FFC6 ; define input channel
CLRCHN = $FFCC ; restore default devices
INPUT = $FFCF ; input a character (official name CHRIN)
CHROUT = $FFD2 ; output a character
LOAD = $FFD5 ; load from device
SAVE = $FFD8 ; save to device
STOP = $FFE1 ; check the STOP key
GETIN = $FFE4 ; get a character
; -----------------------------------------------------------------------------
; set up origin
.WEAK
ORG = $9519
.ENDWEAK
* = ORG
; -----------------------------------------------------------------------------
; initial entry point
SUPER LDY #MSG4-MSGBAS ; display "..SYS "
JSR SNDMSG
LDA SUPAD ; store entry point address in tmp0
STA TMP0
LDA SUPAD+1
STA TMP0+1
JSR CVTDEC ; convert address to decimal
LDA #0
LDX #6
LDY #3
JSR NMPRNT ; print entry point address
JSR CRLF
LDA LINKAD ; set BRK vector
STA BKVEC
LDA LINKAD+1
STA BKVEC+1
LDA #$80 ; disable kernel control messages
JSR SETMSG ; and enable error messages
BRK
; -----------------------------------------------------------------------------
; BRK handler
BREAK LDX #$05 ; pull registers off the stack
BSTACK PLA ; order: Y,X,A,SR,PCL,PCH
STA PCH,X ; store in memory
DEX
BPL BSTACK
CLD ; disable bcd mode
TSX ; store stack pointer in memory
STX SP
CLI ; enable interupts
; -----------------------------------------------------------------------------
; display registers [R]
DSPLYR LDY #MSG2-MSGBAS ; display headers
JSR SNDCLR
LDA #$3B ; prefix registers with "; " to allow editing
JSR CHROUT
LDA #$20
JSR CHROUT
LDA PCH ; print 2-byte program counter
JSR WRTWO
LDY #1 ; start 1 byte after PC high byte
DISJ LDA PCH,Y ; loop through rest of the registers
JSR WRBYTE ; print 1-byte register value
INY
CPY #7 ; there are a total of 5 registers to print
BCC DISJ
; -----------------------------------------------------------------------------
; main loop
STRT JSR CRLF ; new line
LDX #0 ; point at start of input buffer
STX CHRPNT
SMOVE JSR INPUT ; CHRIN kernal call to input a character
STA INBUFF,X ; store in input buffer
INX
CPX #ENDIN-INBUFF ; error if buffer is full
BCS ERROR
CMP #$0D ; keep reading until CR
BNE SMOVE
LDA #0 ; null-terminate input buffer
STA INBUFF-1,X ; (replacing the CR)
ST1 JSR GETCHR ; get a character from the buffer
BEQ STRT ; start over if buffer is empty
CMP #$20 ; skip leading spaces
BEQ ST1
S0 LDX #KEYTOP-KEYW ; loop through valid command characters
S1 CMP KEYW,X ; see if input character matches
BEQ S2 ; command matched, dispatch it
DEX ; no match, check next command
BPL S1 ; keep trying until we've checked them all
; then fall through to error handler
; -----------------------------------------------------------------------------
; handle error
ERROR LDY #MSG3-MSGBAS ; display "?" to indicate error and go to new line
JSR SNDMSG
JMP STRT ; back to main loop
; -----------------------------------------------------------------------------
; dispatch command
S2 CPX #$13 ; last 3 commands in table are load/save/validate
BCS LSV ; which are handled by the same subroutine
CPX #$0F ; next 4 commands are base conversions
BCS CNVLNK ; which are handled by the same subroutine
TXA ; remaining commands dispatch through vector table
ASL A ; multiply index of command by 2
TAX ; since table contains 2-byte addresses
LDA KADDR+1,X ; push address from vector table onto stack
PHA ; so that the RTS from GETPAR will jump there
LDA KADDR,X
PHA
JMP GETPAR ; get the first parameter for the command
LSV STA SAVY ; handle load/save/validate
JMP LD
CNVLNK JMP CONVRT ; handle base conversion
; -----------------------------------------------------------------------------
; exit monitor [X]
EXIT JMP ($A002) ; jump to warm-start vector to reinitialize BASIC
; -----------------------------------------------------------------------------
; display memory [M]
DSPLYM BCS DSPM11 ; start from previous end addr if no address given
JSR COPY12 ; save start address in TMP2
JSR GETPAR ; get end address in TMP0
BCC DSMNEW ; did user specify one?
DSPM11 LDA #$0B ; if not, show 12 lines by default
STA TMP0
BNE DSPBYT ; always true, but BNE uses 1 byte less than JMP
DSMNEW JSR SUB12 ; end addr given, calc bytes between start and end
BCC MERROR ; error if start is after end
LDX #3 ; divide by 8 (shift right 3 times)
DSPM01 LSR TMP0+1
ROR TMP0
DEX
BNE DSPM01
DSPBYT JSR STOP ; check for stop key
BEQ DSPMX ; exit early if pressed
JSR DISPMEM ; display 1 line containing 8 bytes
LDA #8 ; increase start address by 8 bytes
JSR BUMPAD2
JSR SUBA1 ; decrement line counter
BCS DSPBYT ; show another line until it's < 0
DSPMX JMP STRT ; back to main loop
MERROR JMP ERROR ; handle error
; -----------------------------------------------------------------------------
; alter registers [;]
ALTR JSR COPY1P ; store first parameter in PC
LDY #0 ; init counter
ALTR1 JSR GETPAR ; get value for next register
BCS ALTRX ; exit early if no more values given
LDA TMP0 ; store in memory, offset from SR
STA SR,Y ; these locations will be transferred to the
INY ; actual registers before exiting the monitor
CPY #$05 ; have we updated all 5 yet?
BCC ALTR1 ; if not, get next
ALTRX JMP STRT ; back to main loop
; -----------------------------------------------------------------------------
; alter memory [>]
ALTM BCS ALTMX ; exit if no parameter provided
JSR COPY12 ; copy parameter to start address
LDY #0
ALTM1 JSR GETPAR ; get value for next byte of memory
BCS ALTMX ; if none given, exit early
LDA TMP0 ; poke value into memory at start address + Y
STA (TMP2),Y
INY ; next byte
CPY #8 ; have we read 8 bytes yet?
BCC ALTM1 ; if not, read the next one
ALTMX LDA #$91 ; move cursor up
JSR CHROUT
JSR DISPMEM ; re-display line to make ascii match hex
JMP STRT ; back to main loop
; -----------------------------------------------------------------------------
; goto (run) [G]
GOTO LDX SP ; load stack pointer from memory
TXS ; save in SP register
GOTO2 JSR COPY1P ; copy provided address to PC
SEI ; disable interrupts
LDA PCH ; push PC high byte on stack
PHA
LDA PCL ; push PC low byte on stack
PHA
LDA SR ; push status byte on stack
PHA
LDA ACC ; load accumulator from memory
LDX XR ; load X from memory
LDY YR ; load Y from memory
RTI ; return from interrupt (pops PC and SR)
; jump to subroutine [J]
JSUB LDX SP ; load stack pointer from memory
TXS ; save value in SP register
JSR GOTO2 ; same as goto command
STY YR ; save Y to memory
STX XR ; save X to memory
STA ACC ; save accumulator to memory
PHP ; push processor status on stack
PLA ; pull processor status into A
STA SR ; save processor status to memory
JMP DSPLYR ; display registers
; -----------------------------------------------------------------------------
; display 8 bytes of memory
DISPMEM JSR CRLF ; new line
LDA #">" ; prefix > so memory can be edited in place
JSR CHROUT
JSR SHOWAD ; show address of first byte on line
LDY #0
BEQ DMEMGO ; SHOWAD already printed a space after the address
DMEMLP JSR SPACE ; print space between bytes
DMEMGO LDA (TMP2),Y ; load byte from start address + Y
JSR WRTWO ; output hex digits for byte
INY ; next byte
CPY #8 ; have we output 8 bytes yet?
BCC DMEMLP ; if not, output next byte
LDY #MSG5-MSGBAS ; if so, output : and turn on reverse video
JSR SNDMSG ; before displaying ascii representation
LDY #0 ; back to first byte in line
DCHAR LDA (TMP2),Y ; load byte at start address + Y
TAX ; stash in X
AND #$BF ; clear 6th bit
CMP #$22 ; is it a quote (")?
BEQ DDOT ; if so, print . instead
TXA ; if not, restore character
AND #$7F ; clear top bit
CMP #$20 ; is it a printable character (>= $20)?
TXA ; restore character
BCS DCHROK ; if printable, output character
DDOT LDA #$2E ; if not, output '.' instaed
DCHROK JSR CHROUT
INY ; next byte
CPY #8 ; have we output 8 bytes yet?
BCC DCHAR ; if not, output next byte
RTS
; -----------------------------------------------------------------------------
; compare memory [C]
COMPAR LDA #0 ; bit 7 clear signals compare
.BYTE $2C ; absolute BIT opcode consumes next word (LDA #$80)
; transfer memory [T]
TRANS LDA #$80 ; bit 7 set signals transfer
STA SAVY ; save compare/transfer flag in SAVY
LDA #0 ; assume we're counting up (bit 7 clear)
STA UPFLG ; save direction flag
JSR GETDIF ; get two addresses and calculate difference
; TMP2 = source start
; STASH = source end
; STORE = length
BCS TERROR ; carry set indicates error
JSR GETPAR ; get destination address in TMP0
BCC TOKAY ; carry set indicates error
TERROR JMP ERROR ; handle error
TOKAY BIT SAVY ; transfer or compare?
BPL COMPAR1 ; high bit clear indicates compare
LDA TMP2 ; if it's a transfer, we must take steps
CMP TMP0 ; to avoid overwriting the source bytes before
LDA TMP2+1 ; they have been transferred
SBC TMP0+1 ; compare source (TMP2) to destination (TMP0)
BCS COMPAR1 ; and count up if source is before than desitnation
LDA STORE ; otherwise, start at end and count down...
ADC TMP0 ; add length (STORE) to desintation (TMP0)
STA TMP0 ; to calculate end of destination
LDA STORE+1
ADC TMP0+1
STA TMP0+1
LDX #1 ; change source pointer from beginning to end
TDOWN LDA STASH,X ; TMP2 = source end (STASH)
STA TMP2,X
DEX
BPL TDOWN
LDA #$80 ; high bit set in UPFLG means count down
STA UPFLG
COMPAR1 JSR CRLF ; new line
LDY #0 ; no offset from pointer
TCLOOP JSR STOP ; check for stop key
BEQ TEXIT ; exit if pressed
LDA (TMP2),Y ; load byte from source
BIT SAVY ; transfer or compare?
BPL COMPAR2 ; skip store if comparing
STA (TMP0),Y ; otherwise, store in destination
COMPAR2 CMP (TMP0),Y ; compare to destination
BEQ TMVAD ; don't show address if equal
JSR SHOWAD ; show address
TMVAD BIT UPFLG ; counting up or down?
BMI TDECAD ; high bit set means we're counting down
INC TMP0 ; increment destination low byte
BNE TINCOK
INC TMP0+1 ; carry to high byte if necessary
BNE TINCOK
JMP ERROR ; error if high byte overflowed
TDECAD JSR SUBA1 ; decrement destination (TMP0)
JSR SUB21 ; decrement source (TMP2)
JMP TMOR
TINCOK JSR ADDA2 ; increment source (TMP2)
TMOR JSR SUB13 ; decrement length
BCS TCLOOP ; loop until length is 0
TEXIT JMP STRT ; back to main loop
; -----------------------------------------------------------------------------
; hunt memory [H]
HUNT JSR GETDIF ; get start (TMP2) and end (TMP0) of haystack
BCS HERROR ; carry indicates error
LDY #0
JSR GETCHR ; get a single character
CMP #"'" ; is it a single quote?
BNE NOSTRH ; if not, input needle as hex bytes
JSR GETCHR ; if so, input needle as string
CMP #0
BEQ HERROR ; error if needle isn't at least one byte
HPAR STA STAGE,Y ; save char in staging area
INY
JSR GETCHR ; get another char
BEQ HTGO ; if it's null start searching
CPY #ESTAGE-STAGE ; have we filled up the needle staging area?
BNE HPAR ; if not, get another character
BEQ HTGO ; if so, start searching
NOSTRH JSR RDPAR ; read hex bytes if string not indicated
HLP LDA TMP0 ; save last read byte in staging area
STA STAGE,Y
INY ; get another hex byte
JSR GETPAR
BCS HTGO ; if there is none, start searching
CPY #ESTAGE-STAGE ; have we filled up the needle staging area?
BNE HLP ; if not, get another byte
HTGO STY SAVY ; save length of needle
JSR CRLF ; new line
HSCAN LDY #0
HLP3 LDA (TMP2),Y ; get first byte in haystack
CMP STAGE,Y ; compare it to first byte of needle
BNE HNOFT ; if it doesn't match, we haven't found anything
INY ; if it does, check the next byte
CPY SAVY ; have we reached the end of the needle?
BNE HLP3 ; if not, keep comparing bytes
JSR SHOWAD ; match found, show address
HNOFT JSR STOP ; no match, check for stop key
BEQ HEXIT ; exit prematurely if pressed
JSR ADDA2 ; increment haystack pointer
JSR SUB13 ; decrement haystack length
BCS HSCAN ; still more haystack? keep searching
HEXIT JMP STRT ; back to main loop
HERROR JMP ERROR ; handle error
; -----------------------------------------------------------------------------
; load, save, or verify [LSV]
LD LDY #1 ; default to reading from tape, device #1
STY FA
STY SADD ; default to secondary address #1
DEY
STY FNLEN ; start with an empty filename
STY SATUS ; clear status
LDA #>STAGE ; set filename pointer to staging buffer
STA FNADR+1
LDA #<STAGE
STA FNADR
L1 JSR GETCHR ; get a character
BEQ LSHORT ; no filename given, try load or verify from tape
CMP #$20 ; skip leading spaces
BEQ L1
CMP #$22 ; error if filename doesn't start with a quote
BNE LERROR
LDX CHRPNT ; load current char pointer into index reg
L3 LDA INBUFF,X ; load current char from buffer to accumulator
BEQ LSHORT ; no filename given, try load or verify from tape
INX ; next char
CMP #$22 ; is it a quote?
BEQ L8 ; if so, we've reached the end of the filename
STA (FNADR),Y ; if not, save character in filename buffer
INC FNLEN ; increment filename length
INY
CPY #ESTAGE-STAGE ; check whether buffer is full
BCC L3 ; if not, get another character
LERROR JMP ERROR ; if so, handle error
L8 STX CHRPNT ; set character pointer to the current index
JSR GETCHR ; eat separator between filename and device #
BEQ LSHORT ; no separator, try to load or verify from tape
JSR GETPAR ; get device number
BCS LSHORT ; no device # given, try load or verify from tape
LDA TMP0 ; set device number for kernal routines
STA FA
JSR GETPAR ; get start address for load or save in TMP0
BCS LSHORT ; no start address, try to load or verify
JSR COPY12 ; transfer start address to TMP2
JSR GETPAR ; get end address for save in TMP0
BCS LDADDR ; no end address, try to load to given start addr
JSR CRLF ; new line
LDX TMP0 ; put low byte of end address in X
LDY TMP0+1 ; put high byte of end address in Y
LDA SAVY ; confirm that we're doing a save
CMP #"S"
BNE LERROR ; if not, error due to too many params
LDA #0
STA SADD ; set secondary address to 0
LDA #TMP2 ; put addr of zero-page pointer to data in A
JSR SAVE ; call kernal save routine
LSVXIT JMP STRT ; back to mainloop
LSHORT LDA SAVY ; check which command we received
CMP #"V"
BEQ LOADIT ; we're doing a verify so don't set A to 0
CMP #"L"
BNE LERROR ; error due to not enough params for save
LDA #0 ; 0 in A signals load, anything else is verify
LOADIT JSR LOAD ; call kernal load routine
LDA SATUS ; get i/o status
AND #$10 ; check bit 5 for checksum error
BEQ LSVXIT ; if no error go back to mainloop
LDA SAVY ; ?? not sure what these two lines are for...
BEQ LERROR ; ?? SAVY will never be 0, so why check?
LDY #MSG6-MSGBAS ; display "ERROR" if checksum didn't match
JSR SNDMSG
JMP STRT ; back to mainloop
LDADDR LDX TMP2 ; load address low byte in X
LDY TMP2+1 ; load address high byte in Y
LDA #0 ; 0 in A signals load
STA SADD ; secondary addr 0 means load to addr in X and Y
BEQ LSHORT ; execute load
; -----------------------------------------------------------------------------
; fill memory [F]
FILL JSR GETDIF ; start in TMP2, end in STASH, length in STORE
BCS AERROR ; carry set indicates error
JSR GETPAR ; get value to fill in TMP0
BCS AERROR ; carry set indicates error
JSR GETCHR ; any more characters triggers an error
BNE AERROR
LDY #0 ; no offset
FILLP LDA TMP0 ; load value to fill in accumulator
STA (TMP2),Y ; store fill value in current address
JSR STOP ; check for stop key
BEQ FSTART ; if pressed, back to main loop
JSR ADDA2 ; increment address
JSR SUB13 ; decrement length
BCS FILLP ; keep going until length reaches 0
FSTART JMP STRT ; back to main loop
; -----------------------------------------------------------------------------
; assemble [A.]
; read in mnemonic
ASSEM BCS AERROR ; error if no address given
JSR COPY12 ; copy address to TMP2
AGET1 LDX #0
STX U0AA0+1 ; clear byte that mnemonic gets shifted into
STX DIGCNT ; clear digit count
AGET2 JSR GETCHR ; get a char
BNE ALMOR ; proceed if the character isn't null
CPX #0 ; it's null, have read a mnemonic yet?
BEQ FSTART ; if not, silently go back to main loop
ALMOR CMP #$20 ; skip leading spaces
BEQ AGET1
STA MNEMW,X ; put character in mnemonic buffer
INX
CPX #3 ; have we read 3 characters yet?
BNE AGET2 ; if not, get next character
; compress mnemonic into two bytes
ASQEEZ DEX ; move to previous char
BMI AOPRND ; if we're done with mnemonic, look for operand
LDA MNEMW,X ; get current character
SEC ; pack 3-letter mnemonic into 2 bytes (15 bits)
SBC #$3F ; subtract $3F from ascii code so A-Z = 2 to 27
LDY #$05 ; letters now fit in 5 bits; shift them out
ASHIFT LSR A ; into the first two bytes of the inst buffer
ROR U0AA0+1 ; catch the low bit from accumulator in right byte
ROR U0AA0 ; catch the low bit from right byte in left byte
DEY ; count down bits
BNE ASHIFT ; keep looping until we reach zero
BEQ ASQEEZ ; unconditional branch to handle next char
AERROR JMP ERROR ; handle error
; parse operand
AOPRND LDX #2 ; mnemonic is in first two bytes so start at third
ASCAN LDA DIGCNT ; did we find address digits last time?
BNE AFORM1 ; if so, look for mode chars
JSR RDVAL ; otherwise, look for an address
BEQ AFORM0 ; we didn't find an address, look for characters
BCS AERROR ; carry flag indicates error
LDA #"$"
STA U0AA0,X ; prefix addresses with $
INX ; next position in buffer
LDY #4 ; non-zero page addresses are 4 hex digits
LDA NUMBIT ; check numeric base in which address was given
CMP #8 ; for addresses given in octal or binary
BCC AADDR ; use only the high byte to determine page
CPY DIGCNT ; for decimal or hex, force non-zero page addressing
BEQ AFILL0 ; if address was given with four digits or more
AADDR LDA TMP0+1 ; check whether high byte of address is zero
BNE AFILL0 ; non-zero high byte means we're not in zero page
LDY #2 ; if it's in zero page, addr is 2 hex digits
AFILL0 LDA #$30 ; use 0 as placeholder for each hex digit in addr
AFIL0L STA U0AA0,X ; put placeholder in assembly buffer
INX ; move to next byte in buffer
DEY ; decrement number of remaining digits
BNE AFIL0L ; loop until all digits have been placed
AFORM0 DEC CHRPNT ; non-numeric input; back 1 char to see what it was
AFORM1 JSR GETCHR ; get next character
BEQ AESCAN ; if there is none, we're finished scanning
CMP #$20 ; skip spaces
BEQ ASCAN
STA U0AA0,X ; store character in assembly buffer
INX ; move to next byte in buffer
CPX #U0AAE-U0AA0 ; is instruction buffer full?
BCC ASCAN ; if not, keep scanning
BCS AERROR ; error if buffer is full
; find matching opcode
AESCAN STX STORE ; save number of bytes in assembly buffer
LDX #0 ; start at opcode $00 and check every one until
STX OPCODE ; we find one that matches our criteria
ATRYOP LDX #0
STX U9F ; reset index into work buffer
LDA OPCODE
JSR INSTXX ; look up instruction format for current opcode
LDX ACMD ; save addressing command for later
STX STORE+1
TAX ; use current opcode as index
LDA MNEMR,X ; check right byte of compressed mnemonic
JSR CHEKOP
LDA MNEML,X ; check left byte of compressed mnemonic
JSR CHEKOP
LDX #6 ; 6 possible characters to check against operand
TRYIT CPX #3 ; are we on character 3?
BNE TRYMOD ; if not, check operand characters
LDY LENGTH ; otherwise, check number of bytes in operand
BEQ TRYMOD ; if zero, check operand characters
TRYAD LDA ACMD ; otherwise, look for an address
CMP #$E8 ; special case for relative addressing mode
; since it's specified with 4 digits in assembly
; but encoded with only 1 byte in object code
LDA #$30 ; '0' is the digit placeholder we're looking for
BCS TRY4B ; ACMD >= $E8 indicates relative addressing
JSR CHEK2B ; ACMD < $E8 indicates normal addressing
DEY ; consume byte
BNE TRYAD ; check for 2 more digits if not zero-page
TRYMOD ASL ACMD ; shift a bit out of the addressing command
BCC UB4DF ; if it's zero, skip checking current character
LDA CHAR1-1,X
JSR CHEKOP ; otherwise first character against operand
LDA CHAR2-1,X ; get second character to check
BEQ UB4DF ; if it's zero, skip checking it
JSR CHEKOP ; otherwise check it against hte operand
UB4DF DEX ; move to next character
BNE TRYIT ; repeat tests
BEQ TRYBRAN
TRY4B JSR CHEK2B ; check for 4 digit address placeholder
JSR CHEK2B ; by checking for 2 digits twice
TRYBRAN LDA STORE ; get number of bytes in assembly buffer
CMP U9F ; more bytes left to check?
BEQ ABRAN ; if not, we've found a match; build instruction
JMP BUMPOP ; if so, this opcode doesn't match; try the next
; convert branches to relative address
ABRAN LDY LENGTH ; get number of bytes in operand
BEQ A1BYTE ; if none, just output the opcode
LDA STORE+1 ; otherwise check the address format
CMP #$9D ; is it a relative branch?
BNE OBJPUT ; if not, skip relative branch calculation
LDA TMP0 ; calculate the difference between the current
SBC TMP2 ; address and the branch target (low byte)
TAX ; save it in X
LDA TMP0+1 ; borrow from the high byte if necessary
SBC TMP2+1
BCC ABBACK ; if result is negative, we're branching back
BNE SERROR ; high bytes must be equal when branching forward
CPX #$82 ; difference between low bytes must be < 130
BCS SERROR ; error if the address is too far away
BCC ABRANX
ABBACK TAY ; when branching backward high byte of target must
INY ; be 1 less than high byte of current address
BNE SERROR ; if not, it's too far away
CPX #$82 ; difference between low bytes must be < 130
BCC SERROR ; if not, it's too far away
ABRANX DEX ; adjust branch target relative to the
DEX ; instruction following this one
TXA
LDY LENGTH ; load length of operand
BNE OBJP2 ; don't use the absolute address
; assemble machine code
OBJPUT LDA TMP0-1,Y ; get the operand
OBJP2 STA (TMP2),Y ; store it after the opcode
DEY
BNE OBJPUT ; copy the other byte of operand if there is one
A1BYTE LDA OPCODE ; put opcode into instruction
STA (TMP2),Y
JSR CRLF ; carriage return
LDA #$91 ; back up one line
JSR CHROUT
LDY #MSG7-MSGBAS ; "A " prefix
JSR SNDCLR ; clear line
JSR DISLIN ; disassemble the instruction we just assembled
INC LENGTH ; instruction length = operand length + 1 byte
LDA LENGTH ; for the opcode
JSR BUMPAD2 ; increment address by length of instruction
LDA #"A" ; stuff keyboard buffer with next assemble command:
STA KEYD ; "A XXXX " where XXXX is the next address
LDA #" " ; after the previously assembled instruction
STA KEYD+1
STA KEYD+6
LDA TMP2+1 ; convert high byte of next address to hex
JSR ASCTWO
STA KEYD+2 ; put it in the keyboard buffer
STX KEYD+3
LDA TMP2 ; convert low byte of next address to hex
JSR ASCTWO
STA KEYD+4 ; put it in the keyboard buffer
STX KEYD+5
LDA #7 ; set number of chars in keyboard buffer
STA NDX
JMP STRT ; back to main loop
SERROR JMP ERROR ; handle error
; check characters in operand
CHEK2B JSR CHEKOP ; check two bytes against value in accumulator
CHEKOP STX SAVX ; stash X
LDX U9F ; get current index into work buffer
CMP U0AA0,X ; check whether this opcode matches the buffer
BEQ OPOK ; matching so far, check the next criteria
PLA ; didn't match, so throw away return address
PLA ; on the stack because we're starting over
BUMPOP INC OPCODE ; check the next opcode
BEQ SERROR ; error if we tried every opcode and none fit
JMP ATRYOP ; start over with new opcode
OPOK INC U9F ; opcode matches so far; check the next criteria
LDX SAVX ; restore X
RTS
; -----------------------------------------------------------------------------
; disassemble [D]
DISASS BCS DIS0AD ; if no address was given, start from last address
JSR COPY12 ; copy start address to TMP2
JSR GETPAR ; get end address in TMP0
BCC DIS2AD ; if one was given, skip default
DIS0AD LDA #$14 ; disassemble 14 bytes by default
STA TMP0 ; store length in TMP0
BNE DISGO ; skip length calculation
DIS2AD JSR SUB12 ; calculate number of bytes between start and end
BCC DERROR ; error if end address is before start address
DISGO JSR CLINE ; clear the current line
JSR STOP ; check for stop key
BEQ DISEXIT ; exit early if pressed
JSR DSOUT1 ; output disassembly prefix ". "
INC LENGTH
LDA LENGTH ; add length of last instruction to start address
JSR BUMPAD2
LDA LENGTH ; subtract length of last inst from end address
JSR SUBA2
BCS DISGO
DISEXIT JMP STRT ; back to mainloop
DERROR JMP ERROR
DSOUT1 LDA #"." ; output ". " prefix to allow edit and reassemble
JSR CHROUT
JSR SPACE
DISLIN JSR SHOWAD ; show the address of the instruction
JSR SPACE ; insert a space
LDY #0 ; no offset
LDA (TMP2),Y ; load operand of current instruction
JSR INSTXX ; get mnemonic and addressing mode for opcode
PHA ; save index into mnemonic table
LDX LENGTH ; get length of operand
INX ; add 1 byte for opcode
DSBYT DEX ; decrement index
BPL DSHEX ; show hex for byte being disassembled
STY SAVY ; save index
LDY #MSG8-MSGBAS ; skip 3 spaces
JSR SNDMSG
LDY SAVY ; restore index
JMP NXBYT
DSHEX LDA (TMP2),Y ; show hex for byte
JSR WRBYTE
NXBYT INY ; next byte
CPY #3 ; have we output 3 bytes yet?
BCC DSBYT ; if not, loop
PLA ; restore index into mnemonic table
LDX #3 ; 3 letters in mnemonic
JSR PROPXX ; print mnemonic
LDX #6 ; 6 possible address mode character combos
PRADR1 CPX #3 ; have we checked the third combo yet?
BNE PRADR3 ; if so, output the leading characters
LDY LENGTH ; get the length of the operand
BEQ PRADR3 ; if it's zero, there's no operand to print
PRADR2 LDA ACMD ; otherwise, get the addressing mode
CMP #$E8 ; check for relative addressing
PHP ; save result of check
LDA (TMP2),Y ; get the operand
PLP ; restore result of check
BCS RELAD ; handle a relative address
JSR WRTWO ; output digits from address
DEY
BNE PRADR2 ; repeat for next byte of operand, if there is one
PRADR3 ASL ACMD ; check whether addr mode uses the current char
BCC PRADR4 ; if not, skip it
LDA CHAR1-1,X ; look up the first char in the table
JSR CHROUT ; print first char
LDA CHAR2-1,X ; look up the second char in the table
BEQ PRADR4 ; if there's no second character, skip it
JSR CHROUT ; print second char
PRADR4 DEX ; next potential address mode character
BNE PRADR1 ; loop if we haven't checked them all yet
RTS ; back to caller
RELAD JSR UB64D ; calculate absolute address from relative
CLC
ADC #1 ; adjust address relative to next instruction
BNE RELEND ; don't increment high byte unless we overflowed
INX ; increment high byte
RELEND JMP WRADDR ; print address
UB64D LDX TMP2+1 ; get high byte of current address
TAY ; is relative address positive or negative?
BPL RELC2 ; if positive, leave high byte alone
DEX ; if negative, decrement high byte
RELC2 ADC TMP2 ; add relative address to low byte
BCC RELC3 ; if there's no carry, we're done
INX ; if there's a carry, increment the high byte
RELC3 RTS
; -----------------------------------------------------------------------------
; get opcode mode and length
; Note: the labels are different, but the code of this subroutine is almost
; identical to the INSDS2 subroutine of the Apple Mini-Assembler on page 78 of
; the Apple II Red Book. I'm not sure exactly where this code originated
; (MOS or Apple) but it's clear that this part of Supermon64 and the
; Mini-Asssembler share a common heritage. The comments showing the way the
; opcodes are transformed into indexes for the mnemonic lookup table come
; from the Mini-Assembler source.
INSTXX TAY ; stash opcode in accumulator in Y for later
LSR A ; is opcode even or odd?
BCC IEVEN
LSR A
BCS ERR ; invalid opcodes XXXXXX11
CMP #$22
BEQ ERR ; invalid opcode 10001001
AND #$07 ; mask bits to 10000XXX
ORA #$80
IEVEN LSR A ; LSB determines whether to use left/right nybble
TAX ; get format index using remaining high bytes
LDA MODE,X
BCS RTMODE ; look at left or right nybble based on carry bit
LSR A ; if carry = 0, use left nybble
LSR A
LSR A
LSR A
RTMODE AND #$0F ; if carry = 1, use right nybble
BNE GETFMT
ERR LDY #$80 ; substitute 10000000 for invalid opcodes
LDA #0
GETFMT TAX
LDA MODE2,X ; lookup operand format using selected nybble
STA ACMD ; save for later use
AND #$03 ; lower 2 bits indicate number of bytes in operand
STA LENGTH
TYA ; restore original opcode
AND #$8F ; mask bits to X000XXXX
TAX ; save it
TYA ; restore original opcode
LDY #3
CPX #$8A ; check if opcode = 1XXX1010
BEQ GTFM4
GTFM2 LSR A ; transform opcode into index for mnemonic table
BCC GTFM4
LSR A ; opcodes transformed as follows:
GTFM3 LSR A ; 1XXX1010->00101XXX
ORA #$20 ; XXXYYY01->00111XXX
DEY ; XXXYYY10->00111XXX
BNE GTFM3 ; XXXYY100->00110XXX
INY ; XXXXX000->000XXXXX
GTFM4 DEY
BNE GTFM2
RTS
; -----------------------------------------------------------------------------
; extract and print packed mnemonics
PROPXX TAY ; use index in accumulator to look up mnemonic
LDA MNEML,Y ; and place a temporary copy in STORE
STA STORE
LDA MNEMR,Y
STA STORE+1
PRMN1 LDA #0 ; clear accumulator
LDY #$05 ; shift 5 times
PRMN2 ASL STORE+1 ; shift right byte
ROL STORE ; rotate bits from right byte into left byte
ROL A ; rotate bits from left byte into accumulator
DEY ; next bit
BNE PRMN2 ; loop until all bits shifted
ADC #$3F ; calculate ascii code for letter by adding to '?'
JSR CHROUT ; output letter
DEX ; next letter
BNE PRMN1 ; loop until all 3 letters are output
JMP SPACE ; output space
; -----------------------------------------------------------------------------
; read parameters
RDPAR DEC CHRPNT ; back up one char
GETPAR JSR RDVAL ; read the value
BCS GTERR ; carry set indicates error
JSR GOTCHR ; check previous character
BNE CKTERM ; if it's not null, check if it's a valid separator
DEC CHRPNT ; back up one char
LDA DIGCNT ; get number of digits read
BNE GETGOT ; found some digits
BEQ GTNIL ; didn't find any digits
CKTERM CMP #$20 ; space or comma are valid separators
BEQ GETGOT ; anything else is an error
CMP #","
BEQ GETGOT
GTERR PLA ; encountered error
PLA ; get rid of command vector pushed on stack
JMP ERROR ; handle error
GTNIL SEC ; set carry to indicate no parameter found
.BYTE $24 ; BIT ZP opcode consumes next byte (CLC)
GETGOT CLC ; clear carry to indicate paremeter returned
LDA DIGCNT ; return number of digits in A
RTS ; return to address pushed from vector table
; -----------------------------------------------------------------------------
; read a value in the specified base
RDVAL LDA #0 ; clear temp
STA TMP0
STA TMP0+1
STA DIGCNT ; clear digit counter
TXA ; save X and Y
PHA
TYA
PHA
RDVMOR JSR GETCHR ; get next character from input buffer
BEQ RDNILK ; null at end of buffer
CMP #$20 ; skip spaces
BEQ RDVMOR
LDX #3 ; check numeric base [$+&%]
GNMODE CMP HIKEY,X
BEQ GOTMOD ; got a match, set up base
DEX
BPL GNMODE ; check next base
INX ; default to hex
DEC CHRPNT ; back up one character
GOTMOD LDY MODTAB,X ; get base value
LDA LENTAB,X ; get bits per digit
STA NUMBIT ; store bits per digit
NUDIG JSR GETCHR ; get next char in A
RDNILK BEQ RDNIL ; end of number if no more characters
SEC
SBC #$30 ; subtract ascii value of 0 to get numeric value
BCC RDNIL ; end of number if character was less than 0
CMP #$0A
BCC DIGMOR ; not a hex digit if less than A
SBC #$07 ; 7 chars between ascii 9 and A, so subtract 7
CMP #$10 ; end of number if char is greater than F
BCS RDNIL
DIGMOR STA INDIG ; store the digit
CPY INDIG ; compare base with the digit
BCC RDERR ; error if the digit >= the base
BEQ RDERR
INC DIGCNT ; increment the number of digits
CPY #10
BNE NODECM ; skip the next part if not using base 10
LDX #1
DECLP1 LDA TMP0,X ; stash the previous 16-bit value for later use
STA STASH,X
DEX
BPL DECLP1
NODECM LDX NUMBIT ; number of bits to shift
TIMES2 ASL TMP0 ; shift 16-bit value by specified number of bits
ROL TMP0+1
BCS RDERR ; error if we overflowed 16 bits
DEX
BNE TIMES2 ; shift remaining bits
CPY #10
BNE NODEC2 ; skip the next part if not using base 10
ASL STASH ; shift the previous 16-bit value one bit left
ROL STASH+1
BCS RDERR ; error if we overflowed 16 bits
LDA STASH ; add shifted previous value to current value
ADC TMP0
STA TMP0
LDA STASH+1
ADC TMP0+1
STA TMP0+1
BCS RDERR ; error if we overflowed 16 bits
NODEC2 CLC
LDA INDIG ; load current digit
ADC TMP0 ; add current digit to low byte
STA TMP0 ; and store result back in low byte
TXA ; A=0
ADC TMP0+1 ; add carry to high byte
STA TMP0+1 ; and store result back in high byte
BCC NUDIG ; get next digit if we didn't overflow
RDERR SEC ; set carry to indicate error
.BYTE $24 ; BIT ZP opcode consumes next byte (CLC)
RDNIL CLC ; clear carry to indicate success
STY NUMBIT ; save base of number
PLA ; restore X and Y
TAY
PLA
TAX
LDA DIGCNT ; return number of digits in A
RTS
; -----------------------------------------------------------------------------
; print address
SHOWAD LDA TMP2