summaryrefslogtreecommitdiff
path: root/chip8.c
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2020-08-16 23:05:55 +0930
committerDaniel Jones <admin@danieljon.es>2020-08-16 23:05:55 +0930
commit32336b183a5d3cbf64d2c67100afe036cf610694 (patch)
tree6e819be7850966f962554825ad5435fa6c139551 /chip8.c
parenteb19009d216f4fb0f7911b939b2f1bc698e40c79 (diff)
downloadchip8-32336b183a5d3cbf64d2c67100afe036cf610694.tar.gz
chip8-32336b183a5d3cbf64d2c67100afe036cf610694.zip
began chip8 interpreter, reading rom file
Diffstat (limited to 'chip8.c')
-rw-r--r--chip8.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/chip8.c b/chip8.c
new file mode 100644
index 0000000..3c41117
--- /dev/null
+++ b/chip8.c
@@ -0,0 +1,41 @@
+#include "chip8.h"
+
+uint8_t key[KEY_SIZE]; // hex key input
+uint32_t video[WIDTH*HEIGHT]; // video memory TODO: video should be 8 bit not 32
+uint8_t V[16]; // registers - 0x0 to 0xF
+uint16_t I; // special I register, stores addresses
+uint16_t PC; // program counter
+uint8_t SP; // stack pointer
+uint16_t stack[STACK_SIZE]; // stack, for address storage when using subroutines, maximum of 16 levels of nested subroutines
+uint8_t memory[MEMORY_SIZE]; // 4KB of program memory
+
+int
+load_rom(char *rom)
+{
+ int ret = 1;
+ printf("loading rom %s..\n", rom);
+ FILE *romfile;
+ romfile = fopen(rom, "rb");
+ if (romfile == NULL)
+ {
+ fprintf(stderr, "cannot read rom %s: %s\n", rom, strerror(errno));
+ ret = 0;
+ }
+
+ /* read a maximum of MAX_ROM_SIZE bytes into memory starting after the reserved memory */
+ fread(&memory[0x200], 1, MAX_ROM_SIZE, romfile);
+
+ fclose(romfile);
+ return ret;
+}
+
+void
+chip8_init()
+{
+ /* clear everything */
+ memset(key, 0, 15);
+ memset(video, 0, (WIDTH*HEIGHT) * sizeof(uint32_t));
+ memset(V, 0, 16);
+ memset(stack, 0, (STACK_SIZE) * sizeof(uint16_t));
+ memset(memory, 0, MEMORY_SIZE);
+}