summaryrefslogtreecommitdiff
path: root/archive/testing/multi_clock.py
blob: e377156366aae97d904728c6535abad9008b9cee (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
from amaranth import *
from amaranth.back import verilog, cxxrtl
from amaranth.cli import main
from amaranth.sim import Simulator, Settle, Delay

BASENAME = "multi_clock"

class SubM(Elaboratable):
    def __init__(self, domain=None):
        self.inv = Signal()
        self.domain=domain

    def elaborate(self, platform):
        m = Module()

        m.d.sync += self.inv.eq(~self.inv)

        return m

class top(Elaboratable):
    def __init__(self):
        self.sig_slow = Signal()
        self.sig_fast = Signal()

        self.div = Signal(2)

    def elaborate(self, platform):
        m = Module()

        m.domains += ClockDomain('slow')
        m.d.sync += [self.div.eq(self.div + 1)]
        m.d.comb += ClockSignal('slow').eq(self.div[-1])

        m.submodules.subm1 = SubM()
        m.submodules.subm2 = DomainRenamer("slow")(SubM())

        m.d.sync += self.sig_fast.eq(m.submodules.subm1.inv)
        m.d.slow += self.sig_slow.eq(m.submodules.subm2.inv)

        return m

def test_shift_reg():
    dut = top()

    def proc1():
        for _ in range(16):
            yield
            yield Settle()
    
    sim = Simulator(dut)
    sim.add_clock(1e-6)
    sim.add_sync_process(proc1)
    
    with sim.write_vcd(BASENAME + '.vcd'):
        sim.run()


if __name__ == '__main__':

    if sys.argv[1] == "sim":
        test_shift_reg()
        exit()

    # m = ShiftReg(8)

    # if sys.argv[1] == "v":
    #     out = verilog.convert(m, ports=m.ports)
    #     with open(BASENAME + '.v','w') as f:
    #         f.write(out)
    
    # elif sys.argv[1] == "cc":
    #     out = cxxrtl.convert(m, ports=m.ports)
    #     with open(BASENAME + '.cc','w') as f:
    #         f.write(out)