-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdos.z80
1297 lines (1202 loc) · 21.8 KB
/
dos.z80
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; DISK OPERATING SYSTEM
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
include 'fat.z80'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; DOS CORE
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Dos:
DosInit:
; INITIALISE EVERYTHING
; Initialise system variables
ld hl, 0x8000
ld (execaddr), hl
ld hl, 0x8000
ld (fileaddr), hl
ld hl, 0x7FFF
ld (sysstack), hl
ld sp, hl
ld hl, 0xFFFF
ld (prgstack), hl
; Setup UART buffering
ld a, 0x00
ld (uart_pause), a
ld (uart_buffering), a
ld (uart_buffer_size), a
ld hl, uart_buffer
ld (uart_buffer_pointer), hl
; Print welcome message
ld hl, welcomestr
call PrintStringA
; Initialise filesystem
call InitFAT
; Check for AUTOEXEC.BAT
; call DosAutoExec
; Start processing commands
jr DosLoop
welcomestr: defm "Welcome to LM-512 microcomputer system.\n\r\0"
DosLoop:
; INFINITE LOOP OF READING A COMMAND LINE,
; PARSING IT AND DISPATCHING THE COMMAND
ld hl, (sysstack)
ld sp, hl
ld hl, dos_promptstr
call PrintStringA
ld hl, dos_linebuf
call ReadLineA
jr nz, DosLoop ; User stopped input using Ctrl-C
ld hl, dos_linebuf
ld a, (hl)
cp "\0"
jr z, DosLoop
call DosHandleLine
jr DosLoop
dos_promptstr:
defm "> \0"
DosHandleLine:
; HANDLE A COMMAND LINE: CONVERT TO UPPERCASE,
; PARSE/TOKENISE LINE AND DISPATCH
ld bc, dos_argc
ld de, dos_dispatch_table
call HandleCommandLine
ret
DosAutoExec:
; CHECK FOR FILE NAMED AUTOEXEC.BAT
; IF FOUND, EXECUTE AS BATCH FILE
ld hl, DosAutoExecStr
call StatFile
ret nz
ld hl, DosAutoExecStr
jp BatchCallback
DosAutoExecStr:
defm "AUTOEXEC.BAT\0"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; COMMAND DISPATCH
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Command strings
batchstr: defm "BATCH\0"
clsstr: defm "CLS\0"
cpstr: defm "CP\0"
datestr: defm "DATE\0"
echostr: defm "ECHO\0"
execstr: defm "EXEC\0"
hashstr: defm "#\0"
intstr: defm "INT\0"
lsstr: defm "LS\0"
monstr: defm "MONITOR\0"
mvstr: defm "MV\0"
serloadstr: defm "SERLOAD\0"
receivestr: defm "RECEIVE\0"
resetstr: defm "RESET\0"
remstr: defm "REM\0"
rmstr: defm "RM\0"
loadstr: defm "LOAD\0"
setstr: defm "SET\0"
showstr: defm "SHOW\0"
sleepstr: defm "SLEEP\0"
typestr: defm "TYPE\0"
uptimestr: defm "UPTIME\0"
; Table linking command strings to function entry points
dos_dispatch_table:
defw batchstr, Batch
defw clsstr, Cls
defw cpstr, Cp
defw datestr, Date
defw execstr, Exec
defw echostr, Echo
defw intstr, Int
defw lsstr, Ls
defw hashstr, Rem
defw helpstr, DosHelp
defw loadstr, DosLoadFile
defw monstr, EnterMonitor
defw mvstr, Mv
defw receivestr, Receive
defw resetstr, Reset
defw remstr, Rem
defw rmstr, Rm
defw serloadstr, SerialLoad
defw setstr, Set
defw sleepstr, SleepDos
defw typestr, Type
defw uptimestr, Uptime
defw nullstr, Default
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Buffered UART
ReadChar:
push hl
ReadCharReenter:
di
ld a, (uart_buffer_size)
cp 0
jr z, ReadCharWait
ld hl, (uart_buffer_pointer)
dec hl
ld a, (hl)
ld (uart_buffer_pointer), hl
ld hl, uart_buffer_size
dec (hl)
ei
pop hl
ret
ReadCharWait:
ei
halt
jr ReadCharReenter
GetSysMemPtr:
ld ix, SYSPAGE
ret
WriteChar:
push af
WriteCharLoop:
ld a, (uart_pause)
cp 0
jr nz, WriteCharWait
pop af
call WriteA
ret
WriteCharWait:
halt
jr WriteCharLoop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; DOS COMMANDS
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Batch:
; Load a file's contents into RAM and
; execute it line-by-line as if it were
; input to the shell
; Check dos_argc
ld a, (dos_argc)
cp 1
jr z, BatchValidate
;; Argc error
ld hl, batchstr
OneFilenameArgcError:
ld de, ExpectsExactlyOneArgStr
ld bc, FilenameStr
jp Print3Strings
BatchValidate:
call ValidateOneExistingFile
ret nz
BatchCallback:
ld de, (fileaddr)
call ReadFile
jp nz, BadFileCallback
ld hl, (fileaddr)
ld (line_start), hl
BatchLoop:
ld a, 0x0A ; Newline
cpir
ld (line_end), hl
push bc
; Overwrite newline with string terminator
dec hl
ld (hl), "\0"
ld hl, (line_start)
call DosHandleLine
ld hl, (line_end)
ld (line_start), hl
pop bc
ld a, b
or c
ret z
jr BatchLoop
ValidateOneExistingFile:
ld hl, (dos_argv)
jp ValidateFilename
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Date:
ld a, (dos_argc)
cp 0
jp z, DateGet
cp 1
jr z, DateSet
ld hl, DateArgcErrStr
call PrintStringA
ret
DateArgcErrStr:
defm "Date expects zero (to get) or one arguments (to set, YYMMDDHHmmSS)\r\n\0"
DateSet:
ld hl, (dos_argv)
call ValidateDateString
ret nz
call SetTimeFromString
ValidateDateString:
call StrLen
cp 12
jr nz, ValidateDateStringErr
call IsStrDec
jr nz, ValidateDateStringErr
ValidateDateStringOkay:
cp a ; Set zero flag
ret
ValidateDateStringErr:
ld hl, ValidateDateStringErrStr
call PrintStringA
or 1 ; Reset zero flag
ret
ValidateDateStringErrStr:
defm "Set date with YYMMDDHHmmSS format\r\n\0"
SetTimeFromString:
call ReadBCDByte
res 7, a ; Strip 7th bit
push af
call ReadBCDByte
res 7, a
push af
call ReadBCDByte
res 7, a
ld e, a
call ReadBCDByte
res 7, a
ld d, a
call ReadBCDByte
res 7, a
ld c, a
call ReadBCDByte
res 7, a
ld b, a
pop af
ld h, a
pop af
ld l, a
call RTC_WRITE
ret
DateGet:
ld hl, message_buffer
push hl
call BuildTimeStr
pop hl
call PrintStringA
ret
BuildTimeStr:
push hl ; Save string pointer
call RTC_READ
ex de, hl ; Move year and month to DE
pop hl ; Restore string pointer
; Year
ld (hl), "2"
inc hl
ld (hl), "0"
inc hl
ld a, e
call WriteComponent
ld (hl), " "
inc hl
ld a, d
; Turn month into non-BCD
; Is the 10 digit non-zero?
bit 4, a
jr z, MonthDeBCDed
; If so, add 10.
and 0x0F
add 10
MonthDeBCDed:
; Now deduct 1 (so Jan is offset 0)
dec a
; Now multiply A by 3 (length of a month str)
ld b, a
add b
add b
; Now use this as an index into month strings
ld b, 0
ld c, a
ex de, hl
ld hl, MonthStrings
add hl, bc
ld bc, 3
ldir
ex de, hl
ld (hl), " "
inc hl
; Day
push hl ; Save string pointer
call RTC_READ
pop hl ; Restore string pointer
push bc
ld a, e
call WriteComponent
ld (hl), " "
inc hl
ld a, d
call WriteComponent
ld (hl), ":"
inc hl
ld a, c
call WriteComponent
ld (hl), ":"
inc hl
pop bc
ld a, b
call WriteComponent
call TerminateLine
ret
WriteComponent:
ld b, a ; Copy
srl a ; Shift down 10 digi
srl a
srl a
srl a
and 00000111b ; Mask off AM/PM
add a, 48 ; Convert to ASCII
ld (hl), a
inc hl
ld a, b
and 00001111b
add a, 48 ; ASCII
ld (hl), a
inc hl
ret
MonthStrings:
defm "JanFebMarAprMayJunJulAugSepOctNovDec"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ECHO
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Echo:
ld a, (dos_argc)
ld b, a
ld hl, dos_argv
EchoLoop:
push bc
ld e, (hl)
inc hl
ld d, (hl)
inc hl
ex de, hl
call PrintStringA
ld a, " "
call SERIAL_WRITE_A
ex de, hl
pop bc
djnz EchoLoop
ld a, "\r"
call SERIAL_WRITE_A
ld a, "\n"
call SERIAL_WRITE_A
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Ls:
ld hl,0x0000
ld (file_count), hl
ld (total_size), hl
ld (total_size+2), hl
ld a, 0
call StatFile ; Get details of first file
LsHandleFile:
; Stash stat results
push bc
push de
push hl
; Update total size count
ld hl, (total_size)
add hl, de
ld (total_size), hl
ld hl, (total_size+2)
ld de, 0
adc hl, de
ld (total_size+2), hl
; Print filename, but first stash length
pop hl
call StrLen
ld c, a
call PrintStringA
; Print spaces
ld a, 12
sub c
call PrintSpaces
; Print size
pop hl
pop de
call B2D32
call PrintB2D
ld hl, BytesStr
call PrintStringA
; Increment file count
ld hl, (file_count)
inc hl
ld (file_count), hl
; Advance
ld hl, 0x0000
ld a, 1
call StatFile
cp 0xFF
jr nz, LsHandleFile
LsEnd:
ld hl, TotalStr
call PrintStringA
ld a, 6
call PrintSpaces
ld hl, (file_count)
call B2D16
call PrintB2D
ld hl, FilesStr
call PrintStringA
ld a, 12
call PrintSpaces
ld hl, (total_size)
ld de, (total_size+2)
call B2D32
call PrintB2D
ld hl, BytesStr
call PrintStringA
ret
TotalStr:
defm "\nTotal:\0"
FilesStr:
defm " files\r\n\0"
BytesStr:
defm " bytes\r\n\0"
PrintB2D:
ld hl, B2DBUF
call PrintStringA
ret
PrintSpaces:
cp 0
ret z
ld b, a
ld a, " "
SpacesLoop:
call WriteA
djnz SpacesLoop
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DosHelp:
ld hl, dos_helpmessage
call PrintStringA
ret
dos_helpmessage: defm "LM-512 DOS commands:\n\r"
defm "BATCH\t\tExecute commands from text file\n\r"
defm "CLS\t\tClear screen\n\r"
defm "CP\t\tCopy file\n\r"
defm "DATE\t\tPrint/set date and time\n\r"
defm "ECHO\t\tEcho string\n\r"
defm "EXEC\t\tLoad and execute machine code from file\n\r"
defm "INT\t\tManage interrupts\n\r"
defm "LOAD\t\tLoad file onto memory\n\r"
defm "LS\t\tList directory contents\n\r"
defm "MONITOR\t\tEnter resident monitore\n\r"
defm "MV\t\tMove (rename) file\n\r"
defm "RECEIVE\t\tRead data from COM2 and save to file\n\r"
defm "RESET\t\tReset system\n\r"
defm "RM\t\tremove file\n\r"
defm "SERLOAD\t\tRead data from COM2 to memory\n\r"
defm "SET\t\tManage system var\n\r"
defm "SLEEP\t\tSleep for some seconds\n\r"
defm "TYPE\t\tShow file contents\n\r"
defm "UPTIME\t\tShow time since boot\n\r"
defm "\0"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EnterMonitor:
call Monitor
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
proto_hello: equ $00
proto_hello_ack: equ $0F
proto_bytes: equ $F0
proto_send: equ $FF
SerialLoad:
; Check dos_argc
ld a, (dos_argc)
cp 1
jr z, SerialLoad00
;; Prepare and pring arg error
ld hl, serloadstr
ld de, ExpectsExactlyOneArgStr
ld bc, AddressStr
jp Print3Strings
ExpectsExactlyOneArgStr:
defm " expects exactly one arg \0"
AddressStr:
defm "(mem address)\r\n\0"
SerialLoad00:
; Validate address
ld hl, (dos_argv)
call ValidateAddress
ret nz
; Parse address and store in HL
call Read16bit
ld h, b
ld l, c
SerialLoadCore:
; Track how many bytes we receive
ld de, 0x0000
; Reset server
ld a, proto_hello
call SERIAL_WRITE_B
call SERIAL_READ_B
cp proto_hello_ack
jr z, SerialLoad01
SerialLoad01:
transfer_outer_loop:
ld a, proto_bytes
call SERIAL_WRITE_B
call SERIAL_READ_B
; Make sure we wren't just told there are no more bytes
cp 0
jr z, SerialLoadEnd
; A now contains byte count. Turn to 16 bit in BC
ld b, 0
ld c, a
; Increment byte received count
ex de, hl
add hl, bc
ex de, hl
ld a, proto_send
call SERIAL_WRITE_B
transfer_inner_loop:
call SERIAL_READ_B
ld (hl), a
inc hl
dec c
ld a, c
cp 0
jr nz, transfer_inner_loop
ld a, "."
call SERIAL_WRITE_A
jr transfer_outer_loop
SerialLoadEnd:
push de ; Preserve byte count
ex de,hl ; Byte count in hl
call B2D16 ; Decimalise
call PrintStringA
ld hl, SerialLoadStr
call PrintStringA
pop de ; Get byte count back
ret
SerialLoadStr:
defm " bytes transferred.\r\n\0"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; SET
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Set:
ld a, (dos_argc)
cp 0
jr z, Show
cp 2
jr z, SetValidate
;; Argc error
ld hl, setstr
ld de, ExpectsExactlyTwoArgsStr
ld bc, SetArgsStr
jp Print3Strings
SetValidate:
ld hl, (dos_argv)
ld bc, SetTable
call MapString2Address
; Now HL is either a RAM address or 0x0000
ld a, l
or h
cp 0
jr nz, SetDoIt
; At this point we know the address is 0000, i.e. we did
; not really match
SetError:
ld hl, SetBadVarStr
call PrintStringA
ret
SetDoIt:
push hl ; The address
ld hl, (dos_argv+2)
call Read16bit
pop hl
ld (hl), c
inc hl
ld (hl), b
ret
Show:
ld de, SetTable
ShowLoop:
ex de, hl
ld e, (hl)
inc hl
ld d, (hl)
inc hl
ex de, hl
call StrLen
cp 0
ret z
call PrintStringA
ld hl, SeparatorStr
call PrintStringA
ex de, hl
ld c, (hl)
inc hl
ld b, (hl)
inc hl
ex de, hl
ld hl, message_buffer
push hl
inc bc
ld a, (bc)
push bc
call StrfHex
pop bc
dec bc
ld a, (bc)
call StrfHex
call TerminateLine
pop hl
call PrintStringA
jr ShowLoop
ExpectsExactlyTwoArgsStr:
defm " expects exactly two args \0"
SetArgsStr:
defm "(var name, value).\r\n\0"
SetBadVarStr:
defm "Unrecognised var. Run SET to see all vars.\r\n\0"
execaddrstr:
defm "EXECADDR\0"
fileaddrstr:
defm "FILEADDR\0"
sysstackstr:
defm "SYSSTACK\0"
prgstackstr:
defm "PRGSTACK\0"
SetTable:
defw execaddrstr, execaddr
defw fileaddrstr, fileaddr
defw sysstackstr, sysstack
defw prgstackstr, prgstack
defw nullstr, 0x0000
SeparatorStr:
defm ":\t\0"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; RECEIVE FILE
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Receive:
; Check dos_argc
ld a, (dos_argc)
cp 1
jr z, ReceiveValidate
;; Argc error
ld hl, receivestr
jp OneFilenameArgcError
FilenameStr:
defm "(8.3 filename(s))\r\n\0"
ReceiveValidate:
ld hl, (dos_argv)
call ValidateFilename
ret nz
call HandleOverwrite
ret nz
;; First download the file
ld hl, (fileaddr)
call SerialLoadCore
;; Now save
ld b, d
ld c, e
ld de, (fileaddr)
ld hl, (dos_argv)
call WriteFile
ret
HandleOverwrite:
;; Check for collisions!
call StatFile
jr z, HandleOverwriteCore
cp a
ret
HandleOverwriteCore:
ld hl, FileExistsStr
call PrintStringA
jr HandleOverwriteAskQuickEnter
HandleOverwriteAskLoop:
ld hl, YNPromptStr
call PrintStringA
HandleOverwriteAskQuickEnter:
ld hl, message_buffer
push hl
call SERIAL_READ_A
ld (hl), a
inc hl
call TerminateLine
pop hl
call ConvertToUpper
call PrintStringA
ld hl, message_buffer
ld a, (hl)
cp "Y"
jr z, HandleOverwriteApprove
cp "N"
jr z, HandleOverwriteDeny
jr HandleOverwriteAskLoop
HandleOverwriteApprove:
call DeleteFile
cp a ; Set zero flag
ret
HandleOverwriteDeny:
or 1 ; Clear zero flag
ret
FileExistsStr:
defm "File exists! Overwrite? (Y/N) \0"
YNPromptStr:
defm "Enter Y or N: \0"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; RESET
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Reset:
rst 0x00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; RENAME FILE (MV)
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Mv:
; Check dos_argc
ld a, (dos_argc)
cp 2
jr z, Mv00
;; Argc error
ld hl, mvstr
TwoFilenamesArgcError:
ld de, ExpectsExactlyTwoArgsStr
ld bc, FilenameStr
jp Print3Strings
Mv00:
call ValidateTwoFilesFirstExistsHandleOverwrite
ret nz
MvCore:
ld hl, (dos_argv)
ld de, (dos_argv+2)
call RenameFile
ret
BadFileCallback:
ld hl, BadFileMsg
call PrintStringA
ret
BadFileMsg:
defm "Bad command or filename.\r\n\0"
ValidateTwoFilesFirstExistsHandleOverwrite:
call ValidateOneExistingFile
ret nz
ld hl, (dos_argv+2)
call ValidateFilename
ret nz
call HandleOverwrite
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; REMOVE FILE (RM)
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Rm:
; Check dos_argc
ld a, (dos_argc)
cp 1
jr z, RmValidate
;; Argc error
ld hl, rmstr
jp OneFilenameArgcError
RmValidate:
call ValidateOneExistingFile
ret nz
call DeleteFile
jp nz, BadFileCallback
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; CLEAR SCREEN (CLS)
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Cls:
ld hl, clsstring
call PrintStringA
ret
clsstring:
defb 0x1b
defm "[2J"
defb 0x1b
defm "[H\0"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; COPY FILE (CP)
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Cp:
; Check dos_argc
ld a, (dos_argc)
cp 2
jr z, Cp00
;; Argc error
ld hl, cpstr
jp TwoFilenamesArgcError
Cp00:
call ValidateTwoFilesFirstExistsHandleOverwrite
ret nz
ld hl, (dos_argv)
ld de, (dos_argv+2)
call CopyFile
jp nz, BadFileCallback
ld hl, (dos_argv+2)
ld de, (fileaddr)
call WriteFile
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; LOAD FILE
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DosLoadFile:
; Check dos_argc
ld a, (dos_argc)
cp 1
jr z, LoadValidate
cp 2
jr z, LoadValidate
;; Argc error
ld hl, loadstr
OneFilenameOptionalAddressArgcError:
ld de, ExpectsOneOrTwoArgStr
ld bc, FilenameAddrStr
jp Print3Strings
ExpectsOneOrTwoArgStr:
defm " expects one or two args \0"
FilenameAddrStr:
defm "(8.3 filename and optional memory address)\r\n\0"
LoadValidate:
call ValidateOneExistingFile
ret nz
; At this point, we know the file exists
; and the current dent is pointing at it
; If we only got the one argument,
; load file to location specified by
; FILEADDR sys var
ld a, (dos_argc)
cp 1
jr z, LoadFileToFileAddr
; If we got a second arg, treat it as the
; loading address
ld hl, (dos_argv+2)
call ValidateAddress
ret nz
; Parse address and store on stack
call Read16bit
ld d, b
ld e, c
jr LoadFile
LoadFileToFileAddr:
ld de, (fileaddr)
; Dent *should* now be pointed at our target
; Read the file into memory
LoadFile:
ld hl, (dos_argv)
ld a, 0
call ReadFile
jp nz, BadFileCallback
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Default Handler
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Default:
ld hl, dos_linebuf
call StatFile
jp nz, BadFileCallback
; At this point, we know the file exists
; Is this a batch file?
ld hl, dos_linebuf
push hl
call StrLen
sub 4 ; .bat
ld b, a
DefaultIncLoop:
inc hl
djnz DefaultIncLoop
ld de, BatchExtensionStr
call StrCmp
pop hl
; If so, treat it as such
jp z, BatchCallback
; If not, treat it as Z80 machine code