Skip to content

Commit

Permalink
Add workspace and #[no_std] support
Browse files Browse the repository at this point in the history
  • Loading branch information
koute committed Mar 11, 2023
1 parent 17c51a1 commit 59c2a65
Show file tree
Hide file tree
Showing 24 changed files with 152 additions and 141 deletions.
18 changes: 2 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
pinky-devui/target
pinky-devui/Cargo.lock
pinky-libretro/target
pinky-libretro/Cargo.lock
pinky-web/target
pinky-web/Cargo.lock
nes-testsuite/target
nes-testsuite/Cargo.lock
mos6502/target
mos6502/Cargo.lock
nes/target
nes/Cargo.lock
emumisc/target
emumisc/Cargo.lock
rp2c02-testsuite/target
rp2c02-testsuite/Cargo.lock
Cargo.toml
Cargo.lock
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[workspace]
members = [
"emumisc",
"mos6502",
"nes",
"nes-testsuite",
"pinky-devui",
"pinky-libretro",
"pinky-web",
"rp2c02-testsuite"
]

[profile.test]
opt-level = 2
6 changes: 2 additions & 4 deletions emumisc/src/bits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use core::mem;

#[inline(always)] pub fn is_b0_set( value: u8 ) -> bool { (value & (1 << 0)) != 0 }
#[inline(always)] pub fn is_b1_set( value: u8 ) -> bool { (value & (1 << 1)) != 0 }
Expand All @@ -11,8 +11,6 @@ use std::mem;

