Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential panic in memory bus code due to off-by-one error #73

Open
MolotovCherry opened this issue Mar 25, 2024 · 0 comments
Open

Potential panic in memory bus code due to off-by-one error #73

MolotovCherry opened this issue Mar 25, 2024 · 0 comments

Comments

@MolotovCherry
Copy link

MolotovCherry commented Mar 25, 2024

This code here defines memory to be 0xFFFF (65535) long, which means you have 0-65534 as a usable range. If you address index 0xFFFF (65535) here, it will panic since that is 1 more than the length we defined. And according to the manuals I read, 0xFFFF is technically a valid addressable part of memory. So the length should be 0x10000 (65536; 0-65535 which makes 0xFFFF addressable) instead of 0xFFFF.

This also has the nice benefit of letting rust optimize out the bounds check.

struct MemoryBus {
  memory: [u8; 0xFFFF]
}

impl MemoryBus {
  fn read_byte(&self, address: u16) -> u8 {
    self.memory[address as usize]
  }
}

(And for example, in this chapter, it says "(0xFFFF or 65,536 of them to be exact)", but 0xFFFF is 65535 not 65536)
https://rylev.github.io/DMG-01/public/book/cpu/executing_instructions.html

@MolotovCherry MolotovCherry changed the title Potential panic in memory bus code Potential panic in memory bus code due to off-by-one error Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant