# ----------------------------- TESTBENCH STARTS ----------------------------- 
.PHONY: default program clean get_random_matrix lint

SVFILES = bit_repr_vector.sv bit_repr_matrix.sv get_random_matrix.sv get_noise_matrix.sv key_switching.sv

# Path to USB Blaster II firmware file in Quartus, only for openFPGALoader

BLASTER-6810 = /opt/intelFPGA_lite/21.1/quartus/linux64/blaster_6810.hex

default :
	@echo "No target given. Try:\n  make bit_repr_vector.vcd\nmake bit_repr_matrix.vcd\n  make get_random_matrix.vcd\n  make get_noise_matrix.vcd\n make key_switching.vcd\n make lint\n  make program"

# Program the FPGA through the USB Blaster connection
# It is the second thing on the JTAG chain (the HPS block is first), hence @2

program : output_files/key_switching.sof
	quartus_pgm -c 1 -m jtag -o "p;output_files/key_switching.sof@2"


# Compile the project in Quartus

output_files/key_switching.sof : key_switching.qpf $(SVFILES)
	quartus_sh --flow compile key_switching

# Create the Quartus project files from a Tcl script

key_switching.qpf key_switching.qsf key_switching.sdc : soc_system.tcl
	quartus_sh -t soc_system.tcl



# Program the FPGA using openFPGALoader, an alternative to the
# Quartus programmer

program-open : output_files/key_switching.svf
	openFPGALoader -b de1Soc \
	  --probe-firmware $(BLASTER-6810) \
	  output_files/key_switching.svf

# Convert the .sof to to an .svf file for openFPGALoader

output_files/key_switching.svf : output_files/key_switching.sof
	quartus_cpf -c \
		--operation p --voltage 3.3 --freq 24.0MHz \
		output_files/key_switching.sof output_files/key_switching.svf



# Quickly check the System Verilog files for errors

lint :
	for file in $(SVFILES); do \
	verilator --lint-only -Wall $$file; done



# Run Verilator simulations

bit_repr_vector.vcd : obj_dir/Vbit_repr_vector
	obj_dir/Vbit_repr_vector

bit_repr_matrix.vcd : obj_dir/Vbit_repr_matrix
	obj_dir/Vbit_repr_matrix

get_random_matrix.vcd : obj_dir/Vget_random_matrix
	obj_dir/Vget_random_matrix

get_noise_matrix.vcd : obj_dir/Vget_noise_matrix
	obj_dir/Vget_noise_matrix

key_switching.vcd : obj_dir/Vkey_switching
	obj_dir/Vkey_switching


# Create Verilator simulations

obj_dir/Vbit_repr_vector : bit_repr_vector.sv bit_repr_vector.cpp
	verilator -trace -cc bit_repr_vector.sv -exe bit_repr_vector.cpp \
		-top-module bit_repr_vector
	cd obj_dir && make -j -f Vbit_repr_vector.mk

obj_dir/Vbit_repr_matrix : bit_repr_matrix.sv bit_repr_matrix.cpp
	verilator -trace -cc bit_repr_matrix.sv -exe bit_repr_matrix.cpp \
		-top-module bit_repr_matrix
	cd obj_dir && make -j -f Vbit_repr_matrix.mk

obj_dir/Vget_random_matrix : get_random_matrix.sv get_random_matrix.cpp
	verilator -trace -cc get_random_matrix.sv -exe get_random_matrix.cpp \
		-top-module get_random_matrix
	cd obj_dir && make -j -f Vget_random_matrix.mk

obj_dir/Vget_noise_matrix : get_noise_matrix.sv get_noise_matrix.cpp
	verilator -trace -cc get_noise_matrix.sv -exe get_noise_matrix.cpp \
		-top-module get_noise_matrix
	cd obj_dir && make -j -f Vget_noise_matrix.mk

obj_dir/Vkey_switching : key_switching.sv key_switching.cpp
	verilator -trace -cc key_switching.sv -exe key_switching.cpp \
		-top-module key_switching
	cd obj_dir && make -j -f Vkey_switching.mk

clean_test: rm -rf obj_dir db incremental_db output_files \
	key_switching.qpf key_switching.qsf key_switching.sdc key_switching.qws c5_pin_model_dump.txt
# ------------------------------ TESTBENCH ENDS ------------------------------