aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaditya Dhruv <[email protected]>2023-01-02 16:36:57 +0530
committerAaditya Dhruv <[email protected]>2023-01-02 16:36:57 +0530
commit8a01a57129de444289834777bae916e28d5a5ae8 (patch)
tree9ee0f5eb65393c1efc923768e4e318fa64334363
parentb8544d8c81d69b27d3c6e8dd222a09bf4706084a (diff)
rand package, core fns
-rw-r--r--Cargo.lock54
-rw-r--r--Cargo.toml1
-rw-r--r--src/lib.rs125
3 files changed, 165 insertions, 15 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 58db0ae..f29c445 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -18,10 +18,22 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "chip8"
version = "0.1.0"
dependencies = [
+ "rand",
"sdl2",
]
[[package]]
+name = "getrandom"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -34,6 +46,42 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
+name = "ppv-lite86"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
+
+[[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.35.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -61,3 +109,9 @@ name = "version-compare"
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"
diff --git a/Cargo.toml b/Cargo.toml
index d4be877..d2f8880 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
sdl2 = "0.35.2"
+rand = "0.8.5"
diff --git a/src/lib.rs b/src/lib.rs
index 244201f..7b5062c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,5 @@
use sdl2::keyboard::Keycode;
+use rand::prelude::*;
use std::fs::File;
use std::io::Read;
use sdl2::rect::Rect;
@@ -19,7 +20,7 @@ pub struct Chip {
index: u16, //16-bit index pointer
pc: u16, //Program counter
stack: [u16; 16], //16 level stack
- sp: u8, //stack pointer
+ sp: usize, //stack pointer
delay_timer: u8, //delay timer
sound_timer: u8, //sound timer
keys : [Keycode; 16], //mapping keys to chip-8 input keys
@@ -27,8 +28,8 @@ pub struct Chip {
display: [u16; DISPLAY_LENGTH as usize], //the display array
instr: u16, //current exec instr
v: u8,
- x: u8,
- y: u8,
+ x: usize,
+ y: usize,
n: u8,
nn: u8,
nnn: u16,
@@ -105,8 +106,8 @@ impl Chip {
pub fn execute(&mut self) {
self.v = ((self.instr & 0xF000) >> 12) as u8;
- self.x = ((self.instr & 0x0F00) >> 8) as u8;
- self.y = ((self.instr & 0x00F0) >> 4) as u8;
+ self.x = ((self.instr & 0x0F00) >> 8) as usize;
+ self.y = ((self.instr & 0x00F0) >> 4) as usize;
self.n = (self.instr & 0x000F) as u8;
self.nn = (self.instr & 0x00FF) as u8;
self.nnn = self.instr & 0x0FFF;
@@ -119,41 +120,132 @@ impl Chip {
0x7000..=0x7fff => { self.add_7xkk() },
0xa000..=0xafff => { self.ld_annn() },
0xd000..=0xdfff => { self.drw_dxyn() },
+ 0x00ee => { self.ret_00ee() },
+ 0x2000..=0x2fff => { self.call_2nnn() },
+ 0x3000..=0x3fff => { self.se_3xkk() }
+ 0x4000..=0x4fff => { self.sne_4xkk() }
+ 0x5000..=0x5ff0 => { self.se_5xy0() }
+ 0x9000..=0x9ff0 => { self.sne_9xy0() }
+ 0x8000..=0x8ff0 => { self.ld_8xy0() }
+ 0x8001..=0x8ff1 => { self.or_8xy1() }
+ 0x8002..=0x8ff2 => { self.and_8xy2() }
+ 0x8003..=0x8ff3 => { self.xor_8xy3() }
+ 0x8004..=0x8ff4 => { self.add_8xy4() }
+ 0x8005..=0x8ff5 => { self.sub_8xy5() }
+ 0x8006..=0x8ff6 => { self.shr_8xy6() }
+ 0x8007..=0x8ff7 => { self.sub_8xy7() }
+ 0x800e..=0x8ffe => { self.shl_8xye() }
+ 0xb000..=0xbfff => { self.jp_bnnn() }
+ 0xc000..=0xcfff => { self.rnd_cxnn() }
_ => { println!("Doing nothing!"); }
}
}
+ fn ret_00ee(&mut self) {
+ self.sp -= 1;
+ self.pc = self.stack[self.sp as usize];
+ }
+ fn call_2nnn(&mut self) {
+ self.stack[self.sp as usize] = self.pc;
+ self.sp += 1;
+ self.pc = self.nnn;
+ }
+ fn se_3xkk(&mut self) {
+ if self.registers[self.x] == self.nn {
+ self.pc += 2;
+ }
+ }
+ fn sne_4xkk(&mut self) {
+ if self.registers[self.x] != self.nn {
+ self.pc += 2;
+ }
+ }
+ fn se_5xy0(&mut self) {
+ if self.registers[self.x] == self.registers[self.y] {
+ self.pc += 2;
+ }
+ }
+ fn sne_9xy0(&mut self) {
+ if self.registers[self.x] != self.registers[self.y] {
+ self.pc += 2;
+ }
+ }
//Clear screen
fn cls_00e0(&mut self) {
self.display = [0; DISPLAY_LENGTH];
}
-
//Jump
fn jmp_1nnn(&mut self) {
self.pc = self.nnn;
}
-
//set register vx
fn ld_6xkk(&mut self) {
- self.registers[self.x as usize] = self.nn as u8;
+ self.registers[self.x] = self.nn as u8;
}
-
//add value to register vx
fn add_7xkk(&mut self) {
- self.registers[self.x as usize] += self.nn as u8;
+ self.registers[self.x] += self.nn as u8;
+ }
+ fn ld_8xy0(&mut self) {
+ self.registers[self.x] = self.registers[self.y];
+ }
+ fn or_8xy1(&mut self) {
+ self.registers[self.x] = self.registers[self.x] | self.registers[self.y];
+ }
+ fn and_8xy2(&mut self) {
+ self.registers[self.x] = self.registers[self.x] & self.registers[self.y];
+ }
+ fn xor_8xy3(&mut self) {
+ self.registers[self.x] = self.registers[self.x] ^ self.registers[self.y];
+ }
+ fn add_8xy4(&mut self) {
+
+ self.registers[self.x] += self.registers[self.y];
+ match self.registers[self.x].checked_add(self.registers[self.y]) {
+ Some(_) => { }
+ None => { self.registers[0xF] = 1; }
+ }
+ }
+ fn sub_8xy5(&mut self) {
+ self.registers[self.x] -= self.registers[self.y];
+ match self.registers[self.x].checked_sub(self.registers[self.y]) {
+ Some(_) => { self.registers[0xF] = 1; }
+ None => { }
+ }
+ }
+ fn sub_8xy7(&mut self) {
+ self.registers[self.x] = self.registers[self.y] - self.registers[self.x];
+ match self.registers[self.y].checked_sub(self.registers[self.x]) {
+ Some(_) => { self.registers[0xF] = 1; }
+ None => { }
+ }
+ }
+ fn shr_8xy6(&mut self) {
+ self.registers[self.x] = self.registers[self.y];
+ self.registers[0xF] = self.registers[self.x] << 7 & 1;
+ self.registers[self.x] >>= 1;
+ }
+ fn shl_8xye(&mut self) {
+ self.registers[self.x] = self.registers[self.y];
+ self.registers[0xF] = self.registers[self.x] >> 7 & 1;
+ self.registers[self.x] <<= 1;
}
-
//set index register
fn ld_annn(&mut self) {
self.index = self.nnn;
}
-
+ fn jp_bnnn(&mut self) {
+ self.pc = self.nnn + self.registers[0x0] as u16;
+ }
+ fn rnd_cxnn(&mut self) {
+ let x : u8 = rand::thread_rng().gen();
+ self.registers[self.x] = x | self.n;
+ }
//draw logic
fn drw_dxyn(&mut self) {
- let x = self.registers[self.x as usize] as u32 % WIDTH; //x-coord
- let y = self.registers[self.y as usize] as u32 % HEIGHT; //y-coord
-
+ let x = self.registers[self.x] as u32 % WIDTH; //x-coord
+ let y = self.registers[self.y] as u32 % HEIGHT; //y-coord
//TODO: Cover up for end of bounds
//for every row
for i in 0..self.n {
@@ -166,6 +258,9 @@ impl Chip {
}
}
}
+ fn skp_ex9e(&mut self) {
+ }
+
//render the current grid of pixels accounting for scale
pub fn render(&mut self, canvas : &mut sdl2::render::WindowCanvas) {