From: bylex Date: Wed, 26 Mar 2025 08:11:10 +0000 (+0100) Subject: Add function for getting system random number X-Git-Url: https://git.bylex.cz/?a=commitdiff_plain;h=93842351e433d138aa66752e812af63bdaae5300;p=maturita.git Add function for getting system random number --- 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 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 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