Skip to content

Commit

Permalink
Make banked memory read and write separately
Browse files Browse the repository at this point in the history
  • Loading branch information
breqdev committed Nov 22, 2023
1 parent 003e204 commit e204a4b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/memory/banked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use super::{ActiveInterrupt, Memory, SystemInfo};
/// address space. The active implementation is selected by external logic.
pub struct BankedMemory {
banks: Vec<Box<dyn Memory>>,
active: Rc<Cell<usize>>,
active: Rc<Cell<(usize, usize)>>,
}

impl BankedMemory {
/// Create a new, empty banked memory.
pub fn new(active: Rc<Cell<usize>>) -> Self {
pub fn new(active: Rc<Cell<(usize, usize)>>) -> Self {

Check warning on line 15 in src/memory/banked.rs

View check run for this annotation

Codecov / codecov/patch

src/memory/banked.rs#L15

Added line #L15 was not covered by tests
Self {
banks: Vec::new(),
active,
Expand All @@ -29,16 +29,16 @@ impl BankedMemory {

impl Memory for BankedMemory {
fn read(&mut self, address: u16) -> u8 {
match self.banks.get_mut(self.active.get()) {
match self.banks.get_mut(self.active.get().0) {

Check warning on line 32 in src/memory/banked.rs

View check run for this annotation

Codecov / codecov/patch

src/memory/banked.rs#L32

Added line #L32 was not covered by tests
Some(memory) => memory.read(address),
None => panic!("Invalid bank {} selected", self.active.get()),
None => panic!("Invalid bank {} selected", self.active.get().0),

Check warning on line 34 in src/memory/banked.rs

View check run for this annotation

Codecov / codecov/patch

src/memory/banked.rs#L34

Added line #L34 was not covered by tests
}
}

fn write(&mut self, address: u16, value: u8) {
match self.banks.get_mut(self.active.get()) {
match self.banks.get_mut(self.active.get().1) {

Check warning on line 39 in src/memory/banked.rs

View check run for this annotation

Codecov / codecov/patch

src/memory/banked.rs#L39

Added line #L39 was not covered by tests
Some(memory) => memory.write(address, value),
None => panic!("Invalid bank {} selected", self.active.get()),
None => panic!("Invalid bank {} selected", self.active.get().1),

Check warning on line 41 in src/memory/banked.rs

View check run for this annotation

Codecov / codecov/patch

src/memory/banked.rs#L41

Added line #L41 was not covered by tests
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/systems/aiie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ impl SystemBuilder<AiieSystem, AiieSystemRoms, AiieSystemConfig> for AiieSystemB
// Box::new(LoggingMemory::new(Box::new(io), "I/O", 0xC000)),
)
.map(0xC100, Box::new(rom));
//.map(0xD000, Box::new(applesoft_interpreter))
//.map(0xF800, Box::new(monitor));
//.map(0xD000, Box::new(applesoft_interpreter))
//.map(0xF800, Box::new(monitor));

let cpu = Mos6502::new(Box::new(memory));

Expand Down Expand Up @@ -274,8 +274,6 @@ impl System for AiieSystem {

#[allow(clippy::identity_op)]
fn render(&mut self, framebuffer: &mut [u8], config: WindowConfig) {

Check warning on line 276 in src/systems/aiie/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/aiie/mod.rs#L276

Added line #L276 was not covered by tests
let flash_state = ((self.cpu.get_info().cycle_count / 500_000) & 0b1) == 0;

// https://retrocomputing.stackexchange.com/a/2541
for index in 0x000..=0x3FF {
let position = match index & 0x7F {
Expand Down
38 changes: 21 additions & 17 deletions src/systems/c64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ pub struct C64BankSwitching {
charen: bool,

/// Selectors to choose what is mapped in each memory region.
selectors: [Rc<Cell<usize>>; 6],
selectors: [Rc<Cell<(usize, usize)>>; 6],
}

impl C64BankSwitching {
pub fn new(mut selectors: [Rc<Cell<usize>>; 6]) -> Self {
selectors.iter_mut().for_each(|s| s.set(0));
pub fn new(mut selectors: [Rc<Cell<(usize, usize)>>; 6]) -> Self {
selectors.iter_mut().for_each(|s| s.set((0, 0)));

Check warning on line 143 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L142-L143

Added lines #L142 - L143 were not covered by tests
Self {
hiram: true,
loram: true,
Expand All @@ -164,28 +164,32 @@ impl Port for C64BankSwitching {
// TODO: EXROM, GAME signals

// Region 2: RAM or inaccessible
self.selectors[0].set(0);
self.selectors[0].set((0, 0));

Check warning on line 167 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L167

Added line #L167 was not covered by tests

// Region 3: RAM or Cartridge ROM Low
self.selectors[1].set(0);
self.selectors[1].set((0, 0));

Check warning on line 170 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L170

Added line #L170 was not covered by tests

// Region 4: BASIC ROM, RAM, Cartridge ROM High, or inaccessible
self.selectors[2].set(if self.hiram && self.loram { 0 } else { 1 });
self.selectors[2].set(if self.hiram && self.loram {
(0, 0)

Check warning on line 174 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L173-L174

Added lines #L173 - L174 were not covered by tests
} else {
(1, 1)

Check warning on line 176 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L176

Added line #L176 was not covered by tests
});

// Region 5: RAM or inaccessible
self.selectors[3].set(0);
self.selectors[3].set((0, 0));

Check warning on line 180 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L180

Added line #L180 was not covered by tests

// Region 6: I/O, RAM, or character rom
self.selectors[4].set(if !self.hiram && !self.loram {
1
(1, 1)

Check warning on line 184 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L184

Added line #L184 was not covered by tests
} else if !self.charen {
2
(2, 2)

Check warning on line 186 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L186

Added line #L186 was not covered by tests
} else {
0
(0, 0)

Check warning on line 188 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L188

Added line #L188 was not covered by tests
});

// Region 7: Kernal ROM or RAM
self.selectors[5].set(if !self.hiram { 1 } else { 0 });
self.selectors[5].set(if !self.hiram { (1, 1) } else { (0, 0) });

Check warning on line 192 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L192

Added line #L192 was not covered by tests
}

fn poll(&mut self, _cycles: u32, _info: &SystemInfo) -> bool {
Expand Down Expand Up @@ -223,33 +227,33 @@ impl SystemBuilder<C64System, C64SystemRoms, C64SystemConfig> for C64SystemBuild
let region1 = BlockMemory::ram(0x1000);

// Region 2: 0x1000 - 0x7FFF
let selector2 = Rc::new(Cell::new(0));
let selector2 = Rc::new(Cell::new((0, 0)));

Check warning on line 230 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L230

Added line #L230 was not covered by tests
let region2 = BankedMemory::new(selector2.clone())
.bank(Box::new(BlockMemory::ram(0x7000)))
.bank(Box::new(NullMemory::new()));

// Region 3: 0x8000 - 0x9FFF
let selector3 = Rc::new(Cell::new(0));
let selector3 = Rc::new(Cell::new((0, 0)));

Check warning on line 236 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L236

Added line #L236 was not covered by tests
let region3 = BankedMemory::new(selector3.clone())
.bank(Box::new(BlockMemory::ram(0x2000)))
.bank(Box::new(NullMemory::new())); // TODO: Cartridge Rom Low

// Region 4: 0xA000 - 0xBFFF
let selector4 = Rc::new(Cell::new(0));
let selector4 = Rc::new(Cell::new((0, 0)));

Check warning on line 242 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L242

Added line #L242 was not covered by tests
let region4 = BankedMemory::new(selector4.clone())
.bank(Box::new(BlockMemory::from_file(0x2000, roms.basic)))
.bank(Box::new(BlockMemory::ram(0x2000)))
.bank(Box::new(NullMemory::new())) // TODO: Cartridge Rom High
.bank(Box::new(NullMemory::new()));

// Region 5: 0xC000 - 0xCFFF
let selector5 = Rc::new(Cell::new(0));
let selector5 = Rc::new(Cell::new((0, 0)));

Check warning on line 250 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L250

Added line #L250 was not covered by tests
let region5 = BankedMemory::new(selector5.clone())
.bank(Box::new(BlockMemory::ram(0x1000)))
.bank(Box::new(NullMemory::new()));

// Region 6: 0xD000 - 0xDFFF
let selector6 = Rc::new(Cell::new(0));
let selector6 = Rc::new(Cell::new((0, 0)));

Check warning on line 256 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L256

Added line #L256 was not covered by tests

let character_rom = BlockMemory::from_file(0x1000, roms.character.clone());
let vic_ii = Rc::new(RefCell::new(VicIIChip::new(Box::new(character_rom))));
Expand Down Expand Up @@ -283,7 +287,7 @@ impl SystemBuilder<C64System, C64SystemRoms, C64SystemConfig> for C64SystemBuild
.bank(Box::new(BlockMemory::from_file(0x1000, roms.character)));

// Region 7: 0xE000 - 0xFFFF
let selector7 = Rc::new(Cell::new(0));
let selector7 = Rc::new(Cell::new((0, 0)));

Check warning on line 290 in src/systems/c64/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/systems/c64/mod.rs#L290

Added line #L290 was not covered by tests
let region7 = BankedMemory::new(selector7.clone())
.bank(Box::new(BlockMemory::from_file(0x2000, roms.kernal)))
.bank(Box::new(BlockMemory::ram(0x2000)))
Expand Down

0 comments on commit e204a4b

Please sign in to comment.