我創建了一個基本模塊,用于表示 Chisel3 中的一個記憶體單元:
class MemristorCellBundle() extends Bundle {
val writeBus = Input(UInt(1.W))
val dataBus = Input(UInt(8.W))
val cellBus = Output(UInt(8.W))
}
class MemCell() extends Module {
val io = IO(new MemCellBundle())
val write = Wire(UInt())
write := io.voltageBus
val internalValue = Reg(UInt())
// More than 50% of total voltage in (255).
when(write === 1.U) {
internalValue := io.dataBus
io.cellBus := io.dataBus
} .otherwise {
io.cellBus := internalValue
}
}
我想要的是它internalValue在write總線為邏輯低時輸出,并用邏輯高更改此值。我對 Chisel 的理解是暫存器可以internalValue在時鐘周期之間保持它,因此它基本上充當單個記憶體單元。
作為一個更大專案的一部分,我正在這樣做。但是,在撰寫單元測驗時,我發現“寫后讀”方案失敗。
class MemCellTest extends FlatSpec with ChiselScalatestTester with Matchers {
behavior of "MemCell"
it should "read and write" in {
test(new MemCell()) { c =>
c.io.dataBus.poke(5.U)
c.io.write.poke(0.U)
c.io.cellBus.expect(0.U)
// Write
c.io.dataBus.poke(5.U)
c.io.write.poke(1.U)
c.io.cellBus.expect(5.U)
// Verify read-after-write
c.io.dataBus.poke(12.U)
c.io.write.poke(0.U)
c.io.cellBus.expect(5.U)
}
}
}
前兩個期望正如我所期望的那樣作業。但是,當我在寫入后嘗試閱讀時,cellBus回傳到0而不是堅持5我以前寫過的。
test MemCell Success: 0 tests passed in 1 cycles in 0.035654 seconds 28.05 Hz
[info] MemCellTest:
[info] MemCell
[info] - should read and write *** FAILED ***
[info] io_cellBus=0 (0x0) did not equal expected=5 (0x5) (lines in MyTest.scala: 10) (MyTest.scala:21)
顯然暫存器沒有保持這個值,因此internalValue恢復為0。但是為什么會發生這種情況,我如何才能創造一個可以持續存在的價值?
uj5u.com熱心網友回復:
Drakinite 的評論是正確的。您需要確保步進時鐘以查看暫存器鎖存值。我調整了您的測驗以包括幾個步驟,它按預期作業:
c.io.dataBus.poke(5.U)
c.io.writeBus.poke(0.U)
c.io.cellBus.expect(0.U)
c.clock.step() // Added step
// Write passthrough (same cycle)
c.io.dataBus.poke(5.U)
c.io.writeBus.poke(1.U)
c.io.cellBus.expect(5.U)
c.clock.step() // Added step
// Verify read-after-write
c.io.dataBus.poke(12.U)
c.io.writeBus.poke(0.U)
c.io.cellBus.expect(5.U)
這是一個可執行示例,表明它有效(使用 chisel3 v3.4.4 和 chiseltest v0.3.4):https ://scastie.scala-lang.org/5E1rOEsYSzSUrLXZCvoyNA
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/372700.html
