aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: e10c8f00ca4f299a860b030acd36524e02e4e8e0 (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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/*
   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 <time.h>

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

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

#include "rom.c"

//
// Config parameters.
//

#if !defined(TEXT_MODE) || TEXT_MODE == 0
#define TEXT_LAYER FG_BACK
#else
#define TEXT_LAYER BG_BACK
#endif

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

#define PROF_ENABLE 0
#ifndef PROF_ENABLE
#define PROF_ENABLE 0
#endif

#if PROF_ENABLE > 0 && PROF_ENABLE < 3

#ifndef PROF_N_FRAMES
#define PROF_N_FRAMES 15
#endif

// Profile method 1: Average per N frames.
#if PROF_ENABLE == 1
#define TEXT_ENABLE 1
#define PROF(F,VAR) \
    do { \
        u32 __tmp_prof = profile_measure();\
        (F);\
        (VAR) += profile_measure() - __tmp_prof;\
    } while (0)

// Profile method 2: Maximum in N frames.
#elif PROF_ENABLE == 2
#define TEXT_ENABLE 1
#define PROF(F,VAR) \
    do { \
        u32 __tmp_prof = profile_measure();\
        (F);\
        (VAR) = MAX(profile_measure() - __tmp_prof, (VAR));\
    } while (0)
#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  %.8lu\n", avg_input_cycles);\
        txt_printf("EVAL   %.8lu\n", avg_eval_cycles);\
        txt_printf("VIDEO\n");\
        txt_printf(">PIX   %.8lu\n", avg_ppu_pixel_cycles);\
        txt_printf(">FILL  %.8lu\n", avg_ppu_fill_cycles);\
        txt_printf(">1BPP  %.8lu\n", avg_ppu_icn_cycles);\
        txt_printf(">2BPP  %.8lu\n", avg_ppu_chr_cycles);\
        txt_printf(">FLIP  %.8lu\n", avg_flip_cycles);\
        txt_printf("AUDIO  %.8lu\n", avg_mix_cycles);\
        txt_printf("TOTAL  %.8lu\n", avg_frame_cycles);\
        u32 frame_time =\
            FP_DIV(\
                FP_NUM(avg_frame_cycles + 1, 2),\
                FP_NUM(2809, 2),\
                2) * 166;\
        u32 fps =\
            FP_DIV(\
                FP_NUM(280896 * 60, 2),\
                FP_NUM(avg_frame_cycles + 1, 2),\
                2);\
        txt_printf("TIME   %.8lu\n", frame_time >> 2);\
        txt_printf("FPS    %.8lu\n", (fps >> 2) + 1);\
        screen_fill(BG_BACK, 0, 0, 8 * 16, 8 * 12, 2);\
    } while (0)

static u32 prof_frame_counter = 0;

static u32 frame_cycles = 0;
static u32 ppu_pixel_cycles = 0;
static u32 ppu_fill_cycles = 0;
static u32 ppu_chr_cycles = 0;
static u32 ppu_icn_cycles = 0;
static u32 flip_cycles = 0;
static u32 eval_cycles = 0;
static u32 input_cycles = 0;
static u32 mix_cycles = 0;

static u32 avg_ppu_pixel_cycles = 0;
static u32 avg_ppu_fill_cycles = 0;
static u32 avg_ppu_chr_cycles = 0;
static u32 avg_ppu_icn_cycles = 0;
static u32 avg_flip_cycles = 0;
static u32 avg_eval_cycles = 0;
static u32 avg_input_cycles = 0;
static u32 avg_mix_cycles = 0;
static u32 avg_frame_cycles = 0;

#if PROF_ENABLE == 1
#define FRAME_START()\
    do { \
        if (prof_frame_counter == PROF_N_FRAMES) {\
            avg_ppu_pixel_cycles = ppu_pixel_cycles / prof_frame_counter;\
            avg_ppu_fill_cycles  = ppu_fill_cycles / prof_frame_counter;\
            avg_ppu_chr_cycles   = ppu_chr_cycles / prof_frame_counter;\
            avg_ppu_icn_cycles   = ppu_icn_cycles / prof_frame_counter;\
            avg_flip_cycles      = flip_cycles / prof_frame_counter;\
            avg_eval_cycles      = eval_cycles / prof_frame_counter;\
            avg_input_cycles     = input_cycles / prof_frame_counter;\
            avg_mix_cycles       = mix_cycles / prof_frame_counter;\
            avg_frame_cycles     = frame_cycles / prof_frame_counter;\
            prof_frame_counter   = 0;\
            frame_cycles         = 0;\
            ppu_pixel_cycles     = 0;\
            ppu_fill_cycles      = 0;\
            ppu_chr_cycles       = 0;\
            ppu_icn_cycles       = 0;\
            flip_cycles          = 0;\
            eval_cycles          = 0;\
            input_cycles         = 0;\
            mix_cycles           = 0;\
        }\
        profile_start();\
    } while (0)
#elif PROF_ENABLE == 2
#define FRAME_START()\
    do { \
        if (prof_frame_counter == PROF_N_FRAMES) {\
            avg_ppu_pixel_cycles = ppu_pixel_cycles;\
            avg_ppu_fill_cycles  = ppu_fill_cycles;\
            avg_ppu_chr_cycles   = ppu_chr_cycles;\
            avg_ppu_icn_cycles   = ppu_icn_cycles;\
            avg_flip_cycles      = flip_cycles;\
            avg_eval_cycles      = eval_cycles;\
            avg_input_cycles     = input_cycles;\
            avg_mix_cycles       = mix_cycles;\
            avg_frame_cycles     = frame_cycles / prof_frame_counter;\
            prof_frame_counter   = 0;\
            frame_cycles         = 0;\
            ppu_pixel_cycles     = 0;\
            ppu_fill_cycles      = 0;\
            ppu_chr_cycles       = 0;\
            ppu_icn_cycles       = 0;\
            flip_cycles          = 0;\
            eval_cycles          = 0;\
            input_cycles         = 0;\
            mix_cycles           = 0;\
        }\
        profile_start();\
    } while (0)
#endif

#define FRAME_END() \
    do { \
        prof_frame_counter++;\
        frame_cycles += profile_stop();\
    } while (0)

#else

// No profiling.
#define PROF(F,VAR) (F)
#define PROF_SHOW()
#define FRAME_START()
#define FRAME_END()
#endif

static time_t seconds = 0;

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;

EWRAM_BSS
u8 uxn_ram[0x10300];

// static Uxn u;

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

// int
// uxn_halt(Uxn *u, u8 instr, u8 err, u16 addr) {
//     (void)u;
//     txt_printf("HALTED\n");
//     txt_printf("I: %lu\n", instr);
//     txt_printf("E: %lu\n", err);
//     txt_printf("A: %lu\n", addr);
//     while (true);
// }

IWRAM_CODE
u8
screen_dei(u8 *d, u8 port) {
    // switch(port) {
    //     case 0x2: return (SCREEN_WIDTH >> 8);
    //     case 0x3: return (SCREEN_WIDTH);
    //     case 0x4: return (SCREEN_HEIGHT >> 8);
    //     case 0x5: return (SCREEN_HEIGHT);
    //     default: return d[port];
    // }
}

IWRAM_CODE
void
screen_deo(u8 *ram, u8 *d, u8 port) {
    // switch(port) {
    //     case 0xe: {
    //         u8 ctrl = d[0xe];
    //         u8 color = ctrl & 0x3;
    //         u16 x0 = PEEK2(d + 0x8);
    //         u16 y0 = PEEK2(d + 0xa);
    //         u8 *layer = (ctrl & 0x40) ? FG_BACK : BG_BACK;
    //         if(ctrl & 0x80) {
    //             u16 x1 = SCREEN_WIDTH - 1;
    //             u16 y1 = SCREEN_HEIGHT - 1;
    //             if(ctrl & 0x10) x1 = x0, x0 = 0;
    //             if(ctrl & 0x20) y1 = y0, y0 = 0;
    //             PROF(screen_fill(layer, x0, y0, x1, y1, color), ppu_fill_cycles);
    //         } else {
    //             PROF(ppu_pixel(layer, x0, y0, color), ppu_pixel_cycles);
    //             if(d[0x6] & 0x1) POKE2(d + 0x8, x0 + 1); /* auto x+1 */
    //             if(d[0x6] & 0x2) POKE2(d + 0xa, y0 + 1); /* auto y+1 */
    //         }
    //         break;
    //     }
    //     case 0xf: {
    //         u16 x, y, dx, dy, addr;
    //         u8 n, twobpp = !!(d[0xf] & 0x80);
    //         x    = PEEK2(d + 0x8);
    //         y    = PEEK2(d + 0xa);
    //         addr = PEEK2(d + 0xc);
    //         n = d[0x6] >> 4;
    //         dx = (d[0x6] & 0x01) << 3;
    //         dy = (d[0x6] & 0x02) << 2;
    //         if(addr > 0x10000 - ((n + 1) << (3 + twobpp))) {
    //             return;
    //         }
    //         u8 *layer = (d[0xf] & 0x40) ? FG_BACK : BG_BACK;
    //         u8 color = d[0xf] & 0xf;
    //         u8 flipx = d[0xf] & 0x10;
    //         u8 flipy = d[0xf] & 0x20;
    //         for(size_t i = 0; i <= n; i++) {
    //             u8 *sprite = &ram[addr];
    //             if (twobpp) {
    //                 PROF(ppu_2bpp(layer,
    //                             x + dy * i,
    //                             y + dx * i,
    //                             sprite,
    //                             color,
    //                             flipx, flipy), ppu_chr_cycles);
    //             } else {
    //                 PROF(ppu_1bpp(layer,
    //                             x + dy * i,
    //                             y + dx * i,
    //                             sprite,
    //                             color,
    //                             flipx, flipy), ppu_icn_cycles);
    //             }
    //             addr += (d[0x6] & 0x04) << (1 + twobpp);
    //         }
    //         POKE2(d + 0xc, addr);   /* auto addr+length */
    //         POKE2(d + 0x8, x + dx); /* auto x+8 */
    //         POKE2(d + 0xa, y + dy); /* auto y+8 */
    //         break;
    //     }
    // }
}

