Skip to content

Commit db847e6

Browse files
committed
Added RAM templates
1 parent 90a8836 commit db847e6

3 files changed

+118
-20
lines changed

edge_detect.sv

100644100755
File mode changed.

true_dual_port_write_first_2_clock_ram.sv

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//------------------------------------------------------------------------------
22
// true_dual_port_write_first_2_clock_ram.sv
3+
// published as part of https://github.com/pConst/basic_verilog
34
// Konstantin Pavlov, [email protected]
45
//------------------------------------------------------------------------------
56

@@ -14,8 +15,9 @@
1415
true_dual_port_write_first_2_clock_ram #(
1516
.RAM_WIDTH( DATA_W ),
1617
.RAM_DEPTH( DEPTH ),
18+
.RAM_STYLE( "init.mem" ), // "block","register","M10K","logic",...
1719
.INIT_FILE( "" )
18-
) bram (
20+
) DR1 (
1921
.clka( w_clk ),
2022
.addra( w_ptr[DEPTH_W-1:0] ),
2123
.ena( w_req ),
@@ -37,6 +39,7 @@ true_dual_port_write_first_2_clock_ram #(
3739
module true_dual_port_write_first_2_clock_ram #( parameter
3840
RAM_WIDTH = 16,
3941
RAM_DEPTH = 8,
42+
RAM_STYLE = "",
4043
INIT_FILE = ""
4144
)(
4245
input clka,
@@ -54,26 +57,33 @@ module true_dual_port_write_first_2_clock_ram #( parameter
5457
output [RAM_WIDTH-1:0] doutb
5558
);
5659

60+
// Xilinx:
61+
// ram_style = "{ auto | block | distributed | register | ultra }"
62+
// "ram_style" is equivalent to "ramstyle" in Vivado
5763

