ruby: clean up a few warnings
[gem5.git] / src / mem / ruby / system / MemoryControl.cc
1
2 /*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * MemoryControl.C
32 *
33 * Description: This module simulates a basic DDR-style memory controller
34 * (and can easily be extended to do FB-DIMM as well).
35 *
36 * This module models a single channel, connected to any number of
37 * DIMMs with any number of ranks of DRAMs each. If you want multiple
38 * address/data channels, you need to instantiate multiple copies of
39 * this module.
40 *
41 * Each memory request is placed in a queue associated with a specific
42 * memory bank. This queue is of finite size; if the queue is full
43 * the request will back up in an (infinite) common queue and will
44 * effectively throttle the whole system. This sort of behavior is
45 * intended to be closer to real system behavior than if we had an
46 * infinite queue on each bank. If you want the latter, just make
47 * the bank queues unreasonably large.
48 *
49 * The head item on a bank queue is issued when all of the
50 * following are true:
51 * the bank is available
52 * the address path to the DIMM is available
53 * the data path to or from the DIMM is available
54 *
55 * Note that we are not concerned about fixed offsets in time. The bank
56 * will not be used at the same moment as the address path, but since
57 * there is no queue in the DIMM or the DRAM it will be used at a constant
58 * number of cycles later, so it is treated as if it is used at the same
59 * time.
60 *
61 * We are assuming closed bank policy; that is, we automatically close
62 * each bank after a single read or write. Adding an option for open
63 * bank policy is for future work.
64 *
65 * We are assuming "posted CAS"; that is, we send the READ or WRITE
66 * immediately after the ACTIVATE. This makes scheduling the address
67 * bus trivial; we always schedule a fixed set of cycles. For DDR-400,
68 * this is a set of two cycles; for some configurations such as
69 * DDR-800 the parameter tRRD forces this to be set to three cycles.
70 *
71 * We assume a four-bit-time transfer on the data wires. This is
72 * the minimum burst length for DDR-2. This would correspond
73 * to (for example) a memory where each DIMM is 72 bits wide
74 * and DIMMs are ganged in pairs to deliver 64 bytes at a shot.
75 * This gives us the same occupancy on the data wires as on the
76 * address wires (for the two-address-cycle case).
77 *
78 * The only non-trivial scheduling problem is the data wires.
79 * A write will use the wires earlier in the operation than a read
80 * will; typically one cycle earlier as seen at the DRAM, but earlier
81 * by a worst-case round-trip wire delay when seen at the memory controller.
82 * So, while reads from one rank can be scheduled back-to-back
83 * every two cycles, and writes (to any rank) scheduled every two cycles,
84 * when a read is followed by a write we need to insert a bubble.
85 * Furthermore, consecutive reads from two different ranks may need
86 * to insert a bubble due to skew between when one DRAM stops driving the
87 * wires and when the other one starts. (These bubbles are parameters.)
88 *
89 * This means that when some number of reads and writes are at the
90 * heads of their queues, reads could starve writes, and/or reads
91 * to the same rank could starve out other requests, since the others
92 * would never see the data bus ready.
93 * For this reason, we have implemented an anti-starvation feature.
94 * A group of requests is marked "old", and a counter is incremented
95 * each cycle as long as any request from that batch has not issued.
96 * if the counter reaches twice the bank busy time, we hold off any
97 * newer requests until all of the "old" requests have issued.
98 *
99 * We also model tFAW. This is an obscure DRAM parameter that says
100 * that no more than four activate requests can happen within a window
101 * of a certain size. For most configurations this does not come into play,
102 * or has very little effect, but it could be used to throttle the power
103 * consumption of the DRAM. In this implementation (unlike in a DRAM
104 * data sheet) TFAW is measured in memory bus cycles; i.e. if TFAW = 16
105 * then no more than four activates may happen within any 16 cycle window.
106 * Refreshes are included in the activates.
107 *
108 *
109 * $Id: $
110 *
111 */
112
113 #include "mem/ruby/common/Global.hh"
114 #include "mem/gems_common/Map.hh"
115 #include "mem/ruby/common/Address.hh"
116 #include "mem/ruby/profiler/Profiler.hh"
117 #include "mem/ruby/slicc_interface/AbstractChip.hh"
118 #include "mem/ruby/system/System.hh"
119 #include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh"
120 #include "mem/ruby/slicc_interface/NetworkMessage.hh"
121 #include "mem/ruby/network/Network.hh"
122
123 #include "mem/ruby/common/Consumer.hh"
124
125 #include "mem/ruby/system/MemoryControl.hh"
126
127 #include <list>
128
129 class Consumer;
130
131 // Value to reset watchdog timer to.
132 // If we're idle for this many memory control cycles,
133 // shut down our clock (our rescheduling of ourselves).
134 // Refresh shuts down as well.
135 // When we restart, we'll be in a different phase
136 // with respect to ruby cycles, so this introduces
137 // a slight inaccuracy. But it is necessary or the
138 // ruby tester never terminates because the event
139 // queue is never empty.
140 #define IDLECOUNT_MAX_VALUE 1000
141
142 // Output operator definition
143
144 ostream& operator<<(ostream& out, const MemoryControl& obj)
145 {
146 obj.print(out);
147 out << flush;
148 return out;
149 }
150
151
152 // ****************************************************************
153
154 // CONSTRUCTOR
155
156 MemoryControl::MemoryControl (AbstractChip* chip_ptr, int version) {
157 m_chip_ptr = chip_ptr;
158 m_version = version;
159 m_msg_counter = 0;
160
161 m_debug = 0;
162 //if (m_version == 0) m_debug = 1;
163
164 m_mem_bus_cycle_multiplier = RubyConfig::memBusCycleMultiplier();
165 m_banks_per_rank = RubyConfig::banksPerRank();
166 m_ranks_per_dimm = RubyConfig::ranksPerDimm();
167 m_dimms_per_channel = RubyConfig::dimmsPerChannel();
168 m_bank_bit_0 = RubyConfig::bankBit0();
169 m_rank_bit_0 = RubyConfig::rankBit0();
170 m_dimm_bit_0 = RubyConfig::dimmBit0();
171 m_bank_queue_size = RubyConfig::bankQueueSize();
172 m_bank_busy_time = RubyConfig::bankBusyTime();
173 m_rank_rank_delay = RubyConfig::rankRankDelay();
174 m_read_write_delay = RubyConfig::readWriteDelay();
175 m_basic_bus_busy_time = RubyConfig::basicBusBusyTime();
176 m_mem_ctl_latency = RubyConfig::memCtlLatency();
177 m_refresh_period = RubyConfig::refreshPeriod();
178 m_memRandomArbitrate = RubyConfig::memRandomArbitrate();
179 m_tFaw = RubyConfig::tFaw();
180 m_memFixedDelay = RubyConfig::memFixedDelay();
181
182 assert(m_tFaw <= 62); // must fit in a uint64 shift register
183
184 m_total_banks = m_banks_per_rank * m_ranks_per_dimm * m_dimms_per_channel;
185 m_total_ranks = m_ranks_per_dimm * m_dimms_per_channel;
186 m_refresh_period_system = m_refresh_period / m_total_banks;
187
188 m_bankQueues = new list<MemoryNode> [m_total_banks];
189 assert(m_bankQueues);
190
191 m_bankBusyCounter = new int [m_total_banks];
192 assert(m_bankBusyCounter);
193
194 m_oldRequest = new int [m_total_banks];
195 assert(m_oldRequest);
196
197 for (int i=0; i<m_total_banks; i++) {
198 m_bankBusyCounter[i] = 0;
199 m_oldRequest[i] = 0;
200 }
201
202 m_busBusyCounter_Basic = 0;
203 m_busBusyCounter_Write = 0;
204 m_busBusyCounter_ReadNewRank = 0;
205 m_busBusy_WhichRank = 0;
206
207 m_roundRobin = 0;
208 m_refresh_count = 1;
209 m_need_refresh = 0;
210 m_refresh_bank = 0;
211 m_awakened = 0;
212 m_idleCount = 0;
213 m_ageCounter = 0;
214
215 // Each tfaw shift register keeps a moving bit pattern
216 // which shows when recent activates have occurred.
217 // m_tfaw_count keeps track of how many 1 bits are set
218 // in each shift register. When m_tfaw_count is >= 4,
219 // new activates are not allowed.
220 m_tfaw_shift = new uint64 [m_total_ranks];
221 m_tfaw_count = new int [m_total_ranks];
222 for (int i=0; i<m_total_ranks; i++) {
223 m_tfaw_shift[i] = 0;
224 m_tfaw_count[i] = 0;
225 }
226 }
227
228
229 // DESTRUCTOR
230
231 MemoryControl::~MemoryControl () {
232 delete [] m_bankQueues;
233 delete [] m_bankBusyCounter;
234 delete [] m_oldRequest;
235 }
236
237
238 // PUBLIC METHODS
239
240 // enqueue new request from directory
241
242 void MemoryControl::enqueue (const MsgPtr& message, int latency) {
243 Time current_time = g_eventQueue_ptr->getTime();
244 Time arrival_time = current_time + latency;
245 const MemoryMsg* memMess = dynamic_cast<const MemoryMsg*>(message.ref());
246 physical_address_t addr = memMess->getAddress().getAddress();
247 MemoryRequestType type = memMess->getType();
248 bool is_mem_read = (type == MemoryRequestType_MEMORY_READ);
249 MemoryNode thisReq(arrival_time, message, addr, is_mem_read, !is_mem_read);
250 enqueueMemRef(thisReq);
251 }
252
253 // Alternate entry point used when we already have a MemoryNode structure built.
254
255 void MemoryControl::enqueueMemRef (MemoryNode& memRef) {
256 m_msg_counter++;
257 memRef.m_msg_counter = m_msg_counter;
258 Time arrival_time = memRef.m_time;
259 uint64 at = arrival_time;
260 bool is_mem_read = memRef.m_is_mem_read;
261 physical_address_t addr = memRef.m_addr;
262 int bank = getBank(addr);
263 if (m_debug) {
264 printf("New memory request%7d: 0x%08llx %c arrived at %10lld ", m_msg_counter, addr, is_mem_read? 'R':'W', at);
265 printf("bank =%3x\n", bank);
266 }
267 g_system_ptr->getProfiler()->profileMemReq(bank);
268 m_input_queue.push_back(memRef);
269 if (!m_awakened) {
270 g_eventQueue_ptr->scheduleEvent(this, 1);
271 m_awakened = 1;
272 }
273 }
274
275
276
277 // dequeue, peek, and isReady are used to transfer completed requests
278 // back to the directory
279
280 void MemoryControl::dequeue () {
281 assert(isReady());
282 m_response_queue.pop_front();
283 }
284
285
286 const Message* MemoryControl::peek () {
287 MemoryNode node = peekNode();
288 Message* msg_ptr = node.m_msgptr.ref();
289 assert(msg_ptr != NULL);
290 return msg_ptr;
291 }
292
293
294 MemoryNode MemoryControl::peekNode () {
295 assert(isReady());
296 MemoryNode req = m_response_queue.front();
297 uint64 returnTime = req.m_time;
298 if (m_debug) {
299 printf("Old memory request%7d: 0x%08llx %c peeked at %10lld\n",
300 req.m_msg_counter, req.m_addr, req.m_is_mem_read? 'R':'W', returnTime);
301 }
302 return req;
303 }
304
305
306 bool MemoryControl::isReady () {
307 return ((!m_response_queue.empty()) &&
308 (m_response_queue.front().m_time <= g_eventQueue_ptr->getTime()));
309 }
310
311 void MemoryControl::setConsumer (Consumer* consumer_ptr) {
312 m_consumer_ptr = consumer_ptr;
313 }
314
315 void MemoryControl::print (ostream& out) const {
316 }
317
318
319 void MemoryControl::printConfig (ostream& out) {
320 out << "Memory Control " << m_version << ":" << endl;
321 out << " Ruby cycles per memory cycle: " << m_mem_bus_cycle_multiplier << endl;
322 out << " Basic read latency: " << m_mem_ctl_latency << endl;
323 if (m_memFixedDelay) {
324 out << " Fixed Latency mode: Added cycles = " << m_memFixedDelay << endl;
325 } else {
326 out << " Bank busy time: " << BANK_BUSY_TIME << " memory cycles" << endl;
327 out << " Memory channel busy time: " << m_basic_bus_busy_time << endl;
328 out << " Dead cycles between reads to different ranks: " << m_rank_rank_delay << endl;
329 out << " Dead cycle between a read and a write: " << m_read_write_delay << endl;
330 out << " tFaw (four-activate) window: " << m_tFaw << endl;
331 }
332 out << " Banks per rank: " << m_banks_per_rank << endl;
333 out << " Ranks per DIMM: " << m_ranks_per_dimm << endl;
334 out << " DIMMs per channel: " << m_dimms_per_channel << endl;
335 out << " LSB of bank field in address: " << m_bank_bit_0 << endl;
336 out << " LSB of rank field in address: " << m_rank_bit_0 << endl;
337 out << " LSB of DIMM field in address: " << m_dimm_bit_0 << endl;
338 out << " Max size of each bank queue: " << m_bank_queue_size << endl;
339 out << " Refresh period (within one bank): " << m_refresh_period << endl;
340 out << " Arbitration randomness: " << m_memRandomArbitrate << endl;
341 }
342
343
344 void MemoryControl::setDebug (int debugFlag) {
345 m_debug = debugFlag;
346 }
347
348
349 // ****************************************************************
350
351 // PRIVATE METHODS
352
353 // Queue up a completed request to send back to directory
354
355 void MemoryControl::enqueueToDirectory (MemoryNode req, int latency) {
356 Time arrival_time = g_eventQueue_ptr->getTime()
357 + (latency * m_mem_bus_cycle_multiplier);
358 req.m_time = arrival_time;
359 m_response_queue.push_back(req);
360
361 // schedule the wake up
362 g_eventQueue_ptr->scheduleEventAbsolute(m_consumer_ptr, arrival_time);
363 }
364
365
366
367 // getBank returns an integer that is unique for each
368 // bank across this memory controller.
369
370 int MemoryControl::getBank (physical_address_t addr) {
371 int dimm = (addr >> m_dimm_bit_0) & (m_dimms_per_channel - 1);
372 int rank = (addr >> m_rank_bit_0) & (m_ranks_per_dimm - 1);
373 int bank = (addr >> m_bank_bit_0) & (m_banks_per_rank - 1);
374 return (dimm * m_ranks_per_dimm * m_banks_per_rank)
375 + (rank * m_banks_per_rank)
376 + bank;
377 }
378
379 // getRank returns an integer that is unique for each rank
380 // and independent of individual bank.
381
382 int MemoryControl::getRank (int bank) {
383 int rank = (bank / m_banks_per_rank);
384 assert (rank < (m_ranks_per_dimm * m_dimms_per_channel));
385 return rank;
386 }
387
388
389 // queueReady determines if the head item in a bank queue
390 // can be issued this cycle
391
392 bool MemoryControl::queueReady (int bank) {
393 if ((m_bankBusyCounter[bank] > 0) && !m_memFixedDelay) {
394 g_system_ptr->getProfiler()->profileMemBankBusy();
395 //if (m_debug) printf(" bank %x busy %d\n", bank, m_bankBusyCounter[bank]);
396 return false;
397 }
398 if (m_memRandomArbitrate >= 2) {
399 if ((random() % 100) < m_memRandomArbitrate) {
400 g_system_ptr->getProfiler()->profileMemRandBusy();
401 return false;
402 }
403 }
404 if (m_memFixedDelay) return true;
405 if ((m_ageCounter > (2 * m_bank_busy_time)) && !m_oldRequest[bank]) {
406 g_system_ptr->getProfiler()->profileMemNotOld();
407 return false;
408 }
409 if (m_busBusyCounter_Basic == m_basic_bus_busy_time) {
410 // Another bank must have issued this same cycle.
411 // For profiling, we count this as an arb wait rather than
412 // a bus wait. This is a little inaccurate since it MIGHT
413 // have also been blocked waiting for a read-write or a
414 // read-read instead, but it's pretty close.
415 g_system_ptr->getProfiler()->profileMemArbWait(1);
416 return false;
417 }
418 if (m_busBusyCounter_Basic > 0) {
419 g_system_ptr->getProfiler()->profileMemBusBusy();
420 return false;
421 }
422 int rank = getRank(bank);
423 if (m_tfaw_count[rank] >= ACTIVATE_PER_TFAW) {
424 g_system_ptr->getProfiler()->profileMemTfawBusy();
425 return false;
426 }
427 bool write = !m_bankQueues[bank].front().m_is_mem_read;
428 if (write && (m_busBusyCounter_Write > 0)) {
429 g_system_ptr->getProfiler()->profileMemReadWriteBusy();
430 return false;
431 }
432 if (!write && (rank != m_busBusy_WhichRank)
433 && (m_busBusyCounter_ReadNewRank > 0)) {
434 g_system_ptr->getProfiler()->profileMemDataBusBusy();
435 return false;
436 }
437 return true;
438 }
439
440
441 // issueRefresh checks to see if this bank has a refresh scheduled
442 // and, if so, does the refresh and returns true
443
444 bool MemoryControl::issueRefresh (int bank) {
445 if (!m_need_refresh || (m_refresh_bank != bank)) return false;
446 if (m_bankBusyCounter[bank] > 0) return false;
447 // Note that m_busBusyCounter will prevent multiple issues during
448 // the same cycle, as well as on different but close cycles:
449 if (m_busBusyCounter_Basic > 0) return false;
450 int rank = getRank(bank);
451 if (m_tfaw_count[rank] >= ACTIVATE_PER_TFAW) return false;
452
453 // Issue it:
454
455 //if (m_debug) {
456 //uint64 current_time = g_eventQueue_ptr->getTime();
457 //printf(" Refresh bank %3x at %lld\n", bank, current_time);
458 //}
459 g_system_ptr->getProfiler()->profileMemRefresh();
460 m_need_refresh--;
461 m_refresh_bank++;
462 if (m_refresh_bank >= m_total_banks) m_refresh_bank = 0;
463 m_bankBusyCounter[bank] = m_bank_busy_time;
464 m_busBusyCounter_Basic = m_basic_bus_busy_time;
465 m_busBusyCounter_Write = m_basic_bus_busy_time;
466 m_busBusyCounter_ReadNewRank = m_basic_bus_busy_time;
467 markTfaw(rank);
468 return true;
469 }
470
471
472 // Mark the activate in the tFaw shift register
473 void MemoryControl::markTfaw (int rank) {
474 if (m_tFaw) {
475 m_tfaw_shift[rank] |= (1 << (m_tFaw-1));
476 m_tfaw_count[rank]++;
477 }
478 }
479
480
481 // Issue a memory request: Activate the bank,
482 // reserve the address and data buses, and queue
483 // the request for return to the requesting
484 // processor after a fixed latency.
485
486 void MemoryControl::issueRequest (int bank) {
487 int rank = getRank(bank);
488 MemoryNode req = m_bankQueues[bank].front();
489 m_bankQueues[bank].pop_front();
490 if (m_debug) {
491 uint64 current_time = g_eventQueue_ptr->getTime();
492 printf(" Mem issue request%7d: 0x%08llx %c at %10lld bank =%3x\n",
493 req.m_msg_counter, req.m_addr, req.m_is_mem_read? 'R':'W', current_time, bank);
494 }
495 if (req.m_msgptr.ref() != NULL) { // don't enqueue L3 writebacks
496 enqueueToDirectory(req, m_mem_ctl_latency + m_memFixedDelay);
497 }
498 m_oldRequest[bank] = 0;
499 markTfaw(rank);
500 m_bankBusyCounter[bank] = m_bank_busy_time;
501 m_busBusy_WhichRank = rank;
502 if (req.m_is_mem_read) {
503 g_system_ptr->getProfiler()->profileMemRead();
504 m_busBusyCounter_Basic = m_basic_bus_busy_time;
505 m_busBusyCounter_Write = m_basic_bus_busy_time + m_read_write_delay;
506 m_busBusyCounter_ReadNewRank = m_basic_bus_busy_time + m_rank_rank_delay;
507 } else {
508 g_system_ptr->getProfiler()->profileMemWrite();
509 m_busBusyCounter_Basic = m_basic_bus_busy_time;
510 m_busBusyCounter_Write = m_basic_bus_busy_time;
511 m_busBusyCounter_ReadNewRank = m_basic_bus_busy_time;
512 }
513 }
514
515
516 // executeCycle: This function is called once per memory clock cycle
517 // to simulate all the periodic hardware.
518
519 void MemoryControl::executeCycle () {
520 // Keep track of time by counting down the busy counters:
521 for (int bank=0; bank < m_total_banks; bank++) {
522 if (m_bankBusyCounter[bank] > 0) m_bankBusyCounter[bank]--;
523 }
524 if (m_busBusyCounter_Write > 0) m_busBusyCounter_Write--;
525 if (m_busBusyCounter_ReadNewRank > 0) m_busBusyCounter_ReadNewRank--;
526 if (m_busBusyCounter_Basic > 0) m_busBusyCounter_Basic--;
527
528 // Count down the tFAW shift registers:
529 for (int rank=0; rank < m_total_ranks; rank++) {
530 if (m_tfaw_shift[rank] & 1) m_tfaw_count[rank]--;
531 m_tfaw_shift[rank] >>= 1;
532 }
533
534 // After time period expires, latch an indication that we need a refresh.
535 // Disable refresh if in memFixedDelay mode.
536 if (!m_memFixedDelay) m_refresh_count--;
537 if (m_refresh_count == 0) {
538 m_refresh_count = m_refresh_period_system;
539 assert (m_need_refresh < 10); // Are we overrunning our ability to refresh?
540 m_need_refresh++;
541 }
542
543 // If this batch of requests is all done, make a new batch:
544 m_ageCounter++;
545 int anyOld = 0;
546 for (int bank=0; bank < m_total_banks; bank++) {
547 anyOld |= m_oldRequest[bank];
548 }
549 if (!anyOld) {
550 for (int bank=0; bank < m_total_banks; bank++) {
551 if (!m_bankQueues[bank].empty()) m_oldRequest[bank] = 1;
552 }
553 m_ageCounter = 0;
554 }
555
556 // If randomness desired, re-randomize round-robin position each cycle
557 if (m_memRandomArbitrate) {
558 m_roundRobin = random() % m_total_banks;
559 }
560
561
562 // For each channel, scan round-robin, and pick an old, ready
563 // request and issue it. Treat a refresh request as if it
564 // were at the head of its bank queue. After we issue something,
565 // keep scanning the queues just to gather statistics about
566 // how many are waiting. If in memFixedDelay mode, we can issue
567 // more than one request per cycle.
568
569 int queueHeads = 0;
570 int banksIssued = 0;
571 for (int i = 0; i < m_total_banks; i++) {
572 m_roundRobin++;
573 if (m_roundRobin >= m_total_banks) m_roundRobin = 0;
574 issueRefresh(m_roundRobin);
575 int qs = m_bankQueues[m_roundRobin].size();
576 if (qs > 1) {
577 g_system_ptr->getProfiler()->profileMemBankQ(qs-1);
578 }
579 if (qs > 0) {
580 m_idleCount = IDLECOUNT_MAX_VALUE; // we're not idle if anything is queued
581 queueHeads++;
582 if (queueReady(m_roundRobin)) {
583 issueRequest(m_roundRobin);
584 banksIssued++;
585 if (m_memFixedDelay) {
586 g_system_ptr->getProfiler()->profileMemWaitCycles(m_memFixedDelay);
587 }
588 }
589 }
590 }
591
592 // memWaitCycles is a redundant catch-all for the specific counters in queueReady
593 g_system_ptr->getProfiler()->profileMemWaitCycles(queueHeads - banksIssued);
594
595 // Check input queue and move anything to bank queues if not full.
596 // Since this is done here at the end of the cycle, there will always
597 // be at least one cycle of latency in the bank queue.
598 // We deliberately move at most one request per cycle (to simulate
599 // typical hardware). Note that if one bank queue fills up, other
600 // requests can get stuck behind it here.
601
602 if (!m_input_queue.empty()) {
603 m_idleCount = IDLECOUNT_MAX_VALUE; // we're not idle if anything is pending
604 MemoryNode req = m_input_queue.front();
605 int bank = getBank(req.m_addr);
606 if (m_bankQueues[bank].size() < m_bank_queue_size) {
607 m_input_queue.pop_front();
608 m_bankQueues[bank].push_back(req);
609 }
610 g_system_ptr->getProfiler()->profileMemInputQ(m_input_queue.size());
611 }
612 }
613
614
615 // wakeup: This function is called once per memory controller clock cycle.
616
617 void MemoryControl::wakeup () {
618
619 // execute everything
620 executeCycle();
621
622 m_idleCount--;
623 if (m_idleCount <= 0) {
624 m_awakened = 0;
625 } else {
626 // Reschedule ourselves so that we run every memory cycle:
627 g_eventQueue_ptr->scheduleEvent(this, m_mem_bus_cycle_multiplier);
628 }
629 }
630
631