From 19f31cfbbf19d7df11ebf3ec3b1eb16493dc9298 Mon Sep 17 00:00:00 2001 From: Kevin Hamacher Date: Wed, 1 Jun 2016 15:02:43 +0200 Subject: [PATCH] Fix some overflows --- src/cpu.rs | 2 +- src/display.rs | 2 +- src/mbc/mbc.rs | 9 +++++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index 3f72d10..0894e21 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -120,7 +120,7 @@ impl CPU { self.clear_flag(FLAG_N); self.set_clear_flag(FLAG_Z, new == 0); - self.set_clear_flag(FLAG_C, ((val + c) > 0 && new < old) || (c == 1 && new == old)); + self.set_clear_flag(FLAG_C, (new < old) || (c == 1 && new == old)); self.set_clear_flag(FLAG_H, ((old & 0x0F) + (val & 0x0F) + c) > 0x0F); } diff --git a/src/display.rs b/src/display.rs index b7b7a9c..f06e4dd 100644 --- a/src/display.rs +++ b/src/display.rs @@ -418,7 +418,7 @@ impl Display { } // Draw stuff. We're currently only in monochrome mode - self.set_pixel(x + x_o, render_y, sdl2::pixels::Color::RGB(factor, factor, factor)); + self.set_pixel(x.wrapping_add(x_o), render_y, sdl2::pixels::Color::RGB(factor, factor, factor)); } } diff --git a/src/mbc/mbc.rs b/src/mbc/mbc.rs index 84b43af..27e3e2d 100644 --- a/src/mbc/mbc.rs +++ b/src/mbc/mbc.rs @@ -20,7 +20,7 @@ impl NoMBC { impl MBC for NoMBC { fn write_byte(&mut self, addr: u16, val: u8) { - panic!("Writing not supported for cartridges without MBC."); + println!("Writing not supported for cartridges without MBC. (Tried to set {:04X} to {:02X})", addr, val); } fn read_byte(&self, addr: u16) -> u8 { @@ -30,7 +30,12 @@ impl MBC for NoMBC { // TODO: Check for ram let addr = (addr as usize) - 0xA000; - self.ram[addr] + if addr >= self.ram.len() { + println!("Tried to access {:04X}, however the memory is not present.", addr + 0xA000); + 0 + } else { + self.ram[addr] + } } _ => { panic!("Cartride: Unable to read from {:04X}", addr);