1 /*
2 * sys-emu - A system emulator for tutorials
3 * Copyright (C) 2018 - 2019 osdevelopment-info
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18 package info.osdevelopment.sysemu.memory
19
20 import info.osdevelopment.sysemu.support.Utilities._
21 import scala.util.{Failure, Try}
22
23 /**
24 * The object SimpleReadWriteMemory used to create instances of the memory. The maximum memory that can be handled is
25 * 1 GiB.
26 */
27 object SimpleReadWriteMemory {
28
29 /**
30 * Creates a read-write memory with the given `size`.
31 * @param size the size of the memory
32 * @return a `Success` with the read-write memory with the given size
33 */
34 def apply(size: Long): Try[SimpleReadWriteMemory] = {
35 if (size <= 0 | size > 1.Gi) Failure(new IllegalArgumentException("Max size supported is 1 GiB"))
36 else Try(new SimpleReadWriteMemory(size))
37 }
38
39 }
40
41 /**
42 * A read-write memory (aka RAM) with the given `size`. The maximum size is 2^30^ Bytes (1 GiB). The memory is backed
43 * by an array.
44 * @param size the size of the memory
45 */
46 class SimpleReadWriteMemory private(val size: Long) extends ReadWriteMemory {
47
48 /** The array to hold the memory data. */
49 val memory = new Array[Byte](size.asInstanceOf[Int])
50
51 /**
52 * The read method to read the value from the array.
53 * @param address the address to read
54 * @return a `Success` with the byte read at the given address
55 */
56 protected override def doRead(address: Long): Try[Byte] = {
57 Try(memory(address.asInstanceOf[Int]))
58 }
59
60 /**
61 * The write method to write the value to the array.
62 * @param address the address to write
63 * @param value the `value` to write
64 * @return a `Success` when the byte is written successfully, `Failure` otherwise
65 */
66 protected override def doWrite(address: Long, value: Byte): Try[Unit] = {
67 Try(memory(address.asInstanceOf[Int]) = value)
68 }
69
70 }
Line |
Stmt Id |
Pos |
Tree |
Symbol |
Code |
35
|
236
|
1319
-
1342
|
Apply
|
scala.Boolean.|
|
size.<=(0).|(size.>(info.osdevelopment.sysemu.support.Utilities.BinaryUnitLong(1L).Gi))
|
35
|
235
|
1331
-
1342
|
Apply
|
scala.Long.>
|
size.>(info.osdevelopment.sysemu.support.Utilities.BinaryUnitLong(1L).Gi)
|
35
|
238
|
1344
-
1412
|
Apply
|
scala.util.Failure.apply
|
scala.util.Failure.apply[Nothing](new scala.`package`.IllegalArgumentException("Max size supported is 1 GiB"))
|
35
|
232
|
1327
-
1328
|
Literal
|
<nosymbol>
|
0
|
35
|
234
|
1338
-
1342
|
Select
|
info.osdevelopment.sysemu.support.Utilities.BinaryUnitLong.Gi
|
info.osdevelopment.sysemu.support.Utilities.BinaryUnitLong(1L).Gi
|
35
|
237
|
1352
-
1411
|
Apply
|
java.lang.IllegalArgumentException.<init>
|
new scala.`package`.IllegalArgumentException("Max size supported is 1 GiB")
|
35
|
239
|
1344
-
1412
|
Block
|
scala.util.Failure.apply
|
scala.util.Failure.apply[Nothing](new scala.`package`.IllegalArgumentException("Max size supported is 1 GiB"))
|
35
|
233
|
1338
-
1339
|
Literal
|
<nosymbol>
|
1L
|
36
|
241
|
1422
-
1458
|
Apply
|
scala.util.Try.apply
|
scala.util.Try.apply[info.osdevelopment.sysemu.memory.SimpleReadWriteMemory](new SimpleReadWriteMemory(size))
|
36
|
240
|
1426
-
1457
|
Apply
|
info.osdevelopment.sysemu.memory.SimpleReadWriteMemory.<init>
|
new SimpleReadWriteMemory(size)
|
36
|
242
|
1422
-
1458
|
Block
|
scala.util.Try.apply
|
scala.util.Try.apply[info.osdevelopment.sysemu.memory.SimpleReadWriteMemory](new SimpleReadWriteMemory(size))
|
49
|
244
|
1789
-
1828
|
Apply
|
scala.Array.<init>
|
new Array[Byte](SimpleReadWriteMemory.this.size.asInstanceOf[Int])
|
49
|
243
|
1805
-
1827
|
TypeApply
|
scala.Any.asInstanceOf
|
SimpleReadWriteMemory.this.size.asInstanceOf[Int]
|
57
|
245
|
2083
-
2108
|
TypeApply
|
scala.Any.asInstanceOf
|
address.asInstanceOf[Int]
|
57
|
247
|
2072
-
2110
|
Apply
|
scala.util.Try.apply
|
scala.util.Try.apply[Byte](SimpleReadWriteMemory.this.memory.apply(address.asInstanceOf[Int]))
|
57
|
246
|
2076
-
2109
|
Apply
|
scala.Array.apply
|
SimpleReadWriteMemory.this.memory.apply(address.asInstanceOf[Int])
|
67
|
250
|
2432
-
2478
|
Apply
|
scala.util.Try.apply
|
scala.util.Try.apply[Unit](SimpleReadWriteMemory.this.memory.update(address.asInstanceOf[Int], value))
|
67
|
249
|
2436
-
2477
|
Apply
|
scala.Array.update
|
SimpleReadWriteMemory.this.memory.update(address.asInstanceOf[Int], value)
|
67
|
248
|
2443
-
2468
|
TypeApply
|
scala.Any.asInstanceOf
|
address.asInstanceOf[Int]
|