Files
eb001/src/main/scala/Switch/mac/MacSRAM.scala

59 lines
1.3 KiB
Scala
Raw Normal View History

2023-06-29 18:29:54 +08:00
package MAC
import chisel3._
import chisel3.util._
class MacSRAMIO extends Bundle{
val we = Input(Vec(4, Bool())) // Write enable input, active high
val oe = Input(Bool()) // Output enable input, active high
val addr = Input(UInt(8.W)) // address bus inputs
val di = Input(UInt(32.W)) // input data bus
val dato = Output(UInt(32.W)) // output data bus
}
class MacSRAM extends Module{
val io: MacSRAMIO = IO(new MacSRAMIO)
// Generic RAM's registers and wires
val mem0 = Mem( 256, UInt(8.W) )
val mem1 = Mem( 256, UInt(8.W) )
val mem2 = Mem( 256, UInt(8.W) )
val mem3 = Mem( 256, UInt(8.W) )
val q = Wire(UInt(32.W))
val raddr = Reg( UInt(8.W) )
// Data output drivers
2023-10-08 18:27:49 +08:00
io.dato := Mux((io.oe), q, DontCare)
2023-06-29 18:29:54 +08:00
// read operation
2023-10-08 18:27:49 +08:00
when( true.B ){
2023-06-29 18:29:54 +08:00
raddr := io.addr // read address needs to be registered to read clock
}
q := Mux(reset.asBool, 0.U, Cat(mem3.read(raddr), mem2.read(raddr), mem1.read(raddr), mem0.read(raddr)))
// write operation
2023-10-08 18:27:49 +08:00
when(io.we(3)){
2023-06-29 18:29:54 +08:00
mem3.write(io.addr, io.di(31,24))
}
2023-10-08 18:27:49 +08:00
when(io.we(2)){
2023-06-29 18:29:54 +08:00
mem2.write(io.addr, io.di(23,16))
}
2023-10-08 18:27:49 +08:00
when(io.we(1)){
2023-06-29 18:29:54 +08:00
mem1.write(io.addr, io.di(15, 8))
}
2023-10-08 18:27:49 +08:00
when(io.we(0)){
2023-06-29 18:29:54 +08:00
mem0.write(io.addr, io.di( 7, 0))
}
}