summaryrefslogtreecommitdiff
path: root/src/mem/dram.cc
blob: 394c70db65a2f35dac1bac1c387edaff9defae7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
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
914
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
/*
 * Copyright (c) 2004 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Ali Saidi
 *          Ron Dreslinski
 */

/*
 Copyright (c) 2000 Computer Engineering and Communication Networks Lab (TIK)
 Swiss Federal Institute of Technology (ETH) Zurich, Switzerland

 All rights reserved.
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.

 IN NO EVENT SHALL THE TIK OR THE ETH ZURICH BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE TIK OR THE ETH ZURICH HAVE BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 THE TIK AND THE ETH ZURICH SPECIFICALLY DISCLAIM ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND TIK AND THE ETH ZURICH
 HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.
*/

/* authors: Andreas Romer 4/99 - 7/99
            Matthias Gries 4/99 - 2/01


References: http://www.tik.ee.ethz.ch/
======================================
-> Publications http://www.tik.ee.ethz.ch/db/tik/publications/form_search_publications.php3


Matthias Gries: A Survey of Synchronous RAM Architectures.
TIK Report Nr. 71, Computer Engineering and Networks Lab (TIK),
Swiss Federal Institute of Technology (ETH) Zurich, April, 1999

-> DRAM survey


Matthias Gries, Andreas Romer: Performance Evaluation of Recent
DRAM Architectures for Embedded Systems.
TIK Report Nr. 82, Computer Engineering and Networks Lab (TIK),
Swiss Federal Institute of Technology (ETH) Zurich, November, 1999.

-> description of the DRAM and controller models for SimpleScalar in the appendix
(note that the current software version additionally supports overlapping in
 closed-page mode with slightly modified timing)


Matthias Gries: The Impact of Recent DRAM Architectures on Embedded Systems Performance.
Euromicro2000, Symposium on Digital Systems Design, IEEE Computer, Maastricht, Netherlands,
Vol. 1, pages 282-289, September, 2000.

-> performance study with SimpleScalar


Matthias Gries: Modeling a Memory Subsystem with Petri Nets: a Case Study.
A. Yakovlev, L. Gomes, and L. Lavagno (Eds), Hardware Design and Petri Nets,
Kluwer Academic, pages 291-310, March, 2000.

-> SDRAM + controller performance model as a high-level Petri net
*/

/**
 * @file
 * Definition of a DRAM like main memory.
 */


#include "mem/dram.hh"
#include "sim/builder.hh"
#include <stdlib.h>
#include <string>

extern int maxThreadsPerCPU;

/* SDRAM system: PC100/PC133 2-2-2 DIMM timing according to
   PC SDRAM Specification, Rev. 1.7, Intel Corp, Nov. 1999.

   64 bit DIMM consists of four 16x organized 256 Mbit SDRAMs, 128 MByte of main memory in total.*/
/* the settings above must be changed if another memory is used */
/* DRDRAM system: 16 bit channel, four chips (single RIMM), 128 MByte of main memory in total.
   Timing: Rambus Inc, Direct RDRAM, preliminary information, 256/288Mbit: 40-800 timing */


#define  DR_STACK_BASE 0x8000000 /* total size of memory: 128 Mbyte */
#define  DR_BANK_SIZE  0x100000  /* size of a bank      :   1 Mbyte */
#define  DR_ROW_SIZE 0x800       /* size of a row       :   2 Kbyte */
#define  DR_NUM_BANKS (DR_STACK_BASE/DR_BANK_SIZE)      /* number of banks        : 128 */
#define  DR_NUM_ROWS (DR_BANK_SIZE/DR_ROW_SIZE)         /* number of rows per bank: 512 */
#define  DR_DATA_BASE  0x4000000 /* Size of textsegment :  64 Mbyte */
#define  DR_NUM_BYTE_MEM  16     /* data packet capacity:  16 byte  */
#define  DR_NUM_DEVS 4   /* number of devices along channel */
#define  DR_BANK_SAMP 16 /* 16 banks are together in one group in each device: bank 15 and 16 have no shared SAMPs */
#define  DR_T_PACKET 4   /* number of cycles (in 400 MHz) the memory needs to deliver a data packet */
#define  DR_T_RCD 7  /* RAS to CAS delay */
#define  DR_T_CAC 8  /* read access delay: number of cylces from read to data  (trailing to leading edge of packet!) */
#define  DR_T_CWD 6  /* Write delay: number of cylces from write to write data (trailing to leading edge of packet!) */
#define  DR_T_RP  8  /* row precharge delay */
#define  DR_T_RTR 8  /* retire delay*/
#define  DR_T_RDP 4  /* min delay from read to precharge in cycles */
#define  DR_T_PP  8  /* precharge to precharge time to any bank in the same device */
#define  DR_T_RAS 20 /* minimal row active time */
/*the settings above need to be changed if the memory is altered*/
#define  DR_DYNAMIC_SIZE (DR_STACK_BASE - DR_DATA_BASE) /* size of the heap and stack at most: 64 Mbyte */
// #define  DR_NUM_BANKS (DR_STACK_BASE/DR_BANK_SIZE)      /* number of banks        : 128 */
// #define  DR_NUM_ROWS (DR_BANK_SIZE/DR_ROW_SIZE)         /* number of rows per bank: 512 */
#define  DR_T_OWR (DR_T_CWD + DR_T_PACKET - DR_T_RTR)   /* overlap after write retire   */
#define  DR_T_HELP (DR_T_CAC+DR_T_PACKET-DR_T_RDP+DR_T_PACKET) /* used for read after read with precharge */
/*delays until data is ready/written to the memory for the DRDRAM*/
#define  DR_T_READ_READ_SROW    (DR_T_CAC + DR_T_PACKET) /* RAR, row hit, current bank */
#define  DR_T_READ_WRITE_SROW   (DR_T_CAC + DR_T_PACKET) /* RAW, row hit, current bank */
#define  DR_T_WRITE_READ_SROW   (DR_T_CWD + DR_T_PACKET) /* WAR, row hit, current bank */
#define  DR_T_WRITE_WRITE_SROW  (DR_T_CWD + DR_T_PACKET) /* WAW, row hit, current bank */
#define  DR_T_READ_READ_SBANK   (DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET) /* RAR, row miss, current bank */
#define  DR_T_READ_WRITE_SBANK  (DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET) /* RAW, row miss, current bank */
#define  DR_T_WRITE_READ_SBANK  (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET) /* WAR, row miss, current bank */
#define  DR_T_WRITE_WRITE_SBANK (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET) /* WAR, row miss, current bank */
#define  DR_T_READ_READ_OBANK   (DR_T_PP+DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET) /* RAR, row miss, another bank */
#define  DR_T_READ_WRITE_OBANK  (DR_T_PP+DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET) /* RAW, row miss, another bank */
#define  DR_T_WRITE_READ_OBANK  (DR_T_PP+DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET) /* WAR, row miss, another bank */
#define  DR_T_WRITE_WRITE_OBANK (DR_T_PP+DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET) /* WAR, row miss, another bank */
/* best-case latencies (due to overlap / row hits in another bank) */
#define  DR_BEST_T_READ_READ_SROW    0                               /* RAR, row hit, current bank */
#define  DR_BEST_T_READ_WRITE_SROW   (DR_T_CAC+DR_T_PACKET-DR_T_OWR) /* RAW, row hit, current bank */
#define  DR_BEST_T_WRITE_READ_SROW   0                               /* WAR, row hit, current bank */
#define  DR_BEST_T_WRITE_WRITE_SROW  (DR_T_CWD+DR_T_PACKET-DR_T_OWR) /* WAR, row hit, current bank */
#define  DR_BEST_T_READ_READ_SBANK   (DR_T_RCD + DR_T_CAC)                            /* RAR, row miss, current bank */
#define  DR_BEST_T_READ_WRITE_SBANK  (DR_T_RP-DR_T_OWR+DR_T_RCD+DR_T_CAC+DR_T_PACKET) /* RAW, row miss, current bank */
#define  DR_BEST_T_WRITE_READ_SBANK  (DR_T_RCD+DR_T_CWD)                              /* WAR, row miss, current bank */
#define  DR_BEST_T_WRITE_WRITE_SBANK (DR_T_RP-DR_T_OWR+DR_T_RCD+DR_T_CWD+DR_T_PACKET) /* WAW, row miss, current bank */
#define  DR_BEST_T_READ_READ_OBANK   0                   /* RAR, row miss/hit, another bank   */
#define  DR_BEST_T_READ_WRITE_OBANK  (DR_T_PACKET+DR_T_CAC-DR_T_OWR) /* RAW, row miss/hit, another bank */
#define  DR_BEST_T_WRITE_READ_OBANK  0                   /* WAR, row miss/hit, another bank   */
#define  DR_BEST_T_WRITE_WRITE_OBANK 0                   /* WAW, row miss/hit, another bank   */
#define  DR_BEST_T_READ_WRITE_ODEV   (DR_T_CAC-DR_T_CWD) /* RAW, row miss/hit, another device */


#define MIN(a,b) ((a<b) ? a : b)
#define  SD_ROW_SIZE 0x1000      /* size of a row       :   4 Kbyte */



DRAMMemory::DRAMMemory(Params *p)
    : PhysicalMemory(p), cpu_ratio(p->cpu_ratio), bus_width(p->bus_width),
      mem_type(p->mem_type), mem_actpolicy(p->mem_actpolicy),
      memctrladdr_type(p->memctrladdr_type), act_lat(p->act_lat),
      cas_lat(p->cas_lat), war_lat(p->war_lat),
      pre_lat(p->pre_lat), dpl_lat(p->dpl_lat),
      trc_lat(p->trc_lat), num_banks(p->num_banks),
      num_cpus(p->num_cpus), last_dev(DR_NUM_DEVS+1),
      lastCmdIsRead(true), precharge(0), same_row_read_access(0), srr_after_read(0),
      srr_after_write(0), same_row_write_access(0), srw_after_read(0),
      srw_after_write(0), same_bank_read_access(0), sbr_after_read(0),
      sbr_after_write(0), same_bank_write_access(0), sbw_after_read(0),
      sbw_after_write(0), other_bank_read_access_hit(0), obr_after_read_hit(0),
      obr_after_write_hit(0), other_bank_write_access_hit(0),
      obw_after_read_hit(0), obw_after_write_hit(0), obr_after_read_miss(0),
      obr_after_write_miss(0),
      obw_after_read_miss(0), obw_after_write_miss(0), total_access(0),
      adjacent_access(0), adjacent_read(0), adjacent_write(0),
      command_overlapping(0), best_case(0), in_between_case(0), worst_case(0),
      full_overlapping(0), partial_overlapping(0), mem_access_details(false),
      memctrlpipe_enable(false), time_last_access(0)
{
    warn("This DRAM module has not been tested with the new memory system at all!");
        bank_size = (params()->addrRange.size() + 1) / num_banks;
        num_rows = bank_size / SD_ROW_SIZE; /* 0x1000 size of row 4Kbtye */
        active_row = new int[num_banks];
        last_bank = num_banks+1;
        last_row  = num_rows;
        busy_until = new Tick[num_banks];
        std::memset(busy_until,0,sizeof(Tick)*num_banks); /* initiliaze */

}

