1+ import pycde
2+
3+ from pycde import Clock , Module , Reset , Input , Output , generator , ir
4+ from pycde .types import Bits
5+ from pycde .circt .dialects import sv , hw
6+ from pycde import support
7+ from pycde .signals import _FromCirctValue
8+
9+ def unknown_location ():
10+ return ir .Location .unknown ()
11+ support .get_user_loc .__code__ = unknown_location .__code__
12+
13+ # 替换support模块中的函数对象
14+
15+ class IfElseExample (Module ):
16+ """ A simple example of using CaseOp in PyCDE."""
17+
18+ module_name = "ifelse_example"
19+ clk = Clock ()
20+ rst = Reset ()
21+
22+ sel = Input (Bits (1 ))
23+ data_i0 = Input (Bits (32 ))
24+ data_i1 = Input (Bits (32 ))
25+ data_o = Output (Bits (32 ))
26+
27+ @generator
28+ def construct (ports ):
29+ i32_type = ir .IntegerType .get_signless (32 )
30+ result_reg = sv .RegOp (hw .InOutType .get (i32_type ), name = "result_reg" )
31+
32+ al = sv .AlwaysCombOp ()
33+ al .body .blocks .append ()
34+ with ir .InsertionPoint (al .body .blocks [0 ]):
35+ if_op = sv .IfOp (ports .sel .value )
36+ # 为每个if分支赋值
37+ if_op .thenRegion .blocks .append ()
38+ with ir .InsertionPoint (if_op .thenRegion .blocks [0 ]):
39+ sv .VerbatimOp (ir .StringAttr .get (f"// value = 32'h{ 0 } ;\n " ), [])
40+ sv .BPAssignOp (result_reg , ports .data_i0 .value )
41+ if_op .elseRegion .blocks .append ()
42+ with ir .InsertionPoint (if_op .elseRegion .blocks [0 ]):
43+ sv .VerbatimOp (ir .StringAttr .get (f"// value = 32'h{ 1 } ;\n " ), [])
44+ sv .BPAssignOp (result_reg , ports .data_i1 .value )
45+
46+ # 从寄存器读取值并赋给输出端口
47+ ports .data_o = _FromCirctValue (sv .ReadInOutOp (result_reg ).result )
48+
49+ if __name__ == "__main__" :
50+
51+ s = pycde .System (IfElseExample ,
52+ name = "ifelse_example" ,
53+ output_directory = "build/ifelse_example" ,)
54+ s .compile ()
0 commit comments