-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.asm
751 lines (634 loc) · 16.5 KB
/
commands.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
;-----------------------------------------------------------------------
; Monitor interactive command line commands
;-----------------------------------------------------------------------
#local ; Avoid polluting namespace
#data _RAM
CMDARGS:: DW 0 ; Pointer to command line arguments
#code _ROM
; Long string commands table
; Commands which are prefixes of others must come last
; Longest match must be tested first
CMDSTBL::
DM ".",0 \ DW CMD_CHADDR ; Change address
DM "?",0 \ DW CMD_HELP ; Help
DM "DEL",0 \ DW CMD_TEMP_DFILE ; Delete file
DM "DIR",0 \ DW CMD_LIST ; List root directory
DM "DIS",0 \ DW CMD_DISASS ; Disassemble
DM "DL",0 \ DW CMD_PROGRAM ; Disk load
DM "D",0 \ DW CMD_DEPOSIT ; Deposit
DM "E",0 \ DW CMD_EXAMINE ; Examine
DM "GO",0 \ DW CMD_RUN ; Run (goto address)
DM "HELP",0 \ DW CMD_HELP
DM "NEW",0 \ DW CMD_TEMP_CFILE ; Create new file
DM "PL",0 \ DW CMD_PC_LOAD ; PC load
DM "READ",0 \ DW CMD_COPYFILE ; Read file into memory
DM "TIME",0 \ DW CMD_TIME ; Time
DB 0 \ DW CMD_INVAL ; Invalid command (catchall)
STR_HELP:
.ascii "Current address is shown in prompt.",10,13
.ascii "Addresses and numbers are hexadecimal",10,13
.ascii ". - Change current address",10,13
.ascii "? - Display this help screen",10,13
.ascii "DEL fn - Delete file fn",10,13
.ascii "DIR - List directory on CF card",10,13
.ascii "DIS [n] - Disassembly n instructions",10,13
.ascii "DL fn - Load fn from disk and run",10,13
.ascii "D - Enter deposit mode",10,13
.ascii "E [n] - Examine n bytes ",10,13
.ascii "GO [a] - Execute at addr a or current",10,13
.ascii "HELP - Display this help screen",10,13
.ascii "NEW fn - Create a new empty file fn",10,13
.ascii "PL fn - Load fn from PC and run",10,13
.ascii "READ fn - Read file fn into memory",10,13
.ascii "TIME - Displays the current time",10,13
db 0
;-----------------------------------------------------------------------
; Change current address
;-----------------------------------------------------------------------
CMD_CHADDR:
INC HL ; Skip over command
CALL SKIPWHITE ; Skip any whitespace
CALL PARSENUM ; Parse up to a 16-bit address
LD A, C
LD (CURADDR), A ;
LD A, B
LD (CURADDR+1), A ; Store address as current
RET
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Examine bytes of memory
; E - Enter interactive examine mode
; E 8 - Examine 8 bytes from curaddr
;-----------------------------------------------------------------------
CMD_EXAMINE:
#local
INC HL
CALL SKIPWHITE
CALL PARSENUM ; Check if we got a number
JR C, INTERACTIVE ; If no number, enter interactive mode
; Count in BC
LD HL, (CURADDR)
MORE:
; Check if BC > 16
LD A, B
AND A
JR NZ, ENOUGH ; > 256 no issue
LD A, C
AND A
JR Z, DONE ; 0 left, we're done
LD A, (HDROWL)
LD E, A
LD A, C
CP E
JR C, SHORT
ENOUGH:
; Subtract 16 from BC
LD A, (HDROWL)
LD E, A
LD A, C
SUB E
LD C, A
LD A, B
SBC 0
LD B, A
LD A, (HDROWL)
CALL HEXDUMPROW ; Dump row, HL is advanced by count
JR MORE ; Continue
SHORT:
LD A, C ; Last few bytes
CALL HEXDUMPROW
DONE:
RET
INTERACTIVE:
; TODO: Do we want this to be actually interactive?
; For now just dump one line and advance CURADDR
LD HL, (CURADDR)
LD A, (HDROWL)
CALL HEXDUMPROW ; Dump row, HL is advanced by count
LD (CURADDR), HL
RET
#endlocal
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Dump one row of memory, up to 'A' bytes
;-----------------------------------------------------------------------
HEXDUMPROW:
#local
PUSH BC
PUSH DE
LD E, A ; Save A
LD BC, HL
CALL PRINTWORD ; Address (BC preserved)
LD HL, STR_COLONSEP
CALL PRINT
LD HL, BC ; Restore address
PUSH HL ; Save inital addr
; Byte values
LD B, E ; Count
BYTEL:
LD A, (HL)
CALL PRINTBYTE
LD A, ' '
CALL PRINTCH
INC HL
DJNZ BYTEL
; Pad if short
LD A, (HDROWL)
SUB E
LD B, A
JR Z, NOPAD
PADB:
LD A, ' '
CALL PRINTCH
CALL PRINTCH
CALL PRINTCH
DJNZ PADB
NOPAD:
; Printable ASCII
LD A, (DISPMODE)
AND 1 ; Check if 40 or 80 cols
JR Z, COL1 ; 40 columns
LD A, '|'
JR COL2
COL1:
LD A, ' '
COL2:
CALL PRINTCH
LD B, E
POP HL ; Restart address
ASCII:
LD A, (HL)
INC HL
CP 127
JR NC, NOPRINT
CP 32
JR NC, DOPRINT
NOPRINT:
LD A, '.'
DOPRINT:
CALL PRINTCH
DJNZ ASCII
; Printable ASCII
LD A, (DISPMODE)
AND 1 ; Check if 40 or 80 cols
JR Z, COL3 ; 40 columns
LD A, '|'
CALL PRINTCH
COL3:
CALL PRINTNL
POP DE
POP BC
RET
#endlocal
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Deposit bytes to memory
; D - Enter deposit mode at curaddr
;-----------------------------------------------------------------------
CMD_DEPOSIT::
#local
NEXTLINE:
LD HL, (CURADDR)
LD BC, HL
CALL PRINTWORD
LD HL, STR_COLONSEP
CALL PRINT
CALL GET_LINE ; Read in new line from user
LD HL, LBUF
CALL SKIPWHITE ; Skip any whitespace
LD A, (HL)
AND A
JR Z, EXIT ; Exit on a blank line
LLOOP:
; Otherwise start reading hex values and depositing
CALL HEX2BYTE
JR C, EXIT ; Stop on first bad byte
LD BC, HL
LD HL, (CURADDR)
LD (HL), A ; Store byte
INC HL
LD (CURADDR), HL
LD HL, BC
CALL SKIPWHITE
LD A, (HL)
AND A
JR NZ, LLOOP
JR NEXTLINE ; Otherwise read another line in
EXIT:
RET
#endlocal
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Dissassemble memory
; X - Disassemble 1 inst (TODO:Interactive dissassemble)
; X 10 - Disassemble 10 instructions from curaddr
;-----------------------------------------------------------------------
CMD_DISASS:
#local
INC HL ; Skip command
CALL SKIPWHITE
LD A, (HL)
CP 0 ; No more command
JR Z, DODIS ; Tail call, do one line
CALL PARSENUM
JR C, DODIS ; Any errors, just do one line
LD A, C ; Only care about low byte
AND A ; Test for 0
RET Z ; Don't disassemble any then
LD B, A
DISLOOP:
PUSH BC
CALL DODIS
POP BC
DJNZ DISLOOP
RET
;--------
; Disassemble one instruction and print result
DODIS:
; Test disassemble at curaddr
LD HL, (CURADDR)
LD BC, HL
CALL PRINTWORD
LD A, $09 ; TAB
CALL PRINTCH
CALL DISINST
LD BC, (CURADDR)
LD (CURADDR), HL ; Next instruction
; Determine number of bytes consumed (min 1, max 4)
SCF
CCF
SBC HL, BC ; L contains number of bytes consumed
LD A, L ; Number of bytes
LD HL, BC ; Old start addr
LD B, A ; Number of bytes
LD C, A ; Save copy in C
BYTES:
LD A, (HL)
CALL PRINTBYTE
LD A, ' '
CALL PRINTCH
INC HL
DJNZ BYTES
; Pad out spaces
LD A, 5 ; One extra since we're doing DJNZ
SUB C ; Number of remaining padding
LD B, A
JR PADTEST
PAD:
LD A, ' '
CALL PRINTCH
CALL PRINTCH
CALL PRINTCH
PADTEST:
DJNZ PAD
LD A, $09 ; TAB
CALL PRINTCH
; Print string
LD HL, DISLINE
CALL PRINTN
RET
#endlocal
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Invalid command handler
;-----------------------------------------------------------------------
CMD_INVAL:
; Let's try and load command name given as a disk COM file
LD HL, LBUF ; Return to start of line
CALL SKIPWHITE ; Make sure we even have a command...
LD A, (HL)
AND A
RET Z ; No command, don't even try
DEC HL ; Minus 1 since
JP CMD_PROGRAM ; CMD_PROGRAM will increment
RET
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Jump to code in memory
; R - Call curaddr
; R 1E00 - Call $1E00
;
; Never returns, if code that's run returns, perform a restart
;-----------------------------------------------------------------------
CMD_RUN:
#local
INC HL
CALL SKIPWHITE
CALL PARSENUM ; Check if we got a number
JR C, CUR ; If no number, run at current addr
;LD HL, START ; Do a cold start if run returns
LD HL, WARM ; Try and do a warm start if run returns
PUSH HL ; Return address
PUSH BC ; Address to jump to
RET ; Jump to code
CUR:
;LD HL, START ; Do a cold start if run returns
LD HL, WARM ; Try and do a warm start if run returns
PUSH HL ; Return address
LD HL, (CURADDR)
PUSH HL ; Address to jump to
RET
#endlocal
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Get time and date from Teensy's RTC
;-----------------------------------------------------------------------
CMD_TIME:
#local
LD BC, 0x0013 ; Day BCD
CALL TEENSY_RDDEV
LD B, A
CALL PRINTBYTE
LD A, '/'
CALL PRINTCH
LD BC, 0x0014 ; Month BCD
CALL TEENSY_RDDEV
LD B, A
CALL PRINTBYTE
LD A, '/'
CALL PRINTCH
LD BC, 0x0015 ; Year BCD
CALL TEENSY_RDDEV
LD B, A
CALL PRINTBYTE
LD A, ' '
CALL PRINTCH
LD BC, 0x0012 ; Read hours (BCD) from RTC
CALL TEENSY_RDDEV
LD B, A
CALL PRINTBYTE
LD A, ':'
CALL PRINTCH
LD BC, 0x0011 ; Read minutes (BCD) from RTC
CALL TEENSY_RDDEV
LD B, A
CALL PRINTBYTE
LD A, ':'
CALL PRINTCH
LD BC, 0x0010 ; Read seconds (BCD) from RTC
CALL TEENSY_RDDEV
LD B, A
CALL PRINTBYTE
CALL PRINTNL
RET
#endlocal
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; List root directory of CF card
;-----------------------------------------------------------------------
CMD_LIST:
CALL FAT_DIR_ROOT
RET
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Read file in from CF card into memory
;-----------------------------------------------------------------------
CMD_COPYFILE:
#local
INC HL ; Skip command
CALL SKIPWHITE ; Skip whitespace
LD A, (HL)
AND A
JR Z, NOFILE ; If null terminator then no filename given
PUSH HL ; Save start pointer
CALL EXTRACTARG ; Extract argument (null terminate it)
POP HL ; Restart pointer to string
PUSH HL
CALL PRINTN ; Print filename
POP HL
LD DE, MON_FS
CALL FS_SETFILENAME ; Set filename
LD HL, MON_FS
CALL FS_OPEN
JR C, NOFILE ; If failure to open
LD HL, (CURADDR) ; Address to load to
LD A, (CURBANK)
LD C, A ; Bank to load to
CALL NORMAL_ADDR ; Normalize addr
LD IX, MON_FS
CALL FS_READFILE ; Read entire file to address
RET
NOFILE:
LD HL, STR_NOFILE
CALL PRINTN
RET
#endlocal
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Load and run a program from disk
;-----------------------------------------------------------------------
CMD_PROGRAM:
#local
INC HL ; Skip command
CALL SKIPWHITE ; Skip whitespace
LD A, (HL)
AND A
JP Z, NOFILE ; If null terminator then no filename given
PUSH HL ; Save start pointer
CALL EXTRACTARG ; Extract argument (null terminate it)
; Save pointer to any arguments
INC HL
LD (CMDARGS), HL
POP HL ; Restart pointer to string
LD DE, MON_FS
CALL FS_SETFILENAME
; Change extension to COM (Kind of cheating)
LD A, 'C'
LD (MON_FS+FS_FNAME+8), A
LD A, 'O'
LD (MON_FS+FS_FNAME+9), A
LD A, 'M'
LD (MON_FS+FS_FNAME+10), A
; Try and open file
LD HL, MON_FS
CALL FS_OPEN
JR C, NOFILE ; If failure to open
; Read the file into bank 0 (Max program size 32768 bytes)
LD HL, 0x100 ; Base address to load to
LD C, 0x08 ; Bank to load into (in LOW 4 bits)
LD IX, MON_FS
CALL FS_READFILE ; Read entire file to address
LD HL, MON_FS
CALL FS_CLOSE
;RET ; Do nothing for now
; Copy needed BIOS code into low 256 bytes
LD HL, 0x0000 ; Copy vector table from rom
LD DE, SECTOR ; Use the SECTOR buffer as scratch (it's 512 bytes)
LD BC, 0x100 ; 256 bytes
LDIR
; Now copy from RAM to the programs bank
LD A, 0x08
LD HL, SECTOR
LD DE, 0x0000
LD BC, 0x100
CALL RAM_BANKCOPY ; Copy between banks
CALL PRINTNL
LD HL, STR_PREJUMP
CALL PRINTN
; Perform bankswitch and call program
LD A, 0x98 ; Free RAM in upper, program RAM in lower
LD (CURBANK), A ; Set current bank
; Alright, we're leaving forever
LD SP, 0xFFFF
JP BIOSSTART ; Bank switch and start!
; We're gone
NOFILE:
LD HL, STR_NOPROG
CALL PRINTN
RET
#endlocal
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Create a new file on CF card
;-----------------------------------------------------------------------
CMD_TEMP_CFILE:
INC HL ; Skip command
CALL SKIPWHITE ; Skip whitespace
LD A, (HL)
AND A
RET Z ; If null terminator then no filename given
PUSH HL ; Save start pointer
CALL EXTRACTARG ; Extract argument (null terminate it)
POP HL ; Restart pointer to string
LD DE, MON_FS
CALL FS_SETFILENAME
LD HL, MON_FS
CALL FS_CREATE
RET NC
CALL PRINTI
.ascii "FS: Failed to create file", 10, 13, 0
RET
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Delete file from CF card
;-----------------------------------------------------------------------
CMD_TEMP_DFILE:
INC HL ; Skip command
CALL SKIPWHITE ; Skip whitespace
LD A, (HL)
AND A
RET Z ; If null terminator then no filename given
PUSH HL ; Save start pointer
CALL EXTRACTARG ; Extract argument (null terminate it)
POP HL ; Restart pointer to string
LD DE, MON_FS
CALL FS_SETFILENAME
LD HL, MON_FS
CALL FS_DELETE
RET NC
CALL PRINTI
.ascii "FS: Failed to delete file",10,13,0
RET
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Load a program from the PC (through teensy) into memory and run
; Program NAME sent to PC, and NAME.COM is returned if it exists
;-----------------------------------------------------------------------
CMD_PC_LOAD:
#local
INC HL ; Skip command
CALL SKIPWHITE ; Skip whitespace
LD A, (HL)
AND A
JP Z, NOFILE ; If null terminator then no filename given
PUSH HL ; Save start pointer
CALL EXTRACTARG ; Extract argument (null terminate it)
INC HL ; Skip null
LD (CMDARGS), HL ; Save argument string
POP HL ; Restart pointer to string
LD B, 0x10 ; Request for program load
CALL TEENSY_WRITE
CALL TEENSY_READ ; Wait for response
CP 0x06 ; ACK
JP NZ, FAIL
; Send file name
FNAME_LOOP:
LD B, (HL)
CALL TEENSY_WRITE
INC HL
LD A, B
AND A
JR NZ, FNAME_LOOP
CALL TEENSY_READ ; Wait for response
CP 0x06 ; ACK
JR NZ, NOFILE
CALL TEENSY_READ ; Get # of pages to load
AND A ; If 0 then failure
JR Z, FAIL
LD C, A ; # of pages
LD DE, 0x0100 ; Load address in D (e used as checksum)
; We're going to reuse our CF card sector buffer to temporarilly load into here
READPAGE:
LD HL, SECTOR
LD B, 0 ; 256 bytes to copy
READIN:
CALL TEENSY_READ ; (only corrupts A)
LD (HL), A
ADD A, E
LD E, A ; Store checksum
INC HL
DJNZ READIN
; Verify checksum
LD B, E
CALL TEENSY_WRITE
CALL TEENSY_READ
CP 0x06 ; ACK
JR NZ, CHKFAIL
; Copy data from sector buffer to ram
PUSH BC ; Save # of pages left (C)
PUSH DE ; Save destination address
LD E, 0 ; Clear low address (checksum)
LD A, 0x08 ; Bank to load into (in LOW 4 bits)
LD BC, 256 ; # of bytes to copy
LD HL, SECTOR
CALL RAM_BANKCOPY ; Copy into program bank
POP DE
INC D ; Next page of destination
POP BC ; Restore pages left
DEC C
JR NZ, READPAGE
; Program is in ram, perform final steps
; Copy needed BIOS code into low 256 bytes
LD HL, 0x0000 ; Copy vector table from rom
LD DE, SECTOR ; Use the SECTOR buffer as scratch (it's 512 bytes)
LD BC, 0x100 ; 256 bytes
LDIR
; Now copy from RAM to the programs bank
LD A, 0x08
LD HL, SECTOR
LD DE, 0x0000
LD BC, 0x100
CALL RAM_BANKCOPY ; Copy between banks
LD HL, STR_PREJUMP
CALL PRINTN
; Perform bankswitch and call program
LD A, 0x98 ; Free RAM in upper, program RAM in lower
LD (CURBANK), A ; Set current bank
; Alright, we're leaving forever
LD SP, 0xFFFF
JP BIOSSTART ; Bank switch and start!
; We're gone
;RET
NOFILE:
LD HL, STR_NOPROG
CALL PRINTN
RET
FAIL:
LD HL, STR_HOSTFAIL
CALL PRINTN
RET
CHKFAIL:
LD HL, STR_CHKFAIL
CALL PRINTN
RET
#endlocal
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; Display the help screen
;-----------------------------------------------------------------------
CMD_HELP:
LD HL, STR_HELP
CALL PRINTN
RET
;-----------------------------------------------------------------------
#endlocal