-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdos.cpp
1079 lines (1010 loc) · 32.6 KB
/
bdos.cpp
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
/**
bdos.cpp - CP/M 2.2 compatible Basic Disk Operating System (BDOS)
Copyright (C) 2020 Costin STROIE <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bdos.h"
// BDOS CALLS
const char BDOS_00[] PROGMEM = "WBOOT";
const char BDOS_01[] PROGMEM = "GETCON";
const char BDOS_02[] PROGMEM = "OUTCON";
const char BDOS_03[] PROGMEM = "GETRDR";
const char BDOS_04[] PROGMEM = "PUNCH";
const char BDOS_05[] PROGMEM = "LIST";
const char BDOS_06[] PROGMEM = "DIRCIO";
const char BDOS_07[] PROGMEM = "GETIOB";
const char BDOS_08[] PROGMEM = "SETIOB";
const char BDOS_09[] PROGMEM = "PRTSTR";
const char BDOS_0A[] PROGMEM = "RDBUFF";
const char BDOS_0B[] PROGMEM = "GETCSTS";
const char BDOS_0C[] PROGMEM = "GETVER";
const char BDOS_0D[] PROGMEM = "RSTDSK";
const char BDOS_0E[] PROGMEM = "SETDSK";
const char BDOS_0F[] PROGMEM = "OPENFIL";
const char BDOS_10[] PROGMEM = "CLOSEFIL";
const char BDOS_11[] PROGMEM = "GETFST";
const char BDOS_12[] PROGMEM = "GETNXT";
const char BDOS_13[] PROGMEM = "DELFILE";
const char BDOS_14[] PROGMEM = "READSEQ";
const char BDOS_15[] PROGMEM = "WRTSEQ";
const char BDOS_16[] PROGMEM = "FCREATE";
const char BDOS_17[] PROGMEM = "RENFILE";
const char BDOS_18[] PROGMEM = "GETLOG";
const char BDOS_19[] PROGMEM = "GETCRNT";
const char BDOS_1A[] PROGMEM = "PUTDMA";
const char BDOS_1B[] PROGMEM = "GETALOC";
const char BDOS_1C[] PROGMEM = "WRTPRTD";
const char BDOS_1D[] PROGMEM = "GETROV";
const char BDOS_1E[] PROGMEM = "SETATTR";
const char BDOS_1F[] PROGMEM = "GETPARM";
const char BDOS_20[] PROGMEM = "GETUSER";
const char BDOS_21[] PROGMEM = "RDRANDOM";
const char BDOS_22[] PROGMEM = "WTRANDOM";
const char BDOS_23[] PROGMEM = "FILESIZE";
const char BDOS_24[] PROGMEM = "SETRAN";
const char BDOS_25[] PROGMEM = "LOGOFF";
const char BDOS_26[] PROGMEM = "RTN";
const char BDOS_27[] PROGMEM = "RTN";
const char BDOS_28[] PROGMEM = "WTSPECL";
const char* const BDOS_CALLS[] PROGMEM = {BDOS_00, BDOS_01, BDOS_02, BDOS_03, BDOS_04, BDOS_05, BDOS_06, BDOS_07,
BDOS_08, BDOS_09, BDOS_0A, BDOS_0B, BDOS_0C, BDOS_0D, BDOS_0E, BDOS_0F,
BDOS_10, BDOS_11, BDOS_12, BDOS_13, BDOS_14, BDOS_15, BDOS_16, BDOS_17,
BDOS_18, BDOS_19, BDOS_1A, BDOS_1B, BDOS_1C, BDOS_1D, BDOS_1E, BDOS_1F,
BDOS_20, BDOS_21, BDOS_22, BDOS_23, BDOS_24, BDOS_25, BDOS_26, BDOS_27,
BDOS_28
};
BDOS::BDOS(I8080 * cpu, RAM * ram, DRIVE * drv, BIOS * bios): cpu(cpu), ram(ram), drv(drv), bios(bios) {
}
BDOS::~BDOS() {
}
// Prepare the BDOS
void BDOS::init() {
Serial.print(F("eCPM: Initializing BDOS: "));
// Serial number, 6 bytes
uint8_t cpmSerialNo[] = {0, 22, 0, 0, 0, 0};
ram->write(BDOSCODE, cpmSerialNo, sizeof(cpmSerialNo));
// BDOS entry (FBASE)
ram->setByte(BDOSCODE + 6, 0xC3); // JP BDOSENTRY
ram->setWord(BDOSCODE + 7, BDOSENTRY);
// BDOS dispatcher (FBASE1)
ram->setByte(BDOSENTRY, 0xDB); // IN A, (0x00)
ram->setByte(BDOSENTRY + 1, 0x00);
ram->setByte(BDOSENTRY + 2, 0xC9); // RET
Serial.printf("0x%04X\r\n", BDOSCODE);
#ifdef DEBUG
ram->hexdump(BDOSCODE, BDOSCODE + 0x10, "BDOS");
ram->hexdump(BDOSENTRY, BDOSENTRY + 0x10, "BDOS ENTRY");
#endif
}
// Dispatch the BDOS call
uint8_t BDOS::call(uint16_t port) {
uint16_t w;
uint8_t b, count, eparam;
char c;
// Save the (DE) parameters
params = cpu->regDE();
eparam = (uint8_t)(params & 0x00FF);
// Get function number
func = cpu->regC();
// Keep single register function here
cpu->regC(eparam);
// HL is address of the function
cpu->regHL(BDOSENTRY);
// Clear return status
result = 0x0000;
#ifdef DEBUG_BDOS_CALLS
Serial.print(F("\r\n\t\tBDOS call 0x"));
Serial.print(func, HEX);
if (func <= 41) {
char buf[8];
Serial.print("\t");
strcpy_P(buf, (char*)pgm_read_dword(&(BDOS_CALLS[func])));
Serial.print(buf);
}
Serial.print(F("\r\n"));
cpu->trace();
#endif
// Dispatch call
switch (func) {
case 0x00: // WBOOT
// System reset
bios->wboot();
break;
case 0x01: // GETCON
// Function to get a character from the console device.
c = bios->conin();
result = c;
if (c == 0x0A or c == 0x0D or c == 0x09 or c == 0x08 or c >= ' ')
cpu->regE(c);
else
// A regular control char so don't echo
break;
case 0x02: // OUTCON
// Function to output (E) to the console device and expand tabs if necessary.
bios->conout(eparam);
break;
case 0x03: // GETRDR
// Function to get a character from the tape reader device.
result = bios->reader();
break;
case 0x04: // PUNCH
// Auxiliary (Punch) output
bios->punch(eparam);
break;
case 0x05: // LIST
// Printer output
bios->list(eparam);
break;
case 0x06: // DIRCIO
// Function to perform direct console i/o. If (C) contains (FF)
// then this is an input request. Otherwise we are to output (C).
if (eparam == 0xFF) {
if (bios->consts() == 0xFF)
result = bios->conin();
else
result = 0x0000;
}
else
bios->conout(eparam);
break;
case 0x07: // GETIOB
// Function to return the i/o byte.
result = ram->getByte(IOBYTE);
bios->ioByte(result & 0x00FF);
break;
case 0x08: // SETIOB
// Function to set the i/o byte.
bios->ioByte(eparam);
ram->setByte(IOBYTE, eparam);
break;
case 0x09: // PRTSTR
// Function to print the character string pointed to by (DE)
// on the console device. The string ends with a '$'.
w = cpu->regDE();
b = ram->getByte(w++);
while (b != '$') {
bios->conout(b);
b = ram->getByte(w++);
}
cpu->regDE(w);
break;
case 0x0A: // RDBUFF
// Function to execute a buffered read.
// DE = Address of buffer
// Read (DE) bytes from the console
// Return: A = Number of chars read
// (DE) = First char
// Get the number of characters to read
w = cpu->regDE();
b = ram->getByte(w);
w++;
count = 0;
// Very simple line input
while (b) {
c = bios->conin();
if (c == 3 && count == 0) {
// ^C
Serial.write("^C");
//Status = 2;
break;
}
else if (c == 5)
// ^E
Serial.write("\r\n");
else if ((c == 0x08 || c == 0x7F) && count > 0) {
// ^H and DEL
Serial.write("\b \b");
count--;
continue;
}
else if (c == 0x0A || c == 0x0D) {
// ^J and ^M
break;
}
else if (c == 18) {
// ^R
Serial.write("#\r\n ");
for (uint8_t j = 1; j <= count; ++j)
bios->conout(ram->getByte(w + j));
}
else if (c == 21) {
// ^U
Serial.write("#\r\n ");
w = cpu->regDE();
b = ram->getByte(w);
w++;
count = 0;
}
else if (c == 24) {
// ^X
for (uint8_t j = 0; j < count; ++j)
Serial.write("\b \b");
w = cpu->regDE();
b = ram->getByte(w);
w++;
count = 0;
}
else if (c < 0x20 || c > 0x7E)
// Invalid character
continue;
bios->conout(c);
++count;
ram->setByte(w + count, c);
// Reached the expected count
if (count == b)
break;
}
// Save the number of characters read
ram->setByte(w, count);
// Gives a visual feedback that read ended
bios->conout('\r');
break;
case 0x0B: // GETCSTS
// Function to interigate the console device.
result = bios->consts();
break;
case 0x0C: // GETVER
// Function to return the current cp/m version number.
// A=L=0x22 => 2.2
// B=H=0x00 => 8080, CP/M
result = 0x0022;
break;
case 0x0D: // RSTDSK
// Function to reset the disk system.
cDrive = 0; // Select drive 'A'
rwoVector = 0x0000; // Clear write protect vector
logVector = 0x0001; // Reset log in vector
ramDMA = TBUFF; // Setup default DMA address
// Check if there is a $$$.SUB on the boot disk
result = drv->checkSUB(cDrive, cUser);
break;
case 0x0E: // SETDSK
// Function to set the active disk number.
result = 0xFF;
// Check if this is a drive change
if (eparam != cDrive) {
// Save the current drive number
tDrive = cDrive;
// Change the drive
cDrive = eparam;
// Check it
if (selDrive(cDrive + 1)) {
// Set the drive log in vector
logVector = logVector | (1 << cDrive);
// Check if there is a $$$.SUB on the boot disk
result = drv->checkSUB(cDrive, cUser);
}
else
// Error, restore the current drive
cDrive = tDrive;
}
break;
case 0x0F: // OPENFIL
// Function to open a specified file.
result = 0xFF;
// Read the FCB from RAM
readFCB();
// Select the drive
if (selDrive(fcb.dr)) {
// Get the filename
fcb2cname(fcb, fName);
// Try to open it
if (drv->open(fName)) {
// Get the file size (in blocks)
fSize = drv->fileSize(fName) / sizBK;
// Reset S1 and S2
fcb.s1 = 0x00;
fcb.s2 = 0x80; // set unmodified flag
// Set the record count
fcb.rc = fSize > maxRC ? maxRC : (uint8_t)fSize;
// Clean up allocation
for (uint8_t i = 0; i < 16; i++)
fcb.al[i] = 0x00;
// Success
result = 0x00;
}
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x10: // CLOSEFIL
// Function to close a specified file.
result = 0xFF;
// Read the FCB from RAM
readFCB();
// Select the drive
if (selDrive(fcb.dr)) {
// Check if the file has been modifed
if (!(fcb.s2 & 0x80)) {
// Check if the drive is write protected
if (!(rwoVector & (1 << fcb.dr))) {
// Get the filename on SD card
fcb2cname(fcb, fName);
// Check if this file is '$$$.SUB', whose FCB is at BATCHFCB RAM address
// This file is written by SUBMIT.COM from the *.SUB file, from the last
// line to the first, one line per record. CCP reads the last record
// (which contains the next line) then decrements the RC field and closes
// the file, efectivelly trimming the last record.
if (ramFCB == BATCHFCB)
// Truncate it to fcb.rc CP/M records so SUBMIT.COM can work
drv->truncate(fName, fcb.rc);
// Close the file
drv->close(fName);
result = 0x00;
}
else
// Return error 4 if write protected
bdosError(4);
}
else
// Success
result = 0x00;
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x11: // GETFST
// Function to return the first occurence of a specified file name.
result = 0xFF;
// Read the FCB from RAM, address in DE
readFCB();
// Select the drive
if (selDrive(fcb.dr)) {
// Keep the drive letter, user hexcode and CP/M filename
// (pattern, actually) in fName: 'A0???????????'
/*
fName[FNDRIVE] = (fcb.dr && fcb.dr != '?') ? (fcb.dr + 'A' - 1) : (cDrive + 'A');
fName[FNUSER] = toHEX(cUser);
memcpy(fName + FNFILE, fcb.fn, 11);
fName[FNZERO] = '\0';
*/
fcb2cname(fcb, fName);
// Check if we need to find file from all users
fAllUsers = fcb.dr == '?';
// Check if we should report all extents
fAllExnts = fcb.ex == '?';
// FIXME
if (fAllUsers)
result = drv->findFirst(fName, fSize);
else
result = drv->findFirst(fName, fSize);
if (result == 0x00) {
// Reset direntry file records, extents and allocation blocks
fRecs = 0;
fExts = 0;
fExtU = 0;
fAllB = 0x10;
// On return, fName contains the drive letter,
// hex user code and the CP/M name.
// Create a directory entry
dirEntry(fName + FNFILE, frHEX(fName[FNUSER]), fSize);
}
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x12: // GETNXT
// Function to return the next occurence of a file name.
result = 0xFF;
// Check if all file records have been reported
if (fRecs > 0 and fAllExnts) {
// Create a new directory entry
dirEntry(fName + FNFILE, frHEX(fName[FNUSER]), fSize);
result = 0x00;
}
else
// Select the drive
if (selDrive(fcb.dr)) {
// FIXME
if (fAllUsers)
result = drv->findNext(fName, fSize);
else
result = drv->findNext(fName, fSize);
if (result == 0x00) {
// Reset direntry file records, extents and allocation blocks
fRecs = 0;
fExts = 0;
fExtU = 0;
fAllB = 0x10;
// On return, fName contains the drive letter,
// hex user code and CP/M name
// Create a new directory entry
dirEntry(fName + FNFILE, frHEX(fName[1]), fSize);
}
}
break;
case 0x13: // DELFILE
// Function to delete a file by name.
result = 0xFF;
// Read the FCB from RAM, address in DE
readFCB();
// Select the drive
if (selDrive(fcb.dr)) {
// Check if the drive is write protected
if (!(rwoVector & (1 << fcb.dr))) {
// Keep the drive letter, user hexcode and CP/M filename
// (pattern, actually) in fName: 'A0???????????'
/*
fName[FNDRIVE] = fcb.dr + 'A';
fName[FNUSER] = toHEX(cUser);
memcpy(fName + FNFILE, fcb.fn, 11);
fName[FNZERO] = '\0';
*/
fcb2cname(fcb, fName);
result = drv->findFirst(fName, fSize);
// Now fName will contain individual matching files
while (result != 0xFF) {
// Delete it, the full file name starts at FNHOST
drv->remove(fName);
// Find the next one
result = drv->findNext(fName, fSize);
}
// Success
result = 0x00;
}
else
// Return error 4 if write protected
bdosError(4);
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x14: // READSEQ
// Function to execute a sequential read of the specified record number.
result = 0xFF;
// Read the FCB from RAM, address in DE
readFCB();
// Get position from FCB
fRec = (fcb.s2 & mskS2) * recS2 + fcb.ex * recEX + fcb.cr;
fPos = fRec * sizBK;
// Select the drive
if (selDrive(fcb.dr)) {
// Get the filename
fcb2cname(fcb, fName);
// Read one block
result = drv->read(ramDMA, fName, fPos);
// Check the result
if (!result) {
// Increase file record and seek position with one block
fRec++;
fPos += sizBK;
// Adjust FCB
fcb.s2 = (fRec / recS2) | (fcb.s2 & 0x80); // extent, high byte
fcb.ex = (fRec % recS2) / recEX; // extent, low byte
fcb.cr = fRec % recEX; // cr (current record)
}
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x15: // WRTSEQ
// Function to write the net sequential record.
result = 0xFF;
// Read the FCB from RAM, address in DE
readFCB();
// Get position from FCB
fRec = (fcb.s2 & mskS2) * recS2 + fcb.ex * recEX + fcb.cr;
fPos = fRec * sizBK;
// Select the drive
if (selDrive(fcb.dr)) {
// Check if the drive is write protected
if (!(rwoVector & (1 << fcb.dr))) {
// Get the filename on SD card
fcb2cname(fcb, fName);
// Write one block
result = drv->write(ramDMA, fName, fPos);
// Check the result
if (!result) {
// Increase file record and seek position with one block
fRec++;
fPos += sizBK;
// Adjust FCB
fcb.s2 = (fRec / recS2) & 0x7F; // extent, high byte, reset unmodified flag
fcb.ex = (fRec % recS2) / recEX; // extent, low byte
fcb.cr = fRec % recEX; // cr (current record)
}
}
else
// Return error 4 if write protected
bdosError(4);
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x16: // FCREATE
// Create a file function.
result = 0xFF;
// Read the FCB from RAM, address in DE
readFCB();
// Select the drive
if (selDrive(fcb.dr)) {
// Check if the drive is write protected
if (!(rwoVector & (1 << fcb.dr))) {
// Get the filename
fcb2cname(fcb, fName);
// Create the file
if (drv->create(fName)) {
// Initializes the FCB
fcb.ex = 0x00;
fcb.s1 = 0x00;
fcb.s2 = 0x00;
fcb.rc = 0x00;
// Clean allocation
for (uint8_t i = 0; i < 16; i++)
fcb.al[i] = 0x00;
fcb.cr = 0x00;
result = 0x00;
}
}
else
// Return error 4 if write protected
bdosError(4);
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x17: // RENFILE
// Function to rename a file.
result = 0xFF;
// Read the FCB from RAM, address in DE
readFCB();
// Select the drive
if (selDrive(fcb.dr)) {
// Check if the drive is write protected
if (!(rwoVector & (1 << fcb.dr))) {
uint16_t ramNewFCB = ramFCB + 16;
// Prevents rename from moving files among drives
ram->setByte(ramNewFCB, ram->getByte(ramFCB));
// Create the new FCB object
FCB_t newfcb;
ram->read(ramNewFCB, newfcb.buf, 36);
// Get the filename on SD card
fcb2cname(fcb, fName);
// Get the new filename on SD card
char newName[128];
fcb2cname(newfcb, newName);
// Rename the file
if (drv->rename(fName, newName))
result = 0x00;
}
else
// Return error 4 if write protected
bdosError(4);
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x18: // GETLOG
// Function to return the login vector.
result = logVector;
break;
case 0x19: // GETCRNT
// Function to return the current disk assignment.
result = cDrive;
break;
case 0x1A: // PUTDMA
// Function to set the dma address.
ramDMA = cpu->regDE();
cpu->regBC(ramDMA);
bios->setdma();
break;
case 0x1B: // GETALOC
// Function to return the allocation vector.
result = alcVector;
break;
case 0x1C: // WRTPRTD
// Function to write protect the current disk.
logVector = logVector | (1 << cDrive);
break;
case 0x1D: // GETROV
// Function to return the read-only status vector.
result = rwoVector;
break;
case 0x1E: // SETATTR
// Function to set the file attributes (read-only, system).
break;
case 0x1F: // GETPARM
// Function to return the address of the disk parameter block for the current drive.
result = BIOSDPB;
break;
case 0x20: // GETUSER
// Function to get or set the user number. If (E) was (FF)
// then this is a request to return the current user number.
// Else set the user number from (E).
if (eparam == 0xFF)
result = cUser & 0x000F;
else {
cUser = eparam & 0x0F;
drv->mkDir(cDrive, cUser);
}
break;
case 0x21: // RDRANDOM
// Function to read a random record from a file.
result = 0xFF;
// Read the FCB from RAM, address in DE
readFCB();
// Compute the file record and seek position
fRec = fcb.r2 * 0x010000UL + fcb.r1 * 0x0100UL + fcb.r0;
fPos = fRec * sizBK;
// Select the drive
if (selDrive(fcb.dr)) {
// Get the filename
fcb2cname(fcb, fName);
// Read one block
result = drv->read(ramDMA, fName, fPos);
// Check the result
if (result == 0 or result == 1 or result == 4) {
// Adjust FCB
fcb.s2 = (fRec / recS2) | (fcb.s2 & 0x80); // extent, high byte
fcb.ex = (fRec % recS2) / recEX; // extent, low byte
fcb.cr = fRec % recEX; // cr (current record)
}
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x22: // WTRANDOM
// Function to write a random record to a file.
result = 0xFF;
// Read the FCB from RAM, address in DE
readFCB();
// Compute the file record and seek position
fRec = fcb.r2 * 0x010000UL + fcb.r1 * 0x0100UL + fcb.r0;
fPos = fRec * sizBK;
// Select the drive
if (selDrive(fcb.dr)) {
// Check if the drive is write protected
if (!(rwoVector & (1 << fcb.dr))) {
// Get the filename
fcb2cname(fcb, fName);
// Write one block
result = drv->write(ramDMA, fName, fPos);
// Check the result
if (!result) {
// Adjust FCB
fcb.s2 = (fRec / recS2) & 0x7F; // extent, high byte, reset unmodified flag
fcb.ex = (fRec % recS2) / recEX; // extent, low byte
fcb.cr = fRec % recEX; // cr (current record)
}
}
else
// Return error 4 if write protected
bdosError(4);
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x23: // FILESIZE
// Function to compute the size of a random file.
result = 0xFF;
// Read the FCB from RAM
readFCB();
// Select the drive
if (selDrive(fcb.dr)) {
// Get the filename
fcb2cname(fcb, fName);
// Get the file size (in blocks)
fSize = drv->fileSize(fName) / sizBK;
if (fSize != -1) {
fcb.r0 = fSize & 0xFF;
fcb.r1 = (fSize >> 8) & 0xFF;
fcb.r2 = (fSize >> 16) & 0xFF;
// Success
result = 0x00;
}
}
// Write the FCB back into RAM
writeFCB();
break;
case 0x24: // SETRAN
// Function to return the random record position of a given
// file which has been read in sequential mode up to now.
result = 0x00;
// Read the FCB from RAM
readFCB();
// Get the last used record (CR, EX, S2)
fRec = fcb.cr & mskCR;
fRec += (fcb.ex & mskEX) * recEX;
fRec += (fcb.s2 & mskS2) * recS2;
// Compute the random position and store it into R0, R1 and R2
fcb.r0 = fRec & 0xFF;
fcb.r1 = (fRec >> 8) & 0xFF;
fcb.r2 = (fRec >> 16) & 0xFF;
// Write the FCB back into RAM
writeFCB();
break;
case 0x25: // LOGOFF
// This allows a program to log off any drives.
break;
case 0x26: // DRVACCESS
// Locks one or more disc drives (not implemented MP/M only).
result = 0x0000;
break;
case 0x27: // DRVFREE
// Releases locks on disc drives (not implemented MP/M only).
result = 0x0000;
break;
case 0x28: // WTSPECL
// For the case where we are writing to unused disk space, this
// space will be zeroed out first.
result = 0xFF;
// Read the FCB from RAM, address in DE
readFCB();
// Compute the file record and seek position
fRec = fcb.r2 * 0x010000UL + fcb.r1 * 0x0100UL + fcb.r0;
fPos = fRec * sizBK;
// Select the drive
if (selDrive(fcb.dr)) {
// Check if the drive is write protected
if (!(rwoVector & (1 << fcb.dr))) {
// Get the filename
fcb2cname(fcb, fName);
// Write one block
result = drv->write(ramDMA, fName, fPos);
// Check the result
if (!result) {
// Adjust FCB
fcb.s2 = (fRec / recS2) & 0x7F; // extent, high byte, reset unmodified flag
fcb.ex = (fRec % recS2) / recEX; // extent, low byte
fcb.cr = fRec % recEX; // cr (current record)
}
}
else
// Return error 4 if write protected
bdosError(4);
}
// Write the FCB back into RAM
writeFCB();
break;
default:
#ifdef DEBUG_BDOS_CALLS
// Show unimplemented BDOS calls only when debugging
Serial.print(F("\r\nUnimplemented BDOS call 0x"));
Serial.print(func, HEX);
Serial.print("\r\n");
cpu->trace();
#endif
break;
}
// Get return status
cpu->regHL(result);
// Force version 1.4 compatibility
cpu->regB((uint8_t)cpu->regH());
cpu->regA((uint8_t)cpu->regL());
// Return
return cpu->regA();
}
// Display and return the error
void BDOS::bdosError(uint8_t err) {
Serial.print(F("\r\nBdos Err On "));
Serial.print((char)(cDrive + 'A'));
Serial.print(F(" : "));
switch (err) {
case 1:
Serial.print(F("Bad Sector"));
break;
case 2:
Serial.print(F("Select"));
break;
case 3:
Serial.print(F("File R/O"));
break;
case 4:
Serial.print(F("R/O"));
break;
}
Serial.print("\r\n");
// Wait for a keypress
bios->conin();
// Restore the TDRIVE byte
cDrive = tDrive;
ram->setByte(TDRIVE, cUser << 4 | cDrive);
// Always reboot on these errors.
bios->wboot();
}
// Read the FCB from RAM, register DE has the address.
// Store the address in ramFCB and the FCB in fcb
void BDOS::readFCB() {
// Get the FCB address
ramFCB = cpu->regDE();
// Create the FCB object
ram->read(ramFCB, fcb.buf, 36);
// Uppercase file name and type
for (uint8_t i = 0; i < 11; i++)
*(fcb.fn + i) = toupper(*(fcb.fn + i) & 0x7F);
#ifdef DEBUG_FCB_READ
// Show FCB
showFCB(BDOS_CALLS[func]);
#endif
}
// Write the FCB back into RAM.
// The address is already in ramFCB and the FCB in fcb
void BDOS::writeFCB() {
// Write the FCB back into RAM
ram->write(ramFCB, fcb.buf, 36);
#ifdef DEBUG_FCB_WRITE
// Show FCB
showFCB(BDOS_CALLS[func]);
#endif
}
// Debug FCB
void BDOS::showFCB(const char* comment) {
char buf[80];
// Start with a new line
Serial.print("\r\n");
// Print the comment
if (comment[0]) {
Serial.print("\r\n; ");
Serial.print(comment);
}
Serial.print(F("\r\nDR FN TP EX S1 S2 RC CR R0 R1 R2"));
Serial.print("\r\n ");
Serial.print((char)(fcb.dr == 0 ? '*' : ((fcb.dr == '?') ? '?' : (fcb.dr + 'A' - 1))));
Serial.print(' ');
for (uint8_t i = 0; i < 8; i++) Serial.print((char)fcb.fn[i]);
Serial.print(' ');
for (uint8_t i = 0; i < 3; i++) Serial.print((char)fcb.tp[i]);
sprintf_P(buf, PSTR(" %02X %02X %02X %02X %02X %02X %02X %02X "),
fcb.ex, fcb.s1, fcb.s2, fcb.rc, fcb.cr, fcb.r0, fcb.r1, fcb.r2);
Serial.print(buf);
//for (uint8_t i = 0; i < 16; i++) {
// sprintf_P(buf, PSTR(" %02X"), fcb.al[i]);
// Serial.print(buf);
//}
Serial.print("\r\n");
}
// Create a directory entry into RAM
void BDOS::dirEntry(char *cname, uint8_t uid, uint32_t fsize) {
uint8_t blocks, i;
// Create a new directory entry object
DIR_t de;
// Zero it out
memset(de.buf, 0, 32);
// Store the user id
de.uu = uid;
// Copy the file name and type from cname (8+3 bytes)
memcpy(de.fn, cname, 11);
// Check if it's a new file and we should report all extents
if (fRecs == 0) {
// Compute the number of records
fRecs = (fsize + sizBK - 1) / sizBK;
// Compute the number of extents
fExts = (fRecs + recEX - 1) / recEX;
// Reset the used extents (start with zero)
fExtU = 0;
}
// Extents per directory entry
uint8_t expde = bios->dpb.exm + 1;
// Check if the file fits in a single directory entry
if (fExts <= expde) { // for now, 2 extents
// Yes, compute the ex, s2 and rc fields
if (fExts > 0) {
// Maximum value of 'ex' is 31 (mskEX)
// Since this might be the tail of multi-extents file, we need
// to add the count of already used extents
uint8_t extents = fExts + fExtU - 1;
de.ex = extents % (mskEX + 1);
de.s2 = extents / (mskEX + 1);
// The 'rc' is the number of the records not already allocated
// in previous extents
de.rc = fRecs - (recEX * (fExts - 1));
}
// Get the number of required allocation blocks
// (for now, 32 recors in one block)
blocks = ((fRecs + bios->dpb.blm) / (bios->dpb.blm + 1));
// Reset the records and extents counters
fRecs = 0;
fExts = 0;
fExtU = 0;
}
else {
// No, we need more extents, max out this directory entry
// Since this might be in the middle of multi-extents file,
// we need to add the count of already used extents
uint8_t extents = expde + fExtU - 1;
de.ex = extents % (mskEX + 1);
de.s2 = extents / (mskEX + 1);
// The 'rc' will be the number of the records in an extent
de.rc = recEX;
// Required allocation blocks may be 8 or 16, according to DSM
// (for now, the disk is 8MB, so 8 blocks of 16 bits)
blocks = bios->dpb.dsm <= 255 ? 16 : 8;
// Update the records and extents counts for next call, using
// the value of extents per directory entry
fRecs -= expde * recEX;
fExts -= expde;
fExtU += expde;
}
// Fill up the appropriate number of allocation blocks
if (bios->dpb.dsm <= 255)
// Maximum of 16 blocks of 8 bits
for (i = 0; i < blocks; i++)
de.al[i] = (uint8_t)fAllB++;
else
// Maximum of 8 blocks of 16 bits
for (i = 0; i < 2 * blocks; i += 2) {