2020-03-12 00:14:44 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2020-04-24 17:53:58 -04:00
|
|
|
#include "emul.h"
|
2020-03-24 13:46:05 -04:00
|
|
|
#ifdef STAGE2
|
2020-04-29 19:38:43 -04:00
|
|
|
#include "forth-bin.h"
|
2020-04-19 21:31:41 -04:00
|
|
|
#include "blkfs-bin.h"
|
2020-03-24 13:46:05 -04:00
|
|
|
#else
|
2020-04-26 10:15:35 -04:00
|
|
|
#include "stage0-bin.h"
|
2020-03-24 13:46:05 -04:00
|
|
|
#endif
|
2020-03-12 00:14:44 -04:00
|
|
|
|
2020-03-22 21:46:43 -04:00
|
|
|
/* Staging binaries
|
2020-03-12 00:14:44 -04:00
|
|
|
|
2020-03-22 21:46:43 -04:00
|
|
|
The role of a stage executable is to compile definitions in a dictionary and
|
|
|
|
then spit the difference between the starting binary and the new binary.
|
|
|
|
|
|
|
|
That binary can then be grafted to an exiting Forth binary to augment its
|
|
|
|
dictionary.
|
2020-03-12 00:14:44 -04:00
|
|
|
|
|
|
|
We could, if we wanted, run only with the bootstrap binary and compile core
|
|
|
|
defs at runtime, but that would mean that those defs live in RAM. In may system,
|
|
|
|
RAM is much more constrained than ROM, so it's worth it to give ourselves the
|
|
|
|
trouble of compiling defs to binary.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-03-13 16:01:09 -04:00
|
|
|
// When DEBUG is set, stage1 is a core-less forth that works interactively.
|
|
|
|
// Useful for... debugging!
|
|
|
|
// By the way: there's a double-echo in stagedbg. It's normal. Don't panic.
|
|
|
|
|
|
|
|
//#define DEBUG
|
2020-04-26 10:15:35 -04:00
|
|
|
#define RAMSTART 0
|
2020-03-12 00:14:44 -04:00
|
|
|
#define STDIO_PORT 0x00
|
2020-03-22 21:46:43 -04:00
|
|
|
// To know which part of RAM to dump, we listen to port 2, which at the end of
|
|
|
|
// its compilation process, spits its HERE addr to port 2 (MSB first)
|
|
|
|
#define HERE_PORT 0x02
|
2020-04-19 21:31:41 -04:00
|
|
|
// Port for block reads. Write 2 bytes, MSB first, on that port and then
|
|
|
|
// read 1024 bytes from the DATA port.
|
|
|
|
#define BLK_PORT 0x03
|
|
|
|
#define BLKDATA_PORT 0x04
|
2020-03-12 00:14:44 -04:00
|
|
|
|
|
|
|
static int running;
|
2020-03-24 13:46:05 -04:00
|
|
|
// We support double-pokes, that is, a first poke to tell where to start the
|
|
|
|
// dump and a second one to tell where to stop. If there is only one poke, it's
|
|
|
|
// then ending HERE and we start at sizeof(KERNEL).
|
|
|
|
static uint16_t start_here = 0;
|
|
|
|
static uint16_t end_here = 0;
|
2020-04-19 21:31:41 -04:00
|
|
|
static uint16_t blkid = 0;
|
|
|
|
static unsigned int blkpos = 0;
|
2020-03-12 00:14:44 -04:00
|
|
|
|
|
|
|
static uint8_t iord_stdio()
|
|
|
|
{
|
2020-03-17 21:44:32 -04:00
|
|
|
int c = getc(stdin);
|
2020-03-12 00:14:44 -04:00
|
|
|
if (c == EOF) {
|
|
|
|
running = 0;
|
|
|
|
}
|
|
|
|
return (uint8_t)c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iowr_stdio(uint8_t val)
|
|
|
|
{
|
|
|
|
// we don't output stdout in stage0
|
2020-03-13 16:01:09 -04:00
|
|
|
#ifdef DEBUG
|
|
|
|
// ... unless we're in DEBUG mode!
|
|
|
|
putchar(val);
|
|
|
|
#endif
|
2020-03-12 00:14:44 -04:00
|
|
|
}
|
|
|
|
|
2020-03-22 21:46:43 -04:00
|
|
|
static void iowr_here(uint8_t val)
|
|
|
|
{
|
2020-03-24 13:46:05 -04:00
|
|
|
start_here <<=8;
|
|
|
|
start_here |= (end_here >> 8);
|
|
|
|
end_here <<= 8;
|
|
|
|
end_here |= val;
|
2020-03-22 21:46:43 -04:00
|
|
|
}
|
|
|
|
|
2020-04-19 21:31:41 -04:00
|
|
|
#ifdef STAGE2
|
|
|
|
static void iowr_blk(uint8_t val)
|
|
|
|
{
|
|
|
|
blkid <<= 8;
|
|
|
|
blkid |= val;
|
|
|
|
blkpos = blkid * 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t iord_blkdata()
|
|
|
|
{
|
|
|
|
return BLKFS[blkpos++];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-12 00:14:44 -04:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
Machine *m = emul_init();
|
|
|
|
m->ramstart = RAMSTART;
|
|
|
|
m->iord[STDIO_PORT] = iord_stdio;
|
|
|
|
m->iowr[STDIO_PORT] = iowr_stdio;
|
2020-03-22 21:46:43 -04:00
|
|
|
m->iowr[HERE_PORT] = iowr_here;
|
2020-04-19 21:31:41 -04:00
|
|
|
#ifdef STAGE2
|
|
|
|
m->iowr[BLK_PORT] = iowr_blk;
|
|
|
|
m->iord[BLKDATA_PORT] = iord_blkdata;
|
|
|
|
#endif
|
2020-03-12 00:14:44 -04:00
|
|
|
// initialize memory
|
|
|
|
for (int i=0; i<sizeof(KERNEL); i++) {
|
|
|
|
m->mem[i] = KERNEL[i];
|
|
|
|
}
|
2020-04-01 08:54:01 -04:00
|
|
|
|
2020-03-12 00:14:44 -04:00
|
|
|
// Run!
|
|
|
|
running = 1;
|
|
|
|
|
|
|
|
while (running && emul_step());
|
|
|
|
|
2020-03-13 16:01:09 -04:00
|
|
|
#ifndef DEBUG
|
2020-03-12 00:14:44 -04:00
|
|
|
// We're done, now let's spit dict data
|
2020-03-24 13:46:05 -04:00
|
|
|
for (int i=start_here; i<end_here; i++) {
|
2020-03-12 00:14:44 -04:00
|
|
|
putchar(m->mem[i]);
|
|
|
|
}
|
2020-03-13 16:01:09 -04:00
|
|
|
#endif
|
2020-03-12 00:14:44 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|