commit 68c62a3
zzo38
·
2025-12-24 01:28:04 +0000 UTC
parent 86181d4
Add a random number generator extension.
3 files changed,
+49,
-0
+2,
-0
1@@ -337,6 +337,8 @@ expansion commands to activate them).
2
3 See the file extension.doc for the definition of the extensions.
4
5+* 4105259c-e061-11f0-963f-002618745416 (uses /dev/urandom)
6+
7 * 80175132-e263-11ed-b8c9-002618745416
8
9 * 970b5a0c-2c44-11ee-ac3b-002618745416
+25,
-0
1@@ -23,6 +23,31 @@ See: https://git.phial.org/d6/uxn11/src/branch/d6/multiprocess/console.txt
2 (This extension may be obsolete; see 9c5f1356-6bc5-11ef-a6c9-002618745416)
3
4
5+=== 4105259c-e061-11f0-963f-002618745416 (Random number generator) ===
6+
7+This extension is for the random number generator. The current verison
8+number is 1.
9+
10+Which device number is possible depends on the implementation; some might
11+ignore the device number. You should specify a device number that your
12+program does not otherwise use.
13+
14+The data of the extension is four bytes:
15+
16+* First is the flag byte, which has bit0 clear for 8-bit output or set for
17+16-bit output. The other bits must be clear.
18+
19+* Second is the number of bytes within a subroutine which the extension is
20+allowed to overwrite the contents of. These number of bytes will have an
21+unspecified contents after this extension is loaded successfully, and you
22+should not modify them (which might or might not affect the extension).
23+
24+* Third is the 16-bit address of the subroutine to override.
25+
26+If the subroutine is called at the specified address, it will push a random
27+number to the data stack followed by the effect of JMP2r.
28+
29+
30 === 80175132-e263-11ed-b8c9-002618745416 (Colours) ===
31
32 Allows setting separate foreground and background colours. The device
M
main.c
+22,
-0
1@@ -13,6 +13,7 @@ exit
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/stat.h>
5+#include <sys/types.h>
6 #include <time.h>
7 #include <unistd.h>
8
9@@ -248,6 +249,7 @@ static Uint8 palet=7;
10 static Uint8 touch_mode=0;
11 static SDL_cond*stdin_cond;
12 static Uint8 screencompat=0x03;
13+static int randomfd=-1;
14
15 static Uint16 scriptc;
16 static Script*scriptv;
17@@ -810,6 +812,12 @@ static Uint8 datetime_in(Device*dev,Uint8 id) {
18 }
19 }
20
21+static Uint8 random_in(Device*dev,Uint8 id) {
22+ Uint8 x[1];
23+ if(read(randomfd,x,1)!=1) err(1,"Cannot read random numbers");
24+ return *x;
25+}
26+
27 static void set_cursor(void) {
28 if(hide_cursor) SDL_ShowCursor(paused || !use_mouse);
29 }
30@@ -1056,6 +1064,7 @@ static void do_extension_by_uuid(Uint16 addr) {
31 static const Uint8 uuid_8color[]="\x80\x17\x51\x32\xE2\x63\x11\xED\xB8\xC9\x00\x26\x18\x74\x54\x16";
32 static const Uint8 uuid_screencompat[]="\x97\x0B\x5A\x0C\x2C\x44\x11\xEE\xAC\x3B\x00\x26\x18\x74\x54\x16";
33 static const Uint8 uuid_subprocess[]="\x9C\x5F\x13\x56\x6B\xC5\x11\xEF\xA6\xC9\x00\x26\x18\x74\x54\x16";
34+ static const Uint8 uuid_random[]="\x41\x05\x25\x9C\xE0\x61\x11\xF0\x96\x3F\x00\x26\x18\x74\x54\x16";
35 int i;
36 if(mem[addr+1]==0x00 && use_screen && !memcmp(mem+addr+2,uuid_8color,16)) {
37 for(i=0;i<8;i++) {
38@@ -1075,6 +1084,19 @@ static void do_extension_by_uuid(Uint16 addr) {
39 } else if(mem[addr+1]==0x10 && use_thread && use_console && allow_write && !memcmp(mem+addr+2,uuid_subprocess,16)) {
40 device[1].in=subprocess_console_in;
41 enable_subprocess=mem[addr+18]=1;
42+ } else if(device[mem[addr+1]>>4].in==default_in && mem[addr+19]<2 && mem[addr+20]>=4 && !memcmp(mem+addr+2,uuid_random,16)) {
43+ if(randomfd==-1) randomfd=open("/dev/urandom",O_RDONLY|O_NOCTTY|O_CLOEXEC);
44+ if(randomfd>=0) {
45+ mem[addr+18]=1;
46+ device[mem[addr+1]>>4].in=random_in;
47+ i=(mem[addr+21]<<8)|mem[addr+22];
48+ mem[i]=0x80;
49+ mem[i+1]=mem[addr+1];
50+ mem[i+2]=0x16+(mem[addr+19]<<5);
51+ mem[i+3]=0x6C;
52+ } else {
53+ warn("Cannot open /dev/urandom");
54+ }
55 }
56 }
57