-
Notifications
You must be signed in to change notification settings - Fork 2
/
RAM_tb.v
90 lines (66 loc) · 1.83 KB
/
RAM_tb.v
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
`timescale 1ns/1ps
module RAM_tb();
localparam CLK_PERIOD = 20;
reg clk;
initial begin
clk <= 0;
forever begin
#(CLK_PERIOD/2);
clk <= ~clk;
end
end
localparam DATA_WIDTH = 12;
localparam DEPTH = 8;
localparam ADDR_WIDTH = $clog2(DEPTH);
reg wrEn;
reg [DATA_WIDTH-1:0] dataIn;
wire [DATA_WIDTH-1:0] dataOut;
reg [ADDR_WIDTH-1:0] address;
RAM #(.DATA_WIDTH(DATA_WIDTH), .DEPTH(DEPTH), .ADDR_WIDTH(ADDR_WIDTH)) dut(.clk(clk), .wrEn(wrEn), .dataIn(dataIn), .address(address),
.dataOut(dataOut));
initial begin
@(posedge clk);
#(CLK_PERIOD*4/5);
wrEn <= 1'b1;
address <= 3;
dataIn <= 100;
@(posedge clk);
#(CLK_PERIOD*4/5);
wrEn <= 1'b0;
address <= 2;
dataIn <=30;
repeat (20) begin
@(posedge clk);
#(CLK_PERIOD*4/5); // to give data litte bit earlier to the posedge of the clk
dataIn = $random();
address = $random();
wrEn = $random();
end
$stop;
end
reg [DATA_WIDTH-1:0]test_memory[0:DEPTH-1];
reg temp_val;
function test_memory_write (input wrEn, [ADDR_WIDTH-1:0] address, [DATA_WIDTH-1:0] dataIn); begin
if (wrEn) begin
test_memory[address] = dataIn;
end
test_memory_write = 1'b0;
end
endfunction
function check (input [DATA_WIDTH-1:0] dataOut, [ADDR_WIDTH-1:0] address); begin
if (dataOut != test_memory[address]) begin
$display("dataOut = %h, memory_value = %h, address = %h", dataOut, test_memory[address], address);
end
check = 1'b0;
end
endfunction
initial begin
forever begin
@(posedge clk);
temp_val = test_memory_write (wrEn, address, dataIn);
@(negedge clk);
#(CLK_PERIOD*1/10);
temp_val = check (dataOut, address);
end
end
endmodule //RAM_tb