# Note: Yosys chokes on spaces in library directories (reported to yosys)
# Library path if not overriden (environment)
NEXTPNR_OPTS=--up5k --package sg48
ICETIME_OPTS=-d up5k
FN=bldc
FLASH_SIZE=524288

${FN}_filled.bin: ${FN}.bin
	python3 -c "import sys; sys.stdout.buffer.write(b'\xff'*${FLASH_SIZE})" > $@;
	dd if=$< of=$@ conv=notrunc

${FN}.json: ${FN}.v $(shell find library -type f -name '*.v')
	./run_yosys.sh ${FN}

${FN}.asc: ${FN}.json ${FN}.pcf
	nextpnr-ice40 ${NEXTPNR_OPTS} --pcf "${FN}.pcf" --json "${FN}.json" --asc "${FN}.asc"

${FN}.rpt: ${FN}.pcf ${FN}.asc
	icetime ${ICETIME_OPTS} -p "${FN}.pcf" -mtr "${FN}.rpt" "${FN}.asc"

${FN}.bin: ${FN}.asc
	icepack ${FN}.asc ${FN}.bin

all: ${FN}_filled.bin timing

timing: ${FN}.rpt

clean:
	rm -f ${FN}.json ${FN}.asc ${FN}.bin ${FN}_filled.bin ${FN}.rpt

flash: ${FN}_filled.bin
	flashrom -p ft2232_spi:type=232H,port=A -w "${FN}_filled.bin"