r8lib/operations/
op1nnn.rs

1//! The implementation of the 1nnn (JP addr) operation.
2
3use log::debug;
4
5use crate::Machine;
6
7use super::{Operation, OperationResult};
8
9/// Implements the 1nnn (JP addr) operation. Jump to location `nnn`.
10pub(crate) struct Op1nnn {
11    /// The `nnn` operation parameter.
12    nnn: u16,
13}
14
15impl Op1nnn {
16    // Creates a new Op1nnn.
17    pub(crate) fn new(nnn: u16) -> Self {
18        Self { nnn }
19    }
20}
21
22impl Operation for Op1nnn {
23    /// Execute the operation 1nnn (JP addr).
24    fn exec(&self, _: &mut Machine) -> OperationResult {
25        debug!("op_1nnn, nnn={:#06x?}", self.nnn);
26
27        OperationResult::JumpTo(self.nnn as usize)
28    }
29}
30
31#[cfg(test)]
32mod test_op1nnn {
33    use super::*;
34
35    #[test]
36    fn test_op1nnn_exec() {
37        let mut machine = Machine::default();
38        let nnn = 0xA;
39
40        let op = Op1nnn::new(nnn);
41        let result = op.exec(&mut machine);
42
43        assert_eq!(
44            result,
45            OperationResult::JumpTo(nnn as usize),
46            "should return JumpTo(nnn)"
47        );
48    }
49}