5 proc WriteCocoTbTemplate {project_name repo_path lib_path {ext_path ""}} {
8 Msg Info "Creating cocotb library build script for project $project_name..."
9 set proj_list_dir $repo_path/Top/$project_name/list
10 set project [
file tail $project_name]
12 -list_files ".src,.ext" $proj_list_dir $repo_path]\
13 listLibraries listProperties listSrcSets
15 set compile_order [
Hierarchy $listProperties $listLibraries $repo_path "" 1 "" "" "" 0 0 1]
16 set py_file [open "cocotb_$project.py" w]
17 set valid_extensions ".vhd .vhdl .v .sv .svh .vh"
19 puts $py_file "#!/usr/bin/env python3"
20 puts $py_file "\"\"\"Auto-generated by Hog COCOTB command for project $project.\"\"\""
22 puts $py_file "import argparse"
23 puts $py_file "from pathlib import Path"
24 puts $py_file "import cocotb"
25 puts $py_file "from cocotb_tools.runner import get_runner"
27 puts $py_file "REPO_ROOT = Path(__file__).resolve().parent"
30 puts $py_file "@cocotb.test()"
31 puts $py_file "async def test_placeholder(dut):"
32 puts $py_file " \"\"\"Placeholder test -- replace with actual test logic.\"\"\""
36 puts $py_file "def build_libs(sim: str, build_dir: str) -> None:"
37 puts $py_file " runner = get_runner(sim)"
38 puts $py_file " build_path = Path(build_dir)"
44 foreach {f _} $compile_order {
45 set ext [
file extension $f]
46 if {$ext eq ".svh" || $ext eq ".vh"} {
47 set dir [
file dirname $f]
48 if {[lsearch -exact $svh_dirs $dir] == -1} {
54 foreach {f l} $compile_order {
56 set ext [
file extension $f]
58 if {![
IsInList $ext $valid_extensions] } {
62 puts $py_file " runner.build("
63 puts $py_file " hdl_library=\"$lib_name\","
64 puts $py_file " sources=\["
65 puts $py_file " REPO_ROOT / \"$rel\","
67 puts $py_file " build_dir=build_path,"
68 if {$ext == ".vhd" || $ext == ".vhdl"} {
69 puts $py_file " build_args = \[ \"-2008\","
70 if {$lib_path != ""} {
71 puts $py_file "\"-modelsimini\", \"$lib_path/modelsim.ini\""
74 }
elseif {$ext == ".sv" || $ext == ".svh" || $ext == ".v" || $ext == ".vh"} {
75 if {[
llength $svh_dirs] > 0} {
76 puts $py_file " build_args = \["
77 foreach dir $svh_dirs {
78 set rel_dir [
Relative $repo_path $dir]
79 puts $py_file " \"+incdir+\" + str(REPO_ROOT / \"$rel_dir\"),"
84 puts $py_file " always=True,"
90 puts $py_file "def run_test(sim: str, build_dir: str, toplevel: str) -> None:"
91 puts $py_file " runner = get_runner(sim)"
92 puts $py_file " runner.test("
93 puts $py_file " hdl_toplevel=toplevel,"
94 puts $py_file " test_module=Path(__file__).stem,"
95 puts $py_file " build_dir=build_dir,"
99 puts $py_file "if __name__ == \"__main__\":"
100 puts $py_file " parser = argparse.ArgumentParser()"
101 puts $py_file " parser.add_argument(\"--sim\", default=\"questa\")"
102 puts $py_file " parser.add_argument(\"--build-dir\", default=\"sim_build\")"
103 puts $py_file " parser.add_argument(\"--toplevel\", default=\"<toplevel>\")"
104 puts $py_file " args = parser.parse_args()"
105 puts $py_file " build_libs(args.sim, args.build_dir)"
106 puts $py_file " run_test(args.sim, args.build_dir, args.toplevel)"
109 Msg Info "cocotb Python script created: cocotb_$project.py"
110 Msg Info "Run it with: python cocotb_$project.py --sim questa --toplevel <your_toplevel>"