#!/usr/bin/make -f

MCU		= stm32f051

# Directories
STYLES_DIR	= styles/
PATTERNS_DIR	= patterns/
PICS_DIR	= pics/

# Database
DATABASE	= furs.db
DATABASE_DATA	= database.data

# SVD's
SVD			= $(MCU).svd
CLEANED_SVD		= $(MCU).cleaned.svd
UNFOLDED_SVD		= $(MCU).unfolded.svd
ABS_ADDRESSES_SVD	= $(MCU).abs.svd

# XSLT Styles
UNFOLDED_STY		= $(STYLES_DIR)unfolded.xsl
ABS_ADDR_STY 		= $(STYLES_DIR)process-hex2num.xsl 
DB_STY			= $(STYLES_DIR)db.xsl
REGISTERS_STY		= $(STYLES_DIR)registers.xsl
BITFIELDS_STY		= $(STYLES_DIR)bitfields.xsl

# Patterns
DATABASE_PAT		= $(PATTERNS_DIR)db.pat
SOURCE_PAT		= $(PATTERNS_DIR)source.pat
STRIP_PAT		= $(PATTERNS_DIR)strip.pat
CONSTANTS_PAT		= $(PATTERNS_DIR)constants.pat
CLEANED_PAT		= $(PATTERNS_DIR)cleaned.pat
REGISTERS_PAT		= $(PATTERNS_DIR)registers.pat
BITFIELDS_PAT		= $(PATTERNS_DIR)bitfields.pat

# Programs
XSL_PROC 		= xsltproc
TEXT_PROC		= gema
DB			= sqlite3

# Files
SOURCE_IN		= source_in.fs
SOURCE_OUT		= source_out.fs
BITFIELDS_OUT		= bitfields_out.fs
REGISTERS_OUT		= registers_out.fs
CONSTANTS_OUT		= constants_out.fs
INTERMEDIATE_OUT	= intermediate_out.fs
STRIPPED_SOURCE_OUT	= upload.fs
PROJ_FILES= src/sys.fs src/gpio.fs src/leds.fs src/comparator.fs src/pulse.fs src/main.fs


all: project db sso upload
sso: $(STRIPPED_SOURCE_OUT)
imo: $(INTERMEDIATE_OUT)
cono: $(CONSTANTS_OUT)
rego: $(REGISTERS_OUT)
bfo: $(BITFIELDS_OUT) 
regpat: $(REGISTERS_PAT)
bfpat: $(BITFIELDS_PAT)
db: $(DATABASE)
dd: $(DATABASE_DATA)
abs:  $(ABS_ADDRESSES_SVD)
unfold: $(UNFOLDED_SVD)
tidy: $(CLEANED_SVD)


project:
	cat $(PROJ_FILES) > $(SOURCE_IN)

$(STRIPPED_SOURCE_OUT): $(STRIP_PAT) $(INTERMEDIATE_OUT)
	$(TEXT_PROC) -t -nobackup -line $(INTERMEDIATE_OUT) -f $(STRIP_PAT) -out $(STRIPPED_SOURCE_OUT)

$(INTERMEDIATE_OUT): $(CONSTANTS_OUT)
	cat $(CONSTANTS_OUT) >> $(INTERMEDIATE_OUT)

$(CONSTANTS_OUT): $(CONSTANTS_PAT) $(REGISTERS_OUT) 
	$(TEXT_PROC) -t -nobackup -line $(REGISTERS_OUT) -f $(CONSTANTS_PAT) -out $(CONSTANTS_OUT)

$(REGISTERS_OUT): $(REGISTERS_PAT) $(BITFIELDS_OUT)
	$(TEXT_PROC) -t -nobackup -line $(BITFIELDS_OUT) -f $(REGISTERS_PAT) -out $(REGISTERS_OUT)

$(BITFIELDS_OUT):  $(BITFIELDS_PAT) $(SOURCE_IN)
	$(TEXT_PROC) -t -nobackup -line $(SOURCE_IN) -f $(BITFIELDS_PAT) -out $(BITFIELDS_OUT)

$(REGISTERS_PAT): $(REGISTERS_STY) $(ABS_ADDRESSES_SVD) 
	$(XSL_PROC) -o $(REGISTERS_PAT) $(REGISTERS_STY) $(ABS_ADDRESSES_SVD)

$(BITFIELDS_PAT): $(BITFIELDS_STY) $(ABS_ADDRESSES_SVD)
	$(XSL_PROC) -o $(BITFIELDS_PAT) $(BITFIELDS_STY) $(ABS_ADDRESSES_SVD)

# Database Creation
$(DATABASE): $(DATABASE_DATA)
	-rm $(DATABASE)
	$(DB) $(DATABASE) < $(DATABASE_DATA)

# Database data creation
$(DATABASE_DATA): $(ABS_ADDRESSES_SVD)
	$(XSL_PROC) -o $(DATABASE_DATA) $(DB_STY) $(ABS_ADDRESSES_SVD)

# SVD ops below here
$(ABS_ADDRESSES_SVD):  $(UNFOLDED_SVD)
	$(XSL_PROC) -o $(ABS_ADDRESSES_SVD) $(ABS_ADDR_STY) $(UNFOLDED_SVD) 
	@xmllint --format $(ABS_ADDRESSES_SVD) > temp1.tmp && mv temp1.tmp $(ABS_ADDRESSES_SVD) || rm -f temp1.tmp

$(UNFOLDED_SVD): $(CLEANED_SVD)
	$(XSL_PROC) -o $(UNFOLDED_SVD) $(UNFOLDED_STY) $(CLEANED_SVD)

# Cleaning transposes '$' for '0x' per Forth conventions, removes large spaces etc 
$(CLEANED_SVD): $(SVD)
	$(TEXT_PROC) -t -nobackup -line  $(SVD) -f $(CLEANED_PAT) -out $(CLEANED_SVD)

upload:
	pkill -QUIT swd2
		
.PHONY: clean
clean:
	-rm $(CLEANED_SVD) $(UNFOLDED_SVD) $(ABS_ADDRESSES_SVD) $(DATABASE_DATA) $(BITFIELDS_OUT) \
	$(REGISTERS_OUT) $(CONSTANTS_OUT) $(INTERMEDIATE_OUT) $(STRIPPED_SOURCE_OUT)

dot:
	dot -Tjpg  src/lmt01-flowchart.dot -o pics/lmt01-flowchart.png
	feh  pics/lmt01-flowchart.png
