ch8_isa/data/key_data.rs
1/*
2 * key_data.rs
3 * Defines a struct that holds data for the KEY instruction
4 * Created on 12/6/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 `KEY` instruction
28pub struct KeyData {
29 /// The register to read the keypress into
30 vx: Register
31}
32
33//implementation
34impl KeyData {
35 /// Constructs a new `KeyData` instance
36 ///
37 /// # Argument
38 ///
39 /// * `new_vx` - The register to read the keypress into
40 ///
41 /// # Panics
42 ///
43 /// This method will panic if `new_vx` refers to the `I` register.
44 ///
45 /// # Returns
46 ///
47 /// A new `KeyData` instance with the given properties
48 pub fn new(new_vx: Register) -> KeyData {
49 //validate the register
50 if new_vx == Register::I {
51 panic!("Cannot read keypress into index register");
52 }
53
54 //and return the instance
55 return KeyData {
56 vx: new_vx
57 };
58 }
59}
60
61//CodeGen implementation
62impl CodeGen for KeyData {
63 /// Generates an opcode for the `KEY` instruction from the data
64 ///
65 /// # Returns
66 ///
67 /// The generated opcode
68 fn gen_opcode(&self) -> u16 {
69 return 0xF00A | ((self.vx.to_id() as u16) << 8);
70 }
71}
72
73//unit tests
74#[cfg(test)]
75mod tests {
76 //import the KeyData struct
77 use super::*;
78
79 //this test checks that the I register cannot be used
80 #[test]
81 #[should_panic]
82 fn test_cannot_read_key_into_index() {
83 let _b = KeyData::new(Register::I);
84 }
85
86 //this test checks opcode generation
87 #[test]
88 fn test_opcode_gen() {
89 let gdld = KeyData::new(Register::V1);
90 assert_eq!(gdld.gen_opcode(), 0xF10A);
91 }
92}
93
94//end of file