Improve workflow and docs somewhat, make minifb optional

This commit is contained in:
2023-04-14 22:20:30 -05:00
parent 674af62465
commit 43fa623da3
12 changed files with 77 additions and 40 deletions

View File

@@ -1,24 +1,8 @@
use chirp::{cpu::Disassembler, error::Result, *};
use chirp::{error::Result, *};
use gumdrop::*;
use owo_colors::OwoColorize;
use std::{fs::read, path::PathBuf};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Options, Hash)]
struct Arguments {
#[options(help = "Show help text")]
help: bool,
#[options(help = "Load a ROM to run on Chirp", free, required)]
pub file: PathBuf,
#[options(help = "Load address (usually 200)", parse(try_from_str = "parse_hex"))]
pub loadaddr: u16,
#[options(help = "Start disassembling at offset...")]
pub offset: usize,
}
fn parse_hex(value: &str) -> std::result::Result<u16, std::num::ParseIntError> {
u16::from_str_radix(value, 16)
}
fn main() -> Result<()> {
let options = Arguments::parse_args_default_or_exit();
let contents = &read(&options.file)?;
@@ -40,3 +24,19 @@ fn main() -> Result<()> {
}
Ok(())
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Options, Hash)]
struct Arguments {
#[options(help = "Show help text")]
help: bool,
#[options(help = "Load a ROM to run on Chirp", free, required)]
pub file: PathBuf,
#[options(help = "Load address (usually 200)", parse(try_from_str = "parse_hex"))]
pub loadaddr: u16,
#[options(help = "Start disassembling at offset...")]
pub offset: usize,
}
fn parse_hex(value: &str) -> std::result::Result<u16, std::num::ParseIntError> {
u16::from_str_radix(value, 16)
}

View File

@@ -6,12 +6,6 @@
#[cfg(test)]
mod tests;
/// Disassembles Chip-8 instructions
pub trait Disassembler {
/// Disassemble a single instruction
fn once(&self, insn: u16) -> String;
}
pub mod disassembler;
pub mod flags;
pub mod instruction;
@@ -19,7 +13,7 @@ pub mod mode;
pub mod quirks;
use self::{
disassembler::{Dis, Insn},
disassembler::{Dis, Disassembler, Insn},
flags::Flags,
mode::Mode,
quirks::Quirks,
@@ -57,7 +51,7 @@ impl Default for Timers {
#[derive(Clone, Debug, PartialEq)]
pub struct CPU {
/// Flags that control how the CPU behaves, but which aren't inherent to the
/// implementation. Includes [Quirks], target IPF, etc.
/// chip-8. Includes [Quirks], target IPF, etc.
pub flags: Flags,
// memory map info
screen: Adr,
@@ -478,7 +472,7 @@ impl CPU {
/// ```
pub fn tick(&mut self, bus: &mut Bus) -> Result<&mut Self> {
// Do nothing if paused
if self.flags.pause || self.flags.draw_wait || self.flags.keypause {
if self.flags.is_paused() {
// always tick in test mode
if self.flags.monotonic.is_some() {
self.cycle += 1;

View File

@@ -1,10 +1,15 @@
//! A disassembler for Chip-8 opcodes
#![allow(clippy::bad_bit_mask)]
use super::Disassembler;
use imperative_rs::InstructionSet;
use owo_colors::{OwoColorize, Style};
use std::fmt::Display;
/// Disassembles Chip-8 instructions
pub trait Disassembler {
/// Disassemble a single instruction
fn once(&self, insn: u16) -> String;
}
#[allow(non_camel_case_types, non_snake_case, missing_docs)]
#[derive(Clone, Copy, Debug, InstructionSet, PartialEq, Eq)]
/// Implements a Disassembler using imperative_rs

View File

@@ -1,8 +1,8 @@
//! Represents flags that aid in implementation but aren't a part of the Chip-8 spec
//! Represents [Flags] that aid in implementation but aren't a part of the Chip-8 spec
use super::{Mode, Quirks};
/// Represents flags that aid in operation, but aren't inherent to the CPU
/// Represents flags that aid in implementation but aren't a part of the Chip-8 spec
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Flags {
/// Set when debug (live disassembly) mode enabled
@@ -55,4 +55,9 @@ impl Flags {
pub fn pause(&mut self) {
self.pause = !self.pause
}
/// Gets whether the CPU is paused for any reason
pub fn is_paused(&self) -> bool {
self.pause || self.draw_wait || self.keypause
}
}

View File

@@ -1,7 +1,7 @@
// (c) 2023 John A. Breaux
// This code is licensed under MIT license (see LICENSE.txt for details)
//! Contains implementations for each instruction defined in [super::disassembler]
//! Contains implementations for each [Insn] as private member functions of [CPU]
use super::*;

View File

@@ -1,4 +1,7 @@
//! Selects the memory behavior of the [super::CPU]
//!
//! Since [super::Quirks] implements [From<Mode>],
//! this can be used to select the appropriate quirk-set
use crate::error::Error;
use std::str::FromStr;

View File

@@ -1,4 +1,4 @@
//! Controls the authenticity behavior of the CPU on a granular level.
//! Controls the [Quirks] behavior of the CPU on a granular level.
use super::Mode;
/// Controls the authenticity behavior of the CPU on a granular level.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

View File

@@ -64,6 +64,7 @@ pub enum Error {
/// Error originated in [std::array::TryFromSliceError]
#[error(transparent)]
TryFromSliceError(#[from] std::array::TryFromSliceError),
#[cfg(feature = "minifb")]
/// Error originated in [minifb]
#[error(transparent)]
MinifbError(#[from] minifb::Error),

View File

@@ -14,8 +14,14 @@ pub mod error;
// Common imports for Chirp
pub use bus::{Bus, Read, Region::*, Write};
pub use cpu::{disassembler::Dis, flags::Flags, mode::Mode, quirks::Quirks, CPU};
pub use error::Result;
pub use cpu::{
disassembler::{Dis, Disassembler},
flags::Flags,
mode::Mode,
quirks::Quirks,
CPU,
};
pub use error::{Error, Result};
/// Holds the state of a Chip-8
#[derive(Clone, Debug, Default, PartialEq)]