memctrl16.v 78.4 KB
Newer Older
1 2 3
/*******************************************************************************
 * Module: memctrl16
 * Date:2015-01-10  
4
 * Author: Andrey Filippov     
5 6
 * Description: 16-channel memory controller
 *
7
 * Copyright (c) 2015 Elphel, Inc.
8 9 10 11 12 13 14 15 16 17 18 19
 * memctrl16.v is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  memctrl16.v is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/> .
20 21 22 23 24 25
 *
 * Additional permission under GNU GPL version 3 section 7:
 * If you modify this Program, or any covered work, by linking or combining it
 * with independent modules provided by the FPGA vendor only (this permission
 * does not extend to any 3-rd party modules, "soft cores" or macros) under
 * different license terms solely for the purpose of generating binary "bitstream"
26
 * files and/or simulating the code, the copyright holders of this Program give
27 28
 * you the right to distribute the covered work without those independent modules
 * as long as the source code for them is available from the FPGA vendor free of
Andrey Filippov's avatar
Andrey Filippov committed
29
 * charge, and there is no dependence on any encrypted modules for simulating of
30 31 32
 * the combined code. This permission applies to you if the distributed code
 * contains all the components and scripts required to completely simulate it
 * with at least one of the Free Software programs.
33 34
 *******************************************************************************/
`timescale 1ns/1ps
35
`include "system_defines.vh" 
36
//`define use200Mhz 1
37
//`define DEBUG_FIFO 1 
38
module  memctrl16 #(
39 40
//command interface parameters
    parameter DLY_LD =            'h080,  // address to generate delay load
41
    parameter DLY_LD_MASK =       'h780,  // address mask to generate delay load
42 43
//0x1000..103f - 0- bit data (set/reset)
    parameter MCONTR_PHY_0BIT_ADDR =           'h020,  // address to set sequnecer channel and  run (4 LSB-s - channel)
44
    parameter MCONTR_PHY_0BIT_ADDR_MASK =      'h7f0,  // address mask to generate sequencer channel/run
45 46 47 48 49 50 51 52 53 54 55 56
//  0x1020       - DLY_SET      // 0 bits -set pre-programmed delays 
//  0x1024..1025 - CMDA_EN      // 0 bits - enable/disable command/address outputs 
//  0x1026..1027 - SDRST_ACT    // 0 bits - enable/disable active-low reset signal to DDR3 memory
//  0x1028..1029 - CKE_EN       // 0 bits - enable/disable CKE signal to memory 
//  0x102a..102b - DCI_RST      // 0 bits - enable/disable CKE signal to memory 
//  0x102c..102d - DLY_RST      // 0 bits - enable/disable CKE signal to memory 
    parameter MCONTR_PHY_0BIT_DLY_SET =        'h0,    // set pre-programmed delays 
    parameter MCONTR_PHY_0BIT_CMDA_EN =        'h4,    // enable/disable command/address outputs 
    parameter MCONTR_PHY_0BIT_SDRST_ACT =      'h6,    // enable/disable active-low reset signal to DDR3 memory
    parameter MCONTR_PHY_0BIT_CKE_EN =         'h8,    // enable/disable CKE signal to memory 
    parameter MCONTR_PHY_0BIT_DCI_RST =        'ha,    // enable/disable CKE signal to memory 
    parameter MCONTR_PHY_0BIT_DLY_RST =        'hc,    // enable/disable CKE signal to memory
57 58
//0x1030..1037 - 0-bit memory cotroller (set/reset)
    parameter MCONTR_TOP_0BIT_ADDR =           'h030,  // address to turn on/off memory controller features
59
    parameter MCONTR_TOP_0BIT_ADDR_MASK =      'h7f8,  // address mask to generate sequencer channel/run
60 61 62 63 64
//  0x1030..1031 - MCONTR_EN  // 0 bits, disable/enable memory controller
//  0x1032..1033 - REFRESH_EN // 0 bits, disable/enable memory refresh
//  0x1034..1037 - reserved
    parameter MCONTR_TOP_0BIT_MCONTR_EN =      'h0,    // set pre-programmed delays 
    parameter MCONTR_TOP_0BIT_REFRESH_EN =     'h2,    // disable/enable command/address outputs 
65 66 67
//0x1040..107f - 16-bit data
//  0x1040..104f - RUN_CHN      // address to set sequncer channel and  run (4 LSB-s - channel) - bits? 
//    parameter RUN_CHN_REL =           'h040,  // address to set sequnecer channel and  run (4 LSB-s - channel)
68
//   parameter RUN_CHN_REL_MASK =      'h7f0,  // address mask to generate sequencer channel/run
69 70
//  0x1050..1057: MCONTR_PHY16
    parameter MCONTR_PHY_16BIT_ADDR =           'h050,  // address to set sequnecer channel and  run (4 LSB-s - channel)
71
    parameter MCONTR_PHY_16BIT_ADDR_MASK =      'h7f8,  // address mask to generate sequencer channel/run
72 73 74 75 76 77 78 79 80 81
//  0x1050       - PATTERNS     // 16 bits
//  0x1051       - PATTERNS_TRI // 16-bit address to set DQM and DQS tristate on/off patterns {dqs_off,dqs_on, dq_off,dq_on} - 4 bits each 
//  0x1052       - WBUF_DELAY   // 4 bits - extra delay (in mclk cycles) to add to write buffer enable (DDR3 read data)
//  0x1053       - EXTRA_REL    // 1 bit - set extra parameters (currently just inv_clk_div)
//  0x1054       - STATUS_CNTRL // 8 bits - write to status control
    parameter MCONTR_PHY_16BIT_PATTERNS =       'h0,    // set DQM and DQS patterns (16'h0055)
    parameter MCONTR_PHY_16BIT_PATTERNS_TRI =   'h1,    // 16-bit address to set DQM and DQS tristate on/off patterns {dqs_off,dqs_on, dq_off,dq_on} - 4 bits each 
    parameter MCONTR_PHY_16BIT_WBUF_DELAY =     'h2,    // 4? bits - extra delay (in mclk cycles) to add to write buffer enable (DDR3 read data)
    parameter MCONTR_PHY_16BIT_EXTRA =          'h3,    // ? bits - set extra parameters (currently just inv_clk_div)
    parameter MCONTR_PHY_STATUS_CNTRL =         'h4,    // write to status control (8-bit)
Andrey Filippov's avatar
Andrey Filippov committed
82 83
   
//0x1060..106f: arbiter priority data
84
    parameter MCONTR_ARBIT_ADDR =               'h060,   // Address to set channel priorities
85
    parameter MCONTR_ARBIT_ADDR_MASK =          'h7f0,   // Address mask to set channel priorities
86 87
//0x1070..1077 - 16-bit top memory controller:
    parameter MCONTR_TOP_16BIT_ADDR =           'h070,  // address to set mcontr top control registers
88
    parameter MCONTR_TOP_16BIT_ADDR_MASK =      'h7f8,  // address mask to set mcontr top control registers
89 90 91 92 93 94 95 96
//  0x1070       - MCONTR_CHN_EN     // 16 bits per-channel enable (want/need requests)
//  0x1071       - REFRESH_PERIOD    // 8-bit refresh period
//  0x1072       - REFRESH_ADDRESS   // 10 bits
//  0x1073       - STATUS_CNTRL      // 8 bits - write to status control (and debug?)
    parameter MCONTR_TOP_16BIT_CHN_EN =         'h0,    // 16 bits per-channel enable (want/need requests)
    parameter MCONTR_TOP_16BIT_REFRESH_PERIOD = 'h1,    // 8-bit refresh period
    parameter MCONTR_TOP_16BIT_REFRESH_ADDRESS= 'h2,    // 10 bits refresh address in the sequencer (PL) memory
    parameter MCONTR_TOP_16BIT_STATUS_CNTRL=    'h3,    // 8 bits - write to status control (and debug?)
Andrey Filippov's avatar
Andrey Filippov committed
97
    
98 99
// Status read address
    parameter MCONTR_PHY_STATUS_REG_ADDR=      'h0,    // 8 or less bits: status register address to use for memory controller phy
100 101 102
    parameter MCONTR_TOP_STATUS_REG_ADDR=      'h1,    // 8 or less bits: status register address to use for memory controller
    
    
103
    parameter CHNBUF_READ_LATENCY =             2,     // external channel buffer extra read latency ( 0 - data available next cycle after re (but prev. data))
104 105
    
    parameter DFLT_DQS_PATTERN=        8'h55,
106
    parameter DFLT_DQM_PATTERN=        8'h00, // 8'h00
107
    parameter DFLT_DQ_TRI_ON_PATTERN=  4'h7,  // DQ tri-state control word, first when enabling output
108 109 110
    parameter DFLT_DQ_TRI_OFF_PATTERN= 4'he,  // DQ tri-state control word, first after disabling output
    parameter DFLT_DQS_TRI_ON_PATTERN= 4'h3,  // DQS tri-state control word, first when enabling output
    parameter DFLT_DQS_TRI_OFF_PATTERN=4'hc,  // DQS tri-state control word, first after disabling output
