package quantumrisc

import spinal.core._
import spinal.lib._
import spinal.lib.bus.amba3.apb._

object MyMem{
  def getApb3Config = Apb3Config(
    addressWidth = 20,
    dataWidth    = 32
  )
}

class MyMem() extends Component {
    val io = new Bundle {
        val bus = slave(Apb3(MyMem.getApb3Config))
    }

    // Define a bundle "mapMem" for the state and the status
    case object mapMem extends Bundle {
        val state = Reg(Bits(128 bit))
        val key = Reg(Bits(128 bit))
        val status = Reg(Bits(32 bit))
        // val myKey = Reg(Bits(32 bit))
    }

    // Create a BusslaveFactory on the io.bus
    val busCtrl = Apb3SlaveFactory(io.bus)

    // Register the bundle "myReg" to the BusSlaveFactory at address 0x30000
    busCtrl.readMultiWord(mapMem,address = 0x30000)
    busCtrl.writeMultiWord(mapMem,address = 0x30000)

    // When state and key are loaded, the status becomes 1
    // and the calculation can start (state = state XOR key).
    // After the calcualtion is finished, the status is set to 0.
    when (mapMem.status === 1){
        mapMem.state := mapMem.state ^ mapMem.key
        mapMem.status := 0
    }
}
