~crc_/retroforth

c4682248dd45aca2d894fbb6fe320177e9ff40a2 — crc 1 year, 8 months ago 53db3ce
nga-c: split ffi & multicore devices to separate files

FossilOrigin-Name: c2c10cf6897243a0086cb147d14daac4cf36e18163726535e92c0be1f562d79d
3 files changed, 135 insertions(+), 111 deletions(-)

A vm/nga-c/dev-ffi.c
A vm/nga-c/dev-multicore.c
M vm/nga-c/retro.c
A vm/nga-c/dev-ffi.c => vm/nga-c/dev-ffi.c +53 -0
@@ 0,0 1,53 @@
/*---------------------------------------------------------------------
  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_FFI
#include <dlfcn.h>

typedef void (*External)(void *);

void *handles[32];
External funcs[32000];
int nlibs, nffi;

void open_library(NgaState *vm) {
  handles[nlibs] = dlopen(string_extract(vm, stack_pop(vm)), RTLD_LAZY);
  stack_push(vm, nlibs);
  nlibs++;
}

void map_symbol(NgaState *vm) {
  int h;
  h = stack_pop(vm);
  char *s = string_extract(vm, stack_pop(vm));
  funcs[nffi] = dlsym(handles[h], s);
  stack_push(vm, nffi);
  nffi++;
}

void invoke(NgaState *vm) {
  funcs[stack_pop(vm)](vm);
}

void io_ffi(NgaState *vm) {
  switch (stack_pop(vm)) {
    case 0: open_library(vm); break;
    case 1: map_symbol(vm); break;
    case 2: invoke(vm); break;
  }
}

void query_ffi(NgaState *vm) {
  stack_push(vm, 0);
  stack_push(vm, 8100);  /* device type 8100 */
}
#endif

A vm/nga-c/dev-multicore.c => vm/nga-c/dev-multicore.c +80 -0
@@ 0,0 1,80 @@
/*---------------------------------------------------------------------
  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
  ---------------------------------------------------------------------*/

/* Multi Core Support ------------------------------------------------ */
#ifdef ENABLE_MULTICORE
void init_core(NgaState *vm, CELL x) {
  int y;
  vm->cpu[x].sp = 0;
  vm->cpu[x].rp = 0;
  vm->cpu[x].ip = 0;
  vm->cpu[x].active = 0;
  vm->cpu[x].u = 0;
  for (y = 0; y < STACK_DEPTH; y++) { vm->cpu[x].data[y] = 0; };
  for (y = 0; y < ADDRESSES; y++) { vm->cpu[x].address[y] = 0; };
  for (y = 0; y < 24; y++) { vm->cpu[x].registers[y] = 0; };
}

void start_core(NgaState *vm, CELL x, CELL ip) {
  vm->cpu[x].ip = ip;
  vm->cpu[x].rp = 1;
  vm->cpu[x].active = -1;
}

void pause_core(NgaState *vm, CELL x) {
  vm->cpu[x].active = 0;
}

void resume_core(NgaState *vm, CELL x) {
  vm->cpu[x].active = -1;
}

void switch_core(NgaState *vm) {
  vm->active += 1;
  if (vm->active >= CORES) { vm->active = 0; }
  if (!vm->cpu[vm->active].active) { switch_core(vm); }
}

void io_multicore(NgaState *vm) {
  int x, y, z;
  x = stack_pop(vm);
  switch(x) {
    case 0: y = stack_pop(vm);
            init_core(vm, y);
            break;
    case 1: y = stack_pop(vm);
            z = stack_pop(vm);
            start_core(vm, y, z);
            break;
    case 2: y = stack_pop(vm);
            pause_core(vm, y);
            break;
    case 3: pause_core(vm, vm->active);
            break;
    case 4: y = stack_pop(vm);
            resume_core(vm, y);
            break;
    case 5: y = stack_pop(vm);
            stack_push(vm, vm->cpu[vm->active].registers[y]);
            break;
    case 6: y = stack_pop(vm);
            z = stack_pop(vm);
            vm->cpu[vm->active].registers[y] = z;
            break;
  }
}

