started to work in memory

This commit is contained in:
Ghostie 2025-02-08 18:04:26 -05:00
parent 73a23407e4
commit c70bbca209
5 changed files with 48 additions and 1 deletions

View File

@ -3,7 +3,7 @@ ECHO=echo -e
CFLAGS=-Wall -Werror -std=gnu99 -O0 -g -Iinclude CFLAGS=-Wall -Werror -std=gnu99 -O0 -g -Iinclude
LIBS=-lSDL2 LIBS=-lSDL2
FILES=build/main.o FILES=build/main.o build/mem.o
OUT=bin/chip8.out OUT=bin/chip8.out
all: $(FILES) all: $(FILES)
@ -14,6 +14,10 @@ build/main.o: src/main.c
@$(ECHO) "CC\t\t"$< @$(ECHO) "CC\t\t"$<
@$(CC) $(CFLAGS) $< -c -o $@ $(LIBS) @$(CC) $(CFLAGS) $< -c -o $@ $(LIBS)
build/mem.o: src/mem.c
@$(ECHO) "CC\t\t"$<
@$(CC) $(CFLAGS) $< -c -o $@ $(LIBS)
run: all run: all
@$(ECHO) "Runing the Chip8 compiler" @$(ECHO) "Runing the Chip8 compiler"
@$(OUT) @$(OUT)

View File

@ -2,9 +2,11 @@
#define __CHIP8_H #define __CHIP8_H
#include "config.h" #include "config.h"
#include "mem.h"
struct chip8 struct chip8
{ {
struct chip8_memory memory;
}; };
#endif #endif

14
include/mem.h Normal file
View File

@ -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

View File

@ -5,6 +5,10 @@
int int
main (int argc, char **argv) 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_Init (SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow ( SDL_Window *window = SDL_CreateWindow (
EMULATOR_WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, EMULATOR_WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,

23
src/mem.c Normal file
View File

@ -0,0 +1,23 @@
#include "mem.h"
#include <assert.h>
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];
}