2019-05-10 04:09:40 +10:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2019-05-17 21:23:59 +10:00
|
|
|
#include "../libz80/z80.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-06-03 05:50:59 +10:00
|
|
|
* This executable takes one argument: the path to a .cfs file to use for
|
|
|
|
* includes.
|
|
|
|
*
|
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-10 04:09:40 +10:00
|
|
|
static Z80Context cpu;
|
2019-05-11 09:55:01 +10:00
|
|
|
static uint8_t mem[0x10000];
|
|
|
|
// 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-06-03 06:46:44 +10:00
|
|
|
static uint8_t fsdev[0x40000] = {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-05-10 04:09:40 +10:00
|
|
|
static uint8_t io_read(int unused, uint16_t addr)
|
|
|
|
{
|
|
|
|
addr &= 0xff;
|
|
|
|
if (addr == STDIO_PORT) {
|
2019-05-11 09:55:01 +10:00
|
|
|
if (inpt_ptr < inpt_size) {
|
|
|
|
return inpt[inpt_ptr++];
|
|
|
|
} else {
|
2019-05-10 04:09:40 +10:00
|
|
|
return 0;
|
|
|
|
}
|
2019-05-17 02:07:57 +10:00
|
|
|
} else if (addr == STDIN_SEEK_PORT) {
|
2019-05-16 10:07:21 +10:00
|
|
|
if (middle_of_seek_tell) {
|
|
|
|
middle_of_seek_tell = 0;
|
|
|
|
return inpt_ptr & 0xff;
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "tell %d\n", inpt_ptr);
|
|
|
|
#endif
|
|
|
|
middle_of_seek_tell = 1;
|
|
|
|
return inpt_ptr >> 8;
|
|
|
|
}
|
2019-05-17 02:07:57 +10:00
|
|
|
} else if (addr == FS_DATA_PORT) {
|
|
|
|
if (fsdev_ptr < fsdev_size) {
|
|
|
|
return fsdev[fsdev_ptr++];
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (addr == FS_SEEK_PORT) {
|
2019-06-05 01:53:02 +10:00
|
|
|
if (fsdev_seek_tell_cnt != 0) {
|
|
|
|
return fsdev_seek_tell_cnt;
|
|
|
|
} else if (fsdev_ptr >= fsdev_size) {
|
|
|
|
return 1;
|
2019-05-29 05:56:39 +10:00
|
|
|
} else {
|
2019-06-05 01:53:02 +10:00
|
|
|
return 0;
|
2019-05-17 02:07:57 +10:00
|
|
|
}
|
2019-05-10 04:09:40 +10:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Out of bounds I/O read: %d\n", addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void io_write(int unused, uint16_t addr, uint8_t val)
|
|
|
|
{
|
|
|
|
addr &= 0xff;
|
|
|
|
if (addr == STDIO_PORT) {
|
2019-05-12 22:49:59 +10:00
|
|
|
// When mem-dumping, we don't output regular stuff.
|
|
|
|
#ifndef MEMDUMP
|
2019-05-10 04:09:40 +10:00
|
|
|
putchar(val);
|
2019-05-12 22:49:59 +10:00
|
|
|
#endif
|
2019-05-17 02:07:57 +10:00
|
|
|
} else if (addr == STDIN_SEEK_PORT) {
|
2019-05-16 10:07:21 +10:00
|
|
|
if (middle_of_seek_tell) {
|
2019-05-16 03:41:56 +10:00
|
|
|
inpt_ptr |= val;
|
2019-05-16 10:07:21 +10:00
|
|
|
middle_of_seek_tell = 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "seek %d\n", inpt_ptr);
|
|
|
|
#endif
|
2019-05-16 03:41:56 +10:00
|
|
|
} else {
|
|
|
|
inpt_ptr = (val << 8) & 0xff00;
|
2019-05-16 10:07:21 +10:00
|
|
|
middle_of_seek_tell = 1;
|
2019-05-16 03:41:56 +10:00
|
|
|
}
|
2019-05-17 02:07:57 +10:00
|
|
|
} else if (addr == FS_DATA_PORT) {
|
|
|
|
if (fsdev_ptr < fsdev_size) {
|
|
|
|
fsdev[fsdev_ptr++] = val;
|
|
|
|
}
|
|
|
|
} else if (addr == FS_SEEK_PORT) {
|
2019-05-29 05:56:39 +10:00
|
|
|
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 {
|
2019-05-17 02:07:57 +10:00
|
|
|
fsdev_ptr |= val;
|
2019-05-29 05:56:39 +10:00
|
|
|
fsdev_seek_tell_cnt = 0;
|
2019-05-17 02:07:57 +10:00
|
|
|
#ifdef DEBUG
|
2019-05-17 11:15:00 +10:00
|
|
|
fprintf(stderr, "FS seek %d\n", fsdev_ptr);
|
2019-05-17 02:07:57 +10:00
|
|
|
#endif
|
|
|
|
}
|
2019-05-18 03:22:45 +10:00
|
|
|
} else if (addr == STDERR_PORT) {
|
2019-06-20 01:42:39 +10:00
|
|
|
#ifdef VERBOSE
|
2019-05-18 03:22:45 +10:00
|
|
|
fputc(val, stderr);
|
2019-06-20 01:42:39 +10:00
|
|
|
#endif
|
2019-05-10 04:09:40 +10:00
|
|
|
} else {
|
2019-06-04 01:16:06 +10:00
|
|
|
fprintf(stderr, "Out of bounds I/O write: %d / %d (0x%x)\n", addr, val, val);
|
2019-05-10 04:09:40 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t mem_read(int unused, uint16_t addr)
|
|
|
|
{
|
|
|
|
return mem[addr];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mem_write(int unused, uint16_t addr, uint8_t val)
|
|
|
|
{
|
|
|
|
mem[addr] = val;
|
|
|
|
}
|
|
|
|
|
2019-06-03 05:50:59 +10:00
|
|
|
int main(int argc, char *argv[])
|
2019-05-10 04:09:40 +10:00
|
|
|
{
|
2019-06-03 05:50:59 +10:00
|
|
|
if (argc > 2) {
|
|
|
|
fprintf(stderr, "Too many args\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-05-10 04:09:40 +10:00
|
|
|
// initialize memory
|
|
|
|
for (int i=0; i<sizeof(KERNEL); i++) {
|
|
|
|
mem[i] = KERNEL[i];
|
|
|
|
}
|
2019-05-20 02:57:59 +10:00
|
|
|
for (int i=0; i<sizeof(USERSPACE); i++) {
|
|
|
|
mem[i+USER_CODE] = USERSPACE[i];
|
|
|
|
}
|
2019-06-03 05:50:59 +10:00
|
|
|
fsdev_size = 0;
|
|
|
|
if (argc == 2) {
|
|
|
|
FILE *fp = fopen(argv[1], "r");
|
|
|
|
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-05-10 04:09:40 +10:00
|
|
|
Z80RESET(&cpu);
|
|
|
|
cpu.ioRead = io_read;
|
|
|
|
cpu.ioWrite = io_write;
|
|
|
|
cpu.memRead = mem_read;
|
|
|
|
cpu.memWrite = mem_write;
|
|
|
|
|
|
|
|
while (!cpu.halted) {
|
|
|
|
Z80Execute(&cpu);
|
|
|
|
}
|
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-05-28 01:04:31 +10:00
|
|
|
int res = cpu.R1.br.A;
|
|
|
|
if (res != 0) {
|
2019-05-28 10:52:40 +10:00
|
|
|
int lineno = cpu.R1.wr.HL;
|
2019-05-28 23:57:29 +10:00
|
|
|
int inclineno = cpu.R1.wr.DE;
|
|
|
|
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
|
|
|
}
|
|
|
|
|