Skip to content

Commit

Permalink
Major fixes to joypad and gpu
Browse files Browse the repository at this point in the history
  • Loading branch information
bfsgr authored Jun 24, 2020
2 parents 2eb3b9e + 6eadeb8 commit 0bb9a78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
34 changes: 18 additions & 16 deletions src/emulator/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ impl GPU {
t1 = GPU::reverse_order(t1);
t2 = GPU::reverse_order(t2);

let b1 = t1.test_bit(px);
let b0 = t2.test_bit(px);
let b1 = t2.test_bit(px);
let b0 = t1.test_bit(px);

let pixel = (b1 as u8) << 1 | b0 as u8; //>

Expand All @@ -324,8 +324,14 @@ impl GPU {
let palette = self.bg_palette;

let WY = self.window_y;
let WX = self.window_x;
//pixels 0..=7 aren't visible
let WX = self.window_x.wrapping_sub(7);

let LY = self.lcd_y;

//window does not appear in this row
if LY < WY || WY > 143 { return (); }
if WX > 159 { return (); }
//tile map address base
let tile_map_addr = match self.LCDC.test_bit(6) {
true => 0x9C00,
Expand All @@ -338,15 +344,10 @@ impl GPU {
false => 0x9000
};

let LY = self.lcd_y;

let buffer: u32 = LY as u32 * 160;

let row = LY / 8;

//window does not appear in this row
if LY < WY || WY > 143 { return (); }
if WX > 159 { return (); }
let row = (LY - WY) / 8;

for i in WX..160 {

Expand Down Expand Up @@ -389,8 +390,8 @@ impl GPU {
t1 = GPU::reverse_order(t1);
t2 = GPU::reverse_order(t2);

let b1 = t1.test_bit(px);
let b0 = t2.test_bit(px);
let b1 = t2.test_bit(px);
let b0 = t1.test_bit(px);

let pixel = (b1 as u8) << 1 | b0 as u8; //>

Expand All @@ -407,6 +408,7 @@ impl GPU {

let ly = self.lcd_y;
let tall = self.LCDC.test_bit(2);
let buffer = ly as u32 * 160;

for sprite in visible.iter().rev() {
let sx = sprite.x;
Expand Down Expand Up @@ -466,8 +468,8 @@ impl GPU {
t1 = GPU::reverse_order(t1);
t2 = GPU::reverse_order(t2);

let b1 = t1.test_bit(px);
let b0 = t2.test_bit(px);
let b1 = t2.test_bit(px);
let b0 = t1.test_bit(px);

let pixel = (b1 as u8) << 1 | b0 as u8; //>

Expand All @@ -477,7 +479,7 @@ impl GPU {

if sprite.priority && priority[actual_x as usize] { continue; }

self.display[(ly as u16 * 160 + actual_x as u16) as usize] = drawn;
self.display[(buffer + actual_x as u32) as usize] = drawn;
}

}
Expand All @@ -493,8 +495,8 @@ impl GPU {
fn to_rgb(&self, pixel: u8, palette: u8) -> u32{
let colors = [
0xE0F8D0, // 0 White
0x346856, // 1 Light Gray
0x88C070, // 2 Dark Gray
0x88C070, // 1 Light Gray
0x346856, // 2 Dark Gray
0x081820, // 3 Black
];

Expand Down
20 changes: 8 additions & 12 deletions src/emulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Gameboy {
}

fn get_input(window: &Window, joypad: &mut Joypad, interrupts: &mut InterruptHandler) {
if window.is_key_pressed(Key::Up, KeyRepeat::Yes) {
if window.is_key_down(Key::Up) {
let int = joypad.up(true);

if int == Interrupt::Joypad {
Expand All @@ -103,7 +103,7 @@ impl Gameboy {
} else {
joypad.up(false);
}
if window.is_key_pressed(Key::Down, KeyRepeat::Yes) {
if window.is_key_down(Key::Down) {
let int = joypad.down(true);

if int == Interrupt::Joypad {
Expand All @@ -113,7 +113,7 @@ impl Gameboy {
joypad.down(false);
}

if window.is_key_pressed(Key::Left, KeyRepeat::Yes) {
if window.is_key_down(Key::Left) {
let int = joypad.left(true);

if int == Interrupt::Joypad {
Expand All @@ -123,7 +123,7 @@ impl Gameboy {
joypad.left(false);
}

if window.is_key_pressed(Key::Right, KeyRepeat::Yes) {
if window.is_key_down(Key::Right) {
let int = joypad.right(true);

if int == Interrupt::Joypad {
Expand All @@ -133,7 +133,7 @@ impl Gameboy {
joypad.right(false);
}

if window.is_key_pressed(Key::F, KeyRepeat::Yes) {
if window.is_key_down(Key::F) {
let int = joypad.start(true);

if int == Interrupt::Joypad {
Expand All @@ -143,7 +143,7 @@ impl Gameboy {
joypad.start(false);
}

if window.is_key_pressed(Key::Z, KeyRepeat::Yes) {
if window.is_key_down(Key::Z) {
let int = joypad.btn_a(true);

if int == Interrupt::Joypad {
Expand All @@ -153,7 +153,7 @@ impl Gameboy {
joypad.btn_a(false);
}

if window.is_key_pressed(Key::X, KeyRepeat::Yes) {
if window.is_key_down(Key::X) {
let int = joypad.btn_b(true);

if int == Interrupt::Joypad {
Expand All @@ -163,7 +163,7 @@ impl Gameboy {
joypad.btn_b(false);
}

if window.is_key_pressed(Key::G, KeyRepeat::Yes) {
if window.is_key_down(Key::G) {
let select = joypad.select(true);
if select == Interrupt::Joypad {
interrupts.request(Interrupt::Joypad);
Expand All @@ -173,10 +173,6 @@ impl Gameboy {
}
}

fn interrupt_running(&self) -> bool {
self.bus.interrupts.enable & 0x00FF != 0
}

fn create_window() -> Window {
let win = Window::new(
"Rusty GB",
Expand Down

0 comments on commit 0bb9a78

Please sign in to comment.