use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AluInput {
pub op: AluOp,
pub op1_data: u32,
pub op2_data: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AluOp {
Base(BaseAluOp),
Mext(MulOp),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BaseAluOp {
Add,
Sub,
Sll,
Srl,
Sra,
And,
Or,
Xor,
Slt,
Sltu,
CopyOp1,
CopyOp2,
Zero,
}
pub fn exe_alu(alu_op1: u32, alu_op2: u32, op: BaseAluOp) -> u32 {
let alu_shamt = alu_op2 & 0x1f;
match op {
BaseAluOp::Add => alu_op1 + alu_op2,
BaseAluOp::Sub => alu_op1 - alu_op2,
BaseAluOp::And => alu_op1 & alu_op2,
BaseAluOp::Or => alu_op1 | alu_op2,
BaseAluOp::Xor => alu_op1 ^ alu_op2,
BaseAluOp::Slt => ((alu_op1 as i32) < (alu_op2 as i32)) as u32,
BaseAluOp::Sltu => (alu_op1 < alu_op2) as u32,
BaseAluOp::Sll => alu_op1 << alu_shamt,
BaseAluOp::Sra => ((alu_op1 as i32) >> alu_shamt) as u32,
BaseAluOp::Srl => alu_op1 >> alu_shamt,
BaseAluOp::CopyOp1 => alu_op1,
BaseAluOp::CopyOp2 => alu_op2,
BaseAluOp::Zero => 0,
}
}