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
use std::fmt;
use std::fmt::{Display, Formatter};

/// Error that is returned when the processor encounters an invalid opcode
#[derive(Debug, Clone)]
pub struct OpcodeError {
    /// The invalid opcode that was encountered
    opcode: u16,
    /// The program conuter at the moment the invalid opcode was encountered
    pc: u8,
}

impl OpcodeError {
    pub fn new(opcode: u16, pc: u8) -> OpcodeError {
        OpcodeError { opcode, pc }
    }
}

impl Display for OpcodeError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(
            f,
            "Invalid opcode {:x} encountered at PC = {:x}",
            self.opcode, self.pc
        )
    }
}