Hog v10.13.0
cocotb.tcl
Go to the documentation of this file.
1 ## @brief Write a cocotb python template to build the selected project with an empty test
2 # @param[in] project_name The name of the project to build
3 # @param[in] repo_path The main path of the repository
4 
5 proc WriteCocoTbTemplate {project_name repo_path lib_path {ext_path ""}} {
6  # Procedure body
7  cd $repo_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]
11  lassign [GetHogFiles -ext_path $ext_path \
12  -list_files ".src,.ext" $proj_list_dir $repo_path]\
13  listLibraries listProperties listSrcSets
14 
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"
18 
19  puts $py_file "#!/usr/bin/env python3"
20  puts $py_file "\"\"\"Auto-generated by Hog COCOTB command for project $project.\"\"\""
21  puts $py_file ""
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"
26  puts $py_file ""
27  puts $py_file "REPO_ROOT = Path(__file__).resolve().parent"
28  puts $py_file ""
29  puts $py_file ""
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.\"\"\""
33  puts $py_file " pass"
34  puts $py_file ""
35  puts $py_file ""
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)"
39  puts $py_file ""
40 
41  # Collect unique directories containing .svh/.vh header files so we can
42  # emit +incdir+ flags for every SV/SVH compilation unit.
43  set svh_dirs [list]
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} {
49  lappend svh_dirs $dir
50  }
51  }
52  }
53 
54  foreach {f l} $compile_order {
55  set lib_name $l
56  set ext [file extension $f]
57  set rel [Relative $repo_path $f]
58  if {![IsInList $ext $valid_extensions] } {
59  continue
60  }
61 
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\","
66  puts $py_file " \],"
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\""
72  }
73  puts $py_file "\],"
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\"),"
80  }
81  puts $py_file " \],"
82  }
83  }
84  puts $py_file " always=True,"
85  puts $py_file " )"
86  puts $py_file ""
87  }
88 
89  puts $py_file ""
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,"
96  puts $py_file " )"
97  puts $py_file ""
98  puts $py_file ""
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)"
107 
108  close $py_file
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>"
111 }