This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
async_memory.py
184 lines (147 loc) · 6.43 KB
/
async_memory.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Disable pylint's "your name is too short" warning.
# pylint: disable=C0103
from typing import List, Tuple
from nmigen import Array, Signal, Module, Elaboratable, ClockDomain
from nmigen.build import Platform
from nmigen.sim import Simulator, Delay
from nmigen.asserts import Assert, Assume, Cover, Past, Stable, Rose, Fell, AnyConst, Initial
from util import main
class AsyncMemory(Elaboratable):
"""Logic for a typical asynchronous memory.
Attributes:
addr: The address to read or write.
data_in: The input data (when writing).
data_out: The output data (when reading).
n_oe: Output enable, active low. When n_oe is 1, the output is 0.
n_wr: Write, active low.
"""
addr: Signal
data_in: Signal
data_out: Signal
n_oe: Signal
n_wr: Signal
def __init__(self, width: int, addr_lines: int, ext_init: bool = False):
"""Constructs an asynchronous memory.
Args:
width: The number of bits in each memory cell ("word").
addr_lines: The number of address lines. We only support memories
up to 16 address lines (so 64k words).
"""
assert width > 0
assert addr_lines > 0
assert addr_lines <= 16
attrs = [] if not ext_init else [("uninitialized", "")]
self.addr = Signal(addr_lines)
self.n_oe = Signal()
self.n_wr = Signal()
# There's no such thing as a tristate or bidirectional port
# in nMigen, so we do this instead.
self.data_in = Signal(width)
self.data_out = Signal(width)
# The actual memory
self._mem = Array(
[Signal(width, reset_less=True, attrs=attrs) for _ in range(2**addr_lines)])
def elaborate(self, _: Platform) -> Module:
"""Implements the logic of an asynchronous memory.
Essentially implements the wr-controlled write cycle, where
the oe signal doesn't matter. We can't really implement the
delays involved, so be safe with your signals :)
"""
m = Module()
# Local clock domain so we can clock data into the memory
# on the positive edge of n_wr.
wr_clk = ClockDomain("wr_clk", local=True)
m.domains.wr_clk = wr_clk
wr_clk.clk = self.n_wr
m.d.comb += self.data_out.eq(0)
with m.If(~self.n_oe & self.n_wr):
m.d.comb += self.data_out.eq(self._mem[self.addr])
m.d.wr_clk += self._mem[self.addr].eq(self.data_in)
return m
@classmethod
def sim(cls):
"""A quick simulation of the async memory."""
m = Module()
m.submodules.mem = mem = AsyncMemory(width=32, addr_lines=5)
sim = Simulator(m)
def process():
yield mem.n_oe.eq(0)
yield mem.n_wr.eq(1)
yield mem.data_in.eq(0xFFFFFFFF)
yield Delay(1e-6)
yield mem.addr.eq(1)
yield mem.n_oe.eq(0)
yield Delay(1e-6)
yield mem.n_oe.eq(1)
yield Delay(1e-6)
yield mem.data_in.eq(0xAAAA1111)
yield Delay(1e-6)
yield mem.n_wr.eq(0)
yield Delay(0.2e-6)
yield mem.n_wr.eq(1)
yield Delay(0.2e-6)
yield mem.data_in.eq(0xFFFFFFFF)
yield mem.n_oe.eq(0)
yield Delay(1e-6)
yield mem.addr.eq(0)
yield Delay(1e-6)
sim.add_process(process)
with sim.write_vcd("async_memory.vcd"):
sim.run_until(10e-6)
@classmethod
def formal(cls) -> Tuple[Module, List[Signal]]:
"""Formal verification for the async memory.
Note that you MUST have multiclock on in the sby file, because there is
more than one clock in the system -- the default formal clock and the
local clock inside the memory.
"""
m = Module()
m.submodules.mem = mem = AsyncMemory(width=32, addr_lines=5)
# Assume "good practices":
# * n_oe and n_wr are never simultaneously 0, and any changes
# are separated by at least a cycle to allow buffers to turn off.
# * memory address remains stable throughout a write cycle, and
# is also stable just before a write cycle.
m.d.comb += Assume(mem.n_oe | mem.n_wr)
# Paren placement is very important! While Python logical operators
# and, or have lower precedence than ==, the bitwise operators
# &, |, ^ have *higher* precedence. It can be confusing when it looks
# like you're writing a boolean expression, but you're actually writing
# a bitwise expression.
with m.If(Fell(mem.n_oe)):
m.d.comb += Assume((mem.n_wr == 1) & (Past(mem.n_wr) == 1))
with m.If(Fell(mem.n_wr)):
m.d.comb += Assume((mem.n_oe == 1) & (Past(mem.n_oe) == 1))
with m.If(Rose(mem.n_wr) | (mem.n_wr == 0)):
m.d.comb += Assume(Stable(mem.addr))
m.d.comb += Cover((mem.data_out == 0xAAAAAAAA)
& (Past(mem.data_out) == 0xBBBBBBBB))
# Make sure that when the output is disabled, the output is zero, and
# when enabled, it's whatever we're pointing at in memory.
with m.If(mem.n_oe == 1):
m.d.comb += Assert(mem.data_out == 0)
with m.Else():
m.d.comb += Assert(mem.data_out == mem._mem[mem.addr])
# If we just wrote data, make sure that cell that we pointed at
# for writing now contains the data we wrote.
with m.If(Rose(mem.n_wr)):
m.d.comb += Assert(mem._mem[Past(mem.addr)] == Past(mem.data_in))
# Pick an address, any address.
check_addr = AnyConst(5)
# We assert that unless that address is written, its data will not
# change. To know when we've written the data, we have to create
# a clock domain to let us save the data when written.
saved_data_clk = ClockDomain("saved_data_clk")
m.domains.saved_data_clk = saved_data_clk
saved_data_clk.clk = mem.n_wr
saved_data = Signal(32)
with m.If(mem.addr == check_addr):
m.d.saved_data_clk += saved_data.eq(mem.data_in)
with m.If(Initial()):
m.d.comb += Assume(saved_data == mem._mem[check_addr])
m.d.comb += Assume(mem.n_wr == 1)
with m.Else():
m.d.comb += Assert(saved_data == mem._mem[check_addr])
return m, [mem.addr, mem.data_in, mem.n_wr, mem.n_oe]
if __name__ == "__main__":
main(AsyncMemory)