aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: de566ac0f381b6e5a777eb6ae8aa7635d38975b9 (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
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "shorthand.h"
#include "ppu.c"
#include "uxn-fast.c"

static Uxn u;
static Device *devscreen;
static Device *devctrl;
static Device *devmouse;
static Device *devaudio;

typedef struct timespec Time;

Time
time_now(){
    struct timespec t;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);
    return t;
}

size_t
time_elapsed(Time since){
    struct timespec now = time_now();
    return (now.tv_sec - since.tv_sec) * 1e9 + (now.tv_nsec - since.tv_nsec);
}

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

void
console_talk(Device *d, u8 b0, u8 w) {
    (void)d;
    (void)b0;
    (void)w;
    // char stmp[2];
    // if(!w) {
    //     return;
    // }
    // switch(b0) {
    //     case 0x8: stmp[0] = d->dat[0x8]; stmp[1] = 0; uart_puts(stmp); break;
    // TODO: implement printf for the uart to be able to format
    // numbers.
    // 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;
    // }
}

static void
docolors(Device *d) {
    for(size_t i = 0; i < 4; ++i) {
        u8 r = ((d->dat[0x8 + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11;
        u8 g = ((d->dat[0xa + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11;
        u8 b = ((d->dat[0xc + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11;

        if (rgb_order) {
            palette[i] = (b << 16) | (g << 8) | r;
        } else {
            palette[i] = (r << 16) | (g << 8) | b;
        }
    }
    for(size_t i = 4; i < 16; ++i) {
        palette[i] = palette[i / 4];
    }

    // Redraw the screen if we change the color palette.
    reqdraw = 1;
    redraw_screen();
}

void
system_talk(Device *d, u8 b0, u8 w) {
    if(!w) { /* read */
        switch(b0) {
            case 0x2: d->dat[0x2] = d->u->wst.ptr; break;
            case 0x3: d->dat[0x3] = d->u->rst.ptr; break;
        }
    } else { /* write */
        switch(b0) {
            case 0x2: d->u->wst.ptr = d->dat[0x2]; break;
            case 0x3: d->u->rst.ptr = d->dat[0x3]; break;
            case 0xf: d->u->ram.ptr = 0x0000; break;
        }
        if(b0 > 0x7 && b0 < 0xe) {
            docolors(d);
        }
    }
    (void)b0;
}

void
screen_talk(Device *d, u8 b0, u8 w) {
    if (w) {
        switch (b0) {
            case 0x1: {
                          d->vector = mempeek16(d->dat, 0x0);
                      } break;
            case 0xe: {
                          u16 x = mempeek16(d->dat, 0x8);
                          u16 y = mempeek16(d->dat, 0xa);
                          u8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
                          u8 *layer = d->dat[0xe] >> 4 & 0x1 ? pixels_fg : pixels_bg;
                          u8 mode = d->dat[0xe] >> 5;
                          u8 color = d->dat[0xf] & 0xf;
                          if(!mode) {
                              ppu_pixel(layer, x, y, d->dat[0xe] & 0x3);
                          } else if(mode-- & 0x1) {
                              u8 flipx = mode & 0x2;
                              u8 flipy = mode & 0x4;
                              ppu_1bpp(layer, x, y, addr, color, flipx, flipy);
                          } else {
                              u8 flipx = mode & 0x2;
                              u8 flipy = mode & 0x4;
                              ppu_2bpp(layer, x, y, addr, color, flipx, flipy);
                          }
                          if(d->dat[0x6] & 0x01) { mempoke16(d->dat, 0x8, x + 1); }
                          if(d->dat[0x6] & 0x02) { mempoke16(d->dat, 0xa, y + 1); }
                      } break;
            case 0xf: {
                          u16 x = mempeek16(d->dat, 0x8);
                          u16 y = mempeek16(d->dat, 0xa);
                          u8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
                          u8 *layer = d->dat[0xf] >> 6 & 0x1 ? pixels_fg : pixels_bg;
                          u8 color = d->dat[0xf] & 0xf;
                          u8 flipx = (d->dat[0xf] >> 0x4) & 0x1;
                          u8 flipy = (d->dat[0xf] >> 0x5) & 0x1;
                          if(d->dat[0xf] >> 0x7 & 0x1) {
                              ppu_2bpp(layer, x, y, addr, color, flipx, flipy);
                              if(d->dat[0x6] & 0x04) {
                                  mempoke16(d->dat, 0xc, mempeek16(d->dat, 0xc) + 16);
                              }
                          } else {
                              ppu_1bpp(layer, x, y, addr, color, flipx, flipy);
                              if(d->dat[0x6] & 0x04) {
                                  mempoke16(d->dat, 0xc, mempeek16(d->dat, 0xc) + 8);
                              }
                          }
                          if(d->dat[0x6] & 0x01) { mempoke16(d->dat, 0x8, x + 8); }
                          if(d->dat[0x6] & 0x02) { mempoke16(d->dat, 0xa, y + 8); }
                      } break;
            default: break;
        }
    }
    reqdraw = 1;
}

static void
audio_talk(Device *d, u8 b0, u8 w) {
    // TODO: Implement...
    (void)d;
    (void)b0;
    (void)w;
}

void
datetime_talk(Device *d, u8 b0, u8 w) {
    // TODO: Implement...
    (void)d;
    (void)b0;
    (void)w;
}

void
file_talk(Device *d, u8 b0, u8 w) {
    // TODO: Implement...
    (void)d;
    (void)b0;
    (void)w;
}


void
load_rom(char *file_name) {
    FILE *file = fopen(file_name, "r");
    if (!file) {
        fprintf(stderr, "error: couldn't open file: %s\n", file_name);
        exit(EXIT_FAILURE);
    }

    fseek(file, 0, SEEK_END);
    size_t rom_size = ftell(file);
    fseek(file, 0, SEEK_SET);
    char *uxn_rom = malloc(rom_size);
    fread(uxn_rom, 1, rom_size, file);
    memcpy(u.ram.dat + PAGE_PROGRAM, uxn_rom, rom_size);
    fclose(file);
    free(uxn_rom);
}

void
init_uxn(char *file_name) {
    // Clear UXN memory.
    memset(&u, 0, sizeof(Uxn));

    // Copy rom to VM.
    load_rom(file_name);

    // Initialize framebuffer.
    ppu_init();

    // Prepare devices.
    uxn_port(&u, 0x0, "system", system_talk);
    uxn_port(&u, 0x1, "console", console_talk);
    devscreen = uxn_port(&u, 0x2, "screen", screen_talk);
    devaudio = uxn_port(&u, 0x3, "audio0", audio_talk);
    uxn_port(&u, 0x4, "audio1", audio_talk);
    uxn_port(&u, 0x5, "audio2", audio_talk);
    uxn_port(&u, 0x6, "audio3", audio_talk);
    uxn_port(&u, 0x7, "---", nil_talk);
    devctrl = uxn_port(&u, 0x8, "controller", nil_talk);
    devmouse = uxn_port(&u, 0x9, "mouse", nil_talk);
    uxn_port(&u, 0xa, "file", file_talk);
    uxn_port(&u, 0xb, "datetime", datetime_talk);
    uxn_port(&u, 0xc, "---", nil_talk);
    uxn_port(&u, 0xd, "---", nil_talk);
    uxn_port(&u, 0xe, "---", nil_talk);
    uxn_port(&u, 0xf, "---", nil_talk);
    mempoke16(devscreen->dat, 2, screen_width);
    mempoke16(devscreen->dat, 4, screen_height);
}

int
main(int argc, char *argv[]) {
    if (argc <= 1) {
        // TODO: If no rom is given, embed the uxn compiler rom.
        fprintf(stderr, "error: no rom selected\n");
        exit(EXIT_FAILURE);
    }
    init_uxn(argv[1]);

    // Main loop.
    uxn_eval(&u, 0x0100);
    Time frame_time = time_now();
    while (true) {
        size_t elapsed = time_elapsed(frame_time);
        if (elapsed >= 16666666) {
            // Echo input to standard output.
            uxn_eval(&u, mempeek16(devscreen->dat, 0));

            // Blit ppu.pixels to the framebuffer.
            blit_framebuffer();
            frame_time = time_now();
        }
    }

    return 0;
}