111
    parameter DFLT_WBUF_DELAY=         4'h8,  // write levelling - 7!
112
    parameter DFLT_INV_CLK_DIV=        1'b0,
113 114 115 116 117
    
    parameter DFLT_CHN_EN=            16'h0,  // channel mask to be enabled at reset
    parameter DFLT_REFRESH_ADDR=      10'h0,  // refresh sequence address in command memory
    parameter DFLT_REFRESH_PERIOD=     8'h0,  // default 8-bit refresh period (scale?)

118 119

    parameter integer ADDRESS_NUMBER=15, 
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    parameter PHASE_WIDTH =     8,
    parameter SLEW_DQ =         "SLOW",
    parameter SLEW_DQS =        "SLOW",
    parameter SLEW_CMDA =       "SLOW",
    parameter SLEW_CLK =        "SLOW",
    parameter IBUF_LOW_PWR =    "TRUE",
`ifdef use200Mhz
    parameter real REFCLK_FREQUENCY = 200.0, // 300.0,
    parameter HIGH_PERFORMANCE_MODE = "FALSE",
    parameter CLKIN_PERIOD          = 20, // 10, //ns >1.25, 600<Fvco<1200 // Hardware 150MHz , change to             | 6.667
    parameter CLKFBOUT_MULT =       16,   // 8, // Fvco=Fclkin*CLKFBOUT_MULT_F/DIVCLK_DIVIDE, Fout=Fvco/CLKOUT#_DIVIDE  | 16
`else
    parameter real REFCLK_FREQUENCY = 300.0,
    parameter HIGH_PERFORMANCE_MODE = "FALSE",
    parameter CLKIN_PERIOD          = 10, //ns >1.25, 600<Fvco<1200
    parameter CLKFBOUT_MULT =       8, // Fvco=Fclkin*CLKFBOUT_MULT_F/DIVCLK_DIVIDE, Fout=Fvco/CLKOUT#_DIVIDE
`endif    
    parameter DIVCLK_DIVIDE=        1,
138
    parameter CLKFBOUT_USE_FINE_PS= 1, // 0 - old, 1 - new 
139 140 141 142 143 144 145 146 147 148
    parameter CLKFBOUT_PHASE =      0.000,
    parameter SDCLK_PHASE =         0.000,
    parameter CLK_PHASE =           0.000,
    parameter CLK_DIV_PHASE =       0.000,
    parameter MCLK_PHASE =          90.000,
    parameter REF_JITTER1 =         0.010,
    parameter SS_EN =              "FALSE",
    parameter SS_MODE =      "CENTER_HIGH",
    parameter SS_MOD_PERIOD =       10000,
    parameter CMD_PAUSE_BITS=       10,
149
    parameter CMD_DONE_BIT=         10
150
    ) (
151
    input                        rst_in,
152
    input                        clk_in,
Andrey Filippov's avatar
Andrey Filippov committed
153
    output                       mclk,     // global clock, half DDR3 clock, synchronizes all I/O through the command port
Andrey Filippov's avatar
Andrey Filippov committed
154 155
    input                        mrst,     // @posedge mclk synchronous reset - should not interrupt mclk generation
    output                       locked,   // to generate sync reset
156
    input                        ref_clk,  // global clock for idelay_ctrl calibration
Andrey Filippov's avatar
Andrey Filippov committed
157
    output                       idelay_ctrl_reset,
158 159 160 161 162 163 164
    // programming interface
    input                  [7:0] cmd_ad,      // byte-serial command address/data (up to 6 bytes: AL-AH-D0-D1-D2-D3 
    input                        cmd_stb,     // strobe (with first byte) for the command a/d
    output                 [7:0] status_ad,   // status address/data - up to 5 bytes: A - {seq,status[1:0]} - status[2:9] - status[10:17] - status[18:25]
    output                       status_rq,   // input request to send status downstream
    input                        status_start, // Acknowledge of the first status packet byte (address)
    
Andrey Filippov's avatar
Andrey Filippov committed
165 166 167 168 169
// command port 0 (filled by software - 32w->32r) - used for mode set, refresh, write levelling, ...
    input                        cmd0_clk,    // clock to write commend sequencer from PS
    input                        cmd0_we,     // write enble to write commend sequencer from PS
    input                [9:0]   cmd0_addr,   // address write commend sequencer from PS
    input               [31:0]   cmd0_data,   // data to write commend sequencer from PS
170 171 172 173

    input                 [31:0] seq_data,  //16x32 data to be written to the sequencer (and start address for software-based sequencer)
    input                        seq_wr,    // strobe for writing sequencer data (address is autoincremented)
    input                        seq_set,   // channel sequencer data is written. If no seq_wr pulses before seq_set, seq_data contains software sequencer start address
Andrey Filippov's avatar
Andrey Filippov committed
174
    
175 176 177 178
// channel interfaces TODO: move request/grant here, add "done"
// channel 0 interface 
`ifdef def_enable_mem_chn0
    input                        want_rq0,   // both want_rq and need_rq should go inactive after being granted  
179
    input                        need_rq0,   // want_rq should be active when need_rq is.
Andrey Filippov's avatar
Andrey Filippov committed
180
    output reg                   channel_pgm_en0, // channel can program sequence data
181
    input                        reject0,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
182
    output                       seq_done0,  // sequencer finished executing sequence for this channel 
183 184
    output                       page_nxt_chn0,
    output                       buf_run0,   // external buffer run (may be used to force page) @posedge mclk
185
  `ifdef def_read_mem_chn0
186
    output                       buf_wr_chn0,   // @ negedge mclk
187
    output                       buf_wpage_nxt_chn0, // @ negedge mclk
188
    output                [63:0] buf_wdata_chn0, // @ negedge mclk
189 190 191
    output                       buf_wrun0,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn0
192
    output                       buf_rd_chn0,
193
    output                       buf_rpage_nxt_chn0,
194 195 196 197 198 199 200 201
    input                 [63:0] buf_rdata_chn0,
  `endif
`endif    

// channel 1 interface 
`ifdef def_enable_mem_chn1
    input                        want_rq1,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq1,
Andrey Filippov's avatar
Andrey Filippov committed
202
    output reg                   channel_pgm_en1, // channel can program sequence data
203
    input                        reject1,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
204
    output                       seq_done1,  // sequencer finished executing sequence for this channel 
205 206
    output                       page_nxt_chn1,
    output                       buf_run1,   // external buffer run (may be used to force page) @posedge mclk
207
  `ifdef def_read_mem_chn1
208
    output                       buf_wr_chn1,   // @ negedge mclk
209
    output                       buf_wpage_nxt_chn1,// @ negedge mclk
210
    output                [63:0] buf_wdata_chn1,// @ negedge mclk
211 212 213
    output                       buf_wrun1,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn1
214
    output                       buf_rd_chn1,
215
    output                       buf_rpage_nxt_chn1,
216 217 218 219 220 221 222 223
    input                 [63:0] buf_rdata_chn1,
  `endif
`endif    
    
// channel 2 interface 
`ifdef def_enable_mem_chn2
    input                        want_rq2,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq2,
Andrey Filippov's avatar
Andrey Filippov committed
224
    output reg                   channel_pgm_en2, // channel can program sequence data
225
    input                        reject2,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
226
    output                       seq_done2,  // sequencer finished executing sequence for this channel 
227 228
    output                       page_nxt_chn2,
    output                       buf_run2,   // external buffer run (may be used to force page) @posedge mclk
229 230
  `ifdef def_read_mem_chn2
    output                       buf_wr_chn2,
231
    output                       buf_wpage_nxt_chn2,
232
    output                [63:0] buf_wdata_chn2,
233 234 235
    output                       buf_wrun2,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn2
236
    output                       buf_rd_chn2,
237
    output                       buf_rpage_nxt_chn2,
238 239 240 241 242 243 244 245
    input                 [63:0] buf_rdata_chn2,
  `endif
`endif    

// channel 3 interface 
`ifdef def_enable_mem_chn3
    input                        want_rq3,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq3,
Andrey Filippov's avatar
Andrey Filippov committed
246
    output reg                   channel_pgm_en3, // channel can program sequence data
247
    input                        reject3,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
248
    output                       seq_done3,  // sequencer finished executing sequence for this channel 
249 250
    output                       page_nxt_chn3,
    output                       buf_run3,   // external buffer run (may be used to force page) @posedge mclk
251 252
  `ifdef def_read_mem_chn3
    output                       buf_wr_chn3,
253
    output                       buf_wpage_nxt_chn3,
254
    output                [63:0] buf_wdata_chn3,
255 256 257
    output                       buf_wrun3,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn3
258
    output                       buf_rd_chn3,
259
    output                       buf_rpage_nxt_chn3,
260 261 262 263 264 265 266 267
    input                 [63:0] buf_rdata_chn3,
  `endif
`endif    

// channel 4 interface 
`ifdef def_enable_mem_chn4
    input                        want_rq4,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq4,
Andrey Filippov's avatar
Andrey Filippov committed
268
    output reg                   channel_pgm_en4, // channel can program sequence data
269
    input                        reject4,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
270
    output                       seq_done4,  // sequencer finished executing sequence for this channel 
