package projet.op

import chisel3._
import chisel3.util.switch
import projet.CUInterface
import projet.AluOpCode
import chisel3.util.is
import projet.DMemSize
import projet.OpType

object OpStore {
    // Implements functions for all instruction of the OpStore kind
    def opStore(io: CUInterface) = {
        val funct3 = io.instruction(14, 12);

        // Meaning of Funct3 in the context of an OpStore instruction
        object Funct3 extends ChiselEnum {
            val SB = "b000".U
            val SH = "b001".U
            val SW = "b010".U
            val SD = "b011".U
        }

        io.optype := OpType.S
        io.alu_opcode := AluOpCode.Add
        io.mux_alu_imm := true.B
        io.mux_rega_pc := true.B
        io.mux_regb_imm := false.B
        io.memory_we := true.B
        io.memory_en := true.B
        switch(funct3) {
            is(Funct3.SB) {
                io.memory_size := DMemSize.Byte
            }
            is(Funct3.SH) {
                io.memory_size := DMemSize.Half
            }
            is(Funct3.SW) {
                io.memory_size := DMemSize.Word
            }
            is(Funct3.SD) {
                io.memory_size := DMemSize.Long
            }
        }
    }
}
