diff --git a/Cargo.lock b/Cargo.lock index 0ec2636..d3c0d07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -133,6 +133,17 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "heck" version = "0.4.1" @@ -185,6 +196,7 @@ dependencies = [ "byteorder", "clap", "log", + "rand", "sdl2", "simple_logger", ] @@ -195,6 +207,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.78" @@ -213,6 +231,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "sdl2" version = "0.36.0" @@ -336,6 +384,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index b8e1fc9..112c3ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,8 @@ log = "0.4.20" simple_logger = "4.3" byteorder = "1.5" sdl2 = "0.36.0" +rand = "0.8.5" [profile.release] strip = true -lto = true \ No newline at end of file +lto = true diff --git a/src/device/device.rs b/src/device/device.rs index 3766166..f695ed3 100644 --- a/src/device/device.rs +++ b/src/device/device.rs @@ -4,6 +4,7 @@ use std::sync::{Arc, Mutex, RwLock}; use std::thread; use std::thread::{JoinHandle, sleep}; use std::time::Duration; +use rand::random; use crate::device::instruction::Instruction; use crate::device::timer::Timer; @@ -117,7 +118,9 @@ impl Device { Instruction::ConditionalEqRegisterSkipNext(_, _) => {} Instruction::ConditionalInEqRegisterSkipNext(_, _) => {} Instruction::JumpWithOffset(_, _) => {} - Instruction::RandomOr(_, _) => {} + Instruction::RandomAnd(dest, n) => { + self.registers.v[dest] = random::() & n; + } Instruction::SkipIfKeyPressed(_) => {} Instruction::SkipIfKeyNotPressed(_) => {} Instruction::Set(x, y) => { diff --git a/src/device/instruction.rs b/src/device/instruction.rs index dc3be23..2a20249 100644 --- a/src/device/instruction.rs +++ b/src/device/instruction.rs @@ -29,7 +29,7 @@ pub enum Instruction { /// B(X+N)NN - Jump to address with offset. Either v0 or specified register JumpWithOffset(usize, u16), /// CXNN - AND a random number with NN and place in register - RandomOr(usize, u16), + RandomAnd(usize, u8), /// DXYN - Draw pixels at xy pointed by register for n bytes long Draw(usize, usize, u8), @@ -123,7 +123,7 @@ impl Instruction { 0xC => { let register_x = (instruction & 0xf00) >> 8; let mask = instruction & 0xff; - Instruction::RandomOr(register_x as usize, mask) + Instruction::RandomAnd(register_x as usize, mask as u8) } 0xD => { let x = (instruction & 0xf00) >> 8; @@ -303,7 +303,7 @@ mod tests { fn test_random_and() { let instruction_bytes = 0xcabd_u16.to_be_bytes(); let ins = Instruction::decode_instruction(&instruction_bytes); - assert_eq!(ins, RandomOr(0xa, 0xbd)); + assert_eq!(ins, RandomAnd(0xa, 0xbd)); }