retroforth/vm/nga-c/dev-rng.c
crc 164dbffec2 nga-c: more small cleanups in clock, error, ffi, files, malloc, rng, sockets
FossilOrigin-Name: 0ccaff8f785347bc39ebcd74c66b2cde9c2dde8bc6d6f304cb46fea931155cd3
2023-12-10 23:49:52 +00:00

38 lines
1 KiB
C

/**************************************************************
_ __ _ _
_ __ ___| |_ _ __ ___ / _| ___ _ __| |_| |__
| '__/ _ \ __| '__/ _ \| |_ / _ \| '__| __| '_ \
| | | __/ |_| | | (_) | _| (_) | | | |_| | | |
|_| \___|\__|_| \___/|_| \___/|_| \__|_| |_|
for nga
(c) Charles Childers, Luke Parrish, Marc Simpsonn,
Jay Skeer, Kenneth Keating
**************************************************************/
#ifdef ENABLE_RNG
V io_rng(NgaState *vm) {
int64_t r = 0;
char buffer[8];
int i;
ssize_t ignore;
int fd = open("/dev/urandom", O_RDONLY);
ignore = read(fd, buffer, 8);
close(fd);
for(i = 0; i < 8; ++i) {
r = r << 8;
r += ((int64_t)buffer[i] & 0xFF);
}
#ifndef BIT64
stack_push(vm, (CELL)abs((CELL)r));
#else
stack_push(vm, (CELL)llabs((CELL)r));
#endif
}
V query_rng(NgaState *vm) {
stack_push(vm, 0);
stack_push(vm, DEVICE_RNG);
}
#endif