Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module initialization test fails with ChiselSim while worked on chiseltest #4214

Open
carlosedp opened this issue Jun 24, 2024 · 0 comments
Open

Comments

@carlosedp
Copy link
Contributor

I have a module and test below that uses module initialization value which works and tests fine under chiseltest but when ported over to ChiselSim, it started to fail.

// Run with scala-cli test thisfile.scala
//> using scala 2.13.14
//> using dep org.scalatest::scalatest:3.2.19
//> using dep org.chipsalliance::chisel:6.4.0
//> using dep edu.berkeley.cs::chiseltest:6.0.0
//> using plugin org.chipsalliance:::chisel-plugin:6.4.0

import chisel3._
import org.scalatest._

import flatspec._
import matchers._

class PCPort(bitWidth: Int = 32) extends Bundle {
  val dataIn = Input(UInt(bitWidth.W))
  val PC = Output(UInt(bitWidth.W))
  val PC4 = Output(UInt(bitWidth.W))
  val writeEnable = Input(Bool())
  val writeAdd = Input(Bool()) // 1 => Add dataIn to PC, 0 => Set dataIn to PC
}

class ProgramCounter(regWidth: Int = 32, entryPoint: BigInt = 0)
    extends Module {
  val io = IO(new PCPort(regWidth))
  val pc = RegInit(entryPoint.U(regWidth.W))
  when(io.writeEnable) {
    pc := Mux(io.writeAdd, (pc.asSInt + io.dataIn.asSInt).asUInt, io.dataIn)
  }
  io.PC4 := pc + 4.U
  io.PC := pc
}

// Use the following to run the test using ChiselSim
import chisel3.simulator.EphemeralSimulator._
class ProgramCounterSpecChiselSim extends AnyFlatSpec with should.Matchers {
  it should "initialize to 0" in {
    simulate(new ProgramCounter()) { c =>
      c.io.PC.peek().litValue should be(0)
    }
  }
  it should "initialize to 0x00400000" in {    // This fails here but works on chiseltest
    simulate(new ProgramCounter(entryPoint = 0x400000)) { c =>
      c.io.PC.peek().litValue should be(0x400000)
    }
  }
}

// Use the following to run the test using chiseltest
// import chiseltest._
// class ProgramCounterSpecChiselTest
//     extends AnyFlatSpec
//     with ChiselScalatestTester
//     with should.Matchers {
//   it should "initialize to 0" in {
//     test(new ProgramCounter()) { c =>
//       c.io.PC.peek().litValue should be(0)
//     }
//   }
//   it should "initialize to 0x00400000" in {
//     test(new ProgramCounter(entryPoint = 0x400000)) { c =>
//       c.io.PC.peek().litValue should be(0x400000)
//     }
//   }
// }

It might be my own code logic that is wrong but it's strange it started failing when I ported to ChiselSim. The Module works fine on simulation and FPGA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant