summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xMakefile2
-rw-r--r--chip8.c64
-rw-r--r--roms/a.ch8bin27 -> 0 bytes
-rw-r--r--roms/breakout.ch8bin0 -> 232 bytes
-rw-r--r--roms/e.asm22
-rw-r--r--roms/joe.ch8bin0 -> 49 bytes
-rw-r--r--ui.c3
7 files changed, 64 insertions, 27 deletions
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 <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
deleted file mode 100644
index f445d49..0000000
--- a/roms/a.ch8
+++ /dev/null
Binary files differ
diff --git a/roms/breakout.ch8 b/roms/breakout.ch8
new file mode 100644
index 0000000..70b50db
--- /dev/null
+++ b/roms/breakout.ch8
Binary files 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
--- /dev/null
+++ b/roms/joe.ch8
Binary files 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);
}