now roms can be loaded into the chip8 memory

This commit is contained in:
Ghostie 2025-02-12 20:11:37 -05:00
parent 620490e654
commit a1edac8a52
8 changed files with 74 additions and 5 deletions

View File

@ -37,7 +37,7 @@ build/screen.o: src/screen.c
run: all run: all
@$(ECHO) "Runing the Chip8 compiler" @$(ECHO) "Runing the Chip8 compiler"
@$(OUT) @$(OUT) $(rom)
clean: clean:
@$(ECHO) "Cleaning files..." @$(ECHO) "Cleaning files..."

View File

@ -1,6 +1,8 @@
#ifndef __CHIP8_H #ifndef __CHIP8_H
#define __CHIP8_H #define __CHIP8_H
#include <stddef.h>
#include "config.h" #include "config.h"
#include "keyboard.h" #include "keyboard.h"
#include "mem.h" #include "mem.h"
@ -18,5 +20,7 @@ struct chip8
}; };
void chip8_init (struct chip8 *chip8); void chip8_init (struct chip8 *chip8);
void chip8_load (struct chip8 *chip8, const char *buf, size_t size);
void chip8_exec (struct chip8 *chip8, unsigned short opcode);
#endif #endif

View File

@ -5,6 +5,7 @@
#define EMULATOR_WINDOW_MULTIPLIER 10 #define EMULATOR_WINDOW_MULTIPLIER 10
#define CHIP8_MEMORY_SIZE 4096 #define CHIP8_MEMORY_SIZE 4096
#define CHIP8_PROGRAM_LOAD_ADDRESS 0x200
#define CHIP8_DISPLAY_WIDTH 64 #define CHIP8_DISPLAY_WIDTH 64
#define CHIP8_DISPLAY_HEIGHT 32 #define CHIP8_DISPLAY_HEIGHT 32

View File

@ -10,5 +10,6 @@ struct chip8_memory
void chip8_memory_set (struct chip8_memory *mem, int index, unsigned char val); void chip8_memory_set (struct chip8_memory *mem, int index, unsigned char val);
unsigned char chip8_memory_get (struct chip8_memory *mem, int index); unsigned char chip8_memory_get (struct chip8_memory *mem, int index);
unsigned short chip8_memory_get_short (struct chip8_memory *mem, int index);
#endif #endif

BIN
roms/horsey_jump.ch8 Normal file

Binary file not shown.

View File

@ -1,4 +1,6 @@
#include "chip8.h" #include "chip8.h"
#include <assert.h>
#include <memory.h> #include <memory.h>
const char chip8_default_character_set[] = { const char chip8_default_character_set[] = {
@ -26,3 +28,16 @@ chip8_init (struct chip8 *chip8)
memcpy (&chip8->memory.memory, chip8_default_character_set, memcpy (&chip8->memory.memory, chip8_default_character_set,
sizeof (chip8_default_character_set)); sizeof (chip8_default_character_set));
} }
void
chip8_load (struct chip8 *chip8, const char *buf, size_t size)
{
assert (size + CHIP8_PROGRAM_LOAD_ADDRESS < CHIP8_MEMORY_SIZE);
memcpy (&chip8->memory.memory[CHIP8_PROGRAM_LOAD_ADDRESS], buf, size);
chip8->registers.PC = CHIP8_PROGRAM_LOAD_ADDRESS;
}
void
chip8_exec (struct chip8 *chip8, unsigned short opcode)
{
}

View File

@ -8,12 +8,39 @@ const char keyboard_map[CHIP8_TOTAL_KEYS]
SDLK_8, SDLK_9, SDLK_a, SDLK_b, SDLK_c, SDLK_d, SDLK_e, SDLK_f }; SDLK_8, SDLK_9, SDLK_a, SDLK_b, SDLK_c, SDLK_d, SDLK_e, SDLK_f };
int int
main (int argc, char **argv) main (int argc, const char **argv)
{ {
if (argc < 2)
{
fprintf (stderr, "you must provide a file to load.\n");
return EXIT_FAILURE;
}
const char *fname = argv[1];
printf ("the file to load is: %s\n", fname);
FILE *f = fopen (fname, "rb");
if (!f)
{
fprintf (stderr, "failed to open the file.\n");
return EXIT_FAILURE;
}
fseek (f, 0, SEEK_END);
long size = ftell (f);
fseek (f, 0, SEEK_SET);
char buf[size];
int res = fread (buf, size, 1, f);
if (res != 1)
{
fprintf (stderr, "failed to read from file.\n");
return EXIT_FAILURE;
}
struct chip8 chip8; struct chip8 chip8;
chip8_init (&chip8); chip8_init (&chip8);
chip8_screen_draw_sprite (&chip8.screen, 62, 10, chip8_load (&chip8, buf, size);
(const char *)&chip8.memory.memory[0x00], 5);
SDL_Init (SDL_INIT_EVERYTHING); SDL_Init (SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow ( SDL_Window *window = SDL_CreateWindow (
@ -23,6 +50,8 @@ main (int argc, char **argv)
SDL_Renderer *renderer SDL_Renderer *renderer
= SDL_CreateRenderer (window, -1, SDL_TEXTUREACCESS_TARGET); = SDL_CreateRenderer (window, -1, SDL_TEXTUREACCESS_TARGET);
chip8.registers.ST = 255;
while (1) while (1)
{ {
SDL_Event event; SDL_Event event;
@ -85,9 +114,20 @@ main (int argc, char **argv)
sleep (100); sleep (100);
chip8.registers.DT -= 1; chip8.registers.DT -= 1;
} }
if (chip8.registers.ST > 0)
{
/* TODO: Beep */
chip8.registers.ST -= 1;
}
unsigned short opcode
= chip8_memory_get_short (&chip8.memory, chip8.registers.PC);
chip8_exec (&chip8, opcode);
chip8.registers.PC += 2;
} }
out: out:
SDL_DestroyWindow (window); SDL_DestroyWindow (window);
return 0; return EXIT_SUCCESS;
} }

View File

@ -21,3 +21,11 @@ chip8_memory_get (struct chip8_memory *mem, int index)
chip8_is_mem_in_bounds (index); chip8_is_mem_in_bounds (index);
return mem->memory[index]; return mem->memory[index];
} }
unsigned short
chip8_memory_get_short (struct chip8_memory *mem, int index)
{
unsigned char byte1 = chip8_memory_get (mem, index);
unsigned char byte2 = chip8_memory_get (mem, index + 1);
return byte1 << 8 | byte2;
}