2019-05-10 04:09:40 +10:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2019-11-16 01:57:53 +11:00
|
|
|
#include <string.h>
|
2019-12-03 08:35:49 +11:00
|
|
|
#include "../emul.h"
|
2019-06-03 22:12:44 +10:00
|
|
|
#include "kernel-bin.h"
|
|
|
|
#include "zasm-bin.h"
|
2019-05-10 04:09:40 +10:00
|
|
|
|
2019-05-11 10:32:05 +10:00
|
|
|
/* zasm reads from a specified blkdev, assemble the file and writes the result
|
|
|
|
* in another specified blkdev. In our emulator layer, we use stdin and stdout
|
|
|
|
* as those specified blkdevs.
|
2019-05-10 04:09:40 +10:00
|
|
|
*
|
2019-11-16 01:57:53 +11:00
|
|
|
* This executable takes two arguments. Both are optional, but you need to
|
|
|
|
* specify the first one if you want to get to the second one.
|
|
|
|
* The first one is the value to send to z80-zasm's 3rd argument (the initial
|
|
|
|
* .org). Defaults to '00'.
|
|
|
|
* The second one is the path to a .cfs file to use for includes.
|
2019-06-03 05:50:59 +10:00
|
|
|
*
|
2019-05-11 10:32:05 +10:00
|
|
|
* Because the input blkdev needs support for Seek, we buffer it in the emulator
|
|
|
|
* layer.
|
2019-05-10 04:09:40 +10:00
|
|
|
*
|
|
|
|
* Memory layout:
|
|
|
|
*
|
|
|
|
* 0x0000 - 0x3fff: ROM code from zasm_glue.asm
|
2019-05-11 08:20:43 +10:00
|
|
|
* 0x4000 - 0x47ff: RAM for kernel and stack
|
|
|
|
* 0x4800 - 0x57ff: Userspace code
|
|
|
|
* 0x5800 - 0xffff: Userspace RAM
|
2019-05-10 04:09:40 +10:00
|
|
|
*
|
|
|
|
* I/O Ports:
|
|
|
|
*
|
|
|
|
* 0 - stdin / stdout
|
2019-05-11 09:55:01 +10:00
|
|
|
* 1 - When written to, rewind stdin buffer to the beginning.
|
2019-05-10 04:09:40 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
// in sync with zasm_glue.asm
|
2019-05-20 02:57:59 +10:00
|
|
|
#define USER_CODE 0x4800
|
2019-05-10 04:09:40 +10:00
|
|
|
#define STDIO_PORT 0x00
|
2019-05-17 02:07:57 +10:00
|
|
|
#define STDIN_SEEK_PORT 0x01
|
|
|
|
#define FS_DATA_PORT 0x02
|
|
|
|
#define FS_SEEK_PORT 0x03
|
2019-05-18 03:22:45 +10:00
|
|
|
#define STDERR_PORT 0x04
|
2019-05-10 04:09:40 +10:00
|
|
|
|
2019-05-11 09:55:01 +10:00
|
|
|
// Other consts
|
|
|
|
#define STDIN_BUFSIZE 0x8000
|
2019-05-12 22:49:59 +10:00
|
|
|
// When defined, we dump memory instead of dumping expected stdout
|
|
|
|
//#define MEMDUMP
|
2019-05-13 12:07:21 +10:00
|
|
|
//#define DEBUG
|
2019-06-20 01:42:39 +10:00
|
|
|
// By default, we don't spit what zasm prints. Too noisy. Define VERBOSE if
|
|
|
|
// you want to spit this content to stderr.
|
|
|
|
//#define VERBOSE
|
2019-05-12 22:49:59 +10:00
|
|
|
|
2019-05-11 09:55:01 +10:00
|
|
|
// STDIN buffer, allows us to seek and tell
|
|
|
|
static uint8_t inpt[STDIN_BUFSIZE];
|
|
|
|
static int inpt_size;
|
|
|
|
static int inpt_ptr;
|
2019-05-16 10:07:21 +10:00
|
|
|
static uint8_t middle_of_seek_tell = 0;
|
2019-05-10 04:09:40 +10:00
|
|
|
|
2019-12-03 12:18:41 +11:00
|
|
|
static uint8_t fsdev[0x80000] = {0};
|
2019-05-29 05:56:39 +10:00
|
|
|
static uint32_t fsdev_size = 0;
|
|
|
|
static uint32_t fsdev_ptr = 0;
|
|
|
|
static uint8_t fsdev_seek_tell_cnt = 0;
|
2019-05-17 02:07:57 +10:00
|
|
|
|
2019-12-03 08:35:49 +11:00
|
|
|
static uint8_t iord_stdio()
|
2019-05-10 04:09:40 +10:00
|
|
|
{
|
2019-12-03 08:35:49 +11:00
|
|
|
if (inpt_ptr < inpt_size) {
|
|
|
|
return inpt[inpt_ptr++];
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t iord_stdin_seek()
|
|
|
|
{
|
|
|
|
if (middle_of_seek_tell) {
|
|
|
|
middle_of_seek_tell = 0;
|
|
|
|
return inpt_ptr & 0xff;
|
|
|
|
} else {
|
2019-05-16 10:07:21 +10:00
|
|
|
#ifdef DEBUG
|
2019-12-03 08:35:49 +11:00
|
|
|
fprintf(stderr, "tell %d\n", inpt_ptr);
|
2019-05-16 10:07:21 +10:00
|
|
|
#endif
|
2019-12-03 08:35:49 +11:00
|
|
|
middle_of_seek_tell = 1;
|
|
|
|
return inpt_ptr >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t iord_fsdata()
|
|
|
|
{
|
|
|
|
if (fsdev_ptr < fsdev_size) {
|
|
|
|
return fsdev[fsdev_ptr++];
|
2019-05-10 04:09:40 +10:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 08:35:49 +11:00
|
|
|
static uint8_t iord_fsseek()
|
|
|
|
{
|
|
|
|
if (fsdev_seek_tell_cnt != 0) {
|
|
|
|
return fsdev_seek_tell_cnt;
|
|
|
|
} else if (fsdev_ptr >= fsdev_size) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iowr_stdio(uint8_t val)
|
2019-05-10 04:09:40 +10:00
|
|
|
{
|
2019-05-12 22:49:59 +10:00
|
|
|
// When mem-dumping, we don't output regular stuff.
|
|
|
|
#ifndef MEMDUMP
|
2019-12-03 08:35:49 +11:00
|
|
|
putchar(val);
|
2019-05-16 10:07:21 +10:00
|
|
|
#endif
|
2019-12-03 08:35:49 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static void iowr_stdin_seek(uint8_t val)
|
|
|
|
{
|
|
|
|
if (middle_of_seek_tell) {
|
|
|
|
inpt_ptr |= val;
|
|
|
|
middle_of_seek_tell = 0;
|
2019-05-17 02:07:57 +10:00
|
|
|
#ifdef DEBUG
|
2019-12-03 08:35:49 +11:00
|
|
|
fprintf(stderr, "seek %d\n", inpt_ptr);
|
2019-06-20 01:42:39 +10:00
|
|
|
#endif
|
2019-05-10 04:09:40 +10:00
|
|
|
} else {
|
2019-12-03 08:35:49 +11:00
|
|
|
inpt_ptr = (val << 8) & 0xff00;
|
|
|
|
middle_of_seek_tell = 1;
|
2019-05-10 04:09:40 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 08:35:49 +11:00
|
|
|
static void iowr_fsdata(uint8_t val)
|
2019-05-10 04:09:40 +10:00
|
|
|
{
|
2019-12-03 08:35:49 +11:00
|
|
|
if (fsdev_ptr < fsdev_size) {
|
|
|
|
fsdev[fsdev_ptr++] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iowr_fsseek(uint8_t val)
|
|
|
|
{
|
|
|
|
if (fsdev_seek_tell_cnt == 0) {
|
|
|
|
fsdev_ptr = val << 16;
|
|
|
|
fsdev_seek_tell_cnt = 1;
|
|
|
|
} else if (fsdev_seek_tell_cnt == 1) {
|
|
|
|
fsdev_ptr |= val << 8;
|
|
|
|
fsdev_seek_tell_cnt = 2;
|
|
|
|
} else {
|
|
|
|
fsdev_ptr |= val;
|
|
|
|
fsdev_seek_tell_cnt = 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "FS seek %d\n", fsdev_ptr);
|
|
|
|
#endif
|
|
|
|
}
|
2019-05-10 04:09:40 +10:00
|
|
|
}
|
|
|
|
|
2019-12-03 08:35:49 +11:00
|
|
|
static void iowr_stderr(uint8_t val)
|
2019-05-10 04:09:40 +10:00
|
|
|
{
|
2019-12-03 08:35:49 +11:00
|
|
|
#ifdef VERBOSE
|
|
|
|
fputc(val, stderr);
|
|
|
|
#endif
|
2019-05-10 04:09:40 +10:00
|
|
|
}
|
|
|
|
|
2019-06-03 05:50:59 +10:00
|
|
|
int main(int argc, char *argv[])
|
2019-05-10 04:09:40 +10:00
|
|
|
{
|
2019-11-16 01:57:53 +11:00
|
|
|
if (argc > 3) {
|
2019-06-03 05:50:59 +10:00
|
|
|
fprintf(stderr, "Too many args\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-12-03 08:35:49 +11:00
|
|
|
Machine *m = emul_init();
|
|
|
|
m->iord[STDIO_PORT] = iord_stdio;
|
|
|
|
m->iord[STDIN_SEEK_PORT] = iord_stdin_seek;
|
|
|
|
m->iord[FS_DATA_PORT] = iord_fsdata;
|
|
|
|
m->iord[FS_SEEK_PORT] = iord_fsseek;
|
|
|
|
m->iowr[STDIO_PORT] = iowr_stdio;
|
|
|
|
m->iowr[STDIN_SEEK_PORT] = iowr_stdin_seek;
|
|
|
|
m->iowr[FS_DATA_PORT] = iowr_fsdata;
|
|
|
|
m->iowr[FS_SEEK_PORT] = iowr_fsseek;
|
|
|
|
m->iowr[STDERR_PORT] = iowr_stderr;
|
2019-05-10 04:09:40 +10:00
|
|
|
// initialize memory
|
|
|
|
for (int i=0; i<sizeof(KERNEL); i++) {
|
2019-12-03 08:35:49 +11:00
|
|
|
m->mem[i] = KERNEL[i];
|
2019-05-10 04:09:40 +10:00
|
|
|
}
|
2019-05-20 02:57:59 +10:00
|
|
|
for (int i=0; i<sizeof(USERSPACE); i++) {
|
2019-12-03 08:35:49 +11:00
|
|
|
m->mem[i+USER_CODE] = USERSPACE[i];
|
2019-05-20 02:57:59 +10:00
|
|
|
}
|
2019-11-16 01:57:53 +11:00
|
|
|
char *init_org = "00";
|
|
|
|
if (argc >= 2) {
|
|
|
|
init_org = argv[1];
|
|
|
|
if (strlen(init_org) != 2) {
|
|
|
|
fprintf(stderr, "Initial org must be a two-character hex string");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// glue.asm knows that it needs to fetch these arguments at this address.
|
2019-12-03 08:35:49 +11:00
|
|
|
m->mem[0xff00] = init_org[0];
|
|
|
|
m->mem[0xff01] = init_org[1];
|
2019-06-03 05:50:59 +10:00
|
|
|
fsdev_size = 0;
|
2019-11-16 01:57:53 +11:00
|
|
|
if (argc == 3) {
|
|
|
|
FILE *fp = fopen(argv[2], "r");
|
2019-06-03 05:50:59 +10:00
|
|
|
if (fp == NULL) {
|
|
|
|
fprintf(stderr, "Can't open file %s\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int c = fgetc(fp);
|
|
|
|
while (c != EOF) {
|
|
|
|
fsdev[fsdev_size] = c;
|
|
|
|
fsdev_size++;
|
|
|
|
c = fgetc(fp);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2019-05-17 02:07:57 +10:00
|
|
|
}
|
2019-05-11 09:55:01 +10:00
|
|
|
// read stdin in buffer
|
|
|
|
inpt_size = 0;
|
|
|
|
inpt_ptr = 0;
|
|
|
|
int c = getchar();
|
|
|
|
while (c != EOF) {
|
|
|
|
inpt[inpt_ptr] = c & 0xff;
|
|
|
|
inpt_ptr++;
|
|
|
|
if (inpt_ptr == STDIN_BUFSIZE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = getchar();
|
|
|
|
}
|
|
|
|
inpt_size = inpt_ptr;
|
|
|
|
inpt_ptr = 0;
|
2019-12-03 08:35:49 +11:00
|
|
|
|
|
|
|
emul_loop();
|
2019-05-12 22:49:59 +10:00
|
|
|
#ifdef MEMDUMP
|
|
|
|
for (int i=0; i<0x10000; i++) {
|
|
|
|
putchar(mem[i]);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-10 04:09:40 +10:00
|
|
|
fflush(stdout);
|
2019-12-03 08:35:49 +11:00
|
|
|
int res = m->cpu.R1.br.A;
|
2019-05-28 01:04:31 +10:00
|
|
|
if (res != 0) {
|
2019-12-03 08:35:49 +11:00
|
|
|
int lineno = m->cpu.R1.wr.HL;
|
|
|
|
int inclineno = m->cpu.R1.wr.DE;
|
2019-05-28 23:57:29 +10:00
|
|
|
if (inclineno) {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"Error %d on line %d, include line %d\n",
|
|
|
|
res,
|
|
|
|
lineno,
|
|
|
|
inclineno);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Error %d on line %d\n", res, lineno);
|
|
|
|
}
|
2019-05-28 01:04:31 +10:00
|
|
|
}
|
|
|
|
return res;
|
2019-05-10 04:09:40 +10:00
|
|
|
}
|
|
|
|
|