diff options
Diffstat (limited to 'sources/main.c')
-rw-r--r-- | sources/main.c | 178 |
1 files changed, 96 insertions, 82 deletions
diff --git a/sources/main.c b/sources/main.c index e1dc077..4bc972c 100644 --- a/sources/main.c +++ b/sources/main.c @@ -351,12 +351,60 @@ void iui_draw(iui* ui) int main(int argc, char** argv) { + chip8 c8 = {0}; xctx x11 = {0}; iui ui = {0}; - bool run = true; - char txtbuf[256] = {0}; + bool run = true, cycle = false;; + + u64 sz = 0; + FILE* f = NULL; + char buf[512] = {0}; + + srand(time(NULL)); + + if(argc > 1) + { + u64 got = 0; + + f = fopen(argv[1], "r"); + if(!f) return 1; + + fseek(f, 0, SEEK_END); + sz = (u64)ftell(f); + rewind(f); + + if(sz >= (sizeof(c8.RAM) - 512)) return 2; + + got = fread(c8.RAM + C8_RESET_VECTOR, 1, sz, f); + if(got != sz) return 3; + + fclose(f); + } + else + { + c8.RAM[C8_RESET_VECTOR + iota] = 0x60; + c8.RAM[C8_RESET_VECTOR + iota] = 0x04; + + c8.RAM[C8_RESET_VECTOR + iota] = 0x61; + c8.RAM[C8_RESET_VECTOR + iota] = 0x03; + + c8.RAM[C8_RESET_VECTOR + iota] = 0x72; + c8.RAM[C8_RESET_VECTOR + iota] = 0x01; + + c8.RAM[C8_RESET_VECTOR + iota] = 0x80; + c8.RAM[C8_RESET_VECTOR + iota] = 0x14; + + c8.RAM[C8_RESET_VECTOR + iota] = 0x30; + c8.RAM[C8_RESET_VECTOR + iota] = 0x10; + + c8.RAM[C8_RESET_VECTOR + iota] = 0x12; + c8.RAM[C8_RESET_VECTOR + iota] = 0x04; + + sz = iota; + } x11_init(800, 600, "xip-8", argc, argv, &x11); + c8_reset(&c8); while(run) { @@ -385,6 +433,35 @@ int main(int argc, char** argv) len = XLookupString(&ev.xkey, name, sizeof(name) - 1, &ks, NULL); if(keyup && ks == XK_Escape) run = false; + + if(!ui.kbdfocus) + { + u16 bits = 0; + switch(name[0]) + { + case 'x': bits = bit(0); break; + case '1': bits = bit(1); break; + case '2': bits = bit(2); break; + case '3': bits = bit(3); break; + case 'q': bits = bit(4); break; + case 'w': bits = bit(5); break; + case 'e': bits = bit(6); break; + case 'd': bits = bit(7); break; + case 's': bits = bit(8); break; + case 'a': bits = bit(9); break; + case 'z': bits = bit(10); break; + case 'c': bits = bit(11); break; + case '4': bits = bit(12); break; + case 'r': bits = bit(13); break; + case 'f': bits = bit(14); break; + case 'v': bits = bit(15); break; + default: break; + } + + if(keyup) c8.keys &= ~bits; + else c8.keys |= bits; + } + if(!keyup && len == 1) { if(ui.kbdfocus && (isgraph(name[0]) || name[0] == ' ')) @@ -481,95 +558,32 @@ int main(int argc, char** argv) } iui_start(&ui, &x11); - if(iui_button(&ui, 25, 25, 100, 35, "Click ME!")) report(DBG, "CLICK!\n"); - if(iui_button(&ui, 150, 25, 100, 35, "NOT ME!!!")) report(DBG, "WHYYYYYY!\n"); - - iui_textbox(&ui, 25, 75, 225, 35, txtbuf, lengthof(txtbuf)); - if(iui_button(&ui, 275, 75, 100, 35, "OK")) report(DBG, "textbox: '%s'!\n", txtbuf); + if(iui_button(&ui, 25, 25, 100, 35, "Cycle")) cycle = !cycle; + iui_textbox(&ui, 25, 75, 225, 35, buf, lengthof(buf)); iui_end(&ui); - usleep(10000); - } - - x11_cleanup(&x11); - return 0; -} - -int main2(int argc, char** argv) -{ - u64 sz = 0; - FILE* f = NULL; - chip8 c8 = {0}; - - srand(time(NULL)); - - c8_reset(&c8); - - if(argc > 1) - { - u64 got = 0; - f = fopen(argv[1], "r"); - if(!f) return 1; - - fseek(f, 0, SEEK_END); - sz = (u64)ftell(f); - rewind(f); - - if(sz >= (sizeof(c8.RAM) - 512)) return 2; - - got = fread(c8.RAM + C8_RESET_VECTOR, 1, sz, f); - if(got != sz) return 3; - - fclose(f); - } - else - { - c8.RAM[C8_RESET_VECTOR + iota] = 0x60; - c8.RAM[C8_RESET_VECTOR + iota] = 0x04; - - c8.RAM[C8_RESET_VECTOR + iota] = 0x61; - c8.RAM[C8_RESET_VECTOR + iota] = 0x03; - - c8.RAM[C8_RESET_VECTOR + iota] = 0x72; - c8.RAM[C8_RESET_VECTOR + iota] = 0x01; - - c8.RAM[C8_RESET_VECTOR + iota] = 0x80; - c8.RAM[C8_RESET_VECTOR + iota] = 0x14; - - c8.RAM[C8_RESET_VECTOR + iota] = 0x30; - c8.RAM[C8_RESET_VECTOR + iota] = 0x10; - - c8.RAM[C8_RESET_VECTOR + iota] = 0x12; - c8.RAM[C8_RESET_VECTOR + iota] = 0x04; - - sz = iota; - } - - printf("Disasm:\n"); - { - u64 i; - char buf[512] = {0}; - - for(i = 0; i < sz; i += 2) + if(cycle) { - u16 pc = C8_RESET_VECTOR + i; - c8_disasm(c8.RAM, pc, buf); - printf("%04X: %s\n", pc, buf); + c8_disasm(c8.RAM, c8.pc, buf); + c8_exec(c8.RAM, c8.pc, &c8); } - } - printf("\nExec:\n"); - while(c8.running) - { - char buf[512] = {0}; + { + usize x, y, s = 4; - c8_disasm(c8.RAM, c8.pc, buf); - printf("%04X: %s\n", c8.pc, buf); + XSetForeground(x11.disp, x11.gc, x11.cols.as.fg); + XSetBackground(x11.disp, x11.gc, x11.cols.as.bg); - c8_exec(c8.RAM, c8.pc, &c8); - c8_dump_state(&c8, false); + for(y = 0; y < 32; y++) + for(x = 0; x < 64; x++) + if(c8.DISPLAY[y] & bit(x)) + XFillRectangle(x11.disp, x11.wnd, x11.gc, 25 + x*s, 128 + y*s, s, s); + } + + usleep(10000); } + x11_cleanup(&x11); return 0; } |