# Makefile for source code running on the DPFmate
#
# 12/2010, <hackfin@section5.ch>
#

VERSION = 0.1alpha

XRAM_START = 0x0e00

MEMMODEL = small

CC = sdcc
AS = asx8051
LD = aslink

ASFLAGS = -olsxffg
CFLAGS = -mmcs51 --model-$(MEMMODEL) --Werror
LDFLAGS = --xram-loc $(XRAM_START) --xram-size 0x100 --model-$(MEMMODEL)
LDFLAGS += --code-loc 0x132a --code-size 0x2d6
# Explicit relocation:
# LDFLAGS += -Wl-bCSEG=0x132a


OBJS = main.rel test.rel usbaux.rel
PATCHES = $(wildcard p_*.s)
APPLETS = $(wildcard app_*.s)
HANDLER = cmdhandler_14f4.ihx cmdhandler_14e5.ihx cmdhandler_big_14fb.ihx


TARGET = $(HANDLER)
TARGET += $(PATCHES:%.s=%.ihx) $(APPLETS:%.s=%.ihx)
# TARGET += sector.ihx

.SUFFIXES: .rel .ihx

all: $(TARGET) fwload Debug/dpf.so

fwload:
	$(MAKE) -C dpflib install DESTDIR=..
	[ -e python ] && \
	$(MAKE) -C python install

bankswitch.ihx: bankswitch.rel
	$(CC) -o $@ \
	-Wl-r \
	-Wl-bBANK1=0x10200 \
	-Wl-bBANK2=0x20300 \
		 $<

LIBOBJS = crtbank.rel crt0.rel

ax206.lib: $(LIBOBJS)
	$(AR) -Sq $@ $(LIBOBJS)
	asranlib $@


mod%.ihx: mod%.rel
	$(CC) -o $@ -Wl-bBANK0=0x1330 $<

main.ihx: $(OBJS)
	$(CC) -o $@ -Wl-bBANK0=0x132a $(OBJS)

# Patches (ABS)
p_%.ihx: p_%.rel
	$(CC) -o $@ $<

app%.ihx: app%.rel
	$(CC) -o $@ $<

sector.ihx: bootstrap.ihx
	python compile.py

chartbl4x8.bin: genfonts.py
	python genfonts.py

chartbl4x8.d52: chartbl4x8.bin chartbl4x8.ctl
	d52 -n -p -b $<

%.asm: %.d52
	@echo Generating $@
	@sed 's/\.equ/=/g;s/^\s*end/; END/g;s/\.org/; \.org/g' $< > $@

chartbl4x8.s: chartbl4x8.asm
	cp $< $@


BOBJS = test.rel bankswitch.rel spi.rel bootstrap.rel lcd.rel tinymod.rel
BOBJS += chartbl4x8.rel

bootstrap.ihx: $(BOBJS) \
	test.lnk
	# $(CC) -o $@ --Werror $(LDFLAGS) $<
	$(LD) -f test.lnk

table.ihx: table.rel
	$(CC) -o $@ --code-loc 0x0088 $<

%.asm: 
	-cd ..; rm $@
	$(MAKE) DUMP=-n -C .. $@
	mv ../$@ .

dump%.s: dump%.asm
	echo "	.include 'dpf.inc'" >>$@
	echo "	.include 'ax206.inc'" >>$@
	echo  >>$@
	echo "	.area HOME(CODE)" >>$@
	echo "	.area BANK0(CODE, ABS)" >>$@
	cat $< >>$@

.s.rel:
	$(AS) $(ASFLAGS) $<

.c.rel:
	$(CC) -M $(CFLAGS) $< > $(<:.c=.d)
	$(CC) $(CFLAGS) -c $<

Debug/dpf.so: dpflib/dpflib.c python/py_device.c
	$(MAKE) -C dpflib all
	$(MAKE) -C python install

# Command handler, relocatable:

RES128 = -Wl-gscreen_resx=128 -Wl-gscreen_resy=128
RES320 = -Wl-gscreen_resx=320 -Wl-gscreen_resy=240

# FIXME: We will no longer mess with the entry point in future, but patch
# the caller.

COM_CMD = -Wl-bBANK0=0x1330
cmdhandler_14f4.ihx: cmdhandler.rel
	$(CC) -o $@ $(COM_CMD) $(RES128) -Wl-bENTRY=0x14f4 $<

cmdhandler_14e5.ihx: cmdhandler.rel
	$(CC) -o $@ $(COM_CMD) $(RES128) -Wl-bENTRY=0x14e5 $<

cmdhandler_big_14fb.ihx: cmdhandler.rel
	$(CC) -o $@ $(COM_CMD) $(RES320) -Wl-bENTRY=0x14fb $<

jmptbl%.ihx: jmptbl%.rel
	$(CC) -o $@ $<

HEXFILES = $(HANDLER)
HEXFILES += p_start0.ihx p_start1.ihx p_start2.ihx p_start3.ihx
HEXFILES += jmptbl0.ihx jmptbl1.ihx jmptbl2.ihx jmptbl3.ihx

HEXDEST = hexfiles

install: $(HEXFILES)
	cp $(HEXFILES) hexfiles

clean:
	$(MAKE) -C dpflib clean
	$(MAKE) -C python clean
	rm -f fwload *.a
	rm -fr *.ihx *.rel *.o
	rm -f *.mem *.sym *.map *.lst *.rst
	rm -f *.raw

FILES = Makefile unixdll.mk README
FILES += cmdhandler.s ax206.inc dpf.inc usb.inc
FILES += $(wildcard p_*.s) $(wildcard jmptbl*.s)
FILES += app_detect.s
FILES += dpflib/Makefile dpflib/dpflib.c dpflib/fwload.c dpflib/rawusb.c
FILES += dpflib/sglib.h dpflib/dpf.h
FILES += include/flash.h
FILES += hexfiles python/Makefile python/py_device.c
FILES += detect.py fulldump.py profiles.py hackit.py

DISTFILES=$(FILES:%=dpf/src/%)

dist:
	cd ../..; \
	tar cfvz dpfhack-$(VERSION).tgz $(DISTFILES)
