~alderwick/pico-uxn

ref: 48774b835fecadae6a18b0453ce1d90dbe73c090 pico-uxn/main.c -rw-r--r-- 1.1 KiB
48774b83Andrew Alderwick Build uxn-boot.h in base directory instead of build/. 8 months ago
                                                                                
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
#include <string.h>

#include "pico/multicore.h"
#include "pico/bootrom.h"

#pragma GCC diagnostic error "-Wpedantic"
#pragma GCC diagnostic error "-Wall"

#include "pico-uxn.h"
#include "uxn-boot.h"

UXN_INIT_DEFINITIONS

static void
nil_talk(Device *d, Uint8 b0, Uint8 w)
{
}

static void
sleep_callback(Uxn *u)
{
	__wfi();
}

static callback_t *callbacks[33];
static int n_callbacks = 0;

static volatile uint32_t waiting_callbacks;

int
add_main_loop_callback(callback_t *cb)
{
	callbacks[n_callbacks] = cb;
	return n_callbacks++;
}

void
trigger_main_loop_callback(int b)
{
	waiting_callbacks |= 0x80000000 >> b;
}

int
main(void)
{
	for(int i = 0; i < 33; ++i)
		callbacks[i] = sleep_callback;

	Uxn u;

	bootuxn(&u);
	memcpy(u.ram.dat + 0x100, uxn_boot_rom, sizeof(uxn_boot_rom));

	UXN_INIT_CALLS;

	for(int i = 0; i < 16; ++i)
		if(u.dev[i].talk == NULL)
			portuxn(&u, i, "empty", nil_talk);

	if(!evaluxn(&u, 0x100))
		reset_usb_boot(0, 0);

	for(;;) {
		int b = __builtin_clz(waiting_callbacks);
		if(b < 32) {
			waiting_callbacks &= ~(0x80000000 >> b);
		} else {
			UXN_TASKS;
		}
		callbacks[b](&u);
	}

	return 0;
}