Add function for getting system random number
authorbylex <mbilek06@gmail.com>
Wed, 26 Mar 2025 08:11:10 +0000 (09:11 +0100)
committerbylex <mbilek06@gmail.com>
Wed, 26 Mar 2025 08:11:10 +0000 (09:11 +0100)
common.c
common.h
interpreter.c
opcodes.c
opcodes.h
test_random.hex [new file with mode: 0644]
test_random.s [new file with mode: 0644]

index c44bf237b7917479e6ae367e66b71c1a7d7cf795..4f4d254a08c92e14cae1a59737c1ded8c296df0c 100644 (file)
--- a/common.c
+++ b/common.c
@@ -1,5 +1,5 @@
 #include "common.h"
 
-char opcodes_strings[N_INSTRUCTIONS][4] = {"nop", "inc", "dec", "lod", "ldl", "sav", "swp", "jmp", "jez", "hlt", "pts", "pfs", "dpx", "lor", "fdr"};
+char opcodes_strings[N_INSTRUCTIONS][4] = {"nop", "inc", "dec", "lod", "ldl", "sav", "swp", "jmp", "jez", "hlt", "pts", "pfs", "dpx", "lor", "fdr", "gri"};
 
-uint8_t opcode_kind[N_INSTRUCTIONS] = {0, 2, 2, 1, 1, 1, 1, 4, 1, 0, 5, 5, 3, 2, 0};
+uint8_t opcode_kind[N_INSTRUCTIONS] = {0, 2, 2, 1, 1, 1, 1, 4, 1, 0, 5, 5, 3, 2, 0, 5};
index 00e87ee833d4f06c39b379e5d04260420226320c..3ce2544bb2777818ba447ca8686c821f21f3d293 100644 (file)
--- a/common.h
+++ b/common.h
@@ -1,6 +1,6 @@
 #pragma once
 #include <stdint.h>
-#define N_INSTRUCTIONS 15
+#define N_INSTRUCTIONS 16
 
 #define OPCODE_NOARGS 0
 #define OPCODE_8_16 1
index bfa72972dfd7c6bf6dbc7c49c991951ef95ad394..17f9e03801041e69f5d8db4cf0b4b14fa947d0ac 100644 (file)
@@ -4,6 +4,7 @@
 #include <string.h>
 #include <stdint.h>
 #include <unistd.h>
+#include <time.h>
 
 #include "/home/bylex/software/tigr/tigr.h"
 
@@ -307,6 +308,7 @@ void run_info()
 
 int main(int argc, char *argv[])
 {
+    srand(time(NULL));
        screen = tigrWindow(256,256, "Interpreter display", 0);
        if(argc == 1)
        {
index 988fc9e7ff2af0abe2c57324ad84094d92ac6d56..13884787d161d38fd04359ed854d3f26c6b5cfae 100644 (file)
--- a/opcodes.c
+++ b/opcodes.c
@@ -1,3 +1,5 @@
+#include <stdlib.h>
+
 #include "common.h"
 #include "opcodes.h"
 
@@ -159,4 +161,11 @@ void fdr(uint8_t unused_1, uint8_t unused_2, uint8_t unused_3)
        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, fdr};
+void gri(uint8_t dest_reg, uint8_t unused_2, uint8_t unused_3)
+{
+    regs[dest_reg] = rand();
+    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, lor, fdr, gri};
index 9560d810d82bb8e261049c2ecb7aaa156b2d5235..91e3d1ce99fa15f541e362e0257470f210953f11 100644 (file)
--- a/opcodes.h
+++ b/opcodes.h
@@ -30,6 +30,7 @@
  *
  * lor, 3 byte length - instruction + dest register + register containing address where to load from
  * fdr, 1 byte length - flush output to screen
+ * gri, 2 byte length - instruction + dest register - get random number from system
  *
  * all instructions are padded to 4 bytes
  */
@@ -78,5 +79,6 @@ extern void dpx(uint8_t pixel_num_reg, uint8_t red_green_reg, uint8_t blue_alpha
 
 extern void lor(uint8_t dest_reg, uint8_t unused_1, uint8_t mem_addr_reg);
 extern void fdr(uint8_t unused_1, uint8_t unused_2, uint8_t unused_3);
+extern void gri(uint8_t dest_reg, uint8_t unused_2, uint8_t unused_3);
 
 extern void (*opcodes[N_INSTRUCTIONS]) (uint8_t, uint8_t, uint8_t);
diff --git a/test_random.hex b/test_random.hex
new file mode 100644 (file)
index 0000000..1477a84
Binary files /dev/null and b/test_random.hex differ
diff --git a/test_random.s b/test_random.s
new file mode 100644 (file)
index 0000000..56f3ab1
--- /dev/null
@@ -0,0 +1,14 @@
+ldl 0 65535
+; reg 0 is a screen pixel index
+ldl 2 1
+$LOOP
+gri 3
+gri 4
+; 3 now contains R and G, 4 contains B and A
+dpx 0 3 4
+dec 0 2
+jez 0 $END
+jmp $LOOP
+$END
+fdr
+hlt