Change to latest git comit of elf crate, add basic symbol support to run, and format code

This commit is contained in:
pjht 2022-10-12 08:04:27 -05:00
parent 1ecd6083a6
commit f4f8890a5d
3 changed files with 277 additions and 257 deletions

3
Cargo.lock generated
View File

@ -180,8 +180,7 @@ checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "elf"
version = "0.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b9c86cae88373e32c872967c296dcdd3b25771dcc7da1c4ba060b4a68fd8db7"
source = "git+https://github.com/cole14/rust-elf?rev=a4a747071316b1a900962dd83cbf2ab4ca4665fa#a4a747071316b1a900962dd83cbf2ab4ca4665fa"
[[package]]
name = "errno"

View File

@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
bitvec = "1.0.0"
derive-try-from-primitive = "1.0.0"
elf = "0.0.12"
elf = { git = "https://github.com/cole14/rust-elf", rev = "a4a747071316b1a900962dd83cbf2ab4ca4665fa" }
human-repr = { version = "1.0.1", features = ["iec", "space"] }
inventory = "0.3.1"
itertools = "0.10.5"

View File

@ -14,7 +14,7 @@ use crate::{
m68k::{BusError, M68K},
};
use disas::DisassemblyError;
use elf::types::Symbol;
use elf::symbol::Symbol;
use itertools::Itertools;
use parse_int::parse;
use reedline_repl_rs::{
@ -22,7 +22,7 @@ use reedline_repl_rs::{
Error as ReplError, Repl,
};
use serde_yaml::Mapping;
use std::{convert::TryFrom, fmt::Display, fs, num::ParseIntError, process};
use std::{convert::TryFrom, error, fmt::Display, fs, num::ParseIntError, process};
#[derive(Debug)]
enum Error {
@ -33,6 +33,13 @@ enum Error {
InvalidPeekSize,
Disassembly(DisassemblyError<BusError>),
Misc(&'static str),
MiscDyn(Box<dyn error::Error>),
}
impl From<Box<dyn error::Error>> for Error {
fn from(v: Box<dyn error::Error>) -> Self {
Self::MiscDyn(v)
}
}
impl From<DisassemblyError<BusError>> for Error {
@ -69,6 +76,7 @@ impl Display for Error {
Self::InvalidPeekSize => f.write_str("Invalid peek size"),
Self::Disassembly(e) => e.fmt(f),
Self::Misc(s) => f.write_str(s),
Self::MiscDyn(e) => e.fmt(f),
}
}
}
@ -184,7 +192,10 @@ fn main() -> Result<(), ReplError> {
Err(e) => panic!("{}", e),
};
}
Repl::<_, Error>::new(EmuState {cpu: M68K::new(backplane), symbols: Vec::new()})
Repl::<_, Error>::new(EmuState {
cpu: M68K::new(backplane),
symbols: Vec::new(),
})
.with_name("68KEmu")
.with_version("0.1.0")
.with_banner("68K Backplane Computer Emulator")
@ -206,7 +217,9 @@ fn main() -> Result<(), ReplError> {
.about("Send a command to a card"),
|args, state| {
let num = args.get_one::<String>("num").unwrap().parse::<u8>()?;
state.cpu.bus_mut()
state
.cpu
.bus_mut()
.cards_mut()
.get_mut(num as usize)
.ok_or(Error::InvalidCard(num))?
@ -225,7 +238,9 @@ fn main() -> Result<(), ReplError> {
|_, state| {
#[allow(unstable_name_collisions)]
Ok(Some(
state.cpu.bus_mut()
state
.cpu
.bus_mut()
.cards()
.iter()
.enumerate()
@ -251,19 +266,18 @@ fn main() -> Result<(), ReplError> {
.long("print_ins")
.short('i')
.action(ArgAction::SetTrue)
.help("Print instructions")
.help("Print instructions"),
)
.arg(
Arg::new("print_regs")
.long("print_regs")
.short('r')
.action(ArgAction::SetTrue)
.help("Print ending registers")
.help("Print ending registers"),
)
.about("Step the CPU"),
|args, state| {
let count =
parse::<u32>(args.get_one::<String>("count").map_or("1", String::as_str))?;
let count = parse::<u32>(args.get_one::<String>("count").map_or("1", String::as_str))?;
let mut out = String::new();
for _ in 0..count {
if state.cpu.stopped {
@ -289,25 +303,33 @@ fn main() -> Result<(), ReplError> {
)
.with_command(
Command::new("run")
.arg(
Arg::new("stop_addr")
.takes_value(true)
.help("Optional address to stop execution at. Works as a breakpoint only for this run")
)
.arg(Arg::new("stop_addr").takes_value(true).help(
"Optional address to stop execution at. Works as a breakpoint only for this run",
))
.arg(
Arg::new("print_ins")
.long("print_ins")
.short('p')
.action(ArgAction::SetTrue)
.help("Print all executed instructions")
.help("Print all executed instructions"),
)
.about("Run the CPU"),
|args, state| {
let mut out = String::new();
while !state.cpu.stopped {
let stop_addr = args
.get_one::<String>("stop_addr")
.map(|s| parse::<u32>(s))
let stop_addr = args.get_one::<String>("stop_addr");
let stop_addr = stop_addr
.map(|s| {
parse::<u32>(s).or_else(|_| {
state
.symbols
.iter()
.find(|sym| &sym.name == s)
.map(|sym| sym.value as u32)
.ok_or(Error::Misc("No such symbol"))
})
})
.transpose()?;
if stop_addr.map(|a| state.cpu.pc() == a).unwrap_or(false) {
break;
@ -348,8 +370,7 @@ fn main() -> Result<(), ReplError> {
}
let fmt = PeekFormat::try_from(fmt_str.chars().next().unwrap())?;
let size = PeekSize::try_from(fmt_str.chars().nth(1).unwrap())?;
let count =
parse::<u32>(args.get_one::<String>("count").map_or("1", String::as_str))?;
let count = parse::<u32>(args.get_one::<String>("count").map_or("1", String::as_str))?;
let addr = parse::<u32>(args.get_one::<String>("addr").unwrap())?;
let mut data = Vec::new();
@ -385,10 +406,7 @@ fn main() -> Result<(), ReplError> {
)
.with_command(
Command::new("disas")
.arg(
Arg::new("addr")
.help("Address to start disassembly at. Defaults to current PC"),
)
.arg(Arg::new("addr").help("Address to start disassembly at. Defaults to current PC"))
.arg(
Arg::new("count")
.short('c')
@ -400,8 +418,7 @@ fn main() -> Result<(), ReplError> {
let mut addr = args
.get_one::<String>("addr")
.map_or(Ok(state.cpu.pc()), |s| parse::<u32>(s))?;
let count =
parse::<u32>(args.get_one::<String>("count").map_or("1", String::as_str))?;
let count = parse::<u32>(args.get_one::<String>("count").map_or("1", String::as_str))?;
let mut out = String::new();
for _ in 0..count {
let (fmt, res) = disas_fmt(&mut state.cpu, addr);
@ -428,20 +445,24 @@ fn main() -> Result<(), ReplError> {
)
.about("Load symbols from an ELF file"),
|args, state| {
let file = args
.get_one::<String>("file").unwrap();
let file = elf::File::open_path(file).unwrap();
let symtab = file.get_section(".symtab").ok_or(Error::Misc("Could not find symbol table section"))?;
let symbols = file.get_symbols(symtab).unwrap();
let file = args.get_one::<String>("file").unwrap();
let file = elf::File::open_path(file).map_err(<Box<dyn error::Error>>::from)?;
let symtab = file
.get_section(".symtab")
.ok_or(Error::Misc("Could not find symbol table section"))?;
let symbols = file
.get_symbols(&symtab)
.map_err(<Box<dyn error::Error>>::from)?;
state.symbols = symbols;
Ok(None)
},
)
.with_command(Command::new("quit")
.with_command(
Command::new("quit")
.visible_alias("q")
.visible_alias("exit")
.about("Quit"),
|_, _| process::exit(0)
|_, _| process::exit(0),
)
// Visible aliases don't actually work, so fake it with hidden subcommands
.with_command(Command::new("q").hide(true), |_, _| process::exit(0))