Add graphics support usign TIGR
authorbylex <mbilek06@gmail.com>
Tue, 29 Oct 2024 18:44:19 +0000 (19:44 +0100)
committerbylex <mbilek06@gmail.com>
Tue, 29 Oct 2024 18:44:19 +0000 (19:44 +0100)
Makefile
assembler.c
assembler.h
common.h
interpreter.c
opcodes.c [moved from opcodes.h with 82% similarity]

index db9300c5d9e1bc20f7fa4ea3498b91c40ad28523..a23ba9af86d8ea64f3585c712d65548d72cd9edc 100644 (file)
--- 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:
index fab62373034c2eea6f0219ffa0cd5b8c421fe430..fb5f67b7bfa3ec5b45a36d31100fad5945a41673 100644 (file)
@@ -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++;
index 22714f881286ffbcee6952025bcae6278c87d910..0361831c59215de9aa372e0a5a898a50db1fff95 100644 (file)
@@ -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"};
index cd94b246597f1cc214dc53071411e2349c73fc6a..a0a432df5923864f25725a5850a3471deeb36be7 100644 (file)
--- a/common.h
+++ b/common.h
@@ -1 +1 @@
-#define N_INSTRUCTIONS 12
+#define N_INSTRUCTIONS 13
index 4e2e349f117e41493c4d6ee8854481680cf767de..4ed9125b63cf813a7a36469fd1f47dd20ef0c344 100644 (file)
@@ -3,7 +3,9 @@
 #include <string.h>
 #include <stdint.h>
 
-#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);
        
similarity index 82%
rename from opcodes.h
rename to opcodes.c
index dd4e2d434dcae2bd89016da724e67db5dc4b509c..59078235993b02ab77a8879c403a8d1bf06c8770 100644 (file)
--- 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};