Skip to content

Commit d780c4e

Browse files
Add UVM32_SYSCALL_RAND to get a "true" random number from the host, as I want to play zigtris and not have it be the same every time.
1 parent 3d83844 commit d780c4e

6 files changed

Lines changed: 18 additions & 1 deletion

File tree

apps/common/uvm32_target.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static uint32_t syscall(uint32_t id, uint32_t param1, uint32_t param2) {
5050
#define printbuf(x, y) syscall_cast(UVM32_SYSCALL_PRINTBUF, x, y)
5151
#define render(x, y) syscall_cast(UVM32_SYSCALL_RENDER, x, y)
5252
#define getkey() syscall_cast(UVM32_SYSCALL_GETKEY, 0, 0)
53+
#define rand() syscall_cast(UVM32_SYSCALL_RAND, 0, 0)
5354

5455
extern char _estack;
5556

apps/zigtris/src/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export fn main() void {
1010
var gameRunning = true;
1111
var lastUpdate:u32 = 0;
1212

13-
zigtris.gamesetup(console, uvm.millis()) catch |err| {
13+
zigtris.gamesetup(console, uvm.rand()) catch |err| {
1414
_ = console.print("err {any}\n", .{err}) catch 0;
1515
_ = console.flush() catch 0;
1616
return;

apps/zigtris/src/uvm.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub inline fn millis() u32 {
2727
return syscall(uvm32.UVM32_SYSCALL_MILLIS, 0, 0);
2828
}
2929

30+
pub inline fn rand() u32 {
31+
return syscall(uvm32.UVM32_SYSCALL_RAND, 0, 0);
32+
}
33+
3034
// dupeZ would be better, but want to avoid using an allocator
3135
// this is of course, unsafe...
3236
var termination_buf:[128]u8 = undefined;

common/uvm32_common_custom.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
#define UVM32_SYSCALL_GETKEY 0x00000009
1515
#define UVM32_SYSCALL_RENDERAUDIO 0x0000000A
1616
#define UVM32_SYSCALL_CANRENDERAUDIO 0x0000000B
17+
#define UVM32_SYSCALL_RAND 0x0000000C
18+
1719

hosts/host-sdl/host-sdl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ int main(int argc, char *argv[]) {
241241
return 1;
242242
}
243243

244+
srand(clock());
245+
244246
uvm32_init(vmst);
245247

246248
if (!uvm32_load(vmst, rom, romlen)) {
@@ -374,6 +376,9 @@ int main(int argc, char *argv[]) {
374376
case UVM32_SYSCALL_MILLIS: {
375377
uvm32_arg_setval(vmst, &evt, RET, SDL_GetTicks());
376378
} break;
379+
case UVM32_SYSCALL_RAND:
380+
uvm32_arg_setval(&vmst, &evt, RET, rand());
381+
break;
377382
case UVM32_SYSCALL_GETC: {
378383
uvm32_arg_setval(vmst, &evt, RET, 0xFFFFFFFF);
379384
} break;

hosts/host/host.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ int main(int argc, char *argv[]) {
190190
return 1;
191191
}
192192

193+
srand(clock());
194+
193195
uvm32_init(&vmst);
194196

195197
if (!uvm32_load(&vmst, rom, romlen)) {
@@ -264,6 +266,9 @@ int main(int argc, char *argv[]) {
264266
case UVM32_SYSCALL_PRINTHEX:
265267
printf("%08x", uvm32_arg_getval(&vmst, &evt, ARG0));
266268
break;
269+
case UVM32_SYSCALL_RAND:
270+
uvm32_arg_setval(&vmst, &evt, RET, rand());
271+
break;
267272
case UVM32_SYSCALL_MILLIS: {
268273
clock_t now = clock() / (CLOCKS_PER_SEC / 1000);
269274
uvm32_arg_setval(&vmst, &evt, RET, now - start_time);

0 commit comments

Comments
 (0)