271 272
    output                       page_nxt_chn4,
    output                       buf_run4,   // external buffer run (may be used to force page) @posedge mclk
273
  `ifdef def_read_mem_chn4
274
    output                       buf_wr_chn4,   // @ negedge mclk
275
    output                       buf_wpage_nxt_chn4,   // @ negedge mclk
276
    output                [63:0] buf_wdata_chn4,   // @ negedge mclk
277 278 279
    output                       buf_wrun4,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn4
280
    output                       buf_rd_chn4,
281
    output                       buf_rpage_nxt_chn4,
282 283 284 285 286 287 288 289
    input                 [63:0] buf_rdata_chn4,
  `endif
`endif    

// channel 5 interface 
`ifdef def_enable_mem_chn5
    input                        want_rq5,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq5,
Andrey Filippov's avatar
Andrey Filippov committed
290
    output reg                   channel_pgm_en5, // channel can program sequence data
291
    input                        reject5,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
292
    output                       seq_done5,  // sequencer finished executing sequence for this channel 
293 294
    output                       page_nxt_chn5,
    output                       buf_run5,   // external buffer run (may be used to force page) @posedge mclk
295
  `ifdef def_read_mem_chn5
296
    output                       buf_wr_chn5,   // @ negedge mclk
297
    output                       buf_wpage_nxt_chn5,   // @ negedge mclk
298
    output                [63:0] buf_wdata_chn5,   // @ negedge mclk
299 300 301
    output                       buf_wrun5,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn5
302
    output                       buf_rd_chn5,
303
    output                       buf_rpage_nxt_chn5,
304 305 306 307 308 309 310 311
    input                 [63:0] buf_rdata_chn5,
  `endif
`endif    

// channel 6 interface 
`ifdef def_enable_mem_chn6
    input                        want_rq6,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq6,
Andrey Filippov's avatar
Andrey Filippov committed
312
    output reg                   channel_pgm_en6, // channel can program sequence data
313
    input                        reject6,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
314
    output                       seq_done6,  // sequencer finished executing sequence for this channel 
315 316
    output                       page_nxt_chn6,
    output                       buf_run6,   // external buffer run (may be used to force page) @posedge mclk
317
  `ifdef def_read_mem_chn6
318
    output                       buf_wr_chn6,   // @ negedge mclk
319
    output                       buf_wpage_nxt_chn6,   // @ negedge mclk
320
    output                [63:0] buf_wdata_chn6,   // @ negedge mclk
321 322 323
    output                       buf_wrun6,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn6
324
    output                       buf_rd_chn6,
325
    output                       buf_rpage_nxt_chn6,
326 327 328 329 330 331 332 333
    input                 [63:0] buf_rdata_chn6,
  `endif
`endif    

// channel 7 interface 
`ifdef def_enable_mem_chn7
    input                        want_rq7,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq7,
Andrey Filippov's avatar
Andrey Filippov committed
334
    output reg                   channel_pgm_en7, // channel can program sequence data
335
    input                        reject7,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
336
    output                       seq_done7,  // sequencer finished executing sequence for this channel 
337 338
    output                       page_nxt_chn7,
    output                       buf_run7,   // external buffer run (may be used to force page) @posedge mclk
339
  `ifdef def_read_mem_chn7
340
    output                       buf_wr_chn7,   // @ negedge mclk
341
    output                       buf_wpage_nxt_chn7,   // @ negedge mclk
342
    output                [63:0] buf_wdata_chn7,   // @ negedge mclk
343 344 345
    output                       buf_wrun7,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn7
346
    output                       buf_rd_chn7,
347
    output                       buf_rpage_nxt_chn7,
348 349 350 351 352 353 354 355
    input                 [63:0] buf_rdata_chn7,
  `endif
`endif    

// channel 8 interface 
`ifdef def_enable_mem_chn8
    input                        want_rq8,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq8,
Andrey Filippov's avatar
Andrey Filippov committed
356
    output reg                   channel_pgm_en8, // channel can program sequence data
357
    input                        reject8,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
358
    output                       seq_done8,  // sequencer finished executing sequence for this channel 
359 360
    output                       page_nxt_chn8,
    output                       buf_run8,   // external buffer run (may be used to force page) @posedge mclk
361
  `ifdef def_read_mem_chn8
362
    output                       buf_wr_chn8,   // @ negedge mclk
363
    output                       buf_wpage_nxt_chn8,   // @ negedge mclk
364
    output                [63:0] buf_wdata_chn8,   // @ negedge mclk
365 366 367
    output                       buf_wrun8,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn8
368
    output                       buf_rd_chn8,
369
    output                       buf_rpage_nxt_chn8,
370 371 372 373 374 375 376 377
    input                 [63:0] buf_rdata_chn8,
  `endif
`endif    

// channel 9 interface 
`ifdef def_enable_mem_chn9
    input                        want_rq9,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq9,
Andrey Filippov's avatar
Andrey Filippov committed
378
    output reg                   channel_pgm_en9, // channel can program sequence data
379
    input                        reject9,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
380
    output                       seq_done9,  // sequencer finished executing sequence for this channel 
381 382
    output                       page_nxt_chn9,
    output                       buf_run9,   // external buffer run (may be used to force page) @posedge mclk
383
  `ifdef def_read_mem_chn9
384
    output                       buf_wr_chn9,   // @ negedge mclk
385
    output                       buf_wpage_nxt_chn9,   // @ negedge mclk
386
    output                [63:0] buf_wdata_chn9,   // @ negedge mclk
387 388 389
    output                       buf_wrun9,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn9
390
    output                       buf_rd_chn9,
391
    output                       buf_rpage_nxt_chn9,
392 393 394 395 396 397 398 399
    input                 [63:0] buf_rdata_chn9,
  `endif
`endif    

// channel 10 interface 
`ifdef def_enable_mem_chn10
    input                        want_rq10,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq10,
Andrey Filippov's avatar
Andrey Filippov committed
400
    output reg                   channel_pgm_en10, // channel can program sequence data
401
    input                        reject10,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
402
    output                       seq_done10,  // sequencer finished executing sequence for this channel 
403 404
    output                       page_nxt_chn10,
    output                       buf_run10,   // external buffer run (may be used to force page) @posedge mclk
405
  `ifdef def_read_mem_chn10
406
    output                       buf_wr_chn10,   // @ negedge mclk
407
    output                       buf_wpage_nxt_chn10,   // @ negedge mclk
408
    output                [63:0] buf_wdata_chn10,   // @ negedge mclk
409 410 411
    output                       buf_wrun10,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn10
412
    output                       buf_rd_chn10,
413
    output                       buf_rpage_nxt_chn10,
414 415 416 417 418 419 420 421
    input                 [63:0] buf_rdata_chn10,
  `endif
`endif    

// channel 11 interface 
`ifdef def_enable_mem_chn11
    input                        want_rq11,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq11,
Andrey Filippov's avatar
Andrey Filippov committed
422
    output reg                   channel_pgm_en11, // channel can program sequence data
423
    input                        reject11,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
424
    output                       seq_done11,  // sequencer finished executing sequence for this channel 
425 426
    output                       page_nxt_chn11,
    output                       buf_run11,   // external buffer run (may be used to force page) @posedge mclk
427
  `ifdef def_read_mem_chn11
428
    output                       buf_wr_chn11,   // @ negedge mclk
429
    output                       buf_wpage_nxt_chn11,   // @ negedge mclk
430
    output                [63:0] buf_wdata_chn11,   // @ negedge mclk
431 432 433
    output                       buf_wrun11,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn11
434
    output                       buf_rd_chn11,
435
    output                       buf_rpage_nxt_chn11,
436 437 438 439 440 441 442 443
    input                 [63:0] buf_rdata_chn11,
  `endif
`endif    

// channel 12 interface 
`ifdef def_enable_mem_chn12
    input                        want_rq12,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq12,
Andrey Filippov's avatar
Andrey Filippov committed
444
    output reg                   channel_pgm_en12, // channel can program sequence data
445
    input                        reject12,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
446
    output                       seq_done12,  // sequencer finished executing sequence for this channel 
447 448
    output                       page_nxt_chn12,
    output                       buf_run12,   // external buffer run (may be used to force page) @posedge mclk
449
  `ifdef def_read_mem_chn12
450
    output                       buf_wr_chn12,   // @ negedge mclk
451
    output                       buf_wpage_nxt_chn12,   // @ negedge mclk
452
    output                [63:0] buf_wdata_chn12,   // @ negedge mclk
453 454 455
    output                       buf_wrun12,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn12
456
    output                       buf_rd_chn12,
457
    output                       buf_rpage_nxt_chn12,
458 459 460 461 462 463 464 465
    input                 [63:0] buf_rdata_chn12,
  `endif
`endif    

// channel 13 interface 
`ifdef def_enable_mem_chn13
    input                        want_rq13,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq13,
Andrey Filippov's avatar
Andrey Filippov committed
466
    output reg                   channel_pgm_en13, // channel can program sequence data
467
    input                        reject13,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
468
    output                       seq_done13,  // sequencer finished executing sequence for this channel 
