-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclock.c
51 lines (44 loc) · 990 Bytes
/
clock.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
#include "clock.h"
#include "glue.h"
#include "mmu.h"
#include "shifter.h"
struct delay {
unsigned clock;
void (*callback)(void);
struct delay *next;
};
static struct delay *first_delay = NULL;
void clock_step(void)
{
while(first_delay != NULL && first_delay->clock == 0) {
first_delay->callback();
free(first_delay);
first_delay = first_delay->next;
}
if(first_delay != NULL)
first_delay->clock--;
glue_clock();
mmu_clock();
shifter_clock();
}
static void insert(struct delay **list, struct delay *elt)
{
if(*list == NULL)
*list = elt;
else if(elt->clock < (*list)->clock) {
elt->next = *list;
*list = elt;
elt->next->clock -= elt->clock;
} else {
elt->clock -= (*list)->clock;
insert(&(*list)->next, elt);
}
}
void clock_delay(unsigned cycles, void (*callback)(void))
{
struct delay *d = xmalloc(sizeof(struct delay));
d->clock = cycles;
d->callback = callback;
d->next = NULL;
insert(&first_delay, d);
}