summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2020-08-16 23:33:50 +0930
committerDaniel Jones <admin@danieljon.es>2020-08-16 23:33:50 +0930
commit43b01c12d1192830034807e664843f780df8b7bb (patch)
tree49e3858587bdb59b951f5de32b7c77c9ba079aa4
parent32336b183a5d3cbf64d2c67100afe036cf610694 (diff)
downloadchip8-43b01c12d1192830034807e664843f780df8b7bb.tar.gz
chip8-43b01c12d1192830034807e664843f780df8b7bb.zip
more initialisation and added the fontset
fontset also loaded into memory at 0x0, we have 0x0 to 0x1FF free for anything we want, that is where the interpreter is meant to be
-rw-r--r--chip8.c49
-rw-r--r--chip8.h2
2 files changed, 47 insertions, 4 deletions
diff --git a/chip8.c b/chip8.c
index 3c41117..2ce911f 100644
--- a/chip8.c
+++ b/chip8.c
@@ -1,5 +1,6 @@
#include "chip8.h"
+uint16_t opcode; // 2 byte opcode
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
@@ -8,6 +9,30 @@ 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 draw_flag; // draw flag, when 1 the screen will be drawn
+uint8_t delay_timer; // delay timer, decremented by one at 60hz, at 0 do nothing
+uint8_t sound_timer; // sound timer, decremented by one at 60hz, while it is > 0 a beep is played
+
+/* 0 - F hexidecimal font as required by the chip8 spec */
+uint8_t chip8_fontset[80] =
+{
+ 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
+ 0x20, 0x60, 0x20, 0x20, 0x70, // 1
+ 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
+ 0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
+ 0x90, 0x90, 0xF0, 0x10, 0x10, // 4
+ 0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
+ 0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
+ 0xF0, 0x10, 0x20, 0x40, 0x40, // 7
+ 0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
+ 0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
+ 0xF0, 0x90, 0xF0, 0x90, 0x90, // A
+ 0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
+ 0xF0, 0x80, 0x80, 0x80, 0xF0, // C
+ 0xE0, 0x90, 0x90, 0x90, 0xE0, // D
+ 0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
+ 0xF0, 0x80, 0xF0, 0x80, 0x80 // F
+};
int
load_rom(char *rom)
@@ -32,10 +57,26 @@ load_rom(char *rom)
void
chip8_init()
{
- /* clear everything */
- memset(key, 0, 15);
+ /* clear everything and set sane defaults */
+ memset(key, 0, sizeof(uint8_t) * 15);
memset(video, 0, (WIDTH*HEIGHT) * sizeof(uint32_t));
- memset(V, 0, 16);
+ memset(V, 0, sizeof(uint8_t) * 16);
memset(stack, 0, (STACK_SIZE) * sizeof(uint16_t));
- memset(memory, 0, MEMORY_SIZE);
+ memset(memory, 0, sizeof(uint8_t) * MEMORY_SIZE);
+
+ PC = 0x200; // set program counter to where roms are stored
+ opcode = 0;
+ I = 0;
+ SP = 0;
+ draw_flag = 1; // draw by default i guess
+ delay_timer = 0;
+ sound_timer = 0;
+ srand(time(NULL)); // seed rng
+
+ /* load font into memory at 0x00: we have 0x00 to 0x1FF free for anything we want */
+ //TODO: add whole alphabet, then can write a rom to write strings or something
+ for (uint8_t i = 0; i < 80; i++)
+ {
+ memory[0x0 + i] = chip8_fontset[i];
+ }
}
diff --git a/chip8.h b/chip8.h
index 7301c45..4faa6e4 100644
--- a/chip8.h
+++ b/chip8.h
@@ -2,9 +2,11 @@
#define CHIP8_H
#include <stdio.h>
+#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
+#include <time.h>
#define WIDTH 64
#define HEIGHT 32