A ChiselTest question

37 views
Skip to first unread message

Martin Schoeberl

unread,
Oct 3, 2022, 3:29:23 PM10/3/22
to chisel...@googlegroups.com
HI all,

I want to have some test functions as part of a Bundle definition. E.g., having a bus interface and providing read and write functions. I am able to do it on a Module, e.g.,

class CpuInterface(addrWidth: Int) extends Module {
val io = IO(new Bundle {
val cpuPort = new MemoryMappedIO(addrWidth)
})

def read(addr: Int): BigInt = {
io.cpuPort.address.poke(addr.U)
io.cpuPort.wr.poke(false.B)
io.cpuPort.rd.poke(true.B)
clock.step()
// Ignore waiting for a moment
io.cpuPort.rdData.peekInt()
}


However, MemoryMappedIO might be used on other classes that are not a CpuInterface. Therefore, I would like to add it to the Bundle (interface definition). But I need a reference to the Module. I can only do it like this:

class MemoryMappedIO(private val addrWidth: Int) extends Bundle {
val address = Input(UInt(addrWidth.W))
val rd = Input(Bool())
val wr = Input(Bool())
val rdData = Output(UInt(32.W))
val wrData = Input(UInt(32.W))
val ack = Output(Bool())

// TODO: it would fit better here than in CpuInterface, but without a reference to a CpuInterface
def readX(addr: Int, d: CpuInterface): BigInt = {
d.io.cpuPort.address.poke(addr.U)
d.io.cpuPort.wr.poke(false.B)
d.io.cpuPort.rd.poke(true.B)
d.clock.step()
// Ignore waiting for a moment
d.io.cpuPort.rdData.peekInt()
}
}

which is worse, as it contains now a reference to a CpuInterface.

Any good idea to solve this elegantly?

Cheers,
Martin

Albert Magyar

unread,
Oct 3, 2022, 4:44:27 PM10/3/22
to chisel...@googlegroups.com
Have you considered making it a free function that takes a MemoryMappedIO argument, along with a separate argument of either a clock or “clock-having module” type? Cleanly capturing behavioral effects within instance methods can be a bit tricky.

--
You received this message because you are subscribed to the Google Groups "chisel-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chisel-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chisel-users/407CBED5-925A-4D5B-95F4-C93C54E9B3B5%40jopdesign.com.

Martin Schoeberl

unread,
Oct 7, 2022, 7:52:09 AM10/7/22
to chisel...@googlegroups.com
My current solution is to have a helper class (in test, as this should not be in the main design part) that takes a CpuInterface object as parameter: https://github.com/t-crest/soc-comm/blob/master/src/test/scala/soc/MemoryMappedIOHelper.scala

But using just the MemoryMappedIO plus a clock is probably the better way. I will try this out.

Thanks,
Martin

Reply all
Reply to author
Forward
0 new messages