# Makefile template for iap15w4k58s4 projects using SDCC
### iap15w4k58s4 code: 58K xram: 4K
### sdcc --std-sdcc2x -mmcs51 --model-large --opt-code-speed --code-size 0xe800 --xram-size 0x1000 --stack-loc 0x0020 -o build/ main.c


# Compiler and linker flags
CFLAGS = -mmcs51 --std-sdcc2x --model-large --opt-code-speed \
		 --code-size 0xffff --xram-size 0x2000

# Define the compiler and the linker
# CC = sdcc
CC = /home/lzyor/programs/sdcc-4.4.0/bin/sdcc

# Define the project directories
SRCDIR = src
INCDIR = inc
BINDIR = bin

# Define the target name
TARGET = $(BINDIR)/main.ihx
ENTRY = main.c

# Define the C source and header files
SRC := $(filter-out $(SRCDIR)/$(ENTRY), $(wildcard $(SRCDIR)/*.c))
OBJ = $(SRC:$(SRCDIR)/%.c=$(BINDIR)/%.rel)
INC = -I$(INCDIR)

# Define the phony targets
.PHONY: all clean

# Default target
all: $(TARGET)

# Link the object files into the final binary
$(TARGET): $(OBJ) $(SRCDIR)/$(ENTRY)
	$(CC) $(CFLAGS) $(INC) -o $(TARGET) $^

# Compile the source files into object files
$(BINDIR)/%.rel: $(SRCDIR)/%.c
	@mkdir -p $(@D)
	$(CC) $(CFLAGS) $(INC) -c $< -o $@

# Clean up the build files
clean:
	rm -f ./$(BINDIR)/*