void
DRAMMemory::regStats()
{
    using namespace Stats;

    accesses
        .init(maxThreadsPerCPU)
        .name(name() + ".accesses")
        .desc("total number of accesses")
        .flags(total)
        ;

    bytesRequested
        .init(maxThreadsPerCPU)
        .name(name() + ".bytes_requested")
        .desc("total number of bytes requested")
        .flags(total)
        ;

    bytesSent
        .init(maxThreadsPerCPU)
        .name(name() + ".bytes_sent")
        .desc("total number of bytes sent")
        .flags(total)
        ;

    compressedAccesses
        .init(maxThreadsPerCPU)
        .name(name() + ".compressed_responses")
        .desc("total number of accesses that are compressed")
        .flags(total)
        ;

        // stats for power modelling
    cycles_nCKE
        .init(1)
        .name(name() + ".cycles_nCKE")
        .desc("cycles when CKE is low")
        .flags(total)
        ;

    cycles_all_precharge_CKE
        .init(1)
        .name(name() + ".cycles_all_precharge_CKE")
        .desc("cycles when all banks precharged")
        .flags(total)
        ;

    cycles_all_precharge_nCKE
        .init(1)
        .name(name() + ".cycles_all_precharge_nCKE")
        .desc("cycles when all banks precharged and CKE is low")
        .flags(total)
        ;

    cycles_bank_active_nCKE
        .init(1)
        .name(name() + ".cycles_bank_active_nCKE")
        .desc("cycles when banks active and CKE low")
        .flags(total)
        ;

        // we derive this from other stats later
        // so DR TODO for now this counter is unused
        cycles_avg_ACT
        .init(1)
        .name(name() + ".cycles_avg_ACT")
        .desc("avg cycles between ACT commands")
        .flags(total)
        ;

        cycles_read_out
        .init(1)
        .name(name() + ".cycles_read_out")
        .desc("cycles outputting read data")
        .flags(total)
        ;

        cycles_write_in
        .init(1)
        .name(name() + ".cycles_write_in")
        .desc("cycles inputting write data")
        .flags(total)
        ;

        cycles_between_misses
        .init(1)
        .name(name() + ".cycles_between_misses")
        .desc("cycles between open page misses")
        .flags(total)
        ;

        other_bank_read_access_miss
        .init(1)
        .name(name() + ".other_bank_read_access_miss")
        .desc("read miss count")
        .flags(total)
        ;

        other_bank_write_access_miss
        .init(1)
        .name(name() + ".other_bank_write_access_miss")
        .desc("write miss count")
        .flags(total)
        ;

    // DR TODO for now, only output stats which are involved in power equations
        total_latency
        .name(name() + ".total_latency")
        .desc("total DRAM latency")
        ;

        total_arb_latency
        .name(name() + ".total_arb_latency")
        .desc("total arbitration latency")
        ;

        avg_latency
        .name(name() + ".avg_latency")
        .desc("average DRAM latency")
        ;

        avg_arb_latency
        .name(name() + ".avg_arb_latency")
        .desc("average arbitration DRAM latency")
        ;

        bank_access_profile
        .init(num_banks,num_cpus)
        .name(name() + "[cpu][bank]")
        .desc("DRAM bank access profile")
        ;

        total_icache_req
        .name(name() + ".total_icache_req")
        .desc("total number of requests from icache")
        ;

        avg_latency = total_latency / accesses;
        avg_arb_latency = total_arb_latency / accesses;
}




// DR DEBUG: assume we have a 500 MHz CPU and 100 MHz RAM
// static float cpu_ratio = 5; // ratio between CPU speed and memory bus speed
// DR TODO: get this parameter from the simulation

static char *mem_access_output=NULL;
                /* latency of access [CPU cycles]*/
