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

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [examples/] [memfifo/] [fpga-2.04b/] [ipcore_dir/] [mem0/] [example_design/] [sim/] [functional/] [ddr_model_c3.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/****************************************************************************************
2
*
3
*    File Name:  ddr.v
4
*      Version:  6.00
5
*        Model:  BUS Functional
6
*
7
* Dependencies:  ddr_model_parameters.vh
8
*
9
*  Description:  Micron SDRAM DDR (Double Data Rate)
10
*
11
*   Limitation:  - Doesn't check for 8K-cycle refresh.
12
*                - Doesn't check power-down entry/exit
13
*                - Doesn't check self-refresh entry/exit.
14
*
15
*         Note:  - Set simulator resolution to "ps" accuracy
16
*                - Set DEBUG = 0 to disable $display messages
17
*                - Model assume Clk and Clk# crossing at both edge
18
*
19
*   Disclaimer   This software code and all associated documentation, comments or other
20
*  of Warranty:  information (collectively "Software") is provided "AS IS" without
21
*                warranty of any kind. MICRON TECHNOLOGY, INC. ("MTI") EXPRESSLY
22
*                DISCLAIMS ALL WARRANTIES EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
23
*                TO, NONINFRINGEMENT OF THIRD PARTY RIGHTS, AND ANY IMPLIED WARRANTIES
24
*                OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE. MTI DOES NOT
25
*                WARRANT THAT THE SOFTWARE WILL MEET YOUR REQUIREMENTS, OR THAT THE
26
*                OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE.
27
*                FURTHERMORE, MTI DOES NOT MAKE ANY REPRESENTATIONS REGARDING THE USE OR
28
*                THE RESULTS OF THE USE OF THE SOFTWARE IN TERMS OF ITS CORRECTNESS,
29
*                ACCURACY, RELIABILITY, OR OTHERWISE. THE ENTIRE RISK ARISING OUT OF USE
30
*                OR PERFORMANCE OF THE SOFTWARE REMAINS WITH YOU. IN NO EVENT SHALL MTI,
31
*                ITS AFFILIATED COMPANIES OR THEIR SUPPLIERS BE LIABLE FOR ANY DIRECT,
32
*                INDIRECT, CONSEQUENTIAL, INCIDENTAL, OR SPECIAL DAMAGES (INCLUDING,
33
*                WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION,
34
*                OR LOSS OF INFORMATION) ARISING OUT OF YOUR USE OF OR INABILITY TO USE
35
*                THE SOFTWARE, EVEN IF MTI HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
36
*                DAMAGES. Because some jurisdictions prohibit the exclusion or
37
*                limitation of liability for consequential or incidental damages, the
38
*                above limitation may not apply to you.
39
*
40
*                Copyright 2003 Micron Technology, Inc. All rights reserved.
41
*
42
* Rev  Author Date        Changes
43
* ---  ------ ----------  ---------------------------------------
44
* 2.1   SPH    03/19/2002  - Second Release
45
*                          - Fix tWR and several incompatability
46
*                            between different simulators
47
* 3.0   TFK    02/18/2003  - Added tDSS and tDSH timing checks.
48
*                          - Added tDQSH and tDQSL timing checks.
49
* 3.1   CAH    05/28/2003  - update all models to release version 3.1
50
*                            (no changes to this model)
51
* 3.2   JMK    06/16/2003  - updated all DDR400 models to support CAS Latency 3
52
* 3.3   JMK    09/11/2003  - Added initialization sequence checks.
53
* 4.0   JMK    12/01/2003  - Grouped parameters into "ddr_parameters.v"
54
*                          - Fixed tWTR check
55
* 4.1   JMK    01/14/2004  - Grouped specify parameters by speed grade
56
*                          - Fixed mem_sizes parameter
57
* 4.2   JMK    03/19/2004  - Fixed pulse width checking on Dqs
58
* 4.3   JMK    04/27/2004  - Changed BL wire size in tb module
59
*                          - Changed Dq_buf size to [15:0]
60
* 5.0   JMK    06/16/2004  - Added read to write checking.
61
*                          - Added read with precharge truncation to write checking.
62
*                          - Added associative memory array to reduce memory consumption.
63
*                          - Added checking for required DQS edges during write.
64
* 5.1   JMK    08/16/2004  - Fixed checking for required DQS edges during write.
65
*                          - Fixed wdqs_valid window.
66
* 5.2   JMK    09/24/2004  - Read or Write without activate will be ignored.
67
* 5.3   JMK    10/27/2004  - Added tMRD checking during Auto Refresh and Activate.
68
*                          - Added tRFC checking during Load Mode and Precharge.
69
* 5.4   JMK    12/13/2004  - The model will not respond to illegal command sequences.
70
* 5.5   SPH    01/13/2005  - The model will issue a halt on illegal command sequences.
71
*       JMK    02/11/2005  - Changed the display format for numbers to hex.
72
* 5.6   JMK    04/22/2005  - Fixed Write with auto precharge calculation.
73
* 5.7   JMK    08/05/2005  - Changed conditions for read with precharge truncation error.
74
*                          - Renamed parameters file with .vh extension.
75
* 5.8   BAS    12/26/2006  - Added parameters for T46A part - 256Mb
76
*                          - Added x32 functionality
77
* 6.00  JMK    05/31/2007  - Added ddr_184_dimm module model
78
* 6.00  BAS    05/31/2007  - Updated 128Mb, 256Mb, 512Mb, and 1024Mb parameter sheets
79
****************************************************************************************/
80
 
81
// DO NOT CHANGE THE TIMESCALE
82
// MAKE SURE YOUR SIMULATOR USE "PS" RESOLUTION
83
`timescale 1ns / 1ps
84
 
85
module ddr_model_c3 (Clk, Clk_n, Cke, Cs_n, Ras_n, Cas_n, We_n, Ba , Addr, Dm, Dq, Dqs);
86
    `include "ddr_model_parameters_c3.vh"
87
 
88
    // Port Declarations
89
    input                         Clk;
90
    input                         Clk_n;
91
    input                         Cke;
92
    input                         Cs_n;
93
    input                         Ras_n;
94
    input                         Cas_n;
95
    input                         We_n;
96
    input                 [1 : 0] Ba;
97
    input     [ADDR_BITS - 1 : 0] Addr;
98
    input       [DM_BITS - 1 : 0] Dm;
99
    inout       [DQ_BITS - 1 : 0] Dq;
100
    inout      [DQS_BITS - 1 : 0] Dqs;
101
 
102
    // Internal Wires (fixed width)
103
    wire                 [31 : 0] Dq_in;
104
    wire                  [3 : 0] Dqs_in;
105
    wire                  [3 : 0] Dm_in;
106
 
107
    assign Dq_in   [DQ_BITS - 1 : 0] = Dq;
108
    assign Dqs_in [DQS_BITS - 1 : 0] = Dqs;
109
    assign Dm_in   [DM_BITS - 1 : 0] = Dm;
110
 
111
    // Data pair
112
    reg                  [31 : 0] dq_rise;
113
    reg                   [3 : 0] dm_rise;
114
    reg                  [31 : 0] dq_fall;
115
    reg                   [3 : 0] dm_fall;
116
    reg                   [7 : 0] dm_pair;
117
    reg                  [31 : 0] Dq_buf;
118
 
119
    // Mode Register
120
    reg       [ADDR_BITS - 1 : 0] Mode_reg;
121
 
122
    // Internal System Clock
123
    reg                           CkeZ, Sys_clk;
124
 
125
    // Internal Dqs initialize
126
    reg                           Dqs_int;
127
 
128
    // Dqs buffer
129
    reg        [DQS_BITS - 1 : 0] Dqs_out;
130
 
131
    // Dq buffer
132
    reg         [DQ_BITS - 1 : 0] Dq_out;
133
 
134
    // Read pipeline variables
135
    reg                           Read_cmnd [0 : 6];
136
    reg                   [1 : 0] Read_bank [0 : 6];
137
    reg        [COL_BITS - 1 : 0] Read_cols [0 : 6];
138
 
139
    // Write pipeline variables
140
    reg                           Write_cmnd [0 : 3];
141
    reg                   [1 : 0] Write_bank [0 : 3];
142
    reg        [COL_BITS - 1 : 0] Write_cols [0 : 3];
143
 
144
    // Auto precharge variables
145
    reg                           Read_precharge  [0 : 3];
146
    reg                           Write_precharge [0 : 3];
147
    integer                       Count_precharge [0 : 3];
148
 
149
    // Manual precharge variables
150
    reg                           A10_precharge  [0 : 6];
151
    reg                   [1 : 0] Bank_precharge [0 : 6];
152
    reg                           Cmnd_precharge [0 : 6];
153
 
154
    // Burst terminate variables
155
    reg                           Cmnd_bst [0 : 6];
156
 
157
    // Memory Banks
158
`ifdef FULL_MEM
159
    reg         [DQ_BITS - 1 : 0] mem_array  [0 : (1<<full_mem_bits)-1];
160
`else
161
    reg         [DQ_BITS - 1 : 0] mem_array  [0 : (1<<part_mem_bits)-1];
162
    reg   [full_mem_bits - 1 : 0] addr_array [0 : (1<<part_mem_bits)-1];
163
    reg   [part_mem_bits     : 0] mem_used;
164
    initial mem_used = 0;
165
`endif
166
 
167
    // Dqs edge checking
168
    integer i;
169
    reg  [3 :0] expect_pos_dqs;
170
    reg  [3 :0] expect_neg_dqs;
171
 
172
    // Burst counter
173
    reg        [COL_BITS - 1 : 0] Burst_counter;
174
 
175
    // Precharge variables
176
    reg                           Pc_b0, Pc_b1, Pc_b2, Pc_b3;
177
 
178
    // Activate variables
179
    reg                           Act_b0, Act_b1, Act_b2, Act_b3;
180
 
181
    // Data IO variables
182
    reg                           Data_in_enable;
183
    reg                           Data_out_enable;
184
 
185
    // Internal address mux variables
186
    reg                   [1 : 0] Prev_bank;
187
    reg                   [1 : 0] Bank_addr;
188
    reg        [COL_BITS - 1 : 0] Cols_addr, Cols_brst, Cols_temp;
189
    reg       [ADDR_BITS - 1 : 0] Rows_addr;
190
    reg       [ADDR_BITS - 1 : 0] B0_row_addr;
191
    reg       [ADDR_BITS - 1 : 0] B1_row_addr;
192
    reg       [ADDR_BITS - 1 : 0] B2_row_addr;
193
    reg       [ADDR_BITS - 1 : 0] B3_row_addr;
194
 
195
    // DLL Reset variable
196
    reg                           DLL_enable;
197
    reg                           DLL_reset;
198
    reg                           DLL_done;
199
    integer                       DLL_count;
200
    integer                       aref_count;
201
    integer                       Prech_count;
202
    reg                           power_up_done;
203
 
204
    // Write DQS for tDSS, tDSH, tDQSH, tDQSL checks
205
    wire      wdqs_valid = Write_cmnd[2] || Write_cmnd[1] || Data_in_enable;
206
 
207
    // Commands Decode
208
    wire      Active_enable   = ~Cs_n & ~Ras_n &  Cas_n &  We_n;
209
    wire      Aref_enable     = ~Cs_n & ~Ras_n & ~Cas_n &  We_n;
210
    wire      Burst_term      = ~Cs_n &  Ras_n &  Cas_n & ~We_n;
211
    wire      Ext_mode_enable = ~Cs_n & ~Ras_n & ~Cas_n & ~We_n &  Ba[0] & ~Ba[1];
212
    wire      Mode_reg_enable = ~Cs_n & ~Ras_n & ~Cas_n & ~We_n & ~Ba[0] & ~Ba[1];
213
    wire      Prech_enable    = ~Cs_n & ~Ras_n &  Cas_n & ~We_n;
214
    wire      Read_enable     = ~Cs_n &  Ras_n & ~Cas_n &  We_n;
215
    wire      Write_enable    = ~Cs_n &  Ras_n & ~Cas_n & ~We_n;
216
 
217
    // Burst Length Decode
218
    wire [3:0] burst_length = 1 << (Mode_reg[2:0]);
219
    reg  [3:0] read_precharge_truncation;
220
 
221
    // CAS Latency Decode
222
    wire [2:0] cas_latency_x2 = (Mode_reg[6:4] === 3'o6) ? 5 : 2*Mode_reg[6:4];
223
 
224
    // DQS Buffer
225
    assign    Dqs = Dqs_out;
226
 
227
    // DQ Buffer
228
    assign    Dq  = Dq_out;
229
 
230
    // Timing Check
231
    time      MRD_chk;
232
    time      RFC_chk;
233
    time      RRD_chk;
234
    time      RAS_chk0, RAS_chk1, RAS_chk2, RAS_chk3;
235
    time      RAP_chk0, RAP_chk1, RAP_chk2, RAP_chk3;
236
    time      RC_chk0, RC_chk1, RC_chk2, RC_chk3;
237
    time      RCD_chk0, RCD_chk1, RCD_chk2, RCD_chk3;
238
    time      RP_chk0, RP_chk1, RP_chk2, RP_chk3;
239
    time      WR_chk0, WR_chk1, WR_chk2, WR_chk3;
240
 
241
    initial begin
242
        CkeZ = 1'b0;
243
        Sys_clk = 1'b0;
244
        {Pc_b0, Pc_b1, Pc_b2, Pc_b3} = 4'b0000;
245
        {Act_b0, Act_b1, Act_b2, Act_b3} = 4'b1111;
246
        Dqs_int = 1'b0;
247
        Dqs_out = {DQS_BITS{1'bz}};
248
        Dq_out = {DQ_BITS{1'bz}};
249
        Data_in_enable = 1'b0;
250
        Data_out_enable = 1'b0;
251
        DLL_enable = 1'b0;
252
        DLL_reset = 1'b0;
253
        DLL_done = 1'b0;
254
        DLL_count = 0;
255
        aref_count = 0;
256
        Prech_count = 0;
257
        power_up_done = 0;
258
        MRD_chk = 0;
259
        RFC_chk = 0;
260
        RRD_chk = 0;
261
        {RAS_chk0, RAS_chk1, RAS_chk2, RAS_chk3} = 0;
262
        {RAP_chk0, RAP_chk1, RAP_chk2, RAP_chk3} = 0;
263
        {RC_chk0, RC_chk1, RC_chk2, RC_chk3} = 0;
264
        {RCD_chk0, RCD_chk1, RCD_chk2, RCD_chk3} = 0;
265
        {RP_chk0, RP_chk1, RP_chk2, RP_chk3} = 0;
266
        {WR_chk0, WR_chk1, WR_chk2, WR_chk3} = 0;
267
        $timeformat (-9, 3, " ns", 12);
268
    end
269
 
270
    // System Clock
271
    always begin
272
        @ (posedge Clk) begin
273
            Sys_clk = CkeZ;
274
            CkeZ = Cke;
275
        end
276
        @ (negedge Clk) begin
277
            Sys_clk = 1'b0;
278
        end
279
    end
280
 
281
    // Check to make sure that we have a Deselect or NOP command on the bus when CKE is brought high
282
    always @(Cke) begin
283
        if (Cke === 1'b1) begin
284
            if (!((Cs_n) || (~Cs_n &  Ras_n & Cas_n &  We_n))) begin
285
                $display ("%m: at time %t MEMORY ERROR:  You must have a Deselect or NOP command applied", $time);
286
                $display ("%m:           when the Clock Enable is brought High.");
287
            end
288
        end
289
    end
290
 
291
    // Check the initialization sequence
292
    initial begin
293
        @ (posedge Cke) begin
294
            @ (posedge DLL_enable) begin
295
                aref_count = 0;
296
                @ (posedge DLL_reset) begin
297
                    @ (Prech_count) begin
298
                        if (aref_count >= 2) begin
299
                            if (DEBUG) $display ("%m: at time %t MEMORY:  Power Up and Initialization Sequence is complete", $time);
300
                            power_up_done = 1;
301
                        end else begin
302
                            aref_count = 0;
303
                            @ (aref_count >= 2) begin
304
                                if (DEBUG) $display ("%m: at time %t MEMORY:  Power Up and Initialization Sequence is complete", $time);
305
                                power_up_done = 1;
306
                            end
307
                        end
308
                    end
309
                end
310
            end
311
        end
312
    end
313
 
314
    // Write Memory
315
    task write_mem;
316
        input [full_mem_bits - 1 : 0] addr;
317
        input       [DQ_BITS - 1 : 0] data;
318
        reg       [part_mem_bits : 0] i;
319
        begin
320
`ifdef FULL_MEM
321
            mem_array[addr] = data;
322
`else
323
            begin : loop
324
                for (i = 0; i < mem_used; i = i + 1) begin
325
                    if (addr_array[i] === addr) begin
326
                        disable loop;
327
                    end
328
                end
329
            end
330
            if (i === mem_used) begin
331
                if (i === (1<<part_mem_bits)) begin
332
                    $display ("At time %t ERROR: Memory overflow.\n  Write to Address %h with Data %h will be lost.\n  You must increase the part_mem_bits parameter or `define FULL_MEM.", $time, addr, data);
333
                end else begin
334
                    mem_used = mem_used + 1;
335
                    addr_array[i] = addr;
336
                end
337
            end
338
            mem_array[i] = data;
339
`endif
340
        end
341
    endtask
342
 
343
    // Read Memory
344
    task read_mem;
345
        input [full_mem_bits - 1 : 0] addr;
346
        output      [DQ_BITS - 1 : 0] data;
347
        reg       [part_mem_bits : 0] i;
348
        begin
349
`ifdef FULL_MEM
350
            data = mem_array[addr];
351
`else
352
            begin : loop
353
                for (i = 0; i < mem_used; i = i + 1) begin
354
                    if (addr_array[i] === addr) begin
355
                        disable loop;
356
                    end
357
                end
358
            end
359
            if (i <= mem_used) begin
360
                data = mem_array[i];
361
            end
362
`endif
363
        end
364
    endtask
365
 
366
    // Burst Decode
367
    task Burst_Decode;
368
    begin
369
 
370
        // Advance Burst Counter
371
        if (Burst_counter < burst_length) begin
372
            Burst_counter = Burst_counter + 1;
373
        end
374
 
375
        // Burst Type
376
        if (Mode_reg[3] === 1'b0) begin                         // Sequential Burst
377
            Cols_temp = Cols_addr + 1;
378
        end else if (Mode_reg[3] === 1'b1) begin                // Interleaved Burst
379
            Cols_temp[2] =  Burst_counter[2] ^ Cols_brst[2];
380
            Cols_temp[1] =  Burst_counter[1] ^ Cols_brst[1];
381
            Cols_temp[0] =  Burst_counter[0] ^ Cols_brst[0];
382
        end
383
 
384
        // Burst Length
385
        if (burst_length === 2) begin
386
            Cols_addr [0] = Cols_temp [0];
387
        end else if (burst_length === 4) begin
388
            Cols_addr [1 : 0] = Cols_temp [1 : 0];
389
        end else if (burst_length === 8) begin
390
            Cols_addr [2 : 0] = Cols_temp [2 : 0];
391
        end else begin
392
            Cols_addr = Cols_temp;
393
        end
394
 
395
        // Data Counter
396
        if (Burst_counter >= burst_length) begin
397
            Data_in_enable = 1'b0;
398
            Data_out_enable = 1'b0;
399
            read_precharge_truncation = 4'h0;
400
        end
401
 
402
    end
403
    endtask
404
 
405
    // Manual Precharge Pipeline
406
    task Manual_Precharge_Pipeline;
407
    begin
408
        // A10 Precharge Pipeline
409
        A10_precharge[0] = A10_precharge[1];
410
        A10_precharge[1] = A10_precharge[2];
411
        A10_precharge[2] = A10_precharge[3];
412
        A10_precharge[3] = A10_precharge[4];
413
        A10_precharge[4] = A10_precharge[5];
414
        A10_precharge[5] = A10_precharge[6];
415
        A10_precharge[6] = 1'b0;
416
 
417
        // Bank Precharge Pipeline
418
        Bank_precharge[0] = Bank_precharge[1];
419
        Bank_precharge[1] = Bank_precharge[2];
420
        Bank_precharge[2] = Bank_precharge[3];
421
        Bank_precharge[3] = Bank_precharge[4];
422
        Bank_precharge[4] = Bank_precharge[5];
423
        Bank_precharge[5] = Bank_precharge[6];
424
        Bank_precharge[6] = 2'b0;
425
 
426
        // Command Precharge Pipeline
427
        Cmnd_precharge[0] = Cmnd_precharge[1];
428
        Cmnd_precharge[1] = Cmnd_precharge[2];
429
        Cmnd_precharge[2] = Cmnd_precharge[3];
430
        Cmnd_precharge[3] = Cmnd_precharge[4];
431
        Cmnd_precharge[4] = Cmnd_precharge[5];
432
        Cmnd_precharge[5] = Cmnd_precharge[6];
433
        Cmnd_precharge[6] = 1'b0;
434
 
435
        // Terminate a Read if same bank or all banks
436
        if (Cmnd_precharge[0] === 1'b1) begin
437
            if (Bank_precharge[0] === Bank_addr || A10_precharge[0] === 1'b1) begin
438
                if (Data_out_enable === 1'b1) begin
439
                    Data_out_enable = 1'b0;
440
                    read_precharge_truncation = 4'hF;
441
                end
442
            end
443
        end
444
    end
445
    endtask
446
 
447
    // Burst Terminate Pipeline
448
    task Burst_Terminate_Pipeline;
449
    begin
450
        // Command Precharge Pipeline
451
        Cmnd_bst[0] = Cmnd_bst[1];
452
        Cmnd_bst[1] = Cmnd_bst[2];
453
        Cmnd_bst[2] = Cmnd_bst[3];
454
        Cmnd_bst[3] = Cmnd_bst[4];
455
        Cmnd_bst[4] = Cmnd_bst[5];
456
        Cmnd_bst[5] = Cmnd_bst[6];
457
        Cmnd_bst[6] = 1'b0;
458
 
459
        // Terminate a Read regardless of banks
460
        if (Cmnd_bst[0] === 1'b1 && Data_out_enable === 1'b1) begin
461
            Data_out_enable = 1'b0;
462
        end
463
    end
464
    endtask
465
 
466
    // Dq and Dqs Drivers
467
    task Dq_Dqs_Drivers;
468
    begin
469
        // read command pipeline
470
        Read_cmnd [0] = Read_cmnd [1];
471
        Read_cmnd [1] = Read_cmnd [2];
472
        Read_cmnd [2] = Read_cmnd [3];
473
        Read_cmnd [3] = Read_cmnd [4];
474
        Read_cmnd [4] = Read_cmnd [5];
475
        Read_cmnd [5] = Read_cmnd [6];
476
        Read_cmnd [6] = 1'b0;
477
 
478
        // read bank pipeline
479
        Read_bank [0] = Read_bank [1];
480
        Read_bank [1] = Read_bank [2];
481
        Read_bank [2] = Read_bank [3];
482
        Read_bank [3] = Read_bank [4];
483
        Read_bank [4] = Read_bank [5];
484
        Read_bank [5] = Read_bank [6];
485
        Read_bank [6] = 2'b0;
486
 
487
        // read column pipeline
488
        Read_cols [0] = Read_cols [1];
489
        Read_cols [1] = Read_cols [2];
490
        Read_cols [2] = Read_cols [3];
491
        Read_cols [3] = Read_cols [4];
492
        Read_cols [4] = Read_cols [5];
493
        Read_cols [5] = Read_cols [6];
494
        Read_cols [6] = 0;
495
 
496
        // Initialize Read command
497
        if (Read_cmnd [0] === 1'b1) begin
498
            Data_out_enable = 1'b1;
499
            Bank_addr = Read_bank [0];
500
            Cols_addr = Read_cols [0];
501
            Cols_brst = Cols_addr [2 : 0];
502
            Burst_counter = 0;
503
 
504
            // Row Address Mux
505
            case (Bank_addr)
506
                2'd0    : Rows_addr = B0_row_addr;
507
                2'd1    : Rows_addr = B1_row_addr;
508
                2'd2    : Rows_addr = B2_row_addr;
509
                2'd3    : Rows_addr = B3_row_addr;
510
                default : $display ("At time %t ERROR: Invalid Bank Address", $time);
511
            endcase
512
        end
513
 
514
        // Toggle Dqs during Read command
515
        if (Data_out_enable === 1'b1) begin
516
            Dqs_int = 1'b0;
517
            if (Dqs_out === {DQS_BITS{1'b0}}) begin
518
                Dqs_out = {DQS_BITS{1'b1}};
519
            end else if (Dqs_out === {DQS_BITS{1'b1}}) begin
520
                Dqs_out = {DQS_BITS{1'b0}};
521
            end else begin
522
                Dqs_out = {DQS_BITS{1'b0}};
523
            end
524
        end else if (Data_out_enable === 1'b0 && Dqs_int === 1'b0) begin
525
            Dqs_out = {DQS_BITS{1'bz}};
526
        end
527
 
528
        // Initialize dqs for Read command
529
        if (Read_cmnd [2] === 1'b1) begin
530
            if (Data_out_enable === 1'b0) begin
531
                Dqs_int = 1'b1;
532
                Dqs_out = {DQS_BITS{1'b0}};
533
            end
534
        end
535
 
536
        // Read latch
537
        if (Data_out_enable === 1'b1) begin
538
            // output data
539
            read_mem({Bank_addr, Rows_addr, Cols_addr}, Dq_out);
540
            if (DEBUG) begin
541
                $display ("At time %t READ : Bank = %h, Row = %h, Col = %h, Data = %h", $time, Bank_addr, Rows_addr, Cols_addr, Dq_out);
542
            end
543
        end else begin
544
            Dq_out = {DQ_BITS{1'bz}};
545
        end
546
    end
547
    endtask
548
 
549
    // Write FIFO and DM Mask Logic
550
    task Write_FIFO_DM_Mask_Logic;
551
    begin
552
        // Write command pipeline
553
        Write_cmnd [0] = Write_cmnd [1];
554
        Write_cmnd [1] = Write_cmnd [2];
555
        Write_cmnd [2] = Write_cmnd [3];
556
        Write_cmnd [3] = 1'b0;
557
 
558
        // Write command pipeline
559
        Write_bank [0] = Write_bank [1];
560
        Write_bank [1] = Write_bank [2];
561
        Write_bank [2] = Write_bank [3];
562
        Write_bank [3] = 2'b0;
563
 
564
        // Write column pipeline
565
        Write_cols [0] = Write_cols [1];
566
        Write_cols [1] = Write_cols [2];
567
        Write_cols [2] = Write_cols [3];
568
        Write_cols [3] = {COL_BITS{1'b0}};
569
 
570
        // Initialize Write command
571
        if (Write_cmnd [0] === 1'b1) begin
572
            Data_in_enable = 1'b1;
573
            Bank_addr = Write_bank [0];
574
            Cols_addr = Write_cols [0];
575
            Cols_brst = Cols_addr [2 : 0];
576
            Burst_counter = 0;
577
 
578
            // Row address mux
579
            case (Bank_addr)
580
                2'd0    : Rows_addr = B0_row_addr;
581
                2'd1    : Rows_addr = B1_row_addr;
582
                2'd2    : Rows_addr = B2_row_addr;
583
                2'd3    : Rows_addr = B3_row_addr;
584
                default : $display ("At time %t ERROR: Invalid Row Address", $time);
585
            endcase
586
        end
587
 
588
        // Write data
589
        if (Data_in_enable === 1'b1) begin
590
 
591
            // Data Buffer
592
            read_mem({Bank_addr, Rows_addr, Cols_addr}, Dq_buf);
593
 
594
            // write negedge Dqs on posedge Sys_clk
595
            if (Sys_clk) begin
596
                if (!dm_fall[0]) begin
597
                    Dq_buf [ 7 : 0] = dq_fall [ 7 : 0];
598
                end
599
                if (!dm_fall[1]) begin
600
                    Dq_buf [15 : 8] = dq_fall [15 : 8];
601
                end
602
                if (!dm_fall[2]) begin
603
                    Dq_buf [23 : 16] = dq_fall [23 : 16];
604
                end
605
                if (!dm_fall[3]) begin
606
                    Dq_buf [31 : 24] = dq_fall [31 : 24];
607
                end
608
                if (~&dm_fall) begin
609
                    if (DEBUG) begin
610
                        $display ("At time %t WRITE: Bank = %h, Row = %h, Col = %h, Data = %h", $time, Bank_addr, Rows_addr, Cols_addr, Dq_buf[DQ_BITS-1:0]);
611
                    end
612
                end
613
            // write posedge Dqs on negedge Sys_clk
614
            end else begin
615
                if (!dm_rise[0]) begin
616
                    Dq_buf [ 7 : 0] = dq_rise [ 7 : 0];
617
                end
618
                if (!dm_rise[1]) begin
619
                    Dq_buf [15 : 8] = dq_rise [15 : 8];
620
                end
621
                if (!dm_rise[2]) begin
622
                    Dq_buf [23 : 16] = dq_rise [23 : 16];
623
                end
624
                if (!dm_rise[3]) begin
625
                    Dq_buf [31 : 24] = dq_rise [31 : 24];
626
                end
627
                if (~&dm_rise) begin
628
                    if (DEBUG) begin
629
                        $display ("At time %t WRITE: Bank = %h, Row = %h, Col = %h, Data = %h", $time, Bank_addr, Rows_addr, Cols_addr, Dq_buf[DQ_BITS-1:0]);
630
                    end
631
                end
632
            end
633
 
634
            // Write Data
635
            write_mem({Bank_addr, Rows_addr, Cols_addr}, Dq_buf);
636
 
637
            // tWR start and tWTR check
638
            if (Sys_clk && &dm_pair === 1'b0)  begin
639
                case (Bank_addr)
640
                    2'd0    : WR_chk0 = $time;
641
                    2'd1    : WR_chk1 = $time;
642
                    2'd2    : WR_chk2 = $time;
643
                    2'd3    : WR_chk3 = $time;
644
                    default : $display ("At time %t ERROR: Invalid Bank Address (tWR)", $time);
645
                endcase
646
 
647
                // tWTR check
648
                if (Read_enable === 1'b1) begin
649
                    $display ("At time %t ERROR: tWTR violation during Read", $time);
650
                end
651
            end
652
        end
653
    end
654
    endtask
655
 
656
    // Auto Precharge Calculation
657
    task Auto_Precharge_Calculation;
658
    begin
659
        // Precharge counter
660
        if (Read_precharge [0] === 1'b1 || Write_precharge [0] === 1'b1) begin
661
            Count_precharge [0] = Count_precharge [0] + 1;
662
        end
663
        if (Read_precharge [1] === 1'b1 || Write_precharge [1] === 1'b1) begin
664
            Count_precharge [1] = Count_precharge [1] + 1;
665
        end
666
        if (Read_precharge [2] === 1'b1 || Write_precharge [2] === 1'b1) begin
667
            Count_precharge [2] = Count_precharge [2] + 1;
668
        end
669
        if (Read_precharge [3] === 1'b1 || Write_precharge [3] === 1'b1) begin
670
            Count_precharge [3] = Count_precharge [3] + 1;
671
        end
672
 
673
        // Read with AutoPrecharge Calculation
674
        //      The device start internal precharge when:
675
        //          1.  Meet tRAS requirement
676
        //          2.  BL/2 cycles after command
677
        if ((Read_precharge[0] === 1'b1) && ($time - RAS_chk0 >= tRAS)) begin
678
            if (Count_precharge[0] >= burst_length/2) begin
679
                Pc_b0 = 1'b1;
680
                Act_b0 = 1'b0;
681
                RP_chk0 = $time;
682
                Read_precharge[0] = 1'b0;
683
            end
684
        end
685
        if ((Read_precharge[1] === 1'b1) && ($time - RAS_chk1 >= tRAS)) begin
686
            if (Count_precharge[1] >= burst_length/2) begin
687
                Pc_b1 = 1'b1;
688
                Act_b1 = 1'b0;
689
                RP_chk1 = $time;
690
                Read_precharge[1] = 1'b0;
691
            end
692
        end
693
        if ((Read_precharge[2] === 1'b1) && ($time - RAS_chk2 >= tRAS)) begin
694
            if (Count_precharge[2] >= burst_length/2) begin
695
                Pc_b2 = 1'b1;
696
                Act_b2 = 1'b0;
697
                RP_chk2 = $time;
698
                Read_precharge[2] = 1'b0;
699
            end
700
        end
701
        if ((Read_precharge[3] === 1'b1) && ($time - RAS_chk3 >= tRAS)) begin
702
            if (Count_precharge[3] >= burst_length/2) begin
703
                Pc_b3 = 1'b1;
704
                Act_b3 = 1'b0;
705
                RP_chk3 = $time;
706
                Read_precharge[3] = 1'b0;
707
            end
708
        end
709
 
710
        // Write with AutoPrecharge Calculation
711
        //      The device start internal precharge when:
712
        //          1.  Meet tRAS requirement
713
        //          2.  Write Latency PLUS BL/2 cycles PLUS tWR after Write command
714
 
715
        if ((Write_precharge[0] === 1'b1) && ($time - RAS_chk0 >= tRAS)) begin
716
            if ((Count_precharge[0] >= burst_length/2+1) && ($time - WR_chk0 >= tWR)) begin
717
                Pc_b0 = 1'b1;
718
                Act_b0 = 1'b0;
719
                RP_chk0 = $time;
720
                Write_precharge[0] = 1'b0;
721
            end
722
        end
723
        if ((Write_precharge[1] === 1'b1) && ($time - RAS_chk1 >= tRAS)) begin
724
            if ((Count_precharge[1] >= burst_length/2+1) && ($time - WR_chk1 >= tWR)) begin
725
                Pc_b1 = 1'b1;
726
                Act_b1 = 1'b0;
727
                RP_chk1 = $time;
728
                Write_precharge[1] = 1'b0;
729
            end
730
        end
731
        if ((Write_precharge[2] === 1'b1) && ($time - RAS_chk2 >= tRAS)) begin
732
            if ((Count_precharge[2] >= burst_length/2+1) && ($time - WR_chk2 >= tWR)) begin
733
                Pc_b2 = 1'b1;
734
                Act_b2 = 1'b0;
735
                RP_chk2 = $time;
736
                Write_precharge[2] = 1'b0;
737
            end
738
        end
739
        if ((Write_precharge[3] === 1'b1) && ($time - RAS_chk3 >= tRAS)) begin
740
            if ((Count_precharge[3] >= burst_length/2+1) && ($time - WR_chk3 >= tWR)) begin
741
                Pc_b3 = 1'b1;
742
                Act_b3 = 1'b0;
743
                RP_chk3 = $time;
744
                Write_precharge[3] = 1'b0;
745
            end
746
        end
747
    end
748
    endtask
749
 
750
    // DLL Counter
751
    task DLL_Counter;
752
    begin
753
        if (DLL_reset === 1'b1 && DLL_done === 1'b0) begin
754
            DLL_count = DLL_count + 1;
755
            if (DLL_count >= 200) begin
756
                DLL_done = 1'b1;
757
            end
758
        end
759
    end
760
    endtask
761
 
762
    // Control Logic
763
    task Control_Logic;
764
    begin
765
        // Auto Refresh
766
        if (Aref_enable === 1'b1) begin
767
            // Display DEBUG Message
768
            if (DEBUG) begin
769
                $display ("At time %t AREF : Auto Refresh", $time);
770
            end
771
 
772
            // Precharge to Auto Refresh
773
            if (($time - RP_chk0 < tRP) || ($time - RP_chk1 < tRP) ||
774
                ($time - RP_chk2 < tRP) || ($time - RP_chk3 < tRP)) begin
775
                $display ("At time %t ERROR: tRP violation during Auto Refresh", $time);
776
            end
777
 
778
            // LMR/EMR to Auto Refresh
779
            if ($time - MRD_chk < tMRD) begin
780
                $display ("At time %t ERROR: tMRD violation during Auto Refresh", $time);
781
            end
782
 
783
            // Auto Refresh to Auto Refresh
784
            if ($time - RFC_chk < tRFC) begin
785
                $display ("At time %t ERROR: tRFC violation during Auto Refresh", $time);
786
            end
787
 
788
            // Precharge to Auto Refresh
789
            if (Pc_b0 === 1'b0 || Pc_b1 === 1'b0 || Pc_b2 === 1'b0 || Pc_b3 === 1'b0) begin
790
                $display ("At time %t ERROR: All banks must be Precharged before Auto Refresh", $time);
791
                if (!no_halt) $stop (0);
792
            end else begin
793
                aref_count = aref_count + 1;
794
                RFC_chk = $time;
795
            end
796
        end
797
 
798
        // Extended Mode Register
799
        if (Ext_mode_enable === 1'b1) begin
800
            if (DEBUG) begin
801
                $display ("At time %t EMR  : Extended Mode Register", $time);
802
            end
803
 
804
            // Precharge to LMR/EMR
805
            if (($time - RP_chk0 < tRP) || ($time - RP_chk1 < tRP) ||
806
                ($time - RP_chk2 < tRP) || ($time - RP_chk3 < tRP)) begin
807
                $display ("At time %t ERROR: tRP violation during Extended Mode Register", $time);
808
            end
809
 
810
            // LMR/EMR to LMR/EMR
811
            if ($time - MRD_chk < tMRD) begin
812
                $display ("At time %t ERROR: tMRD violation during Extended Mode Register", $time);
813
            end
814
 
815
            // Auto Refresh to LMR/EMR
816
            if ($time - RFC_chk < tRFC) begin
817
                $display ("At time %t ERROR: tRFC violation during Extended Mode Register", $time);
818
            end
819
 
820
            // Precharge to LMR/EMR
821
            if (Pc_b0 === 1'b0 || Pc_b1 === 1'b0 || Pc_b2 === 1'b0 || Pc_b3 === 1'b0) begin
822
                $display ("At time %t ERROR: all banks must be Precharged before Extended Mode Register", $time);
823
                if (!no_halt) $stop (0);
824
            end else begin
825
                if (Addr[0] === 1'b0) begin
826
                    DLL_enable = 1'b1;
827
                    if (DEBUG) begin
828
                        $display ("At time %t EMR  : Enable DLL", $time);
829
                    end
830
                end else begin
831
                    DLL_enable = 1'b0;
832
                    if (DEBUG) begin
833
                        $display ("At time %t EMR  : Disable DLL", $time);
834
                    end
835
                end
836
                MRD_chk = $time;
837
            end
838
        end
839
 
840
        // Load Mode Register
841
        if (Mode_reg_enable === 1'b1) begin
842
            if (DEBUG) begin
843
                $display ("At time %t LMR  : Load Mode Register", $time);
844
            end
845
 
846
            // Precharge to LMR/EMR
847
            if (($time - RP_chk0 < tRP) || ($time - RP_chk1 < tRP) ||
848
                ($time - RP_chk2 < tRP) || ($time - RP_chk3 < tRP)) begin
849
                $display ("At time %t ERROR: tRP violation during Load Mode Register", $time);
850
            end
851
 
852
            // LMR/EMR to LMR/EMR
853
            if ($time - MRD_chk < tMRD) begin
854
                $display ("At time %t ERROR: tMRD violation during Load Mode Register", $time);
855
            end
856
 
857
            // Auto Refresh to LMR/EMR
858
            if ($time - RFC_chk < tRFC) begin
859
                $display ("At time %t ERROR: tRFC violation during Load Mode Register", $time);
860
            end
861
 
862
            // Precharge to LMR/EMR
863
            if (Pc_b0 === 1'b0 || Pc_b1 === 1'b0 || Pc_b2 === 1'b0 || Pc_b3 === 1'b0) begin
864
                $display ("At time %t ERROR: all banks must be Precharged before Load Mode Register", $time);
865
            end else begin
866
                // Register Mode
867
                Mode_reg = Addr;
868
 
869
                // DLL Reset
870
                if (DLL_enable === 1'b1 && Addr [8] === 1'b1) begin
871
                    DLL_reset = 1'b1;
872
                    DLL_done = 1'b0;
873
                    DLL_count = 0;
874
                end else if (DLL_enable === 1'b1 && DLL_reset === 1'b0 && Addr [8] === 1'b0) begin
875
                    $display ("At time %t ERROR: DLL is ENABLE: DLL RESET is required.", $time);
876
                end else if (DLL_enable === 1'b0 && Addr [8] === 1'b1) begin
877
                    $display ("At time %t ERROR: DLL is DISABLE: DLL RESET will be ignored.", $time);
878
                end
879
 
880
                // Burst Length
881
                case (Addr [2 : 0])
882
                    3'b001  : $display ("At time %t LMR  : Burst Length = 2", $time);
883
                    3'b010  : $display ("At time %t LMR  : Burst Length = 4", $time);
884
                    3'b011  : $display ("At time %t LMR  : Burst Length = 8", $time);
885
                    default : $display ("At time %t ERROR: Burst Length not supported", $time);
886
                endcase
887
 
888
                // CAS Latency
889
                case (Addr [6 : 4])
890
                    3'b010  : $display ("At time %t LMR  : CAS Latency = 2", $time);
891
                    3'b110  : $display ("At time %t LMR  : CAS Latency = 2.5", $time);
892
                    3'b011  : $display ("At time %t LMR  : CAS Latency = 3", $time);
893
                    default : $display ("At time %t ERROR: CAS Latency not supported", $time);
894
                endcase
895
 
896
                // Record current tMRD time
897
                MRD_chk = $time;
898
            end
899
        end
900
 
901
        // Activate Block
902
        if (Active_enable === 1'b1) begin
903
            if (!(power_up_done)) begin
904
                $display ("%m: at time %t ERROR: Power Up and Initialization Sequence not completed before executing Activate command", $time);
905
            end
906
            // Display DEBUG Message
907
            if (DEBUG) begin
908
                $display ("At time %t ACT  : Bank = %h, Row = %h", $time, Ba, Addr);
909
            end
910
 
911
            // Activate to Activate (different bank)
912
            if ((Prev_bank != Ba) && ($time - RRD_chk < tRRD)) begin
913
                $display ("At time %t ERROR: tRRD violation during Activate bank %h", $time, Ba);
914
            end
915
 
916
            // LMR/EMR to Activate
917
            if ($time - MRD_chk < tMRD) begin
918
                $display ("At time %t ERROR: tMRD violation during Activate bank %h", $time, Ba);
919
            end
920
 
921
            // AutoRefresh to Activate
922
            if ($time - RFC_chk < tRFC) begin
923
                $display ("At time %t ERROR: tRFC violation during Activate bank %h", $time, Ba);
924
            end
925
 
926
            // Precharge to Activate
927
            if ((Ba === 2'b00 && Pc_b0  === 1'b0) || (Ba === 2'b01 && Pc_b1  === 1'b0) ||
928
                (Ba === 2'b10 && Pc_b2  === 1'b0) || (Ba === 2'b11 && Pc_b3  === 1'b0)) begin
929
                $display ("At time %t ERROR: Bank = %h is already activated - Command Ignored", $time, Ba);
930
                if (!no_halt) $stop (0);
931
            end else begin
932
                // Activate Bank 0
933
                if (Ba === 2'b00 && Pc_b0 === 1'b1) begin
934
                    // Activate to Activate (same bank)
935
                    if ($time - RC_chk0 < tRC) begin
936
                        $display ("At time %t ERROR: tRC violation during Activate bank %h", $time, Ba);
937
                    end
938
 
939
                    // Precharge to Activate
940
                    if ($time - RP_chk0 < tRP) begin
941
                        $display ("At time %t ERROR: tRP violation during Activate bank %h", $time, Ba);
942
                    end
943
 
944
                    // Record variables for checking violation
945
                    Act_b0 = 1'b1;
946
                    Pc_b0 = 1'b0;
947
                    B0_row_addr = Addr;
948
                    RC_chk0  = $time;
949
                    RCD_chk0 = $time;
950
                    RAS_chk0 = $time;
951
                    RAP_chk0 = $time;
952
                end
953
 
954
                // Activate Bank 1
955
                if (Ba === 2'b01 && Pc_b1 === 1'b1) begin
956
                    // Activate to Activate (same bank)
957
                    if ($time - RC_chk1 < tRC) begin
958
                        $display ("At time %t ERROR: tRC violation during Activate bank %h", $time, Ba);
959
                    end
960
 
961
                    // Precharge to Activate
962
                    if ($time - RP_chk1 < tRP) begin
963
                        $display ("At time %t ERROR: tRP violation during Activate bank %h", $time, Ba);
964
                    end
965
 
966
                    // Record variables for checking violation
967
                    Act_b1 = 1'b1;
968
                    Pc_b1 = 1'b0;
969
                    B1_row_addr = Addr;
970
                    RC_chk1  = $time;
971
                    RCD_chk1 = $time;
972
                    RAS_chk1 = $time;
973
                    RAP_chk1 = $time;
974
                end
975
 
976
                // Activate Bank 2
977
                if (Ba === 2'b10 && Pc_b2 === 1'b1) begin
978
                    // Activate to Activate (same bank)
979
                    if ($time - RC_chk2 < tRC) begin
980
                        $display ("At time %t ERROR: tRC violation during Activate bank %h", $time, Ba);
981
                    end
982
 
983
                    // Precharge to Activate
984
                    if ($time - RP_chk2 < tRP) begin
985
                        $display ("At time %t ERROR: tRP violation during Activate bank %h", $time, Ba);
986
                    end
987
 
988
                    // Record variables for checking violation
989
                    Act_b2 = 1'b1;
990
                    Pc_b2 = 1'b0;
991
                    B2_row_addr = Addr;
992
                    RC_chk2  = $time;
993
                    RCD_chk2 = $time;
994
                    RAS_chk2 = $time;
995
                    RAP_chk2 = $time;
996
                end
997
 
998
                // Activate Bank 3
999
                if (Ba === 2'b11 && Pc_b3 === 1'b1) begin
1000
                    // Activate to Activate (same bank)
1001
                    if ($time - RC_chk3 < tRC) begin
1002
                        $display ("At time %t ERROR: tRC violation during Activate bank %h", $time, Ba);
1003
                    end
1004
 
1005
                    // Precharge to Activate
1006
                    if ($time - RP_chk3 < tRP) begin
1007
                        $display ("At time %t ERROR: tRP violation during Activate bank %h", $time, Ba);
1008
                    end
1009
 
1010
                    // Record variables for checking violation
1011
                    Act_b3 = 1'b1;
1012
                    Pc_b3 = 1'b0;
1013
                    B3_row_addr = Addr;
1014
                    RC_chk3  = $time;
1015
                    RCD_chk3 = $time;
1016
                    RAS_chk3 = $time;
1017
                    RAP_chk3 = $time;
1018
                end
1019
                // Record variable for checking violation
1020
                RRD_chk = $time;
1021
                Prev_bank = Ba;
1022
                read_precharge_truncation[Ba] = 1'b0;
1023
            end
1024
        end
1025
 
1026
        // Precharge Block - consider NOP if bank already precharged or in process of precharging
1027
        if (Prech_enable === 1'b1) begin
1028
            // Display DEBUG Message
1029
            if (DEBUG) begin
1030
                $display ("At time %t PRE  : Addr[10] = %b, Bank = %b", $time, Addr[10], Ba);
1031
            end
1032
 
1033
            // LMR/EMR to Precharge
1034
            if ($time - MRD_chk < tMRD) begin
1035
                $display ("At time %t ERROR: tMRD violation during Precharge", $time);
1036
            end
1037
 
1038
            // AutoRefresh to Precharge
1039
            if ($time - RFC_chk < tRFC) begin
1040
                $display ("At time %t ERROR: tRFC violation during Precharge", $time);
1041
            end
1042
 
1043
            // Precharge bank 0
1044
            if ((Addr[10] === 1'b1 || (Addr[10] === 1'b0 && Ba === 2'b00)) && Act_b0 === 1'b1) begin
1045
                Act_b0 = 1'b0;
1046
                Pc_b0 = 1'b1;
1047
                RP_chk0 = $time;
1048
 
1049
                // Activate to Precharge Bank
1050
                if ($time - RAS_chk0 < tRAS) begin
1051
                    $display ("At time %t ERROR: tRAS violation during Precharge", $time);
1052
                end
1053
 
1054
                // tWR violation check for Write
1055
                if ($time - WR_chk0 < tWR) begin
1056
                    $display ("At time %t ERROR: tWR violation during Precharge", $time);
1057
                end
1058
            end
1059
 
1060
            // Precharge bank 1
1061
            if ((Addr[10] === 1'b1 || (Addr[10] === 1'b0 && Ba === 2'b01)) && Act_b1 === 1'b1) begin
1062
                Act_b1 = 1'b0;
1063
                Pc_b1 = 1'b1;
1064
                RP_chk1 = $time;
1065
 
1066
                // Activate to Precharge Bank 1
1067
                if ($time - RAS_chk1 < tRAS) begin
1068
                    $display ("At time %t ERROR: tRAS violation during Precharge", $time);
1069
                end
1070
 
1071
                // tWR violation check for Write
1072
                if ($time - WR_chk1 < tWR) begin
1073
                    $display ("At time %t ERROR: tWR violation during Precharge", $time);
1074
                end
1075
            end
1076
 
1077
            // Precharge bank 2
1078
            if ((Addr[10] === 1'b1 || (Addr[10] === 1'b0 && Ba === 2'b10)) && Act_b2 === 1'b1) begin
1079
                Act_b2 = 1'b0;
1080
                Pc_b2 = 1'b1;
1081
                RP_chk2 = $time;
1082
 
1083
                // Activate to Precharge Bank 2
1084
                if ($time - RAS_chk2 < tRAS) begin
1085
                    $display ("At time %t ERROR: tRAS violation during Precharge", $time);
1086
                end
1087
 
1088
                // tWR violation check for Write
1089
                if ($time - WR_chk2 < tWR) begin
1090
                    $display ("At time %t ERROR: tWR violation during Precharge", $time);
1091
                end
1092
            end
1093
 
1094
            // Precharge bank 3
1095
            if ((Addr[10] === 1'b1 || (Addr[10] === 1'b0 && Ba === 2'b11)) && Act_b3 === 1'b1) begin
1096
                Act_b3 = 1'b0;
1097
                Pc_b3 = 1'b1;
1098
                RP_chk3 = $time;
1099
 
1100
                // Activate to Precharge Bank 3
1101
                if ($time - RAS_chk3 < tRAS) begin
1102
                    $display ("At time %t ERROR: tRAS violation during Precharge", $time);
1103
                end
1104
 
1105
                // tWR violation check for Write
1106
                if ($time - WR_chk3 < tWR) begin
1107
                    $display ("At time %t ERROR: tWR violation during Precharge", $time);
1108
                end
1109
            end
1110
 
1111
            // Prech_count is to make sure we have met part of the initialization sequence
1112
            Prech_count = Prech_count + 1;
1113
 
1114
            // Pipeline for READ
1115
            A10_precharge [cas_latency_x2] = Addr[10];
1116
            Bank_precharge[cas_latency_x2] = Ba;
1117
            Cmnd_precharge[cas_latency_x2] = 1'b1;
1118
        end
1119
 
1120
        // Burst terminate
1121
        if (Burst_term === 1'b1) begin
1122
            // Display DEBUG Message
1123
            if (DEBUG) begin
1124
                $display ("At time %t BST  : Burst Terminate",$time);
1125
            end
1126
 
1127
            if (Data_in_enable === 1'b1) begin
1128
                // Illegal to burst terminate a Write
1129
                $display ("At time %t ERROR: It's illegal to burst terminate a Write", $time);
1130
                if (!no_halt) $stop (0);
1131
            end else if (Read_precharge[0] === 1'b1 || Read_precharge[1] === 1'b1 ||
1132
                // Illegal to burst terminate a Read with Auto Precharge
1133
                Read_precharge[2] === 1'b1 || Read_precharge[3] === 1'b1) begin
1134
                $display ("At time %t ERROR: It's illegal to burst terminate a Read with Auto Precharge", $time);
1135
                if (!no_halt) $stop (0);
1136
            end else begin
1137
                // Burst Terminate Command Pipeline for Read
1138
                Cmnd_bst[cas_latency_x2] = 1'b1;
1139
            end
1140
 
1141
        end
1142
 
1143
        // Read Command
1144
        if (Read_enable === 1'b1) begin
1145
            if (!(power_up_done)) begin
1146
                $display ("%m: at time %t ERROR: Power Up and Initialization Sequence not completed before executing Read Command", $time);
1147
            end
1148
            // Check for DLL reset before Read
1149
            if (DLL_reset === 1 && DLL_done === 0) begin
1150
                $display ("%m: at time %t ERROR: You need to wait 200 tCK after DLL Reset Enable to Read, Not %0d clocks.", $time, DLL_count);
1151
            end
1152
            // Display DEBUG Message
1153
            if (DEBUG) begin
1154
                $display ("At time %t READ : Bank = %h, Col = %h", $time, Ba, {Addr [11], Addr [9 : 0]});
1155
            end
1156
 
1157
            // Terminate a Write
1158
            if (Data_in_enable === 1'b1) begin
1159
                Data_in_enable = 1'b0;
1160
            end
1161
 
1162
            // Activate to Read without Auto Precharge
1163
            if ((Addr [10] === 1'b0 && Ba === 2'b00 && $time - RCD_chk0 < tRCD) ||
1164
                (Addr [10] === 1'b0 && Ba === 2'b01 && $time - RCD_chk1 < tRCD) ||
1165
                (Addr [10] === 1'b0 && Ba === 2'b10 && $time - RCD_chk2 < tRCD) ||
1166
                (Addr [10] === 1'b0 && Ba === 2'b11 && $time - RCD_chk3 < tRCD)) begin
1167
                $display("At time %t ERROR: tRCD violation during Read", $time);
1168
            end
1169
 
1170
            // Activate to Read with Auto Precharge
1171
            if ((Addr [10] === 1'b1 && Ba === 2'b00 && $time - RAP_chk0 < tRAP) ||
1172
                (Addr [10] === 1'b1 && Ba === 2'b01 && $time - RAP_chk1 < tRAP) ||
1173
                (Addr [10] === 1'b1 && Ba === 2'b10 && $time - RAP_chk2 < tRAP) ||
1174
                (Addr [10] === 1'b1 && Ba === 2'b11 && $time - RAP_chk3 < tRAP)) begin
1175
                $display ("At time %t ERROR: tRAP violation during Read", $time);
1176
            end
1177
 
1178
            // Interrupt a Read with Auto Precharge (same bank only)
1179
            if (Read_precharge [Ba] === 1'b1) begin
1180
                $display ("At time %t ERROR: It's illegal to interrupt a Read with Auto Precharge", $time);
1181
                if (!no_halt) $stop (0);
1182
                // Cancel Auto Precharge
1183
                if (Addr[10] === 1'b0) begin
1184
                    Read_precharge [Ba]= 1'b0;
1185
                end
1186
            end
1187
            // Activate to Read
1188
            if ((Ba === 2'b00 && Pc_b0 === 1'b1) || (Ba === 2'b01 && Pc_b1 === 1'b1) ||
1189
                (Ba === 2'b10 && Pc_b2 === 1'b1) || (Ba === 2'b11 && Pc_b3 === 1'b1)) begin
1190
                $display("At time %t ERROR: Bank is not Activated for Read", $time);
1191
                if (!no_halt) $stop (0);
1192
            end else begin
1193
                // CAS Latency pipeline
1194
                Read_cmnd[cas_latency_x2] = 1'b1;
1195
                Read_bank[cas_latency_x2] = Ba;
1196
                Read_cols[cas_latency_x2] = {Addr [ADDR_BITS - 1 : 11], Addr [9 : 0]};
1197
                // Auto Precharge
1198
                if (Addr[10] === 1'b1) begin
1199
                    Read_precharge [Ba]= 1'b1;
1200
                    Count_precharge [Ba]= 0;
1201
                end
1202
            end
1203
        end
1204
 
1205
        // Write Command
1206
        if (Write_enable === 1'b1) begin
1207
            if (!(power_up_done)) begin
1208
                $display ("%m: at time %t ERROR: Power Up and Initialization Sequence not completed before executing Write Command", $time);
1209
                if (!no_halt) $stop (0);
1210
            end
1211
            // display DEBUG message
1212
            if (DEBUG) begin
1213
                $display ("At time %t WRITE: Bank = %h, Col = %h", $time, Ba, {Addr [ADDR_BITS - 1 : 11], Addr [9 : 0]});
1214
            end
1215
 
1216
            // Activate to Write
1217
            if ((Ba === 2'b00 && $time - RCD_chk0 < tRCD) ||
1218
                (Ba === 2'b01 && $time - RCD_chk1 < tRCD) ||
1219
                (Ba === 2'b10 && $time - RCD_chk2 < tRCD) ||
1220
                (Ba === 2'b11 && $time - RCD_chk3 < tRCD)) begin
1221
                $display("At time %t ERROR: tRCD violation during Write to Bank %h", $time, Ba);
1222
            end
1223
 
1224
            // Read to Write
1225
            if (Read_cmnd[0] || Read_cmnd[1] || Read_cmnd[2] || Read_cmnd[3] ||
1226
                Read_cmnd[4] || Read_cmnd[5] || Read_cmnd[6] || (Burst_counter < burst_length)) begin
1227
                if (Data_out_enable || read_precharge_truncation[Ba]) begin
1228
                    $display("At time %t ERROR: Read to Write violation", $time);
1229
                end
1230
            end
1231
 
1232
            // Interrupt a Write with Auto Precharge (same bank only)
1233
            if (Write_precharge [Ba] === 1'b1) begin
1234
                $display ("At time %t ERROR: it's illegal to interrupt a Write with Auto Precharge", $time);
1235
                if (!no_halt) $stop (0);
1236
                // Cancel Auto Precharge
1237
                if (Addr[10] === 1'b0) begin
1238
                    Write_precharge [Ba]= 1'b0;
1239
                end
1240
            end
1241
            // Activate to Write
1242
            if ((Ba === 2'b00 && Pc_b0 === 1'b1) || (Ba === 2'b01 && Pc_b1 === 1'b1) ||
1243
                (Ba === 2'b10 && Pc_b2 === 1'b1) || (Ba === 2'b11 && Pc_b3 === 1'b1)) begin
1244
                $display("At time %t ERROR: Bank is not Activated for Write", $time);
1245
                if (!no_halt) $stop (0);
1246
            end else begin
1247
                // Pipeline for Write
1248
                Write_cmnd [3] = 1'b1;
1249
                Write_bank [3] = Ba;
1250
                Write_cols [3] = {Addr [ADDR_BITS - 1 : 11], Addr [9 : 0]};
1251
                // Auto Precharge
1252
                if (Addr[10] === 1'b1) begin
1253
                    Write_precharge [Ba]= 1'b1;
1254
                    Count_precharge [Ba]= 0;
1255
                end
1256
            end
1257
        end
1258
    end
1259
    endtask
1260
 
1261
    task check_neg_dqs;
1262
    begin
1263
        if (Write_cmnd[2] || Write_cmnd[1] || Data_in_enable) begin
1264
            for (i=0; i<DQS_BITS; i=i+1) begin
1265
                if (expect_neg_dqs[i]) begin
1266
                    $display ("At time %t ERROR: Negative DQS[%1d] transition required.", $time, i);
1267
                end
1268
                expect_neg_dqs[i] = 1'b1;
1269
            end
1270
        end else begin
1271
            expect_pos_dqs = 0;
1272
            expect_neg_dqs = 0;
1273
        end
1274
    end
1275
    endtask
1276
 
1277
    task check_pos_dqs;
1278
    begin
1279
        if (Write_cmnd[2] || Write_cmnd[1] || Data_in_enable) begin
1280
            for (i=0; i<DQS_BITS; i=i+1) begin
1281
                if (expect_pos_dqs[i]) begin
1282
                    $display ("At time %t ERROR: Positive DQS[%1d] transition required.", $time, i);
1283
                end
1284
                expect_pos_dqs[i] = 1'b1;
1285
            end
1286
        end else begin
1287
            expect_pos_dqs = 0;
1288
            expect_neg_dqs = 0;
1289
        end
1290
    end
1291
    endtask
1292
 
1293
    // Main Logic
1294
    always @ (posedge Sys_clk) begin
1295
        Manual_Precharge_Pipeline;
1296
        Burst_Terminate_Pipeline;
1297
        Dq_Dqs_Drivers;
1298
        Write_FIFO_DM_Mask_Logic;
1299
        Burst_Decode;
1300
        check_neg_dqs;
1301
        Auto_Precharge_Calculation;
1302
        DLL_Counter;
1303
        Control_Logic;
1304
    end
1305
 
1306
    always @ (negedge Sys_clk) begin
1307
        Manual_Precharge_Pipeline;
1308
        Burst_Terminate_Pipeline;
1309
        Dq_Dqs_Drivers;
1310
        Write_FIFO_DM_Mask_Logic;
1311
        Burst_Decode;
1312
        check_pos_dqs;
1313
    end
1314
 
1315
    // Dqs Receiver
1316
    always @ (posedge Dqs_in[0]) begin
1317
        // Latch data at posedge Dqs
1318
        dq_rise[7 : 0] = Dq_in[7 : 0];
1319
        dm_rise[0] = Dm_in[0];
1320
        expect_pos_dqs[0] = 0;
1321
    end
1322
 
1323
    always @ (posedge Dqs_in[1]) begin
1324
        // Latch data at posedge Dqs
1325
        dq_rise[15 : 8] = Dq_in[15 : 8];
1326
        dm_rise[1] = Dm_in [1];
1327
        expect_pos_dqs[1] = 0;
1328
    end
1329
 
1330
    always @ (posedge Dqs_in[2]) begin
1331
        // Latch data at posedge Dqs
1332
        dq_rise[23 : 16] = Dq_in[23 : 16];
1333
        dm_rise[2] = Dm_in [2];
1334
        expect_pos_dqs[2] = 0;
1335
    end
1336
 
1337
    always @ (posedge Dqs_in[3]) begin
1338
        // Latch data at posedge Dqs
1339
        dq_rise[31 : 24] = Dq_in[31 : 24];
1340
        dm_rise[3] = Dm_in [3];
1341
        expect_pos_dqs[3] = 0;
1342
    end
1343
 
1344
    always @ (negedge Dqs_in[0]) begin
1345
        // Latch data at negedge Dqs
1346
        dq_fall[7 : 0] = Dq_in[7 : 0];
1347
        dm_fall[0] = Dm_in[0];
1348
        dm_pair[1:0]  = {dm_rise[0], dm_fall[0]};
1349
        expect_neg_dqs[0] = 0;
1350
    end
1351
 
1352
    always @ (negedge Dqs_in[1]) begin
1353
        // Latch data at negedge Dqs
1354
        dq_fall[15: 8] = Dq_in[15 : 8];
1355
        dm_fall[1] = Dm_in[1];
1356
        dm_pair[3:2]  = {dm_rise[1], dm_fall[1]};
1357
        expect_neg_dqs[1] = 0;
1358
    end
1359
 
1360
    always @ (negedge Dqs_in[2]) begin
1361
        // Latch data at negedge Dqs
1362
        dq_fall[23: 16] = Dq_in[23 : 16];
1363
        dm_fall[2] = Dm_in[2];
1364
        dm_pair[5:4]  = {dm_rise[2], dm_fall[2]};
1365
        expect_neg_dqs[2] = 0;
1366
    end
1367
 
1368
    always @ (negedge Dqs_in[3]) begin
1369
        // Latch data at negedge Dqs
1370
        dq_fall[31: 24] = Dq_in[31 : 24];
1371
        dm_fall[3] = Dm_in[3];
1372
        dm_pair[7:6]  = {dm_rise[3], dm_fall[3]};
1373
        expect_neg_dqs[3] = 0;
1374
    end
1375
 
1376
    specify
1377
                                              // SYMBOL UNITS DESCRIPTION
1378
                                              // ------ ----- -----------
1379
`ifdef sg5B                                   //              specparams for -5B (CL = 3)
1380
        specparam tDSS             =     1.0; // tDSS   ns    DQS falling edge to CLK rising (setup time) = 0.2*tCK
1381
        specparam tDSH             =     1.0; // tDSH   ns    DQS falling edge from CLK rising (hold time) = 0.2*tCK
1382
        specparam tIH              =   0.750; // tIH    ns    Input Hold Time
1383
        specparam tIS              =   0.750; // tIS    ns    Input Setup Time
1384
        specparam tDQSH            =    1.75; // tDQSH  ns    DQS input High Pulse Width = 0.35*tCK
1385
        specparam tDQSL            =    1.75; // tDQSL  ns    DQS input Low Pulse Width = 0.35*tCK
1386
`else `ifdef sg6                              //              specparams for -6 (CL = 2.5)
1387
        specparam tDSS             =     1.2; // tDSS   ns    DQS falling edge to CLK rising (setup time) = 0.2*tCK
1388
        specparam tDSH             =     1.2; // tDSH   ns    DQS falling edge from CLK rising (hold time) = 0.2*tCK
1389
        specparam tIH              =   0.750; // tIH    ns    Input Hold Time
1390
        specparam tIS              =   0.750; // tIS    ns    Input Setup Time
1391
        specparam tDQSH            =     2.1; // tDQSH  ns    DQS input High Pulse Width = 0.35*tCK
1392
        specparam tDQSL            =     2.1; // tDQSL  ns    DQS input Low Pulse Width = 0.35*tCK
1393
`else `ifdef sg6T                             //              specparams for -6 (CL = 2.5)
1394
        specparam tDSS             =     1.2; // tDSS   ns    DQS falling edge to CLK rising (setup time) = 0.2*tCK
1395
        specparam tDSH             =     1.2; // tDSH   ns    DQS falling edge from CLK rising (hold time) = 0.2*tCK
1396
        specparam tIH              =   0.750; // tIH    ns    Input Hold Time
1397
        specparam tIS              =   0.750; // tIS    ns    Input Setup Time
1398
        specparam tDQSH            =     2.1; // tDQSH  ns    DQS input High Pulse Width = 0.35*tCK
1399
        specparam tDQSL            =     2.1; // tDQSL  ns    DQS input Low Pulse Width = 0.35*tCK
1400
`else `ifdef sg75                             //              specparams for -75E (CL = 2)
1401
        specparam tDSS             =     1.5; // tDSS   ns    DQS falling edge to CLK rising (setup time) = 0.2*tCK
1402
        specparam tDSH             =     1.5; // tDSH   ns    DQS falling edge from CLK rising (hold time) = 0.2*tCK
1403
        specparam tIH              =   0.900; // tIH    ns    Input Hold Time
1404
        specparam tIS              =   0.900; // tIS    ns    Input Setup Time
1405
        specparam tDQSH            =   2.625; // tDQSH  ns    DQS input High Pulse Width = 0.35*tCK
1406
        specparam tDQSL            =   2.625; // tDQSL  ns    DQS input Low Pulse Width = 0.35*tCK
1407
`else `ifdef sg75E                            //              specparams for -75E (CL = 2)
1408
        specparam tDSS             =     1.5; // tDSS   ns    DQS falling edge to CLK rising (setup time) = 0.2*tCK
1409
        specparam tDSH             =     1.5; // tDSH   ns    DQS falling edge from CLK rising (hold time) = 0.2*tCK
1410
        specparam tIH              =   0.900; // tIH    ns    Input Hold Time
1411
        specparam tIS              =   0.900; // tIS    ns    Input Setup Time
1412
        specparam tDQSH            =   2.625; // tDQSH  ns    DQS input High Pulse Width = 0.35*tCK
1413
        specparam tDQSL            =   2.625; // tDQSL  ns    DQS input Low Pulse Width = 0.35*tCK
1414
`else `define sg75Z                           //              specparams for -75Z (CL = 2)
1415
        specparam tDSS             =     1.5; // tDSS   ns    DQS falling edge to CLK rising (setup time) = 0.2*tCK
1416
        specparam tDSH             =     1.5; // tDSH   ns    DQS falling edge from CLK rising (hold time) = 0.2*tCK
1417
        specparam tIH              =   0.900; // tIH    ns    Input Hold Time
1418
        specparam tIS              =   0.900; // tIS    ns    Input Setup Time
1419
        specparam tDQSH            =   2.625; // tDQSH  ns    DQS input High Pulse Width = 0.35*tCK
1420
        specparam tDQSL            =   2.625; // tDQSL  ns    DQS input Low Pulse Width = 0.35*tCK
1421
`endif `endif `endif `endif `endif
1422
        $width    (posedge Dqs_in[0] &&& wdqs_valid, tDQSH);
1423
        $width    (posedge Dqs_in[1] &&& wdqs_valid, tDQSH);
1424
        $width    (negedge Dqs_in[0] &&& wdqs_valid, tDQSL);
1425
        $width    (negedge Dqs_in[1] &&& wdqs_valid, tDQSL);
1426
        $setuphold(posedge Clk,   Cke,   tIS, tIH);
1427
        $setuphold(posedge Clk,   Cs_n,  tIS, tIH);
1428
        $setuphold(posedge Clk,   Cas_n, tIS, tIH);
1429
        $setuphold(posedge Clk,   Ras_n, tIS, tIH);
1430
        $setuphold(posedge Clk,   We_n,  tIS, tIH);
1431
        $setuphold(posedge Clk,   Addr,  tIS, tIH);
1432
        $setuphold(posedge Clk,   Ba,    tIS, tIH);
1433
        $setuphold(posedge Clk, negedge Dqs &&& wdqs_valid, tDSS, tDSH);
1434
    endspecify
1435
 
1436
endmodule

powered by: WebSVN 2.1.0

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