mirror of
https://github.com/mre/mos6502.git
synced 2025-02-19 16:32:10 +00:00
extract memory functionality into a trait
This commit is contained in:
parent
f3f15de8cc
commit
a8dac6e805
@ -20,6 +20,7 @@ Source: [Wikipedia](https://en.wikipedia.org/wiki/MOS_Technology_6502)
|
||||
## How to use this library
|
||||
|
||||
```rust
|
||||
use mos6502::memory::Bus;
|
||||
use mos6502::cpu;
|
||||
|
||||
fn main() {
|
||||
|
@ -1,6 +1,7 @@
|
||||
extern crate mos6502;
|
||||
|
||||
use mos6502::cpu;
|
||||
use mos6502::memory::Bus;
|
||||
|
||||
fn main() {
|
||||
println!("Enter two numbers (< 128) to know their GCD:");
|
||||
|
@ -29,6 +29,7 @@ extern crate mos6502;
|
||||
|
||||
#[cfg(not(test))]
|
||||
use mos6502::cpu;
|
||||
use mos6502::memory::Bus;
|
||||
|
||||
#[cfg(not(test))]
|
||||
fn main() {
|
||||
|
@ -26,6 +26,7 @@
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
use crate::instruction::{self, DecodedInstr, Instruction, OpInput};
|
||||
use crate::memory::Bus;
|
||||
use crate::memory::Memory;
|
||||
use crate::registers::{Registers, StackPointer, Status, StatusArgs};
|
||||
|
||||
|
@ -26,6 +26,7 @@
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
use crate::cpu::CPU;
|
||||
use crate::memory::Bus;
|
||||
use crate::memory::Memory;
|
||||
|
||||
// Abbreviations
|
||||
|
@ -58,26 +58,39 @@ impl Default for Memory {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Bus {
|
||||
fn get_byte(&self, address: u16) -> u8;
|
||||
fn set_byte(&mut self, address: u16, value: u8) -> u8;
|
||||
|
||||
fn set_bytes(&mut self, start: u16, values: &[u8]) {
|
||||
for i in 0..values.len() as u16 {
|
||||
self.set_byte(start + i, values[i as usize]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
pub fn new() -> Memory {
|
||||
Memory {
|
||||
bytes: [0; MEMORY_SIZE],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_byte(&self, address: u16) -> u8 {
|
||||
impl Bus for Memory {
|
||||
fn get_byte(&self, address: u16) -> u8 {
|
||||
self.bytes[address as usize]
|
||||
}
|
||||
|
||||
// Sets the byte at the given address to the given value and returns the
|
||||
// previous value at the address.
|
||||
pub fn set_byte(&mut self, address: u16, value: u8) -> u8 {
|
||||
fn set_byte(&mut self, address: u16, value: u8) -> u8 {
|
||||
let old_value = self.get_byte(address);
|
||||
self.bytes[address as usize] = value;
|
||||
old_value
|
||||
}
|
||||
|
||||
pub fn set_bytes(&mut self, start: u16, values: &[u8]) {
|
||||
fn set_bytes(&mut self, start: u16, values: &[u8]) {
|
||||
let start = start as usize;
|
||||
|
||||
// This panics if the range is invalid
|
||||
|
Loading…
x
Reference in New Issue
Block a user