lli

A small emulated asm like lang
Log | Files | Refs

commit 07cb2d7cd9b825c87aec47207073f793be320883
parent 79af4d94d841d4607bf1a84e88fcad239dfe8567
Author: thing1 <l.standen@posteo.com>
Date:   Sat,  4 Oct 2025 22:17:21 +0100

added proper core support via dlopen and dlsym

Diffstat:
M.gitignore | 1+
MMakefile | 12++++++++----
Acores/Makefile | 8++++++++
Mcores/unix_core.c | 4++--
Dcores/unix_core.h | 13-------------
Mlli.c | 34++++++++++++++++++++++++++++++----
6 files changed, 49 insertions(+), 23 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ lli lliasm *.rom +*.so diff --git a/Makefile b/Makefile @@ -1,11 +1,13 @@ -CFLAGS=-O0 +CFLAGS=-O0 -ggdb -all: lli lliasm +all: lli lliasm cores -lli: lli.c cores/unix_core.c - cc lli.c cores/unix_core.c -o lli ${CFLAGS} +lli: lli.c + cc lli.c -o lli ${CFLAGS} -ldl lliasm: lliasm.c cc lliasm.c -o lliasm ${CFLAGS} +cores: cores/* + cd cores && make install: all cp lli lliasm /usr/local/bin @@ -16,3 +18,5 @@ uninstall: clean: rm -rf lli lliasm *.rom + cd cores && make clean + diff --git a/cores/Makefile b/cores/Makefile @@ -0,0 +1,8 @@ +CFLAGS=-ggdb -fPIC -shared + +all: unix + +unix: unix_core.c + cc unix_core.c -o unix_core.so ${CFLAGS} +clean: + rm -rf *.so diff --git a/cores/unix_core.c b/cores/unix_core.c @@ -10,7 +10,7 @@ */ void -onwrite_unix_memio(char *addr) { +onwrite(char *addr) { if (*(addr + 1)) { putc(*(addr + 1), stdout); *(addr + 1) = 0; @@ -22,7 +22,7 @@ onwrite_unix_memio(char *addr) { } void -onread_unix_memio(const char *addr, char *ptr) { +onread(char *addr, char *ptr) { if (ptr == addr) { *ptr = getc(stdin); } diff --git a/cores/unix_core.h b/cores/unix_core.h @@ -1,13 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -/* table of memory - * | function | size | - * ------------------ - * | stdin | 1 | - * | stdout | 1 | - * | stderr | 1 | - */ - -void onwrite_unix_memio(const char *addr); -void onread_unix_memio(const char *addr, const char *ptr); diff --git a/lli.c b/lli.c @@ -2,11 +2,11 @@ #include <stdlib.h> #include <string.h> #include <stdbool.h> +#include <dlfcn.h> #include "arg.h" char *argv0; #include "lli.h" -#include "cores/unix_core.h" #define mkpop(type) \ type pop_##type(stack *s) {\ @@ -48,6 +48,11 @@ mkpush(uint32_t); mkpush(uint16_t); mkpush(uint8_t); +void *corehandles[8] = {0}; +void (*onread[8])(char *, char *); +void (*onwrite[8])(char *); +int address[8] = {0}; +int corecount = 0; char memory[INT32_MAX]; char *pc = &memory[0x1000]; @@ -112,7 +117,8 @@ step: case LOADS: case LOADI: loc = pop_uint32_t(&s); - onread_unix_memio(&memory[0x0], &memory[loc]); + for (int i = 0; i < corecount; i++) + onread[i](&memory[address[i]], &memory[loc]); switch (opcode) { case LOADB: push_uint8_t(&s, (void*)&memory[loc]); break; case LOADS: push_uint16_t(&s, (void*)&memory[loc]); break; @@ -138,7 +144,8 @@ step: memcpy(&memory[loc], &val32, sizeof(uint32_t)); break; } - onwrite_unix_memio(&memory[0x0]); + for (int i = 0; i < corecount; i++) + onwrite[i](&memory[address[i]]); break; case DUPB: @@ -295,7 +302,7 @@ end: void usage() { - fprintf(stderr, "usage: %s [-dh] file\n", argv0); + fprintf(stderr, "usage: %s [-dh] [-c core address] file\n", argv0); exit(1); } @@ -306,6 +313,25 @@ main(int argc, char **argv) { ARGBEGIN { case 'd': debug = true; break; case 'h': usage(); break; + case 'c': + corehandles[corecount] = dlopen(EARGF(usage()), RTLD_LAZY); + if (!corehandles[corecount]) { + fprintf(stderr, "%s\n", dlerror()); + exit(1); + } + address[corecount] = strtol(EARGF(usage()), NULL, 0); + if (!(onread[corecount] = dlsym(corehandles[corecount], "onread"))) { + fprintf(stderr, "failed to open function: onread\n"); + exit(1); + } + if (!(onwrite[corecount] = dlsym(corehandles[corecount], "onwrite"))) { + fprintf(stderr, "failed to open function: onwrite\n"); + exit(1); + } + corecount++; + argv++; + + break; default: usage(); } ARGEND;