ch8_isa/data/register.rs
1/*
2 * register.rs
3 * Enumerates register IDs
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/// The ID of a Chip-8 register
24#[derive(Debug, Clone, PartialEq)]
25pub enum Register {
26 //general purpose registers
27 V0,
28 V1,
29 V2,
30 V3,
31 V4,
32 V5,
33 V6,
34 V7,
35 V8,
36 V9,
37 VA,
38 VB,
39 VC,
40 VD,
41 VE,
42 VF,
43 //index register
44 I
45}
46
47//implementation
48impl Register {
49 /// Converts a `Register` to its numeric ID
50 ///
51 /// # Returns
52 ///
53 /// The ID of a given `Register`
54 pub fn to_id(&self) -> u8 {
55 return match *self {
56 Register::I => 0x10,
57 Register::V0 => 0x0,
58 Register::V1 => 0x1,
59 Register::V2 => 0x2,
60 Register::V3 => 0x3,
61 Register::V4 => 0x4,
62 Register::V5 => 0x5,
63 Register::V6 => 0x6,
64 Register::V7 => 0x7,
65 Register::V8 => 0x8,
66 Register::V9 => 0x9,
67 Register::VA => 0xA,
68 Register::VB => 0xB,
69 Register::VC => 0xC,
70 Register::VD => 0xD,
71 Register::VE => 0xE,
72 Register::VF => 0xF,
73 };
74 }
75}
76
77//unit tests
78#[cfg(test)]
79mod tests {
80 //import the Register enum
81 use super::*;
82
83 //this test checks converting
84 //a Register to its ID
85 #[test]
86 fn test_register_to_id() {
87 assert_eq!(Register::I.to_id(), 0x10);
88 assert_eq!(Register::V0.to_id(), 0x0);
89 assert_eq!(Register::V1.to_id(), 0x1);
90 assert_eq!(Register::V2.to_id(), 0x2);
91 assert_eq!(Register::V3.to_id(), 0x3);
92 assert_eq!(Register::V4.to_id(), 0x4);
93 assert_eq!(Register::V5.to_id(), 0x5);
94 assert_eq!(Register::V6.to_id(), 0x6);
95 assert_eq!(Register::V7.to_id(), 0x7);
96 assert_eq!(Register::V8.to_id(), 0x8);
97 assert_eq!(Register::V9.to_id(), 0x9);
98 assert_eq!(Register::VA.to_id(), 0xA);
99 assert_eq!(Register::VB.to_id(), 0xB);
100 assert_eq!(Register::VC.to_id(), 0xC);
101 assert_eq!(Register::VD.to_id(), 0xD);
102 assert_eq!(Register::VE.to_id(), 0xE);
103 assert_eq!(Register::VF.to_id(), 0xF);
104 }
105}
106
107//end of file