commit efb7a2abce2772b736fa3bc27280b3165dae77c9
parent efe2699622b09eea984ebc4b659b86cfa5fa90fa
Author: prenev <an2qzavok@gmail.com>
Date: Mon, 11 Jul 2022 15:23:44 +0300
more mem code
Diffstat:
M | bus.h | | | 5 | +++++ |
M | cell.c | | | 13 | ++++++++++++- |
M | cell.h | | | 3 | ++- |
M | main.c | | | 17 | ++++++++++------- |
M | mem.c | | | 66 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
M | mem.h | | | 16 | ++++++++++++++++ |
6 files changed, 108 insertions(+), 12 deletions(-)
diff --git a/bus.h b/bus.h
@@ -2,6 +2,11 @@
#define BusBufMax 256
+enum {
+ ChDebug,
+ ChMem,
+};
+
typedef struct Bus Bus;
typedef struct Msg Msg;
diff --git a/cell.c b/cell.c
@@ -1,9 +1,10 @@
#include <stdlib.h>
+#include <string.h>
#include "cell.h"
Cell *
-createcell(int n, char *p)
+createcell(int n, void *p)
{
Cell *new = malloc(sizeof(Cell));
new->n = n;
@@ -11,6 +12,16 @@ createcell(int n, char *p)
return new;
}
+Cell *
+celldup(Cell *cell)
+{
+ Cell *dup = malloc(sizeof(Cell));
+ dup->n = cell->n;
+ dup->p = malloc(cell->n);
+ memcpy(dup->p, cell->p, cell->n);
+ return cell;
+}
+
void
freecell(Cell *cell)
{
diff --git a/cell.h b/cell.h
@@ -7,5 +7,6 @@ struct Cell {
char *p;
};
-Cell * createcell(int, char *);
+Cell * createcell(int, void *);
+Cell * celldup(Cell *);
void freecell(Cell *);
diff --git a/main.c b/main.c
@@ -9,14 +9,17 @@ int
main(void)
{
Bus *bus = createbus();
+ Mem *mem = createmem(bus);
Cell *cell = createcell(6, strdup("hello"));
- Msg *msg = createmsg(1, 0, cell);
- buspush(bus, msg);
-
- Mem mem = {bus};
- memstep((void *)&mem);
- Msg *ret = buspull(bus, 1);
- if (ret != NULL) printf("yep\n");
+ printf("cell: %s\n", cell->p);
+ memstore(ChDebug, mem, 1234, cell);
+ memstep(mem);
+ memfetch(ChDebug, mem, 1234);
+ memstep(mem);
+ Msg *ret = buspull(bus, ChDebug);
+ if (ret != NULL) {
+ printf("yes: %s\n", ret->dat->p);
+ }
else printf("nope\n");
freemsg(ret);
return 0;
diff --git a/mem.c b/mem.c
@@ -1,19 +1,79 @@
#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
#include "cell.h"
#include "bus.h"
#include "mem.h"
+Mem *
+createmem(Bus *bus)
+{
+ Mem *new = calloc(sizeof(Mem), 1);
+ new->bus = bus;
+ return new;
+}
+
int
memstep(void *v)
{
Mem *mem = (Mem *)v;
+ Msg *ret, *msg = buspull(mem->bus, ChMem);
+ Cell *ram, *cell;
- Msg *msg = buspull(mem->bus, 0);
if (msg == NULL) return 1;
- Msg *ret = createmsg(0, msg->src, createcell(0, NULL));
- buspush(mem->bus, ret);
+
+ if ((msg->dat == NULL)
+ || (msg->dat->n < sizeof(MemCmd))) {
+ freemsg(msg);
+ fprintf(stderr, "mem: cmd too short\n");
+ return 0;
+ }
+ MemCmd *cmd = (MemCmd *)msg->dat->p;
+ if ((cmd->addr < 0) || (cmd->addr >= MaxRAM)) {
+ fprintf(stderr, "mem: invalid addr: %x\n",
+ cmd->addr);
+ } else switch (cmd->op) {
+ case '!': //fetch
+ ram = &mem->ram[cmd->addr];
+ cell = celldup(ram);
+ ret = createmsg(ChMem, msg->src, cell);
+ buspush(mem->bus, ret);
+ break;
+ case '@': //store
+ ram = &mem->ram[cmd->addr];
+ ram->n = msg->dat->n - sizeof(MemCmd);
+ ram->p = realloc(ram->p, ram->n);
+ memcpy(ram->p, msg->dat->p + sizeof(MemCmd),
+ ram->n);
+ break;
+ default:
+ fprintf(stderr, "mem: invalid op: %c\n", cmd->op);
+ }
freemsg(msg);
return 0;
}
+void
+memfetch(int src, Mem *mem, int n)
+{
+ MemCmd *f = malloc(sizeof(MemCmd));
+ f->op = '!';
+ f->addr = n;
+ Cell *c = createcell(sizeof(MemCmd), f);
+ Msg *msg = createmsg(src, ChMem, c);
+ buspush(mem->bus, msg);
+}
+
+void
+memstore(int src, Mem *mem, int n, Cell *c)
+{
+ MemCmd *s = malloc(sizeof(MemCmd) + c->n);
+ s->op = '@';
+ s->addr = n;
+ memcpy((char *)s + sizeof(MemCmd), c->p, c->n);
+ Cell *cell = createcell(sizeof(MemCmd) + c->n, s);
+ Msg *msg = createmsg(src, ChMem, cell);
+ buspush(mem->bus, msg);
+}
+
diff --git a/mem.h b/mem.h
@@ -1,9 +1,25 @@
/* concerning the memory management */
+/* currently memory structure is a simple
+ array of Cells. */
+
+#define MaxRAM 4096
+
typedef struct Mem Mem;
+typedef struct MemCmd MemCmd;
struct Mem {
Bus *bus;
+ Cell ram[MaxRAM];
};
+struct MemCmd {
+ char op;
+ int addr;
+};
+
+Mem * createmem(Bus *);
int memstep(void *);
+
+void memfetch(int, Mem *, int);
+void memstore(int, Mem *, int, Cell *);