aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2023-04-30 16:03:55 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2023-04-30 16:03:55 +0300
commit191eaea25cf569ad9c5fb12b8755ae6400750e58 (patch)
treea0cd14f4a772da01b445355eb35eec54616d01ec /include
downloadgran-191eaea25cf569ad9c5fb12b8755ae6400750e58.tar.gz
gran-191eaea25cf569ad9c5fb12b8755ae6400750e58.zip
initial
Diffstat (limited to 'include')
-rw-r--r--include/gran/clock_domain.h40
-rw-r--r--include/gran/common.h34
-rw-r--r--include/gran/component.h107
-rw-r--r--include/gran/components/bus/simple_bus.h10
-rw-r--r--include/gran/components/mem/simple_mem.h8
-rw-r--r--include/gran/root.h14
6 files changed, 213 insertions, 0 deletions
diff --git a/include/gran/clock_domain.h b/include/gran/clock_domain.h
new file mode 100644
index 0000000..3f960ae
--- /dev/null
+++ b/include/gran/clock_domain.h
@@ -0,0 +1,40 @@
+#ifndef EXSIM_CLOCK_DOMAIN_H
+#define EXSIM_CLOCK_DOMAIN_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include <gran/component.h>
+
+typedef uint64_t tick;
+
+struct clock_time {
+ uint64_t s;
+ uint64_t fs;
+};
+
+#define MAX_CLOCK (struct clock_time){-1, -1}
+
+#define FS(x) (x)
+#define PS(x) (1000ULL * FS(x))
+#define NS(x) (1000ULL * PS(x))
+#define US(x) (1000ULL * NS(x))
+#define MS(x) (1000ULL * US(x))
+#define SEC(x) (1000ULL * MS(x))
+
+struct clock_domain;
+
+struct clock_domain *create_clock_domain(tick ps);
+void destroy_clock_domain(struct clock_domain *);
+
+stat clock_domain_add(struct clock_domain *, struct component *);
+stat clock_domain_tick(struct clock_domain *);
+
+bool eq_time(struct clock_time a, struct clock_time b);
+bool lt_time(struct clock_time a, struct clock_time b);
+bool le_time(struct clock_time a, struct clock_time b);
+
+struct clock_time max_time(struct clock_time a, struct clock_time b);
+struct clock_time domain_time(struct clock_domain *);
+
+#endif /* EXSIM_CLOCK_DOMAIN_H */
diff --git a/include/gran/common.h b/include/gran/common.h
new file mode 100644
index 0000000..c012c49
--- /dev/null
+++ b/include/gran/common.h
@@ -0,0 +1,34 @@
+#ifndef EXSIM_COMMON_H
+#define EXSIM_COMMON_H
+
+#include <stdio.h>
+
+typedef enum {
+ OK = 0,
+ DONE,
+ EBUS,
+ EBUSY,
+ EMEM,
+ ENOMEM,
+ EEXISTS,
+ ENOSUCH,
+ ESIZE,
+} stat;
+
+#define error(x, ...) \
+ do {fprintf(stderr, "error: " x "\n",##__VA_ARGS__);} while(0)
+
+#define warn(x, ...) \
+ do {fprintf(stderr, "warn: " x "\n",##__VA_ARGS__);} while(0)
+
+#define info(x, ...) \
+ do {fprintf(stderr, "info: " x "\n",##__VA_ARGS__);} while(0)
+
+#if DEBUG
+#define debug(x, ...) \
+ do {fprintf(stderr, "debug: " x "\n",##__VA_ARGS__);} while(0)
+#else
+#define debug(x, ...)
+#endif
+
+#endif /* EXSIM_COMMON_H */
diff --git a/include/gran/component.h b/include/gran/component.h
new file mode 100644
index 0000000..cd21a6b
--- /dev/null
+++ b/include/gran/component.h
@@ -0,0 +1,107 @@
+#ifndef EXSIM_COMPONENT_H
+#define EXSIM_COMPONENT_H
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+
+#include <gran/common.h>
+
+struct component;
+
+typedef stat (*write_callback)(struct component *, uintptr_t, size_t, void *);
+typedef stat (*read_callback)(struct component *, uintptr_t, size_t, void *);
+typedef stat (*swap_callback)(struct component *, uintptr_t, size_t, void *,
+ size_t, void *);
+typedef stat (*irq_callback)(struct component *, int);
+typedef stat (*clock_callback)(struct component *);
+
+typedef stat (*stat_callback)(struct component *, FILE *);
+typedef stat (*dts_callback)(struct component *, FILE *);
+
+typedef void (*destroy_callback)(struct component *);
+
+struct component {
+ /* optional */
+ char *name;
+
+ write_callback write;
+ read_callback read;
+ swap_callback swap;
+
+ irq_callback irq;
+ clock_callback clock;
+
+ dts_callback dts;
+ stat_callback stat;
+
+ destroy_callback destroy;
+};
+
+static inline stat write(struct component *component, uintptr_t addr,
+ size_t size, void *buf)
+{
+ if (!component->write) {
+ error(
+ "tried writing %p:%" PRIuPTR " but it doesn't support writing",
+ component->name, addr);
+ return ENOSUCH;
+ }
+
+ return component->write(component, addr, size, buf);
+}
+
+static inline stat read(struct component *component, uintptr_t addr,
+ size_t size, void *buf)
+{
+ if (!component->read) {
+ error(
+ "tried reading %p:%" PRIuPTR " but it doesn't support reading",
+ component->name, addr);
+ return ENOSUCH;
+ }
+
+ return component->read(component, addr, size, buf);
+}
+
+static inline stat swap(struct component *component, uintptr_t addr,
+ size_t wsize, void *wbuf, size_t rsize, void *rbuf)
+{
+ if (!component->swap) {
+ error(
+ "tried swapping %p:%" PRIuPTR " but it doesn't support swapping",
+ component->name, addr);
+ return ENOSUCH;
+ }
+
+ return component->swap(component, addr, wsize, wbuf, rsize, rbuf);
+}
+
+static inline void destroy(struct component *component)
+{
+ if (component->destroy)
+ component->destroy(component);
+ else
+ free(component);
+}
+
+static inline stat write_u8(struct component *component, uintptr_t addr,
+ uint8_t c)
+{
+ return write(component, addr, sizeof(uint8_t), &c);
+}
+
+static inline stat read_u8(struct component *component, uintptr_t addr,
+ uint8_t *c)
+{
+ return read(component, addr, sizeof(uint8_t), c);
+}
+
+static inline stat swap_u8(struct component *component, uintptr_t addr,
+ uint8_t *c)
+{
+ return swap(component, addr, sizeof(uint8_t), c, sizeof(uint8_t), c);
+}
+
+#endif /* EXSIM_COMPONENT_H */
diff --git a/include/gran/components/bus/simple_bus.h b/include/gran/components/bus/simple_bus.h
new file mode 100644
index 0000000..3a5f814
--- /dev/null
+++ b/include/gran/components/bus/simple_bus.h
@@ -0,0 +1,10 @@
+#ifndef EXSIM_SIMPLE_BUS_H
+#define EXSIM_SIMPLE_BUS_H
+
+#include <stdint.h>
+
+struct component *create_simple_bus();
+stat simple_bus_add(struct component *bus, struct component *component,
+ uintptr_t addr, size_t size);
+
+#endif /* EXSIM_SIMPLE_BUS_H */
diff --git a/include/gran/components/mem/simple_mem.h b/include/gran/components/mem/simple_mem.h
new file mode 100644
index 0000000..8e0845c
--- /dev/null
+++ b/include/gran/components/mem/simple_mem.h
@@ -0,0 +1,8 @@
+#ifndef EXSIM_SIMPLE_MEM_H
+#define EXSIM_SIMPLE_MEM_H
+
+#include <gran/component.h>
+
+struct component *create_simple_mem(size_t size);
+
+#endif /* EXSIM_SIMPLE_MEM_H */
diff --git a/include/gran/root.h b/include/gran/root.h
new file mode 100644
index 0000000..d158b04
--- /dev/null
+++ b/include/gran/root.h
@@ -0,0 +1,14 @@
+#ifndef EXSIM_ROOT_H
+#define EXSIM_ROOT_H
+
+#include <gran/common.h>
+#include <gran/clock_domain.h>
+
+struct gran_root;
+
+struct gran_root *create_root();
+stat root_add_clock(struct gran_root *, struct clock_domain *);
+stat root_run(struct gran_root *);
+void destroy_root(struct gran_root *);
+
+#endif /* EXSIM_ROOT_H */