Struct vm6502::vm::VirtualMachine
source · [−]pub struct VirtualMachine {
pub registers: Registers,
pub flatmap: BytesMut,
pub zero_bounds: (usize, usize),
pub stack_bounds: (usize, usize),
pub heap_bounds: (usize, usize),
pub vheap_bounds: (usize, usize),
pub interrupt_bounds: (usize, usize),
pub reset_bounds: (usize, usize),
pub irq_bounds: (usize, usize),
pub addr_mode: Mode,
pub cycles: u64,
pub halted: bool,
}Expand description
The virtual machine implementation
This module provides VirtualMachine, a 6502 cpu vm. It’s API is intended to closely adhere to the 6502 specs.
See Masswerk’s 6502 Instruction Set for more info on the spec.
Fields
registers: RegistersMachine registers struct.
flatmap: BytesMutThe machine memory in a linear layout. We set the size to 64k+1 to allow easy indexing.
zero_bounds: (usize, usize)Machine zero page bounds. This is a tuple of (start, end) addresses.
stack_bounds: (usize, usize)Machine stack page bounds. The stack grows downwards from 0x01FF to 0x0100.
heap_bounds: (usize, usize)Machine heap(dynamic memory) bounds. This is the only memory that can be dynamically allocated. Accessing memory outside of these bounds is undefined behavior.
vheap_bounds: (usize, usize)interrupt_bounds: (usize, usize)Interrupt vector table bounds. Placed at end of heap.
Three vectors are used: reset, irq, nmi. Each vector is 2 bytes.
reset_bounds: (usize, usize)irq_bounds: (usize, usize)addr_mode: ModeCurrent mode state, this is generally set internally by step.
cycles: u64The current cycle count of the vm. This is incremented by step.
halted: boolImplementations
sourceimpl VirtualMachine
impl VirtualMachine
Trait Implementations
sourceimpl Debug for VirtualMachine
impl Debug for VirtualMachine
sourceimpl Default for VirtualMachine
impl Default for VirtualMachine
sourceimpl HeapInterface for VirtualMachine
impl HeapInterface for VirtualMachine
sourcefn get_page_offset(&self) -> u8
fn get_page_offset(&self) -> u8
Returns the page offset for the current PC.
sourcefn set_page_offset(&mut self, virt_addr: u8)
fn set_page_offset(&mut self, virt_addr: u8)
Sets the PC to the given page offset.
sourcefn bounds_check(&self, virt_addr: usize) -> bool
fn bounds_check(&self, virt_addr: usize) -> bool
Checks if the given address is within the heap bounds. TODO: Reimplement.
sourceimpl InstructionController for VirtualMachine
impl InstructionController for VirtualMachine
Virtual machine core control functionality.
This provides three main internal functions, step, mode, and fetch.
Examples
step
use vm6502::prelude::*;
let mut vm = VirtualMachine::new();
vm.insert_program(0x00, "69FFFF");
vm.registers.pc = 0x00;
vm.step();
assert_eq!(vm.flatmap[vm.registers.pc as usize + vm.heap_bounds.0], 0xFF);mode
use vm6502::prelude::*;
let mut vm = VirtualMachine::new();
let mode = vm.mode(0x69);
assert_eq!(mode, Mode::Immediate);fetch
// TODO: Unignore this later.
use vm6502::prelude::*;
let mut vm = VirtualMachine::new();
let byte = 0x01;
// 0x200 is heap start. See `VirtualMachine::heap_bounds`.
vm.set_heap(0x0000, 0x69);
vm.set_heap(0x0001, byte);
assert_ne!(vm.flatmap[0x0001], byte, "Byte {} was not set to 0x0201", byte);
assert_eq!(byte, vm.flatmap[0x0201], "Byte {} was not set at 0x0201", byte);
// Should PC be 0x01 or two here?
vm.registers.pc = 0x01;
vm.addr_mode = Mode::Immediate;
let fetched = vm.fetch();
assert_eq!(vm.registers.pc, 0x02, "PC should be incremented by 1 after fetch");
assert_eq!(fetched, byte, "Fetched byte {} does not match expected byte {}", fetched, byte);sourceimpl Instructions for VirtualMachine
impl Instructions for VirtualMachine
sourceimpl ProgramController for VirtualMachine
impl ProgramController for VirtualMachine
sourcefn insert_program(&mut self, offset: u16, prog: &str)
fn insert_program(&mut self, offset: u16, prog: &str)
Insert a hex encoded string prog at heap offset offset.s
sourcefn set_program(&mut self, offset: u16, prog: &str)
fn set_program(&mut self, offset: u16, prog: &str)
Replaces and runs the program at offset.
sourcefn execute(&mut self) -> u64
fn execute(&mut self) -> u64
Run the internally set program. Intended API for running programs.
sourcefn run(&mut self, duration: Duration) -> (u64, Duration)
fn run(&mut self, duration: Duration) -> (u64, Duration)
Run the internally set program for duration time, returning the number of cycles executed.
sourcefn fill_stack(&mut self, ops: Vec<u8>)
fn fill_stack(&mut self, ops: Vec<u8>)
Fill the stack with ops.
sourcefn set_interrupt_vectors(&mut self, nmi: u16, irq: u16, brk: u16)
fn set_interrupt_vectors(&mut self, nmi: u16, irq: u16, brk: u16)
sourcefn default_interrupt_vectors(&mut self)
fn default_interrupt_vectors(&mut self)
sourceimpl StackInterface for VirtualMachine
impl StackInterface for VirtualMachine
sourceimpl StatusInterface for VirtualMachine
impl StatusInterface for VirtualMachine
Implements a high level interface for accessing the status register.
This is the intended API access for frontends to use the VM.