Skip to content

Commit

Permalink
chore: implementation of the BusComponent
Browse files Browse the repository at this point in the history
More standard support for the `BusComponent` interface.
  • Loading branch information
joamag committed Mar 3, 2024
1 parent da268f3 commit 1be6a7f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/apu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::VecDeque;

use crate::{gb::GameBoy, warnln};
use crate::{gb::GameBoy, mmu::BusComponent, warnln};

const DUTY_TABLE: [[u8; 8]; 4] = [
[0, 0, 0, 0, 0, 0, 0, 1],
Expand Down Expand Up @@ -1076,6 +1076,16 @@ impl Apu {
}
}

impl BusComponent for Apu {
fn read(&mut self, addr: u16) -> u8 {
self.read(addr)
}

fn write(&mut self, addr: u16, value: u8) {
self.write(addr, value);
}
}

impl Default for Apu {
fn default() -> Self {
Self::new(44100, 2, 1.0, GameBoy::CPU_FREQ)
Expand Down
11 changes: 11 additions & 0 deletions src/dma.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
consts::{DMA_ADDR, HDMA1_ADDR, HDMA2_ADDR, HDMA3_ADDR, HDMA4_ADDR, HDMA5_ADDR},
mmu::BusComponent,
warnln,
};

Expand Down Expand Up @@ -173,6 +174,16 @@ impl Dma {
}
}

impl BusComponent for Dma {
fn read(&mut self, addr: u16) -> u8 {
self.read(addr)
}

fn write(&mut self, addr: u16, value: u8) {
self.write(addr, value);
}
}

impl Default for Dma {
fn default() -> Self {
Self::new()
Expand Down
15 changes: 15 additions & 0 deletions src/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ pub const BOOT_SIZE_CGB: usize = 2304;
pub const RAM_SIZE_DMG: usize = 8192;
pub const RAM_SIZE_CGB: usize = 32768;

pub trait BusComponent {
fn read(&mut self, addr: u16) -> u8;
fn write(&mut self, addr: u16, value: u8);
fn read_many(&mut self, addr: u16, count: usize) -> Vec<u8> {
(0..count)
.map(|offset| self.read(addr + offset as u16))
.collect()
}
fn write_many(&mut self, addr: u16, values: &[u8]) {
for (offset, &value) in values.iter().enumerate() {
self.write(addr + offset as u16, value);
}
}
}

pub struct Mmu {
/// Register that controls the interrupts that are considered
/// to be enabled and should be triggered.
Expand Down
12 changes: 11 additions & 1 deletion src/pad.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Gamepad related functions and structures.
use crate::warnln;
use crate::{mmu::BusComponent, warnln};

#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -167,6 +167,16 @@ impl Pad {
}
}

impl BusComponent for Pad {
fn read(&mut self, addr: u16) -> u8 {
self.read(addr)
}

fn write(&mut self, addr: u16, value: u8) {
self.write(addr, value);
}
}

impl Default for Pad {
fn default() -> Self {
Self::new()
Expand Down
11 changes: 11 additions & 0 deletions src/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{

use crate::{
gb::{GameBoyConfig, GameBoyMode},
mmu::BusComponent,
util::SharedThread,
warnln,
};
Expand Down Expand Up @@ -2091,6 +2092,16 @@ impl Ppu {
}
}

impl BusComponent for Ppu {
fn read(&mut self, addr: u16) -> u8 {
self.read(addr)
}

fn write(&mut self, addr: u16, value: u8) {
self.write(addr, value);
}
}

impl Default for Ppu {
fn default() -> Self {
Self::new(
Expand Down
13 changes: 12 additions & 1 deletion src/rom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
debugln,
error::Error,
gb::GameBoyMode,
mmu::BusComponent,
util::read_file,
warnln,
};
Expand Down Expand Up @@ -370,7 +371,7 @@ impl Cartridge {
Self::from_data(&data)
}

pub fn read(&self, addr: u16) -> u8 {
pub fn read(&mut self, addr: u16) -> u8 {
match addr & 0xf000 {
0x0000 | 0x1000 | 0x2000 | 0x3000 | 0x4000 | 0x5000 | 0x6000 | 0x7000 => {
(self.handler.read_rom)(self, addr)
Expand Down Expand Up @@ -823,6 +824,16 @@ impl Cartridge {
}
}

impl BusComponent for Cartridge {
fn read(&mut self, addr: u16) -> u8 {
self.read(addr)
}

fn write(&mut self, addr: u16, value: u8) {
self.write(addr, value);
}
}

impl Default for Cartridge {
fn default() -> Self {
Self::new()
Expand Down
12 changes: 11 additions & 1 deletion src/serial.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::warnln;
use crate::{mmu::BusComponent, warnln};

pub trait SerialDevice {
/// Sends a byte (u8) to the attached serial connection.
Expand Down Expand Up @@ -202,6 +202,16 @@ impl Serial {
}
}

impl BusComponent for Serial {
fn read(&mut self, addr: u16) -> u8 {
self.read(addr)
}

fn write(&mut self, addr: u16, value: u8) {
self.write(addr, value);
}
}

impl Default for Serial {
fn default() -> Self {
Self::new()
Expand Down
11 changes: 11 additions & 0 deletions src/timer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
consts::{DIV_ADDR, TAC_ADDR, TIMA_ADDR, TMA_ADDR},
mmu::BusComponent,
warnln,
};

Expand Down Expand Up @@ -147,6 +148,16 @@ impl Timer {
}
}

impl BusComponent for Timer {
fn read(&mut self, addr: u16) -> u8 {
self.read(addr)
}

fn write(&mut self, addr: u16, value: u8) {
self.write(addr, value);
}
}

impl Default for Timer {
fn default() -> Self {
Self::new()
Expand Down

0 comments on commit 1be6a7f

Please sign in to comment.