-
Notifications
You must be signed in to change notification settings - Fork 3
/
bios.c
903 lines (741 loc) · 19.5 KB
/
bios.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
/*-----------------------------------------------------------------------*\
| bios.c -- CP/M emulator "front-end" for the Z80 emulator -- runs |
| standard CP/M executables with no changes as long as they use CP/M |
| system calls to do all their work. |
| |
| Originally by Kevin Kayes, but he refused any responsibility for it, |
| then I later hacked it up, enhanced it, and debugged it. |
| |
| Copyright 1986-1988 by Parag Patel. All Rights Reserved. |
| Copyright 1994-1995,2000 by CodeGen, Inc. All Rights Reserved. |
\*-----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include "cpmdisc.h"
#include "defs.h"
#ifdef macintosh
#include <stat.h>
#else
#include <sys/stat.h>
#endif
/* definition of: extern unsigned char cpm_array[]; */
#include "cpm.c"
/* The BDOS/CCP had better be built with these values! */
#define CCP 0xD400
#define BDOS 0xDC00
#define CBIOS 0xEA00
#define NENTRY 30 /* number of BIOS entries */
#define STACK 0xEF00 /* grows down from here */
/* The first NUMHDISCS drives may be specified as hard-drives. */
#define NUMHDISCS 2
#define NUMDISCS (MAXDISCS - NUMHDISCS)
#if NUMHDISCS > MAXDISCS
# error Too many hard-discs specified here.
#endif
/* disk parameter block information */
#define DPBSIZE 15
#define HDPBLOCK (CBIOS + NENTRY * 8)
#define DPBLOCK (HDPBLOCK + DPBSIZE)
#define DIRBUF (DPBLOCK + DPBSIZE)
#define DPHSIZE 16
/* hard disc parameter info */
#define HDPBASE (DIRBUF + SECTORSIZE)
#define DPBASE (HDPBASE + DPHSIZE * NUMHDISCS)
#define CVSIZE 0
#define ALVSIZE 33
#define CVBASE (DPBASE + DPHSIZE * NUMDISCS)
#define ALVBASE (CVBASE + CVSIZE * NUMDISCS)
/* ST-506 allocation vector size */
#define HALVBASE (ALVBASE + ALVSIZE * NUMDISCS)
#define HALVSIZE 306
/* buffer for where to return time/date info */
#define TIMEBUF (HALVBASE + HALVSIZE * NUMHDISCS)
#define TIMEBUFSIZE 5
/* just a marker for future expansion */
#define END_OF_BIOS (TIMEBUF + TIMEBUFSIZE)
/* ST-506 HD sector info (floppy defs are in cpmdisc.h for makedisc.c) */
#define HDSECTORSPERTRACK 64
#define HDTRACKSPERDISC 610
/* offsets into FCB needed for reading/writing Unix files */
#define FDOFFSET 12
#define BLKOFFSET 16
#define SZOFFSET 20
/* this batch of macros are not used at the moment */
#define CBUFF CCP+7
#define CIBUFF CCP+8
#define DEFAULTFCB 0x005C
#define DEFAULTBUF 0x0080
#define IOBYTE 0x0003
#define DISK 0x0004
#define DMA 0x0008
#define USER 0x000A
#define USERSTART 0x0100
/* forward declarations: */
static void seldisc(z80info *z80);
static void
closeall(z80info *z80)
{
int i;
for (i = 0; i < MAXDISCS; i++)
{
if (z80->drives[i] != NULL)
{
fclose(z80->drives[i]);
z80->drives[i] = NULL;
}
}
}
static void
warmboot(z80info *z80)
{
int i;
closeall(z80);
/* load CCP and BDOS into memory (max 0x1600 in size) */
for (i = 0; i < 0x1600 && i < sizeof cpm_array; i++)
SETMEM(CCP + i, cpm_array[i]);
/* try to load CCP/BDOS from disk, but ignore any errors */
loadfile(z80, "bdos.hex");
loadfile(z80, "ccp.hex");
/* CP/M system reset via "JP 00" - entry into BIOS warm-boot */
SETMEM(0x0000, 0xC3); /* JP CBIOS+3 */
SETMEM(0x0001, ((CBIOS + 3) & 0xFF));
SETMEM(0x0002, ((CBIOS + 3) >> 8));
/* 0x0003 is the IOBYTE, 0x0004 is the current DISK */
SETMEM(0x0003, 0x00);
SETMEM(0x0004, z80->drive);
/* CP/M syscall via "CALL 05" - entry into BDOS */
SETMEM(0x0005, 0xC3); /* JP BDOS+6 */
SETMEM(0x0006, ((BDOS+6) & 0xFF));
SETMEM(0x0007, ((BDOS+6) >> 8));
/* fake BIOS entry points */
for (i = 0; i < NENTRY; i++)
{
/* JP <bios-entry> */
SETMEM(CBIOS + 3 * i, 0xC3);
SETMEM(CBIOS + 3 * i + 1, (CBIOS + NENTRY * 3 + i * 5) & 0xFF);
SETMEM(CBIOS + 3 * i + 2, (CBIOS + NENTRY * 3 + i * 5) >> 8);
/* LD A,<bios-call> - start of bios-entry */
SETMEM(CBIOS + NENTRY * 3 + i * 5, 0x3E);
SETMEM(CBIOS + NENTRY * 3 + i * 5 + 1, i);
/* OUT A,0FFH - we use port 0xFF to fake the BIOS call */
SETMEM(CBIOS + NENTRY * 3 + i * 5 + 2, 0xD3);
SETMEM(CBIOS + NENTRY * 3 + i * 5 + 3, 0xFF);
/* RET - end of bios-entry */
SETMEM(CBIOS + NENTRY * 3 + i * 5 + 4, 0xC9);
}
/* disc parameter block - a 5Mb ST-506 hard disc */
SETMEM(HDPBLOCK, HDSECTORSPERTRACK & 0xFF);/* SPT - sectors per track */
SETMEM(HDPBLOCK + 1, HDSECTORSPERTRACK >> 8);
SETMEM(HDPBLOCK + 2, 4); /* BSH - data block shift factor */
SETMEM(HDPBLOCK + 3, 15); /* BLM - data block mask */
SETMEM(HDPBLOCK + 4, 0); /* EXM - extent mask */
SETMEM(HDPBLOCK + 5, 2441 & 0xFF); /* DSM - total drive capacity */
SETMEM(HDPBLOCK + 6, 2441 >> 8);
SETMEM(HDPBLOCK + 7, 1023 & 0xFF); /* DRM - total dir entries */
SETMEM(HDPBLOCK + 8, 1023 >> 8);
SETMEM(HDPBLOCK + 9, 0xFF); /* AL0 - blocks for directory entries */
SETMEM(HDPBLOCK + 10, 0xFF); /* AL1 */
SETMEM(HDPBLOCK + 11, 0x00); /* CKS - directory check vector */
SETMEM(HDPBLOCK + 12, 0x00);
SETMEM(HDPBLOCK + 13, RESERVEDTRACKS & 0xFF);/* OFF - reserved tracks */
SETMEM(HDPBLOCK + 14, RESERVEDTRACKS >> 8);
/* disk parameter headers for hard disc */
for (i = 0; i < NUMHDISCS; i++)
{
SETMEM(HDPBASE + DPHSIZE * i, 0x00); /* XLT */
SETMEM(HDPBASE + DPHSIZE * i + 1, 0x00);
SETMEM(HDPBASE + DPHSIZE * i + 2, 0x00); /* scratch 1 */
SETMEM(HDPBASE + DPHSIZE * i + 3, 0x00);
SETMEM(HDPBASE + DPHSIZE * i + 4, 0x00); /* scratch 2 */
SETMEM(HDPBASE + DPHSIZE * i + 5, 0x00);
SETMEM(HDPBASE + DPHSIZE * i + 6, 0x00); /* scratch 3 */
SETMEM(HDPBASE + DPHSIZE * i + 7, 0x00);
SETMEM(HDPBASE + DPHSIZE * i + 8, DIRBUF & 0xFF); /* DIRBUF */
SETMEM(HDPBASE + DPHSIZE * i + 9, DIRBUF >> 8);
SETMEM(HDPBASE + DPHSIZE * i + 10, HDPBLOCK & 0xFF); /* DPB */
SETMEM(HDPBASE + DPHSIZE * i + 11, HDPBLOCK >> 8);
SETMEM(HDPBASE + DPHSIZE * i + 12, 0x00);
SETMEM(HDPBASE + DPHSIZE * i + 13, 0x00);
SETMEM(HDPBASE + DPHSIZE * i + 14,
(HALVBASE + HALVSIZE * i) & 0xFF);
SETMEM(HDPBASE + DPHSIZE * i + 15,
(HALVBASE + HALVSIZE * i) >> 8);
}
/* disc parameter block - a single-sided single-density 8" 256k disc */
SETMEM(DPBLOCK, SECTORSPERTRACK & 0xFF); /* SPT - sectors per track */
SETMEM(DPBLOCK + 1, SECTORSPERTRACK >> 8);
SETMEM(DPBLOCK + 2, 3); /* BSH - data block shift factor */
SETMEM(DPBLOCK + 3, 7); /* BLM - data block mask */
SETMEM(DPBLOCK + 4, 0); /* EXM - extent mask */
SETMEM(DPBLOCK + 5, 242); /* DSM - total capacity of drive */
SETMEM(DPBLOCK + 6, 0);
SETMEM(DPBLOCK + 7, 63); /* DRM - total directory entries */
SETMEM(DPBLOCK + 8, 0);
SETMEM(DPBLOCK + 9, 0xC0); /* AL0 - blocks for directory entries */
SETMEM(DPBLOCK + 10, 0x00); /* AL1 */
SETMEM(DPBLOCK + 11, 0x00); /* CKS - directory check vector */
SETMEM(DPBLOCK + 12, 0x00);
SETMEM(DPBLOCK + 13, RESERVEDTRACKS & 0xFF); /* OFF - reserved tracks */
SETMEM(DPBLOCK + 14, RESERVEDTRACKS >> 8);
/* disc parameter headers */
for (i = 0; i < NUMDISCS; i++)
{
SETMEM(DPBASE + DPHSIZE * i, 0x00); /* XLT */
SETMEM(DPBASE + DPHSIZE * i + 1, 0x00);
SETMEM(DPBASE + DPHSIZE * i + 2, 0x00); /* scratch 1 */
SETMEM(DPBASE + DPHSIZE * i + 3, 0x00);
SETMEM(DPBASE + DPHSIZE * i + 4, 0x00); /* scratch 2 */
SETMEM(DPBASE + DPHSIZE * i + 5, 0x00);
SETMEM(DPBASE + DPHSIZE * i + 6, 0x00); /* scratch 3 */
SETMEM(DPBASE + DPHSIZE * i + 7, 0x00);
SETMEM(DPBASE + DPHSIZE * i + 8, DIRBUF & 0xFF); /* DIRBUF */
SETMEM(DPBASE + DPHSIZE * i + 9, DIRBUF >> 8);
SETMEM(DPBASE + DPHSIZE * i + 10, DPBLOCK & 0xFF); /* DPB */
SETMEM(DPBASE + DPHSIZE * i + 11, DPBLOCK >> 8);
#if (CVSIZE == 0)
SETMEM(DPBASE + DPHSIZE * i + 12, 0x00);
SETMEM(DPBASE + DPHSIZE * i + 13, 0x00);
#else
SETMEM(DPBASE + DPHSIZE * i + 12,
(CVBASE + CVSIZE * i) & 0xFF);
SETMEM(DPBASE + DPHSIZE * i + 13,
(CVBASE + CVSIZE * i) >> 8);
#endif
SETMEM(DPBASE + DPHSIZE * i + 14,
(ALVBASE + ALVSIZE * i) & 0xFF);
SETMEM(DPBASE + DPHSIZE * i + 15,
(ALVBASE + ALVSIZE * i) >> 8);
}
/* set up the stack for an 8-level RET to do a system reset */
SP = STACK;
for (i = 0; i < 8; i++)
{
/* push reboot entry (CBIOS + 3) onto stack */
--SP;
SETMEM(SP, (CBIOS + 3) >> 8);
--SP;
SETMEM(SP, (CBIOS + 3) & 0xFF);
}
/* set up the default disk (A:) and dma address */
z80->dma = 0x0080;
/* and all our default drive info */
z80->track = 0;
z80->sector = 1;
/* make sure the current file/disk is open */
B = 0;
C = z80->drive;
seldisc(z80);
PC = CCP;
}
static void
boot(z80info *z80)
{
z80->drive = 0;
warmboot(z80);
}
void
sysreset(z80info *z80)
{
boot(z80);
}
static void
consstat(z80info *z80)
{
input(z80, 0x01, 0x01, &A);
}
static void
consin(z80info *z80)
{
input(z80, 0x00, 0x00, &A);
if (A == CNTL('S'))
input(z80, 0x00, 0x00, &A);
}
static void
consout(z80info *z80)
{
output(z80, 0x00, 0x00, C & 0x7F);
}
/* list character in C */
static void
list(z80info *z80)
{
static FILE *fp = NULL;
if (fp == NULL)
{
fp = fopen("list", "w");
if (fp == NULL)
return;
}
/* close up on EOF */
if (C == CNTL('D') || C == '\0')
{
fclose(fp);
fp = NULL;
return;
}
putc(C, fp);
}
/* punch character in C */
static void
punch(z80info *z80)
{
}
/* return reader char in A, ^Z is EOF */
static void
reader(z80info *z80)
{
A = CNTL('Z');
}
static void
home(z80info *z80)
{
z80->track = 0;
z80->sector = 1;
}
static void
seldisc(z80info *z80)
{
char hdrivebuf[] = "A-Hdrive";
char drivebuf[] = "A-drive";
char *drivestr = C < NUMHDISCS ? hdrivebuf : drivebuf;
*drivestr += C; /* set the 1st letter to the drive name */
H = 0;
L = 0;
if (C >= MAXDISCS)
{
fprintf(stderr, "seldisc(): Attempt to open bogus drive %d\r\n",
C);
return;
}
if (z80->drives[C] == NULL)
{
struct stat statbuf;
long secs;
FILE *fp;
fp = fopen(drivestr, "rb+");
if (fp == NULL)
fp = fopen(drivestr, "wb+");
if (fp == NULL)
{
fprintf(stderr, "seldisc(): Cannot open file '%s'!\r\n",
drivestr);
return;
}
if (stat(drivestr, &statbuf) < 0)
{
fprintf(stderr, "seldisc(): Cannot stat file '%s'!\r\n",
drivestr);
fclose(fp);
return;
}
secs = statbuf.st_size / SECTORSIZE;
if (secs == 0)
{
char buf[SECTORSIZE];
memset(buf, 0xE5, SECTORSIZE);
if (fwrite(buf, 1, SECTORSIZE, fp) != SECTORSIZE)
{
fprintf(stderr, "seldisc(): Cannot create file '%s'!\r\n",
drivestr);
fclose(fp);
return;
}
secs = 1;
}
z80->drives[C] = fp;
z80->drivelen[C] = secs * SECTORSIZE;
}
z80->drive = C;
if (z80->drive < NUMHDISCS)
{
L = (HDPBASE + DPHSIZE * C) & 0xFF;
H = (HDPBASE + DPHSIZE * C) >> 8;
}
else
{
L = (DPBASE + DPHSIZE * C) & 0xFF;
H = (DPBASE + DPHSIZE * C) >> 8;
}
home(z80);
}
static void
settrack(z80info *z80)
{
int tracks = (z80->drive < NUMHDISCS) ?
HDTRACKSPERDISC : TRACKSPERDISC;
z80->track = (B << 8) + C;
if (z80->track < RESERVEDTRACKS || z80->track >= tracks)
fprintf(stderr, "settrack(): bogus track %d!\r\n",
z80->track);
}
static void
setsector(z80info *z80)
{
int sectors = (z80->drive < NUMHDISCS) ?
HDSECTORSPERTRACK : SECTORSPERTRACK;
z80->sector = (B << 8) + C;
if (z80->sector < SECTOROFFSET || z80->sector > sectors)
fprintf(stderr, "setsector(): bogus sector %d!\r\n",
z80->sector);
}
static void
setdma(z80info *z80)
{
z80->dma = (B << 8) + C;
}
static void
rdsector(z80info *z80)
{
int n;
int drive = z80->drive;
int sectors = (drive < NUMHDISCS) ? HDSECTORSPERTRACK : SECTORSPERTRACK;
long offset = SECTORSIZE * ((long)z80->sector - SECTOROFFSET +
sectors * ((long)z80->track - TRACKOFFSET));
FILE *fp = z80->drives[drive];
long len = z80->drivelen[drive];
if (fp == NULL)
{
fprintf(stderr, "rdsector(): file/drive %d not open!\r\n",
drive);
A = 1;
return;
}
if (len && offset >= len)
{
memset(&(z80->mem[z80->dma]), 0xE5, SECTORSIZE);
A = 0;
return;
}
if (fseek(fp, offset, SEEK_SET) != 0)
{
fprintf(stderr, "rdsector(): fseek failure offset=0x%lX!\r\n",
offset);
A = 1;
return;
}
n = fread(&(z80->mem[z80->dma]), 1, SECTORSIZE, fp);
if (n != SECTORSIZE)
{
fprintf(stderr, "rdsector(): read failure %d!\r\n", n);
A = 1;
}
else
A = 0;
}
static void
wrsector(z80info *z80)
{
int drive = z80->drive;
int sectors = (drive < NUMHDISCS) ? HDSECTORSPERTRACK : SECTORSPERTRACK;
long offset = SECTORSIZE * ((long)z80->sector - SECTOROFFSET +
sectors * ((long)z80->track - TRACKOFFSET));
FILE *fp = z80->drives[drive];
long len = z80->drivelen[drive];
if (fp == NULL)
{
fprintf(stderr, "wrsector(): file/drive %d not open!\r\n",
drive);
A = 1;
return;
}
if (len && offset > len)
{
char buf[SECTORSIZE];
if (fseek(fp, len, SEEK_SET) != 0)
{
fprintf(stderr, "wrsector(): fseek failure offset=0x%lX!\r\n",
len);
A = 1;
return;
}
memset(buf, 0xE5, SECTORSIZE);
while (offset > len)
{
if (fwrite(buf, 1, SECTORSIZE, fp) != SECTORSIZE)
{
fprintf(stderr, "wrsector(): write failure!\r\n");
A = 1;
return;
}
len += SECTORSIZE;
z80->drivelen[drive] = len;
}
}
if (fseek(fp, offset, SEEK_SET) != 0)
{
fprintf(stderr, "wrsector(): fseek failure offset=0x%lX!\r\n",
offset);
A = 1;
return;
}
if (fwrite(&(z80->mem[z80->dma]), 1, SECTORSIZE, fp) != SECTORSIZE)
{
fprintf(stderr, "wrsector(): write failure!\r\n");
A = 1;
}
else
{
A = 0;
if (offset + SECTORSIZE > len)
z80->drivelen[drive] = offset + SECTORSIZE;
}
}
static void
secttran(z80info *z80)
{
if (z80->drive < NUMHDISCS)
{
/* simple sector translation for hard disc */
HL = BC + 1;
if (BC >= HDSECTORSPERTRACK)
fprintf(stderr, "secttran(): bogus sector %d!\r\n", BC);
}
else
{
/* we do not need to use DE to find our translation table */
HL = sectorxlat[BC];
if (BC >= SECTORSPERTRACK)
fprintf(stderr, "secttran(): bogus sector %d!\r\n", BC);
}
}
static void
liststat(z80info *z80)
{
A = 0xFF;
}
/* These two routines read and write ints at arbitrary aligned addrs.
* The values are stored in the z80 in little-endian order regardless
* of the byte-order on the host.
*/
static int
addr2int(char *addr)
{
unsigned char *a = (unsigned char*)addr;
unsigned int t;
t = a[0] | (a[1] << 8) | (a[2] << 16) | (a[3] << 24);
return (int)t;
}
static void
int2addr(char *addr, int val)
{
unsigned char *a = (unsigned char*)addr;
unsigned int t = (unsigned int)val;
a[0] = t & 0xFF;
a[1] = (t >> 8) & 0xFF;
a[2] = (t >> 16) & 0xFF;
a[3] = (t >> 24) & 0xFF;
}
/* DE points to a CP/M FCB.
On return, A contains 0 if all went well, 0xFF otherwise.
The algorithm uses the FCB to store info about the UNIX file.
*/
static void
openunix(z80info *z80)
{
char filename[20], *fp;
byte *cp;
int i;
FILE *fd;
cp = &(z80->mem[DE + 1]);
fp = filename;
for (i = 0; (*cp != ' ') && (i < 8); i++)
*fp++ = tolower(*cp++);
cp = &(z80->mem[DE + 9]);
if (*cp != ' ')
{
*fp++ = '.';
for (i = 0; (*cp != ' ') && (i < 3); i++)
*fp++ = tolower(*cp++);
}
*fp = 0;
A = 0xFF;
/* if file is not readable, try opening it read-only */
if ((fd = fopen(filename, "rb+")) == NULL)
if ((fd = fopen(filename, "rb")) == NULL)
return;
A = 0;
int2addr(&z80->mem[DE + FDOFFSET], (int)fd);
int2addr(&z80->mem[DE + BLKOFFSET], 0);
int2addr(&z80->mem[DE + SZOFFSET], 0);
}
/* DE points to a CP/M FCB.
On return, A contains 0 if all went well, 0xFF otherwise.
The algorithm uses the FCB to store info about the UNIX file.
*/
static void
createunix(z80info *z80)
{
char filename[20], *fp;
byte *cp;
int i;
FILE *fd;
cp = &(z80->mem[DE + 1]);
fp = filename;
for (i = 0; (*cp != ' ') && (i < 8); i++)
*fp++ = tolower(*cp++);
cp = &(z80->mem[DE + 9]);
if (*cp != ' ')
{
*fp++ = '.';
for (i = 0; (*cp != ' ') && (i < 3); i++)
*fp++ = tolower(*cp++);
}
*fp = 0;
A = 0xFF;
if ((fd = fopen(filename, "wb+")) == NULL)
return;
A = 0;
int2addr(&z80->mem[DE + FDOFFSET], (int)fd);
int2addr(&z80->mem[DE + BLKOFFSET], 0);
int2addr(&z80->mem[DE + SZOFFSET], 0);
}
/* DE points to a CP/M FCB.
On return, A contains 0 if all went well, 0xFF otherwise.
The algorithm uses the FCB to store info about the UNIX file.
*/
static void
rdunix(z80info *z80)
{
byte *cp;
int i, blk, size;
FILE *fd;
cp = &(z80->mem[z80->dma]);
fd = (FILE *)addr2int(&z80->mem[DE + FDOFFSET]);
blk = addr2int(&z80->mem[DE + BLKOFFSET]);
size = addr2int(&z80->mem[DE + SZOFFSET]);
A = 0xFF;
if (fseek(fd, (long)blk << 7, SEEK_SET) != 0)
return;
i = fread(cp, 1, SECTORSIZE, fd);
size = i;
if (i == 0)
return;
for (; i < SECTORSIZE; i++)
cp[i] = CNTL('Z');
A = 0;
blk += 1;
int2addr(&z80->mem[DE + FDOFFSET], (int)fd);
int2addr(&z80->mem[DE + BLKOFFSET], blk);
int2addr(&z80->mem[DE + SZOFFSET], size);
}
/* DE points to a CP/M FCB.
On return, A contains 0 if all went well, 0xFF otherwise.
The algorithm uses the FCB to store info about the UNIX file.
*/
static void
wrunix(z80info *z80)
{
byte *cp;
int i, blk, size;
FILE *fd;
cp = &(z80->mem[z80->dma]);
fd = (FILE *)addr2int(&z80->mem[DE + FDOFFSET]);
blk = addr2int(&z80->mem[DE + BLKOFFSET]);
size = addr2int(&z80->mem[DE + SZOFFSET]);
A = 0xFF;
if (fseek(fd, (long)blk << 7, SEEK_SET) != 0)
return;
i = fwrite(cp, 1, size = SECTORSIZE, fd);
if (i != SECTORSIZE)
return;
A = 0;
blk += 1;
int2addr(&z80->mem[DE + FDOFFSET], (int)fd);
int2addr(&z80->mem[DE + BLKOFFSET], blk);
int2addr(&z80->mem[DE + SZOFFSET], size);
}
/* DE points to a CP/M FCB.
On return, A contains 0 if all went well, 0xFF otherwise.
*/
static void
closeunix(z80info *z80)
{
FILE *fd;
fd = (FILE *)addr2int(&z80->mem[DE + FDOFFSET]);
A = 0xFF;
if (fclose(fd) != 0)
return;
A = 0;
}
/* clean up and quit - never returns */
static void
finish(z80info *z80)
{
resetterm();
exit(0);
}
/* Get/set the time - although only the get-time part is implemented.
If C==0, then get the time, else of C==0xFF, then set the time.
HL returns a pointer to our time table:
HL+0:DATE LSB SINCE 1,1,1978
HL+1:DATE MSB
HL+2:HOURS (BCD)
HL+3:MINUTES (BCD)
HL+4:SECONDS (BCD)
*/
static void
dotime(z80info *z80)
{
time_t now;
struct tm *t;
word days;
int y;
if (C != 0) /* do not support setting the time yet */
return;
time(&now);
t = localtime(&now);
/* days since Jan 1, 1978 + one since tm_yday starts at zero */
days = (t->tm_year - 78) * 365 + t->tm_yday + 1;
/* add in the number of days for the leap years - dumb but accurate */
for (y = 78; y < t->tm_year; y++)
if (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
days++;
HL = TIMEBUF;
SETMEM(HL + 0, days & 0xFF);
SETMEM(HL + 1, days >> 8);
SETMEM(HL + 2, ((t->tm_hour / 10) << 4) + (t->tm_hour % 10));
SETMEM(HL + 3, ((t->tm_min / 10) << 4) + (t->tm_min % 10));
SETMEM(HL + 4, ((t->tm_sec / 10) << 4) + (t->tm_sec % 10));
}
void
bios(z80info *z80, int fn)
{
static void (*bioscall[])(z80info *z80) =
{
boot, /* 0 */
warmboot, /* 1 */
consstat, /* 2 */
consin, /* 3 */
consout, /* 4 */
list, /* 5 */
punch, /* 6 */
reader, /* 7 */
home, /* 8 */
seldisc, /* 9 */
settrack, /* 10 */
setsector, /* 11 */
setdma, /* 12 */
rdsector, /* 13 */
wrsector, /* 14 */
liststat, /* 15 */
secttran, /* 16 */
openunix, /* 17 */
createunix, /* 18 */
rdunix, /* 19 */
wrunix, /* 20 */
closeunix, /* 21 */
finish, /* 22 */
dotime /* 23 */
};
if (fn < 0 || fn >= sizeof bioscall / sizeof *bioscall)
{
fprintf(stderr, "Illegal BIOS call %d\r\n", fn);
return;
}
bioscall[fn](z80);
/* let z80 handle return */
}