From 93842351e433d138aa66752e812af63bdaae5300 Mon Sep 17 00:00:00 2001 From: bylex Date: Wed, 26 Mar 2025 09:11:10 +0100 Subject: [PATCH] Add function for getting system random number --- common.c | 4 ++-- common.h | 2 +- interpreter.c | 2 ++ opcodes.c | 11 ++++++++++- opcodes.h | 2 ++ test_random.hex | Bin 0 -> 131072 bytes test_random.s | 14 ++++++++++++++ 7 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 test_random.hex create mode 100644 test_random.s diff --git a/common.c b/common.c index c44bf23..4f4d254 100644 --- 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}; diff --git a/common.h b/common.h index 00e87ee..3ce2544 100644 --- a/common.h +++ b/common.h @@ -1,6 +1,6 @@ #pragma once #include -#define N_INSTRUCTIONS 15 +#define N_INSTRUCTIONS 16 #define OPCODE_NOARGS 0 #define OPCODE_8_16 1 diff --git a/interpreter.c b/interpreter.c index bfa7297..17f9e03 100644 --- a/interpreter.c +++ b/interpreter.c @@ -4,6 +4,7 @@ #include #include #include +#include #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) { diff --git a/opcodes.c b/opcodes.c index 988fc9e..1388478 100644 --- a/opcodes.c +++ b/opcodes.c @@ -1,3 +1,5 @@ +#include + #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}; diff --git a/opcodes.h b/opcodes.h index 9560d81..91e3d1c 100644 --- 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 index 0000000000000000000000000000000000000000..1477a847413280aa74bc4433a72e24c71a33a8be GIT binary patch literal 131072 zcmeIu!3n@16hy(*d`N_#{a0)3BeU>e&r{jEdfKYw&2?`2lgvA*nPi-d009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U vAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7csfqw{mJQf0M literal 0 HcmV?d00001 diff --git a/test_random.s b/test_random.s new file mode 100644 index 0000000..56f3ab1 --- /dev/null +++ b/test_random.s @@ -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 -- 2.25.1