# This example shows how SDCC can be used to link the object files
# to create a final executable.
#
# SDCC will automatically create a linker file and invoke link-z80.
#
# This makefile is designed for GNU make.
#


# These are the source files which make up the program.
# 
# The source files are listed in the order they will be built,
# and are listed with a '.o' extension instead of a '.c' or '.s' 
# extension.
#
# These files are built using the rules defined at the end of the 
# makefile.
#
# crt0.s must be first in the list!

OBJS_O = crt0.o main.o

# this variable defines the compiler to use
# the compiler is used to compile and assemble the source files
CC = sdcc

# this variable defines the assemblr to use
AS = as-z80

# this variable defines the linker to use
LD = sdcc

# this variable defines the parameters to the compiler
# -mz80 tells SDCC to build for the Z80 cpu.
CFLAGS = -mz80

# this variable defines the parameters for the linker.
LDFLAGS = -mz80 --no-std-crt0

# this is a variable defining the parameters for the assembler
#
# -g causes the assembler to ignore undefined labels. The linker will
# try to resolve these.
# -o causes the assembler to create an output file which can be linked.
AFLAGS = -g -o

# this is the rule to build the program called 'file'
#
# The first line will invoke the appropiate rule to build
# each source file.
# 
# The second line links the objects using SDCC.
#
# The third line converts the file.ihx to a binary file.
#
# The fourth line adds an AMSDOS header.
#
# The fifth line injects the file into a disk image
#
# "make file"
file: $(OBJS_O)
	$(CC) $(LDFLAGS) -o file $(OBJS_O)
	./makebin -p -b 256 < file.ihx > file.bin
	./addhead -x 256 file.bin file_ams.bin
	./cpcxfs -b -nd test.dsk -p file_ams.bin file.bin

# use this rule to clean all intermediate build files
#
# e.g. "make clean"
clean: 
	rm *.sym *.o *.asm *.lst *.map *.ihx *.bin *.lnk

# this rule defines how a '.o' file is created from a '.c' file.
%.o: %.c
	$(CC) $(CFLAGS) $<

# this rule defines how a '.o' file is created from a '.s' file.
%.o: %.s
	$(AS) $(AFLAGS) $*.o $<
