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