From a95ae7068a35f8c7a65da48681eec334fa2c26d7 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Tue, 24 Sep 2019 23:48:46 -0400 Subject: draw instruction --- Makefile | 2 +- chip8.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++--- roms/a.ch8 | Bin 27 -> 0 bytes roms/breakout.ch8 | Bin 0 -> 232 bytes roms/e.asm | 22 ------------------- roms/joe.ch8 | Bin 0 -> 49 bytes ui.c | 3 ++- 7 files changed, 64 insertions(+), 27 deletions(-) delete mode 100644 roms/a.ch8 create mode 100644 roms/breakout.ch8 delete mode 100644 roms/e.asm create mode 100644 roms/joe.ch8 diff --git a/Makefile b/Makefile index b504d98..d1cb938 100755 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ all: executable win: executable all: LDFLAGS = -lncursesw -ltinfow -lpthread -win: LDFLAGS = ./win/pdcurses.a +win: LDFLAGS = ./win/pdcurses.a -lpthread executable: chip8.c ui.c util.c $(CC) $(CFLAGS) -o $(BIN) chip8.c ui.c util.c $(LDFLAGS) diff --git a/chip8.c b/chip8.c index c21b1bc..c5e5561 100644 --- a/chip8.c +++ b/chip8.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #ifdef _WIN32 @@ -14,7 +15,7 @@ #include "util.h" #include "chip8.h" -#define ROM_FILE "roms/a.ch8" +#define ROM_FILE "roms/joe.ch8" static void load_rom(); static void init_vm(); @@ -62,10 +63,16 @@ void vm_thread(void* v) { char str[5]; sprintf(str, "%x", instr); + u8 buf[255]; + + if (instr == 0xE0) { + memset(&vm.screen, 0, sizeof(vm.screen)); + } + switch (*str) { case '0': if (instr == 0x00E0) { - //clearscreen + memset(&vm.screen, 0, sizeof(vm.screen)); } else if (instr == 0x00EE) { vm.PC = vm.stack[vm.SP--]; } @@ -97,7 +104,11 @@ void vm_thread(void* v) { vm.Vx[hex_char(str[1])] = hex_str(str+2); break; case '7': + //if ((vm.Vx[hex_char(str[1])] + hex_str(str+2)) > 255) { + // vm.Vx[hex_char(str[1])] = (vm.Vx[hex_char(str[1])] + hex_str(str+2)) - 255; + //} else { vm.Vx[hex_char(str[1])] += hex_str(str+2); + //} break; case '8': switch(str[3]) { @@ -155,12 +166,59 @@ void vm_thread(void* v) { vm.Vx[hex_char(str[1])] *= 2; break; } + break; + case '9': + if (vm.Vx[hex_char(str[1])] != vm.Vx[hex_char(str[2])]) { + vm.PC += 0x2; + } + break; + case 'a': + vm.I = hex_str(str+1); + break; + case 'b': + vm.PC = hex_str(str+1) + vm.Vx[0]; + break; + case 'c': + vm.Vx[hex_char(str[1])] = (rand() % 255) & hex_str(str+2); + break; + case 'd': + memcpy(buf, &vm.rom[vm.I - 0x200], hex_char(str[3])); + for (int i = 0; i < hex_char(str[3]); i++) { + for (int s = 0; s < 8; s++) { + u8 bit = (buf[i]>>s)&1; + + if (bit) { + u8 x = (vm.Vx[hex_char(str[1])] + 7) - s; + u8 y = i + vm.Vx[hex_char(str[2])]; + + while(x > 63) { + x -= 63; + } + + while (y > 31) { + y -= 31; + } + + if (vm.screen[x][y]) { + vm.screen[x][y] = 0; + } else { + vm.screen[x][y] = 1; + } + } + } + } + break; + //case 'f': + // switch(str[2]) { + // case '2': + } vm.PC += 0x2; } int main(void) { + srand(time(NULL)); init_vm(); load_rom(); @@ -177,7 +235,7 @@ int main(void) { void *loop(void) { while(1) { vm_thread(0); - usleep(100); + usleep(1000); } return 0; } diff --git a/roms/a.ch8 b/roms/a.ch8 deleted file mode 100644 index f445d49..0000000 Binary files a/roms/a.ch8 and /dev/null differ diff --git a/roms/breakout.ch8 b/roms/breakout.ch8 new file mode 100644 index 0000000..70b50db Binary files /dev/null and b/roms/breakout.ch8 differ diff --git a/roms/e.asm b/roms/e.asm deleted file mode 100644 index 2dd3761..0000000 --- a/roms/e.asm +++ /dev/null @@ -1,22 +0,0 @@ -CLS - -LD V0, 0 -LD V1, 0 -LD V2, 0 - -loop: - -LD F, V0 -LD I, esprite -DRW V1, V2, 5 -ADD V1, 5 -ADD V2, 6 - -JP loop - -esprite: -db %11110000, - %10000000, - %11110000, - %10000000, - %11110000, diff --git a/roms/joe.ch8 b/roms/joe.ch8 new file mode 100644 index 0000000..a8f155f Binary files /dev/null and b/roms/joe.ch8 differ diff --git a/ui.c b/ui.c index 2691674..cd0bb12 100644 --- a/ui.c +++ b/ui.c @@ -19,7 +19,7 @@ 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, "[]"); + mvaddstr(y, x * 2, "[]"); } } } @@ -55,6 +55,7 @@ void curses_thread(void* v) { endwin(); exit(1); } + erase(); draw(); usleep(100); } -- cgit v1.2.3-101-g0448