469 470
    output                       page_nxt_chn13,
    output                       buf_run13,   // external buffer run (may be used to force page) @posedge mclk
471
  `ifdef def_read_mem_chn13
472
    output                       buf_wr_chn13,   // @ negedge mclk
473
    output                       buf_wpage_nxt_chn13,   // @ negedge mclk
474
    output                [63:0] buf_wdata_chn13,   // @ negedge mclk
475 476 477
    output                       buf_wrun13,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn13
478
    output                       buf_rd_chn13,
479
    output                       buf_rpage_nxt_chn13,
480 481 482 483 484 485 486 487
    input                 [63:0] buf_rdata_chn13,
  `endif
`endif    

// channel 14 interface 
`ifdef def_enable_mem_chn14
    input                        want_rq14,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq14,
Andrey Filippov's avatar
Andrey Filippov committed
488
    output reg                   channel_pgm_en14, // channel can program sequence data
489
    input                        reject14,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
490
    output                       seq_done14,  // sequencer finished executing sequence for this channel 
491 492
    output                       page_nxt_chn14,
    output                       buf_run14,   // external buffer run (may be used to force page) @posedge mclk
493
  `ifdef def_read_mem_chn14
494
    output                       buf_wr_chn14,   // @ negedge mclk
495
    output                       buf_wpage_nxt_chn14,   // @ negedge mclk
496
    output                [63:0] buf_wdata_chn14,   // @ negedge mclk
497 498 499
    output                       buf_wrun14,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn14
500
    output                       buf_rd_chn14,
501
    output                       buf_rpage_nxt_chn14,
502 503 504 505 506 507 508 509
    input                 [63:0] buf_rdata_chn14,
  `endif
`endif    

// channel 15 interface 
`ifdef def_enable_mem_chn15
    input                        want_rq15,   // both want_rq and need_rq should go inactive after being granted  
    input                        need_rq15,
Andrey Filippov's avatar
Andrey Filippov committed
510
    output reg                   channel_pgm_en15, // channel can program sequence data
511
    input                        reject15,    // reject grant
Andrey Filippov's avatar
Andrey Filippov committed
512
    output                       seq_done15,  // sequencer finished executing sequence for this channel 
513 514
    output                       page_nxt_chn15,
    output                       buf_run15,   // external buffer run (may be used to force page) @posedge mclk
515
  `ifdef def_read_mem_chn15
516
    output                       buf_wr_chn15,   // @ negedge mclk
517
    output                       buf_wpage_nxt_chn15,   // @ negedge mclk
518
    output                [63:0] buf_wdata_chn15,   // @ negedge mclk
519 520 521
    output                       buf_wrun15,   // external buffer run with delays compensated fro write, reclocked to @negedge
  `endif
  `ifdef def_write_mem_chn15
522
    output                       buf_rd_chn15,
523
    output                       buf_rpage_nxt_chn15,
524 525 526 527
    input                 [63:0] buf_rdata_chn15,
  `endif
