-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathcpm.h
More file actions
2156 lines (1970 loc) · 59.4 KB
/
Copy pathcpm.h
File metadata and controls
2156 lines (1970 loc) · 59.4 KB
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
#ifndef CPM_H
#define CPM_H
enum eBIOSFunc {
// CP/M 2.2 Stuff
B_BOOT = 0,
B_WBOOT = 3,
B_CONST = 6,
B_CONIN = 9,
B_CONOUT = 12,
B_LIST = 15,
B_AUXOUT = 18,
B_READER = 21,
B_HOME = 24,
B_SELDSK = 27,
B_SETTRK = 30,
B_SETSEC = 33,
B_SETDMA = 36,
B_READ = 39,
B_WRITE = 42,
B_LISTST = 45,
B_SECTRAN = 48,
// CP/M 3.0 Stuff
B_CONOST = 51,
B_AUXIST = 54,
B_AUXOST = 57,
B_DEVTBL = 60,
B_DEVINI = 63,
B_DRVTBL = 66,
B_MULTIO = 69,
B_FLUSH = 72,
B_MOVE = 75,
B_TIME = 78,
B_SELMEM = 81,
B_SETBNK = 84,
B_XMOVE = 87,
B_USERF = 90, // Used by internal CCP to return to prompt
B_RESERV1 = 93,
B_RESERV2 = 96
};
enum eBDOSFunc {
// CP/M 2.2 Stuff
P_TERMCPM = 0,
C_READ = 1,
C_WRITE = 2,
A_READ = 3,
A_WRITE = 4,
L_WRITE = 5,
C_RAWIO = 6,
A_STATIN = 7,
A_STATOUT = 8,
C_WRITESTR = 9,
C_READSTR = 10,
C_STAT = 11,
S_BDOSVER = 12,
DRV_ALLRESET = 13,
DRV_SET = 14,
F_OPEN = 15,
F_CLOSE = 16,
F_SFIRST = 17,
F_SNEXT = 18,
F_DELETE = 19,
F_READ = 20,
F_WRITE = 21,
F_MAKE = 22,
F_RENAME = 23,
DRV_LOGINVEC = 24,
DRV_GET = 25,
F_DMAOFF = 26,
DRV_ALLOCVEC = 27,
DRV_SETRO = 28,
DRV_ROVEC = 29,
F_ATTRIB = 30,
DRV_PDB = 31,
F_USERNUM = 32,
F_READRAND = 33,
F_WRITERAND = 34,
F_SIZE = 35,
F_RANDREC = 36,
DRV_RESET = 37,
DRV_ACCESS_MPM = 38, // This is an MP/M function that is not supported under CP/M 3.
DRV_FREE_MPM = 39, // This is an MP/M function that is not supported under CP/M 3.
F_WRITEZF = 40,
// CP/M 3.0 Stuff
F_TESTWRITE = 41,
F_LOCKFILE = 42,
F_UNLOCKFILE = 43,
F_MULTISEC = 44,
F_ERRMODE = 45,
DRV_SPACE = 46,
P_CHAIN = 47,
DRV_FLUSH = 48,
S_SCB = 49,
S_BIOS = 50,
P_LOAD = 59,
S_RSX = 60,
F_CLEANUP = 98,
F_TRUNCATE = 99,
DRV_SETLABEL = 100,
DRV_GETLABEL = 101,
F_TIMEDATE = 102,
F_WRITEXFCB = 103,
T_SET = 104,
T_GET = 105,
F_PASSWD = 106,
S_SERIAL = 107,
P_CODE = 108,
C_MODE = 109,
C_DELIMIT = 110,
C_WRITEBLK = 111,
L_WRITEBLK = 112,
F_PARSE = 152,
// RunCPM Stuff
F_PINMODE = 220,
F_DREAD = 221,
F_DWRITE = 222,
F_AREAD = 223,
F_AWRITE = 224,
F_SETMASK = 230,
F_BDOSCALL = 231,
F_UPTIME = 248,
F_MAKEDISK = 249,
F_HOSTOS = 250,
F_VERSION = 251,
F_CCPVERSION = 252,
F_CCPADDR = 253,
F_SETCPUSPEED = 254
};
/* see main.c for definition */
#define JP 0xc3
#define CALL 0xcd
#define RET 0xc9
#define INa 0xdb // Triggers a BIOS call
#define OUTa 0xd3 // Triggers a BDOS call
// Interruption handling
#define RST_08 0xcf // RST 08h - BIOS calls
#define RST_10 0xd7 // RST 10h - BDOS calls
#define RST_18 0xdf // RST 18h - Hardware calls
#define NOP 0x00 // No operation
/* set up full PUN and LST filenames to be on drive A: user 0 */
#ifdef USE_PUN
char pun_file[17] = {'A', FOLDERCHAR, '0', FOLDERCHAR, 'P', 'U', 'N', '.', 'T', 'X', 'T', 0};
#endif // ifdef USE_PUN
#ifdef USE_LST
char lst_file[17] = {'A', FOLDERCHAR, '0', FOLDERCHAR, 'L', 'S', 'T', '.', 'T', 'X', 'T', 0};
#endif // ifdef USE_LST
#ifdef PROFILE
unsigned long time_start = 0;
unsigned long time_now = 0;
#endif // ifdef PROFILE
void _PatchBIOS(void) {
uint16 i;
// Patches in the BIOS jump destinations
for (i = 0; i < 99; i = i + 3) {
_RamWrite(BIOSjmppage + i, JP);
_RamWrite16(BIOSjmppage + i + 1, BIOSpage + i);
}
// Patches in the BIOS page content
for (i = 0; i < 99; i = i + 3) {
#ifdef INT_HANDOFF
_RamWrite(BIOSpage + i, RST_08);
_RamWrite(BIOSpage + i + 1, RET);
_RamWrite(BIOSpage + i + 2, NOP);
#else
_RamWrite(BIOSpage + i, OUTa);
_RamWrite(BIOSpage + i + 1, 0xFF);
_RamWrite(BIOSpage + i + 2, RET);
#endif
}
} //_PatchBIOS
void _PatchCPM(void) {
uint16 i;
// ********** Patch CP/M page zero into the memory **********
/* BIOS entry point */
_RamWrite(0x0000, JP); /* JP BIOS+3 (warm boot) */
_RamWrite16(0x0001, BIOSjmppage + 3);
if (Status != STATUS_RESTART) {
/* IOBYTE - Points to Console */
_RamWrite(IOByte, 0x3D);
/* Current drive/user - A:/0 */
_RamWrite(DSKByte, 0x00);
}
/* BDOS entry point (0x0005) */
_RamWrite(0x0005, JP);
_RamWrite16(0x0006, BDOSjmppage + 0x06);
// ********** Patch CP/M Version into the memory so the CCP can see it
#ifdef ABDOS
// Loads the ABDOS.SYS file into memory or throws an error if it doesn't exist
if (_sys_exists((uint8 *)"A/0/ABDOS.SYS")) {
_RamLoad((uint8 *)"A/0/ABDOS.SYS", BDOSjmppage, 0);
} else {
_puts("\r\nABDOS.SYS not found");
exit(1);
}
#else
_RamWrite16(BDOSjmppage, 0x1600);
_RamWrite16(BDOSjmppage + 2, 0x0000);
_RamWrite16(BDOSjmppage + 4, 0x0000);
// Patches in the BDOS jump destination
_RamWrite(BDOSjmppage + 6, JP);
_RamWrite16(BDOSjmppage + 7, BDOSpage);
// Patches in the BDOS page content
#ifdef INT_HANDOFF
_RamWrite(BDOSpage, RST_10);
_RamWrite(BDOSpage + 1, RET);
_RamWrite(BDOSpage + 2, NOP);
#else
_RamWrite(BDOSpage, INa);
_RamWrite(BDOSpage + 1, 0xFF);
_RamWrite(BDOSpage + 2, RET);
#endif
_PatchBIOS();
#endif
// ********** Patch CP/M (fake) Disk Parameter Block after the BDOS call entry **********
i = DPBaddr;
_RamWrite(i++, 0x00); // DEFW spt - Sectors Per Track (256)
_RamWrite(i++, 0x01);
_RamWrite(i++, 0x05); // DEFB bsh - Data allocation "Block Shift Factor" (for 4096 block size)
_RamWrite(i++, 0x1F); // DEFB blm - Data allocation Block Mask (31 for 4096 block size)
_RamWrite(i++, 0x01); // DEFB exm - Data allocation Extent Mask (1 = total blocks > 256)
_RamWrite(i++, 0xF7); // DEFW dsm - Logical disk size in blocks - 1 (2039)
_RamWrite(i++, 0x07);
_RamWrite(i++, 0xFF); // DEFW drm - Maximum directory entries - 1 (1023)
_RamWrite(i++, 0x03);
_RamWrite(i++, 0xFF); // DEFB al0 - Reserved directory blocks
_RamWrite(i++, 0x00); // DEFB al1 - Reserved directory blocks
_RamWrite(i++, 0x00); // DEFW cks - Check area Size (0 for fixed disks)
_RamWrite(i++, 0x00);
_RamWrite(i++, 0x01); // DEFW off - Number of system reserved tracks at the beginning of the ( logical ) disk
_RamWrite(i++, 0x00);
blockShift = _RamRead(DPBaddr + 2);
blockMask = _RamRead(DPBaddr + 3);
extentMask = _RamRead(DPBaddr + 4);
numAllocBlocks = _RamRead16(DPBaddr + 5) + 1;
extentsPerDirEntry = extentMask + 1;
// ********** Patch CP/M (fake) Disk Parameter Header after the Disk Parameter Block **********
_RamWrite(i++, 0); // Addr of the sector translation table
_RamWrite(i++, 0);
_RamWrite(i++, 0); // Workspace
_RamWrite(i++, 0);
_RamWrite(i++, 0);
_RamWrite(i++, 0);
_RamWrite(i++, 0);
_RamWrite(i++, 0);
_RamWrite(i++, 0x80); // Addr of the Sector Buffer
_RamWrite(i++, 0);
_RamWrite(i++, LOW_REGISTER(DPBaddr)); // Addr of the DPB Disk Parameter Block
_RamWrite(i++, HIGH_REGISTER(DPBaddr));
_RamWrite(i++, 0); // Addr of the Directory Checksum Vector
_RamWrite(i++, 0);
_RamWrite(i++, 0); // Addr of the Allocation Vector
_RamWrite(i++, 0);
//
// figure out the number of the first allocation block
// after the directory for the phoney allocation block
// list in _findnext()
firstBlockAfterDir = 0;
i = 0x80;
while (_RamRead(DPBaddr + 9) & i) {
firstBlockAfterDir++;
i >>= 1;
}
if (_RamRead(DPBaddr + 9) == 0xFF) {
i = 0x80;
while (_RamRead(DPBaddr + 10) & i) {
firstBlockAfterDir++;
i >>= 1;
}
}
physicalExtentBytes = logicalExtentBytes * (extentMask + 1);
} // _PatchCPM
#ifdef DEBUGLOG
uint8 LogBuffer[128];
void _logRegs(void) {
uint8 J, I;
uint8 Flags[9] = {'S', 'Z', '5', 'H', '3', 'P', 'N', 'C'};
uint8 c = HIGH_REGISTER(AF);
if ((c < 32) || (c > 126))
c = 46;
for (J = 0, I = LOW_REGISTER(AF); J < 8; ++J, I <<= 1)
Flags[J] = I & 0x80 ? Flags[J] : '.';
sprintf((char *)LogBuffer, " BC:%04x DE:%04x HL:%04x AF:%02x(%c)|%s| IX:%04x IY:%04x SP:%04x PC:%04x\n",
WORD16(BC), WORD16(DE), WORD16(HL), HIGH_REGISTER(AF), c, Flags, WORD16(IX), WORD16(IY), WORD16(SP), WORD16(PC));
_sys_logbuffer(LogBuffer);
} // _logRegs
void _logMem(uint16 address, uint8 amount) { // Amount = number of 16 bytes lines, so 1 CP/M block = 8, not 128
uint8 i, m, c, pos;
uint8 head = 8;
uint8 hexa[] = "0123456789ABCDEF";
for (i = 0; i < amount; ++i) {
pos = 0;
for (m = 0; m < head; ++m)
LogBuffer[pos++] = ' ';
sprintf((char *)LogBuffer, " %04x: ", address);
for (m = 0; m < 16; ++m) {
c = _RamRead(address++);
LogBuffer[pos++] = hexa[c >> 4];
LogBuffer[pos++] = hexa[c & 0x0f];
LogBuffer[pos++] = ' ';
LogBuffer[m + head + 48] = c > 31 && c < 127 ? c : '.';
}
pos += 16;
LogBuffer[pos++] = 0x0a;
LogBuffer[pos++] = 0x00;
_sys_logbuffer(LogBuffer);
}
} // _logMem
void _logChar(char *txt, uint8 c) {
uint8 asc[2];
asc[0] = c > 31 && c < 127 ? c : '.';
asc[1] = 0;
sprintf((char *)LogBuffer, " %s = %02xh:%3d (%s)\n", txt, c, c, asc);
_sys_logbuffer(LogBuffer);
} // _logChar
void _logBiosIn(uint8 ch) {
#ifdef LOGBIOS_NOT
if (ch == LOGBIOS_NOT)
return;
#endif // ifdef LOGBIOS_NOT
#ifdef LOGBIOS_ONLY
if (ch != LOGBIOS_ONLY)
return;
#endif // ifdef LOGBIOS_ONLY
static const char *BIOSCalls[33] =
{
"boot", "wboot", "const", "conin", "conout", "list", "punch/aux", "reader", "home", "seldsk", "settrk", "setsec", "setdma",
"read", "write", "listst", "sectran", "conost", "auxist", "auxost", "devtbl", "devini", "drvtbl", "multio", "flush", "move",
"time", "selmem", "setbnk", "xmove", "userf", "reserv1", "reserv2"};
int index = ch / 3;
if (index < 18) {
sprintf((char *)LogBuffer, "\nBios call: %3d/%02xh (%s) IN:\n", ch, ch, BIOSCalls[index]);
_sys_logbuffer(LogBuffer);
} else {
sprintf((char *)LogBuffer, "\nBios call: %3d/%02xh IN:\n", ch, ch);
_sys_logbuffer(LogBuffer);
}
_logRegs();
} // _logBiosIn
void _logBiosOut(uint8 ch) {
#ifdef LOGBIOS_NOT
if (ch == LOGBIOS_NOT)
return;
#endif // ifdef LOGBIOS_NOT
#ifdef LOGBIOS_ONLY
if (ch != LOGBIOS_ONLY)
return;
#endif // ifdef LOGBIOS_ONLY
sprintf((char *)LogBuffer, " OUT:\n");
_sys_logbuffer(LogBuffer);
_logRegs();
} // _logBiosOut
void _logBdosIn(uint8 ch) {
#ifdef LOGBDOS_NOT
if (ch == LOGBDOS_NOT)
return;
#endif // ifdef LOGBDOS_NOT
#ifdef LOGBDOS_ONLY
if (ch != LOGBDOS_ONLY)
return;
#endif // ifdef LOGBDOS_ONLY
uint16 address = 0;
uint8 size = 0;
static const char *CPMCalls[41] =
{
"System Reset", "Console Input", "Console Output", "Reader Input", "Punch Output", "List Output", "Direct I/O",
"Get IOByte", "Set IOByte", "Print String", "Read Buffered", "Console Status", "Get Version", "Reset Disk",
"Select Disk", "Open File", "Close File", "Search First", "Search Next", "Delete File", "Read Sequential",
"Write Sequential", "Make File", "Rename File", "Get Login Vector", "Get Current Disk", "Set DMA Address",
"Get Alloc", "Write Protect Disk", "Get R/O Vector", "Set File Attr", "Get Disk Params", "Get/Set User",
"Read Random", "Write Random", "Get File Size", "Set Random Record", "Reset Drive", "N/A", "N/A",
"Write Random 0 fill"};
if (ch < 41) {
sprintf((char *)LogBuffer, "\nBdos call: %3d/%02xh (%s) IN from 0x%04x:\n", ch, ch, CPMCalls[ch], _RamRead16(SP) - 3);
_sys_logbuffer(LogBuffer);
} else {
sprintf((char *)LogBuffer, "\nBdos call: %3d/%02xh IN from 0x%04x:\n", ch, ch, _RamRead16(SP) - 3);
_sys_logbuffer(LogBuffer);
}
_logRegs();
switch (ch) {
case 2:
case 4:
case 5:
case 6: {
_logChar("E", LOW_REGISTER(DE));
break;
}
case 9:
case 10: {
address = DE;
size = 8;
break;
}
case 15:
case 16:
case 17:
case 18:
case 19:
case 22:
case 23:
case 30:
case 35:
case 36: {
address = DE;
size = 3;
break;
}
case 20:
case 21:
case 33:
case 34:
case 40: {
address = DE;
size = 3;
_logMem(address, size);
sprintf((char *)LogBuffer, "\n");
_sys_logbuffer(LogBuffer);
address = dmaAddr;
size = 8;
break;
}
default: {
break;
}
} // switch
if (size)
_logMem(address, size);
} // _logBdosIn
void _logBdosOut(uint8 ch) {
#ifdef LOGBDOS_NOT
if (ch == LOGBDOS_NOT)
return;
#endif // ifdef LOGBDOS_NOT
#ifdef LOGBDOS_ONLY
if (ch != LOGBDOS_ONLY)
return;
#endif // ifdef LOGBDOS_ONLY
uint16 address = 0;
uint8 size = 0;
sprintf((char *)LogBuffer, " OUT:\n");
_sys_logbuffer(LogBuffer);
_logRegs();
switch (ch) {
case 1:
case 3:
case 6: {
_logChar("A", HIGH_REGISTER(AF));
break;
}
case 10: {
address = DE;
size = 8;
break;
}
case 20:
case 21:
case 33:
case 34:
case 40: {
address = DE;
size = 3;
_logMem(address, size);
sprintf((char *)LogBuffer, "\n");
_sys_logbuffer(LogBuffer);
address = dmaAddr;
size = 8;
break;
}
case 26: {
address = dmaAddr;
size = 8;
break;
}
case 15:
case 16:
case 17:
case 18:
case 19:
case 22:
case 23:
case 30:
case 35:
case 36: {
address = DE;
size = 3;
break;
}
default: {
break;
}
} // switch
if (size)
_logMem(address, size);
} // _logBdosOut
#endif // ifdef DEBUGLOG
void _Bios(void) {
uint8 ch = LOW_REGISTER(PCX);
uint8 disk[2] = {'A', 0};
#ifdef DEBUGLOG
_logBiosIn(ch);
#endif
switch (ch) {
case B_BOOT: {
Status = STATUS_EXIT; // 0 - Ends RunCPM
break;
}
case B_WBOOT: {
Status = STATUS_RESTART; // 1 - Back to CCP
break;
}
case B_CONST: { // 2 - Console status
SET_HIGH_REGISTER(AF, _chready());
break;
}
case B_CONIN: { // 3 - Console input
SET_HIGH_REGISTER(AF, _getcon());
#ifdef DEBUG
if (HIGH_REGISTER(AF) == DEBUGKEY)
Debug = 1;
#endif // ifdef DEBUG
break;
}
case B_CONOUT: { // 4 - Console output
_putcon(LOW_REGISTER(BC));
break;
}
case B_LIST: { // 5 - List output
break;
}
case B_AUXOUT: { // 6 - Aux/Punch output
break;
}
case B_READER: { // 7 - Reader input (returns 0x1a = device not implemented)
SET_HIGH_REGISTER(AF, 0x1a);
break;
}
case B_HOME: { // 8 - Home disk head
break;
}
case B_SELDSK: { // 9 - Select disk drive
disk[0] += LOW_REGISTER(BC);
HL = 0x0000;
if (_sys_select(&disk[0]))
HL = DPHaddr;
break;
}
case B_SETTRK: { // 10 - Set track number
break;
}
case B_SETSEC: { // 11 - Set sector number
break;
}
case B_SETDMA: { // 12 - Set DMA address
HL = BC;
dmaAddr = BC;
break;
}
case B_READ: { // 13 - Read selected sector
SET_HIGH_REGISTER(AF, 0x00);
break;
}
case B_WRITE: { // 14 - Write selected sector
SET_HIGH_REGISTER(AF, 0x00);
break;
}
case B_LISTST: { // 15 - Get list device status
SET_HIGH_REGISTER(AF, 0x0ff);
break;
}
case B_SECTRAN: { // 16 - Sector translate
HL = BC; // HL=BC=No translation (1:1)
break;
}
case B_CONOST: { // 17 - Return status of current screen output device
SET_HIGH_REGISTER(AF, 0x0ff);
break;
}
case B_AUXIST: { // 18 - Return status of current auxiliary input device
SET_HIGH_REGISTER(AF, 0x00);
break;
}
case B_AUXOST: { // 19 - Return status of current auxiliary output device
SET_HIGH_REGISTER(AF, 0x00);
break;
}
case B_DEVTBL: { // 20 - Return the address of the devices table, or 0 if not implemented
HL = 0x0000;
break;
}
case B_DEVINI: { // 21 - Reinitialise character device number C
break;
}
case B_DRVTBL: { // 22 - Return the address of the drive table
HL = 0x0FFFF;
break;
}
case B_MULTIO: { // 23 - Notify the BIOS of multi sector transfer
break;
}
case B_FLUSH: { // 24 - Write any pending data to disc
SET_HIGH_REGISTER(AF, 0x00);
break;
}
case B_MOVE: { // 25 - Move a block of memory
if (!isXmove) {
srcBank = dstBank = curBank;
srcBankBase = dstBankBase = curBankBase;
}
while (BC--)
RAM[dstBankBase + HL++] = RAM[srcBankBase + DE++];
isXmove = FALSE;
break;
}
case B_TIME: { // 26 - Get/Set SCB time
break;
}
case B_SELMEM: { // 27 - Select memory bank
curBank = HIGH_REGISTER(AF);
if (curBank >= BANKS) // guard against RAM[] overrun on an out-of-range bank
curBank = BANKS - 1;
curBankBase = ((uint32)curBank) << 16; // banks are 0-based: bank N -> RAM offset N*64K
break;
}
case B_SETBNK: { // 28 - Set the bank to be used for the next read/write sector operation
ioBank = HIGH_REGISTER(AF);
if (ioBank >= BANKS)
ioBank = BANKS - 1;
ioBankBase = ((uint32)ioBank) << 16;
break; // without this, SETBNK fell through into XMOVE and corrupted srcBank/dstBank/isXmove
}
case B_XMOVE: { // 29 - Preload banks for MOVE
srcBank = LOW_REGISTER(BC);
dstBank = HIGH_REGISTER(BC);
if (srcBank >= BANKS)
srcBank = BANKS - 1;
if (dstBank >= BANKS)
dstBank = BANKS - 1;
srcBankBase = ((uint32)srcBank) << 16;
dstBankBase = ((uint32)dstBank) << 16;
isXmove = TRUE;
break;
}
case B_USERF: { // 30 - This allows programs ending in RET return to internal CCP
Status = STATUS_RETURN;
break;
}
case B_RESERV1:
case B_RESERV2: {
break;
}
default: {
#ifdef DEBUG // Show unimplemented BIOS calls only when debugging
_puts("\r\nUnimplemented BIOS call.\r\n");
_puts("C = 0x");
_puthex8(ch);
_puts("\r\n");
#endif // ifdef DEBUG
break;
}
} // switch
#ifdef DEBUGLOG
_logBiosOut(ch);
#endif
} // _Bios
/* Packed BCD helpers for the CP/M 3 clock (BDOS 104/105) */
#define BCD2DEC(b) ((((b) >> 4) & 0x0F) * 10 + ((b) & 0x0F))
#define DEC2BCD(d) ((uint8)((((d) / 10) << 4) | ((d) % 10)))
/* Difference (in seconds) between the host clock and the clock set via
BDOS 104 (T_SET). Lets a set time round-trip through BDOS 105 (T_GET). */
static long clockOffset = 0;
/* Program return code, get/set via BDOS 108 (P_CODE). */
static uint16 programRetCode = 0;
/* Console mode word, get/set via BDOS 109 (C_MODE). RunCPM's console layer is
already raw (no ^S pause, no tab expansion, no ^C termination, no ^P echo),
so most "disable" bits are effectively always on. Bits 8-9 are honored by
function 11 (Get console status). */
static uint16 consoleMode = 0;
void _Bdos(void) {
uint8 ch = LOW_REGISTER(BC);
#ifdef DEBUGLOG
_logBdosIn(ch);
#endif
HL = 0x0000; // HL is reset by the BDOS
SET_LOW_REGISTER(BC, LOW_REGISTER(DE)); // C ends up equal to E
switch (ch) {
#ifndef ABDOS
/*
C = 0 : System reset
Doesn't return. Reloads CP/M
*/
case P_TERMCPM: {
Status = STATUS_RESTART; // Same as call to "BOOT"
break;
}
/*
C = 1 : Console input
Gets a char from the console
Returns: A=Char
*/
case C_READ: {
HL = _getconE();
#ifdef DEBUG
if (HL == DEBUGKEY)
Debug = 1;
#endif // ifdef DEBUG
break;
}
/*
C = 2 : Console output
E = Char
Sends the char in E to the console
*/
case C_WRITE: {
_putcon(LOW_REGISTER(DE));
break;
}
/*
C = 3 : Auxiliary (Reader) input
Returns: A=Char
*/
case A_READ: {
HL = 0x1a;
break;
}
/*
C = 4 : Auxiliary (Punch) output
*/
case A_WRITE: {
#ifdef USE_PUN
if (!pun_open) {
pun_dev = _sys_fopen_w((uint8 *)pun_file);
pun_open = TRUE;
}
if (pun_dev) {
_sys_fputc(LOW_REGISTER(DE), pun_dev);
}
#endif // ifdef USE_PUN
break;
}
/*
C = 5 : Printer output
*/
case L_WRITE: {
#ifdef USE_LST
if (!lst_open) {
lst_dev = _sys_fopen_w((uint8 *)lst_file);
lst_open = TRUE;
}
if (lst_dev)
_sys_fputc(LOW_REGISTER(DE), lst_dev);
#endif // ifdef USE_LST
break;
}
/*
C = 6 : Direct console IO
E = 0xFF : Checks for char available and returns it, or 0x00 if none (read)
E = 0xFE : Return console input status. Zero if no character is waiting, nonzero otherwise. (CPM3)
E = 0xFD : Wait until a character is ready, return it without echoing. (CPM3)
E = char : Outputs char (write)
Returns: A=Char or 0x00 (on read)
*/
case C_RAWIO: {
uint8 e = LOW_REGISTER(DE);
if (e == 0xFF) {
// Check for char available and return it, or 0x00 if none (non-blocking read)
HL = _getconNB();
#ifdef DEBUG
if (HL == DEBUGKEY)
Debug = 1;
#endif // ifdef DEBUG
#ifdef CPM3
} else if (e == 0xFE) {
// Return console input status. Zero if no character is waiting, nonzero otherwise. (CPM3)
HL = _chready() ? 0x00FF : 0x0000;
} else if (e == 0xFD) {
// Wait until a character is ready, return it without echoing. (CPM3)
HL = _getcon();
#ifdef DEBUG
if (HL == DEBUGKEY)
Debug = 1;
#endif // ifdef DEBUG
#endif // ifdef CPM3
} else {
// E = char : Outputs char (write)
_putcon(e);
}
break;
}
/*
C = 7 : Get IOBYTE (CPM2)
Gets the system IOBYTE
Returns: A = IOBYTE (CPM2)
C = 7 : Auxiliary Input status (CPM3)
0FFh is returned if the Auxiliary Input device has a character ready; otherwise 0 is returned.
Returns: A=0 or 0FFh (CPM3)
*/
case A_STATIN: {
#ifdef CPM3
HL = _chready() ? 0xFF : 0x00;
#else
HL = _RamRead(0x0003);
#endif
break;
}
/*
C = 8 : Set IOBYTE (CPM2)
E = IOBYTE
Sets the system IOBYTE to E
C = 8 : Auxiliary Output status (CPM3)
0FFh is returned if the Auxiliary Output device is ready for characters; otherwise 0 is returned.
Returns: A=0 or 0FFh (CPM3)
*/
case A_STATOUT: {
#ifdef CPM3
HL = 0xFF; // Auxiliary Output device is always ready
#else
_RamWrite(0x0003, LOW_REGISTER(DE));
#endif
break;
}
/*
C = 9 : Output string
DE = Address of string
Sends the $ terminated string pointed by (DE) to the screen
Under CP/M 3 the termination character can be changed by the user via BDOS call 110 (C_DELIMIT).
*/
case C_WRITESTR: {
uint8 delim = outputDelimiter;
while ((ch = _RamRead(DE++)) != delim)
_putcon(ch);
break;
}
/*
C = 10 (0Ah) : Buffered input
CP/M 2:
DE = Address of buffer
CP/M 3:
DE = Address of buffer
DE = 0 Use DMA address abd the buffer already contains data
if DE=address:
buffer: DEFB size
DEFB ?
DEFB bytes
if DE=0:
buffer: DEFB size
DEFB len
DEFB bytes
Reads (DE) bytes from the console
Returns: A = Number of chars read
DE) = First char
*/
case C_READSTR: {
uint16 i;
uint8 j, chr;
// Under CP/M3 DE==0 means use the DMA buffer. Compute a common base address
// so the rest of the code can operate on either DE-provided buffer or DMA.
uint16 bufBase = (WORD16(DE) == 0) ? dmaAddr : WORD16(DE);
uint16 chrsMaxIdx = bufBase; // index to max number of characters
uint16 chrsCntIdx = (chrsMaxIdx + 1) & 0xFFFF; // index to number of characters read
uint16 chrsIdx = (chrsCntIdx + 1) & 0xFFFF; // index to characters
// printf("\n\r chrsMaxIdx: %0X, chrsCntIdx: %0X", chrsMaxIdx, chrsCntIdx);
static uint8 *last = 0;
if (!last)
last = (uint8 *)calloc(1, 256); // allocate one (for now!)
#ifdef PROFILE
if (time_start != 0) {
time_now = millis();
printf(" (%ld)\n", time_now - time_start);
time_start = 0;
}
#endif // ifdef PROFILE
uint8 chrsMax = _RamRead(chrsMaxIdx); // Gets the max number of characters that can be read
uint8 chrsCnt = 0; // this is the number of characters read
#ifdef CPM3
// CP/M3 behaviour: if DE==0 the DMA buffer may already contain data
// Layout when DE==0: [size][len][bytes...]
if (WORD16(DE) == 0) {
uint8 existingLen = _RamRead(chrsCntIdx);
if (existingLen) { // buffer already contains data, return immediately
HL = existingLen;
break;
}
// otherwise fall through and fill the DMA buffer interactively
}
#endif // ifdef CPM3
uint8 curCol = 0; // this is the cursor column (relative to where it started)
while (chrsMax) {
// pre-backspace, retype & post backspace counts
uint8 preBS = 0, reType = 0, postBS = 0;
chr = _getcon(); // input a character
if (chr == 1) { // ^A - Move cursor one character to the left
if (curCol > 0) {
preBS++; // backspace one
} else {
_putcon('\007'); // ring the bell
}
}
if (chr == 2) { // ^B - Toggle between beginning & end of line
if (curCol) {
preBS = curCol; // move to beginning
} else {
reType = chrsCnt - curCol; // move to EOL
}
}
if ((chr == 3) && (chrsCnt == 0)) { // ^C - Abort string input
_puts("^C");
Status = STATUS_RESTART;
break;
}
#ifdef DEBUG
if (chr == DEBUGKEY) { // Enter debugger
Debug = 1;
break;
}
#endif // ifdef DEBUG
if (chr == 5) { // ^E - goto beginning of next line
_putcon('\n');