58-
59-
logic [RAM_WIDTH-1:0] BRAM [RAM_DEPTH-1:0];
60-
logic [RAM_WIDTH-1:0] ram_data_a = {RAM_WIDTH{1'b0}};
61-
logic [RAM_WIDTH-1:0] ram_data_b = {RAM_WIDTH{1'b0}};
64+
// Altera:
65+
// ramstyle = "{ logic | M9K | MLAB }" and other variants
6266

6367
// ONLY FOR QUARTUS IDE
6468
// You can provide initialization in convinient .mif format
65-
//(* ram_init_file = INIT_FILE *) logic [RAM_WIDTH-1:0] BRAM [RAM_DEPTH-1:0];
69+
//(* ram_init_file = INIT_FILE *) logic [RAM_WIDTH-1:0] data_mem [RAM_DEPTH-1:0];
70+
71+
(* ramstyle = RAM_STYLE *) logic [RAM_WIDTH-1:0] data_mem [RAM_DEPTH-1:0];
72+
73+
74+
logic [RAM_WIDTH-1:0] ram_data_a = {RAM_WIDTH{1'b0}};
75+
logic [RAM_WIDTH-1:0] ram_data_b = {RAM_WIDTH{1'b0}};
6676

6777
// either initializes the memory values to a specified file or to all zeros
6878
generate
6979
if (INIT_FILE != "") begin: use_init_file
7080
initial
71-
$readmemh(INIT_FILE, BRAM, 0, RAM_DEPTH-1);
81+
$readmemh(INIT_FILE, data_mem, 0, RAM_DEPTH-1);
7282
end else begin: init_bram_to_zero
73-
integer ram_index;
83+
integer i;
7484
initial begin
75-
for (ram_index=0; ram_index<RAM_DEPTH; ram_index=ram_index+1 ) begin
76-
BRAM[ram_index] = {RAM_WIDTH{1'b0}};
85+
for (i=0; i<RAM_DEPTH; i=i+1 ) begin
86+
data_mem[i] = {RAM_WIDTH{1'b0}};
7787
end
7888
end
7989
end
@@ -82,21 +92,21 @@ module true_dual_port_write_first_2_clock_ram #( parameter
8292
always @(posedge clka) begin
8393
if (ena) begin
8494
if (wea) begin
85-
BRAM[addra] <= dina;
95+
data_mem[addra] <= dina;
8696
ram_data_a <= dina;
8797
end else begin
88-
ram_data_a <= BRAM[addra];
98+
ram_data_a <= data_mem[addra];
8999
end
90100
end
91101
end
92102

93103
always @(posedge clkb) begin
94104
if (enb) begin
95105
if (web) begin
96-
BRAM[addrb] <= dinb;
106+
data_mem[addrb] <= dinb;
97107
ram_data_b <= dinb;
98108
end else begin
99-
ram_data_b <= BRAM[addrb];
109+
ram_data_b <= data_mem[addrb];
100110
end
101111
end
102112
end
@@ -105,12 +115,7 @@ module true_dual_port_write_first_2_clock_ram #( parameter
105115
assign douta = ram_data_a;
106116
assign doutb = ram_data_b;
107117

108-
// calculates the address width based on specified RAM depth
109-
function integer clogb2;
110-
input integer depth;
111-
for (clogb2=0; depth>0; clogb2=clogb2+1)
112-
depth = depth >> 1;
113-
endfunction
118+
`include "clogb2.svh"
114119

115120
endmodule
116121

true_single_port_write_first_ram.sv

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//------------------------------------------------------------------------------
2+
// true_single_port_write_first_ram.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// This is single port RAM/ROM module
8+
// Also tested for Quartus IDE to automatically infer block memories
9+
//
10+
11+
12+
/* --- INSTANTIATION TEMPLATE BEGIN ---
13+
14+
true_single_port_write_first_ram #(
15+
.RAM_WIDTH( DATA_W ),
16+
.RAM_DEPTH( DEPTH ),
17+
.RAM_STYLE( "init.mem" ), // "block","register","M10K","logic",...
18+
.INIT_FILE( "" )
19+
) SR1 (
20+
.clka( w_clk ),
21+
.addra( w_ptr[DEPTH_W-1:0] ),
22+
.ena( w_req ),
23+
.wea( 1'b1 ),
24+
.dina( w_data[DATA_W-1:0] ),
25+
.douta( )
26+
);
27+
28+
--- INSTANTIATION TEMPLATE END ---*/
29+
30+
31+
module true_single_port_write_first_ram #( parameter
32+
RAM_WIDTH = 16,
33+
RAM_DEPTH = 8,
34+
RAM_STYLE = "",
35+
INIT_FILE = ""
36+
)(
37+
input clka,
38+
input [clogb2(RAM_DEPTH-1)-1:0] addra,
39+
input ena,
40+
input wea,
41+
input [RAM_WIDTH-1:0] dina,
42+
output [RAM_WIDTH-1:0] douta
43+
);
44+
45+
// Xilinx:
46+
// ram_style = "{ auto | block | distributed | register | ultra }"
47+
// "ram_style" is equivalent to "ramstyle" in Vivado
48+
49+
// Altera:
50+
// ramstyle = "{ logic | M9K | MLAB }" and other variants
51+
52+
// ONLY FOR QUARTUS IDE
53+
// You can provide initialization in convinient .mif format
54+
//(* ram_init_file = INIT_FILE *) logic [RAM_WIDTH-1:0] data_mem [RAM_DEPTH-1:0];
55+
56+
(* ramstyle = RAM_STYLE *) logic [RAM_WIDTH-1:0] data_mem [RAM_DEPTH-1:0];
57+
58+
59+
logic [RAM_WIDTH-1:0] ram_data_a = {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, data_mem, 0, RAM_DEPTH-1);
66+
end else begin: init_bram_to_zero
67+
integer i;
68+
initial begin
69+
for (i=0; i<RAM_DEPTH; i=i+1 ) begin
70+
data_mem[i] = {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+
data_mem[addra] <= dina;
80+
ram_data_a <= dina;
81+
end else begin
82+
ram_data_a <= data_mem[addra];
83+
end
84+
end
85+
end
86+
87+
// no output register
88+
assign douta = ram_data_a;
89+
90+
`include "clogb2.svh"
91+
92+
endmodule
93+

0 commit comments

Comments
 (0)