-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 an option to produce a textfile trace of instructions executed #39
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
use crate::trace::TraceHandler; | ||
|
||
pub mod mos6502; | ||
|
||
pub trait Cpu { | ||
/// Reset this CPU, clearing internal state. | ||
fn reset(&mut self); | ||
|
||
/// Attach the given handler to receive trace events from this CPU. | ||
fn attach_trace_handler(&mut self, trace: Box<dyn TraceHandler>); | ||
|
||
/// Return the number of cycles elapsed since the system last reset. | ||
fn get_cycle_count(&self) -> u64; | ||
|
||
/// Execute a single instruction. Return the number of cycles elapsed. | ||
fn tick(&mut self) -> u8; | ||
|
||
/// Clean up any resources used by this CPU. | ||
fn cleanup(&mut self) -> Result<(), &str>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::trace::{CpuTrace, TraceHandler}; | ||
use std::{ | ||
fs::File, | ||
io::{BufWriter, Write}, | ||
}; | ||
|
||
pub struct FileTraceHandler { | ||
file: BufWriter<File>, | ||
} | ||
|
||
impl FileTraceHandler { | ||
pub fn new(filename: String) -> Self { | ||
Self { | ||
file: BufWriter::new(File::create(filename).expect("Invalid filename")), | ||
} | ||
} | ||
} | ||
|
||
impl TraceHandler for FileTraceHandler { | ||
fn handle(&mut self, trace: &CpuTrace) { | ||
self | ||
.file | ||
.write_all(format!("{:04X}: {:02X}\n", trace.address, trace.opcode).as_bytes()) | ||
.unwrap(); | ||
} | ||
|
||
fn flush(&mut self) -> Result<(), &str> { | ||
self.file.flush().map_err(|_| "failed to flush file") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#[cfg(not(target_arch = "wasm32"))] | ||
pub mod file; | ||
|
||
/// Trace information provided after each instruction by the CPU. | ||
pub struct CpuTrace { | ||
pub address: u16, | ||
pub opcode: u8, | ||
} | ||
|
||
/// An item which can handle a CPU trace (e.g. logging to a file) | ||
pub trait TraceHandler { | ||
/// Handle a trace event. | ||
fn handle(&mut self, trace: &CpuTrace); | ||
|
||
/// Flush any existing resource buffers. | ||
fn flush(&mut self) -> Result<(), &str> { | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be good to make this buffered with
BufWriter
, i'm happy to implement that