`endif    
    
528 529 530
    
    // priority programming
    // TODO: Move to ps7 instance in this module
531 532 533
//    input       [3:0] pgm_addr,  // channel address to program priority
//    input [width-1:0] pgm_data,  // priority data for the channel
//    input             pgm_en,     // enable programming priority data (use different clock?)
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    // DDR3 interface
    output                       SDRST, // DDR3 reset (active low)
    output                       SDCLK, // DDR3 clock differential output, positive
    output                       SDNCLK,// DDR3 clock differential output, negative
    output  [ADDRESS_NUMBER-1:0] SDA,   // output address ports (14:0) for 4Gb device
    output                 [2:0] SDBA,  // output bank address ports
    output                       SDWE,  // output WE port
    output                       SDRAS, // output RAS port
    output                       SDCAS, // output CAS port
    output                       SDCKE, // output Clock Enable port
    output                       SDODT, // output ODT port

    inout                 [15:0] SDD,   // DQ  I/O pads
    output                       SDDML, // LDM  I/O pad (actually only output)
    inout                        DQSL,  // LDQS I/O pad
    inout                        NDQSL, // ~LDQS I/O pad
    output                       SDDMU, // UDM  I/O pad (actually only output)
    inout                        DQSU,  // UDQS I/O pad
552
    inout                        NDQSU //,
553 554
// temporary debug data    
    ,output                [11:0] tmp_debug // add some signals generated here?
555
);
556
    wire        reject;     // OR-ed reject from all channels
557
    wire        ext_buf_rd;
558
    wire        ext_buf_rpage_nxt;
559
    wire        ext_buf_page_nxt;
560
    wire  [3:0] ext_buf_rchn; 
561 562
    wire        ext_buf_rrefresh;
    wire        ext_buf_rrun; // run read sequence (to be used with external buffer to set initial address
563
    reg  [63:0] ext_buf_rdata; 
564
    wire        ext_buf_wr;
565
    wire        ext_buf_wpage_nxt;
566
    wire  [3:0] ext_buf_wchn; 
Andrey Filippov's avatar
Andrey Filippov committed
567
    wire        ext_buf_wrefresh; 
568
    wire        ext_buf_wrun;  // @negedge,first cycle of sequencer run matching write delay 
569 570 571 572
    wire [63:0] ext_buf_wdata; 

    wire                  [15:0] want_rq;   // both want_rq and need_rq should go inactive after being granted  
    wire                  [15:0] need_rq;
Andrey Filippov's avatar
Andrey Filippov committed
573 574


575 576 577 578 579 580 581 582 583
// status data from phy (sequencer)
    wire [7:0] status_ad_phy;
    wire       status_rq_phy;
    wire       status_start_phy;
    
// status data from top level controller 
    wire [7:0] status_ad_mcontr;
    wire       status_rq_mcontr;
    wire       status_start_mcontr;
584
    wire       set_status_w;
Andrey Filippov's avatar
Andrey Filippov committed
585 586 587 588 589 590 591 592

    wire        en_schedul; // enable channel arbitration, needs to be disabled before next access can be scheduled
    wire        need;       // granted access is "needed" one, not just "wanted"
    wire        grant;      // single-cycle granted channel access
    wire  [3:0] grant_chn; // granted  channel number, valid with grant, stays valid until en_schedul is deasserted
    wire  [3:0] priority_addr;  // channel address to program priority
    wire [15:0] priority_data;  // priority data for the channel
    wire        priority_en;    // enable programming priority data (use different clock?) 
593 594 595 596 597 598 599 600 601 602 603 604 605
    
    reg     [3:0]   cmd_wr_chn;     // channel granted write access to command sequencer memory
    reg     [9:0]   cmd_addr_cur;   // current address in the command sequencer memory bank1 (PL)
    reg    [10:0]   cmd_addr_start; // sequencer start address (including bank 0/1)
    reg             grant_r;
    reg             cmd_seq_set;    // some command sequencer data was set (so use it)

    reg             cmd_seq_fill;   // command sequencer is in the process of filling by a channel
    reg             cmd_seq_full;   // command sequencer is filled (if using PL sequencer bank)
    reg             cmd_seq_need;   // memory request by a cnannel in the sequencer is urgent (valid with cmd_seq_full)

    reg             cmd_seq_run;    // run command sequencer - single cycle
    reg     [3:0]   cmd_seq_chn;    // channel number corresponding to the pending memory request:  valid with cmd_seq_run
Andrey Filippov's avatar
Andrey Filippov committed
606
    reg             cmd_seq_refresh;     // sequencer is running refresh
607 608
    reg    [10:0]   cmd_seq_addr;   // start address of the command sequencer (MSB - bank: 0 - PS, 1:PL): valid with cmd_seq_run

609
    wire            sel_refresh_w;  // select refresh over channel, only valid @ a single-cycle pre_run_seq_w
610 611 612 613 614 615
    wire            pre_run_seq_w;  // initiate run sequence next cycle
    wire            pre_run_chn_w;  // initiate run sequence next cycle for a channel (not refresh)
    wire            mcontr_reset;   // reset controller, generated with ddr_rst in the sequencer
    wire            mcontr_enabled; // enabled and not reset

    wire            sequencer_run_busy; // sequencer is busy
Andrey Filippov's avatar
Andrey Filippov committed
616
    wire            sequencer_run_done; // to notify channels
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

    wire            refresh_want;
    wire            refresh_need;
    reg             refresh_grant;

    reg             refresh_en;
    reg     [7:0]   refresh_period; // remove 
    reg     [9:0]   refresh_addr;   // TODO: set command
    reg             mcontr_en;      // enable controller
    reg    [15:0]   mcontr_chn_en;  // per-channel request enable (will not reset transaction in progress)

    reg             chn_want_some;
    reg             chn_need_some;
    reg    [15:0]   chn_want_r;
    wire   [17:0]   status_data;
    
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    wire [2:0] mcontr_0bit_addr;
    wire       mcontr_0bit_we;
    wire  [2:0] mcontr_16bit_addr;
    // ???
 //   wire [15:0] aaaa;
//   assign aaaa={cmd_ad,cmd_ad};// just debugging Vivado synth
    
    wire [15:0] mcontr_16bit_data; //  = {cmd_ad,cmd_ad};
//   assign mcontr_16bit_data={cmd_ad,cmd_ad};// just debugging Vivado synth
    
    wire        mcontr_16bit_we;
    
//   wire [15:0] aaaa;
//  assign aaaa={cmd_ad,cmd_ad};// just debugging Vivado synth
    
    
    
650 651
    assign status_data={chn_want_r,chn_need_some,chn_want_some};
    
652 653
// mux status info from the memory controller and other modules    
    status_router2 status_router2_top_i (
Andrey Filippov's avatar
Andrey Filippov committed
654 655 656 657 658 659 660 661
        .rst       (1'b0),                // rst),  // input
        .clk       (mclk),                // input
        .srst      (mrst),                // input
        .db_in0    (status_ad_phy),       // input[7:0] 
        .rq_in0    (status_rq_phy),       // input
        .start_in0 (status_start_phy),    // output
        .db_in1    (status_ad_mcontr),    // input[7:0] 
        .rq_in1    (status_rq_mcontr),    // input
662
        .start_in1 (status_start_mcontr), // output
Andrey Filippov's avatar
Andrey Filippov committed
663 664 665
        .db_out    (status_ad),           // output[7:0] 
        .rq_out    (status_rq),           // output
        .start_out (status_start)         // input
666
    );
667 668 669 670 671
    
    status_generate #(
        .STATUS_REG_ADDR  (MCONTR_TOP_STATUS_REG_ADDR),
        .PAYLOAD_BITS     (18)
    ) status_generate_i (
Andrey Filippov's avatar
Andrey Filippov committed
672 673 674 675
        .rst              (1'b0),                   // rst), // input
        .clk              (mclk),                   // input
        .srst             (mrst),                   // input
        .we               (set_status_w),           // input
676
        .wd               (mcontr_16bit_data[7:0]), // input[7:0] 
Andrey Filippov's avatar
Andrey Filippov committed
677 678 679 680
        .status           (status_data),            // input[25:0] 
        .ad               (status_ad_mcontr),       // output[7:0] 
        .rq               (status_rq_mcontr),       // output
        .start            (status_start_mcontr)     // input
681 682
    );
    
Andrey Filippov's avatar
Andrey Filippov committed
683 684 685 686 687 688 689 690
// generate 16-bit data commands (and set defaults to registers)
    cmd_deser #(
        .ADDR       (MCONTR_ARBIT_ADDR),
        .ADDR_MASK  (MCONTR_ARBIT_ADDR_MASK),
        .NUM_CYCLES (4),
        .ADDR_WIDTH (4),
        .DATA_WIDTH (16)
    ) cmd_deser_mcontr_16bit_i (
Andrey Filippov's avatar
Andrey Filippov committed
691 692 693 694 695 696 697 698
        .rst        (1'b0),               // rst), // input
        .clk        (mclk),               // input
        .srst       (mrst),               // input
        .ad         (cmd_ad),             // input[7:0] 
        .stb        (cmd_stb),            // input
        .addr       (priority_addr),      // output[15:0] 
        .data       (priority_data),      // output[31:0] 
        .we         (priority_en)         // output
Andrey Filippov's avatar
Andrey Filippov committed
699
    );
700
  
701 702 703 704 705 706 707 708
// generate on/off dependent on lsb and 0-bit commands
    cmd_deser #(
        .ADDR       (MCONTR_TOP_0BIT_ADDR),
        .ADDR_MASK  (MCONTR_TOP_0BIT_ADDR_MASK),
        .NUM_CYCLES (2),
        .ADDR_WIDTH (3),
        .DATA_WIDTH (0)
    ) cmd_deser_0bit_i (
Andrey Filippov's avatar
Andrey Filippov committed
709 710 711 712 713 714 715 716
        .rst        (1'b0),              //  rst), // input
        .clk        (mclk),              // input
        .srst       (mrst),              // input
        .ad         (cmd_ad),            // input[7:0] 
        .stb        (cmd_stb),           // input
        .addr       (mcontr_0bit_addr),  // output[15:0] 
        .data       (),                  // output[31:0] 
        .we         (mcontr_0bit_we)     // output
717
    );
Andrey Filippov's avatar
Andrey Filippov committed
718 719
    always @ (posedge mclk) begin
        if (mrst)                                                                            mcontr_en <= 0;
720 721
        else if (mcontr_0bit_we && (mcontr_0bit_addr[2:1]==(MCONTR_TOP_0BIT_MCONTR_EN>>1)))  mcontr_en <= mcontr_0bit_addr[0];
        
Andrey Filippov's avatar
Andrey Filippov committed
722
        if (mrst)                                                                            refresh_en <= 0 ; // 1;
723 724 725 726 727 728 729 730 731 732 733 734
        else if (mcontr_0bit_we && (mcontr_0bit_addr[2:1]==(MCONTR_TOP_0BIT_REFRESH_EN>>1))) refresh_en <= mcontr_0bit_addr[0];
        
    end
  
// generate 16-bit data commands (and set defaults to registers)
    cmd_deser #(
        .ADDR       (MCONTR_TOP_16BIT_ADDR),
        .ADDR_MASK  (MCONTR_TOP_16BIT_ADDR_MASK),
        .NUM_CYCLES (4),
        .ADDR_WIDTH (3),
        .DATA_WIDTH (16)
    ) cmd_deser_16bit_i (
Andrey Filippov's avatar
Andrey Filippov committed
735
        .rst        (1'b0), // input
736
        .clk        (mclk), // input
Andrey Filippov's avatar
Andrey Filippov committed
737
        .srst       (mrst), // input
738 739 740 741 742 743
        .ad         (cmd_ad), // input[7:0] 
        .stb        (cmd_stb), // input
        .addr       (mcontr_16bit_addr), // output[15:0] 
        .data       (mcontr_16bit_data), // output[31:0] 
        .we         (mcontr_16bit_we) // output
    );
744

745 746 747 748 749 750 751 752 753 754
    wire set_chn_en_w;
    wire set_refresh_period_w;
    wire set_refresh_address_w;
    reg  set_refresh_period;
    
    assign set_chn_en_w=           mcontr_16bit_we && (mcontr_16bit_addr[2:0]==MCONTR_TOP_16BIT_CHN_EN);
    assign set_refresh_period_w=   mcontr_16bit_we && (mcontr_16bit_addr[2:0]==MCONTR_TOP_16BIT_REFRESH_PERIOD);
    assign set_refresh_address_w=  mcontr_16bit_we && (mcontr_16bit_addr[2:0]==MCONTR_TOP_16BIT_REFRESH_ADDRESS);
    assign set_status_w=           mcontr_16bit_we && (mcontr_16bit_addr[2:0]==MCONTR_TOP_16BIT_STATUS_CNTRL);

Andrey Filippov's avatar
Andrey Filippov committed
755 756 757
    always @ (posedge mclk) begin
        if (mrst) set_refresh_period  <= 0;
        else      set_refresh_period <= set_refresh_period_w;
758

Andrey Filippov's avatar
Andrey Filippov committed
759
        if      (mrst)                  mcontr_chn_en <= DFLT_CHN_EN;
760 761
        else if (set_chn_en_w)          mcontr_chn_en <= mcontr_16bit_data[15:0];
        
Andrey Filippov's avatar
Andrey Filippov committed
762
        if      (mrst)                  refresh_addr  <= DFLT_REFRESH_ADDR;
763 764
        else if (set_refresh_address_w) refresh_addr <= mcontr_16bit_data[9:0];

Andrey Filippov's avatar
Andrey Filippov committed
765
        if      (mrst)                  refresh_period <= DFLT_REFRESH_PERIOD;
766 767
        else if (set_refresh_period_w)  refresh_period <= mcontr_16bit_data[7:0];
        
Andrey Filippov's avatar
Andrey Filippov committed
768 769
        if (mrst) chn_want_some <= 0;
        else      chn_want_some <= |want_rq;
770

Andrey Filippov's avatar
Andrey Filippov committed
771 772
        if (mrst) chn_need_some <= 0;
        else      chn_need_some <= |need_rq;
773
        
Andrey Filippov's avatar
Andrey Filippov committed
774 775
        if (mrst) chn_want_r <= 0;
        else      chn_want_r <= want_rq ; // unmasked channel requests
776 777 778 779
        
    end
        
  
780

Andrey Filippov's avatar
Andrey Filippov committed
781 782 783
    scheduler16 #(
        .width      (16)
    ) scheduler16_i (
Andrey Filippov's avatar
Andrey Filippov committed
784
        .mrst       (mrst), // input
Andrey Filippov's avatar
Andrey Filippov committed
785
        .clk        (mclk), // input
786
        .chn_en     (mcontr_chn_en), // input[15:0]
Andrey Filippov's avatar
Andrey Filippov committed
787 788 789 790 791 792 793 794 795 796
        .want_rq    (want_rq), // input[15:0] 
        .need_rq    (need_rq), // input[15:0] 
        .en_schedul (en_schedul), // input
        .need       (need), // output
        .grant      (grant), // output
        .grant_chn  (grant_chn), // output[3:0] 
        .pgm_addr   (priority_addr), // input[3:0] 
        .pgm_data   (priority_data), // input[15:0] 
        .pgm_en     (priority_en) // input
    );
797 798 799 800 801


        
assign mcontr_enabled=mcontr_en && !mcontr_reset; 
assign sel_refresh_w= refresh_need || (refresh_want && !(cmd_seq_need && cmd_seq_full));
802 803
assign pre_run_seq_w= mcontr_enabled && !sequencer_run_busy && !cmd_seq_run && (cmd_seq_full || refresh_want);

804 805
assign pre_run_chn_w= pre_run_seq_w && !sel_refresh_w;
assign en_schedul= mcontr_enabled && !cmd_seq_fill && !cmd_seq_full;
806
    reg reject_r;
807
// sequential logic for commands transfer to the sequencer 
Andrey Filippov's avatar
Andrey Filippov committed
808 809 810
always @ (posedge mclk) begin
    if (mrst) grant_r <= 0;
    else      grant_r <= grant;
811 812 813

    if (mrst) reject_r <= 0;
    else      reject_r <= reject;
Andrey Filippov's avatar
Andrey Filippov committed
814
    
Andrey Filippov's avatar
Andrey Filippov committed
815
    if (mrst)          cmd_seq_set <= 0;
Andrey Filippov's avatar
Andrey Filippov committed
816 817 818
    else if (grant_r)  cmd_seq_set <= 0;
    else if (seq_wr)   cmd_seq_set <= 1;
    
Andrey Filippov's avatar
Andrey Filippov committed
819
    if (mrst)       cmd_wr_chn <= 0;
820 821 822
    else if (grant) cmd_wr_chn <= grant_chn;


823
//TODO: Modify,cmd_seq_fill was initially used to see if any sequaence data was written (or PS is used), now it is cmd_seq_set
824 825 826
    if (mrst)                                                        cmd_seq_fill <= 0;
    else if (!mcontr_enabled || seq_set || cmd_seq_full || reject_r) cmd_seq_fill <= 0;
    else if (grant)                                                  cmd_seq_fill <= 1;
827

Andrey Filippov's avatar
Andrey Filippov committed
828
    if (mrst)                                   cmd_seq_full <= 0;
829
    else if (!mcontr_enabled || pre_run_chn_w ) cmd_seq_full <= 0;
Andrey Filippov's avatar
Andrey Filippov committed
830
    else if (seq_set)                           cmd_seq_full <= 1; // even with no data
831 832


Andrey Filippov's avatar
Andrey Filippov committed
833
    if (mrst)                                   cmd_seq_need <= 0;
834 835
    else if (grant)                             cmd_seq_need <= need;

Andrey Filippov's avatar
Andrey Filippov committed
836

Andrey Filippov's avatar
Andrey Filippov committed
837
    if (mrst)        cmd_addr_cur <= 0;
Andrey Filippov's avatar
Andrey Filippov committed
838 839
    else if (seq_wr) cmd_addr_cur <= cmd_addr_cur+1;
    
Andrey Filippov's avatar
Andrey Filippov committed
840
    if (mrst)                          cmd_addr_start <= 0;
841
    else if (grant_r)                  cmd_addr_start <= {1'b1,cmd_addr_cur};  // address in PL bank
Andrey Filippov's avatar
Andrey Filippov committed
842
    else if (!cmd_seq_set && seq_set)  cmd_addr_start <= {1'b0,seq_data[9:0]}; // address in PS bank
843 844
    
    
Andrey Filippov's avatar
Andrey Filippov committed
845 846
    if (mrst) cmd_seq_run <= 0;
    else      cmd_seq_run <= pre_run_seq_w;
847
    
Andrey Filippov's avatar
Andrey Filippov committed
848 849
// add refresh address here?    
end   
850 851 852
always @ (posedge mclk) begin
    if (pre_run_seq_w) cmd_seq_addr <= sel_refresh_w ? {1'b0,refresh_addr} : cmd_addr_start;
    if (pre_run_seq_w) cmd_seq_chn <= cmd_wr_chn;
Andrey Filippov's avatar
Andrey Filippov committed
853
    if (pre_run_seq_w) cmd_seq_refresh <= sel_refresh_w;
854
end
Andrey Filippov's avatar
Andrey Filippov committed
855 856 857 858 859 860


//   assign run_seq_rq_in = refresh_en && refresh_need; // higher priority request input

    
    ddr_refresh ddr_refresh_i (
Andrey Filippov's avatar
Andrey Filippov committed
861 862 863 864
        .mrst             (mrst),               // input
        .clk              (mclk),               // input
        .en               (refresh_en),         // input
        .refresh_period   (refresh_period),     // input[7:0] 
865
        .set              (set_refresh_period), // input
Andrey Filippov's avatar
Andrey Filippov committed
866 867 868
        .want             (refresh_want),       // output
        .need             (refresh_need),       // output
        .grant            (refresh_grant)       // input
Andrey Filippov's avatar
Andrey Filippov committed
869
    );
Andrey Filippov's avatar
Andrey Filippov committed
870 871 872
    always @(posedge mclk) begin
         if (mrst) refresh_grant <= 0; 
         else      refresh_grant <= pre_run_seq_w && sel_refresh_w; 
Andrey Filippov's avatar
Andrey Filippov committed
873 874 875
    end


876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    mcontr_sequencer #(
        .DLY_LD                        (DLY_LD),
        .DLY_LD_MASK                   (DLY_LD_MASK),
        .MCONTR_PHY_0BIT_ADDR          (MCONTR_PHY_0BIT_ADDR),
        .MCONTR_PHY_0BIT_ADDR_MASK     (MCONTR_PHY_0BIT_ADDR_MASK),
        .MCONTR_PHY_0BIT_DLY_SET       (MCONTR_PHY_0BIT_DLY_SET),
        .MCONTR_PHY_0BIT_CMDA_EN       (MCONTR_PHY_0BIT_CMDA_EN),
        .MCONTR_PHY_0BIT_SDRST_ACT     (MCONTR_PHY_0BIT_SDRST_ACT),
        .MCONTR_PHY_0BIT_CKE_EN        (MCONTR_PHY_0BIT_CKE_EN),
        .MCONTR_PHY_0BIT_DCI_RST       (MCONTR_PHY_0BIT_DCI_RST),
        .MCONTR_PHY_0BIT_DLY_RST       (MCONTR_PHY_0BIT_DLY_RST),
        .MCONTR_PHY_STATUS_REG_ADDR    (MCONTR_PHY_STATUS_REG_ADDR),
        .MCONTR_PHY_16BIT_ADDR         (MCONTR_PHY_16BIT_ADDR),
        .MCONTR_PHY_16BIT_ADDR_MASK    (MCONTR_PHY_16BIT_ADDR_MASK),
        .MCONTR_PHY_16BIT_PATTERNS     (MCONTR_PHY_16BIT_PATTERNS),
        .MCONTR_PHY_16BIT_PATTERNS_TRI (MCONTR_PHY_16BIT_PATTERNS_TRI),
        .MCONTR_PHY_16BIT_WBUF_DELAY   (MCONTR_PHY_16BIT_WBUF_DELAY),
        .MCONTR_PHY_16BIT_EXTRA        (MCONTR_PHY_16BIT_EXTRA),
        .MCONTR_PHY_STATUS_CNTRL       (MCONTR_PHY_STATUS_CNTRL),
        .DFLT_DQS_PATTERN              (DFLT_DQS_PATTERN),
        .DFLT_DQM_PATTERN              (DFLT_DQM_PATTERN),
        .DFLT_DQ_TRI_ON_PATTERN        (DFLT_DQ_TRI_ON_PATTERN),
        .DFLT_DQ_TRI_OFF_PATTERN       (DFLT_DQ_TRI_OFF_PATTERN),
        .DFLT_DQS_TRI_ON_PATTERN       (DFLT_DQS_TRI_ON_PATTERN),
        .DFLT_DQS_TRI_OFF_PATTERN      (DFLT_DQS_TRI_OFF_PATTERN),
        .DFLT_WBUF_DELAY               (DFLT_WBUF_DELAY),
        .DFLT_INV_CLK_DIV              (DFLT_INV_CLK_DIV),
        .PHASE_WIDTH                   (PHASE_WIDTH),
        .SLEW_DQ                       (SLEW_DQ),
        .SLEW_DQS                      (SLEW_DQS),
        .SLEW_CMDA                     (SLEW_CMDA),
        .SLEW_CLK                      (SLEW_CLK),
        .IBUF_LOW_PWR                  (IBUF_LOW_PWR),
        .REFCLK_FREQUENCY              (REFCLK_FREQUENCY),
        .HIGH_PERFORMANCE_MODE         (HIGH_PERFORMANCE_MODE),
        .CLKIN_PERIOD                  (CLKIN_PERIOD),
        .CLKFBOUT_MULT                 (CLKFBOUT_MULT),
        .DIVCLK_DIVIDE                 (DIVCLK_DIVIDE),
914
        .CLKFBOUT_USE_FINE_PS          (CLKFBOUT_USE_FINE_PS),
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
        .CLKFBOUT_PHASE                (CLKFBOUT_PHASE),
        .SDCLK_PHASE                   (SDCLK_PHASE),
        .CLK_PHASE                     (CLK_PHASE),
        .CLK_DIV_PHASE                 (CLK_DIV_PHASE),
        .MCLK_PHASE                    (MCLK_PHASE),
        .REF_JITTER1                   (REF_JITTER1),
        .SS_EN                         (SS_EN),
        .SS_MODE                       (SS_MODE),
        .SS_MOD_PERIOD                 (SS_MOD_PERIOD),
        .CMD_PAUSE_BITS                (CMD_PAUSE_BITS),
        .CMD_DONE_BIT                  (CMD_DONE_BIT)
    ) mcontr_sequencer_i (
        .SDRST          (SDRST), // output
        .SDCLK          (SDCLK), // output
        .SDNCLK         (SDNCLK), // output
        .SDA            (SDA[14:0]), // output[14:0] // BUG with localparam - fixed
        .SDBA           (SDBA[2:0]), // output[2:0] 
        .SDWE           (SDWE), // output
        .SDRAS          (SDRAS), // output
        .SDCAS          (SDCAS), // output
        .SDCKE          (SDCKE), // output
        .SDODT          (SDODT), // output
        .SDD            (SDD[15:0]), // inout[15:0] 
        .SDDML          (SDDML), // inout
        .DQSL           (DQSL), // inout
        .NDQSL          (NDQSL), // inout
        .SDDMU          (SDDMU), // inout
        .DQSU           (DQSU), // inout
        .NDQSU          (NDQSU), // inout
        
        .clk_in         (clk_in), // axi_aclk), // input
        .rst_in         (rst_in), // axi_rst), // input TODO: move buffer outside?
        .mclk           (mclk), // output
Andrey Filippov's avatar
Andrey Filippov committed
948 949
        .mrst           (mrst), // input
        .locked         (locked), // output
950
        .ref_clk        (ref_clk), // input
Andrey Filippov's avatar
Andrey Filippov committed
951
        .idelay_ctrl_reset (idelay_ctrl_reset),
952
        
Andrey Filippov's avatar
Andrey Filippov committed
953 954 955 956 957
        .cmd0_clk       (cmd0_clk), // input
        .cmd0_we        (cmd0_we), // input
        .cmd0_addr      (cmd0_addr[9:0]), // input[9:0] 
        .cmd0_data      (cmd0_data[31:0]), // input[31:0] 

958
        .cmd1_clk       (mclk), // input
Andrey Filippov's avatar
Andrey Filippov committed
959 960 961
        .cmd1_we        (seq_wr), // input
        .cmd1_addr      (cmd_addr_cur), // input[9:0] 
        .cmd1_data      (seq_data), // input[31:0]
962 963 964

        .run_addr       (cmd_seq_addr[10:0]), // input[10:0] 
        .run_chn        (cmd_seq_chn[3:0]), // input[3:0] 
Andrey Filippov's avatar
Andrey Filippov committed
965 966
        .run_refresh    (cmd_seq_refresh), // input
        .run_seq        (cmd_seq_run), // input
Andrey Filippov's avatar
Andrey Filippov committed
967
        .run_done       (sequencer_run_done), // output
968 969
        .run_busy       (sequencer_run_busy), // output ASSUMING it is high next cycle after run_seq - TODO: verify - yes, if not mcontr_reset
        .mcontr_reset   (mcontr_reset), // output == ddr_reset that also resets sequencer  
970 971 972 973 974
        .cmd_ad         (cmd_ad), // input[7:0] 
        .cmd_stb        (cmd_stb), // input
        .status_ad      (status_ad_phy), // output[7:0] 
        .status_rq      (status_rq_phy), // output
        .status_start   (status_start_phy), // input
975
        .ext_buf_page_nxt (ext_buf_page_nxt),
976
        .ext_buf_rd     (ext_buf_rd), // output
977
        .ext_buf_rpage_nxt  (ext_buf_rpage_nxt), // output
978
//        .ext_buf_raddr  (ext_buf_raddr), // output[6:0] 
979
        .ext_buf_rchn   (ext_buf_rchn), // output[3:0] 
Andrey Filippov's avatar
Andrey Filippov committed
980
        .ext_buf_rrefresh(ext_buf_rrefresh), // output 
981
        .ext_buf_rrun   (ext_buf_rrun), // run read sequence (to be used with external buffer to set initial address
982 983
        .ext_buf_rdata  (ext_buf_rdata), // input[63:0] 
        .ext_buf_wr     (ext_buf_wr), // output
984
        .ext_buf_wpage_nxt  (ext_buf_wpage_nxt), // output[6:0] 
985
//        .ext_buf_waddr  (ext_buf_waddr), // output[6:0] 
986
        .ext_buf_wchn   (ext_buf_wchn), // output[3:0] 
987 988
        .ext_buf_wrefresh(ext_buf_wrefresh), // output
        .ext_buf_wrun   (ext_buf_wrun),     // @negedge,first cycle of sequencer run matching write delay 
989 990 991 992 993
        .ext_buf_wdata  (ext_buf_wdata), // output[63:0] 
        .tmp_debug      (tmp_debug) // output[11:0] 
    );

// Registering existing channel buffers I/Os
994
`ifdef def_enable_mem_chn0
Andrey Filippov's avatar
Andrey Filippov committed
995
    mcont_common_chnbuf_reg #( .CHN_NUMBER(0)) mcont_common_chnbuf_reg0_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