u8
audio_dei(int instance, u8 *d, u8 port) {
    // AudioChannel *c = &channels[instance];
    // switch(port) {
    //     // case 0x4: return apu_get_vu(instance);
    //     case 0x2: {
    //         POKE2(d + 0x2, c->pos);
    //         c->pos <<= 12; // fixed point.
    //         break;
    //     }
    // }
    return d[port];
}

void
audio_deo(int instance, u8 *d, u8 port, Uxn *u) {
    // AudioChannel *c = &channels[instance];
    // if (port == 0xf) {
    //     u16 length = 0;
    //     u16 adsr = 0;
    //     u16 addr = 0;
    //     u8 pitch = d[0xf] & 0x7f;
    //     adsr = PEEK2(d + 0x8);
    //     length = PEEK2(d + 0xa);
    //     addr = PEEK2(d + 0xc);
    //     u8 *data = &u->ram[addr];
    //     u32 vol = MAX(d[0xe] >> 4, d[0xe] & 0xf) * 4 / 3;
    //     bool loop = !(d[0xf] & 0x80);
    //     update_channel(c, data, length, pitch, adsr, vol, loop);
    // }
}

u8
datetime_dei(u8 *d, u8 port) {
    struct tm *t = gmtime(&seconds);
    switch(port) {
        case 0x0: return (t->tm_year + 1900) >> 8;
        case 0x1: return (t->tm_year + 1900);
        case 0x2: return t->tm_mon;
        case 0x3: return t->tm_mday;
        case 0x4: return t->tm_hour;
        case 0x5: return t->tm_min;
        case 0x6: return t->tm_sec;
        case 0x7: return t->tm_wday;
        case 0x8: return t->tm_yday >> 8;
        case 0x9: return t->tm_yday;
        case 0xa: return t->tm_isdst;
        default: return d[port];
    }
}

