OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [boards/] [xilinx/] [ml501/] [rtl/] [verilog/] [xilinx_ddr2/] [ddr2_ctrl.v] - Blame information for rev 751

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 412 julius
//*****************************************************************************
2
// DISCLAIMER OF LIABILITY
3
//
4
// This file contains proprietary and confidential information of
5
// Xilinx, Inc. ("Xilinx"), that is distributed under a license
6
// from Xilinx, and may be used, copied and/or disclosed only
7
// pursuant to the terms of a valid license agreement with Xilinx.
8
//
9
// XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION
10
// ("MATERIALS") "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
11
// EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING WITHOUT
12
// LIMITATION, ANY WARRANTY WITH RESPECT TO NONINFRINGEMENT,
13
// MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE. Xilinx
14
// does not warrant that functions included in the Materials will
15
// meet the requirements of Licensee, or that the operation of the
16
// Materials will be uninterrupted or error-free, or that defects
17
// in the Materials will be corrected. Furthermore, Xilinx does
18
// not warrant or make any representations regarding use, or the
19
// results of the use, of the Materials in terms of correctness,
20
// accuracy, reliability or otherwise.
21
//
22
// Xilinx products are not designed or intended to be fail-safe,
23
// or for use in any application requiring fail-safe performance,
24
// such as life-support or safety devices or systems, Class III
25
// medical devices, nuclear facilities, applications related to
26
// the deployment of airbags, or any other applications that could
27
// lead to death, personal injury or severe property or
28
// environmental damage (individually and collectively, "critical
29
// applications"). Customer assumes the sole risk and liability
30
// of any use of Xilinx products in critical applications,
31
// subject only to applicable laws and regulations governing
32
// limitations on product liability.
33
//
34
// Copyright 2006, 2007, 2008 Xilinx, Inc.
35
// All rights reserved.
36
//
37
// This disclaimer and copyright notice must be retained as part
38
// of this file at all times.
39
//*****************************************************************************
40
//   ____  ____
41
//  /   /\/   /
42
// /___/  \  /    Vendor: Xilinx
43
// \   \   \/     Version: 3.0
44
//  \   \         Application: MIG
45
//  /   /         Filename: ddr2_ctrl.v
46
// /___/   /\     Date Last Modified: $Date: 2008/12/23 14:26:00 $
47
// \   \  /  \    Date Created: Wed Aug 30 2006
48
//  \___\/\___\
49
//
50
//
51
//Device: Virtex-5
52
//Design Name: DDR/DDR2
53
//Purpose:
54
//   This module is the main control logic of the memory interface. All
55
//   commands are issued from here according to the burst, CAS Latency and the
56
//   user commands.
57
//Reference:
58
//Revision History:
59
//   Rev 1.2 - Fixed auto refresh to activate bug. KP 11-19-2007
60
//   Rev 1.3 - For Dual Rank parts support CS logic modified. KP. 05/08/08
61
//   Rev 1.4 - AUTO_REFRESH_WAIT state modified for Auto Refresh flag asserted
62
//             immediately after calibration is completed. KP. 07/28/08
63
//   Rev 1.5 - Assignment of bank_valid_r is modified to fix a bug in 
64
//             Bank Management logic. PK. 10/29/08
65
//*****************************************************************************
66
 
