summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2019-09-25 17:48:01 -0400
committerAndrew Opalach <andrew@akon.city> 2019-09-25 17:48:01 -0400
commit1b26a6e8221f19e5a2d6e16a1e2f4585ae36eb4e (patch)
tree117ffb0af974914363ae0f6c7aa57dce05bf62e4
parenta95ae7068a35f8c7a65da48681eec334fa2c26d7 (diff)
downloadchip8-1b26a6e8221f19e5a2d6e16a1e2f4585ae36eb4e.tar.gz
chip8-1b26a6e8221f19e5a2d6e16a1e2f4585ae36eb4e.tar.bz2
chip8-1b26a6e8221f19e5a2d6e16a1e2f4585ae36eb4e.zip
finish all but one instruction
-rw-r--r--chip8.c60
-rw-r--r--chip8.h2
-rw-r--r--roms/tetris.rombin0 -> 494 bytes
-rw-r--r--ui.c32
-rw-r--r--ui.h4
5 files changed, 76 insertions, 22 deletions
diff --git a/chip8.c b/chip8.c
index c5e5561..b66d331 100644
--- a/chip8.c
+++ b/chip8.c
@@ -1,6 +1,8 @@
#include <stdio.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
+#include <math.h>
#include <time.h>
#include <arpa/inet.h>
@@ -15,7 +17,7 @@
#include "util.h"
#include "chip8.h"
-#define ROM_FILE "roms/joe.ch8"
+#define ROM_FILE "roms/e.ch8"
static void load_rom();
static void init_vm();
@@ -104,11 +106,7 @@ 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]) {
@@ -191,13 +189,8 @@ void vm_thread(void* v) {
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;
- }
+ while (x > 63) { x -= 63; }
+ while (y > 31) { y -= 31; }
if (vm.screen[x][y]) {
vm.screen[x][y] = 0;
@@ -208,10 +201,45 @@ void vm_thread(void* v) {
}
}
break;
- //case 'f':
- // switch(str[2]) {
- // case '2':
-
+ case 'e':
+ if (hex_str(str+2) == 0x9E) {
+ if (vm.keyboard[vm.Vx[hex_char(str[1])]]) {
+ vm.PC += 0x2;
+ }
+ } else if (hex_str(str+2) == 0xA1) {
+ if (!vm.keyboard[vm.Vx[hex_char(str[1])]]) {
+ vm.PC += 0x2;
+ }
+ }
+ break;
+ case 'f':
+ if (hex_str(str+2) == 0x07) {
+ vm.Vx[hex_char(str[1])] = vm.DT;
+ } else if (hex_str(str+2) == 0x0A) {
+ vm.Vx[hex_char(str[1])] = ui_get_key(true);
+ } else if (hex_str(str+2) == 0x15) {
+ vm.DT = vm.Vx[hex_char(str[1])];
+ } else if (hex_str(str+2) == 0x18) {
+ vm.ST = vm.Vx[hex_char(str[1])];
+ } else if (hex_str(str+2) == 0x1E) {
+ vm.I += vm.Vx[hex_char(str[1])];
+ } else if (hex_str(str+2) == 0x29) {
+ // characters
+ } else if (hex_str(str+2) == 0x33) {
+ u8 tmp = vm.Vx[hex_char(str[1])];
+ vm.rom[vm.I - 0x200] = floor(tmp / 100);
+ vm.rom[(vm.I - 0x200)+1] = floor((tmp % 100)/10);
+ vm.rom[(vm.I - 0x200)+2] = floor(tmp % 10);
+ } else if (hex_str(str+2) == 0x55) {
+ for (int i = 0; i < hex_char(str[1]); i++) {
+ vm.rom[(vm.I - 0x200) - i] = vm.Vx[i];
+ }
+ } else if (hex_str(str+2) == 0x65) {
+ for (int i = 0; i < hex_char(str[1]); i++) {
+ vm.Vx[i] = vm.rom[(vm.I - 0x200) - i];
+ }
+ }
+ break;
}
vm.PC += 0x2;
diff --git a/chip8.h b/chip8.h
index ce47d75..490ef94 100644
--- a/chip8.h
+++ b/chip8.h
@@ -20,6 +20,8 @@ typedef struct {
u8 Vx[16];
+ u8 keyboard[16];
+
u8 screen[64][32];
} chip8_vm;
diff --git a/roms/tetris.rom b/roms/tetris.rom
new file mode 100644
index 0000000..9f5e087
--- /dev/null
+++ b/roms/tetris.rom
Binary files differ
diff --git a/ui.c b/ui.c
index cd0bb12..b8039ad 100644
--- a/ui.c
+++ b/ui.c
@@ -1,5 +1,6 @@
#include <ncurses.h>
#include <string.h>
+#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
@@ -47,16 +48,35 @@ void draw() {
mvaddstr(36, 46, buf);
}
+u8 ui_get_key(bool block) {
+ do {
+ char c = getch();
+ switch (c) {
+ case '1': return 0x0;
+ case '2': return 0x1;
+ case '3': return 0x2;
+ case '4': return 0x3;
+ case 'q': return 0x4;
+ case 'w': return 0x5;
+ case 'e': return 0x6;
+ case 'r': return 0x7;
+ case 'a': return 0x8;
+ case 's': return 0x9;
+ case 'd': return 0xA;
+ case 'f': return 0xB;
+ case 'z': return 0xC;
+ case 'x': return 0xD;
+ case 'c': return 0xE;
+ case 'v': return 0xF;
+ }
+ } while (block);
+}
+
void curses_thread(void* v) {
- char c;
while(1) {
- c = getch();
- if (c == 'q') {
- endwin();
- exit(1);
- }
erase();
draw();
+ vm.keyboard[ui_get_key(false)] = 1;
usleep(100);
}
}
diff --git a/ui.h b/ui.h
index ea588df..5944c32 100644
--- a/ui.h
+++ b/ui.h
@@ -1,4 +1,8 @@
#pragma once
+#include <stdbool.h>
+#include "util.h"
+
void curses_thread(void* v);
+u8 ui_get_key(bool block);
void init_curses();