| 1 |
(* More tweaks by Matthew Fluet ([email protected]) on 2017-12-06 to |
| 2 |
* generalize implementation of I/O instructions and only output the |
| 3 |
* last run of each DLX program in the benchmark. |
| 4 |
*) |
| 5 |
(* Minor tweaks by Stephen Weeks ([email protected]) on 2001-07-17 to turn into a |
| 6 |
* benchmark. |
| 7 |
* Added rand function. |
| 8 |
*) |
| 9 |
(* |
| 10 |
* Matthew Thomas Fluet |
| 11 |
* Harvey Mudd College |
| 12 |
* Claremont, CA 91711 |
| 13 |
* e-mail: [email protected] |
| 14 |
* |
| 15 |
* A DLX Simulator in Standard ML |
| 16 |
* |
| 17 |
* Description: |
| 18 |
* The DLX Simulator is a partial implementation of the RISC instruction |
| 19 |
* set described in Patterson's and Hennessy's _Computer Architecture_. |
| 20 |
* Currently, the DLX Simulator implements the following instructions: |
| 21 |
* ADD ADDI |
| 22 |
* ADDU ADDUI |
| 23 |
* SUB SUBI |
| 24 |
* SUBU SUBUI |
| 25 |
* AND ANDI |
| 26 |
* OR ORI |
| 27 |
* XOR XORI |
| 28 |
* |
| 29 |
* LHI |
| 30 |
* |
| 31 |
* SLL SLLI |
| 32 |
* SRL SRLI |
| 33 |
* SRA SRAI |
| 34 |
* |
| 35 |
* SEQ SEQI |
| 36 |
* SNE SNEI |
| 37 |
* SLT SLTI |
| 38 |
* SGT SGTI |
| 39 |
* SLE SLEI |
| 40 |
* SGE SGEI |
| 41 |
* |
| 42 |
* LB LBU SB |
| 43 |
* LH LHU SH |
| 44 |
* LW SW |
| 45 |
* |
| 46 |
* BEQZ BNEZ |
| 47 |
* J JR |
| 48 |
* JAL JALR |
| 49 |
* |
| 50 |
* TRAP |
| 51 |
* |
| 52 |
* NOP |
| 53 |
* |
| 54 |
* Currently, the DLX Simulator uses 32 bit words for addressing and |
| 55 |
* the register file and a 65535 word memory. To augment the memory |
| 56 |
* a cache can be installed in the simulator, with a number of different |
| 57 |
* caching options that can be made. Caches can also cache other caches, |
| 58 |
* so realistic dual level caches can be simulated. Input and output |
| 59 |
* is limited to requesting and outputing signed integers. |
| 60 |
* |
| 61 |
* Usage: |
| 62 |
* DLXSimulatorCX.run_file : string -> unit |
| 63 |
* DLXSimulatorCX.run_prog : string list -> unit; |
| 64 |
* The DLXSimualatorCX family of structures represent different caches |
| 65 |
* used on the simulator. The following table describes the different |
| 66 |
* caches used: |
| 67 |
* C1: a small level 1 cache |
| 68 |
* DLXSimulatorCX.run_file attempts to open and execute the instructions |
| 69 |
* in a file. DLXSimulatorCX.run_prog runs a set of instructions as |
| 70 |
* a list of strings. Four programs are included here. |
| 71 |
* Simple : simply outputs the number 42. |
| 72 |
* Twos: performs the twos complement on an inputed number. |
| 73 |
* Abs: performs the absolute value on an imputed number. |
| 74 |
* Fact: performs the factorial on an inputed number. |
| 75 |
* GCD: performs the greatest common divisor on two imputed numbers. |
| 76 |
* After running, the DLX Simulator outputs a set of statistics |
| 77 |
* concerning memory reads and writes, and cache hits and misses. |
| 78 |
* |
| 79 |
* Future Work: |
| 80 |
* With the implementation of the PACK_REAL structures |
| 81 |
* as documented in the SML'97 Basis Library, the remainder |
| 82 |
* of the DLX instruction set should be implemented. |
| 83 |
* Currently, without an efficient and correct means of |
| 84 |
* converting a 32 bit word into a 32 bit float, it is |
| 85 |
* difficult to incorporate these instructions. |
| 86 |
* In order to finish following the current development |
| 87 |
* model, a FPALU structure should be implemented as the |
| 88 |
* floating point arithmetic-logic unit. |
| 89 |
* Another possibility for future work would be to |
| 90 |
* model a pipelined processor. Currently, the DLX Simulator |
| 91 |
* uses a simple one cycle per instruction model. |
| 92 |
* It should be possible to break this model and implement |
| 93 |
* a pipeline, but it would mean a major reworking of the |
| 94 |
* DLXSimulatorFun functor. |
| 95 |
* |
| 96 |
* References: |
| 97 |
* Patterson, David A. and John L. Hennessy. _Computer Architecture: A |
| 98 |
* Quantitative Approach: Second Edition_. San Francisco: Morgan |
| 99 |
* Kaufmann Publishers, Inc., 1996. |
| 100 |
* |
| 101 |
*) |
| 102 |
|
| 103 |
(* ************************************************************************* *) |
| 104 |
|
| 105 |
(* sweeks added rand *) |
| 106 |
local |
| 107 |
open Word |
| 108 |
val seed: word ref = ref 0w13 |
| 109 |
in |
| 110 |
(* From page 284 of Numerical Recipes in C. *) |
| 111 |
fun rand (): word = |
| 112 |
let |
| 113 |
val res = 0w1664525 * !seed + 0w1013904223 |
| 114 |
val _ = seed := res |
| 115 |
in |
| 116 |
res |
| 117 |
end |
| 118 |
end |
| 119 |
|
| 120 |
(* |
| 121 |
* ImmArray.sml |
| 122 |
* |
| 123 |
* The ImmArray structure defines an immutable array implementation. |
| 124 |
* An immarray is stored internally as a list. |
| 125 |
* This results in O(n) sub and update functions, as opposed |
| 126 |
* to O(1) sub and update functions found in Array. However, |
| 127 |
* immutable arrays are truly immutable, and can be integrated |
| 128 |
* with a functionally programming style easier than mutable |
| 129 |
* arrays. |
| 130 |
* |
| 131 |
* The ImmArray structure mimics the Array structure as much as possible. |
| 132 |
* The most obvious deviation is that unit return types in Array are replaced |
| 133 |
* by 'a immarray return types in ImmArray. Unlike an 'a array, an 'a immarray |
| 134 |
* is an equality type if and only if 'a is an equality type. Further immarray |
| 135 |
* equality is structural, rather than the "creation" equality used by Array. |
| 136 |
* Additionally, no vector type is supported, and consequently no copyVec |
| 137 |
* function is supported. Finally, the functions mapi and map provide |
| 138 |
* similar functionality as modifyi and modify, but relax the constraint that |
| 139 |
* the argument function need be of type 'a -> 'a. |
| 140 |
* |
| 141 |
* Future Work : There are random-access list implementations |
| 142 |
* that support O(log n) sub and update functions, |
| 143 |
* which may provide a faster implementation, although |
| 144 |
* possibly at the expense of space and the ease of |
| 145 |
* implementing app, foldl, foldr, modify, and map functions. |
| 146 |
*) |
| 147 |
|
| 148 |
signature IMMARRAY |
| 149 |
= sig |
| 150 |
type 'a immarray; |
| 151 |
|
| 152 |
val maxLen : int; |
| 153 |
val immarray : (int * 'a) -> 'a immarray; |
| 154 |
val fromList : 'a list -> 'a immarray; |
| 155 |
val toList : 'a immarray -> 'a list; |
| 156 |
|
| 157 |
val tabulate : int * (int -> 'a) -> 'a immarray; |
| 158 |
val length : 'a immarray -> int; |
| 159 |
|
| 160 |
val sub : 'a immarray * int -> 'a; |
| 161 |
val update : 'a immarray * int * 'a -> 'a immarray; |
| 162 |
val extract : 'a immarray * int * int option -> 'a immarray; |
| 163 |
|
| 164 |
val copy : {src : 'a immarray, si : int, len : int option, |
| 165 |
dst : 'a immarray, di : int} -> 'a immarray; |
| 166 |
|
| 167 |
val appi : (int * 'a -> unit) -> ('a immarray * int * int option) |
| 168 |
-> unit; |
| 169 |
val app : ('a -> unit) -> 'a immarray -> unit; |
| 170 |
val foldli : ((int * 'a * 'b) -> 'b) -> 'b |
| 171 |
-> ('a immarray * int * int option) -> 'b; |
| 172 |
val foldri : ((int * 'a * 'b) -> 'b) -> 'b |
| 173 |
-> ('a immarray * int * int option) -> 'b; |
| 174 |
val foldl : (('a * 'b) -> 'b) -> 'b -> 'a immarray -> 'b; |
| 175 |
val foldr : (('a * 'b) -> 'b) -> 'b -> 'a immarray -> 'b; |
| 176 |
val mapi : ((int * 'a) -> 'b) -> ('a immarray * int * int option) |
| 177 |
-> 'b immarray; |
| 178 |
val map : ('a -> 'b) -> 'a immarray -> 'b immarray; |
| 179 |
val modifyi : ((int * 'a) -> 'a) -> ('a immarray * int * int option) |
| 180 |
-> 'a immarray; |
| 181 |
val modify : ('a -> 'a) -> 'a immarray -> 'a immarray; |
| 182 |
end; |
| 183 |
|
| 184 |
|
| 185 |
structure ImmArray : IMMARRAY |
| 186 |
= struct |
| 187 |
|
| 188 |
(* datatype 'a immarray |
| 189 |
* An immarray is stored internally as a list. |
| 190 |
* The use of a constructor prevents list functions from |
| 191 |
* treating immarray type as a list. |
| 192 |
*) |
| 193 |
datatype 'a immarray = IA of 'a list; |
| 194 |
|
| 195 |
(* val maxLen : int |
| 196 |
* The maximum length of immarrays supported. |
| 197 |
* Technically, under this implementation, the maximum length |
| 198 |
* of immarrays is the same as the maximum length of a list, |
| 199 |
* but for convience and compatibility, use the Array structure's |
| 200 |
* maximum length. |
| 201 |
*) |
| 202 |
val maxLen = Array.maxLen; |
| 203 |
|
| 204 |
(* val tabulate : int * (int -> 'a) -> 'a immarray |
| 205 |
* val immarray : int * 'a -> 'a immarray |
| 206 |
* val fromList : 'a list -> 'a immarray |
| 207 |
* val toList : 'a immarray -> 'a list |
| 208 |
* val length : 'a immarray -> int |
| 209 |
* These functions perform basic immarray functions. |
| 210 |
* The tabulate, immarray, and fromList functions create an immarray. |
| 211 |
* The toList function converts an immarray to a list. |
| 212 |
* The length function returns the length of an immarray. |
| 213 |
*) |
| 214 |
fun tabulate (n, initfn) = IA (List.tabulate (n, initfn)); |
| 215 |
fun immarray (n, init) = tabulate (n, fn _ => init); |
| 216 |
fun fromList l = IA l; |
| 217 |
fun toList (IA ia) = ia; |
| 218 |
fun length (IA ia) = List.length ia; |
| 219 |
|
| 220 |
(* val sub : 'a immarray * int -> 'a |
| 221 |
* val update : 'a immarray * int * 'a -> 'a immarray |
| 222 |
* These functions sub and update an immarray by index. |
| 223 |
*) |
| 224 |
fun sub (IA ia, i) = List.nth (ia, i); |
| 225 |
fun update (IA ia, i, x) = IA ((List.take (ia, i)) @ |
| 226 |
(x::(List.drop (ia, i + 1)))); |
| 227 |
|
| 228 |
(* val extract : 'a immarray * int * int option -> 'a immarray |
| 229 |
* This function extracts an immarray slice from an immarray from |
| 230 |
* one index either through the rest of the immarray (NONE) |
| 231 |
* or for n elements (SOME n), as described in the |
| 232 |
* Standard ML Basis Library. |
| 233 |
*) |
| 234 |
fun extract (IA ia, i, NONE) = IA (List.drop (ia, i)) |
| 235 |
| extract (IA ia, i, SOME n) = IA (List.take (List.drop (ia, i), n)); |
| 236 |
|
| 237 |
(* val copy : {src : 'a immarray, si : int, len : int option, |
| 238 |
dst : 'a immarray, di : int} -> 'a immarray |
| 239 |
* This function copies an immarray slice from src into dst starting |
| 240 |
* at the di element. |
| 241 |
*) |
| 242 |
fun copy {src, si, len, dst=IA ia, di} |
| 243 |
= let |
| 244 |
val IA sia = extract (src, si, len); |
| 245 |
val pre = List.take (ia, di); |
| 246 |
val post = case len |
| 247 |
of NONE => List.drop (ia, di+(List.length sia)) |
| 248 |
| SOME n => List.drop (ia, di+n); |
| 249 |
in |
| 250 |
IA (pre @ sia @ post) |
| 251 |
end; |
| 252 |
|
| 253 |
(* val appi : ('a * int -> unit) -> ('a immarray * int * int option) |
| 254 |
* -> unit |
| 255 |
* val app : ('a -> unit) -> 'a immarray -> unit |
| 256 |
* These functions apply a function to every element |
| 257 |
* of an immarray. The appi function also provides the |
| 258 |
* index of the element as an argument to the applied function |
| 259 |
* and uses an immarray slice argument. |
| 260 |
*) |
| 261 |
local |
| 262 |
fun appi_aux f i [] = () |
| 263 |
| appi_aux f i (h::t) = (f(i,h); appi_aux f (i + 1) t); |
| 264 |
in |
| 265 |
fun appi f (IA ia, i, len) = let |
| 266 |
val IA sia = extract (IA ia, i, len); |
| 267 |
in |
| 268 |
appi_aux f i sia |
| 269 |
end; |
| 270 |
end; |
| 271 |
fun app f immarr = appi (f o #2) (immarr, 0, NONE); |
| 272 |
|
| 273 |
(* val foldli : (int * 'a * 'b -> 'b) -> 'b |
| 274 |
* -> ('a immarray * int * int option) -> 'b; |
| 275 |
* val foldri : (int * 'a * 'b -> 'b) -> 'b |
| 276 |
* -> ('a immarray * int * int option) -> 'b; |
| 277 |
* val foldl : ('a * 'b -> 'b) -> 'b -> 'a immarray -> 'b |
| 278 |
* val foldr : ('a * 'b -> 'b) -> 'b -> 'a immarray -> 'b |
| 279 |
* These functions fold a function over every element |
| 280 |
* of an immarray. The foldri and foldli functions also provide |
| 281 |
* the index of the element as an argument to the folded function |
| 282 |
* and uses an immarray slice argument. |
| 283 |
*) |
| 284 |
local |
| 285 |
fun foldli_aux f b i [] = b |
| 286 |
| foldli_aux f b i (h::t) = foldli_aux f (f(i,h,b)) (i+1) t; |
| 287 |
fun foldri_aux f b i [] = b |
| 288 |
| foldri_aux f b i (h::t) = f(i,h,foldri_aux f b (i+1) t); |
| 289 |
in |
| 290 |
fun foldli f b (IA ia, i, len) |
| 291 |
= let |
| 292 |
val IA ia2 = extract (IA ia, i, len); |
| 293 |
in |
| 294 |
foldli_aux f b i ia2 |
| 295 |
end; |
| 296 |
fun foldri f b (IA ia, i, len) |
| 297 |
= let |
| 298 |
val IA ia2 = extract (IA ia, i, len); |
| 299 |
in |
| 300 |
foldri_aux f b i ia2 |
| 301 |
end; |
| 302 |
end; |
| 303 |
fun foldl f b (IA ia) = foldli (fn (_,i,x) => f(i,x)) b (IA ia, 0, NONE); |
| 304 |
fun foldr f b (IA ia) = foldri (fn (_,i,x) => f(i,x)) b (IA ia, 0, NONE); |
| 305 |
|
| 306 |
(* val mapi : ('a * int -> 'b) -> 'a immarray -> 'b immarray |
| 307 |
* val map : ('a -> 'b) -> 'a immarray -> 'b immarray |
| 308 |
* These functions map a function over every element |
| 309 |
* of an immarray. The mapi function also provides the |
| 310 |
* index of the element as an argument to the mapped function |
| 311 |
* and uses an immarray slice argument. Although there are |
| 312 |
* similarities between mapi and modifyi, note that when mapi is |
| 313 |
* used with an immarray slice, the resulting immarray is the |
| 314 |
* same size as the slice. This is necessary to preserve the |
| 315 |
* type of the resulting immarray. Thus, mapi with the identity |
| 316 |
* function reduces to the extract function. |
| 317 |
*) |
| 318 |
local |
| 319 |
fun mapi_aux f i [] = [] |
| 320 |
| mapi_aux f i (h::t) = (f (i,h))::(mapi_aux f (i + 1) t); |
| 321 |
in |
| 322 |
fun mapi f (IA ia, i, len) = let |
| 323 |
val IA ia2 = extract (IA ia, i, len); |
| 324 |
in |
| 325 |
IA (mapi_aux f i ia2) |
| 326 |
end; |
| 327 |
end; |
| 328 |
fun map f (IA ia)= mapi (f o #2) (IA ia, 0, NONE); |
| 329 |
|
| 330 |
(* val modifyi : (int * 'a -> 'a) -> ('a immarray * int * int option) |
| 331 |
* -> 'a immarray |
| 332 |
* val modify : ('a -> 'a) -> 'a immarray -> 'a immarray |
| 333 |
* These functions apply a function to every element of an immarray |
| 334 |
* in left to right order and returns a new immarray where corresponding |
| 335 |
* elements are replaced by their modified values. The modifyi |
| 336 |
* function also provides the index of the element as an argument |
| 337 |
* to the mapped function and uses an immarray slice argument. |
| 338 |
*) |
| 339 |
local |
| 340 |
fun modifyi_aux f i [] = [] |
| 341 |
| modifyi_aux f i (h::t) = (f (i,h))::(modifyi_aux f (i + 1) t); |
| 342 |
in |
| 343 |
fun modifyi f (IA ia, i, len) |
| 344 |
= let |
| 345 |
val pre = List.take (ia, i); |
| 346 |
val IA ia2 = extract (IA ia, i, len); |
| 347 |
val post = case len |
| 348 |
of NONE => [] |
| 349 |
| SOME n => List.drop (ia, i+n); |
| 350 |
in |
| 351 |
IA (pre @ (modifyi_aux f i ia2) @ post) |
| 352 |
end; |
| 353 |
end; |
| 354 |
fun modify f (IA ia) = modifyi (f o #2) (IA ia, 0, NONE); |
| 355 |
|
| 356 |
end; |
| 357 |
|
| 358 |
(* ************************************************************************* *) |
| 359 |
|
| 360 |
(* |
| 361 |
* ImmArray2.sml |
| 362 |
* |
| 363 |
* The ImmArray2 structure defines a two dimensional immutable array |
| 364 |
* implementation. An immarray2 is stored internally as an immutable |
| 365 |
* array of immutable arrays. As such, the ImmArray2 makes heavy use |
| 366 |
* of the ImmArray structure. |
| 367 |
* |
| 368 |
* The ImmArray2 structure mimics the Array2 structure as much as possible. |
| 369 |
* The most obvious deviation is that unit return types in Array2 are replaced |
| 370 |
* by 'a immarray2 return types in ImmArray2. Unlike an 'a array, |
| 371 |
* an 'a immarray2 is an equality type if and only if 'a is an equality type. |
| 372 |
* Further immarray2 equality is structural, rather than the "creation" |
| 373 |
* equality used by Array2. Also, the 'a region type is not included in |
| 374 |
* ImmArray2, but all functions in Array2 that require 'a regions are present |
| 375 |
* with arguments taken in the natural order. Finally, the functions mapi |
| 376 |
* and map provide similar functionality as modifyi and modify, but relax |
| 377 |
* the constraint that the argument function need be of type 'a -> 'a. |
| 378 |
*) |
| 379 |
|
| 380 |
signature IMMARRAY2 |
| 381 |
= sig |
| 382 |
|
| 383 |
type 'a immarray2; |
| 384 |
|
| 385 |
datatype traversal = RowMajor | ColMajor |
| 386 |
|
| 387 |
val immarray2 : int * int * 'a -> 'a immarray2; |
| 388 |
val tabulate : traversal -> int * int * ((int * int) -> 'a) |
| 389 |
-> 'a immarray2; |
| 390 |
val fromList : 'a list list -> 'a immarray2; |
| 391 |
val dimensions : 'a immarray2 -> int * int; |
| 392 |
|
| 393 |
val sub : 'a immarray2 * int * int -> 'a; |
| 394 |
val update : 'a immarray2 * int * int * 'a -> 'a immarray2; |
| 395 |
val extract : 'a immarray2 * int * int * int option * int option |
| 396 |
-> 'a immarray2; |
| 397 |
|
| 398 |
val copy : {src : 'a immarray2, si : int, sj : int, |
| 399 |
ilen : int option, jlen : int option, |
| 400 |
dst : 'a immarray2, di : int, dj : int} -> 'a immarray2; |
| 401 |
|
| 402 |
val nRows : 'a immarray2 -> int; |
| 403 |
val nCols : 'a immarray2 -> int; |
| 404 |
val row : 'a immarray2 * int -> 'a ImmArray.immarray; |
| 405 |
val column : 'a immarray2 * int -> 'a ImmArray.immarray; |
| 406 |
|
| 407 |
val appi : traversal -> (int * int * 'a -> unit) |
| 408 |
-> ('a immarray2 * int * int * int option * int option) |
| 409 |
-> unit; |
| 410 |
val app : traversal -> ('a -> unit) -> 'a immarray2 -> unit; |
| 411 |
val foldli : traversal -> ((int * int * 'a * 'b) -> 'b) -> 'b |
| 412 |
-> ('a immarray2 * int * int * int option * int option) |
| 413 |
-> 'b |
| 414 |
val foldri : traversal -> ((int * int * 'a * 'b) -> 'b) -> 'b |
| 415 |
-> ('a immarray2 * int * int * int option * int option) |
| 416 |
-> 'b |
| 417 |
val foldl : traversal -> (('a * 'b) -> 'b) -> 'b -> 'a immarray2 -> 'b |
| 418 |
val foldr : traversal -> (('a * 'b) -> 'b) -> 'b -> 'a immarray2 -> 'b |
| 419 |
val mapi : traversal -> (int * int * 'a -> 'b) |
| 420 |
-> ('a immarray2 * int * int * int option * int option) |
| 421 |
-> 'b immarray2; |
| 422 |
val map : traversal -> ('a -> 'b) -> 'a immarray2 -> 'b immarray2; |
| 423 |
val modifyi : traversal -> ((int * int * 'a) -> 'a) |
| 424 |
-> ('a immarray2 * int * int * int option * int option) |
| 425 |
-> 'a immarray2; |
| 426 |
val modify : traversal -> ('a -> 'a) -> 'a immarray2 -> 'a immarray2; |
| 427 |
end; |
| 428 |
|
| 429 |
structure ImmArray2 : IMMARRAY2 |
| 430 |
= struct |
| 431 |
|
| 432 |
(* datatype 'a immarray2 |
| 433 |
* An immarray2 is stored internally as an immutable array |
| 434 |
* of immutable arrays. The use of a contructor prevents ImmArray |
| 435 |
* functions from treating the immarray2 type as an immarray. |
| 436 |
*) |
| 437 |
datatype 'a immarray2 = IA2 of 'a ImmArray.immarray ImmArray.immarray; |
| 438 |
datatype traversal = RowMajor | ColMajor |
| 439 |
|
| 440 |
(* val tabulate : traversal -> int * int * (int * int -> 'a) |
| 441 |
* -> 'a immarray2 |
| 442 |
* val immarray2 : int * int * 'a -> 'a immarray2 |
| 443 |
* val fromList : 'a list list -> 'a immarray2 |
| 444 |
* val dmensions : 'a immarray2 -> int * int |
| 445 |
* These functions perform basic immarray2 functions. |
| 446 |
* The tabulate and immarray2 functions create an immarray2. |
| 447 |
* The fromList function converts a list of lists into an immarray2. |
| 448 |
* Unlike Array2.fromList, fromList will accept lists of different |
| 449 |
* lengths, allowing one to create an immarray2 in which the |
| 450 |
* rows have different numbers of columns, although it is likely that |
| 451 |
* exceptions will be raised when other ImmArray2 functions are applied |
| 452 |
* to such an immarray2. Note that dimensions will return the |
| 453 |
* number of columns in row 0. |
| 454 |
* The dimensions function returns the dimensions of an immarray2. |
| 455 |
*) |
| 456 |
fun tabulate RowMajor (r, c, initfn) |
| 457 |
= let |
| 458 |
fun initrow r = ImmArray.tabulate (c, fn ic => initfn (r,ic)); |
| 459 |
in |
| 460 |
IA2 (ImmArray.tabulate (r, fn ir => initrow ir)) |
| 461 |
end |
| 462 |
| tabulate ColMajor (r, c, initfn) |
| 463 |
= turn (tabulate RowMajor (c,r, fn (c,r) => initfn(r,c))) |
| 464 |
and immarray2 (r, c, init) = tabulate RowMajor (r, c, fn (_, _) => init) |
| 465 |
and fromList l |
| 466 |
= IA2 (ImmArray.tabulate (length l, |
| 467 |
fn ir => ImmArray.fromList (List.nth(l,ir)))) |
| 468 |
and dimensions (IA2 ia2) = (ImmArray.length ia2, |
| 469 |
ImmArray.length (ImmArray.sub (ia2, 0))) |
| 470 |
|
| 471 |
(* turn : 'a immarray2 -> 'a immarray2 |
| 472 |
* This function reverses the rows and columns of an immarray2 |
| 473 |
* to allow handling of ColMajor traversals. |
| 474 |
*) |
| 475 |
and turn ia2 = let |
| 476 |
val (r,c) = dimensions ia2; |
| 477 |
in |
| 478 |
tabulate RowMajor (c,r,fn (cc,rr) => sub (ia2,rr,cc)) |
| 479 |
end |
| 480 |
|
| 481 |
(* val sub : 'a immarray2 * int * int -> 'a |
| 482 |
* val update : 'a immarray2 * int * int * 'a -> 'a immarray2 |
| 483 |
* These functions sub and update an immarray2 by indices. |
| 484 |
*) |
| 485 |
and sub (IA2 ia2, r, c) = ImmArray.sub(ImmArray.sub (ia2, r), c); |
| 486 |
fun update (IA2 ia2, r, c, x) |
| 487 |
= IA2 (ImmArray.update (ia2, r, |
| 488 |
ImmArray.update (ImmArray.sub (ia2, r), |
| 489 |
c, x))); |
| 490 |
|
| 491 |
(* val extract : 'a immarray2 * int * int * |
| 492 |
* int option * int option -> 'a immarray2 |
| 493 |
* This function extracts a subarray from an immarray2 from |
| 494 |
* one pair of indices either through the rest of the |
| 495 |
* immarray2 (NONE, NONE) or for the specfied number of elements. |
| 496 |
*) |
| 497 |
fun extract (IA2 ia2, i, j, rlen, clen) |
| 498 |
= IA2 (ImmArray.map (fn ia => ImmArray.extract (ia, j, clen)) |
| 499 |
(ImmArray.extract (ia2, i, rlen))); |
| 500 |
|
| 501 |
(* val nRows : 'a immarray2 -> int |
| 502 |
* val nCols : 'a immarray2 -> int |
| 503 |
* These functions return specific dimensions of an immarray2. |
| 504 |
*) |
| 505 |
fun nRows (IA2 ia2) = (#1 o dimensions) (IA2 ia2); |
| 506 |
fun nCols (IA2 ia2) = (#2 o dimensions) (IA2 ia2); |
| 507 |
(* val row : immarray2 * int -> ImmArray.immarray |
| 508 |
* val column : immarray2 * int -> ImmArray.immarray |
| 509 |
* These functions extract an entire row or column from |
| 510 |
* an immarray2 by index, returning the row or column as |
| 511 |
* an ImmArray.immarray. |
| 512 |
*) |
| 513 |
fun row (ia2, r) = let |
| 514 |
val (c, _) = dimensions ia2; |
| 515 |
in |
| 516 |
ImmArray.tabulate (c, fn i => sub (ia2, r, i)) |
| 517 |
end; |
| 518 |
fun column (ia2, c) = let |
| 519 |
val (_, r) = dimensions ia2; |
| 520 |
in |
| 521 |
ImmArray.tabulate (r, fn i => sub (ia2, i, c)) |
| 522 |
end; |
| 523 |
|
| 524 |
(* val copy : {src : 'a immarray2, si : int, sj : int, |
| 525 |
* ilen : int option, jlen : int option, |
| 526 |
* dst : 'a immarray2, di : int, dj : int}; |
| 527 |
* This function copies an immarray2 slice from src int dst starting |
| 528 |
* at the di,dj element. |
| 529 |
*) |
| 530 |
fun copy {src, si, sj, ilen, jlen, dst=IA2 ia2, di, dj} |
| 531 |
= let |
| 532 |
val nilen = case ilen |
| 533 |
of NONE => SOME ((nRows src) - si) |
| 534 |
| SOME n => SOME n; |
| 535 |
in |
| 536 |
IA2 (ImmArray.modifyi (fn (r, ia) |
| 537 |
=> ImmArray.copy {src=row (src, si+r-di), |
| 538 |
si=sj, len=jlen, |
| 539 |
dst=ia, di=dj}) |
| 540 |
(ia2, di, nilen)) |
| 541 |
end; |
| 542 |
|
| 543 |
(* val appi : traversal -> ('a * int * int -> unit) -> 'a immarray2 |
| 544 |
* -> unit |
| 545 |
* val app : traversal -> ('a -> unit) -> 'a immarray2 -> unit |
| 546 |
* These functions apply a function to every element |
| 547 |
* of an immarray2. The appi function also provides the |
| 548 |
* indices of the element as an argument to the applied function |
| 549 |
* and uses an immarray2 slice argument. |
| 550 |
*) |
| 551 |
fun appi RowMajor f (IA2 ia2, i, j, rlen, clen) |
| 552 |
= ImmArray.appi (fn (r,ia) => ImmArray.appi (fn (c,x) => f(r,c,x)) |
| 553 |
(ia, j, clen)) |
| 554 |
(ia2, i, rlen) |
| 555 |
| appi ColMajor f (ia2, i, j, rlen, clen) |
| 556 |
= appi RowMajor (fn (c,r,x) => f(r,c,x)) (turn ia2, j, i, clen, rlen); |
| 557 |
fun app tr f (IA2 ia2) = appi tr (f o #3) (IA2 ia2, 0, 0, NONE, NONE); |
| 558 |
|
| 559 |
(* val foldli : traversal -> ((int * int * 'a * 'b) -> 'b) -> 'b |
| 560 |
* -> ('a immarray2 * int * int * int option * int option) |
| 561 |
* -> 'b |
| 562 |
* val foldri : traversal -> ((int * int * 'a * 'b) -> 'b) -> 'b |
| 563 |
* -> ('a immarray2 * int * int * int option * int option) |
| 564 |
* -> 'b |
| 565 |
* val foldl : traversal -> ('a * 'b -> 'b) -> 'b -> 'a immarray2 -> 'b |
| 566 |
* val foldr : traversal -> ('a * 'b -> 'b) -> 'b -> 'a immarray2 -> 'b |
| 567 |
* These functions fold a function over every element |
| 568 |
* of an immarray2. The foldri and foldli functions also provide |
| 569 |
* the index of the element as an argument to the folded function |
| 570 |
* and uses an immarray2 slice argument. |
| 571 |
*) |
| 572 |
fun foldli RowMajor f b (IA2 ia2, i, j, rlen, clen) |
| 573 |
= ImmArray.foldli (fn (r,ia,b) |
| 574 |
=> ImmArray.foldli (fn (c,x,b) => f(r,c,x,b)) |
| 575 |
b |
| 576 |
(ia, j, clen)) |
| 577 |
b |
| 578 |
(ia2, i, rlen) |
| 579 |
| foldli ColMajor f b (ia2, i, j, rlen, clen) |
| 580 |
= foldli RowMajor (fn (c,r,x,b) => f(r,c,x,b)) b |
| 581 |
(turn ia2, j, i, clen, rlen); |
| 582 |
fun foldri RowMajor f b (IA2 ia2, i, j, rlen, clen) |
| 583 |
= ImmArray.foldri (fn (r,ia,b) |
| 584 |
=> ImmArray.foldri (fn (c,x,b) => f(r,c,x,b)) |
| 585 |
b |
| 586 |
(ia, j, clen)) |
| 587 |
b |
| 588 |
(ia2, i, rlen) |
| 589 |
| foldri ColMajor f b (ia2, i, j, rlen, clen) |
| 590 |
= foldri RowMajor (fn (c,r,x,b) => f(r,c,x,b)) b |
| 591 |
(turn ia2, j, i, clen, rlen); |
| 592 |
fun foldl tr f b (IA2 ia2) |
| 593 |
= foldli tr (fn (_,_,x,b) => f(x,b)) b (IA2 ia2, 0, 0, NONE, NONE); |
| 594 |
fun foldr tr f b (IA2 ia2) |
| 595 |
= foldri tr (fn (_,_,x,b) => f(x,b)) b (IA2 ia2, 0, 0, NONE, NONE); |
| 596 |
|
| 597 |
(* val mapi : traversal -> ('a * int * int -> 'b) -> 'a immarray2 |
| 598 |
* -> 'b immarray2 |
| 599 |
* val map : traversal -> ('a -> 'b) -> 'a immarray2 -> 'b immarray2 |
| 600 |
* These functions map a function over every element |
| 601 |
* of an immarray2. The mapi function also provides the |
| 602 |
* indices of the element as an argument to the mapped function |
| 603 |
* and uses an immarray2 slice argument. Although there are |
| 604 |
* similarities between mapi and modifyi, note that when mapi is |
| 605 |
* used with an immarray2 slice, the resulting immarray2 is the |
| 606 |
* same size as the slice. This is necessary to preserve the |
| 607 |
* type of the resulting immarray2. Thus, mapi with the identity |
| 608 |
* function reduces to the extract function. |
| 609 |
*) |
| 610 |
fun mapi RowMajor f (IA2 ia2, i, j, rlen, clen) |
| 611 |
= IA2 (ImmArray.mapi (fn (r,ia) => ImmArray.mapi (fn (c,x) => f(r,c,x)) |
| 612 |
(ia, j, clen)) |
| 613 |
(ia2, i, rlen)) |
| 614 |
| mapi ColMajor f (ia2, i, j, rlen, clen) |
| 615 |
= turn (mapi RowMajor (fn (c,r,x) => f(r,c,x)) |
| 616 |
(turn ia2, j, i, clen, rlen)) |
| 617 |
fun map tr f (IA2 ia2) |
| 618 |
= mapi tr (f o #3) (IA2 ia2, 0, 0, NONE, NONE); |
| 619 |
|
| 620 |
(* val modifyi : traversal -> (int * int* 'a -> 'a) |
| 621 |
-> ('a immarray2 * int * int * int option * int option) |
| 622 |
* -> 'a immarray2 |
| 623 |
* val modify : traversal -> ('a -> 'a) -> 'a immarray2 -> 'a immarray2 |
| 624 |
* These functions apply a function to every element of an immarray2 |
| 625 |
* in row by column order and returns a new immarray2 where corresponding |
| 626 |
* elements are replaced by their modified values. The modifyi |
| 627 |
* function also provides the index of the element as an argument |
| 628 |
* to the mapped function and uses an immarray2 slice argument. |
| 629 |
*) |
| 630 |
fun modifyi RowMajor f (IA2 ia2, i, j, rlen, clen) |
| 631 |
= IA2 (ImmArray.modifyi (fn (r,ia) => ImmArray.modifyi (fn (c,x) |
| 632 |
=> f(r,c,x)) |
| 633 |
(ia, j, clen)) |
| 634 |
(ia2, i, rlen)) |
| 635 |
| modifyi ColMajor f (ia2, i, j, rlen, clen) |
| 636 |
= turn (modifyi RowMajor (fn (c,r,x) => f(r,c,x)) |
| 637 |
(turn ia2, j, i, clen, rlen)); |
| 638 |
fun modify tr f (IA2 ia2) |
| 639 |
= modifyi tr (f o #3) (IA2 ia2, 0, 0, NONE, NONE); |
| 640 |
|
| 641 |
end; |
| 642 |
|
| 643 |
(* ************************************************************************* *) |
| 644 |
|
| 645 |
(* |
| 646 |
* RegisterFile.sig |
| 647 |
* |
| 648 |
* This defines the exported datatype and functions provided by the |
| 649 |
* register file. The datatype registerfile provides the encapsulation |
| 650 |
* of the register file, InitRegisterFile initializes the registerfile, |
| 651 |
* setting all registers to zero and setting r0, gp, sp, and fp to |
| 652 |
* their appropriate values, LoadRegister takes a registerfile and |
| 653 |
* an integer corresponding to the register, and returns the |
| 654 |
* Word32.word value at that register, and StoreRegister takes a |
| 655 |
* registerfile, an integer corresponding to the register, and a |
| 656 |
* Word32.word and returns the registerfile updated with the word |
| 657 |
* stored in the appropriate register. |
| 658 |
*) |
| 659 |
|
| 660 |
signature REGISTERFILE |
| 661 |
= sig |
| 662 |
|
| 663 |
type registerfile; |
| 664 |
|
| 665 |
val InitRegisterFile : unit -> registerfile; |
| 666 |
|
| 667 |
val LoadRegister : registerfile * int -> Word32.word; |
| 668 |
|
| 669 |
val StoreRegister : registerfile * int * Word32.word -> registerfile; |
| 670 |
|
| 671 |
end; |
| 672 |
|
| 673 |
(*****************************************************************************) |
| 674 |
|
| 675 |
(* |
| 676 |
* RegisterFile.sml |
| 677 |
* |
| 678 |
* This defines the RegisterFile structure, which provides the |
| 679 |
* functionality of the register file. The datatype registerfile |
| 680 |
* provides the encapsulation of the register file, InitRegisterFile |
| 681 |
* initializes the registerfile, setting all registers to zero and |
| 682 |
* setting r0, gp, sp, and fp to their appropriate values, |
| 683 |
* LoadRegister takes a registerfile and an integer corresponding to |
| 684 |
* the register, and returns the Word32.word value at that register, |
| 685 |
* and StoreRegister takes a registerfile, an integer corresponding to |
| 686 |
* the register, and a Word32.word and returns the registerfile |
| 687 |
* updated with the word stored in the appropriate register. |
| 688 |
* |
| 689 |
* The underlying structure of registerfile is an immutable array of |
| 690 |
* Word32.word. |
| 691 |
*) |
| 692 |
|
| 693 |
structure RegisterFile : REGISTERFILE |
| 694 |
= struct |
| 695 |
|
| 696 |
type registerfile = Word32.word ImmArray.immarray; |
| 697 |
|
| 698 |
fun InitRegisterFile () |
| 699 |
= ImmArray.update |
| 700 |
(ImmArray.update |
| 701 |
(ImmArray.update |
| 702 |
(ImmArray.update |
| 703 |
(ImmArray.immarray(32, 0wx00000000 : Word32.word), |
| 704 |
00, 0wx00000000 : Word32.word), |
| 705 |
28, 0wx00000000 : Word32.word), |
| 706 |
29, 0wx00040000 : Word32.word), |
| 707 |
30, 0wx00040000 : Word32.word) : registerfile; |
| 708 |
|
| 709 |
fun LoadRegister (rf, reg) = ImmArray.sub(rf, reg); |
| 710 |
|
| 711 |
fun StoreRegister (rf, reg, data) = ImmArray.update(rf, reg, data); |
| 712 |
|
| 713 |
end; |
| 714 |
|
| 715 |
|
| 716 |
(*****************************************************************************) |
| 717 |
|
| 718 |
(* |
| 719 |
* ALU.sig |
| 720 |
* |
| 721 |
* This defines the exported datatype and function provided by the |
| 722 |
* ALU. The datatype ALUOp provides a means to specify which |
| 723 |
* operation is to be performed by the ALU, and PerformAL performs |
| 724 |
* one of the operations on two thirty-two bit words, returning the |
| 725 |
* result as a thirty-two bit word. |
| 726 |
*) |
| 727 |
|
| 728 |
signature ALU |
| 729 |
= sig |
| 730 |
|
| 731 |
datatype ALUOp = SLL | SRL | SRA | |
| 732 |
ADD | ADDU | |
| 733 |
SUB | SUBU | |
| 734 |
AND | OR | XOR | |
| 735 |
SEQ | SNE | |
| 736 |
SLT | SGT | |
| 737 |
SLE | SGE; |
| 738 |
|
| 739 |
val PerformAL : (ALUOp * Word32.word * Word32.word) -> Word32.word; |
| 740 |
|
| 741 |
end; |
| 742 |
|
| 743 |
(*****************************************************************************) |
| 744 |
|
| 745 |
(* |
| 746 |
* ALU.sml |
| 747 |
* |
| 748 |
* This defines the ALU structure, which provides the functionality of |
| 749 |
* an Arithmetic/Logic Unit. The datatype ALUOp provides a means to |
| 750 |
* specify which operation is to be performed by the ALU, and |
| 751 |
* PerformAL performs one of the operations on two thirty-two bit |
| 752 |
* words, returning the result as a thirty-two bit word. |
| 753 |
* |
| 754 |
* A note about SML'97 Basis Library implementation of thirty-two bit |
| 755 |
* numbers: the Word32.word is an unsigned thirty-two bit integer, |
| 756 |
* while Int.int (equivalent to Int.int) is a signed thirty-two |
| 757 |
* bit integer. In order to perform the signed operations, it is |
| 758 |
* necessary to convert the words to signed form, using the |
| 759 |
* Word32.toIntX function, which performs sign extension, |
| 760 |
* and to convert the result back into unsigned form using the |
| 761 |
* Word32.fromInt function. In addition, to perform a shift, |
| 762 |
* the second Word32.word needs to be "downsized" to a normal |
| 763 |
* Word.word using the Word.fromWord function. |
| 764 |
*) |
| 765 |
|
| 766 |
structure ALU : ALU |
| 767 |
= struct |
| 768 |
|
| 769 |
datatype ALUOp = SLL | SRL | SRA | |
| 770 |
ADD | ADDU | |
| 771 |
SUB | SUBU | |
| 772 |
AND | OR | XOR | |
| 773 |
SEQ | SNE | |
| 774 |
SLT | SGT | |
| 775 |
SLE | SGE; |
| 776 |
|
| 777 |
fun PerformAL (opcode, s1, s2) = |
| 778 |
(case opcode |
| 779 |
of SLL => |
| 780 |
Word32.<< (s1, Word.fromLarge (Word32.toLarge s2)) |
| 781 |
| SRL => |
| 782 |
Word32.>> (s1, Word.fromLarge (Word32.toLarge s2)) |
| 783 |
| SRA => |
| 784 |
Word32.~>> (s1, Word.fromLarge (Word32.toLarge s2)) |
| 785 |
| ADD => |
| 786 |
Word32.fromInt (Int.+ (Word32.toIntX s1, |
| 787 |
Word32.toIntX s2)) |
| 788 |
| ADDU => |
| 789 |
Word32.+ (s1, s2) |
| 790 |
| SUB => |
| 791 |
Word32.fromInt (Int.- (Word32.toIntX s1, |
| 792 |
Word32.toIntX s2)) |
| 793 |
| SUBU => |
| 794 |
Word32.- (s1, s2) |
| 795 |
| AND => |
| 796 |
Word32.andb (s1, s2) |
| 797 |
| OR => |
| 798 |
Word32.orb (s1, s2) |
| 799 |
| XOR => |
| 800 |
Word32.xorb (s1, s2) |
| 801 |
| SEQ => |
| 802 |
if (s1 = s2) |
| 803 |
then 0wx00000001 : Word32.word |
| 804 |
else 0wx00000000 : Word32.word |
| 805 |
| SNE => |
| 806 |
if not (s1 = s2) |
| 807 |
then 0wx00000001 : Word32.word |
| 808 |
else 0wx00000000 : Word32.word |
| 809 |
| SLT => |
| 810 |
if Int.< (Word32.toIntX s1, Word32.toIntX s2) |
| 811 |
then 0wx00000001 : Word32.word |
| 812 |
else 0wx00000000 : Word32.word |
| 813 |
| SGT => |
| 814 |
if Int.> (Word32.toIntX s1, Word32.toIntX s2) |
| 815 |
then 0wx00000001 : Word32.word |
| 816 |
else 0wx00000000 : Word32.word |
| 817 |
| SLE => |
| 818 |
if Int.<= (Word32.toIntX s1, Word32.toIntX s2) |
| 819 |
then 0wx00000001 : Word32.word |
| 820 |
else 0wx00000000 : Word32.word |
| 821 |
| SGE => |
| 822 |
if Int.>= (Word32.toIntX s1, Word32.toIntX s2) |
| 823 |
then 0wx00000001 : Word32.word |
| 824 |
else 0wx00000000 : Word32.word) |
| 825 |
(* |
| 826 |
* This handle will handle all ALU errors, most |
| 827 |
* notably overflow and division by zero, and will |
| 828 |
* print an error message and return 0. |
| 829 |
*) |
| 830 |
handle _ => |
| 831 |
(print "Error : ALU returning 0\n"; |
| 832 |
0wx00000000 : Word32.word); |
| 833 |
|
| 834 |
end; |
| 835 |
|
| 836 |
(*****************************************************************************) |
| 837 |
|
| 838 |
(* |
| 839 |
* Memory.sig |
| 840 |
* |
| 841 |
* This defines the exported datatype and functions provided by |
| 842 |
* memory. The datatype memory provides the encapsulation |
| 843 |
* of memory, InitMemory initializes memory, setting all |
| 844 |
* addresses to zero, LoadWord takes memory and |
| 845 |
* a Word32.word corresponding to the address, and returns the |
| 846 |
* Word32.word value at that address, StoreWord takes memory, |
| 847 |
* a Word32.word corresponding to the address, and a |
| 848 |
* Word32.word and returns memory updated with the word |
| 849 |
* stored at the appropriate address. LoadHWord, LoadHWordU, |
| 850 |
* LoadByte, and LoadByteU load halfwords, unsigned halfwords, |
| 851 |
* bytes, and unsigned bytes respectively from memory into the |
| 852 |
* lower portion of the returned Word32.word. StoreHWord and |
| 853 |
* StoreByte store halfwords and bytes taken from the lower portion |
| 854 |
* of the Word32.word into memory. |
| 855 |
* GetStatistics takes memory and returns the read and write |
| 856 |
* statistics as a string. |
| 857 |
*) |
| 858 |
|
| 859 |
signature MEMORY |
| 860 |
= sig |
| 861 |
|
| 862 |
type memory; |
| 863 |
|
| 864 |
val InitMemory : unit -> memory; |
| 865 |
|
| 866 |
val LoadWord : memory * Word32.word -> memory * Word32.word; |
| 867 |
val StoreWord : memory * Word32.word * Word32.word -> memory; |
| 868 |
|
| 869 |
val LoadHWord : memory * Word32.word -> memory * Word32.word; |
| 870 |
val LoadHWordU : memory * Word32.word -> memory * Word32.word; |
| 871 |
val StoreHWord : memory * Word32.word * Word32.word -> memory; |
| 872 |
|
| 873 |
val LoadByte : memory * Word32.word -> memory * Word32.word; |
| 874 |
val LoadByteU : memory * Word32.word -> memory * Word32.word; |
| 875 |
val StoreByte : memory * Word32.word * Word32.word -> memory; |
| 876 |
|
| 877 |
val GetStatistics : memory -> string; |
| 878 |
|
| 879 |
end; |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
(*****************************************************************************) |
| 886 |
|
| 887 |
(* |
| 888 |
* Memory.sml |
| 889 |
* |
| 890 |
* This defines the Memory structure, which provides the functionality |
| 891 |
* of memory. The datatype memory provides the encapsulation of |
| 892 |
* memory, InitMemory initializes memory, setting all |
| 893 |
* addresses to zero, LoadWord takes memory and |
| 894 |
* a Word32.word corresponding to the address, and returns the |
| 895 |
* Word32.word value at that address and the updated memory, |
| 896 |
* StoreWord takes memory, a Word32.word corresponding to the |
| 897 |
* address, and a Word32.word and returns memory updated with the word |
| 898 |
* stored at the appropriate address. LoadHWord, LoadHWordU, |
| 899 |
* LoadByte, and LoadByteU load halfwords, unsigned halfwords, |
| 900 |
* bytes, and unsigned bytes respectively from memory into the |
| 901 |
* lower portion of the returned Word32.word. StoreHWord and |
| 902 |
* StoreByte store halfwords and bytes taken from the lower portion |
| 903 |
* of the Word32.word into memory. |
| 904 |
* GetStatistics takes memory and returns the read and write |
| 905 |
* statistics as a string. |
| 906 |
* |
| 907 |
* The underlying structure of memory is an immutable array of Word32.word. |
| 908 |
* The array has a length of 0x10000, since every element of the array |
| 909 |
* corresponds to a thirty-two bit integer. |
| 910 |
* |
| 911 |
* Also, the functions AlignWAddress and AlignHWAddress aligns a memory |
| 912 |
* address to a word and halfword address, respectively. If LoadWord, |
| 913 |
* StoreWord, LoadHWord, LoadHWordU, or StoreHWord is asked to access an |
| 914 |
* unaligned address, it writes an error message, and uses the address |
| 915 |
* rounded down to the aligned address. |
| 916 |
*) |
| 917 |
|
| 918 |
structure Memory : MEMORY |
| 919 |
= struct |
| 920 |
|
| 921 |
type memory = Word32.word ImmArray.immarray * (int * int); |
| 922 |
|
| 923 |
fun InitMemory () = |
| 924 |
(ImmArray.immarray(Word32.toInt(0wx10000 : Word32.word), |
| 925 |
0wx00000000 : Word32.word), |
| 926 |
(0, 0)) : memory; |
| 927 |
|
| 928 |
fun AlignWAddress address |
| 929 |
= Word32.<< (Word32.>> (address, 0wx0002), 0wx0002); |
| 930 |
|
| 931 |
fun AlignHWAddress address |
| 932 |
= Word32.<< (Word32.>> (address, 0wx0001), 0wx0001); |
| 933 |
|
| 934 |
(* Load and Store provide errorless access to memory. |
| 935 |
* They provide a common interface to memory, while |
| 936 |
* the LoadX and StoreX specifically access words, |
| 937 |
* halfwords and bytes, requiring address to be aligned. |
| 938 |
* In Load and Store, two intermediate values are |
| 939 |
* generated. The value aligned_address is the aligned |
| 940 |
* version of the given address, and is used to compare with |
| 941 |
* the original address to determine if it was aligned. The |
| 942 |
* value use_address is equivalent to aligned_address divided |
| 943 |
* by four, and it corresponds to the index of the memory |
| 944 |
* array where the corresponding aligned address can be found. |
| 945 |
*) |
| 946 |
|
| 947 |
fun Load ((mem, (reads, writes)), address) |
| 948 |
= let |
| 949 |
val aligned_address = AlignWAddress address; |
| 950 |
val use_address = Word32.>> (aligned_address, 0wx0002); |
| 951 |
in |
| 952 |
((mem, (reads + 1, writes)), |
| 953 |
ImmArray.sub(mem, Word32.toInt(use_address))) |
| 954 |
end; |
| 955 |
|
| 956 |
fun Store ((mem, (reads, writes)), address, data) |
| 957 |
= let |
| 958 |
val aligned_address = AlignWAddress address; |
| 959 |
val use_address = Word32.>> (aligned_address, 0wx0002); |
| 960 |
in |
| 961 |
(ImmArray.update(mem, Word32.toInt(use_address), data), |
| 962 |
(reads, writes + 1)) |
| 963 |
end; |
| 964 |
|
| 965 |
|
| 966 |
fun LoadWord (mem, address) |
| 967 |
= let |
| 968 |
val aligned_address |
| 969 |
= if address = AlignWAddress address |
| 970 |
then address |
| 971 |
else (print "Error LW: Memory using aligned address\n"; |
| 972 |
AlignWAddress address); |
| 973 |
in |
| 974 |
Load(mem, aligned_address) |
| 975 |
end; |
| 976 |
|
| 977 |
fun StoreWord (mem, address, data) |
| 978 |
= let |
| 979 |
val aligned_address |
| 980 |
= if address = AlignWAddress address |
| 981 |
then address |
| 982 |
else (print "Error SW: Memory using aligned address\n"; |
| 983 |
AlignWAddress address); |
| 984 |
in |
| 985 |
Store(mem, aligned_address, data) |
| 986 |
end; |
| 987 |
|
| 988 |
fun LoadHWord (mem, address) |
| 989 |
= let |
| 990 |
val aligned_address |
| 991 |
= if address = AlignHWAddress address |
| 992 |
then address |
| 993 |
else (print "Error LH: Memory using aligned address\n"; |
| 994 |
AlignHWAddress address); |
| 995 |
val (nmem,l_word) = Load(mem, aligned_address); |
| 996 |
in |
| 997 |
(nmem, |
| 998 |
case aligned_address |
| 999 |
of 0wx00000000 : Word32.word |
| 1000 |
=> Word32.~>>(Word32.<<(l_word, 0wx0010), |
| 1001 |
0wx0010) |
| 1002 |
| 0wx00000010 : Word32.word |
| 1003 |
=> Word32.~>>(Word32.<<(l_word, 0wx0000), |
| 1004 |
0wx0010) |
| 1005 |
| _ => (print "Error LH: Memory returning 0\n"; |
| 1006 |
0wx00000000 : Word32.word)) |
| 1007 |
end; |
| 1008 |
|
| 1009 |
fun LoadHWordU (mem, address) |
| 1010 |
= let |
| 1011 |
val aligned_address |
| 1012 |
= if address = AlignHWAddress address |
| 1013 |
then address |
| 1014 |
else (print "Error LHU: Memory using aligned address\n"; |
| 1015 |
AlignHWAddress address); |
| 1016 |
val (nmem, l_word) = Load(mem, aligned_address); |
| 1017 |
in |
| 1018 |
(nmem, |
| 1019 |
case aligned_address |
| 1020 |
of 0wx00000000 : Word32.word |
| 1021 |
=> Word32.>>(Word32.<<(l_word, 0wx0010), |
| 1022 |
0wx0010) |
| 1023 |
| 0wx00000010 : Word32.word |
| 1024 |
=> Word32.>>(Word32.<<(l_word, 0wx0000), |
| 1025 |
0wx0010) |
| 1026 |
| _ => (print "Error LHU: Memory returning 0\n"; |
| 1027 |
0wx00000000 : Word32.word)) |
| 1028 |
end; |
| 1029 |
|
| 1030 |
fun StoreHWord (mem, address, data) |
| 1031 |
= let |
| 1032 |
val aligned_address |
| 1033 |
= if address = AlignHWAddress address |
| 1034 |
then address |
| 1035 |
else (print "Error SH: Memory using aligned address\n"; |
| 1036 |
AlignWAddress address); |
| 1037 |
val (_, s_word) = Load(mem, aligned_address); |
| 1038 |
in |
| 1039 |
case aligned_address |
| 1040 |
of 0wx00000000 : Word32.word |
| 1041 |
=> Store(mem, aligned_address, |
| 1042 |
Word32.orb(Word32.andb(0wxFFFF0000 : Word32.word, |
| 1043 |
s_word), |
| 1044 |
Word32.<<(Word32.andb(0wx0000FFFF : |
| 1045 |
Word32.word, |
| 1046 |
data), |
| 1047 |
0wx0000))) |
| 1048 |
| 0wx00000010 : Word32.word |
| 1049 |
=> Store(mem, aligned_address, |
| 1050 |
Word32.orb(Word32.andb(0wx0000FFFF : Word32.word, |
| 1051 |
s_word), |
| 1052 |
Word32.<<(Word32.andb(0wx0000FFFF : |
| 1053 |
Word32.word, |
| 1054 |
data), |
| 1055 |
0wx0010))) |
| 1056 |
| _ => (print "Error SH: Memory unchanged\n"; |
| 1057 |
mem) |
| 1058 |
end; |
| 1059 |
|
| 1060 |
fun LoadByte (mem, address) |
| 1061 |
= let |
| 1062 |
val aligned_address = address; |
| 1063 |
val (nmem, l_word) = Load(mem, aligned_address); |
| 1064 |
in |
| 1065 |
(nmem, |
| 1066 |
case aligned_address |
| 1067 |
of 0wx00000000 : Word32.word |
| 1068 |
=> Word32.~>>(Word32.<<(l_word, |
| 1069 |
0wx0018), |
| 1070 |
0wx0018) |
| 1071 |
| 0wx00000008 : Word32.word |
| 1072 |
=> Word32.~>>(Word32.<<(l_word, |
| 1073 |
0wx0010), |
| 1074 |
0wx0018) |
| 1075 |
| 0wx00000010 : Word32.word |
| 1076 |
=> Word32.~>>(Word32.<<(l_word, |
| 1077 |
0wx0008), |
| 1078 |
0wx0018) |
| 1079 |
| 0wx00000018 : Word32.word |
| 1080 |
=> Word32.~>>(Word32.<<(l_word, |
| 1081 |
0wx0000), |
| 1082 |
0wx0018) |
| 1083 |
| _ => (print "Error LB: Memory returning 0\n"; |
| 1084 |
0wx00000000 : Word32.word)) |
| 1085 |
end; |
| 1086 |
|
| 1087 |
fun LoadByteU (mem, address) |
| 1088 |
= let |
| 1089 |
val aligned_address = address; |
| 1090 |
val (nmem, l_word) = Load(mem, aligned_address); |
| 1091 |
in |
| 1092 |
(nmem, |
| 1093 |
case aligned_address |
| 1094 |
of 0wx00000000 : Word32.word |
| 1095 |
=> Word32.>>(Word32.<<(l_word, |
| 1096 |
0wx0018), |
| 1097 |
0wx0018) |
| 1098 |
| 0wx00000008 : Word32.word |
| 1099 |
=> Word32.>>(Word32.<<(l_word, |
| 1100 |
0wx0010), |
| 1101 |
0wx0018) |
| 1102 |
| 0wx00000010 : Word32.word |
| 1103 |
=> Word32.>>(Word32.<<(l_word, |
| 1104 |
0wx0008), |
| 1105 |
0wx0018) |
| 1106 |
| 0wx00000018 : Word32.word |
| 1107 |
=> Word32.>>(Word32.<<(l_word, |
| 1108 |
0wx0000), |
| 1109 |
0wx0018) |
| 1110 |
| _ => (print "Error LBU: Memory returning 0\n"; |
| 1111 |
0wx00000000 : Word32.word)) |
| 1112 |
end; |
| 1113 |
|
| 1114 |
fun StoreByte (mem, address, data) |
| 1115 |
= let |
| 1116 |
val aligned_address = address; |
| 1117 |
val (_, s_word) = Load(mem, aligned_address); |
| 1118 |
in |
| 1119 |
case aligned_address |
| 1120 |
of 0wx00000000 : Word32.word |
| 1121 |
=> Store(mem, aligned_address, |
| 1122 |
Word32.orb(Word32.andb(0wxFFFFFF00 : Word32.word, |
| 1123 |
s_word), |
| 1124 |
Word32.<<(Word32.andb(0wx000000FF : |
| 1125 |
Word32.word, |
| 1126 |
data), |
| 1127 |
0wx0000))) |
| 1128 |
| 0wx00000008 : Word32.word |
| 1129 |
=> Store(mem, aligned_address, |
| 1130 |
Word32.orb(Word32.andb(0wxFFFF00FF : Word32.word, |
| 1131 |
s_word), |
| 1132 |
Word32.<<(Word32.andb(0wx000000FF : |
| 1133 |
Word32.word, |
| 1134 |
data), |
| 1135 |
0wx0008))) |
| 1136 |
| 0wx00000010 : Word32.word |
| 1137 |
=> Store(mem, aligned_address, |
| 1138 |
Word32.orb(Word32.andb(0wxFF00FFFF : Word32.word, |
| 1139 |
s_word), |
| 1140 |
Word32.<<(Word32.andb(0wx000000FF : |
| 1141 |
Word32.word, |
| 1142 |
data), |
| 1143 |
0wx0010))) |
| 1144 |
| 0wx00000018 : Word32.word |
| 1145 |
=> Store(mem, aligned_address, |
| 1146 |
Word32.orb(Word32.andb(0wx00FFFFFF : Word32.word, |
| 1147 |
s_word), |
| 1148 |
Word32.<<(Word32.andb(0wx000000FF : |
| 1149 |
Word32.word, |
| 1150 |
data), |
| 1151 |
0wx0018))) |
| 1152 |
| _ => (print "Error SB: Memory unchanged\n"; |
| 1153 |
mem) |
| 1154 |
end; |
| 1155 |
|
| 1156 |
fun GetStatistics (mem, (reads, writes)) |
| 1157 |
= "Memory :\n" ^ |
| 1158 |
"Memory Reads : " ^ (Int.toString reads) ^ "\n" ^ |
| 1159 |
"Memory Writes : " ^ (Int.toString writes) ^ "\n"; |
| 1160 |
|
| 1161 |
end; |
| 1162 |
|
| 1163 |
(*****************************************************************************) |
| 1164 |
|
| 1165 |
(* |
| 1166 |
* CacheSpec.sig |
| 1167 |
* |
| 1168 |
* This defines the signature that outlines the specifications to |
| 1169 |
* describe a cache. The two datatypes are given to provide clear |
| 1170 |
* means of differentiating between the write hit and write miss |
| 1171 |
* options. CacheName can be any string describing the cache. |
| 1172 |
* CacheSize is an integer that represents the total number of words |
| 1173 |
* in the cache. BlockSize is an integer that represents the total |
| 1174 |
* number of words in a block. Associativity is an integer that |
| 1175 |
* represents the associativity of the cache. WriteHit and WriteMiss |
| 1176 |
* represent the write hit and write miss options to be implemented by |
| 1177 |
* this cache. |
| 1178 |
*) |
| 1179 |
|
| 1180 |
signature CACHESPEC |
| 1181 |
= sig |
| 1182 |
|
| 1183 |
datatype WriteHitOption = Write_Through |
| 1184 |
| Write_Back; |
| 1185 |
|
| 1186 |
datatype WriteMissOption = Write_Allocate |
| 1187 |
| Write_No_Allocate; |
| 1188 |
|
| 1189 |
val CacheName : string; |
| 1190 |
val CacheSize : int; |
| 1191 |
val BlockSize : int; |
| 1192 |
val Associativity : int; |
| 1193 |
val WriteHit : WriteHitOption; |
| 1194 |
val WriteMiss : WriteMissOption; |
| 1195 |
|
| 1196 |
end; |
| 1197 |
|
| 1198 |
(*****************************************************************************) |
| 1199 |
|
| 1200 |
(* |
| 1201 |
* CachedMemory.sml |
| 1202 |
* |
| 1203 |
* This defines the CachedMemory functor, which provides the |
| 1204 |
* functionality of a cached memory and which takes two structures, |
| 1205 |
* corresponding to the cache specification and the the level of |
| 1206 |
* memory which the cache will be caching. The datatype memory |
| 1207 |
* provides the encapsulation of the cache along with the memory |
| 1208 |
* system that is being cached, InitMemory initializes the cache and |
| 1209 |
* the memory system that is being cached, LoadWord takes memory and a |
| 1210 |
* Word32.word corresponding to the address, and returns the |
| 1211 |
* Word32.word at that address and the updated cache and memory, |
| 1212 |
* StoreWord takes memory, a Word32.word corresponding to the address, |
| 1213 |
* and a Word32.word and returns the cache and memory updated with the |
| 1214 |
* stored at the appropriate address. LoadHWord, LoadHWordU, |
| 1215 |
* LoadByte, and LoadByteU load halfwords, unsigned halfwords, |
| 1216 |
* bytes, and unsigned bytes respectively from memory into the |
| 1217 |
* lower portion of the returned Word32.word. StoreHWord and |
| 1218 |
* StoreByte store halfwords and bytes taken from the lower portion |
| 1219 |
* of the Word32.word into memory. |
| 1220 |
* GetStatistics takes memory and returns the read and write |
| 1221 |
* statistics as a string. |
| 1222 |
* |
| 1223 |
* The underlying structure of cache is a two dimensional array of |
| 1224 |
* cache lines, where a cache line consists of a valid bit, dirty bit, |
| 1225 |
* a tag and a block of words, as a Word32.word array. |
| 1226 |
* The size of the cache, the associativity, and the block size are |
| 1227 |
* specified by the cache specification. |
| 1228 |
* |
| 1229 |
* Also, the functions AlignWAddress and AlignHWAddress aligns a memory |
| 1230 |
* address to a word and halfword address, respectively. If LoadWord, |
| 1231 |
* StoreWord, LoadHWord, LoadHWordU, or StoreHWord is asked to access an |
| 1232 |
* unaligned address, it writes an error message, and uses the address |
| 1233 |
* rounded down to the aligned address. |
| 1234 |
*) |
| 1235 |
|
| 1236 |
functor CachedMemory (structure CS : CACHESPEC; |
| 1237 |
structure MEM : MEMORY;) : MEMORY |
| 1238 |
= struct |
| 1239 |
|
| 1240 |
type cacheline |
| 1241 |
= bool * bool * Word32.word * Word32.word ImmArray.immarray; |
| 1242 |
|
| 1243 |
type cacheset |
| 1244 |
= cacheline ImmArray.immarray; |
| 1245 |
|
| 1246 |
type cache |
| 1247 |
= cacheset ImmArray.immarray; |
| 1248 |
|
| 1249 |
type memory = (cache * (int * int * int * int)) * MEM.memory; |
| 1250 |
|
| 1251 |
|
| 1252 |
(* Performs log[base2] on an integer. *) |
| 1253 |
fun exp2 0 = 1 |
| 1254 |
| exp2 n = 2 * (exp2 (n-1)) |
| 1255 |
fun log2 x = let |
| 1256 |
fun log2_aux n = if exp2 n > x |
| 1257 |
then (n-1) |
| 1258 |
else log2_aux (n+1) |
| 1259 |
in |
| 1260 |
log2_aux 0 |
| 1261 |
end |
| 1262 |
|
| 1263 |
open CS; |
| 1264 |
|
| 1265 |
(* |
| 1266 |
* The following values of index size and field bits are |
| 1267 |
* calculated from the values in the cache specification |
| 1268 |
* structure. |
| 1269 |
*) |
| 1270 |
val IndexSize = CacheSize div (BlockSize * Associativity); |
| 1271 |
val BlockOffsetBits = log2 (BlockSize * 4); |
| 1272 |
val IndexBits = log2 IndexSize; |
| 1273 |
val TagBits = 32 - BlockOffsetBits - IndexBits; |
| 1274 |
|
| 1275 |
|
| 1276 |
(* |
| 1277 |
* RandEntry returns a random number between |
| 1278 |
* [0, Associativity - 1]. It is used to determine |
| 1279 |
* replacement of data in the cache. |
| 1280 |
*) |
| 1281 |
val RandEntry = let |
| 1282 |
val modulus = Word.fromInt(Associativity - 1) |
| 1283 |
in |
| 1284 |
fn () => Word.toInt(Word.mod(rand (), |
| 1285 |
modulus)) |
| 1286 |
end |
| 1287 |
|
| 1288 |
(* |
| 1289 |
* The InitCache function initializes the cache to |
| 1290 |
* not-valid, not-dirty, 0wx00000000 tag, blocks initialized |
| 1291 |
* to 0wx00000000. |
| 1292 |
*) |
| 1293 |
fun InitCache () |
| 1294 |
= let |
| 1295 |
val cacheline = (false, false, 0wx00000000 : Word32.word, |
| 1296 |
ImmArray.immarray (BlockSize, |
| 1297 |
0wx00000000 : Word32.word)); |
| 1298 |
val cacheset = ImmArray.immarray (Associativity, cacheline); |
| 1299 |
in |
| 1300 |
(ImmArray.immarray (IndexSize, cacheset), |
| 1301 |
(0, 0, 0, 0)) |
| 1302 |
end; |
| 1303 |
|
| 1304 |
|
| 1305 |
(* |
| 1306 |
* The InitMemory function initializes the cache |
| 1307 |
* and the memory being cached. |
| 1308 |
*) |
| 1309 |
fun InitMemory () = (InitCache (), MEM.InitMemory ()) : memory; |
| 1310 |
|
| 1311 |
|
| 1312 |
(* |
| 1313 |
* GetTag returns the Word32.word corresponding to the tag field of |
| 1314 |
* address |
| 1315 |
*) |
| 1316 |
fun GetTag address |
| 1317 |
= Word32.>> (address, |
| 1318 |
Word.fromInt (IndexBits + BlockOffsetBits)); |
| 1319 |
|
| 1320 |
|
| 1321 |
(* |
| 1322 |
* GetIndex returns the Word32.word corresponding to the index |
| 1323 |
* field of address. |
| 1324 |
*) |
| 1325 |
fun GetIndex address |
| 1326 |
= let |
| 1327 |
val mask |
| 1328 |
= Word32.notb |
| 1329 |
(Word32.<< |
| 1330 |
(Word32.>> (0wxFFFFFFFF : Word32.word, |
| 1331 |
Word.fromInt (IndexBits + BlockOffsetBits)), |
| 1332 |
Word.fromInt (IndexBits + BlockOffsetBits))); |
| 1333 |
in |
| 1334 |
Word32.>> (Word32.andb (address, mask), |
| 1335 |
Word.fromInt (BlockOffsetBits)) |
| 1336 |
end; |
| 1337 |
|
| 1338 |
|
| 1339 |
(* |
| 1340 |
* GetBlockOffset returns the Word32.word corresponding to the |
| 1341 |
* block offset field of address. |
| 1342 |
*) |
| 1343 |
fun GetBlockOffset address |
| 1344 |
= let |
| 1345 |
val mask |
| 1346 |
= Word32.notb |
| 1347 |
(Word32.<< |
| 1348 |
(Word32.>> (0wxFFFFFFFF : Word32.word, |
| 1349 |
Word.fromInt BlockOffsetBits), |
| 1350 |
Word.fromInt BlockOffsetBits)); |
| 1351 |
in |
| 1352 |
Word32.andb (address, mask) |
| 1353 |
end; |
| 1354 |
|
| 1355 |
|
| 1356 |
(* |
| 1357 |
* The InCache* family of functions returns a boolean value |
| 1358 |
* that determines if the word specified by address is in the |
| 1359 |
* cache at the current time (and that the data is valid). |
| 1360 |
*) |
| 1361 |
fun InCache_aux_entry ((valid, dirty, tag, block), address) |
| 1362 |
= tag = (GetTag address) andalso valid; |
| 1363 |
|
| 1364 |
fun InCache_aux_set (set, address) |
| 1365 |
= ImmArray.foldr (fn (entry, result) => |
| 1366 |
(InCache_aux_entry (entry, address)) orelse |
| 1367 |
result) |
| 1368 |
false |
| 1369 |
set; |
| 1370 |
|
| 1371 |
fun InCache (cac, address) |
| 1372 |
= InCache_aux_set (ImmArray.sub (cac, |
| 1373 |
Word32.toInt (GetIndex address)), |
| 1374 |
address); |
| 1375 |
|
| 1376 |
(* |
| 1377 |
* The ReadCache* family of functions returns the Word32.word |
| 1378 |
* stored at address in the cache. |
| 1379 |
*) |
| 1380 |
fun ReadCache_aux_entry ((valid, dirty, tag, block), address) |
| 1381 |
= ImmArray.sub (block, |
| 1382 |
Word32.toInt (Word32.>> (GetBlockOffset address, |
| 1383 |
0wx0002))); |
| 1384 |
|
| 1385 |
fun ReadCache_aux_set (set, address) |
| 1386 |
= ImmArray.foldr (fn (entry, result) => |
| 1387 |
if InCache_aux_entry (entry, address) |
| 1388 |
then ReadCache_aux_entry (entry, address) |
| 1389 |
else result) |
| 1390 |
(0wx00000000 : Word32.word) |
| 1391 |
set; |
| 1392 |
|
| 1393 |
fun ReadCache (cac, address) |
| 1394 |
= ReadCache_aux_set (ImmArray.sub (cac, |
| 1395 |
Word32.toInt(GetIndex address)), |
| 1396 |
address); |
| 1397 |
|
| 1398 |
|
| 1399 |
(* |
| 1400 |
* The WriteCache* family of functions returns the updated |
| 1401 |
* cache with data stored at address. |
| 1402 |
*) |
| 1403 |
fun WriteCache_aux_entry ((valid, dirty, tag, block), address, data) |
| 1404 |
= let |
| 1405 |
val ndirty = case WriteHit |
| 1406 |
of Write_Through => false |
| 1407 |
| Write_Back => true; |
| 1408 |
in |
| 1409 |
(true, ndirty, tag, |
| 1410 |
ImmArray.update (block, |
| 1411 |
Word32.toInt (Word32.>> |
| 1412 |
(GetBlockOffset address, |
| 1413 |
0wx0002)), |
| 1414 |
data)) |
| 1415 |
end; |
| 1416 |
|
| 1417 |
fun WriteCache_aux_set (set, address, data) |
| 1418 |
= ImmArray.map (fn entry => |
| 1419 |
if InCache_aux_entry (entry, address) |
| 1420 |
then WriteCache_aux_entry (entry, address, |
| 1421 |
data) |
| 1422 |
else entry) |
| 1423 |
set; |
| 1424 |
|
| 1425 |
fun WriteCache (cac, address, data) |
| 1426 |
= let |
| 1427 |
val index = Word32.toInt (GetIndex address); |
| 1428 |
val nset = WriteCache_aux_set (ImmArray.sub (cac, index), |
| 1429 |
address, data); |
| 1430 |
in |
| 1431 |
ImmArray.update (cac, index, nset) |
| 1432 |
end; |
| 1433 |
|
| 1434 |
|
| 1435 |
(* |
| 1436 |
* The LoadBlock function returns the updated |
| 1437 |
* memory and the block containing address loaded from memory. |
| 1438 |
*) |
| 1439 |
fun LoadBlock (mem, address) |
| 1440 |
= ImmArray.foldr (fn (offset, (block, mem)) => |
| 1441 |
let |
| 1442 |
val laddress |
| 1443 |
= Word32.+ (Word32.<< |
| 1444 |
(Word32.>> |
| 1445 |
(address, |
| 1446 |
Word.fromInt |
| 1447 |
BlockOffsetBits), |
| 1448 |
Word.fromInt |
| 1449 |
BlockOffsetBits), |
| 1450 |
Word32.<< (Word32.fromInt |
| 1451 |
offset, |
| 1452 |
0wx0002)); |
| 1453 |
val (nmem, nword) = MEM.LoadWord (mem, |
| 1454 |
laddress); |
| 1455 |
in |
| 1456 |
(ImmArray.update (block, offset, nword), nmem) |
| 1457 |
end) |
| 1458 |
(ImmArray.immarray (BlockSize, |
| 1459 |
0wx00000000 : Word32.word), mem) |
| 1460 |
(ImmArray.tabulate (BlockSize, fn i => i)); |
| 1461 |
|
| 1462 |
|
| 1463 |
(* |
| 1464 |
* The StoreBlock functionsreturns the updated |
| 1465 |
* memory with block stored into the block containing address. |
| 1466 |
*) |
| 1467 |
fun StoreBlock (block, mem, address) |
| 1468 |
= ImmArray.foldr (fn (offset, mem) => |
| 1469 |
let |
| 1470 |
val saddress |
| 1471 |
= Word32.+ (Word32.<< |
| 1472 |
(Word32.>> |
| 1473 |
(address, |
| 1474 |
Word.fromInt |
| 1475 |
BlockOffsetBits), |
| 1476 |
Word.fromInt |
| 1477 |
BlockOffsetBits), |
| 1478 |
Word32.<< (Word32.fromInt |
| 1479 |
offset, |
| 1480 |
0wx0002)); |
| 1481 |
in |
| 1482 |
MEM.StoreWord (mem, saddress, |
| 1483 |
ImmArray.sub (block, offset)) |
| 1484 |
end) |
| 1485 |
mem |
| 1486 |
(ImmArray.tabulate (BlockSize, fn i => i)); |
| 1487 |
|
| 1488 |
|
| 1489 |
(* |
| 1490 |
* The LoadCache* family of functions returns the updated |
| 1491 |
* cache and memory, with the block containing address loaded |
| 1492 |
* into the cache at the appropriate cache line, and dirty |
| 1493 |
* data written back to memory as needed. |
| 1494 |
*) |
| 1495 |
fun LoadCache_aux_entry ((valid, dirty, tag, block), mem, address) |
| 1496 |
= let |
| 1497 |
val saddress |
| 1498 |
= Word32.orb (Word32.<< (tag, |
| 1499 |
Word.fromInt TagBits), |
| 1500 |
Word32.<< (GetIndex address, |
| 1501 |
Word.fromInt IndexBits)); |
| 1502 |
val nmem = if valid andalso dirty |
| 1503 |
then StoreBlock (block, mem, saddress) |
| 1504 |
else mem; |
| 1505 |
val (nblock, nnmem) = LoadBlock (nmem, address); |
| 1506 |
in |
| 1507 |
((true, false, GetTag address, nblock), nnmem) |
| 1508 |
end; |
| 1509 |
|
| 1510 |
fun LoadCache_aux_set (set, mem, address) |
| 1511 |
= let |
| 1512 |
val entry = RandEntry (); |
| 1513 |
val (nentry, nmem) = LoadCache_aux_entry (ImmArray.sub (set, |
| 1514 |
entry), |
| 1515 |
mem, address); |
| 1516 |
in |
| 1517 |
(ImmArray.update (set, entry, nentry), nmem) |
| 1518 |
end; |
| 1519 |
|
| 1520 |
fun LoadCache (cac, mem, address) |
| 1521 |
= let |
| 1522 |
val index = Word32.toInt (GetIndex address); |
| 1523 |
val (nset, nmem) |
| 1524 |
= LoadCache_aux_set (ImmArray.sub (cac, index), |
| 1525 |
mem, address); |
| 1526 |
in |
| 1527 |
(ImmArray.update (cac, index, nset), nmem) |
| 1528 |
end; |
| 1529 |
|
| 1530 |
|
| 1531 |
(* |
| 1532 |
* The remainder of the function defined here satisfy the MEMORY |
| 1533 |
* signature. This allows a CachedMemory to act exactly like |
| 1534 |
* a normal Memory, and thus caches can be nested to an arbitrary |
| 1535 |
* depth. |
| 1536 |
*) |
| 1537 |
|
| 1538 |
fun AlignWAddress address |
| 1539 |
= Word32.<< (Word32.>> (address, 0wx0002), 0wx0002); |
| 1540 |
|
| 1541 |
fun AlignHWAddress address |
| 1542 |
= Word32.<< (Word32.>> (address, 0wx0001), 0wx0001); |
| 1543 |
|
| 1544 |
(* Load and Store provide errorless access to memory. |
| 1545 |
* They provide a common interface to memory, while |
| 1546 |
* the LoadX and StoreX specifically access words, |
| 1547 |
* halfwords and bytes, requiring address to be aligned. |
| 1548 |
* In Load and Store, two intermediate values are |
| 1549 |
* generated. The value aligned_address is the aligned |
| 1550 |
* version of the given address, and is used to compare with |
| 1551 |
* the original address to determine if it was aligned. The |
| 1552 |
* value use_address is equivalent to aligned_address divided |
| 1553 |
* by four, and it corresponds to the index of the memory |
| 1554 |
* array where the corresponding aligned address can be found. |
| 1555 |
*) |
| 1556 |
|
| 1557 |
fun Load (((cac, (rh, rm, wh, wm)), mem), address) |
| 1558 |
= let |
| 1559 |
val aligned_address = AlignWAddress address; |
| 1560 |
in |
| 1561 |
if InCache (cac, aligned_address) |
| 1562 |
then (((cac, (rh + 1, rm, wh, wm)), mem), |
| 1563 |
ReadCache (cac, aligned_address)) |
| 1564 |
else let |
| 1565 |
val (ncac, nmem) |
| 1566 |
= LoadCache (cac, mem, aligned_address); |
| 1567 |
in |
| 1568 |
(((ncac, (rh, rm + 1, wh, wm)), nmem), |
| 1569 |
ReadCache (ncac, aligned_address)) |
| 1570 |
end |
| 1571 |
end; |
| 1572 |
|
| 1573 |
|
| 1574 |
fun Store (((cac, (rh, rm, wh, wm)), mem), address, data) |
| 1575 |
= let |
| 1576 |
val aligned_address = AlignWAddress address; |
| 1577 |
in |
| 1578 |
if InCache (cac, aligned_address) |
| 1579 |
then let |
| 1580 |
val ncac = WriteCache (cac, aligned_address, data); |
| 1581 |
in |
| 1582 |
case WriteHit |
| 1583 |
of Write_Through => |
| 1584 |
((ncac, (rh, rm, wh + 1, wm)), |
| 1585 |
MEM.StoreWord (mem, aligned_address, data)) |
| 1586 |
| Write_Back => |
| 1587 |
((ncac, (rh, rm, wh + 1, wm)), mem) |
| 1588 |
end |
| 1589 |
else case WriteMiss |
| 1590 |
of Write_Allocate => |
| 1591 |
let |
| 1592 |
val (ncac, nmem) |
| 1593 |
= LoadCache (cac, mem, aligned_address); |
| 1594 |
val nncac |
| 1595 |
= WriteCache (ncac, aligned_address, data); |
| 1596 |
in |
| 1597 |
case WriteHit |
| 1598 |
of Write_Through => |
| 1599 |
((nncac, (rh, rm, wh, wm + 1)), |
| 1600 |
MEM.StoreWord (nmem, aligned_address, |
| 1601 |
data)) |
| 1602 |
| Write_Back => |
| 1603 |
((nncac, (rh, rm, wh, wm + 1)), |
| 1604 |
nmem) |
| 1605 |
end |
| 1606 |
| Write_No_Allocate => |
| 1607 |
((cac, (rh, rm, wh, wm + 1)), |
| 1608 |
MEM.StoreWord (mem, aligned_address, data)) |
| 1609 |
end; |
| 1610 |
|
| 1611 |
fun LoadWord (mem, address) |
| 1612 |
= let |
| 1613 |
val aligned_address |
| 1614 |
= if address = AlignWAddress address |
| 1615 |
then address |
| 1616 |
else (print "Error LW: Memory using aligned address\n"; |
| 1617 |
AlignWAddress address); |
| 1618 |
in |
| 1619 |
Load(mem, aligned_address) |
| 1620 |
end; |
| 1621 |
|
| 1622 |
fun StoreWord (mem, address, data) |
| 1623 |
= let |
| 1624 |
val aligned_address |
| 1625 |
= if address = AlignWAddress address |
| 1626 |
then address |
| 1627 |
else (print "Error SW: Memory using aligned address\n"; |
| 1628 |
AlignWAddress address); |
| 1629 |
in |
| 1630 |
Store(mem, aligned_address, data) |
| 1631 |
end; |
| 1632 |
|
| 1633 |
fun LoadHWord (mem, address) |
| 1634 |
= let |
| 1635 |
val aligned_address |
| 1636 |
= if address = AlignHWAddress address |
| 1637 |
then address |
| 1638 |
else (print "Error LH: Memory using aligned address\n"; |
| 1639 |
AlignHWAddress address); |
| 1640 |
val (nmem,l_word) = Load(mem, aligned_address); |
| 1641 |
in |
| 1642 |
(nmem, |
| 1643 |
case aligned_address |
| 1644 |
of 0wx00000000 : Word32.word |
| 1645 |
=> Word32.~>>(Word32.<<(l_word, 0wx0010), |
| 1646 |
0wx0010) |
| 1647 |
| 0wx00000010 : Word32.word |
| 1648 |
=> Word32.~>>(Word32.<<(l_word, 0wx0000), |
| 1649 |
0wx0010) |
| 1650 |
| _ => (print "Error LH: Memory returning 0\n"; |
| 1651 |
0wx00000000 : Word32.word)) |
| 1652 |
end; |
| 1653 |
|
| 1654 |
fun LoadHWordU (mem, address) |
| 1655 |
= let |
| 1656 |
val aligned_address |
| 1657 |
= if address = AlignHWAddress address |
| 1658 |
then address |
| 1659 |
else (print "Error LHU: Memory using aligned address\n"; |
| 1660 |
AlignHWAddress address); |
| 1661 |
val (nmem, l_word) = Load(mem, aligned_address); |
| 1662 |
in |
| 1663 |
(nmem, |
| 1664 |
case aligned_address |
| 1665 |
of 0wx00000000 : Word32.word |
| 1666 |
=> Word32.>>(Word32.<<(l_word, 0wx0010), |
| 1667 |
0wx0010) |
| 1668 |
| 0wx00000010 : Word32.word |
| 1669 |
=> Word32.>>(Word32.<<(l_word, 0wx0000), |
| 1670 |
0wx0010) |
| 1671 |
| _ => (print "Error LHU: Memory returning 0\n"; |
| 1672 |
0wx00000000 : Word32.word)) |
| 1673 |
end; |
| 1674 |
|
| 1675 |
fun StoreHWord (mem, address, data) |
| 1676 |
= let |
| 1677 |
val aligned_address |
| 1678 |
= if address = AlignHWAddress address |
| 1679 |
then address |
| 1680 |
else (print "Error SH: Memory using aligned address\n"; |
| 1681 |
AlignWAddress address); |
| 1682 |
val (_, s_word) = Load(mem, aligned_address); |
| 1683 |
in |
| 1684 |
case aligned_address |
| 1685 |
of 0wx00000000 : Word32.word |
| 1686 |
=> Store(mem, aligned_address, |
| 1687 |
Word32.orb(Word32.andb(0wxFFFF0000 : Word32.word, |
| 1688 |
s_word), |
| 1689 |
Word32.<<(Word32.andb(0wx0000FFFF : |
| 1690 |
Word32.word, |
| 1691 |
data), |
| 1692 |
0wx0000))) |
| 1693 |
| 0wx00000010 : Word32.word |
| 1694 |
=> Store(mem, aligned_address, |
| 1695 |
Word32.orb(Word32.andb(0wx0000FFFF : Word32.word, |
| 1696 |
s_word), |
| 1697 |
Word32.<<(Word32.andb(0wx0000FFFF : |
| 1698 |
Word32.word, |
| 1699 |
data), |
| 1700 |
0wx0010))) |
| 1701 |
| _ => (print "Error SH: Memory unchanged\n"; |
| 1702 |
mem) |
| 1703 |
end; |
| 1704 |
|
| 1705 |
fun LoadByte (mem, address) |
| 1706 |
= let |
| 1707 |
val aligned_address = address; |
| 1708 |
val (nmem, l_word) = Load(mem, aligned_address); |
| 1709 |
in |
| 1710 |
(nmem, |
| 1711 |
case aligned_address |
| 1712 |
of 0wx00000000 : Word32.word |
| 1713 |
=> Word32.~>>(Word32.<<(l_word, |
| 1714 |
0wx0018), |
| 1715 |
0wx0018) |
| 1716 |
| 0wx00000008 : Word32.word |
| 1717 |
=> Word32.~>>(Word32.<<(l_word, |
| 1718 |
0wx0010), |
| 1719 |
0wx0018) |
| 1720 |
| 0wx00000010 : Word32.word |
| 1721 |
=> Word32.~>>(Word32.<<(l_word, |
| 1722 |
0wx0008), |
| 1723 |
0wx0018) |
| 1724 |
| 0wx00000018 : Word32.word |
| 1725 |
=> Word32.~>>(Word32.<<(l_word, |
| 1726 |
0wx0000), |
| 1727 |
0wx0018) |
| 1728 |
| _ => (print "Error LB: Memory returning 0\n"; |
| 1729 |
0wx00000000 : Word32.word)) |
| 1730 |
end; |
| 1731 |
|
| 1732 |
fun LoadByteU (mem, address) |
| 1733 |
= let |
| 1734 |
val aligned_address = address; |
| 1735 |
val (nmem, l_word) = Load(mem, aligned_address); |
| 1736 |
in |
| 1737 |
(nmem, |
| 1738 |
case aligned_address |
| 1739 |
of 0wx00000000 : Word32.word |
| 1740 |
=> Word32.>>(Word32.<<(l_word, |
| 1741 |
0wx0018), |
| 1742 |
0wx0018) |
| 1743 |
| 0wx00000008 : Word32.word |
| 1744 |
=> Word32.>>(Word32.<<(l_word, |
| 1745 |
0wx0010), |
| 1746 |
0wx0018) |
| 1747 |
| 0wx00000010 : Word32.word |
| 1748 |
=> Word32.>>(Word32.<<(l_word, |
| 1749 |
0wx0008), |
| 1750 |
0wx0018) |
| 1751 |
| 0wx00000018 : Word32.word |
| 1752 |
=> Word32.>>(Word32.<<(l_word, |
| 1753 |
0wx0000), |
| 1754 |
0wx0018) |
| 1755 |
| _ => (print "Error LBU: Memory returning 0\n"; |
| 1756 |
0wx00000000 : Word32.word)) |
| 1757 |
end; |
| 1758 |
|
| 1759 |
fun StoreByte (mem, address, data) |
| 1760 |
= let |
| 1761 |
val aligned_address = address; |
| 1762 |
val (_, s_word) = Load(mem, aligned_address); |
| 1763 |
in |
| 1764 |
case aligned_address |
| 1765 |
of 0wx00000000 : Word32.word |
| 1766 |
=> Store(mem, aligned_address, |
| 1767 |
Word32.orb(Word32.andb(0wxFFFFFF00 : Word32.word, |
| 1768 |
s_word), |
| 1769 |
Word32.<<(Word32.andb(0wx000000FF : |
| 1770 |
Word32.word, |
| 1771 |
data), |
| 1772 |
0wx0000))) |
| 1773 |
| 0wx00000008 : Word32.word |
| 1774 |
=> Store(mem, aligned_address, |
| 1775 |
Word32.orb(Word32.andb(0wxFFFF00FF : Word32.word, |
| 1776 |
s_word), |
| 1777 |
Word32.<<(Word32.andb(0wx000000FF : |
| 1778 |
Word32.word, |
| 1779 |
data), |
| 1780 |
0wx0008))) |
| 1781 |
| 0wx00000010 : Word32.word |
| 1782 |
=> Store(mem, aligned_address, |
| 1783 |
Word32.orb(Word32.andb(0wxFF00FFFF : Word32.word, |
| 1784 |
s_word), |
| 1785 |
Word32.<<(Word32.andb(0wx000000FF : |
| 1786 |
Word32.word, |
| 1787 |
data), |
| 1788 |
0wx0010))) |
| 1789 |
| 0wx00000018 : Word32.word |
| 1790 |
=> Store(mem, aligned_address, |
| 1791 |
Word32.orb(Word32.andb(0wx00FFFFFF : Word32.word, |
| 1792 |
s_word), |
| 1793 |
Word32.<<(Word32.andb(0wx000000FF : |
| 1794 |
Word32.word, |
| 1795 |
data), |
| 1796 |
0wx0018))) |
| 1797 |
| _ => (print "Error SB: Memory unchanged\n"; |
| 1798 |
mem) |
| 1799 |
end; |
| 1800 |
|
| 1801 |
fun GetStatistics ((cac, (rh, rm, wh, wm)), mem) |
| 1802 |
= let |
| 1803 |
|
| 1804 |
val th = rh + wh; |
| 1805 |
|
| 1806 |
val tm = rm + wm; |
| 1807 |
|
| 1808 |
val who = case WriteHit |
| 1809 |
of Write_Through => "Write Through" |
| 1810 |
| Write_Back => "Write Back"; |
| 1811 |
|
| 1812 |
val wmo = case WriteMiss |
| 1813 |
of Write_Allocate => "Write Allocate" |
| 1814 |
| Write_No_Allocate => "Write No Allocate"; |
| 1815 |
|
| 1816 |
in |
| 1817 |
CacheName ^ " :\n" ^ |
| 1818 |
"CacheSize : " ^ (Int.toString CacheSize) ^ "\n" ^ |
| 1819 |
"BlockSize : " ^ (Int.toString BlockSize) ^ "\n" ^ |
| 1820 |
"Associativity : " ^ (Int.toString Associativity) ^ "\n" ^ |
| 1821 |
"Write Hit : " ^ who ^ "\n" ^ |
| 1822 |
"Write Miss : " ^ wmo ^ "\n" ^ |
| 1823 |
"Read hits : " ^ (Int.toString rh) ^ "\n" ^ |
| 1824 |
"Read misses : " ^ (Int.toString rm) ^ "\n" ^ |
| 1825 |
"Write hits : " ^ (Int.toString wh) ^ "\n" ^ |
| 1826 |
"Write misses : " ^ (Int.toString wm) ^ "\n" ^ |
| 1827 |
"Total hits : " ^ (Int.toString th) ^ "\n" ^ |
| 1828 |
"Total misses : " ^ (Int.toString tm) ^ "\n" ^ |
| 1829 |
(MEM.GetStatistics mem) |
| 1830 |
end; |
| 1831 |
|
| 1832 |
end; |
| 1833 |
|
| 1834 |
(*****************************************************************************) |
| 1835 |
|
| 1836 |
(* |
| 1837 |
* DLXSimulator.sig |
| 1838 |
* |
| 1839 |
* This defines the exported function provided by the DLXSimulator. |
| 1840 |
* The function run_file takes a string corresponding to the name of the |
| 1841 |
* file to be run, and executes it. The function run_prog takes a |
| 1842 |
* list of instructions and executes them. |
| 1843 |
*) |
| 1844 |
|
| 1845 |
signature DLXSIMULATOR |
| 1846 |
= sig |
| 1847 |
|
| 1848 |
val run_file : string -> unit; |
| 1849 |
val run_prog : {instructions: string list, |
| 1850 |
trap: {inputFn: {state: 'state} -> {input: int, state: 'state}, |
| 1851 |
outputFn: {output: int, state: 'state} -> {state: 'state}, |
| 1852 |
state: 'state}} -> |
| 1853 |
{state: 'state, |
| 1854 |
statistics: unit -> string} |
| 1855 |
|
| 1856 |
end; |
| 1857 |
|
| 1858 |
(*****************************************************************************) |
| 1859 |
|
| 1860 |
(* |
| 1861 |
* DLXSimulator.sml |
| 1862 |
* |
| 1863 |
* This defines the DLXSimulatorFun functor, which takes three |
| 1864 |
* structures, corresponding to the register file, the ALU, and memory, |
| 1865 |
* and provides the functionality of a DLX processor, able to execute |
| 1866 |
* DLX programs. The function run_file takes a string corresponding to the |
| 1867 |
* name of the file to be executed, and executes it. The function |
| 1868 |
* run_prog takes a list of instructions and executes them. |
| 1869 |
*) |
| 1870 |
|
| 1871 |
functor DLXSimulatorFun (structure RF : REGISTERFILE; |
| 1872 |
structure ALU : ALU; |
| 1873 |
structure MEM : MEMORY; ) : DLXSIMULATOR |
| 1874 |
= struct |
| 1875 |
|
| 1876 |
(* |
| 1877 |
* The datatype Opcode provides a means of differentiating * |
| 1878 |
* among the main opcodes. |
| 1879 |
*) |
| 1880 |
datatype Opcode = |
| 1881 |
(* for R-type opcodes *) |
| 1882 |
SPECIAL | |
| 1883 |
(* I-type opcodes *) |
| 1884 |
BEQZ | BNEZ | |
| 1885 |
ADDI | ADDUI | SUBI | SUBUI | |
| 1886 |
ANDI | ORI | XORI | |
| 1887 |
LHI | |
| 1888 |
SLLI | SRLI | SRAI | |
| 1889 |
SEQI | SNEI | SLTI | SGTI | SLEI | SGEI | |
| 1890 |
LB | LBU | SB | |
| 1891 |
LH | LHU | SH | |
| 1892 |
LW | SW | |
| 1893 |
(* J-type opcodes *) |
| 1894 |
J | JAL | TRAP | JR | JALR | |
| 1895 |
(* Unrecognized opcode *) |
| 1896 |
NON_OP; |
| 1897 |
|
| 1898 |
(* |
| 1899 |
* The datatype RRFuncCode provides a means of |
| 1900 |
* differentiating among |
| 1901 |
* the register-register function codes. |
| 1902 |
*) |
| 1903 |
datatype RRFunctCode = NOP | SLL | SRL | SRA | |
| 1904 |
ADD | ADDU | SUB | SUBU | |
| 1905 |
AND | OR | XOR | |
| 1906 |
SEQ | SNE | SLT | SGT | SLE | SGE | |
| 1907 |
NON_FUNCT; |
| 1908 |
|
| 1909 |
(* |
| 1910 |
* The datatype Instruction provides a means of |
| 1911 |
* differentiating among the three different types of |
| 1912 |
* instructions, I-type, R-type, and J-type. |
| 1913 |
* An I-type is interpreted as (opcode, rs1, rd, immediate). |
| 1914 |
* An R-type is interpreted as (opcode, rs1, rs2, rd, shamt, funct). |
| 1915 |
* An J-type is interpreted as (opcode, offset). |
| 1916 |
* An ILLEGAL causes the simulator to end. |
| 1917 |
*) |
| 1918 |
datatype Instruction |
| 1919 |
= ITYPE of Opcode * int * int * Word32.word |
| 1920 |
| RTYPE of Opcode * int * int * int * int * RRFunctCode |
| 1921 |
| JTYPE of Opcode * Word32.word |
| 1922 |
| ILLEGAL; |
| 1923 |
|
| 1924 |
(* |
| 1925 |
* The value HALT is set to the DLX instruction TRAP #0, |
| 1926 |
* and is used to check for the halt of the program. |
| 1927 |
*) |
| 1928 |
val HALT = JTYPE (TRAP, 0wx00000000); |
| 1929 |
|
| 1930 |
(* |
| 1931 |
* The function DecodeIType decodes a Word32.word into an |
| 1932 |
* I-type instruction. |
| 1933 |
*) |
| 1934 |
fun DecodeIType instr |
| 1935 |
= let |
| 1936 |
val opc = Word32.andb (Word32.>> (instr, |
| 1937 |
0wx001A), |
| 1938 |
0wx0000003F : Word32.word); |
| 1939 |
|
| 1940 |
val opcode = case opc |
| 1941 |
of 0wx00000004 : Word32.word => BEQZ |
| 1942 |
| 0wx00000005 : Word32.word => BNEZ |
| 1943 |
| 0wx00000008 : Word32.word => ADDI |
| 1944 |
| 0wx00000009 : Word32.word => ADDUI |
| 1945 |
| 0wx0000000A : Word32.word => SUBI |
| 1946 |
| 0wx0000000B : Word32.word => SUBUI |
| 1947 |
| 0wx0000000C : Word32.word => ANDI |
| 1948 |
| 0wx0000000D : Word32.word => ORI |
| 1949 |
| 0wx0000000E : Word32.word => XORI |
| 1950 |
| 0wx0000000F : Word32.word => LHI |
| 1951 |
| 0wx00000014 : Word32.word => SLLI |
| 1952 |
| 0wx00000016 : Word32.word => SRLI |
| 1953 |
| 0wx00000017 : Word32.word => SRAI |
| 1954 |
| 0wx00000018 : Word32.word => SEQI |
| 1955 |
| 0wx00000019 : Word32.word => SNEI |
| 1956 |
| 0wx0000001A : Word32.word => SLTI |
| 1957 |
| 0wx0000001B : Word32.word => SGTI |
| 1958 |
| 0wx0000001C : Word32.word => SLEI |
| 1959 |
| 0wx0000001D : Word32.word => SGEI |
| 1960 |
| 0wx00000020 : Word32.word => LB |
| 1961 |
| 0wx00000024 : Word32.word => LBU |
| 1962 |
| 0wx00000028 : Word32.word => SB |
| 1963 |
| 0wx00000021 : Word32.word => LH |
| 1964 |
| 0wx00000025 : Word32.word => LHU |
| 1965 |
| 0wx00000029 : Word32.word => SH |
| 1966 |
| 0wx00000023 : Word32.word => LW |
| 1967 |
| 0wx0000002B : Word32.word => SW |
| 1968 |
| _ => (print "Error : Non I-Type opcode\n"; |
| 1969 |
NON_OP); |
| 1970 |
|
| 1971 |
val rs1 = Word32.toInt(Word32.andb (Word32.>> (instr, 0wx0015), |
| 1972 |
0wx0000001F : Word32.word)); |
| 1973 |
|
| 1974 |
val rd = Word32.toInt(Word32.andb (Word32.>> (instr, 0wx0010), |
| 1975 |
0wx0000001F : Word32.word)); |
| 1976 |
|
| 1977 |
val immediate = Word32.~>> (Word32.<< (instr, 0wx0010), |
| 1978 |
0wx0010); |
| 1979 |
|
| 1980 |
in |
| 1981 |
if opcode = NON_OP |
| 1982 |
then ILLEGAL |
| 1983 |
else ITYPE (opcode, rs1, rd, immediate) |
| 1984 |
end; |
| 1985 |
|
| 1986 |
(* |
| 1987 |
* The function DecodeRType decodes a Word32.word into an |
| 1988 |
* R-type instruction. |
| 1989 |
*) |
| 1990 |
fun DecodeRType instr |
| 1991 |
= let |
| 1992 |
|
| 1993 |
val rs1 = Word32.toInt (Word32.andb (Word32.>> (instr, 0wx0015), |
| 1994 |
0wx0000001F : Word32.word)); |
| 1995 |
|
| 1996 |
val rs2 = Word32.toInt (Word32.andb (Word32.>> (instr, 0wx0010), |
| 1997 |
0wx0000001F : Word32.word)); |
| 1998 |
|
| 1999 |
val rd = Word32.toInt (Word32.andb (Word32.>> (instr, 0wx000B), |
| 2000 |
0wx0000001F : Word32.word)); |
| 2001 |
|
| 2002 |
val shamt |
| 2003 |
= Word32.toInt (Word32.andb (Word32.>> (instr, 0wx0006), |
| 2004 |
0wx0000001F : Word32.word)); |
| 2005 |
|
| 2006 |
val funct = Word32.andb (instr, 0wx0000003F : Word32.word); |
| 2007 |
|
| 2008 |
val functcode = case funct |
| 2009 |
of 0wx00000000 : Word32.word => NOP |
| 2010 |
| 0wx00000004 : Word32.word => SLL |
| 2011 |
| 0wx00000006 : Word32.word => SRL |
| 2012 |
| 0wx00000007 : Word32.word => SRA |
| 2013 |
| 0wx00000020 : Word32.word => ADD |
| 2014 |
| 0wx00000021 : Word32.word => ADDU |
| 2015 |
| 0wx00000022 : Word32.word => SUB |
| 2016 |
| 0wx00000023 : Word32.word => SUBU |
| 2017 |
| 0wx00000024 : Word32.word => AND |
| 2018 |
| 0wx00000025 : Word32.word => OR |
| 2019 |
| 0wx00000026 : Word32.word => XOR |
| 2020 |
| 0wx00000028 : Word32.word => SEQ |
| 2021 |
| 0wx00000029 : Word32.word => SNE |
| 2022 |
| 0wx0000002A : Word32.word => SLT |
| 2023 |
| 0wx0000002B : Word32.word => SGT |
| 2024 |
| 0wx0000002C : Word32.word => SLE |
| 2025 |
| 0wx0000002D : Word32.word => SGE |
| 2026 |
| _ => (print "Error : Non R-type funct\n"; |
| 2027 |
NON_FUNCT); |
| 2028 |
|
| 2029 |
in |
| 2030 |
if functcode = NON_FUNCT |
| 2031 |
then ILLEGAL |
| 2032 |
else RTYPE (SPECIAL, rs1, rs2, rd, shamt, functcode) |
| 2033 |
end; |
| 2034 |
|
| 2035 |
(* |
| 2036 |
* The function DecodeJType decodes a Word32.word into an |
| 2037 |
* J-type instruction. |
| 2038 |
*) |
| 2039 |
fun DecodeJType instr |
| 2040 |
= let |
| 2041 |
|
| 2042 |
val opc = Word32.andb (Word32.>> (instr, 0wx1A), |
| 2043 |
0wx0000003F : Word32.word); |
| 2044 |
|
| 2045 |
val opcode = case opc |
| 2046 |
of 0wx00000002 : Word32.word => J |
| 2047 |
| 0wx00000003 : Word32.word => JAL |
| 2048 |
| 0wx00000011 : Word32.word => TRAP |
| 2049 |
| 0wx00000012 : Word32.word => JR |
| 2050 |
| 0wx00000013 : Word32.word => JALR |
| 2051 |
| _ => (print "Error : Non J-type opcode\n"; |
| 2052 |
NON_OP); |
| 2053 |
|
| 2054 |
val offset = Word32.~>> (Word32.<< (instr, 0wx0006), |
| 2055 |
0wx0006); |
| 2056 |
|
| 2057 |
in |
| 2058 |
if opcode = NON_OP |
| 2059 |
then ILLEGAL |
| 2060 |
else JTYPE (opcode, offset) |
| 2061 |
end; |
| 2062 |
|
| 2063 |
(* |
| 2064 |
* The function DecodeInstr decodes a Word32.word into an |
| 2065 |
* instruction. It first checks the opcode, and then calls |
| 2066 |
* one of DecodeIType, DecodeJType, and DecodeRType to |
| 2067 |
* complete the decoding process. |
| 2068 |
*) |
| 2069 |
fun DecodeInstr instr |
| 2070 |
= let |
| 2071 |
|
| 2072 |
val opcode = Word32.andb (Word32.>> (instr, 0wx1A), |
| 2073 |
0wx0000003F : Word32.word); |
| 2074 |
|
| 2075 |
in |
| 2076 |
case opcode |
| 2077 |
of 0wx00000000 : Word32.word => DecodeRType instr |
| 2078 |
| 0wx00000002 : Word32.word => DecodeJType instr |
| 2079 |
| 0wx00000003 : Word32.word => DecodeJType instr |
| 2080 |
| 0wx00000004 : Word32.word => DecodeIType instr |
| 2081 |
| 0wx00000005 : Word32.word => DecodeIType instr |
| 2082 |
| 0wx00000008 : Word32.word => DecodeIType instr |
| 2083 |
| 0wx00000009 : Word32.word => DecodeIType instr |
| 2084 |
| 0wx0000000A : Word32.word => DecodeIType instr |
| 2085 |
| 0wx0000000B : Word32.word => DecodeIType instr |
| 2086 |
| 0wx0000000C : Word32.word => DecodeIType instr |
| 2087 |
| 0wx0000000D : Word32.word => DecodeIType instr |
| 2088 |
| 0wx0000000E : Word32.word => DecodeIType instr |
| 2089 |
| 0wx0000000F : Word32.word => DecodeIType instr |
| 2090 |
| 0wx00000011 : Word32.word => DecodeJType instr |
| 2091 |
| 0wx00000012 : Word32.word => DecodeJType instr |
| 2092 |
| 0wx00000013 : Word32.word => DecodeJType instr |
| 2093 |
| 0wx00000016 : Word32.word => DecodeIType instr |
| 2094 |
| 0wx00000017 : Word32.word => DecodeIType instr |
| 2095 |
| 0wx00000018 : Word32.word => DecodeIType instr |
| 2096 |
| 0wx00000019 : Word32.word => DecodeIType instr |
| 2097 |
| 0wx0000001A : Word32.word => DecodeIType instr |
| 2098 |
| 0wx0000001B : Word32.word => DecodeIType instr |
| 2099 |
| 0wx0000001C : Word32.word => DecodeIType instr |
| 2100 |
| 0wx0000001D : Word32.word => DecodeIType instr |
| 2101 |
| 0wx00000020 : Word32.word => DecodeIType instr |
| 2102 |
| 0wx00000024 : Word32.word => DecodeIType instr |
| 2103 |
| 0wx00000028 : Word32.word => DecodeIType instr |
| 2104 |
| 0wx00000021 : Word32.word => DecodeIType instr |
| 2105 |
| 0wx00000025 : Word32.word => DecodeIType instr |
| 2106 |
| 0wx00000029 : Word32.word => DecodeIType instr |
| 2107 |
| 0wx00000023 : Word32.word => DecodeIType instr |
| 2108 |
| 0wx0000002B : Word32.word => DecodeIType instr |
| 2109 |
| _ => (print "Error : Unrecognized opcode\n"; |
| 2110 |
ILLEGAL) |
| 2111 |
end; |
| 2112 |
|
| 2113 |
|
| 2114 |
(* |
| 2115 |
* The function PerformIType performs one of the I-Type |
| 2116 |
* instructions. A number of the instructions make use of the |
| 2117 |
* ALU, and as such, call ALU.PerformAL. |
| 2118 |
*) |
| 2119 |
fun PerformIType ((BEQZ, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2120 |
= if (RF.LoadRegister(rf, rs1) = (0wx00000000 : Word32.word)) |
| 2121 |
then (Word32.fromInt (Int.+ (Word32.toIntX PC, |
| 2122 |
Word32.toIntX |
| 2123 |
(Word32.<< (immediate, |
| 2124 |
0wx0002)))), |
| 2125 |
rf, mem, trap) |
| 2126 |
else (PC, rf, mem, trap) |
| 2127 |
|
| 2128 |
| PerformIType ((BNEZ, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2129 |
= if not (RF.LoadRegister(rf, rs1) = (0wx00000000 : Word32.word)) |
| 2130 |
then (Word32.fromInt (Int.+ (Word32.toIntX PC, |
| 2131 |
Word32.toIntX |
| 2132 |
(Word32.<< (immediate, |
| 2133 |
0wx0002)))), |
| 2134 |
rf, mem, trap) |
| 2135 |
else (PC, rf, mem, trap) |
| 2136 |
|
| 2137 |
| PerformIType ((ADDI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2138 |
= (PC, |
| 2139 |
RF.StoreRegister(rf, rd, |
| 2140 |
ALU.PerformAL(ALU.ADD, |
| 2141 |
RF.LoadRegister(rf, rs1), |
| 2142 |
immediate)), |
| 2143 |
mem, trap) |
| 2144 |
|
| 2145 |
| PerformIType ((ADDUI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2146 |
= (PC, |
| 2147 |
RF.StoreRegister(rf, rd, |
| 2148 |
ALU.PerformAL(ALU.ADDU, |
| 2149 |
RF.LoadRegister(rf, rs1), |
| 2150 |
immediate)), |
| 2151 |
mem, trap) |
| 2152 |
|
| 2153 |
| PerformIType ((SUBI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2154 |
= (PC, |
| 2155 |
RF.StoreRegister(rf, rd, |
| 2156 |
ALU.PerformAL(ALU.SUB, |
| 2157 |
RF.LoadRegister(rf, rs1), |
| 2158 |
immediate)), |
| 2159 |
mem, trap) |
| 2160 |
|
| 2161 |
| PerformIType ((SUBUI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2162 |
= (PC, |
| 2163 |
RF.StoreRegister(rf, rd, |
| 2164 |
ALU.PerformAL(ALU.SUBU, |
| 2165 |
RF.LoadRegister(rf, rs1), |
| 2166 |
immediate)), |
| 2167 |
mem, trap) |
| 2168 |
|
| 2169 |
| PerformIType ((ANDI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2170 |
= (PC, |
| 2171 |
RF.StoreRegister(rf, rd, |
| 2172 |
ALU.PerformAL(ALU.AND, |
| 2173 |
RF.LoadRegister(rf, rs1), |
| 2174 |
immediate)), |
| 2175 |
mem, trap) |
| 2176 |
|
| 2177 |
| PerformIType ((ORI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2178 |
= (PC, |
| 2179 |
RF.StoreRegister(rf, rd, |
| 2180 |
ALU.PerformAL(ALU.OR, |
| 2181 |
RF.LoadRegister(rf, rs1), |
| 2182 |
immediate)), |
| 2183 |
mem, trap) |
| 2184 |
|
| 2185 |
| PerformIType ((XORI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2186 |
= (PC, |
| 2187 |
RF.StoreRegister(rf, rd, |
| 2188 |
ALU.PerformAL(ALU.XOR, |
| 2189 |
RF.LoadRegister(rf, rs1), |
| 2190 |
immediate)), |
| 2191 |
mem, trap) |
| 2192 |
|
| 2193 |
| PerformIType ((LHI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2194 |
= (PC, RF.StoreRegister(rf, rd, Word32.<< (immediate, 0wx0010)), mem, trap) |
| 2195 |
|
| 2196 |
| PerformIType ((SLLI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2197 |
= (PC, RF.StoreRegister(rf, rd, |
| 2198 |
Word32.<< (RF.LoadRegister(rf, rs1), |
| 2199 |
Word.fromLarge (Word32.toLarge immediate))), |
| 2200 |
mem, trap) |
| 2201 |
|
| 2202 |
| PerformIType ((SRLI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2203 |
= (PC, RF.StoreRegister(rf, rd, |
| 2204 |
Word32.>> (RF.LoadRegister(rf, rs1), |
| 2205 |
Word.fromLarge (Word32.toLarge immediate))), |
| 2206 |
mem, trap) |
| 2207 |
|
| 2208 |
| PerformIType ((SRAI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2209 |
= (PC, RF.StoreRegister(rf, rd, |
| 2210 |
Word32.~>> (RF.LoadRegister(rf, rs1), |
| 2211 |
Word.fromLarge (Word32.toLarge immediate))), |
| 2212 |
mem, trap) |
| 2213 |
|
| 2214 |
| PerformIType ((SEQI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2215 |
= (PC, |
| 2216 |
RF.StoreRegister(rf, rd, |
| 2217 |
ALU.PerformAL(ALU.SEQ, |
| 2218 |
RF.LoadRegister(rf, rs1), |
| 2219 |
immediate)), |
| 2220 |
mem, trap) |
| 2221 |
|
| 2222 |
| PerformIType ((SNEI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2223 |
= (PC, |
| 2224 |
RF.StoreRegister(rf, rd, |
| 2225 |
ALU.PerformAL(ALU.SNE, |
| 2226 |
RF.LoadRegister(rf, rs1), |
| 2227 |
immediate)), |
| 2228 |
mem, trap) |
| 2229 |
|
| 2230 |
| PerformIType ((SLTI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2231 |
= (PC, |
| 2232 |
RF.StoreRegister(rf, rd, |
| 2233 |
ALU.PerformAL(ALU.SLT, |
| 2234 |
RF.LoadRegister(rf, rs1), |
| 2235 |
immediate)), |
| 2236 |
mem, trap) |
| 2237 |
|
| 2238 |
| PerformIType ((SGTI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2239 |
= (PC, |
| 2240 |
RF.StoreRegister(rf, rd, |
| 2241 |
ALU.PerformAL(ALU.SGT, |
| 2242 |
RF.LoadRegister(rf, rs1), |
| 2243 |
immediate)), |
| 2244 |
mem, trap) |
| 2245 |
|
| 2246 |
| PerformIType ((SLEI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2247 |
= (PC, |
| 2248 |
RF.StoreRegister(rf, rd, |
| 2249 |
ALU.PerformAL(ALU.SLE, |
| 2250 |
RF.LoadRegister(rf, rs1), |
| 2251 |
immediate)), |
| 2252 |
mem, trap) |
| 2253 |
|
| 2254 |
| PerformIType ((SGEI, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2255 |
= (PC, |
| 2256 |
RF.StoreRegister(rf, rd, |
| 2257 |
ALU.PerformAL(ALU.SGE, |
| 2258 |
RF.LoadRegister(rf, rs1), |
| 2259 |
immediate)), |
| 2260 |
mem, trap) |
| 2261 |
|
| 2262 |
| PerformIType ((LB, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2263 |
= let |
| 2264 |
val (nmem, l_byte) |
| 2265 |
= MEM.LoadByte(mem, Word32.+ (RF.LoadRegister(rf, rs1), |
| 2266 |
immediate)); |
| 2267 |
in |
| 2268 |
(PC, |
| 2269 |
RF.StoreRegister(rf, rd, l_byte), |
| 2270 |
nmem, trap) |
| 2271 |
end |
| 2272 |
|
| 2273 |
| PerformIType ((LBU, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2274 |
= let |
| 2275 |
val (nmem, l_byte) |
| 2276 |
= MEM.LoadByteU(mem, Word32.+ (RF.LoadRegister(rf, rs1), |
| 2277 |
immediate)); |
| 2278 |
in |
| 2279 |
(PC, |
| 2280 |
RF.StoreRegister(rf, rd, l_byte), |
| 2281 |
nmem, trap) |
| 2282 |
end |
| 2283 |
|
| 2284 |
| PerformIType ((SB, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2285 |
= (PC, |
| 2286 |
rf, |
| 2287 |
MEM.StoreByte(mem, |
| 2288 |
Word32.+ (RF.LoadRegister(rf, rs1), immediate), |
| 2289 |
Word32.andb(0wx000000FF, RF.LoadRegister(rf, rd))), |
| 2290 |
trap) |
| 2291 |
|
| 2292 |
| PerformIType ((LH, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2293 |
= let |
| 2294 |
val (nmem, l_hword) |
| 2295 |
= MEM.LoadHWord(mem, Word32.+ (RF.LoadRegister(rf, rs1), |
| 2296 |
immediate)); |
| 2297 |
in |
| 2298 |
(PC, |
| 2299 |
RF.StoreRegister(rf, rd, l_hword), |
| 2300 |
nmem, trap) |
| 2301 |
end |
| 2302 |
|
| 2303 |
| PerformIType ((LHU, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2304 |
= let |
| 2305 |
val (nmem, l_hword) |
| 2306 |
= MEM.LoadHWordU(mem, Word32.+ (RF.LoadRegister(rf, rs1), |
| 2307 |
immediate)); |
| 2308 |
in |
| 2309 |
(PC, |
| 2310 |
RF.StoreRegister(rf, rd, l_hword), |
| 2311 |
nmem, trap) |
| 2312 |
end |
| 2313 |
|
| 2314 |
| PerformIType ((SH, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2315 |
= (PC, |
| 2316 |
rf, |
| 2317 |
MEM.StoreByte(mem, |
| 2318 |
Word32.+ (RF.LoadRegister(rf, rs1), immediate), |
| 2319 |
Word32.andb(0wx0000FFFF, RF.LoadRegister(rf, rd))), |
| 2320 |
trap) |
| 2321 |
|
| 2322 |
|
| 2323 |
| PerformIType ((LW, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2324 |
= let |
| 2325 |
val (nmem, l_word) |
| 2326 |
= MEM.LoadWord(mem, Word32.+ (RF.LoadRegister(rf, rs1), |
| 2327 |
immediate)); |
| 2328 |
in |
| 2329 |
(PC, |
| 2330 |
RF.StoreRegister(rf, rd, l_word), |
| 2331 |
nmem, trap) |
| 2332 |
end |
| 2333 |
|
| 2334 |
| PerformIType ((SW, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2335 |
= (PC, |
| 2336 |
rf, |
| 2337 |
MEM.StoreWord(mem, |
| 2338 |
Word32.+ (RF.LoadRegister(rf, rs1), immediate), |
| 2339 |
RF.LoadRegister(rf, rd)), |
| 2340 |
trap) |
| 2341 |
|
| 2342 |
| PerformIType ((_, rs1, rd, immediate), (PC, rf, mem, trap)) |
| 2343 |
= (print "Error : Non I-Type opcode, performing NOP\n"; |
| 2344 |
(PC, rf, mem, trap)); |
| 2345 |
|
| 2346 |
|
| 2347 |
(* |
| 2348 |
* The function PerformRType performs one of the R-Type |
| 2349 |
* instructions. All of the instructions make use of the |
| 2350 |
* ALU, and as such, call ALU.PerformAL. |
| 2351 |
*) |
| 2352 |
fun PerformRType ((SPECIA, rs1, rs2, rd, shamt, NOP), (PC, rf, mem, trap)) |
| 2353 |
= (PC, rf, mem, trap) |
| 2354 |
|
| 2355 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SLL), (PC, rf, mem, trap)) |
| 2356 |
= (PC, |
| 2357 |
RF.StoreRegister(rf, rd, |
| 2358 |
ALU.PerformAL(ALU.SLL, |
| 2359 |
RF.LoadRegister(rf, rs1), |
| 2360 |
RF.LoadRegister(rf, rs2))), |
| 2361 |
mem, trap) |
| 2362 |
|
| 2363 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SRL), (PC, rf, mem, trap)) |
| 2364 |
= (PC, |
| 2365 |
RF.StoreRegister(rf, rd, |
| 2366 |
ALU.PerformAL(ALU.SRL, |
| 2367 |
RF.LoadRegister(rf, rs1), |
| 2368 |
RF.LoadRegister(rf, rs2))), |
| 2369 |
mem, trap) |
| 2370 |
|
| 2371 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SRA), (PC, rf, mem, trap)) |
| 2372 |
= (PC, |
| 2373 |
RF.StoreRegister(rf, rd, |
| 2374 |
ALU.PerformAL(ALU.SRA, |
| 2375 |
RF.LoadRegister(rf, rs1), |
| 2376 |
RF.LoadRegister(rf, rs2))), |
| 2377 |
mem, trap) |
| 2378 |
|
| 2379 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, ADD), (PC, rf, mem, trap)) |
| 2380 |
= (PC, |
| 2381 |
RF.StoreRegister(rf, rd, |
| 2382 |
ALU.PerformAL(ALU.ADD, |
| 2383 |
RF.LoadRegister(rf, rs1), |
| 2384 |
RF.LoadRegister(rf, rs2))), |
| 2385 |
mem, trap) |
| 2386 |
|
| 2387 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, ADDU), (PC, rf, mem, trap)) |
| 2388 |
= (PC, |
| 2389 |
RF.StoreRegister(rf, rd, |
| 2390 |
ALU.PerformAL(ALU.ADDU, |
| 2391 |
RF.LoadRegister(rf, rs1), |
| 2392 |
RF.LoadRegister(rf, rs2))), |
| 2393 |
mem, trap) |
| 2394 |
|
| 2395 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SUB), (PC, rf, mem, trap)) |
| 2396 |
= (PC, |
| 2397 |
RF.StoreRegister(rf, rd, |
| 2398 |
ALU.PerformAL(ALU.SUB, |
| 2399 |
RF.LoadRegister(rf, rs1), |
| 2400 |
RF.LoadRegister(rf, rs2))), |
| 2401 |
mem, trap) |
| 2402 |
|
| 2403 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SUBU), (PC, rf, mem, trap)) |
| 2404 |
= (PC, |
| 2405 |
RF.StoreRegister(rf, rd, |
| 2406 |
ALU.PerformAL(ALU.SUBU, |
| 2407 |
RF.LoadRegister(rf, rs1), |
| 2408 |
RF.LoadRegister(rf, rs2))), |
| 2409 |
mem, trap) |
| 2410 |
|
| 2411 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, AND), (PC, rf, mem, trap)) |
| 2412 |
= (PC, |
| 2413 |
RF.StoreRegister(rf, rd, |
| 2414 |
ALU.PerformAL(ALU.AND, |
| 2415 |
RF.LoadRegister(rf, rs1), |
| 2416 |
RF.LoadRegister(rf, rs2))), |
| 2417 |
mem, trap) |
| 2418 |
|
| 2419 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, OR), (PC, rf, mem, trap)) |
| 2420 |
= (PC, |
| 2421 |
RF.StoreRegister(rf, rd, |
| 2422 |
ALU.PerformAL(ALU.OR, |
| 2423 |
RF.LoadRegister(rf, rs1), |
| 2424 |
RF.LoadRegister(rf, rs2))), |
| 2425 |
mem, trap) |
| 2426 |
|
| 2427 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, XOR), (PC, rf, mem, trap)) |
| 2428 |
= (PC, |
| 2429 |
RF.StoreRegister(rf, rd, |
| 2430 |
ALU.PerformAL(ALU.XOR, |
| 2431 |
RF.LoadRegister(rf, rs1), |
| 2432 |
RF.LoadRegister(rf, rs2))), |
| 2433 |
mem, trap) |
| 2434 |
|
| 2435 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SEQ), (PC, rf, mem, trap)) |
| 2436 |
= (PC, |
| 2437 |
RF.StoreRegister(rf, rd, |
| 2438 |
ALU.PerformAL(ALU.SEQ, |
| 2439 |
RF.LoadRegister(rf, rs1), |
| 2440 |
RF.LoadRegister(rf, rs2))), |
| 2441 |
mem, trap) |
| 2442 |
|
| 2443 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SNE), (PC, rf, mem, trap)) |
| 2444 |
= (PC, |
| 2445 |
RF.StoreRegister(rf, rd, |
| 2446 |
ALU.PerformAL(ALU.SNE, |
| 2447 |
RF.LoadRegister(rf, rs1), |
| 2448 |
RF.LoadRegister(rf, rs2))), |
| 2449 |
mem, trap) |
| 2450 |
|
| 2451 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SLT), (PC, rf, mem, trap)) |
| 2452 |
= (PC, |
| 2453 |
RF.StoreRegister(rf, rd, |
| 2454 |
ALU.PerformAL(ALU.SLT, |
| 2455 |
RF.LoadRegister(rf, rs1), |
| 2456 |
RF.LoadRegister(rf, rs2))), |
| 2457 |
mem, trap) |
| 2458 |
|
| 2459 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SGT), (PC, rf, mem, trap)) |
| 2460 |
= (PC, |
| 2461 |
RF.StoreRegister(rf, rd, |
| 2462 |
ALU.PerformAL(ALU.SGT, |
| 2463 |
RF.LoadRegister(rf, rs1), |
| 2464 |
RF.LoadRegister(rf, rs2))), |
| 2465 |
mem, trap) |
| 2466 |
|
| 2467 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SLE), (PC, rf, mem, trap)) |
| 2468 |
= (PC, |
| 2469 |
RF.StoreRegister(rf, rd, |
| 2470 |
ALU.PerformAL(ALU.SLE, |
| 2471 |
RF.LoadRegister(rf, rs1), |
| 2472 |
RF.LoadRegister(rf, rs2))), |
| 2473 |
mem, trap) |
| 2474 |
|
| 2475 |
| PerformRType ((SPECIAL, rs1, rs2, rd, shamt, SGE), (PC, rf, mem, trap)) |
| 2476 |
= (PC, |
| 2477 |
RF.StoreRegister(rf, rd, |
| 2478 |
ALU.PerformAL(ALU.SGE, |
| 2479 |
RF.LoadRegister(rf, rs1), |
| 2480 |
RF.LoadRegister(rf, rs2))), |
| 2481 |
mem, trap) |
| 2482 |
|
| 2483 |
| PerformRType ((_, rs1, rs2, rd, shamt, _), (PC, rf, mem, trap)) |
| 2484 |
= (print "Error : Non R-Type opcode, performing NOP\n"; |
| 2485 |
(PC, rf, mem, trap)); |
| 2486 |
|
| 2487 |
|
| 2488 |
(* |
| 2489 |
* The function PerformJType performs one of the J-Type |
| 2490 |
* instructions. |
| 2491 |
*) |
| 2492 |
fun PerformJType ((J, offset), (PC, rf, mem, trap)) |
| 2493 |
= (Word32.fromInt (Int.+ (Word32.toIntX PC, |
| 2494 |
Word32.toIntX |
| 2495 |
(Word32.<< (offset, 0wx0002)))), |
| 2496 |
rf, mem, trap) |
| 2497 |
|
| 2498 |
| PerformJType ((JR, offset), (PC, rf, mem, trap)) |
| 2499 |
= (RF.LoadRegister(rf, |
| 2500 |
Word32.toInt(Word32.andb (Word32.>> (offset, |
| 2501 |
0wx0015), |
| 2502 |
0wx0000001F : |
| 2503 |
Word32.word))), |
| 2504 |
rf, mem, trap) |
| 2505 |
|
| 2506 |
| PerformJType ((JAL, offset), (PC, rf, mem, trap)) |
| 2507 |
= (Word32.fromInt (Int.+ (Word32.toIntX PC, |
| 2508 |
Word32.toIntX |
| 2509 |
(Word32.<< (offset, 0wx0002)))), |
| 2510 |
RF.StoreRegister(rf, 31, PC), |
| 2511 |
mem, trap) |
| 2512 |
|
| 2513 |
| PerformJType ((JALR, offset), (PC, rf, mem, trap)) |
| 2514 |
= (RF.LoadRegister(rf, |
| 2515 |
Word32.toInt (Word32.andb (Word32.>> (offset, |
| 2516 |
0wx0015), |
| 2517 |
0wx0000001F : |
| 2518 |
Word32.word))), |
| 2519 |
RF.StoreRegister(rf, 31, PC), |
| 2520 |
mem, trap) |
| 2521 |
|
| 2522 |
| PerformJType ((TRAP, 0wx00000003 : Word32.word), (PC, rf, mem, trap)) |
| 2523 |
= let |
| 2524 |
val {inputFn, outputFn, state} = trap |
| 2525 |
val {input, state} = inputFn {state = state} |
| 2526 |
val trap = {inputFn = inputFn, |
| 2527 |
outputFn = outputFn, |
| 2528 |
state = state} |
| 2529 |
in |
| 2530 |
(PC, |
| 2531 |
RF.StoreRegister(rf, 14, Word32.fromInt input), |
| 2532 |
mem, |
| 2533 |
trap) |
| 2534 |
end |
| 2535 |
|
| 2536 |
| PerformJType ((TRAP, 0wx00000004 : Word32.word), (PC, rf, mem, trap)) |
| 2537 |
= let |
| 2538 |
val output = Word32.toIntX (RF.LoadRegister(rf, 14)); |
| 2539 |
val {inputFn, outputFn, state} = trap |
| 2540 |
val {state} = outputFn {output = output, state = state} |
| 2541 |
val trap = {inputFn = inputFn, |
| 2542 |
outputFn = outputFn, |
| 2543 |
state = state} |
| 2544 |
in |
| 2545 |
(PC, rf, mem, trap) |
| 2546 |
end |
| 2547 |
|
| 2548 |
| PerformJType ((_, offset), (PC, rf, mem, trap)) |
| 2549 |
= (print "Error : Non J-Type opcode, performing NOP\n"; |
| 2550 |
(PC, rf, mem, trap)); |
| 2551 |
|
| 2552 |
|
| 2553 |
(* |
| 2554 |
* The function PerformInstr performs an instruction by |
| 2555 |
* passing the instruction to the appropriate auxiliary function. |
| 2556 |
*) |
| 2557 |
fun PerformInstr (ITYPE instr, (PC, rf, mem, trap)) |
| 2558 |
= PerformIType (instr, (PC, rf, mem, trap)) |
| 2559 |
| PerformInstr (RTYPE instr, (PC, rf, mem, trap)) |
| 2560 |
= PerformRType (instr, (PC, rf, mem, trap)) |
| 2561 |
| PerformInstr (JTYPE instr, (PC, rf, mem, trap)) |
| 2562 |
= PerformJType (instr, (PC, rf, mem, trap)) |
| 2563 |
| PerformInstr (ILLEGAL, (PC, rf, mem, trap)) |
| 2564 |
= (PC, rf, mem, trap); |
| 2565 |
|
| 2566 |
|
| 2567 |
(* |
| 2568 |
* The function CycleLoop represents the basic clock cycle of |
| 2569 |
* the DLX processor. It takes as input the current program |
| 2570 |
* counter, the current register file, and the current memory. |
| 2571 |
* It loads, decodes, and executes an instruction and increments |
| 2572 |
* the program counter. If the instruction that was loaded is |
| 2573 |
* the HALT instruction, the program terminates, otherwise, |
| 2574 |
* CycleLoop is recursively called with the result of performing |
| 2575 |
* the instruction. |
| 2576 |
*) |
| 2577 |
fun CycleLoop (PC, rf, mem, trap) |
| 2578 |
= let |
| 2579 |
val (nmem, instr_word) = MEM.LoadWord (mem, PC); |
| 2580 |
val instr = DecodeInstr instr_word; |
| 2581 |
val nPC = Word32.+ (PC, 0wx00000004 : Word32.word); |
| 2582 |
in |
| 2583 |
if instr = HALT orelse instr = ILLEGAL |
| 2584 |
then (fn () => MEM.GetStatistics nmem, #state trap) |
| 2585 |
else CycleLoop (PerformInstr (instr, (nPC, rf, nmem, trap))) |
| 2586 |
end |
| 2587 |
|
| 2588 |
|
| 2589 |
(* |
| 2590 |
* The function LoadProgAux is an auxilary function that |
| 2591 |
* assists in loading a program into memory. It recursively |
| 2592 |
* calls itself, each time loading an instruction and incrementing |
| 2593 |
* the address to which the next instruction is to be loaded. |
| 2594 |
*) |
| 2595 |
fun LoadProgAux ([], mem, address) |
| 2596 |
= mem |
| 2597 |
| LoadProgAux (instrs::instr_list, mem, address) |
| 2598 |
= let |
| 2599 |
val instro = Word32.fromString instrs; |
| 2600 |
val instr = if isSome instro |
| 2601 |
then valOf instro |
| 2602 |
else (print ("Error : Invalid " ^ |
| 2603 |
"instruction format, " ^ |
| 2604 |
"returning NOP\n"); |
| 2605 |
0wx00000000 : Word32.word); |
| 2606 |
in |
| 2607 |
LoadProgAux (instr_list, |
| 2608 |
MEM.StoreWord (mem, address, instr), |
| 2609 |
Word32.+ (address, 0wx00000004 : Word32.word)) |
| 2610 |
end; |
| 2611 |
|
| 2612 |
(* |
| 2613 |
* The function LoadProg takes a list of instructions and memory, and |
| 2614 |
* loads the file into memory, beginning at 0x10000. |
| 2615 |
*) |
| 2616 |
fun LoadProg (instr_list, mem) |
| 2617 |
= LoadProgAux (instr_list, mem, 0wx00010000 : Word32.word); |
| 2618 |
|
| 2619 |
|
| 2620 |
(* |
| 2621 |
* The function ReadFileToInstr reads the sequence of |
| 2622 |
* instructions in a file into a list. |
| 2623 |
*) |
| 2624 |
fun ReadFileToInstr file |
| 2625 |
= (case TextIO.inputLine file of |
| 2626 |
NONE => [] |
| 2627 |
| SOME l => l :: (ReadFileToInstr file)); |
| 2628 |
|
| 2629 |
|
| 2630 |
(* |
| 2631 |
* The function run_prog is exported by DLXSimulator. |
| 2632 |
* It takes a list of instructions, then begins |
| 2633 |
* execution of the instructions loaded at 0x10000, with an |
| 2634 |
* initialized register file, and the loaded program in an |
| 2635 |
* initialised memory. |
| 2636 |
*) |
| 2637 |
fun run_prog {instructions, trap} |
| 2638 |
= let |
| 2639 |
val (statistics, state) |
| 2640 |
= CycleLoop (0wx00010000 : Word32.word, |
| 2641 |
RF.InitRegisterFile (), |
| 2642 |
LoadProg (instructions, MEM.InitMemory ()), |
| 2643 |
trap); |
| 2644 |
in |
| 2645 |
{state = state, |
| 2646 |
statistics = statistics} |
| 2647 |
end |
| 2648 |
|
| 2649 |
(* |
| 2650 |
* The function run_file is exported by DLXSimulator. |
| 2651 |
* It takes the name of a file to be run, then begins |
| 2652 |
* execution of the loaded program at 0x10000, with an |
| 2653 |
* initialized register file, and the loaded program in an |
| 2654 |
* initialized memory. |
| 2655 |
*) |
| 2656 |
fun run_file filename |
| 2657 |
= let |
| 2658 |
val instructions = ReadFileToInstr (TextIO.openIn filename) |
| 2659 |
val trap = let |
| 2660 |
fun inputFn {state = ()} = |
| 2661 |
let |
| 2662 |
val x = TextIO.print "Value? "; |
| 2663 |
val input = |
| 2664 |
case TextIO.inputLine TextIO.stdIn of |
| 2665 |
NONE => (TextIO.print "Error : Returning 0\n"; |
| 2666 |
Int.fromInt 0) |
| 2667 |
| SOME s => |
| 2668 |
(case Int.fromString s of |
| 2669 |
NONE => (TextIO.print "Error : Returning 0\n"; |
| 2670 |
Int.fromInt 0) |
| 2671 |
| SOME i => i) |
| 2672 |
in |
| 2673 |
{input = input, state = ()} |
| 2674 |
end |
| 2675 |
fun outputFn {output, state = ()} = |
| 2676 |
(TextIO.print ("Output: " ^ (Int.toString output) ^ "\n") |
| 2677 |
; {state = ()}) |
| 2678 |
in |
| 2679 |
{inputFn = inputFn, |
| 2680 |
outputFn = outputFn, |
| 2681 |
state = ()} |
| 2682 |
end |
| 2683 |
val {state, statistics} = run_prog {instructions = instructions, trap = trap} |
| 2684 |
in |
| 2685 |
print "Program halted.\n"; |
| 2686 |
print (statistics ()); |
| 2687 |
() |
| 2688 |
end |
| 2689 |
|
| 2690 |
end; |
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
(* ************************************************************************* *) |
| 2696 |
|
| 2697 |
(* |
| 2698 |
* Cache1.sml |
| 2699 |
* |
| 2700 |
* This file describes a small simple level 1 cache. |
| 2701 |
*) |
| 2702 |
|
| 2703 |
structure L1CacheSpec1 : CACHESPEC |
| 2704 |
= struct |
| 2705 |
|
| 2706 |
datatype WriteHitOption = Write_Through |
| 2707 |
| Write_Back; |
| 2708 |
|
| 2709 |
datatype WriteMissOption = Write_Allocate |
| 2710 |
| Write_No_Allocate; |
| 2711 |
|
| 2712 |
val CacheName = "Level 1 Cache"; |
| 2713 |
val CacheSize = 256; |
| 2714 |
val BlockSize = 4; |
| 2715 |
val Associativity = 2; |
| 2716 |
val WriteHit = Write_Through; |
| 2717 |
val WriteMiss = Write_No_Allocate; |
| 2718 |
|
| 2719 |
end; |
| 2720 |
|
| 2721 |
|
| 2722 |
structure L1Cache1 : MEMORY |
| 2723 |
= CachedMemory (structure CS = L1CacheSpec1; |
| 2724 |
structure MEM = Memory; ); |
| 2725 |
|
| 2726 |
|
| 2727 |
structure DLXSimulatorC1 : DLXSIMULATOR |
| 2728 |
= DLXSimulatorFun (structure RF = RegisterFile; |
| 2729 |
structure ALU = ALU; |
| 2730 |
structure MEM = L1Cache1; ); |
| 2731 |
|
| 2732 |
(* Example programs *) |
| 2733 |
|
| 2734 |
val Simple = ["200E002F", |
| 2735 |
"44000004", |
| 2736 |
"44000000", |
| 2737 |
"00000000"]; |
| 2738 |
|
| 2739 |
val Twos = ["44000003", |
| 2740 |
"00000000", |
| 2741 |
"3D00FFFF", |
| 2742 |
"3508FFFF", |
| 2743 |
"010E7026", |
| 2744 |
"25CE0001", |
| 2745 |
"44000004", |
| 2746 |
"00000000", |
| 2747 |
"44000000", |
| 2748 |
"00000000"]; |
| 2749 |
|
| 2750 |
val Abs = ["44000003", |
| 2751 |
"00000000", |
| 2752 |
"01C0402A", |
| 2753 |
"11000002", |
| 2754 |
"00000000", |
| 2755 |
"000E7022", |
| 2756 |
"44000004", |
| 2757 |
"00000000", |
| 2758 |
"44000000", |
| 2759 |
"00000000"]; |
| 2760 |
|
| 2761 |
val Fact = ["0C000002", |
| 2762 |
"00000000", |
| 2763 |
"44000000", |
| 2764 |
"44000003", |
| 2765 |
"000E2020", |
| 2766 |
"2FBD0020", |
| 2767 |
"AFBF0014", |
| 2768 |
"AFBE0010", |
| 2769 |
"27BE0020", |
| 2770 |
"0C000009", |
| 2771 |
"00000000", |
| 2772 |
"8FBE0010", |
| 2773 |
"8FBF0014", |
| 2774 |
"27BD0020", |
| 2775 |
"00027020", |
| 2776 |
"44000004", |
| 2777 |
"00001020", |
| 2778 |
"4BE00000", |
| 2779 |
"00000000", |
| 2780 |
"20080001", |
| 2781 |
"0088402C", |
| 2782 |
"11000004", |
| 2783 |
"00000000", |
| 2784 |
"20020001", |
| 2785 |
"08000016", |
| 2786 |
"00000000", |
| 2787 |
"2FBD0004", |
| 2788 |
"AFA40000", |
| 2789 |
"28840001", |
| 2790 |
"2FBD0020", |
| 2791 |
"AFBF0014", |
| 2792 |
"AFBE0010", |
| 2793 |
"27BE0020", |
| 2794 |
"0FFFFFF1", |
| 2795 |
"00000000", |
| 2796 |
"8FBE0010", |
| 2797 |
"8FBF0014", |
| 2798 |
"27BD0020", |
| 2799 |
"8FA40000", |
| 2800 |
"27BD0004", |
| 2801 |
"00004020", |
| 2802 |
"10800005", |
| 2803 |
"00000000", |
| 2804 |
"01024020", |
| 2805 |
"28840001", |
| 2806 |
"0BFFFFFB", |
| 2807 |
"00000000", |
| 2808 |
"01001020", |
| 2809 |
"4BE00000", |
| 2810 |
"00000000"]; |
| 2811 |
|
| 2812 |
val GCD = ["0C000002", |
| 2813 |
"00000000", |
| 2814 |
"44000000", |
| 2815 |
"44000003", |
| 2816 |
"00000000", |
| 2817 |
"000E2020", |
| 2818 |
"0080402A", |
| 2819 |
"11000002", |
| 2820 |
"00000000", |
| 2821 |
"00042022", |
| 2822 |
"44000003", |
| 2823 |
"00000000", |
| 2824 |
"000E2820", |
| 2825 |
"00A0402A", |
| 2826 |
"11000002", |
| 2827 |
"00000000", |
| 2828 |
"00052822", |
| 2829 |
"2FBD0020", |
| 2830 |
"AFBF0014", |
| 2831 |
"AFBE0010", |
| 2832 |
"27BE0020", |
| 2833 |
"0C00000A", |
| 2834 |
"00000000", |
| 2835 |
"8FBE0010", |
| 2836 |
"8FBF0014", |
| 2837 |
"27BD0020", |
| 2838 |
"00027020", |
| 2839 |
"44000004", |
| 2840 |
"00000000", |
| 2841 |
"00001020", |
| 2842 |
"4BE00000", |
| 2843 |
"00000000", |
| 2844 |
"14A00004", |
| 2845 |
"00000000", |
| 2846 |
"00801020", |
| 2847 |
"08000013", |
| 2848 |
"00000000", |
| 2849 |
"0085402C", |
| 2850 |
"15000006", |
| 2851 |
"00000000", |
| 2852 |
"00804020", |
| 2853 |
"00A02020", |
| 2854 |
"01002820", |
| 2855 |
"08000002", |
| 2856 |
"00000000", |
| 2857 |
"00A42822", |
| 2858 |
"2FBD0020", |
| 2859 |
"AFBF0014", |
| 2860 |
"AFBE0010", |
| 2861 |
"27BE0020", |
| 2862 |
"0FFFFFED", |
| 2863 |
"00000000", |
| 2864 |
"8FBE0010", |
| 2865 |
"8FBF0014", |
| 2866 |
"27BD0020", |
| 2867 |
"4BE00000", |
| 2868 |
"00000000"]; |
| 2869 |
|
| 2870 |
(* |
| 2871 |
val _ = DLXSimulatorC1.run_prog GCD |
| 2872 |
*) |
| 2873 |
|
| 2874 |
structure Main = |
| 2875 |
struct |
| 2876 |
fun doit last (instructions, inputs) = |
| 2877 |
let |
| 2878 |
fun inputFn {state = (inputs, outputs)} = |
| 2879 |
case inputs of |
| 2880 |
[] => {input = 0, |
| 2881 |
state = ([], outputs)} |
| 2882 |
| input::inputs => {input = input, |
| 2883 |
state = (inputs, outputs)} |
| 2884 |
fun outputFn {output, state = (inputs, outputs)} = |
| 2885 |
{state = (inputs, output::outputs)} |
| 2886 |
val state = (inputs, []) |
| 2887 |
val trap = {inputFn = inputFn, |
| 2888 |
outputFn = outputFn, |
| 2889 |
state = state} |
| 2890 |
val {state = (_, outputs), statistics} = |
| 2891 |
DLXSimulatorC1.run_prog {instructions = instructions, trap = trap} |
| 2892 |
in |
| 2893 |
if last |
| 2894 |
then (app (fn output => print ("Output: " ^ (Int.toString output) ^ "\n")) outputs; |
| 2895 |
print (statistics ()); |
| 2896 |
print "\n"; |
| 2897 |
()) |
| 2898 |
else () |
| 2899 |
end |
| 2900 |
val doit = fn last => |
| 2901 |
app (doit last) |
| 2902 |
[(Simple, []), |
| 2903 |
(Twos, [10]), |
| 2904 |
(Abs, [~10]), |
| 2905 |
(Fact, [12]), |
| 2906 |
(GCD, [123456789,98765])] |
| 2907 |
|
| 2908 |
val doit = |
| 2909 |
fn size => |
| 2910 |
let |
| 2911 |
fun loop n = |
| 2912 |
if n = 0 |
| 2913 |
then () |
| 2914 |
else (doit(n = 1); |
| 2915 |
loop(n-1)) |
| 2916 |
in loop size |
| 2917 |
end |
| 2918 |
end |