Non-linear memory safety #2
Replies: 2 comments 14 replies
-
@TylerBloom |
Beta Was this translation helpful? Give feedback.
-
I have implemented a simple idea sketch of /// Array corresponding to the memory of each bank
pub struct BankArray<T> {
bank: MemBank,
size: usize,
ptr: *mut T,
} BankArray is an array assigned to a particular bank space that has an address and size together. I haven't thought of safe referencing for this collection yet. I've implemented the safe (with bound) Index trait, but it's not safe if the bank changes. What's troubling is that the stack of sm83 is in the WRAM where banking is done. If we switch the bank, the stack looks like it's going to be broken. (I've never tested it, so I'm not sure.) In addition, arrays sized at compilation time appear to be stored in the stack. Therefore, the stack can become very large. |
Beta Was this translation helpful? Give feedback.
-
Hello, this will hopefully be the first part of a larger design discussion. I love the idea of being able to compile Rust code for the GBC; however, in order to do so safely, parts of the hardware need to be modeled at some level point.
Unlike most modern computers, the Gameboy had a non-linear memory space. In other words, multiple physical chips were mapped to the same memory addresses. This was controlled by various registers in the memory space. For example, the Gameboy Color had a WRAM selection register at
0xFF70
. Changing this register would change what chip was used in the address range0xD000..=0xDFFF
.Why is this an issue? Rust's memory safety rules assume that your memory safe does not have connections like this. That is, if you have mutable access to a given address/address slice, nothing can change the values you are pointing at except you. This is not true for the GB. If you have a reference into the WRAM address space, the value you are pointing at can be changed by writing to the selection register.
What am I proposing? We should provide a crate that models the hardware behavior for users. This would look something like a HAL (hardware abstraction layer). The goal of this library would be to capture the behavior of the hardware in a safe way so that users don't have to write
unsafe
code. Part of capturing theunsafe
code would be us upholding the necessary safety rules, and handling non-linear memory safety would be a good starting point.Beta Was this translation helpful? Give feedback.
All reactions