1
votes

Is the best way to initialize a memory in chisel implementing a for loop to write in it ?

is(s_multiplier){
      when(ready){state := s_ready}
      // Initialization of C memory to 0
      for(i <- 0 to matrixSize - 1){
        for(j <- 0 to matrixSize - 1){
          memC.write(i + j, 0.asSInt((2 * cellSize).W))
        }
      }
1

1 Answers

3
votes

The disadvantage of using a for loop is that the above code will try to execute the nested for loops in one clock cycle which is not practical since usually the best case scenario is that you perform one write per clock cycle or use a burst mode mechanism. I would suggest to replace i and j with counters that increment at the tick of the clock and stay in the s_multiplier state till you fill up your matrix.

is(s_multiplier){
  when(ready){state := s_ready}
  // Initialization of C memory to 0
  when(counti <= matrixSize -1){ //Initialize counti and countj to 0
    when(countj <= matrixSize - 1){
      memC.write(counti + countj, 0.asSInt((2 * cellSize).W))
      countj := countj + 1.U
    }
    counti <= counti + 1.U
    state := s_multiplier
  }. otherwise{state := S_go_to_some_other_state}