cpu.rs: Fix up ROM loading in new(), and doctests

This commit is contained in:
John 2023-04-29 18:42:19 -05:00
parent 4ec9e2cb71
commit c7226bf9cc

View File

@ -6,7 +6,7 @@
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub mod behavior; mod behavior;
pub mod bus; pub mod bus;
pub mod flags; pub mod flags;
pub mod instruction; pub mod instruction;
@ -71,7 +71,7 @@ impl CPU {
/// ```rust /// ```rust
/// # use chirp::*; /// # use chirp::*;
/// let cpu = CPU::new( /// let cpu = CPU::new(
/// 0xf00, // screen location /// None::<&str>, // ROM to load
/// 0x50, // font location /// 0x50, // font location
/// 0x200, // start of program /// 0x200, // start of program
/// Dis::default(), /// Dis::default(),
@ -81,23 +81,31 @@ impl CPU {
/// dbg!(cpu); /// dbg!(cpu);
/// ``` /// ```
pub fn new( pub fn new(
rom: impl AsRef<std::path::Path>, rom: Option<impl AsRef<std::path::Path>>,
font: Adr, font: Adr,
pc: Adr, pc: Adr,
disassembler: Dis, disassembler: Dis,
breakpoints: Vec<Adr>, breakpoints: Vec<Adr>,
flags: Flags, flags: Flags,
) -> Result<Self> { ) -> Result<Self> {
const CHARSET: &[u8] = include_bytes!("mem/charset.bin");
let mem = bus! {
Charset [font as usize..font as usize + CHARSET.len()] = CHARSET,
Program [pc as usize..0x1000],
};
let mut cpu = CPU { let mut cpu = CPU {
disassembler, disassembler,
font, font,
pc, pc,
breakpoints, breakpoints,
flags, flags,
mem,
..Default::default() ..Default::default()
}; };
// load the provided rom // load the provided rom
if let Some(rom) = rom {
cpu.load_program(rom)?; cpu.load_program(rom)?;
}
Ok(cpu) Ok(cpu)
} }
@ -161,7 +169,7 @@ impl CPU {
/// If key is outside range `0..=0xF`, returns [Error::InvalidKey]. /// If key is outside range `0..=0xF`, returns [Error::InvalidKey].
/// ///
/// If [Flags::keypause] was enabled, it is disabled, /// If [Flags::keypause] was enabled, it is disabled,
/// and the [Flags::lastkey] is recorded. /// and the lastkey is recorded.
/// # Examples /// # Examples
/// ```rust /// ```rust
/// # use chirp::*; /// # use chirp::*;
@ -291,13 +299,13 @@ impl CPU {
/// ```rust /// ```rust
/// # use chirp::*; /// # use chirp::*;
/// let mut cpu = CPU::new( /// let mut cpu = CPU::new(
/// 0xf00, /// None::<&str>,
/// 0x50, /// 0x50,
/// 0x340, /// 0x340,
/// Dis::default(), /// Dis::default(),
/// vec![], /// vec![],
/// Flags::default() /// Flags::default()
/// ); /// ).unwrap();
/// cpu.flags.keypause = true; /// cpu.flags.keypause = true;
/// cpu.flags.draw_wait = true; /// cpu.flags.draw_wait = true;
/// assert_eq!(0x340, cpu.pc()); /// assert_eq!(0x340, cpu.pc());
@ -389,14 +397,11 @@ impl CPU {
/// ```rust /// ```rust
/// # use chirp::*; /// # use chirp::*;
/// let mut cpu = CPU::default(); /// let mut cpu = CPU::default();
/// let mut bus = bus!{ /// let mut screen = bus!{
/// Program [0x0200..0x0f00] = &[ /// Screen [0x000..0x100],
/// 0x00, 0xe0, // cls
/// 0x22, 0x02, // jump 0x202 (pc)
/// ],
/// Screen [0x0f00..0x1000],
/// }; /// };
/// cpu.singlestep(&mut bus).unwrap(); /// cpu.load_program_bytes(&[0x00, 0xe0, 0x22, 0x02]);
/// cpu.singlestep(&mut screen).unwrap();
/// assert_eq!(0x202, cpu.pc()); /// assert_eq!(0x202, cpu.pc());
/// assert_eq!(1, cpu.cycle()); /// assert_eq!(1, cpu.cycle());
/// ``` /// ```
@ -559,8 +564,8 @@ impl CPU {
) )
}) })
.collect::<String>(), .collect::<String>(),
self.delay as u8, self.delay,
self.sound as u8, self.sound,
self.cycle, self.cycle,
); );
} }