6e72fed59448de6f38264752f974736eafe63837
[fleet.git] / src / edu / berkeley / fleet / fpga / greg / 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 user�s 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 APPDATA_BURST_LEN = 2,
76    parameter APPDATA_BURST_BITS = 1,
77    parameter ECC_ENABLE    = 0,
78    parameter ROW_WIDTH     = 14
79    )
80   (
81    input                         clk0,                  //write buffer phy clock
82    input                         clk90,
83    input                         rst0,
84    // start of changes xtan & gdgib
85    input                         wb_clk,                //write buffer user clock
86    input                         wb_rst,                //write buffer user reset
87    //end of changes
88    // Write data FIFO interface
89    input                         app_wdf_wren,
90    input [APPDATA_WIDTH-1:0]     app_wdf_data,
91    input [(APPDATA_WIDTH/8)-1:0] app_wdf_mask_data,
92    input                         wdf_rden,
93    output                        app_wdf_afull,
94    output [(2*DQ_WIDTH)-1:0]     wdf_data,
95    output [((2*DQ_WIDTH)/8)-1:0] wdf_mask_data,
96    output                                                       wr_fifo_clear,
97    input                                                        wr_fifo_burst
98    );
99
100   // determine number of FIFO72's to use based on data width
101   // round up to next integer value when determining WDF_FIFO_NUM
102   localparam WDF_FIFO_NUM = (ECC_ENABLE) ? (APPDATA_WIDTH+63)/64 :
103              ((2*DQ_WIDTH)+63)/64;
104   // MASK_WIDTH = number of bytes in data bus
105   localparam MASK_WIDTH = DQ_WIDTH/8;
106
107   wire [WDF_FIFO_NUM-1:0]      i_wdf_afull;
108   wire [DQ_WIDTH-1:0]          i_wdf_data_fall_in;
109   wire [DQ_WIDTH-1:0]          i_wdf_data_fall_out;
110   wire [(64*WDF_FIFO_NUM)-1:0] i_wdf_data_in;
111   wire [(64*WDF_FIFO_NUM)-1:0] i_wdf_data_out;
112   wire [DQ_WIDTH-1:0]          i_wdf_data_rise_in;
113   wire [DQ_WIDTH-1:0]          i_wdf_data_rise_out;
114   wire [MASK_WIDTH-1:0]        i_wdf_mask_data_fall_in;
115   wire [MASK_WIDTH-1:0]        i_wdf_mask_data_fall_out;
116   wire [(8*WDF_FIFO_NUM)-1:0]  i_wdf_mask_data_in;
117   wire [(8*WDF_FIFO_NUM)-1:0]  i_wdf_mask_data_out;
118   wire [MASK_WIDTH-1:0]        i_wdf_mask_data_rise_in;
119   wire [MASK_WIDTH-1:0]        i_wdf_mask_data_rise_out;
120   reg                          rst_r;
121
122   // ECC signals
123   wire [(2*DQ_WIDTH)-1:0]      i_wdf_data_out_ecc;
124   wire [((2*DQ_WIDTH)/8)-1:0]  i_wdf_mask_data_out_ecc;
125   wire [63:0]                  i_wdf_mask_data_out_ecc_wire;
126   wire [((2*DQ_WIDTH)/8)-1:0]  mask_data_in_ecc;
127   wire [63:0]                  mask_data_in_ecc_wire;
128   
129   //***************************************************************************
130   
131   reg   [APPDATA_BURST_BITS-1:0] blfifo_writecount;
132   wire                                                  blfifo_writeterminal;
133   wire                                                  blfifo_write;
134   wire                                                  blfifo_empty, blfifo_full;
135   
136   // Doesn't NEED to be async for now, but will need to be if we add async data writes....
137   asyncfifo_dmem_1b blfifo (
138         .din(1'b0), 
139         .rd_clk(clk0),
140         .rd_en(wr_fifo_burst),
141         .rst(rst_r | wb_rst),
142         .wr_clk(wb_clk),
143         .wr_en(blfifo_write),
144         .dout(), 
145         .empty(blfifo_empty),
146         .full(blfifo_full));
147         assign wr_fifo_clear = ~blfifo_empty;
148         
149         always @ (posedge wb_clk) begin
150                 if (wb_rst) blfifo_writecount <= 0;
151                 else if (app_wdf_wren & ~blfifo_full) begin
152                         if (blfifo_writeterminal) blfifo_writecount <= 0;
153                         else blfifo_writecount <= blfifo_writecount + 1;
154                 end
155         end
156         
157         assign  blfifo_writeterminal = (blfifo_writecount == (APPDATA_BURST_LEN - 1));
158         assign  blfifo_write = blfifo_writeterminal & app_wdf_wren & ~blfifo_full;
159
160   //***************************************************************************
161
162   assign app_wdf_afull = i_wdf_afull[0] | blfifo_full;
163
164   always @(posedge clk0 )
165       rst_r <= rst0;
166
167   genvar wdf_di_i;
168   genvar wdf_do_i;
169   genvar mask_i;
170   genvar wdf_i;
171   generate
172     if(ECC_ENABLE) begin    // ECC code
173
174       assign wdf_data = i_wdf_data_out_ecc;
175
176       // the byte 9 dm is always held to 0
177       assign wdf_mask_data = i_wdf_mask_data_out_ecc;
178
179
180
181       // generate for write data fifo .
182       for (wdf_i = 0; wdf_i < WDF_FIFO_NUM; wdf_i = wdf_i + 1) begin: gen_wdf
183
184         FIFO36_72  #
185           (
186            .ALMOST_EMPTY_OFFSET     (9'h007),
187            .ALMOST_FULL_OFFSET      (9'h00F),
188            .DO_REG                  (1),          // extra CC output delay
189            .EN_ECC_WRITE            ("TRUE"),
190            .EN_ECC_READ             ("FALSE"),
191            .EN_SYN                  ("FALSE"),
192            .FIRST_WORD_FALL_THROUGH ("FALSE")
193            )
194           u_wdf_ecc
195             (
196              .ALMOSTEMPTY (),
197              .ALMOSTFULL  (i_wdf_afull[wdf_i]),
198              .DBITERR     (),
199              .DO          (i_wdf_data_out_ecc[((64*(wdf_i+1))+(wdf_i *8))-1:
200                                               (64*wdf_i)+(wdf_i *8)]),
201              .DOP         (i_wdf_data_out_ecc[(72*(wdf_i+1))-1:
202                                               (64*(wdf_i+1))+ (8*wdf_i) ]),
203              .ECCPARITY   (),
204              .EMPTY       (),
205              .FULL        (),
206              .RDCOUNT     (),
207              .RDERR       (),
208              .SBITERR     (),
209              .WRCOUNT     (),
210              .WRERR       (),
211              .DI          (app_wdf_data[(64*(wdf_i+1))-1:
212                                         (64*wdf_i)]),
213              .DIP         (),
214              .RDCLK       (clk90),
215              .RDEN        (wdf_rden),
216              .RST         (rst_r | wb_rst),          // or can use rst0
217              .WRCLK       (wb_clk),         // xtan: clk0 -> wb_clk
218              .WREN        (app_wdf_wren & ~blfifo_full)
219              );
220       end
221
222       // remapping the mask data. The mask data from user i/f does not have
223       // the mask for the ECC byte. Assigning 0 to the ECC mask byte.
224       for (mask_i = 0; mask_i < (DQ_WIDTH)/36;
225            mask_i = mask_i +1) begin: gen_mask
226         assign mask_data_in_ecc[((8*(mask_i+1))+ mask_i)-1:((8*mask_i)+mask_i)]
227                  = app_wdf_mask_data[(8*(mask_i+1))-1:8*(mask_i)] ;
228         assign mask_data_in_ecc[((8*(mask_i+1))+mask_i)] = 1'd0;
229       end
230
231       // assign ecc bits to temp variables to avoid
232       // sim warnings. Not all the 64 bits of the fifo
233       // are used in ECC mode.
234        assign  mask_data_in_ecc_wire[((2*DQ_WIDTH)/8)-1:0] = mask_data_in_ecc;
235        assign  mask_data_in_ecc_wire[63:((2*DQ_WIDTH)/8)]  =
236               {(64-((2*DQ_WIDTH)/8)){1'b0}};
237        assign i_wdf_mask_data_out_ecc =
238                i_wdf_mask_data_out_ecc_wire[((2*DQ_WIDTH)/8)-1:0];
239
240
241       FIFO36_72  #
242         (
243          .ALMOST_EMPTY_OFFSET     (9'h007),
244          .ALMOST_FULL_OFFSET      (9'h00F),
245          .DO_REG                  (1),          // extra CC output delay
246          .EN_ECC_WRITE            ("TRUE"),
247          .EN_ECC_READ             ("FALSE"),
248          .EN_SYN                  ("FALSE"),
249          .FIRST_WORD_FALL_THROUGH ("FALSE")
250          )
251         u_wdf_ecc_mask
252           (
253            .ALMOSTEMPTY (),
254            .ALMOSTFULL  (),
255            .DBITERR     (),
256            .DO          (i_wdf_mask_data_out_ecc_wire),
257            .DOP         (),
258            .ECCPARITY   (),
259            .EMPTY       (),
260            .FULL        (),
261            .RDCOUNT     (),
262            .RDERR       (),
263            .SBITERR     (),
264            .WRCOUNT     (),
265            .WRERR       (),
266            .DI          (mask_data_in_ecc_wire),
267            .DIP         (),
268            .RDCLK       (clk90),
269            .RDEN        (wdf_rden),
270            .RST         (rst_r | wb_rst),          // or can use rst0
271            .WRCLK       (wb_clk),         // xtan: clk0->wb_clk
272            .WREN        (app_wdf_wren & ~blfifo_full)
273            );
274     end else begin
275
276       //***********************************************************************
277
278       // Define intermediate buses:
279       assign i_wdf_data_rise_in
280         = app_wdf_data[DQ_WIDTH-1:0];
281       assign i_wdf_data_fall_in
282         = app_wdf_data[(2*DQ_WIDTH)-1:DQ_WIDTH];
283       assign i_wdf_mask_data_rise_in
284         = app_wdf_mask_data[MASK_WIDTH-1:0];
285       assign i_wdf_mask_data_fall_in
286         = app_wdf_mask_data[(2*MASK_WIDTH)-1:MASK_WIDTH];
287
288       //***********************************************************************
289       // Write data FIFO Input:
290       // Arrange DQ's so that the rise data and fall data are interleaved.
291       // the data arrives at the input of the wdf fifo as {fall,rise}.
292       // It is remapped as:
293       //     {...fall[15:8],rise[15:8],fall[7:0],rise[7:0]}
294       // This is done to avoid having separate fifo's for rise and fall data
295       // and to keep rise/fall data for the same DQ's on same FIFO
296       // Data masks are interleaved in a similar manner
297       // NOTE: Initialization data from PHY_INIT module does not need to be
298       //  interleaved - it's already in the correct format - and the same
299       //  initialization pattern from PHY_INIT is sent to all write FIFOs
300       //***********************************************************************
301
302       for (wdf_di_i = 0; wdf_di_i < MASK_WIDTH;
303            wdf_di_i = wdf_di_i + 1) begin: gen_wdf_data_in
304         assign i_wdf_data_in[(16*wdf_di_i)+15:(16*wdf_di_i)]
305                  = {i_wdf_data_fall_in[(8*wdf_di_i)+7:(8*wdf_di_i)],
306                     i_wdf_data_rise_in[(8*wdf_di_i)+7:(8*wdf_di_i)]};
307         assign i_wdf_mask_data_in[(2*wdf_di_i)+1:(2*wdf_di_i)]
308                  = {i_wdf_mask_data_fall_in[wdf_di_i],
309                     i_wdf_mask_data_rise_in[wdf_di_i]};
310       end
311
312       //***********************************************************************
313       // Write data FIFO Output:
314       // FIFO DQ and mask outputs must be untangled and put in the standard
315       // format of {fall,rise}. Same goes for mask output
316       //***********************************************************************
317
318       for (wdf_do_i = 0; wdf_do_i < MASK_WIDTH;
319            wdf_do_i = wdf_do_i + 1) begin: gen_wdf_data_out
320         assign i_wdf_data_rise_out[(8*wdf_do_i)+7:(8*wdf_do_i)]
321                  = i_wdf_data_out[(16*wdf_do_i)+7:(16*wdf_do_i)];
322         assign i_wdf_data_fall_out[(8*wdf_do_i)+7:(8*wdf_do_i)]
323                  = i_wdf_data_out[(16*wdf_do_i)+15:(16*wdf_do_i)+8];
324         assign i_wdf_mask_data_rise_out[wdf_do_i]
325                  = i_wdf_mask_data_out[2*wdf_do_i];
326         assign i_wdf_mask_data_fall_out[wdf_do_i]
327                  = i_wdf_mask_data_out[(2*wdf_do_i)+1];
328       end
329
330       assign wdf_data = {i_wdf_data_fall_out,
331                          i_wdf_data_rise_out};
332
333       assign wdf_mask_data = {i_wdf_mask_data_fall_out,
334                               i_wdf_mask_data_rise_out};
335
336       //***********************************************************************
337
338       for (wdf_i = 0; wdf_i < WDF_FIFO_NUM; wdf_i = wdf_i + 1) begin: gen_wdf
339
340         FIFO36_72  #
341           (
342            .ALMOST_EMPTY_OFFSET     (9'h007),
343            .ALMOST_FULL_OFFSET      (9'h00F),
344            .DO_REG                  (1),          // extra CC output delay
345            .EN_ECC_WRITE            ("FALSE"),
346            .EN_ECC_READ             ("FALSE"),
347            .EN_SYN                  ("FALSE"),
348            .FIRST_WORD_FALL_THROUGH ("FALSE")
349            )
350           u_wdf
351             (
352              .ALMOSTEMPTY (),
353              .ALMOSTFULL  (i_wdf_afull[wdf_i]),
354              .DBITERR     (),
355              .DO          (i_wdf_data_out[(64*(wdf_i+1))-1:64*wdf_i]),
356              .DOP         (i_wdf_mask_data_out[(8*(wdf_i+1))-1:8*wdf_i]),
357              .ECCPARITY   (),
358              .EMPTY       (),
359              .FULL        (),
360              .RDCOUNT     (),
361              .RDERR       (),
362              .SBITERR     (),
363              .WRCOUNT     (),
364              .WRERR       (),
365              .DI          (i_wdf_data_in[(64*(wdf_i+1))-1:64*wdf_i]),
366              .DIP         (i_wdf_mask_data_in[(8*(wdf_i+1))-1:8*wdf_i]),
367              .RDCLK       (clk90),
368              .RDEN        (wdf_rden),
369              .RST         (rst_r | wb_rst),          // or can use rst0
370              .WRCLK       (wb_clk),         // xtan: clk0 -> wb_clk
371              .WREN        (app_wdf_wren & ~blfifo_full)
372              );
373       end
374     end
375   endgenerate
376
377 endmodule