From 4c4110d6529a502efba928ffb03af64816053eb9 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 23 Sep 2019 22:39:37 -0400 Subject: improve ui and implement 2 more instructions --- Makefile | 2 +- chip8.c | 16 +++++++++++++--- chip8.h | 2 ++ ui.c | 40 ++++++++++++++++++++++++++++++++++------ 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index b504d98..a7976fe 100755 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ win: BIN = chip8.exe all: executable win: executable -all: LDFLAGS = -lncursesw -ltinfow -lpthread +all: LDFLAGS = -lncursesw -lpthread win: LDFLAGS = ./win/pdcurses.a executable: chip8.c ui.c util.c diff --git a/chip8.c b/chip8.c index c48f543..ad1b6bf 100644 --- a/chip8.c +++ b/chip8.c @@ -55,9 +55,6 @@ void push_top_of_stack(u16 val) { } void vm_thread(void* v) { - if (vm.PC > vm.rom_size) { - return; - } // read 2 bytes and convert to big-endian u16 instr = htons(vm.rom[(vm.PC) - 0x1FF]<<8 | vm.rom[(vm.PC) - 0x200]); @@ -113,6 +110,19 @@ void vm_thread(void* v) { case '2': vm.Vx[hex_char(str[1])] &= vm.Vx[hex_char(str[2])]; break; + case '3': + vm.Vx[hex_char(str[1])] ^= vm.Vx[hex_char(str[2])]; + break; + case '4': + vm.Vx[hex_char(str[1])] += vm.Vx[hex_char(str[2])] + if (vm.Vx[hex_char(str[1])] + vm.Vx[hex_char(str[2])] > 255) { + vm.Vx[15] = 1; + vm.Vx[hex_char(str[1])] &= 0x255; + } else { + vm.Vx[15] = 0; + } + break; + } } diff --git a/chip8.h b/chip8.h index 4ea248f..ce47d75 100644 --- a/chip8.h +++ b/chip8.h @@ -19,6 +19,8 @@ typedef struct { u16 I; u8 Vx[16]; + + u8 screen[64][32]; } chip8_vm; extern chip8_vm vm; diff --git a/ui.c b/ui.c index d48f3e1..2691674 100644 --- a/ui.c +++ b/ui.c @@ -5,6 +5,7 @@ #include #include "chip8.h" +#include "util.h" void init_curses() { initscr(); @@ -14,6 +15,38 @@ void init_curses() { clear(); } +void draw() { + for (int x = 0; x < 64; x++) { + for (int y = 0; y < 32; y++) { + if (vm.screen[x][y]) { + mvaddstr(x * 2, y, "[]"); + } + } + } + + char buf[25]; + for (int i = 0; i < 7; i++) { + sprintf(buf, "V%x: 0x%x", i, vm.Vx[i]); + mvaddstr(34, 9 * i, buf); + } + for (int i = 7; i < 15; i++) { + sprintf(buf, "V%x: 0x%x", i, vm.Vx[i]); + mvaddstr(35, 9 * (i - 8), buf); + } + sprintf(buf, "Vf: 0x%x", vm.Vx[15]); + mvaddstr(36, 0, buf); + sprintf(buf, "SP: 0x%x", vm.SP); + mvaddstr(36, 9, buf); + sprintf(buf, "DT: 0x%x", vm.DT); + mvaddstr(36, 18, buf); + sprintf(buf, "ST: 0x%x", vm.ST); + mvaddstr(36, 27, buf); + sprintf(buf, "PC: 0x%x", vm.PC); + mvaddstr(36, 36, buf); + sprintf(buf, "I: 0x%x", vm.I); + mvaddstr(36, 46, buf); +} + void curses_thread(void* v) { char c; while(1) { @@ -22,12 +55,7 @@ void curses_thread(void* v) { endwin(); exit(1); } - mvaddstr(0, 0, "V0 V1 V2 V3 V4 V5 V6 V7 V8 V9 VA VB VC VD VE VF"); - char buf[5]; - for (int i = 0; i < 16; i++) { - sprintf(buf, "%i", vm.Vx[i]); - mvaddstr(1, i * 4, buf); - } + draw(); usleep(100); } } -- cgit v1.2.3-101-g0448