Skip to content

Commit 343abf8

Browse files
committed
Added fifo initialization from file
1 parent 2fad297 commit 343abf8

File tree

3 files changed

+118
-75
lines changed

3 files changed

+118
-75
lines changed

fifo_single_clock_ram.sv

+4-71
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// - configurable depth and data width
1616
// - only "normal" mode is supported here, no FWFT mode
1717
// - protected against overflow and underflow
18+
// - provides fifo contents initialization (!)
1819
//
1920

2021

@@ -49,7 +50,8 @@ module fifo_single_clock_ram #( parameter
4950
DEPTH_W = $clog2(DEPTH)+1, // elements counter width, extra bit to store
5051
// "fifo full" state, see cnt[] variable comments
5152

52-
DATA_W = 32 // data field width
53+
DATA_W = 32, // data field width
54+
INIT_FILE = ""
5355
)(
5456

5557
input clk,
@@ -87,7 +89,7 @@ assign r_req_f = r_req && ~empty;
8789
true_dual_port_write_first_2_clock_ram #(
8890
.RAM_WIDTH( DATA_W ),
8991
.RAM_DEPTH( DEPTH ),
90-
.INIT_FILE( "" )
92+
.INIT_FILE( INIT_FILE )
9193
) data_ram (
9294
.clka( clk ),
9395
.addra( w_ptr[DEPTH_W-1:0] ),
@@ -152,72 +154,3 @@ end
152154

153155
endmodule
154156

155-
156-
157-
module true_dual_port_write_first_2_clock_ram #( parameter
158-
RAM_WIDTH = 16,
159-
RAM_DEPTH = 8,
160-
INIT_FILE = ""
161-
)(
162-
input clka,
163-
input [clogb2(RAM_DEPTH-1)-1:0] addra,
164-
input ena,
165-
input wea,
166-
input [RAM_WIDTH-1:0] dina,
167-
output [RAM_WIDTH-1:0] douta,
168-
169-
input clkb,
170-
input [clogb2(RAM_DEPTH-1)-1:0] addrb,
171-
input enb,
172-
input web,
173-
input [RAM_WIDTH-1:0] dinb,
174-
output [RAM_WIDTH-1:0] doutb
175-
);
176-
177-
reg [RAM_WIDTH-1:0] BRAM [RAM_DEPTH-1:0];
178-
reg [RAM_WIDTH-1:0] ram_data_a = {RAM_WIDTH{1'b0}};
179-
reg [RAM_WIDTH-1:0] ram_data_b = {RAM_WIDTH{1'b0}};
180-
181-
// either initializes the memory values to a specified file or to all zeros
182-
// to match hardware
183-
generate
184-
if (INIT_FILE != "") begin: use_init_file
185-
initial
186-
$readmemh(INIT_FILE, BRAM, 0, RAM_DEPTH-1);
187-
end else begin: init_bram_to_zero
188-
integer ram_index;
189-
initial
190-
for (ram_index = 0; ram_index < RAM_DEPTH; ram_index = ram_index + 1)
191-
BRAM[ram_index] = {RAM_WIDTH{1'b0}};
192-
end
193-
endgenerate
194-
195-
always @(posedge clka)
196-
if (ena)
197-
if (wea) begin
198-
BRAM[addra] <= dina;
199-
ram_data_a <= dina;
200-
end else
201-
ram_data_a <= BRAM[addra];
202-
203-
always @(posedge clkb)
204-
if (enb)
205-
if (web) begin
206-
BRAM[addrb] <= dinb;
207-
ram_data_b <= dinb;
208-
end else
209-
ram_data_b <= BRAM[addrb];
210-
211-
// no output register
212-
assign douta = ram_data_a;
213-
assign doutb = ram_data_b;
214-
215-
// calculates the address width based on specified RAM depth
216-
function integer clogb2;
217-
input integer depth;
218-
for (clogb2=0; depth>0; clogb2=clogb2+1)
219-
depth = depth >> 1;
220-
endfunction
221-
222-
endmodule
223-

pos2bin.sv

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ integer i;
4545

4646
logic found_hot;
4747
always_comb begin
48-
err_multi_hot=0;
49-
bin[(BIN_WIDTH-1):0]=0;
48+
err_multi_hot = 0;
49+
bin[(BIN_WIDTH-1):0] = 0;
5050
found_hot = 0;
51-
for (i = 0; i < POS_WIDTH ; i++) begin
51+
for (i=0; i<POS_WIDTH; i++) begin
5252

5353
if ( ~found_hot && pos[i] ) begin
54-
bin[(BIN_WIDTH-1):0] = i;
54+
bin[(BIN_WIDTH-1):0] = i[(BIN_WIDTH-1):0];
5555
end
5656

5757
if ( found_hot && pos[i] ) begin
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//------------------------------------------------------------------------------
2+
// true_dual_port_write_first_2_clock_ram.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// This is originally a Vivado template for block RAM with some minor edits
8+
// Also tested for Quartus IDE to automatically infer block memories
9+
//
10+
11+
12+
/* --- INSTANTIATION TEMPLATE BEGIN ---
13+
14+
true_dual_port_write_first_2_clock_ram #(
15+
.RAM_WIDTH( DATA_W ),
16+
.RAM_DEPTH( DEPTH ),
17+
.INIT_FILE( "" )
18+
) bram (
19+
.clka( w_clk ),
20+
.addra( w_ptr[DEPTH_W-1:0] ),
21+
.ena( w_req ),
22+
.wea( 1'b1 ),
23+
.dina( w_data[DATA_W-1:0] ),
24+
.douta( ),
25+
26+
.clkb( r_clk ),
27+
.addrb( r_ptr[DEPTH_W-1:0] ),
28+
.enb( r_req ),
29+
.web( 1'b0 ),
30+
.dinb( '0 ),
31+
.doutb( r_data[DATA_W-1:0] )
32+
);
33+
34+
--- INSTANTIATION TEMPLATE END ---*/
35+
36+
37+
module true_dual_port_write_first_2_clock_ram #( parameter
38+
RAM_WIDTH = 16,
39+
RAM_DEPTH = 8,
40+
INIT_FILE = ""
41+
)(
42+
input clka,
43+
input [clogb2(RAM_DEPTH-1)-1:0] addra,
44+
input ena,
45+
input wea,
46+
input [RAM_WIDTH-1:0] dina,
47+
output [RAM_WIDTH-1:0] douta,
48+
49+
input clkb,
50+
input [clogb2(RAM_DEPTH-1)-1:0] addrb,
51+
input enb,
52+
input web,
53+
input [RAM_WIDTH-1:0] dinb,
54+
output [RAM_WIDTH-1:0] doutb
55+
);
56+
57+
logic [RAM_WIDTH-1:0] BRAM [RAM_DEPTH-1:0];
58+
logic [RAM_WIDTH-1:0] ram_data_a = {RAM_WIDTH{1'b0}};
59+
logic [RAM_WIDTH-1:0] ram_data_b = {RAM_WIDTH{1'b0}};
60+
61+
// either initializes the memory values to a specified file or to all zeros
62+
generate
63+
if (INIT_FILE != "") begin: use_init_file
64+
initial
65+
$readmemh(INIT_FILE, BRAM, 0, RAM_DEPTH-1);
66+
end else begin: init_bram_to_zero
67+
integer ram_index;
68+
initial begin
69+
for (ram_index=0; ram_index<RAM_DEPTH; ram_index=ram_index+1 ) begin
70+
BRAM[ram_index] = {RAM_WIDTH{1'b0}};
71+
end
72+
end
73+
end
74+
endgenerate
75+
76+
always @(posedge clka) begin
77+
if (ena) begin
78+
if (wea) begin
79+
BRAM[addra] <= dina;
80+
ram_data_a <= dina;
81+
end else begin
82+
ram_data_a <= BRAM[addra];
83+
end
84+
end
85+
end
86+
87+
always @(posedge clkb) begin
88+
if (enb) begin
89+
if (web) begin
90+
BRAM[addrb] <= dinb;
91+
ram_data_b <= dinb;
92+
end else begin
93+
ram_data_b <= BRAM[addrb];
94+
end
95+
end
96+
end
97+
98+
// no output register
99+
assign douta = ram_data_a;
100+
assign doutb = ram_data_b;
101+
102+
// calculates the address width based on specified RAM depth
103+
function integer clogb2;
104+
input integer depth;
105+
for (clogb2=0; depth>0; clogb2=clogb2+1)
106+
depth = depth >> 1;
107+
endfunction
108+
109+
endmodule
110+

0 commit comments

Comments
 (0)