-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathram.scala
More file actions
315 lines (298 loc) · 12 KB
/
ram.scala
File metadata and controls
315 lines (298 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import scala.io.Source
//TODO or not TODO that is the question
val filename = args(0)
val mode = args(1)
var codeMode = true
var cur = 0
val mask = 65535
var mem = collection.mutable.Map[Int, Int]()
var mem_trunc = collection.mutable.Map[Int, Int]()
var regs = collection.mutable.Map[Char, Int]()
var new_regs = List[Char]()
var new_mem = List[(Int,Int)]()
def writeM(a: Int, v: Int) = {
// write the value v to address a in memory, mod mask
mem(a) = v & mask
if (mode == "1") new_mem = (a, mem(a))::new_mem
}
def readM(a: Int) = {
if (!mem.contains(a)) 0
else mem(a)
}
def writeR(a: Char, v: Int) = {
// only write to memory if the address is not in [0, 9]
if (!('0' <= a && a <= '9')) regs(a) = v
if (mode == "1") new_regs = a::new_regs
}
def readR(a: Char):Int = {
if('0' <= a && a <= '9'){
readValue(a.toString)
} else if (regs.contains(a)) {
regs(a)
} else 0
}
def readI(a: Int) = {
val x = readM(a)
val y = readM(a+1)
Array(
x & 0xff, (x >> 8) & 0xff,
y & 0xff, (y >> 8) & 0xff
).map(_.toChar).mkString("")
}
def readValue(a: String) = {
if (a.startsWith("0x"))
Integer.parseInt(a.stripPrefix("0x"), 16)
else
Integer.parseInt(a, 10)
}
// for (line <- Source.fromFile(filename).getLines) {
val lines = Source.fromFile(filename).getLines
var destReg = " " // name of the destination register
// var sourceRegs = "" // the 0, 1, or 2 source registers
var lastInst = "" // the previous instruction
while (lines.hasNext) {
val line = lines.next()
if (line.startsWith("code: ")) {
val address = line.stripPrefix("code: ").split(" ")(0)
cur = readValue(address)
codeMode = true
} else if (line.startsWith("data: ")) {
val n = line.stripPrefix("data: ").split(" ")
cur = readValue(n(0)) // memory address
val t = if (n.length == 2) readValue(n(1)) else 0 // store the truncate value in a mapping, if it exists
mem_trunc(cur) = t
codeMode = false
} else {
if (codeMode) {
val code = line.split("#")(0).trim().padTo(4,' ')
if (code != " ") {
writeM(cur, code(0).toInt % 256
+(code(1).toInt % 256)*256)
writeM(cur+1, code(2).toInt % 256
+(code(3).toInt % 256)*256)
if (mode == "4") {
var s = f"Hazard: ${lastInst}, ${code}"
if ("BbEe<l>g".contains(code(0))) { // branches
if (f"${code(1)}".contains(destReg)) println(s)
destReg = " "
lastInst = code
} else if ("+-*/%".contains(code(0))) { // maths
if (f"${code(1)}${code(2)}".contains(destReg)) println(s)
destReg = code(3).toString
lastInst = code
} else if ("JI".contains(code(0))) { // jumps/immediates
destReg = code(1).toString
lastInst = code
} else if ('!' == code(0)) { // syscalls
if (f"${code(1)}".contains(destReg)) println(s)
destReg = code(1).toString
lastInst = code
} else if ('L' == code(0)) { // loads
if (f"${code(1)}".contains(destReg)) println(s)
destReg = code(2).toString
lastInst = code
} else if ('R' == code(0)) { // returns
if (f"${code(1)}".contains(destReg)) println(s)
destReg = " "
lastInst = code
}
}
cur += 2
}
} else {
val data = line.split("#")(0).trim()
.split(" ").filter(_ != "")
.map(readValue)
for (d <- data) {
writeM(cur, d)
cur += 1
}
}
}
}
// writeR('P', 0)
var inst = ""
var halt = false
do {
inst = readI(readR('P'))
inst(0) match{
// Load and Store
case 'L' => val r = inst(1)
val d = inst(2)
val a = readR(r)
val v = readM(a)
writeR(d, v)
writeR('P', readR('P') + 2)
case 'S' => val s = inst(1)
val w = inst(2)
val a = readR(w)
val v = readR(s)
writeM(a, v)
writeR('P', readR('P') + 2)
// Mathematical Operands
case '+' => val s1 = inst(1)
val s2 = inst(2)
val d = inst(3)
val v1 = readR(s1)
val v2 = readR(s2)
val v = v1 + v2
writeR(d, v)
writeR('P', readR('P') + 2)
case '-' => val s1 = inst(1)
val s2 = inst(2)
val d = inst(3)
val v1 = readR(s1)
val v2 = readR(s2)
val v = v1 - v2
writeR(d, v)
writeR('P', readR('P') + 2)
case '*' => val s1 = inst(1)
val s2 = inst(2)
val d = inst(3)
val v1 = readR(s1)
val v2 = readR(s2)
val v = v1 * v2
writeR(d, v)
writeR('P', readR('P') + 2)
case '/' => val s1 = inst(1)
val s2 = inst(2)
val d = inst(3)
val v1 = readR(s1)
val v2 = readR(s2)
val v = v1 / v2
writeR(d, v)
writeR('P', readR('P') + 2)
case '%' => val s1 = inst(1)
val s2 = inst(2)
val d = inst(3)
val v1 = readR(s1)
val v2 = readR(s2)
val v = v1 % v2
writeR(d, v)
writeR('P', readR('P') + 2)
// Branch Operands
case 'B' => val s = inst(1)
val hh_hl = inst(2).toString + inst(3).toString
if (readR(s) != 0) {
writeR('P', readR('P') + readValue("0x" + hh_hl) * 2)
} else {
writeR('P', readR('P') + 2)
}
case 'b' => val s = inst(1)
val hh_hl = inst(2).toString + inst(3).toString
if(readR(s) != 0) {
writeR('P', readR('P') - readValue("0x" + hh_hl) * 2)
} else {
writeR('P', readR('P') + 2)
}
case 'E' => val s = inst(1)
val hh_hl = inst(2).toString + inst(3).toString
if(readR(s) == 0) {
writeR('P', readR('P') + readValue("0x" + hh_hl) * 2)
} else {
writeR('P', readR('P') + 2)
}
case 'e' => val s = inst(1)
val hh_hl = inst(2).toString + inst(3).toString
if(readR(s) == 0) {
writeR('P', readR('P') - readValue("0x" + hh_hl) * 2)
} else {
writeR('P', readR('P') + 2)
}
case '<' => val s = inst(1)
val hh_hl = inst(2).toString + inst(3).toString
if(readR(s) < 0) {
writeR('P', readR('P') + readValue("0x" + hh_hl) * 2)
} else {
writeR('P', readR('P') + 2)
}
case 'l' => val s = inst(1)
val hh_hl = inst(2).toString + inst(3).toString
if(readR(s) < 0) {
writeR('P', readR('P') - readValue("0x" + hh_hl) * 2)
} else {
writeR('P', readR('P') + 2)
}
case '>' => val s = inst(1)
val hh_hl = inst(2).toString + inst(3).toString
if(readR(s) > 0) {
writeR('P', readR('P') + readValue("0x" + hh_hl) * 2)
} else {
writeR('P', readR('P') + 2)
}
case 'g' => val s = inst(1)
val hh_hl = inst(2).toString + inst(3).toString
if(readR(s) > 0) {
writeR('P', readR('P') - readValue("0x" + hh_hl) * 2)
} else {
writeR('P', readR('P') + 2)
}
// Return
case 'R' => val s = inst(1)
writeR('P', readR(s))
case 'H' => halt = true
// Jump and Imm
case 'J' => val imm = readValue("0x" + readI(readR('P') + 2))
writeR(inst(1), readR('P') + 4)
writeR('P', imm)
case 'I' => val imm = readValue("0x" + readI(readR('P') + 2))
writeR(inst(1), imm)
writeR('P', readR('P') + 4)
// Syscall
case '!' => val m = inst(1)
var a = readR(m)
val hh_hl = inst(2).toString + inst(3).toString
hh_hl match {
case "01" => {
while (readM(a) != 0) {
print(readM(a).toChar)
a += 1
}
}
case "02" => {
val scanner = new java.util.Scanner(System.in)
writeR(m, readValue(scanner.nextLine()))
}
}
writeR('P', readR('P') + 2)
case _ => throw new IllegalArgumentException("Instruction not found.")
}
mode match {
case "1" => println(f"I: $inst, R: ${new_regs.mkString("[", ", ", "]")}, M: ${new_mem.mkString("[", ", ", "]")}")
/*
Output a trace of execution that includes on each line the instruction executed,
the new values of any registers changed by that instruction,
and the address and value of any changed memory locations.
*/
case "2" =>// is below
/*
Execute without tracing (faster) and output the values in each data section at the end of execution.
Use the second value in each the data section header as the number of memory locations to display at the end.
*/
case "3" => // perform syscall operands - if not mode 3, then don't do any syscalls
case "4" =>
/*
Analyse the program by outputting all the read-after-write hazards in the program.
For each one, output the addresses and pair of instructions.
*/
case _ => throw new IllegalArgumentException("Not a valid mode.")
}
new_regs = List[Char]()
new_mem = List[(Int,Int)]()
} while (!halt)
if (mode == "2") {
// output the values in each data section
// truncated by the second number in the data header, if there is one
for (d <- mem_trunc.toList.filter(_._2 != 0)) {
var a = d._1
print(f"Address: ${d._1}\nData: ")
if (d._2 != 0) {
// print the data starting at a until d._2 is reached
for (i <- 1 to d._2) {
print(readM(a))
a += 1
}
print('\n')
}
}
}