From 2a65b774aad59ec4717f727e2432d1744601c159 Mon Sep 17 00:00:00 2001 From: Parker Macdonald Date: Fri, 22 Nov 2024 14:11:26 -0700 Subject: [PATCH] add files --- Makefile | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ cpu.c | 22 ++++++++++++++++++++++ cpu.h | 24 ++++++++++++++++++++++++ main.c | 0 4 files changed, 98 insertions(+) create mode 100644 Makefile create mode 100644 cpu.c create mode 100644 cpu.h create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2334fe2 --- /dev/null +++ b/Makefile @@ -0,0 +1,52 @@ +TARGET := dented +SRC := ${wildcard *.c} +OBJ := $(SRC:.c=.o) + +INCS := -I/usr/local/include/ +LIBS := -L/usr/local/lib/ -lraylib + +# -pedantic means using gnu extentions will throw an error +# this allows for better code portability between compilers +CFLAGS_ERRORS = -Werror -Wall -Wextra -Wconversion -Wdouble-promotion -Wunreachable-code -Wshadow -Wpedantic -pedantic-errors -pedantic +CFLAGS = -std=c17 $(CFLAGS_ERRORS) $(INCS) +LDFLAGS = $(LIBS) + +ifeq ($(shell echo "Windows"), "Windows") + TARGET := $(TARGET).exe + DEL = del + Q = +else + DEL = rm -f + Q = " +endif + +# debug is enabled by default +# debug can be disabled with `make DEBUG=0` +DEBUG := 1 + +ifeq ($(DEBUG),1) +# -Og compiles with debug specific optimizations +# -g3 compiles with max debug information + CFLAGS += -Og -g3 +else +# -O3 compiles with extreme optimizations for performance +# -flto compiles with link time optimizations (LTO), supposed to improve performance by ~5% + CFLAGS += -O3 -flto +# -s when run on the linker will strip out debug symbols for a smaller file size + LDFLAGS += -s -flto +endif + +# I believe using cc is better since not every installation has gcc +# this can always be overridden with `make CC=gcc` if needed +CC := cc + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +.c.o: + $(CC) -o $@ -c $< $(CFLAGS) + +clean: + $(DEL) $(TARGET) $(OBJ) diff --git a/cpu.c b/cpu.c new file mode 100644 index 0000000..9e61f16 --- /dev/null +++ b/cpu.c @@ -0,0 +1,22 @@ +#include "cpu.h" +#include +#include + +void execute_instruction(cpu* cpu, uint8_t opcode, uint16_t operand) { + switch (opcode) { + // SEI: Set Interrupt Disable Flag + case 0x78: + cpu->p |= 0x4; + break; + // CLD: Clear Decimal Flag + case 0xd8: + cpu->p &= 0xfd; + break; + case 0xa2: + cpu->x = (uint8_t)operand; + break; + default: + printf("Unknown Opcode: %#04x, with Operand: %#08x\n", opcode, operand); + break; + } +} \ No newline at end of file diff --git a/cpu.h b/cpu.h new file mode 100644 index 0000000..9bce40d --- /dev/null +++ b/cpu.h @@ -0,0 +1,24 @@ +#ifndef CPU_H +#define CPU_H + +#include + +typedef struct { + // program counter + uint16_t pc; + // stack pointer + uint16_t sp; + + // accumulator register + uint8_t a; + // x index register + uint8_t x; + // y index register + uint8_t y; + // processor statuc register + uint8_t p; + + uint8_t memory[4096]; +} cpu; + +#endif \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..e69de29