ch8_isa/data/
rand_data.rs

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