Struct CPU

Source
pub struct CPU {
    pub reg: Registers,
    pub flags: Flags,
    pub pc: u16,
    pub sp: u16,
    pub bus: Bus,
    pub halt: bool,
    pub int: (bool, u8),
    pub inte: bool,
    pub debug: Debug,
    /* private fields */
}

Fields§

§reg: Registers§flags: Flags§pc: u16§sp: u16§bus: Bus§halt: bool§int: (bool, u8)

Interrupt request : true / false, instruction to execute (normally a RST command)

§inte: bool

Interrupt enable bit

§debug: Debug

Outputs CPU state and disassembled code to stdout after each execute()

3E 0f     MVI A,$0f
PC : 0x0003	SP : 0xff00	S : 0	Z : 0	A : 0	P : 0	C : 0
B : 0x00	C : 0x00	D : 0x00	E : 0x00	H : 0x00	L : 0x00 ...

Implementations§

Source§

impl CPU

Source

pub fn export_snapshot(&mut self) -> Vec<u8>

Source

pub fn import_snapshot( &mut self, snapshot: Vec<u8>, ) -> Result<(), SnapshotError>

Source§

impl CPU

Source

pub fn new(size: u16) -> CPU

Creates a new CPU instance and its 16 bits address bus.

Examples found in repository?
examples/interrupt_test.rs (line 13)
11fn load_execute() -> Result<(), Box<dyn Error>> {
12    let a: Vec<String> = env::args().collect();
13    let mut c = CPU::new(0xFFFF);
14    c.debug.switch = true;
15
16    // Loads assembled program into memory
17    c.bus.load_bin(&a[1], 0x000)?;
18
19    c.sp = 0xff00;
20    c.inte = false;
21    c.int = (true, 0xcf);
22
23    loop {
24        c.execute();
25        println!("{}\n", c.debug.string);
26        if c.pc == 0x0000 {
27            break;
28        } //  if CP/M warm boot -> we exit
29    }
30    Ok(())
31}
More examples
Hide additional examples
examples/cpmrun.rs (line 13)
11fn load_execute() -> Result<(), Box<dyn Error>> {
12    let a: Vec<String> = env::args().collect();
13    let mut c = CPU::new(0xFFFF);
14    // Loads assembled program into memory
15    c.bus.load_bin(&a[1], 0x100)?;
16
17    // RET at 0x05 for mocking of CP/M BDOS system calls
18    c.bus.write_word(0x0005, 0xc9);
19
20    // Setting PC to 0x0100 (CP/M Binaries are loaded with a 256 byte offset)
21    c.pc = 0x0100;
22
23    /* Setting up stack : by disassembling CP/M software, it seems
24    that the $0006 address is read to set the stack by some programs */
25    c.bus.write_word(0x0006, 0xFF00);
26
27    /* Setting up stack in case of the program does not read the $0006 address
28    and does not set any stack. */
29    c.sp = 0xFF00;
30
31    loop {
32        c.execute();
33        if c.pc == 0x0005 {
34            bdos_call(&c)
35        }
36        if c.pc == 0x0000 {
37            break;
38        } //  if CP/M warm boot -> we exit
39    }
40    Ok(())
41}
Source

pub fn set_freq(&mut self, f: f32)

Sets CPU frequency (MHz)

use intel8080::cpu::CPU;
let mut c = CPU::new(0xFFFF);
c.set_freq(1.7);            // CPU will run at 1.7 Mhz
Source

pub fn execute_timed(&mut self) -> Option<u32>

Fetches and executes one instruction from (pc). Returns the sleep time when slice_max_cycles is reached.

Source

pub fn execute(&mut self) -> u32

Fetches and executes one instruction from (pc). Returns the number of consumed clock cycles. No execution speed limit.

Examples found in repository?
examples/interrupt_test.rs (line 24)
11fn load_execute() -> Result<(), Box<dyn Error>> {
12    let a: Vec<String> = env::args().collect();
13    let mut c = CPU::new(0xFFFF);
14    c.debug.switch = true;
15
16    // Loads assembled program into memory
17    c.bus.load_bin(&a[1], 0x000)?;
18
19    c.sp = 0xff00;
20    c.inte = false;
21    c.int = (true, 0xcf);
22
23    loop {
24        c.execute();
25        println!("{}\n", c.debug.string);
26        if c.pc == 0x0000 {
27            break;
28        } //  if CP/M warm boot -> we exit
29    }
30    Ok(())
31}
More examples
Hide additional examples
examples/cpmrun.rs (line 32)
11fn load_execute() -> Result<(), Box<dyn Error>> {
12    let a: Vec<String> = env::args().collect();
13    let mut c = CPU::new(0xFFFF);
14    // Loads assembled program into memory
15    c.bus.load_bin(&a[1], 0x100)?;
16
17    // RET at 0x05 for mocking of CP/M BDOS system calls
18    c.bus.write_word(0x0005, 0xc9);
19
20    // Setting PC to 0x0100 (CP/M Binaries are loaded with a 256 byte offset)
21    c.pc = 0x0100;
22
23    /* Setting up stack : by disassembling CP/M software, it seems
24    that the $0006 address is read to set the stack by some programs */
25    c.bus.write_word(0x0006, 0xFF00);
26
27    /* Setting up stack in case of the program does not read the $0006 address
28    and does not set any stack. */
29    c.sp = 0xFF00;
30
31    loop {
32        c.execute();
33        if c.pc == 0x0005 {
34            bdos_call(&c)
35        }
36        if c.pc == 0x0000 {
37            break;
38        } //  if CP/M warm boot -> we exit
39    }
40    Ok(())
41}
Source§

impl CPU

Source

pub fn dasm(&self, address: u16) -> String

Disassembles code at (address)

Auto Trait Implementations§

§

impl Freeze for CPU

§

impl RefUnwindSafe for CPU

§

impl Send for CPU

§

impl Sync for CPU

§

impl Unpin for CPU

§

impl UnwindSafe for CPU

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.