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