From: bylex Date: Wed, 25 Sep 2024 06:24:58 +0000 (+0200) Subject: Add initial interpreter state X-Git-Url: https://git.bylex.cz/?a=commitdiff_plain;h=4bb1fa32671bdab08289e5be66652d8ed771b922;p=maturita.git Add initial interpreter state --- diff --git a/interpreter.c b/interpreter.c new file mode 100644 index 0000000..10ab391 --- /dev/null +++ b/interpreter.c @@ -0,0 +1,53 @@ +#include +#include + +#include "opcodes.h" + +uint16_t regs[256]; +uint16_t mem[65536]; + +uint16_t stack[256]; +uint8_t stack_pointer; + +uint16_t current_instruction = 0; // entry location +bool halted = false; +uint8_t halted_reason = 0; + +uint64_t instruction_counter = 0; + + + +void load_mem() +{ + mem[0] = (uint16_t)4 << 8 | 0; + mem[1] = 1234; + + mem[2] = (uint16_t)4 << 8 | 1; + mem[3] = 1000; + + mem[6] = (uint16_t)1 << 8 | 0; + mem[7] = (uint16_t)1 << 8 | 0; + + mem[8] = (uint16_t)9 << 8 | 0; + mem[9] = 0; +} + + + +int main(int argc, char *argv[]) +{ + load_mem(); + while(!halted) + { + uint8_t arg1 = (uint8_t)mem[current_instruction]; + uint8_t instruction = (uint8_t)(mem[current_instruction] >> 8); + uint8_t arg3 = (uint8_t)mem[current_instruction + 1]; + uint8_t arg2 = (uint8_t)(mem[current_instruction + 1] >> 8); + printf("Executing at 0x%04x; Instruction number %ld\r", current_instruction, instruction_counter); + fflush(stdout); + (*opcodes[instruction]) (arg1, arg2, arg3); + instruction_counter++; + } + printf("Execution halted: cause %d\n", halted_reason); + +}