1 |
2 |
eexuke |
//--------------------------------------------------------------------------------------------------
|
2 |
|
|
// Design : nova
|
3 |
|
|
// Author(s) : Ke Xu
|
4 |
|
|
// Email : eexuke@yahoo.com
|
5 |
|
|
// File : ram_sync_1r_sync_1w.v
|
6 |
|
|
// Generated : April 25,2005
|
7 |
|
|
// Copyright (C) 2008 Ke Xu
|
8 |
|
|
//-------------------------------------------------------------------------------------------------
|
9 |
|
|
// Description
|
10 |
|
|
// Synch Write, Synch Read RAM, NOT synthesizable
|
11 |
|
|
// In real silicon, use customized RAM instead of DFF
|
12 |
|
|
// legal range:data_width [ 1 to 256 ]
|
13 |
|
|
// legal range:data_depth [ 2 to 1024 ]
|
14 |
|
|
// Input data :data_in[data_width-1:0]
|
15 |
|
|
// Output data:data_out[data_width-1:0]
|
16 |
|
|
// Read Address :rd_addr[addr_width-1:0]
|
17 |
|
|
// Write Address:wr_addr[addr_width-1:0]
|
18 |
|
|
// Write enable (active low): wr_n
|
19 |
|
|
// Chip select (active low): cs_n
|
20 |
|
|
// Reset (active low): rst_n
|
21 |
|
|
// Clock:clk
|
22 |
|
|
//-------------------------------------------------------------------------------------------------
|
23 |
|
|
|
24 |
|
|
// synopsys translate_off
|
25 |
|
|
`include "timescale.v"
|
26 |
|
|
// synopsys translate_on
|
27 |
|
|
`include "nova_defines.v"
|
28 |
|
|
|
29 |
|
|
module ram_sync_1r_sync_1w (clk, rst_n, wr_n, rd_n, wr_addr, rd_addr, data_in, data_out);
|
30 |
|
|
|
31 |
|
|
parameter data_width = 4; //will be overrided during module instantiation
|
32 |
|
|
parameter data_depth = 8; //will be overrided during module instantiation
|
33 |
|
|
|
34 |
|
|
`define addr_width ((data_depth>32)?((data_depth>256)?((data_depth>512)?10:9):((data_depth>128)?8:((data_depth>64)?7:6))):((data_depth>8) ?((data_depth>16) ?5:4) :((data_depth>4) ?3:((data_depth>2) ?1:0))))
|
35 |
|
|
|
36 |
|
|
input clk;
|
37 |
|
|
input rst_n;
|
38 |
|
|
input wr_n;
|
39 |
|
|
input rd_n;
|
40 |
|
|
input [`addr_width-1:0] wr_addr;
|
41 |
|
|
input [`addr_width-1:0] rd_addr;
|
42 |
|
|
input [data_width-1:0] data_in;
|
43 |
|
|
output [data_width-1:0] data_out;
|
44 |
|
|
|
45 |
|
|
reg [data_width-1:0] data_out;
|
46 |
|
|
reg [data_width-1:0] ram [data_depth-1:0];
|
47 |
|
|
|
48 |
|
|
//data_width, data_depth, simultaneously read/write check
|
49 |
|
|
initial
|
50 |
|
|
begin:parameter_check
|
51 |
|
|
integer param_error_flag;
|
52 |
|
|
param_error_flag = 0;
|
53 |
|
|
|
54 |
|
|
if ( (data_width < 1) || (data_width > 256) )
|
55 |
|
|
begin
|
56 |
|
|
param_error_flag = 1;
|
57 |
|
|
$display("Error: %m :\n Invalid value (%d) for parameter data_width (legal range: 1 to 256)",data_width );
|
58 |
|
|
end
|
59 |
|
|
|
60 |
|
|
if ( (data_depth < 2) || (data_depth > 1024 ) )
|
61 |
|
|
begin
|
62 |
|
|
param_error_flag = 1;
|
63 |
|
|
$display("Error: %m :\n Invalid value (%d) for parameter data_depth (legal range: 2 to 1024 )",data_depth );
|
64 |
|
|
end
|
65 |
|
|
|
66 |
|
|
/*
|
67 |
|
|
if ( (cs_n == 1'b0 && wr_n == 1'b0 && rd_n == 1'b0 ) )
|
68 |
|
|
begin
|
69 |
|
|
param_error_flag = 1;
|
70 |
|
|
$display("Error: %m :\n Not allowed! RAM simultaneously read and write occur");
|
71 |
|
|
end
|
72 |
|
|
*/
|
73 |
|
|
if ( param_error_flag == 1)
|
74 |
|
|
begin
|
75 |
|
|
$display("%m :\n Simulation aborted due to invalid parameter value(s)");
|
76 |
|
|
$finish;
|
77 |
|
|
end
|
78 |
|
|
|
79 |
|
|
end // end data_width & data_depth check
|
80 |
|
|
|
81 |
|
|
//read
|
82 |
|
|
always @ (posedge clk or negedge rst_n)
|
83 |
|
|
if (rst_n == 1'b0)
|
84 |
|
|
data_out <= 0;
|
85 |
|
|
else if (!rd_n)
|
86 |
|
|
data_out <= ram[rd_addr];
|
87 |
|
|
|
88 |
|
|
//write
|
89 |
|
|
always @ (posedge clk)
|
90 |
|
|
if (!wr_n)
|
91 |
|
|
ram[wr_addr] <= data_in;
|
92 |
|
|
|
93 |
|
|
endmodule
|
94 |
|
|
|