# This is an example makefile which shows:
# - how to use SDCC suite to build programs for the Amstrad
# - how to build a program from multiple source files
#
# This example compiles and assembles the files, then links them.
# The compile and assemble stages are done together using SDCC.
#
# 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' extension.
#
# crt0.c must be first in the list!

OBJS_O = crt0.o main.o other.o putchar.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 parameters to the compiler
# -mz80 tells SDCC to build for the Z80 cpu.
# --compile-only tells SDCC to compile and assemble but not to link.
# --no-std-crt0 tells SDCC to not include the standard crt0 startup
CFLAGS = -mz80 --compile-only --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 SDCC for each source file to build
# the output files ('.o')
# 
# The second line links these to make the final program.
# 
# 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)
	link-z80 -f main.lnk
	./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

# 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 $<
