From c70bbca209621d62af265023ca6b0d3a6477b001 Mon Sep 17 00:00:00 2001 From: Ghostie Date: Sat, 8 Feb 2025 18:04:26 -0500 Subject: [PATCH] started to work in memory --- Makefile | 6 +++++- include/chip8.h | 2 ++ include/mem.h | 14 ++++++++++++++ src/main.c | 4 ++++ src/mem.c | 23 +++++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 include/mem.h create mode 100644 src/mem.c diff --git a/Makefile b/Makefile index dcf7bfb..fb72f74 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ ECHO=echo -e CFLAGS=-Wall -Werror -std=gnu99 -O0 -g -Iinclude LIBS=-lSDL2 -FILES=build/main.o +FILES=build/main.o build/mem.o OUT=bin/chip8.out all: $(FILES) @@ -14,6 +14,10 @@ build/main.o: src/main.c @$(ECHO) "CC\t\t"$< @$(CC) $(CFLAGS) $< -c -o $@ $(LIBS) +build/mem.o: src/mem.c + @$(ECHO) "CC\t\t"$< + @$(CC) $(CFLAGS) $< -c -o $@ $(LIBS) + run: all @$(ECHO) "Runing the Chip8 compiler" @$(OUT) diff --git a/include/chip8.h b/include/chip8.h index ed504fe..b214a05 100644 --- a/include/chip8.h +++ b/include/chip8.h @@ -2,9 +2,11 @@ #define __CHIP8_H #include "config.h" +#include "mem.h" struct chip8 { + struct chip8_memory memory; }; #endif diff --git a/include/mem.h b/include/mem.h new file mode 100644 index 0000000..1475913 --- /dev/null +++ b/include/mem.h @@ -0,0 +1,14 @@ +#ifndef __MEM_H +#define __MEM_H + +#include "config.h" + +struct chip8_memory +{ + unsigned char memory[CHIP8_MEMORY_SIZE]; +}; + +void chip8_memory_set (struct chip8_memory *mem, int index, unsigned char val); +unsigned char chip8_memory_get (struct chip8_memory *mem, int index); + +#endif diff --git a/src/main.c b/src/main.c index f2c6603..a667faf 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,10 @@ int main (int argc, char **argv) { + struct chip8 chip8 = { 0 }; + chip8_memory_set (&chip8.memory, 50, 'Z'); + printf ("%c\n", chip8_memory_get (&chip8.memory, 50)); + SDL_Init (SDL_INIT_EVERYTHING); SDL_Window *window = SDL_CreateWindow ( EMULATOR_WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, diff --git a/src/mem.c b/src/mem.c new file mode 100644 index 0000000..3d4a30d --- /dev/null +++ b/src/mem.c @@ -0,0 +1,23 @@ +#include "mem.h" + +#include + +static void +chip8_is_mem_in_bounds (int index) +{ + assert (index >= 0 && index < CHIP8_MEMORY_SIZE); +} + +void +chip8_memory_set (struct chip8_memory *mem, int index, unsigned char val) +{ + chip8_is_mem_in_bounds (index); + mem->memory[index] = val; +} + +unsigned char +chip8_memory_get (struct chip8_memory *mem, int index) +{ + chip8_is_mem_in_bounds (index); + return mem->memory[index]; +}