From: bylex Date: Tue, 29 Oct 2024 18:44:19 +0000 (+0100) Subject: Add graphics support usign TIGR X-Git-Url: https://git.bylex.cz/?a=commitdiff_plain;h=2e04a64f58e29b7a6a40f59ced4123eae15c8016;p=maturita.git Add graphics support usign TIGR --- diff --git a/Makefile b/Makefile index db9300c..a23ba9a 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,8 @@ CFLAGS = -g -w -O0 all: int ass - int: - $(CC) $(CFLAGS) interpreter.c -o interpreter + $(CC) -I$(HOME)/software/tigr $(HOME)/software/tigr/tigr.c interpreter.c -o interpreter -lGLU -lGL -lX11 $(CFLAGS) ass: $(CC) $(CFLAGS) assembler.c -o assembler clean: diff --git a/assembler.c b/assembler.c index fab6237..fb5f67b 100644 --- a/assembler.c +++ b/assembler.c @@ -57,7 +57,7 @@ struct inst_t parse_line(char* line_buffer, struct label_t* labels, unsigned int case 0: // current token is instruction code uint8_t i = 0; - while(i < N_INSTRUCTIONS - 1) + while(i < N_INSTRUCTIONS) { int token_len = strlen(token); if(token_len != 3) @@ -82,7 +82,53 @@ struct inst_t parse_line(char* line_buffer, struct label_t* labels, unsigned int break; case 1: // current token is first parameter - inst.params[0] = (uint8_t)atoi(token); + // jump only has an address + if(inst.instruction_code == 7) + { + if(token[0] == '$') + { + int i = 0; + int token_len = strlen(token); + while(i < token_len) + { + if(isspace(token[i])) + { + token[i] = '\0'; + } + i++; + } + + int j = 0; + bool label_known = false; + while(j < labels_index) + { + if(strcmp(labels[j].label_name, token) == 0) + { + inst.params[1] = (uint8_t)(labels[j].addr >> 8); + inst.params[2] = (uint8_t)(labels[j].addr); + label_known = true; + break; + } + j++; + } + if(!label_known) + { + fprintf(stderr, "Unknown identifier\n"); + exit(1); + } + break; + } + else + { + uint16_t token_int = atoi(token); + inst.params[1] = (uint8_t)(token_int >> 8); + inst.params[2] = (uint8_t)(token_int); + } + } + else + { + inst.params[0] = (uint8_t)atoi(token); + } break; case 2: // current token is second parameter @@ -121,14 +167,24 @@ struct inst_t parse_line(char* line_buffer, struct label_t* labels, unsigned int } else { - uint16_t token_int = atoi(token); - inst.params[1] = (uint8_t)(token_int >> 8); - inst.params[2] = (uint8_t)(token_int); + if(inst.instruction_code == 12) + { + uint8_t token_int = atoi(token); + inst.params[1] = (uint8_t)(token_int); + } + else + { + uint16_t token_int = atoi(token); + inst.params[1] = (uint8_t)(token_int >> 8); + inst.params[2] = (uint8_t)(token_int); + } break; } break; case 3: // current token is third parameter + uint8_t token_int = atoi(token); + inst.params[2] = token_int; break; } token_count++; diff --git a/assembler.h b/assembler.h index 22714f8..0361831 100644 --- a/assembler.h +++ b/assembler.h @@ -1,3 +1,3 @@ #include "common.h" -char opcodes_strings[N_INSTRUCTIONS][4] = {"nop", "inc", "dec", "lod", "ldl", "sav", "swp", "jmp", "jez", "hlt", "pts", "pfs"}; +char opcodes_strings[N_INSTRUCTIONS][4] = {"nop", "inc", "dec", "lod", "ldl", "sav", "swp", "jmp", "jez", "hlt", "pts", "pfs", "dpx"}; diff --git a/common.h b/common.h index cd94b24..a0a432d 100644 --- a/common.h +++ b/common.h @@ -1 +1 @@ -#define N_INSTRUCTIONS 12 +#define N_INSTRUCTIONS 13 diff --git a/interpreter.c b/interpreter.c index 4e2e349..4ed9125 100644 --- a/interpreter.c +++ b/interpreter.c @@ -3,7 +3,9 @@ #include #include -#include "opcodes.h" +#include "tigr.h" + +#include "opcodes.c" uint16_t regs[256]; uint16_t mem[65536]; @@ -11,6 +13,8 @@ uint16_t mem[65536]; uint16_t stack[256]; uint8_t stack_pointer; +Tigr* screen; + uint16_t current_instruction = 0; // entry location bool halted = false; uint8_t halted_reason = 0; @@ -42,6 +46,7 @@ void load_mem(char* file_name) int main(int argc, char *argv[]) { + screen = tigrWindow(256,256, "Interpreter display", 0); if(argc == 1) { fprintf(stderr, "No memory file!\n"); @@ -58,6 +63,10 @@ int main(int argc, char *argv[]) fflush(stdout); (*opcodes[instruction]) (arg1, arg2, arg3); instruction_counter++; + if(instruction_counter % 1024 == 0) + { + tigrUpdate(screen); + } } printf("\nExecution halted: cause %d\n", halted_reason); diff --git a/opcodes.h b/opcodes.c similarity index 82% rename from opcodes.h rename to opcodes.c index dd4e2d4..5907823 100644 --- a/opcodes.h +++ b/opcodes.c @@ -12,7 +12,7 @@ * 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 + register - jump to address in 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 @@ -25,6 +25,8 @@ * * 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) * * all instructions are padded to 4 bytes */ @@ -34,6 +36,8 @@ #include "common.h" +#include "tigr.h" + extern uint16_t regs[256]; extern uint16_t mem[65536]; @@ -44,6 +48,8 @@ extern uint16_t current_instruction; // what location is currently being execute 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) { current_instruction += 2; @@ -164,4 +170,12 @@ void pfs(uint8_t dest_reg, uint8_t unused_1, uint8_t unused_2) return; } -void (*opcodes[N_INSTRUCTIONS]) (uint8_t, uint8_t, uint8_t) = {nop, inc, dec, lod, ldl, sav, swp, jmp, jez, hlt, pts, pfs}; +void dpx(uint8_t pixel_num_reg, uint8_t red_green_reg, uint8_t blue_alpha_reg) +{ +// tigrPlot(screen, (uint8_t)(regs[pixel_num_reg]), (uint8_t)(regs[pixel_num_reg] >> 8), tigrRGBA((uint8_t)(regs[red_green_reg] >> 8), (uint8_t)regs[red_green_reg], (uint8_t)(regs[blue_alpha_reg] >> 8), (uint8_t)regs[blue_alpha_reg])); + screen->pix[regs[pixel_num_reg]] = tigrRGBA((uint8_t)(regs[red_green_reg] >> 8), (uint8_t)regs[red_green_reg], (uint8_t)(regs[blue_alpha_reg] >> 8), (uint8_t)regs[blue_alpha_reg]); + current_instruction += 2; + return; +} + +void (*opcodes[N_INSTRUCTIONS]) (uint8_t, uint8_t, uint8_t) = {nop, inc, dec, lod, ldl, sav, swp, jmp, jez, hlt, pts, pfs, dpx};