aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 77cabcc67ad33f1e87e5a9e2e011049460b0aaa6 (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
#include <string.h>

#include "common.h"
#include "bitmap.h"
#include "text.h"
#include "small-font.c"

#include "uxn/uxn.h"
#include "uxn/uxn.c"
#include "uxn/devices/ppu.h"
#include "uxn/devices/ppu.c"
// #include "uxn/roms/console.c"
#include "uxn/roms/dvd.c"
// #include "uxn/roms/proportional_fonts.c"
// #include "uxn/roms/automata.c"
// #include "uxn/roms/life.c"

static Ppu ppu;
u8 reqdraw = 0;
static Device *devscreen;

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

void
console_talk(Device *d, u8 b0, u8 w) {
	if(!w) {
        return;
    }
    switch(b0) {
        case 0x8: txt_printf("%c", d->dat[0x8]); 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, Uint8 b0, Uint8 w)
{
	if(!w) {
		d->dat[0x2] = d->u->wst.ptr;
		d->dat[0x3] = d->u->rst.ptr;
	} else {
		putcolors(&ppu, &d->dat[0x8]);
		reqdraw = 1;
	}
	(void)b0;
}

void
screen_talk(Device *d, Uint8 b0, Uint8 w) {
	if(w && b0 == 0xe) {
		Uint16 x = mempeek16(d->dat, 0x8);
		Uint16 y = mempeek16(d->dat, 0xa);
		Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
		Uint8 *layer = d->dat[0xe] >> 4 & 0x1 ? ppu.fg : ppu.bg;
		Uint8 mode = d->dat[0xe] >> 5;
		if(!mode) {
			putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3);
		} else if(mode-- & 0x1) {
			puticn(&ppu, layer, x, y, addr, d->dat[0xe] & 0xf, mode & 0x2, mode & 0x4);
        } else {
			putchr(&ppu, layer, x, y, addr, d->dat[0xe] & 0xf, mode & 0x2, mode & 0x4);
        }
		reqdraw = 1;
	}
}

void
redraw(Uint16 *dst, Uxn *u) {
    // TODO: The screen will flicker but using Mode4 for double buffering would
    // be way too slow.
	drawppu(&ppu);
    clear_screen_m3();
    // Copy ppu data to framebuffer.
    for (size_t j = 0; j < ppu.height; ++j) {
        for (size_t i = 0; i < ppu.width; ++i) {
            FRAMEBUFFER[j][i] = ppu.output[i + j * ppu.width];
        }
    }
	reqdraw = 0;
}

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

	// 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);
	portuxn(u, 0x3, "---", nil_talk);
	portuxn(u, 0x4, "---", nil_talk);
	portuxn(u, 0x5, "---", nil_talk);
	portuxn(u, 0x6, "---", nil_talk);
	portuxn(u, 0x7, "---", nil_talk);
	portuxn(u, 0x8, "---", nil_talk);
	portuxn(u, 0x9, "---", nil_talk);
	portuxn(u, 0xa, "---", nil_talk);
	portuxn(u, 0xb, "---", nil_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);
}

int main(void) {
    // Initialize display mode and bg palette.
    DISP_CTRL = DISP_MODE_3 | DISP_BG_2;

    // Initialize text engine.
    txt_init_bitmap(
            TXT_MODE_MODE3,
            (Font){
                .data = small_font,
                .char_width = 4,
                .char_height = 8,
                .color = COLOR_BLUE,
                .char_map = small_font_map,
            });

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

    // Initialize VM.
    Uxn u = {0};
    init_uxn(&u);
	evaluxn(&u, 0x0100);

    // Main loop.
    int frame_counter = 0;
    while(true) {
        bios_vblank_wait();
        poll_keys();
		evaluxn(&u, mempeek16(devscreen->dat, 0));
		if(reqdraw) {
			redraw(ppu.output, &u);
        }
        frame_counter++;
    };

    return 0;
}