aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 06d2d5b5dfda79b351fb5166392b6e6857f4ab18 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
Copyright (c) 2021 Bad Diode

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

#include <string.h>

#include "common.h"
#include "bitmap.h"
#include "filesystem.c"

#include "rom.c"
#include "uxn.c"
#include "ppu.c"
#include "apu.c"
#include "text.h"

//
// Config parameters.
//

#if !defined(TEXT_MODE) || TEXT_MODE == 0
#define TEXT_LAYER ppu.fg
#else
#define TEXT_LAYER ppu.bg
#endif

#ifndef CONTROL_METHODS
#define CONTROL_METHODS CONTROL_CONTROLLER,CONTROL_MOUSE,CONTROL_KEYBOARD
#endif

#ifdef PROF_ENABLE
#if PROF_ENABLE == 0
#define PROF(F,VAR) (profile_start(),(F),(VAR) = profile_stop())
#elif PROF_ENABLE == 1
#define PROF(F,VAR) (profile_start(),(F),(VAR) = MAX(profile_stop(), (VAR)))
#endif
#ifndef PROF_SHOW_X
#define PROF_SHOW_X 0
#endif
#ifndef PROF_SHOW_Y
#define PROF_SHOW_Y 0
#endif
#define PROF_SHOW() \
    do { \
        txt_position((PROF_SHOW_X), (PROF_SHOW_Y));\
        txt_printf("INPUT: %lu    ", input_cycles);\
        txt_position((PROF_SHOW_X), (PROF_SHOW_Y)+1);\
        txt_printf("EVAL:  %lu    ", eval_cycles);\
        txt_position((PROF_SHOW_X), (PROF_SHOW_Y)+2);\
        txt_printf("FLIP:  %lu    ", flip_cycles);\
        txt_position((PROF_SHOW_X), (PROF_SHOW_Y)+3);\
        txt_printf("MIX:   %lu    ", mix_cycles);\
    } while (0)
#define PROF_INIT() \
    u32 flip_cycles = 0;\
    u32 eval_cycles = 0;\
    u32 input_cycles = 0;\
    u32 mix_cycles = 0;
#else
#define PROF(F,VAR) (F)
#define PROF_SHOW()
#define PROF_INIT()
#endif

typedef enum {
    CONTROL_CONTROLLER,
    CONTROL_MOUSE,
    CONTROL_KEYBOARD,
} ControlMethod;

const ControlMethod ctrl_methods[] = {
    CONTROL_METHODS
};
static ControlMethod ctrl_idx = 0;

#define MOUSE_DELTA 1
typedef struct Mouse {
    int x;
    int y;
} Mouse;

static Ppu ppu;
static Device *devscreen;
static Device *devctrl;
static Device *devmouse;
static Device *devaudio;

static Mouse mouse = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};

void
nil_talk(Device *d, u8 b0, u8 w) {
    (void)d;
    (void)b0;
    (void)w;
}

void
console_talk(Device *d, u8 b0, u8 w) {
    char stmp[2];
    if(!w) {
        return;
    }
    switch(b0) {
        case 0x8: stmp[0] = d->dat[0x8]; stmp[1] = 0; txt_printf(stmp); break;
        case 0x9: txt_printf("0x%02x", d->dat[0x9]); break;
        case 0xb: txt_printf("0x%04x", mempeek16(d->dat, 0xa)); break;
        case 0xd: txt_printf("%s", &d->mem[mempeek16(d->dat, 0xc)]); break;
    }
}

void
system_talk(Device *d, u8 b0, u8 w) {
    if(!w) {
        d->dat[0x2] = d->u->wst.ptr;
        d->dat[0x3] = d->u->rst.ptr;
    } else {
        putcolors(&d->dat[0x8]);
    }
    (void)b0;
}

