aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 4a4b58c0466196e5a46ce21713dd90841d446d25 (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
/*
   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.c"
#include "ppu.c"
#include "apu.c"
#include "file.c"
#include "text.h"

#include "rom.c"
#include "config.c"
#include "profiling.c"
#include "devices.c"

#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;

EWRAM_BSS
u8 uxn_ram[KB(64) * 2];

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, 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));
}

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;
}