1
0
mirror of https://github.com/mre/mos6502.git synced 2024-06-01 14:41:38 +00:00
mre-mos6502/src/memory.rs

103 lines
3.8 KiB
Rust
Raw Normal View History

2014-09-28 01:32:41 +00:00
// Copyright (C) 2014 The 6502-rs Developers
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the names of the copyright holders nor the names of any
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
2017-08-01 07:55:45 +00:00
use std::iter::repeat;
use address::{Address, AddressDiff};
2014-09-28 01:32:41 +00:00
// JAM: We can probably come up with a better way to represent address ranges.
// Address range type?
2017-08-01 07:55:45 +00:00
//
// // Address range -- inclusive on both sides
// pub struct AddressRangeIncl {
// begin: Address,
// end: Address,
// }
2014-10-16 22:50:45 +00:00
const ADDR_LO_BARE: u16 = 0x0000;
const ADDR_HI_BARE: u16 = 0xFFFF;
2017-08-01 07:55:45 +00:00
pub const MEMORY_ADDRESS_LO: Address = Address(ADDR_LO_BARE);
pub const MEMORY_ADDRESS_HI: Address = Address(ADDR_HI_BARE);
pub const STACK_ADDRESS_LO: Address = Address(0x0100);
pub const STACK_ADDRESS_HI: Address = Address(0x01FF);
2014-10-16 22:50:45 +00:00
pub const IRQ_INTERRUPT_VECTOR_LO: Address = Address(0xFFFE);
pub const IRQ_INTERRUPT_VECTOR_HI: Address = Address(0xFFFF);
2014-09-29 01:06:12 +00:00
2017-08-01 07:55:45 +00:00
const MEMORY_SIZE: usize = (ADDR_HI_BARE - ADDR_LO_BARE) as usize + 1usize;
2014-12-18 22:57:42 +00:00
// FIXME: Should this use indirection for `bytes`?
2017-08-01 07:55:45 +00:00
#[derive(Clone)]
2014-09-28 02:09:31 +00:00
pub struct Memory {
2017-08-01 07:55:45 +00:00
bytes: Vec<u8>,
2014-09-28 02:09:31 +00:00
}
2014-09-28 01:32:41 +00:00
2014-09-28 02:09:31 +00:00
impl Memory {
pub fn new() -> Memory {
2017-08-01 07:55:45 +00:00
Memory { bytes: repeat(0).take(MEMORY_SIZE).collect() }
}
pub fn get_byte(&self, address: Address) -> u8 {
2015-01-17 22:53:21 +00:00
self.bytes[address.to_usize()]
}
2014-11-02 20:40:34 +00:00
pub fn get_byte_mut_ref(&mut self, address: Address) -> &mut u8 {
2015-01-17 22:53:21 +00:00
&mut self.bytes[address.to_usize()]
2014-11-02 20:40:34 +00:00
}
2017-08-01 07:55:45 +00:00
pub fn get_slice(&self, Address(start): Address, AddressDiff(diff): AddressDiff) -> &[u8] {
2015-01-17 22:53:21 +00:00
let start = start as usize;
let diff = diff as usize;
let end = start + diff;
2015-01-17 22:53:21 +00:00
&self.bytes[start..end]
}
// 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: Address, value: u8) -> u8 {
let old_value = self.get_byte(address);
2015-01-17 22:53:21 +00:00
self.bytes[address.to_usize()] = value;
old_value
}
pub fn set_bytes(&mut self, Address(start): Address, values: &[u8]) {
2015-01-17 22:53:21 +00:00
let start = start as usize;
// This panics if the range is invalid
2015-01-17 22:53:21 +00:00
let end = start + values.len();
let slice = &mut self.bytes[start..end];
// JAM: Is this the best way to do this copy?
for (dest, src) in slice.iter_mut().zip(values.iter()) {
*dest = *src;
}
}
pub fn is_stack_address(address: &Address) -> bool {
STACK_ADDRESS_LO <= *address && *address <= STACK_ADDRESS_HI
}
2014-09-28 01:32:41 +00:00
}