u8
file_dei(u8 id, u8 *d, u8 port) {
    // UxnFile *c = &uxn_file[id];
    // u16 res;
    // switch(port) {
    //     case 0xc:
    //     case 0xd: {
    //         res = file_read(c, &d[port], 1);
    //         POKE2(d + 0x2, res);
    //         break;
    //     }
    // }
    return d[port];
}

void
file_deo(u8 id, u8 *ram, u8 *d, u8 port) {
    // u16 a, b, res;
    // UxnFile *f = &uxn_file[id];
    // switch(port) {
    //     case 0x5: {
    //         a = PEEK2(d + 0x4);
    //         b = PEEK2(d + 0xa);
    //         if(b > 0x10000 - a) {
    //             b = 0x10000 - a;
    //         }
    //         res = file_stat(f, &ram[a], b);
    //         POKE2(d + 0x2, res);
    //     } break;
    //     case 0x6: {
    //         // TODO: no file deletion for now
    //         // res = file_delete();
    //         // POKE2(d + 0x2, res);
    //     } break;
    //     case 0x9: {
    //         a = PEEK2(d + 0x8);
    //         res = file_init(f, &ram[a]);
    //         POKE2(d + 0x2, res);
    //     } break;
    //     case 0xd: {
    //         a = PEEK2(d + 0xc);
    //         b = PEEK2(d + 0xa);
    //         if(b > 0x10000 - a) {
    //             b = 0x10000 - a;
    //         }
    //         res = file_read(f, &ram[a], b);
    //         POKE2(d + 0x2, res);
    //     } break;
    //     case 0xf: {
    //         a = PEEK2(d + 0xe);
    //         b = PEEK2(d + 0xa);
    //         if(b > 0x10000 - a) {
    //             b = 0x10000 - a;
    //         }
    //         res = file_write(f, &ram[a], b, d[0x7]);
    //         POKE2(d + 0x2, res);
    //     } break;
    // }
}

