ch8_isa/data/xor_data.rs
1/*
2 * xor_data.rs
3 * Defines a struct that holds data for the XOR instruction
4 * Created on 12/5/2019
5 * Created by Andrew Davis
6 *
7 * Copyright (C) 2019 Andrew Davis
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23//usage statements
24use super::Register;
25use super::super::codegen::CodeGen;
26
27/// Contextual data for the `XOR` instruction
28pub struct XorData {
29 /// The first (destination) operand
30 vx: Register,
31
32 /// The second operand
33 vy: Register
34}
35
36//implementation
37impl XorData {
38 /// Constructs a new `XorData` instance
39 ///
40 /// # Arguments
41 ///
42 /// * `new_vx` - The first (destination) operand
43 /// * `new_vy` - The second operand
44 ///
45 /// # Panics
46 ///
47 /// This method will panic if either register is the `I` register
48 ///
49 /// # Returns
50 ///
51 /// A new `XorData` instance with the given properties
52 pub fn new(new_vx: Register, new_vy: Register) -> XorData {
53 //validate the registers
54 if (new_vx == Register::I) || (new_vy == Register::I) {
55 panic!("Index register cannot be XORed!");
56 }
57
58 //and return an instance
59 return XorData {
60 vx: new_vx,
61 vy: new_vy
62 };
63 }
64}
65
66//CodeGen implementation
67impl CodeGen for XorData {
68 /// Generates an opcode for the `XOR` instruction
69 ///
70 /// # Returns
71 ///
72 /// An opcode for the `XOR` instruction from the data
73 fn gen_opcode(&self) -> u16 {
74 let mut code = 0x8003;
75 code |= (self.vx.to_id() as u16) << 8;
76 code |= (self.vy.to_id() as u16) << 4;
77 return code;
78 }
79}
80
81//unit tests
82#[cfg(test)]
83mod tests {
84 //import the XorData struct
85 use super::*;
86
87 //this test checks that the I register cannot be used
88 #[test]
89 #[should_panic]
90 fn test_cannot_xor_index() {
91 let _d1 = XorData::new(Register::I, Register::V0);
92 let _d2 = XorData::new(Register::V0, Register::I);
93 }
94
95 //this test checks opcode generation
96 #[test]
97 fn test_opcode_gen() {
98 let xord = XorData::new(Register::V1, Register::V2);
99 assert_eq!(xord.gen_opcode(), 0x8123);
100 }
101}
102
103//end of file