ch8_isa/data/
call_data.rs

1/*
2 * call_data.rs
3 * Defines a struct that holds data for the CALL 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 `CALL` instruction
27pub struct CallData {
28    /// The address of the subroutine to call 
29    addr: u16
30}
31
32//struct implementation
33impl CallData {
34    /// Creates a new `CallData` instance
35    ///
36    /// # Argument
37    ///
38    /// * `new_addr` - The address of the subroutine to call 
39    /// 
40    /// # Returns
41    ///
42    /// A new `CallData` instance with the given address
43    pub fn new(new_addr: u16) -> CallData {
44        //mask the new address
45        let mask_addr = new_addr & 0x0FFF;
46
47        //and return a new instance
48        return CallData {
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 CallData {
65    /// Generates the opcode for the data
66    /// 
67    /// # Returns
68    ///
69    /// The numeric opcode for the data
70    fn gen_opcode(&self) -> u16 {
71        return 0x2000 | self.addr;
72    }
73}
74
75//unit tests
76#[cfg(test)]
77mod tests {
78    //import the CallData struct
79    use super::*;
80
81    //this test checks that address
82    //values are masked when a new
83    //instance is created
84    #[test]
85    fn test_address_is_masked() {
86        let data = CallData::new(0xFFFF);
87        assert_eq!(data.addr, 0x0FFF);
88    }
89
90    //this test checks proper opcode generation
91    #[test]
92    fn test_opcode_gen() {
93        let data = CallData::new(0x0CCC);
94        assert_eq!(data.gen_opcode(), 0x2CCC);
95    }
96}
97
98//end of file