IWRAM_CODE
void
screen_talk(Device *d, u8 b0, u8 w) {
    if(w && b0 == 0xe) {
        u16 x = mempeek16(d->dat, 0x8);
        u16 y = mempeek16(d->dat, 0xa);
        u8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
        u8 *layer = d->dat[0xe] >> 4 & 0x1 ? ppu.fg : ppu.bg;
        u8 mode = d->dat[0xe] >> 5;
        u8 color = d->dat[0xf] & 0xf;
        if(!mode) {
            ppu_pixel(layer, x, y, d->dat[0xe] & 0x3);
        } else if(mode-- & 0x1) {
            u8 flipx = mode & 0x2;
            u8 flipy = mode & 0x4;
            ppu_1bpp(layer, x, y, addr, color, flipx, flipy);
        } else {
            u8 flipx = mode & 0x2;
            u8 flipy = mode & 0x4;
            ppu_2bpp(layer, x, y, addr, color, flipx, flipy);
        }
    } else if(w && b0 == 0xf) {
        u16 x = mempeek16(d->dat, 0x8);
        u16 y = mempeek16(d->dat, 0xa);
        u8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
        u8 *layer = d->dat[0xf] >> 6 & 0x1 ? ppu.fg : ppu.bg;
        u8 color = d->dat[0xf] & 0xf;
        u8 flipx = (d->dat[0xf] >> 0x4) & 0x1;
        u8 flipy = (d->dat[0xf] >> 0x5) & 0x1;
        if(d->dat[0xf] >> 0x7 & 0x1) {
            ppu_2bpp(layer, x, y, addr, color, flipx, flipy);
        } else {
            ppu_1bpp(layer, x, y, addr, color, flipx, flipy);
        }
    }
}

static void
audio_talk(Device *d, u8 b0, u8 w) {
    AudioChannel *c = &channels[d - devaudio];
    if(!w) {
        if(b0 == 0x2) {
            mempoke16(d->dat, 0x2, c->pos);
            c->pos <<= 12; // fixed point.
        } else if(b0 == 0x4) {
            // d->dat[0x4] = apu_get_vu(c);
        }
    } else if(b0 == 0xf) {
        u16 length = mempeek16(d->dat, 0xa);
        u8 *data = &d->mem[mempeek16(d->dat, 0xc)];
        u8 pitch = d->dat[0xf] & 0x7f;
        u16 adsr = mempeek16(d->dat, 0x8);
        u32 vol = MAX(d->dat[0xe] >> 4, d->dat[0xe] & 0xf) * 4 / 3;
        bool loop = !(d->dat[0xf] & 0x80);
        update_channel(c, data, length, pitch, adsr, vol, loop);
    }
}

void
datetime_talk(Device *d, u8 b0, u8 w) {
    (void)d;
    (void)b0;
    (void)w;
}

void
file_talk(Device *d, u8 b0, u8 w) {
    u8 read = b0 == 0xd;
    if(w && (read || b0 == 0xf)) {
        char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)];
        u16 result = 0, length = mempeek16(d->dat, 0xa);
        u16 offset = mempeek16(d->dat, 0x4);
        u16 addr = mempeek16(d->dat, b0 - 1);
        OpenMode mode = FS_OPEN_READ;
        if (!read) {
            mode = offset ? FS_OPEN_APPEND : FS_OPEN_WRITE;
        }
        File file = fs_open_file(name, mode);
        if (file.index != FS_NULL) {
            if(fs_seek(&file, offset, SEEK_SET) != -1) {
                if (read) {
                    result = fs_read(&d->mem[addr], length, &file);
                } else {
                    result = fs_write(&d->mem[addr], length, &file);
                }
            }
        }
        mempoke16(d->dat, 0x2, result);
    }
}

