2019-05-09 14:09:40 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "libz80/z80.h"
|
|
|
|
#include "zasm-kernel.h"
|
|
|
|
#include "zasm-user.h"
|
|
|
|
|
|
|
|
/* zasm is a "pure memory" application. It starts up being told memory location
|
|
|
|
* to read and memory location to write.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Memory layout:
|
|
|
|
*
|
|
|
|
* 0x0000 - 0x3fff: ROM code from zasm_glue.asm
|
2019-05-10 18:20:43 -04:00
|
|
|
* 0x4000 - 0x47ff: RAM for kernel and stack
|
|
|
|
* 0x4800 - 0x57ff: Userspace code
|
|
|
|
* 0x5800 - 0xffff: Userspace RAM
|
2019-05-09 14:09:40 -04:00
|
|
|
*
|
|
|
|
* I/O Ports:
|
|
|
|
*
|
|
|
|
* 0 - stdin / stdout
|
2019-05-10 19:55:01 -04:00
|
|
|
* 1 - When written to, rewind stdin buffer to the beginning.
|
2019-05-09 14:09:40 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// in sync with zasm_glue.asm
|
2019-05-10 18:20:43 -04:00
|
|
|
#define USER_CODE 0x4800
|
2019-05-09 14:09:40 -04:00
|
|
|
#define STDIO_PORT 0x00
|
2019-05-10 19:55:01 -04:00
|
|
|
#define STDIN_REWIND 0x01
|
2019-05-09 14:09:40 -04:00
|
|
|
|
2019-05-10 19:55:01 -04:00
|
|
|
// Other consts
|
|
|
|
#define STDIN_BUFSIZE 0x8000
|
2019-05-09 14:09:40 -04:00
|
|
|
static Z80Context cpu;
|
2019-05-10 19:55:01 -04: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-09 14:09:40 -04:00
|
|
|
|
|
|
|
static uint8_t io_read(int unused, uint16_t addr)
|
|
|
|
{
|
|
|
|
addr &= 0xff;
|
|
|
|
if (addr == STDIO_PORT) {
|
2019-05-10 19:55:01 -04:00
|
|
|
if (inpt_ptr < inpt_size) {
|
|
|
|
return inpt[inpt_ptr++];
|
|
|
|
} else {
|
2019-05-09 14:09:40 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} 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) {
|
|
|
|
putchar(val);
|
2019-05-10 19:55:01 -04:00
|
|
|
} else if (addr == STDIN_REWIND) {
|
|
|
|
inpt_ptr = 0;
|
2019-05-09 14:09:40 -04:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Out of bounds I/O write: %d / %d\n", addr, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// initialize memory
|
|
|
|
for (int i=0; i<sizeof(KERNEL); i++) {
|
|
|
|
mem[i] = KERNEL[i];
|
|
|
|
}
|
|
|
|
for (int i=0; i<sizeof(USERSPACE); i++) {
|
|
|
|
mem[i+USER_CODE] = USERSPACE[i];
|
|
|
|
}
|
2019-05-10 19:55:01 -04: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-09 14:09:40 -04:00
|
|
|
Z80RESET(&cpu);
|
|
|
|
cpu.ioRead = io_read;
|
|
|
|
cpu.ioWrite = io_write;
|
|
|
|
cpu.memRead = mem_read;
|
|
|
|
cpu.memWrite = mem_write;
|
|
|
|
|
|
|
|
while (!cpu.halted) {
|
|
|
|
Z80Execute(&cpu);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|