summaryrefslogtreecommitdiff
path: root/hdl/testing/memtest.py
blob: 459450a8355ac1b6f0534ad06a88523f570d2675 (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
from amaranth import *
from amaranth.cli import main

from amaranth.back import verilog, cxxrtl, rtlil

from hdl.config import *
from hdl.utils import *

class RegisterFile(Elaboratable):
    def __init__(self, sim=True):
        self.adr   = Signal(12)
        self.dat_r = Signal(16)
        self.dat_w = Signal(16)
        self.we    = Signal()
        self.mem   = Memory(width=16, depth=2**len(self.adr), no_init=True, simulate=sim)

        self.ports = {'in': [self.adr, self.we], 'out': [self.dat_r]}

    def elaborate(self, platform):
        m = Module()
        m.submodules.rdport = rdport = self.mem.read_port()
        m.submodules.wrport = wrport = self.mem.write_port()
        m.d.comb += [
            rdport.addr.eq(self.adr),
            self.dat_r.eq(rdport.data),
            wrport.addr.eq(self.adr),
            wrport.data.eq(self.dat_w),
            wrport.en.eq(self.we),
        ]
        return m


def test_mem():
    dut = RegisterFile(sim=True)
    def proc():
        yield from eval()
        yield dut.adr.eq(0)
        yield dut.dat_w.eq(27)
        yield dut.we.eq(1)
        yield from step()
        yield dut.we.eq(0)
        yield from step()
        data_out = yield dut.dat_r
        print(f'Data out: {data_out}')
    sim(dut, proc)


if __name__ == "__main__":
    hdl = RegisterFile()
    # test_mem()
    out = verilog.convert(hdl, ports=hdl.ports['in'] + hdl.ports['out'])
    with open(os.path.join(VERILOG_DIR, os.path.basename("memtest.py".replace('.py', '.v'))), 'w') as f:
        f.write(out)