void
init_uxn(Uxn *u) {
    // Initialize PPU.
    initppu(&ppu, 30, 20, 0);

    // Enable sound.
    init_sound();

    // Copy rom to VM.
    memcpy(u->ram.dat + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom));

    // Prepare devices.
    portuxn(u, 0x0, "system", system_talk);
    portuxn(u, 0x1, "console", console_talk);
    devscreen = portuxn(u, 0x2, "screen", screen_talk);
    devaudio = portuxn(u, 0x3, "audio0", audio_talk);
    portuxn(u, 0x4, "audio1", audio_talk);
    portuxn(u, 0x5, "audio2", audio_talk);
    portuxn(u, 0x6, "audio3", audio_talk);
    portuxn(u, 0x7, "---", nil_talk);
    devctrl = portuxn(u, 0x8, "controller", nil_talk);
    devmouse = portuxn(u, 0x9, "mouse", nil_talk);
    portuxn(u, 0xa, "file", file_talk);
    portuxn(u, 0xb, "datetime", datetime_talk);
    portuxn(u, 0xc, "---", nil_talk);
    portuxn(u, 0xd, "---", nil_talk);
    portuxn(u, 0xe, "---", nil_talk);
    portuxn(u, 0xf, "---", nil_talk);
    mempoke16(devscreen->dat, 2, ppu.hor * 8);
    mempoke16(devscreen->dat, 4, ppu.ver * 8);
}

