1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! Instructions related to processor interrupts and traps.

use enum_primitive::FromPrimitive;
use faucon_asm::{Instruction, Operand};

use super::{Cpu, CpuFlag, Trap, PC};

/// Returns from an interrupt handler.
pub fn iret(cpu: &mut Cpu, _: &Instruction) -> usize {
    // Restore return address from the stack.
    cpu.registers[PC] = cpu.stack_pop();

    // Restore the interrupt state.
    cpu.registers
        .set_flag(CpuFlag::IE0, cpu.registers.get_flag(CpuFlag::IS0));
    cpu.registers
        .set_flag(CpuFlag::IE1, cpu.registers.get_flag(CpuFlag::IS1));
    cpu.registers
        .set_flag(CpuFlag::IE2, cpu.registers.get_flag(CpuFlag::IS2));

    // Signal regular PC increment to the CPU.
    cpu.increment_pc = true;

    1
}

/// Triggers a software trap.
pub fn trap(cpu: &mut Cpu, insn: &Instruction) -> usize {
    // Extract the instruction operands (trap value).
    let trap = insn.operands()[0];

    // Trigger the software trap.
    if let Operand::UnsignedImmediate(imm) = trap {
        cpu.trigger_trap(Trap::from_u32(imm).unwrap());
    } else {
        unreachable!();
    }

    // Signal irregular PC modification to the CPU.
    cpu.increment_pc = false;

    1
}