r8lib/operations/
opfx1e.rs

1//! The implementation of the Fx1E (ADD I, Vx) operation.
2
3use log::debug;
4
5use crate::Machine;
6
7use super::{Operation, OperationResult};
8
9/// Implements the Fx1E (ADD I, Vx) operation. Set `I = I + Vx`.
10pub(crate) struct Opfx1e {
11    /// The `x` operation parameter.
12    x: u8,
13}
14
15impl Opfx1e {
16    // Creates a new Opfx1e.
17    pub(crate) fn new(x: u8) -> Self {
18        Self { x }
19    }
20}
21
22impl Operation for Opfx1e {
23    /// Execute the operation Fx1E (ADD I, Vx).
24    fn exec(&self, machine: &mut Machine) -> OperationResult {
25        debug!("op_fx1e, x={}", self.x);
26
27        machine.i += machine.v[self.x as usize] as usize;
28
29        OperationResult::Next
30    }
31}
32
33#[cfg(test)]
34mod test_opfx1e {
35    use super::*;
36
37    #[test]
38    fn test_opfx1e_exec() {
39        let mut machine = Machine::default();
40        let x = 0x1;
41
42        machine.i = 0xFF0;
43        machine.v[x as usize] = 0xF;
44
45        let op = Opfx1e::new(x);
46        let result = op.exec(&mut machine);
47
48        assert_eq!(result, OperationResult::Next, "should return Next");
49        assert_eq!(
50            machine.i, 0xFFF,
51            "machine i register value should be summed with v[{:#02x?}] value",
52            x
53        );
54    }
55}