#   This file is part of the 9086 project.
#
#   Copyright (c) 2023 Efthymios Kritikos
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

SYSTEM_VVP=system/system.vvp
VERILATOR_BIN=system/obj_dir/Vsystem
BOOT_CODE=boot_code/brainfuck_mandelbrot.txt
GTKWSAVE=./gtkwave_savefile.gtkw
MICROCODE=system/ucode.txt
BOOTABLES=boot_code/brainfuck_compiled.txt boot_code/brainfuck_interpreted.txt boot_code/pipeline_ideal.txt boot_code/fibonacci.txt boot_code/gnome_sort.txt boot_code/cache_fill_and_empty.txt ${BOOT_CODE} boot_code/colored_led.txt boot_code/bios.stxt
PRINT_PATH_PREFIX=.

NO_ASM=1
include common.mk

.PHONY: ${BOOTABLES}
.PHONY: ${subst .txt,.bin,${BOOTABLES}}

${BOOTABLES} ${subst .stxt,.bin,${subst .txt,.bin,${BOOTABLES}}} :
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=boot_code/ -C boot_code $(subst boot_code/,,$@)

boot_code/%.txt:
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=boot_code/ -C boot_code $(subst boot_code/,,$@)

.PHONY:${SYSTEM_VVP}
${SYSTEM_VVP}:
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=system/ -C system system.vvp

.PHONY:${VERILATOR_BIN}
${VERILATOR_BIN}:
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=system/ -C system obj_dir/Vsystem

.PHONY: clean
clean:
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=system/ -C system clean
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=boot_code/ -C boot_code clean

.PHONY: mrproper
mrproper: clean
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=system/ -C system mrproper
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=boot_code/ -C boot_code mrproper

.PHONY: upload
upload: boot_code/bios.stxt
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=system/ -C system upload

.PHONY: fpga_sim
fpga_sim: boot_code/bios.stxt
	${Q}make ${MAKEOPTS} PRINT_PATH_PREFIX=system/ -C system fpga_sim

.PHONY: help
help:
	@echo 'Simulation targets:'
	@echo '                          - No target will start a simulation with the default'
	@echo '                            program loaded to memory'
	@echo '  boot_code/[name].run    - Start a simulation and load memory with the'
	@echo '                            assembled code from boot_code/[name].asm'
	@echo '  boot_code/[name].wave   - like the above but write trace file and start'
	@echo '                            gtkwave with it'
	@echo '  boot_code/[name].disas  - assemble and show the disassembly of the binary'
	@echo ''
	@echo 'Synthesis targets:'
	@echo '  upload                  - Build a bitstream for the board selected in'
	@echo '                            common.mk and upload it if possible'
	@echo '  fpga_sim                - Simulate the SoC that gets build for the configured'
	@echo '                            fpga board'
	@echo ''
	@echo 'Cleaning targets:'
	@echo '  clean                   - Delete all non-source files generated by the build'
	@echo '                            system'
	@echo '  mrproper                - clean + delete downloaded external source IP files'