Emmanuelle
Emmanuelle

Reputation: 73

Chisel Cannot reassign to read-only

This is a continuation from the other question i asked : Instanciation of a class doesn't work (Chisel/Scala)

Actually, I wanna print the "result" variable. I wrote this command : sbt test, and I don't understand this error :

"chisel3.internal.ChiselException: Cannot reassign to read-only combinedTausworthe.b: OpResult[UInt<32>] "

This is the Test file

import chiseltest._
import org.scalatest.freespec.AnyFreeSpec


class combined_test extends AnyFreeSpec with ChiselScalatestTester {
  "combinedTauswhore" in {

    test(new combinedTausworthe() ){ c =>
      //Initialisation
       c.clock.step(1)
      println(c.result)

      c.clock.step(1)
      println(c.result)

    }

  }

}

This is the class :


import chisel3._

 class combinedTausworthe extends Module{
   val io = IO(new Bundle {
   })
   val seed1 = RegInit(322769304.U(32.W))
   val seed2 = RegInit(424235419.U(32.W))
   val seed3 = RegInit(212119443.U(32.W))
   var result= 0.U

   var b = ((seed1 << 13)(31,0) ^ seed1)
   seed1 := ((seed1 & 4294967294L.U) << 12)(31,0) ^ b
   b := ((seed2 << 2)(31,0) ^ seed2) >> 25
   seed2 := ((seed2 & 4294967294L.U) << 4)(31,0) ^ b
   b := ((seed3<<3)(31,0) ^ seed3) >> 11
   seed3 := ((seed3 & 4294967294L.U) <<17)(31,0) ^ b

   result := (seed1 ^ seed2 ^ seed3)
   

 }

object Main extends App {
 
}

Upvotes: 1

Views: 704

Answers (1)

Jack Koenig
Jack Koenig

Reputation: 6064

In Chisel3, := is the connection operator for driving a signal with some value. Think about it as creating an electrical connection. In your code, it looks like you are trying to reassign to a mutable variable rather than drive the value of that variable with a value, try =:

import chisel3._

class combinedTausworthe extends Module{
  val io = IO(new Bundle {
  })
  val seed1 = RegInit(322769304.U(32.W))
  val seed2 = RegInit(424235419.U(32.W))
  val seed3 = RegInit(212119443.U(32.W))
  var result= 0.U

  var b = ((seed1 << 13)(31,0) ^ seed1)
  seed1 := ((seed1 & 4294967294L.U) << 12)(31,0) ^ b
  // Note using assignment (=), not connection (:=)
  b = ((seed2 << 2)(31,0) ^ seed2) >> 25
  seed2 := ((seed2 & 4294967294L.U) << 4)(31,0) ^ b
  b = ((seed3<<3)(31,0) ^ seed3) >> 11
  seed3 := ((seed3 & 4294967294L.U) <<17)(31,0) ^ b

  // Same thing here
  result = (seed1 ^ seed2 ^ seed3)   
}

That being said, as Scala is a functional programming language and we stylistically prefer pure/immutable constructs, try just using values:

import chisel3._

class combinedTausworthe extends Module{
  val io = IO(new Bundle {
  })
  val seed1 = RegInit(322769304.U(32.W))
  val seed2 = RegInit(424235419.U(32.W))
  val seed3 = RegInit(212119443.U(32.W))
  // Note result var removed and replaced with val below

  val b1 = ((seed1 << 13)(31,0) ^ seed1)
  seed1 := ((seed1 & 4294967294L.U) << 12)(31,0) ^ b1
  val b2 = ((seed2 << 2)(31,0) ^ seed2) >> 25
  seed2 := ((seed2 & 4294967294L.U) << 4)(31,0) ^ b2
  val b3 = ((seed3<<3)(31,0) ^ seed3) >> 11
  seed3 := ((seed3 & 4294967294L.U) <<17)(31,0) ^ b3

  val result = (seed1 ^ seed2 ^ seed3)
}

Upvotes: 1

Related Questions