Skip to content

Commit

Permalink
Refactor Port to be MOS 652x specific, add separate trait for MOS 6510
Browse files Browse the repository at this point in the history
  • Loading branch information
breqdev committed Apr 27, 2024
1 parent 69ba92e commit 1ac18f9
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 34 deletions.
5 changes: 1 addition & 4 deletions src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ mod banked;
mod block;
mod branch;
mod logging;
mod mos6510;
pub mod mos6510;
/// The various interface adapters (6520, 6522, 6526) for the MOS 6502 CPU.
pub mod mos652x;
mod null;
mod ports;

pub use banked::BankedMemory;
pub use block::BlockMemory;
pub use branch::BranchMemory;
pub use logging::LoggingMemory;
pub use mos6510::Mos6510Port;
pub use null::NullMemory;
pub use ports::{NullPort, Port};

/// Represents the state of the interrupts on the system.
#[derive(Debug, PartialEq, Eq)]
Expand Down
18 changes: 12 additions & 6 deletions src/memory/mos6510.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use super::{ActiveInterrupt, Memory, Port};
use super::{ActiveInterrupt, Memory};

pub trait Mos6510PortInterface {
fn reset(&mut self);
fn read(&mut self) -> u8;
fn write(&mut self, value: u8);
}

/// Represents the port built into a MOS 6510 processor, mapped to memory addresses 0x0000 (for the DDR) and 0x0001 (for the port itself).
pub struct Mos6510Port {
/// The port itself.
port: Box<dyn Port>,
port: Box<dyn Mos6510PortInterface>,

/// If the DDR is write, the current written value.
writes: u8,
Expand All @@ -14,7 +20,7 @@ pub struct Mos6510Port {

impl Mos6510Port {
/// Create a new MOS 6510 port with the given port.
pub fn new(port: Box<dyn Port>) -> Self {
pub fn new(port: Box<dyn Mos6510PortInterface>) -> Self {

Check warning on line 23 in src/memory/mos6510.rs

View check run for this annotation

Codecov / codecov/patch

src/memory/mos6510.rs#L23

Added line #L23 was not covered by tests
Self {
port,
writes: 0,
Expand All @@ -27,7 +33,7 @@ impl Memory for Mos6510Port {
fn read(&mut self, address: u16) -> u8 {
match address % 2 {
0 => self.ddr,
1 => (self.port.read_data() & !self.ddr) | (self.writes & self.ddr),
1 => (self.port.read() & !self.ddr) | (self.writes & self.ddr),
_ => unreachable!(),
}
}
Expand All @@ -36,11 +42,11 @@ impl Memory for Mos6510Port {
match address % 2 {
0 => {
self.ddr = value;
self.port.write_data(self.writes & self.ddr);
self.port.write(self.writes & self.ddr);
}
1 => {
self.writes = value;
self.port.write_data(value & self.ddr);
self.port.write(value & self.ddr);
}
_ => unreachable!(),
}
Expand Down
6 changes: 3 additions & 3 deletions src/memory/mos652x/cia.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::memory::{
mos652x::{InterruptRegister, PortRegisters, ShiftRegister, Timer},
ActiveInterrupt, Memory, Port,
mos652x::{InterruptRegister, Port, PortRegisters, ShiftRegister, Timer},
ActiveInterrupt, Memory,
};

struct TimeRegisters {
Expand Down Expand Up @@ -234,7 +234,7 @@ impl Memory for Cia {

#[cfg(test)]
mod tests {
use crate::memory::NullPort;
use crate::memory::mos652x::NullPort;

use super::*;

Expand Down
4 changes: 2 additions & 2 deletions src/memory/mos652x/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod cia;
mod pia;
mod port;
mod via;

pub use cia::Cia;
pub use pia::Pia;
pub use port::{NullPort, Port};
pub use via::Via;

use crate::memory::Port;

#[derive(PartialEq)]
pub enum ActiveTransition {
Rising,
Expand Down
4 changes: 2 additions & 2 deletions src/memory/mos652x/pia.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::memory::{ActiveInterrupt, Memory, Port};
use crate::memory::{mos652x::Port, ActiveInterrupt, Memory};

// MOS 6520

Expand Down Expand Up @@ -180,7 +180,7 @@ impl Memory for Pia {

#[cfg(test)]
mod tests {
use crate::memory::NullPort;
use crate::memory::mos652x::NullPort;

use super::*;

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/memory/mos652x/via.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::memory::{
mos652x::{InterruptRegister, PortRegisters, ShiftRegister, Timer, TimerOutput},
ActiveInterrupt, Memory, Port,
mos652x::{InterruptRegister, Port, PortRegisters, ShiftRegister, Timer, TimerOutput},
ActiveInterrupt, Memory,
};

use super::ActiveTransition;
Expand Down Expand Up @@ -256,7 +256,7 @@ impl Memory for Via {

#[cfg(test)]
mod tests {
use crate::memory::NullPort;
use crate::memory::mos652x::NullPort;

use super::*;

Expand Down
16 changes: 6 additions & 10 deletions src/systems/c64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use crate::{
KeyAdapter, KeyMappingStrategy, SymbolAdapter,
},
memory::{
mos652x::Cia, BankedMemory, BlockMemory, BranchMemory, Mos6510Port, NullMemory, NullPort, Port,
mos6510::{Mos6510Port, Mos6510PortInterface},
mos652x::{Cia, NullPort, Port},
BankedMemory, BlockMemory, BranchMemory, NullMemory,
},
platform::{PlatformProvider, WindowConfig},
systems::System,
Expand Down Expand Up @@ -154,13 +156,13 @@ impl C64BankSwitching {
}
}

impl Port for C64BankSwitching {
fn read_data(&mut self) -> u8 {
impl Mos6510PortInterface for C64BankSwitching {
fn read(&mut self) -> u8 {
(self.loram as u8) | (self.hiram as u8) << 1 | (self.charen as u8) << 2
}

#[allow(clippy::bool_to_int_with_if)]
fn write_data(&mut self, value: u8) {
fn write(&mut self, value: u8) {
self.loram = (value & 0b001) != 0;
self.hiram = (value & 0b010) != 0;
self.charen = (value & 0b100) != 0;
Expand Down Expand Up @@ -192,17 +194,11 @@ impl Port for C64BankSwitching {
self.selectors[5].set(if !self.hiram { 1 } else { 0 });
}

fn read_control(&mut self, _cycles_since_poll: u64, _total_cycle_count: u64) -> (bool, bool) {
(false, false)
}

fn reset(&mut self) {
self.hiram = true;
self.loram = true;
self.charen = true;
}

fn write_cx2(&mut self, _value: bool) {}
}

/// Configuration for a Commodore 64 system.
Expand Down
4 changes: 2 additions & 2 deletions src/systems/pet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::cpu::{
Cpu,
};
use crate::keyboard::{KeyAdapter, KeyMappingStrategy, SymbolAdapter};
use crate::memory::mos652x::{Pia, Via};
use crate::memory::{BlockMemory, BranchMemory, NullMemory, NullPort, Port};
use crate::memory::mos652x::{NullPort, Pia, Port, Via};
use crate::memory::{BlockMemory, BranchMemory, NullMemory};
use crate::platform::{Color, PlatformProvider, WindowConfig};
use crate::systems::{BuildableSystem, System};
use instant::Instant;
Expand Down
4 changes: 2 additions & 2 deletions src/systems/vic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::keyboard::{
commodore::{C64KeyboardAdapter, C64SymbolAdapter},
KeyAdapter, KeyMappingStrategy, SymbolAdapter,
};
use crate::memory::mos652x::Via;
use crate::memory::{BlockMemory, BranchMemory, NullMemory, NullPort, Port};
use crate::memory::mos652x::{NullPort, Port, Via};
use crate::memory::{BlockMemory, BranchMemory, NullMemory};
use crate::platform::{PlatformProvider, WindowConfig};
use crate::roms::RomFile;
use crate::systems::System;
Expand Down

0 comments on commit 1ac18f9

Please sign in to comment.