Skip to content

Commit

Permalink
Merge pull request #65 from MarkMcCaskey/development
Browse files Browse the repository at this point in the history
automatically detect controller each frame and print message on unplu…
  • Loading branch information
MarkMcCaskey authored Apr 11, 2017
2 parents 0f6eca1 + 62024bf commit 727d5dd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 92 deletions.
16 changes: 9 additions & 7 deletions src/cpu/cartridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ impl Cartridgey for Cartridge {
});*/
}

#[allow(unused_variables)]
fn read_rom_value(&self, index: u16) -> byte {
3
unimplemented!()
// *self.index(index)
}
fn read_ram_value(&self, index: u16) -> byte {

4
#[allow(unused_variables)]
fn read_ram_value(&self, index: u16) -> byte {
unimplemented!()
//*self.index(index)
//panic!("This cartridge type does not provide RAM")
}
Expand Down Expand Up @@ -223,14 +225,14 @@ impl Index<u16> for Cartridge {
}
Some(CartridgeSubType::Mbc1 { memory_model: Mbc1Type::SixteenEight,
memory_banks: ref mb,
ram_active: ra,
mem_bank_selector: index }) => {
mem_bank_selector: index,
.. }) => {
&mb[((ind - 0x4000) as usize) + ((index * 0x4000) as usize)]
}
Some(CartridgeSubType::Mbc1 { memory_model: Mbc1Type::FourThirtytwo,
Some(CartridgeSubType::Mbc1 { /*memory_model: Mbc1Type::FourThirtytwo,
memory_banks: ref mb,
ram_active: ra,
mem_bank_selector: index }) => {
mem_bank_selector: index*/ .. }) => {
panic!("Indexing {:X}", ind)
}

Expand Down
67 changes: 8 additions & 59 deletions src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,23 @@ impl Cpu {
self.sp = 0xFFFE;
self.pc = 0x100;
self.cycles = 0;
// if let Some(ref mut el) = self.event_logger {
// el.events_deq.clear();
// }

info!("reset");
self.mem.reset();
//a hack to test this
// let temp_mem = [0]

// self.event_logger = Some(DeqCpuEventLogger::new(Some(&temp_mem[..])));

//boot sequence (maybe do this by running it as a proper rom?)
self.set_bc(0x0013);
self.set_de(0x00D8);
self.set_hl(0x014D);
}

pub fn reinit_logger(&mut self) {
panic!("Cannot start logger!");
// self.event_logger = Some(DeqCpuEventLogger::new(Some(&self.mem[..])));
let mut mem_cpy = [0u8; 0xFFFF];

for i in 0..0xFFFF {
mem_cpy[i] = self.mem[i];
}

self.event_logger = Some(DeqCpuEventLogger::new(Some(&mem_cpy[..])));
}

pub fn toggle_logger(&mut self) {
Expand All @@ -305,13 +303,6 @@ impl Cpu {
}
}

// #[inline]
// fn log_event(&mut self, event: CpuEvent) {
// if let Some(ref mut logger) = self.event_logger {
// logger.log_event(self.cycles, event);
// };
// }

///FF04 Div
///
/// This needs to be called 16384 (~16779 on SGB) times a second
Expand Down Expand Up @@ -1092,36 +1083,6 @@ impl Cpu {
}


fn load_bank(&mut self, bank_num: u8) {
unimplemented!()
/*
if let Some(cart_type) = self.cartridge_type {
match cart_type {
CartridgeType::RomMBC1 |
CartridgeType::RomMBC1Ram |
CartridgeType::RomMBC1RamBatt => {
let bn = if (bank_num & 0x1F) == 0 {1} else {bank_num & 0x1F} as usize;
if self.memory_banks.len() <= (bn - 1) {
error!("Tried to swap in memory bank {}, but only {} memory banks exist",
bn - 1, self.memory_banks.len() - 1);
} else {
for i in 0x4000..0x8000 {
self.mem[i] = self.memory_banks[bn - 1][i - 0x4000];
}
}
}
otherwise => {
error!("No support/invalid request to swap in bank {} of cartridge type {:?}",
bank_num, otherwise);
}
}
} else { //could not find cartridge type
error!("No cartridge type specified! Cannot switch banks");
}
*/
}

fn ldnnn(&mut self, nn: CpuRegister, n: u8) {
self.set_register(nn, n as byte);
}
Expand All @@ -1131,12 +1092,6 @@ impl Cpu {
self.set_register(r1, val);
}

// ldr1r2 is probably used instead
// fn ldan(&mut self, n: CpuRegister) {
// let val = self.access_register(n).expect("Invalid register");
// self.set_register(CpuRegister::A, val);
// }

fn ldan16(&mut self, n: CpuRegister16) {
let addr = self.access_register16(n);
let val = self.get_mem(addr);
Expand All @@ -1149,12 +1104,6 @@ impl Cpu {
self.set_register(CpuRegister::A, val);
}

// ldr1r2() is used instead
// fn ldna(&mut self, n: CpuRegister) {
// let val = self.access_register(CpuRegister::A).expect("Invalid register");
// self.set_register(n, val);
// }

fn ldna16(&mut self, n: CpuRegister16) {
let val = self.access_register(CpuRegister::A).expect("Invalid register");
let addr = self.access_register16(n);
Expand Down
34 changes: 30 additions & 4 deletions src/io/applicationstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use sdl2::rect::{Point, Rect};
use std::num::Wrapping;

/// Holds all the data needed to use the emulator in meaningful ways
#[allow(dead_code)] //suppress pointers to things to keep them alive,
// could almost certainly be done in a better way
pub struct ApplicationState {
pub gameboy: cpu::Cpu,
sdl_context: Sdl, // sdl_sound: sdl2::audio,
Expand Down Expand Up @@ -70,7 +72,7 @@ impl ApplicationState {
.build(Root::builder().appender("stdout").build(if trace_mode {
LogLevelFilter::Trace
} else {
LogLevelFilter::Debug
LogLevelFilter::Info
}))
.unwrap();

Expand Down Expand Up @@ -190,6 +192,26 @@ impl ApplicationState {
}
}

pub fn load_controller_if_none_exist(&mut self) {
let (valid_controller, just_unplugged) = if let Some(ref c) = self.controller {
if !c.attached() {
info!("Controller {} has been unplugged", c.name());
(false, true)
} else {
(true, false)
}
} else {
(false, false)
};

match (valid_controller, just_unplugged) {
(true, false) => (),
(false, true) => self.controller = None,
(false, false) => self.controller = setup_controller_subsystem(&self.sdl_context),
_ => unreachable!(),
}
}

pub fn display_coords_to_ui_point(&self, x: i32, y: i32) -> Point {
let s_x = (x as f32 / self.ui_scale) as i32;
let s_y = (y as f32 / self.ui_scale) as i32;
Expand All @@ -210,7 +232,7 @@ impl ApplicationState {
match event {
Event::ControllerAxisMotion { axis, value: val, .. } => {
let deadzone = 10000;
debug!("Axis {:?} moved to {}", axis, val);
trace!("Axis {:?} moved to {}", axis, val);
match axis {
controller::Axis::LeftX if deadzone < (val as i32).abs() => {
if val < 0 {
Expand Down Expand Up @@ -243,7 +265,7 @@ impl ApplicationState {

}
Event::ControllerButtonDown { button, .. } => {
debug!("Button {:?} down", button);
trace!("Button {:?} down", button);
match button {
controller::Button::A => {
self.gameboy.press_a();
Expand All @@ -258,7 +280,7 @@ impl ApplicationState {
}

Event::ControllerButtonUp { button, .. } => {
debug!("Button {:?} up", button);
trace!("Button {:?} up", button);
match button {
controller::Button::A => {
self.gameboy.unpress_a();
Expand Down Expand Up @@ -351,6 +373,7 @@ impl ApplicationState {
}

/// Runs the game application forward one "unit of time"
/// Attepmts to load a controller if it can find one every time a frame is drawn
/// TODO: elaborate
pub fn step(&mut self) {
// handle_events(&mut sdl_context, &mut gameboy);
Expand Down Expand Up @@ -447,6 +470,9 @@ impl ApplicationState {
// TODO make this variable based on whether it's GB, SGB, etc.

if (self.cycle_count - self.prev_time) >= CPU_CYCLES_PER_VBLANK {
// check for controller every time a frame is drawn
self.load_controller_if_none_exist();

if let Some(ref mut dbg) = self.debugger {
dbg.step(&mut self.gameboy);
}
Expand Down
44 changes: 22 additions & 22 deletions src/io/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,27 @@ pub fn setup_audio(sdl_context: &sdl2::Sdl) -> AudioDevice<GBSound> {
};

audio_subsystem.open_playback(None, &desired_spec, |spec| {
// Show obtained AudioSpec
println!("{:?}", spec);

// initialize the audio callback
GBSound {
out_freq: spec.freq as f32,
channel1: SquareWave {
phase_inc: 440.0 / spec.freq as f32,
phase: 0.0,
volume: 0.025,
wave_duty: 0.25,
add: true,
},
channel2: SquareWave {
phase_inc: 440.0 / spec.freq as f32,
phase: 0.0,
volume: 0.025,
wave_duty: 0.25,
add: true,
}
// Show obtained AudioSpec
debug!("{:?}", spec);

}
}).unwrap()
// initialize the audio callback
GBSound {
out_freq: spec.freq as f32,
channel1: SquareWave {
phase_inc: 440.0 / spec.freq as f32,
phase: 0.0,
volume: 0.025,
wave_duty: 0.25,
add: true,
},
channel2: SquareWave {
phase_inc: 440.0 / spec.freq as f32,
phase: 0.0,
volume: 0.025,
wave_duty: 0.25,
add: true,
},
}
})
.unwrap()
}

0 comments on commit 727d5dd

Please sign in to comment.