void
console_deo(u8 *d, u8 port) {
    // switch(port) {
    // case 0x8:
    //     txt_putc(d[port]);
    //     return;
    // case 0x9:
    //     txt_printf("ERROR: %c");
    //     txt_putc(d[port]);
    //     return;
    // }
}

#define RAM_PAGES 0x10

static void
system_cmd(u8 *ram, u16 addr) {
    if(ram[addr] == 0x01) {
        // NOTE: Handle rom paging on a case by case basis if a rom has to be
        // split in multiple chunks. The GBA compiler doesn't like allocating
        // big arrays, but it's fine if we split it into chunks of 64KB, for
        // example.
        //
        // u16 i, length = PEEK2(ram + addr + 1);
        // u16 a_page = PEEK2(ram + addr + 1 + 2);
        // u16 a_addr = PEEK2(ram + addr + 1 + 4);
        // u16 b_addr = PEEK2(ram + addr + 1 + 8);
        // u8 *rom = uxn_rom;
        // for(i = 0; i < length; i++) {
        //     switch (a_page % RAM_PAGES) {
        //         case 0: { rom = uxn_rom;   } break;
        //         case 1: { rom = uxn_rom_2; } break;
        //         case 2: { rom = uxn_rom_3; } break;
        //         case 3: { rom = uxn_rom_4; } break;
        //         case 4: { rom = uxn_rom_5; } break;
        //         case 5: { rom = uxn_rom_6; } break;
        //         case 6: { rom = uxn_rom_7; } break;
        //     }
        //     ram[(u16)(b_addr + i)] = rom[(u16)(a_addr + i)];
        // }
    }
}

void
system_deo(Uxn *u, u8 *d, u8 port) {
    // switch(port) {
    //     case 0x3: {
    //         system_cmd(u->ram, PEEK2(d + 2));
    //     } break;
    // }
}

u8
uxn_dei(Uxn *u, u8 addr) {
    u8 p = addr & 0x0f, d = addr & 0xf0;
    switch(d) {
        case 0x20: return screen_dei(&u->dev[d], p);
        case 0x30: return audio_dei(0, &u->dev[d], p);
        case 0x40: return audio_dei(1, &u->dev[d], p);
        case 0x50: return audio_dei(2, &u->dev[d], p);
        case 0x60: return audio_dei(3, &u->dev[d], p);
        case 0xa0: return file_dei(0, &u->dev[d], p);
        case 0xb0: return file_dei(1, &u->dev[d], p);
        case 0xc0: return datetime_dei(&u->dev[d], p);
    }
    return u->dev[addr];
}

void
uxn_deo(Uxn *u, u8 addr) {
	u8 p = addr & 0x0f, d = addr & 0xf0;
	switch(d) {
        case 0x00:
            system_deo(u, &u->dev[d], p);
            if(p > 0x7 && p < 0xe) {
                putcolors(&u->dev[0x8]);
            }
            break;
        case 0x10: console_deo(&u->dev[d], p); break;
        case 0x20: screen_deo(u->ram, &u->dev[d], p); break;
        case 0x30: audio_deo(0, &u->dev[d], p, u); break;
        case 0x40: audio_deo(1, &u->dev[d], p, u); break;
        case 0x50: audio_deo(2, &u->dev[d], p, u); break;
        case 0x60: audio_deo(3, &u->dev[d], p, u); break;
        case 0xa0: file_deo(0, u->ram, &u->dev[d], p); break;
        case 0xb0: file_deo(1, u->ram, &u->dev[d], p); break;
	}
}

