-
Notifications
You must be signed in to change notification settings - Fork 4
/
com.c
1122 lines (1029 loc) · 30.8 KB
/
com.c
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
/*
** Portable MINCE
** Jeffrey H. Johnson <[email protected]>
**
** Based on the CP/M 2.2 (8080/Z-80) simulator for UNIX systems
** by Jim Cathey, Edmund Ramm, Charlie Gibbs, and Willi Kusche
**
** Usage: mince [-<A..P> path ...] [-b] [-c] [-d] [-i infile] [-v] [-V]
** [-l listfile] file[.com] [args ...]
**
** Where -b turns off buffering of output, -c makes an 8080 core file
** upon error, -d inhibits the DEL to BS conversion on keyboard input,
** -l listfile hooks up CP/M's list output to the named file, and -v
** turns off the H19 to VT100 translation. The -A to -P options give
** UNIX paths for each of CP/M's 16 logical drives. The -i option
** takes input from the given file instead of standard in until EOF,
** whereupon it reverts to standard in.
*/
#include "com.h"
int debugoutput; /* For making sure debugout has NL at end. */
int main(argc, argv) int argc;
char *argv[];
{
char ch, *filename, *argp, linebuf[256];
unsigned char *tpa;
struct regs *regp;
if (!(tpa = malloc(TPASIZE + TPAPAD + sizeof(struct regs))))
exit(2);
memset(tpa, 0x00, TPASIZE + TPAPAD + sizeof(struct regs));
gregp = regp = (struct regs *)tpa;
tpa += sizeof(struct regs); /* Allocate Pseudo-CPU storage. */
regp->vtstate = idle; /* Fill in what we must right now. */
regp->listfd = -1;
holdingchar = -1;
if (!(regp->seed = time(0)))
regp->seed = 1;
infilen = 0;
infile = 0; /* STDIN by default. */
filename = 0;
progname = *argv;
#ifdef NOBUFF
regp->miscflags |= NOBUFFER;
#endif
while (--argc > 0) {
argp = *++argv;
if (*argp == '-') {
argp++;
switch (ch = *argp++) {
#ifndef EMBED
case 'A':
case 'B':
case 'C':
case 'D': /* For Drive/User */
case 'E':
case 'F':
case 'G':
case 'H': /* assignment. */
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
if (!*argp)
--argc, argp = *++argv;
drives[ch - 'A'] = argp;
break;
#endif
case 'b': /* No buffering. */
regp->miscflags |= NOBUFFER;
break;
case 'c': /* Make 8080 core file on error. */
regp->trcoptions |= TRCCORE;
break;
case 'd': /* No DEL->BS conversion. */
regp->miscflags |= NODELTOBS;
break;
case 'i': /* Use file instead of standard input. */
if (!*argp)
--argc, argp = *++argv;
infilen = argp;
break;
case 'l': /* List (print) output to file. */
if (!*argp)
--argc, argp = *++argv;
regp->listname = argp;
break;
case 'v': /* No H19->VT-100 conversion. */
regp->vtstate = off;
break;
case 'V': /* Version printout. */
linebuf[0] = '\0';
strcat(linebuf, "\rCCOM ");
strcat(linebuf, VERSION);
strcat(linebuf,
" - Jeffrey H. Johnson <[email protected]>\r\n\n"
"Adapted from COM 3.0, Copyright (C) 1984-2006\r\n Jim "
"Cathey, Edmund Ramm, Charlie Gibbs, & Willi Kusche.\r\n");
write(1, linebuf, strlen(linebuf));
exit(0);
break;
case 'x': /* For debugging. */
regp->miscflags |= NOIOCTL;
break;
default:
goto usage;
}
} else {
#ifdef NOBUFF
regp->miscflags |= NOBUFFER;
#endif
#ifdef EMBED
if (filename = strrchr(argp, '/')) {
*filename++ = 0;
drives[0] = argp;
} else
filename = argp;
*argv = filename; /* Leave basename in argv. */
#else
filename = argp;
argc--;
argv++;
#endif
break;
}
}
#ifdef EMBED
if (filename == progname) { /* Always false!*/
#else
if (!filename) {
#endif
char *cp, *cp2;
usage:
#ifdef EMBED
cp = "";
cp2 = " <file>";
#else
cp = "[-<A..P> path ...] ";
cp2 = " cpmfile[.com] [args...]";
#endif
write(2, linebuf,
mysprintf(linebuf,
"Usage: %s %s[-b] [-c] [-d] "
"[-i infile] [-l listfile] [-v] [-V]%s\n",
progname, cp, cp2));
exit(1);
}
if (infilen)
redirectin(infilen);
if (tpa) {
if (loadcom(filename, tpa + LOADADDR)) { /* Load program. */
loadfdos(tpa, regp); /* Set up Pseudo-CPU registers & memory. */
parseargs(tpa, argc, argv);
setupstdin(regp);
regp->running = 2; /* Run continuously. */
opcodesimulator(tpa); /* Run the Pseudo-CPU. */
if (regp->pppc && regp->badflag)
dumplast(tpa, regp, linebuf);
} else {
write(2, linebuf,
mysprintf(linebuf, "%s: Can't open %s\n", progname, filename));
exit(3);
}
}
doexit(0);
}
/*
** Convert remaining Unix command-line arguments to CP/M's strange
** pre-filled FCB's and command tail.
*/
void parseargs(tpa, argc, argv) unsigned char *tpa;
int argc;
char *argv[];
{
int i, len, arg;
char *cp;
struct regs *regp;
regp = (struct regs *)(tpa - sizeof(struct regs));
if (regp->trcoptions & TRCCOUNT) {
write(2, argv[-1], strlen(argv[-1]));
for (i = 0; i < argc; i++) {
write(2, " ", 1);
write(2, argv[i], strlen(argv[i]));
}
write(2, "\n", 1);
}
ufntocpmfn(argc ? argv[0] : 0, &tpa[FCB1]);
ufntocpmfn((argc > 1) ? argv[1] : 0, &tpa[FCB2]);
i = 0; /* Make up command tail. */
cp = (char *)&tpa[DEFDMA + 1];
arg = 0;
while (argc-- && i < 126) {
*cp++ = ' ';
len = strlen(argv[arg]);
if (len + i > 126)
len = 126 - i;
strncpy(cp, argv[arg++], len);
cp += len;
i += len;
}
if (i)
tpa[DEFDMA] = i + 1;
else
tpa[DEFDMA] = 0;
}
/*
** Convert a Unix filename to a CP/M FCB name.
*/
void ufntocpmfn(name, fcb) char *name;
unsigned char *fcb;
{
char *cp, chr;
int i;
fcb[0] = 0; /* Default drive. */
memset(&fcb[1], ' ', 11);
if (name) {
if (cp = strchr(name, ':')) { /* Drive letter? */
chr = cp[-1];
fcb[0] = chr & 0x0F;
name = cp + 1;
}
i = 8;
if (cp = strchr(name, '.')) {
i = cp - name;
if (i > 8)
i = 8;
}
strncpy((char *)&fcb[1], name, i);
if (cp = strrchr(name, '.'))
strncpy((char *)&fcb[9], cp + 1, 3);
for (i = 1; i < 12; i++) {
chr = fcb[i];
if (!chr)
chr = ' ';
if (chr >= 'a' && chr <= 'z')
chr -= 'a' - 'A';
fcb[i] = chr;
}
}
}
/*
** Signal catcher, so we can fix up things before exiting.
*/
void catcher(signum) int signum;
{ doexit(0); }
/*
** Signal catchers, for trace flag manipulation.
*/
void u1catcher(signum) int signum;
{
if (gregp)
gregp->trcflag = 1;
if (gregp2)
gregp2->trcflag = 1;
signal(SIGUSR1, u1catcher);
}
void u2catcher(signum) int signum;
{
if (gregp)
gregp->trcflag = 0;
if (gregp2)
gregp2->trcflag = 0;
signal(SIGUSR2, u2catcher);
}
/*
** Bail out, cleaning up before we go.
*/
void doexit(code) int code;
{
flusher(0); /* Make sure all output is done. */
restorestdin(); /* Fix the terminal. */
if (debugoutput)
write(2, "\n", 1);
exit(code); /* We be gone! */
}
/*
** Make an 8080 core file, so to speak. For debugging.
*/
void docore(tpa, regp) unsigned char *tpa;
struct regs *regp;
{
#ifndef EMBED
int fd;
if ((fd = creat("8080.core", 0666)) >= 0) {
write(fd, tpa, TPASIZE);
write(fd, regp, sizeof(*regp));
close(fd);
}
#endif
}
/*
** Load the .COM program into memory.
*/
int loadcom(name, where) char *name;
unsigned char *where;
{
int fd, fsize, i;
char *cp, *tail, fnambuf[1024];
#ifdef EMBED
if (nfakefiles) {
memcpy(where, fakefs[0].data, *fakefs[0].size);
return 1;
}
#else
tail = ".com";
strncpy(fnambuf, name, sizeof fnambuf);
if (cp = strchr(name, '.')) {
if (strcmp(cp, tail))
strcpy(cp, tail);
} else
strncat(fnambuf, tail, sizeof(fnambuf) - 1);
if ((fd = open(fnambuf, 0)) >= 0) {
fsize = lseek(fd, 0L, 2);
lseek(fd, 0L, 0);
if (fsize <= (TPASIZE - LOADADDR - FDOSSIZE - 2)) { /* Too big? */
while (fsize > 0) { /* No, load it. */
i = read(fd, where, fsize);
if (i < 0)
exit(2);
fsize -= i;
}
close(fd);
return 1;
}
close(fd);
}
return 0;
#endif
}
/*
** The fake FDOS (BIOS & BDOS) for the 8080. This is mostly a jump
** table stashed at the high end of memory. It calls the simulator's
** service routine by using the HLT instruction followed by an opcode.
** Routines marked '@@' aren't implemented, partly for safety, partly
** due to laziness, and mostly due to lack of need. The BIOS part
** is required to be a jump table, so this array is twice as big
** as would otherwise be needed. (The original design wasn't a
** jump table, and it failed miserably.)
*/
unsigned char fdos[] = {
0x76, 0x00, 0xC9, /* Fake BDOS for target system. */
/* BIOS Jump Table */
0xC3, 0x33, 0xFF, /* Wboot */
0xC3, 0x36, 0xFF, /* Const */
0xC3, 0x39, 0xFF, /* Conin */
0xC3, 0x3C, 0xFF, /* Conout */
0xC3, 0x3F, 0xFF, /* List */
0xC3, 0x42, 0xFF, /* Punch */
0xC3, 0x45, 0xFF, /* Reader */
0xC3, 0x48, 0xFF, /* Home */
0xC3, 0x4B, 0xFF, /* Seldsk */
0xC3, 0x4E, 0xFF, /* Settrk */
0xC3, 0x51, 0xFF, /* Setsec */
0xC3, 0x54, 0xFF, /* Setdma */
0xC3, 0x57, 0xFF, /* Read */
0xC3, 0x5A, 0xFF, /* Write */
0xC3, 0x5D, 0xFF, /* Listst */
0xC3, 0x60, 0xFF, /* Sectran */
/* Fake BIOS for target system */
0x76, 1, 0xC9, /* Wboot */
0x76, 2, 0xC9, /* Const */
0x76, 3, 0xC9, /* Conin */
0x76, 4, 0xC9, /* Conout */
0x76, 5, 0xC9, /* List */
0x76, 6, 0xC9, /* Punch */
0x76, 7, 0xC9, /* Reader */
0x76, 8, 0xC9, /* Home @@ */
0x76, 9, 0xC9, /* Seldsk @@ */
0x76, 10, 0xC9, /* Settrk @@ */
0x76, 11, 0xC9, /* Setsec @@ */
0x76, 12, 0xC9, /* Setdma @@ */
0x76, 13, 0xC9, /* Read @@ */
0x76, 14, 0xC9, /* Write @@ */
0x76, 15, 0xC9, /* Listst */
0x76, 16, 0xC9 /* Sectran @@ */
};
/*
** Build the FDOS (BIOS & BDOS) image in the 8080's space.
*/
void loadfdos(tpa, regp) unsigned char *tpa;
struct regs *regp;
{
unsigned char *cp;
int ii;
memcpy(&tpa[TPASIZE - FDOSSIZE], fdos, sizeof(fdos));
tpa[0] = tpa[5] = 0xC3; /* Build BIOS and BDOS jumps. */
tpa[6] = (TPASIZE - FDOSSIZE) & 0xFF;
tpa[7] = (TPASIZE - FDOSSIZE) >> 8;
tpa[1] = tpa[6] + 3;
tpa[2] = tpa[7];
tpa[TPASIZE - FDOSSIZE - 1] = 0; /* Return address to exit simulation. */
tpa[TPASIZE - FDOSSIZE - 2] = 0;
for (ii = 0; ii < TPAPAD; ii += 2) { /* Overrun protection. */
tpa[TPASIZE + ii] = 0x76; /* HLT */
tpa[TPASIZE + ii + 1] = 0x01; /* Warm Start*/
}
regp->ppc = &tpa[LOADADDR]; /* Start executing here. */
regp->psp = &tpa[TPASIZE - FDOSSIZE - 2]; /* Initial stack here. */
regp->dma = &tpa[DEFDMA]; /* Initial DMA buffer here. */
}
/*
** What to do when a CP/M FDOS service request (HLT #n) is detected.
**
** A static variable is used so that a second, paralleled simulation
** (if any) will get the same input values.
*/
void service(tpa) unsigned char *tpa;
{
char *cp, linebuf[128];
unsigned char byte;
struct regs *regp;
static unsigned char lastrega;
regp = (struct regs *)(tpa - sizeof(struct regs));
byte = *(regp->ppc + 1);
switch (byte) {
case 0: /* BDOS function. */
bdos(tpa, regp, &lastrega);
break;
/* BIOS functions. */
case 1: /* Warm Boot. */
doexit(0);
break;
case 2: /* Console Status. */
flushconout(regp, 1);
if (regp->miscflags & DUMMYSVC)
regp->rega = lastrega;
else
lastrega = regp->rega = myrdchk(infile) ? 0xFF : 0;
break;
case 3: /* Console Input. */
if (regp->miscflags & DUMMYSVC)
regp->rega = lastrega;
else {
flushconout(regp, 0);
conin(regp, (char *)®p->rega);
lastrega = regp->rega;
}
break;
case 4: /* Console Output. */
if (!(regp->miscflags & DUMMYSVC))
conout(regp, regp->regc);
break;
case 5: /* List Output. */
if (!(regp->miscflags & DUMMYSVC))
listout(regp, regp->regc);
break;
case 15: /* Return List Status. */
if (!(regp->miscflags & DUMMYSVC))
flushlistout(regp, 1);
regp->rega = 0xFF;
break;
case 6: /* Auxiliary Output. */
case 7: /* Auxiliary Input. */
default:
cp = linebuf;
cp += mysprintf(cp, "\r\nInvalid Service instruction %02X.", byte);
cp += mysprintf(cp, "\r\nRegister contents:");
write(2, linebuf, cp - linebuf);
dumpregs(tpa);
if (regp->trcoptions & TRCCORE)
docore(tpa, regp);
break;
}
}
/*
** Perform CP/M BDOS function.
**
** Static variables are used so that a second, paralleled simulation
** (if any) will get the same input values.
*/
void bdos(tpa, regp, lastregap) unsigned char *tpa;
struct regs *regp;
unsigned char *lastregap;
{
int len, fileio, seekval, ioval;
char *cp, *cp2, *ufn, linebuf[512];
static char lineinbuf[256];
static int linelen;
unsigned char fcode, *ucp1, *ucp2;
struct fcb *fcbp;
struct fcache *fp;
struct stat st;
static char lastdn[3];
fileio = 0;
fcode = regp->regc;
if (fcode > 14) { /* File I/O operation? */
fileio = 1; /* Which ones use FCB's? */
switch (fcode) {
case 24: /* Return Log-in Vector. */
case 25: /* Return Current Disk. */
case 26: /* Set DMA Address. */
case 32: /* Get/Set User Code. */
case 37: /* Reset Drive. */
fileio = 0;
}
}
if (fileio) {
fcbp = (struct fcb *)&tpa[(regp->regd << 8) | regp->rege];
switch (fcode) {
case 33: /* Read Random. */
case 34: /* Write Random. */
case 40: /* Write Random with Zero Fill. */
if (fcbp->dn[0] >= 0) {
fcbp->cr = fcbp->rr[0] & 0x7F;
fcbp->extent = fcbp->rr[1] << 1 | fcbp->rr[0] >> 7;
}
break;
}
} else
fcbp = 0;
switch (fcode) {
case 0: /* System Reset. */
doexit(0);
break;
case 1: /* Console Input. */
if (regp->miscflags & DUMMYSVC)
regp->rega = *lastregap;
else {
flushconout(regp, 0);
conin(regp, (char *)®p->rega);
*lastregap = regp->rega;
if (regp->rega >= ' ' || regp->rega == '\n' || regp->rega == '\r' ||
regp->rega == '\t' || regp->rega == 8)
conout(regp, regp->rega);
}
break;
case 2: /* Console Output. */
if (!(regp->miscflags & DUMMYSVC))
conout(regp, regp->rege);
break;
case 5: /* List (print) output. */
if (!(regp->miscflags & DUMMYSVC))
listout(regp, regp->rege);
break;
case 6: /* Direct Console I/O. */
if (regp->rege == 0xFF) {
if (!(regp->miscflags & DUMMYSVC)) {
flushconout(regp, 1);
if (myrdchk(infile)) {
conin(regp, (char *)®p->rega);
*lastregap = regp->rega;
} else
*lastregap = regp->rega = 0;
} else
regp->rega = *lastregap;
} else if (!(regp->miscflags & DUMMYSVC))
conout(regp, regp->rege);
break;
case 9: /* Print String. */
ucp1 = &tpa[(regp->regd << 8) | regp->rege];
ucp2 = ucp1;
while (*ucp2 != '$' && ucp2 < &tpa[TPASIZE])
ucp2++;
if (len = (ucp2 - ucp1))
if (!(regp->miscflags & DUMMYSVC))
conoutstr(regp, (char *)ucp1, len);
break;
case 10: /* Read Console Buffer. */
ucp1 = &tpa[(regp->regd << 8) | regp->rege];
if (!(regp->miscflags & DUMMYSVC)) {
stdinlineon(regp);
linelen = ucp1[0];
do
linelen = read(0, lineinbuf, linelen);
while (linelen < 0 && errno == EINTR);
if (linelen > 0) {
ucp1[1] = linelen;
memcpy(&ucp1[2], lineinbuf, linelen);
} else
ucp1[1] = 0;
stdinlineoff(regp);
} else {
if (linelen > 0) {
ucp1[1] = linelen;
memcpy(&ucp1[2], lineinbuf, linelen);
} else
ucp1[1] = 0;
}
break;
case 11: /* Get Console Status. */
if (regp->miscflags & DUMMYSVC)
regp->rega = *lastregap;
else
*lastregap = regp->rega = myrdchk(infile) ? 0xFF : 0;
break;
case 12: /* Return Version Number. */
regp->regb = regp->regh = 0; /* CP/M */
regp->rega = regp->regl = 0x22; /* 2.2 */
break;
case 13: /* Reset Disk System. */
regp->driveuser &= 0x0F; /* Drive A, */
regp->dma = &tpa[DEFDMA]; /* default DMA address. */
break;
case 14: /* Select Disk. */
regp->driveuser = (regp->rege & 0x0F) << 4 | (regp->driveuser & 0x0F);
break;
case 15: /* Open File. */
if (regp->miscflags & DUMMYSVC) {
regp->rega = *lastregap;
memset(&fcbp->s1, 0, 19);
fcbp->dn[0] = lastdn[0];
fcbp->dn[1] = lastdn[1];
fcbp->dn[2] = lastdn[2];
} else {
*lastregap = regp->rega = 0xFF; /* Assume failure. */
memset(&fcbp->s1, 0, 19);
fcbp->dn[1] = lastdn[1] = O_RDONLY; /* Unix's file mode. */
if (cp2 = fncanon(fcbp, 0, regp->driveuser >> 4)) {
if (fp = cachedfile(cp2, &openfilehead)) {
ioval = fp->fd;
fcbp->dn[2] = lastdn[2] = fp->fakeid;
} else {
ioval = -1; /* Actually Unix fd. */
if (ufn = cfntoufn(cp2)) {
fileio = -1; /* Search fake file list first. */
for (ioval = 0; ioval < nfakefiles; ioval++) {
if (!strcmp(&cp2[2], fakefs[ioval].name)) {
fileio = ioval;
break;
}
}
if (fileio < 0) { /* Fake not found, open for real. */
ioval = open(ufn, OROF);
fileio = 0; /* No fake ID, either. */
} else /* Fake found, flag it so. */
ioval = 0;
}
}
fcbp->dn[0] = lastdn[0] = ioval; /* Unix fd, if non-zero. */
if (ioval >= 0) {
*lastregap = regp->rega = 0; /* Open successful. */
if (!fp) {
fp = cachefile(cp2, &openfilehead);
fp->fd = ioval;
fcbp->dn[2] = lastdn[2] = fp->fakeid = fileio;
}
}
}
}
break;
case 16: /* Close File. */
if (regp->miscflags & DUMMYSVC)
regp->rega = *lastregap;
else
*lastregap = regp->rega = 0xFF; /* Assume Error. */
if (fcbp->dn[0] >= 0) {
cp2 = fncanon(fcbp, 0, regp->driveuser >> 4);
if (!(regp->miscflags & DUMMYSVC)) {
uncachefile(cp2, &openfilehead);
if (fcbp->dn[0] > 2)
*lastregap = regp->rega = close(fcbp->dn[0]) ? 0xFF : 0;
else
*lastregap = regp->rega = 0;
}
}
fcbp->dn[0] = fcbp->dn[1] = -1;
break;
case 17: /* Search For First. */
cp2 = ambigname(fncanon(fcbp, 0, regp->driveuser >> 4));
if (!(regp->miscflags & DUMMYSVC))
getflist(regp, cp2); /* then fall into... */
case 18: /* Search For Next. */
if (dirlisthead) {
ufntocpmfn(dirlisthead->name, regp->dma);
if (((regp->driveuser >> 4) + 1) == regp->dma[0])
regp->dma[0] = 0; /* Default drive? Make it so. */
uncachefile(dirlisthead->name, &dirlisthead);
*lastregap = regp->rega = 0;
} else
*lastregap = regp->rega = 0xFF;
break;
case 19: /* Delete File. */
if (regp->miscflags & DUMMYSVC) {
regp->rega = *lastregap;
break;
}
*lastregap = regp->rega = 0xFF; /* Assume Error. */
if (cp2 = fncanon(fcbp, 0, regp->driveuser >> 4)) {
if (ufn = cfntoufn(cp2)) {
fileio = -1; /* Search fake file list first. */
for (ioval = 0; ioval < nfakefiles; ioval++) {
if (!strcmp(&cp2[2], fakefs[ioval].name)) {
fileio = ioval;
break;
}
}
if (fileio < 0) {
if (!unlink(ufn))
*lastregap = regp->rega = 0;
} else {
fakefs[fileio].name[0] = 0;
*lastregap = regp->rega = 0;
}
}
}
break;
case 33: /* Read Random. */
case 20: /* Read Sequential. */
*lastregap = regp->rega = 1; /* Assume Error. */
if (fcbp->dn[0] >= 0) {
len = 0x4000 * fcbp->extent + 128 * fcbp->cr;
if (fcbp->dn[0] == 0) /* Fake file? */
seekval = (len < *fakefs[fcbp->dn[2]].size) ? len : 0;
else
seekval = lseek(fcbp->dn[0], len, 0);
if (seekval == len) {
if (fcbp->dn[0] == 0) { /* Fake file? */
memcpy(regp->dma, &fakefs[fcbp->dn[2]].data[seekval], 128);
ioval = 128;
} else
ioval = read(fcbp->dn[0], regp->dma, 128);
if (ioval > 0) {
*lastregap = regp->rega = 0;
if (ioval != 128) /* Padding (^Z's) required? */
memset(®p->dma[ioval], 26, 128 - ioval);
*lastregap = regp->rega = 0;
}
if (fcode == 20) /* Only update record position on seq read. */
if (++fcbp->cr >= 128) {
fcbp->cr = 0;
fcbp->extent++;
}
} else if (fcode == 33) /* Invalid Random Read? */
*lastregap = regp->rega = 6;
}
break;
case 34: /* Write Random. */
case 40: /* Write Random with Zero Fill. */
case 21: /* Write Sequential. */
*lastregap = regp->rega = 1; /* Assume Error. */
if (fcbp->dn[0] > 0) { /* Fake files are RO. */
if (cp2 = fncanon(fcbp, 0, regp->driveuser >> 4)) {
if (!fcbp->dn[1]) { /* Open for writing yet? */
fcbp->dn[1] = O_RDWR; /* No, reopen the file RD/WR. */
close(fcbp->dn[0]);
fcbp->dn[0] = -1;
if (ufn = cfntoufn(cp2)) {
if ((fcbp->dn[0] = open(ufn, ORWF)) < 0) {
fcbp->dn[0] = open(ufn, OROF);
break;
}
}
}
len = 0x4000 * fcbp->extent + 128 * fcbp->cr;
if ((seekval = lseek(fcbp->dn[0], len, 0)) == len) {
if ((ioval = write(fcbp->dn[0], regp->dma, 128)) == 128)
*lastregap = regp->rega = 0;
if (fcode == 21) /* Only update record pos on seq write. */
if (++fcbp->cr >= 128) {
fcbp->cr = 0;
fcbp->extent++;
}
} else if (fcode != 21) /* Invalid Random Write? */
*lastregap = regp->rega = 6;
}
}
break;
case 22: /* Make File. */
if (regp->miscflags & DUMMYSVC) {
regp->rega = *lastregap;
memset(&fcbp->s1, 0, 19);
fcbp->dn[0] = lastdn[0];
fcbp->dn[1] = lastdn[1];
} else {
*lastregap = regp->rega = 0xFF; /* Assume error. */
if (cp2 = fncanon(fcbp, 0, regp->driveuser >> 4)) {
if (ufn = cfntoufn(cp2)) {
memset(&fcbp->s1, 0, 19);
if ((fcbp->dn[0] = creat(ufn, 0666)) >= 0) {
fcbp->dn[1] = O_RDWR;
lastdn[0] = fcbp->dn[0];
lastdn[1] = fcbp->dn[1];
*lastregap = regp->rega = 0;
cachefile(cp2, &openfilehead)->fd = fcbp->dn[0];
}
}
}
}
break;
case 23: /* Rename file. */
if (regp->miscflags & DUMMYSVC) {
regp->rega = *lastregap;
break;
}
*lastregap = regp->rega = 0xFF; /* Assume error. */
if (cp2 = fncanon(fcbp, 16, regp->driveuser >> 4)) {
if (ufn = cfntoufn(cp2)) {
strcpy(linebuf, ufn);
if (cp2 = fncanon(fcbp, 0, regp->driveuser >> 4)) {
if (ufn = cfntoufn(cp2)) {
fileio = -1; /* Search fake file list first. */
for (ioval = 0; ioval < nfakefiles; ioval++) {
if (!strcmp(&cp2[2], fakefs[ioval].name)) {
fileio = ioval;
break;
}
}
if (fileio < 0) {
if (!link(ufn, linebuf))
if (!unlink(ufn))
*lastregap = regp->rega = 0;
} else {
strncpy(fakefs[fileio].name, linebuf, sizeof fakefs[fileio].name);
*lastregap = regp->rega = 0;
}
}
}
}
}
break;
case 24: /* Return Log-in Vector. */
seekval = 1;
for (len = 0; len < (sizeof(drives) / sizeof(drives[0])); len++) {
regp->regh = regp->regb |= (seekval >> 8);
regp->regl = regp->rega |= seekval;
seekval <<= 1;
}
break;
case 25: /* Return Current Disk. */
regp->rega = regp->driveuser >> 4;
break;
case 26: /* Set DMA address. */
regp->dma = &tpa[(regp->regd << 8) | regp->rege];
break;
case 32: /* Get/Set User Code. */
if (regp->rege == 0xFF)
regp->rega = regp->driveuser & 0x0F;
else
regp->driveuser = (regp->rege & 0x0F) | (regp->driveuser & 0xF0);
break;
case 35: /* Compute File Size. */
if (regp->miscflags & DUMMYSVC) {
fcbp->rr[0] = lastdn[0];
fcbp->rr[1] = lastdn[1];
fcbp->rr[2] = lastdn[2];
} else {
if (cp2 = fncanon(fcbp, 0, regp->driveuser >> 4)) {
if (ufn = cfntoufn(cp2)) {
fileio = -1; /* Search fake file list first. */
for (ioval = 0; ioval < nfakefiles; ioval++) {
if (!strcmp(&cp2[2], fakefs[ioval].name)) { /*Ignr A:*/
fileio = ioval;
break;
}
}
if (fileio < 0) {
if (!stat(ufn, &st)) {
len = st.st_size + 127;
fcbp->rr[0] = lastdn[0] = len >> 7;
fcbp->rr[1] = lastdn[1] = len >> 15;
fcbp->rr[2] = lastdn[2] = len >> 23;
}
} else {
len = *fakefs[fileio].size + 127;
fcbp->rr[0] = lastdn[0] = len >> 7;
fcbp->rr[1] = lastdn[1] = len >> 15;
fcbp->rr[2] = lastdn[2] = len >> 23;
}
}
}
}
break;
case 36: /* Set Random Record. */
if (fcbp->dn[0] >= 0) {
fcbp->rr[0] = fcbp->cr;
fcbp->rr[1] = fcbp->extent;
fcbp->rr[2] = 0;
}
break;
case 37: /* Reset Drive. */
regp->rega = 0;
break;
default:
cp = linebuf;
cp += mysprintf(cp, "\r\nInvalid BDOS call $%02X.", fcode);
cp += mysprintf(cp, "\r\nRegister contents:");
write(2, linebuf, cp - linebuf);
dumpregs(tpa);
if (regp->trcoptions & TRCCORE)
docore(tpa, regp);
break;
}
}
/*
** Dump out the previous instruction. For supplying context for
** bad opcode or simulation mismatch reports.
*/
void dumplast(tpa, regp, linebuf) unsigned char *tpa;
struct regs *regp;
char *linebuf;
{
char *cp;
unsigned char *lastpc;
lastpc = regp->pppc;
cp = linebuf;
cp += mysprintf(cp, "\nPrevious instruction:"
" ");
if (*lastpc == 0xCB || *lastpc == 0xDD || *lastpc == 0xED || *lastpc == 0xFD)
cp += mysprintf(cp, "%02X%02X %02X%02X ", (lastpc - tpa) >> 8,
(lastpc - tpa) & 0xFF, lastpc[0], lastpc[1]);
else
cp += mysprintf(cp, "%02X%02X %02X ", (lastpc - tpa) >> 8,
(lastpc - tpa) & 0xFF, lastpc[0]);
cp += mysprintf(cp, " %s", decode(lastpc, regp->trcoptions & ZILOG, 1));
write(2, linebuf, cp - linebuf);
}
/*
** What to do when an illegal 8080 instruction is detected.
*/
void illegal(tpa) unsigned char *tpa;
{
char *cp, linebuf[128];