-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathdyn.sv
More file actions
188 lines (167 loc) · 5.52 KB
/
Copy pathdyn.sv
File metadata and controls
188 lines (167 loc) · 5.52 KB
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
185
186
187
188
/**
Implements a memory with sequential reads and writes.
- Both reads and writes are not guaranteed to have a given latency.
- Attempting to read and write at the same time is an error.
- The out signal is registered to the last value requested by the read_en signal.
- The out signal is undefined once write_en is asserted.
NOTE(nate): In practice we expect this implementation to be single cycle,
but should not be relied on as such.
In particular we probably eventually want to have `dyn_mems` exist as "virtual operators."
Which have a flexible latency, where the compiler can decide upon actual latency.
See #2111 (PR introducing this: https://github.com/calyxir/calyx/pull/2111)
and a more in depth discussion #1151 (https://github.com/calyxir/calyx/issues/1151)
*/
module dyn_mem_d1 #(
parameter WIDTH = 32,
parameter SIZE = 16,
parameter IDX_SIZE = 4
) (
// Common signals
input wire logic clk,
input wire logic reset,
input wire logic [IDX_SIZE-1:0] addr0,
input wire logic content_en,
output logic done,
// Read signal
output logic [ WIDTH-1:0] read_data,
// Write signals
input wire logic [ WIDTH-1:0] write_data,
input wire logic write_en
);
// Internal memory
logic [WIDTH-1:0] mem[SIZE-1:0];
// Register for the read output
logic [WIDTH-1:0] read_out;
assign read_data = read_out;
// Read value from the memory
always_ff @(posedge clk) begin
if (reset) begin
read_out <= '0;
end else if (content_en && !write_en) begin
/* verilator lint_off WIDTH */
read_out <= mem[addr0];
end else if (content_en && write_en) begin
// Explicitly clobber the read output when a write is performed
read_out <= 'x;
end else begin
read_out <= read_out;
end
end
// Propagate the done signal
always_ff @(posedge clk) begin
if (reset) begin
done <= '0;
end else if (content_en) begin
done <= '1;
end else begin
done <= '0;
end
end
// Write value to the memory
always_ff @(posedge clk) begin
if (!reset && content_en && write_en)
mem[addr0] <= write_data;
end
// Check for out of bounds access
`ifdef VERILATOR
always_comb begin
if (content_en && !write_en)
if (addr0 >= SIZE)
$error(
"comb_mem_d1: Out of bounds access\n",
"addr0: %0d\n", addr0,
"SIZE: %0d", SIZE
);
end
`endif
endmodule
module dyn_mem_d2 #(
parameter WIDTH = 32,
parameter D0_SIZE = 16,
parameter D1_SIZE = 16,
parameter D0_IDX_SIZE = 4,
parameter D1_IDX_SIZE = 4
) (
// Common signals
input wire logic clk,
input wire logic reset,
input wire logic [D0_IDX_SIZE-1:0] addr0,
input wire logic [D1_IDX_SIZE-1:0] addr1,
input wire logic content_en,
output logic done,
// Read signal
output logic [WIDTH-1:0] read_data,
// Write signals
input wire logic write_en,
input wire logic [ WIDTH-1:0] write_data
);
wire [D0_IDX_SIZE+D1_IDX_SIZE-1:0] addr;
assign addr = addr0 * D1_SIZE + addr1;
dyn_mem_d1 #(.WIDTH(WIDTH), .SIZE(D0_SIZE * D1_SIZE), .IDX_SIZE(D0_IDX_SIZE+D1_IDX_SIZE)) mem
(.clk(clk), .reset(reset), .addr0(addr),
.content_en(content_en), .read_data(read_data), .write_data(write_data), .write_en(write_en),
.done(done));
endmodule
module dyn_mem_d3 #(
parameter WIDTH = 32,
parameter D0_SIZE = 16,
parameter D1_SIZE = 16,
parameter D2_SIZE = 16,
parameter D0_IDX_SIZE = 4,
parameter D1_IDX_SIZE = 4,
parameter D2_IDX_SIZE = 4
) (
// Common signals
input wire logic clk,
input wire logic reset,
input wire logic [D0_IDX_SIZE-1:0] addr0,
input wire logic [D1_IDX_SIZE-1:0] addr1,
input wire logic [D2_IDX_SIZE-1:0] addr2,
input wire logic content_en,
output logic done,
// Read signal
output logic [WIDTH-1:0] read_data,
// Write signals
input wire logic write_en,
input wire logic [ WIDTH-1:0] write_data
);
wire [D0_IDX_SIZE+D1_IDX_SIZE+D2_IDX_SIZE-1:0] addr;
assign addr = addr0 * (D1_SIZE * D2_SIZE) + addr1 * (D2_SIZE) + addr2;
dyn_mem_d1 #(.WIDTH(WIDTH), .SIZE(D0_SIZE * D1_SIZE * D2_SIZE), .IDX_SIZE(D0_IDX_SIZE+D1_IDX_SIZE+D2_IDX_SIZE)) mem
(.clk(clk), .reset(reset), .addr0(addr),
.content_en(content_en), .read_data(read_data), .write_data(write_data), .write_en(write_en),
.done(done));
endmodule
module dyn_mem_d4 #(
parameter WIDTH = 32,
parameter D0_SIZE = 16,
parameter D1_SIZE = 16,
parameter D2_SIZE = 16,
parameter D3_SIZE = 16,
parameter D0_IDX_SIZE = 4,
parameter D1_IDX_SIZE = 4,
parameter D2_IDX_SIZE = 4,
parameter D3_IDX_SIZE = 4
) (
// Common signals
input wire logic clk,
input wire logic reset,
input wire logic [D0_IDX_SIZE-1:0] addr0,
input wire logic [D1_IDX_SIZE-1:0] addr1,
input wire logic [D2_IDX_SIZE-1:0] addr2,
input wire logic [D3_IDX_SIZE-1:0] addr3,
input wire logic content_en,
output logic done,
// Read signal
output logic [WIDTH-1:0] read_data,
// Write signals
input wire logic write_en,
input wire logic [ WIDTH-1:0] write_data
);
wire [D0_IDX_SIZE+D1_IDX_SIZE+D2_IDX_SIZE+D3_IDX_SIZE-1:0] addr;
assign addr = addr0 * (D1_SIZE * D2_SIZE * D3_SIZE) + addr1 * (D2_SIZE * D3_SIZE) + addr2 * (D3_SIZE) + addr3;
dyn_mem_d1 #(.WIDTH(WIDTH), .SIZE(D0_SIZE * D1_SIZE * D2_SIZE * D3_SIZE), .IDX_SIZE(D0_IDX_SIZE+D1_IDX_SIZE+D2_IDX_SIZE+D3_IDX_SIZE)) mem
(.clk(clk), .reset(reset), .addr0(addr),
.content_en(content_en), .read_data(read_data), .write_data(write_data), .write_en(write_en),
.done(done));
endmodule