#include /* Copyright (c) 2021 Devine Lu Linvega 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 "uxn.h" #pragma mark - Core int error(char *msg, const char *err) { printf("Error %s: %s\n", msg, err); return 0; } #pragma mark - Devices Uint8 console_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) { Uint8 *m = u->ram.dat; switch(b0) { case 0x08: printf("%c", b1); break; case 0x09: printf("0x%02x\n", b1); break; case 0x0b: printf("0x%04x\n", (m[ptr + 0x0a] << 8) + b1); break; } fflush(stdout); (void)m; (void)ptr; (void)b0; return b1; } Uint8 file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) { Uint8 *m = u->ram.dat; char *name = (char *)&m[(m[ptr + 8] << 8) + m[ptr + 8 + 1]]; Uint16 length = (m[ptr + 8 + 2] << 8) + m[ptr + 8 + 3]; if(b0 == 0x0d) { Uint16 addr = (m[ptr + 8 + 4] << 8) + b1; FILE *f = fopen(name, "r"); if(f && fread(&m[addr], length, 1, f)) { fclose(f); printf("Loaded %d bytes, at %04x from %s\n", length, addr, name); } } else if(b0 == 0x0f) { Uint16 addr = (m[ptr + 8 + 6] << 8) + b1; FILE *f = fopen(name, "w"); if(fwrite(&m[addr], length, 1, f)) { fclose(f); printf("Saved %d bytes, at %04x from %s\n", length, addr, name); } } return b1; } Uint8 ppnil(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) { (void)u; (void)ptr; (void)b0; return b1; } #pragma mark - Generics int start(Uxn *u) { printf("RESET --------\n"); if(!evaluxn(u, u->vreset)) return error("Reset", "Failed"); printf("FRAME --------\n"); if(!evaluxn(u, u->vframe)) return error("Frame", "Failed"); return 1; } int main(int argc, char **argv) { Uxn u; if(argc < 2) return error("Input", "Missing"); if(!bootuxn(&u)) return error("Boot", "Failed"); if(!loaduxn(&u, argv[1])) return error("Load", "Failed"); portuxn(&u, "console", console_poke); portuxn(&u, "empty", ppnil); portuxn(&u, "empty", ppnil); portuxn(&u, "empty", ppnil); portuxn(&u, "empty", ppnil); portuxn(&u, "empty", ppnil); portuxn(&u, "file", file_poke); start(&u); return 0; }