commit f4b4e02a65bc63d5eff03cc76fe7da43a369856d
parent 9aa5f22ab7c94e120b62bb59015924bd89c2c535
Author: glenda <glenda@9front.local>
Date: Fri, 11 Feb 2022 21:48:22 +0000
map boilerplate funcions for future new memory model
Diffstat:
A | mem.c | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | mkfile | | | 8 | +++++--- |
M | vsm.c | | | 33 | +++++++++++++++++++++++++-------- |
M | vsm.h | | | 16 | ++++++++++++++++ |
4 files changed, 96 insertions(+), 11 deletions(-)
diff --git a/mem.c b/mem.c
@@ -0,0 +1,50 @@
+#include <u.h>
+#include <libc.h>
+
+#include "vsm.h"
+
+int
+_mapget(int id, Mapentry **m)
+{
+ /* TODO: binary search */
+ int i;
+ for (i = 0; (i < mapsize) && (map[i].id <= id); i++) {
+ if (map[i].id == id) {
+ *m = map + i;
+ return i;
+ }
+ }
+ *m = nil;
+ return i;
+}
+
+void *
+mapget(int id)
+{
+ Mapentry *m;
+ _mapget(id, &m);
+ if (m != nil) return m->vp;
+ return nil;
+}
+
+void
+mapset(int id, void *vp)
+{
+ int n;
+ Mapentry *m;
+
+ n = _mapget(id, &m);
+
+ if (m != nil) {
+ m->vp = vp;
+ return;
+ }
+
+ mapsize++;
+ map = realloc(map, sizeof(Mapentry) * mapsize);
+ memcpy(map + n, map + n + 1, sizeof(Mapentry) * (mapsize - n - 1));
+ map[n].id = id;
+ map[n].vp = vp;
+
+ return;
+}
diff --git a/mkfile b/mkfile
@@ -1,10 +1,12 @@
</$objtype/mkfile
-TARG=run\
- asm
+TARG=\
+ run \
+ asm \
OFILES=\
- vsm.$O\
+ mem.$O \
+ vsm.$O \
HFILES=vsm.h
diff --git a/vsm.c b/vsm.c
@@ -82,19 +82,27 @@ void i_FROMR(void)
/* Memory Management: */
void i_STORE(void)
{
- ram[*dsp] = *(dsp - 1);
+ mapset(*dsp, dsp - 1);
dsp -= 2;
}
void i_FETCH(void)
{
- *dsp = ram[*dsp];
+ CELL *r;
+ r = mapget(*dsp);
+ if (r != nil) *dsp = *r;
+ else *dsp = 0;
}
/* Literals: */
void i_LIT(void)
{
- *(dsp + 1) = ram[pc + 1];
+ CELL *r;
+ r = mapget(pc + 1);
+ if (r != nil) *(dsp + 1) = *r;
+ else {
+ *(dsp + 1) = 0;
+ }
dsp++;
pc++;
}
@@ -161,8 +169,6 @@ instruction instructions[128] = {
void
vsm_reset(void)
{
- //CELL i;
- //for (i = 0; i < IMAGE_SIZE; i++) ram[i] = VSM_NOP;
pc = 0;
dsp = dstack;
rsp = rstack;
@@ -194,9 +200,12 @@ vsm_run(void)
fprint(2, "pc reached end of the image\n");
return;
}
+
op = ram[pc];
+
+
if (op >= NUM_OPS) {
- fprint(2, "invalid op == %x\n", op);
+ fprint(2, "invalid op == %d\n", op);
return;
}
vsm_exec(op);
@@ -214,11 +223,19 @@ vsm_readimage(char *path)
if (fd <= 0) return -1;
mp = ram;
while((n = read(fd, buf, 64)) > 0){
+ if (mp + n/sizeof(CELL) >= ram + IMAGE_SIZE)
+ sysfatal("file bigger than image size");
+
memcpy(mp, buf, n);
mp += n/sizeof(CELL);
- if (mp >= ram + IMAGE_SIZE)
- sysfatal("file bigger than image size");
}
close(fd);
+
+ int i;
+ for (i = 0; i < IMAGE_SIZE; i++) {
+ if (ram[i] == 0) break;
+ mapset(i, ram + i);
+ }
+
return 0;
}
diff --git a/vsm.h b/vsm.h
@@ -3,6 +3,22 @@
#define STACK_DEPTH 512
typedef void (*instruction)(void);
+/* Memory */
+
+typedef struct Mapentry Mapentry;
+
+struct Mapentry {
+ int id;
+ void *vp;
+};
+
+Mapentry *map;
+usize mapsize;
+
+int _mapget(int, Mapentry **);
+void * mapget(int);
+void mapset(int, void *);
+
/* Operators */
enum {