From 17f802145470260cce62db0d57b45d6a14d8ce8a Mon Sep 17 00:00:00 2001 From: Kime78 Date: Wed, 31 Mar 2021 12:57:50 +0300 Subject: [PATCH] final-ish --- src/graphics.rs | 3 +- src/main.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++--- src/opcodes.rs | 7 ++- 3 files changed, 146 insertions(+), 9 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index e87dfb5..2f5cfca 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -17,6 +17,7 @@ impl PPU { } pub fn draw_sprite(&mut self, cpu: &mut CPU, win: &mut RenderWindow) { + let highhalf: u16 = cpu.ram[cpu.pc] as u16; let high: u16 = (highhalf << 8) | (cpu.ram[cpu.pc + 1] as u16); let x: u8 = ((high & 0x0F00) >> 8) as u8; @@ -40,7 +41,7 @@ impl PPU { self.pixels[(cx * 4 + cy * 64 * 4) as usize] = 0; self.pixels[(cx * 4 + cy * 64 * 4) as usize + 1] = 0; self.pixels[(cx * 4 + cy * 64 * 4) as usize + 2] = 0; - self.pixels[(cx * 4 + cy * 64 * 4) as usize + 3] = 255; + self.pixels[(cx * 4 + cy * 64 * 4) as usize + 3] = 0; cpu.regs[0xF] = 1; } else { self.pixels[(cx * 4 + cy * 64 * 4) as usize] = 0; diff --git a/src/main.rs b/src/main.rs index 43e5e6f..9e1505f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,160 @@ +use graphics::PPU; use opcodes::emulate_cycle; use processor::CPU; use sfml::{ graphics::{Color, RenderTarget, RenderWindow}, window::{Event, Style}, }; +use std::io; use std::io::prelude::*; +use std::process::exit; mod graphics; -mod processor; -use graphics::PPU; mod keypad; mod opcodes; -use std::process::exit; -fn main() { - let mut window = RenderWindow::new((640, 320), "Rusty Chip", Style::CLOSE, &Default::default()); +mod processor; +fn main() { let mut cpu: CPU = CPU::new(); let mut ppu: PPU = PPU::new(); let mut gae: [u8; 4096] = [0; 4096]; - let mut file_in = std::fs::File::open("./roms/delay").unwrap(); - file_in.read(&mut gae).unwrap(); + + println!("Selectati ce joc doriti sa jucati din lista de mai jos:"); + println!("1. Tetris"); + println!("2. Brix"); + println!("3. Pong"); + println!("4. Merlin"); + println!("5. KaleID"); + //let mut path = String::new(); + let mut game = String::new(); + + io::stdin() + .read_line(&mut game) + .expect("Failed to read line"); + let game: u32 = game + .trim() + .parse() + .expect("Caracterul introdus nu este valid"); + + if game == 1 { + println!("Doriti instructiuni pentru joc? [D / N]"); + let mut instructiuni = String::new(); + io::stdin() + .read_line(&mut instructiuni) + .expect("Failed to read line"); + + if instructiuni.chars().nth(0).expect("idfk") == 'D' + || instructiuni.chars().nth(0).expect("idfk") == 'd' + { + println!("Q - Roteste Piesa\nW si E - Deplaseaza piesa stanga-dreapta"); + println!("Apasati Enter pentru a juca"); + let mut any = String::new(); + io::stdin() + .read_line(&mut any) + .expect("Failed to read line"); + } + + let mut file_in = std::fs::File::open("./roms/tetris").unwrap(); + file_in.read(&mut gae).unwrap(); + } else if game == 2 { + println!("Doriti instructiuni pentru joc? [D / N]"); + let mut instructiuni = String::new(); + io::stdin() + .read_line(&mut instructiuni) + .expect("Failed to read line"); + + if instructiuni.chars().nth(0).expect("idfk") == 'D' + || instructiuni.chars().nth(0).expect("idfk") == 'd' + { + println!("Q si E - Deplaseaza paleta stanga-dreapta"); + println!("Apasati Enter pentru a juca"); + let mut any = String::new(); + io::stdin() + .read_line(&mut any) + .expect("Failed to read line"); + } + + let mut file_in = std::fs::File::open("./roms/brix").unwrap(); + file_in.read(&mut gae).unwrap(); + } else if game == 3 { + println!("Doriti instructiuni pentru joc? [D / N]"); + let mut instructiuni = String::new(); + io::stdin() + .read_line(&mut instructiuni) + .expect("Failed to read line"); + + if instructiuni.chars().nth(0).expect("idfk") == 'D' + || instructiuni.chars().nth(0).expect("idfk") == 'd' + { + println!("1 si Q - Deplaseaza paleta din stanga sus-jos"); + println!("4 si R - Deplaseaza paleta din dreapta sus-jos"); + println!("Apasati Enter pentru a juca"); + let mut any = String::new(); + io::stdin() + .read_line(&mut any) + .expect("Failed to read line"); + } + + let mut file_in = std::fs::File::open("./roms/pong").unwrap(); + file_in.read(&mut gae).unwrap(); + } else if game == 4 { + println!("Doriti instructiuni pentru joc? [D / N]"); + let mut instructiuni = String::new(); + io::stdin() + .read_line(&mut instructiuni) + .expect("Failed to read line"); + + if instructiuni.chars().nth(0).expect("idfk") == 'D' + || instructiuni.chars().nth(0).expect("idfk") == 'd' + { + println!("Q - Apasa Butonul din coltul stanga-sus"); + println!("W - Apasa Butonul din coltul dreapta-sus"); + println!("A - Apasa Butonul din coltul stanga-jos"); + println!("S - Apasa Butonul din coltul dreapta-jos"); + + println!("Apasati Enter pentru a juca"); + let mut any = String::new(); + io::stdin() + .read_line(&mut any) + .expect("Failed to read line"); + } + let mut file_in = std::fs::File::open("./roms/merlin").unwrap(); + file_in.read(&mut gae).unwrap(); + } else if game == 5 { + println!("Doriti instructiuni pentru joc? [D / N]"); + let mut instructiuni = String::new(); + io::stdin() + .read_line(&mut instructiuni) + .expect("Failed to read line"); + + if instructiuni.chars().nth(0).expect("idfk") == 'D' + || instructiuni.chars().nth(0).expect("idfk") == 'd' + { + println!("Demo de generare a unor pattern-uri simetrice"); + println!("Q si E - Deplasarea liniei stanga-dreapta"); + println!("2 si S - Deplasarea liniei sus-jos"); + println!("Apasati Enter pentru a juca"); + let mut any = String::new(); + io::stdin() + .read_line(&mut any) + .expect("Failed to read line"); + } + let mut file_in = std::fs::File::open("./roms/kaleid").unwrap(); + file_in.read(&mut gae).unwrap(); + } else { + panic!("Numarul introdus nu este valid, introduce-ti un numar de la 1 la 5"); + //do a repetition + } + // for argument in env::args() { + // println!("{}", argument); + // //path = argument; + // } + // print!("{}", z); for i in 0..4096 { cpu.ram[0x200 + i] = gae[i]; } + let mut window = RenderWindow::new((640, 320), "Rusty Chip", Style::CLOSE, &Default::default()); window.set_framerate_limit(60); diff --git a/src/opcodes.rs b/src/opcodes.rs index 2f86086..475f11c 100644 --- a/src/opcodes.rs +++ b/src/opcodes.rs @@ -2,7 +2,7 @@ use crate::{graphics, processor}; use graphics::PPU; use processor::CPU; use sfml::graphics::RenderWindow; - +use std::{thread, time}; pub fn emulate_cycle(cpu: &mut CPU, ppu: &mut PPU, win: &mut RenderWindow) { let highhalf: u16 = cpu.ram[cpu.pc] as u16; let high: u16 = (highhalf << 8) | (cpu.ram[cpu.pc + 1] as u16); @@ -11,8 +11,13 @@ pub fn emulate_cycle(cpu: &mut CPU, ppu: &mut PPU, win: &mut RenderWindow) { let operation: u8 = ((opcode & 0xF000) >> 12) as u8; let mini_op = opcode & 0x000F; if cpu.delay != 0 { + let ten_millis = time::Duration::from_nanos(1); + thread::sleep(ten_millis); cpu.delay -= 1; } + if cpu.sound != 0 { + cpu.sound -= 1; + } //print!("PC = {} : {}\n",cpu.pc, opcode ); match operation { 0x0 => {