void query_multicore(NgaState *vm) {
  stack_push(vm, 0);
  stack_push(vm, 8000);  /* device type 8000 */
}
#endif

M vm/nga-c/retro.c => vm/nga-c/retro.c +2 -111
@@ 28,10 28,6 @@
#include <signal.h>
#endif

#ifdef ENABLE_FFI
#include <dlfcn.h>
#endif

#ifdef ENABLE_MULTICORE
#define CORES 8
#else


@@ 363,118 359,13 @@ void query_blocks(NgaState *vm) {

/* Multi Core Support ------------------------------------------------ */
#ifdef ENABLE_MULTICORE
void init_core(NgaState *vm, CELL x) {
  int y;
  vm->cpu[x].sp = 0;
  vm->cpu[x].rp = 0;
  vm->cpu[x].ip = 0;
  vm->cpu[x].active = 0;
  vm->cpu[x].u = 0;
  for (y = 0; y < STACK_DEPTH; y++) { vm->cpu[x].data[y] = 0; };
  for (y = 0; y < ADDRESSES; y++) { vm->cpu[x].address[y] = 0; };
  for (y = 0; y < 24; y++) { vm->cpu[x].registers[y] = 0; };
}

void start_core(NgaState *vm, CELL x, CELL ip) {
  vm->cpu[x].ip = ip;
  vm->cpu[x].rp = 1;
  vm->cpu[x].active = -1;
}

void pause_core(NgaState *vm, CELL x) {
  vm->cpu[x].active = 0;
}

void resume_core(NgaState *vm, CELL x) {
  vm->cpu[x].active = -1;
}

void switch_core(NgaState *vm) {
  vm->active += 1;
  if (vm->active >= CORES) { vm->active = 0; }
  if (!vm->cpu[vm->active].active) { switch_core(vm); }
}

void io_multicore(NgaState *vm) {
  int x, y, z;
  x = stack_pop(vm);
  switch(x) {
    case 0: y = stack_pop(vm);
            init_core(vm, y);
            break;
    case 1: y = stack_pop(vm);
            z = stack_pop(vm);
            start_core(vm, y, z);
            break;
    case 2: y = stack_pop(vm);
            pause_core(vm, y);
            break;
    case 3: pause_core(vm, vm->active);
            break;
    case 4: y = stack_pop(vm);
            resume_core(vm, y);
            break;
    case 5: y = stack_pop(vm);
            stack_push(vm, vm->cpu[vm->active].registers[y]);
            break;
    case 6: y = stack_pop(vm);
            z = stack_pop(vm);
            vm->cpu[vm->active].registers[y] = z;
            break;
  }
}

void query_multicore(NgaState *vm) {
  stack_push(vm, 0);
  stack_push(vm, 8000);  /* device type 8000 */
}
#include "dev-multicore.c"
#endif



/* External Functions ------------------------------------------------ */

#ifdef ENABLE_FFI
typedef void (*External)(void *);

void *handles[32];
External funcs[32000];
int nlibs, nffi;

void open_library(NgaState *vm) {
  handles[nlibs] = dlopen(string_extract(vm, stack_pop(vm)), RTLD_LAZY);
  stack_push(vm, nlibs);
  nlibs++;
}

void map_symbol(NgaState *vm) {
  int h;
  h = stack_pop(vm);
  char *s = string_extract(vm, stack_pop(vm));
  funcs[nffi] = dlsym(handles[h], s);
  stack_push(vm, nffi);
  nffi++;
}

void invoke(NgaState *vm) {
  funcs[stack_pop(vm)](vm);
}

void io_ffi(NgaState *vm) {
  switch (stack_pop(vm)) {
    case 0: open_library(vm); break;
    case 1: map_symbol(vm); break;
    case 2: invoke(vm); break;
  }
}

void query_ffi(NgaState *vm) {
  stack_push(vm, 0);
  stack_push(vm, 8100);  /* device type 8100 */
}
#include "dev-ffi.c"
#endif


#ifdef ENABLE_FLOATS
#include "dev-float.c"
#endif