aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: ecd9766f4a7e8d783f59f7c5512339b153db00d2 (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
/*
   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"

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)];

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

    // Copy rom to VM.
    u8 uxn_rom[] = {
        // // ADD test.
        // 0x80, 0x04, 0x80, 0x08, 0x18,             // #04 #08 ADD      -> 0c
        // 0xa0, 0x00, 0x04, 0xa0, 0x00, 0x08, 0x38, // #0004 #0008 ADD2 -> 000c
        // 0xa0, 0x00, 0xff, 0xa0, 0x00, 0x02, 0x38, // #00ff #0002 ADD2 -> 0101
        // // SUB test.
        // 0x80, 0x08, 0x80, 0x03, 0x19,             // #08 #03 SUB      -> 05
        // 0xa0, 0x00, 0x08, 0xa0, 0x00, 0x03, 0x39, // #0008 #0003 SUB2 -> 0005
        // 0xa0, 0x01, 0x01, 0xa0, 0x00, 0x02, 0x39, // #0008 #0003 SUB2 -> 00ff
        // // MUL test.
        // 0x80, 0x03, 0x80, 0x04, 0x1a,             // #03 #04 MUL      -> 0c
        // 0xa0, 0x00, 0x03, 0xa0, 0x00, 0x04, 0x3a, // #0003 #0004 MUL2 -> 000c
        // 0xa0, 0x00, 0xff, 0xa0, 0x00, 0x02, 0x3a, // #00ff #0002 MUL2 -> 01fe
        // DIV test.
        0x80, 0x08, 0x80, 0x04, 0x1b,             // #08 #04 DIV      -> 02
        0xa0, 0x00, 0x08, 0xa0, 0x00, 0x04, 0x3b, // #0008 #0004 DIV2 -> 0002
        // // INC test.
        // 0x80, 0x09, 0x01,                         // #09 INC          -> 0a
        // 0xa0, 0x00, 0x09, 0x21,                   // #0009 INC2       -> 000a
        // 0xa0, 0x00, 0xff, 0x21,                   // #0009 INC2       -> 0100
        // 0xa0, 0x00, 0xff, 0x21, 0x21,             // #0009 INC2 INC2  -> 0101
        // // POP.
        // 0xa0, 0x12, 0x34, 0x02,
        // 0xa0, 0x12, 0x34, 0x22,
        // 0xa0, 0x00, 0x00,
        // // NIP.
        // 0xa0, 0x12, 0x34, 0x03, // -> ( 34 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x56, 0x78, 0x23, // -> ( 56 78 )
        // // SWP.
        // 0xa0, 0x12, 0x34, 0x04, // -> ( 34 12 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x56, 0x78, 0x24, // -> ( 56 78 12 34 )
        // // ROT.
        // 0xa0, 0x12, 0x34, 0x80, 0x56, 0x05, // -> ( 34 56 12 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x56, 0x78, 0xa0, 0x9a, 0xbc, 0x25, // -> ( 56 78 9a bc 12 34 )
        // // DUP.
        // 0xa0, 0x12, 0x34, 0x06, // -> ( 12 34 34 )
        // 0xa0, 0x12, 0x34, 0x26, // -> ( 12 34 12 34 )
        // // OVR.
        // 0xa0, 0x12, 0x34, 0x07, // -> ( 12 34 12 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x56, 0x78, 0x27, // -> ( 12 34 56 78 12 34 )
        // // EQU.
        // 0xa0, 0x12, 0x12, 0x08, // -> ( 01 )
        // 0xa0, 0x12, 0x34, 0x08, // -> ( 00 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x12, 0x34, 0x28, // -> ( 01 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x56, 0x78, 0x28, // -> ( 00 )
        // // NEQ.
        // 0xa0, 0x12, 0x12, 0x09, // -> ( 00 )
        // 0xa0, 0x12, 0x34, 0x09, // -> ( 01 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x12, 0x34, 0x29, // -> ( 00 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x56, 0x78, 0x29, // -> ( 01 )
        // // GTH.
        // 0xa0, 0x12, 0x34, 0x0a, // -> ( 00 )
        // 0xa0, 0x34, 0x12, 0x0a, // -> ( 01 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x34, 0x56, 0x2a, // -> ( 00 )
        // 0xa0, 0x34, 0x56, 0xa0, 0x12, 0x34, 0x2a, // -> ( 01 )
        // // LTH.
        // 0xa0, 0x12, 0x34, 0x0b, // -> ( 01 )
        // 0xa0, 0x34, 0x12, 0x0b, // -> ( 00 )
        // 0xa0, 0x12, 0x34, 0xa0, 0x34, 0x56, 0x2b, // -> ( 01 )
        // 0xa0, 0x34, 0x56, 0xa0, 0x12, 0x34, 0x2b, // -> ( 00 )
        // // AND.
        // 0xa0, 0x0f, 0x88, 0x1c, // -> ( 08 )
        // 0xa0, 0xf0, 0x88, 0x1c, // -> ( 80 )
        // 0xa0, 0x00, 0x0f, 0xa0, 0x99, 0x88, 0x3c, // -> ( 00 08 )
        // // ORA.
        // 0xa0, 0x0f, 0x88, 0x1d, // -> ( 8f )
        // 0xa0, 0xf0, 0x08, 0x1d, // -> ( f8 )
        // 0xa0, 0xf0, 0x0f, 0xa0, 0xaa, 0xaa, 0x3d, // -> ( fa af )
        // // EOR.
        // 0xa0, 0x0f, 0x88, 0x1e, // -> ( 87 )
        // 0xa0, 0xf0, 0x08, 0x1e, // -> ( f8 )
        // 0xa0, 0xf0, 0x0f, 0xa0, 0xaa, 0xaa, 0x3e, // -> ( 5a a5 )
        // // SFT.
        // 0xa0, 0x34, 0x10, 0x1f, // -> ( 68 )
        // 0xa0, 0x34, 0x01, 0x1f, // -> ( 1a )
        // 0xa0, 0x34, 0x33, 0x1f, // -> ( 30 )
        // 0xa0, 0x12, 0x48, 0x80, 0x34, 0x3f, // -> ( 09 20 )
        // // STZ / LDZ.
        // 0xa0, 0x34, 0x00, 0x11,             // STZ   (u8*)uxn_ram[1] = 0x34
        // 0xa0, 0xaa, 0xbb, 0x80, 0x01, 0x31, // STZ2 (u16*)uxn_ram[1] = 0x34
        // 0x80, 0x00, 0x10,                   // LDZ  -> ( 34 )
        // 0x80, 0x01, 0x30,                   // LDZ2 -> ( aa bb )
        // // LDR / SRT
        // 0xa0, 0xab, 0xcd, 0x80, 0xfb, 0x12,
        // 0xa0, 0xab, 0xcd, 0x80, 0xfb, 0x32,
        // 0x80, 1, 0x12, 0xa0, 0xaa, 0xbb,
        // 0xa0, 0xff, 0xee, 0x80, 0xfa, 0x33,
        // 0x80, 0xff, 0x80, 1, 0x13, 0xa0, 0xaa, 0xbb,
        // 0xa0, 0xff, 0xee, 0x80, 1, 0x33, 0xa0, 0xaa, 0xbb,
        // LDA /STA
        // 0xa0, 0x01, 0x05, 0x14, 0xa0, 0xaa, 0xbb,
        // 0xa0, 0x01, 0x05, 0x34, 0xa0, 0xcc, 0xdd,
        // 0xa0, 0xab, 0xcd, 0xa0, 0x01, 0x08, 0x15,
        // 0xa0, 0xab, 0xcd, 0xa0, 0x01, 0x08, 0x35,
        // JMP
        // 0x80, 0x01, 0x0c, 0x00, 0x80, 0xaa,
        // 0xa0, 0x01, 0x05, 0x2c, 0x00, 0x80, 0xbb,
        // JCN
        // 0xa0, 0xab, 0xcd, 0x80, 0x01, 0x80, 0x01, 0x0d, 0x04, 0x02,
        // 0xa0, 0xab, 0xcd, 0x80, 0x00, 0x80, 0x01, 0x0d, 0x04, 0x02,
        // 0xa0, 0xab, 0xcd, 0x80, 0x01, 0xa0, 0x01, 0x0a, 0x2d, 0x04, 0x02,
        // 0xa0, 0xab, 0xcd, 0x80, 0x00, 0xa0, 0x01, 0x15, 0x2d, 0x04, 0x02,
        // JSR
        // 0x80, 0x03, 0x0e, 0x80, 0x01, 0x00, 0x80, 0x02, 0x6c,
        // 0x80, 0x03, 0x0e, 0x80, 0x01, 0x00, 0x80, 0x02, 0x6c,
    };
    memcpy(uxn_ram + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom));
}

// TODO:
//  - DEI/DEO
//  - Jump instructions (JCI/JMI/JSI)
//  - OPr modes
//  - OPk modes
//  - OPrk modes

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

    uxn_eval_asm(PAGE_PROGRAM);

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

    txt_printf("WST (");
    txt_printf("SIZE: %d)", wst_ptr - (uintptr_t)wst);
    for (size_t i = 0; i < 64; i++) {
        if (i % 8 == 0) {
            txt_printf("\n");
        }
        if (i >= (wst_ptr - (uintptr_t)wst)) {
            txt_printf("%02x ", 0);
        } else {
            txt_printf("%02x ", wst[i]);
        }
    }
    txt_printf("\n\n");
    txt_printf("RST (");
    txt_printf("SIZE: %d)", rst_ptr - (uintptr_t)rst);
    for (size_t i = 0; i < 64; i++) {
        if (i % 8 == 0) {
            txt_printf("\n");
        }
        if (i >= (rst_ptr - (uintptr_t)rst)) {
            txt_printf("%02x ", 0);
        } else {
            txt_printf("%02x ", rst[i]);
        }
    }
    // txt_printf("\n\n");
    // txt_printf("RAM (ZP)\n");
    // for (size_t i = 0; i < 64; i++) {
    //     if (i % 8 == 0) {
    //         txt_printf("\n");
    //     }
    //     txt_printf("%02x ", uxn_ram[i]);
    // }

    uintptr_t stack_size = wst_ptr - (uintptr_t)wst;
    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;
}