简体   繁体   English

凿子将值保留在模块中,直到新写入

[英]Chisel persist value in module until new write

I have created a basic module that is meant to represent a unit of memory in Chisel3:我创建了一个基本模块,用于表示 Chisel3 中的 memory 单元:

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
    }
}

What I want is for it to output the internalValue when the write bus is logic LOW, and change this value with logic HIGH.我想要的是当write总线为逻辑低电平时,它为 output internalValue值,并用逻辑高电平更改此值。 My understanding of Chisel is that the register can persist this internalValue between clock cycles, so that this basically acts as a single unit of memory.我对Chisel的理解是寄存器可以在时钟周期之间持久化这个internalValue ,这样基本上就相当于memory的一个单元。

I'm doing it in this way as part of a larger project.作为一个更大项目的一部分,我正在这样做。 However when writing a unit test I am finding that the 'read-after-write' scenario fails.但是,在编写单元测试时,我发现“写后读”方案失败。

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)
        }
    }
}

The first two expectations work just as I would expect.前两个期望正如我所期望的那样工作。 However, when I try to read after writing, the cellBus returns to 0 instead of persisting the 5 that I had written previously.但是,当我在写入后尝试读取时, 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)

Clearly the register is not keeping this value, and so internalValue reverts to 0 .显然,寄存器没有保留这个值,因此internalValue恢复为0 But why does this happen, and how would I be able to create a value that can persist?但是为什么会发生这种情况,我如何才能创造一个可以持续存在的价值呢?

Drakinite's comment is correct. Drakinite 的评论是正确的。 You need to make sure to step the clock in order to see the register latch the value.您需要确保步进时钟以查看寄存器锁存值。 I tweaked your test to include a couple of steps and it works as expected:我调整了您的测试以包含几个步骤,它按预期工作:

  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)

Here's an executable example showing that this works (using chisel3 v3.4.4 and chiseltest v0.3.4): https://scastie.scala-lang.org/5E1rOEsYSzSUrLXZCvoyNA这是一个可执行示例,表明它有效(使用 chisel3 v3.4.4 和 chiseltest v0.3.4): https://scastie.scala-lang.org/5E1rOEsSYSzSUrLXZCvoyNA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM