michaelburge / nes-emulator Goto Github PK
View Code? Open in Web Editor NEWA NES emulator made to learn the Rust programming language
Home Page: https://www.michaelburge.us/2019/03/18/nes-design.html
A NES emulator made to learn the Rust programming language
Home Page: https://www.michaelburge.us/2019/03/18/nes-design.html
Hey there,
So, I can't 100% get this running (Rust-SDL2/rust-sdl2#865), but I wanted to show you a diff that I think will get things working:
diff --git a/src/headless.rs b/src/headless.rs
index dbe25e0..73f831a 100644
--- a/src/headless.rs
+++ b/src/headless.rs
@@ -14,7 +14,6 @@ use std::io::Read;
use std::io::Write;
use std::fs::File;
use std::fmt::Debug;
-use std::os::unix::io::FromRawFd;
use crate::joystick::Joystick;
use crate::mapper::AddressSpace;
@@ -27,8 +26,8 @@ use crate::common::Clocked;
fn main() {
// Standard stdout() object is line-buffered
- let stdin = unsafe { File::from_raw_fd(0) };
- let stdout = unsafe { File::from_raw_fd(1) };
+ let stdin = std::io::stdin();
+ let stdout = std::io::stdout();
let mut headless = Headless::new(
Box::new(stdin),
Box::new(stdout),
In general, using the standard library here helps with portability. Of course, it's your project, and so feel free to ignore/close this issue!
Line 30 in 0b9e321
What's wrong with line-buffered stdout?
Line 117 in 0b9e321
https://doc.rust-lang.org/reference/behavior-considered-undefined.html : Dereferencing a null or dangling raw pointer.
Headless::joystick1/2 are initialized with:
impl Headless {
fn command_load_rom(&mut self) {
let mut record_tas = self.read_byte();
let filename = self.read_length_string();
let mut joystick1 = Box::new(Joystick::new());
let mut joystick2 = Box::new(Joystick::new());
self.joystick1 = &mut *joystick1;
self.joystick2 = &mut *joystick2;
...
// drop joystick1 joystick2
// Headless::joystick1/2 are dangling pointers since joystick1 joystick2 are freed
}
nes-emulator/src/serialization.rs
Line 144 in 0b9e321
impl<T: Savable> Savable for Vec<T> {
...
fn load(&mut self, fh: &mut Read) {
let mut len = 0usize;
len.load(fh);
self.truncate(0);
self.reserve(len);
for i in 0..len {
let mut x:T = unsafe { std::mem::uninitialized() };
x.load(fh);
self.push(x);
}
}
In case of Vec<bool>
we got:
let mut x:bool = unsafe { std::mem::uninitialized() };
Which is UB by definition: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
See comment rust-lang/rust#53491 (comment)
Building with nightly
instead of stable produces:
error[E0597]: `texture_creator` does not live long enough
--> src\main.rs:96:32
|
96 | let mut texture = Box::new(texture_creator.create_texture(
| ^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
142 | texture: &mut *texture,
| ------------- cast requires that `texture_creator` is borrowed for `'static`
...
179 | }
| - `texture_creator` dropped here while still borrowed
I am not sure why this is an error in nightly but not in stable...
Anyway, what it's complaining about is this: as main ends, texture_creator
will be freed, and then GLOBAL_STATE
will be holding invalid memory; any destructors that run after texture_creator
is freed could access that invalid memory.
Ideally, GLOBAL_STATE
would use lazy_static
, I think. Not 100% sure how much contortion that would take.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.