diff options
| -rwxr-xr-x | Makefile | 2 | ||||
| -rw-r--r-- | chip8.c | 64 | ||||
| -rw-r--r-- | roms/a.ch8 | bin | 27 -> 0 bytes | |||
| -rw-r--r-- | roms/breakout.ch8 | bin | 0 -> 232 bytes | |||
| -rw-r--r-- | roms/e.asm | 22 | ||||
| -rw-r--r-- | roms/joe.ch8 | bin | 0 -> 49 bytes | |||
| -rw-r--r-- | ui.c | 3 |
7 files changed, 64 insertions, 27 deletions
@@ -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)
@@ -1,6 +1,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> #include <arpa/inet.h> #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 Binary files differdeleted file mode 100644 index f445d49..0000000 --- a/roms/a.ch8 +++ /dev/null diff --git a/roms/breakout.ch8 b/roms/breakout.ch8 Binary files differnew file mode 100644 index 0000000..70b50db --- /dev/null +++ b/roms/breakout.ch8 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 Binary files differnew file mode 100644 index 0000000..a8f155f --- /dev/null +++ b/roms/joe.ch8 @@ -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); } |