File: DLXSimulator.sml

package info (click to toggle)
mlton 20130715-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 60,900 kB
  • ctags: 69,386
  • sloc: xml: 34,418; ansic: 17,399; lisp: 2,879; makefile: 1,605; sh: 1,254; pascal: 256; python: 143; asm: 97
file content (2845 lines) | stat: -rw-r--r-- 116,500 bytes parent folder | download | duplicates (6)
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
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
(* Minor tweaks by Stephen Weeks (sweeks@sweeks.com) on 2001-07-17 to turn into a
 * benchmark.
 * Added rand function.
 *)
(*
 * Matthew Thomas Fluet
 * Harvey Mudd College
 * Claremont, CA 91711
 * e-mail: Matthew_Fluet@hmc.edu
 *
 * A DLX Simulator in Standard ML
 *
 * Description:
 * The DLX Simulator is a partial implementation of the RISC instruction
 * set described in Patterson's and Hennessy's _Computer Architecture_.
 * Currently, the DLX Simulator implements the following instructions:
 *   ADD     ADDI
 *   ADDU    ADDUI
 *   SUB     SUBI
 *   SUBU    SUBUI
 *   AND     ANDI
 *   OR      ORI
 *   XOR     XORI
 *
 *   LHI
 *
 *   SLL     SLLI
 *   SRL     SRLI
 *   SRA     SRAI
 *
 *   SEQ     SEQI
 *   SNE     SNEI
 *   SLT     SLTI
 *   SGT     SGTI
 *   SLE     SLEI
 *   SGE     SGEI
 *
 *   LB      LBU     SB
 *   LH      LHU     SH
 *   LW      SW
 *
 *   BEQZ    BNEZ
 *   J       JR
 *   JAL     JALR
 *
 *   TRAP
 *
 *   NOP
 *
 * Currently, the DLX Simulator uses 32 bit words for addressing and
 * the register file and a 65535 word memory.  To augment the memory
 * a cache can be installed in the simulator, with a number of different
 * caching options that can be made.  Caches can also cache other caches,
 * so realistic dual level caches can be simulated.  Input and output
 * is limited to requesting and outputing signed integers.
 *
 * Usage:
 * DLXSimulatorCX.run_file : string -> unit
 * DLXSimulatorCX.run_prog : string list -> unit;
 * The DLXSimualatorCX family of structures represent different caches
 * used on the simulator.  The following table describes the different
 * caches used:
 * C1: a small level 1 cache
 * DLXSimulatorCX.run_file attempts to open and execute the instructions
 * in a file.  DLXSimulatorCX.run_prog runs a set of instructions as
 * a list of strings.  Four programs are included here.
 * Simple : simply outputs the number 42.
 * Twos: performs the twos complement on an inputed number.
 * Abs: performs the absolute value on an imputed number.
 * Fact: performs the factorial on an inputed number.
 * GCD: performs the greatest common divisor on two imputed numbers.
 * After running, the DLX Simulator outputs a set of statistics
 * concerning memory reads and writes, and cache hits and misses.
 *
 * Future Work: 
 * With the implementation of the PACK_REAL structures
 * as documented in the SML'97 Basis Library, the remainder
 * of the DLX instruction set should be implemented.
 * Currently, without an efficient and correct means of 
 * converting a 32 bit word into a 32 bit float, it is
 * difficult to incorporate these instructions.
 * In order to finish following the current development
 * model, a FPALU structure should be implemented as the 
 * floating point arithmetic-logic unit.
 * Another possibility for future work would be to 
 * model a pipelined processor.  Currently, the DLX Simulator
 * uses a simple one cycle per instruction model.
 * It should be possible to break this model and implement
 * a pipeline, but it would mean a major reworking of the
 * DLXSimulatorFun functor.
 *
 * References:
 * Patterson, David A. and John L. Hennessy.  _Computer Architecture: A
 *   Quantitative Approach: Second Edition_.  San Francisco: Morgan 
 *   Kaufmann Publishers, Inc., 1996.
 *
 *)

(* ************************************************************************* *)

(* sweeks added rand *)
local
   open Word
   val seed: word ref = ref 0w13
in
   (* From page 284 of Numerical Recipes in C. *)
   fun rand (): word =
      let
         val res = 0w1664525 * !seed + 0w1013904223
         val _ = seed := res
      in
         res
      end
end

(* 
 * ImmArray.sml
 *
 * The ImmArray structure defines an immutable array implementation.
 * An immarray is stored internally as a list.
 * This results in O(n) sub and update functions, as opposed
 * to O(1) sub and update functions found in Array.  However,
 * immutable arrays are truly immutable, and can be integrated
 * with a functionally programming style easier than mutable
 * arrays.
 *
 * The ImmArray structure mimics the Array structure as much as possible.
 * The most obvious deviation is that unit return types in Array are replaced
 * by 'a immarray return types in ImmArray.  Unlike an 'a array, an 'a immarray
 * is an equality type if and only if 'a is an equality type.  Further immarray
 * equality is structural, rather than the "creation" equality used by Array.
 * Additionally, no vector type is supported, and consequently no copyVec
 * function is supported.  Finally, the functions mapi and map provide
 * similar functionality as modifyi and modify, but relax the constraint that
 * the argument function need be of type 'a -> 'a.
 *
 * Future Work : There are random-access list implementations
 *               that support O(log n) sub and update functions,
 *               which may provide a faster implementation, although
 *               possibly at the expense of space and the ease of 
 *               implementing app, foldl, foldr, modify, and map functions.
 *)

signature IMMARRAY
  = sig
      type 'a immarray;
        
      val maxLen : int;
      val immarray : (int * 'a) -> 'a immarray;
      val fromList : 'a list -> 'a immarray;
      val toList : 'a immarray -> 'a list;
  
      val tabulate : int * (int -> 'a) -> 'a immarray;
      val length : 'a immarray -> int;
        
      val sub : 'a immarray * int -> 'a;
      val update : 'a immarray * int * 'a -> 'a immarray;
      val extract : 'a immarray * int * int option -> 'a immarray;

      val copy : {src : 'a immarray, si : int, len : int option, 
                  dst : 'a immarray, di : int} -> 'a immarray;

      val appi : (int * 'a -> unit) -> ('a immarray * int * int option) 
                 -> unit;
      val app : ('a -> unit) -> 'a immarray -> unit;
      val foldli : ((int * 'a * 'b) -> 'b) -> 'b 
                   -> ('a immarray * int * int option) -> 'b;
      val foldri : ((int * 'a * 'b) -> 'b) -> 'b 
                   -> ('a immarray * int * int option) -> 'b;
      val foldl : (('a * 'b) -> 'b) -> 'b -> 'a immarray -> 'b;
      val foldr : (('a * 'b) -> 'b) -> 'b -> 'a immarray -> 'b;
      val mapi : ((int * 'a) -> 'b) -> ('a immarray * int * int option)
                 ->  'b immarray;
      val map : ('a -> 'b) -> 'a immarray -> 'b immarray;
      val modifyi : ((int * 'a) -> 'a) -> ('a immarray * int * int option) 
                    -> 'a immarray;
      val modify : ('a -> 'a) -> 'a immarray -> 'a immarray;
    end;
    
    
structure ImmArray : IMMARRAY
  = struct
  
      (* datatype 'a immarray
       * An immarray is stored internally as a list.
       * The use of a constructor prevents list functions from
       * treating immarray type as a list.
       *)
      datatype 'a immarray = IA of 'a list;
        
      (* val maxLen : int
       * The maximum length of immarrays supported.
       * Technically, under this implementation, the maximum length
       * of immarrays is the same as the maximum length of a list,
       * but for convience and compatibility, use the Array structure's
       * maximum length.
       *)
      val maxLen = Array.maxLen;

      (* val tabulate : int * (int -> 'a) -> 'a immarray
       * val immarray : int * 'a -> 'a immarray
       * val fromList : 'a list -> 'a immarray
       * val toList : 'a immarray -> 'a list
       * val length : 'a immarray -> int
       * These functions perform basic immarray functions.
       * The tabulate, immarray, and fromList functions create an immarray.
       * The toList function converts an immarray to a list.
       * The length function returns the length of an immarray.
       *)
      fun tabulate (n, initfn) = IA (List.tabulate (n, initfn));
      fun immarray (n, init) = tabulate (n, fn _ => init);
      fun fromList l = IA l;
      fun toList (IA ia) = ia;
      fun length (IA ia) = List.length ia;
        
      (* val sub : 'a immarray * int -> 'a
       * val update : 'a immarray * int * 'a -> 'a immarray
       * These functions sub and update an immarray by index.
       *)
      fun sub (IA ia, i) = List.nth (ia, i);
      fun update (IA ia, i, x) = IA ((List.take (ia, i)) @ 
                                     (x::(List.drop (ia, i + 1))));
        
      (* val extract : 'a immarray * int * int option -> 'a immarray
       * This function extracts an immarray slice from an immarray from
       * one index either through the rest of the immarray (NONE)
       * or for n elements (SOME n), as described in the 
       * Standard ML Basis Library.
       *)
      fun extract (IA ia, i, NONE) = IA (List.drop (ia, i))
        | extract (IA ia, i, SOME n) = IA (List.take (List.drop (ia, i), n));
        
      (* val copy : {src : 'a immarray, si : int, len : int option, 
                     dst : 'a immarray, di : int} -> 'a immarray
       * This function copies an immarray slice from src into dst starting
       * at the di element.
       *)
      fun copy {src, si, len, dst=IA ia, di} 
        = let
            val IA sia = extract (src, si, len);
            val pre = List.take (ia, di);
            val post = case len 
                         of NONE => List.drop (ia, di+(List.length sia))
                          | SOME n => List.drop (ia, di+n);
          in 
            IA (pre @ sia @ post)
          end;
                                           
      (* val appi : ('a * int -> unit) -> ('a immarray * int * int option) 
       *            -> unit
       * val app : ('a -> unit) -> 'a immarray -> unit
       * These functions apply a function to every element
       * of an immarray.  The appi function also provides the
       * index of the element as an argument to the applied function
       * and uses an immarray slice argument.
       *)
      local
        fun appi_aux f i [] = ()
          | appi_aux f i (h::t) = (f(i,h); appi_aux f (i + 1) t);
      in
        fun appi f (IA ia, i, len) = let 
                                       val IA sia = extract (IA ia, i, len);
                                     in 
                                       appi_aux f i sia
                                     end;
      end;
      fun app f immarr = appi (f o #2) (immarr, 0, NONE);

      (* val foldli : (int * 'a * 'b -> 'b) -> 'b 
       *              -> ('a immarray * int * int option) -> 'b;
       * val foldri : (int * 'a * 'b -> 'b) -> 'b
       *              -> ('a immarray * int * int option) -> 'b;
       * val foldl : ('a * 'b -> 'b) -> 'b -> 'a immarray -> 'b
       * val foldr : ('a * 'b -> 'b) -> 'b -> 'a immarray -> 'b
       * These functions fold a function over every element
       * of an immarray.  The foldri and foldli functions also provide
       * the index of the element as an argument to the folded function
       * and uses an immarray slice argument.
       *)
      local 
        fun foldli_aux f b i [] = b
          | foldli_aux f b i (h::t) = foldli_aux f (f(i,h,b)) (i+1) t;
        fun foldri_aux f b i [] = b
          | foldri_aux f b i (h::t) = f(i,h,foldri_aux f b (i+1) t);
      in
        fun foldli f b (IA ia, i, len) 
          = let 
              val IA ia2 = extract (IA ia, i, len);
            in 
              foldli_aux f b i ia2
            end;
        fun foldri f b (IA ia, i, len) 
          = let
              val IA ia2 = extract (IA ia, i, len);
            in 
              foldri_aux f b i ia2
            end;
      end;
      fun foldl f b (IA ia) = foldli (fn (_,i,x) => f(i,x)) b (IA ia, 0, NONE);
      fun foldr f b (IA ia) = foldri (fn (_,i,x) => f(i,x)) b (IA ia, 0, NONE);

      (* val mapi : ('a * int -> 'b) -> 'a immarray -> 'b immarray
       * val map : ('a -> 'b) -> 'a immarray -> 'b immarray
       * These functions map a function over every element 
       * of an immarray.  The mapi function also provides the
       * index of the element as an argument to the mapped function
       * and uses an immarray slice argument.  Although there are
       * similarities between mapi and modifyi, note that when mapi is 
       * used with an immarray slice, the resulting immarray is the
       * same size as the slice.  This is necessary to preserve the
       * type of the resulting immarray.  Thus, mapi with the identity
       * function reduces to the extract function. 
       *)
      local
        fun mapi_aux f i [] = []
          | mapi_aux f i (h::t) = (f (i,h))::(mapi_aux f (i + 1) t);
      in
        fun mapi f (IA ia, i, len) = let 
                                       val IA ia2 = extract (IA ia, i, len);
                                     in 
                                       IA (mapi_aux f i ia2)
                                     end;
      end;
      fun map f (IA ia)= mapi (f o #2) (IA ia, 0, NONE);

      (* val modifyi : (int * 'a -> 'a) -> ('a immarray * int * int option) 
       *               -> 'a immarray
       * val modify : ('a -> 'a) -> 'a immarray -> 'a immarray
       * These functions apply a function to every element of an immarray
       * in left to right order and returns a new immarray where corresponding
       * elements are replaced by their modified values.  The modifyi 
       * function also provides the index of the element as an argument 
       * to the mapped function and uses an immarray slice argument.
       *)
      local
        fun modifyi_aux f i [] = []
          | modifyi_aux f i (h::t) = (f (i,h))::(modifyi_aux f (i + 1) t);
      in
        fun modifyi f (IA ia, i, len)
          = let
              val pre = List.take (ia, i);
              val IA ia2 = extract (IA ia, i, len);
              val post = case len 
                           of NONE => []
                            | SOME n => List.drop (ia, i+n);
            in
              IA (pre @ (modifyi_aux f i ia2) @ post)
            end;
      end;
      fun modify f (IA ia) = modifyi (f o #2) (IA ia, 0, NONE);

    end;

(* ************************************************************************* *)

(*
 * ImmArray2.sml
 * 
 * The ImmArray2 structure defines a two dimensional immutable array 
 * implementation.  An immarray2 is stored internally as an immutable 
 * array of immutable arrays.  As such, the ImmArray2 makes heavy use 
 * of the ImmArray structure.
 *
 * The ImmArray2 structure mimics the Array2 structure as much as possible.
 * The most obvious deviation is that unit return types in Array2 are replaced
 * by 'a immarray2 return types in ImmArray2.  Unlike an 'a array, 
 * an 'a immarray2 is an equality type if and only if 'a is an equality type.
 * Further immarray2 equality is structural, rather than the "creation" 
 * equality used by Array2.  Also, the 'a region type is not included in
 * ImmArray2, but all functions in Array2 that require 'a regions are present
 * with arguments taken in the natural order.  Finally, the functions mapi 
 * and map provide similar functionality as modifyi and modify, but relax 
 * the constraint that the argument function need be of type 'a -> 'a.
 *)

signature IMMARRAY2
  = sig

      type 'a immarray2;

      datatype traversal = RowMajor | ColMajor

      val immarray2 : int * int * 'a -> 'a immarray2;
      val tabulate : traversal -> int * int * ((int * int) -> 'a) 
                     -> 'a immarray2;
      val fromList : 'a list list -> 'a immarray2;
      val dimensions : 'a immarray2 -> int * int;

      val sub : 'a immarray2 * int * int -> 'a;
      val update : 'a immarray2 * int * int * 'a -> 'a immarray2;
      val extract : 'a immarray2 * int * int * int option * int option 
                    -> 'a immarray2;
                    
      val copy : {src : 'a immarray2, si : int, sj : int,
                  ilen : int option, jlen : int option,
                  dst : 'a immarray2, di : int, dj : int} -> 'a immarray2;

      val nRows : 'a immarray2 -> int;
      val nCols : 'a immarray2 -> int;
      val row : 'a immarray2 * int -> 'a ImmArray.immarray;
      val column : 'a immarray2 * int -> 'a ImmArray.immarray;

      val appi : traversal -> (int * int * 'a -> unit) 
                 -> ('a immarray2 * int * int * int option * int option) 
                 -> unit;
      val app : traversal -> ('a -> unit) -> 'a immarray2 -> unit;
      val foldli : traversal -> ((int * int * 'a * 'b) -> 'b) -> 'b 
                   -> ('a immarray2 * int * int * int option * int option) 
                   -> 'b
      val foldri : traversal -> ((int * int * 'a * 'b) -> 'b) -> 'b 
                   -> ('a immarray2 * int * int * int option * int option) 
                   -> 'b
      val foldl : traversal -> (('a * 'b) -> 'b) -> 'b -> 'a immarray2 -> 'b
      val foldr : traversal -> (('a * 'b) -> 'b) -> 'b -> 'a immarray2 -> 'b
      val mapi : traversal -> (int * int * 'a -> 'b) 
                 -> ('a immarray2 * int * int * int option * int option) 
                 -> 'b immarray2;
      val map : traversal -> ('a -> 'b) -> 'a immarray2 -> 'b immarray2;
      val modifyi : traversal -> ((int * int * 'a) -> 'a) 
                    -> ('a immarray2 * int * int * int option * int option) 
                    -> 'a immarray2;
      val modify : traversal -> ('a -> 'a) -> 'a immarray2 -> 'a immarray2;
    end;

structure ImmArray2 : IMMARRAY2
  = struct
  
      (* datatype 'a immarray2
       * An immarray2 is stored internally as an immutable array 
       * of immutable arrays.  The use of a contructor prevents ImmArray
       * functions from treating the immarray2 type as an immarray.
      *)
      datatype 'a immarray2 = IA2 of 'a ImmArray.immarray ImmArray.immarray;
      datatype traversal = RowMajor | ColMajor

      (* val tabulate : traversal -> int * int * (int * int -> 'a) 
       *                -> 'a immarray2
       * val immarray2 : int * int * 'a -> 'a immarray2
       * val fromList : 'a list list -> 'a immarray2
       * val dmensions : 'a immarray2 -> int * int
       * These functions perform basic immarray2 functions.
       * The tabulate and immarray2 functions create an immarray2.
       * The fromList function converts a list of lists into an immarray2.
       * Unlike Array2.fromList, fromList will accept lists of different
       * lengths, allowing one to create an immarray2 in which the
       * rows have different numbers of columns, although it is likely that
       * exceptions will be raised when other ImmArray2 functions are applied
       * to such an immarray2.  Note that dimensions will return the 
       * number of columns in row 0.
       * The dimensions function returns the dimensions of an immarray2.
       *)
      fun tabulate RowMajor (r, c, initfn)
        = let 
            fun initrow r = ImmArray.tabulate (c, fn ic => initfn (r,ic));  
          in
            IA2 (ImmArray.tabulate (r, fn ir => initrow ir))
          end
        | tabulate ColMajor (r, c, initfn)
          = turn (tabulate RowMajor (c,r, fn (c,r) => initfn(r,c)))
      and immarray2 (r, c, init) = tabulate RowMajor (r, c, fn (_, _) => init)
      and fromList l 
        = IA2 (ImmArray.tabulate (length l, 
                                  fn ir => ImmArray.fromList (List.nth(l,ir))))
      and dimensions (IA2 ia2) = (ImmArray.length ia2, 
                                  ImmArray.length (ImmArray.sub (ia2, 0)))

      (* turn : 'a immarray2 -> 'a immarray2
       * This function reverses the rows and columns of an immarray2
       * to allow handling of ColMajor traversals.
       *)
      and turn ia2 = let
                       val (r,c) = dimensions ia2;
                     in 
                       tabulate RowMajor (c,r,fn (cc,rr) => sub (ia2,rr,cc))
                     end

      (* val sub : 'a immarray2 * int * int -> 'a
       * val update : 'a immarray2 * int * int * 'a -> 'a immarray2
       * These functions sub and update an immarray2 by indices.
       *)
      and sub (IA2 ia2, r, c) = ImmArray.sub(ImmArray.sub (ia2, r), c);
      fun update (IA2 ia2, r, c, x)
          = IA2 (ImmArray.update (ia2, r, 
                                  ImmArray.update (ImmArray.sub (ia2, r), 
                                                   c, x)));       

      (* val extract : 'a immarray2 * int * int * 
       *               int option * int option -> 'a immarray2
       * This function extracts a subarray from an immarray2 from
       * one pair of indices either through the rest of the
       * immarray2 (NONE, NONE) or for the specfied number of elements.
       *)
      fun extract (IA2 ia2, i, j, rlen, clen)
          = IA2 (ImmArray.map (fn ia => ImmArray.extract (ia, j, clen))
                              (ImmArray.extract (ia2, i, rlen)));
        
      (* val nRows : 'a immarray2 -> int
       * val nCols : 'a immarray2 -> int
       * These functions return specific dimensions of an immarray2.
       *)
      fun nRows (IA2 ia2) = (#1 o dimensions) (IA2 ia2);
      fun nCols (IA2 ia2) = (#2 o dimensions) (IA2 ia2);
      (* val row : immarray2 * int -> ImmArray.immarray
       * val column : immarray2 * int -> ImmArray.immarray
       * These functions extract an entire row or column from
       * an immarray2 by index, returning the row or column as
       * an ImmArray.immarray.
       *)
      fun row (ia2, r) = let
                           val (c, _) = dimensions ia2;
                         in
                           ImmArray.tabulate (c, fn i => sub (ia2, r, i))
                         end;
      fun column (ia2, c) = let
                              val (_, r) = dimensions ia2;
                            in
                              ImmArray.tabulate (r, fn i => sub (ia2, i, c))
                            end;

      (* val copy : {src : 'a immarray2, si : int, sj : int,
       *             ilen : int option, jlen : int option,
       *             dst : 'a immarray2, di : int, dj : int};
       * This function copies an immarray2 slice from src int dst starting
       * at the di,dj element.
       *)
      fun copy {src, si, sj, ilen, jlen, dst=IA2 ia2, di, dj}
        = let
            val nilen = case ilen 
                          of NONE => SOME ((nRows src) - si)
                           | SOME n => SOME n;
          in 
            IA2 (ImmArray.modifyi (fn (r, ia) 
                                   => ImmArray.copy {src=row (src, si+r-di),
                                                     si=sj, len=jlen, 
                                                     dst=ia, di=dj}) 
                                  (ia2, di, nilen))
          end;

      (* val appi : traversal -> ('a * int * int -> unit) -> 'a immarray2 
       *            -> unit
       * val app : traversal -> ('a -> unit) -> 'a immarray2 -> unit
       * These functions apply a function to every element
       * of an immarray2.  The appi function also provides the
       * indices of the element as an argument to the applied function
       * and uses an immarray2 slice argument.
       *)
      fun appi RowMajor f (IA2 ia2, i, j, rlen, clen)
        = ImmArray.appi (fn (r,ia) => ImmArray.appi (fn (c,x) => f(r,c,x))
                                                    (ia, j, clen))
                        (ia2, i, rlen)
        | appi ColMajor f (ia2, i, j, rlen, clen)
        = appi RowMajor (fn (c,r,x) => f(r,c,x)) (turn ia2, j, i, clen, rlen); 
      fun app tr f (IA2 ia2) = appi tr (f o #3) (IA2 ia2, 0, 0, NONE, NONE);

      (* val foldli : traversal -> ((int * int * 'a * 'b) -> 'b) -> 'b 
       *              -> ('a immarray2 * int * int * int option * int option) 
       *              -> 'b
       * val foldri : traversal -> ((int * int * 'a * 'b) -> 'b) -> 'b 
       *              -> ('a immarray2 * int * int * int option * int option) 
       *              -> 'b
       * val foldl : traversal -> ('a * 'b -> 'b) -> 'b -> 'a immarray2 -> 'b
       * val foldr : traversal -> ('a * 'b -> 'b) -> 'b -> 'a immarray2 -> 'b
       * These functions fold a function over every element
       * of an immarray2.  The foldri and foldli functions also provide
       * the index of the element as an argument to the folded function
       * and uses an immarray2 slice argument.
       *)
      fun foldli RowMajor f b (IA2 ia2, i, j, rlen, clen)
        = ImmArray.foldli (fn (r,ia,b) 
                           => ImmArray.foldli (fn (c,x,b) => f(r,c,x,b))
                                              b
                                              (ia, j, clen))
                          b
                          (ia2, i, rlen)
        | foldli ColMajor f b (ia2, i, j, rlen, clen)
        = foldli RowMajor (fn (c,r,x,b) => f(r,c,x,b)) b 
                 (turn ia2, j, i, clen, rlen);
      fun foldri RowMajor f b (IA2 ia2, i, j, rlen, clen)
        = ImmArray.foldri (fn (r,ia,b) 
                           => ImmArray.foldri (fn (c,x,b) => f(r,c,x,b))
                                              b
                                              (ia, j, clen))
                          b
                          (ia2, i, rlen)
        | foldri ColMajor f b (ia2, i, j, rlen, clen)
        = foldri RowMajor (fn (c,r,x,b) => f(r,c,x,b)) b 
                          (turn ia2, j, i, clen, rlen);
      fun foldl tr f b (IA2 ia2)
        = foldli tr (fn (_,_,x,b) => f(x,b)) b (IA2 ia2, 0, 0, NONE, NONE);
      fun foldr tr f b (IA2 ia2)
        = foldri tr (fn (_,_,x,b) => f(x,b)) b (IA2 ia2, 0, 0, NONE, NONE);

      (* val mapi : traversal -> ('a * int * int -> 'b) -> 'a immarray2 
       *            -> 'b immarray2
       * val map : traversal -> ('a -> 'b) -> 'a immarray2 -> 'b immarray2
       * These functions map a function over every element
       * of an immarray2.  The mapi function also provides the
       * indices of the element as an argument to the mapped function
       * and uses an immarray2 slice argument.  Although there are
       * similarities between mapi and modifyi, note that when mapi is 
       * used with an immarray2 slice, the resulting immarray2 is the
       * same size as the slice.  This is necessary to preserve the
       * type of the resulting immarray2.  Thus, mapi with the identity
       * function reduces to the extract function.
       *)
      fun mapi RowMajor f (IA2 ia2, i, j, rlen, clen)
        = IA2 (ImmArray.mapi (fn (r,ia) => ImmArray.mapi (fn (c,x) => f(r,c,x))
                                                         (ia, j, clen))
                             (ia2, i, rlen))
        | mapi ColMajor f (ia2, i, j, rlen, clen)
        = turn (mapi RowMajor (fn (c,r,x) => f(r,c,x)) 
                     (turn ia2, j, i, clen, rlen))
      fun map tr f (IA2 ia2)
        = mapi tr (f o #3) (IA2 ia2, 0, 0, NONE, NONE);

      (* val modifyi : traversal -> (int * int* 'a -> 'a) 
                       -> ('a immarray2 * int * int * int option * int option) 
       *               -> 'a immarray2
       * val modify : traversal -> ('a -> 'a) -> 'a immarray2 -> 'a immarray2
       * These functions apply a function to every element of an immarray2
       * in row by column order and returns a new immarray2 where corresponding
       * elements are replaced by their modified values.  The modifyi 
       * function also provides the index of the element as an argument 
       * to the mapped function and uses an immarray2 slice argument.
       *)
      fun modifyi RowMajor f (IA2 ia2, i, j, rlen, clen)
        = IA2 (ImmArray.modifyi (fn (r,ia) => ImmArray.modifyi (fn (c,x) 
                                                                => f(r,c,x)) 
                                                               (ia, j, clen)) 
              (ia2, i, rlen))
        | modifyi ColMajor f (ia2, i, j, rlen, clen)
        = turn (modifyi RowMajor (fn (c,r,x) => f(r,c,x)) 
               (turn ia2, j, i, clen, rlen));
      fun modify tr f (IA2 ia2) 
        = modifyi tr (f o #3) (IA2 ia2, 0, 0, NONE, NONE);

    end;

(* ************************************************************************* *)

(*
 * RegisterFile.sig
 *
 * This defines the exported datatype and functions provided by the
 * register file.  The datatype registerfile provides the encapsulation
 * of the register file, InitRegisterFile initializes the registerfile,
 * setting all registers to zero and setting r0, gp, sp, and fp to
 * their appropriate values, LoadRegister takes a registerfile and
 * an integer corresponding to the register, and returns the
 * Word32.word value at that register, and StoreRegister takes a
 * registerfile, an integer corresponding to the register, and a
 * Word32.word and returns the registerfile updated with the word
 * stored in the appropriate register.
 *)

signature REGISTERFILE
  = sig
  
      type registerfile;
        
      val InitRegisterFile : unit  -> registerfile; 

      val LoadRegister : registerfile * int -> Word32.word;

      val StoreRegister : registerfile * int * Word32.word -> registerfile;

    end;

(*****************************************************************************)

(*
 * RegisterFile.sml
 *
 * This defines the RegisterFile structure, which provides the
 * functionality of the register file.  The datatype registerfile
 * provides the encapsulation of the register file, InitRegisterFile
 * initializes the registerfile, setting all registers to zero and
 * setting r0, gp, sp, and fp to their appropriate values,
 * LoadRegister takes a registerfile and an integer corresponding to
 * the register, and returns the Word32.word value at that register, 
 * and StoreRegister takes a registerfile, an integer corresponding to
 * the register, and a Word32.word and returns the registerfile
 * updated with the word stored in the appropriate register.
 *
 * The underlying structure of registerfile is an immutable array of
 * Word32.word.
 *)

structure RegisterFile : REGISTERFILE
  = struct

      type registerfile = Word32.word ImmArray.immarray;
          
      fun InitRegisterFile () 
          = ImmArray.update
            (ImmArray.update
             (ImmArray.update
              (ImmArray.update
               (ImmArray.immarray(32, 0wx00000000 : Word32.word),
                00, 0wx00000000 : Word32.word),
               28, 0wx00000000 : Word32.word),
              29, 0wx00040000 : Word32.word),
             30, 0wx00040000 : Word32.word) : registerfile;
            
      fun LoadRegister (rf, reg) = ImmArray.sub(rf, reg);

      fun StoreRegister (rf, reg, data) = ImmArray.update(rf, reg, data);
        
    end;
    

(*****************************************************************************)

(*
 * ALU.sig
 * 
 * This defines the exported datatype and function provided by the
 * ALU.  The datatype ALUOp provides a means to specify which
 * operation is to be performed by the ALU, and PerformAL performs
 * one of the operations on two thirty-two bit words, returning the
 * result as a thirty-two bit word.
 *)
 
signature ALU
  = sig

      datatype ALUOp = SLL | SRL | SRA |
                       ADD | ADDU | 
                       SUB | SUBU | 
                       AND | OR | XOR | 
                       SEQ | SNE | 
                       SLT | SGT | 
                       SLE | SGE;
  
      val PerformAL : (ALUOp * Word32.word * Word32.word) -> Word32.word;
        
    end;

(*****************************************************************************)

(*
 * ALU.sml
 *
 * This defines the ALU structure, which provides the functionality of
 * an Arithmetic/Logic Unit.  The datatype ALUOp provides a means to 
 * specify which operation is to be performed by the ALU, and
 * PerformAL performs one of the operations on two thirty-two bit
 * words, returning the result as a thirty-two bit word.
 *
 * A note about SML'97 Basis Library implementation of thirty-two bit 
 * numbers: the Word32.word is an unsigned thirty-two bit integer,
 * while Int.int (equivalent to Int.int) is a signed thirty-two
 * bit integer.  In order to perform the signed operations, it is
 * necessary to convert the words to signed form, using the
 * Word32.toIntX function, which performs sign extension,
 * and to convert the result back into unsigned form using the
 * Word32.fromInt function.  In addition, to perform a shift,
 * the second Word32.word needs to be "downsized" to a normal
 * Word.word using the Word.fromWord function.
 *)

structure ALU : ALU 
  = struct

      datatype ALUOp = SLL | SRL | SRA |
                       ADD | ADDU | 
                       SUB | SUBU | 
                       AND | OR | XOR | 
                       SEQ | SNE | 
                       SLT | SGT | 
                       SLE | SGE;

      fun PerformAL (opcode, s1, s2) =
        (case opcode
           of SLL => 
                Word32.<< (s1, Word.fromLarge (Word32.toLarge s2))
            | SRL => 
                Word32.>> (s1, Word.fromLarge (Word32.toLarge s2))
            | SRA => 
                Word32.~>> (s1, Word.fromLarge (Word32.toLarge s2))
            | ADD => 
                Word32.fromInt (Int.+ (Word32.toIntX s1,
                                                 Word32.toIntX s2))
            | ADDU => 
                Word32.+ (s1, s2)
            | SUB =>
                Word32.fromInt (Int.- (Word32.toIntX s1,
                                                 Word32.toIntX s2))
            | SUBU => 
                Word32.- (s1, s2)
            | AND => 
                Word32.andb (s1, s2)
            | OR => 
                Word32.orb (s1, s2)
            | XOR => 
                Word32.xorb (s1, s2)
            | SEQ => 
                if (s1 = s2)
                  then 0wx00000001 : Word32.word
                  else 0wx00000000 : Word32.word
            | SNE => 
                if not (s1 = s2)
                  then 0wx00000001 : Word32.word
                  else 0wx00000000 : Word32.word
            | SLT => 
                if Int.< (Word32.toIntX s1, Word32.toIntX s2)
                  then 0wx00000001 : Word32.word
                  else 0wx00000000 : Word32.word
            | SGT => 
                if Int.> (Word32.toIntX s1, Word32.toIntX s2)
                  then 0wx00000001 : Word32.word
                  else 0wx00000000 : Word32.word
            | SLE => 
                if Int.<= (Word32.toIntX s1, Word32.toIntX s2)
                  then 0wx00000001 : Word32.word
                  else 0wx00000000 : Word32.word
            | SGE => 
                if Int.>= (Word32.toIntX s1, Word32.toIntX s2)
                  then 0wx00000001 : Word32.word
                  else 0wx00000000 : Word32.word)
           (* 
            * This handle will handle all ALU errors, most
            * notably overflow and division by zero, and will
            * print an error message and return 0.                
            *)
           handle _ => 
             (print "Error : ALU returning 0\n";
              0wx00000000 : Word32.word);
             
    end;

(*****************************************************************************)

(*
 * Memory.sig
 *
 * This defines the exported datatype and functions provided by 
 * memory.  The datatype memory provides the encapsulation
 * of memory, InitMemory initializes memory, setting all 
 * addresses to zero, LoadWord takes memory and
 * a Word32.word corresponding to the address, and returns the
 * Word32.word value at that address, StoreWord takes memory, 
 * a Word32.word corresponding to the address, and a
 * Word32.word and returns memory updated with the word
 * stored at the appropriate address.  LoadHWord, LoadHWordU, 
 * LoadByte, and LoadByteU load halfwords, unsigned halfwords,
 * bytes, and unsigned bytes respectively from memory into the
 * lower portion of the returned Word32.word.  StoreHWord and 
 * StoreByte store halfwords and bytes taken from the lower portion
 * of the Word32.word into memory.
 * GetStatistics takes memory and returns the read and write 
 * statistics as a string.
 *)

signature MEMORY
  = sig

      type memory;
        
      val InitMemory : unit -> memory; 

      val LoadWord : memory * Word32.word -> memory * Word32.word;
      val StoreWord : memory * Word32.word * Word32.word -> memory;

      val LoadHWord : memory * Word32.word -> memory * Word32.word;
      val LoadHWordU : memory * Word32.word -> memory * Word32.word;
      val StoreHWord : memory * Word32.word * Word32.word -> memory;

      val LoadByte : memory * Word32.word -> memory * Word32.word;
      val LoadByteU : memory * Word32.word -> memory * Word32.word;
      val StoreByte : memory * Word32.word * Word32.word -> memory;

      val GetStatistics : memory -> string;

    end;





(*****************************************************************************)

(*
 * Memory.sml
 *
 * This defines the Memory structure, which provides the functionality
 * of memory.  The datatype memory provides the encapsulation of
 * memory, InitMemory initializes memory, setting all 
 * addresses to zero, LoadWord takes memory and
 * a Word32.word corresponding to the address, and returns the
 * Word32.word value at that address and the updated memory, 
 * StoreWord takes memory, a Word32.word corresponding to the 
 * address, and a Word32.word and returns memory updated with the word
 * stored at the appropriate address.  LoadHWord, LoadHWordU, 
 * LoadByte, and LoadByteU load halfwords, unsigned halfwords,
 * bytes, and unsigned bytes respectively from memory into the
 * lower portion of the returned Word32.word.  StoreHWord and 
 * StoreByte store halfwords and bytes taken from the lower portion
 * of the Word32.word into memory.
 * GetStatistics takes memory and returns the read and write 
 * statistics as a string.
 *
 * The underlying structure of memory is an immutable array of Word32.word.
 * The array has a length of 0x10000, since every element of the array
 * corresponds to a thirty-two bit integer.
 *
 * Also, the functions AlignWAddress and AlignHWAddress aligns a memory 
 * address to a word and halfword address, respectively.  If LoadWord,
 * StoreWord, LoadHWord, LoadHWordU, or StoreHWord is asked to access an 
 * unaligned address, it writes an error message, and uses the address 
 * rounded down to the aligned address.
 *)

structure Memory : MEMORY
  = struct
        
      type memory = Word32.word ImmArray.immarray * (int * int);
        
      fun InitMemory () =
        (ImmArray.immarray(Word32.toInt(0wx10000 : Word32.word),
                           0wx00000000 : Word32.word), 
         (0, 0)) : memory;

      fun AlignWAddress address 
          = Word32.<< (Word32.>> (address, 0wx0002), 0wx0002);
            
      fun AlignHWAddress address
          = Word32.<< (Word32.>> (address, 0wx0001), 0wx0001);
            
      (* Load and Store provide errorless access to memory.
       * They provide a common interface to memory, while
       * the LoadX and StoreX specifically access words,
       * halfwords and bytes, requiring address to be aligned.
       * In Load and Store, two intermediate values are
       * generated.  The value aligned_address is the aligned
       * version of the given address, and is used to compare with
       * the original address to determine if it was aligned.  The 
       * value use_address is equivalent to aligned_address divided 
       * by four, and it corresponds to the index of the memory 
       * array where the corresponding aligned address can be found.
       *)
          
      fun Load ((mem, (reads, writes)), address)
          = let
              val aligned_address = AlignWAddress address;
              val use_address = Word32.>> (aligned_address, 0wx0002);
            in
              ((mem, (reads + 1, writes)), 
               ImmArray.sub(mem, Word32.toInt(use_address)))
            end;

      fun Store ((mem, (reads, writes)), address, data)
          = let
              val aligned_address = AlignWAddress address;
              val use_address = Word32.>> (aligned_address, 0wx0002);
            in
              (ImmArray.update(mem, Word32.toInt(use_address), data),
               (reads, writes + 1))
            end;


      fun LoadWord (mem, address)
          = let
              val aligned_address 
                  = if address = AlignWAddress address
                      then address
                      else (print "Error LW: Memory using aligned address\n";
                            AlignWAddress address);
            in
              Load(mem, aligned_address)
            end;
            
      fun StoreWord (mem, address, data)
          = let
              val aligned_address
                  = if address = AlignWAddress address
                      then address
                      else (print "Error SW: Memory using aligned address\n";
                            AlignWAddress address);
            in
              Store(mem, aligned_address, data)
            end;

      fun LoadHWord (mem, address)
          = let
              val aligned_address 
                  = if address = AlignHWAddress address
                      then address
                      else (print "Error LH: Memory using aligned address\n";
                            AlignHWAddress address);
              val (nmem,l_word) = Load(mem, aligned_address);
            in
              (nmem,
               case aligned_address 
                 of 0wx00000000 : Word32.word
                   => Word32.~>>(Word32.<<(l_word, 0wx0010), 
                                 0wx0010)
                  | 0wx00000010 : Word32.word
                   => Word32.~>>(Word32.<<(l_word, 0wx0000),
                                 0wx0010)
                  | _ => (print "Error LH: Memory returning 0\n";
                          0wx00000000 : Word32.word))
            end;

      fun LoadHWordU (mem, address)
          = let
              val aligned_address 
                  = if address = AlignHWAddress address
                      then address
                      else (print "Error LHU: Memory using aligned address\n";
                            AlignHWAddress address);
              val (nmem, l_word) = Load(mem, aligned_address);
            in
              (nmem,
               case aligned_address 
                 of 0wx00000000 : Word32.word
                   => Word32.>>(Word32.<<(l_word, 0wx0010), 
                                0wx0010)
                  | 0wx00000010 : Word32.word
                   => Word32.>>(Word32.<<(l_word, 0wx0000),
                                0wx0010)
                  | _ => (print "Error LHU: Memory returning 0\n";
                          0wx00000000 : Word32.word))
            end;
            
      fun StoreHWord (mem, address, data)
          = let
              val aligned_address
                  = if address = AlignHWAddress address
                      then address
                      else (print "Error SH: Memory using aligned address\n";
                            AlignWAddress address);
              val (_, s_word) = Load(mem, aligned_address);
            in
              case aligned_address
                of 0wx00000000 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wxFFFF0000 : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx0000FFFF : 
                                                            Word32.word,
                                                            data),
                                                0wx0000)))
                 | 0wx00000010 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wx0000FFFF : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx0000FFFF : 
                                                            Word32.word,
                                                            data),
                                                0wx0010)))
                 | _ => (print "Error SH: Memory unchanged\n";
                         mem)
            end;

      fun LoadByte (mem, address)
          = let
              val aligned_address = address;
              val (nmem, l_word) = Load(mem, aligned_address);
            in
              (nmem,
               case aligned_address 
                 of 0wx00000000 : Word32.word
                   => Word32.~>>(Word32.<<(l_word, 
                                           0wx0018), 
                                 0wx0018)
                  | 0wx00000008 : Word32.word
                   => Word32.~>>(Word32.<<(l_word,
                                           0wx0010),
                                 0wx0018)
                  | 0wx00000010 : Word32.word
                   => Word32.~>>(Word32.<<(l_word,
                                           0wx0008),
                                 0wx0018)
                  | 0wx00000018 : Word32.word
                   => Word32.~>>(Word32.<<(l_word,
                                           0wx0000),
                                 0wx0018)
                  | _ => (print "Error LB: Memory returning 0\n";
                          0wx00000000 : Word32.word))
            end;

      fun LoadByteU (mem, address)
          = let
              val aligned_address = address;
              val (nmem, l_word) = Load(mem, aligned_address);
            in
              (nmem,
               case aligned_address 
                 of 0wx00000000 : Word32.word
                   => Word32.>>(Word32.<<(l_word,
                                          0wx0018),
                                0wx0018)
                  | 0wx00000008 : Word32.word
                   => Word32.>>(Word32.<<(l_word,
                                          0wx0010),
                                0wx0018)
                  | 0wx00000010 : Word32.word
                   => Word32.>>(Word32.<<(l_word,
                                          0wx0008),
                                0wx0018)
                  | 0wx00000018 : Word32.word
                   => Word32.>>(Word32.<<(l_word,
                                          0wx0000),
                                0wx0018)
                  | _ => (print "Error LBU: Memory returning 0\n";
                          0wx00000000 : Word32.word))
            end;
            
      fun StoreByte (mem, address, data)
          = let
              val aligned_address = address;
              val (_, s_word) = Load(mem, aligned_address);
            in
              case aligned_address
                of 0wx00000000 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wxFFFFFF00 : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx000000FF : 
                                                            Word32.word,
                                                            data),
                                                0wx0000)))
                 | 0wx00000008 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wxFFFF00FF : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx000000FF : 
                                                            Word32.word,
                                                            data),
                                                0wx0008)))
                 | 0wx00000010 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wxFF00FFFF : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx000000FF : 
                                                            Word32.word,
                                                            data),
                                                0wx0010)))
                 | 0wx00000018 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wx00FFFFFF : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx000000FF : 
                                                            Word32.word,
                                                            data),
                                                0wx0018)))
                 | _ => (print "Error SB: Memory unchanged\n";
                         mem)
            end;

      fun GetStatistics (mem, (reads, writes))
          = "Memory :\n" ^
            "Memory Reads : " ^ (Int.toString reads) ^ "\n" ^
            "Memory Writes : " ^ (Int.toString writes) ^ "\n";
            
    end;

(*****************************************************************************)

(*
 * CacheSpec.sig
 *
 * This defines the signature that outlines the specifications to
 * describe a cache.  The two datatypes are given to provide clear
 * means of differentiating between the write hit and write miss
 * options.  CacheName can be any string describing the cache.
 * CacheSize is an integer that represents the total number of words
 * in the cache.  BlockSize is an integer that represents the total
 * number of words in a block.  Associativity is an integer that
 * represents the associativity of the cache.  WriteHit and WriteMiss
 * represent the write hit and write miss options to be implemented by
 * this cache.
 *)

signature CACHESPEC
  = sig
  
      datatype WriteHitOption = Write_Through
                              | Write_Back;

      datatype WriteMissOption = Write_Allocate
                               | Write_No_Allocate;

      val CacheName : string;
      val CacheSize : int;
      val BlockSize : int;
      val Associativity : int;
      val WriteHit : WriteHitOption;
      val WriteMiss : WriteMissOption;

    end;

(*****************************************************************************)

(*
 * CachedMemory.sml
 *
 * This defines the CachedMemory functor, which provides the
 * functionality of a cached memory and which takes two structures,
 * corresponding to the cache specification and the the level of
 * memory which the cache will be caching.  The datatype memory
 * provides the encapsulation of the cache along with the memory
 * system that is being cached, InitMemory initializes the cache and
 * the memory system that is being cached, LoadWord takes memory and a
 * Word32.word corresponding to the address, and returns the
 * Word32.word at that address and the updated cache and memory,
 * StoreWord takes memory, a Word32.word corresponding to the address,
 * and a Word32.word and returns the cache and memory updated with the
 * stored at the appropriate address.  LoadHWord, LoadHWordU, 
 * LoadByte, and LoadByteU load halfwords, unsigned halfwords,
 * bytes, and unsigned bytes respectively from memory into the
 * lower portion of the returned Word32.word.  StoreHWord and 
 * StoreByte store halfwords and bytes taken from the lower portion
 * of the Word32.word into memory.
 * GetStatistics takes memory and returns the read and write 
 * statistics as a string.
 *
 * The underlying structure of cache is a two dimensional array of
 * cache lines, where a cache line consists of a valid bit, dirty bit,
 * a tag and a block of words, as a Word32.word array.
 * The size of the cache, the associativity, and the block size are
 * specified by the cache specification.
 *
 * Also, the functions AlignWAddress and AlignHWAddress aligns a memory 
 * address to a word and halfword address, respectively.  If LoadWord,
 * StoreWord, LoadHWord, LoadHWordU, or StoreHWord is asked to access an 
 * unaligned address, it writes an error message, and uses the address 
 * rounded down to the aligned address.
 *)

functor CachedMemory (structure CS : CACHESPEC;
                      structure MEM : MEMORY;) : MEMORY 
  = struct
  
      type cacheline 
           = bool * bool * Word32.word * Word32.word ImmArray.immarray;

      type cacheset
           = cacheline ImmArray.immarray;

      type cache 
           = cacheset ImmArray.immarray;
            
      type memory = (cache * (int * int * int * int)) * MEM.memory;
            
            
      (* Performs log[base2] on an integer. *)
      fun exp2 0 = 1
        | exp2 n = 2 * (exp2 (n-1))
      fun log2 x = let
                     fun log2_aux n = if exp2 n > x
                                        then (n-1)
                                        else log2_aux (n+1)
                   in 
                     log2_aux 0
                   end

      open CS;

      (*
       * The following values of index size and field bits are
       * calculated from the values in the cache specification
       * structure.
       *)
      val IndexSize = CacheSize div (BlockSize * Associativity);
      val BlockOffsetBits = log2 (BlockSize * 4);
      val IndexBits = log2 IndexSize;
      val TagBits = 32 - BlockOffsetBits - IndexBits;
            

      (*
       * RandEntry returns a random number between 
       * [0, Associativity - 1].  It is used to determine
       * replacement of data in the cache.
       *)
      val RandEntry = let
                        val modulus = Word.fromInt(Associativity - 1)
                      in 
                        fn () => Word.toInt(Word.mod(rand (),
                                                     modulus))
                      end

      (* 
       * The InitCache function initializes the cache to
       * not-valid, not-dirty, 0wx00000000 tag, blocks initialized
       * to 0wx00000000.  
       *)
      fun InitCache ()
          = let
              val cacheline = (false, false, 0wx00000000 : Word32.word,
                               ImmArray.immarray (BlockSize,
                                                  0wx00000000 : Word32.word));
              val cacheset = ImmArray.immarray (Associativity, cacheline);
            in 
              (ImmArray.immarray (IndexSize, cacheset),
               (0, 0, 0, 0))
            end;
         

      (* 
       * The InitMemory function initializes the cache
       * and the memory being cached.
       *)
      fun InitMemory () = (InitCache (), MEM.InitMemory ()) : memory;
            
        
      (* 
       * GetTag returns the Word32.word corresponding to the tag field of
       * address
       *)
      fun GetTag address 
          = Word32.>> (address, 
                       Word.fromInt (IndexBits + BlockOffsetBits));
            

      (* 
       * GetIndex returns the Word32.word corresponding to the index
       * field of address.
       *)
      fun GetIndex address
          = let
              val mask
                  = Word32.notb 
                    (Word32.<< 
                     (Word32.>> (0wxFFFFFFFF : Word32.word,
                                 Word.fromInt (IndexBits + BlockOffsetBits)), 
                      Word.fromInt (IndexBits + BlockOffsetBits)));
            in
              Word32.>> (Word32.andb (address, mask),
                         Word.fromInt (BlockOffsetBits))
            end;
            

      (* 
       * GetBlockOffset returns the Word32.word corresponding to the
       * block offset field of address.
       *)
      fun GetBlockOffset address
          = let
              val mask
                  = Word32.notb 
                    (Word32.<< 
                     (Word32.>> (0wxFFFFFFFF : Word32.word,
                                 Word.fromInt BlockOffsetBits), 
                      Word.fromInt BlockOffsetBits));
            in
              Word32.andb (address, mask)
            end;
            
           
      (* 
       * The InCache* family of functions returns a boolean value
       * that determines if the word specified by address is in the
       * cache at the current time (and that the data is valid).
       *)
      fun InCache_aux_entry ((valid, dirty, tag, block), address)
          = tag = (GetTag address) andalso valid;

      fun InCache_aux_set (set, address)
          = ImmArray.foldr (fn (entry, result) => 
                               (InCache_aux_entry (entry, address)) orelse 
                               result) 
                           false 
                           set;

      fun InCache (cac, address)
          = InCache_aux_set (ImmArray.sub (cac, 
                                           Word32.toInt (GetIndex address)),
                             address);

      (* 
       * The ReadCache* family of functions returns the Word32.word 
       * stored at address in the cache.
       *)
      fun ReadCache_aux_entry ((valid, dirty, tag, block), address) 
          = ImmArray.sub (block, 
                          Word32.toInt (Word32.>> (GetBlockOffset address, 
                                                   0wx0002)));
        
      fun ReadCache_aux_set (set, address) 
          = ImmArray.foldr (fn (entry, result) =>
                               if InCache_aux_entry (entry, address)
                                 then ReadCache_aux_entry (entry, address)
                                 else result)
                           (0wx00000000 : Word32.word)
                           set;
        
      fun ReadCache (cac, address)
          = ReadCache_aux_set (ImmArray.sub (cac, 
                                             Word32.toInt(GetIndex address)),
                               address);


      (* 
       * The WriteCache* family of functions returns the updated
       * cache with data stored at address.
       *)
      fun WriteCache_aux_entry ((valid, dirty, tag, block), address, data)
          = let
              val ndirty = case WriteHit
                             of Write_Through => false
                              | Write_Back => true; 
            in
              (true, ndirty, tag, 
               ImmArray.update (block, 
                                Word32.toInt (Word32.>> 
                                              (GetBlockOffset address, 
                                               0wx0002)), 
                                data))
            end;
            
      fun WriteCache_aux_set (set, address, data) 
          = ImmArray.map (fn entry =>
                             if InCache_aux_entry (entry, address)
                               then WriteCache_aux_entry (entry, address, 
                                                          data)
                               else entry)
                         set;

      fun WriteCache (cac, address, data)
          = let
              val index = Word32.toInt (GetIndex address);
              val nset = WriteCache_aux_set (ImmArray.sub (cac, index),
                                             address, data);
            in
              ImmArray.update (cac, index, nset)
            end;


      (* 
       * The LoadBlock function returns the updated
       * memory and the block containing address loaded from memory.
       *)
      fun LoadBlock (mem, address)
          = ImmArray.foldr (fn (offset, (block, mem)) =>
                               let
                                 val laddress
                                     = Word32.+ (Word32.<<
                                                 (Word32.>> 
                                                  (address,
                                                   Word.fromInt 
                                                   BlockOffsetBits),
                                                  Word.fromInt 
                                                  BlockOffsetBits), 
                                                 Word32.<< (Word32.fromInt 
                                                            offset, 
                                                            0wx0002));
                                 val (nmem, nword) = MEM.LoadWord (mem, 
                                                                   laddress);
                               in
                                 (ImmArray.update (block, offset, nword), nmem)
                               end)
                           (ImmArray.immarray (BlockSize, 
                                               0wx00000000 : Word32.word), mem)
                           (ImmArray.tabulate (BlockSize, fn i => i));

 
      (* 
       * The StoreBlock functionsreturns the updated
       * memory with block stored into the block containing address.
       *)
      fun StoreBlock (block, mem, address)
          = ImmArray.foldr (fn (offset, mem) =>
                               let
                                 val saddress
                                     = Word32.+ (Word32.<<
                                                 (Word32.>> 
                                                  (address,
                                                   Word.fromInt 
                                                   BlockOffsetBits),
                                                  Word.fromInt 
                                                  BlockOffsetBits), 
                                                 Word32.<< (Word32.fromInt 
                                                            offset, 
                                                            0wx0002));
                               in
                                 MEM.StoreWord (mem, saddress,
                                                ImmArray.sub (block, offset))
                               end)
                           mem
                           (ImmArray.tabulate (BlockSize, fn i => i));


      (* 
       * The LoadCache* family of functions returns the updated
       * cache and memory, with the block containing address loaded
       * into the cache at the appropriate cache line, and dirty
       * data written back to memory as needed.
       *)
      fun LoadCache_aux_entry ((valid, dirty, tag, block), mem, address)
          = let
              val saddress 
                  = Word32.orb (Word32.<< (tag,
                                           Word.fromInt TagBits),
                                Word32.<< (GetIndex address,
                                           Word.fromInt IndexBits));
              val nmem = if valid andalso dirty
                           then StoreBlock (block, mem, saddress)
                           else mem;
              val (nblock, nnmem) = LoadBlock (nmem, address);
            in
              ((true, false, GetTag address, nblock), nnmem)
            end;

      fun LoadCache_aux_set (set, mem, address)
          = let
              val entry = RandEntry ();
              val (nentry, nmem) = LoadCache_aux_entry (ImmArray.sub (set, 
                                                                      entry),
                                                        mem, address);
            in
              (ImmArray.update (set, entry, nentry), nmem)
            end;

      fun LoadCache (cac, mem, address) 
          = let
              val index = Word32.toInt (GetIndex address);
              val (nset, nmem) 
                  = LoadCache_aux_set (ImmArray.sub (cac, index), 
                                       mem, address);
            in
              (ImmArray.update (cac, index, nset), nmem)
            end;


      (* 
       * The remainder of the function defined here satisfy the MEMORY
       * signature.  This allows a CachedMemory to act exactly like
       * a normal Memory, and thus caches can be nested to an arbitrary
       * depth.
       *)

      fun AlignWAddress address 
          = Word32.<< (Word32.>> (address, 0wx0002), 0wx0002);
            
      fun AlignHWAddress address
          = Word32.<< (Word32.>> (address, 0wx0001), 0wx0001);
            
      (* Load and Store provide errorless access to memory.
       * They provide a common interface to memory, while
       * the LoadX and StoreX specifically access words,
       * halfwords and bytes, requiring address to be aligned.
       * In Load and Store, two intermediate values are
       * generated.  The value aligned_address is the aligned
       * version of the given address, and is used to compare with
       * the original address to determine if it was aligned.  The 
       * value use_address is equivalent to aligned_address divided 
       * by four, and it corresponds to the index of the memory 
       * array where the corresponding aligned address can be found.
       *)

      fun Load (((cac, (rh, rm, wh, wm)), mem), address)
          = let
              val aligned_address = AlignWAddress address;
            in
              if InCache (cac, aligned_address)
                then (((cac, (rh + 1, rm, wh, wm)), mem), 
                      ReadCache (cac, aligned_address))
                else let
                       val (ncac, nmem) 
                           = LoadCache (cac, mem, aligned_address);
                     in
                       (((ncac, (rh, rm + 1, wh, wm)), nmem),
                        ReadCache (ncac, aligned_address))
                     end
            end;


      fun Store (((cac, (rh, rm, wh, wm)), mem), address, data)
          = let
              val aligned_address = AlignWAddress address;
            in
              if InCache (cac, aligned_address)
                then let
                       val ncac = WriteCache (cac, aligned_address, data);
                     in
                       case WriteHit 
                         of Write_Through => 
                              ((ncac, (rh, rm, wh + 1, wm)),
                               MEM.StoreWord (mem, aligned_address, data))
                          | Write_Back => 
                              ((ncac, (rh, rm, wh + 1, wm)), mem)
                     end
                else case WriteMiss
                       of Write_Allocate =>
                            let
                              val (ncac, nmem) 
                                = LoadCache (cac, mem, aligned_address);
                              val nncac
                                = WriteCache (ncac, aligned_address, data);
                            in
                              case WriteHit
                                of Write_Through => 
                                     ((nncac, (rh, rm, wh, wm + 1)), 
                                      MEM.StoreWord (nmem, aligned_address, 
                                                     data))
                                 | Write_Back =>
                                     ((nncac, (rh, rm, wh, wm + 1)),
                                      nmem)
                            end
                        | Write_No_Allocate =>
                            ((cac, (rh, rm, wh, wm + 1)),
                             MEM.StoreWord (mem, aligned_address, data))
            end;

      fun LoadWord (mem, address)
          = let
              val aligned_address 
                  = if address = AlignWAddress address
                      then address
                      else (print "Error LW: Memory using aligned address\n";
                            AlignWAddress address);
            in
              Load(mem, aligned_address)
            end;
            
      fun StoreWord (mem, address, data)
          = let
              val aligned_address
                  = if address = AlignWAddress address
                      then address
                      else (print "Error SW: Memory using aligned address\n";
                            AlignWAddress address);
            in
              Store(mem, aligned_address, data)
            end;

      fun LoadHWord (mem, address)
          = let
              val aligned_address 
                  = if address = AlignHWAddress address
                      then address
                      else (print "Error LH: Memory using aligned address\n";
                            AlignHWAddress address);
              val (nmem,l_word) = Load(mem, aligned_address);
            in
              (nmem,
               case aligned_address 
                 of 0wx00000000 : Word32.word
                   => Word32.~>>(Word32.<<(l_word, 0wx0010), 
                                 0wx0010)
                  | 0wx00000010 : Word32.word
                   => Word32.~>>(Word32.<<(l_word, 0wx0000),
                                 0wx0010)
                  | _ => (print "Error LH: Memory returning 0\n";
                          0wx00000000 : Word32.word))
            end;

      fun LoadHWordU (mem, address)
          = let
              val aligned_address 
                  = if address = AlignHWAddress address
                      then address
                      else (print "Error LHU: Memory using aligned address\n";
                            AlignHWAddress address);
              val (nmem, l_word) = Load(mem, aligned_address);
            in
              (nmem,
               case aligned_address 
                 of 0wx00000000 : Word32.word
                   => Word32.>>(Word32.<<(l_word, 0wx0010), 
                                0wx0010)
                  | 0wx00000010 : Word32.word
                   => Word32.>>(Word32.<<(l_word, 0wx0000),
                                0wx0010)
                  | _ => (print "Error LHU: Memory returning 0\n";
                          0wx00000000 : Word32.word))
            end;
            
      fun StoreHWord (mem, address, data)
          = let
              val aligned_address
                  = if address = AlignHWAddress address
                      then address
                      else (print "Error SH: Memory using aligned address\n";
                            AlignWAddress address);
              val (_, s_word) = Load(mem, aligned_address);
            in
              case aligned_address
                of 0wx00000000 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wxFFFF0000 : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx0000FFFF : 
                                                            Word32.word,
                                                            data),
                                                0wx0000)))
                 | 0wx00000010 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wx0000FFFF : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx0000FFFF : 
                                                            Word32.word,
                                                            data),
                                                0wx0010)))
                 | _ => (print "Error SH: Memory unchanged\n";
                         mem)
            end;

      fun LoadByte (mem, address)
          = let
              val aligned_address = address;
              val (nmem, l_word) = Load(mem, aligned_address);
            in
              (nmem,
               case aligned_address 
                 of 0wx00000000 : Word32.word
                   => Word32.~>>(Word32.<<(l_word, 
                                           0wx0018), 
                                 0wx0018)
                  | 0wx00000008 : Word32.word
                   => Word32.~>>(Word32.<<(l_word,
                                           0wx0010),
                                 0wx0018)
                  | 0wx00000010 : Word32.word
                   => Word32.~>>(Word32.<<(l_word,
                                           0wx0008),
                                 0wx0018)
                  | 0wx00000018 : Word32.word
                   => Word32.~>>(Word32.<<(l_word,
                                           0wx0000),
                                 0wx0018)
                  | _ => (print "Error LB: Memory returning 0\n";
                          0wx00000000 : Word32.word))
            end;

      fun LoadByteU (mem, address)
          = let
              val aligned_address = address;
              val (nmem, l_word) = Load(mem, aligned_address);
            in
              (nmem,
               case aligned_address 
                 of 0wx00000000 : Word32.word
                   => Word32.>>(Word32.<<(l_word,
                                          0wx0018),
                                0wx0018)
                  | 0wx00000008 : Word32.word
                   => Word32.>>(Word32.<<(l_word,
                                          0wx0010),
                                0wx0018)
                  | 0wx00000010 : Word32.word
                   => Word32.>>(Word32.<<(l_word,
                                          0wx0008),
                                0wx0018)
                  | 0wx00000018 : Word32.word
                   => Word32.>>(Word32.<<(l_word,
                                          0wx0000),
                                0wx0018)
                  | _ => (print "Error LBU: Memory returning 0\n";
                          0wx00000000 : Word32.word))
            end;
            
      fun StoreByte (mem, address, data)
          = let
              val aligned_address = address;
              val (_, s_word) = Load(mem, aligned_address);
            in
              case aligned_address
                of 0wx00000000 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wxFFFFFF00 : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx000000FF : 
                                                            Word32.word,
                                                            data),
                                                0wx0000)))
                 | 0wx00000008 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wxFFFF00FF : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx000000FF : 
                                                            Word32.word,
                                                            data),
                                                0wx0008)))
                 | 0wx00000010 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wxFF00FFFF : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx000000FF : 
                                                            Word32.word,
                                                            data),
                                                0wx0010)))
                 | 0wx00000018 : Word32.word
                  => Store(mem, aligned_address,
                           Word32.orb(Word32.andb(0wx00FFFFFF : Word32.word,
                                                  s_word),
                                      Word32.<<(Word32.andb(0wx000000FF : 
                                                            Word32.word,
                                                            data),
                                                0wx0018)))
                 | _ => (print "Error SB: Memory unchanged\n";
                         mem)
            end;

      fun GetStatistics ((cac, (rh, rm, wh, wm)), mem)
          = let

              val th = rh + wh;
                
              val tm = rm + wm;

              val who = case WriteHit
                          of Write_Through => "Write Through"
                           | Write_Back => "Write Back";

              val wmo = case WriteMiss
                          of Write_Allocate => "Write Allocate"
                           | Write_No_Allocate => "Write No Allocate";

            in
              CacheName ^ " :\n" ^
              "CacheSize : " ^ (Int.toString CacheSize) ^ "\n" ^
              "BlockSize : " ^ (Int.toString BlockSize) ^ "\n" ^
              "Associativity : " ^ (Int.toString Associativity) ^ "\n" ^
              "Write Hit : " ^ who ^ "\n" ^
              "Write Miss : " ^ wmo ^ "\n" ^
              "Read hits : " ^ (Int.toString rh) ^ "\n" ^
              "Read misses : " ^ (Int.toString rm) ^ "\n" ^
              "Write hits : " ^ (Int.toString wh) ^ "\n" ^
              "Write misses : " ^ (Int.toString wm) ^ "\n" ^
              "Total hits : " ^ (Int.toString th) ^ "\n" ^
              "Total misses : " ^ (Int.toString tm) ^ "\n" ^
              (MEM.GetStatistics mem)
            end;

    end;

(*****************************************************************************)

(*
 * DLXSimulator.sig
 *
 * This defines the exported function provided by the DLXSimulator.
 * The function run_file takes a string corresponding to the name of the
 * file to be run, and executes it.  The function run_prog takes a
 * list of instructions and executes them.
 *)

signature DLXSIMULATOR
  = sig
  
      val run_file : string -> unit;
      val run_prog : string list -> unit; 
        
    end;

(*****************************************************************************)

(*
 * DLXSimulator.sml
 *
 * This defines the DLXSimulatorFun functor, which takes three
 * structures, corresponding to the register file, the ALU, and memory,
 * and provides the functionality of a DLX processor, able to execute 
 * DLX programs.  The function run_file takes a string corresponding to the
 * name of the file to be executed, and executes it.  The function
 * run_prog takes a list of instructions and executes them.
 *)

functor DLXSimulatorFun (structure RF : REGISTERFILE; 
                         structure ALU : ALU;
                         structure MEM : MEMORY; ) : DLXSIMULATOR
  = struct
                
      (*
       * The datatype Opcode provides a means of differentiating *
       * among the main opcodes.
       *)
      datatype Opcode = 
        (* for R-type opcodes *)
        SPECIAL |
        (* I-type opcodes *)
        BEQZ | BNEZ |
        ADDI | ADDUI | SUBI | SUBUI |
        ANDI | ORI | XORI |
        LHI |
        SLLI | SRLI | SRAI | 
        SEQI | SNEI | SLTI | SGTI | SLEI | SGEI |
        LB | LBU | SB |
        LH | LHU | SH |
        LW | SW |
        (* J-type opcodes *)
        J | JAL | TRAP | JR | JALR |
        (* Unrecognized opcode *)
        NON_OP; 
            
      (*
       * The datatype RRFuncCode provides a means of 
       * differentiating among
       * the register-register function codes.
       *) 
      datatype RRFunctCode = NOP | SLL | SRL | SRA |
                             ADD | ADDU | SUB | SUBU |
                             AND | OR | XOR |
                             SEQ | SNE | SLT | SGT | SLE | SGE |
                             NON_FUNCT;
            
      (*
       * The datatype Instruction provides a means of
       * differentiating among the three different types of
       * instructions, I-type, R-type, and J-type.
       * An I-type is interpreted as (opcode, rs1, rd, immediate).
       * An R-type is interpreted as (opcode, rs1, rs2, rd, shamt, funct).
       * An J-type is interpreted as (opcode, offset).
       * An ILLEGAL causes the simulator to end.
       *)
      datatype Instruction 
        = ITYPE of Opcode * int * int * Word32.word
        | RTYPE of Opcode * int * int * int * int * RRFunctCode 
        | JTYPE of Opcode * Word32.word 
        | ILLEGAL;

      (*
       * The value HALT is set to the DLX instruction TRAP #0,
       * and is used to check for the halt of the program.
       *)
      val HALT = JTYPE (TRAP, 0wx00000000);
  
      (*
       * The function DecodeIType decodes a Word32.word into an
       * I-type instruction.
       *)
      fun DecodeIType instr
          = let
              val opc = Word32.andb (Word32.>> (instr,
                                                0wx001A),
                                     0wx0000003F : Word32.word);
              
              val opcode = case opc
                             of 0wx00000004 : Word32.word => BEQZ
                              | 0wx00000005 : Word32.word => BNEZ
                              | 0wx00000008 : Word32.word => ADDI
                              | 0wx00000009 : Word32.word => ADDUI
                              | 0wx0000000A : Word32.word => SUBI
                              | 0wx0000000B : Word32.word => SUBUI
                              | 0wx0000000C : Word32.word => ANDI
                              | 0wx0000000D : Word32.word => ORI
                              | 0wx0000000E : Word32.word => XORI
                              | 0wx0000000F : Word32.word => LHI
                              | 0wx00000014 : Word32.word => SLLI
                              | 0wx00000016 : Word32.word => SRLI
                              | 0wx00000017 : Word32.word => SRAI
                              | 0wx00000018 : Word32.word => SEQI
                              | 0wx00000019 : Word32.word => SNEI
                              | 0wx0000001A : Word32.word => SLTI
                              | 0wx0000001B : Word32.word => SGTI
                              | 0wx0000001C : Word32.word => SLEI
                              | 0wx0000001D : Word32.word => SGEI
                              | 0wx00000020 : Word32.word => LB
                              | 0wx00000024 : Word32.word => LBU
                              | 0wx00000028 : Word32.word => SB
                              | 0wx00000021 : Word32.word => LH
                              | 0wx00000025 : Word32.word => LHU
                              | 0wx00000029 : Word32.word => SH
                              | 0wx00000023 : Word32.word => LW
                              | 0wx0000002B : Word32.word => SW
                              | _ => (print "Error : Non I-Type opcode\n";
                                      NON_OP);
                             
              val rs1 = Word32.toInt(Word32.andb (Word32.>> (instr, 0wx0015),
                                                  0wx0000001F : Word32.word));
                    
              val rd = Word32.toInt(Word32.andb (Word32.>> (instr, 0wx0010),
                                                 0wx0000001F : Word32.word));
                    
              val immediate = Word32.~>> (Word32.<< (instr, 0wx0010),
                                          0wx0010);

            in
              if opcode = NON_OP
                then ILLEGAL
                else ITYPE (opcode, rs1, rd, immediate)
            end;
            
      (*
       * The function DecodeRType decodes a Word32.word into an
       * R-type instruction.
       *)
      fun DecodeRType instr
          = let
                
              val rs1 = Word32.toInt (Word32.andb (Word32.>> (instr, 0wx0015),
                                                   0wx0000001F : Word32.word));
                
              val rs2 = Word32.toInt (Word32.andb (Word32.>> (instr, 0wx0010),
                                                   0wx0000001F : Word32.word));
                
              val rd = Word32.toInt (Word32.andb (Word32.>> (instr, 0wx000B),
                                                  0wx0000001F : Word32.word));
                
              val shamt 
                  = Word32.toInt (Word32.andb (Word32.>> (instr, 0wx0006),
                                               0wx0000001F : Word32.word));
                    
              val funct = Word32.andb (instr, 0wx0000003F : Word32.word);
                    
              val functcode = case funct
                                of 0wx00000000 : Word32.word => NOP
                                 | 0wx00000004 : Word32.word => SLL
                                 | 0wx00000006 : Word32.word => SRL
                                 | 0wx00000007 : Word32.word => SRA
                                 | 0wx00000020 : Word32.word => ADD
                                 | 0wx00000021 : Word32.word => ADDU
                                 | 0wx00000022 : Word32.word => SUB
                                 | 0wx00000023 : Word32.word => SUBU
                                 | 0wx00000024 : Word32.word => AND
                                 | 0wx00000025 : Word32.word => OR
                                 | 0wx00000026 : Word32.word => XOR
                                 | 0wx00000028 : Word32.word => SEQ
                                 | 0wx00000029 : Word32.word => SNE
                                 | 0wx0000002A : Word32.word => SLT
                                 | 0wx0000002B : Word32.word => SGT
                                 | 0wx0000002C : Word32.word => SLE
                                 | 0wx0000002D : Word32.word => SGE
                                 | _ => (print "Error : Non R-type funct\n";
                                         NON_FUNCT);
                                     
            in
              if functcode = NON_FUNCT
                then ILLEGAL
                else RTYPE (SPECIAL, rs1, rs2, rd, shamt, functcode)
            end;
            
      (*
       * The function DecodeJType decodes a Word32.word into an
       * J-type instruction.
       *)
      fun DecodeJType instr 
          = let

              val opc = Word32.andb (Word32.>> (instr, 0wx1A),
                                     0wx0000003F : Word32.word);
                    
              val opcode = case opc
                             of 0wx00000002 : Word32.word => J
                              | 0wx00000003 : Word32.word => JAL
                              | 0wx00000011 : Word32.word => TRAP
                              | 0wx00000012 : Word32.word => JR
                              | 0wx00000013 : Word32.word => JALR
                              | _ => (print "Error : Non J-type opcode\n";
                                      NON_OP);
                                  
              val offset = Word32.~>> (Word32.<< (instr, 0wx0006),
                                       0wx0006);

            in
                if opcode = NON_OP
                    then ILLEGAL
                    else JTYPE (opcode, offset)
            end;
            
      (*
       * The function DecodeInstr decodes a Word32.word into an
       * instruction.  It first checks the opcode, and then calls 
       * one of DecodeIType, DecodeJType, and DecodeRType to
       * complete the decoding process.
       *)
      fun DecodeInstr instr
          = let

              val opcode = Word32.andb (Word32.>> (instr, 0wx1A),
                                        0wx0000003F : Word32.word);
                
            in
              case opcode
                of 0wx00000000 : Word32.word => DecodeRType instr
                 | 0wx00000002 : Word32.word => DecodeJType instr
                 | 0wx00000003 : Word32.word => DecodeJType instr
                 | 0wx00000004 : Word32.word => DecodeIType instr
                 | 0wx00000005 : Word32.word => DecodeIType instr
                 | 0wx00000008 : Word32.word => DecodeIType instr
                 | 0wx00000009 : Word32.word => DecodeIType instr
                 | 0wx0000000A : Word32.word => DecodeIType instr
                 | 0wx0000000B : Word32.word => DecodeIType instr
                 | 0wx0000000C : Word32.word => DecodeIType instr
                 | 0wx0000000D : Word32.word => DecodeIType instr
                 | 0wx0000000E : Word32.word => DecodeIType instr
                 | 0wx0000000F : Word32.word => DecodeIType instr
                 | 0wx00000011 : Word32.word => DecodeJType instr
                 | 0wx00000012 : Word32.word => DecodeJType instr
                 | 0wx00000013 : Word32.word => DecodeJType instr
                 | 0wx00000016 : Word32.word => DecodeIType instr
                 | 0wx00000017 : Word32.word => DecodeIType instr
                 | 0wx00000018 : Word32.word => DecodeIType instr
                 | 0wx00000019 : Word32.word => DecodeIType instr
                 | 0wx0000001A : Word32.word => DecodeIType instr
                 | 0wx0000001B : Word32.word => DecodeIType instr
                 | 0wx0000001C : Word32.word => DecodeIType instr
                 | 0wx0000001D : Word32.word => DecodeIType instr
                 | 0wx00000020 : Word32.word => DecodeIType instr
                 | 0wx00000024 : Word32.word => DecodeIType instr
                 | 0wx00000028 : Word32.word => DecodeIType instr
                 | 0wx00000021 : Word32.word => DecodeIType instr
                 | 0wx00000025 : Word32.word => DecodeIType instr
                 | 0wx00000029 : Word32.word => DecodeIType instr
                 | 0wx00000023 : Word32.word => DecodeIType instr
                 | 0wx0000002B : Word32.word => DecodeIType instr
                 | _ => (print "Error : Unrecognized opcode\n";
                         ILLEGAL)
            end;
            

      (*
       * The function PerformIType performs one of the I-Type
       * instructions.  A number of the instructions make use of the
       * ALU, and as such, call ALU.PerformAL.
       *)
      fun PerformIType ((BEQZ, rs1, rd, immediate), (PC, rf, mem)) 
        = if (RF.LoadRegister(rf, rs1) = (0wx00000000 : Word32.word))
            then (Word32.fromInt (Int.+ (Word32.toIntX PC,
                                                Word32.toIntX
                                                (Word32.<< (immediate, 
                                                            0wx0002)))),
                  rf, mem)
            else (PC, rf, mem)
              
        | PerformIType ((BNEZ, rs1, rd, immediate), (PC, rf, mem)) 
          = if not (RF.LoadRegister(rf, rs1) = (0wx00000000 : Word32.word))
              then (Word32.fromInt (Int.+ (Word32.toIntX PC,
                                                  Word32.toIntX
                                                  (Word32.<< (immediate,
                                                              0wx0002)))),
                    rf, mem)
              else (PC, rf, mem)
            
        | PerformIType ((ADDI, rs1, rd, immediate), (PC, rf, mem))
          = (PC, 
             RF.StoreRegister(rf, rd, 
                              ALU.PerformAL(ALU.ADD,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)

        | PerformIType ((ADDUI, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             RF.StoreRegister(rf, rd, 
                              ALU.PerformAL(ALU.ADDU,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)
            
        | PerformIType ((SUBI, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             RF.StoreRegister(rf, rd, 
                              ALU.PerformAL(ALU.SUB,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)

        | PerformIType ((SUBUI, rs1, rd, immediate), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd, 
                              ALU.PerformAL(ALU.SUBU,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)
            
        | PerformIType ((ANDI, rs1, rd, immediate), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd, 
                              ALU.PerformAL(ALU.AND,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)
            
        | PerformIType ((ORI, rs1, rd, immediate), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd, 
                              ALU.PerformAL(ALU.OR,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)
          
        | PerformIType ((XORI, rs1, rd, immediate), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd, 
                              ALU.PerformAL(ALU.XOR,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)
                    
        | PerformIType ((LHI, rs1, rd, immediate), (PC, rf, mem)) 
          = (PC, RF.StoreRegister(rf, rd, Word32.<< (immediate, 0wx0010)), mem)

        | PerformIType ((SLLI, rs1, rd, immediate), (PC, rf, mem))
          = (PC, RF.StoreRegister(rf, rd, 
                                  Word32.<< (RF.LoadRegister(rf, rs1),
                                             Word.fromLarge (Word32.toLarge immediate))),
             mem)

        | PerformIType ((SRLI, rs1, rd, immediate), (PC, rf, mem))
          = (PC, RF.StoreRegister(rf, rd, 
                                  Word32.>> (RF.LoadRegister(rf, rs1),
                                             Word.fromLarge (Word32.toLarge immediate))),
             mem)

        | PerformIType ((SRAI, rs1, rd, immediate), (PC, rf, mem))
          = (PC, RF.StoreRegister(rf, rd, 
                                  Word32.~>> (RF.LoadRegister(rf, rs1),
                                              Word.fromLarge (Word32.toLarge immediate))),
             mem)

        | PerformIType ((SEQI, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SEQ,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)

        | PerformIType ((SNEI, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SNE,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)

        | PerformIType ((SLTI, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SLT,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)

        | PerformIType ((SGTI, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SGT,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)

        | PerformIType ((SLEI, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SLE,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)

        | PerformIType ((SGEI, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SGE,
                                            RF.LoadRegister(rf, rs1),
                                            immediate)),
             mem)
          
        | PerformIType ((LB, rs1, rd, immediate), (PC, rf, mem))
          = let
              val (nmem, l_byte)
                  = MEM.LoadByte(mem, Word32.+ (RF.LoadRegister(rf, rs1),
                                                immediate));
            in
              (PC,
               RF.StoreRegister(rf, rd, l_byte),
               nmem)
            end

        | PerformIType ((LBU, rs1, rd, immediate), (PC, rf, mem))
          = let
              val (nmem, l_byte)
                  = MEM.LoadByteU(mem, Word32.+ (RF.LoadRegister(rf, rs1),
                                                 immediate));
            in
              (PC,
               RF.StoreRegister(rf, rd, l_byte),
               nmem)
            end
          
        | PerformIType ((SB, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             rf,
             MEM.StoreByte(mem, 
                           Word32.+ (RF.LoadRegister(rf, rs1), immediate),
                           Word32.andb(0wx000000FF, RF.LoadRegister(rf, rd))))
            
        | PerformIType ((LH, rs1, rd, immediate), (PC, rf, mem))
          = let
              val (nmem, l_hword)
                  = MEM.LoadHWord(mem, Word32.+ (RF.LoadRegister(rf, rs1),
                                                 immediate));
            in
              (PC,
               RF.StoreRegister(rf, rd, l_hword),
               nmem)
            end

        | PerformIType ((LHU, rs1, rd, immediate), (PC, rf, mem))
          = let
              val (nmem, l_hword)
                  = MEM.LoadHWordU(mem, Word32.+ (RF.LoadRegister(rf, rs1),
                                                  immediate));
            in
              (PC,
               RF.StoreRegister(rf, rd, l_hword),
               nmem)
            end
          
        | PerformIType ((SH, rs1, rd, immediate), (PC, rf, mem))
          = (PC,
             rf,
             MEM.StoreByte(mem, 
                           Word32.+ (RF.LoadRegister(rf, rs1), immediate),
                           Word32.andb(0wx0000FFFF, RF.LoadRegister(rf, rd))))
            

        | PerformIType ((LW, rs1, rd, immediate), (PC, rf, mem)) 
          = let
              val (nmem, l_word) 
                  = MEM.LoadWord(mem, Word32.+ (RF.LoadRegister(rf, rs1),
                                                immediate));
            in
              (PC,
               RF.StoreRegister(rf, rd, l_word),
               nmem)
            end

        | PerformIType ((SW, rs1, rd, immediate), (PC, rf, mem)) 
          = (PC,
             rf,
             MEM.StoreWord(mem,
                           Word32.+ (RF.LoadRegister(rf, rs1), immediate),
                           RF.LoadRegister(rf, rd)))
            
        | PerformIType ((_, rs1, rd, immediate), (PC, rf, mem)) 
          = (print "Error : Non I-Type opcode, performing NOP\n";
             (PC, rf, mem));


      (*
       * The function PerformRType performs one of the R-Type
       * instructions.  All of the instructions make use of the
       * ALU, and as such, call ALU.PerformAL.
       *)
      fun PerformRType ((SPECIA, rs1, rs2, rd, shamt, NOP), (PC, rf, mem))
          = (PC, rf, mem)

        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SLL), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SLL,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)

        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SRL), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SRL,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)

        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SRA), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SRA,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)

        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, ADD), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.ADD,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)

        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, ADDU), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.ADDU,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SUB), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SUB,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)

        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SUBU), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SUBU,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, AND), (PC, rf, mem))
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.AND,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, OR), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.OR,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, XOR), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.XOR,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SEQ), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SEQ,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SNE), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SNE,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SLT), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SLT,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SGT), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SGT,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SLE), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SLE,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SGE), (PC, rf, mem)) 
          = (PC,
             RF.StoreRegister(rf, rd,
                              ALU.PerformAL(ALU.SGE,
                                            RF.LoadRegister(rf, rs1),
                                            RF.LoadRegister(rf, rs2))),
             mem)
          
        | PerformRType ((_, rs1, rs2, rd, shamt, _), (PC, rf, mem)) 
          = (print "Error : Non R-Type opcode, performing NOP\n";
             (PC, rf, mem));

          
      (*
       * The function PerformJType performs one of the J-Type
       * instructions.
       *)
      fun PerformJType ((J, offset), (PC, rf, mem)) 
          = (Word32.fromInt (Int.+ (Word32.toIntX PC,
                                           Word32.toIntX 
                                           (Word32.<< (offset, 0wx0002)))), 
             rf, mem)
          
        | PerformJType ((JR, offset), (PC, rf, mem)) 
          = (RF.LoadRegister(rf,
                             Word32.toInt(Word32.andb (Word32.>> (offset,
                                                                  0wx0015),
                                                       0wx0000001F : 
                                                       Word32.word))),
             rf, mem)
          
        | PerformJType ((JAL, offset), (PC, rf, mem)) 
          = (Word32.fromInt (Int.+ (Word32.toIntX PC,
                                           Word32.toIntX 
                                           (Word32.<< (offset, 0wx0002)))),
             RF.StoreRegister(rf, 31, PC), 
             mem)
          
        | PerformJType ((JALR, offset), (PC, rf, mem)) 
          = (RF.LoadRegister(rf,
                             Word32.toInt (Word32.andb (Word32.>> (offset,
                                                                   0wx0015),
                                                        0wx0000001F : 
                                                        Word32.word))),
             RF.StoreRegister(rf, 31, PC), 
             mem)
          
        | PerformJType ((TRAP, 0wx00000003 : Word32.word), (PC, rf, mem)) 
          = let
              val x = TextIO.print "Value? ";
              val s = "10" (* TextIO.inputLine TextIO.stdIn; *)
              val i = Int.fromString s;
              val input = if isSome i
                            then valOf i
                            else (TextIO.print "Error : Returning 0\n";
                                  Int.fromInt 0);
            in
              (PC,
               RF.StoreRegister(rf, 14, Word32.fromInt input),
               mem)
            end
        
        | PerformJType ((TRAP, 0wx00000004 : Word32.word), (PC, rf, mem)) 
          = let
              val output =  Int.toString (Word32.toIntX 
                                               (RF.LoadRegister(rf, 14)));

            in
              (TextIO.print ("Output: " ^ output ^ "\n");
               (PC, rf, mem))
            end
        
        | PerformJType ((_, offset), (PC, rf, mem)) 
          = (print "Error : Non J-Type opcode, performing NOP\n";
             (PC, rf, mem));
            
            
      (*
       * The function PerformInstr performs an instruction by
       * passing the instruction to the appropriate auxiliary function.
       *)
      fun PerformInstr (ITYPE instr, (PC, rf, mem))
          = PerformIType (instr, (PC, rf, mem))
        | PerformInstr (RTYPE instr, (PC, rf, mem))
          = PerformRType (instr, (PC, rf, mem))
        | PerformInstr (JTYPE instr, (PC, rf, mem))
          = PerformJType (instr, (PC, rf, mem))
        | PerformInstr (ILLEGAL, (PC, rf, mem))
          = (PC, rf, mem);

            
      (*
       * The function CycleLoop represents the basic clock cylce of
       * the DLX processor.  It takes as input the current program
       * counter, the current register file, and the current memory.
       * It loads, decodes, and executes an instruction and increments
       * the program counter.  If the instruction that was loaded is
       * the HALT instruction, the program terminates, otherwise,
       * CycleLoop is recursively called with the result of performing
       * the instruction.
       *)
      fun CycleLoop (PC, rf, mem)
          = let
              val (nmem, instr_word) = MEM.LoadWord (mem, PC);
              val instr = DecodeInstr instr_word;
              val nPC = Word32.+ (PC, 0wx00000004 : Word32.word);
            in
              if instr = HALT orelse instr = ILLEGAL
                then (print "Program halted.\n"; 
                      print (MEM.GetStatistics (nmem));
                      ())
                else CycleLoop (PerformInstr (instr, (nPC, rf, nmem)))
            end

        
      (*
       * The function LoadProgAux is an auxilary function that
       * assists in loading a program into memory.  It recursively
       * calls itself, each time loading an instruction and incrementing
       * the address to which the next instruction is to be loaded.
       *)
      fun LoadProgAux ([], mem, address)
          = mem
        | LoadProgAux (instrs::instr_list, mem, address)
          = let
              val instro = Word32.fromString instrs;
              val instr = if isSome instro
                            then valOf instro
                            else (print ("Error : Invalid " ^ 
                                         "instruction format, " ^
                                         "returning NOP\n");
                                  0wx00000000 : Word32.word);
            in
              LoadProgAux (instr_list,
                           MEM.StoreWord (mem, address, instr),
                           Word32.+ (address, 0wx00000004 : Word32.word))
            end;

      (*
       * The function LoadProg takes a list of instructions and memory, and
       * loads the file into memory, beginning at 0x10000.
       *)
      fun LoadProg (instr_list, mem)
          = LoadProgAux (instr_list, mem, 0wx00010000 : Word32.word);


      (*
       * The function ReadFileToInstr reads the sequence of
       * instructions in a file into a list.
       *)
      fun ReadFileToInstr file
         = (case TextIO.inputLine file of
               NONE => []
             | SOME l => l :: (ReadFileToInstr file));


      (*
       * The function run_prog is exported by DLXSimulator.
       * It takes a list of instructions, then begins
       * execution of the instructions loaded at 0x10000, with an
       * initialized register file, and the loaded program in an
       * initialised memory.
       *)
      fun run_prog instructions
          = CycleLoop (0wx00010000 : Word32.word,
                       RF.InitRegisterFile (),
                       LoadProg (instructions, MEM.InitMemory ()));

      (*
       * The function run_file is exported by DLXSimulator.
       * It takes the name of a file to be run, then begins
       * execution of the loaded program at 0x10000, with an
       * initialized register file, and the loaded program in an
       * initialized memory.
       *)
      fun run_file filename 
          = (run_prog o ReadFileToInstr) (TextIO.openIn filename);

    end;




(* ************************************************************************* *)

(*
 * Cache1.sml
 *
 * This file describes a small simple level 1 cache.
 *)

structure L1CacheSpec1 : CACHESPEC 
  = struct

      datatype WriteHitOption = Write_Through
                              | Write_Back;

      datatype WriteMissOption = Write_Allocate
                               | Write_No_Allocate;
                                 
      val CacheName = "Level 1 Cache";
      val CacheSize = 256;
      val BlockSize = 4;
      val Associativity = 2;
      val WriteHit = Write_Through;
      val WriteMiss = Write_No_Allocate;

    end;


structure L1Cache1 : MEMORY
  = CachedMemory (structure CS = L1CacheSpec1;
                  structure MEM = Memory; );


structure DLXSimulatorC1 : DLXSIMULATOR
  = DLXSimulatorFun (structure RF = RegisterFile;
                     structure ALU = ALU;
                     structure MEM = L1Cache1; );

(* Example programs *)

val Simple = ["200E002F", 
              "44000004", 
              "44000000"];

val Twos = ["44000003",
            "00000000",
            "3D00FFFF",
            "3508FFFF",
            "010E7026",
            "25CE0001",
            "44000004",
            "00000000",
            "44000000",
            "00000000"];


val Abs = ["44000003",
           "00000000",
           "01C0402A",
           "11000002",
           "00000000",
           "000E7022",
           "44000004",
           "00000000",
           "44000000",
           "00000000"]

val Fact = ["0C000002",
            "00000000",
            "44000000",
            "44000003",
            "000E2020",
            "2FBD0020",
            "AFBF0014",
            "AFBE0010",
            "27BE0020",
            "0C000009",
            "00000000",
            "8FBE0010",
            "8FBF0014",
            "27BD0020",
            "00027020",
            "44000004",
            "00001020",
            "4BE00000",
            "00000000",
            "20080001",
            "0088402C",
            "11000004",
            "00000000",
            "20020001",
            "08000016",
            "00000000",
            "2FBD0004",
            "AFA40000",
            "28840001",
            "2FBD0020",
            "AFBF0014",
            "AFBE0010",
            "27BE0020",
            "0FFFFFF1",
            "00000000",
            "8FBE0010",
            "8FBF0014",
            "27BD0020",
            "8FA40000",
            "27BD0004",
            "00004020",
            "10800005",
            "00000000",
            "01024020",
            "28840001",
            "0BFFFFFB",
            "00000000",
            "01001020",
            "4BE00000",
            "00000000"];

val GCD = ["0C000002",
           "00000000",
           "44000000",
           "44000003",
           "00000000",
           "000E2020",
           "0080402A",
           "11000002",
           "00000000",
           "00042022",
           "44000003",
           "00000000",
           "000E2820",
           "00A0402A",
           "11000002",
           "00000000",
           "00052822",
           "2FBD0020",
           "AFBF0014",
           "AFBE0010",
           "27BE0020",
           "0C00000A",
           "00000000",
           "8FBE0010",
           "8FBF0014",
           "27BD0020",
           "00027020",
           "44000004",
           "00000000",
           "00001020",
           "4BE00000",
           "00000000",
           "14A00004",
           "00000000",
           "00801020",
           "08000013",
           "00000000",
           "0085402C",
           "15000006",
           "00000000",
           "00804020",
           "00A02020",
           "01002820",
           "08000002",
           "00000000",
           "00A42822",
           "2FBD0020",
           "AFBF0014",
           "AFBE0010",
           "27BE0020",
           "0FFFFFED",
           "00000000",
           "8FBE0010",
           "8FBF0014",
           "27BD0020",
           "4BE00000",
           "00000000"];

(*
val _ = DLXSimulatorC1.run_prog GCD
*)

structure Main =
   struct
      fun doit () =
         (DLXSimulatorC1.run_prog Simple
          ; DLXSimulatorC1.run_prog Twos
          ; DLXSimulatorC1.run_prog Abs
          ; DLXSimulatorC1.run_prog Fact
          ; DLXSimulatorC1.run_prog GCD
          )

      val doit =
         fn size =>
         let
            fun loop n =
               if n = 0
                  then ()
               else (doit();
                     loop(n-1))
         in loop size
         end
   end