home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Source Code 1992 March
/
Source_Code_CD-ROM_Walnut_Creek_March_1992.iso
/
usenet
/
altsrcs
/
2
/
2562
/
cpm.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-01-22
|
20KB
|
1,084 lines
/*
cpm
CP/M emulator.
Written by D'Arcy J.M. Cain
darcy@druid
*/
#define CPM_DATA
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <ctype.h>
#include <termio.h>
#include <io.h>
#include <signal.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include "cpm.h"
#define FILLER_SIZE (16 - sizeof(FILE *))
#ifdef DEBUG
#define debug(x) fprintf(stderr, "%s: %4d %s\n", __FILE__, __LINE__, x)
#else
#define debug(x)
#endif
typedef struct {
byte dr; /* drive: 0 = default, 1 = A, 2 = B, etc. */
char name[8]; /* file name up to 8 characters */
char typ[3]; /* file type up to 3 characters */
byte ex, s1, s2, rc;
FILE *fp; /* Unix file pointer */
byte filler[FILLER_SIZE];
byte cr, r0, r1, r2;
} FCB;
#ifdef CPM_DEBUG
#define dump_registers(x) \
fprintf(x, reg_dump, A, BC, DE, HL, SP, PC,\
SIGN ? 'S' : '-',\
ZERO ? 'Z' : '-',\
HALF_CARRY ? 'H' : '-',\
PARITY ? 'P' : '-',\
BCD ? 'N' : '-',\
CARRY ? 'C' : '-',\
ram[PC], dasm(ram + PC))
static int dasm_flag = 0;
static char *reg_dump =
"A=%02.2x BC=%4.4x DE=%04.4x HL=%04.4x SP=%04.4x PC=%04.4x %c%c%c%c%c%c %02.2x %s\r\n";
#else
#define dump_registers(x)
#endif
struct termio old_term, termp;
static int console, user_break;
#ifndef COMPILE_TEST
static byte *dma;
static char *tail;
static int out_delim = '$', def_drive = 1;
static FILE *reader = NULL, *punch = NULL, *list = NULL;
#endif
/* clean up routine */
void cleanup(int sig)
{
if (sig == SIGINT)
{
user_break = 1;
signal(SIGINT, cleanup);
return;
}
ioctl(console, TCSETA, &old_term);
printf("\nWe now return you to your regularly scheduled OS\n");
exit(0);
}
/* How CP/M drives map to Unix: */
/* below is an array of 17 strings. This corresponds to the allowable */
/* drive names in CP/M (A to P) plus the default drive which actually */
/* is a duplicate of one of the next 16. At startup, The current Unix */
/* directory is copied into cpm_drive[1] and becomes drive A. This may */
/* be modified by the startup sequence. As well, the other drives may */
/* be set up to other directories. At the end of the startup sequence */
/* the "strcpy(cpm_drive[0], cpm_drive[1]) causes drive A to be the CP/M */
/* default drive. From that point on, a switch to a new drive involves */
/* simply copying that drive's directory into cpm_drive[0]. I did this */
/* in this way since I expect changing drives to occur less frequently */
/* than accessing files. */
#ifndef COMPILE_TEST
static char cpm_drive[17][128];
/* Convert string to upper case */
static void strtoup(char *s)
{
while (*s)
{
*s = toupper(*s);
s++;
}
}
/* take a string, terminate it at the first white space and return the
string that follows. I.E: "DIR *.COM" puts a 0 in the first space
and returns a pointer to "*.COM". Note that "DIR" returns a pointer
to a NULL string - NOT a NULL pointer. */
static char *chop_cmd(char *buf)
{
char *ptr = buf;
/* discard leading space */
while (isspace(*ptr))
ptr++;
/* quad left the string */
strcpy(buf, ptr);
/* terminate first word */
ptr = buf;
while (!isspace(*ptr) && *ptr)
ptr++;
/* is there more? */
if (*ptr)
{
/* terminate first word */
*ptr++ = 0;
/* skip any leading space */
while (isspace(*ptr))
ptr++;
}
return(ptr);
}
/* given a drive unit (0 - 16) and a file name, returns Unix file name */
static char *mk_name(int dr, char *fname)
{
static char full_name[148];
sprintf(full_name, "%s/%s", cpm_drive[dr], fname);
if (strchr(fname, '.') == NULL)
strcat(full_name, ".");
return(full_name);
}
/* given a file spec in standard CP/M format returns Unix file name */
static char *real_name(char *fname)
{
/* does it include a drive letter? */
if (fname[1] == ':')
return(mk_name(*fname - '@', fname + 2));
/* else use default drive */
return(mk_name(0, fname));
}
/* given a pointer to an FCB, returns real file name */
char *fcb2real(FCB *buf)
{
char temp[16], *p = temp;
int k = 0;
for (k = 0; k < 8; k++)
if (!isspace(buf->name[k]))
*p++ = buf->name[k];
*p++ = '.';
for (k = 0; k < 3; k++)
if (!isspace(buf->typ[k]))
*p++ = buf->typ[k];
*p = 0;
return(mk_name(buf->dr, temp));
}
/* calls system command with CP/M file name converted to Unix */
static void fsystem(const char *s, char *file)
{
char command[256];
sprintf(command, s, real_name(file));
ioctl(console, TCSETA, &old_term);
system(command);
ioctl(console, TCSETA, &termp);
}
/* formats a CP/M file name into an FCB */
static void mk_fcb(FCB *buf, char *fname)
{
char *p = fname;
int k, l;
/* clear FCB to start with */
memset(buf, 0, 16);
/* check for drive name */
if (p[1] == ':')
{
debug("");
buf->dr = *p - '@';
p += 2;
}
k = l = 0;
/* format primary name */
for (k = 0; k < 8; k++)
{
debug("");
if ((p[l] == '.') || (p[l] == 0))
{
debug("");
while (k < 8)
{
debug("");
buf->name[k++] = ' ';
}
}
else if (p[l] == '*')
{
debug("");
while (k < 8)
buf->name[k++] = '?';
debug("");
while (p[l] && (p[l] != '.'))
l++;
debug("");
}
else
buf->name[k] = p[l];
debug("");
l++;
}
debug("");
/* format file type */
for (k = 0; k < 3; k++)
{
debug("");
if ((p[l] == '.') || (p[l] == 0))
while (k < 3)
buf->typ[k++] = ' ';
else if (p[l] == '*')
while (k < 3)
buf->typ[k++] = '?';
else
buf->typ[k] = p[l];
debug("");
l++;
}
debug("");
return;
}
/* add extension to file name. replace current one if necessary */
static void addext(char *s1, char *s2)
{
char *p;
if ((p = strchr(s1, '.')) == NULL)
strcat(s1, ".");
strcat(s1, s2);
}
#endif
/* get a character from the terminal */
byte getch(void)
{
byte c = 0;
while (read(console, &c, 1) != 1)
;
return(c);
}
/* see if character waiting */
#define kbhit() ioctl(console, FIORDCHK, NULL)
/* get a string */
int get_str(char *buffer, int maxlen)
{
int k = 0, c;
while ((c = getch()) != '\r' && c != '\n')
{
if (k == maxlen)
c = '\a';
else if (c == '\b')
{
if (k)
{
fprintf(stderr, "\b \b");
k--;
}
}
else
{
fputc(c, stdout);
buffer[k++] = c;
}
}
fprintf(stderr, "\r\n");
return(k);
}
#ifdef CPM_DEBUG
#define is_breakpoint(x) breakpoint(0, (x))
#define list_breakpoints() breakpoint(0, 0)
#define add_breakpoint(x) breakpoint(1, (x))
#define del_breakpoint(x) breakpoint(2, (x))
int breakpoint(int cmd, int bpoint)
{
static int bp[64];
int k;
switch(cmd)
{
case 0:
for (k = 0; k < 64; k++)
{
if (bp[k])
{
if (!bpoint)
fprintf(stderr, "Breakpoint %2d: 0x%04.4x\r\n");
else if (bp[k] == bpoint)
return(1);
}
}
return(0);
break;
case 1:
for (k = 0; k < 64; k++)
if (bp[k] == bpoint)
return(k);
for (k = 0; k < 64; k++)
{
if (!bp[k])
{
bp[k] = bpoint;
return(k);
}
}
fprintf(stderr, "Too many breakpoints\r\n");
return(-1);
break;
case 2:
for (k = 0; k < 64; k++)
if (bp[k] == bpoint)
bp[k] = 0;
return(0);
break;
}
return(-1);
}
int debugger()
{
char entry[128], *ptr;
user_break = 0;
for (;;)
{
fprintf(stderr, "\r\nDEBUG> ");
ptr = entry;
while ((*ptr = getch()) != '\n')
{
if (*ptr == '\b')
{
if (ptr > entry)
{
fprintf(stderr, "\b \b");
ptr--;
}
}
else
fputc(*ptr++, stdout);
}
*ptr = 0;
strtoup(entry);
fprintf(stderr, "\r\n");
if (!*entry)
;
else if (*entry == 'G')
return(0);
else if (*entry == 'Q')
return(1);
else if (*entry == 'R')
dump_registers(stdout);
else if (*entry == '+')
add_breakpoint(atoi(entry + 1));
else if (*entry == '-')
del_breakpoint(atoi(entry + 1));
else if (*entry == 'L')
list_breakpoints();
else if (isdigit(*entry))
dasm_flag = *entry - '0';
#if 0
else if (*entry == '')
else if (*entry == '')
else if (*entry == '')
else if (*entry == '')
#endif
else
fprintf(stderr, "\aUnknown command: %c\n", *entry);
}
}
#endif
#ifndef COMPILE_TEST
/* run a program */
static int run(char *program)
{
byte *mem_ptr = ram + 0x100;
char *fn, fn2[128];
int c, k, pc;
FILE *fp;
FCB *fcb = NULL;
long f_pos;
struct stat s;
debug("Start run function");
/* find the program name */
strcpy((char *)(mem_ptr), program);
addext((char *)(mem_ptr), "COM");
/* open the command file - return error if not found */
if ((fp = fopen((char *)(mem_ptr), "rb")) == NULL)
return(-1);
debug("");
/* load command into memory */
while (fread(mem_ptr, 1, 0x100, fp))
{
if (mem_ptr > (ram + 0xf000))
{
fprintf(stderr, "\aCommand file too big\r\n");
return(-2);
}
mem_ptr += 0x100;
}
fclose(fp);
debug("");
/* set up registers and page zero */
PC = 0x100;
SP = 0xfff0;
/* following for test purposes */
A = 1;
BC = 0x2345;
DE = 0x6789;
HL = 0xabcd;
debug("");
strcpy((char *)(ram + 0x80), tail);
debug("");
mem_ptr = (byte *)(chop_cmd(tail));
debug("");
mk_fcb((FCB *)(ram + 0x5c), tail);
debug("");
mk_fcb((FCB *)(ram + 0x6c), (char *)(mem_ptr));
debug("");
memcpy(ram, page_zero, sizeof(page_zero));
debug("");
dma = ram + 0x80;
debug("");
debug("");
/* BDOS, BIOS and default stack */
for (k = 0xfc00; k < 0x10000; k++)
ram[k] = 0;
debug("");
/* run program. loop stops if PC = 0 - "JP 0" e.g. */
while (PC)
{
#ifdef CPM_DEBUG
if (dasm_flag > 1)
dump_registers(stderr);
if ((user_break && debugger()) || is_breakpoint(PC))
#else
if (user_break)
#endif
{
fprintf(stderr, "\r\n\n\a* Program Interrupted by user *\r\n", ram[PC]);
dump_registers(stderr);
return(-5);
}
debug("");
pc = PC;
/* check if PC = BDOS entry point */
if (PC == BDOS)
{
/* do CP/M service if so */
switch (C)
{
case 0: /* system reset */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: System reset\r\n");
#endif
return(0);
case 1: /* conin */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Console in\r\n");
#endif
fputc((A = getch()), stdout);
break;
case 2: /* conout */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Console out (%c)\r\n", E >= ' ' ? E : '.');
#endif
fputc(E, stdout);
break;
case 3: /* RDR */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Reader in\r\n");
#endif
if (reader != NULL)
A = fgetc(reader);
break;
case 4: /* PUN */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Punch out (%c)\r\n", E >= ' ' ? E : '.');
#endif
if (punch != NULL)
fputc(E, punch);
break;
case 5: /* LST */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: List out (%c)\r\n", E >= ' ' ? E : '.');
#endif
if (list != NULL)
fputc(E, list);
break;
case 6: /* CONIO */
#ifdef CPM_DEBUG
if (dasm_flag)
{
fprintf(stderr, "BDOS: Conio ");
if (E == 0xff)
fprintf(stderr, "in\r\n");
else
fprintf(stderr, "out (%c)\r\n", E >= ' ' ? E : '.');
}
#endif
if (E == 0xff)
A = getch();
else
fputc(E, stdout);
break;
case 7: /* get IOBYTE */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Get IOBYTE\r\n");
#endif
A = 0x95;
break;
case 8: /* set IOBYTE */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Set IOBYTE\r\n");
#endif
break;
case 28: /* write protect disk */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Write protect disk\r\n");
#endif
break;
case 9: /* prstr */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Print string\r\n");
#endif
mem_ptr = ram + DE;
while (*mem_ptr != out_delim)
fputc(*mem_ptr++, stdout);
break;
case 10: /* rdstr */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Read console buffer\r\n");
#endif
ram[DE + 1] = get_str((char *)(ram) + DE + 2, ram[DE]);
break;
case 11: /* CONSTAT */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Get console status\r\n");
#endif
A = kbhit() ? 0xff : 0;
break;
case 12: /* VERSION */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Return version number\r\n");
#endif
HL = 0x0022;
break;
case 13: /* RSTDSK */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Reset disk system\r\n");
#endif
break;
case 14: /* SELDSK */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Select disk %c:\r\n", E + 'A');
#endif
k = E + 1;
A = 0xff;
if ((k < 1) || (k > 16))
H = 4;
else if (*cpm_drive[k] == 0)
H = 1;
else
{
def_drive = k;
strcpy(cpm_drive[0], cpm_drive[k]);
A = 0;
}
break;
case 15: /* OPENF */
fcb = (FCB *)(ram + DE);
fn = fcb2real(fcb);
memset(&fcb->fp, 0, 24);
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Open file %s\r\n", fn);
#endif
A = 0xff;
if (strchr(fn, '?') != NULL)
HL = 9;
else if ((fcb->dr < 0) || (fcb->dr > 16))
HL = 4;
else if (*cpm_drive[fcb->dr] == 0)
HL = 1;
else if ((fcb->fp = fopen(fn, "r+")) == NULL)
HL = 0;
else
A = HL = 0;
break;
case 16:
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Close file\r\n");
#endif
fcb = (FCB *)(ram + DE);
if (fcb->fp != NULL)
fclose(fcb->fp);
fcb->fp = NULL;
break;
case 19:
fcb = (FCB *)(ram + DE);
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Delete file\r\n", fcb2real(fcb));
#endif
unlink(fcb2real(fcb));
fcb->fp = NULL;
break;
case 20: /* READ */
case 33: /* READ RANDOM */
#ifdef CPM_DEBUG
if (dasm_flag)
{
fprintf(stderr, "BDOS: Read ");
if (C == 20)
fprintf(stderr, "sequential");
else
fprintf(stderr, "random");
}
#endif
if ((fcb = (FCB *)(ram + DE)) == NULL)
{
A = 9;
break;
}
memset(dma, 0x1a, 0x80);
if (C == 33)
{
f_pos = (fcb->r2 << 16) + (fcb->r1 << 8) + fcb->r0;
fseek(fcb->fp, f_pos * 0x80, SEEK_SET);
}
if (fread(dma, 1, 0x80, fcb->fp) == 0)
A = 1;
else
A = 0;
break;
case 21: /* WRITE */
case 34: /* WRITE RANDOM */
case 40: /* Write Random Zero Fill */
#ifdef CPM_DEBUG
if (dasm_flag)
{
fprintf(stderr, "BDOS: Write ");
if (C == 21)
fprintf(stderr, "sequential\r\n");
else if (C == 34)
fprintf(stderr, "random\r\n");
else
fprintf(stderr, "random with zero fill\r\n");
}
#endif
if ((fcb = (FCB *)(ram + DE)) == NULL)
{
A = 9;
break;
}
if (C == 34)
{
f_pos = (fcb->r2 << 16) + (fcb->r1 << 8) + fcb->r0;
fseek(fcb->fp, f_pos * 0x80, SEEK_SET);
}
if (fwrite(dma, 1, 0x80, fcb->fp) == 0)
A = 1;
else
A = 0;
break;
case 22: /* MAKEF */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Make file\r\n");
#endif
fcb = (FCB *)(ram + DE);
fn = fcb2real(fcb);
if ((fcb->fp = fopen(fn, "r")) != NULL)
{
fclose(fcb->fp);
A = 0xff;
break;
}
memset(&fcb->fp, 0, 24);
A = 0xff;
if (strchr(fn, '?') != NULL)
HL = 9;
else if ((fcb->dr < 0) || (fcb->dr > 16))
HL = 4;
else if (*cpm_drive[fcb->dr] == 0)
HL = 1;
else if ((fcb->fp = fopen(fn, "w")) == NULL)
HL = 0;
else
A = HL = 0;
break;
case 23: /* RENAME */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Rename file\r\n");
#endif
fcb = (FCB *)(ram + DE);
strcpy(fn2, fcb2real(fcb));
fn = fcb2real(fcb + 16);
if (link(fn2, fn) == -1)
A = 0xff;
else
{
unlink(fn2);
A = 0;
}
break;
case 24: /* get log in vector */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Get login vector\r\n");
#endif
c = 1;
HL = 0;
for (k = 1; k <= 16; k++)
{
if (*cpm_drive[k])
HL |= c;
c <<= 1;
}
A = L;
break;
case 25:
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Return current disk\r\n");
#endif
A = def_drive - 1;
break;
case 26:
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Set DMA address\r\n");
#endif
dma = ram + DE;
break;
case 29: /* get R/O vector */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Get read only vector\r\n");
#endif
HL = 0;
break;
case 35: /* get file size */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Compute file size\r\n");
#endif
fcb = (FCB *)(ram + DE);
if (stat(fcb2real(fcb), &s) == -1)
{
A = 0xff;
break;
}
A = 0;
/* fall through */
case 36: /* set random record */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Set random record\r\n");
#endif
if (C == 36)
{
if ((fcb = (FCB *)(ram + DE)) == NULL)
break;
s.st_size = ftell(fcb->fp);
}
s.st_size >>= 7;
fcb->r0 = s.st_size & 0xff;
s.st_size >>= 8;
fcb->r1 = s.st_size & 0xff;
s.st_size >>= 8;
fcb->r2 = s.st_size & 0xff;
break;
case 37: /* reset drive */
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BDOS: Reset drive\r\n");
#endif
A = 0;
break;
default:
fprintf(stderr, "\a\r\nInvalid BDOS call %d\r\n", C);
return(-3);
}
}
else if (PC >= BIOS)
{
if (PC % 3)
{
fprintf(stderr, "\a\r\nInvalid BIOS jump 0%04.4x\r\n", pc);
PC = pc;
dump_registers(stderr);
return(-5);
}
#ifdef CPM_DEBUG
if (dasm_flag)
fprintf(stderr, "BIOS: Function %d\r\n", (PC - BIOS)/3);
#endif
switch (PC)
{
case bios(0):
return(0);
default:
PC = pc;
fprintf(stderr, "Unimplemented BIOS jump 0%04.4xH\r\n", PC);
dump_registers(stderr);
return(-6);
}
}
if (decode())
{
PC = pc;
fprintf(stderr, "\a\r\nInvalid processor instruction 0x%02.2x\r\n", ram[PC]);
dump_registers(stderr);
return(-4);
}
#ifdef CPM_DEBUG
if (dasm_flag > 1 && pc >= BDOS)
getch();
#endif
}
return(0);
}
#endif
FILE *open_device(char *dev, char *typ)
{
FILE *fp;
if (*dev == '!')
fp = popen(dev + 1, typ);
else
fp = fopen(dev, typ);
if (fp != NULL)
return(fp);
fprintf(stderr, "Error on %s\r\n", dev);
perror("Can't open virtual device");
exit(1);
return(NULL);
}
#ifndef COMPILE_TEST
static int do_command(char *cmd_str)
{
char entry[256];
FILE *fp;
if ((*cmd_str == ';') || (*cmd_str == '#'))
return(0);
strcpy(entry, cmd_str);
if (*entry == '!')
{
int r;
ioctl(console, TCSETA, &old_term);
r = system(entry + 1);
ioctl(console, TCSETA, &termp);
return(r);
}
strtoup(entry);
tail = chop_cmd(entry);
user_break = 0;
if ((isspace(entry[2]) || (entry[2] == 0)) && (entry[1] == ':'))
{
*entry -= '@';
if ((*entry < 1) || (*entry > MAX_DRIVES) || (*cpm_drive[*entry] == 0))
{
fprintf(stderr, "\a\r\nInvalid drive specification\r\n");
return(-1);