initial commit

This commit is contained in:
Lauchmelder 2022-08-23 19:01:26 +02:00
commit a03d23097b
7 changed files with 161 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target/
.vscode/

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rusty-nes"
version = "0.1.0"

8
Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "rusty-nes"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

43
src/bus.rs Normal file
View file

@ -0,0 +1,43 @@
use std::cell::RefCell;
use std::rc::{Rc, Weak};
use crate::cpu::CPU;
pub struct Bus
{
cpu: Weak<RefCell<CPU>>,
ram: Vec<u8>
}
impl Bus
{
pub fn new() -> Bus
{
Bus
{
cpu: Weak::new(),
ram: vec![0; 0x800]
}
}
pub fn run(&self)
{
let cpu = self.cpu.upgrade().unwrap();
loop
{
cpu.borrow_mut().execute();
}
}
pub fn attach_cpu(&mut self, cpu: &Rc<RefCell<CPU>>)
{
self.cpu = Rc::downgrade(cpu);
}
pub fn read_cpu(&self, addr: u16) -> u8
{
self.ram[addr as usize]
}
}

57
src/cpu.rs Normal file
View file

@ -0,0 +1,57 @@
use std::cell::RefCell;
use std::rc::{Rc, Weak};
use crate::bus::Bus;
pub struct CPU
{
acc: u8,
x: u8,
y: u8,
p: u8,
sp: u8,
pc: u16,
bus: Weak<RefCell<Bus>>
}
impl CPU
{
pub fn new(bus: &Rc<RefCell<Bus>>) -> CPU
{
CPU {
acc: 0,
x: 0,
y: 0,
p: 0,
sp: 0,
pc: 0,
bus: Rc::downgrade(bus)
}
}
pub fn powerup(&mut self)
{
self.p = 0x34;
self.acc = 0;
self.x = 0;
self.y = 0;
self.sp = 0xFD;
}
pub fn execute(&mut self)
{
let bus = self.bus.upgrade().unwrap();
let opcode: u8 = bus.borrow().read_cpu(self.pc);
self.pc += 1;
match (opcode)
{
_ => panic!("Unimplemented opcode {}", opcode)
}
}
}

10
src/main.rs Normal file
View file

@ -0,0 +1,10 @@
mod nes;
mod bus;
mod cpu;
use nes::NES;
fn main() {
let nes = NES::new();
nes.powerup();
}

34
src/nes.rs Normal file
View file

@ -0,0 +1,34 @@
use std::cell::RefCell;
use std::rc::Rc;
use crate::bus::Bus;
use crate::cpu::CPU;
pub struct NES
{
bus: Rc<RefCell<Bus>>,
cpu: Rc<RefCell<CPU>>
}
impl NES
{
pub fn new() -> NES
{
let bus: Rc<RefCell<Bus>> = Rc::new(RefCell::new(Bus::new()));
let cpu: Rc<RefCell<CPU>> = Rc::new(RefCell::new(CPU::new(&bus)));
bus.borrow_mut().attach_cpu(&cpu);
NES
{
bus: bus,
cpu: cpu
}
}
pub fn powerup(&self)
{
self.cpu.borrow_mut().powerup();
self.bus.borrow().run();
}
}