Skip to content

Commit

Permalink
format and migrate to 2018 edition (#76)
Browse files Browse the repository at this point in the history
* run cargo fmt

* cargo fix --edition

* migrate debugger flag to 2018 edition too
  • Loading branch information
MarkMcCaskey authored Mar 2, 2019
1 parent 2ee1436 commit 529b547
Show file tree
Hide file tree
Showing 23 changed files with 245 additions and 221 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "gameboy"
version = "0.1.1"
edition = "2018"
authors = ["MarkMcCaskey <rabidwaffle@gmail.com>"]
description = "A Gameboy emulator and related tools"

Expand Down
2 changes: 1 addition & 1 deletion src/assembler/CSL.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cpu::constants::*;
use std::collections::HashMap;
use cpu::constants::*;

#[derive(Debug, PartialEq, Clone)]
pub enum Value {
Expand Down
38 changes: 16 additions & 22 deletions src/cpu/cartridge/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::{Index, IndexMut};
use std::path::PathBuf;

use cpu::constants::*;
use crate::cpu::constants::*;

/// A thing that is like a Cartridge
pub trait Cartridgey {
Expand Down Expand Up @@ -177,8 +177,7 @@ impl Cartridgey for Cartridge {
ram_bank_selector: ref mut rbs,
ram_active: ref mut ra,
..
}) if index <= 0x7FFF =>
{
}) if index <= 0x7FFF => {
match index {
//RAM activation
0x0000...0x1FFF => {
Expand Down Expand Up @@ -275,15 +274,6 @@ pub enum CartridgeSubType {
ram_bank: [byte; 0x2000],
},
Mbc1 {
/*
MBC1 has two modes:
* 16mbit ROM (with 128 banks), 8KB RAM (1 bank)
* 4mbit (with 32 banks), 32KB RAM (4 banks)
*/
//13 bits for 8KB addressing
//addressing 16mbit = 2MB, (1kb = 10) (8kb = 13) (16kb = 14)
//(2mb = 21)
//21bits to index fully, because first 0x4000 address are sep
memory_model: Mbc1Type,
//memory_banks: [byte; 0x4000], //(2 << 13) + (2 << 21) - 0x4000],
memory_banks: Vec<[byte; 0x4000]>,
Expand Down Expand Up @@ -483,8 +473,8 @@ impl Index<u16> for Cartridge {

impl Cartridge {
pub fn load_ram(&mut self, path: &PathBuf) {
use std::io::Read;
use std::fs::File;
use std::io::Read;

let mut file = match File::open(path) {
Ok(f) => f,
Expand All @@ -495,16 +485,18 @@ impl Cartridge {
Some(CartridgeSubType::Mbc1 {
ram_banks: ref mut ra,
..
}) => for ref mut b in ra {
file.read_exact(&mut b[..]).unwrap();
},
}) => {
for ref mut b in ra {
file.read_exact(&mut b[..]).unwrap();
}
}
_ => (),
}
}

pub fn save_ram(&self, path: &PathBuf) {
use std::io::Write;
use std::fs::File;
use std::io::Write;

let mut file = match File::create(path) {
Ok(f) => f,
Expand All @@ -514,12 +506,14 @@ impl Cartridge {
match self.cart_sub {
Some(CartridgeSubType::Mbc1 {
ram_banks: ref ra, ..
}) => for ref b in ra {
match file.write(&b[..]) {
Ok(_) => (),
Err(e) => error!("Error saving ram: {:?}", e),
}) => {
for ref b in ra {
match file.write(&b[..]) {
Ok(_) => (),
Err(e) => error!("Error saving ram: {:?}", e),
}
}
},
}
_ => (),
}
}
Expand Down
49 changes: 24 additions & 25 deletions src/cpu/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ macro_rules! setter_unsetter_and_getter {
($name_setter:ident, $name_unsetter:ident, $name_getter:ident,
$memory_location:expr) => {
macro_rules! $name_setter {
($name:ident, $location:expr) => {
//TODO: maybe add an option for setting them public?
pub fn $name(&mut self) {
let orig_val = self.mem[$memory_location] as u8;
($name:ident, $location:expr) => {
//TODO: maybe add an option for setting them public?
pub fn $name(&mut self) {
let orig_val = self.mem[$memory_location] as u8;

self.mem[$memory_location] = (orig_val | $location) as byte;
self.mem[$memory_location] = (orig_val | $location) as byte;
}
}
}
}
}

macro_rules! $name_unsetter {
($name:ident, $location:expr) => {
fn $name(&mut self) {
let orig_val = self.mem[$memory_location] as u8;
($name:ident, $location:expr) => {
fn $name(&mut self) {
let orig_val = self.mem[$memory_location] as u8;

self.mem[$memory_location] = (orig_val & (!$location)) as byte;
self.mem[$memory_location] = (orig_val & (!$location)) as byte;
}
}
}
}
}

macro_rules! $name_getter {
($name:ident, $location:expr) => {
pub fn $name(&self) -> bool{
((self.mem[$memory_location] as u8) & $location)
== $location
($name:ident, $location:expr) => {
pub fn $name(&self) -> bool{
((self.mem[$memory_location] as u8) & $location)
== $location
}
}
}
}
}
}
};
}

//NOTE: look into separate sound on/off storage outside of
Expand Down Expand Up @@ -61,29 +61,28 @@ macro_rules! even_odd_dispatch {
($num:expr, $cpu:ident, $func0:ident, $func1:ident,
$f0dispfunc:ident, $f1dispfunc:ident, $f0pcincs:expr,
$f1pcincs:expr) => {

if $num % 2 == 0 {
let adjusted_number:u8 = $num / 2;
let adjusted_number: u8 = $num / 2;
$cpu.$func0($f0dispfunc(adjusted_number));

// TODO: Verify this executes it n-1 times
for _ in 1..($f0pcincs) {
$cpu.inc_pc();
}
} else {
let adjusted_number:u8 = $num / 2;
let adjusted_number: u8 = $num / 2;
$cpu.$func1($f1dispfunc(adjusted_number));

for _ in 1..($f1pcincs) {
$cpu.inc_pc();
}
}
}
};
}

/* Unfortunately, there's just no way to prevent this boiler plate in
Rust right now... The concat_idents! does not work for new identifiers
and interpolate_idents! seems to have problems and only supports nightly
and interpolate_idents! seems to have problems and only supports nightly
anyway.
TODO: fix Rust macro system to allow this or update interpolate_idents
*/
Expand Down
8 changes: 4 additions & 4 deletions src/cpu/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
//! Contains all "Virtual memory" handling, memory bank controlling, and otherwise
//! Its behavior is dictated by the cartridge, but is separate
use std::ops::{Index, IndexMut};
use std::iter::Iterator;
use std::ops::{Index, IndexMut};
use std::path::PathBuf;

use cpu::cartridge::*;
use cpu::constants::*;
use cpu::memvis::cpumemvis::*;
use crate::cpu::cartridge::*;
use crate::cpu::constants::*;
use crate::cpu::memvis::cpumemvis::*;

pub struct Memory {
cartridge: Box<Cartridge>,
Expand Down
2 changes: 1 addition & 1 deletion src/cpu/memvis/cpumemvis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::VecDeque;

use cpu::constants::*;
use crate::cpu::constants::*;

pub trait CpuEventLogger {
fn new(mem: Option<&[u8]>) -> Self;
Expand Down
4 changes: 2 additions & 2 deletions src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::path::PathBuf;
use self::constants::*;
use self::memory::*;
use self::memvis::cpumemvis::*;
use disasm::*;
use crate::disasm::*;

#[inline]
pub fn byte_to_u16(low_byte: u8, high_byte: u8) -> u16 {
Expand Down Expand Up @@ -2305,7 +2305,7 @@ impl Cpu {
}

pub fn remove_old_events(&mut self) {
use io::constants::FADE_DELAY;
use crate::io::constants::FADE_DELAY;

let event_logger = match self.mem.logger {
Some(ref mut logger) => logger,
Expand Down
32 changes: 14 additions & 18 deletions src/cpu/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#[cfg(test)]
use super::constants::*;
#[allow(unused_imports)]
use cpu::*;
use crate::cpu::*;

macro_rules! test_op {
($func:ident, $method:ident, $input:expr, $output_reg:ident,
$expected_output:expr, $flag_find_value:expr,
$flag_expected_value:expr, $pre_exec:expr) => {

#[test]
fn $func() {
let mut cpu = Cpu::new();
Expand All @@ -27,14 +26,13 @@ macro_rules! test_op {
assert_eq!(cpu.$output_reg, $expected_output);
assert_eq!($flag_find_value(cpu.f), $flag_expected_value);
}
}
};
}

macro_rules! test_op_no_arg {
($func:ident, $method:ident, $output_reg:expr, $input:expr,
$expected_output:expr, $flag_find_value:expr,
$flag_expected_value:expr) => {

#[test]
fn $func() {
let mut cpu = Cpu::new();
Expand All @@ -43,18 +41,18 @@ macro_rules! test_op_no_arg {
cpu.$method($output_reg);

assert_eq!(
cpu.access_register($output_reg).expect("invalid register")
, $expected_output);
cpu.access_register($output_reg).expect("invalid register"),
$expected_output
);
assert_eq!($flag_find_value(cpu.f), $flag_expected_value);
}
}
};
}

macro_rules! test_op_really_no_arg {
($func:ident, $method:ident, $output_reg:expr, $input:expr,
$expected_output:expr, $flag_find_value:expr,
$flag_expected_value:expr) => {

#[test]
fn $func() {
let mut cpu = Cpu::new();
Expand All @@ -63,31 +61,29 @@ macro_rules! test_op_really_no_arg {
cpu.$method();

assert_eq!(
cpu.access_register($output_reg).expect("invalid register")
, $expected_output);
cpu.access_register($output_reg).expect("invalid register"),
$expected_output
);
assert_eq!($flag_find_value(cpu.f), $flag_expected_value);
}
}
};
}

macro_rules! test_op16 {
($func:ident, $method:ident, $input_reg:expr, $output_reg:expr, $input:expr,
$expected_output:expr, $flag_find_value:expr,
$flag_expected_value:expr) => {

#[test]
fn $func() {
let mut cpu = Cpu::new();

cpu.set_register16($input_reg, $input);
cpu.$method($input_reg);

assert_eq!(
cpu.access_register16($output_reg)
, $expected_output);
assert_eq!(cpu.access_register16($output_reg), $expected_output);
assert_eq!($flag_find_value(cpu.f), $flag_expected_value);
}
}
};
}

test_op!(
Expand Down Expand Up @@ -626,9 +622,9 @@ fn test_daa() {

#[test]
fn segfault() {
use cpu::cartridge::*;
use crate::cpu::cartridge::*;
// let wat = CartridgeSubType::Dummy;
let cart = Cartridge::new();
let _cart = Cartridge::new();
//let mem = Memory::new();
//let cpu = Cpu::new();
}
4 changes: 2 additions & 2 deletions src/debugger/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use ncurses::*;
// use std::collections::HashMap;
use super::super::disasm::*;
use super::language::*;
use cpu::constants::*;
use cpu::*;
use crate::cpu::constants::*;
use crate::cpu::*;
use std::collections::BTreeSet;

//#[cfg(feature = "debugger")]
Expand Down
22 changes: 16 additions & 6 deletions src/debugger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
//! ncurses-based TUI interactive debugger
#[cfg(feature = "debugger")]
mod language;
#[allow(unknown_lints, useless_attribute, needless_lifetimes, match_same_arms,
cyclomatic_complexity, clone_on_copy, type_complexity, dead_code, unused_comparisons,
unused_label, absurd_extreme_comparisons)]
#[allow(
unknown_lints,
useless_attribute,
needless_lifetimes,
match_same_arms,
cyclomatic_complexity,
clone_on_copy,
type_complexity,
dead_code,
unused_comparisons,
unused_label,
absurd_extreme_comparisons
)]
#[cfg(feature = "debugger")]
pub mod graphics;
#[cfg(feature = "debugger")]
mod language;
#[cfg(feature = "debugger")]
mod tests;

/*
Expand All @@ -26,7 +36,7 @@ mod dbglanguage {

#[cfg(not(feature = "debugger"))]
pub mod graphics {
use cpu::*;
use crate::cpu::*;
pub struct Debugger {}

#[allow(unused_variables, dead_code)]
Expand Down
Loading

0 comments on commit 529b547

Please sign in to comment.