2023-02-16 01:46:22 +00:00
# This file is part of the 9086 project.
#
2024-02-10 15:52:13 +00:00
# Copyright (c) 2024 Efthymios Kritikos
2023-02-16 01:46:22 +00:00
#
# 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/>.
#
2023-05-11 11:11:17 +00:00
SOURCES = processor.v memory.v registers.v alu.v decoder.v general.v biu.v execute.v
2023-11-01 19:09:59 +00:00
INCLUDES = exec_state_def.v alu_header.v config.v ucode_header.v error_header.v
2023-02-22 01:28:23 +00:00
MICROCODE = ucode.txt
2023-12-06 18:12:57 +00:00
SYSTEM_VVP = system.vvp
PRINT_PATH_PREFIX = ./
2023-12-07 16:39:04 +00:00
BUILD_FILES_PREFIX = build/
2023-02-16 01:46:22 +00:00
2023-12-06 18:12:57 +00:00
BOOT_CODE = boot_code.txt
VERILATOR_BIN = obj_dir/Vsystem
2023-12-07 16:39:04 +00:00
VERILATOR_FPGA_BIN = /Vfpga_top
2023-02-22 01:51:14 +00:00
NO_ASM = 0
2023-02-16 01:46:22 +00:00
i n c l u d e . . / c o m m o n . m k
2023-12-07 16:39:04 +00:00
$( shell mkdir -p $ ( BUILD_FILES_PREFIX ) )
FPGA_SEED :: = $( shell seq 1 200|sort -R |head -n 1)
i f e q "${BUILD_SEED_DIFFERENTIATION}" "1"
BUILD_NAME = ${ FPGA_BOARD } _${ FPGA_SEED }
e l s e
BUILD_NAME = ${ FPGA_BOARD }
e n d i f
i n c l u d e f p g a _ c o n f i g / $ { F P G A _ B O A R D } / c o n f i g . m k
# Synthesis and bitstream creation for ECP5
i f e q "${ECP5_DEVICE}" "25F"
NEXTPNR_ECP5_DEV = --25k
e l s e i f e q "${ECP5_DEVICE}" "85F"
NEXTPNR_ECP5_DEV = --85k
e l s e
$( error invalid ECP 5 device $ {ECP 5_DEVICE })
e n d i f
2023-12-05 21:46:46 +00:00
EVENT_SIM_TESTBENCH = testbench.v
VERILATOR_TESTBENCH = testbench.cpp
SIMULATION_TOP_LEVEL_SOURCE = system.v
GTKWSAVE = ../gtkwave_savefile.gtkw
2023-12-07 16:39:04 +00:00
SIMULATED_SOURCES :: = ${SIMULATION_TOP_LEVEL_SOURCE } ${SOURCES } ${INCLUDES }
FPGA_SIM_SOURCES :: = fpga_config /${FPGA_BOARD }/fpga_top .v ${SOURCES } ${FPGA_SOC_SIM_SOURCES } ${INCLUDES }
FPGA_SOURCES :: = fpga_config /${FPGA_BOARD }/fpga_top .v ${SOURCES } ${FPGA_SOC_SOURCES } ${INCLUDES }
2023-03-05 23:11:18 +00:00
#build options
VERILATOR_OPTS += --cc --exe
#binary options
VERILATOR_OPTS += --trace-fst --threads 1 --autoflush
#linter options
2023-11-12 00:07:33 +00:00
VERILATOR_OPTS += -Wall --Wno-DECLFILENAME
2023-03-05 23:11:18 +00:00
#optimisation options
VERILATOR_OPTS += -x-assign fast --x-initial fast
#For testing use:
2023-05-07 12:34:15 +00:00
#VERILATOR_OPTS += -x-assign unique --x-initial unique
2023-03-05 23:11:18 +00:00
2023-12-07 16:39:04 +00:00
################################################################################
#### SIMULATION RECIPES ####
################################################################################
2023-02-16 01:46:22 +00:00
# COMPILING
2023-12-07 16:39:04 +00:00
${SYSTEM_VVP} : ${SIMULATED_SOURCES } ${EVENT_SIM_TESTBENCH }
2023-02-16 01:46:22 +00:00
${ QUIET_IVERILOG }
2023-12-05 21:46:46 +00:00
${ Q } iverilog -g2012 -D CALCULATE_IPC -D OUTPUT_JSON_STATISTICS -o " $@ " ${ SIMULATION_TOP_LEVEL_SOURCE } ${ SOURCES } ${ EVENT_SIM_TESTBENCH }
2023-02-16 01:46:22 +00:00
2023-03-05 23:11:18 +00:00
${VERILATOR_BIN} : ${VERILATOR_BIN }.mk
2023-12-06 18:12:57 +00:00
${ Q } make ${ MAKEOPTS } PRINT_PATH_PREFIX = ${ PRINT_PATH_PREFIX } obj_dir/ OPT_FAST = "-O2 -march=native -mtune=native" -C obj_dir -f ../verilator_makefile Vsystem
2023-03-05 23:11:18 +00:00
2023-12-07 16:39:04 +00:00
${VERILATOR_BIN}.mk : ${VERILATOR_TESTBENCH } ${SIMULATED_SOURCES }
2023-03-04 08:37:43 +00:00
${ QUIET_VERILATOR }
2023-12-07 16:39:04 +00:00
mkdir -p ${ BUILD_FILES_PREFIX } /sim_${ FPGA_BOARD } /
2023-12-09 02:39:14 +00:00
rm -f ${ VERILATOR_BIN } .mk # Verilator doesn't update this when just changing the testbench and the makefile breaks so we remove it to make sure
2023-12-03 19:24:12 +00:00
${ Q } verilator -DCALCULATE_IPC -DOUTPUT_JSON_STATISTICS ${ VERILATOR_OPTS } $^
2023-02-16 01:46:22 +00:00
2023-12-07 16:39:04 +00:00
${BUILD_FILES_PREFIX}/sim_${FPGA_BOARD}/${VERILATOR_FPGA_BIN} : ${BUILD_FILES_PREFIX }/sim_ ${FPGA_BOARD }/${VERILATOR_FPGA_BIN }.mk
${ Q } make ${ MAKEOPTS } PRINT_PATH_PREFIX = ${ PRINT_PATH_PREFIX } ${ BUILD_FILES_PREFIX } /sim_${ FPGA_BOARD } / OPT_FAST = "-O2 -march=native -mtune=native" -C " ${ BUILD_FILES_PREFIX } /sim_ ${ FPGA_BOARD } / " -f ../../verilator_makefile_fpga Vfpga_top
2023-12-05 21:46:46 +00:00
2023-12-07 16:39:04 +00:00
${BUILD_FILES_PREFIX}/sim_${FPGA_BOARD}/${VERILATOR_FPGA_BIN}.mk : fpga_config /${FPGA_BOARD }/testbench .cpp ${FPGA_SIM_SOURCES }
${ QUIET_VERILATOR }
2023-12-09 02:39:14 +00:00
mkdir -p " ${ BUILD_FILES_PREFIX } /sim_ ${ FPGA_BOARD } / " # Verilator doesn't update this when just changing the testbench and the makefile breaks so we remove it to make sure
rm -f ${ BUILD_FILES_PREFIX } /sim_${ FPGA_BOARD } /${ VERILATOR_FPGA_BIN } .mk
2023-12-07 16:39:04 +00:00
${ Q } verilator -DCALCULATE_IPC -DOUTPUT_JSON_STATISTICS --Mdir ${ BUILD_FILES_PREFIX } /sim_${ FPGA_BOARD } / ${ VERILATOR_OPTS } ../../fpga_config/${ FPGA_BOARD } /testbench.cpp ${ FPGA_SIM_SOURCES }
2023-12-05 21:46:46 +00:00
2023-12-07 16:39:04 +00:00
.PHONY : fpga_sim
2024-02-09 23:28:21 +00:00
fpga_sim fpga_sim.fst : ../boot_code /i 2c_bootloader .stxt ${BUILD_FILES_PREFIX }/sim_ ${FPGA_BOARD }/${VERILATOR_FPGA_BIN } ${MICROCODE } simplified_ucode .txt
2023-12-07 16:39:04 +00:00
$( call QUIET_VERILATOR_RUN,$( word 2,$^) ,$<)
2024-02-09 23:28:21 +00:00
${ Q } ${ NUMACTL } " ${ BUILD_FILES_PREFIX } /sim_ ${ FPGA_BOARD } / ${ VERILATOR_FPGA_BIN } " +VERSION= ${ VERSION } +WAVEFORM= "fpga_sim.fst" +COMMIT= ${ COMMIT } +BOOT_CODE= "../boot_code/i2c_bootloader.stxt" +MICROCODE= "simplified_ucode.txt"
2023-11-07 14:37:22 +00:00
2023-12-07 16:39:04 +00:00
################################################################################
#### FPGA/ASIC RECIPES ####
################################################################################
2023-11-02 00:29:14 +00:00
2023-11-12 04:04:56 +00:00
simplified_ucode.txt : ucode .txt
${ Q } tr 'x' '0' < $^ | sed 's@//.*@@' | grep ^@ | sort | sed 's/.* .//;s/ $$//' | tr -d _ > $@
2023-12-05 21:46:46 +00:00
external_ip/litedram_core_ecp5_phy.v :
${ QUIET_DOWNLOAD }
${ Q } ../tools/gen_litedram.sh -q " $@ "
2023-12-07 16:39:04 +00:00
external_ip/litedram_core_ecp5_phy_sim.v :
${ QUIET_DOWNLOAD }
${ Q } ../tools/gen_litedram.sh --simulation -q " $@ "
2023-12-05 21:46:46 +00:00
#########################################
## SYNTHESIS RECIPES
2023-11-06 08:12:58 +00:00
#TODO: we are relying on yosys to trim the input program txt file and hope its enough for the whole program...
2023-12-07 16:39:04 +00:00
${BUILD_FILES_PREFIX}synth_ecp5_${FPGA_BOARD}.json : ${FPGA_SOURCES } ${FPGA_BOOTCODE } simplified_ucode .txt
2023-11-02 00:29:14 +00:00
${ QUIET_YOSYS }
2023-12-07 16:39:04 +00:00
${ Q } yosys -q -p 'read_verilog -defer -noautowire -sv ' " ${ FPGA_SOURCES } ; attrmap -tocase keep -imap keep= " true" keep=1 -imap keep=" false" keep=0 -remove keep=0; synth_ecp5 -json \" $@ \" -abc9 -top fpga_top "
2023-11-02 00:29:14 +00:00
2023-12-05 21:46:46 +00:00
##########################################
## PLACE AND ROUTE RECIPES
2023-11-23 23:26:13 +00:00
2023-12-06 18:12:57 +00:00
${BUILD_FILES_PREFIX}nextpnr-ecp5_${BUILD_NAME}.bit : ${BUILD_FILES_PREFIX }synth_ ${FPGA_BOARD }.json
2023-11-02 00:29:14 +00:00
${ QUIET_NEXTPNR }
2023-12-05 21:46:46 +00:00
${ Q } printf '\e[1;30mNotice: nextpnr rng seed is : %s\e[0m\n' " ${ FPGA_SEED } "
${ Q } nextpnr-ecp5 --seed ${ FPGA_SEED } --Werror -q --json $< --textcfg " $@ _config_temp " ${ NEXTPNR_ECP5_DEV } --package ${ ECP5_PACKAGE } --speed ${ ECP5_SPEED_GRADE } --lpf fpga_config/${ FPGA_BOARD } /pin_constraint.pcf --report= ${ BUILD_FILES_PREFIX } nextpnr_report_${ BUILD_NAME } .json
${ Q } ../tools/parse_nextpnr_stats.sh --brief ${ BUILD_FILES_PREFIX } nextpnr_report_${ BUILD_NAME } .json
${ Q } mv " $@ _config_temp " " $@ _config " # nextpnr-ecp5 will still generate a file even if it fails breaking the assumptions of the build system.
2023-11-02 00:29:14 +00:00
${ QUIET_ECPPACK }
2023-12-05 21:46:46 +00:00
${ Q } ecppack --compress --freq 38.8 --input $@ _config --bit $@
nextpnr-gui : ${BUILD_FILES_PREFIX }synth_ ${FPGA_BOARD }.json
${ QUIET_NEXTPNR }
${ Q } nextpnr-ecp5 --seed ${ FPGA_SEED } --json $< ${ NEXTPNR_ECP5_DEV } --package ${ ECP5_PACKAGE } --speed ${ ECP5_SPEED_GRADE } --lpf fpga_config/${ FPGA_BOARD } /pin_constraint.pcf --gui
##########################################
## BITSTREAM MODIFICATION FOR/AND UPLOADING
2023-11-02 00:29:14 +00:00
2023-12-05 21:46:46 +00:00
${BUILD_FILES_PREFIX}bitstream_${BUILD_NAME}.dfu : ${BUILD_FILES_PREFIX }bitstream_ ${BUILD_NAME }.bit
2023-11-02 00:29:14 +00:00
${ QUIET_DFU_SUFFIX }
2023-12-05 21:46:46 +00:00
${ Q } cp " $< " " $<.tempdfu "
2023-11-02 00:29:14 +00:00
@#From some testing, dfu-suffix does output errors to stderr so this should be fine
2023-12-05 21:46:46 +00:00
${ Q } dfu-suffix --vid 1209 --pid 5af0 --add " $<.tempdfu " > /dev/null
${ Q } mv " $<.tempdfu " " $@ "
2023-11-02 00:29:14 +00:00
2023-12-05 21:46:46 +00:00
dfu_upload : ${BUILD_FILES_PREFIX }bitstream_ ${BUILD_NAME }.dfu
2023-11-02 00:29:14 +00:00
${ QUIET_DFU_UTIL }
${ Q } stdbuf -o0 dfu-util --download " $< " | stdbuf -o0 tr '\n' '\a' | stdbuf -o0 tr '\r' '\n' | grep Download --line-buffered | stdbuf -o0 tr '\n' '\r' | stdbuf -o0 tr '\a' '\n'
2024-02-09 23:28:21 +00:00
minipro_upload :
${ QUIET_MINIPRO }
${ Q } minipro -p ${ ROM_PART_ID } -w ${ ROM_FILE }
2023-12-03 19:24:12 +00:00
2023-12-05 21:46:46 +00:00
################################################################################
#### CLEAN-UP ####
################################################################################
2023-11-23 23:26:13 +00:00
2023-11-02 00:29:14 +00:00
2023-02-16 01:46:22 +00:00
.PHONY : clean
clean :
$( call QUIET_CLEAN,system)
2023-12-05 21:46:46 +00:00
${ Q } rm -rf ${ SYSTEM_VVP } *.fst boot_code.txt boot_code.bin *memdump *memdumptxt obj_dir simplified_ucode.txt abc.history build
2023-12-08 22:10:11 +00:00
.PHONY : mrproper
mrproper :
$( call QUIET_MRPROPER,system)
${ Q } rm -f external_ip/litedram_core_ecp5_phy.v external_ip/litedram_core_ecp5_phy_sim.v