ch8_isa/data/
jmp_data.rs

1/*
2 * jmp_data.rs
3 * Defines a struct that holds data for the JMP instruction
4 * Created on 12/2/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 statement
24use super::super::codegen::CodeGen;
25
26/// Contextual data for the `JMP` instruction
27pub struct JmpData {
28    /// The address to jump to
29    addr: u16,
30}
31
32//struct implementation
33impl JmpData {
34    /// Creates a new `JmpData` instance
35    ///
36    /// # Argument
37    ///
38    /// * `new_addr` - The address to jump to 
39    ///
40    /// # Returns
41    ///
42    /// A new `JmpData` instance with the given address
43    pub fn new(new_addr: u16) -> JmpData {
44        //mask the new address
45        let mask_addr = new_addr & 0x0FFF;
46
47        //and return a new instance
48        return JmpData {
49            addr: mask_addr,
50        };
51    }
52
53    /// Gets the address to jump to 
54    ///
55    /// # Returns
56    ///
57    /// The address value of the data 
58    pub fn get_addr(&self) -> u16 {
59        return self.addr;
60    }
61}
62
63//CodeGen implementation
64impl CodeGen for JmpData {
65    /// Generates the opcode for the instruction
66    /// 
67    /// # Returns 
68    ///
69    /// The opcode for the `JMP` instruction
70    fn gen_opcode(&self) -> u16 {
71        //return the opcode
72        return 0x1000 | self.addr;
73    }
74}
75
76//unit tests
77#[cfg(test)]
78mod tests {
79    //import the JmpData struct
80    use super::*;
81
82    //this test checks that address
83    //values are masked when a new
84    //instance is created
85    #[test]
86    fn test_address_is_masked() {
87        let data = JmpData::new(0xFFFF);
88        assert_eq!(data.addr, 0x0FFF);
89    }
90
91    //this test checks opcode generation
92    #[test]
93    fn test_opcode_gen() {;
94        let jpd = JmpData::new(0x0CCC);
95        assert_eq!(jpd.gen_opcode(), 0x1CCC);
96    }
97}
98
99//end of file