void
init_uxn() {
    // Initialize uxn.
    u32 fill = 0;
    dma_fill(uxn_ram, fill, 0x10300, 3);
    // uxn_boot(u, uxn_ram, uxn_dei, uxn_deo);

    // Copy rom to VM.
    u8 uxn_rom[32] = {
        // Hello world (h)
        // 0x80, 0x68, 0x80, 0x18, 0x17,
        //
        // ADD test
        // 0x80, 0x04, 0x80, 0x03, 0x80, 0x02, 0x80, 0x01, 0x18, 0x18
        // SUB test
        // 0x80, 0x04, 0x80, 0x03, 0x80, 0x02, 0x80, 0x01, 0x19, 0x19, 0x19
        // LIT2 04 03 LIT2 02 01 ADD ADD ADD BRK
        // 0xa0, 0x04, 0x03,
        // 0xa0, 0x04, 0x03, 0xa0, 0x02, 0x01,
        // 0xa0, 0x04, 0x03, 0xa0, 0x02, 0x01, 0x18, 0x18, 0x00
        // #0004 #0008 ADD
        // 0xa0, 0x00, 0x04, 0xa0, 0x00, 0x08, 0x18,
        // #0004 #0008 ADD2
        0x80, 0xFF, 0xff,
        0xa0, 0x00, 0x08, 0xa0, 0x00, 0x04, 0x38,
        // 0xa0, 0x00, 0x08, 0xa0, 0x00, 0x03, 0x39,
        // 0xa0, 0x00, 0x01, 0x38,
        //
        // 0x80, 0xFF, 0xff,
        // 0xa0, 0xba, 0xdd,
        // 0xa0, 0x10, 0xde,
        // 0xa0, 0xfe, 0xed,
        // 0xa0, 0xde, 0xaf,
        // 0xa0, 0xba, 0xdd,
        // 0xa0, 0x10, 0xde,
        // 0xa0, 0xfe, 0xed,
        // 0xa0, 0xde, 0xaf,
        //
        // #0008 #0003 SUB2
        // 0xa0, 0x00, 0x08, 0xa0, 0x00, 0x03,
    };
    memcpy(uxn_ram + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom));
}

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: {
    //             u8 *d = &u->dev[0x80];
    //             d[2] = 0;
    //             uxn_eval(u, PEEK2(d));
    //             d[3] = 0;
    //         } break;
    //         case CONTROL_MOUSE: {
    //             u8 *d = &u->dev[0x90];
    //             d[6] = 0;
    //             d[7] = 0;
    //             POKE2(d + 0x2, -10);
    //             POKE2(d + 0x4, -10);
    //             uxn_eval(u, PEEK2(d));
    //         } 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) {
    //     u8 *d = &u->dev[0x80];
    //     // TODO: We don't need ifs if we use KEY_INPUTS directly and maybe just
    //     // swap some things if needed.
    //     u8 *flag = &d[2];
    //     if (key_tap(KEY_A)) {
    //         *flag |= 0x01;
    //     } else {
    //         *flag &= ~0x01;
    //     }
    //     if (key_tap(KEY_B)) {
    //         *flag |= 0x02;
    //     } else {
    //         *flag &= ~0x02;
    //     }
    //     if (key_tap(KEY_L)) {
    //         *flag |= 0x04;
    //     } else {
    //         *flag &= ~0x04;
    //     }
    //     if (key_tap(KEY_R)) {
    //         *flag |= 0x08;
    //     } else {
    //         *flag &= ~0x08;
    //     }
    //     if (key_tap(KEY_UP)) {
    //         *flag |= 0x10;
    //     } else {
    //         *flag &= ~0x10;
    //     }
    //     if (key_tap(KEY_DOWN)) {
    //         *flag |= 0x20;
    //     } else {
    //         *flag &= ~0x20;
    //     }
    //     if (key_tap(KEY_LEFT)) {
    //         *flag |= 0x40;
    //     } else {
    //         *flag &= ~0x40;
    //     }
    //     if (key_tap(KEY_RIGHT)) {
    //         *flag |= 0x80;
    //     } else {
    //         *flag &= ~0x80;
    //     }

    //     if (key_prev != key_curr) {
    //         uxn_eval(u, PEEK2(d));
    //     }
    //     d[3] = 0;
    // } else if (ctrl_methods[ctrl_idx] == CONTROL_MOUSE) {
    //     u8 *d = &u->dev[0x90];
    //     // Detect "mouse key press".
    //     u8 flag = d[6];
    //     bool event = false;
    //     if (key_tap(KEY_B)) {
    //         event = true;
    //         flag |= 0x01;
    //     } else if (key_released(KEY_B)) {
    //         event = true;
    //         flag &= ~0x01;
    //     }
    //     if (key_tap(KEY_A)) {
    //         event = true;
    //         flag |= 0x10;
    //     } else if (key_released(KEY_A)) {
    //         event = true;
    //         flag &= ~0x10;
    //     }

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

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

    //     // Eval mouse.
    //     POKE2(d + 0x2, mouse.x);
    //     POKE2(d + 0x4, mouse.y);
    //     if (event) {
    //         uxn_eval(u, PEEK2(d));
    //     }
    // } else if (ctrl_methods[ctrl_idx] == CONTROL_KEYBOARD) {
    //     u8 *d = &u->dev[0x80];
    //     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.
    //                 d[3] = 0x08;
    //             } break;
    //             case 0x14: {
    //                 // New line.
    //                 d[3] = 0x0d;
    //             } break;
    //             case 0x18: {
    //                 // Arrow up.
    //                 d[2] = 0x10;
    //             } break;
    //             case 0x19: {
    //                 // Arrow down.
    //                 d[2] = 0x20;
    //             } break;
    //             case 0x1b: {
    //                 // Arrow left.
    //                 d[2] = 0x40;
    //             } break;
    //             case 0x1a: {
    //                 // Arrow right.
    //                 d[2] = 0x80;
    //             } break;
    //             default: {
    //                 d[3] = symbol;
    //             } break;
    //         }
    //         uxn_eval(u, PEEK2(d));
    //         d[3] = 0;
    //     }
    // }
}

