From 933c76dc06d36169776ab4bb8397f7885c067eba Mon Sep 17 00:00:00 2001 From: zongor Date: Tue, 29 Jul 2025 23:50:57 -0400 Subject: [PATCH] wip move to byte model --- src/common.h | 13 +++ src/opcodes.h | 247 ++++++++++++++++++++++++++++++++++++++------------ src/vm.c | 7 +- 3 files changed, 201 insertions(+), 66 deletions(-) diff --git a/src/common.h b/src/common.h index eb5de65..394c946 100644 --- a/src/common.h +++ b/src/common.h @@ -6,4 +6,17 @@ #include #include +typedef unsigned char u8; +typedef signed char i8; +typedef unsigned short u16; +typedef signed short i16; +typedef unsigned long u32; +typedef signed long i32; + +/* For 64-bit, use compiler extensions or split into hi/lo u32 on pure C89 platforms */ +#if defined(__GNUC__) || defined(_MSC_VER) +typedef unsigned long long u64; +typedef signed long long i64; +#endif + #endif diff --git a/src/opcodes.h b/src/opcodes.h index 223699c..4d841f7 100644 --- a/src/opcodes.h +++ b/src/opcodes.h @@ -3,13 +3,6 @@ #include "common.h" -typedef union value_u { - int32_t i; /* Integers */ - float f; /* Float */ - uint32_t u; /* Unsigned integers, also used for pointer address */ - char c[4]; /* 4 Byte char array for string packing */ -} Value; - typedef struct slice_s { uint32_t start; uint32_t end; @@ -17,16 +10,16 @@ typedef struct slice_s { #define MAX_REGS 32 typedef struct frame_s { - Value registers[MAX_REGS]; /* R0-R31 */ - uint32_t rp; /* register pointer (last unused) */ - Slice allocated; /* start and end of global allocated block */ + uint32_t registers[MAX_REGS]; /* R0-R31 */ + uint32_t rp; /* register pointer (last unused) */ + Slice allocated; /* start and end of global allocated block */ } Frame; typedef struct screen_t { uint8_t width; uint8_t height; Slice allocated; - Value *buffer; + uint8_t *buffer; } Screen; typedef struct mouse_t { @@ -37,7 +30,7 @@ typedef struct mouse_t { uint8_t btn3; } Mouse; -typedef struct keyboard_t { +typedef struct keyboard_t { uint32_t length; const uint8_t *keys; } Keyboard; @@ -51,7 +44,7 @@ typedef union device_u { #define MEMORY_SIZE 65536 #define CODE_SIZE 8192 -#define FRAMES_SIZE 128 +#define FRAMES_SIZE 128 #define STACK_SIZE 256 #define DEVICES_SIZE 8 typedef struct vm_s { @@ -67,68 +60,202 @@ typedef struct vm_s { uint32_t frames_size; Frame frames[FRAMES_SIZE]; /* function call frames */ uint32_t stack_size; - Value stack[STACK_SIZE]; /* main stack */ + uint8_t stack[STACK_SIZE]; /* main stack */ uint32_t return_stack_size; - Value return_stack[STACK_SIZE]; /* return stack (for recursion) */ + uint8_t return_stack[STACK_SIZE]; /* return stack (for recursion) */ uint32_t code_size; - Value code[CODE_SIZE]; /* code block */ + uint8_t code[CODE_SIZE]; /* code block */ uint32_t memory_size; - Value memory[MEMORY_SIZE]; /* memory block */ + uint8_t memory[MEMORY_SIZE]; /* memory block */ } VM; +u8 read_u8(const u8 *mem, u32 index) { return mem[index]; } + +i8 read_i8(const u8 *mem, u32 index) { return (i8)mem[index]; } + +u16 read_u16(const u8 *mem, u32 index) { + return (u16)mem[index] | ((u16)mem[index + 1] << 8); +} + +i16 read_i16(const u8 *mem, u32 index) { + return (i16)(mem[index] | (mem[index + 1] << 8)); +} + +u32 read_u32(const u8 *mem, u32 index) { + return (u32)mem[index] | ((u32)mem[index + 1] << 8) | + ((u32)mem[index + 2] << 16) | ((u32)mem[index + 3] << 24); +} + +i32 read_i32(const u8 *mem, u32 index) { + return (i32)(mem[index] | (mem[index + 1] << 8) | (mem[index + 2] << 16) | + (mem[index + 3] << 24)); +} + +u64 read_u64(const u8 *mem, u32 index) { + return (u64)mem[index] | ((u64)mem[index + 1] << 8) | + ((u64)mem[index + 2] << 16) | ((u64)mem[index + 3] << 24) | + ((u64)mem[index + 4] << 32) | ((u64)mem[index + 5] << 40) | + ((u64)mem[index + 6] << 48) | ((u64)mem[index + 7] << 56); +} + +i64 read_i64(const u8 *mem, u32 index) { return (i64)read_u64(mem, index); } + +void write_u8(u8 *mem, u32 index, u8 value) { mem[index] = value; } + +void write_i8(u8 *mem, u32 index, i8 value) { mem[index] = (u8)value; } + +void write_u16(u8 *mem, u32 index, u16 value) { + mem[index] = value & 0xff; + mem[index + 1] = (value >> 8) & 0xff; +} + +void write_i16(u8 *mem, u32 index, i16 value) { + write_u16(mem, index, (u16)value); +} + +void write_u32(u8 *mem, u32 index, u32 value) { + mem[index] = value & 0xff; + mem[index + 1] = (value >> 8) & 0xff; + mem[index + 2] = (value >> 16) & 0xff; + mem[index + 3] = (value >> 24) & 0xff; +} + +void write_i32(u8 *mem, u32 index, i32 value) { + write_u32(mem, index, (u32)value); +} + +void write_u64(u8 *mem, u32 index, u64 value) { + mem[index] = value & 0xff; + mem[index + 1] = (value >> 8) & 0xff; + mem[index + 2] = (value >> 16) & 0xff; + mem[index + 3] = (value >> 24) & 0xff; + mem[index + 4] = (value >> 32) & 0xff; + mem[index + 5] = (value >> 40) & 0xff; + mem[index + 6] = (value >> 48) & 0xff; + mem[index + 7] = (value >> 56) & 0xff; +} + +void write_i64(u8 *mem, u32 index, i64 value) { + write_u64(mem, index, (u64)value); +} + typedef enum { - OP_HALT, /* halt : terminate execution */ - OP_LOADI, /* lodi : dest = next memory location as int */ - OP_LOADU, /* lodu : dest = next memory location as uint */ - OP_LOADF, /* lodf : dest = next memory location as float */ - OP_STOREI, /* stri : next memory location = src1 as int */ - OP_STOREU, /* stru : next memory location = src1 as uint */ - OP_STOREF, /* strf : next memory location = src1 as float */ - OP_PUSHI, /* pshi : push int from register onto the stack */ - OP_PUSHU, /* pshu : push uint from register onto the stack */ - OP_PUSHF, /* pshf : push float from register onto the stack */ - OP_PUSHS, /* pshs : push str ref from register onto the stack and copy str */ - OP_POPI, /* popi : pop int from stack onto the register */ - OP_POPU, /* popu : pop uint from stack onto the register */ - OP_POPF, /* popf : pop float from stack onto the register */ - OP_POPS, /* pops : pop str ref from stack and move/copy to register */ - OP_ADD_INT, /* addi : dest = src1 + src2 */ - OP_SUB_INT, /* subi : dest = src1 - src2 */ - OP_MUL_INT, /* muli : dest = src1 * src2 */ - OP_DIV_INT, /* divi : dest = src1 / src2 */ - OP_JEQ_INT, /* jeqi : jump to address dest if src1 as int == src2 as int */ - OP_JGT_INT, /* jgti : jump to address dest if src1 as int > src2 as int*/ - OP_JLT_INT, /* jlti : jump to address dest if src1 as int < src2 as int */ - OP_JLE_INT, /* jlei : jump to address dest if src1 as int <= src2 as int */ - OP_JGE_INT, /* jgei : jump to address dest if src1 as int >= src2 as int*/ - OP_INT_TO_REAL, /* itor : dest = src1 as f32 */ - OP_ADD_UINT, /* addu : dest = src1 + src2 */ - OP_SUB_UINT, /* subu : dest = src1 - src2 */ - OP_MUL_UINT, /* mulu : dest = src1 * src2 */ - OP_DIV_UINT, /* divu : dest = src1 / src2 */ - OP_JEQ_UINT, /* jequ : jump to address dest if src1 as int == src2 as uint */ - OP_JGT_UINT, /* jgtu : jump to address dest if src1 as int > src2 as uint*/ - OP_JLT_UINT, /* jltu : jump to address dest if src1 as int < src2 as uint */ - OP_JLE_UINT, /* jleu : jump to address dest if src1 as int <= src2 as uint */ - OP_JGE_UINT, /* jgeu : jump to address dest if src1 as int >= src2 as uint*/ - OP_UINT_TO_REAL, /* utor : dest = src1 as f32 */ - OP_ADD_REAL, /* addr : dest = src1 + src2 */ - OP_SUB_REAL, /* subr : dest = src1 - src2 */ - OP_MUL_REAL, /* mulr : dest = src1 * src2 */ - OP_DIV_REAL, /* divr : dest = src1 / src2 */ + OP_HALT, /* halt : terminate execution */ + OP_LOADI8, /* lodi : dest */ + OP_LOADU8, /* lodu : dest = next memory location as u32 */ + OP_LOADI16, /* lodi : dest */ + OP_LOADU16, /* lodu : dest = next memory location as u32 */ + OP_LOADI32, /* lodi : dest */ + OP_LOADU32, /* lodu : dest = next memory location as u32 */ + OP_LOADF32, /* lodf : dest = next memory location as f32 */ + OP_STOREI8, /* stri : next memory location = src1 as i32 */ + OP_STOREU8, /* stru : next memory location = src1 as u32 */ + OP_STOREI16, /* stri : next memory location = src1 as i32 */ + OP_STOREU16, /* stru : next memory location = src1 as u32 */ + OP_STOREI32, /* stri : next memory location = src1 as i32 */ + OP_STOREU32, /* stru : next memory location = src1 as u32 */ + OP_STOREF32, /* strf : next memory location = src1 as f32 */ + OP_PUSHI8, /* pshi : push int from register onto the stack */ + OP_PUSHU8, /* pshu : push uint from register onto the stack */ + OP_PUSHI16, /* pshi : push int from register onto the stack */ + OP_PUSHU16, /* pshu : push uint from register onto the stack */ + OP_PUSHI32, /* pshi : push int from register onto the stack */ + OP_PUSHU32, /* pshu : push uint from register onto the stack */ + OP_PUSHF32, /* pshf : push float from register onto the stack */ + OP_PUSHS, /* pshs : push str ref from register onto the stack and copy str */ + OP_POPI8, /* popi : pop int from stack onto the register */ + OP_POPU8, /* popu : pop uint from stack onto the register */ + OP_POPI16, /* popi : pop int from stack onto the register */ + OP_POPU16, /* popu : pop uint from stack onto the register */ + OP_POPI32, /* popi : pop int from stack onto the register */ + OP_POPU32, /* popu : pop uint from stack onto the register */ + OP_POPF32, /* popf : pop float from stack onto the register */ + OP_POPS, /* pops : pop str ref from stack and move/copy to register */ + OP_ADD_I8, /* addi : dest = src1 + src2 */ + OP_SUB_I8, /* subi : dest = src1 - src2 */ + OP_MUL_I8, /* muli : dest = src1 * src2 */ + OP_DIV_I8, /* divi : dest = src1 / src2 */ + OP_JEQ_I8, /* jeqi : jump to address dest if src1 as int == src2 as int */ + OP_JGT_I8, /* jgti : jump to address dest if src1 as int > src2 as int*/ + OP_JLT_I8, /* jlti : jump to address dest if src1 as int < src2 as int */ + OP_JLE_I8, /* jlei : jump to address dest if src1 as int <= src2 as int */ + OP_JGE_I8, /* jgei : jump to address dest if src1 as int >= src2 as int*/ + OP_ADD_I16, /* addi : dest = src1 + src2 */ + OP_SUB_I16, /* subi : dest = src1 - src2 */ + OP_MUL_I16, /* muli : dest = src1 * src2 */ + OP_DIV_I16, /* divi : dest = src1 / src2 */ + OP_JEQ_I16, /* jeqi : jump to address dest if src1 as int == src2 as int */ + OP_JGT_I16, /* jgti : jump to address dest if src1 as int > src2 as int*/ + OP_JLT_I16, /* jlti : jump to address dest if src1 as int < src2 as int */ + OP_JLE_I16, /* jlei : jump to address dest if src1 as int <= src2 as int */ + OP_JGE_I16, /* jgei : jump to address dest if src1 as int >= src2 as int*/ + OP_ADD_I32, /* addi : dest = src1 + src2 */ + OP_SUB_I32, /* subi : dest = src1 - src2 */ + OP_MUL_I32, /* muli : dest = src1 * src2 */ + OP_DIV_I32, /* divi : dest = src1 / src2 */ + OP_JEQ_I32, /* jeqi : jump to address dest if src1 as int == src2 as int */ + OP_JGT_I32, /* jgti : jump to address dest if src1 as int > src2 as int*/ + OP_JLT_I32, /* jlti : jump to address dest if src1 as int < src2 as int */ + OP_JLE_I32, /* jlei : jump to address dest if src1 as int <= src2 as int */ + OP_JGE_I32, /* jgei : jump to address dest if src1 as int >= src2 as int*/ + OP_I8_TO_REAL, /* itor : dest = src1 as f32 */ + OP_I16_TO_REAL, /* itor : dest = src1 as f32 */ + OP_I32_TO_REAL, /* itor : dest = src1 as f32 */ + OP_ADD_U8, /* addu : dest = src1 + src2 */ + OP_SUB_U8, /* subu : dest = src1 - src2 */ + OP_MUL_U8, /* mulu : dest = src1 * src2 */ + OP_DIV_U8, /* divu : dest = src1 / src2 */ + OP_JEQ_U8, /* jequ : jump to address dest if src1 as int == src2 as uint */ + OP_JGT_U8, /* jgtu : jump to address dest if src1 as int > src2 as uint*/ + OP_JLT_U8, /* jltu : jump to address dest if src1 as int < src2 as uint */ + OP_JLE_U8, /* jleu : jump to address dest if src1 as int <= src2 as uint */ + OP_JGE_U8, /* jgeu : jump to address dest if src1 as int >= src2 as uint*/ + OP_ADD_U16, /* addu : dest = src1 + src2 */ + OP_SUB_U16, /* subu : dest = src1 - src2 */ + OP_MUL_U16, /* mulu : dest = src1 * src2 */ + OP_DIV_U16, /* divu : dest = src1 / src2 */ + OP_JEQ_U16, /* jequ : jump to address dest if src1 as int == src2 as uint */ + OP_JGT_U16, /* jgtu : jump to address dest if src1 as int > src2 as uint*/ + OP_JLT_U16, /* jltu : jump to address dest if src1 as int < src2 as uint */ + OP_JLE_U16, /* jleu : jump to address dest if src1 as int <= src2 as uint */ + OP_JGE_U16, /* jgeu : jump to address dest if src1 as int >= src2 as uint*/ + OP_ADD_U32, /* addu : dest = src1 + src2 */ + OP_SUB_U32, /* subu : dest = src1 - src2 */ + OP_MUL_U32, /* mulu : dest = src1 * src2 */ + OP_DIV_U32, /* divu : dest = src1 / src2 */ + OP_JEQ_U32, /* jequ : jump to address dest if src1 as int == src2 as uint */ + OP_JGT_U32, /* jgtu : jump to address dest if src1 as int > src2 as uint*/ + OP_JLT_U32, /* jltu : jump to address dest if src1 as int < src2 as uint */ + OP_JLE_U32, /* jleu : jump to address dest if src1 as int <= src2 as uint */ + OP_JGE_U32, /* jgeu : jump to address dest if src1 as int >= src2 as uint*/ + OP_U8_TO_REAL, /* utor : dest = src1 as f32 */ + OP_U16_TO_REAL, /* utor : dest = src1 as f32 */ + OP_U32_TO_REAL, /* utor : dest = src1 as f32 */ + OP_ADD_REAL, /* addr : dest = src1 + src2 */ + OP_SUB_REAL, /* subr : dest = src1 - src2 */ + OP_MUL_REAL, /* mulr : dest = src1 * src2 */ + OP_DIV_REAL, /* divr : dest = src1 / src2 */ OP_JEQ_REAL, /* jeqr : jump to address dest if src1 as real == src2 as real */ OP_JGE_REAL, /* jgtr : jump to address dest if src1 as real >= src2 as real */ OP_JGT_REAL, /* jltr : jump to address dest if src1 as real > src2 as real */ OP_JLT_REAL, /* jler : jump to address dest if src1 as real < src2 as real */ OP_JLE_REAL, /* jger : jump to address dest if src1 as real <= src2 as real */ - OP_REAL_TO_INT, /* rtoi : dest = src1 as int */ - OP_REAL_TO_UINT, /* rtou : dest = src1 as uint */ + OP_REAL_TO_I8, /* rtoi : dest = src1 as int */ + OP_REAL_TO_I16, /* rtoi : dest = src1 as int */ + OP_REAL_TO_I32, /* rtoi : dest = src1 as int */ + OP_REAL_TO_U8, /* rtou : dest = src1 as uint */ + OP_REAL_TO_U16, /* rtou : dest = src1 as uint */ + OP_REAL_TO_U32, /* rtou : dest = src1 as uint */ OP_MOV, /* move : dest = src1 */ OP_JMP, /* jump : jump to address src1 unconditionally */ OP_CALL, /* call : creates a new frame */ OP_RETURN, /* retn : returns from a frame to the parent frame */ - OP_INT_TO_STRING, /* itos : dest = src1 as str */ - OP_UINT_TO_STRING, /* utos : dest = src1 as str */ + OP_I8_TO_STRING, /* itos : dest = src1 as str */ + OP_I16_TO_STRING, /* itos : dest = src1 as str */ + OP_I32_TO_STRING, /* itos : dest = src1 as str */ + OP_U8_TO_STRING, /* utos : dest = src1 as str */ + OP_U16_TO_STRING, /* utos : dest = src1 as str */ + OP_U32_TO_STRING, /* utos : dest = src1 as str */ OP_REAL_TO_STRING, /* rtos : dest = src1 as str */ OP_READ_STRING, /* gets : dest = gets as str */ OP_PRINT_STRING, /* puts : write src1 to stdout */ diff --git a/src/vm.c b/src/vm.c index aea8fa0..4c0e428 100644 --- a/src/vm.c +++ b/src/vm.c @@ -47,12 +47,7 @@ uint32_t str_alloc(VM *vm, const char *str, uint32_t length) { */ bool step_vm(VM *vm) { /* Get current instruction & Advance to next instruction */ - uint32_t instruction = vm->code[vm->pc++].u; - - uint8_t opcode = (instruction >> 24) & 0xFF; - uint8_t dest = (instruction >> 16) & 0xFF; - uint8_t src1 = (instruction >> 8) & 0xFF; - uint8_t src2 = instruction & 0xFF; + uint32_t opcode = vm->code[vm->pc++]; switch (opcode) { case OP_HALT: