From 95acceeabd941a260053eb110d870c091790c11e Mon Sep 17 00:00:00 2001 From: august Date: Tue, 13 Feb 2024 22:27:24 -0500 Subject: [PATCH] good god that took way to long but this kinda works --- Cargo.lock | 86 ++++++++++++++++++++++++++ Cargo.toml | 2 + src/cpu.rs | 6 +- src/error.rs | 1 + src/george | Bin 65536 -> 65536 bytes src/george.asm | 2 + src/georgeencoding.txt | 134 ++++++++++++++++++++--------------------- src/main.rs | 6 +- src/memory.rs | 10 ++- src/video.rs | 103 +++++++++++++++++++++++++++---- 10 files changed, 262 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b4db118..bf7457c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,31 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bdf" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f550a6818e6f42ccd5883f44e45fff4f68415a0d09abdc81e7d1d78e0780af14" +dependencies = [ + "bit-set", + "thiserror", +] + +[[package]] +name = "bit-set" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84527c7b0452f22545cc010e72d366a435561d2b28b978035550b3778c4d428d" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + [[package]] name = "bitflags" version = "1.3.2" @@ -20,6 +45,18 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + [[package]] name = "bumpalo" version = "3.14.0" @@ -78,6 +115,12 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "futures" version = "0.3.30" @@ -171,6 +214,8 @@ dependencies = [ name = "georgeemu" version = "0.1.0" dependencies = [ + "bdf", + "bitvec", "minifb", ] @@ -347,6 +392,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "raw-window-handle" version = "0.4.3" @@ -453,6 +504,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tempfile" version = "3.9.0" @@ -466,6 +523,26 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "thiserror" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-ident" version = "1.0.12" @@ -781,6 +858,15 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "x11-dl" version = "2.21.0" diff --git a/Cargo.toml b/Cargo.toml index 2407314..9f17eb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +bdf = "0.6.0" +bitvec = "1.0.1" minifb = "0.25.0" diff --git a/src/cpu.rs b/src/cpu.rs index 9fa05a5..116069b 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -212,9 +212,9 @@ impl Cpu { let instruction = get_instruction(opcode); match instruction { Instruction::Valid(valid_instruction) => { - println!("Instruction: {:?}, {:?}", valid_instruction.opcode, opcode); - println!("a: {a:#04x}, x: {x:#04x}, y: {y:#04x}, pc: {pc:#06x}, sp: {s:#04x}, sr: {p:#010b}, irq: {irq:?}, nmi: {nmi:?}", a = self.a, x = self.x, y = self.y, pc = self.pc, s = self.s, p = self.p, irq = self.irq, nmi = self.nmi); - println!(""); + //println!("Instruction: {:?}, {:?}", valid_instruction.opcode, opcode); + //println!("a: {a:#04x}, x: {x:#04x}, y: {y:#04x}, pc: {pc:#06x}, sp: {s:#04x}, sr: {p:#010b}, irq: {irq:?}, nmi: {nmi:?}", a = self.a, x = self.x, y = self.y, pc = self.pc, s = self.s, p = self.p, irq = self.irq, nmi = self.nmi); + //println!(""); self.pc += 1; match valid_instruction.opcode.call(self) { Ok(_) => { diff --git a/src/error.rs b/src/error.rs index ffb15e4..ed6fdac 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,7 @@ pub enum GeorgeErrorKind { Memory(MemoryError), Execution(ExecutionError), AddrMode(AddressingModeError), + Mapping(MappingError), } impl From for GeorgeErrorKind { diff --git a/src/george b/src/george index b9b7bd4073056727abfb0e197b4e7ebfa78b5476..1ca1b4fa6dc31877cb0b0dd244edd0bdf5001c16 100644 GIT binary patch delta 31 mcmZo@U}z3H~#^nGz%pF delta 28 jcmZo@U} Self { Self { areas: vec![area] } } - pub fn add_area(&mut self, area: MemMappedDevice) -> Result<(), MappingError> { + pub fn add_area(&mut self, area: MemMappedDevice) -> Result<(), GeorgeError> { for existing_area in &self.areas { if existing_area.contains(area.end) || existing_area.contains(area.start) { - return Err(MappingError::RegionOccupied); + return Err(GeorgeError { + desc: format!( + "Tried to assign an area already allocated to {:?}", + existing_area + ), + kind: GeorgeErrorKind::Mapping(MappingError::RegionOccupied), + }); } } self.areas.push(area); diff --git a/src/video.rs b/src/video.rs index 74b41f0..e996848 100644 --- a/src/video.rs +++ b/src/video.rs @@ -1,27 +1,111 @@ -use crate::Mem; +use crate::{memory, Mem}; use minifb::{Window, WindowOptions}; use std::{ - sync::{Arc, RwLock}, + char, + fs::File, + io::Read, + process::exit, + sync::{Arc, RwLock, RwLockReadGuard}, thread::sleep, time::{Duration, Instant}, }; +const FG_COLOR: u32 = 0xFFCC00; +//const BG_COLOR: u32 = 0x110500; +const BG_COLOR: u32 = 0x22BB00; + pub struct Crtc { memory: Arc>, buffer: Vec, window: minifb::Window, + char_rom: Vec, +} + +pub fn get_char_bin(path: &str) -> Vec { + let mut file = File::open(path).unwrap(); + let mut buffer = vec![0; 0xFFFF]; + file.read_exact(&mut buffer).unwrap(); + buffer } impl Crtc { pub fn new(memory: Arc>) -> Self { let window = Window::new("screen", 512, 380, WindowOptions::default()).unwrap(); + let char_rom = get_char_bin("./src/cozette.bin"); Self { memory, buffer: vec![0; 512 * 380], window, + char_rom, } } fn draw(&mut self) { + match &mut self.memory.clone().try_read() { + Ok(memory) => { + let hw_ctrl = memory.read(0x4000).unwrap(); + match hw_ctrl & 0b0000_1000 == 0b0000_1000 { + false => self.draw_chars(), + true => self.draw_hires(), + }; + } + Err(_) => { + println!("Couldn't acquire read on shared memory in main thread"); + return; + } + }; + + self.window + .update_with_buffer(&self.buffer, 512, 380) + .unwrap(); + } + + fn draw_chars(&mut self) { + let memory = match self.memory.try_read() { + Ok(read) => read, + Err(_) => { + println!("Couldn't acquire read on shared memory in main thread"); + return; + } + }; + for char_col in 0..65 { + let pixel_col = char_col * 8; + for char_row in 0..30 { + let pixel_row = char_row * 13; + for row in 0..13 { + for i in 0..8 { + let address: usize = ((pixel_col * pixel_row + row * 13) + i) * 8; + self.buffer[address] = BG_COLOR; + } + } + } + } + //for char_col in 0..64 { + // for char_row in 0..29 { + // let ascii = memory.read(0x6000 + char_row * char_col).unwrap(); + // for row in 0..13 { + // for char in 0..=255 { + // if ascii == char { + // let byte = self.char_rom[char as usize + (row * 0xFF)]; + // for i in 0..8 { + // let address: usize = ((char_col as usize * 64 + (8 * i)) + // + (char_row as usize + (13 * row) * 29)) + // * 8 + // + i; + // if (byte >> i) & 0b1 == 1 { + // self.buffer[address] = FG_COLOR; + // } else { + // self.buffer[address] = BG_COLOR; + // } + // } + // } + // } + // } + // } + //} + println!("{:?}", self.buffer); + //exit(0); + } + fn draw_hires(&mut self) { let memory = match self.memory.try_read() { Ok(read) => read, Err(_) => { @@ -29,24 +113,17 @@ impl Crtc { return; } }; - // let bitmask = [128, 64, 32, 16, 8, 4, 2, 1]; - let bitmask = [1, 2, 4, 8, 16, 32, 64, 128]; // I think this is how the bytes will be - // serialized? low bits first? - for addr in 0x6000..0xBF00 { // TODO: eventually this will access memory in the weird interleaved way the hardware is // designed, but this is easiest to implement for now let byte = memory.read(addr).unwrap(); - for (i, mask) in bitmask.iter().enumerate() { - match byte & mask == 0 { - true => self.buffer[(addr - 0x6000) as usize * 8 + i] = 0x110500, - false => self.buffer[(addr - 0x6000) as usize * 8 + i] = 0xFFCC00, + for i in 0..8 { + match byte & 0x80 >> i == 0 { + true => self.buffer[(addr - 0x6000) as usize * 8 + i] = BG_COLOR, + false => self.buffer[(addr - 0x6000) as usize * 8 + i] = FG_COLOR, } } } - self.window - .update_with_buffer(&self.buffer, 512, 380) - .unwrap(); } pub fn run(&mut self) { let frame_duration = Duration::from_millis(16);