127 lines
3.9 KiB
C
127 lines
3.9 KiB
C
/*
|
|
* Equi platform reference implementation
|
|
*
|
|
* Created in 2022 by Luxferre, released into public domain
|
|
*
|
|
* See equi.md file for the specification and manual
|
|
*
|
|
* @license Unlicense <https://unlicense.org>
|
|
* @author Luxferre
|
|
*/
|
|
|
|
/* Standard includes */
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
/* Definitions section */
|
|
|
|
#define ushort unsigned short /* basic 16-bit integer */
|
|
#define uchar unsigned char /* basic 8-bit integer */
|
|
#define WS sizeof(ushort) /* Equi word size in bytes */
|
|
#define CLT_ENTRY_LEN 6 /* Amount of significant compiled word characters */
|
|
#define CLT_ENTRY_SIZE (CLT_ENTRY_LEN + WS) /* Full size in bytes taken by one CLT entry */
|
|
|
|
/* Configuration section (constants overridable at compile-time) */
|
|
|
|
/* Main and return stack size in bytes */
|
|
#ifndef STACK_SIZE
|
|
#define STACK_SIZE 256
|
|
#endif
|
|
|
|
/* Literal stack size in bytes */
|
|
#ifndef LIT_STACK_SIZE
|
|
#define LIT_STACK_SIZE 32
|
|
#endif
|
|
|
|
/* GPD (general purpose data) area start address */
|
|
#ifndef GPD_AREA_START
|
|
#define GPD_AREA_START 0x2300
|
|
#endif
|
|
|
|
/* Command buffer start address */
|
|
#ifndef CMD_BUF_START
|
|
#define CMD_BUF_START 0x5000
|
|
#endif
|
|
|
|
/* Command buffer size in bytes */
|
|
#ifndef CMD_BUF_SIZE
|
|
#define CMD_BUF_SIZE 0x5700
|
|
#endif
|
|
|
|
/* Some necessary constants and offsets derived from the above values */
|
|
#define STACK_SIZE_WORDS (STACK_SIZE / WS) /* Main and return stack size in words */
|
|
|
|
#define MAIN_STACK_ADDR 0 /* Main stack address */
|
|
#define RETURN_STACK_ADDR STACK_SIZE /* Return stack address */
|
|
#define LIT_STACK_ADDR (RETURN_STACK_ADDR + STACK_SIZE) /* Literal stack address */
|
|
#define PC_ADDR (LIT_STACK_ADDR + LIT_STACK_SIZE) /* Program counter address */
|
|
#define CBP_ADDR (PC_ADDR + WS) /* Compilation buffer pointer location address */
|
|
#define CLTP_ADDR (CBP_ADDR + WS) /* Compilation lookup table pointer location address */
|
|
#define FLAGS_ADDR (CLTP_ADDR + WS) /* Flags register address */
|
|
#define GPD_START_LOC_ADDR (FLAGS_ADDR + 1) /* Location where GPD_AREA_START value is stored */
|
|
#define CMD_START_LOC_ADDR (GPD_START_LOC_ADDR + WS) /* Location where CMD_BUF_START value is stored */
|
|
#define CMD_SIZE_LOC_ADDR (CMD_START_LOC_ADDR + WS) /* Location where CMD_BUF_SIZE value is stored */
|
|
#define CLT_START (CMD_SIZE_LOC_ADDR + 213u) /* should be 0x300 by default */
|
|
#define CLT_SIZE (GPD_AREA_START - CLT_START) /* Compilation lookup table size in bytes */
|
|
#define CLT_ENTRIES_MAX (CLT_SIZE / CLT_ENTRY_SIZE) /* Maximum amount of entries in CLT, must be integer */
|
|
#define GPD_AREA_SIZE (CMD_BUF_START - GPD_AREA_START) /* GPD area size in bytes */
|
|
|
|
/*
|
|
* Structures that describe Equi machine RAM using the above configuration
|
|
*/
|
|
|
|
struct EquiFlags {
|
|
uchar II:1; /* instruction ignore mode */
|
|
uchar CM:1; /* compilation mode */
|
|
uchar IM:1; /* interpretation mode */
|
|
};
|
|
|
|
struct CLTEntry {
|
|
uchar name[6]; /* compiled word name */
|
|
ushort loc; /* compiled word location */
|
|
};
|
|
|
|
struct EquiRAM {
|
|
ushort main_stack[STACK_SIZE_WORDS];
|
|
ushort return_stack[STACK_SIZE_WORDS];
|
|
uchar literal_stack[LIT_STACK_SIZE];
|
|
ushort pc; /* program counter */
|
|
ushort msp; /* main stack pointer */
|
|
ushort rsp; /* return stack pointer */
|
|
uchar lsp; /* literal stack pointer */
|
|
ushort cbp; /* compilation buffer pointer */
|
|
ushort cltp; /* compilation lookup table pointer */
|
|
struct EquiFlags flags;
|
|
ushort gpd_start;
|
|
ushort cmd_start;
|
|
ushort cmd_size;
|
|
uchar reserved[206]; /* reserved space */
|
|
struct CLTEntry clt[CLT_ENTRIES_MAX]; /* compilation lookup table */
|
|
uchar gpd[GPD_AREA_SIZE];
|
|
uchar cmdbuf[CMD_BUF_SIZE];
|
|
};
|
|
|
|
|
|
/*
|
|
* Before running the main code, instantiate and initialize the machine RAM
|
|
*/
|
|
|
|
static struct EquiRAM ram = {
|
|
.gpd_start = GPD_AREA_START,
|
|
.cmd_start = CMD_BUF_START,
|
|
.cmd_size = CMD_BUF_SIZE
|
|
};
|
|
|
|
/* Also create an alternative view of the same RAM area for direct offset-based access */
|
|
static uchar* flatram = (uchar *)&ram;
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
/* */
|
|
|
|
printf("0x%X", ram.cmd_size);
|
|
|
|
return 0;
|
|
}
|