[Day08] First fight with nMigen on simply an adder!

Prerequisites

If you would like to perform simulation with GTKwave, a opensource free waveform viewer. Download the software here

and put it under C:/gtkwave. Go to the windows path setting and add it under the $path variable.

The main function

We create a simple 8-bit adder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from typing import List

from nmigen import Elaboratable, Module, Signal
from nmigen.back.pysim import Simulator, Delay
from nmigen.build import Platform
from nmigen.cli import main_parser, main_runner

class Adder(Elaboratable):
    def __init__(self):
        self.x = Signal(8)
        self.y = Signal(8)
        self.out = Signal(8)

    def elaborate(self, paltform: Platform) -> Module:
        m = Module()

        m.d.comb += self.out.eq(self.x + self.y)
        return m 
    def ports(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)
    def process():
        yield x.eq(0x00)
        yield y.eq(0x00)
        yield Delay(1e-6)
        yield x.eq(0xFF)
        yield y.eq(0xFF)
        yield Delay(1e-6)
        yield x.eq(0x00)
        yield Delay(1e-6)
    sim.add_process(process)
    with sim.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. Imgur

updatedupdated2021-09-192021-09-19