#define STACK_SIZE 16
u8 stack[STACK_SIZE] = {0};
extern void uxn_eval_asm(u16 pc);

// TODO: This should be on the IWRAM for maximum speed, as these are operations
// we will use very often.
extern u8 wst[256];
extern u8 rst[256];
extern u8 io_ports[256];
extern uintptr_t wst_ptr;
extern uintptr_t rst_ptr;

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 PPU.
    video_init();

    // Initialize text engine.
// #ifdef TEXT_ENABLE
    txt_init(1, TEXT_LAYER);
    txt_position(0,0);
// #endif

    // Initialize UXN.
    init_uxn();

    // Enable sound.
    init_sound();

    // Main loop.
    // uxn_eval(&u, PAGE_PROGRAM);
    u8 frame_counter = 0;
    // NOTE: A VBLANK is 83776 cycles, anything other than that will make it so
    // we fail to render at 60FPS.

    // TODO: Initialize memory.
    // Initialize stack.
    for (size_t i = 0; i < 256; i++) {
        wst[i] = 0;
        rst[i] = 0;
        io_ports[i] = 0;
    }

    txt_printf("ROM\n\n");
    for (size_t i = 0; i < 32; i++) {
        if (i % 8 == 0) {
            txt_printf("\n");
        }
        txt_printf("%02x ", uxn_ram[i + PAGE_PROGRAM]);
    }
    txt_printf("\n\n");

    uxn_eval_asm(PAGE_PROGRAM);
    txt_printf("STACK\n");
    txt_printf("PTR: %d\n", wst_ptr - (uintptr_t)wst);
    for (size_t i = 0; i < STACK_SIZE; i++) {
        if (i % 8 == 0) {
            txt_printf("\n");
        }
        txt_printf("%02x ", wst[i]);
    }

    uintptr_t stack_base = wst;
    uintptr_t stack_size = wst_ptr - stack_base;
    while(true) {
        txt_position(0,0);
        // bios_vblank_wait();
        // FRAME_START();
        // // PROF(handle_input(&u), input_cycles);
        // // PROF(uxn_eval(&u, PEEK2(&u.dev[0x20])), eval_cycles);
        // // PROF(sound_mix(), mix_cycles);
        // // // TODO: allow configuration to do VSYNC at 15 or 30 fps to avoid too
        // // // much memory copying on demanding uxn roms.
        // // PROF_SHOW();
        // // PROF(flipbuf(), flip_cycles);
        // // frame_counter++;
        // // if (frame_counter == 60) {
        // //     seconds++;
        // //     frame_counter = 0;
        // // }
        // FRAME_END();
        bios_vblank_wait();
        FRAME_START();
        flipbuf();
        // screen_fill(0);
        // txt_printf("WST: 0x%08x\n", wst);
        // txt_printf("PTR: 0x%08x\n", wst_ptr);
        FRAME_END();
        PROF_SHOW();
        // txt_render();
        // txt_clear();
    }

    return 0;
}