996 997
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done0),.page_nxt(page_nxt_chn0),.buf_run(buf_run0));
998
  `ifdef def_read_mem_chn0
Andrey Filippov's avatar
Andrey Filippov committed
999
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 0)) mcont_to_chnbuf_reg0_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1000 1001
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn0),
1002 1003 1004
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn0),.buf_run(buf_wrun0),.buf_wdata_chn(buf_wdata_chn0));
  `endif
  `ifdef def_write_mem_chn0
1005
    wire [63:0] ext_buf_rdata0;
Andrey Filippov's avatar
Andrey Filippov committed
1006
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 0),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg0_i (.rst(mrst),.clk(mclk),
1007 1008
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata0),.buf_rd_chn(buf_rd_chn0),.rpage_nxt(buf_rpage_nxt_chn0),.buf_rdata_chn(buf_rdata_chn0));
1009 1010 1011
  `endif
`endif    

1012
`ifdef def_enable_mem_chn1
Andrey Filippov's avatar
Andrey Filippov committed
1013
    mcont_common_chnbuf_reg #( .CHN_NUMBER(1)) mcont_common_chnbuf_reg1_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1014 1015
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done1),.page_nxt(page_nxt_chn1),.buf_run(buf_run1));
1016
  `ifdef def_read_mem_chn1
Andrey Filippov's avatar
Andrey Filippov committed
1017
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 1)) mcont_to_chnbuf_reg1_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1018 1019
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn1),
1020 1021 1022
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn1),.buf_run(buf_wrun1),.buf_wdata_chn(buf_wdata_chn1));
  `endif
  `ifdef def_write_mem_chn1
1023
    wire [63:0] ext_buf_rdata1;
Andrey Filippov's avatar
Andrey Filippov committed
1024
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 1),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg1_i (.rst(mrst),.clk(mclk),
1025 1026
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata1),.buf_rd_chn(buf_rd_chn1),.rpage_nxt(buf_rpage_nxt_chn1),.buf_rdata_chn(buf_rdata_chn1));
1027 1028 1029
  `endif
