-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathAxi4SlaveBfm.scala
More file actions
203 lines (171 loc) · 5.18 KB
/
Copy pathAxi4SlaveBfm.scala
File metadata and controls
203 lines (171 loc) · 5.18 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
/*
Copyright (C) 2019-2025 Antmicro
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/
package DMAController.Bfm
import DMAController.Bus._
import chisel3.Bits
import java.nio._
class Axi4MemoryBfm(val axi: AXI4,
baseAddress: Long,
val size: Int,
val peek: Bits => BigInt,
val poke: (Bits, BigInt) => Unit,
val println: String => Unit)
extends Axi4Bfm {
val base = baseAddress / 4
var buf: Array[Int] = new Array[Int](size)
private def addrToBufOffset(addr: Long): Int = {
require(addr >= base, s"AXI word addr 0x${addr.toHexString} is below BFM base 0x${base.toHexString}")
val bufOffset = addr - base
require(bufOffset.isValidInt, s"Buffer offset $bufOffset too large for int")
require(bufOffset < buf.length, s"Buffer offset $bufOffset exceeds buffer size ${buf.length}")
bufOffset.toInt
}
class Write {
private object State extends Enumeration {
type State = Value
val Idle, WriteAddr, WriteData, WriteResp = Value
}
private var state = State.Idle
private var awaddr: BigInt = 0
private var awlen: BigInt = 0
private var awvalid: BigInt = 0
private var wdata: BigInt = 0
private var wlast: BigInt = 0
private var wvalid: BigInt = 0
private var bready: BigInt = 0
private var addr: Long = 0
private var len: Int = 0
private var xferLen: Int = 0
private def peekInputs(): Unit = {
awaddr = peek(axi.aw.awaddr)
awlen = peek(axi.aw.awlen)
awvalid = peek(axi.aw.awvalid)
wdata = peek(axi.w.wdata)
wlast = peek(axi.w.wlast)
wvalid = peek(axi.w.wvalid)
bready = peek(axi.b.bready)
}
def update(t: Long): Unit = {
state match {
case State.Idle => {
poke(axi.aw.awready, 1)
state = State.WriteAddr
len = 0
}
case State.WriteAddr => {
if(awvalid != 0) {
addr = awaddr.toLong / 4
xferLen = awlen.toInt + 1
poke(axi.aw.awready, 0)
poke(axi.w.wready, 1)
state = State.WriteData
}
}
case State.WriteData => {
if(wvalid != 0) {
buf(addrToBufOffset(addr)) = wdata.toInt
addr += 1
len += 1
if(wlast != 0) {
poke(axi.w.wready, 0)
poke(axi.b.bvalid, 1)
poke(axi.b.bresp, 0)
state = State.WriteResp
if(xferLen != len) {
println("Transfer len doesn't match, %d %d".format(xferLen, len))
}
}
}
}
case State.WriteResp => {
if(bready != 0) {
poke(axi.b.bvalid, 0)
state = State.Idle
}
}
}
peekInputs()
}
}
class Read {
private object State extends Enumeration {
type State = Value
val Idle, ReadAddr, ReadData = Value
}
private var state = State.Idle
private var araddr: BigInt = 0
private var arlen: BigInt = 0
private var arvalid: BigInt = 0
private var rready: BigInt = 0
private var addr: Long = 0
private var len: Int = 0
private var xferLen: Int = 0
private def peekInputs(): Unit = {
araddr = peek(axi.ar.araddr)
arlen = peek(axi.ar.arlen)
arvalid = peek(axi.ar.arvalid)
rready = peek(axi.r.rready)
}
def update(t: Long): Unit = {
state match {
case State.Idle => {
poke(axi.ar.arready, 1)
poke(axi.r.rlast, 0)
poke(axi.r.rvalid, 0)
state = State.ReadAddr
len = 0
}
case State.ReadAddr => {
if(arvalid != 0) {
addr = araddr.toLong / 4
xferLen = arlen.toInt + 1
poke(axi.ar.arready, 0)
state = State.ReadData
}
}
case State.ReadData => {
if(rready != 0) {
poke(axi.r.rdata, buf(addrToBufOffset(addr)))
addr += 1
len += 1
poke(axi.r.rvalid, 1)
if(xferLen == len) {
poke(axi.r.rlast, 1)
state = State.Idle
}
}
}
}
peekInputs()
}
}
def loadFromFile(filename: String): Unit = {
val path = file.Paths.get(filename)
val buffer = file.Files.readAllBytes(path)
val bb = ByteBuffer.wrap(buffer)
buf = new Array[Int](buffer.length/4)
bb.asIntBuffer.get(buf)
}
def saveToFile(filename: String): Unit = {
val path = file.Paths.get(filename)
val bb = ByteBuffer.allocate(4*buf.length)
for (i <- 0 until buf.length) {
bb.putInt(buf(i))
}
file.Files.write(path, bb.array())
}
private val writer = new Write()
private val reader = new Read()
def update(t: Long): Unit = {
writer.update(t)
reader.update(t)
}
}