Skip to content

Commit

Permalink
Remove SystemBuilder structs, refactor trait into BuildableSystem
Browse files Browse the repository at this point in the history
Co-authored-by: Ava Silver <ava@avasilver.dev>
  • Loading branch information
breqdev and ava-silver committed Dec 30, 2023
1 parent c62509c commit 712012e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 42 deletions.
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use libnoentiendo::{
platform::{SyncPlatform, TextPlatform, WinitPlatform},
roms::DiskLoadable,
systems::{
basic::BasicSystemBuilder, c64::C64SystemBuilder, c64::C64SystemConfig, c64::C64SystemRoms,
easy::Easy6502SystemBuilder, klaus::KlausSystemBuilder, pet::PetSystemBuilder,
pet::PetSystemConfig, pet::PetSystemRoms, vic::Vic20SystemBuilder, vic::Vic20SystemConfig,
vic::Vic20SystemRoms, SystemBuilder,
basic::BasicSystem, c64::C64System, c64::C64SystemConfig, c64::C64SystemRoms,
easy::Easy6502System, klaus::KlausSystem, pet::PetSystem, pet::PetSystemConfig,
pet::PetSystemRoms, vic::Vic20System, vic::Vic20SystemConfig, vic::Vic20SystemRoms,
BuildableSystem,
},
};

Expand Down Expand Up @@ -75,30 +75,30 @@ fn main() {
};

