From 191eaea25cf569ad9c5fb12b8755ae6400750e58 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sun, 30 Apr 2023 16:03:55 +0300 Subject: initial --- src/root.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/root.c (limited to 'src/root.c') diff --git a/src/root.c b/src/root.c new file mode 100644 index 0000000..4324437 --- /dev/null +++ b/src/root.c @@ -0,0 +1,65 @@ +#include + +#include +#include + +#define MAX_DOMAINS 512 + +struct gran_root { + size_t num_domains; + struct clock_domain *(domains[MAX_DOMAINS]); +}; + +struct gran_root *create_root() +{ + return calloc(1, sizeof(struct gran_root)); +} + +stat root_add_clock(struct gran_root *root, struct clock_domain *clk) +{ + if (root->num_domains == MAX_DOMAINS) { + error("too many clock domains"); + return ESIZE; + } + + root->domains[root->num_domains++] = clk; + return OK; +} + +static struct clock_domain *most_delayed_domain(struct gran_root *root) +{ + struct clock_domain *min = root->domains[0]; + + for (size_t i = 1; i < root->num_domains; ++i) { + struct clock_domain *cur = root->domains[i]; + if (lt_time(domain_time(cur), domain_time(min))) + min = cur; + } + + return min; +} + +stat root_run(struct gran_root *root) +{ + if (root->num_domains == 0) { + info("no clock domains added to root, exiting"); + return OK; + } + + stat ret = OK; + while (ret == OK) + ret = clock_domain_tick(most_delayed_domain(root)); + + if (ret == DONE) + return OK; + + return ret; +} + +void destroy_root(struct gran_root *root) +{ + for (size_t i = 0; i < root->num_domains; ++i) + destroy_clock_domain(root->domains[i]); + + free(root); +} -- cgit v1.3