#include #include "common.h" #include "bitmap.h" #include "bd-font.c" #include "filesystem.c" #include "rom.c" #include "uxn/uxn.h" #include "uxn/uxn.c" #include "uxn/devices/ppu.h" #include "uxn/devices/ppu.c" #include "text.h" /* 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. */ typedef enum { CONTROL_CONTROLLER, CONTROL_MOUSE, } ControlMethod; static ControlMethod control_method = CONTROL_CONTROLLER; #define MOUSE_DELTA 1 typedef struct Mouse { int x; int y; } Mouse; static Ppu ppu; static Device *devscreen; static Device *devctrl; static Device *devmouse; static Mouse mouse = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2}; void nil_talk(Device *d, Uint8 b0, Uint8 w) { (void)d; (void)b0; (void)w; } void console_talk(Device *d, u8 b0, u8 w) { char stmp[2]; if(!w) { return; } switch(b0) { case 0x8: stmp[0] = d->dat[0x8]; stmp[1] = 0; txt_printf(stmp); 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]); } (void)b0; } IWRAM_CODE 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); } } } void datetime_talk(Device *d, Uint8 b0, Uint8 w) { (void)d; (void)b0; (void)w; } void file_talk(Device *d, Uint8 b0, Uint8 w) { Uint8 read = b0 == 0xd; if(w && (read || b0 == 0xf)) { char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)]; Uint16 result = 0, length = mempeek16(d->dat, 0xa); Uint16 offset = mempeek16(d->dat, 0x4); Uint16 addr = mempeek16(d->dat, b0 - 1); int file_idx = fs_open_file(name, read ? OPEN_READ : OPEN_WRITE); if (file_idx >= 0) { if (read) { result = fs_read(&d->mem[addr], length, file_idx, offset); } else { result = fs_write(&d->mem[addr], length, file_idx, offset, offset > 0); } } mempoke16(d->dat, 0x2, result); } } 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); devctrl = portuxn(u, 0x8, "controller", nil_talk); devmouse = portuxn(u, 0x9, "mouse", nil_talk); portuxn(u, 0xa, "file", file_talk); portuxn(u, 0xb, "datetime", datetime_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); } IWRAM_CODE void handle_input(Uxn *u) { poll_keys(); if (key_tap(KEY_SELECT)) { switch (control_method) { case CONTROL_CONTROLLER: { devctrl->dat[2] = 0; evaluxn(u, mempeek16(devctrl->dat, 0)); devctrl->dat[3] = 0; control_method = CONTROL_MOUSE; } break; default: { devmouse->dat[6] = 0; devmouse->dat[7] = 0; mempoke16(devmouse->dat, 0x2, -10); mempoke16(devmouse->dat, 0x4, -10); evaluxn(u, mempeek16(devmouse->dat, 0)); control_method = CONTROL_CONTROLLER; } break; } } if (control_method == CONTROL_CONTROLLER) { // TODO: We don't need ifs if we use KEY_INPUTS directly and mayvbe just // swap some things if needed. Uint8 *flag = &devctrl->dat[2]; if (key_pressed(KEY_A)) { *flag |= 0x01; } else { *flag &= ~0x01; } if (key_pressed(KEY_B)) { *flag |= 0x02; } else { *flag &= ~0x02; } if (key_pressed(KEY_L)) { *flag |= 0x04; } else { *flag &= ~0x04; } if (key_pressed(KEY_R)) { *flag |= 0x08; } else { *flag &= ~0x08; } if (key_pressed(KEY_UP)) { *flag |= 0x10; } else { *flag &= ~0x10; } if (key_pressed(KEY_DOWN)) { *flag |= 0x20; } else { *flag &= ~0x20; } if (key_pressed(KEY_LEFT)) { *flag |= 0x40; } else { *flag &= ~0x40; } if (key_pressed(KEY_RIGHT)) { *flag |= 0x80; } else { *flag &= ~0x80; } evaluxn(u, mempeek16(devctrl->dat, 0)); devctrl->dat[3] = 0; } else if (control_method == CONTROL_MOUSE) { // Detect "mouse key press". Uint8 flag = devmouse->dat[6]; if (key_tap(KEY_B)) { flag |= 0x01; } else if (key_released(KEY_B)) { flag &= ~0x01; } if (key_tap(KEY_A)) { flag |= 0x10; } else if (key_released(KEY_A)) { flag &= ~0x10; } // Handle chording. devmouse->dat[6] = flag; if(flag == 0x10 && (devmouse->dat[6] & 0x01)) { devmouse->dat[7] = 0x01; } if(flag == 0x01 && (devmouse->dat[6] & 0x10)) { devmouse->dat[7] = 0x10; } // Detect mouse movement. if (key_pressed(KEY_UP)) { mouse.y = CLAMP(mouse.y - MOUSE_DELTA, 0, SCREEN_HEIGHT - 8); } else if (key_pressed(KEY_DOWN)) { mouse.y = CLAMP(mouse.y + MOUSE_DELTA, 0, SCREEN_HEIGHT - 8); } if (key_pressed(KEY_LEFT)) { mouse.x = CLAMP(mouse.x - MOUSE_DELTA, 0, SCREEN_WIDTH - 8); } else if (key_pressed(KEY_RIGHT)) { mouse.x = CLAMP(mouse.x + MOUSE_DELTA, 0, SCREEN_WIDTH - 8); } // Eval mouse. mempoke16(devmouse->dat, 0x2, mouse.x); mempoke16(devmouse->dat, 0x4, mouse.y); evaluxn(u, mempeek16(devmouse->dat, 0)); } } static Uxn u; EWRAM_BSS static Uint8 umem[65536]; int main(void) { // Initialize filesystem. fs_init(); // Register interrupts. irq_init(); irs_set(IRQ_VBLANK, irs_stub); // Initialize VM. memset(&u, 0, sizeof(u)); u.ram.dat = umem; init_uxn(&u); // Initialize text engine. txt_init_hybrid( TXT_MODE_HYBRID, (Font){ .data = bd_font, .char_height = 8, .char_width = 8, }, ppu.fg); txt_position(0,0); // Main loop. int frame_counter = 0; evaluxn(&u, 0x0100); while(true) { bios_vblank_wait(); handle_input(&u); evaluxn(&u, mempeek16(devscreen->dat, 0)); flipbuf(&ppu); frame_counter++; } return 0; }