#[inline(always)]
pub fn to_bit( bit: u8, value: bool ) -> u8 {
use std::mem;

debug_assert!( bit < 8 );
let converted: u8 = unsafe { mem::transmute( value ) };
debug_assert!( converted == 0 || converted == 1 );
Expand All @@ -37,7 +35,7 @@ macro_rules! impl_bit_extra {
impl BitExtra for $typ {
#[inline(always)]
fn mask_to_shift( mask: Self ) -> u8 {
use std::mem::size_of;
use core::mem::size_of;
debug_assert!( mask != 0 );

for n_bit in 0..(size_of::< Self >() * 8) as u8 {
Expand Down
19 changes: 11 additions & 8 deletions emumisc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#![no_std]

extern crate unreachable;
extern crate alloc;

#[macro_use]
mod misc;
Expand Down Expand Up @@ -31,7 +34,7 @@ pub use memory::{as_bytes, allocate_slice, copy_memory};
#[macro_export]
macro_rules! impl_deref {
($container: ty, $field_type: ty, $field: ident) => (
impl ::std::ops::Deref for $container {
impl ::core::ops::Deref for $container {
type Target = $field_type;

#[inline(always)]
Expand All @@ -40,7 +43,7 @@ macro_rules! impl_deref {
}
}

impl ::std::ops::DerefMut for $container {
impl ::core::ops::DerefMut for $container {
#[inline(always)]
fn deref_mut( &mut self ) -> &mut Self::Target {
&mut self.$field
Expand All @@ -52,14 +55,14 @@ macro_rules! impl_deref {
#[macro_export]
macro_rules! impl_as_ref {
($container: ty, $field_type: ty, $field: ident) => (
impl ::std::convert::AsRef< $field_type > for $container {
impl ::core::convert::AsRef< $field_type > for $container {
#[inline(always)]
fn as_ref( &self ) -> &$field_type {
&self.$field
}
}

impl ::std::convert::AsMut< $field_type > for $container {
impl ::core::convert::AsMut< $field_type > for $container {
#[inline(always)]
fn as_mut( &mut self ) -> &mut $field_type {
&mut self.$field
Expand All @@ -74,7 +77,7 @@ macro_rules! newtype {
$(#[$attr])*
pub struct $new( pub $old );

impl ::std::ops::Deref for $new {
impl ::core::ops::Deref for $new {
type Target = $old;

#[inline(always)]
Expand All @@ -83,21 +86,21 @@ macro_rules! newtype {
}
}

impl ::std::ops::DerefMut for $new {
impl ::core::ops::DerefMut for $new {
#[inline(always)]
fn deref_mut( &mut self ) -> &mut Self::Target {
&mut self.0
}
}

impl ::std::convert::AsRef< $old > for $new {
impl ::core::convert::AsRef< $old > for $new {
#[inline(always)]
fn as_ref( &self ) -> &$old {
&self.0
}
}

impl ::std::convert::AsMut< $old > for $new {
impl ::core::convert::AsMut< $old > for $new {
#[inline(always)]
fn as_mut( &mut self ) -> &mut $old {
&mut self.0
Expand Down
8 changes: 5 additions & 3 deletions emumisc/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::ptr;
use std::slice;
use std::mem;
use core::ptr;
use core::slice;
use core::mem;
use alloc::boxed::Box;
use alloc::vec::Vec;

#[inline]
pub fn as_bytes< T: Copy >( array: &[T] ) -> &[u8] {
Expand Down
3 changes: 3 additions & 0 deletions mos6502/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ bitflags = "0.7"
[dependencies.emumisc]
path = "../emumisc"

[features]
default = ["std"]
std = []
3 changes: 1 addition & 2 deletions mos6502/src/generate_decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,8 @@ def generate_opcode_attributes
#[inline(always)]
fn get_register( &self ) -> Register8 {
use std::mem;
unsafe {
mem::transmute( (self.attr & 0b00011000) >> 3 )
core::mem::transmute( (self.attr & 0b00011000) >> 3 )
}
}
Expand Down
6 changes: 6 additions & 0 deletions mos6502/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(error_in_core))]

#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

#[cfg(feature = "std")]
extern crate std as core;

#[macro_use]
extern crate emumisc;

Expand Down
10 changes: 5 additions & 5 deletions mos6502/src/virtual_mos6502.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use self::Register8::*;
use self::Direction::*;

use std::mem::transmute;
use std::fmt;
use std::ops::Deref;
use std::error;
use core::mem::transmute;
use core::fmt;
use core::ops::Deref;

use emumisc::WrappingExtra;

Expand Down Expand Up @@ -227,7 +226,7 @@ impl fmt::Display for EmulationError {
}
}

impl error::Error for EmulationError {
impl core::error::Error for EmulationError {
fn description( &self ) -> &str {
use self::EmulationError::*;
match *self {
Expand Down Expand Up @@ -1018,6 +1017,7 @@ trait Private: Sized + Context {
result
}

#[cfg(feature = "std")]
fn print_registers( &self ) {
println!( " NV-BDIZC A X Y PC" );
println!( " {:08b} {:02X} {:02X} {:02X} {:04X}", self.state().status, self.state().a, self.state().x, self.state().y, self.state().pc );
Expand Down
3 changes: 1 addition & 2 deletions mos6502/src/virtual_mos6502_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,8 @@ macro_rules! decoding_logic {

#[inline(always)]
fn get_register( &self ) -> Register8 {
use std::mem;
unsafe {
mem::transmute( (self.attr & 0b00011000) >> 3 )
core::mem::transmute( (self.attr & 0b00011000) >> 3 )
}
}

Expand Down
14 changes: 5 additions & 9 deletions nes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ version = "0.1.0"
authors = ["Jan Bujak <[email protected]>"]

[dependencies]
byteorder = "0.5"
log = "0.3"
log = { version = "0.4", default-features = false, optional = true }
bitflags = "0.7"

[dependencies.emumisc]
path = "../emumisc"

[dependencies.mos6502]
path = "../mos6502"
default-features = false

[dev-dependencies.nes-testsuite]
path = "../nes-testsuite"

[dev-dependencies.rp2c02-testsuite]
path = "../rp2c02-testsuite"

[profile.test]
opt-level = 2
debug = true
rpath = false
lto = false
debug-assertions = true
codegen-units = 4
[features]
default = ["std"]
std = ["mos6502/std", "log", "log/std"]
7 changes: 5 additions & 2 deletions nes/src/generic_mapper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::fmt;
use core::fmt;
use alloc::vec::Vec;

use emumisc::{PeekPoke, At};
use rom::Mirroring;
use mappers::Mapper;
Expand Down Expand Up @@ -464,6 +466,7 @@ impl GenericMapper {
#[inline]
pub fn poke_cpu_memory_space( &mut self, address: u16, value: u8 ) {
if self.is_cpu_address_writable( address ) == false {
#[cfg(feature = "log")]
warn!( "Unhandled write to 0x{:04X} (value=0x{:02X})", address, value );
return;
}
Expand Down Expand Up @@ -523,7 +526,7 @@ impl Mapper for GenericMapper {
}
}

use std::ops::Sub;
use core::ops::Sub;
use rom::{NesRom, LoadError};
#[inline]
fn wraparound< T: Sub< Output = T > + PartialOrd + Copy >( limit: T, mut value: T ) -> T {
Expand Down
13 changes: 12 additions & 1 deletion nes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(error_in_core))]

#![allow(dead_code)]
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
#![allow(missing_copy_implementations)]

extern crate byteorder;
#[cfg(feature = "std")]
extern crate std as core;

#[cfg(feature = "std")]
extern crate std as alloc;

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(feature = "log")]
#[macro_use]
extern crate log;

Expand Down
1 change: 1 addition & 0 deletions nes/src/mapper_mmc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl Mapper for MapperMMC1 {
_ => unsafe { fast_unreachable!() }
};

#[cfg(feature = "log")]
debug!( "ROM switching mode = {:?}, VROM switching mode = {:?}",
self.rom_switching_mode,
self.vrom_switching_mode
Expand Down
12 changes: 12 additions & 0 deletions nes/src/mappers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use alloc::boxed::Box;
use alloc::format;

use rom::{NesRom, LoadError};
use generic_mapper::GenericMapper;
use mapper_mmc1::MapperMMC1;
Expand All @@ -10,20 +13,24 @@ pub trait Mapper {
fn poke_rom( &mut self, address: u16, value: u8 );

fn peek_sram( &self, address: u16 ) -> u8 {
#[cfg(feature = "log")]
warn!( "Unhandled read from the save RAM at 0x{:04X}", address );
0
}

fn poke_sram( &mut self, address: u16, value: u8 ) {
#[cfg(feature = "log")]
warn!( "Unhandled write to the save RAM at 0x{:04X} (value=0x{:02X})", address, value );
}

fn peek_expansion_rom( &self, address: u16 ) -> u8 {
#[cfg(feature = "log")]
warn!( "Unhandled read from the expansion ROM at 0x{:04X}", address );
0
}

fn poke_expansion_rom( &self, address: u16, value: u8 ) {
#[cfg(feature = "log")]
warn!( "Unhandled write to the expansion ROM at 0x{:04X} (value=0x{:02X})", address, value );
}

Expand All @@ -35,20 +42,24 @@ pub struct MapperNull;

impl Mapper for MapperNull {
fn peek_rom( &self, address: u16 ) -> u8 {
#[cfg(feature = "log")]
warn!( "Unhandled read from the ROM at 0x{:04X}", address );
0
}

fn poke_rom( &mut self, address: u16, value: u8 ) {
#[cfg(feature = "log")]
warn!( "Unhandled write to the ROM at 0x{:04X} (value=0x{:02X})", address, value );
}

fn peek_video_memory( &self, address: u16 ) -> u8 {
#[cfg(feature = "log")]
warn!( "Unhandled read from the VROM at 0x{:04X}", address );
0
}

fn poke_video_memory( &mut self, address: u16, value: u8 ) {
#[cfg(feature = "log")]
warn!( "Unhandled write to the VROM at 0x{:04X} (value=0x{:02X})", address, value );
}
}
Expand All @@ -65,6 +76,7 @@ pub fn create_mapper( rom: NesRom ) -> Result< Box< Mapper >, LoadError > {
mapper.initialize_video_rom( &rom.video_rom[..] );
mapper.initialize_background_tilemaps( rom.mirroring );

#[cfg(feature = "log")]
debug!( "Initialized mapper: {:?}", mapper );
Ok( Box::new( mapper ) )
},
Expand Down
4 changes: 2 additions & 2 deletions nes/src/orphan.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::mem;
use std::ops::{Deref, DerefMut};
use core::mem;
use core::ops::{Deref, DerefMut};

// A shim like this is necessary to implement an orphaned instance in Rust.
// It's very important to keep the structure opaque to keep it safe to use.
Expand Down
Loading

0 comments on commit 59c2a65

Please sign in to comment.