summaryrefslogtreecommitdiff
path: root/arm9/source/main.c
blob: 56103da5464309fa946ed4511cb9877d71b6a63f (plain) (blame)
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
#include <stdio.h>
#include <time.h>
#include <nds.h>
#include "devices/clock.h"
#include "devices/math.h"
#include "devices/screen.h"
#include "bang.h"
#include "core.h"
#include "main.h"

#define NUM_BR 5

Bedrock br[NUM_BR] = {};
Bedrock *br_main;
Bedrock *br_sub;
bool main_on_bottom = TRUE;

char *system_name = "bedrock-nds/0.1.0-alpha";
char *system_authors = "Ben Bridle";

u8 main_program[] = {
    #include "../include/cobalt.br.inc"
};
u8 keyboard_program[] = {
    #include "../include/keyboard.br.inc"
};


// Change to the next screen layout.
void change_layout(void) {
    lcdSwap();
    main_on_bottom = !main_on_bottom;
    if (main_on_bottom) {
        scr_unmake(&br_sub->scr);
    } else {
        scr_make_sub(&br_sub->scr);
    }
}

void receive_keyboard_byte(u8 byte) {
    if (br_main) {
        inp_receive_byte(&br_main->inp, byte);
    }
}

bool is_keyboard_open(void) {
    return !main_on_bottom;
}

void open_keyboard(void) {
    if (main_on_bottom) {
        change_layout();
    }
}

void close_keyboard(void) {
    if (!main_on_bottom) {
        change_layout();
    }
}

// Scan for input and handle emulator-specific keys.
void receive_input(void) {
    scanKeys();
    if (keysDown() & KEY_SELECT) change_layout();
}


int main(void) {
    #define ALIVE(br)  (br && br->alive)
    #define DEAD(br)   (br && !br->alive)
    #define AWAKE(br)  (br && br->alive &&  br->awake)
    #define ASLEEP(br) (br && br->alive && !br->awake)

    // Set memory identifiers and system device strings.
    for (int i=0; i<NUM_BR; i++) {
        br[i].mem.id = i+1;
        br[i].sys.name.mem = (u8*) &system_name[0];
        br[i].sys.authors.mem = (u8*) &system_authors[0];
    }

    init_screens();
    init_clock();
    init_filesystem();
    lcdMainOnBottom();

    // Load program
    start_br(&br[0], main_program, sizeof(main_program));
    start_br(&br[1], keyboard_program, sizeof(keyboard_program));
    br_main = &br[0];
    br_sub = &br[1];
    scr_make_main(&br_main->scr);

    while (1) {
        if (AWAKE(br_main)) run_br(br_main);
        if (AWAKE(br_sub))  run_br(br_sub);
        if (DEAD(br_main)) br_main = NULL;
        if (DEAD(br_sub))  br_sub  = NULL;

        bool main_flip = ASLEEP(br_main) && br_main->scr.dirty;
        bool sub_flip = ASLEEP(br_sub) && br_sub->scr.dirty;
        if (main_flip || sub_flip) swiWaitForVBlank();
        if (main_flip) { flip_buffer(br_main->scr.nds); br_main->scr.dirty = false; }
        if (sub_flip)  { flip_buffer(br_sub->scr.nds);  br_sub->scr.dirty  = false; }

        rouse:
        receive_input();
        if (ALIVE(br_main) && main_on_bottom) {
            inp_read_navigation(&br_main->inp);
            inp_read_gamepad(&br_main->inp);
            inp_read_touch(&br_main->inp);
        }
        if (ALIVE(br_sub) && !main_on_bottom) {
            inp_read_navigation(&br_sub->inp);
            inp_read_gamepad(&br_sub->inp);
            inp_read_touch(&br_sub->inp);
        }
        if (ASLEEP(br_main)) rouse_br(br_main);
        if (ASLEEP(br_sub))  rouse_br(br_sub);
        if (ASLEEP(br_main) && ASLEEP(br_sub)) {
            // Sleep until next keypad event or clock tick.
            swiIntrWait(1, IRQ_KEYS | IRQ_TIMER0);
            goto rouse;
        }

        if (!br_main) {
            systemShutDown();
            return 0;
        }
    }
}