created the stack

This commit is contained in:
Ghostie 2025-02-08 18:32:28 -05:00
parent 155a7f7a04
commit 733d0b73a7
6 changed files with 59 additions and 4 deletions

View File

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

View File

@ -4,11 +4,13 @@
#include "config.h"
#include "mem.h"
#include "registers.h"
#include "stack.h"
struct chip8
{
struct chip8_memory memory;
struct chip8_registers registers;
struct chip8_stack stack;
};
#endif

View File

@ -9,5 +9,6 @@
#define CHIP8_DISPLAY_HEIGHT 32
#define CHIP8_TOTAL_DATA_REGISTERS 16
#define CHIP8_TOTAL_STACK_DEPTH 16
#endif

15
include/stack.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef __STACK_H
#define __STACK_H
#include "config.h"
struct chip8;
struct chip8_stack
{
unsigned short stack[CHIP8_TOTAL_STACK_DEPTH];
};
void chip8_stack_push (struct chip8 *chip8, unsigned short val);
unsigned short chip8_stack_pop (struct chip8 *chip8);
#endif

View File

@ -5,9 +5,13 @@
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));
struct chip8 chip8;
chip8.registers.SP = 0;
chip8_stack_push (&chip8, 0xff);
chip8_stack_push (&chip8, 0xaa);
printf ("%x\n", chip8_stack_pop (&chip8));
printf ("%x\n", chip8_stack_pop (&chip8));
SDL_Init (SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow (

29
src/stack.c Normal file
View File

@ -0,0 +1,29 @@
#include "stack.h"
#include "chip8.h"
#include <assert.h>
static void
chip8_stack_in_bounds (struct chip8 *chip8)
{
assert (chip8->registers.SP < sizeof (chip8->stack.stack));
}
void
chip8_stack_push (struct chip8 *chip8, unsigned short val)
{
chip8->registers.SP += 1;
chip8_stack_in_bounds (chip8);
chip8->stack.stack[chip8->registers.SP] = val;
}
unsigned short
chip8_stack_pop (struct chip8 *chip8)
{
chip8_stack_in_bounds (chip8);
unsigned short result = chip8->stack.stack[chip8->registers.SP];
chip8->registers.SP -= 1;
return result; // we don't set the program counter
}