let system = match args.system {
SystemArg::Basic => BasicSystemBuilder::build(romfile.unwrap(), (), platform.provider()),
SystemArg::Easy => Easy6502SystemBuilder::build(romfile.unwrap(), (), platform.provider()),
SystemArg::Klaus => KlausSystemBuilder::build(
SystemArg::Basic => BasicSystem::build(romfile.unwrap(), (), platform.provider()),
SystemArg::Easy => Easy6502System::build(romfile.unwrap(), (), platform.provider()),
SystemArg::Klaus => KlausSystem::build(
romfile.unwrap(),
KlausSystemConfig {
pc_report: None,
variant: Mos6502Variant::NMOS,
},
platform.provider(),
),
SystemArg::Pet => PetSystemBuilder::build(
SystemArg::Pet => PetSystem::build(
PetSystemRoms::from_disk(),
PetSystemConfig { mapping },
platform.provider(),
),
SystemArg::Vic => Vic20SystemBuilder::build(
SystemArg::Vic => Vic20System::build(
Vic20SystemRoms::from_disk(match romfile {
Some(_) => Some(args.rom_path.as_str()),
None => None,
}),
Vic20SystemConfig { mapping },
platform.provider(),
),
SystemArg::C64 => C64SystemBuilder::build(
SystemArg::C64 => C64System::build(
C64SystemRoms::from_disk(),
C64SystemConfig { mapping },
platform.provider(),
Expand Down
7 changes: 2 additions & 5 deletions src/systems/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::memory::{ActiveInterrupt, Memory};
use crate::memory::{BlockMemory, BranchMemory};
use crate::platform::{PlatformProvider, WindowConfig};
use crate::roms::RomFile;
use crate::systems::{System, SystemBuilder};
use crate::systems::{BuildableSystem, System};
use std::io::Write;
use std::sync::Arc;

Expand Down Expand Up @@ -68,10 +68,7 @@ impl Memory for MappedStdIO {
}
}

/// A factory for creating a BasicSystem.
pub struct BasicSystemBuilder;

impl SystemBuilder<BasicSystem, RomFile, ()> for BasicSystemBuilder {
impl BuildableSystem<RomFile, ()> for BasicSystem {
fn build(rom: RomFile, _config: (), platform: Arc<dyn PlatformProvider>) -> Box<dyn System> {
let ram = BlockMemory::ram(0x4000);
let io = MappedStdIO::new(platform);
Expand Down
7 changes: 2 additions & 5 deletions src/systems/c64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use self::{
vic_ii::{VicIIChip, VicIIChipIO},
};

use super::SystemBuilder;
use super::BuildableSystem;

/// Port A on the first CIA chip on the C64 deals with setting the keyboard row being scanned.
struct C64Cia1PortA {
Expand Down Expand Up @@ -204,10 +204,7 @@ pub struct C64SystemConfig {
pub mapping: KeyMappingStrategy,
}

/// A factory for creating a Commodore 64 system.
pub struct C64SystemBuilder;

impl SystemBuilder<C64System, C64SystemRoms, C64SystemConfig> for C64SystemBuilder {
impl BuildableSystem<C64SystemRoms, C64SystemConfig> for C64System {
fn build(
roms: C64SystemRoms,
config: C64SystemConfig,
Expand Down
6 changes: 2 additions & 4 deletions src/systems/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::keyboard::KeyPosition;
use crate::memory::{ActiveInterrupt, BlockMemory, BranchMemory, Memory};
use crate::platform::{Color, PlatformProvider, WindowConfig};
use crate::roms::RomFile;
use crate::systems::{System, SystemBuilder};
use crate::systems::{BuildableSystem, System};
use std::sync::Arc;

const WIDTH: u32 = 32;
Expand Down Expand Up @@ -61,9 +61,7 @@ impl Memory for EasyIO {
}

/// A factory for the Easy6502 system.
pub struct Easy6502SystemBuilder;

impl SystemBuilder<Easy6502System, RomFile, ()> for Easy6502SystemBuilder {
impl BuildableSystem<RomFile, ()> for Easy6502System {
fn build(rom: RomFile, _config: (), platform: Arc<dyn PlatformProvider>) -> Box<dyn System> {
platform.request_window(WindowConfig::new(WIDTH, WIDTH, SCALE as f64));

Expand Down
10 changes: 4 additions & 6 deletions src/systems/klaus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ use std::cell::Cell;
use std::rc::Rc;
use std::sync::Arc;

use super::SystemBuilder;
use super::BuildableSystem;

pub struct KlausSystemConfig {
pub pc_report: Option<Rc<Cell<u16>>>,
pub variant: Mos6502Variant,
}

/// A factory for creating a system that runs Klaus Dormann's 6502 CPU test suite.
pub struct KlausSystemBuilder;

impl SystemBuilder<KlausSystem, RomFile, KlausSystemConfig> for KlausSystemBuilder {
impl BuildableSystem<RomFile, KlausSystemConfig> for KlausSystem {
fn build(
rom: RomFile,
config: KlausSystemConfig,
Expand Down Expand Up @@ -77,7 +75,7 @@ mod tests {
let platform = TextPlatform::new();
let pc = Rc::new(Cell::new(0));

let mut system = KlausSystemBuilder::build(
let mut system = KlausSystem::build(
roms,
KlausSystemConfig {
pc_report: Some(pc.clone()),
Expand All @@ -99,7 +97,7 @@ mod tests {
let platform = TextPlatform::new();
let pc = Rc::new(Cell::new(0));

let mut system = KlausSystemBuilder::build(
let mut system = KlausSystem::build(
roms,
KlausSystemConfig {
pc_report: Some(pc.clone()),
Expand Down
4 changes: 2 additions & 2 deletions src/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub mod klaus;
pub mod pet;
pub mod vic;

pub trait SystemBuilder<SystemType, RomRegistry, SystemConfig> {
/// Create a new system from the given roms, configuration, and with I/O provided by the given
pub trait BuildableSystem<RomRegistry, SystemConfig> {
/// Instantiate this system from the given roms, configuration, and with I/O provided by the given
/// platform provider.
fn build(
roms: RomRegistry,
Expand Down
7 changes: 2 additions & 5 deletions src/systems/pet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::keyboard::{KeyAdapter, KeyMappingStrategy, SymbolAdapter};
use crate::memory::mos652x::{Pia, Via};
use crate::memory::{BlockMemory, BranchMemory, NullMemory, NullPort, Port};
use crate::platform::{Color, PlatformProvider, WindowConfig};
use crate::systems::{System, SystemBuilder};
use crate::systems::{BuildableSystem, System};
use instant::Instant;
use std::cell::Cell;
use std::rc::Rc;
Expand Down Expand Up @@ -151,10 +151,7 @@ pub struct PetSystemConfig {
pub mapping: KeyMappingStrategy,
}

/// A factory for the Commodore PET.
pub struct PetSystemBuilder;

impl SystemBuilder<PetSystem, PetSystemRoms, PetSystemConfig> for PetSystemBuilder {
impl BuildableSystem<PetSystemRoms, PetSystemConfig> for PetSystem {
fn build(
roms: PetSystemRoms,
config: PetSystemConfig,
Expand Down
7 changes: 2 additions & 5 deletions src/systems/vic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use wasm_bindgen::JsCast;
#[cfg(target_arch = "wasm32")]
use js_sys::Uint8Array;

use super::SystemBuilder;
use super::BuildableSystem;

/// The set of ROM files required to run a VIC-20 system.
pub struct Vic20SystemRoms {
Expand Down Expand Up @@ -249,10 +249,7 @@ pub struct Vic20SystemConfig {
pub mapping: KeyMappingStrategy,
}

/// A factory for creating a VIC-20 system.
pub struct Vic20SystemBuilder;

impl SystemBuilder<Vic20System, Vic20SystemRoms, Vic20SystemConfig> for Vic20SystemBuilder {
impl BuildableSystem<Vic20SystemRoms, Vic20SystemConfig> for Vic20System {
fn build(
roms: Vic20SystemRoms,
config: Vic20SystemConfig,
Expand Down

0 comments on commit 712012e

Please sign in to comment.