67
`timescale 1ns/1ps
68
 
69
module ddr2_ctrl #
70
  (
71
   // Following parameters are for 72-bit RDIMM design (for ML561 Reference
72
   // board design). Actual values may be different. Actual parameters values
73
   // are passed from design top module ddr2_mig module. Please refer to
74
   // the ddr2_mig module for actual values.
75
   parameter BANK_WIDTH    = 2,
76
   parameter COL_WIDTH     = 10,
77
   parameter CS_BITS       = 0,
78
   parameter CS_NUM        = 1,
79
   parameter ROW_WIDTH     = 14,
80
   parameter ADDITIVE_LAT  = 0,
81
   parameter BURST_LEN     = 4,
82
   parameter CAS_LAT       = 5,
83
   parameter ECC_ENABLE    = 0,
84
   parameter REG_ENABLE    = 1,
85
   parameter TREFI_NS      = 7800,
86
   parameter TRAS          = 40000,
87
   parameter TRCD          = 15000,
88
   parameter TRRD          = 10000,
89
   parameter TRFC          = 105000,
90
   parameter TRP           = 15000,
91
   parameter TRTP          = 7500,
92
   parameter TWR           = 15000,
93
   parameter TWTR          = 10000,
94
   parameter CLK_PERIOD    = 3000,
95
   parameter MULTI_BANK_EN = 1,
96
   parameter TWO_T_TIME_EN = 0,
97
   parameter DDR_TYPE      = 1
98
   )
99
  (
100
   input                   clk,
101
   input                   rst,
102
   input [2:0]             af_cmd,
103
   input [30:0]            af_addr,
104
   input                   af_empty,
105
   input                   phy_init_done,
106
   output                  ctrl_ref_flag,
107
   output                  ctrl_af_rden,
108
   output reg              ctrl_wren,
109
   output reg              ctrl_rden,
110
   output [ROW_WIDTH-1:0]  ctrl_addr,
111
   output [BANK_WIDTH-1:0] ctrl_ba,
112
   output                  ctrl_ras_n,
113
   output                  ctrl_cas_n,
114
   output                  ctrl_we_n,
115
   output [CS_NUM-1:0]     ctrl_cs_n
116
   );
117
 
118
  // input address split into various ranges
119
  localparam ROW_RANGE_START     = COL_WIDTH;
120
  localparam ROW_RANGE_END       = ROW_WIDTH + ROW_RANGE_START - 1;
121
  localparam BANK_RANGE_START    = ROW_RANGE_END + 1;
122
  localparam BANK_RANGE_END      = BANK_WIDTH + BANK_RANGE_START - 1;
123
  localparam CS_RANGE_START      = BANK_RANGE_START + BANK_WIDTH;
124
  localparam CS_RANGE_END        = CS_BITS + CS_RANGE_START - 1;
125
  // compare address (for determining bank/row hits) split into various ranges
126
  // (compare address doesn't include column bits)
127
  localparam CMP_WIDTH            = CS_BITS + BANK_WIDTH + ROW_WIDTH;
128
  localparam CMP_ROW_RANGE_START  = 0;
129
  localparam CMP_ROW_RANGE_END    = ROW_WIDTH + CMP_ROW_RANGE_START - 1;
130
  localparam CMP_BANK_RANGE_START = CMP_ROW_RANGE_END + 1;
131
  localparam CMP_BANK_RANGE_END   = BANK_WIDTH + CMP_BANK_RANGE_START - 1;
132
  localparam CMP_CS_RANGE_START   = CMP_BANK_RANGE_END + 1;
133
  localparam CMP_CS_RANGE_END     = CS_BITS + CMP_CS_RANGE_START-1;
134
 
135
  localparam BURST_LEN_DIV2      = BURST_LEN / 2;
136
  localparam OPEN_BANK_NUM       = 4;
137
  localparam CS_BITS_FIX         = (CS_BITS == 0) ? 1 : CS_BITS;
138
 
139
  // calculation counters based on clock cycle and memory parameters
140
  // TRAS: ACTIVE->PRECHARGE interval - 2
141
  localparam integer TRAS_CYC = (TRAS + CLK_PERIOD)/CLK_PERIOD;
142
  // TRCD: ACTIVE->READ/WRITE interval - 3 (for DDR2 factor in ADD_LAT)
143
  localparam integer TRRD_CYC = (TRRD + CLK_PERIOD)/CLK_PERIOD;
144
  localparam integer TRCD_CYC = (((TRCD + CLK_PERIOD)/CLK_PERIOD) >
145
                                 ADDITIVE_LAT )?
146
             ((TRCD+CLK_PERIOD)/ CLK_PERIOD) - ADDITIVE_LAT : 0;
147
  // TRFC: REFRESH->REFRESH, REFRESH->ACTIVE interval - 2
148
  localparam integer TRFC_CYC = (TRFC + CLK_PERIOD)/CLK_PERIOD;
149
  // TRP: PRECHARGE->COMMAND interval - 2
150
   // for precharge all add 1 extra clock cycle
151
  localparam integer TRP_CYC =  ((TRP + CLK_PERIOD)/CLK_PERIOD) +1;
152
  // TRTP: READ->PRECHARGE interval - 2 (Al + BL/2 + (max (TRTP, 2tck))-2
153
  localparam integer TRTP_TMP_MIN = (((TRTP + CLK_PERIOD)/CLK_PERIOD) >= 2)?
154
                                     ((TRTP + CLK_PERIOD)/CLK_PERIOD) : 2;
155
  localparam integer TRTP_CYC = TRTP_TMP_MIN + ADDITIVE_LAT
156
                                + BURST_LEN_DIV2 - 2;
157
  // TWR: WRITE->PRECHARGE interval - 2
158
  localparam integer WR_LAT = (DDR_TYPE > 0) ? CAS_LAT + ADDITIVE_LAT - 1 : 1;
159
  localparam integer TWR_CYC = ((TWR + CLK_PERIOD)/CLK_PERIOD) +
160
             WR_LAT + BURST_LEN_DIV2 ;
161
  // TWTR: WRITE->READ interval - 3 (for DDR1, TWTR = 2 clks)
162
  // DDR2 = CL-1 + BL/2 +TWTR
163
  localparam integer TWTR_TMP_MIN = (TWTR + CLK_PERIOD)/CLK_PERIOD;
164
  localparam integer TWTR_CYC = (DDR_TYPE > 0) ? (TWTR_TMP_MIN + (CAS_LAT -1)
165
                                 + BURST_LEN_DIV2 ): 2;
166
 
167
  //  TRTW: READ->WRITE interval - 3
168
  //  DDR1: CL + (BL/2)
169
  //  DDR2: (BL/2) + 2. Two more clocks are added to
170
  //  the DDR2 counter to account for the delay in
171
  //  arrival of the DQS during reads (pcb trace + buffer
172
  //  delays + memory parameters).
173
  localparam TRTW_CYC = (DDR_TYPE > 0) ? BURST_LEN_DIV2 + 4 :
174
             (CAS_LAT == 25) ? 2 + BURST_LEN_DIV2 : CAS_LAT + BURST_LEN_DIV2;
175
 
176
  localparam integer CAS_LAT_RD = (CAS_LAT == 25) ? 2 : CAS_LAT;
177
 
178
  // Make sure all values >= 0 (some may be = 0)
179
  localparam TRAS_COUNT = (TRAS_CYC > 0) ? TRAS_CYC : 0;
180
  localparam TRCD_COUNT = (TRCD_CYC > 0) ? TRCD_CYC : 0;
181
  localparam TRRD_COUNT = (TRRD_CYC > 0) ? TRRD_CYC : 0;
182
  localparam TRFC_COUNT = (TRFC_CYC > 0) ? TRFC_CYC : 0;
183
  localparam TRP_COUNT  = (TRP_CYC > 0)  ? TRP_CYC  : 0;
184
  localparam TRTP_COUNT = (TRTP_CYC > 0) ? TRTP_CYC : 0;
185
  localparam TWR_COUNT  = (TWR_CYC > 0)  ? TWR_CYC  : 0;
186
  localparam TWTR_COUNT = (TWTR_CYC > 0) ? TWTR_CYC : 0;
187
  localparam TRTW_COUNT = (TRTW_CYC > 0) ? TRTW_CYC : 0;
188
 
189
  // Auto refresh interval
190
  localparam TREFI_COUNT = ((TREFI_NS * 1000)/CLK_PERIOD) - 1;
191
 
192
  // memory controller states
193
  localparam   CTRL_IDLE                =     5'h00;
194
  localparam   CTRL_PRECHARGE           =     5'h01;
195
  localparam   CTRL_PRECHARGE_WAIT      =     5'h02;
196
  localparam   CTRL_AUTO_REFRESH        =     5'h03;
197
  localparam   CTRL_AUTO_REFRESH_WAIT   =     5'h04;
198
  localparam   CTRL_ACTIVE              =     5'h05;
199
  localparam   CTRL_ACTIVE_WAIT         =     5'h06;
200
  localparam   CTRL_BURST_READ          =     5'h07;
201
  localparam   CTRL_READ_WAIT           =     5'h08;
202
  localparam   CTRL_BURST_WRITE         =     5'h09;
203
  localparam   CTRL_WRITE_WAIT          =     5'h0A;
204
  localparam   CTRL_PRECHARGE_WAIT1     =     5'h0B;
205
 
206
 
207
  reg [CMP_WIDTH-1:0]                      act_addr_r;
208
  wire [30:0]                              af_addr_r;
209
  reg [30:0]                               af_addr_r1;
210
  reg [30:0]                               af_addr_r2;
211
  reg [30:0]                               af_addr_r3;
212
  wire [2:0]                               af_cmd_r;
213
  reg [2:0]                                af_cmd_r1;
214
  reg [2:0]                                af_cmd_r2;
215
  reg                                      af_valid_r;
216
  reg                                      af_valid_r1;
217
  reg                                      af_valid_r2;
218
  reg [CS_BITS_FIX :0]                     auto_cnt_r;
219
  reg                                      auto_ref_r;
220
  reg [(OPEN_BANK_NUM*CMP_WIDTH)-1:0]      bank_cmp_addr_r;
221
  reg [OPEN_BANK_NUM-1:0]                  bank_hit;
222
  reg [OPEN_BANK_NUM-1:0]                  bank_hit_r;
223
  reg [OPEN_BANK_NUM-1:0]                  bank_hit_r1;
224
  reg [OPEN_BANK_NUM-1:0]                  bank_valid_r;
225
  reg                                      bank_conflict_r;
226
  reg                                      conflict_resolved_r;
227
  reg                                      ctrl_af_rden_r;
228
  reg                                      conflict_detect_r;
229
  wire                                     conflict_detect;
230
  reg                                      cs_change_r;
231
  reg                                      cs_change_sticky_r;
232
  reg [ROW_WIDTH-1:0]                      ddr_addr_r;
233
  wire [ROW_WIDTH-1:0]                     ddr_addr_col;
234
  wire [ROW_WIDTH-1:0]                     ddr_addr_row;
235
  reg [BANK_WIDTH-1:0]                     ddr_ba_r;
236
  reg                                      ddr_cas_n_r;
237
  reg [CS_NUM-1:0]                         ddr_cs_n_r;
238
  reg                                      ddr_ras_n_r;
239
  reg                                      ddr_we_n_r;
240
  reg [4:0]                                next_state;
241
  reg                                      no_precharge_wait_r;
242
  reg                                      no_precharge_r;
243
  reg                                      no_precharge_r1;
244
  reg                                      phy_init_done_r;
245
  reg [4:0]                                precharge_ok_cnt_r;
246
  reg                                      precharge_ok_r;
247
  reg [4:0]                                ras_cnt_r;
248
  reg [3:0]                                rcd_cnt_r;
249
  reg                                      rcd_cnt_ok_r;
250
  reg [2:0]                                rdburst_cnt_r;
251
  reg                                      rdburst_ok_r;
252
  reg                                      rdburst_rden_ok_r;
253
  reg                                      rd_af_flag_r;
254
  wire                                     rd_flag;
255
  reg                                      rd_flag_r;
256
  reg [4:0]                                rd_to_wr_cnt_r;
257
  reg                                      rd_to_wr_ok_r;
258
  reg                                      ref_flag_r;
259
  reg [11:0]                               refi_cnt_r;
260
  reg                                      refi_cnt_ok_r;
261
  reg                                      rst_r
262
                                           /* synthesis syn_preserve = 1 */;
263
  reg                                      rst_r1
264
                                           /* synthesis syn_maxfan = 10 */;
265
  reg [7:0]                                rfc_cnt_r;
266
  reg                                      rfc_ok_r;
267
  reg [3:0]                                row_miss;
268
  reg [3:0]                                row_conflict_r;
269
  reg [3:0]                                rp_cnt_r;
270
  reg                                      rp_cnt_ok_r;
271
  reg [CMP_WIDTH-1:0]                      sb_open_add_r;
272
  reg [4:0]                                state_r;
273
  reg [4:0]                                state_r1;
274
  wire                                     sm_rden;
275
  reg                                      sm_rden_r;
276
  reg [2:0]                                trrd_cnt_r;
277
  reg                                      trrd_cnt_ok_r;
278
  reg [2:0]                                two_t_enable_r;
279
  reg [CS_NUM-1:0]                         two_t_enable_r1;
280
  reg [2:0]                                wrburst_cnt_r;
281
  reg                                      wrburst_ok_r;
282
  reg                                      wrburst_wren_ok_r;
283
  wire                                     wr_flag;
284
  reg                                      wr_flag_r;
285
  reg [4:0]                                wr_to_rd_cnt_r;
286
  reg                                      wr_to_rd_ok_r;
287
 
288
  // XST attributes for local reset "tree"
289
  // synthesis attribute shreg_extract of rst_r is "no";
290
  // synthesis attribute shreg_extract of rst_r1 is "no";
291
  // synthesis attribute equivalent_register_removal of rst_r is "no"
292
 
293
  //***************************************************************************
294
 
295
  // sm_rden is used to assert read enable to the address FIFO
296
  assign sm_rden = ((state_r == CTRL_BURST_WRITE) ||
297
                    (state_r == CTRL_BURST_READ)) ;
298
 
299
   // Assert this when approaching refresh and not in an access
300
   reg                                     ref_approaching;
301
   always @(posedge clk)
302
     ref_approaching <= (refi_cnt_r >= (TREFI_COUNT -80)) & ~af_valid_r2;
303
 
304
   reg                                     ref_just_happened;
305
   always @(posedge clk)
306
     ref_just_happened <= (refi_cnt_r <  12'h30) & ~af_valid_r2;
307
 
308
 
309
  // assert read flag to the adress FIFO
310
  assign ctrl_af_rden = (sm_rden || rd_af_flag_r) & !(ref_approaching | ref_just_happened);
311
 
312
  // local reset "tree" for controller logic only. Create this to ease timing
313
  // on reset path. Prohibit equivalent register removal on RST_R to prevent
314
  // "sharing" with other local reset trees (caution: make sure global fanout
315
  // limit is set to large enough value, otherwise SLICES may be used for
316
  // fanout control on RST_R.
317
  always @(posedge clk) begin
318
    rst_r  <= rst;
319
    rst_r1 <= rst_r;
320
  end
321
 
322
  //*****************************************************************
323
  // interpret commands from Command/Address FIFO
324
  //*****************************************************************
325
 
326
  assign wr_flag = (af_valid_r2) ? ((af_cmd_r2 == 3'b000) ? 1'b1 : 1'b0): 1'b0;
327
  assign rd_flag = (af_valid_r2) ? ((af_cmd_r2 == 3'b001) ? 1'b1 : 1'b0): 1'b0;
328
 
329
  always @(posedge clk) begin
330
    rd_flag_r <= rd_flag;
331
    wr_flag_r <= wr_flag;
332
  end
333
 
334
  //////////////////////////////////////////////////
335
  // The data from the address FIFO is fetched and
336
  // stored in two register stages. The data will be
337
  // pulled out of the second register stage whenever
338
  // the state machine can handle new data from the
339
  // address FIFO.
340
 
341
  // This flag is asserted when there is no
342
  // cmd & address in the pipe. When there is
343
  // valid cmd & addr from the address FIFO the
344
  // af_valid signals will be asserted. This flag will
345
  // be set the cycle af_valid_r is de-asserted.
346
  always @(posedge clk) begin
347
    // for simulation purposes - to force CTRL_AF_RDEN low during reset
348
    if (rst_r1)
349
      rd_af_flag_r <= 1'd0;
350 480 julius
    else if (rd_af_flag_r) // jb - probably should find a way 
351
                           // to stop this toggling all the time
352
      rd_af_flag_r <= 0;   // jb
353 412 julius
    else if((ctrl_af_rden_r) ||
354 480 julius
            (/*rd_af_flag_r &&*/ (af_valid_r || af_valid_r1)))
355
      // Fixed bug where third addresses would get lost (pulled off fifo and 
356
      // then  clobbered by other value later, thus ignored/skipped) - just 
357
      // make sure we don't get too excited and pull too many off at once  - jb
358 412 julius
         rd_af_flag_r <= 1'd0;
359
    else if (~af_valid_r1 || ~af_valid_r)
360
         rd_af_flag_r <= 1'd1;
361
 
362
  end // always @ (posedge clk)
363
 
364
 
365
 
366
  // First register stage for the cmd & add from the FIFO.
367
  // The af_valid_r signal gives the status of the data
368
  // in this stage. The af_valid_r will be asserted when there
369
  // is valid data. This register stage will be updated
370
  // 1. read to the FIFO and the FIFO not empty
371
  // 2. After write and read states
372
  // 3. The valid signal is not asserted in the last stage.
373
  always @(posedge clk) begin
374
    if (rst_r1)begin
375
      af_valid_r <= 1'd0;
376
    end else begin
377
      if (ctrl_af_rden_r || sm_rden_r || ~af_valid_r1
378
          || ~af_valid_r2)begin
379
        af_valid_r <= ctrl_af_rden_r;
380
      end
381
    end
382
  end
383
 
384
  // The output register in the FIFO is used. The addr
385
  // and command are already registered in the FIFO.
386
  assign af_addr_r = af_addr;
387
  assign af_cmd_r = af_cmd;
388
 
389
  // Second register stage for the cmd & add from the FIFO.
390
  // The af_valid_r1 signal gives the status of the data
391
  // in this stage. The af_valid_r will be asserted when there
392
  // is valid data. This register stage will be updated
393
  // 1. read to the FIFO and the FIFO not empty and there
394
  // is no valid data on this stage
395
  // 2. After write and read states
396
  // 3. The valid signal is not asserted in the last stage.
397
  always@(posedge clk) begin
398
    if (rst_r1)begin
399
      af_valid_r1 <= 1'd0;
400
      af_addr_r1 <= {31{1'bx}};
401
      af_cmd_r1 <= {3{1'bx}};
402
    end else if (~af_valid_r1 || sm_rden_r ||
403
                  ~af_valid_r2) begin
404
      af_valid_r1 <= af_valid_r;
405
      af_addr_r1 <= af_addr_r;
406
      af_cmd_r1 <= af_cmd_r;
407
    end
408
  end
409
 
410
  // The state machine uses the address and command in this
411
  // register stage. The data is fetched from the second
412
  // register stage whenever the state machine can accept new
413
  // addr. The conflict flags are also generated based on the
414
  // second register stage and updated when the new address
415
  // is loaded for the state machine.
416
  always@(posedge clk) begin
417
    if (rst_r1)begin
418
      af_valid_r2 <= 1'd0;
419
      af_addr_r2 <= {31{1'bx}};
420
      af_cmd_r2 <= {3{1'bx}};
421
      bank_hit_r <= {OPEN_BANK_NUM{1'bx}};
422
      bank_conflict_r <= 1'bx;
423
      row_conflict_r <= 4'bx;
424
    end else if(sm_rden || ~af_valid_r2)begin
425
      af_valid_r2 <= af_valid_r1;
426
      af_addr_r2 <= af_addr_r1;
427
      af_cmd_r2 <= af_cmd_r1;
428
      if(MULTI_BANK_EN)begin
429
        bank_hit_r <= bank_hit;
430
        row_conflict_r <= row_miss;
431
        bank_conflict_r <= (~(|bank_hit));
432
      end else begin
433
        bank_hit_r <= {OPEN_BANK_NUM{1'b0}};
434
        bank_conflict_r <= 1'd0;
435
        row_conflict_r[0] <= (af_addr_r1[CS_RANGE_END:ROW_RANGE_START]
436
                              != sb_open_add_r[CMP_WIDTH-1:0]);
437
      end
438
    end
439
  end // always@ (posedge clk)
440
 
441
  //detecting cs change for multi chip select case
442
  generate
443
    if(CS_NUM > 1) begin: gen_cs_change
444
       always @(posedge clk) begin
445
          if(sm_rden || ~af_valid_r2)begin
446
            cs_change_r <= af_addr_r1[CS_RANGE_END:CS_RANGE_START] !=
447
                       af_addr_r2[CS_RANGE_END:CS_RANGE_START] ;
448
            cs_change_sticky_r <=
449
             af_addr_r1[CS_RANGE_END:CS_RANGE_START] !=
450
             af_addr_r2[CS_RANGE_END:CS_RANGE_START] ;
451
          end else
452
            cs_change_r <= 1'd0;
453
       end
454
    end // block: gen_cs_change
455
    else begin: gen_cs_0
456
       always @(posedge clk) begin
457
          cs_change_r <= 1'd0;
458
          cs_change_sticky_r <= 1'd0;
459
       end
460
    end
461
 endgenerate
462
 
463
  assign conflict_detect = (MULTI_BANK_EN) ?
464
                           ((|(row_conflict_r[3:0] & bank_hit_r[3:0]))
465
                            | bank_conflict_r) & af_valid_r2 :
466
                           row_conflict_r[0] & af_valid_r2;
467
 
468
  always @(posedge clk) begin
469
    conflict_detect_r <= conflict_detect;
470
    sm_rden_r <= sm_rden;
471
    af_addr_r3 <= af_addr_r2;
472
    ctrl_af_rden_r <= ctrl_af_rden & ~af_empty;
473
  end
474
 
475
  // conflict resolved signal. When this signal is asserted
476
  // the conflict is resolved. The address to be compared
477
  // for the conflict_resolved_r will be stored in act_add_r
478
  // when the bank is opened.
479
  always @(posedge clk) begin
480
   conflict_resolved_r <= (act_addr_r ==
481
                           af_addr_r2[CS_RANGE_END:ROW_RANGE_START]);
482
    if((state_r == CTRL_ACTIVE))
483
      act_addr_r <= af_addr_r2[CS_RANGE_END:ROW_RANGE_START];
484
  end
485
 
486
  //***************************************************************************
487
  // Bank management logic
488
  // Semi-hardcoded for now for 4 banks
489
  // will keep multiple banks open if MULTI_BANK_EN is true.
490
  //***************************************************************************
491
 
492
  genvar bank_i;
493
  generate // if multiple bank option chosen
494
    if(MULTI_BANK_EN) begin: gen_multi_bank_open
495
 
496
      for (bank_i = 0; bank_i < OPEN_BANK_NUM;
497
           bank_i = bank_i + 1) begin: gen_bank_hit1
498
        // asserted if bank address match + open bank entry is valid
499
        always @(*) begin
500
          bank_hit[bank_i]
501
            = ((bank_cmp_addr_r[(CMP_WIDTH*(bank_i+1))-1:
502
                                (CMP_WIDTH*bank_i)+ROW_WIDTH] ==
503
                af_addr_r1[CS_RANGE_END:BANK_RANGE_START]) &&
504
               bank_valid_r[bank_i]);
505
          // asserted if row address match (no check for bank entry valid, rely
506
          // on this term to be used in conjunction with BANK_HIT[])
507
          row_miss[bank_i]
508
            = (bank_cmp_addr_r[(CMP_WIDTH*bank_i)+ROW_WIDTH-1:
509
                               (CMP_WIDTH*bank_i)] !=
510
               af_addr_r1[ROW_RANGE_END:ROW_RANGE_START]);
511
        end
512
      end
513
 
514
      always @(posedge clk) begin
515
        no_precharge_wait_r  <= bank_valid_r[3] & bank_conflict_r;
516
        bank_hit_r1 <= bank_hit_r;
517
      end
518
 
519
      always@(*)
520
        no_precharge_r = ~bank_valid_r[3] & bank_conflict_r;
521
 
522
      always@(posedge clk)
523
        no_precharge_r1 <= no_precharge_r;
524
 
525
 
526
      always @(posedge clk) begin
527
        // Clear all bank valid bits during AR (i.e. since all banks get
528
        // precharged during auto-refresh)
529
        if ((state_r1 == CTRL_AUTO_REFRESH)) begin
530
          bank_valid_r    <= {OPEN_BANK_NUM{1'b0}};
531
          bank_cmp_addr_r <= {(OPEN_BANK_NUM*CMP_WIDTH-1){1'b0}};
532
        end else begin
533
          if (state_r1 == CTRL_ACTIVE) begin
534
            // 00 is always going to have the latest bank and row.
535
            bank_cmp_addr_r[CMP_WIDTH-1:0]
536
              <= af_addr_r3[CS_RANGE_END:ROW_RANGE_START];
537
            // This indicates the bank was activated
538
            bank_valid_r[0] <= 1'b1;
539
 
540
            case ({bank_hit_r1[2:0]})
541
              3'b001: begin
542
                bank_cmp_addr_r[CMP_WIDTH-1:0]
543
                  <= af_addr_r3[CS_RANGE_END:ROW_RANGE_START];
544
                // This indicates the bank was activated
545
                bank_valid_r[0] <= 1'b1;
546
              end
547
              3'b010: begin //(b0->b1)
548
                bank_cmp_addr_r[(2*CMP_WIDTH)-1:CMP_WIDTH]
549
                  <= bank_cmp_addr_r[CMP_WIDTH-1:0];
550
                bank_valid_r[1] <= bank_valid_r[0];
551
              end
552
              3'b100:begin //(b0->b1, b1->b2)
553
                bank_cmp_addr_r[(2*CMP_WIDTH)-1:CMP_WIDTH]
554
                  <= bank_cmp_addr_r[CMP_WIDTH-1:0];
555
                bank_cmp_addr_r[(3*CMP_WIDTH)-1:2*CMP_WIDTH]
556
                  <= bank_cmp_addr_r[(2*CMP_WIDTH)-1:CMP_WIDTH];
557
                bank_valid_r[1] <= bank_valid_r[0];
558
                bank_valid_r[2] <= bank_valid_r[1];
559
              end
560
              default: begin //(b0->b1, b1->b2, b2->b3)
561
                bank_cmp_addr_r[(2*CMP_WIDTH)-1:CMP_WIDTH]
562
                  <= bank_cmp_addr_r[CMP_WIDTH-1:0];
563
                bank_cmp_addr_r[(3*CMP_WIDTH)-1:2*CMP_WIDTH]
564
                  <= bank_cmp_addr_r[(2*CMP_WIDTH)-1:CMP_WIDTH];
565
                bank_cmp_addr_r[(4*CMP_WIDTH)-1:3*CMP_WIDTH]
566
                  <= bank_cmp_addr_r[(3*CMP_WIDTH)-1:2*CMP_WIDTH];
567
                bank_valid_r[1] <= bank_valid_r[0];
568
                bank_valid_r[2] <= bank_valid_r[1];
569
                bank_valid_r[3] <= bank_valid_r[2];
570
              end
571
            endcase
572
          end
573
        end
574
      end
575
    end else begin: gen_single_bank_open // single bank option
576
      always @(posedge clk) begin
577
        no_precharge_r       <= 1'd0;
578
        no_precharge_r1      <= 1'd0;
579
        no_precharge_wait_r  <= 1'd0;
580
        if (rst_r1)
581
          sb_open_add_r <= {CMP_WIDTH{1'b0}};
582
        else if (state_r == CTRL_ACTIVE)
583
          sb_open_add_r <= af_addr_r2[CS_RANGE_END:ROW_RANGE_START];
584
      end
585
    end
586
  endgenerate
587
 
588
  //***************************************************************************
589
  // Timing counters
590
  //***************************************************************************
591
 
592
  //*****************************************************************
593
  // Write and read enable generation for PHY
594
  //*****************************************************************
595
 
596
  // write burst count. Counts from (BL/2 to 1).
597
  // Also logic for controller write enable.
598
  always @(posedge clk) begin
599
    if (state_r == CTRL_BURST_WRITE) begin
600
      wrburst_cnt_r <= BURST_LEN_DIV2;
601
    end else if (wrburst_cnt_r >= 3'd1)
602
      wrburst_cnt_r <= wrburst_cnt_r - 1;
603
  end // always @ (posedge clk)
604
 
605
 
606
  always @(posedge clk) begin
607
    if (rst_r1) begin
608
      ctrl_wren   <= 1'b0;
609
    end else if (state_r == CTRL_BURST_WRITE) begin
610
      ctrl_wren   <= 1'b1;
611
    end else if (wrburst_wren_ok_r)
612
      ctrl_wren   <= 1'b0;
613
  end
614
 
615
 
616
  always @(posedge clk) begin
617
    if ((state_r == CTRL_BURST_WRITE)
618
        && (BURST_LEN_DIV2 > 2))
619
      wrburst_ok_r <= 1'd0;
620
    else if ((wrburst_cnt_r <= 3'd3) ||
621
             (BURST_LEN_DIV2 <= 2))
622
      wrburst_ok_r <= 1'b1;
623
  end
624
 
625
  // flag to check when wrburst count has reached
626
  // a value of 1. This flag is used in the ctrl_wren
627
  // logic
628
  always @(posedge clk) begin
629
     if(wrburst_cnt_r == 3'd2)
630
       wrburst_wren_ok_r <=1'b1;
631
     else
632
       wrburst_wren_ok_r <= 1'b0;
633
  end
634
 
635
 
636
  // read burst count. Counts from (BL/2 to 1)
637
  always @(posedge clk) begin
638
   if (state_r == CTRL_BURST_READ) begin
639
      rdburst_cnt_r <= BURST_LEN_DIV2;
640
    end else if (rdburst_cnt_r >= 3'd1)
641
      rdburst_cnt_r <= rdburst_cnt_r - 1;
642
  end // always @ (posedge clk)
643
 
644
 
645
   always @(posedge clk) begin
646
    if (rst_r1) begin
647
      ctrl_rden   <= 1'b0;
648
    end else if (state_r == CTRL_BURST_READ) begin
649
      ctrl_rden   <= 1'b1;
650
    end else if (rdburst_rden_ok_r)
651
      ctrl_rden   <= 1'b0;
652
   end
653
 
654
  // the rd_burst_ok_r signal will be asserted one cycle later
655
  // in multi chip select cases if the back to back read is to
656
  // different chip selects. The cs_changed_sticky_r signal will
657
  // be asserted only for multi chip select cases.
658
  always @(posedge clk) begin
659
    if ((state_r == CTRL_BURST_READ)
660
        && (BURST_LEN_DIV2 > 2))
661
      rdburst_ok_r <= 1'd0;
662
    else if ((rdburst_cnt_r <=( 3'd3 - cs_change_sticky_r)) ||
663
             (BURST_LEN_DIV2 <= 2))
664
      rdburst_ok_r <= 1'b1;
665
  end
666
 
667
  // flag to check when rdburst count has reached
668
  // a value of 1. This flag is used in the ctrl_rden
669
  // logic
670
  always @(posedge clk) begin
671
     if (rdburst_cnt_r == 3'd2)
672
       rdburst_rden_ok_r <= 1'b1;
673
     else
674
       rdburst_rden_ok_r <= 1'b0;
675
  end
676
 
677
 
678
  //*****************************************************************
679
  // Various delay counters
680
  // The counters are checked for value of <= 3 to determine the
681
  // if the count values are reached during different commands.
682
  // It is checked for 3 because
683
  // 1. The counters are loaded during the state when the command
684
  //    state is reached (+1)
685
  // 2. After the <= 3 condition is reached the sm takes two cycles
686
  //    to transition to the new command state (+2)
687
  //*****************************************************************
688
 
689
  // tRP count - precharge command period
690
  always @(posedge clk) begin
691
    if (state_r == CTRL_PRECHARGE)
692
      rp_cnt_r <= TRP_COUNT;
693
    else if (rp_cnt_r != 4'd0)
694
      rp_cnt_r <= rp_cnt_r - 1;
695
  end
696
 
697
  always @(posedge clk) begin
698
    if (state_r == CTRL_PRECHARGE)
699
      rp_cnt_ok_r <= 1'd0;
700
    else if (rp_cnt_r <= 4'd3)
701
      rp_cnt_ok_r <= 1'd1;
702
  end
703
 
704
  // tRFC count - refresh-refresh, refresh-active
705
  always @(posedge clk) begin
706
    if (state_r == CTRL_AUTO_REFRESH)
707
      rfc_cnt_r <= TRFC_COUNT;
708
    else if (rfc_cnt_r != 8'd0)
709
      rfc_cnt_r <= rfc_cnt_r - 1;
710
  end
711
 
712
  always @(posedge clk) begin
713
    if (state_r == CTRL_AUTO_REFRESH)
714
      rfc_ok_r <= 1'b0;
715
    else if(rfc_cnt_r <= 8'd3)
716
      rfc_ok_r <= 1'b1;
717
  end
718
 
719
  // tRCD count - active to read/write
720
  always @(posedge clk) begin
721
    if (state_r == CTRL_ACTIVE)
722
      rcd_cnt_r <= TRCD_COUNT;
723
    else if (rcd_cnt_r != 4'd0)
724
      rcd_cnt_r <= rcd_cnt_r - 1;
725
  end
726
 
727
  always @(posedge clk) begin
728
    if ((state_r == CTRL_ACTIVE)
729
        && (TRCD_COUNT > 2))
730
      rcd_cnt_ok_r <= 1'd0;
731
    else if (rcd_cnt_r <= 4'd3)
732
      rcd_cnt_ok_r <= 1;
733
  end
734
 
735
  // tRRD count - active to active
736
  always @(posedge clk) begin
737
    if (state_r == CTRL_ACTIVE)
738
      trrd_cnt_r <= TRRD_COUNT;
739
    else if (trrd_cnt_r != 3'd0)
740
      trrd_cnt_r <= trrd_cnt_r - 1;
741
  end
742
 
743
  always @(posedge clk) begin
744
    if (state_r == CTRL_ACTIVE)
745
      trrd_cnt_ok_r <= 1'd0;
746
    else if (trrd_cnt_r <= 3'd3)
747
      trrd_cnt_ok_r <= 1;
748
  end
749
 
750
  // tRAS count - active to precharge
751
  always @(posedge clk) begin
752
    if (state_r == CTRL_ACTIVE)
753
      ras_cnt_r <= TRAS_COUNT;
754
    else if (ras_cnt_r != 5'd0)
755
      ras_cnt_r <= ras_cnt_r - 1;
756
  end
757
 
758
  // counter for write to prcharge
759
  // read to precharge and
760
  // activate to precharge
761
  // precharge_ok_cnt_r is added with trtp count,
762
  // there can be cases where the sm can go from
763
  // activate to read and the act->pre count time
764
  // would not have been satisfied. The rd->pre
765
   // time is very less. wr->pre time is almost the
766
   // same as act-> pre
767
  always @(posedge clk) begin
768
    if (state_r == CTRL_BURST_READ) begin
769
      // assign only if the cnt is < TRTP_COUNT
770
      if (precharge_ok_cnt_r < TRTP_COUNT)
771
        precharge_ok_cnt_r <= TRTP_COUNT;
772
    end else if (state_r == CTRL_BURST_WRITE)
773
      precharge_ok_cnt_r <= TWR_COUNT;
774
    else if (state_r == CTRL_ACTIVE)
775
      precharge_ok_cnt_r <= TRAS_COUNT;
776
    else if (precharge_ok_cnt_r != 5'd0)
777
      precharge_ok_cnt_r <= precharge_ok_cnt_r - 1;
778
  end
779
 
780
  always @(posedge clk) begin
781
    if ((state_r == CTRL_BURST_READ) ||
782
        (state_r == CTRL_BURST_WRITE)||
783
        (state_r == CTRL_ACTIVE))
784
      precharge_ok_r <= 1'd0;
785
    else if(precharge_ok_cnt_r <= 5'd3)
786
      precharge_ok_r <=1'd1;
787
  end
788
 
789
  // write to read counter
790
  // write to read includes : write latency + burst time + tWTR
791
  always @(posedge clk) begin
792
    if (rst_r1)
793
      wr_to_rd_cnt_r <= 5'd0;
794
    else if (state_r == CTRL_BURST_WRITE)
795
      wr_to_rd_cnt_r <= (TWTR_COUNT);
796
    else if (wr_to_rd_cnt_r != 5'd0)
797
      wr_to_rd_cnt_r <= wr_to_rd_cnt_r - 1;
798
  end
799
 
800
  always @(posedge clk) begin
801
    if (state_r == CTRL_BURST_WRITE)
802
      wr_to_rd_ok_r <= 1'd0;
803
    else if (wr_to_rd_cnt_r <= 5'd3)
804
      wr_to_rd_ok_r <= 1'd1;
805
  end
806
 
807
  // read to write counter
808
  always @(posedge clk) begin
809
    if (rst_r1)
810
      rd_to_wr_cnt_r <= 5'd0;
811
    else if (state_r == CTRL_BURST_READ)
812
      rd_to_wr_cnt_r <= (TRTW_COUNT);
813
    else if (rd_to_wr_cnt_r != 5'd0)
814
      rd_to_wr_cnt_r <= rd_to_wr_cnt_r - 1;
815
  end
816
 
817
  always @(posedge clk) begin
818
    if (state_r == CTRL_BURST_READ)
819
      rd_to_wr_ok_r <= 1'b0;
820
    else if (rd_to_wr_cnt_r <= 5'd3)
821
      rd_to_wr_ok_r <= 1'b1;
822
  end
823
 
824
  always @(posedge clk) begin
825
     if(refi_cnt_r == (TREFI_COUNT -1))
826
       refi_cnt_ok_r <= 1'b1;
827
     else
828
       refi_cnt_ok_r <= 1'b0;
829
  end
830
 
831
  // auto refresh interval counter in refresh_clk domain
832
  always @(posedge clk) begin
833
    if ((rst_r1) || (refi_cnt_ok_r))  begin
834
      refi_cnt_r <= 12'd0;
835
    end else begin
836
      refi_cnt_r <= refi_cnt_r + 1;
837
    end
838
  end // always @ (posedge clk)
839
 
840
  // auto refresh flag
841
  always @(posedge clk) begin
842
    if (refi_cnt_ok_r) begin
843
      ref_flag_r <= 1'b1;
844
    end else begin
845
      ref_flag_r <= 1'b0;
846
    end
847
  end // always @ (posedge clk)
848
 
849
  assign ctrl_ref_flag = ref_flag_r;
850
 
851
  //refresh flag detect
852
  //auto_ref high indicates auto_refresh requirement
853
  //auto_ref is held high until auto refresh command is issued.
854
  always @(posedge clk)begin
855
    if (rst_r1)
856
      auto_ref_r <= 1'b0;
857
    else if (ref_flag_r)
858
      auto_ref_r <= 1'b1;
859
    else if (state_r == CTRL_AUTO_REFRESH)
860
      auto_ref_r <= 1'b0;
861
  end
862
 
863
 
864
  // keep track of which chip selects got auto-refreshed (avoid auto-refreshing
865
  // all CS's at once to avoid current spike)
866
  always @(posedge clk)begin
867
    if (rst_r1 || (state_r1 == CTRL_PRECHARGE))
868
      auto_cnt_r <= 'd0;
869
    else if (state_r1 == CTRL_AUTO_REFRESH)
870
      auto_cnt_r <= auto_cnt_r + 1;
871
  end
872
 
873
  // register for timing purposes. Extra delay doesn't really matter
874
  always @(posedge clk)
875
    phy_init_done_r <= phy_init_done;
876
 
877
  always @(posedge clk)begin
878
    if (rst_r1) begin
879
      state_r    <= CTRL_IDLE;
880
      state_r1 <= CTRL_IDLE;
881
    end else begin
882
      state_r    <= next_state;
883
      state_r1 <= state_r;
884
    end
885
  end
886
 
887
  //***************************************************************************
888
  // main control state machine
889
  //***************************************************************************
890
 
891
  always @(*) begin
892
    next_state = state_r;
893
    (* full_case, parallel_case *) case (state_r)
894
      CTRL_IDLE: begin
895
        // perform auto refresh as soon as we are done with calibration.
896
        // The calibration logic does not do any refreshes.
897
        if (phy_init_done_r)
898
          next_state = CTRL_AUTO_REFRESH;
899
      end
900
 
901
      CTRL_PRECHARGE: begin
902
        if (auto_ref_r)
903
          next_state = CTRL_PRECHARGE_WAIT1;
904
        // when precharging an LRU bank, do not have to go to wait state
905
        // since we can't possibly be activating row in same bank next
906
        // disabled for 2t timing. There needs to be a gap between cmds
907
        // in 2t timing
908
        else if (no_precharge_wait_r && !TWO_T_TIME_EN)
909
          next_state = CTRL_ACTIVE;
910
        else
911
          next_state = CTRL_PRECHARGE_WAIT;
912
      end
913
 
914
      CTRL_PRECHARGE_WAIT:begin
915
        if (rp_cnt_ok_r)begin
916
          if (auto_ref_r)
917
            // precharge again to make sure we close all the banks
918
            next_state = CTRL_PRECHARGE;
919
          else
920
            next_state = CTRL_ACTIVE;
921
        end
922
      end
923
 
924
      CTRL_PRECHARGE_WAIT1:
925
        if (rp_cnt_ok_r)
926
          next_state = CTRL_AUTO_REFRESH;
927
 
928
      CTRL_AUTO_REFRESH:
929
        next_state = CTRL_AUTO_REFRESH_WAIT;
930
 
931
      CTRL_AUTO_REFRESH_WAIT:
932
      //staggering Auto refresh for multi
933
      // chip select designs. The SM waits
934
      // for the rfc time before issuing the
935
      // next auto refresh.
936
        if (auto_cnt_r < (CS_NUM))begin
937
           if (rfc_ok_r )
938
              next_state = CTRL_AUTO_REFRESH;
939
           end else if (rfc_ok_r)begin
940
              if(auto_ref_r)
941
                // MIG 2.3: For deep designs if Auto Refresh
942
                // flag asserted immediately after calibration is completed
943
                next_state = CTRL_PRECHARGE;
944
              else if  ( wr_flag || rd_flag)
945
                next_state = CTRL_ACTIVE;
946
            end
947
 
948
      CTRL_ACTIVE:
949
        next_state = CTRL_ACTIVE_WAIT;
950
 
951
      CTRL_ACTIVE_WAIT: begin
952
        if (rcd_cnt_ok_r) begin
953
          if ((conflict_detect_r && ~conflict_resolved_r) ||
954
              auto_ref_r) begin
955
            if (no_precharge_r1 && ~auto_ref_r && trrd_cnt_ok_r)
956
              next_state = CTRL_ACTIVE;
957
            else  if(precharge_ok_r)
958
              next_state = CTRL_PRECHARGE;
959
          end else if ((wr_flag_r) && (rd_to_wr_ok_r))
960
            next_state = CTRL_BURST_WRITE;
961
          else if ((rd_flag_r)&& (wr_to_rd_ok_r))
962
            next_state = CTRL_BURST_READ;
963
        end
964
      end
965
 
966
      // beginning of write burst
967
      CTRL_BURST_WRITE: begin
968
        if (BURST_LEN_DIV2 == 1) begin
969
          // special case if BL = 2 (i.e. burst lasts only one clk cycle)
970
          if (wr_flag)
971
            // if we have another non-conflict write command right after the
972
            // current write, then stay in this state
973
            next_state = CTRL_BURST_WRITE;
974
          else
975
            // otherwise, if we're done with this burst, and have no write
976
            // immediately scheduled after this one, wait until write-read
977
            // delay has passed
978
            next_state = CTRL_WRITE_WAIT;
979
        end else
980
          // otherwise BL > 2, and we  have at least one more write cycle for
981
          // current burst
982
          next_state = CTRL_WRITE_WAIT;
983
        // continuation of write burst (also covers waiting after write burst
984
        // has completed for write-read delay to pass)
985
      end
986
 
987
      CTRL_WRITE_WAIT: begin
988
        if ((conflict_detect) || auto_ref_r) begin
989
          if (no_precharge_r && ~auto_ref_r && wrburst_ok_r)
990
            next_state = CTRL_ACTIVE;
991
          else if (precharge_ok_r)
992
            next_state = CTRL_PRECHARGE;
993
        end else if (wrburst_ok_r && wr_flag)
994
          next_state = CTRL_BURST_WRITE;
995
        else if ((rd_flag) && (wr_to_rd_ok_r))
996
          next_state = CTRL_BURST_READ;
997
      end
998
 
999
      CTRL_BURST_READ: begin
1000
        if (BURST_LEN_DIV2 == 1) begin
1001
          // special case if BL = 2 (i.e. burst lasts only one clk cycle)
1002
          if (rd_flag)
1003
            next_state = CTRL_BURST_READ;
1004
          else
1005
            next_state = CTRL_READ_WAIT;
1006
        end else
1007
          next_state = CTRL_READ_WAIT;
1008
      end
1009
 
1010
      CTRL_READ_WAIT: begin
1011
        if ((conflict_detect) || auto_ref_r)begin
1012
          if (no_precharge_r && ~auto_ref_r && rdburst_ok_r)
1013
            next_state = CTRL_ACTIVE;
1014
          else if (precharge_ok_r)
1015
            next_state = CTRL_PRECHARGE;
1016
        // for burst of 4 in multi chip select
1017
        // if there is a change in cs wait one cycle before the
1018
        // next read command. cs_change_r will be asserted.
1019
        end else if (rdburst_ok_r  && rd_flag && ~cs_change_r)
1020
          next_state = CTRL_BURST_READ;
1021
        else if (wr_flag && (rd_to_wr_ok_r))
1022
          next_state = CTRL_BURST_WRITE;
1023
      end
1024
    endcase
1025
  end
1026
 
1027
  //***************************************************************************
1028
  // control signals to memory
1029
  //***************************************************************************
1030
 
1031
  always @(posedge clk) begin
1032
     if ((state_r == CTRL_AUTO_REFRESH) ||
1033
         (state_r == CTRL_ACTIVE) ||
1034
         (state_r == CTRL_PRECHARGE)) begin
1035
       ddr_ras_n_r <= 1'b0;
1036
       two_t_enable_r[0] <= 1'b0;
1037
     end else begin
1038
       if (TWO_T_TIME_EN)
1039
         ddr_ras_n_r <= two_t_enable_r[0] ;
1040
       else
1041
         ddr_ras_n_r <= 1'd1;
1042
       two_t_enable_r[0] <= 1'b1;
1043
     end
1044
  end
1045
 
1046
  always @(posedge clk)begin
1047
    if ((state_r == CTRL_BURST_WRITE) ||
1048
        (state_r == CTRL_BURST_READ) ||
1049
        (state_r == CTRL_AUTO_REFRESH)) begin
1050
      ddr_cas_n_r <= 1'b0;
1051
      two_t_enable_r[1] <= 1'b0;
1052
    end else begin
1053
      if (TWO_T_TIME_EN)
1054
        ddr_cas_n_r <= two_t_enable_r[1];
1055
      else
1056
        ddr_cas_n_r <= 1'b1;
1057
      two_t_enable_r[1] <= 1'b1;
1058
    end
1059
  end
1060
 
1061
  always @(posedge clk) begin
1062
    if ((state_r == CTRL_BURST_WRITE) ||
1063
        (state_r == CTRL_PRECHARGE)) begin
1064
      ddr_we_n_r <= 1'b0;
1065
      two_t_enable_r[2] <= 1'b0;
1066
    end else begin
1067
      if(TWO_T_TIME_EN)
1068
        ddr_we_n_r <= two_t_enable_r[2];
1069
      else
1070
        ddr_we_n_r <= 1'b1;
1071
      two_t_enable_r[2] <= 1'b1;
1072
    end
1073
  end
1074
 
1075
  // turn off auto-precharge when issuing commands (A10 = 0)
1076
  // mapping the col add for linear addressing.
1077
  generate
1078
    if (TWO_T_TIME_EN) begin: gen_addr_col_two_t
1079
      if (COL_WIDTH == ROW_WIDTH-1) begin: gen_ddr_addr_col_0
1080
        assign ddr_addr_col = {af_addr_r3[COL_WIDTH-1:10], 1'b0,
1081
                               af_addr_r3[9:0]};
1082
      end else begin
1083
        if (COL_WIDTH > 10) begin: gen_ddr_addr_col_1
1084
          assign ddr_addr_col = {{(ROW_WIDTH-COL_WIDTH-1){1'b0}},
1085
                                 af_addr_r3[COL_WIDTH-1:10], 1'b0,
1086
                                 af_addr_r3[9:0]};
1087
        end else begin: gen_ddr_addr_col_2
1088
          assign ddr_addr_col = {{(ROW_WIDTH-COL_WIDTH-1){1'b0}}, 1'b0,
1089
                               af_addr_r3[COL_WIDTH-1:0]};
1090
        end
1091
      end
1092
    end else begin: gen_addr_col_one_t
1093
      if (COL_WIDTH == ROW_WIDTH-1) begin: gen_ddr_addr_col_0_1
1094
        assign ddr_addr_col = {af_addr_r2[COL_WIDTH-1:10], 1'b0,
1095
                               af_addr_r2[9:0]};
1096
      end else begin
1097
        if (COL_WIDTH > 10) begin: gen_ddr_addr_col_1_1
1098
          assign ddr_addr_col = {{(ROW_WIDTH-COL_WIDTH-1){1'b0}},
1099
                                 af_addr_r2[COL_WIDTH-1:10], 1'b0,
1100
                                 af_addr_r2[9:0]};
1101
        end else begin: gen_ddr_addr_col_2_1
1102
          assign ddr_addr_col = {{(ROW_WIDTH-COL_WIDTH-1){1'b0}}, 1'b0,
1103
                                 af_addr_r2[COL_WIDTH-1:0]};
1104
        end
1105
      end
1106
    end
1107
  endgenerate
1108
 
1109
  // Assign address during row activate
1110
  generate
1111
    if (TWO_T_TIME_EN)
1112
      assign ddr_addr_row = af_addr_r3[ROW_RANGE_END:ROW_RANGE_START];
1113
    else
1114
      assign ddr_addr_row = af_addr_r2[ROW_RANGE_END:ROW_RANGE_START];
1115
  endgenerate
1116
 
1117
 
1118
  always @(posedge clk)begin
1119
    if ((state_r == CTRL_ACTIVE) ||
1120
        ((state_r1 == CTRL_ACTIVE) && TWO_T_TIME_EN))
1121
      ddr_addr_r <= ddr_addr_row;
1122
    else if ((state_r == CTRL_BURST_WRITE) ||
1123
             (state_r == CTRL_BURST_READ)  ||
1124
             (((state_r1 == CTRL_BURST_WRITE) ||
1125
               (state_r1 == CTRL_BURST_READ)) &&
1126
              TWO_T_TIME_EN))
1127
      ddr_addr_r <= ddr_addr_col;
1128
    else if (((state_r == CTRL_PRECHARGE)  ||
1129
              ((state_r1 == CTRL_PRECHARGE) && TWO_T_TIME_EN))
1130
             && auto_ref_r) begin
1131
      // if we're precharging as a result of AUTO-REFRESH, precharge all banks
1132
      ddr_addr_r <= {ROW_WIDTH{1'b0}};
1133
      ddr_addr_r[10] <= 1'b1;
1134
    end else if ((state_r == CTRL_PRECHARGE) ||
1135
                 ((state_r1 == CTRL_PRECHARGE) && TWO_T_TIME_EN))
1136
      // if we're precharging to close a specific bank/row, set A10=0
1137
      ddr_addr_r <= {ROW_WIDTH{1'b0}};
1138
    else
1139
      ddr_addr_r <= {ROW_WIDTH{1'bx}};
1140
  end
1141
 
1142
  always @(posedge clk)begin
1143
    // whenever we're precharging, we're either: (1) precharging all banks (in
1144
    // which case banks bits are don't care, (2) precharging the LRU bank,
1145
    // b/c we've exceeded the limit of # of banks open (need to close the LRU
1146
    // bank to make room for a new one), (3) we haven't exceed the maximum #
1147
    // of banks open, but we trying to open a different row in a bank that's
1148
    // already open
1149
    if (((state_r == CTRL_PRECHARGE)  ||
1150
         ((state_r1 == CTRL_PRECHARGE) && TWO_T_TIME_EN)) &&
1151
        bank_conflict_r && MULTI_BANK_EN)
1152
      // When LRU bank needs to be closed
1153
      ddr_ba_r <= bank_cmp_addr_r[(3*CMP_WIDTH)+CMP_BANK_RANGE_END:
1154
                                  (3*CMP_WIDTH)+CMP_BANK_RANGE_START];
1155
    else begin
1156
      // Either precharge due to refresh or bank hit case
1157
      if (TWO_T_TIME_EN)
1158
        ddr_ba_r <= af_addr_r3[BANK_RANGE_END:BANK_RANGE_START];
1159
      else
1160
        ddr_ba_r <= af_addr_r2[BANK_RANGE_END:BANK_RANGE_START];
1161
    end
1162
  end
1163
 
1164
  // chip enable generation logic
1165
  generate
1166
    // if only one chip select, always assert it after reset
1167
    if (CS_BITS == 0) begin: gen_ddr_cs_0
1168
      always @(posedge clk)
1169
        if (rst_r1)
1170
          ddr_cs_n_r[0] <= 1'b1;
1171
        else
1172
          ddr_cs_n_r[0] <= 1'b0;
1173
    // otherwise if we have multiple chip selects
1174
      end else begin: gen_ddr_cs_1
1175
      if(TWO_T_TIME_EN) begin: gen_2t_cs
1176
         always @(posedge clk)
1177
           if (rst_r1)
1178
             ddr_cs_n_r <= {CS_NUM{1'b1}};
1179
           else if ((state_r1 == CTRL_AUTO_REFRESH)) begin
1180
             // if auto-refreshing, only auto-refresh one CS at any time (avoid
1181
             // beating on the ground plane by refreshing all CS's at same time)
1182
             ddr_cs_n_r <= {CS_NUM{1'b1}};
1183
             ddr_cs_n_r[auto_cnt_r] <= 1'b0;
1184
           end else if (auto_ref_r && (state_r1 == CTRL_PRECHARGE)) begin
1185
             ddr_cs_n_r <= {CS_NUM{1'b0}};
1186
           end else if ((state_r1 == CTRL_PRECHARGE) && ( bank_conflict_r
1187
                    && MULTI_BANK_EN))begin
1188
                  // precharging the LRU bank
1189
                  ddr_cs_n_r <= {CS_NUM{1'b1}};
1190
                  ddr_cs_n_r[bank_cmp_addr_r[(3*CMP_WIDTH)+CMP_CS_RANGE_END:
1191
                  (3*CMP_WIDTH)+CMP_CS_RANGE_START]] <= 1'b0;
1192
           end else begin
1193
          // otherwise, check the upper address bits to see which CS to assert
1194
             ddr_cs_n_r <= {CS_NUM{1'b1}};
1195
             ddr_cs_n_r[af_addr_r3[CS_RANGE_END:CS_RANGE_START]] <= 1'b0;
1196
           end // else: !if(((state_r == CTRL_PRECHARGE)  ||...
1197
        end else begin: gen_1t_cs // block: gen_2t_cs
1198
         always @(posedge clk)
1199
           if (rst_r1)
1200
             ddr_cs_n_r <= {CS_NUM{1'b1}};
1201
           else if ((state_r == CTRL_AUTO_REFRESH) ) begin
1202
             // if auto-refreshing, only auto-refresh one CS at any time (avoid
1203
             // beating on the ground plane by refreshing all CS's at same time)
1204
             ddr_cs_n_r <= {CS_NUM{1'b1}};
1205
             ddr_cs_n_r[auto_cnt_r] <= 1'b0;
1206
           end else if (auto_ref_r && (state_r == CTRL_PRECHARGE) ) begin
1207
             ddr_cs_n_r <= {CS_NUM{1'b0}};
1208
           end else if ((state_r == CTRL_PRECHARGE)  &&
1209
                 (bank_conflict_r && MULTI_BANK_EN))begin
1210
                  // precharging the LRU bank
1211
                  ddr_cs_n_r <= {CS_NUM{1'b1}};
1212
                  ddr_cs_n_r[bank_cmp_addr_r[(3*CMP_WIDTH)+CMP_CS_RANGE_END:
1213
                  (3*CMP_WIDTH)+CMP_CS_RANGE_START]] <= 1'b0;
1214
           end else begin
1215
          // otherwise, check the upper address bits to see which CS to assert
1216
             ddr_cs_n_r <= {CS_NUM{1'b1}};
1217
             ddr_cs_n_r[af_addr_r2[CS_RANGE_END:CS_RANGE_START]] <= 1'b0;
1218
           end // else: !if(((state_r == CTRL_PRECHARGE)  ||...
1219
        end // block: gen_1t_cs
1220
    end
1221
  endgenerate
1222
 
1223
  // registring the two_t timing enable signal.
1224
  // This signal will be asserted (low) when the
1225
  // chip select has to be asserted.
1226
  always @(posedge clk)begin
1227
     if(&two_t_enable_r)
1228
        two_t_enable_r1 <= {CS_NUM{1'b1}};
1229
     else
1230
        two_t_enable_r1 <= {CS_NUM{1'b0}};
1231
  end
1232
 
1233
  assign ctrl_addr  = ddr_addr_r;
1234
  assign ctrl_ba    = ddr_ba_r;
1235
  assign ctrl_ras_n = ddr_ras_n_r;
1236
  assign ctrl_cas_n = ddr_cas_n_r;
1237
  assign ctrl_we_n  = ddr_we_n_r;
1238
  assign ctrl_cs_n  = (TWO_T_TIME_EN) ?
1239
                      (ddr_cs_n_r | two_t_enable_r1) :
1240
                      ddr_cs_n_r;
1241
 
1242
endmodule
1243
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.