forked from maltanar/axi-in-chisel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleReg.scala
68 lines (52 loc) · 2.44 KB
/
SimpleReg.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
66
67
package AXIChisel
import Chisel._
import Literal._
import Node._
import AXILiteDefs._
// a Chisel traslation of the AXI Lite Slave template code generated by Vivado
// with some simplifications (no byte-addressing in writes, no mux on read data output)
class SimpleReg(regCount: Int, addrBits: Int, dataBits: Int) extends Module {
val io = new AXILiteSlaveIF(addrBits, dataBits)
val regBank = Vec.fill(regCount) {Reg(init=UInt(0, width=dataBits))}
val wordSelBits = log2Up(dataBits/8)
// rename AXI signals to match Xilinx templates
io.renameSignals()
// register for write address channel ready signal
val writeAddrReadyReg = Reg(init=Bool(false))
val canDoWrite = io.writeAddr.valid && io.writeData.valid && !writeAddrReadyReg
writeAddrReadyReg := canDoWrite
io.writeAddr.ready := writeAddrReadyReg
// register for keeping write address
val writeAddrReg = Reg(init=UInt(0, addrBits))
when (canDoWrite) {writeAddrReg := io.writeAddr.bits.addr}
val writeReadyReg = Reg(init=Bool(false), next=canDoWrite)
io.writeData.ready := writeReadyReg
// register bank write
val doWrite = writeReadyReg && io.writeData.valid && writeAddrReadyReg && io.writeAddr.valid
val writeRegSelect = writeAddrReg(addrBits-1, wordSelBits)
// note that we write the entire word (no byte select using write strobe)
when (doWrite) { regBank(writeRegSelect) := io.writeData.bits.data }
// write response generation
io.writeResp.bits := UInt(0) // always OK
val writeRespValidReg = Reg(init=Bool(false))
writeRespValidReg := doWrite && !writeRespValidReg
io.writeResp.valid := writeRespValidReg
// read address ready generation
val readAddrReadyReg = Reg(init=Bool(false))
val canDoRead = !readAddrReadyReg && io.readAddr.valid
readAddrReadyReg := canDoRead
io.readAddr.ready := readAddrReadyReg
// read address latching
val readAddrReg = Reg(init=UInt(0, addrBits))
when (canDoRead) { readAddrReg := io.readAddr.bits.addr }
// read data valid and response generation
val readValidReg = Reg(init=Bool(false))
val doRead = readAddrReadyReg && io.readAddr.valid && !readValidReg
readValidReg := doRead
io.readData.valid := readValidReg
io.readData.bits.resp := UInt(0) // always OK
// register bank read
val readRegSelect = io.readAddr.bits.addr(addrBits-1, wordSelBits)
val outputReg = Reg(init=UInt(0, addrBits), next=regBank(readRegSelect))
io.readData.bits.data := outputReg
}