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
|
//-------------------------------------------------------------------
// Filename: gh_fifo_async16_sr.vhd
//
//
// Description:
// an Asynchronous FIFO
//
// Copyright (c) 2006 by George Huber
// an OpenCores.org Project
// free to use, but see documentation for conditions
//
// Revision History:
// Revision Date Author Comment
// -------- ---------- --------- -----------
// 1.0 12/17/06 h lefevre Initial revision
//
//------------------------------------------------------
// no timescale needed
module gh_fifo_async16_sr(
input wire clk_WR,
input wire clk_RD,
input wire rst,
input wire srst,
input wire WR,
input wire RD,
input wire [data_width - 1:0] D,
output wire [data_width - 1:0] Q,
output wire empty,
output wire full
);
parameter [31:0] data_width=8;
// size of data bus
// write clock
// read clock
// resets counters
// resets counters (sync with clk_WR)
// write control
// read control
wire [data_width - 1:0] ram_mem[15:0];
wire iempty;
wire ifull;
wire add_WR_CE;
reg [4:0] add_WR; // 4 bits are used to address MEM
reg [4:0] add_WR_GC; // 5 bits are used to compare
wire [4:0] n_add_WR; // for empty, full flags
reg [4:0] add_WR_RS; // synced to read clk
wire add_RD_CE;
reg [4:0] add_RD;
reg [4:0] add_RD_GC;
reg [4:0] add_RD_GCwc;
wire [4:0] n_add_RD;
reg [4:0] add_RD_WS; // synced to write clk
reg srst_w;
reg isrst_w;
reg srst_r;
reg isrst_r;
//------------------------------------------
//----- memory -----------------------------
//------------------------------------------
always @(posedge clk_WR) begin
if(((WR == 1'b1) && (ifull == 1'b0))) begin
//ram_mem(to_integer(unsigned(add_WR(3 downto 0)))) <= D;
end
end
//Q <= ram_mem(to_integer(unsigned(add_RD(3 downto 0))));
//---------------------------------------
//--- Write address counter -------------
//---------------------------------------
assign add_WR_CE = (ifull == 1'b1) ? 1'b0 : (WR == 1'b0) ? 1'b0 : 1'b1;
assign n_add_WR = (add_WR) + 4'h1;
always @(posedge clk_WR, posedge rst) begin
if((rst == 1'b1)) begin
add_WR <= {5{1'b0}};
add_RD_WS <= 5'b11000;
add_WR_GC <= {5{1'b0}};
end else begin
add_RD_WS <= add_RD_GCwc;
if((srst_w == 1'b1)) begin
add_WR <= {5{1'b0}};
add_WR_GC <= {5{1'b0}};
end
else if((add_WR_CE == 1'b1)) begin
add_WR <= n_add_WR;
add_WR_GC[0] <= n_add_WR[0] ^ n_add_WR[1];
add_WR_GC[1] <= n_add_WR[1] ^ n_add_WR[2];
add_WR_GC[2] <= n_add_WR[2] ^ n_add_WR[3];
add_WR_GC[3] <= n_add_WR[3] ^ n_add_WR[4];
add_WR_GC[4] <= n_add_WR[4];
end
else begin
add_WR <= add_WR;
add_WR_GC <= add_WR_GC;
end
end
end
assign full = ifull;
assign ifull = (iempty == 1'b1) ? 1'b0 : (add_RD_WS != add_WR_GC) ? 1'b0 : 1'b1;
//---------------------------------------
//--- Read address counter --------------
//---------------------------------------
assign add_RD_CE = (iempty == 1'b1) ? 1'b0 : (RD == 1'b0) ? 1'b0 : 1'b1;
assign n_add_RD = (add_RD) + 4'h1;
always @(posedge clk_RD, posedge rst) begin
if((rst == 1'b1)) begin
add_RD <= {5{1'b0}};
add_WR_RS <= {5{1'b0}};
add_RD_GC <= {5{1'b0}};
add_RD_GCwc <= 5'b11000;
end else begin
add_WR_RS <= add_WR_GC;
if((srst_r == 1'b1)) begin
add_RD <= {5{1'b0}};
add_RD_GC <= {5{1'b0}};
add_RD_GCwc <= 5'b11000;
end
else if((add_RD_CE == 1'b1)) begin
add_RD <= n_add_RD;
add_RD_GC[0] <= n_add_RD[0] ^ n_add_RD[1];
add_RD_GC[1] <= n_add_RD[1] ^ n_add_RD[2];
add_RD_GC[2] <= n_add_RD[2] ^ n_add_RD[3];
add_RD_GC[3] <= n_add_RD[3] ^ n_add_RD[4];
add_RD_GC[4] <= n_add_RD[4];
add_RD_GCwc[0] <= n_add_RD[0] ^ n_add_RD[1];
add_RD_GCwc[1] <= n_add_RD[1] ^ n_add_RD[2];
add_RD_GCwc[2] <= n_add_RD[2] ^ n_add_RD[3];
add_RD_GCwc[3] <= n_add_RD[3] ^ ( ~n_add_RD[4]);
add_RD_GCwc[4] <= ~n_add_RD[4];
end
else begin
add_RD <= add_RD;
add_RD_GC <= add_RD_GC;
add_RD_GCwc <= add_RD_GCwc;
end
end
end
assign empty = iempty;
assign iempty = (add_WR_RS == add_RD_GC) ? 1'b1 : 1'b0;
//--------------------------------
//- sync rest stuff --------------
//- srst is sync with clk_WR -----
//- srst_r is sync with clk_RD ---
//--------------------------------
always @(posedge clk_WR, posedge rst) begin
if((rst == 1'b1)) begin
srst_w <= 1'b0;
isrst_r <= 1'b0;
end else begin
isrst_r <= srst_r;
if((srst == 1'b1)) begin
srst_w <= 1'b1;
end
else if((isrst_r == 1'b1)) begin
srst_w <= 1'b0;
end
end
end
always @(posedge clk_RD, posedge rst) begin
if((rst == 1'b1)) begin
srst_r <= 1'b0;
isrst_w <= 1'b0;
end else begin
isrst_w <= srst_w;
if((isrst_w == 1'b1)) begin
srst_r <= 1'b1;
end
else begin
srst_r <= 1'b0;
end
end
end
endmodule
|