Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to control debug level #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
target/
Cargo.lock
32 changes: 0 additions & 32 deletions Cargo.lock

This file was deleted.

9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ name = "carter-emu"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "ce"
path = "src/main.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = "*"
pretty-hex = "*"
itertools = "0.10.*"
pretty-hex = "0.3.*"
clap = {version = "3.2.*", features=["derive"]}
63 changes: 54 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
use clap::Parser;
use itertools::Itertools;
use pretty_hex::*;
use std::env;
use std::fs;
use std::io::{stdin, Read};

#[derive(Parser)]
#[clap(version, about)]
struct Args {
/// Path to file to execute
#[clap(value_parser)]
path: String,

/// Set debug level
/// -d for stepping
/// -dd for debug info at step
#[clap(short, action = clap::ArgAction::Count)]
debug: u8,
}

fn main() -> Result<(), std::io::Error> {
println!("carter-emu v{}", env!("CARGO_PKG_VERSION"));

// load program from file into memory
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
panic!("usage: ce <filename>");
}
let args = Args::parse();

let mut in_bytes: Vec<u8> = fs::read(&args[1])?;
let mut in_bytes: Vec<u8> = fs::read(&args.path)?;
in_bytes.pop(); // remove trailing newline
let instructions = Instruction::from_chars(in_bytes.into_iter());

Expand All @@ -28,21 +40,23 @@ fn main() -> Result<(), std::io::Error> {
}
mem.resize(256, 0);

execute(&mut mem);
execute(&mut mem, args.debug);

println!("{}", pretty_hex(&mem));

Ok(())
}

fn execute(mem: &mut Vec<u8>) {
fn execute(mem: &mut Vec<u8>, step: u8) {
// prep registers
let mut dp = 0; // data pointer
let mut ipl = 0; // instruction pointer low
let mut iph = 0; // instruction pointer high
let mut rpl = 0; // return pointer low
let mut rph = 0; // return pointer high

let mut stdin = stdin();

loop {
if ipl == 256 {
return;
Expand All @@ -51,6 +65,15 @@ fn execute(mem: &mut Vec<u8>) {

loop {
let inst = ins[iph];

if step > 1 {
print_state(ipl, iph, dp, mem, rpl, rph);
}
if step > 0 {
println!(">>> {inst}");
let _ = stdin.read(&mut [0u8]).unwrap();
}

match inst {
Instruction::LoopOpen => {
rpl = ipl;
Expand Down Expand Up @@ -85,6 +108,24 @@ fn execute(mem: &mut Vec<u8>) {
}
}

fn print_state(ipl: usize, iph: usize, dp: usize, mem: &Vec<u8>, rpl: usize, rph: usize) {
println!(
"ipl: {:2X} = {}
iph: {:2X}
dp: {:2X} = {:2X}
rpl: {:2X} = {}
rph: {:2X}",
ipl,
Instruction::display_byte(mem[ipl]),
iph,
dp,
mem[dp],
rpl,
Instruction::display_byte(mem[rpl]),
rph,
);
}

#[derive(Copy, Clone)]
enum Instruction {
LoopOpen,
Expand All @@ -110,7 +151,6 @@ impl fmt::Display for Instruction {
}

impl Instruction {
#[allow(dead_code)]
fn from_byte(byte: u8) -> Vec<Instruction> {
// split into 2-bit pairs before mapping
[
Expand Down Expand Up @@ -156,4 +196,9 @@ impl Instruction {
| Instruction::to_pair(is.get(2)) << 2
| Instruction::to_pair(is.get(3))
}

fn display_byte(byte: u8) -> String {
let is = Instruction::from_byte(byte);
format!("{} {} {} {}", is[0], is[1], is[2], is[3])
}
}