1
0
mirror of https://github.com/mre/mos6502.git synced 2024-11-28 07:49:19 +00:00

Use labels

This commit is contained in:
Matthias 2023-06-28 23:48:03 +02:00
parent 360e00da20
commit 902b69a053

View File

@ -1,7 +1,9 @@
use mos6502::cpu;
use mos6502::memory::Bus;
use mos6502::memory::Memory;
use std::fs::read;
use std::collections::HashMap;
use std::fs::{read, File};
use std::io::{BufRead, BufReader};
fn main() {
// Load the binary file from disk
@ -18,13 +20,20 @@ fn main() {
cpu.memory.set_bytes(0x00, &program);
cpu.registers.program_counter = 0x400;
let labels =
load_labels("examples/asm/functional_test/labels.dbg").expect("Could not load labels");
// run step-by-step
let mut old_pc = cpu.registers.program_counter;
while cpu.registers.program_counter != 0x3468 {
// Use `fetch_next_and_decode` instead of
// `single_step` to see the decoded instruction
if let Some(decoded_instr) = cpu.fetch_next_and_decode() {
println!("{decoded_instr}");
let label = labels.get(&cpu.registers.program_counter);
match label {
Some(name) => println!("{}: {}", name, decoded_instr),
None => println!("{}", decoded_instr),
}
cpu.execute_instruction(decoded_instr);
}
cpu.single_step();
@ -38,3 +47,22 @@ fn main() {
old_pc = cpu.registers.program_counter;
}
}
fn load_labels(path: &str) -> std::io::Result<HashMap<u16, String>> {
let mut labels = HashMap::new();
let file = File::open(path)?;
let reader = BufReader::new(file);
for line in reader.lines() {
let line = line?;
let parts: Vec<&str> = line.split(' ').collect();
if parts.len() < 3 {
continue;
}
let address = u16::from_str_radix(parts[1], 16).unwrap();
let label = parts[2].to_owned();
labels.insert(address, label);
}
Ok(labels)
}