2019-04-17 03:36:57 +10:00
|
|
|
#include <stdint.h>
|
2019-04-17 03:59:19 +10:00
|
|
|
#include <stdio.h>
|
2019-04-17 03:36:57 +10:00
|
|
|
#include "libz80/z80.h"
|
2019-04-17 04:26:45 +10:00
|
|
|
#include "kernel.h"
|
2019-04-17 03:36:57 +10:00
|
|
|
#include "zasm.h"
|
|
|
|
|
|
|
|
/* zasm is a "pure memory" application. It starts up being told memory location
|
|
|
|
* to read and memory location to write.
|
|
|
|
*
|
|
|
|
* This program works be writing stdin in a specific location in memory, run
|
|
|
|
* zasm in a special wrapper, wait until we receive the stop signal, then
|
|
|
|
* spit the contents of the dest memory to stdout.
|
|
|
|
*/
|
2019-04-17 03:59:19 +10:00
|
|
|
|
2019-04-17 04:26:45 +10:00
|
|
|
// in sync with glue.asm
|
|
|
|
#define READFROM 0xa000
|
|
|
|
#define WRITETO 0xd000
|
|
|
|
#define ZASM_CODE_OFFSET 0x8000
|
2019-04-17 03:59:19 +10:00
|
|
|
|
2019-04-17 03:36:57 +10:00
|
|
|
static Z80Context cpu;
|
|
|
|
static uint8_t mem[0xffff];
|
|
|
|
static int running;
|
2019-04-17 03:59:19 +10:00
|
|
|
// Number of bytes written to WRITETO
|
|
|
|
// We receive that result from an OUT (C), A call. C contains LSB, A is MSB.
|
|
|
|
static uint16_t written;
|
2019-04-17 03:36:57 +10:00
|
|
|
|
|
|
|
|
|
|
|
static uint8_t io_read(int unused, uint16_t addr)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void io_write(int unused, uint16_t addr, uint8_t val)
|
|
|
|
{
|
2019-04-18 00:34:30 +10:00
|
|
|
written = ((addr & 0xff) << 8) + (val & 0xff);
|
2019-04-17 03:36:57 +10:00
|
|
|
running = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-04-17 04:26:45 +10:00
|
|
|
for (int i=0; i<sizeof(KERNEL); i++) {
|
|
|
|
mem[i] = KERNEL[i];
|
2019-04-17 03:36:57 +10:00
|
|
|
}
|
2019-04-17 04:26:45 +10:00
|
|
|
for (int i=0; i<sizeof(ZASM); i++) {
|
|
|
|
mem[i+ZASM_CODE_OFFSET] = ZASM[i];
|
2019-04-17 03:36:57 +10:00
|
|
|
}
|
2019-04-17 03:59:19 +10:00
|
|
|
int ptr = READFROM;
|
|
|
|
int c = getchar();
|
|
|
|
while (c != EOF) {
|
|
|
|
mem[ptr] = c;
|
|
|
|
ptr++;
|
|
|
|
c = getchar();
|
|
|
|
}
|
2019-04-17 03:36:57 +10:00
|
|
|
// Run!
|
|
|
|
running = 1;
|
|
|
|
Z80RESET(&cpu);
|
|
|
|
cpu.ioRead = io_read;
|
|
|
|
cpu.ioWrite = io_write;
|
|
|
|
cpu.memRead = mem_read;
|
|
|
|
cpu.memWrite = mem_write;
|
|
|
|
|
|
|
|
while (running) {
|
|
|
|
Z80Execute(&cpu);
|
|
|
|
}
|
2019-04-17 03:59:19 +10:00
|
|
|
|
|
|
|
for (int i=0; i<written; i++) {
|
|
|
|
putchar(mem[WRITETO+i]);
|
|
|
|
}
|
2019-04-17 03:36:57 +10:00
|
|
|
return 0;
|
|
|
|
}
|