add DDR2 controller, generated via MIG
[fleet.git] / src / edu / berkeley / fleet / fpga / ddr2 / ddr2_usr_wr.v
1 //*****************************************************************************
2 // DISCLAIMER OF LIABILITY
3 // 
4 // This text/file contains proprietary, confidential
5 // information of Xilinx, Inc., is distributed under license
6 // from Xilinx, Inc., and may be used, copied and/or
7 // disclosed only pursuant to the terms of a valid license
8 // agreement with Xilinx, Inc. Xilinx hereby grants you a 
9 // license to use this text/file solely for design, simulation, 
10 // implementation and creation of design files limited 
11 // to Xilinx devices or technologies. Use with non-Xilinx 
12 // devices or technologies is expressly prohibited and 
13 // immediately terminates your license unless covered by
14 // a separate agreement.
15 //
16 // Xilinx is providing this design, code, or information 
17 // "as-is" solely for use in developing programs and 
18 // solutions for Xilinx devices, with no obligation on the 
19 // part of Xilinx to provide support. By providing this design, 
20 // code, or information as one possible implementation of 
21 // this feature, application or standard, Xilinx is making no 
22 // representation that this implementation is free from any 
23 // claims of infringement. You are responsible for 
24 // obtaining any rights you may require for your implementation. 
25 // Xilinx expressly disclaims any warranty whatsoever with 
26 // respect to the adequacy of the implementation, including 
27 // but not limited to any warranties or representations that this
28 // implementation is free from claims of infringement, implied 
29 // warranties of merchantability or fitness for a particular 
30 // purpose.
31 //
32 // Xilinx products are not intended for use in life support
33 // appliances, devices, or systems. Use in such applications is
34 // expressly prohibited.
35 //
36 // Any modifications that are made to the Source Code are 
37 // done at the users sole risk and will be unsupported.
38 //
39 // Copyright (c) 2006-2007 Xilinx, Inc. All rights reserved.
40 //
41 // This copyright and support notice must be retained as part 
42 // of this text at all times. 
43 //*****************************************************************************
44 //   ____  ____
45 //  /   /\/   /
46 // /___/  \  /    Vendor: Xilinx
47 // \   \   \/     Version: 2.3
48 //  \   \         Application: MIG
49 //  /   /         Filename: ddr2_usr_wr.v
50 // /___/   /\     Date Last Modified: $Date: 2008/05/08 15:20:47 $
51 // \   \  /  \    Date Created: Mon Aug 28 2006
52 //  \___\/\___\
53 //
54 //Device: Virtex-5
55 //Design Name: DDR/DDR2
56 //Purpose:
57 //   This module instantiates the modules containing internal FIFOs
58 //Reference:
59 //Revision History:
60 //*****************************************************************************
61
62 `timescale 1ns/1ps
63
64 module ddr2_usr_wr #
65   (
66    // Following parameters are for 72-bit RDIMM design (for ML561 Reference 
67    // board design). Actual values may be different. Actual parameters values 
68    // are passed from design top module ddr2_sdram module. Please refer to
69    // the ddr2_sdram module for actual values.
70    parameter BANK_WIDTH    = 2,
71    parameter COL_WIDTH     = 10,
72    parameter CS_BITS       = 0,
73    parameter DQ_WIDTH      = 72,
74    parameter APPDATA_WIDTH = 144,
75    parameter ECC_ENABLE    = 0,
76    parameter ROW_WIDTH     = 14
77    )
78   (
79    input                         clk0,
80    input                         clk90,
81    input                         rst0,
82    // Write data FIFO interface
83    input                         app_wdf_wren,
84    input [APPDATA_WIDTH-1:0]     app_wdf_data,
85    input [(APPDATA_WIDTH/8)-1:0] app_wdf_mask_data,
86    input                         wdf_rden,
87    output                        app_wdf_afull,
88    output [(2*DQ_WIDTH)-1:0]     wdf_data,
89    output [((2*DQ_WIDTH)/8)-1:0] wdf_mask_data
90    );
91
92   // determine number of FIFO72's to use based on data width
93   // round up to next integer value when determining WDF_FIFO_NUM
94   localparam WDF_FIFO_NUM = (ECC_ENABLE) ? (APPDATA_WIDTH+63)/64 :
95              ((2*DQ_WIDTH)+63)/64;
96   // MASK_WIDTH = number of bytes in data bus
97   localparam MASK_WIDTH = DQ_WIDTH/8;
98
99   wire [WDF_FIFO_NUM-1:0]      i_wdf_afull;
100   wire [DQ_WIDTH-1:0]          i_wdf_data_fall_in;
101   wire [DQ_WIDTH-1:0]          i_wdf_data_fall_out;
102   wire [(64*WDF_FIFO_NUM)-1:0] i_wdf_data_in;
103   wire [(64*WDF_FIFO_NUM)-1:0] i_wdf_data_out;
104   wire [DQ_WIDTH-1:0]          i_wdf_data_rise_in;
105   wire [DQ_WIDTH-1:0]          i_wdf_data_rise_out;
106   wire [MASK_WIDTH-1:0]        i_wdf_mask_data_fall_in;
107   wire [MASK_WIDTH-1:0]        i_wdf_mask_data_fall_out;
108   wire [(8*WDF_FIFO_NUM)-1:0]  i_wdf_mask_data_in;
109   wire [(8*WDF_FIFO_NUM)-1:0]  i_wdf_mask_data_out;
110   wire [MASK_WIDTH-1:0]        i_wdf_mask_data_rise_in;
111   wire [MASK_WIDTH-1:0]        i_wdf_mask_data_rise_out;
112   reg                          rst_r;
113
114   // ECC signals
115   wire [(2*DQ_WIDTH)-1:0]      i_wdf_data_out_ecc;
116   wire [((2*DQ_WIDTH)/8)-1:0]  i_wdf_mask_data_out_ecc;
117   wire [63:0]                  i_wdf_mask_data_out_ecc_wire;
118   wire [((2*DQ_WIDTH)/8)-1:0]  mask_data_in_ecc;
119   wire [63:0]                  mask_data_in_ecc_wire;
120
121   //***************************************************************************
122
123   assign app_wdf_afull = i_wdf_afull[0];
124
125   always @(posedge clk0 )
126       rst_r <= rst0;
127
128   genvar wdf_di_i;
129   genvar wdf_do_i;
130   genvar mask_i;
131   genvar wdf_i;
132   generate
133     if(ECC_ENABLE) begin    // ECC code
134
135       assign wdf_data = i_wdf_data_out_ecc;
136
137       // the byte 9 dm is always held to 0
138       assign wdf_mask_data = i_wdf_mask_data_out_ecc;
139
140
141
142       // generate for write data fifo .
143       for (wdf_i = 0; wdf_i < WDF_FIFO_NUM; wdf_i = wdf_i + 1) begin: gen_wdf
144
145         FIFO36_72  #
146           (
147            .ALMOST_EMPTY_OFFSET     (9'h007),
148            .ALMOST_FULL_OFFSET      (9'h00F),
149            .DO_REG                  (1),          // extra CC output delay
150            .EN_ECC_WRITE            ("TRUE"),
151            .EN_ECC_READ             ("FALSE"),
152            .EN_SYN                  ("FALSE"),
153            .FIRST_WORD_FALL_THROUGH ("FALSE")
154            )
155           u_wdf_ecc
156             (
157              .ALMOSTEMPTY (),
158              .ALMOSTFULL  (i_wdf_afull[wdf_i]),
159              .DBITERR     (),
160              .DO          (i_wdf_data_out_ecc[((64*(wdf_i+1))+(wdf_i *8))-1:
161                                               (64*wdf_i)+(wdf_i *8)]),
162              .DOP         (i_wdf_data_out_ecc[(72*(wdf_i+1))-1:
163                                               (64*(wdf_i+1))+ (8*wdf_i) ]),
164              .ECCPARITY   (),
165              .EMPTY       (),
166              .FULL        (),
167              .RDCOUNT     (),
168              .RDERR       (),
169              .SBITERR     (),
170              .WRCOUNT     (),
171              .WRERR       (),
172              .DI          (app_wdf_data[(64*(wdf_i+1))-1:
173                                         (64*wdf_i)]),
174              .DIP         (),
175              .RDCLK       (clk90),
176              .RDEN        (wdf_rden),
177              .RST         (rst_r),          // or can use rst0
178              .WRCLK       (clk0),
179              .WREN        (app_wdf_wren)
180              );
181       end
182
183       // remapping the mask data. The mask data from user i/f does not have
184       // the mask for the ECC byte. Assigning 0 to the ECC mask byte.
185       for (mask_i = 0; mask_i < (DQ_WIDTH)/36;
186            mask_i = mask_i +1) begin: gen_mask
187         assign mask_data_in_ecc[((8*(mask_i+1))+ mask_i)-1:((8*mask_i)+mask_i)]
188                  = app_wdf_mask_data[(8*(mask_i+1))-1:8*(mask_i)] ;
189         assign mask_data_in_ecc[((8*(mask_i+1))+mask_i)] = 1'd0;
190       end
191
192       // assign ecc bits to temp variables to avoid
193       // sim warnings. Not all the 64 bits of the fifo
194       // are used in ECC mode.
195        assign  mask_data_in_ecc_wire[((2*DQ_WIDTH)/8)-1:0] = mask_data_in_ecc;
196        assign  mask_data_in_ecc_wire[63:((2*DQ_WIDTH)/8)]  =
197               {(64-((2*DQ_WIDTH)/8)){1'b0}};
198        assign i_wdf_mask_data_out_ecc =
199                i_wdf_mask_data_out_ecc_wire[((2*DQ_WIDTH)/8)-1:0];
200
201
202       FIFO36_72  #
203         (
204          .ALMOST_EMPTY_OFFSET     (9'h007),
205          .ALMOST_FULL_OFFSET      (9'h00F),
206          .DO_REG                  (1),          // extra CC output delay
207          .EN_ECC_WRITE            ("TRUE"),
208          .EN_ECC_READ             ("FALSE"),
209          .EN_SYN                  ("FALSE"),
210          .FIRST_WORD_FALL_THROUGH ("FALSE")
211          )
212         u_wdf_ecc_mask
213           (
214            .ALMOSTEMPTY (),
215            .ALMOSTFULL  (),
216            .DBITERR     (),
217            .DO          (i_wdf_mask_data_out_ecc_wire),
218            .DOP         (),
219            .ECCPARITY   (),
220            .EMPTY       (),
221            .FULL        (),
222            .RDCOUNT     (),
223            .RDERR       (),
224            .SBITERR     (),
225            .WRCOUNT     (),
226            .WRERR       (),
227            .DI          (mask_data_in_ecc_wire),
228            .DIP         (),
229            .RDCLK       (clk90),
230            .RDEN        (wdf_rden),
231            .RST         (rst_r),          // or can use rst0
232            .WRCLK       (clk0),
233            .WREN        (app_wdf_wren)
234            );
235     end else begin
236
237       //***********************************************************************
238
239       // Define intermediate buses:
240       assign i_wdf_data_rise_in
241         = app_wdf_data[DQ_WIDTH-1:0];
242       assign i_wdf_data_fall_in
243         = app_wdf_data[(2*DQ_WIDTH)-1:DQ_WIDTH];
244       assign i_wdf_mask_data_rise_in
245         = app_wdf_mask_data[MASK_WIDTH-1:0];
246       assign i_wdf_mask_data_fall_in
247         = app_wdf_mask_data[(2*MASK_WIDTH)-1:MASK_WIDTH];
248
249       //***********************************************************************
250       // Write data FIFO Input:
251       // Arrange DQ's so that the rise data and fall data are interleaved.
252       // the data arrives at the input of the wdf fifo as {fall,rise}.
253       // It is remapped as:
254       //     {...fall[15:8],rise[15:8],fall[7:0],rise[7:0]}
255       // This is done to avoid having separate fifo's for rise and fall data
256       // and to keep rise/fall data for the same DQ's on same FIFO
257       // Data masks are interleaved in a similar manner
258       // NOTE: Initialization data from PHY_INIT module does not need to be
259       //  interleaved - it's already in the correct format - and the same
260       //  initialization pattern from PHY_INIT is sent to all write FIFOs
261       //***********************************************************************
262
263       for (wdf_di_i = 0; wdf_di_i < MASK_WIDTH;
264            wdf_di_i = wdf_di_i + 1) begin: gen_wdf_data_in
265         assign i_wdf_data_in[(16*wdf_di_i)+15:(16*wdf_di_i)]
266                  = {i_wdf_data_fall_in[(8*wdf_di_i)+7:(8*wdf_di_i)],
267                     i_wdf_data_rise_in[(8*wdf_di_i)+7:(8*wdf_di_i)]};
268         assign i_wdf_mask_data_in[(2*wdf_di_i)+1:(2*wdf_di_i)]
269                  = {i_wdf_mask_data_fall_in[wdf_di_i],
270                     i_wdf_mask_data_rise_in[wdf_di_i]};
271       end
272
273       //***********************************************************************
274       // Write data FIFO Output:
275       // FIFO DQ and mask outputs must be untangled and put in the standard
276       // format of {fall,rise}. Same goes for mask output
277       //***********************************************************************
278
279       for (wdf_do_i = 0; wdf_do_i < MASK_WIDTH;
280            wdf_do_i = wdf_do_i + 1) begin: gen_wdf_data_out
281         assign i_wdf_data_rise_out[(8*wdf_do_i)+7:(8*wdf_do_i)]
282                  = i_wdf_data_out[(16*wdf_do_i)+7:(16*wdf_do_i)];
283         assign i_wdf_data_fall_out[(8*wdf_do_i)+7:(8*wdf_do_i)]
284                  = i_wdf_data_out[(16*wdf_do_i)+15:(16*wdf_do_i)+8];
285         assign i_wdf_mask_data_rise_out[wdf_do_i]
286                  = i_wdf_mask_data_out[2*wdf_do_i];
287         assign i_wdf_mask_data_fall_out[wdf_do_i]
288                  = i_wdf_mask_data_out[(2*wdf_do_i)+1];
289       end
290
291       assign wdf_data = {i_wdf_data_fall_out,
292                          i_wdf_data_rise_out};
293
294       assign wdf_mask_data = {i_wdf_mask_data_fall_out,
295                               i_wdf_mask_data_rise_out};
296
297       //***********************************************************************
298
299       for (wdf_i = 0; wdf_i < WDF_FIFO_NUM; wdf_i = wdf_i + 1) begin: gen_wdf
300
301         FIFO36_72  #
302           (
303            .ALMOST_EMPTY_OFFSET     (9'h007),
304            .ALMOST_FULL_OFFSET      (9'h00F),
305            .DO_REG                  (1),          // extra CC output delay
306            .EN_ECC_WRITE            ("FALSE"),
307            .EN_ECC_READ             ("FALSE"),
308            .EN_SYN                  ("FALSE"),
309            .FIRST_WORD_FALL_THROUGH ("FALSE")
310            )
311           u_wdf
312             (
313              .ALMOSTEMPTY (),
314              .ALMOSTFULL  (i_wdf_afull[wdf_i]),
315              .DBITERR     (),
316              .DO          (i_wdf_data_out[(64*(wdf_i+1))-1:64*wdf_i]),
317              .DOP         (i_wdf_mask_data_out[(8*(wdf_i+1))-1:8*wdf_i]),
318              .ECCPARITY   (),
319              .EMPTY       (),
320              .FULL        (),
321              .RDCOUNT     (),
322              .RDERR       (),
323              .SBITERR     (),
324              .WRCOUNT     (),
325              .WRERR       (),
326              .DI          (i_wdf_data_in[(64*(wdf_i+1))-1:64*wdf_i]),
327              .DIP         (i_wdf_mask_data_in[(8*(wdf_i+1))-1:8*wdf_i]),
328              .RDCLK       (clk90),
329              .RDEN        (wdf_rden),
330              .RST         (rst_r),          // or can use rst0
331              .WRCLK       (clk0),
332              .WREN        (app_wdf_wren)
333              );
334       end
335     end
336   endgenerate
337
338 endmodule