From: bylex Date: Wed, 26 Feb 2025 08:02:00 +0000 (+0100) Subject: Split opcodes.c to opcodes.c and .h X-Git-Url: https://git.bylex.cz/?a=commitdiff_plain;h=491c43f07e95ea357e7be3c27b7e0c27caefad61;p=maturita.git Split opcodes.c to opcodes.c and .h --- diff --git a/opcodes.c b/opcodes.c index d8c3673..a46b74a 100644 --- a/opcodes.c +++ b/opcodes.c @@ -1,42 +1,9 @@ -/* - * operation codes with description - * register size is 2 bytes - * - * nop, 1 byte length - instruction - no operation - * - * inc, 3 byte length - instruction + dest register + source register - increment dest register by source register - * dec, 3 byte length - instruction + dest register + source register - decrement dest register by source register - * - * lod, 4 byte length - instruction + dest register + source address - load 2 bytes from memory to register - * ldl, 4 byte length - instruction + dest register + number literal - load 2 bytes from instruction to register - * sav, 4 byte length - instruction + dest register + source address - save 2 bytes from register to memory - * swp, 4 byte length - instruction + register + address - swap 2 bytes between register and memory - * - * jmp, 2 byte length - instruction + unused + address - jump to address contained in register in memory - * jez, 4 byte length - instruction + cond register + address - conditional jump to address in memory, if untrue acts as nop - * - * hlt, 1 byte length - instruction - halts execution - * - * pts, 2 byte length - instruction + source register - puts a value on the stack - * pfs, 2 byte length - instruction + dest register - pulls a value from the stack - * stack is 256 values long, 16 bit values - * - * TODO: figure out input/output - * - * wex, 2 byte length - instruction + source register - write 2 bytes to external output(stdout) - * rex, 2 byte length - instruction + dest register - read from external(stdin) - blocks execution until 2 bytes are read - * - * dpx, 4 byte length - instruction + register with pixel location + register with R and G + register with B and A(R and B are first 8 bits, G and A are second 8 bits) - * - * lor, 3 byte length - instruction + dest register + register containing address where to load from - * - * all instructions are padded to 4 bytes - */ #include #include #include "common.h" +#include "opcodes.h" #include "tigr.h" @@ -75,7 +42,6 @@ void dec(uint8_t dest_reg, uint8_t unused_1, uint8_t source_reg) } -// loads addressed byte and the next byte into register void lod(uint8_t dest_reg, uint8_t source_addr_1, uint8_t source_addr_2) { uint16_t source_addr = (uint16_t)source_addr_1 << 8 | source_addr_2; @@ -189,4 +155,3 @@ void lor(uint8_t dest_reg, uint8_t unused_1, uint8_t mem_addr_reg) return; } -void (*opcodes[N_INSTRUCTIONS]) (uint8_t, uint8_t, uint8_t) = {nop, inc, dec, lod, ldl, sav, swp, jmp, jez, hlt, pts, pfs, dpx, lor}; diff --git a/opcodes.h b/opcodes.h new file mode 100644 index 0000000..025b3f3 --- /dev/null +++ b/opcodes.h @@ -0,0 +1,75 @@ +/* + * operation codes with description + * register size is 2 bytes + * + * nop, 1 byte length - instruction - no operation + * + * inc, 3 byte length - instruction + dest register + source register - increment dest register by source register + * dec, 3 byte length - instruction + dest register + source register - decrement dest register by source register + * + * lod, 4 byte length - instruction + dest register + source address - load 2 bytes from memory to register + * ldl, 4 byte length - instruction + dest register + number literal - load 2 bytes from instruction to register + * sav, 4 byte length - instruction + dest register + source address - save 2 bytes from register to memory + * swp, 4 byte length - instruction + register + address - swap 2 bytes between register and memory + * + * jmp, 2 byte length - instruction + unused + address - jump to address contained in register in memory + * jez, 4 byte length - instruction + cond register + address - conditional jump to address in memory, if untrue acts as nop + * + * hlt, 1 byte length - instruction - halts execution + * + * pts, 2 byte length - instruction + source register - puts a value on the stack + * pfs, 2 byte length - instruction + dest register - pulls a value from the stack + * stack is 256 values long, 16 bit values + * + * TODO: figure out input/output + * + * wex, 2 byte length - instruction + source register - write 2 bytes to external output(stdout) + * rex, 2 byte length - instruction + dest register - read from external(stdin) - blocks execution until 2 bytes are read + * + * dpx, 4 byte length - instruction + register with pixel location + register with R and G + register with B and A(R and B are first 8 bits, G and A are second 8 bits) + * + * lor, 3 byte length - instruction + dest register + register containing address where to load from + * + * all instructions are padded to 4 bytes + */ +#include "common.h" + +#include "tigr.h" + +extern uint16_t regs[256]; +extern uint16_t mem[65536]; + +extern uint16_t stack[256]; +extern uint8_t stack_pointer; + +extern uint16_t current_instruction; // what location is currently being executed +extern bool halted; // is the execution halted? +extern uint8_t halted_reason; + +extern Tigr* screen; + +void nop(uint8_t unused_1, uint8_t unused_2, uint8_t unused_3); + +void inc(uint8_t dest_reg, uint8_t unused_1, uint8_t source_reg); +void dec(uint8_t dest_reg, uint8_t unused_1, uint8_t source_reg); + +// loads addressed byte and the next byte into register +void lod(uint8_t dest_reg, uint8_t source_addr_1, uint8_t source_addr_2); +void ldl(uint8_t dest_reg, uint8_t num_literal_1, uint8_t num_literal_2); +void sav(uint8_t source_reg, uint8_t dest_addr_1, uint8_t dest_addr_2); + +void swp(uint8_t reg, uint8_t addr_1, uint8_t addr_2); + +void jmp(uint8_t unused_1, uint8_t addr_1, uint8_t addr_2); +void jez(uint8_t reg, uint8_t addr_1, uint8_t addr_2); + +void hlt(uint8_t unused_1, uint8_t unused_2, uint8_t unused_3); + +void pts(uint8_t source_reg, uint8_t unused_1, uint8_t unused_2); +void pfs(uint8_t dest_reg, uint8_t unused_1, uint8_t unused_2); + +void dpx(uint8_t pixel_num_reg, uint8_t red_green_reg, uint8_t blue_alpha_reg); + +void lor(uint8_t dest_reg, uint8_t unused_1, uint8_t mem_addr_reg); + +void (*opcodes[N_INSTRUCTIONS]) (uint8_t, uint8_t, uint8_t) = {nop, inc, dec, lod, ldl, sav, swp, jmp, jez, hlt, pts, pfs, dpx, lor};