fox32: Introduce better error handling

Instead of panicing, we now just print an error and exit in some
situations where a panic is too verbose. Maybe we should just change
the panic handler instead though?
This commit is contained in:
Ry 2022-03-11 11:03:27 -08:00
parent 1419bb5b1f
commit fe279c4d85
2 changed files with 15 additions and 6 deletions

View File

@ -13,7 +13,7 @@ use mouse::Mouse;
use std::env; use std::env;
use std::fs::{File, read}; use std::fs::{File, read};
//use std::process::exit; use std::process::exit;
use std::sync::{Arc, mpsc, Mutex}; use std::sync::{Arc, mpsc, Mutex};
use std::thread; use std::thread;
@ -47,11 +47,20 @@ fn read_rom() -> Vec<u8> {
read("fox32.rom").unwrap_or_else(|_| { read("fox32.rom").unwrap_or_else(|_| {
println!("fox32.rom not found, attempting to open ../fox32rom/fox32.rom instead"); println!("fox32.rom not found, attempting to open ../fox32rom/fox32.rom instead");
read("../fox32rom/fox32.rom").unwrap_or_else(|_| { read("../fox32rom/fox32.rom").unwrap_or_else(|_| {
panic!("oh fuck"); error("fox32.rom not found!");
}) })
}) })
} }
pub fn error(message: &str) -> ! {
println!("Error: {}", message);
exit(1);
}
pub fn warn(message: &str) {
println!("Warning: {}", message);
}
fn main() { fn main() {
let version_string = format!("fox32 {} ({})", env!("VERGEN_BUILD_SEMVER"), env!("VERGEN_GIT_SHA_SHORT")); let version_string = format!("fox32 {} ({})", env!("VERGEN_BUILD_SEMVER"), env!("VERGEN_GIT_SHA_SHORT"));
println!("{}", version_string); println!("{}", version_string);

View File

@ -1,6 +1,6 @@
// memory.rs // memory.rs
use crate::Overlay; use crate::{Overlay, warn};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -50,7 +50,7 @@ impl Memory {
} else if address >= rom_bottom_address && address <= rom_top_address { } else if address >= rom_bottom_address && address <= rom_top_address {
self.rom[address - rom_bottom_address] self.rom[address - rom_bottom_address]
} else { } else {
println!("Warning: attempting to read unmapped memory address: {:#010X}", address); warn(&format!("attempting to read unmapped memory address: {:#010X}", address));
0 0
} }
} }
@ -82,9 +82,9 @@ impl Memory {
let mut shared_memory_lock = self.shared_memory.lock().unwrap(); let mut shared_memory_lock = self.shared_memory.lock().unwrap();
shared_memory_lock[address - shared_bottom_address] = byte; shared_memory_lock[address - shared_bottom_address] = byte;
} else if address >= rom_bottom_address && address <= rom_top_address { } else if address >= rom_bottom_address && address <= rom_top_address {
println!("Warning: attempting to write to ROM address: {:#010X}", address); warn(&format!("attempting to write to ROM address: {:#010X}", address));
} else { } else {
println!("Warning: attempting to write to unmapped memory address: {:#010X}", address); warn(&format!("attempting to write to unmapped memory address: {:#010X}", address));
} }
} }
pub fn write_16(&mut self, address: u32, half: u16) { pub fn write_16(&mut self, address: u32, half: u16) {