core: bus: Change read_x methods of Bus trait to take &mut self

Former-commit-id: ee95b949585420e1daf95ea50939b1a8c9b77349
Former-commit-id: 651203037284fc46eb669cd0e40dc2ebd85bd96b
This commit is contained in:
Michel Heily 2020-10-17 05:39:01 -07:00 committed by MishMish
parent 85bef5a1c0
commit 3fa858f969
9 changed files with 44 additions and 41 deletions

View file

@ -1,20 +1,20 @@
pub type Addr = u32;
pub trait Bus {
fn read_32(&self, addr: Addr) -> u32 {
fn read_32(&mut self, addr: Addr) -> u32 {
self.read_16(addr) as u32 | (self.read_16(addr + 2) as u32) << 16
}
fn read_16(&self, addr: Addr) -> u16 {
fn read_16(&mut self, addr: Addr) -> u16 {
self.default_read_16(addr)
}
#[inline(always)]
fn default_read_16(&self, addr: Addr) -> u16 {
fn default_read_16(&mut self, addr: Addr) -> u16 {
self.read_8(addr) as u16 | (self.read_8(addr + 1) as u16) << 8
}
fn read_8(&self, addr: Addr) -> u8;
fn read_8(&mut self, addr: Addr) -> u8;
fn write_32(&mut self, addr: Addr, value: u32) {
self.write_16(addr, (value & 0xffff) as u16);
@ -33,7 +33,7 @@ pub trait Bus {
fn write_8(&mut self, addr: Addr, value: u8);
fn get_bytes(&self, range: std::ops::Range<u32>) -> Vec<u8> {
fn get_bytes(&mut self, range: std::ops::Range<u32>) -> Vec<u8> {
let mut bytes = Vec::new();
for b in range {
bytes.push(self.read_8(b));
@ -44,17 +44,17 @@ pub trait Bus {
/// Helper trait for reading memory as if we were an all-powerfull debugger
pub trait DebugRead: Bus {
fn debug_read_32(&self, addr: Addr) -> u32 {
fn debug_read_32(&mut self, addr: Addr) -> u32 {
self.debug_read_16(addr) as u32 | (self.debug_read_16(addr + 2) as u32) << 16
}
fn debug_read_16(&self, addr: Addr) -> u16 {
fn debug_read_16(&mut self, addr: Addr) -> u16 {
self.debug_read_8(addr) as u16 | (self.debug_read_8(addr + 1) as u16) << 8
}
fn debug_read_8(&self, addr: Addr) -> u8;
fn debug_read_8(&mut self, addr: Addr) -> u8;
fn debug_get_bytes(&self, range: std::ops::Range<u32>) -> Vec<u8> {
fn debug_get_bytes(&mut self, range: std::ops::Range<u32>) -> Vec<u8> {
let mut bytes = Vec::new();
for b in range {
bytes.push(self.debug_read_8(b));

View file

@ -64,7 +64,7 @@ impl Cartridge {
&self.bytes
}
// 'clones' the cartridge without the ROM buffer
// 'Clones' the cartridge without the ROM buffer
pub fn thin_copy(&self) -> Cartridge {
Cartridge {
header: self.header.clone(),
@ -96,7 +96,7 @@ fn is_gpio_access(addr: u32) -> bool {
}
impl Bus for Cartridge {
fn read_8(&self, addr: Addr) -> u8 {
fn read_8(&mut self, addr: Addr) -> u8 {
let offset = (addr & 0x01ff_ffff) as usize;
match addr & 0xff000000 {
SRAM_LO | SRAM_HI => match &self.backup {
@ -114,7 +114,7 @@ impl Bus for Cartridge {
}
}
fn read_16(&self, addr: u32) -> u16 {
fn read_16(&mut self, addr: u32) -> u16 {
if is_gpio_access(addr) {
if let Some(gpio) = &self.gpio {
if !(gpio.is_readable()) {
@ -165,7 +165,7 @@ impl Bus for Cartridge {
}
impl DebugRead for Cartridge {
fn debug_read_8(&self, addr: Addr) -> u8 {
fn debug_read_8(&mut self, addr: Addr) -> u8 {
let offset = (addr & 0x01ff_ffff) as usize;
self.bytes[offset]
}

View file

@ -298,7 +298,7 @@ impl Gpu {
}
/// helper method that reads the palette index from a base address and x + y
pub fn read_pixel_index(&self, addr: u32, x: u32, y: u32, format: PixelFormat) -> usize {
pub fn read_pixel_index(&mut self, addr: u32, x: u32, y: u32, format: PixelFormat) -> usize {
match format {
PixelFormat::BPP4 => self.read_pixel_index_bpp4(addr, x, y),
PixelFormat::BPP8 => self.read_pixel_index_bpp8(addr, x, y),
@ -306,7 +306,7 @@ impl Gpu {
}
#[inline]
pub fn read_pixel_index_bpp4(&self, addr: u32, x: u32, y: u32) -> usize {
pub fn read_pixel_index_bpp4(&mut self, addr: u32, x: u32, y: u32) -> usize {
let ofs = addr + index2d!(u32, x / 2, y, 4);
let ofs = ofs as usize;
let byte = self.vram.read_8(ofs as u32);
@ -318,13 +318,13 @@ impl Gpu {
}
#[inline]
pub fn read_pixel_index_bpp8(&self, addr: u32, x: u32, y: u32) -> usize {
pub fn read_pixel_index_bpp8(&mut self, addr: u32, x: u32, y: u32) -> usize {
let ofs = addr;
self.vram.read_8(ofs + index2d!(u32, x, y, 8)) as usize
}
#[inline(always)]
pub fn get_palette_color(&self, index: u32, palette_bank: u32, offset: u32) -> Rgb15 {
pub fn get_palette_color(&mut self, index: u32, palette_bank: u32, offset: u32) -> Rgb15 {
if index == 0 || (palette_bank != 0 && index % 16 == 0) {
return Rgb15::TRANSPARENT;
}
@ -659,7 +659,7 @@ impl Gpu {
}
impl Bus for Gpu {
fn read_8(&self, addr: Addr) -> u8 {
fn read_8(&mut self, addr: Addr) -> u8 {
let page = (addr >> 24) as usize;
match page {
PAGE_PALRAM => self.palette_ram.read_8(addr & 0x3ff),
@ -716,7 +716,7 @@ impl Bus for Gpu {
}
impl DebugRead for Gpu {
fn debug_read_8(&self, addr: Addr) -> u8 {
fn debug_read_8(&mut self, addr: Addr) -> u8 {
let page = (addr >> 24) as usize;
match page {
PAGE_PALRAM => self.palette_ram.read_8(addr & 0x3ff),

View file

@ -55,7 +55,7 @@ impl ObjAttrs {
}
impl Gpu {
fn get_affine_matrix(&self, affine_index: u32) -> AffineMatrix {
fn get_affine_matrix(&mut self, affine_index: u32) -> AffineMatrix {
let mut offset = AFFINE_FILL + affine_index * 16 * 2;
let pa = self.oam.read_16(offset) as i16 as i32;
offset += 2 + AFFINE_FILL;
@ -68,7 +68,7 @@ impl Gpu {
AffineMatrix { pa, pb, pc, pd }
}
fn read_obj_attrs(&self, obj: usize) -> ObjAttrs {
fn read_obj_attrs(&mut self, obj: usize) -> ObjAttrs {
let addr = ATTRS_SIZE * (obj as u32);
let attr0 = Attribute0(self.oam.read_16(addr + 0));
let attr1 = Attribute1(self.oam.read_16(addr + 2));

View file

@ -124,16 +124,13 @@ impl Gpu {
let tile_index = self.vram.read_8(map_addr) as u32;
let tile_addr = char_block + tile_index * 0x40;
let color = self.get_palette_color(
self.read_pixel_index(
tile_addr,
(t.0 % 8) as u32,
(t.1 % 8) as u32,
PixelFormat::BPP8,
) as u32,
0,
0,
);
let pixel_index = self.read_pixel_index(
tile_addr,
(t.0 % 8) as u32,
(t.1 % 8) as u32,
PixelFormat::BPP8,
) as u32;
let color = self.get_palette_color(pixel_index, 0, 0);
self.backgrounds[bg].line[screen_x as usize] = color;
}
}

View file

@ -59,7 +59,7 @@ impl Gpu {
}
#[allow(unused)]
fn layer_to_pixel(&self, x: usize, y: usize, layer: &RenderLayer) -> Rgb15 {
fn layer_to_pixel(&mut self, x: usize, y: usize, layer: &RenderLayer) -> Rgb15 {
match layer.kind {
RenderLayerKind::Background0 => self.backgrounds[0].line[x],
RenderLayerKind::Background1 => self.backgrounds[1].line[x],

View file

@ -78,7 +78,7 @@ impl InterruptConnect for IoDevices {
}
impl Bus for IoDevices {
fn read_16(&self, addr: Addr) -> u16 {
fn read_16(&mut self, addr: Addr) -> u16 {
let io = self;
let io_addr = addr + IO_BASE;
if addr > 0x0800 {
@ -134,7 +134,7 @@ impl Bus for IoDevices {
}
}
fn read_8(&self, addr: Addr) -> u8 {
fn read_8(&mut self, addr: Addr) -> u8 {
let t = self.read_16(addr & !1);
if addr & 1 != 0 {
(t >> 8) as u8
@ -300,7 +300,7 @@ impl Bus for IoDevices {
}
impl DebugRead for IoDevices {
fn debug_read_8(&self, addr: Addr) -> u8 {
fn debug_read_8(&mut self, addr: Addr) -> u8 {
self.read_8(addr)
}
}

View file

@ -288,7 +288,8 @@ impl SysBus {
}
impl Bus for SysBus {
fn read_32(&self, addr: Addr) -> u32 {
#[inline]
fn read_32(&mut self, addr: Addr) -> u32 {
match addr & 0xff000000 {
BIOS_ADDR => self.bios.read_32(addr),
EWRAM_ADDR => self.onboard_work_ram.read_32(addr & 0x3_fffc),
@ -314,7 +315,8 @@ impl Bus for SysBus {
}
}
fn read_16(&self, addr: Addr) -> u16 {
#[inline]
fn read_16(&mut self, addr: Addr) -> u16 {
match addr & 0xff000000 {
BIOS_ADDR => self.bios.read_16(addr),
EWRAM_ADDR => self.onboard_work_ram.read_16(addr & 0x3_fffe),
@ -340,7 +342,8 @@ impl Bus for SysBus {
}
}
fn read_8(&self, addr: Addr) -> u8 {
#[inline]
fn read_8(&mut self, addr: Addr) -> u8 {
match addr & 0xff000000 {
BIOS_ADDR => self.bios.read_8(addr),
EWRAM_ADDR => self.onboard_work_ram.read_8(addr & 0x3_ffff),
@ -366,6 +369,7 @@ impl Bus for SysBus {
}
}
#[inline]
fn write_32(&mut self, addr: Addr, value: u32) {
match addr & 0xff000000 {
BIOS_ADDR => {}
@ -390,6 +394,7 @@ impl Bus for SysBus {
}
}
#[inline]
fn write_16(&mut self, addr: Addr, value: u16) {
match addr & 0xff000000 {
BIOS_ADDR => {}
@ -414,6 +419,7 @@ impl Bus for SysBus {
}
}
#[inline]
fn write_8(&mut self, addr: Addr, value: u8) {
match addr & 0xff000000 {
BIOS_ADDR => {}
@ -440,7 +446,7 @@ impl Bus for SysBus {
}
impl DebugRead for SysBus {
fn debug_read_8(&self, addr: Addr) -> u8 {
fn debug_read_8(&mut self, addr: Addr) -> u8 {
match addr & 0xff000000 {
BIOS_ADDR => self.bios.debug_read_8(addr),
EWRAM_ADDR => self.onboard_work_ram.debug_read_8(addr & 0x3_ffff),

View file

@ -274,7 +274,7 @@ impl BoxedMemory {
}
impl Bus for BoxedMemory {
fn read_8(&self, addr: Addr) -> u8 {
fn read_8(&mut self, addr: Addr) -> u8 {
unsafe { *self.mem.get_unchecked(addr as usize) }
}
@ -286,7 +286,7 @@ impl Bus for BoxedMemory {
}
impl DebugRead for BoxedMemory {
fn debug_read_8(&self, addr: Addr) -> u8 {
fn debug_read_8(&mut self, addr: Addr) -> u8 {
self.mem[addr as usize]
}
}