# Makefile for Z80

# Set to "1" for more verbose output
SDCC_VERBOSE = 0

# Set to "1" if user-supplied crt0, else use the crt0.rel
#  file located in the SDCC 'Z80' library directory
EXT_CRT0 = 0

#--------------------------------------------------------------------------------------
# Define project name here:
TARGET = iDisk-S250317_R090518

#--------------------------------------------------------------------------------------
# Define source files here:
SRC = $(TARGET).c

#--------------------------------------------------------------------------------------
# MCU name
#--------------------------------------------------------------------------------------
MCU = z80

#--------------------------------------------------------------------------------------
# Processor frequency.
#     This will define a symbol, F_CPU, in all source code files equal to the
#     processor frequency in Hz. You can then use this symbol in your source code to
#     calculate timings. Do NOT tack on a 'UL' at the end, this will be done
#     automatically to create a 32-bit value in your source code.
#F_CPU = 4000000
F_CPU = 8000000

#--------------------------------------------------------------------------------------
# Code, Data and ExtRAM locations
# Comment out any of these to use the SDCC defaults
#--------------------------------------------------------------------------------------
#CODE_LOC = 0x0200
DATA_LOC = 0x4000
#XRAM_LOC = 0x0000

#--------------------------------------------------------------------------------------
# Output format. (can be srec, ihex, binary)
FORMAT = ihex

#--------------------------------------------------------------------------------------
# Optimization level, can be [--opt-code-speed or --opt-code-size].
OPT = --opt-code-speed
#OPT = --opt-code-size

#--------------------------------------------------------------------------------------
# Define SDCC compiler flags
CFLAGS = -m$(MCU)
CFLAGS += --std-sdcc11
CFLAGS += $(OPT) -c
ifeq ($(SDCC_VERBOSE), 1)
  CFLAGS += -V
endif

#--------------------------------------------------------------------------------------
# Define SDCC linker flags
LDFLAGS = -m$(MCU) --out-fmt-ihx -m
ifdef CODE_LOC
  LDFLAGS += --code-loc $(CODE_LOC)
endif
ifdef DATA_LOC
  LDFLAGS += --data-loc $(DATA_LOC)
endif
ifdef XRAM_LOC
  LDFLAGS += --xram-loc $(XRAM_LOC)
endif
ifeq ($(EXT_CRT0), 1)
  LDFLAGS += --no-std-crt0
endif
ifeq ($(SDCC_VERBOSE), 1)
  LDFLAGS += -V
endif

#--------------------------------------------------------------------------------------
# Define Z80 assembler flags
ASFLAGS = -l -s
ASFLAGS +=

#--------------------------------------------------------------------------------------
# Define Z80 disassembler flags
DASMFLAGS =
DASMFLAGS +=

#--------------------------------------------------------------------------------------
# Define some programs
CC = sdcc
ASM = sdasz80
DASM = z80dasm
PACKIHX = packihx
SREC = srec_cat
REMOVE = rm -f

#--------------------------------------------------------------------------------------
# Define Messages
MSG_ERRORS_NONE = Errors: none
MSG_BEGIN = -------- begin --------
MSG_END = --------  end  --------
MSG_SIZE_BEFORE = Size before:
MSG_SIZE_AFTER = Size after:
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = - Linking:
MSG_COMPILING = - Compiling:
MSG_ASSEMBLING = - Assembling:
MSG_HEXFILE = - Creating HEX file:
MSG_CLEANING = Cleaning project:

#--------------------------------------------------------------------------------------
# Default target.
all: begin gccversion build finished end

#build: elf hex eep lst sym
#build: hex lst sym
ifeq ($(EXT_CRT0), 1)
  build: crt0.rel ihx
else
  build: ihx
endif
#build: crt0.rel rel ihx

ihx: $(TARGET).ihx
hex: $(TARGET).hex
rel: $(TARGET).rel
sym: $(TARGET).sym
bin: $(TARGET).bin

#--------------------------------------------------------------------------------------
begin:
	@echo
	@echo $(MSG_BEGIN)
	@echo "Sources: $(SRC)"
	@echo

finished:
	@echo $(MSG_ERRORS_NONE)

end:
	@echo $(MSG_END)
	@echo

#--------------------------------------------------------------------------------------
# Display compiler version information.
gccversion :
	@$(CC) --version

#--------------------------------------------------------------------------------------
%.bin: $(TARGET).ihx
	@$(SREC) $(TARGET).hex -intel -o $(TARGET).bin -binary

#--------------------------------------------------------------------------------------
crt0.rel: crt0.s
	@echo $(MSG_ASSEMBLING) $@
	@$(ASM) $(ASFLAGS) -o crt0.rel $<

#--------------------------------------------------------------------------------------
%.rel: $(TARGET).c
	@echo $(MSG_COMPILING) $@
	@$(CC) $(CFLAGS) $<

#--------------------------------------------------------------------------------------
%.ihx: $(TARGET).rel
	@echo $(MSG_LINKING) $@
ifeq ($(EXT_CRT0), 1)
	@$(CC) $(LDFLAGS) -o $(TARGET).ihx crt0.rel $<
else
	@$(CC) $(LDFLAGS) -o $(TARGET).ihx $<
endif

#--------------------------------------------------------------------------------------
%.hex: $(TARGET).ihx
	@echo $(MSG_HEXFILE) $@
	$(PACKIHX) > $(TARGET).hex $<

#--------------------------------------------------------------------------------------
%.dasm: $(TARGET).bin
	$(DASM) -t -g 0x0 -l $(TARGET).bin -o $(TARGET).dasm

#--------------------------------------------------------------------------------------
# Target: clean project.
clean: clean_list

clean_list :
	@echo
	@echo $(MSG_CLEANING)
#	$(REMOVE) $(TARGET).ihx
	$(REMOVE) $(TARGET).rel
	$(REMOVE) $(TARGET).map
	$(REMOVE) $(TARGET).sym
	$(REMOVE) $(TARGET).noi
	$(REMOVE) $(TARGET).dasm
	$(REMOVE) $(TARGET).lst
	$(REMOVE) $(TARGET).asm
	$(REMOVE) $(TARGET).lk
	$(REMOVE) -r crt0.rel
	$(REMOVE) -r crt0.lst
	$(REMOVE) -r crt0.noi
	$(REMOVE) -r crt0.lst
	$(REMOVE) -r .dep/*

#--------------------------------------------------------------------------------------
# Listing of phony targets.
.PHONY : all begin finish end sizebefore sizeafter gccversion \
	build elf hex eep lss sym clean clean_list program

#-- End of Makefile -------------------------------------------------------------------
