fromtypingimportListfromnmigenimportElaboratable,Module,Signalfromnmigen.back.pysimimportSimulator,Delayfromnmigen.buildimportPlatformfromnmigen.cliimportmain_parser,main_runnerclassAdder(Elaboratable):def__init__(self):self.x=Signal(8)self.y=Signal(8)self.out=Signal(8)defelaborate(self,paltform:Platform)->Module:m=Module()m.d.comb+=self.out.eq(self.x+self.y)returnmdefports(self)->List[Signal]:return[self.x,self.y,self.out]if__name__=="__main__":parser=main_parser()args=parser.parse_args()m=Module()m.submodules.adder=adder=Adder()# main_runner(parser, args, m, ports=[] + adder.ports())x=Signal(8)y=Signal(8)m.d.comb+=adder.x.eq(x)m.d.comb+=adder.y.eq(y)sim=Simulator(m)defprocess():yieldx.eq(0x00)yieldy.eq(0x00)yieldDelay(1e-6)yieldx.eq(0xFF)yieldy.eq(0xFF)yieldDelay(1e-6)yieldx.eq(0x00)yieldDelay(1e-6)sim.add_process(process)withsim.write_vcd("test.vcd","test.gtkw",traces=[x,y]+adder.ports()):sim.run()print("end of code")
Run the simulation
python3 adder.py
Remember to type python3!
Get the simulation waveform from GTKwave
After firing simulator, it will generate a file call test.vcd.
gtkwave.exe test.vcd &
The GTKwave will pop up and click adder on the left. Select the signals and right click > recurse import > append.