feat: initial version
This commit is contained in:
parent
0a67fb21d1
commit
ee790e99d6
12 changed files with 798 additions and 0 deletions
34
hdrs/chip8.h
Normal file
34
hdrs/chip8.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef CHIP8_H
|
||||
# define CHIP8_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "./memory.h"
|
||||
#include "./graphics.h"
|
||||
#include "./input.h"
|
||||
|
||||
#define FRAME_TIME_MS 1000 / 60
|
||||
|
||||
typedef struct s_chip8
|
||||
{
|
||||
t_memory memory;
|
||||
t_display display;
|
||||
t_keyboard keyboard;
|
||||
|
||||
uint8_t v[16];
|
||||
uint16_t i;
|
||||
uint16_t pc;
|
||||
|
||||
uint8_t delay_timer;
|
||||
uint8_t sound_timer;
|
||||
|
||||
uint16_t stack[16];
|
||||
uint8_t sp;
|
||||
|
||||
int running;
|
||||
} t_chip8;
|
||||
|
||||
int chip8_load_rom(t_chip8 *emu, const char *path);
|
||||
void chip8_init(t_chip8 *emu);
|
||||
void chip8_run(t_chip8 *emu);
|
||||
|
||||
#endif
|
||||
23
hdrs/cpu.h
Normal file
23
hdrs/cpu.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef CPU_H
|
||||
# define CPU_H
|
||||
|
||||
#include "./chip8.h"
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct s_instruction
|
||||
{
|
||||
uint16_t opcode;
|
||||
uint8_t type;
|
||||
uint8_t x;
|
||||
uint8_t y;
|
||||
uint8_t n;
|
||||
uint8_t nn;
|
||||
uint16_t nnn;
|
||||
} t_instruction;
|
||||
|
||||
|
||||
uint16_t cpu_fetch(t_chip8 *emu);
|
||||
t_instruction cpu_decode(uint16_t opcode);
|
||||
void cpu_execute(t_chip8 *emu, t_instruction instruction);
|
||||
|
||||
#endif
|
||||
27
hdrs/font.h
Normal file
27
hdrs/font.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef FONT_H
|
||||
#define FONT_H
|
||||
|
||||
#define FONTSET_SIZE 80
|
||||
#define FONTSET_START (MEMORY_SIZE - FONTSET_SIZE)
|
||||
|
||||
static const unsigned char FONTSET[FONTSET_SIZE] = {
|
||||
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
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
29
hdrs/graphics.h
Normal file
29
hdrs/graphics.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef GRAPHICS_H
|
||||
#define GRAPHICS_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct s_chip8;
|
||||
typedef struct s_chip8 t_chip8;
|
||||
|
||||
#define SCREEN_WIDTH 64
|
||||
#define SCREEN_HEIGHT 32
|
||||
#define SCALE 12
|
||||
|
||||
typedef struct s_display
|
||||
{
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Texture *texture;
|
||||
uint32_t pixels[SCREEN_WIDTH * SCREEN_HEIGHT];
|
||||
} t_display;
|
||||
|
||||
int graphics_init(t_display *display);
|
||||
void graphics_render(t_chip8 *emu);
|
||||
void graphics_cleanup(t_display *display);
|
||||
void graphics_draw_sprite(t_chip8 *emu, uint8_t x, uint8_t y, uint8_t n);
|
||||
int graphics_display_ready();
|
||||
|
||||
|
||||
#endif
|
||||
26
hdrs/input.h
Normal file
26
hdrs/input.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef INPUT_H
|
||||
#define INPUT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#define KEY_COUNT 16
|
||||
|
||||
struct s_chip8;
|
||||
typedef struct s_chip8 t_chip8;
|
||||
|
||||
typedef struct s_keyboard
|
||||
{
|
||||
uint8_t keyboard[KEY_COUNT];
|
||||
uint8_t waiting_for_key;
|
||||
uint8_t key_pressed;
|
||||
uint8_t key_register;
|
||||
} t_keyboard;
|
||||
|
||||
|
||||
void input_update(SDL_Event *event, uint8_t *keyboard);
|
||||
uint8_t map_sdl_to_chip8(SDL_Keycode key);
|
||||
uint8_t get_pressed_key(t_chip8 *emu);
|
||||
|
||||
|
||||
#endif // INPUT_H
|
||||
22
hdrs/memory.h
Normal file
22
hdrs/memory.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef MEMORY_H
|
||||
# define MEMORY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "graphics.h"
|
||||
|
||||
#define MEMORY_SIZE 4096
|
||||
|
||||
typedef struct s_memory
|
||||
{
|
||||
uint8_t memory[MEMORY_SIZE];
|
||||
uint8_t screen[SCREEN_WIDTH * SCREEN_HEIGHT];
|
||||
} t_memory;
|
||||
|
||||
void memory_init(t_memory *mem);
|
||||
int memory_load_rom(t_memory *mem, FILE *fp);
|
||||
void memory_load_fontset(t_memory *mem);
|
||||
void memory_clear_screen(t_memory *mem);
|
||||
void memory_dump(t_memory *mem, const char *path);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue