Skip to content

Commit 0d8fce1

Browse files
committed
Create emulate_frame function
Also, events are now polled once a frame instead of every instruction
1 parent 396c5ac commit 0d8fce1

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

core/src/gba/mod.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub use crate::io::{
1212
pub struct GBA {
1313
cpu: CPU,
1414
io: IO,
15+
next_frame_cycle: usize,
1516
}
1617

1718
impl GBA {
@@ -23,14 +24,19 @@ impl GBA {
2324
(GBA {
2425
cpu: CPU::new(false, &mut io),
2526
io,
27+
next_frame_cycle: 0,
2628
}, pixels, debug_windows_spec)
2729
}
2830

29-
pub fn emulate(&mut self) {
31+
pub fn emulate_frame(&mut self) {
3032
self.io.poll_keypad_updates();
31-
self.io.run_dma();
32-
self.cpu.handle_irq(&mut self.io);
33-
self.cpu.emulate_instr(&mut self.io);
33+
// TODO: This will overflow on 32-bit systems
34+
self.next_frame_cycle += CLOCKS_PER_FRAME;
35+
while self.io.cycle < self.next_frame_cycle {
36+
self.io.run_dma();
37+
self.cpu.handle_irq(&mut self.io);
38+
self.cpu.emulate_instr(&mut self.io);
39+
}
3440
}
3541

3642
pub fn peek_mem(&self, region: VisibleMemoryRegion, addr: usize) -> u8 {
@@ -45,7 +51,10 @@ pub const SCALE: usize = 2;
4551
pub const AUDIO_SAMPLE_RATE: usize = 0x8000;
4652
pub const AUDIO_BUFFER_LEN: usize = 4096;
4753
pub const CLOCK_FREQ: usize = 1 << 24;
48-
pub const FRAME_PERIOD: std::time::Duration = std::time::Duration::from_nanos(1e9 as u64 * 280896 / CLOCK_FREQ as u64);
54+
pub const CLOCKS_PER_FRAME: usize = 280896;
55+
pub const FRAME_PERIOD: std::time::Duration = std::time::Duration::from_nanos(
56+
1e9 as u64 * CLOCKS_PER_FRAME as u64 / CLOCK_FREQ as u64
57+
);
4958

5059
#[derive(Clone, Copy)]
5160
pub enum VisibleMemoryRegion {

core/src/io/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub struct IO {
5555
bios_latch: Cell<u32>,
5656

5757
mgba_test_suite: mgba_test_suite::MGBATestSuite,
58-
cycle: usize,
58+
pub cycle: usize,
5959
}
6060

6161
impl IO {

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn main() {
5454
let (mut gba, pixels_mutex, debug_windows_spec_mutex) =
5555
GBA::new(PathBuf::from("suite.gba"), render_tx, keypad_rx);
5656
mutexes_tx.send((pixels_mutex, debug_windows_spec_mutex)).unwrap();
57-
loop { gba.emulate() }
57+
loop { gba.emulate_frame() }
5858
});
5959
let (pixels_mutex, debug_windows_spec_mutex) = mutexes_rx.recv().unwrap();
6060
let mut pixels_lock = None;

0 commit comments

Comments
 (0)