-
Notifications
You must be signed in to change notification settings - Fork 1
/
bios.c
566 lines (518 loc) · 12.6 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
/* Basic i/o system module.
Copyright (C) 1995 Frank D. Cringle.
This file is part of yaze - yet another Z80 emulator.
Yaze 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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <termios.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/mman.h>
#include "simz80.h"
#include "yaze.h"
#include "bios.h"
/* $Id: bios.c,v 1.4 2004/04/23 09:50:15 fdc Exp $ */
#ifndef lint
static char vcid[] __attribute__((unused)) = "$Id: bios.c,v 1.4 2004/04/23 09:50:15 fdc Exp $";
#endif /* lint */
#ifndef SYSCONFDIR
#define SYSCONFDIR "/usr/local/etc"
#endif
/* Z80 registers */
#define AF af[af_sel]
#define BC regs[regs_sel].bc
#define DE regs[regs_sel].de
#define HL regs[regs_sel].hl
/* location on current disk */
static WORD track;
static WORD sector;
static WORD dma;
typedef unsigned long COUNT;
static COUNT bioscount;
static BYTE old_iobyte;
static void
new_iobyte(int io)
{
switch ((io >> 6) & 3) {
case 0: chn[CHNlst] = TTYout;
break;
case 1: chn[CHNlst] = CRTout;
break;
case 2: chn[CHNlst] = LPTout;
break;
case 3: chn[CHNlst] = UL1out;
break;
}
switch ((io >> 4) & 3) {
case 0: chn[CHNpun] = TTYout;
break;
case 1: chn[CHNpun] = PUNout;
break;
case 2: chn[CHNpun] = UP1out;
break;
case 3: chn[CHNpun] = UP2out;
break;
}
switch ((io >> 2) & 3) {
case 0: chn[CHNrdr] = TTYin;
break;
case 1: chn[CHNrdr] = RDRin;
break;
case 2: chn[CHNrdr] = UR1in;
break;
case 3: chn[CHNrdr] = UR2in;
break;
}
switch (io & 3) {
case 0: chn[CHNconin] = TTYin;
chn[CHNconout] = TTYout;
break;
case 1: chn[CHNconin] = CRTin;
chn[CHNconout] = CRTout;
break;
case 2: chn[CHNconin] = chn[CHNrdr];
chn[CHNconout] = chn[CHNlst];
break;
case 3: chn[CHNconin] = UC1in;
chn[CHNconout] = UC1out;
break;
}
old_iobyte = io;
}
static struct sio *
getsiop(int chan)
{
if (ram[3] != old_iobyte)
new_iobyte(ram[3]);
return &siotab[chn[chan]];
}
/* gr: */
int isafifo(int desc)
{
struct stat buf;
return fstat(desc, &buf) == 0 && S_ISFIFO(buf.st_mode);
}
int
bios_init(const char *initfile)
{
int go = 0;
FILE *su;
const char *fn = initfile;
char *name;
char buf[BUFSIZ];
#ifdef DEBUG
signal(SIGINT, sighand);
#endif
if (z3env && z3env > bios_top && z3env < dptable) {
/* reserve space for a z-system environment page */
WORD p = z3env-bios_top;
int i = 1024;
while (i--) {
global_alv[p>>3] |= (0x80 >> (p&7));
p++;
}
}
siotab[CRTin].fp = stdin;
name = ttyname(fileno(stdin));
siotab[CRTin].filename = name ? newstr(name) : "(stdin)";
siotab[CRTin].tty = isatty(fileno(stdin));
/* gr: */
siotab[CRTin].canselect = siotab[CRTin].tty || isafifo(fileno(stdin));
siotab[CRTout].fp = stdout;
name = ttyname(fileno(stdout));
siotab[CRTout].filename = name ? newstr(name) : "(stdout)";
siotab[CRTout].tty = isatty(fileno(stdout));
/* gr: */
siotab[CRTout].canselect = siotab[CRTout].tty || isafifo(fileno(stdout));
if (siotab[CRTin].tty) {
ttyflags = ISATTY;
if (tcgetattr(fileno(stdin), &cookedtio) != 0) {
perror("tcgetattr");
exit(1);
}
rawtio = cookedtio;
rawtio.c_iflag = 0;
rawtio.c_oflag = 0;
rawtio.c_lflag = interrupt ? ISIG : 0;
memclr(rawtio.c_cc, NCCS);
rawtio.c_cc[VINTR] = interrupt;
rawtio.c_cc[VMIN] = 1;
}
if (*initfile != '/' && access(initfile, R_OK) != 0) {
char *h = getenv("HOME");
if (h) {
fn = buf;
sprintf((char *) fn, "%s/%s", h, initfile);
}
if (access(fn, R_OK) != 0) {
fn = buf;
sprintf((char *) fn, "%s/%s", SYSCONFDIR, "yazerc");
}
}
if ((su = fopen(fn, "r")) == NULL)
return 0;
while (!go && fgets(buf, BUFSIZ-1, su))
go = docmd(buf);
fclose(su);
atexit(ttycook);
ttyraw();
return go;
}
/* The constat() routine is complicated by the need to deal with CP/M programs
that poll console status without doing any other bios calls.
Examples are Wordstar and Backgrounder II. In Wordstar the purpose is to
act as a crude timer that can be interrupted by the user pressing a key
(delay before a menu is shown). In Backgrounder it's an artifact of the
pseudo multitasking logic.
A simple-minded constat() implementation leads to such CP/M programs soaking
up 100% of the host CPU and getting confused about the system speed.
The 2 constants CSTTIME and CSTCOUNT control the emulation.
constat() normally returns the current console status immediately.
However, after it has been called CSTCOUNT times without any other bios
activity, it waits CSTTIME microseconds (or until a character is available)
before returning.
*/
#define CSTTIME 100000
#define CSTCOUNT 128
static const struct timeval immediate = { 0, 0 };
static const struct timeval delay = { 0, CSTTIME };
static int
constat(void)
{
static int consecutive; /* number of consecutive calls */
static COUNT lastcount; /* previous call */
struct timeval t = immediate;
fd_set rdy;
struct sio *s = getsiop(CHNconin);
int fd;
if (s->fp == NULL) /* no file */
return 0;
if (s->canselect == 0) /* disk files are not always ready! (gr) */
return 1;
if (bioscount != lastcount+1)
consecutive = 0;
else if (++consecutive == CSTCOUNT) {
consecutive = 0;
t = delay;
}
lastcount = bioscount;
fd = fileno(s->fp);
FD_ZERO(&rdy);
FD_SET(fd, &rdy);
(void) select(fd+1, &rdy, NULL, NULL, &t);
return FD_ISSET(fd, &rdy);
}
static int
lststat(void)
{
static int consecutive; /* number of consecutive calls */
static COUNT lastcount; /* previous call */
struct timeval t = immediate;
fd_set rdy;
struct sio *s = getsiop(CHNlst);
int fd;
if (s->fp == NULL) /* no file */
return 0;
if (s->tty == 0) /* disk files are always ready */
return 1;
if (bioscount != lastcount+1)
consecutive = 0;
else if (++consecutive == CSTCOUNT) {
consecutive = 0;
t = delay;
}
lastcount = bioscount;
fd = fileno(s->fp);
FD_ZERO(&rdy);
FD_SET(fd, &rdy);
(void) select(fd+1, &rdy, NULL, NULL, &t);
return FD_ISSET(fd, &rdy);
}
static int
serin(int chan)
{
char c;
int ch;
struct sio *s = getsiop(chan);
if (s->fp == NULL)
return 0x1a;
if (s->tty) {
if (read(fileno(s->fp), &c, 1) == 0) {
return 0x1a;
} else {
return c;
}
}
if ((ch = getc(s->fp)) == EOF)
return 0x1a;
else
return ch & 0xff;
}
static void
serout(int chan, char c)
{
struct sio *s = getsiop(chan);
unsigned int unused __attribute__((unused));
if (s->fp == NULL)
return;
if (s->tty)
unused = write(fileno(s->fp), &c, 1);
else
unused = fwrite(&c, 1, 1, s->fp);
}
static WORD
seldsk(int disk)
{
if (disk < 0 || disk > 15 || !(mnttab[disk].flags & MNT_ACTIVE))
return 0;
curdisk = mnttab + disk;
return curdisk->dph;
}
#ifdef BGii_BUG
/* I cannot persuade Backgrounder ii to output the H at the end of a
VT-100/xterm cursor-positioning sequence. This kludges it. */
void
BGiiFix(int c)
{
static const char cc[] = "\033[0;0";
static const char *p = cc;
if (c == 0)
return;
if ('0' <= c && c <= '9')
return;
if (*p == '0')
p++;
if (*p == c) {
p++;
return;
}
if (*p) {
p = cc;
return;
}
p = cc;
if (c == 'H')
return;
serout(CHNconout, 'H');
}
#else
#define BGiiFix(x)
#endif
#define FCACHE_SIZE 4
static struct fc {
FILE *f;
struct mnt *disk;
struct fdesc *fd;
} fcache[FCACHE_SIZE];
/* clear the file cache when disk is unmounted */
void
clearfc(struct mnt *dp)
{
int i;
for (i = 0; i < FCACHE_SIZE; i++) {
struct fc *f = fcache+i;
if (f->f && f->disk == dp) {
fclose(f->f);
f->f = NULL;
}
}
}
static int
readsec(void)
{
unsigned long offset, blockno;
if (curdisk == NULL || !(curdisk->flags & MNT_ACTIVE))
return 1;
offset = (track*GetWORD(curdisk->dph+16) + sector)*128;
blockno = offset / BLOCK_SIZE;
if ((curdisk->flags & MNT_UNIXDIR) == 0) {
/* the 'disk image' case is easy */
memcpy(ram + dma, curdisk->data + offset, 128);
return 0;
}
/* handle a 'unix directory' disc */
if (blockno < 16) {
/* directory access */
if (offset/32 < curdisk->nde)
memcpy(ram+dma, curdisk->data+offset, 128);
else
memset(ram+dma, 0xe5, 128);
}
else { /* file access */
int i;
for (i = 0; i < FCACHE_SIZE; i++) {
struct fc *f = fcache + i;
if ((f->f == NULL) ||
(f->disk == curdisk && blockno >= f->fd->firstblock &&
blockno <= f->fd->lastblock))
break;
}
if (i == FCACHE_SIZE) {
/* cache overflow */
fclose(fcache[FCACHE_SIZE-1].f);
memmove(fcache+1, fcache, (FCACHE_SIZE - 1) * sizeof(struct fc));
i = 0;
fcache[0].f = NULL;
}
if (fcache[i].f == NULL) {
struct fc *f = fcache + i;
struct fdesc *fd = curdisk->fds;
int j;
f->disk = curdisk;
for (j = 0; j < curdisk->nfds; j++, fd++)
if (blockno >= fd->firstblock && blockno <= fd->lastblock)
break;
if (j >= curdisk->nfds) {
/* cant happen */
return 1;
}
if ((f->f = fopen(fd->fullname, "rb")) == NULL) {
perror(fd->fullname);
return 1;
}
f->fd = fd;
}
if (i != 0) {
/* sort least-recently-used to front */
struct fc temp;
temp = fcache[i];
memmove(fcache+1, fcache, i * sizeof(struct fc));
fcache[0] = temp;
}
if (fseek(fcache[0].f, offset-fcache[0].fd->firstblock*BLOCK_SIZE,
SEEK_SET) < 0)
return 1;
if ((i = fread(ram+dma, 1, 128, fcache[0].f)) < 128)
memset(ram+dma+i, 0x1a, 128-i); /* EOF */
}
return 0;
}
static int
writesec(int stype)
{
if (curdisk == NULL ||
((curdisk->flags & (MNT_ACTIVE|MNT_RDONLY)) != MNT_ACTIVE))
return 1;
memcpy(curdisk->data +
(track*GetWORD(curdisk->dph+16) + sector)*128, ram + dma, 128);
return 0;
}
/* This only works for single-byte sector numbers
(but then, so does the CP/M 2.2 CBIOS code).
*/
static WORD
sectrans(WORD sec, WORD table)
{
if (table == 0)
return sec;
return GetBYTE(table + sec);
}
static BYTE savcpm[CPM_LENGTH]; /* saved copy of cpm */
void
bios(int func)
{
bioscount++;
switch (func) {
case 0: /* cold boot */
memcpy(savcpm, ram + ccp_base, CPM_LENGTH);
goto gocpm;
case 1: /* warm boot */
memcpy(ram + ccp_base, savcpm, CPM_LENGTH);
gocpm:
dma = 0x80;
ram[0] = 0xc3;
ram[1] = 0x03;
ram[2] = hreg(bios_base);
ram[5] = 0xc3;
ram[6] = 0x06;
ram[7] = hreg(bdos_base);
Setlreg(BC, ram[4]);
pc = ccp_base;
return;
case 2: /* console status */
Sethreg(AF, constat() ? 0xff : 0x00);
break;
case 3: /* console input */
Sethreg(AF, serin(CHNconin) & 0x7f);
break;
case 4: /* console output */
BGiiFix(lreg(BC));
serout(CHNconout, lreg(BC));
break;
case 5: /* list output */
serout(CHNlst, lreg(BC));
break;
case 6: /* punch output */
serout(CHNpun, lreg(BC));
break;
case 7: /* tape reader input */
Sethreg(AF, serin(CHNrdr));
break;
case 8: /* home disk */
track = 0;
break;
case 9: /* select disk */
HL = seldsk(lreg(BC));
break;
case 10: /* set track */
track = BC;
break;
case 11: /* set sector */
sector = BC;
break;
case 12: /* set dma */
dma = BC;
break;
case 13: /* read sector */
Sethreg(AF, readsec());
break;
case 14: /* write sector */
Sethreg(AF, writesec(lreg(BC)));
break;
case 15: /* list status */
Sethreg(AF, lststat() ? 0xff : 0x00);
break;
case 16: /* translate sector */
HL = sectrans(BC, DE);
break;
case 253: /* return from recursive call */
return;
case 254:
++pc; /* meta-level command */
if (GetBYTE(pc) == 0)
monitor(0);
else {
/* need a copy because docmd() scratches its argument */
char *sav = newstr((char *) ram+pc);
ttycook();
(void) docmd(sav);
ttyraw();
free(sav);
pc += strlen((char *) ram+pc);
}
break;
case 255: /* quit */
exit(0);
break;
default:
fprintf(stderr, "Invalid bios function: %d\r\n", func);
}
pc++;
}