aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c124
1 files changed, 51 insertions, 73 deletions
diff --git a/src/main.c b/src/main.c
index f2ffa5b..fc073c9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,6 +4,7 @@
4#include "rom.c" 4#include "rom.c"
5 5
6static Uxn u; 6static Uxn u;
7static Ppu ppu;
7static Device *devscreen; 8static Device *devscreen;
8static Device *devctrl; 9static Device *devctrl;
9static Device *devmouse; 10static Device *devmouse;
@@ -24,6 +25,8 @@ console_talk(Device *d, u8 b0, u8 w) {
24 } 25 }
25 switch(b0) { 26 switch(b0) {
26 case 0x8: stmp[0] = d->dat[0x8]; stmp[1] = 0; uart_puts(stmp); break; 27 case 0x8: stmp[0] = d->dat[0x8]; stmp[1] = 0; uart_puts(stmp); break;
28 // TODO: implement printf for the uart to be able to format
29 // numbers.
27 // case 0x9: txt_printf("0x%02x", d->dat[0x9]); break; 30 // case 0x9: txt_printf("0x%02x", d->dat[0x9]); break;
28 // case 0xb: txt_printf("0x%04x", mempeek16(d->dat, 0xa)); break; 31 // case 0xb: txt_printf("0x%04x", mempeek16(d->dat, 0xa)); break;
29 // case 0xd: txt_printf("%s", &d->mem[mempeek16(d->dat, 0xc)]); break; 32 // case 0xd: txt_printf("%s", &d->mem[mempeek16(d->dat, 0xc)]); break;
@@ -32,72 +35,55 @@ console_talk(Device *d, u8 b0, u8 w) {
32 35
33static void 36static void
34docolors(Device *d) { 37docolors(Device *d) {
35 for(size_t i = 0; i < 4; ++i) { 38 for(size_t i = 0; i < 4; ++i) {
36 u8 r = ((d->dat[0x8 + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11; 39 u8 r = ((d->dat[0x8 + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11;
37 u8 g = ((d->dat[0xa + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11; 40 u8 g = ((d->dat[0xa + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11;
38 u8 b = ((d->dat[0xc + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11; 41 u8 b = ((d->dat[0xc + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11;
39 palette[i] = (b << 16) | (g << 8) | r; 42 palette[i] = (b << 16) | (g << 8) | r;
40 } 43 }
44 for(size_t i = 4; i < 16; ++i) {
45 palette[i] = palette[i / 4];
46 }
41} 47}
42 48
43void 49void
44system_talk(Device *d, u8 b0, u8 w) { 50system_talk(Device *d, u8 b0, u8 w) {
45 // uart_puts("system_talk\n"); 51 if(!w) { /* read */
46 // TODO: Implement... 52 switch(b0) {
47 if(!w) { /* read */ 53 case 0x2: d->dat[0x2] = d->u->wst.ptr; break;
48 switch(b0) { 54 case 0x3: d->dat[0x3] = d->u->rst.ptr; break;
49 case 0x2: d->dat[0x2] = d->u->wst.ptr; break; 55 }
50 case 0x3: d->dat[0x3] = d->u->rst.ptr; break; 56 } else { /* write */
51 } 57 switch(b0) {
52 } else { /* write */ 58 case 0x2: d->u->wst.ptr = d->dat[0x2]; break;
53 switch(b0) { 59 case 0x3: d->u->rst.ptr = d->dat[0x3]; break;
54 case 0x2: d->u->wst.ptr = d->dat[0x2]; break; 60 case 0xf: d->u->ram.ptr = 0x0000; break;
55 case 0x3: d->u->rst.ptr = d->dat[0x3]; break;
56 case 0xf: d->u->ram.ptr = 0x0000; break;
57 }
58 if(b0 > 0x7 && b0 < 0xe) {
59 docolors(d);
60 } 61 }
61 } 62 if(b0 > 0x7 && b0 < 0xe) {
63 docolors(d);
64 }
65 }
62 (void)b0; 66 (void)b0;
63} 67}
64 68
65void 69void
66screen_talk(Device *d, u8 b0, u8 w) { 70screen_talk(Device *d, u8 b0, u8 w) {
67 // uart_puts("screen_talk\n"); 71 // uart_puts("screen_talk\n");
68 // TODO: Implement... 72 if(w && b0 == 0xe) {
69 // if(w && b0 == 0xe) { 73 Uint16 x = mempeek16(d->dat, 0x8);
70 // u16 x = mempeek16(d->dat, 0x8); 74 Uint16 y = mempeek16(d->dat, 0xa);
71 // u16 y = mempeek16(d->dat, 0xa); 75 Uint8 layer = d->dat[0xe] >> 4 & 0x1;
72 // u8 *addr = &d->mem[mempeek16(d->dat, 0xc)]; 76 ppu_pixel(&ppu, layer, x, y, d->dat[0xe] & 0x3);
73 // u8 *layer = d->dat[0xe] >> 4 & 0x1 ? ppu.fg : ppu.bg; 77 } else if(w && b0 == 0xf) {
74 // u8 mode = d->dat[0xe] >> 5; 78 Uint16 x = mempeek16(d->dat, 0x8);
75 // u8 color = d->dat[0xf] & 0xf; 79 Uint16 y = mempeek16(d->dat, 0xa);
76 // if(!mode) { 80 Uint8 layer = d->dat[0xf] >> 0x6 & 0x1;
77 // ppu_pixel(layer, x, y, d->dat[0xe] & 0x3); 81 Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
78 // } else if(mode-- & 0x1) { 82 if(d->dat[0xf] >> 0x7 & 0x1)
79 // u8 flipx = mode & 0x2; 83 ppu_2bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] >> 0x4 & 0x1, d->dat[0xf] >> 0x5 & 0x1);
80 // u8 flipy = mode & 0x4; 84 else
81 // ppu_1bpp(layer, x, y, addr, color, flipx, flipy); 85 ppu_1bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] >> 0x4 & 0x1, d->dat[0xf] >> 0x5 & 0x1);
82 // } else { 86 }
83 // u8 flipx = mode & 0x2;
84 // u8 flipy = mode & 0x4;
85 // ppu_2bpp(layer, x, y, addr, color, flipx, flipy);
86 // }
87 // } else if(w && b0 == 0xf) {
88 // u16 x = mempeek16(d->dat, 0x8);
89 // u16 y = mempeek16(d->dat, 0xa);
90 // u8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
91 // u8 *layer = d->dat[0xf] >> 6 & 0x1 ? ppu.fg : ppu.bg;
92 // u8 color = d->dat[0xf] & 0xf;
93 // u8 flipx = (d->dat[0xf] >> 0x4) & 0x1;
94 // u8 flipy = (d->dat[0xf] >> 0x5) & 0x1;
95 // if(d->dat[0xf] >> 0x7 & 0x1) {
96 // ppu_2bpp(layer, x, y, addr, color, flipx, flipy);
97 // } else {
98 // ppu_1bpp(layer, x, y, addr, color, flipx, flipy);
99 // }
100 // }
101} 87}
102 88
103static void 89static void
@@ -111,8 +97,8 @@ audio_talk(Device *d, u8 b0, u8 w) {
111 97
112void 98void
113datetime_talk(Device *d, u8 b0, u8 w) { 99datetime_talk(Device *d, u8 b0, u8 w) {
114 // uart_puts("datetime_talk\n");
115 // TODO: Implement... 100 // TODO: Implement...
101 // uart_puts("datetime_talk\n");
116 (void)d; 102 (void)d;
117 (void)b0; 103 (void)b0;
118 (void)w; 104 (void)w;
@@ -120,8 +106,8 @@ datetime_talk(Device *d, u8 b0, u8 w) {
120 106
121void 107void
122file_talk(Device *d, u8 b0, u8 w) { 108file_talk(Device *d, u8 b0, u8 w) {
123 // uart_puts("file_talk\n");
124 // TODO: Implement... 109 // TODO: Implement...
110 // uart_puts("file_talk\n");
125 (void)d; 111 (void)d;
126 (void)b0; 112 (void)b0;
127 (void)w; 113 (void)w;
@@ -131,7 +117,7 @@ void
131init_uxn() { 117init_uxn() {
132 uart_puts("Initializing UXN.\n"); 118 uart_puts("Initializing UXN.\n");
133 uart_init(); 119 uart_init();
134 ppu_init(); 120 ppu_init(&ppu, SCREEN_WIDTH / 8, SCREEN_HEIGHT / 8);
135 121
136 // Copy rom to VM. 122 // Copy rom to VM.
137 memcpy(u.ram.dat + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom)); 123 memcpy(u.ram.dat + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom));
@@ -153,8 +139,8 @@ init_uxn() {
153 uxn_port(&u, 0xd, "---", nil_talk); 139 uxn_port(&u, 0xd, "---", nil_talk);
154 uxn_port(&u, 0xe, "---", nil_talk); 140 uxn_port(&u, 0xe, "---", nil_talk);
155 uxn_port(&u, 0xf, "---", nil_talk); 141 uxn_port(&u, 0xf, "---", nil_talk);
156 // mempoke16(devscreen->dat, 2, ppu.hor * 8); 142 mempoke16(devscreen->dat, 2, ppu.width);
157 // mempoke16(devscreen->dat, 4, ppu.ver * 8); 143 mempoke16(devscreen->dat, 4, ppu.height);
158} 144}
159 145
160void main(void) { 146void main(void) {
@@ -166,19 +152,11 @@ void main(void) {
166 // Echo input to standard output. 152 // Echo input to standard output.
167 uxn_eval(&u, mempeek16(devscreen->dat, 0)); 153 uxn_eval(&u, mempeek16(devscreen->dat, 0));
168 154
169 // DEBUG: testing fb drawing. 155 // Blit ppu.pixels to the framebuffer.
170 u32 lines = SCREEN_WIDTH * SCREEN_HEIGHT / 4; 156 // NOTE: This is very inefficient, we likely want to keep track of the
171 for (size_t i = 0; i < lines; i++) { 157 // lines/tiles that have changed and only copy those.
172 framebuffer[i] = palette[0]; 158 for (size_t i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) {
173 } 159 framebuffer[i] = palette[ppu.pixels[i]];
174 for (size_t i = lines; i < lines * 2; i++) {
175 framebuffer[i] = palette[1];
176 }
177 for (size_t i = lines * 2; i < lines * 3; i++) {
178 framebuffer[i] = palette[2];
179 }
180 for (size_t i = lines * 3; i < lines * 4; i++) {
181 framebuffer[i] = palette[3];
182 } 160 }
183 } 161 }
184} 162}