IWRAM_CODE
void
handle_input(Uxn *u) {
    poll_keys();
    if (key_tap(KEY_SELECT)) {
        // Reset control variables on method switch.
        switch (ctrl_methods[ctrl_idx]) {
            case CONTROL_CONTROLLER: {
                devctrl->dat[2] = 0;
                evaluxn(u, mempeek16(devctrl->dat, 0));
                devctrl->dat[3] = 0;
            } break;
            case CONTROL_MOUSE: {
                devmouse->dat[6] = 0;
                devmouse->dat[7] = 0;
                mempoke16(devmouse->dat, 0x2, -10);
                mempoke16(devmouse->dat, 0x4, -10);
                evaluxn(u, mempeek16(devmouse->dat, 0));
            } break;
            case CONTROL_KEYBOARD: {
                toggle_keyboard();
            } break;
        }

        // Update ctrl_idx.
        ctrl_idx = (ctrl_idx + 1 > (int)LEN(ctrl_methods) - 1) ? 0 : ctrl_idx + 1;

        // Initialize controller variables here.
        if (ctrl_methods[ctrl_idx] == CONTROL_KEYBOARD) {
            toggle_keyboard();
        }
    }

    if (ctrl_methods[ctrl_idx] == CONTROL_CONTROLLER) {
        // TODO: We don't need ifs if we use KEY_INPUTS directly and mayvbe just
        // swap some things if needed.
        u8 *flag = &devctrl->dat[2];
        if (key_pressed(KEY_A)) {
            *flag |= 0x01;
        } else {
            *flag &= ~0x01;
        }
        if (key_pressed(KEY_B)) {
            *flag |= 0x02;
        } else {
            *flag &= ~0x02;
        }
        if (key_pressed(KEY_L)) {
            *flag |= 0x04;
        } else {
            *flag &= ~0x04;
        }
        if (key_pressed(KEY_R)) {
            *flag |= 0x08;
        } else {
            *flag &= ~0x08;
        }
        if (key_pressed(KEY_UP)) {
            *flag |= 0x10;
        } else {
            *flag &= ~0x10;
        }
        if (key_pressed(KEY_DOWN)) {
            *flag |= 0x20;
        } else {
            *flag &= ~0x20;
        }
        if (key_pressed(KEY_LEFT)) {
            *flag |= 0x40;
        } else {
            *flag &= ~0x40;
        }
        if (key_pressed(KEY_RIGHT)) {
            *flag |= 0x80;
        } else {
            *flag &= ~0x80;
        }

        evaluxn(u, mempeek16(devctrl->dat, 0));
        devctrl->dat[3] = 0;
    } else if (ctrl_methods[ctrl_idx] == CONTROL_MOUSE) {
        // Detect "mouse key press".
        u8 flag = devmouse->dat[6];
        if (key_tap(KEY_B)) {
            flag |= 0x01;
        } else if (key_released(KEY_B)) {
            flag &= ~0x01;
        }
        if (key_tap(KEY_A)) {
            flag |= 0x10;
        } else if (key_released(KEY_A)) {
            flag &= ~0x10;
        }

        // Handle chording.
        devmouse->dat[6] = flag;
        if(flag == 0x10 && (devmouse->dat[6] & 0x01)) {
            devmouse->dat[7] = 0x01;
        }
        if(flag == 0x01 && (devmouse->dat[6] & 0x10)) {
            devmouse->dat[7] = 0x10;
        }

        // Detect mouse movement.
        if (key_pressed(KEY_UP)) {
            mouse.y = CLAMP(mouse.y - MOUSE_DELTA, 0, SCREEN_HEIGHT - 8);
        } else if (key_pressed(KEY_DOWN)) {
            mouse.y = CLAMP(mouse.y + MOUSE_DELTA, 0, SCREEN_HEIGHT - 8);
        }
        if (key_pressed(KEY_LEFT)) {
            mouse.x = CLAMP(mouse.x - MOUSE_DELTA, 0, SCREEN_WIDTH - 8);
        } else if (key_pressed(KEY_RIGHT)) {
            mouse.x = CLAMP(mouse.x + MOUSE_DELTA, 0, SCREEN_WIDTH - 8);
        }

        // Eval mouse.
        mempoke16(devmouse->dat, 0x2, mouse.x);
        mempoke16(devmouse->dat, 0x4, mouse.y);
        evaluxn(u, mempeek16(devmouse->dat, 0));
    } else if (ctrl_methods[ctrl_idx] == CONTROL_KEYBOARD) {
        if (key_tap(KEY_LEFT)) {
            update_cursor(cursor_position - 1);
        } else if (key_tap(KEY_RIGHT)) {
            update_cursor(cursor_position + 1);
        }
        if (key_tap(KEY_UP) && cursor_position >= KEYBOARD_ROW_SIZE) {
            update_cursor(cursor_position - KEYBOARD_ROW_SIZE);
        } else if (key_tap(KEY_DOWN)
                && cursor_position < LEN(keyboard) - KEYBOARD_ROW_SIZE) {
            update_cursor(cursor_position + KEYBOARD_ROW_SIZE);
        }
        if (key_tap(KEY_B)) {
            u8 symbol = keyboard[cursor_position].symbol;
            switch (symbol) {
                case 0x7f: {
                    // Backspace.
                    devctrl->dat[3] = 0x08;
                } break;
                case 0x14: {
                    // New line.
                    devctrl->dat[3] = 0x0d;
                } break;
                case 0x18: {
                    // Arrow up.
                    devctrl->dat[2] = 0x10;
                } break;
                case 0x19: {
                    // Arrow down.
                    devctrl->dat[2] = 0x20;
                } break;
                case 0x1b: {
                    // Arrow left.
                    devctrl->dat[2] = 0x40;
                } break;
                case 0x1a: {
                    // Arrow right.
                    devctrl->dat[2] = 0x80;
                } break;
                default: {
                    devctrl->dat[3] = symbol;
                } break;
            }
            evaluxn(u, mempeek16(devctrl->dat, 0));
            devctrl->dat[3] = 0;
        }
    }
}

static Uxn u;
EWRAM_BSS
static u8 umem[KB(65)];

int main(void) {
    // Adjust system wait times.
    SYSTEM_WAIT = SYSTEM_WAIT_CARTRIDGE;

    // Initialize filesystem.
    fs_init();

    // Register interrupts.
    irq_init();
    irs_set(IRQ_VBLANK, sound_vsync);

    // Initialize VM.
    dma_fill(&u, 0, sizeof(u), 3);
    u.ram.dat = umem;
    init_uxn(&u);

    // Initialize text engine.
    txt_init(1, TEXT_LAYER);
    txt_position(0,0);

    // Initialize sound mixer.
    init_sound();

    // Main loop.
    evaluxn(&u, 0x0100);
    PROF_INIT();
    while(true) {
        bios_vblank_wait();
        PROF(handle_input(&u), input_cycles);
        PROF(evaluxn(&u, mempeek16(devscreen->dat, 0)), eval_cycles);
        PROF(sound_mix(), mix_cycles);
        PROF_SHOW();
        PROF(flipbuf(&ppu), flip_cycles);
    }

    return 0;
}