gcrecomp_core/runtime/
context.rs1#[derive(Debug, Clone)]
3pub struct CpuContext {
4 pub gpr: [u32; 32], pub pc: u32, pub lr: u32, pub ctr: u32, pub cr: u32, pub xer: u32, pub fpscr: u32, pub fpr: [f64; 32], pub msr: u32, }
14
15impl CpuContext {
16 pub fn new() -> Self {
17 Self {
18 gpr: [0; 32],
19 pc: 0,
20 lr: 0,
21 ctr: 0,
22 cr: 0,
23 xer: 0,
24 fpscr: 0,
25 fpr: [0.0; 32],
26 msr: 0,
27 }
28 }
29
30 pub fn get_register(&self, reg: u8) -> u32 {
31 if reg < 32 {
32 self.gpr[reg as usize]
33 } else {
34 0
35 }
36 }
37
38 pub fn set_register(&mut self, reg: u8, value: u32) {
39 if reg < 32 {
40 self.gpr[reg as usize] = value;
41 }
42 }
43
44 pub fn get_cr_field(&self, field: u8) -> u8 {
45 if field < 8 {
46 ((self.cr >> (4 * field)) & 0xF) as u8
47 } else {
48 0
49 }
50 }
51
52 pub fn set_cr_field(&mut self, field: u8, value: u8) {
53 if field < 8 {
54 let mask = !(0xF << (4 * field));
55 self.cr = (self.cr & mask) | ((value as u32 & 0xF) << (4 * field));
56 }
57 }
58
59 pub fn get_fpr(&self, reg: u8) -> f64 {
60 if reg < 32 {
61 self.fpr[reg as usize]
62 } else {
63 0.0
64 }
65 }
66
67 pub fn set_fpr(&mut self, reg: u8, value: f64) {
68 if reg < 32 {
69 self.fpr[reg as usize] = value;
70 }
71 }
72}
73
74impl Default for CpuContext {
75 fn default() -> Self {
76 Self::new()
77 }
78}
79