APPDATA := $(HOME)/.wine/drive_c/windows/profiles/jcomeau/Application Data
PROGRAMS := $(HOME)/.wine/drive_c/Program Files
CORIDIUM := $(PROGRAMS)/Coridium
DEVELOPING_IN_C ?=
PATH := $(PATH):.
ifdef DEVELOPING_IN_C
 BIN := $(CORIDIUM)/bin
 PREFIX := arm-none-eabi-
 SUFFIX := .exe
 COM_PORT ?= com5
else
 #BIN := /opt/toolchain/gnuarm-3.4.3/bin  # doesn't support push and pop
 #BIN := /usr/local/arm/bin  # doesn't support clz
 BIN := /usr/arm-linux-gnueabi/bin
 #PREFIX := arm-elf-
 #PREFIX :=
 SUFFIX :=
 COM_PORT ?= /dev/ttyUSB0
endif
#PREASM = /usr/local/bin/preasm.tcl
PREASM = preasm.sh
LNKFLAGS =  -v -T minimal.ld -nostartfiles
INI_FILE := $(APPDATA)/Coridium/MakeItC.ini
MY_DOCS := $(HOME)/.wine/drive_c/windows/profiles/jcomeau/My Documents
BUILD_DIR := $(MY_DOCS)/Coridium
WORK_DIR ?= src/armmite
GCC_OPTS := -mcpu=arm7tdmi-s \
	 -funsigned-char \
	 -mthumb-interwork \
	 -Os \
	 -I"$(CORIDIUM)/CMSIS/include" \
	 -fno-inline
LINK_OPTS := -lm
EXTRA_OBJECTS := startup.S
FIXED_BROKEN_LINKER := 1
DRYRUN ?= --dry-run
SERVER ?= www
export
# when plugging in the armmite USB cable, you should see /dev/ttyUSB0 appear
default: minimal.bin minimal.bin.burn minimal.xxd
bin: "$(BIN)"  # this doesn't actually work due to spaces in dirnames
	ln -s "$<" $@
src: "$(BUILD_DIR)"  # this doesn't actually work due to spaces in dirnames
	ln -s "$<" $@
$(HOME)/.wine/dosdevices/$(COM_PORT): /dev/ttyUSB*
	ln -sf $< $@
toggle: toggle.py
	./$<
clean:
	@ echo "...cleaning"
	rm -f *.o *.elf *.s *.lst *.lnkh *.lnkt *.rom *.ram *.xxd *.dmp *.pyc
distclean: clean
	rm -f *.hex [ac-z]*.bin *.dictionary
$(COM_PORT): $(HOME)/.wine/dosdevices/$(COM_PORT)
makeitc:  # just use for Tools | Build Hex File
	cd bin && ./MakeItC.exe
loadhelp: bin/load21xx.exe
	./$< --help
ramload: bin/load21xx.exe $(COM_PORT)
	# just an example of how to load data to RAM
	./$< 1756 testfile.hex $(COM_PORT) RAM 0x10000000
progload: bin/load21xx.exe $(COM_PORT)
	./$< 2103 "$(BUILD_DIR)/sample.hex" $(COM_PORT)
%.progload: bin/load21xx.exe $(COM_PORT) %
	./$< 2103 "$*" $(COM_PORT)
ini_edit:  # edit ini file to set com port (doesn't help)
	vi "$(INI_FILE)"
%.load: bin/load21xx.exe $(COM_PORT)
	./$< 2103 "$(BUILD_DIR)/$*.hex" $(COM_PORT)
comm: /dev/ttyUSB*
	./armmite_term.py $< 19200
	#/usr/bin/miniterm.py --rts=0 --dtr=0 $< 19200
	#./TclTerm.tcl
%.disasm: %.elf
	bin/arm-none-eabi-objdump.exe --disassemble $<
%.disassemble: %.bin
	bin/arm-none-eabi-objdump.exe --disassemble-all --target=binary \
	 --architecture=arm $<
	bin/arm-none-eabi-objdump.exe --disassemble-all --target=binary \
	 --architecture=arm --disassembler-options=force-thumb $<
%.bin: %.dump
	./undump.py $< > $@
sync:
	bin/sync21xx.exe $(COM_PORT)
riscydoc:
	firefox /usr/src/riscypygness/manual.html
riscyburn:
	# the 20000 is the resonant frequency of the crystal in kHz
	cd riscypygness && make && \
	 lpc21isp -control -verify \
	 -bin kernel-lpc2103.bin /dev/ttyUSB0 19200 20000
riscy:
	cd riscypygness && ./riscy.tcl -image kernel-lpc2103.bin \
	 -port /dev/ttyUSB0
/tmp/riscy.patch: riscypygness
	-cd $< && gendiff . .orig > $@
%.hex: %.elf
	"$(BIN)/$(PREFIX)objcopy$(SUFFIX)" \
	 --input-target elf \
	 --output-target ihex \
	 $< $@
%.elf: %.c
	"$(BIN)/$(PREFIX)gcc$(SUFFIX)" $(GCC_OPTS) -o $@ $<
%.hex: %.asm
	SOURCE="$$(readlink -f $<)" && cd riscypygness && \
	      $(MAKE) "$${SOURCE%%.asm}.hex"
%.log.extract: %.log
	sed -n -e "s/^[.]*Sending '\(.*\)[(]0A[)]'$$/\1/p" \
	 -e "s/^Sending: [']\(.*\)\\\r\\\n[0-9]\+[']$$/\1/p" \
	 $<
%.bin.burn: %.bin
	./burn.py 0 $<
%.bin.disassemble: %.bin
	"$(BIN)/$(PREFIX)objdump$(SUFFIX)" --disassemble-all \
	 --target=binary --architecture=arm $<
ifdef FIXED_BROKEN_LINKER
%.s: %.asm
	$(PREASM) $*.asm $@ 
%.o: %.s
	"$(BIN)/$(PREFIX)as$(SUFFIX)" -mcpu=arm7tdmi-s -mapcs-32 \
	 -gstabs -ahls=$*.lst  -o $@ $<
%.bin: %.elf
	"$(BIN)/$(PREFIX)objcopy$(SUFFIX)" -O binary $<  $@
%.elf: %.o
	@ echo "...linking $@"
	"$(BIN)/$(PREFIX)ld$(SUFFIX)" $(LNKFLAGS) -o $@ $<
	"$(BIN)/$(PREFIX)objdump$(SUFFIX)" -h $@ > $*.lnkh
	"$(BIN)/$(PREFIX)objdump$(SUFFIX)" -t $@ > $*.lnkt
endif
set:
	set
%.xxd:	%.bin
	xxd $< $@
%.dmp:	%.bin
	./dump.py 0 $$(($$(wc -c $< | awk '{print $$1}') + 16)) > $@
%.diff: %.xxd %.dmp
	diff $+
%.ram:	%.bin
	./dump.py 0x40000000 2048 > $@
upload: clean
	find . -type l | sed 's/^..//' | \
	 rsync -avuzp \
	  $(DRYRUN) \
	  --exclude='.bzr*' \
	  --exclude='*sample.*' \
	  --exclude='*.tcl' \
	  --exclude='bootblock*' \
	  --exclude='*.[ch]' \
	  --exclude='*.bin' \
	  --exclude='*.hex' \
	  --exclude='job.txt' \
	  --exclude-from=- \
	  . $(SERVER):$(WORK_DIR)
backup:
	$(MAKE) WORK_DIR=rentacoder/jcomeau/armmite upload
