commit 369e31c26c9374e7cf42c9de7a59ef2862441b46 from: Witcher01 date: Tue Jul 20 21:41:30 2021 UTC initial commit commit - /dev/null commit + 369e31c26c9374e7cf42c9de7a59ef2862441b46 blob - /dev/null blob + ea8c4bf7f35f6f77f75d92ad8ce8349f6e81ddba (mode 644) --- /dev/null +++ .gitignore @@ -0,0 +1 @@ +/target blob - /dev/null blob + 0d5d13fef0de4be642f68668397a1dd5f0788607 (mode 644) --- /dev/null +++ Cargo.lock @@ -0,0 +1,63 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" + +[[package]] +name = "sdl2" +version = "0.34.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "deecbc3fa9460acff5a1e563e05cb5f31bba0aa0c214bb49a43db8159176d54b" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "sdl2-sys", +] + +[[package]] +name = "sdl2-sys" +version = "0.34.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41a29aa21f175b5a41a6e26da572d5e5d1ee5660d35f9f9d0913e8a802098f74" +dependencies = [ + "cfg-if", + "libc", + "version-compare", +] + +[[package]] +name = "version-compare" +version = "0.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1" + +[[package]] +name = "yac8e" +version = "0.1.0" +dependencies = [ + "sdl2", +] blob - /dev/null blob + e37f2505d58993aa1d6f26eb18d4a07382039575 (mode 644) --- /dev/null +++ Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "yac8e" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sdl2 = "0.34" blob - /dev/null blob + 4b055bbd7b017259a2000c802efb1f80797b9570 (mode 644) --- /dev/null +++ src/chip8.rs @@ -0,0 +1,40 @@ +#[derive(Debug)] +/// Represents a Chip8 machine +pub struct Chip8 { + /// Memory of 4096 8 bit wide locations + /// 0x000 - 0x1FF is usually reserved for the interpreter + /// 0x200 is where most Chip8 programs start + mem: [u8; 4096], + /// 16 registers, commonly named V0-VF + regs: [u8; 16], + /// 12 bit wide address register + i_reg: u16, + /// 16-bit Program Counter + pc: u16, + /// Call stack with a maximum size of 16 + stack: [u16; 16], + /// Stack pointer, pointing to the topmost level of the stack + stack_pointer: u8, + /// Delay timer + /// The value of timer is being decremented at a rate of 60Hz + delay_timer: u8, + /// Sound timer + /// The value of this timer is being decremented at a rate of 60Hz + sound_timer: u8, +} + +// should be implemented ourselves since Default is not implemented for array with sizes > 32 +impl Default for Chip8 { + fn default() -> Self { + Self { + mem: [0; 4096], + regs: [0; 16], + i_reg: 0, + pc: 0, + stack: [0; 16], + stack_pointer: 0, + delay_timer: 0, + sound_timer: 0, + } + } +} blob - /dev/null blob + 75610e3ae29c81229f75bf314274646c7e7f51c3 (mode 644) --- /dev/null +++ src/drivers/mod.rs @@ -0,0 +1,3 @@ +mod video_driver; + +pub use video_driver::VideoDriver; blob - /dev/null blob + 90f40acbcae29e6deb41ee3c1b092bb3dd23b87d (mode 644) --- /dev/null +++ src/drivers/video_driver.rs @@ -0,0 +1,32 @@ +use sdl2::render::Canvas; +use sdl2::video::Window; +use sdl2::pixels::Color; + +/// Contains the canvas to draw to. +pub struct VideoDriver { + canvas: Canvas, +} + +impl VideoDriver { + /// Create a new canvas from a sdl2 context. + pub fn new(sdl_context: &sdl2::Sdl) -> Self { + let video_subsystem = sdl_context.video().expect("unable to initialize video subsystem"); + + let window = video_subsystem.window("yet another chip8 emulator", 64, 32) + .position_centered() + .build() + .expect("unable to initialize video subsystem"); + + let mut canvas = window.into_canvas().build().expect("unable to crate canvas"); + canvas.set_draw_color(Color::RGB(0, 255, 255)); + canvas.clear(); + canvas.present(); + + Self { canvas: canvas } + } + + /// Draw the given array to the canvas. + pub fn draw(&mut self, pixels: &[[u8; CHIP8_WIDTH]; CHIP8_HEIGHT]) { + todo!(); + } +} blob - /dev/null blob + 956667c61a5dbe4d84da9cbde5083c4c49892d78 (mode 644) --- /dev/null +++ src/main.rs @@ -0,0 +1,14 @@ +extern crate sdl2; +mod chip8; +mod drivers; + +use drivers::VideoDriver; + +fn main() -> Result<(), String> { + let sdl_context = sdl2::init().expect("unable to initialize sdl2"); + let v = VideoDriver::new(&sdl_context); + + std::thread::sleep(std::time::Duration::from_secs(1)); + + Ok(()) +}