ch8_isa/data/
sub_data.rs

1/*
2 * sub_data.rs
3 * Defines a struct that holds data for the SUB 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 `SUB` instruction
28pub struct SubData {
29    /// The destination register
30    vx: Register,
31
32    /// The register to be subtracted from `vx`
33    vy: Register 
34}
35
36//implementation
37impl SubData {
38    /// Constructs a new `SubData` instance
39    ///
40    /// # Arguments
41    ///
42    /// * `new_vx` - The destination register
43    /// * `new_vy` - The register to subtract from `new_vx`
44    ///
45    /// # Panics
46    ///
47    /// This method will panic if either `Register` argument 
48    /// refers the the `I` register.
49    ///
50    /// # Returns
51    ///
52    /// A new `SubData` instance with the given properties
53    pub fn new(new_vx: Register, new_vy: Register) -> SubData {
54        //check the registers
55        if (new_vx == Register::I) || (new_vy == Register::I) {
56            panic!("Subtraction is not defined for the index register");
57        }
58
59        //and return an instance
60        return SubData {
61            vx: new_vx,
62            vy: new_vy 
63        };
64    }
65}
66
67//CodeGen implementation
68impl CodeGen for SubData {
69    /// Generates an opcode for the `SUB` instruction
70    /// 
71    /// # Returns
72    ///
73    /// An opcode for the `SUB` instruction from the data
74    fn gen_opcode(&self) -> u16 {
75        let mut code = 0x8005;
76        code |= (self.vx.to_id() as u16) << 8;
77        code |= (self.vy.to_id() as u16) << 4;
78        return code;
79    }
80}
81
82//tests
83#[cfg(test)]
84mod tests {
85    //import the SubData struct
86    use super::*;
87
88    //this test ensures that the I register cannot be used in subtraction
89    #[test]
90    #[should_panic]
91    fn test_cannot_subtract_index() {
92        let _b1 = SubData::new(Register::I, Register::V0);
93        let _b2 = SubData::new(Register::V0, Register::I);
94    }
95
96    //this test checks opcode generation
97    #[test]
98    fn test_opcode_gen() {
99        let subd = SubData::new(Register::V1, Register::V2);
100        assert_eq!(subd.gen_opcode(), 0x8125);
101    }
102}
103
104//end of file