commit f7f62c8ee1a3fc9a4dcd48748a5355e9b3103cf3
parent f4e58ce4ccf22ea7e101fa052cb52496addf1b3c
Author: prenev <an2qzavok@gmail.com>
Date: Sat, 9 Jul 2022 15:20:24 +0300
some bus code
Diffstat:
M | Makefile | | | 4 | ++++ |
A | bus.c | | | 33 | +++++++++++++++++++++++++++++++++ |
A | bus.h | | | 23 | +++++++++++++++++++++++ |
M | main.c | | | 11 | +++++++++++ |
4 files changed, 71 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,4 @@
+main: main.c
+ cc -o main main.c
+
+
diff --git a/bus.c b/bus.c
@@ -0,0 +1,33 @@
+#include "cell.h"
+#include "bus.h"
+
+Bus *
+createbus(void) {
+ Bus *new = calloc(sizeof(Bus), 1);
+ return new;
+}
+
+void
+buspush(Bus *bus, Msg *msg)
+{
+ /* TODO: free msg if it's not NULL before overwriting */
+ bus->buf[bus->n%BusBufMax] = msg;
+ bus->n++;
+}
+
+Msg *
+busspull(Bus *bus, int dst)
+{
+ int i;
+ Msg *msg = NULL:
+ for (i = 0; i < BusBufMax; i++) {
+ msg = bus->buf[i];
+ if (msg == NULL) continue;
+ if (msg->dst == dst) {
+ bus->buf[i] = NULL;
+ return msg;
+ }
+ }
+ return NULL;
+}
+
diff --git a/bus.h b/bus.h
@@ -0,0 +1,23 @@
+/* about passing messages around */
+
+#define BusBufMax 256
+
+typedef struct Bus Bus;
+typedef struct Msg Msg;
+
+struct Bus {
+ unsigned int n;
+ Msg * buf[BusBufMax];
+};
+
+Bus * createbus(void);
+void buspush(Bus *, Msg *);
+Msg * buspull(Bus *, int);
+
+struct Msg {
+ unsigned int src;
+ unsigned int dst;
+ Cell *dat;
+};
+
+Msg * createmsg(int, int, Cell *);
diff --git a/main.c b/main.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+#include "bus.h"
+
+int
+main(void)
+{
+ printf("blurb\n");
+ return 0;
+}
+