Tick
DRAMMemory::calculateLatency(PacketPtr pkt)
{

  bool cmdIsRead = pkt->isRead();

  int lat=0, temp=0, current_bank=0;
  int current_row=0, current_device=0;

  int was_miss = 0;	// determines if there was an active row miss this access

  //md_addr_t physic_address; /* linear memory address to be accessed */
  Addr physic_address; /* linear memory address to be accessed */

  int num_blocks=0;
  int corrected_overlap, /* overlap of consecutive accesses [CPU cycles] */
    overlap=0;           /* overlap of consecutive accesses [mem bus cycles] */
  int adjacent=0; /* 1 indicates that current bank is adjacent to the last accessed  one*/

  int chunks = (pkt->getSize() + (bus_width - 1)) / bus_width; // burst length
  assert(chunks >0);
  physic_address = pkt->getAddr();

    ///////////////////////////////////////////////////////////////////////////
    // DR added more stats for power modelling
    // NOTE:
    // for DRAM closed-page, automatic precharge after read or write,
    // i.e. whenever idle


    // count number of cycles where dram is not busy, use for CKE low signal
    // calculate as percentage of all clock cycles
    // if busy, do not add to idle count.  Else add cycles since last access
/* #define  SD_NUM_BANKS (SD_STACK_BASE/SD_BANK_SIZE)     */      /* number of banks */
/* #define  SD_NUM_ROWS (SD_BANK_SIZE/SD_ROW_SIZE)       */       /* number of rows per bank */
/*delays until data is ready/written to the memory for the SDRAM*/
        int SD_T_READ_READ_SROW = cas_lat; /* RAR, row hit, current bank  */
        int SD_T_READ_WRITE_SROW = cas_lat; /* RAW, row hit, current bank  */
        int SD_T_WRITE_READ_SROW = war_lat-1; /* WAR, row hit, current bank  */
        int SD_T_WRITE_WRITE_SROW = 0; /* WAW, row hit, current bank  */
        int SD_T_READ_READ_SBANK = (pre_lat+act_lat+cas_lat); /* RAR, row miss, current bank */
        int SD_T_READ_WRITE_SBANK = (pre_lat+act_lat+cas_lat+(dpl_lat-1)); /* RAW, row miss, current bank */
        int SD_T_WRITE_READ_SBANK = (pre_lat+act_lat); /* WAR, row miss, current bank */
        int SD_T_WRITE_WRITE_SBANK = (pre_lat+act_lat+(dpl_lat-1)); /* WAW, row miss, current bank */
        int SD_T_READ_READ_OBANK = (pre_lat+act_lat+cas_lat); /* RAR, row miss, another bank */
        int SD_T_READ_WRITE_OBANK = (pre_lat+act_lat+cas_lat); /* RAW, row miss, another bank */
        int SD_T_WRITE_READ_OBANK = (pre_lat+act_lat); /* WAR, row miss, another bank */
        int SD_T_WRITE_WRITE_OBANK = (pre_lat+act_lat); /* WAW, row miss, another bank */
/* best-case latencies (due to overlap / row hits in another bank) */
        int SD_BEST_T_READ_READ_SROW = 0; /* RAR, row hit, current bank  */
        int SD_BEST_T_READ_READ_SBANK = (act_lat+cas_lat); /* RAR, row miss, current bank */
        int SD_BEST_T_WRITE_READ_SBANK = (act_lat); /* WAR, row miss, current bank */
        int SD_BEST_T_READ_READ_OBANK = 0; /* RAR, row miss/hit, another bank */
        int SD_BEST_T_READ_WRITE_OBANK = cas_lat; /* RAW, row miss/hit, another bank */
        int SD_BEST_T_WRITE_READ_OBANK = (war_lat -1); /* WAR, row miss/hit, another bank */
        int SD_BEST_T_WRITE_WRITE_OBANK = 0; /* WAW, row miss/hit, another bank */

    Tick time_since_last_access = curTick-time_last_access;
    Tick time_last_miss = 0;	// used for keeping track of times between activations (page misses)
    //int was_idle = (curTick > busy_until);
        bool srow_flag = false;
        int timing_correction = 0;

    int was_idle = (curTick > busy_until[current_bank]);
    cycles_nCKE[0] += was_idle ? MIN(curTick-busy_until[current_bank], time_since_last_access) : 0;

    // bank is precharged
    //active_row[current_bank] == DR_NUM_ROWS
    int all_precharged = 1;
    int bank_max = num_banks;
    int row_max = num_rows;

    if( (mem_type == "SDRAM") && (mem_actpolicy == "closed") ) {
        // SDRAM does not use the active_row array in closed_page mode
        // TODO: handle closed page operation

    } else {		// DRDRAM uses the active_row array
        for( int i = 0; i < bank_max; i++ ) {
                if( (active_row[current_bank] != row_max)) all_precharged = 0;
        }
    }

    if(all_precharged) {
        if(was_idle) {
                cycles_all_precharge_nCKE[0] += MIN(curTick-busy_until[current_bank], time_since_last_access);
                cycles_all_precharge_CKE[0] += MIN(0, busy_until[current_bank]-time_last_access);
        }
        else {
                cycles_all_precharge_CKE[0] += time_since_last_access;
        }
    } else { // some bank is active
        if(was_idle) {
                cycles_bank_active_nCKE[0] += MIN(curTick-busy_until[current_bank], time_since_last_access);
        }
        else {
        }
    }

    if( cmdIsRead ) {
        cycles_read_out[0] += chunks;
    } else {
        cycles_write_in[0] += chunks;
    }


    time_last_access = curTick;
    ////////////////////////////////////////////////////////////////////////////

    if ((mem_type == "SDRAM") && (mem_actpolicy == "open"))
      {
        /* Split transaction on m5 makes it challenging to  */
        /* model the DRAM. A single cycle latency is assumed */
        /* for dequeueing an address bus request. In response to  */
        /* that, the current DRAM implementation assumes that a */
        /* seperate DRAM command generator / controller exists per */
        /* bank and the dequeued addresses are queued to these */
        /* controllers. We can view this as an ideal scenario for */
        /* a shared DRAM command generator / controller with */
        /* support for overlapping DRAM commands. */
        /* Compare DRAM PRE,ACT,CAS etc. latencies, DRAM clock  */
        /* frequency and the number of banks to determine whether */
        /* the ideal scenario with a shared DRAM command generator */
        /* is equivalent to having multiple DRAM command generators */
        /* per bank */
        if ((memctrladdr_type != "interleaved"))/* i.e. mc_type is linear */
          {
            current_bank=physic_address/bank_size;
            temp=physic_address-current_bank*bank_size;/*address in bank*/
            current_row=temp/SD_ROW_SIZE;
          }
        else/* mc_type interleaved */
          /* This memory controller maps the addresses differently
           * depending on the row_size, every row is mapped to another
           * bank. Thus, the text segment uses half of every bank, the heap
           * the next quarter of each bank, and the stack the rest.
           */

          {
            num_blocks = physic_address/SD_ROW_SIZE; /* row number */
            current_bank=num_blocks%num_banks;
            current_row=num_blocks/num_banks;
          }

        if (mem_access_details == true)
          {
                // DR TODO
            //fprintf(mem_accessfd,"       %09u  %4d   %3d\n",physic_address,current_row,current_bank);
          }
        else
          {
            if (mem_access_output!=0)
              {
                //fprintf(mem_accessfd,"\n");
              }
          }
        total_access++;

        if (memctrlpipe_enable == true)
          {
            overlap=(int)(busy_until[current_bank] - curTick);
          }
        else overlap = 0;

        if (cpu_ratio < 1.0)
          {
            corrected_overlap = overlap*((int)(1/cpu_ratio)); /* floor */
          }
        else
          {
            corrected_overlap = (int) (overlap/cpu_ratio);
          }

        /*fprintf(stderr,"%10.0f %10.0f %4d %4d ",(double)busy_until, (double)curTick, overlap, corrected_overlap); debugging*/

        if (cmdIsRead == lastCmdIsRead)/*same command*/
          {
            if (current_bank == last_bank)/*same bank*/
              {
                if (current_row == last_row)/*same row*/
                  {
                          /* Page Hit */
                    if (cmdIsRead)
                      {
                        if (corrected_overlap > 0)/*overlapping*/
                          {
                            /*best case*/
                            if (corrected_overlap >= cas_lat)
                              {
                                lat=SD_BEST_T_READ_READ_SROW;
                                srow_flag = true;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = cas_lat-corrected_overlap;
                                srow_flag = true;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else
                          {
                            /*worst case*/
                            lat = SD_T_READ_READ_SROW;
                                srow_flag = true;
                            worst_case++;
                          }
                        same_row_read_access++;
                        srr_after_read++;
                      }
                    else/*write*/
                      {/*no option case*/
                        lat = SD_T_WRITE_WRITE_SROW;
                        srow_flag = true;
                        same_row_write_access++;
                        srw_after_write++;
                        worst_case++;
                      }
                  }
                else /*other row in same bank*/
                  {
                        /* Page miss */
                    if (cmdIsRead)
                      {
                        if (corrected_overlap > 0)/*overlapping*/
                          {
                            if (corrected_overlap >= pre_lat)/*best case*/
                              {
                                lat = SD_BEST_T_READ_READ_SBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = SD_T_READ_READ_SBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = SD_T_READ_READ_SBANK;
                            worst_case++;
                          }
                        same_bank_read_access++;
                        sbr_after_read++;
                      }
                    else/*write*/
                      {/*no option case*/
                        lat = SD_T_WRITE_WRITE_SBANK;
                        same_bank_write_access++;
                        sbw_after_write++;
                        worst_case++;
                      }
                  }
              }
            else /*other bank*/
              {
                if (cmdIsRead)
                  {
                        /* Page empty */
                    if (current_row == active_row[current_bank])/*row is still active*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if(corrected_overlap >= pre_lat)/*best case*/
                              {
                                lat = SD_BEST_T_READ_READ_OBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = SD_T_READ_READ_OBANK - corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*in between case*/
                          {
                            lat = SD_T_READ_READ_OBANK;
                            in_between_case++;
                          }
                        other_bank_read_access_hit++;
                        obr_after_read_hit++;
                      }
                    else/*row is not active*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if(corrected_overlap >= SD_T_READ_READ_OBANK )/*best case*/
                              {
                                lat = SD_BEST_T_READ_READ_OBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = SD_T_READ_READ_OBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = SD_T_READ_READ_OBANK;
                            worst_case++;
                          }

                          // DR keep track of time between misses
                          was_miss = 1;

                        other_bank_read_access_miss[0]++;
                        obr_after_read_miss++;
                      }
                  }
                else/*write*/
                  {
                    if (current_row == active_row[current_bank])/*row is still active*/
                      { /*best_case*/
                        lat = SD_BEST_T_WRITE_WRITE_OBANK;
                        best_case++;
                        other_bank_write_access_hit++;
                        obw_after_write_hit++;
                      }
                    else/*row is not active*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if(corrected_overlap >=SD_T_WRITE_WRITE_OBANK)/*best case*/
                              {
                                lat = SD_BEST_T_WRITE_WRITE_OBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = SD_T_WRITE_WRITE_OBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = SD_T_WRITE_WRITE_OBANK;
                            worst_case++;
                          }

                          // DR keep track of time between misses
                          was_miss = 1;

                        other_bank_write_access_miss[0]++;
                        obw_after_write_miss++;

                      }
                  }
              }
          }
        else /*lastCmdIsRead != cmdIsRead*/
          {
            if (current_bank == last_bank)/*same bank*/
              {
                if (current_row == last_row)/*same row*/
                  {
                        /* Page Hit */
                    if (cmdIsRead)
                      {/*worst case*/
                        lat = SD_T_READ_WRITE_SROW;
                        srow_flag = true;
                        same_row_read_access++;
                        srr_after_write++;
                        worst_case++;
                      }
                    else/*write*/
                      {/*worst case*/
                        lat = SD_T_WRITE_READ_SROW;
                        srow_flag = true;
                        same_row_write_access++;
                        srw_after_read++;
                        worst_case++;
                      }
                  }
                else /*other row in same bank*/
                  {
                        /* Page Miss */
                    if (cmdIsRead)
                      {/*worst case*/
                        lat = SD_T_READ_WRITE_SBANK;
                        same_bank_read_access++;
                        sbr_after_write++;
                        worst_case++;
                      }
                    else/*write*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if (corrected_overlap >= pre_lat)/*best case*/
                              {
                                lat = SD_BEST_T_WRITE_READ_SBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = SD_T_WRITE_READ_SBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = SD_T_WRITE_READ_OBANK;
                            worst_case++;
                          }
                        same_bank_write_access++;
                        sbw_after_read++;
                      }
                  }
              }
            else /*other bank*/
              {
                        /* Page empty */
                if (cmdIsRead)
                  {
                    if (current_row == active_row[current_bank])/*row is still active*/
                      { /*best case*/
                        lat = SD_BEST_T_READ_WRITE_OBANK;
                        best_case++;
                        other_bank_read_access_hit++;
                        obr_after_write_hit++;
                      }
                    else/*row is not active*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if(corrected_overlap >= (pre_lat+act_lat))/*best case*/
                              {
                                lat = SD_BEST_T_READ_WRITE_OBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = SD_T_READ_WRITE_OBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = SD_T_READ_WRITE_OBANK;
                            worst_case++;
                          }
                          // DR keep track of time between misses
                          was_miss = 1;

                        other_bank_read_access_miss[0]++;
                        obr_after_write_miss++;
                      }
                  }
                else/*write*/
                  {
                    if (current_row == active_row[current_bank])/*row is still active*/
                      { /*best case*/
                        lat = SD_BEST_T_WRITE_READ_OBANK;
                        best_case++;
                        other_bank_write_access_hit++;
                        obw_after_read_hit++;
                      }
                    else/*row is not active*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if (corrected_overlap >= (SD_T_WRITE_READ_OBANK-SD_BEST_T_WRITE_READ_OBANK))/*best case*/
                              {/*best case*/
                                lat = SD_BEST_T_WRITE_READ_OBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = SD_T_WRITE_READ_OBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = SD_T_WRITE_READ_OBANK;
                            worst_case++;
                          }

                          // DR keep track of time between misses
                          was_miss = 1;

                        other_bank_write_access_miss[0]++;
                        obw_after_read_miss++;
                      }
                  }
              }
          }
        /*fprintf(stderr,"%4d %4d ",lat,active_row[current_bank]);debugging*/

        lat += chunks; /* burst length added*/
        if(srow_flag == false)
                timing_correction = cpu_ratio*(trc_lat - pre_lat - act_lat - cas_lat - 1);


        /*fprintf(stderr,"%4d ",lat);debugging*/

        active_row[current_bank]=current_row; /* open-page (hit) register */
        lastCmdIsRead = cmdIsRead;
        last_bank = current_bank;
        last_row  = current_row;

        if (cpu_ratio < 1.0)
          {
            lat = (lat+((int)(1/cpu_ratio)-1))/(int)(1/cpu_ratio);
          }
        else
          {
            temp = (int)(lat*cpu_ratio);
            lat = (lat*cpu_ratio == temp)?temp:(temp+1); /*round up*/
          }

        /*fprintf(stderr,"%4d \n",lat);debugging*/

        if (overlap <= 0) /*memory interface is not busy*/
          {
            if (memctrlpipe_enable == true)
              {
              busy_until[current_bank]=curTick+lat+
                        timing_correction;
              }
            else
              {
                if (busy_until[current_bank] >= curTick)
                  {
                    busy_until[current_bank]+=(lat+
                                timing_correction);
                        total_arb_latency += (busy_until[current_bank]
                                - curTick - lat
                                - timing_correction);
                    lat=busy_until[current_bank] - curTick;
                  }
                else busy_until[current_bank]=curTick+lat+
                        timing_correction;
              }
          }
        else/*the memory request will be satisfied temp cycles after curTick*/
          {
            busy_until[current_bank] +=(lat+
                        timing_correction);
            command_overlapping++;
            lat+=overlap;
                total_arb_latency += overlap;
          }

          // DR for power stats
          if( was_miss ) {
                cycles_between_misses[0] += (busy_until[current_bank] - time_last_miss);
                time_last_miss = busy_until[current_bank];
          }
        // cout <<"cpu id = " << _cpu_num << "current_bank = " << current_bank << endl;
        // if((_cpu_num < num_cpus) && (_cpu_num >= 0))
    //	bank_access_profile[_cpu_num][current_bank]++;

        return lat;
      }


    /***********************************************************/
    /********************     DRDRAM     ***********************/
    /***********************************************************/

    else if ((mem_type == "DRDRAM") && (mem_actpolicy == "open"))/*DRDRAM*/ /*a closed bank has an activ_row number of DR_NUM_ROWS: highest +1*/
      {
        if ((memctrladdr_type != "interleaved"))/* i.e. mc_type is linear */
          {
            current_bank=physic_address/DR_BANK_SIZE;
            temp=physic_address-current_bank*DR_BANK_SIZE;/*address in bank*/
            current_row=temp/DR_ROW_SIZE;
            current_device=current_bank/(DR_NUM_BANKS/DR_NUM_DEVS);
          }

        else/*mc_type interleaved*/
          /* This memory controller maps the addresses differently
           * depending on the row-size, every row is mapped to another
           * bank. So the text segment uses half of every bank. The heap
           * the next quarter of each bank and the stack the rest.
           */

          {
            num_blocks = physic_address/DR_ROW_SIZE; /* row number */
            current_bank=(num_blocks%DR_NUM_BANKS)*2; /*every 'second' bank will be used*/
            /*banks above DR_NUM_BANKS are the uneven banks*/
            current_bank = ((current_bank <  DR_NUM_BANKS) ? current_bank:(current_bank - DR_NUM_BANKS+1));
            current_row=num_blocks/DR_NUM_BANKS;
            current_device=current_bank/(DR_NUM_BANKS/DR_NUM_DEVS);
          }
        if (abs(current_bank-last_bank)==1)/*access to an adjacent bank*/
          {
            if (!((current_bank%DR_BANK_SAMP == (DR_BANK_SAMP-1))&&(last_bank%DR_BANK_SAMP == 0))/*not 15/16 (current/last)*/
                &&(!((last_bank%DR_BANK_SAMP == (DR_BANK_SAMP-1))&&(current_bank%DR_BANK_SAMP == 0))))/*not 16/15(current/last)*/
              {
                adjacent_access++;
                adjacent=1;/*an adjacent bank is accessed*/
                if (cmdIsRead)
                  adjacent_read++;
                else
                  adjacent_write++;
              }
          }
        precharge=0;/*at this moment no bank needs to be precharged*/
        if (active_row[current_bank] == DR_NUM_ROWS)/*bank is precharged*/
          {
            if (prechargeBanksAround(current_bank)> 0)/*a bank next to the current is activated*/
              {
                if ((adjacent==1)&&(precharge==1))
                  {
                    /*since adjacent banks share SAMPs, this access would be the same as (in terms of latency)
                     *an access to another row in the same bank if only one adjacent bank was active*/
                    last_bank = current_bank;
                    last_row = current_row+1;
                    precharge=0;/*set to 0 for next memory access*/
                  }
              }
          }
        if (mem_access_details == true)
          {
            //fprintf(mem_accessfd,"       %09u  %4d   %3d  %15d\n",physic_address,current_row,current_bank,(int)adjacent_access);
          }
        else
          {
            if (mem_access_output!=NULL)
              {
                //fprintf(mem_accessfd,"\n");
              }
          }
        total_access++;

        if (memctrlpipe_enable == true)
          {
            overlap=(int)(busy_until[current_bank] - curTick);
          }
        else overlap=0;

        if (cpu_ratio < 1.0)
          {
            corrected_overlap = overlap*((int)(1/cpu_ratio)); /* floor */
          }
        else
          {
            corrected_overlap = (int) (overlap/cpu_ratio);
          }

        /*fprintf(stderr,"%10.0f %10.0f %6d %6d %2d %2d ",(double)busy_until, (double)curTick, overlap, corrected_overlap,precharge,adjacent);debugging*/

        if (cmdIsRead == lastCmdIsRead)/*same command*/
          {
            if (current_bank == last_bank)/*same bank*/
              {
                if (current_row == last_row)/*same row*/
                  {
                    if (cmdIsRead)
                      {
                        if (corrected_overlap > 0)/*overlapping*/
                          {
                            /*best case*/
                            if (corrected_overlap >= DR_T_READ_READ_SROW)
                              {
                                lat=DR_BEST_T_READ_READ_SROW;
                                srow_flag = true;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_READ_READ_SROW-corrected_overlap;
                                srow_flag = true;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else
                          {
                            /*worst case*/
                            lat = DR_T_READ_READ_SROW;
                                srow_flag = true;
                            worst_case++;
                          }
                        same_row_read_access++;
                        srr_after_read++;
                      }
                    else/*write, always retire the previous data*/
                      {
                        if (corrected_overlap > 0)/*overlapping*/
                          {
                            /*best case*/
                            if (corrected_overlap >= DR_T_OWR)
                              {
                                lat=DR_BEST_T_WRITE_WRITE_SROW;
                                srow_flag = true;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_WRITE_WRITE_SROW-corrected_overlap;
                                srow_flag = true;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else
                          {
                            /*worst case*/
                            lat = DR_T_WRITE_WRITE_SROW;
                                srow_flag = true;
                            worst_case++;
                          }
                        same_row_write_access++;
                        srw_after_write++;
                      }
                  }
                else /*other row in same bank*/
                  {
                    if (cmdIsRead)
                      {
                        if (corrected_overlap > 0)/*overlapping*/
                          {
                            if (corrected_overlap >= DR_T_HELP)/*best case*/
                              {
                                lat = DR_BEST_T_READ_READ_SBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_READ_READ_SBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = DR_T_READ_READ_SBANK;
                            worst_case++;
                          }
                        same_bank_read_access++;
                        sbr_after_read++;
                      }
                    else/*write*/
                      {
                        if (corrected_overlap > 0)/*overlapping*/
                          {
                            if (corrected_overlap >= DR_T_OWR)/*best case*/
                              {
                                lat = DR_BEST_T_WRITE_WRITE_SBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_WRITE_WRITE_SBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = DR_T_WRITE_WRITE_SBANK;
                            worst_case++;
                          }
                        same_bank_write_access++;
                        sbw_after_write++;
                      }
                  }
              }
            else /*other bank*/
              {
                if (cmdIsRead)
                  {
                    if (current_row == active_row[current_bank])/*row is still active*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if(corrected_overlap >= (DR_T_CAC+DR_T_PACKET))/*best case*/
                              {
                                lat = DR_BEST_T_READ_READ_OBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_CAC+DR_T_PACKET-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*in between case*/
                          {
                            lat = DR_T_CAC+DR_T_PACKET;
                            in_between_case++;
                          }
                        other_bank_read_access_hit++;
                        obr_after_read_hit++;
                      }
                    else/*row is not active or bank is precharged/not active*/
                      {
                        if (active_row[current_bank]!=DR_NUM_ROWS)/*row is not active, but bank is active*/
                          {
                            if (corrected_overlap > 0 )/*overlapping*/
                              {
                                if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET))/*best case*/
                                  {
                                    lat = DR_BEST_T_READ_READ_OBANK;
                                    best_case++;
                                    full_overlapping++;
                                  }
                                else/*in between case*/
                                  {
                                    lat = DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET-corrected_overlap;
                                    in_between_case++;
                                    partial_overlapping++;
                                  }
                              }
                            else/*worst case*/
                              {
                                lat = DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET;
                                in_between_case++;
                              }
                          }
                        else/*current_row == DR_NUM_ROWS:precharged or inactive*/
                          {
                            if(precharge == 0)/*no adjacent bank is active*/
                              {
                                if (corrected_overlap > 0 )/*overlapping*/
                                  {
                                    if(corrected_overlap >= (DR_T_RCD+DR_T_CAC+DR_T_PACKET))/*best case*/
                                      {
                                        lat = DR_BEST_T_READ_READ_OBANK;
                                        best_case++;
                                        full_overlapping++;
                                      }
                                    else/*in between case*/
                                      {
                                        lat = DR_T_RCD+DR_T_CAC+DR_T_PACKET-corrected_overlap;
                                        in_between_case++;
                                        partial_overlapping++;
                                      }
                                  }
                                else/*worst case*/
                                  {
                                    lat = DR_T_RCD+DR_T_CAC+DR_T_PACKET;
                                    in_between_case++;
                                  }
                              }
                            else/*one ore two adjacent banks are active*/
                              {
                                if (precharge == 1)
                                  {
                                    if (corrected_overlap > 0 )/*overlapping*/
                                      {
                                        if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET))/*best case*/
                                          {
                                            lat = DR_BEST_T_READ_READ_OBANK;
                                            best_case++;
                                            full_overlapping++;
                                          }
                                        else/*in between case*/
                                          {
                                            lat = (DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET)-corrected_overlap;
                                            in_between_case++;
                                            partial_overlapping++;
                                          }
                                      }
                                    else/*worst case*/
                                      {
                                        lat = (DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET);
                                        in_between_case++;
                                      }
                                  }
                                else /*precharge ==2: two rows must be precharged*/
                                  {
                                    if (adjacent == 1)/*these banks are adjacent*/
                                      {
                                        if (corrected_overlap > 0 )/*overlapping*/
                                          {
                                            if(corrected_overlap >= DR_T_PP+2*DR_T_PACKET-DR_T_RDP+DR_T_CAC)/*best case*/
                                              {
                                                lat = DR_T_RDP+DR_T_RP+DR_T_RCD-DR_T_PACKET;
                                                in_between_case++;
                                                full_overlapping++;
                                              }
                                            else/*in between case*/
                                              {
                                                lat = DR_T_READ_READ_OBANK-corrected_overlap;
                                                in_between_case++;
                                                partial_overlapping++;
                                              }
                                          }
                                        else/*worst case*/
                                          {
                                            lat = DR_T_READ_READ_OBANK;
                                            worst_case++;
                                          }
                                      }
                                    else/*adjacent == 0: two not adjacent banks need to be precharged*/
                                      {
                                        if (corrected_overlap > 0 )/*overlapping*/
                                          {
                                            if(corrected_overlap >= DR_T_READ_READ_OBANK)/*best case*/
                                              {
                                                lat = DR_BEST_T_READ_READ_OBANK;
                                                best_case++;
                                                full_overlapping++;
                                              }
                                            else/*in between case*/
                                              {
                                                lat = DR_T_READ_READ_OBANK-corrected_overlap;
                                                in_between_case++;
                                                partial_overlapping++;
                                              }
                                          }
                                        else/*worst case*/
                                          {
                                            lat = DR_T_READ_READ_OBANK;
                                            worst_case++;
                                          }
                                      }
                                  }
                              }
                          }
                        other_bank_read_access_miss[0]++;
                        obr_after_read_miss++;
                      }
                  }
                else/*write*/
                  {
                    if (current_row == active_row[current_bank])/*row is still active*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if(corrected_overlap >= (DR_T_CWD+DR_T_PACKET))/*best case*/
                              {
                                lat = DR_BEST_T_WRITE_WRITE_OBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_CWD+DR_T_PACKET-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = DR_T_CWD+DR_T_PACKET;
                            in_between_case++;
                          }
                        other_bank_write_access_hit++;
                        obw_after_write_hit++;
                      }
                    else/*row is not active or bank is precharged/not active*/
                      {
                        if (active_row[current_bank] != DR_NUM_ROWS)/*row is not active,but bank is active*/
                          {
                            if (corrected_overlap > 0 )/*overlapping*/
                              {
                                if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                  {
                                    lat = DR_BEST_T_WRITE_WRITE_OBANK;
                                    best_case++;
                                    full_overlapping++;
                                  }
                                else/*in between case*/
                                  {
                                    lat = DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET-corrected_overlap;
                                    in_between_case++;
                                    partial_overlapping++;
                                  }
                              }
                            else/*worst case*/
                              {
                                lat = DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET;
                                in_between_case++;
                              }
                          }
                        else/*current_row == DR_NUM_ROWS:precharged or inactive*/
                          {
                            if(precharge == 0)/*no adjacent bank is active*/
                              {
                                if (corrected_overlap > 0 )/*overlapping*/
                                  {
                                    if(corrected_overlap >= (DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                      {
                                        lat = DR_BEST_T_WRITE_WRITE_OBANK;
                                        best_case++;
                                        full_overlapping++;
                                      }
                                    else/*in between case*/
                                      {
                                        lat = DR_T_RCD+DR_T_CWD+DR_T_PACKET-corrected_overlap;
                                        in_between_case++;
                                        partial_overlapping++;
                                      }
                                  }
                                else/*worst case*/
                                  {
                                    lat = DR_T_RCD+DR_T_CWD+DR_T_PACKET;
                                    in_between_case++;
                                  }
                              }
                            else/*one ore two adjacent banks are active*/
                              {
                                if (precharge == 1)/*last_bank is no adjacent*/
                                  {
                                    if (corrected_overlap > 0 )/*overlapping*/
                                      {
                                        if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                          {
                                            lat = DR_BEST_T_WRITE_WRITE_OBANK;
                                            best_case++;
                                            full_overlapping++;
                                          }
                                        else/*in between case*/
                                          {
                                            lat = (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET)-corrected_overlap;
                                            in_between_case++;
                                            partial_overlapping++;
                                          }
                                      }
                                    else/*worst case*/
                                      {
                                        lat = (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET);
                                        in_between_case++;
                                      }
                                  }
                                else /*precharge ==2: two rows have to be precharged*/
                                  {
                                    if (adjacent == 1)/*these banks are adjacent*/
                                      {
                                        if (corrected_overlap > 0 )/*overlapping*/
                                          {
                                            if(corrected_overlap >= DR_T_OWR+DR_T_PP)/*best case*/
                                              {
                                                lat = DR_T_WRITE_WRITE_OBANK-DR_T_OWR-DR_T_PP;
                                                in_between_case++;
                                                full_overlapping++;
                                              }
                                            else/*in between case*/
                                              {
                                                lat = DR_T_WRITE_WRITE_OBANK-corrected_overlap;
                                                in_between_case++;
                                                partial_overlapping++;
                                              }
                                          }
                                        else/*worst case*/
                                          {
                                            lat = DR_T_WRITE_WRITE_OBANK;
                                            worst_case++;
                                          }
                                      }
                                    else/*adjacent == 0: two not adjacent banks need to be precharged*/
                                      {
                                        if (corrected_overlap > 0 )/*overlapping*/
                                          {
                                            if(corrected_overlap >= DR_T_WRITE_WRITE_OBANK)/*best case*/
                                              {
                                                lat = DR_BEST_T_WRITE_WRITE_OBANK;
                                                best_case++;
                                                full_overlapping++;
                                              }
                                            else/*in between case*/
                                              {
                                                lat = DR_T_WRITE_WRITE_OBANK-corrected_overlap;
                                                in_between_case++;
                                                partial_overlapping++;
                                              }
                                          }
                                        else/*worst case*/
                                          {
                                            lat = DR_T_WRITE_WRITE_OBANK;
                                            worst_case++;
                                          }
                                      }
                                  }
                              }
                          }
                        other_bank_write_access_miss[0]++;
                        obw_after_write_miss++;
                      }
                  }
              }
          }
        else /*lastCmdIsRead != cmdIsRead*/
          {
            if (current_bank == last_bank)/*same bank*/
              {
                if (current_row == last_row)/*same row*/
                  {
                    if (cmdIsRead)
                      {
                        if (corrected_overlap > 0)/*overlapping*/
                          {
                            /*best case*/
                            if (corrected_overlap >= DR_T_OWR)
                              {
                                lat=DR_BEST_T_READ_WRITE_SROW;
                                srow_flag = true;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_READ_WRITE_SROW-corrected_overlap;
                                srow_flag = true;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else
                          {
                            /*worst case*/
                            lat = DR_T_READ_WRITE_SROW;
                                srow_flag = true;
                            worst_case++;
                          }
                        same_row_read_access++;
                        srr_after_write++;
                      }
                    else/*write*/
                      {
                        if (corrected_overlap > 0)/*overlapping*/
                          {
                            /*best case*/
                            if (corrected_overlap >= DR_T_WRITE_READ_SROW)
                              {
                                lat=DR_BEST_T_WRITE_READ_SROW;
                                srow_flag = true;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_WRITE_READ_SROW-corrected_overlap;
                                srow_flag = true;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else
                          {
                            /*worst case*/
                            lat = DR_T_WRITE_READ_SROW;
                                srow_flag = true;
                            worst_case++;
                          }
                        same_row_write_access++;
                        srw_after_read++;
                      }
                  }
                else /*other row in same bank*/
                  {
                    if (cmdIsRead)
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if (corrected_overlap >= DR_T_OWR)/*best case*/
                              {
                                lat = DR_BEST_T_READ_WRITE_SBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_READ_WRITE_SBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = DR_T_READ_WRITE_SBANK;
                            worst_case++;
                          }
                        same_bank_read_access++;
                        sbr_after_write++;
                      }
                    else/*write*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if (corrected_overlap >= DR_T_HELP)/*best case*/
                              {
                                lat = DR_BEST_T_WRITE_READ_SBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_WRITE_READ_SBANK-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*worst case*/
                          {
                            lat = DR_T_WRITE_READ_SBANK;
                            worst_case++;
                          }
                        same_bank_write_access++;
                        sbw_after_read++;
                      }
                  }
              }
            else /*other bank*/
              {
                if (cmdIsRead)
                  {
                    if (current_row == active_row[current_bank])/*row is still active*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if(last_dev != current_device)
                              {
                                if(corrected_overlap >= DR_T_CWD+DR_T_PACKET)/*best case*/
                                  {
                                    lat = DR_BEST_T_READ_WRITE_ODEV;
                                    best_case++;
                                    full_overlapping++;
                                  }
                                else/*in between case*/
                                  {
                                    lat = DR_T_CAC+DR_T_PACKET-corrected_overlap;
                                    in_between_case++;
                                    partial_overlapping++;
                                  }
                              }
                            else /* same device */
                              {
                                if(corrected_overlap >= DR_T_OWR)/*best case*/
                                  {
                                    lat = DR_BEST_T_READ_WRITE_OBANK;
                                    best_case++;
                                    full_overlapping++;
                                  }
                                else/*in between case*/
                                  {
                                    lat = DR_T_CAC+DR_T_PACKET-corrected_overlap;
                                    in_between_case++;
                                    partial_overlapping++;
                                  }
                              }
                          }
                        else/*in between case, no overlap*/
                          {
                            lat = DR_T_CAC+DR_T_PACKET;
                            in_between_case++;
                          }
                        other_bank_read_access_hit++;
                        obr_after_write_hit++;
                      }

                    else/*row is not active or bank is precharged/not active*/
                      {
                        if (active_row[current_bank] != DR_NUM_ROWS)/*row is not active,but bank is active*/
                          {
                            if (corrected_overlap > 0 )/*overlapping*/
                              {
                                if (last_dev != current_device)
                                  {
                                    if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                      {
                                        lat = DR_BEST_T_READ_WRITE_ODEV;
                                        best_case++;
                                        full_overlapping++;
                                      }
                                    else/*in between case*/
                                      {
                                        lat = DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET-corrected_overlap;
                                        in_between_case++;
                                        partial_overlapping++;
                                      }
                                  }
                                else /* same device */
                                  {
                                    if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_OWR))/*best case*/
                                      {
                                        lat = DR_BEST_T_READ_WRITE_OBANK;
                                        best_case++;
                                        full_overlapping++;
                                      }
                                    else/*in between case*/
                                      {
                                        lat = DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET-corrected_overlap;
                                        in_between_case++;
                                        partial_overlapping++;
                                      }
                                  }
                              }
                            else/*worst case*/
                              {
                                lat = DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET;
                                in_between_case++;
                              }
                          }
                        else/*current_row == DR_NUM_ROWS:precharged or inactive*/
                          {
                            if(precharge == 0)/*no adjacent bank is active*/
                              {
                                if (corrected_overlap > 0 )/*overlapping*/
                                  {
                                    if(last_dev != current_device)
                                      {
                                        if(corrected_overlap >= (DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                          {
                                            lat = DR_BEST_T_READ_WRITE_ODEV;
                                            best_case++;
                                            full_overlapping++;
                                          }
                                        else/*in between case*/
                                          {
                                            lat = DR_T_RCD+DR_T_CAC+DR_T_PACKET-corrected_overlap;
                                            in_between_case++;
                                            partial_overlapping++;
                                          }
                                      }
                                    else /* same device */
                                      {
                                        if(corrected_overlap >= (DR_T_RCD+DR_T_OWR))/*best case*/
                                          {
                                            lat = DR_BEST_T_READ_WRITE_OBANK;
                                            best_case++;
                                            full_overlapping++;
                                          }
                                        else/*in between case*/
                                          {
                                            lat = DR_T_RCD+DR_T_CAC+DR_T_PACKET-corrected_overlap;
                                            in_between_case++;
                                            partial_overlapping++;
                                          }
                                      }
                                  }
                                else/*worst case*/
                                  {
                                    lat = DR_T_RCD+DR_T_CAC+DR_T_PACKET;
                                    in_between_case++;
                                  }
                              }
                            else/*one or two adjacent banks are active*/
                              {
                                if (precharge == 1)/*an adjacent bank (!=last_bank) needs to be precharged*/
                                  {
                                    if (corrected_overlap > 0 )/*overlapping*/
                                      {
                                        if(last_dev != current_device)
                                          {
                                            if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                              {
                                                lat = DR_BEST_T_READ_WRITE_ODEV;
                                                best_case++;
                                                full_overlapping++;
                                              }
                                            else/*in between case*/
                                              {
                                                lat = (DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET)-corrected_overlap;
                                                in_between_case++;
                                                partial_overlapping++;
                                              }
                                          }
                                        else /* same device */
                                          {
                                            if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_OWR))/*best case*/
                                              {
                                                lat = DR_BEST_T_READ_WRITE_OBANK;
                                                best_case++;
                                                full_overlapping++;
                                              }
                                            else/*in between case*/
                                              {
                                                lat = (DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET)-corrected_overlap;
                                                in_between_case++;
                                                partial_overlapping++;
                                              }
                                          }
                                      }
                                    else/*worst case*/
                                      {
                                        lat = (DR_T_RP+DR_T_RCD+DR_T_CAC+DR_T_PACKET);
                                        in_between_case++;
                                      }
                                  }
                                else /*precharge ==2: two rows have to be precharged*/
                                  {
                                    if (adjacent == 1) /* the banks are adjacent */
                                      {
                                        if (corrected_overlap > 0 )/*overlapping*/
                                          {
                                            if(corrected_overlap >= DR_T_OWR + DR_T_PP)/*best case*/
                                              {
                                                lat = DR_T_READ_WRITE_OBANK-DR_T_OWR - DR_T_PP;
                                                in_between_case++;
                                                full_overlapping++;
                                              }
                                            else/*in between case*/
                                              {
                                                lat = DR_T_READ_WRITE_OBANK-corrected_overlap;
                                                in_between_case++;
                                                partial_overlapping++;
                                              }
                                          }
                                        else/*worst case*/
                                          {
                                            lat = DR_T_READ_WRITE_OBANK;
                                            worst_case++;
                                          }
                                      }
                                    else/*adjacent == 0: two not adjacent banks need to be precharged*/
                                      {
                                        if (corrected_overlap > 0 )/*overlapping*/
                                          {
                                            if (last_dev != current_device)
                                              {
                                                if(corrected_overlap >= (DR_T_PP+DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                                  {
                                                    lat = DR_BEST_T_READ_WRITE_ODEV;
                                                    best_case++;
                                                    full_overlapping++;
                                                  }
                                                else/*in between case*/
                                                  {
                                                    lat = DR_T_READ_WRITE_OBANK-corrected_overlap;
                                                    in_between_case++;
                                                    partial_overlapping++;
                                                  }
                                              }
                                            else /* same device */
                                              {
                                                if(corrected_overlap >= (DR_T_PP+DR_T_RP+DR_T_RCD+DR_T_OWR))/*best case*/
                                                  {
                                                    lat = DR_BEST_T_READ_WRITE_OBANK;
                                                    best_case++;
                                                    full_overlapping++;
                                                  }
                                                else/*in between case*/
                                                  {
                                                    lat = DR_T_READ_WRITE_OBANK-corrected_overlap;
                                                    in_between_case++;
                                                    partial_overlapping++;
                                                  }
                                              }
                                          }
                                        else/*worst case*/
                                          {
                                            lat = DR_T_READ_WRITE_OBANK;
                                            worst_case++;
                                          }
                                      }
                                  }
                              }
                          }
                        other_bank_read_access_miss[0]++;
                        obr_after_write_miss++;
                      }
                  }
                else/*write*/
                  {
                    if (current_row == active_row[current_bank])/*row is still active*/
                      {
                        if (corrected_overlap > 0 )/*overlapping*/
                          {
                            if(corrected_overlap >= DR_T_CWD+DR_T_PACKET)/*best case*/
                              {
                                lat = DR_BEST_T_WRITE_READ_OBANK;
                                best_case++;
                                full_overlapping++;
                              }
                            else/*in between case*/
                              {
                                lat = DR_T_CWD+DR_T_PACKET-corrected_overlap;
                                in_between_case++;
                                partial_overlapping++;
                              }
                          }
                        else/*in between case*/
                          {
                            lat = DR_T_CWD+DR_T_PACKET;
                            in_between_case++;
                          }
                        other_bank_write_access_hit++;
                        obw_after_read_hit++;
                      }
                    else/*row is not active or bank is precharged/not active*/
                      {
                        if (active_row[current_bank] != DR_NUM_ROWS)/*row is not active,but bank is active*/
                          {
                            if (corrected_overlap > 0 )/*overlapping*/
                              {
                                if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                  {
                                    lat = DR_BEST_T_WRITE_READ_OBANK;
                                    best_case++;
                                    full_overlapping++;
                                  }
                                else/*in between case*/
                                  {
                                    lat = DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET-corrected_overlap;
                                    in_between_case++;
                                    partial_overlapping++;
                                  }
                              }
                            else/*worst case*/
                              {
                                lat = DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET;
                                in_between_case++;
                              }
                          }
                        else/*current_row == DR_NUM_ROWS:precharged or inactive*/
                          {
                            if(precharge == 0)/*no adjacent bank is active*/
                              {
                                if (corrected_overlap > 0 )/*overlapping*/
                                  {
                                    if(corrected_overlap >= (DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                      {
                                        lat = DR_BEST_T_WRITE_READ_OBANK;
                                        best_case++;
                                        full_overlapping++;
                                      }
                                    else/*in between case*/
                                      {
                                        lat = DR_T_RCD+DR_T_CWD+DR_T_PACKET-corrected_overlap;
                                        in_between_case++;
                                        partial_overlapping++;
                                      }
                                  }
                                else/*worst case*/
                                  {
                                    lat = DR_T_RCD+DR_T_CWD+DR_T_PACKET;
                                    in_between_case++;
                                  }
                              }
                            else/*one or two adjacent banks are active*/
                              {
                                if (precharge == 1)/*an adjacent bank (!=last_bank)  needs to be precharged first*/
                                  {
                                    if (corrected_overlap > 0 )/*overlapping*/
                                      {
                                        if(corrected_overlap >= (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET))/*best case*/
                                          {
                                            lat = DR_BEST_T_WRITE_READ_OBANK;
                                            best_case++;
                                            full_overlapping++;
                                          }
                                        else/*in between case*/
                                          {
                                            lat = (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET)-corrected_overlap;
                                            in_between_case++;
                                            partial_overlapping++;
                                          }
                                      }
                                    else/*worst case*/
                                      {
                                        lat = (DR_T_RP+DR_T_RCD+DR_T_CWD+DR_T_PACKET);
                                        in_between_case++;
                                      }
                                  }
                                else /*precharge ==2: two rows have to be precharged*/
                                  {
                                    if (adjacent == 1)/*these banks are adjacent*/
                                      {
                                        if (corrected_overlap > 0 )/*overlapping*/
                                          {
                                            if(corrected_overlap >= DR_T_PP-DR_T_RDP+2*DR_T_PACKET+DR_T_CAC)/*best case*/
                                              {
                                                lat = DR_T_WRITE_READ_OBANK-(DR_T_PP-DR_T_RDP+2*DR_T_PACKET+DR_T_CAC);
                                                in_between_case++;
                                                full_overlapping++;
                                              }
                                            else/*in between case*/
                                              {
                                                lat = DR_T_WRITE_READ_OBANK-corrected_overlap;
                                                in_between_case++;
                                                partial_overlapping++;
                                              }
                                          }
                                        else/*worst case*/
                                          {
                                            lat = DR_T_WRITE_READ_OBANK;
                                            worst_case++;
                                          }
                                      }
                                    else/*adjacent == 0: two not adjacent banks need to be precharged*/
                                      {
                                        if (corrected_overlap > 0 )/*overlapping*/
                                          {
                                            if(corrected_overlap >= DR_T_WRITE_READ_OBANK)/*best case*/
                                              {
                                                lat = DR_BEST_T_WRITE_READ_OBANK;
                                                best_case++;
                                                full_overlapping++;
                                              }
                                            else/*in between case*/
                                              {
                                                lat = DR_T_WRITE_READ_OBANK-corrected_overlap;
                                                in_between_case++;
                                                partial_overlapping++;
                                              }
                                          }
                                        else/*worst case*/
                                          {
                                            lat = DR_T_WRITE_READ_OBANK;
                                            worst_case++;
                                          }
                                      }
                                  }
                              }
                          }
                        other_bank_write_access_miss[0]++;
                        obw_after_read_miss++;
                      }
                  }
              }
          }
        /*fprintf(stderr,"%4d %4d ",lat,active_row[current_bank]);debugging*/

        lat += chunks * DR_T_PACKET; /*every 128 bit need DR_NUM_CYCLES*/

        /*fprintf(stderr,"%4d ",lat);debugging*/

        if (cpu_ratio < 1.0)
          {
            lat = (lat+((int)(1/cpu_ratio)-1))/(int)(1/cpu_ratio);
          }
        else
          {
            temp = (int)(lat*cpu_ratio);
            lat = (lat*cpu_ratio == temp)?temp:(temp+1); /*round up*/
          }

        active_row[current_bank]=current_row;
        lastCmdIsRead = cmdIsRead;
        last_bank=current_bank;
        last_row = current_row;
        last_dev = current_device;

        /*fprintf(stderr,"%4d \n",lat);debugging*/

        if (overlap <= 0) /*memory interface is not busy*/
          {
            if (memctrlpipe_enable == true)
              {
                busy_until[current_bank] =curTick+lat;
              }
            else
              {
                if (busy_until[current_bank] >= curTick)
                  {
                    busy_until[current_bank] +=lat;
                    lat=busy_until[current_bank] - curTick;
                  }
                else busy_until[current_bank] = curTick+lat;
              }
          }
        else/*the memory request will be satisfied temp cycles after curTick*/
          {
            busy_until[current_bank] +=lat;
            command_overlapping++;
            lat+=overlap;
          }

        // if((_cpu_num < num_cpus) && (_cpu_num >= 0))
                // cout <<"cpu id = " << _cpu_num << "current_bank = " << current_bank << endl;
    //	bank_access_profile[_cpu_num][current_bank]++;
        return lat;
      }

    /******************************************************************/

        else if ((mem_type== "SDRAM") && (mem_actpolicy == "closed") && (memctrlpipe_enable == true))
           /* SDRAM closed-page with overlap, 2/00 MG */
      {
        if ((memctrladdr_type != "interleaved"))/* i.e. mc_type is linear*/
          {
            // current_bank=physic_address/SD_BANK_SIZE;
            current_bank=physic_address/bank_size;
          }
        else/*mc_type interleaved*/
          /* This memory management unit maps the addresses different
           * depending on the row_size, every row is mapped to another
           * bank. So the text segment uses half of every bank. The heap
           * the next quarter of each bank and the stack the rest.
           */
          {
            num_blocks = physic_address/SD_ROW_SIZE; /* row number */
            // current_bank=num_blocks%SD_NUM_BANKS;
            current_bank=num_blocks%num_banks;
          }

        if (mem_access_details == true)
          {
            //fprintf(mem_accessfd,"       %09u   %3d\n",physic_address,current_bank);
          }
        else
          {
            if (mem_access_output!=NULL)
              {
                //fprintf(mem_accessfd,"\n");
              }
          }
        total_access++;

        overlap=(int)(busy_until[current_bank] - curTick);

        if (current_bank == last_bank)/*same bank*/
          {
            if ((lastCmdIsRead == cmdIsRead) && (cmdIsRead))/* RAR */
              {
                lat = act_lat + cas_lat;
              }
            else if ((lastCmdIsRead == cmdIsRead) && (!cmdIsRead)) /* WAW */
              {
                lat = act_lat;
              }
            else if ((lastCmdIsRead != cmdIsRead) && (cmdIsRead)) /* RAW */
              {
                lat = act_lat + cas_lat;
              }
            else /* WAR */
              {
                lat = act_lat;
              }
          }
        else /* other bank */
          {
            if (cpu_ratio < 1.0)
              {
                corrected_overlap = overlap*((int)(1/cpu_ratio)); /* floor */
              }
            else
              {
                corrected_overlap = (int) (overlap/cpu_ratio);
              }

            if ((lastCmdIsRead == cmdIsRead) && (cmdIsRead))/* RAR */
              {
                if (corrected_overlap > act_lat + cas_lat)
                  {
                    lat = 0;
                    best_case++;
                    full_overlapping++;
                  }
                else if (corrected_overlap > 0)
                  {
                    lat = act_lat + cas_lat - corrected_overlap;
                    in_between_case++;
                    partial_overlapping++;
                  }
                else
                  {
                    lat = act_lat + cas_lat;
                    worst_case++;
                  }
              }
            else if ((lastCmdIsRead == cmdIsRead) && (!cmdIsRead)) /* WAW */
              {
                if (corrected_overlap > act_lat + pre_lat + (dpl_lat-1))
                  {
                    lat = - pre_lat - dpl_lat +1;
                    best_case++;
                    full_overlapping++;
                  }
                else if (corrected_overlap > 0)
                  {
                    lat = act_lat - corrected_overlap;
                    in_between_case++;
                    partial_overlapping++;
                  }
                else
                  {
                    lat = act_lat;
                    worst_case++;
                  }
              }
            else if ((lastCmdIsRead != cmdIsRead) && (cmdIsRead)) /* RAW */
              {
                if (corrected_overlap > cas_lat + pre_lat + dpl_lat - 1 )
                  {
                    lat = act_lat - (pre_lat + dpl_lat - 1);
                    best_case++;
                    partial_overlapping++;
                  }
                else if (corrected_overlap > 0)
                  {
                    lat = act_lat + cas_lat - corrected_overlap;
                    in_between_case++;
                    partial_overlapping++;
                  }
                else
                  {
                    lat = act_lat + cas_lat;
                    worst_case++;
                  }
              }
            else /* WAR */
              {
                if (corrected_overlap > act_lat - (dpl_lat-1))
                  {
                    lat = dpl_lat-1;
                    best_case++;
                    partial_overlapping++;
                  }
                else if (corrected_overlap > 0)
                  {
                    lat = act_lat - corrected_overlap;
                    in_between_case++;
                    partial_overlapping++;
                  }
                else
                  {
                    lat = act_lat;
                    worst_case++;
                  }
              }
          }
        lastCmdIsRead = cmdIsRead;
        last_bank=current_bank;
        last_row = current_row;

        lat += chunks;

        if (cpu_ratio < 1.0)
          {
            lat = (lat+((int)(1/cpu_ratio)-1))/(int)(1/cpu_ratio);
          }
        else
          {
            temp = (int)(lat*cpu_ratio);
            lat = (lat*cpu_ratio == temp)?temp:(temp+1); /*round up*/
          }

        /*fprintf(stderr,"%4d \n",lat); debugging */

        if (overlap <= 0) /*memory interface is not busy*/
          {
            busy_until[current_bank] = curTick+lat;
          }
        else /*the memory request will be satisfied temp cycles after curTick*/
          {
            busy_until[current_bank] +=lat;
            command_overlapping++;
                total_arb_latency += overlap;
            lat+=overlap;
          }
        if (!cmdIsRead)
          {
           temp = (int)(((dpl_lat-1) + pre_lat)*cpu_ratio);
           busy_until[current_bank] += (((dpl_lat-1) + pre_lat)*cpu_ratio == temp)?temp:(temp+1);
          }



        /*fprintf(stderr,"%10.0f %10.0f %4d %4d \n",(double)busy_until, (double)curTick, overlap, lat);debug*/
        // if((_cpu_num < num_cpus) && (_cpu_num >= 0))
                // cout <<"cpu id = " << _cpu_num << "current_bank = " << current_bank << endl;
    //	bank_access_profile[_cpu_num][current_bank]++;
        return lat;
      }

    /******************************************************************/

    else if ((mem_type == "DRDRAM") && (mem_actpolicy == "closed") && (memctrlpipe_enable == true))
      /* DRDRAM closed-page with overlap*/
     {

        if ((memctrladdr_type != "interleaved"))/*i.e. mc_type is linear*/
          {
            current_bank=physic_address/DR_BANK_SIZE;
            current_device=current_bank/(DR_NUM_BANKS/DR_NUM_DEVS);
          }
        else/*mc_type interleaved*/
          /* This memory management unit maps the addresses different
           * depending on the row-size, every row is mapped to another
           * bank. So the text segment uses half of every bank. The heap
           * the next quarter of each bank and the stack the rest.
           */
          {
            num_blocks = physic_address/DR_ROW_SIZE; /* row number */
            current_bank=(num_blocks%DR_NUM_BANKS)*2; /*every 'second' bank will be used*/
            /*banks above DR_NUM_BANKS are the uneven banks*/
            current_bank = ((current_bank <  DR_NUM_BANKS) ? current_bank:(current_bank - DR_NUM_BANKS+1));
            current_device=current_bank/(DR_NUM_BANKS/DR_NUM_DEVS);
          }


        if (mem_access_details == true)
          {
            //fprintf(mem_accessfd,"       %09u   %3d  \n",physic_address,current_bank);
          }
        else
          {
            if (mem_access_output!=NULL)
              {
                //fprintf(mem_accessfd,"\n");
              }
          }
        total_access++;

        overlap=(int)(busy_until[current_bank] - curTick);

        if (cpu_ratio < 1.0)
          {
            corrected_overlap = overlap*((int)(1/cpu_ratio)); /* floor */
          }
        else
          {
            corrected_overlap = (int) (overlap/cpu_ratio);
          }

        if (current_bank == last_bank)/*same bank*/
          {
            if ((lastCmdIsRead == cmdIsRead) && (cmdIsRead))/* RAR */
              {
                lat = DR_T_RCD + DR_T_CAC + DR_T_PACKET;
                worst_case++;
              }
            else if ((lastCmdIsRead == cmdIsRead) && (!cmdIsRead)) /* WAW */
              {
                lat = DR_T_RCD + DR_T_CWD + DR_T_PACKET;
                worst_case++;
              }
            else if ((lastCmdIsRead != cmdIsRead) && (cmdIsRead)) /* RAW */
              {
                lat = DR_T_RCD + DR_T_CAC + DR_T_PACKET;
                worst_case++;
              }
            else /* WAR */
              {
                lat = DR_T_RCD + DR_T_CWD + DR_T_PACKET;
                worst_case++;
              }
          }
        else /* other bank */
          {
            if ((lastCmdIsRead == cmdIsRead) && (cmdIsRead))/* RAR */
              {
                if (corrected_overlap > DR_T_RAS + DR_T_RP - 2 * DR_T_PACKET)
                  {
                    lat = - DR_T_RAS + DR_T_RCD + DR_T_PACKET + DR_T_CAC;
                    best_case++;
                    full_overlapping++;
                  }
                else if (corrected_overlap > 0)
                  {
                    lat = DR_T_RCD + DR_T_CAC + DR_T_PACKET - corrected_overlap;
                    in_between_case++;
                    partial_overlapping++;
                  }
                else
                  {
                    lat = DR_T_RCD + DR_T_CAC + DR_T_PACKET;
                    worst_case++;
                  }
              }
            else if ((lastCmdIsRead == cmdIsRead) && (!cmdIsRead)) /* WAW */
              {
                if (corrected_overlap > DR_T_RCD + DR_T_RTR  + DR_T_RP)
                  {
                    lat = - DR_T_CWD - 2 * DR_T_PACKET + DR_T_RTR;
                    best_case++;
                    full_overlapping++;
                  }
                else if (corrected_overlap > 0)
                  {
                    lat = DR_T_RCD + DR_T_CWD + DR_T_PACKET - corrected_overlap;
                    in_between_case++;
                    partial_overlapping++;
                  }
                else
                  {
                    lat = DR_T_RCD + DR_T_CWD + DR_T_PACKET;
                    worst_case++;
                  }
              }
            else if ((lastCmdIsRead != cmdIsRead) && (cmdIsRead)) /* RAW */
              {
                if (current_device == last_dev) /* same device */
                  {
                    if (corrected_overlap >  DR_T_RCD + DR_T_RP)
                      {
                        lat = DR_T_PACKET + DR_T_CAC - DR_T_RP;
                        best_case++;
                        partial_overlapping++;
                      }
                    else if (corrected_overlap > 0)
                      {
                        lat = DR_T_RCD + DR_T_CAC + DR_T_PACKET - corrected_overlap;
                        in_between_case++;
                        partial_overlapping++;
                      }
                    else
                      {
                        lat = DR_T_RCD + DR_T_CAC + DR_T_PACKET;
                        worst_case++;
                      }
                  }
                else /* other device */
                  {
                    if (corrected_overlap > DR_T_RCD + DR_T_RP + 2 * DR_T_PACKET)
                      {
                        lat = - DR_T_PACKET + DR_T_CAC - DR_T_RP;
                        best_case++;
                        partial_overlapping++;
                      }
                    else if (corrected_overlap > 0)
                      {
                        lat = DR_T_RCD + DR_T_CAC + DR_T_PACKET - corrected_overlap;
                        in_between_case++;
                        partial_overlapping++;
                      }
                    else
                      {
                        lat = DR_T_RCD + DR_T_CAC + DR_T_PACKET;
                        worst_case++;
                      }
                  }
              }
            else /* WAR */
              {
                if (corrected_overlap > DR_T_RAS + DR_T_RP - 2 * DR_T_PACKET - (DR_T_CAC - DR_T_CWD))
                  {
                    lat = - DR_T_RAS + DR_T_RCD + DR_T_PACKET + DR_T_CAC;
                    best_case++;
                    full_overlapping++;
                  }
                else if (corrected_overlap > 0)
                  {
                    lat = DR_T_RCD + DR_T_CWD + DR_T_PACKET - corrected_overlap;
                    in_between_case++;
                    partial_overlapping++;
                  }
                else
                  {
                    lat = DR_T_RCD + DR_T_CWD + DR_T_PACKET;
                    worst_case++;
                  }
              }
          }

        lat += chunks * DR_T_PACKET; /*every 128 bit need DR_NUM_CYCLES*/

        /*fprintf(stderr,"%4d ",lat);debugging*/

        if (cpu_ratio < 1.0)
          {
            lat = (lat+((int)(1/cpu_ratio)-1))/(int)(1/cpu_ratio);
          }
        else
          {
            temp = (int)(lat*cpu_ratio);
            lat = (lat*cpu_ratio == temp)?temp:(temp+1); /*round up*/
          }

        lastCmdIsRead=cmdIsRead;
        last_bank=current_bank;
        last_dev = current_device;

        /*fprintf(stderr,"%4d \n",lat);debugging*/

        if (overlap <= 0) /*memory interface is not busy*/
          {
            busy_until[current_bank] = curTick+lat;
          }
        else/*the memory request will be satisfied temp cycles after curTick*/
          {
            busy_until[current_bank] +=lat;
            command_overlapping++;
            lat+=overlap;
          }

        /*fprintf(stderr,"%10.0f %10.0f %4d %4d \n",(double)busy_until, (double)curTick, overlap, lat);*/

        if (cmdIsRead)
          {
            if (cpu_ratio < 1.0)
              {
                busy_until[current_bank] += abs(((DR_T_RAS - (DR_T_RCD + DR_T_PACKET + DR_T_CAC)) + 1)/((int)(1/cpu_ratio))); /* CPU clock cycles */
              }
            else
              {
                busy_until[current_bank] += (int) abs(((DR_T_RAS - (DR_T_RCD + DR_T_PACKET + DR_T_CAC)) + 1)*cpu_ratio); /* CPU clock cycles */
              }
          }
         else /* !cmdIsRead */
          {
            if (cpu_ratio < 1.0)
              {
                busy_until[current_bank] += abs((-DR_T_PACKET + DR_T_RTR + DR_T_RP - DR_T_CWD + 1)/((int)(1/cpu_ratio))); /* CPU clock cycles */
              }
            else
              {
                busy_until[current_bank] += (int) abs((-DR_T_PACKET + DR_T_RTR + DR_T_RP - DR_T_CWD + 1)*cpu_ratio); /* CPU clock cycles */
              }
          }

        // if((_cpu_num < num_cpus) && (_cpu_num >= 0))
                // cout <<"cpu id = " << _cpu_num << "current_bank = " << current_bank << endl;
    //	bank_access_profile[_cpu_num][current_bank]++;
        return lat;
      }

    /******************************************************************/

    else if ((mem_type == "SDRAM") && (mem_actpolicy == "closed") && (memctrlpipe_enable == false))
      /* SDRAM closed-page without overlap, 7/99 MG */
      {
        if (mem_access_output != NULL)
          {
            //fprintf(mem_accessfd,"\n");
          }
        assert(chunks >0);

        if (cmdIsRead)
          {
            lat = act_lat + cas_lat;
          }
        else /* !cmdIsRead */
          {
            lat = act_lat;
          }
        total_access++;
        lat += chunks;

        overlap=(int)(busy_until[current_bank] - curTick);
        lastCmdIsRead=cmdIsRead;

        if (cpu_ratio < 1.0)
          {
            lat = (lat+((int)(1/cpu_ratio)-1))/(int)(1/cpu_ratio);
          }
        else
          {
            temp = (int)(lat*cpu_ratio);
            lat = (lat*cpu_ratio == temp)?temp:(temp+1); /*round up*/
          }

        if (overlap <= 0) /*memory interface is not busy*/
          {
            busy_until[current_bank] = curTick+lat;
          }
        else/*the memory request will be satisfied temp cycles after curTick*/
          {
            busy_until[current_bank] +=lat;
            command_overlapping++;
            lat+=overlap;
          }
        if (!cmdIsRead)
          {
            temp = (int)(((dpl_lat-1) + pre_lat)*cpu_ratio);
            busy_until[current_bank] += (((dpl_lat-1) + pre_lat)*cpu_ratio == temp)?temp:(temp+1);
          }

        // if((_cpu_num < num_cpus) && (_cpu_num >= 0))
                // cout <<"cpu id = " << _cpu_num << "current_bank = " << current_bank << endl;
    //	bank_access_profile[_cpu_num][current_bank]++;
        return lat;
      }

    /******************************************************************/

    else if ((mem_type == "DRDRAM") && (mem_actpolicy == "closed") && (memctrlpipe_enable == false))
      /* DRDRAM closed-page without overlap */
      {
        if (cmdIsRead)
          {
            lat = DR_T_RCD + DR_T_CAC + DR_T_PACKET; /*  DR_T_RP + */
          }
        else /* !cmdIsRead */
          {
            lat = DR_T_RCD + DR_T_CWD + DR_T_PACKET; /* DR_T_RP + */
          }
        total_access++;
        overlap=(int)(busy_until[current_bank] - curTick);
        lat += chunks * DR_T_PACKET; /*every 128 bit need DR_NUM_CYCLES*/

        if (cpu_ratio < 1.0)
          {
            lat = (lat+((int)(1/cpu_ratio)-1))/(int)(1/cpu_ratio);
          }
        else
          {
            temp = (int)(lat*cpu_ratio);
            lat = (lat*cpu_ratio == temp)?temp:(temp+1); /*round up*/
          }

        lastCmdIsRead=cmdIsRead;

        if (overlap <= 0) /*memory interface is not busy*/
          {
            busy_until[current_bank] = curTick+lat;
          }
        else/*the memory request will be satisfied temp cycles after curTick*/
          {
            busy_until[current_bank] += lat;
            command_overlapping++;
            lat+=overlap;
          }

        if (cmdIsRead)
          {
            if (cpu_ratio < 1.0)
              {
                busy_until[current_bank] += abs(((DR_T_RAS - (DR_T_RCD + DR_T_PACKET + DR_T_CAC)) + 1)/((int)(1/cpu_ratio))); /* CPU clock cycles */
              }
            else
              {
                busy_until[current_bank] += (int) abs(((DR_T_RAS - (DR_T_RCD + DR_T_PACKET + DR_T_CAC)) + 1)*cpu_ratio); /* CPU clock cycles */
              }
          }
         else /* !cmdIsRead */
          {
            if (cpu_ratio < 1.0)
              {
                busy_until[current_bank] += abs((-DR_T_PACKET + DR_T_RTR + DR_T_RP - DR_T_CWD + 1)/((int)(1/cpu_ratio))); /* CPU clock cycles */
              }
            else
              {
                busy_until[current_bank] += (int) abs((-DR_T_PACKET + DR_T_RTR + DR_T_RP - DR_T_CWD + 1)*cpu_ratio); /* CPU clock cycles */
              }
          }
        // if((_cpu_num < num_cpus) && (_cpu_num >= 0))
                // cout <<"cpu id = " << _cpu_num << "current_bank = " << current_bank << endl;
    //	bank_access_profile[_cpu_num][current_bank]++;
        return lat;
      }

    /******************************************************************/

    else /*STD*/
      {
        if (mem_access_output != NULL)
          {
            //fprintf(mem_accessfd,"\n");
          }
        assert(chunks >0);
        // if((_cpu_num < num_cpus) && (_cpu_num >= 0))
                // cout <<"cpu id = " << _cpu_num << "current_bank = " << current_bank << endl;
    //	bank_access_profile[_cpu_num][current_bank]++;
        return(/* first chunk latency */act_lat +
               (/* remainder chunk latency */cas_lat * (chunks - 1)));
      }

}

/*end added by ar, MG*/

/*begin added by ar, MG*/

/* ================ helper functions ========================= */

/****** DRDRAM specific: shared sense amplifiers ******/
/* precharges the adjacent banks and returns the number of them (1 or 2)*/
int /*number of precharged banks*/
DRAMMemory::prechargeBanksAround(int bank)/*access to bank */
{
int temp;

temp=bank%DR_BANK_SAMP;

if (temp == 0) /*bank 0, 16,32 ....*/
  {
    if (active_row[bank+1]!=DR_NUM_ROWS)
      {
        precharge++;
        active_row[bank+1]=DR_NUM_ROWS;
      }
  }
else
  {
    if (temp==DR_BANK_SAMP-1)/*banks 15,31 ...*/
      {
        if (active_row[bank-1]!=DR_NUM_ROWS)
          {
            precharge++;
            active_row[bank-1]=DR_NUM_ROWS;
          }
      }
    else
      {
        if (active_row[bank-1]!=DR_NUM_ROWS)
          {
            precharge++;
            active_row[bank-1]=DR_NUM_ROWS;
          }
        if (active_row[bank+1]!=DR_NUM_ROWS)
          {
            precharge++;
            active_row[bank+1]=DR_NUM_ROWS;
          }
      }
  }
return precharge;
}


#ifndef DOXYGEN_SHOULD_SKIP_THIS

BEGIN_DECLARE_SIM_OBJECT_PARAMS(DRAMMemory)

    Param<std::string> file;
    Param<Range<Addr> > range;
    Param<Tick> latency;
    /* additional params for dram protocol*/
    Param<int> cpu_ratio;
    Param<std::string> mem_type;
    Param<std::string> mem_actpolicy;
    Param<std::string> memctrladdr_type;
    Param<int> bus_width;
    Param<int> act_lat;
    Param<int> cas_lat;
    Param<int> war_lat;
    Param<int> pre_lat;
    Param<int> dpl_lat;
    Param<int> trc_lat;
    Param<int> num_banks;
    Param<int> num_cpus;

END_DECLARE_SIM_OBJECT_PARAMS(DRAMMemory)

BEGIN_INIT_SIM_OBJECT_PARAMS(DRAMMemory)

    INIT_PARAM_DFLT(file, "memory mapped file", ""),
    INIT_PARAM(range, "Device Address Range"),
    INIT_PARAM(latency, "Memory access latency"),

    /* additional params for dram protocol*/
    INIT_PARAM_DFLT(cpu_ratio,"ratio between CPU speed and memory bus speed",5),
    INIT_PARAM_DFLT(mem_type,"type of DRAM","SDRAM"),
    INIT_PARAM_DFLT(mem_actpolicy,"open / closed page policy","open"),
    INIT_PARAM_DFLT(memctrladdr_type,"interleaved or direct mapping","interleaved"),
    INIT_PARAM_DFLT(bus_width,"memory access bus width",16),
    INIT_PARAM_DFLT(act_lat,"RAS to CAS delay",2),
    INIT_PARAM_DFLT(cas_lat,"CAS delay",1),
    INIT_PARAM_DFLT(war_lat,"write after read delay",2),
    INIT_PARAM_DFLT(pre_lat,"precharge delay",2),
    INIT_PARAM_DFLT(dpl_lat,"data in to precharge delay",2),
    INIT_PARAM_DFLT(trc_lat,"row cycle delay",6),
    INIT_PARAM_DFLT(num_banks,"Number of Banks",4),
    INIT_PARAM_DFLT(num_cpus,"Number of CPUs connected to DRAM",4)

END_INIT_SIM_OBJECT_PARAMS(DRAMMemory)

CREATE_SIM_OBJECT(DRAMMemory)
{
    DRAMMemory::Params *p = new DRAMMemory::Params;
    p->name = getInstanceName();
    p->addrRange = range;
    p->latency = latency;

    /* additional params for dram */
    p->cpu_ratio = cpu_ratio;
    p->bus_width = bus_width;
    p->mem_type = mem_type;
    p->mem_actpolicy = mem_actpolicy;
    p->memctrladdr_type = memctrladdr_type;
    p->act_lat = act_lat;
    p->cas_lat = cas_lat;
    p->war_lat = war_lat;
    p->pre_lat = pre_lat;
    p->dpl_lat = dpl_lat;
    p->trc_lat = trc_lat;
    p->num_banks = num_banks;
    p->num_cpus = num_cpus;

    return new DRAMMemory(p);
}

REGISTER_SIM_OBJECT("DRAMMemory", DRAMMemory)

#endif // DOXYGEN_SHOULD_SKIP_THIS