GBC: Fix vram selection

This commit is contained in:
Kevin Hamacher 2020-02-18 14:35:11 +01:00
parent 79e31318e0
commit 261fa4d78a

View File

@ -64,6 +64,9 @@ const MONOCHROME_PALETTE: &'static [[u8; 3]; 4] = &[
struct CgbPalette([u8; 8]);
impl CgbPalette {
fn get_color(&self, n: usize) -> sdl2::pixels::Color {
if n == 0 {
return sdl2::pixels::Color::RGB(255, 255, 255);
}
let v = ((self.0[2 * n + 1] as u16) << 8) | (self.0[2 * n] as u16);
let r = (v & 0b1_1111) as u8;
let g = ((v >> 5) & 0b1_1111) as u8;
@ -333,7 +336,7 @@ impl Display {
pub fn write_byte(&mut self, addr: u16, val: u8) {
match addr {
0x8000..=0x9FFF => {
if self.vram_bank == 0 {
if (self.vram_bank & 1) == 0 {
self.vram0[(addr - 0x8000) as usize] = val
} else {
self.vram1[(addr - 0x8000) as usize] = val
@ -392,7 +395,7 @@ impl Display {
pub fn read_byte(&self, addr: u16) -> u8 {
match addr {
0x8000..=0x9FFF => {
if self.vram_bank == 0 {
if (self.vram_bank & 1) == 0 {
self.vram0[(addr - 0x8000) as usize]
} else {
self.vram1[(addr - 0x8000) as usize]
@ -431,6 +434,8 @@ impl Display {
let vram_offset: usize =
background_map + ((tile_index_y as usize) * 32) + tile_index_x as usize;
*/
// Background attributes are only in this memory range.
assert!(vram_offset >= 0x1800 && vram_offset < 0x2000, format!("offset: {:04X}", vram_offset));
BgMapAttributes(self.vram1[vram_offset])
}
@ -669,6 +674,11 @@ impl Display {
}
}
pub fn dump_vram(&self) {
std::fs::File::create("vram0.dat").unwrap().write_all(&self.vram0);
std::fs::File::create("vram1.dat").unwrap().write_all(&self.vram1);
}
#[inline]
fn renderscan(&mut self) {
// Points to the background map offset to use.
@ -738,13 +748,9 @@ impl Display {
let tile_id = self.vram0[vram_offset];
// GBC stuff
let bg_attribs = self.get_background_attribute(vram_offset);
// Get BG map attributes
let vram = if bg_attribs.vram_bank_number() == 0 {
&*self.vram0
} else {
&*self.vram1
};
let bg_attribs = self.get_background_attribute(vram_offset);
let vram = [&*self.vram0, &*self.vram1][bg_attribs.vram_bank_number()];
// TODO: Priority
let tile_offset_y = if bg_attribs.vertical_flip() {