`endif    

1030
`ifdef def_enable_mem_chn2
Andrey Filippov's avatar
Andrey Filippov committed
1031
    mcont_common_chnbuf_reg #( .CHN_NUMBER(2)) mcont_common_chnbuf_reg2_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1032 1033
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done2),.page_nxt(page_nxt_chn2),.buf_run(buf_run2));
1034
  `ifdef def_read_mem_chn2
Andrey Filippov's avatar
Andrey Filippov committed
1035
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 2)) mcont_to_chnbuf_reg2_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1036 1037
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn2),
1038 1039 1040
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn2),.buf_run(buf_wrun2),.buf_wdata_chn(buf_wdata_chn2));
  `endif
  `ifdef def_write_mem_chn2
1041
    wire [63:0] ext_buf_rdata2;
Andrey Filippov's avatar
Andrey Filippov committed
1042
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 2),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg2_i (.rst(mrst),.clk(mclk),
1043 1044
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata2),.buf_rd_chn(buf_rd_chn2),.rpage_nxt(buf_rpage_nxt_chn2),.buf_rdata_chn(buf_rdata_chn2));
1045 1046 1047
  `endif
`endif    

1048
`ifdef def_enable_mem_chn3
Andrey Filippov's avatar
Andrey Filippov committed
1049
    mcont_common_chnbuf_reg #( .CHN_NUMBER(3)) mcont_common_chnbuf_reg3_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1050 1051
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done3),.page_nxt(page_nxt_chn3),.buf_run(buf_run3));
1052
  `ifdef def_read_mem_chn3
Andrey Filippov's avatar
Andrey Filippov committed
1053
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 3)) mcont_to_chnbuf_reg3_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1054 1055
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn3),
1056 1057 1058
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn3),.buf_run(buf_wrun3),.buf_wdata_chn(buf_wdata_chn3));
  `endif
  `ifdef def_write_mem_chn3
1059
    wire [63:0] ext_buf_rdata3;
Andrey Filippov's avatar
Andrey Filippov committed
1060
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 3),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg3_i (.rst(mrst),.clk(mclk),
1061 1062
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata3),.buf_rd_chn(buf_rd_chn3),.rpage_nxt(buf_rpage_nxt_chn3),.buf_rdata_chn(buf_rdata_chn3));
1063 1064 1065
  `endif
`endif    

1066
`ifdef def_enable_mem_chn4
Andrey Filippov's avatar
Andrey Filippov committed
1067
    mcont_common_chnbuf_reg #( .CHN_NUMBER(4)) mcont_common_chnbuf_reg4_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1068 1069
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done4),.page_nxt(page_nxt_chn4),.buf_run(buf_run4));
1070
  `ifdef def_read_mem_chn4
Andrey Filippov's avatar
Andrey Filippov committed
1071
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 4)) mcont_to_chnbuf_reg4_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1072 1073
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn4),
1074 1075 1076
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn4),.buf_run(buf_wrun4),.buf_wdata_chn(buf_wdata_chn4));
  `endif
  `ifdef def_write_mem_chn4
1077
    wire [63:0] ext_buf_rdata4;
Andrey Filippov's avatar
Andrey Filippov committed
1078
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 4),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg4_i (.rst(mrst),.clk(mclk),
1079 1080
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata4),.buf_rd_chn(buf_rd_chn4),.rpage_nxt(buf_rpage_nxt_chn4),.buf_rdata_chn(buf_rdata_chn4));
1081 1082 1083
  `endif
`endif    

1084
`ifdef def_enable_mem_chn5
Andrey Filippov's avatar
Andrey Filippov committed
1085
    mcont_common_chnbuf_reg #( .CHN_NUMBER(5)) mcont_common_chnbuf_reg5_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1086 1087
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done5),.page_nxt(page_nxt_chn5),.buf_run(buf_run5));
1088
  `ifdef def_read_mem_chn5
Andrey Filippov's avatar
Andrey Filippov committed
1089
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 5)) mcont_to_chnbuf_reg5_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1090 1091
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn5),
1092 1093 1094
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn5),.buf_run(buf_wrun5),.buf_wdata_chn(buf_wdata_chn5));
  `endif
  `ifdef def_write_mem_chn5
1095
    wire [63:0] ext_buf_rdata5;
Andrey Filippov's avatar
Andrey Filippov committed
1096
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 5),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg5_i (.rst(mrst),.clk(mclk),
1097 1098
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata5),.buf_rd_chn(buf_rd_chn5),.rpage_nxt(buf_rpage_nxt_chn5),.buf_rdata_chn(buf_rdata_chn5));
1099 1100 1101
  `endif
`endif    

1102
`ifdef def_enable_mem_chn6
Andrey Filippov's avatar
Andrey Filippov committed
1103
    mcont_common_chnbuf_reg #( .CHN_NUMBER(6)) mcont_common_chnbuf_reg6_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1104 1105
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done6),.page_nxt(page_nxt_chn6),.buf_run(buf_run6));
1106
  `ifdef def_read_mem_chn6
Andrey Filippov's avatar
Andrey Filippov committed
1107
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 6)) mcont_to_chnbuf_reg6_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1108 1109
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn6),
1110 1111 1112
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn6),.buf_run(buf_wrun6),.buf_wdata_chn(buf_wdata_chn6));
  `endif
  `ifdef def_write_mem_chn6
1113
    wire [63:0] ext_buf_rdata6;
Andrey Filippov's avatar
Andrey Filippov committed
1114
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 6),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg6_i (.rst(mrst),.clk(mclk),
1115 1116
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata6),.buf_rd_chn(buf_rd_chn6),.rpage_nxt(buf_rpage_nxt_chn6),.buf_rdata_chn(buf_rdata_chn6));
1117 1118 1119
  `endif
`endif    

1120
`ifdef def_enable_mem_chn7
Andrey Filippov's avatar
Andrey Filippov committed
1121
    mcont_common_chnbuf_reg #( .CHN_NUMBER(7)) mcont_common_chnbuf_reg7_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1122 1123
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done7),.page_nxt(page_nxt_chn7),.buf_run(buf_run7));
1124
  `ifdef def_read_mem_chn7
Andrey Filippov's avatar
Andrey Filippov committed
1125
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 7)) mcont_to_chnbuf_reg7_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1126 1127
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn7),
1128 1129 1130
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn7),.buf_run(buf_wrun7),.buf_wdata_chn(buf_wdata_chn7));
  `endif
  `ifdef def_write_mem_chn7
1131
    wire [63:0] ext_buf_rdata7;
Andrey Filippov's avatar
Andrey Filippov committed
1132
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 7),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg7_i (.rst(mrst),.clk(mclk),
1133 1134
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata7),.buf_rd_chn(buf_rd_chn7),.rpage_nxt(buf_rpage_nxt_chn7),.buf_rdata_chn(buf_rdata_chn7));
1135 1136 1137
  `endif
`endif    

1138
`ifdef def_enable_mem_chn8
Andrey Filippov's avatar
Andrey Filippov committed
1139
    mcont_common_chnbuf_reg #( .CHN_NUMBER(8)) mcont_common_chnbuf_reg8_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1140 1141
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done8),.page_nxt(page_nxt_chn8),.buf_run(buf_run8));
1142
  `ifdef def_read_mem_chn8
Andrey Filippov's avatar
Andrey Filippov committed
1143
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 8)) mcont_to_chnbuf_reg8_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1144 1145
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn8),
1146 1147 1148
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn8),.buf_run(buf_wrun8),.buf_wdata_chn(buf_wdata_chn8));
  `endif
  `ifdef def_write_mem_chn8
1149
    wire [63:0] ext_buf_rdata8;
Andrey Filippov's avatar
Andrey Filippov committed
1150
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 8),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg8_i (.rst(mrst),.clk(mclk),
1151 1152
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata8),.buf_rd_chn(buf_rd_chn8),.rpage_nxt(buf_rpage_nxt_chn8),.buf_rdata_chn(buf_rdata_chn8));
1153 1154 1155
  `endif
`endif    

1156
`ifdef def_enable_mem_chn9
Andrey Filippov's avatar
Andrey Filippov committed
1157
    mcont_common_chnbuf_reg #( .CHN_NUMBER(9)) mcont_common_chnbuf_reg9_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1158 1159
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done9),.page_nxt(page_nxt_chn9),.buf_run(buf_run9));
1160
  `ifdef def_read_mem_chn9
Andrey Filippov's avatar
Andrey Filippov committed
1161
    mcont_to_chnbuf_reg #(.CHN_NUMBER( 9)) mcont_to_chnbuf_reg9_i(.rst(mrst),.clk(mclk),.ext_buf_wr(ext_buf_wr),
1162 1163
        .ext_buf_wpage_nxt(ext_buf_wpage_nxt),.ext_buf_wchn(ext_buf_wchn), .ext_buf_wrefresh(ext_buf_wrefresh),
        .ext_buf_wrun(ext_buf_wrun),.ext_buf_wdata(ext_buf_wdata),.buf_wr_chn(buf_wr_chn9),
1164 1165 1166
        .buf_wpage_nxt_chn(buf_wpage_nxt_chn9),.buf_run(buf_wrun9),.buf_wdata_chn(buf_wdata_chn9));
  `endif
  `ifdef def_write_mem_chn9
1167
    wire [63:0] ext_buf_rdata9;
Andrey Filippov's avatar
Andrey Filippov committed
1168
    mcont_from_chnbuf_reg #(.CHN_NUMBER( 9),.CHN_LATENCY(CHNBUF_READ_LATENCY)) mcont_from_chnbuf_reg9_i (.rst(mrst),.clk(mclk),
1169 1170
        .ext_buf_rd(ext_buf_rd),.ext_buf_rchn(ext_buf_rchn), .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_rpage_nxt(ext_buf_rpage_nxt),
        .ext_buf_rdata(ext_buf_rdata9),.buf_rd_chn(buf_rd_chn9),.rpage_nxt(buf_rpage_nxt_chn9),.buf_rdata_chn(buf_rdata_chn9));
1171 1172 1173
  `endif
`endif    

1174
`ifdef def_enable_mem_chn10
Andrey Filippov's avatar
Andrey Filippov committed
1175
    mcont_common_chnbuf_reg #( .CHN_NUMBER(10)) mcont_common_chnbuf_reg10_i(.rst(mrst),.clk(mclk), .ext_buf_rchn(ext_buf_rchn),
1176 1177
        .ext_buf_rrefresh(ext_buf_rrefresh),.ext_buf_page_nxt(ext_buf_page_nxt),.seq_done(sequencer_run_done), .ext_buf_run(ext_buf_rrun),
        .buf_done(seq_done10),.page_nxt(page_nxt_chn10),.buf_run(buf_run10));
1178