-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ram.scala
65 lines (52 loc) · 1.62 KB
/
Ram.scala
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
package MySpinalHardware
import java.nio.file.{Files, Paths}
import spinal.core._
/* returns max value for number of bit
*/
object max_bits {
def apply(value: Int): Int = {
if (value < 0) SpinalError(s"No negative value ($value) on ${this.getClass.getSimpleName}")
((0x1 << value) - 1)
}
}
class RamInit(File: String, AddrDepth: Int = 8) extends Component {
val io = new Bundle {
val ena = in Bool()
val wea = in Bits(1 bit)
val addra = in Bits(AddrDepth bit)
val douta = out Bits(8 bit)
val dina = in Bits(8 bit)
}
def readBin(file : String): Seq[Bits] = Files.readAllBytes(Paths.get(file)).map(b => B(b.toInt & 0xFF))
def MemRom(initialContent: Seq[Bits]) = new Mem(Bits(8 bit), initialContent.length) init(initialContent)
val mem = MemRom(readBin(File))
mem.write(
enable = io.ena & io.wea.asBool,
address = io.addra.asUInt,
data = io.dina
)
io.douta := mem.readSync(
enable = io.ena,
address = io.addra.asUInt
)
}
class Ram(AddrDepth: Int = 8) extends Component {
val io = new Bundle {
val ena = in Bool()
val wea = in Bool()
val addra = in Bits(AddrDepth bit)
val douta = out Bits(8 bit)
val dina = in Bits(8 bit)
}
def MemRom(Depth: Int) = new Mem(Bits(8 bit), max_bits(Depth))
val mem = MemRom(AddrDepth)
mem.write(
enable = io.ena & io.wea,
address = io.addra.asUInt,
data = io.dina
)
io.douta := mem.readSync(
enable = io.ena,
address = io.addra.asUInt
)
}