-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkabel.c
134 lines (118 loc) · 3.45 KB
/
kabel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "dyn_arr.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KABEL_PRIV
#include "kabel.h"
#define dbg(f, x) fprintf(stderr, #x ": " f "\n", x);
void* alloc(size_t size){
void* data = malloc(size);
if(!data) {
perror("alloc");
abort();
}
return data;
}
KabelHdr* kabel_hdr(void* kabel){
return (KabelHdr*)((u8*)kabel - sizeof(KabelHdr));
}
KSim* new_ksim(){
KSim* out = alloc(sizeof(KSim));
*out = (KSim){
.cur_list = 0,
.todolist = {0},
};
return out;
}
void ksim_schedule_update(KSim *sim, KabelHdr *hdr){
TodoList *lst = &sim->todolist[sim->cur_list];
// NOLINTBEGIN array of pointers needs to sizeof a pointer
apush(lst, hdr);
// NOLINTEND
}
bool ksim_step(KSim* sim){
TodoList *lst = &sim->todolist[sim->cur_list];
sim->cur_list = !sim->cur_list;
for (size_t i = 0; i < lst->len; i++) {
KabelHdr *h = aget(lst, i);
for (size_t i = 0; i < h->on_change.len; i++) {
Update u = aget(&h->on_change, i);
u.update(sim, u.captured);
}
}
lst->len = 0;
return sim->todolist[sim->cur_list].len != 0;
}
void* new_kabel_impl(KSim *sim, size_t size){
KabelHdr *hdr = alloc(sizeof(KabelHdr) + size);
memset(hdr, 0, sizeof(*hdr));
hdr->data_size = size;
hdr->on_change = (ArrOfUpdate){0};
void* kabel = (u8*)hdr + sizeof(KabelHdr);
memset(kabel, 0, size);
ksim_schedule_update(sim, hdr);
return kabel;
}
void kabel_watch(void* kabel, Update fn){
KabelHdr *header = kabel_hdr(kabel);
apush(&header->on_change, fn);
}
void kabel_write_ptr(KSim *sim, void* kabel, void* data){
KabelHdr *hdr = kabel_hdr(kabel);
size_t size = hdr->data_size;
if(memcmp(kabel, data, size)!=0){
memcpy(kabel, data, size);
bool found = 0;
TodoList *lst = &sim->todolist[sim->cur_list];
for (size_t i = 0; i < lst->len; i++) {
if(aget(lst, i) == hdr){
found = 1;
break;
}
}
if(!found){
ksim_schedule_update(sim, hdr);
}
}
}
static void b_not_update(KSim *sim, void* cap){
struct{bool *in; bool *out;} *data = cap;
bool val = !*data->in;
kabel_write_ptr(sim, data->out, &val);
}
void b_not(bool *in, bool *out){
struct{bool *in; bool *out;} *data;
data = malloc(sizeof(*data));
data->in = in;
data->out = out;
kabel_watch(in, (Update){b_not_update, data});
}
static void b_wire_update(KSim *sim, void* cap){
struct{bool *in; bool *out;} *data = cap;
kabel_write_ptr(sim, data->out, data->in);
}
void b_wire(bool *in, bool *out){
struct{bool *in; bool *out;} *data;
data = malloc(sizeof(*data));
data->in = in;
data->out = out;
kabel_watch(in, (Update){b_not_update, data});
}
#define GATE2(name, op) \
static void b_ ## name ## _update(KSim *sim, void* cap){ \
struct{bool *a; bool *b; bool *out;} *data = cap; \
bool val = (*data->a) op (*data->b); \
kabel_write_ptr(sim, data->out, &val); \
} \
void b_ ## name (bool *a, bool *b, bool *out) { \
struct{bool *a; bool *b; bool *out;} *data; \
data = malloc(sizeof(*data)); \
data->a = a; \
data->b = b; \
data->out = out; \
kabel_watch(a, (Update){b_ ## name ## _update, data}); \
kabel_watch(b, (Update){b_ ## name ## _update, data}); \
}
GATE2(and, &&)
GATE2(or, ||)
GATE2(xor, !=)