forked from jdah/jdh-8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.c
54 lines (43 loc) · 1.21 KB
/
mod.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
#include "emu.h"
struct Module {
const char *name;
void (*init)(struct JDH8*, struct Device*);
};
#define MODULES_MAX 128
static usize num_modules = 0;
static struct Module modules[MODULES_MAX];
#define MOD_DECL(_name) do { \
extern void _name##_init(struct JDH8 *, struct Device*);\
modules[num_modules++] = (struct Module) { \
.name = #_name, \
.init = _name##_init \
}; \
} while (0);
void mod_init() {
MOD_DECL(screen);
MOD_DECL(kb);
}
usize mod_list(const char **names, usize n) {
usize i = 0;
for (; i < num_modules && i < n; i++) {
names[i] = modules[i].name;
}
return i;
}
int mod_load(struct JDH8 *state, const char *name) {
usize i;
struct Module *mod;
for (i = 0; i < num_modules; i++) {
mod = &modules[i];
if (!strcasecmp(name, mod->name)) {
break;
}
}
if (i == num_modules) {
return -1;
}
struct Device dev;
mod->init(state, &dev);
add_device(state, dev);
return 0;
}