A while back I had a project to write a Game Boy Advance emulator. One thing I ran into which I had no good solution to was how to ergonomically model the register banks on the ARM7TDMI.
In other languages, I'd use properties, something like this Rust-ish pseudocode.
struct Processor {
mode: ProcessorMode,
r0: u32,
// ...
r8_sys: u32,
r8_fiq: u32,
r9_sys: u32,
r9_fiq: u32,
// ...
property r8: u32 {
get(&self) -> u32 {
match self.mode {
ProcessorMode::Fiq => self.r8_fiq,
_ => self.r8_sys,
}
},
set(&mut self, arg: u32) {
match self.mode {
ProcessorMode::Fiq => self.r8_fiq = arg,
_ => self.r8_sys = arg,
};
},
},
// ...
}
When I asked on IRC I was pointed in the direction of a trick using Deref
to redirect access to a substructure, but I've since learned that's considered an anti-pattern. As such I am left wondering: is there a better way to do this in Rust?