#***************************************************************************************
# Copyright (c) 2025 Beijing Institute of Open Source Chip (BOSC)
# Copyright (c) 2025 Institute of Computing Technology, Chinese Academy of Sciences
#
# XiangShan is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#          http://license.coscl.org.cn/MulanPSL2
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#
# See the Mulan PSL v2 for more details.
#***************************************************************************************

PDB_HOME          := $(abspath $(BUILD_DIR)/xspdb)
RTL_HOME          := $(abspath $(RTL_DIR))
RTL_FILELIST      := $(RTL_HOME)/filelist.f
DIFFTEST_HOME     := $(abspath $(PDB_HOME)/pydifftest)

DIFFTEST_INCLUDE   = $(abspath difftest/src/test/vsrc/common)
PICKER_INCLUDE     = $(shell picker --show_xcom_lib_location_cpp | grep include | awk '{print $$2}')
PYTHON_VERSION     = $(shell python3 --version | awk '{print $$2}' | cut -d'.' -f1-2)
SIM_LDFLAGS        = $(shell cat $(DIFFTEST_HOME)/sim_ld_flags.txt)

PYTHON_DEP_FILE    = scripts/xspdb/requirements.txt
PDB_PYTHONPATH    := $(PDB_HOME):script/xspdb
PDB_LD_PRELOAD    := $(PDB_HOME)/pyxscore/xspcomm/libxspcomm.so.0.0.1 \
			      $(PDB_HOME)/pyxscore/libdifftest.so

.PHONY: pdb pdb-run check-deps

pdb: $(DIFFTEST_HOME)/_difftest.so $(PDB_HOME)/libUTSimTop.so

$(RTL_FILELIST): sim-verilog

$(DIFFTEST_HOME)/_difftest.so: $(RTL_FILELIST)
	$(MAKE) -C ./difftest pydifftest WITH_CONSTANTIN=0 WITH_CHISELDB=0
	ln -srf ../pydifftest/_difftest.so $(DIFFTEST_HOME)/libdifftest.so

$(PDB_HOME)/picker.f: $(DIFFTEST_HOME)/_difftest.so
	find $(realpath $(RTL_HOME)) -maxdepth 1 \( -name "*.sv" -o -name "*.v" -o -name "*.cpp" -o -name "*.so" \) -not -name "SimTop.sv" -printf "%p\n" > $(PDB_HOME)/picker.f
	find $(realpath $(DIFFTEST_INCLUDE)) -maxdepth 1 \( -name "*.v" \) -not -name "SimTop.sv" -printf "%p\n" >> $(PDB_HOME)/picker.f
	echo "$(DIFFTEST_HOME)/libdifftest.so" >> $(PDB_HOME)/picker.f

$(PDB_HOME)/libUTSimTop.so: $(PDB_HOME)/picker.f 
	time picker export $(RTL_HOME)/SimTop.sv \
		--rw 1 \
		-w $(PDB_HOME)/xs.fst \
		--lang python \
		--tdir $(PDB_HOME)/pyxscore \
		--fs $(PDB_HOME)/picker.f \
		-V "--no-timing;--threads;128;+define+DIFFTEST;-I$(NOOP_HOME)/build/generated-src" \
		-C "-fPIC -lz -I$(PICKER_INCLUDE) -L$(PDB_HOME)/pyxscore -ldifftest -lpython$(PYTHON_VERSION) $(SIM_LDFLAGS)" \
		--autobuild=false
	ln -s ../pydifftest/_difftest.so $(PDB_HOME)/pyxscore/libdifftest.so
	$(MAKE) -C $(PDB_HOME)/pyxscore

pdb-run: check-deps
	LD_PRELOAD="$(PDB_LD_PRELOAD)" \
	PYTHONPATH="$(PDB_PYTHONPATH)" \
	python3 scripts/pdb-run.py

check-deps:
	@[ -f "$(PYTHON_DEP_FILE)" ] || { echo "Error: $(PYTHON_DEP_FILE) not found"; exit 1; }; \
	grep -v '^[#[:space:]]*$$' $(PYTHON_DEP_FILE) | while read -r req; do \
		ver=$$(pip show "$${req%%>=*}" 2>/dev/null | awk '/^Version:/{print $$2}') || exit 1; \
		[ -n "$$ver" ] && [ "$$(printf "$$ver\n$${req##*>=}" | sort -V | head -n1)" = "$${req##*>=}" ] || \
			{ echo "Error: Dependency requirement not satisfied, Please run 'pip install -r $(PYTHON_DEP_FILE)'"; exit 1; }; \
	done

