mirror of
https://git.sr.ht/~crc_/retroforth
synced 2024-09-29 05:36:52 +02:00
nga-c: split malloc and rng devices into separate files
FossilOrigin-Name: 226d56002e03e440e9fbdde22db6cfcc9b037f4608431823f7c5d8e08d8fadef
This commit is contained in:
parent
ce03a8525f
commit
5d34094ff0
3 changed files with 130 additions and 104 deletions
88
vm/nga-c/dev-malloc.c
Normal file
88
vm/nga-c/dev-malloc.c
Normal file
|
@ -0,0 +1,88 @@
|
|||
/*---------------------------------------------------------------------
|
||||
Copyright (c) 2008 - 2022, Charles Childers
|
||||
|
||||
Portions are based on Ngaro, which was additionally copyright
|
||||
by the following:
|
||||
|
||||
Copyright (c) 2009 - 2010, Luke Parrish
|
||||
Copyright (c) 2010, Marc Simpson
|
||||
Copyright (c) 2010, Jay Skeer
|
||||
Copyright (c) 2011, Kenneth Keating
|
||||
---------------------------------------------------------------------*/
|
||||
|
||||
#ifdef ENABLE_MALLOC
|
||||
#ifdef BIT64
|
||||
typedef union {
|
||||
void* val;
|
||||
struct {
|
||||
CELL msw;
|
||||
CELL lsw;
|
||||
};
|
||||
} double_cell;
|
||||
|
||||
void double_add(NgaState *vm) {
|
||||
double_cell a;
|
||||
double_cell b;
|
||||
double_cell c;
|
||||
b.msw = stack_pop(vm);
|
||||
b.lsw = stack_pop(vm);
|
||||
a.msw = stack_pop(vm);
|
||||
a.lsw = stack_pop(vm);
|
||||
}
|
||||
|
||||
void double_sub(NgaState *vm) {
|
||||
}
|
||||
|
||||
void double_mul(NgaState *vm) {
|
||||
}
|
||||
|
||||
void double_divmod(NgaState *vm) {
|
||||
}
|
||||
|
||||
void malloc_allocate(NgaState *vm) {
|
||||
stack_push(vm, (CELL)malloc(stack_pop(vm)));
|
||||
}
|
||||
|
||||
void malloc_free(NgaState *vm) {
|
||||
free((CELL*)stack_pop(vm));
|
||||
}
|
||||
|
||||
void malloc_store(NgaState *vm) {
|
||||
CELL value = stack_pop(vm);
|
||||
double_cell addr;
|
||||
*(CELL *) stack_pop(vm) = value;
|
||||
}
|
||||
|
||||
void malloc_fetch(NgaState *vm) {
|
||||
double_cell addr;
|
||||
CELL value = *(CELL *)stack_pop(vm);
|
||||
stack_push(vm, value);
|
||||
}
|
||||
|
||||
void malloc_realloc(NgaState *vm) {
|
||||
CELL bytes = stack_pop(vm);
|
||||
CELL* addr1;
|
||||
addr1 = (CELL*)stack_pop(vm);
|
||||
CELL* addr2;
|
||||
addr2 = (CELL*)realloc(addr1, bytes);
|
||||
stack_push(vm, (CELL)addr2);
|
||||
}
|
||||
|
||||
void query_malloc(NgaState *vm) {
|
||||
stack_push(vm, 0);
|
||||
stack_push(vm, 15);
|
||||
}
|
||||
|
||||
void io_malloc(NgaState *vm) {
|
||||
int i = stack_pop(vm);
|
||||
switch (i) {
|
||||
case 0: malloc_allocate(vm); return;
|
||||
case 1: malloc_free(vm); return;
|
||||
case 2: malloc_store(vm); return;
|
||||
case 3: malloc_fetch(vm); return;
|
||||
case 4: malloc_realloc(vm); return;
|
||||
}
|
||||
stack_push(vm, -1);
|
||||
}
|
||||
#endif
|
||||
#endif
|
38
vm/nga-c/dev-rng.c
Normal file
38
vm/nga-c/dev-rng.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*---------------------------------------------------------------------
|
||||
Copyright (c) 2008 - 2022, Charles Childers
|
||||
|
||||
Portions are based on Ngaro, which was additionally copyright
|
||||
by the following:
|
||||
|
||||
Copyright (c) 2009 - 2010, Luke Parrish
|
||||
Copyright (c) 2010, Marc Simpson
|
||||
Copyright (c) 2010, Jay Skeer
|
||||
Copyright (c) 2011, Kenneth Keating
|
||||
---------------------------------------------------------------------*/
|
||||
|
||||
/* Random Number Generator --------------------------------------------*/
|
||||
#ifdef ENABLE_RNG
|
||||
void 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
|
||||
}
|
||||
|
||||
void query_rng(NgaState *vm) {
|
||||
stack_push(vm, 0);
|
||||
stack_push(vm, 10);
|
||||
}
|
||||
#endif
|
108
vm/nga-c/retro.c
108
vm/nga-c/retro.c
|
@ -239,78 +239,7 @@ void guard(NgaState *vm, int n, int m, int diff) {
|
|||
/* Dynamic Memory / `malloc` support --------------------------------- */
|
||||
#ifdef ENABLE_MALLOC
|
||||
#ifdef BIT64
|
||||
typedef union {
|
||||
void* val;
|
||||
struct {
|
||||
CELL msw;
|
||||
CELL lsw;
|
||||
};
|
||||
} double_cell;
|
||||
|
||||
void double_add(NgaState *vm) {
|
||||
double_cell a;
|
||||
double_cell b;
|
||||
double_cell c;
|
||||
b.msw = stack_pop(vm);
|
||||
b.lsw = stack_pop(vm);
|
||||
a.msw = stack_pop(vm);
|
||||
a.lsw = stack_pop(vm);
|
||||
}
|
||||
|
||||
void double_sub(NgaState *vm) {
|
||||
}
|
||||
|
||||
void double_mul(NgaState *vm) {
|
||||
}
|
||||
|
||||
void double_divmod(NgaState *vm) {
|
||||
}
|
||||
|
||||
void malloc_allocate(NgaState *vm) {
|
||||
stack_push(vm, (CELL)malloc(stack_pop(vm)));
|
||||
}
|
||||
|
||||
void malloc_free(NgaState *vm) {
|
||||
free((CELL*)stack_pop(vm));
|
||||
}
|
||||
|
||||
void malloc_store(NgaState *vm) {
|
||||
CELL value = stack_pop(vm);
|
||||
double_cell addr;
|
||||
*(CELL *) stack_pop(vm) = value;
|
||||
}
|
||||
|
||||
void malloc_fetch(NgaState *vm) {
|
||||
double_cell addr;
|
||||
CELL value = *(CELL *)stack_pop(vm);
|
||||
stack_push(vm, value);
|
||||
}
|
||||
|
||||
void malloc_realloc(NgaState *vm) {
|
||||
CELL bytes = stack_pop(vm);
|
||||
CELL* addr1;
|
||||
addr1 = (CELL*)stack_pop(vm);
|
||||
CELL* addr2;
|
||||
addr2 = (CELL*)realloc(addr1, bytes);
|
||||
stack_push(vm, (CELL)addr2);
|
||||
}
|
||||
|
||||
void query_malloc(NgaState *vm) {
|
||||
stack_push(vm, 0);
|
||||
stack_push(vm, 15);
|
||||
}
|
||||
|
||||
void io_malloc(NgaState *vm) {
|
||||
int i = stack_pop(vm);
|
||||
switch (i) {
|
||||
case 0: malloc_allocate(vm); return;
|
||||
case 1: malloc_free(vm); return;
|
||||
case 2: malloc_store(vm); return;
|
||||
case 3: malloc_fetch(vm); return;
|
||||
case 4: malloc_realloc(vm); return;
|
||||
}
|
||||
stack_push(vm, -1);
|
||||
}
|
||||
#include "dev-malloc.c"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
@ -357,6 +286,8 @@ void query_blocks(NgaState *vm) {
|
|||
}
|
||||
#endif
|
||||
|
||||
#include "dev-files.c"
|
||||
|
||||
/* Multi Core Support ------------------------------------------------ */
|
||||
#ifdef ENABLE_MULTICORE
|
||||
#include "dev-multicore.c"
|
||||
|
@ -370,44 +301,13 @@ void query_blocks(NgaState *vm) {
|
|||
#include "dev-float.c"
|
||||
#endif
|
||||
|
||||
|
||||
/* FileSystem Device ------------------------------------------------- */
|
||||
|
||||
#include "dev-files.c"
|
||||
|
||||
/* Time and Date Functions --------------------------------------------*/
|
||||
#ifdef ENABLE_CLOCK
|
||||
#include "dev-clock.c"
|
||||
#endif
|
||||
|
||||
|
||||
/* Random Number Generator --------------------------------------------*/
|
||||
#ifdef ENABLE_RNG
|
||||
void 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));
|
||||
#include "dev-rng.c"
|
||||
#endif
|
||||
}
|
||||
|
||||
void query_rng(NgaState *vm) {
|
||||
stack_push(vm, 0);
|
||||
stack_push(vm, 10);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef ENABLE_SOCKETS
|
||||
#include "dev-sockets.c"
|
||||
|
|
Loading…
Reference in a new issue