Skip to content

Commit

Permalink
emu: Temporary fix for program start address
Browse files Browse the repository at this point in the history
Need to figure out how to specify where the disassembler should start
looking for the program. Maybe check if 0xfffc is set?
  • Loading branch information
Granddave committed Jan 7, 2024
1 parent 0cfc020 commit d35773d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 63 deletions.
24 changes: 1 addition & 23 deletions src/bin/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,6 @@ use anyhow::{Context, Result};
use tracing_chrome::ChromeLayerBuilder;
use tracing_subscriber::prelude::*;

fn demo() -> &'static str {
"
define zero $00
LDX #zero
LDY #$00
firstloop:
TXA
STA $0200,Y
PHA
INX
INY
CPY #$10
BNE firstloop ;loop until Y is $10
secondloop:
PLA
STA $0200,Y
INY
CPY #$20 ;loop until Y is $20
BNE secondloop
"
}

const PROGRAM_START: u16 = 0x8000;

#[tracing::instrument]
Expand All @@ -34,7 +12,7 @@ fn main() -> Result<()> {
let input_source = if let Some(filename) = std::env::args().nth(1) {
std::fs::read_to_string(filename).unwrap()
} else {
demo().to_string()
return Err(anyhow::anyhow!("No file specified. Allowed filetype: .asm"));
};

let bytes = mos6502::assembler::compile_code(&input_source, PROGRAM_START)
Expand Down
25 changes: 2 additions & 23 deletions src/bin/emu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,7 @@ use mos6502::{
emulator::{self, tui},
};

fn demo() -> &'static str {
"
define zero $00
LDX #zero
LDY #$00
firstloop:
TXA
STA $0200,Y
PHA
INX
INY
CPY #$10
BNE firstloop ;loop until Y is $10
secondloop:
PLA
STA $0200,Y
INY
CPY #$20 ;loop until Y is $20
BNE secondloop
"
}

// TODO: Either pass this as an argument, or check 0xfffc...?
const PROGRAM_START: u16 = 0x8000;

fn get_program_bytes() -> anyhow::Result<Vec<u8>> {
Expand All @@ -45,7 +24,7 @@ fn get_program_bytes() -> anyhow::Result<Vec<u8>> {
return Err(anyhow::anyhow!("Unknown file type. Allowed: .bin, .asm"));
}
} else {
compile_code(demo(), PROGRAM_START).with_context(|| "Compilation failed")
Err(anyhow::anyhow!("No file specified. Allowed: .bin, .asm"))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub mod tui;
/// Runs the emulator with the given program bytes and program start address.
pub fn run(program_bytes: &[u8], program_start: u16) -> anyhow::Result<()> {
let mut memory = Memory::new();
memory.write_word(cpu::RESET_VECTOR, program_start);
memory.load(program_start, program_bytes);
memory.write_word(cpu::RESET_VECTOR, program_start); // TODO: Include in the program
memory.load(0x0000, program_bytes);
let mut cpu = Cpu::new();
cpu.reset();
cpu.run(&mut memory, RunOption::StopOnBreakInstruction);
Expand Down
31 changes: 16 additions & 15 deletions src/emulator/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,20 @@ impl App {
// turn a Vec<Instruction> into a Vec<(usize, String)>
// where the usize is the memory address of the instruction.
// TODO: Refactor this into a function
let disassembly: Vec<(usize, String)> = disassemble_code(program)
.iter()
.scan(0, |acc, ins| {
let addr = *acc;
*acc += ins.size();
Some((addr, ins.clone()))
})
.map(|(addr, node)| {
let memory_addr = program_start as usize + addr;
let line = listing::generate_line(memory_addr, &node);
(memory_addr, line)
})
.collect();
let disassembly: Vec<(usize, String)> =
disassemble_code(&program[program_start as usize..])
.iter()
.scan(0, |acc, ins| {
let addr = *acc;
*acc += ins.size();
Some((addr, ins.clone()))
})
.map(|(addr, node)| {
let memory_addr = program_start as usize + addr;
let line = listing::generate_line(memory_addr, &node);
(memory_addr, line)
})
.collect();

let mut app = Self {
program: program.to_vec(),
Expand All @@ -82,8 +83,8 @@ impl App {
pub fn reset(&mut self) {
self.memory = Memory::new();
self.memory
.write_word(cpu::RESET_VECTOR, self.program_start);
self.memory.load(self.program_start, &self.program);
.write_word(cpu::RESET_VECTOR, self.program_start); // TODO: Include in the program
self.memory.load(0x0000, &self.program);

self.cpu = Cpu::new();
self.cpu.reset();
Expand Down

0 comments on commit d35773d

Please sign in to comment.