Skip to content

Commit

Permalink
final-ish
Browse files Browse the repository at this point in the history
  • Loading branch information
Kime78 committed Mar 31, 2021
1 parent 10293e3 commit 17f8021
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
145 changes: 138 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
7 changes: 6 additions & 1 deletion src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 => {
Expand Down

0 comments on commit 17f8021

Please sign in to comment.