Compare commits

...

113 Commits

Author SHA1 Message Date
8a352ca807 chore: prevent bitrot 2024-10-13 14:59:24 -05:00
068e4124a6 chore: update dependencies 2024-10-13 14:46:46 -05:00
fedae65cc8 chore: update dependencies 2023-03-17 23:42:51 -05:00
41586d8cc4 chore: update gilrs 2023-02-03 15:20:38 -06:00
79740ac245 chore: update default presenting mode 2022-11-09 08:19:15 -04:00
d6bfde081e feat(ppu): implement double buffering 2022-11-05 02:02:41 -03:00
d2f57ee66b chore: update clap to 4.0 2022-11-05 01:57:44 -03:00
3841d3a98c chore(readme): add build instructions
closes #1
2022-10-26 21:05:00 -03:00
85940c8744 fix: actually paint the gui 2022-10-21 01:17:02 -03:00
c7adf54976 chore: run cargo fmt 2022-10-21 01:13:44 -03:00
f0f9003247 chore: update wgpu 2022-10-21 00:46:14 -03:00
5eba097ad4 chore: update recommended vscode extensions 2022-10-21 00:14:10 -03:00
756c806c2a chore: satisfy clippy 2022-09-12 07:06:09 -03:00
77af5453f0 chore: refactor gui code 2022-09-12 07:00:50 -03:00
41bc4f7afe chore: update dependencies 2022-09-12 06:01:34 -03:00
36e46d3780 chore: update dependencies 2022-04-26 11:45:57 -05:00
926c66746d chore: update some dependencies 2022-04-07 15:30:46 -03:00
cf2357f917 feat: add disassembler that is aware of immediate values 2021-12-16 12:16:58 -04:00
3fb7edd333 chore: add some missing comments to instruction decoding 2021-12-16 12:16:36 -04:00
3ab512d663 fix(ppu): implement proper PPU reset behaviour 2021-12-09 10:46:27 -04:00
61156ca8a8 fix(cart): rename enum variant and enable MBC3 w/ battery 2021-12-09 09:30:47 -04:00
2c411d5792 chore(cart): add test for all-whitespace title 2021-12-09 07:27:52 -04:00
4c657c9832 chore: update dependencies 2021-12-09 06:23:32 -04:00
e72b11f946 feat: add more debug tools to gui 2021-12-09 05:21:05 -04:00
bbcbbd8ce3 chore: fix dependency conflict 2021-12-06 13:36:23 -04:00
c4226e6e76 Merge branch 'main' of ssh://git.musuka.dev:2222/paoda/gb into main 2021-12-06 13:34:14 -04:00
409314a4e5 fix(dbg): don't crash when attempting to read data as executable code 2021-12-06 13:33:22 -04:00
952bf68bd0 chore: add extension to recommended 2021-12-06 13:24:05 -04:00
865c11d53c chore: update dependencies 2021-12-03 16:34:12 -04:00
90d2da9272 chore(gui): implement basic disassembler
Also implement CPU and Interrupt debug information.

Of note:
1. IE and IRQ status boxes are slightly misaligned
2. Whenever the disassembler accidentally reads into game data rather
   than executable code the emulator crashes
   * Thus I should turn Instruction decoding into a Result<> rather than
     panic on failure
2021-11-30 10:23:06 -04:00
4a1a21a08f chore: satisfy clippy 2021-11-30 04:13:12 -04:00
9d23b571fb chore: update cargo.toml 2021-11-30 04:00:34 -04:00
70e0b5868d chore: code cleanup 2021-11-30 03:59:44 -04:00
a0e3c7c602 chore: remove useless default implementations 2021-11-25 04:07:45 -04:00
ef4f3d9ec6 chore(cart): refactor sections of cartridge code 2021-11-25 03:32:22 -04:00
7f872a554b chore(cart): code cleanup 2021-11-25 03:32:18 -04:00
dda0257655 fix(cart): support cartridge headers w/ manufactor codes 2021-11-25 00:12:35 -04:00
c10816c048 chore: imrpove private and public APIs 2021-11-24 22:12:26 -04:00
2405fd027f chore: remove Emulator struct 2021-11-21 05:27:04 -04:00
939c25ce1a chore: update comment 2021-11-15 22:05:26 -04:00
6f5e863645 chore: drop pixels-rs and add egui w/ wgpu backend 2021-11-15 21:34:24 -04:00
ce5d58e1d2 chore(ppu): rename some structs 2021-11-07 14:37:40 -06:00
ebcd5bec5c fix(cart): reset sub-cycle counting in RTC
specifically when RTC S is written to
2021-11-07 13:05:41 -06:00
f2c49b398c chore(cart): improve code quality of RtcClock 2021-11-05 23:05:36 -03:00
6f76571d6c chore: update README 2021-11-05 22:40:31 -03:00
55da5a29d8 chore: remove more getters 2021-11-01 18:04:22 +09:00
7dfcd44820 chore(emu): remove bus and bus_mut from cpu 2021-11-01 17:56:42 +09:00
80a15039e0 chore(apu): improve accuracy of Channel 1 2021-10-30 21:53:08 +09:00
32405c0734 fix(apu): implement some obscure behaviour for ch1,2 & 3 2021-10-30 20:04:16 +09:00
2ef8fefbb7 chore(apu): implement more obscure behaviour
implement capacitor
2021-10-30 18:41:31 +09:00
548945a4ee chore: remove imports of tracing macros 2021-10-30 16:41:06 +09:00
1526b4a09d fix(cart): improve title parsin
Now both Tokimeki Memorial titles are read properly
2021-10-30 15:48:47 +09:00
4125ea5c74 fix(instr): fix timing issues with select instructions
instr_timing now runs (and fails on everything)
2021-10-30 14:02:28 +09:00
b9519d9b7a chore(bus): stub some CGB IO registers 2021-10-30 14:02:05 +09:00
01278ca83f chore: reduce size of public interface 2021-10-30 10:28:20 +09:00
2bb8bd6d8f chore: update dependencies 2021-10-30 09:37:24 +09:00
080c1e7518 fix(bus): don't panic on non-existent cartridge 2021-10-28 22:19:38 -03:00
765f9d8288 Merge branch 'main' of ssh://musuka.dev:2222/paoda/gb 2021-10-28 22:08:04 -03:00
8780c4a59b chore(emu): remove and replace EmulatorBuilder 2021-10-28 22:07:36 -03:00
e19a540650 chore: upgrade to rust 2021 2021-10-22 01:42:40 -03:00
4cfd951ede chore(ppu): improve bg-sprite priority functionality 2021-10-20 20:08:09 -03:00
de8fe1a367 chore(ppu): remove object buffer iter_mut method 2021-10-20 19:41:27 -03:00
c5fa41b20d chore(ppu): refactor FetcherState 2021-10-20 19:40:29 -03:00
552cfd4a18 chore(ppu): sort OAM Memory 2021-10-20 17:36:38 -03:00
4c516804e4 fix(ppu): refactor and improve accuracy of background fetcher 2021-10-20 16:58:19 -03:00
9b3ab73bb1 fix(ppu): incremental improvements to accuracy of ppu fetcher 2021-10-20 05:21:49 -03:00
d9f1d661ae chore(ppu): refactor behaviour w.r.t window enabling 2021-10-20 04:48:20 -03:00
da83032e24 chore(ppu): use "dot" intead of "cycle" 2021-10-20 03:34:09 -03:00
67e2447d3f chore(ppu): rename window_stat to win_stat 2021-10-20 03:16:37 -03:00
37cf3d92e4 chore(ppu): refactor OAM Scan implementation 2021-10-20 03:14:30 -03:00
293e5762c3 chore: small code-cleanup changes 2021-10-20 02:48:44 -03:00
999f661e6b chore: update cargo.lock 2021-10-14 14:58:25 -03:00
e573411890 chore: update dependencies 2021-10-11 00:06:51 -03:00
352a65b705 chore(apu): update spsc depenency 2021-10-07 15:27:51 -03:00
64230973f1 chore: suggest inline for hot code 2021-09-24 16:16:14 -03:00
dbbf87af52 chore: add a creative-commons boot rom for compat 2021-09-24 16:15:55 -03:00
1440cd1fc7 chore: update README.md 2021-09-24 12:34:48 -03:00
9964b49ce1 fix(ppu): improve accuracy of timer 2021-09-21 12:50:31 -03:00
142231d355 chore(ppu): remoe unnecessary brackets 2021-09-21 12:13:55 -03:00
227928e8ca Revert "fix(ppu): explicity choose to use sign extension"
This reverts commit 1001b0b124.
2021-09-21 12:09:35 -03:00
1001b0b124 fix(ppu): explicity choose to use sign extension 2021-09-21 09:52:12 -03:00
71ce3f43e0 chore: satisfy clippy 2021-09-20 04:15:05 -03:00
ce121864d2 feat: implement cartridge saving
Implemented for MBC1, MBC2, MBC3 and MBC5
2021-09-20 04:13:25 -03:00
e1fe00ab64 fix: have start and select keybinds match other emulators 2021-09-20 03:26:06 -03:00
5882678bc5 chore: inline some functions
Some checks reported errors
continuous-integration/drone/push Build was killed
2021-09-20 01:34:41 -03:00
bcd67cb317 chore: begin refactor of public api 2021-09-20 01:34:21 -03:00
4516ca8477 chore: run cargo fix
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-12 04:56:39 -03:00
6087e3b20b chore: remove Cycle struct and begin scheduler design 2021-09-12 04:56:34 -03:00
10ac579c40 fix(main): Use LogicalSize and PhysicalSize properly 2021-09-11 22:56:40 -03:00
ee5504111b Merge branch 'main' of ssh://git.musuka.dev:2222/paoda/gb into main
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-11 20:48:05 -03:00
a628f64d28 chore: update dependencies 2021-09-11 20:47:45 -03:00
318a6e0386 fix(emu): remove GAMEPAD_ENABLED const flag
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-09 11:12:50 -03:00
db012c7f4b fix(main): remove code unrelated to audio sync
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-07 02:09:02 -03:00
e42c87aeb7 fix(apu): quiet gameboy APU 2021-09-07 01:52:02 -03:00
9113e95fa0 fix(apu): pass blargg apu sweep tests 2021-09-07 01:17:01 -03:00
9973dc8714 fix(cartridge): don't read from RAM that doesn't exist
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-06 23:45:04 -03:00
e128025208 fix(cartridge): improve accuracy of MBC1 with large file sizes 2021-09-06 23:37:55 -03:00
44ac0c8ebd feat(cartridge): implement MBC2
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-06 18:13:34 -03:00
01064bab69 chore(cpu): comment out blargg-specific code
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-22 01:49:01 -05:00
634bc2d2c0 fix(apu): remove redundant code 2021-08-22 01:48:34 -05:00
d794a94b68 fix(timer): increase accuracy of timer
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-20 00:17:28 -05:00
b87e31d3f4 fix(cartridge): remove unnecessary dbg statement 2021-08-20 00:17:05 -05:00
3c2456611e fix(cartridge): trim whitespace from cartridge title
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-19 21:56:07 -05:00
b829f05a34 chore(cartridge): clean-up code
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-19 20:39:04 -05:00
afd2d16371 chore(cartridge): re-rename RamInfo and RomRinfo 2021-08-19 20:05:48 -05:00
1f8fa48168 chore: add reccomended vscode extensions
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-19 17:05:36 -05:00
c6fbb79189 chore(cartridge): reognanize code 2021-08-19 16:10:39 -05:00
8b78b2943e fix(cartridge): specify intentional overflow in MBC5
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-19 16:09:58 -05:00
0af95a1dd3 feat(cartridge): implement MBC5 and cleanup code
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-18 22:40:15 -05:00
9fa40f8584 fix(main): do not drop output stream 2021-08-18 22:39:55 -05:00
b10bc7b4fd chore(main): add flag that enables audio 2021-08-18 16:34:26 -05:00
4658a0d106 fix(apu): ch4 set_len should set timer to 64 - len
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-18 02:32:48 -05:00
f92b9d61ef chore(apu_gen): cosmetic changes to sample generation
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-18 01:43:38 -05:00
25 changed files with 4556 additions and 2966 deletions

9
.vscode/extensions.json vendored Normal file
View File

@@ -0,0 +1,9 @@
{
"recommendations": [
"rust-lang.rust-analyzer",
"tamasfe.even-better-toml",
"serayuzgur.crates",
"vadimcn.vscode-lldb",
"donaldhays.rgbds-z80"
]
}

2910
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -2,20 +2,27 @@
name = "gb"
version = "0.1.0"
authors = ["Rekai Musuka <rekai@musuka.dev>"]
edition = "2018"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "^1.0"
bitfield = "^0.13"
clap = "^2.33"
gilrs = "^0.8"
pixels = "^0.5"
winit = "^0.25"
winit_input_helper = "^0.10"
rodio = "^0.14"
rtrb = "^0.1.4"
anyhow = "1.0"
bitfield = "0.17"
clap = { version = "4.0", features = ["cargo"] }
gilrs = "0.11"
winit = "0.28"
egui = "0.21"
egui_wgpu_backend = "0.22"
egui_winit_platform = "0.18"
pollster = "0.3"
rodio = "0.19"
rtrb = "0.3"
directories-next = "2.0"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["std", "env-filter"] }
thiserror = "1.0"
once_cell = "1.10"
[profile.release]
debug = true

View File

@@ -1,14 +1,43 @@
# Rekai's Gameboy Emulator
[![Build Status](https://ci.paoda.moe/api/badges/paoda/gb/status.svg)](https://ci.paoda.moe/paoda/gb)
### Status
* Passes Blargg's cpu_instrs Test
* Renders Background & Window Tiles
* Implements a PPU FIFO
* From [Blargg Test ROMs](https://github.com/L-P/blargg-test-roms/)
* [x] cpu_instrs
* [x] instr_timing
* [x] mem_timing
* [x] mem_timing-2
* [ ] dmg_sound (partial)
* [x] [dmg-acid2](https://github.com/mattcurrie/dmg-acid2)
* From [mooneye-gb](https://github.com/Gekkio/mooneye-gb):
* Cartridges:
* [x] MBC1
* [ ] MBC1M
* [x] MBC2
* [x] MBC5
* Implements a cycle-accurate PPU FIFO
* Doesn't \*exactly\* work just yet
Supports: ROM-only, MBC1, MBC2, MBC3 and MBC5 games.
### Notes
* [gameboy-logs](https://github.com/wheremyfoodat/Gameboy-logs) suggests that there are still some underlying problems with the cpu implementation
* The Sprite FIFO does not work as expected yet
* Sound is neither emulated nor stubbed. Upon writing / reading to a APU related register the emulator will panic.
* Code cleanup is pending completion of some minimum viable product of the emulator
### Compiling
This project was last successfully built on [Rust 1.64.0](https://github.com/rust-lang/rust/tree/1.64.0)
1. `git clone https://github.com/paoda/gb`
2. `cd gb`
3. `cargo run --release`
### Controls
Controls are defined [here](https://github.com/paoda/gb/blob/85940c874460b9cb31bf9b8211bf7dda596d114a/src/joypad.rs#L114)
Key | Button
--- | ---
<kbd>X</kbd> | B
<kbd>Z</kbd> | A
<kbd>Enter</kbd> | START
<kbd>Shift</kbd> | SELECT
Then use the Arrow keys for the D-Pad
### Credits
The Boot ROM found in the `bin/` directory was made by [Optix](https://github.com/Hacktix) over [here](https://github.com/Hacktix/Bootix)

BIN
bin/bootix_dmg.bin Normal file

Binary file not shown.

View File

@@ -1,3 +1,4 @@
use crate::apu::gen::SAMPLE_RATE;
use crate::bus::BusIo;
use crate::emu::SM83_CLOCK_SPEED;
use gen::SampleProducer;
@@ -5,8 +6,8 @@ use types::ch1::{Sweep, SweepDirection};
use types::ch3::Volume as Ch3Volume;
use types::ch4::{CounterWidth, Frequency as Ch4Frequency, PolynomialCounter};
use types::common::{EnvelopeDirection, FrequencyHigh, SoundDuty, VolumeEnvelope};
use types::fs::{FrameSequencer, State as FrameSequencerState};
use types::{ChannelControl, SoundOutput};
use types::fs::{FrameSequencer, State as FSState};
use types::{ChannelControl, NRx4, SoundOutput};
pub mod gen;
mod types;
@@ -26,11 +27,13 @@ pub struct Apu {
/// Noise
ch4: Channel4,
sequencer: FrameSequencer,
fs: FrameSequencer,
div_prev: Option<u16>,
prod: Option<SampleProducer<f32>>,
pub(crate) prod: Option<SampleProducer<f32>>,
sample_counter: u64,
cap: f32,
}
impl BusIo for Apu {
@@ -54,7 +57,7 @@ impl BusIo for Apu {
0x26 => self.ctrl.status(self),
0x30..=0x3F => self.ch3.read_byte(addr),
_ => {
eprintln!("Read 0xFF from unused IO register {:#06X} [APU]", addr);
tracing::warn!("Attempted read from {:#06X}", addr);
0xFF
}
}
@@ -66,29 +69,26 @@ impl BusIo for Apu {
0x11 if self.ctrl.enabled => self.ch1.set_duty(byte),
0x12 if self.ctrl.enabled => self.ch1.set_envelope(byte),
0x13 if self.ctrl.enabled => self.ch1.set_freq_lo(byte),
0x14 if self.ctrl.enabled => self.ch1.set_freq_hi(byte),
0x14 if self.ctrl.enabled => self.ch1.set_freq_hi(&self.fs, byte),
0x16 if self.ctrl.enabled => self.ch2.set_duty(byte),
0x17 if self.ctrl.enabled => self.ch2.set_envelope(byte),
0x18 if self.ctrl.enabled => self.ch2.set_freq_lo(byte),
0x19 if self.ctrl.enabled => self.ch2.set_freq_hi(byte),
0x19 if self.ctrl.enabled => self.ch2.set_freq_hi(&self.fs, byte),
0x1A if self.ctrl.enabled => self.ch3.set_dac_enabled(byte),
0x1B if self.ctrl.enabled => self.ch3.set_len(byte),
0x1C if self.ctrl.enabled => self.ch3.set_volume(byte),
0x1D if self.ctrl.enabled => self.ch3.set_freq_lo(byte),
0x1E if self.ctrl.enabled => self.ch3.set_freq_hi(byte),
0x1E if self.ctrl.enabled => self.ch3.set_freq_hi(&self.fs, byte),
0x20 if self.ctrl.enabled => self.ch4.set_len(byte),
0x21 if self.ctrl.enabled => self.ch4.set_envelope(byte),
0x22 if self.ctrl.enabled => self.ch4.set_poly(byte),
0x23 if self.ctrl.enabled => self.ch4.set_frequency(byte),
0x23 if self.ctrl.enabled => self.ch4.set_frequency(&self.fs, byte),
0x24 if self.ctrl.enabled => self.ctrl.set_channel(byte),
0x25 if self.ctrl.enabled => self.ctrl.set_output(byte),
0x26 => self.set_status(byte),
0x30..=0x3F => self.ch3.write_byte(addr, byte),
_ if !self.ctrl.enabled => {}
_ => eprintln!(
"Wrote {:#04X} to unused IO register {:#06X} [APU]",
byte, addr
),
_ => tracing::warn!("Attempted write of {:#04X} to {:#06X}", byte, addr),
}
}
}
@@ -99,9 +99,9 @@ impl Apu {
// Frame Sequencer (512Hz)
if self.is_falling_edge(12, div) {
use FrameSequencerState::*;
use FSState::*;
match self.sequencer.state() {
match self.fs.state() {
Length => self.clock_length(),
LengthAndSweep => {
self.clock_length();
@@ -111,7 +111,7 @@ impl Apu {
Nothing => {}
}
self.sequencer.next();
self.fs.next();
}
self.div_prev = Some(div);
@@ -124,33 +124,42 @@ impl Apu {
if self.sample_counter >= SM83_CLOCK_SPEED {
self.sample_counter %= SM83_CLOCK_SPEED;
if let Some(ref mut prod) = self.prod {
if let Some(prod) = self.prod.as_mut() {
if prod.available_blocking() {
// Sample the APU
let ch1_amplitude = self.ch1.amplitude();
let ch1_left = self.ctrl.output.ch1_left() as u8 as f32 * ch1_amplitude;
let ch1_right = self.ctrl.output.ch1_right() as u8 as f32 * ch1_amplitude;
let ch2_amplitude = self.ch2.amplitude();
let ch2_left = self.ctrl.output.ch2_left() as u8 as f32 * ch2_amplitude;
let ch2_right = self.ctrl.output.ch2_right() as u8 as f32 * ch2_amplitude;
let ch1_amplitude =
Self::high_pass(&mut self.cap, self.ch1.amplitude(), self.ch1.enabled);
let (left, right) = self.ctrl.out.ch1();
let ch1_left = if left { ch1_amplitude } else { 0.0 };
let ch1_right = if right { ch1_amplitude } else { 0.0 };
let ch3_amplitude = self.ch3.amplitude();
let ch3_left = self.ctrl.output.ch3_left() as u8 as f32 * ch3_amplitude;
let ch3_right = self.ctrl.output.ch3_right() as u8 as f32 * ch3_amplitude;
let ch2_amplitude =
Self::high_pass(&mut self.cap, self.ch2.amplitude(), self.ch2.enabled);
let (left, right) = self.ctrl.out.ch2();
let ch2_left = if left { ch2_amplitude } else { 0.0 };
let ch2_right = if right { ch2_amplitude } else { 0.0 };
let ch4_amplitude = self.ch4.amplitude();
let ch4_left = self.ctrl.output.ch4_left() as u8 as f32 * ch4_amplitude;
let ch4_right = self.ctrl.output.ch4_right() as u8 as f32 * ch4_amplitude;
let ch3_amplitude =
Self::high_pass(&mut self.cap, self.ch3.amplitude(), self.ch3.enabled);
let (left, right) = self.ctrl.out.ch3();
let ch3_left = if left { ch3_amplitude } else { 0.0 };
let ch3_right = if right { ch3_amplitude } else { 0.0 };
let ch4_amplitude =
Self::high_pass(&mut self.cap, self.ch4.amplitude(), self.ch4.enabled);
let (left, right) = self.ctrl.out.ch4();
let ch4_left = if left { ch4_amplitude } else { 0.0 };
let ch4_right = if right { ch4_amplitude } else { 0.0 };
let left_mixed = (ch1_left + ch2_left + ch3_left + ch4_left) / 4.0;
let right_mixed = (ch1_right + ch2_right + ch3_right + ch4_right) / 4.0;
let left = (self.ctrl.channel.left_volume() + 1.0) * left_mixed;
let right = (self.ctrl.channel.right_volume() + 1.0) * right_mixed;
let left_sample = (self.ctrl.channel.left_volume() + 1.0) * left_mixed;
let right_sample = (self.ctrl.channel.right_volume() + 1.0) * right_mixed;
prod.push(left)
.and(prod.push(right))
prod.push(left_sample)
.and(prod.push(right_sample))
.expect("Add samples to ring buffer");
}
}
@@ -163,7 +172,7 @@ impl Apu {
if self.ctrl.enabled {
// Frame Sequencer reset to Step 0
self.sequencer.reset();
self.fs.reset();
// Square Duty units are reset to first step
self.ch1.duty_pos = 0;
@@ -179,10 +188,6 @@ impl Apu {
}
}
pub fn attach_producer(&mut self, prod: SampleProducer<f32>) {
self.prod = Some(prod);
}
fn reset(&mut self) {
self.ch1.sweep = Default::default();
self.ch1.duty = Default::default();
@@ -206,8 +211,8 @@ impl Apu {
self.ch4.poly = Default::default();
self.ch4.freq = Default::default();
self.ctrl.channel = Default::default();
self.ctrl.output = Default::default();
self.ctrl.channel = ChannelControl(0);
self.ctrl.out = SoundOutput(0);
// Disable the Channels
self.ch1.enabled = Default::default();
@@ -216,25 +221,13 @@ impl Apu {
self.ch4.enabled = Default::default();
}
fn process_length(freq_hi: &FrequencyHigh, length_timer: &mut u16, enabled: &mut bool) {
if freq_hi.length_disable() && *length_timer > 0 {
*length_timer -= 1;
fn process_length(freq: &impl NRx4, counter: &mut u16, enabled: &mut bool) {
if freq.length_enable() && *counter > 0 {
*counter -= 1;
// Check in this scope ensures (only) the above subtraction
// made length_timer 0
if *length_timer == 0 {
*enabled = false;
}
}
}
fn ch4_process_length(freq: &Ch4Frequency, length_timer: &mut u16, enabled: &mut bool) {
if freq.length_disable() && *length_timer > 0 {
*length_timer -= 1;
// Check in this scope ensures (only) the above subtraction
// made length_timer 0
if *length_timer == 0 {
if *counter == 0 {
*enabled = false;
}
}
@@ -243,25 +236,25 @@ impl Apu {
fn clock_length(&mut self) {
Self::process_length(
&self.ch1.freq_hi,
&mut self.ch1.length_timer,
&mut self.ch1.length_counter,
&mut self.ch1.enabled,
);
Self::process_length(
&self.ch2.freq_hi,
&mut self.ch2.length_timer,
&mut self.ch2.length_counter,
&mut self.ch2.enabled,
);
Self::process_length(
&self.ch3.freq_hi,
&mut self.ch3.length_timer,
&mut self.ch3.length_counter,
&mut self.ch3.enabled,
);
Self::ch4_process_length(
Self::process_length(
&self.ch4.freq,
&mut self.ch4.length_timer,
&mut self.ch4.length_counter,
&mut self.ch4.enabled,
);
}
@@ -278,7 +271,7 @@ impl Apu {
if self.ch1.sweep_enabled && period != 0 {
let new_freq = self.ch1.calc_sweep_freq();
if new_freq <= 2047 && self.ch1.sweep.shift_count() != 0 {
if new_freq <= 0x7FF && self.ch1.sweep.shift_count() != 0 {
self.ch1.set_frequency(new_freq);
self.ch1.shadow_freq = new_freq;
@@ -336,18 +329,41 @@ impl Apu {
None => false,
}
}
fn high_pass(capacitor: &mut f32, input: f32, enabled: bool) -> f32 {
const CHARGE_FACTOR: f32 = 0.999958;
let mut output = 0.0;
if enabled {
output = input - *capacitor;
*capacitor =
input - output * CHARGE_FACTOR.powi(SM83_CLOCK_SPEED as i32 / SAMPLE_RATE as i32);
}
output
}
}
#[derive(Debug, Default)]
#[derive(Debug)]
pub(crate) struct SoundControl {
/// 0xFF24 | NR50 - Channel Control
channel: ChannelControl,
/// 0xFF25 | NR51 - Selection of Sound output terminal
output: SoundOutput,
out: SoundOutput,
enabled: bool,
}
impl Default for SoundControl {
fn default() -> Self {
Self {
channel: ChannelControl(0),
out: SoundOutput(0),
enabled: Default::default(),
}
}
}
impl SoundControl {
/// 0xFF24 | NR50 - Channel Control
pub(crate) fn channel(&self) -> u8 {
@@ -363,13 +379,13 @@ impl SoundControl {
/// 0xFF25 | NR51 - Selection of Sound output terminal
pub(crate) fn output(&self) -> u8 {
u8::from(self.output)
u8::from(self.out)
}
/// 0xFF25 | NR51 - Selection of Sound output terminal
pub(crate) fn set_output(&mut self, byte: u8) {
if self.enabled {
self.output = byte.into();
self.out = byte.into();
}
}
@@ -407,7 +423,7 @@ pub(crate) struct Channel1 {
sweep_enabled: bool,
// Length Functionality
length_timer: u16,
length_counter: u16,
freq_timer: u16,
duty_pos: u8,
@@ -434,11 +450,11 @@ impl Channel1 {
/// 0xFF11 | NR11 - Channel 1 Sound length / Wave pattern duty
pub(crate) fn set_duty(&mut self, byte: u8) {
self.duty = byte.into();
self.length_timer = 64 - self.duty.sound_length() as u16;
self.length_counter = 64 - self.duty.sound_length() as u16;
}
/// 0xFF12 | NR12 - Channel 1 Volume Envelope
pub fn envelope(&self) -> u8 {
pub(crate) fn envelope(&self) -> u8 {
u8::from(self.envelope)
}
@@ -462,38 +478,54 @@ impl Channel1 {
}
/// 0xFF14 | NR14 - Channel 1 Frequency high
pub(crate) fn set_freq_hi(&mut self, byte: u8) {
self.freq_hi = byte.into();
pub(crate) fn set_freq_hi(&mut self, fs: &FrameSequencer, byte: u8) {
let mut new_freq: FrequencyHigh = byte.into();
// If this bit is set, a trigger event occurs
if self.freq_hi.initial() {
// Envelope Behaviour during trigger event
if new_freq.trigger() {
self.enabled = true;
if self.length_counter == 0 {
self.length_counter = 64;
// length_counter was 0 so length evidently wasn't enabled
self.freq_hi.set_length_enable(false);
};
// reload freq_timer but last two bits are unmodified
self.freq_timer = obscure::square::freq_timer_reload(self.freq_timer, self.frequency());
// Volume Envelope loaded w/ period
self.period_timer = self.envelope.period();
// Channel Volume reloaded
self.current_volume = self.envelope.init_vol();
// Sweep behaviour during trigger event
// Channel 1 Sweep Behaviour
let sweep_period = self.sweep.period();
let sweep_shift = self.sweep.shift_count();
self.shadow_freq = self.frequency();
self.sweep_timer = if sweep_period == 0 { 8 } else { sweep_period };
if sweep_period != 0 || sweep_shift != 0 {
self.sweep_enabled = true;
}
self.sweep_enabled = sweep_period != 0 || sweep_shift != 0;
if sweep_shift != 0 {
let _ = self.calc_sweep_freq();
}
// Length behaviour during trigger event
if self.length_timer == 0 {
self.length_timer = 64;
}
if self.is_dac_enabled() {
self.enabled = true;
}
self.enabled = self.is_dac_enabled();
}
obscure::nrx4::length_update(
&mut new_freq,
fs,
&mut self.length_counter,
&mut self.enabled,
self.freq_hi.length_enable(),
);
self.freq_hi = new_freq;
}
fn tick(&mut self) {
@@ -508,8 +540,10 @@ impl Channel1 {
}
fn amplitude(&self) -> f32 {
if self.is_dac_enabled() && self.enabled {
let input = self.duty.wave_pattern().amplitude(self.duty_pos) * self.current_volume;
if self.is_dac_enabled() {
let sample = self.duty.wave_pattern().amplitude(self.duty_pos) * self.current_volume;
let input = if self.enabled { sample } else { 0 };
(input as f32 / 7.5) - 1.0
} else {
0.0
@@ -553,7 +587,7 @@ impl Channel1 {
pub(crate) struct Channel2 {
/// 0xFF16 | NR21 - Channel 2 Sound length / Wave Pattern Duty
duty: SoundDuty,
/// 0xFF17 | NR22 - Channel 2 Volume ENvelope
/// 0xFF17 | NR22 - Channel 2 Volume Envelope
envelope: VolumeEnvelope,
/// 0xFF18 | NR23 - Channel 2 Frequency low (lower 8 bits only)
freq_lo: u8,
@@ -565,7 +599,7 @@ pub(crate) struct Channel2 {
current_volume: u8,
// Length Functionality
length_timer: u16,
length_counter: u16,
freq_timer: u16,
duty_pos: u8,
@@ -582,15 +616,15 @@ impl Channel2 {
/// 0xFF16 | NR21 - Channel 2 Sound length / Wave Pattern Duty
pub(crate) fn set_duty(&mut self, byte: u8) {
self.duty = byte.into();
self.length_timer = 64 - self.duty.sound_length() as u16;
self.length_counter = 64 - self.duty.sound_length() as u16;
}
/// 0xFF17 | NR22 - Channel 2 Volume ENvelope
/// 0xFF17 | NR22 - Channel 2 Volume Envelope
pub(crate) fn envelope(&self) -> u8 {
u8::from(self.envelope)
}
/// 0xFF17 | NR22 - Channel 2 Volume ENvelope
/// 0xFF17 | NR22 - Channel 2 Volume Envelope
pub(crate) fn set_envelope(&mut self, byte: u8) {
self.envelope = byte.into();
@@ -610,28 +644,44 @@ impl Channel2 {
}
/// 0xFF19 | NR24 - Channel 2 Frequency high
pub(crate) fn set_freq_hi(&mut self, byte: u8) {
pub(crate) fn set_freq_hi(&mut self, fs: &FrameSequencer, byte: u8) {
let prev_le = self.freq_hi.length_enable();
self.freq_hi = byte.into();
if self.freq_hi.initial() {
// Envelope behaviour during trigger event
obscure::nrx4::length_update(
&mut self.freq_hi,
fs,
&mut self.length_counter,
&mut self.enabled,
prev_le,
);
if self.freq_hi.trigger() {
self.enabled = true;
// Reload length counter if need be
if self.length_counter == 0 {
self.length_counter = 64;
}
// reload frequency timer
self.freq_timer = obscure::square::freq_timer_reload(self.freq_timer, self.frequency());
// reload envelope
self.period_timer = self.envelope.period();
// reload volume
self.current_volume = self.envelope.init_vol();
// Length behaviour during trigger event
if self.length_timer == 0 {
self.length_timer = 64;
}
if self.is_dac_enabled() {
self.enabled = true;
}
self.enabled = self.is_dac_enabled();
}
}
fn amplitude(&self) -> f32 {
if self.is_dac_enabled() && self.enabled {
let input = self.duty.wave_pattern().amplitude(self.duty_pos) * self.current_volume;
if self.is_dac_enabled() {
let sample = self.duty.wave_pattern().amplitude(self.duty_pos) * self.current_volume;
let input = if self.enabled { sample } else { 0 };
(input as f32 / 7.5) - 1.0
} else {
0.0
@@ -674,7 +724,7 @@ pub(crate) struct Channel3 {
wave_ram: [u8; WAVE_PATTERN_RAM_LEN],
// Length Functionality
length_timer: u16,
length_counter: u16,
freq_timer: u16,
offset: u8,
@@ -720,7 +770,7 @@ impl Channel3 {
/// 0xFF1B | NR31 - Sound Length
pub(crate) fn set_len(&mut self, byte: u8) {
self.len = byte;
self.length_timer = 256 - self.len as u16;
self.length_counter = 256 - self.len as u16;
}
/// 0xFF1C | NR32 - Channel 3 Volume
@@ -752,18 +802,32 @@ impl Channel3 {
}
/// 0xFF1E | NR34 - Channel 3 Frequency high
pub(crate) fn set_freq_hi(&mut self, byte: u8) {
pub(crate) fn set_freq_hi(&mut self, fs: &FrameSequencer, byte: u8) {
let prev_le = self.freq_hi.length_enable();
self.freq_hi = byte.into();
if self.freq_hi.initial() {
obscure::nrx4::length_update(
&mut self.freq_hi,
fs,
&mut self.length_counter,
&mut self.enabled,
prev_le,
);
if self.freq_hi.trigger() {
self.enabled = true;
// Length behaviour during trigger event
if self.length_timer == 0 {
self.length_timer = 256;
if self.length_counter == 0 {
self.length_counter = 256;
}
if self.dac_enabled {
self.enabled = true;
}
self.freq_timer = (2048 - self.frequency()) * 2;
// reset wave channel's ptr into wave RAM
self.offset = 0;
self.enabled = self.dac_enabled;
}
}
@@ -779,8 +843,10 @@ impl Channel3 {
}
fn amplitude(&self) -> f32 {
if self.dac_enabled && self.enabled {
let input = self.read_sample(self.offset) >> self.volume.shift_count();
if self.dac_enabled {
let sample = self.read_sample(self.offset) >> self.volume.shift_count();
let input = if self.enabled { sample } else { 0 };
(input as f32 / 7.5) - 1.0
} else {
0.0
@@ -820,7 +886,7 @@ pub(crate) struct Channel4 {
current_volume: u8,
// Length Functionality
length_timer: u16,
length_counter: u16,
/// Linear Feedback Shift Register (15-bit)
lf_shift: u16,
@@ -834,7 +900,7 @@ impl Channel4 {
/// 0xFF20 | NR41 - Channel 4 Sound Length
pub(crate) fn set_len(&mut self, byte: u8) {
self.len = byte & 0x3F;
self.length_timer = 256 - self.len as u16;
self.length_counter = 64 - self.len as u16;
}
/// 0xFF21 | NR42 - Channel 4 Volume Envelope
@@ -867,25 +933,37 @@ impl Channel4 {
}
/// 0xFF23 | NR44 - Channel 4 Counter / Consecutive Selector and Restart
pub(crate) fn set_frequency(&mut self, byte: u8) {
pub(crate) fn set_frequency(&mut self, fs: &FrameSequencer, byte: u8) {
let prev_le = self.freq.length_enable();
self.freq = byte.into();
if self.freq.initial() {
// Envelope behaviour during trigger event
obscure::nrx4::length_update(
&mut self.freq,
fs,
&mut self.length_counter,
&mut self.enabled,
prev_le,
);
if self.freq.trigger() {
self.enabled = true;
if self.length_counter == 0 {
self.length_counter = 64;
}
// FIXME: Frequency Timer reloaded?
// reload envelope
self.period_timer = self.envelope.period();
//reload volume
self.current_volume = self.envelope.init_vol();
// Length behaviour during trigger event
if self.length_timer == 0 {
self.length_timer = 64;
}
// LFSR behaviour during trigger event
// LFSR reset
self.lf_shift = 0x7FFF;
if self.is_dac_enabled() {
self.enabled = true;
}
self.enabled = self.is_dac_enabled();
}
}
@@ -908,8 +986,10 @@ impl Channel4 {
}
fn amplitude(&self) -> f32 {
if self.is_dac_enabled() && self.enabled {
let input = (!self.lf_shift & 0x01) as u8 * self.current_volume;
if self.is_dac_enabled() {
let sample = (!self.lf_shift & 0x01) as u8 * self.current_volume;
let input = if self.enabled { sample } else { 0 };
(input as f32 / 7.5) - 1.0
} else {
0.0
@@ -928,3 +1008,39 @@ impl Channel4 {
code << 4
}
}
mod obscure {
pub(super) mod square {
pub(crate) fn freq_timer_reload(freq_timer: u16, frequency: u16) -> u16 {
(freq_timer & 0x0003) | (((2048 - frequency) * 4) & 0xFFFC)
}
}
pub(super) mod nrx4 {
use super::super::{FrameSequencer, NRx4};
/// Implements the obscure behaviour when writing to NRX4 under certain
/// conditions
///
/// # Arguments
/// * `freq_hi` - mutable reference to a channel's frequency high register
/// * `fs` - reference to the APU's frame sequencer
/// * `counter` - mutable reference to a channel's internal enabled flag
/// * `prev_le` - what length_enable was before NRx4 was written with a new value
pub(crate) fn length_update(
freq: &mut impl NRx4,
fs: &FrameSequencer,
counter: &mut u16,
enabled: &mut bool,
prev_le: bool,
) {
if !fs.next_clocks_length() && !prev_le && freq.length_enable() && *counter != 0 {
*counter -= 1;
if *counter == 0 && !freq.trigger() {
*enabled = false;
}
}
}
}
}

View File

@@ -5,33 +5,12 @@ pub(crate) const SAMPLE_RATE: u32 = 48000; // Hz
const CHANNEL_COUNT: usize = 2;
const BUFFER_CAPACITY: usize = 2048 * CHANNEL_COUNT; // # of samples * the # of channels
pub struct AudioSPSC<T> {
inner: RingBuffer<T>,
}
impl<T> Default for AudioSPSC<T> {
fn default() -> Self {
Self {
inner: RingBuffer::new(BUFFER_CAPACITY),
}
}
}
impl<T> AudioSPSC<T> {
pub fn new(capacity: usize) -> Self {
Self {
inner: RingBuffer::new(capacity),
}
}
pub fn init(self) -> (SampleProducer<T>, SampleConsumer<T>) {
let (prod, cons) = self.inner.split();
(
SampleProducer { inner: prod },
SampleConsumer { inner: cons },
)
}
pub fn init<T>() -> (SampleProducer<T>, SampleConsumer<T>) {
let (prod, cons) = RingBuffer::new(BUFFER_CAPACITY);
(
SampleProducer { inner: prod },
SampleConsumer { inner: cons },
)
}
pub struct SampleProducer<T> {

View File

@@ -1,5 +1,11 @@
use bitfield::bitfield;
pub(crate) trait NRx4 {
fn trigger(&self) -> bool;
fn length_enable(&self) -> bool;
fn set_length_enable(&mut self, value: bool);
}
pub(crate) mod ch1 {
use super::bitfield;
@@ -103,7 +109,7 @@ pub(super) mod ch3 {
}
pub(super) mod ch4 {
use super::bitfield;
use super::{bitfield, NRx4};
bitfield! {
pub struct PolynomialCounter(u8);
@@ -163,7 +169,7 @@ pub(super) mod ch4 {
match byte & 0x01 {
0b00 => Self::Short,
0b01 => Self::Long,
_ => unreachable!("{:#04X} is not a valid value for CounterWidth"),
_ => unreachable!("{:#04X} is not a valid value for CounterWidth", byte),
}
}
}
@@ -177,17 +183,21 @@ pub(super) mod ch4 {
bitfield! {
pub struct Frequency(u8);
impl Debug;
_initial, _: 7;
_length_disable, _: 6;
_trigger, _: 7;
_length_enable, _set_length_enable: 6;
}
impl Frequency {
pub(crate) fn length_disable(&self) -> bool {
self._length_disable()
impl NRx4 for Frequency {
fn trigger(&self) -> bool {
self._trigger()
}
pub(crate) fn initial(&self) -> bool {
self._initial()
fn length_enable(&self) -> bool {
self._length_enable()
}
fn set_length_enable(&mut self, value: bool) {
self._set_length_enable(value);
}
}
@@ -218,23 +228,27 @@ pub(super) mod ch4 {
}
pub(super) mod common {
use super::bitfield;
use super::{bitfield, NRx4};
bitfield! {
pub struct FrequencyHigh(u8);
impl Debug;
_initial, _: 7;
_length_disable, _: 6;
_trigger, _: 7;
_length_enable, _set_length_enable: 6;
pub freq_bits, set_freq_bits: 2, 0;
}
impl FrequencyHigh {
pub(crate) fn initial(&self) -> bool {
self._initial()
impl NRx4 for FrequencyHigh {
fn trigger(&self) -> bool {
self._trigger()
}
pub(crate) fn length_disable(&self) -> bool {
self._length_disable()
fn length_enable(&self) -> bool {
self._length_enable()
}
fn set_length_enable(&mut self, value: bool) {
self._set_length_enable(value);
}
}
@@ -402,12 +416,6 @@ pub(super) mod common {
}
}
impl Default for WavePattern {
fn default() -> Self {
Self::OneEighth // Rationale: OneEighth is 0x00
}
}
impl From<WavePattern> for u8 {
fn from(pattern: WavePattern) -> Self {
pattern as Self
@@ -440,6 +448,24 @@ bitfield! {
pub ch1_right, _: 0;
}
impl SoundOutput {
pub(super) fn ch1(&self) -> (bool, bool) {
(self.ch1_left(), self.ch1_right())
}
pub(super) fn ch2(&self) -> (bool, bool) {
(self.ch2_left(), self.ch2_right())
}
pub(super) fn ch3(&self) -> (bool, bool) {
(self.ch3_left(), self.ch3_right())
}
pub(super) fn ch4(&self) -> (bool, bool) {
(self.ch4_left(), self.ch4_right())
}
}
impl Copy for SoundOutput {}
impl Clone for SoundOutput {
fn clone(&self) -> Self {
@@ -447,12 +473,6 @@ impl Clone for SoundOutput {
}
}
impl Default for SoundOutput {
fn default() -> Self {
Self(0)
}
}
impl From<u8> for SoundOutput {
fn from(byte: u8) -> Self {
Self(byte)
@@ -491,12 +511,6 @@ impl Clone for ChannelControl {
}
}
impl Default for ChannelControl {
fn default() -> Self {
Self(0)
}
}
impl From<u8> for ChannelControl {
fn from(byte: u8) -> Self {
Self(byte)
@@ -539,6 +553,15 @@ pub(super) mod fs {
};
}
pub(crate) fn next_clocks_length(&self) -> bool {
use State::*;
match self.state {
Length | LengthAndSweep => true,
Nothing | Envelope => false,
}
}
pub(crate) fn state(&self) -> State {
self.state
}
@@ -549,7 +572,7 @@ pub(super) mod fs {
}
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum State {
Length,
Nothing,

View File

@@ -7,63 +7,30 @@ use crate::ppu::{Ppu, PpuMode};
use crate::serial::Serial;
use crate::timer::Timer;
use crate::work_ram::{VariableWorkRam, WorkRam};
use std::{fs::File, io::Read};
const BOOT_ROM_SIZE: usize = 0x100;
pub(crate) const BOOT_SIZE: usize = 0x100;
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Bus {
boot: Option<[u8; BOOT_ROM_SIZE]>, // Boot ROM is 256b long
cartridge: Option<Cartridge>,
boot: Option<[u8; BOOT_SIZE]>, // Boot ROM is 256b long
pub(crate) cart: Option<Cartridge>,
pub(crate) ppu: Ppu,
work_ram: WorkRam,
var_ram: VariableWorkRam,
pub(crate) timer: Timer,
timer: Timer,
int: Interrupt,
pub(crate) apu: Apu,
high_ram: HighRam,
serial: Serial,
pub(crate) joypad: Joypad,
}
impl Default for Bus {
fn default() -> Self {
Self {
boot: None,
cartridge: None,
ppu: Default::default(),
work_ram: Default::default(),
var_ram: Default::default(),
timer: Default::default(),
int: Default::default(),
apu: Default::default(),
high_ram: Default::default(),
serial: Default::default(),
joypad: Default::default(),
}
}
pub(crate) joyp: Joypad,
}
impl Bus {
pub(crate) fn with_boot(path: &str) -> anyhow::Result<Self> {
let mut file = File::open(path)?;
let mut boot_rom = [0u8; 256];
file.read_exact(&mut boot_rom)?;
Ok(Self {
boot: Some(boot_rom),
pub(crate) fn with_boot(rom: [u8; 256]) -> Self {
Self {
boot: Some(rom),
..Default::default()
})
}
pub(crate) fn load_cartridge(&mut self, path: &str) -> std::io::Result<()> {
self.cartridge = Some(Cartridge::new(path)?);
Ok(())
}
pub(crate) fn rom_title(&self) -> Option<&str> {
self.cartridge.as_ref()?.title()
}
}
#[allow(dead_code)]
@@ -71,13 +38,19 @@ impl Bus {
self.boot.is_some()
}
#[inline]
pub(crate) fn clock(&mut self) {
self.tick(4);
}
#[inline]
fn tick(&mut self, limit: u8) {
for _ in 0..limit {
self.timer.tick();
if let Some(c) = self.cart.as_mut() {
c.tick()
}
self.ppu.tick();
self.apu.tick(self.timer.divider);
self.dma_tick()
@@ -93,7 +66,7 @@ impl Bus {
}
impl Bus {
pub fn oam_read_byte(&self, addr: u16) -> u8 {
pub(crate) fn oam_read_byte(&self, addr: u16) -> u8 {
match addr {
0x0000..=0x7FFF => {
// 16KB ROM bank 00 (ends at 0x3FFF)
@@ -104,16 +77,16 @@ impl Bus {
}
}
match self.cartridge.as_ref() {
match self.cart.as_ref() {
Some(cart) => cart.read_byte(addr),
None => panic!("Tried to read from a non-existent cartridge"),
None => 0xFF,
}
}
0x8000..=0x9FFF => self.ppu.read_byte(addr), // 8KB Video RAM
0xA000..=0xBFFF => match self.cartridge.as_ref() {
0xA000..=0xBFFF => match self.cart.as_ref() {
// 8KB External RAM
Some(cart) => cart.read_byte(addr),
None => panic!("Tried to read from a non-existent cartridge"),
None => 0xFF,
},
0xC000..=0xCFFF => self.work_ram.read_byte(addr), // 4KB Work RAM Bank 0
0xD000..=0xDFFF => self.var_ram.read_byte(addr), // 4KB Work RAM Bank 1 -> N
@@ -140,7 +113,7 @@ impl Bus {
}
}
pub fn oam_write_byte(&mut self, addr: u16, byte: u8) {
pub(crate) fn oam_write_byte(&mut self, addr: u16, byte: u8) {
self.ppu.oam.write_byte(addr, byte);
}
}
@@ -157,9 +130,9 @@ impl BusIo for Bus {
}
}
match self.cartridge.as_ref() {
match self.cart.as_ref() {
Some(cart) => cart.read_byte(addr),
None => panic!("Tried to read from a non-existent cartridge"),
None => 0xFF,
}
}
0x8000..=0x9FFF => {
@@ -169,10 +142,10 @@ impl BusIo for Bus {
_ => self.ppu.read_byte(addr),
}
}
0xA000..=0xBFFF => match self.cartridge.as_ref() {
0xA000..=0xBFFF => match self.cart.as_ref() {
// 8KB External RAM
Some(cart) => cart.read_byte(addr),
None => panic!("Tried to read from a non-existent cartridge"),
None => 0xFF,
},
0xC000..=0xCFFF => self.work_ram.read_byte(addr), // 4KB Work RAM Bank 0
0xD000..=0xDFFF => self.var_ram.read_byte(addr), // 4KB Work RAM Bank 1 -> N
@@ -219,7 +192,7 @@ impl BusIo for Bus {
// Every address here starts with 0xFF so we can just check the
// low byte to figure out which register it is
match addr & 0x00FF {
0x00 => self.joypad.p1,
0x00 => self.joyp.p1,
0x01 => self.serial.next,
0x02 => self.serial.ctrl.into(),
0x04 => (self.timer.divider >> 8) as u8,
@@ -240,8 +213,9 @@ impl BusIo for Bus {
0x49 => self.ppu.monochrome.obj_palette_1.into(),
0x4A => self.ppu.pos.window_y,
0x4B => self.ppu.pos.window_x,
0x4F => 0xFF, // CGB VRAM Bank Select
_ => {
eprintln!("Read 0xFF from unused IO register {:#06X}.", addr);
tracing::warn!("Attempted read from {:#06X} on IO", addr);
0xFF
}
}
@@ -262,9 +236,8 @@ impl BusIo for Bus {
0x0000..=0x7FFF => {
// 16KB ROM bank 00 (ends at 0x3FFF)
// and 16KB ROM Bank 01 -> NN (switchable via MB)
match self.cartridge.as_mut() {
Some(cart) => cart.write_byte(addr, byte),
None => panic!("Tried to write into non-existent cartridge"),
if let Some(cart) = self.cart.as_mut() {
cart.write_byte(addr, byte);
}
}
0x8000..=0x9FFF => {
@@ -276,9 +249,8 @@ impl BusIo for Bus {
}
0xA000..=0xBFFF => {
// 8KB External RAM
match self.cartridge.as_mut() {
Some(cart) => cart.write_byte(addr, byte),
None => panic!("Tried to write into non-existent cartridge"),
if let Some(cart) = self.cart.as_mut() {
cart.write_byte(addr, byte);
}
}
0xC000..=0xCFFF => self.work_ram.write_byte(addr, byte), // 4KB Work RAM Bank 0
@@ -318,7 +290,7 @@ impl BusIo for Bus {
// Every address here starts with 0xFF so we can just check the
// low byte to figure out which register it is
match addr & 0x00FF {
0x00 => self.joypad.update(byte),
0x00 => self.joyp.update(byte),
0x01 => self.serial.next = byte,
0x02 => self.serial.ctrl = byte.into(),
0x04 => self.timer.divider = 0x0000,
@@ -352,13 +324,15 @@ impl BusIo for Bus {
0x4A => self.ppu.pos.window_y = byte,
0x4B => self.ppu.pos.window_x = byte,
0x4D => {} // CGB Specific Register
0x4F => {} // CGB VRAM Bank Select
0x50 => {
// Disable Boot ROM
if byte != 0 {
self.boot = None;
}
}
_ => eprintln!("Wrote {:#04X} to unused IO register {:#06X}.", byte, addr),
0x70 => {} // CGB WRAM Bank Select
_ => tracing::warn!("Attempted write of {:#04X} to {:#06X} on IO", byte, addr),
};
}
0xFF80..=0xFFFE => {
@@ -380,7 +354,7 @@ impl Bus {
let lcd_stat = self.ppu.int.lcd_stat();
// Read the current interrupt information from the Joypad
let joypad = self.joypad.interrupt();
let joypad = self.joyp.interrupt();
// Read the current interrupt information from the Timer
let timer = self.timer.interrupt();
@@ -410,7 +384,7 @@ impl Bus {
self.ppu.int.set_lcd_stat(lcd_stat);
// Update the Joypad's instance of the following interrupts
self.joypad.set_interrupt(joypad);
self.joyp.set_interrupt(joypad);
// Update the Timer's instance of the following interrupts
self.timer.set_interrupt(timer);

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,13 @@
use crate::apu::Apu;
use crate::bus::{Bus, BusIo};
use crate::instruction::cycle::Cycle;
use crate::bus::{Bus, BusIo, BOOT_SIZE};
use crate::instruction::Instruction;
use crate::interrupt::{InterruptEnable, InterruptFlag};
use crate::joypad::Joypad;
use crate::ppu::Ppu;
use crate::Cycle;
use bitfield::bitfield;
use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Cpu {
pub bus: Bus,
pub(crate) bus: Bus,
reg: Registers,
flags: Flags,
ime: ImeState,
@@ -18,31 +15,16 @@ pub struct Cpu {
}
impl Cpu {
pub fn new() -> Self {
pub(crate) fn new(rom: [u8; BOOT_SIZE]) -> Self {
Self {
reg: Registers {
a: 0x01,
b: 0x00,
c: 0x13,
d: 0x00,
e: 0xD8,
h: 0x01,
l: 0x4D,
sp: 0xFFFE,
pc: 0x0100,
},
flags: 0xb0.into(),
..Default::default()
bus: Bus::with_boot(rom),
reg: Default::default(),
flags: Flags(0),
ime: ImeState::Disabled,
state: State::Execute,
}
}
pub fn boot_new(path: &str) -> anyhow::Result<Self> {
Ok(Self {
bus: Bus::with_boot(path)?,
..Default::default()
})
}
pub(crate) fn ime(&self) -> ImeState {
self.ime
}
@@ -60,10 +42,7 @@ impl Cpu {
}
pub(crate) fn is_halted(&self) -> bool {
match self.state {
State::Halt(_) => true,
_ => false,
}
matches!(self.state, State::Halt(_))
}
pub(crate) fn halt_kind(&self) -> Option<HaltKind> {
@@ -72,13 +51,11 @@ impl Cpu {
_ => None,
}
}
}
pub fn load_cartridge(&mut self, path: &str) -> std::io::Result<()> {
self.bus.load_cartridge(path)
}
pub fn rom_title(&self) -> Option<&str> {
self.bus.rom_title()
impl Default for Cpu {
fn default() -> Self {
Cpu::new(*include_bytes!("../bin/bootix_dmg.bin"))
}
}
@@ -116,7 +93,7 @@ impl Cpu {
/// routine.
///
/// Handle HALT and interrupts.
pub fn step(&mut self) -> Cycle {
pub(crate) fn step(&mut self) -> Cycle {
// Log instructions
// if self.reg.pc > 0xFF {
// let out = std::io::stdout();
@@ -131,13 +108,10 @@ impl Cpu {
use HaltKind::*;
self.bus.clock();
let elapsed = match kind {
ImeEnabled | NonePending => Cycle::new(4),
return match kind {
ImeEnabled | NonePending => 4,
SomePending => todo!("Implement HALT bug"),
};
return elapsed;
}
let opcode = self.fetch();
@@ -146,11 +120,11 @@ impl Cpu {
self.handle_ei();
// For use in Blargg's Test ROMs
if self.read_byte(0xFF02) == 0x81 {
let c = self.read_byte(0xFF01) as char;
self.write_byte(0xFF02, 0x00);
eprint!("{}", c);
}
// if self.read_byte(0xFF02) == 0x81 {
// let c = self.read_byte(0xFF01) as char;
// self.write_byte(0xFF02, 0x00);
// eprint!("{}", c);
// }
elapsed
}
@@ -167,18 +141,6 @@ impl BusIo for Cpu {
}
impl Cpu {
pub fn ppu(&mut self) -> &Ppu {
&self.bus.ppu
}
pub fn apu_mut(&mut self) -> &mut Apu {
&mut self.bus.apu
}
pub(crate) fn joypad_mut(&mut self) -> &mut Joypad {
&mut self.bus.joypad
}
fn handle_ei(&mut self) {
match self.ime {
ImeState::EiExecuted => self.ime = ImeState::Pending,
@@ -276,12 +238,6 @@ enum State {
// Stop,
}
impl Default for State {
fn default() -> Self {
Self::Execute
}
}
impl Cpu {
pub(crate) fn set_register(&mut self, register: Register, value: u8) {
use Register::*;
@@ -475,12 +431,6 @@ impl Clone for Flags {
}
}
impl Default for Flags {
fn default() -> Self {
Self(0)
}
}
impl Display for Flags {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
if self.z() {
@@ -536,8 +486,38 @@ pub(crate) enum ImeState {
Enabled,
}
impl Default for ImeState {
fn default() -> Self {
Self::Disabled
pub(crate) mod dbg {
use super::{Cpu, ImeState, RegisterPair};
pub(crate) fn flags(cpu: &Cpu) -> u8 {
cpu.flags.into()
}
pub(crate) fn af(cpu: &Cpu) -> u16 {
cpu.register_pair(RegisterPair::AF)
}
pub(crate) fn bc(cpu: &Cpu) -> u16 {
cpu.register_pair(RegisterPair::BC)
}
pub(crate) fn de(cpu: &Cpu) -> u16 {
cpu.register_pair(RegisterPair::DE)
}
pub(crate) fn hl(cpu: &Cpu) -> u16 {
cpu.register_pair(RegisterPair::HL)
}
pub(crate) fn sp(cpu: &Cpu) -> u16 {
cpu.register_pair(RegisterPair::SP)
}
pub(crate) fn pc(cpu: &Cpu) -> u16 {
cpu.register_pair(RegisterPair::PC)
}
pub(crate) fn ime(cpu: &Cpu) -> bool {
matches!(cpu.ime, ImeState::Enabled)
}
}

View File

@@ -1,73 +1,163 @@
use crate::cpu::Cpu as SM83;
use crate::instruction::cycle::Cycle;
use crate::joypad;
use crate::ppu::Ppu;
use anyhow::Result;
use crate::apu::gen::SampleProducer;
use crate::bus::BOOT_SIZE;
use crate::cartridge::Cartridge;
use crate::cpu::Cpu;
use crate::{Cycle, GB_HEIGHT, GB_WIDTH};
use clap::crate_name;
use gilrs::Gilrs;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;
use winit_input_helper::WinitInputHelper;
use thiserror::Error;
use winit::event::KeyboardInput;
use winit::event_loop::ControlFlow;
pub const SM83_CYCLE_TIME: Duration = Duration::from_nanos(1_000_000_000 / SM83_CLOCK_SPEED);
pub const CYCLES_IN_FRAME: Cycle = Cycle::new(456 * 154); // 456 Cycles times 154 scanlines
pub const CYCLES_IN_FRAME: Cycle = 456 * 154; // 456 Cycles times 154 scanlines
pub(crate) const SM83_CLOCK_SPEED: u64 = 0x40_0000; // Hz which is 4.194304Mhz
const DEFAULT_TITLE: &str = "DMG-01 Emulator";
const GAMEPAD_ENABLED: bool = true;
const DEFAULT_TITLE: &str = "Game Boy Screen";
pub fn init(boot_path: Option<&str>, rom_path: &str) -> Result<SM83> {
let mut cpu = match boot_path {
Some(path) => SM83::boot_new(path)?,
None => SM83::new(),
};
eprintln!("Initialized GB Emulator");
cpu.load_cartridge(rom_path)?;
Ok(cpu)
#[inline]
pub fn run_frame(cpu: &mut Cpu, gamepad: &mut Gilrs, key: KeyboardInput) {
run(cpu, gamepad, key, CYCLES_IN_FRAME)
}
pub fn rom_title(game_boy: &SM83) -> &str {
game_boy.rom_title().unwrap_or(DEFAULT_TITLE)
#[inline]
pub fn run(cpu: &mut Cpu, gamepad: &mut Gilrs, key: KeyboardInput, cycles: Cycle) {
let mut elapsed = 0;
if let Some(event) = gamepad.next_event() {
crate::joypad::handle_gamepad_input(&mut cpu.bus.joyp, event);
}
crate::joypad::handle_keyboard_input(&mut cpu.bus.joyp, key);
while elapsed < cycles {
elapsed += cpu.step();
}
}
pub fn run(
game_boy: &mut SM83,
gamepad: &mut Gilrs,
input: &WinitInputHelper,
target: Cycle,
) -> Cycle {
let mut elapsed = Cycle::new(0);
pub fn save_and_exit(cpu: &Cpu, control_flow: &mut ControlFlow) {
write_save(cpu);
*control_flow = ControlFlow::Exit;
}
if GAMEPAD_ENABLED {
if let Some(event) = gamepad.next_event() {
joypad::handle_gamepad_input(game_boy.joypad_mut(), event);
#[inline]
pub fn pixel_buf(cpu: &Cpu) -> &[u8; GB_HEIGHT * GB_WIDTH * 4] {
use crate::ppu::Device;
cpu.bus.ppu.frame_buf.get(Device::Host)
}
pub fn from_boot_rom<P: AsRef<Path>>(path: P) -> std::io::Result<Cpu> {
Ok(Cpu::new(read_boot(path)?))
}
pub fn read_game_rom<P: AsRef<Path>>(cpu: &mut Cpu, path: P) -> std::io::Result<()> {
cpu.bus.cart = Some(Cartridge::new(std::fs::read(path.as_ref())?));
Ok(())
}
pub fn set_audio_prod(cpu: &mut Cpu, prod: SampleProducer<f32>) {
cpu.bus.apu.prod = Some(prod);
}
pub fn rom_title(cpu: &Cpu) -> &str {
cpu.bus
.cart
.as_ref()
.and_then(|c| c.title.as_deref())
.unwrap_or(DEFAULT_TITLE)
}
pub fn write_save(cpu: &Cpu) {
match cpu.bus.cart.as_ref() {
Some(cart) => match write_save_to_file(cart) {
Ok(path) => tracing::info!("Wrote to save at {:?}", path),
Err(err @ SaveError::NotApplicable) => tracing::warn!("Unable to Save: {:?}", err),
Err(SaveError::DiffSize) => unreachable!(),
Err(SaveError::Io(err)) => tracing::error!("{:?}", err),
},
None => tracing::error!("No cartridge is currently present"),
}
}
pub fn load_save(cpu: &mut Cpu) {
match cpu.bus.cart.as_mut() {
Some(cart) => match read_save_from_file(cart) {
Ok(path) => tracing::info!("Loaded save from {:?}", path),
Err(err @ SaveError::NotApplicable) => tracing::warn!("Unable to load save: {}", err),
Err(err @ SaveError::DiffSize) => tracing::error!("Unable to load save: {}", err),
Err(SaveError::Io(err)) => match err.kind() {
std::io::ErrorKind::NotFound => tracing::warn!("Save not found"),
_ => tracing::error!("{:?}", err),
},
},
None => tracing::error!("No cartridge is currently present"),
}
}
fn write_save_to_file(cart: &Cartridge) -> Result<PathBuf, SaveError> {
match cart.title.as_ref().zip(cart.ext_ram()) {
Some((title, ram)) => {
let mut save_path = data_path().unwrap_or_else(|| PathBuf::from("."));
save_path.push(title);
save_path.set_extension("sav");
let mut file = File::create(&save_path)?;
file.write_all(ram)?;
Ok(save_path)
}
None => Err(SaveError::NotApplicable),
}
joypad::handle_keyboard_input(game_boy.joypad_mut(), input);
while elapsed < target {
elapsed += game_boy.step();
}
elapsed
}
pub fn run_frame(game_boy: &mut SM83, gamepad: &mut Gilrs, input: &WinitInputHelper) -> Cycle {
let mut elapsed = Cycle::new(0);
fn read_save_from_file(cart: &mut Cartridge) -> Result<PathBuf, SaveError> {
match cart.title.clone().zip(cart.ext_ram_mut()) {
Some((title, ext_ram)) => {
let mut save_path = data_path().unwrap_or_else(|| PathBuf::from("."));
save_path.push(title);
save_path.set_extension("sav");
if GAMEPAD_ENABLED {
if let Some(event) = gamepad.next_event() {
joypad::handle_gamepad_input(game_boy.joypad_mut(), event);
let mut file = File::open(&save_path)?;
let mut memory = Vec::new();
file.read_to_end(&mut memory)?;
if ext_ram.len() != memory.len() {
return Err(SaveError::DiffSize);
}
ext_ram.copy_from_slice(&memory);
Ok(save_path)
}
None => Err(SaveError::NotApplicable),
}
joypad::handle_keyboard_input(game_boy.joypad_mut(), input);
while elapsed < CYCLES_IN_FRAME {
elapsed += game_boy.step();
}
elapsed
}
pub fn draw(ppu: &Ppu, frame: &mut [u8]) {
ppu.copy_to_gui(frame);
fn read_boot<P: AsRef<Path>>(path: P) -> std::io::Result<[u8; BOOT_SIZE]> {
let mut buf = [0; BOOT_SIZE];
let mut file = File::open(path.as_ref())?;
file.read_exact(&mut buf)?;
Ok(buf)
}
fn data_path() -> Option<PathBuf> {
match directories_next::ProjectDirs::from("dev", "musuka", crate_name!()) {
Some(dirs) => {
let data_local = dirs.data_local_dir();
std::fs::create_dir_all(data_local).ok()?;
Some(data_local.to_path_buf())
}
None => None,
}
}
#[derive(Debug, Error)]
pub enum SaveError {
#[error("cartridge lacks title and/or external ram")]
NotApplicable,
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("save file size differs from external ram")]
DiffSize,
}

472
src/gui.rs Normal file
View File

@@ -0,0 +1,472 @@
use egui::{Context, TextureId};
use egui_wgpu_backend::{wgpu, RenderPass, ScreenDescriptor};
use egui_winit_platform::Platform;
use wgpu::{
Adapter, CommandEncoder, Device, Extent3d, FilterMode, Instance, Queue, RequestDeviceError,
Surface, SurfaceCapabilities, SurfaceConfiguration, Texture, TextureView,
TextureViewDescriptor,
};
use winit::dpi::PhysicalSize;
use winit::error::OsError;
use winit::event::{ElementState, Event, KeyboardInput};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::Window;
use crate::cpu::Cpu;
use crate::{emu, GB_HEIGHT, GB_WIDTH};
const EGUI_DIMENSIONS: (usize, usize) = (1280, 720);
const FILTER_MODE: FilterMode = FilterMode::Nearest;
const WINDOW_TITLE: &str = "DMG-01 Emulator";
const SCALE: f32 = 3.0;
pub struct Gui {
pub should_quit: bool,
pub title: String,
pub mode: EmuMode,
window: Window,
platform: Platform,
surface: Surface,
device: Device,
queue: Queue,
surface_config: SurfaceConfiguration,
render_pass: RenderPass,
texture: Texture,
texture_size: Extent3d,
texture_id: TextureId,
}
impl Gui {
pub fn new<T>(title: String, event_loop: &EventLoop<T>, cpu: &Cpu) -> Self {
use wgpu::InstanceDescriptor;
let window = build_window(event_loop).expect("build window");
let instance = Instance::new(InstanceDescriptor::default());
let surface = unsafe {
instance
.create_surface(&window)
.expect("create wgpu instance surface")
};
let adapter = request_adapter(&instance, &surface).expect("request adaptor");
let (device, queue) = request_device(&adapter).expect("request device");
let capabilities = surface.get_capabilities(&adapter);
let surface_config = surface_config(&window, capabilities);
surface.configure(&device, &surface_config);
let platform = platform(&window);
let mut render_pass = RenderPass::new(&device, surface_config.format, 1);
let texture_size = texture_size();
let texture = create_texture(&device, texture_size);
write_to_texture(&queue, &texture, emu::pixel_buf(cpu), texture_size);
let view = texture.create_view(&TextureViewDescriptor::default());
let texture_id = expose_texture_to_egui(&mut render_pass, &device, &view);
Self {
should_quit: Default::default(),
title,
mode: EmuMode::Running,
window,
platform,
surface,
device,
queue,
surface_config,
render_pass,
texture,
texture_size,
texture_id,
}
}
pub fn maybe_quit(&self, cpu: &Cpu, control_flow: &mut ControlFlow) {
if self.should_quit {
emu::save_and_exit(cpu, control_flow);
}
}
pub fn request_redraw(&self) {
self.window.request_redraw();
}
pub fn handle_event<T>(&mut self, event: &Event<T>) {
self.platform.handle_event(event);
}
pub fn update_time(&mut self, elapsed_seconds: f64) {
self.platform.update_time(elapsed_seconds);
}
pub fn resize(&mut self, size: PhysicalSize<u32>) {
// See: https://github.com/rust-windowing/winit/issues/208
if size.width > 0 && size.height > 0 {
self.surface_config.width = size.width;
self.surface_config.height = size.height;
self.surface.configure(&self.device, &self.surface_config);
}
}
pub fn paint(&mut self, cpu: &Cpu) {
use wgpu::{Color, SurfaceError};
let data = emu::pixel_buf(cpu);
write_to_texture(&self.queue, &self.texture, data, self.texture_size);
let output_frame = match self.surface.get_current_texture() {
Ok(frame) => frame,
Err(SurfaceError::Outdated) => return, // Occurs on minimization on Windows
Err(e) => {
eprintln!("Dropped frame with error: {}", e);
return;
}
};
let output_view = output_frame
.texture
.create_view(&TextureViewDescriptor::default());
// Begin to draw Egui components
self.platform.begin_frame();
self.draw_egui(cpu, &self.platform.context(), self.texture_id);
// End the UI frame. We could now handle the output and draw the UI with the backend.
let full_output = self.platform.end_frame(Some(&self.window));
let paint_jobs = self.platform.context().tessellate(full_output.shapes);
let mut encoder = create_command_encoder(&self.device);
let screen_descriptor = create_screen_descriptor(&self.window, &self.surface_config);
let tdelta = full_output.textures_delta;
// Upload all resources for the GPU.
self.render_pass
.add_textures(&self.device, &self.queue, &tdelta)
.expect("add texture ok");
self.render_pass
.update_buffers(&self.device, &self.queue, &paint_jobs, &screen_descriptor);
// Record all render passes.
self.render_pass
.execute(
&mut encoder,
&output_view,
&paint_jobs,
&screen_descriptor,
Some(Color::BLACK),
)
.expect("execute render pass");
// Submit the commands.
self.queue.submit(std::iter::once(encoder.finish()));
// Redraw egui
output_frame.present();
self.render_pass
.remove_textures(tdelta)
.expect("remove texture delta");
}
#[inline]
pub fn draw_egui(&mut self, cpu: &Cpu, ctx: &Context, texture_id: TextureId) {
use crate::{cpu, instruction, ppu};
fn selectable_text(ui: &mut egui::Ui, mut text: &str) -> egui::Response {
ui.add(egui::TextEdit::multiline(&mut text).code_editor())
}
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
ui.menu_button("File", |ui| {
if ui.button("Quit").clicked() {
self.should_quit = true;
}
});
egui::Window::new(&self.title).show(ctx, |ui| {
ui.image(
texture_id,
[GB_WIDTH as f32 * SCALE, GB_HEIGHT as f32 * SCALE],
);
});
egui::Window::new("Disassembly").show(ctx, |ui| {
selectable_text(ui, &instruction::dbg::tmp_disasm(cpu, 20));
});
egui::Window::new("Settings").show(ctx, |ui| {
egui::ComboBox::from_label("Emulation Mode")
.selected_text(format!("{:?}", self.mode))
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.mode, EmuMode::Running, "Running");
ui.selectable_value(&mut self.mode, EmuMode::Stopped, "Stopped");
ui.selectable_value(&mut self.mode, EmuMode::StepFrame, "Step Frame");
ui.selectable_value(&mut self.mode, EmuMode::Step, "Step");
})
});
egui::Window::new("GB Info").show(ctx, |ui| {
ui.horizontal(|ui| {
ui.vertical(|ui| {
ui.heading("CPU");
ui.monospace(format!("AF: {:#06X}", cpu::dbg::af(cpu)));
ui.monospace(format!("BC: {:#06X}", cpu::dbg::bc(cpu)));
ui.monospace(format!("DE: {:#06X}", cpu::dbg::de(cpu)));
ui.monospace(format!("HL: {:#06X}", cpu::dbg::hl(cpu)));
ui.add_space(10.0);
ui.monospace(format!("SP: {:#06X}", cpu::dbg::sp(cpu)));
ui.monospace(format!("PC: {:#06X}", cpu::dbg::pc(cpu)));
});
ui.vertical(|ui| {
let ppu = &cpu.bus.ppu;
ui.heading("PPU");
ui.monospace(format!("LY: {}", ppu::dbg::ly(ppu)));
ui.horizontal(|ui| {
ui.monospace(format!("SCX: {}", ppu::dbg::scx(ppu)));
ui.monospace(format!("SCY: {}", ppu::dbg::scy(ppu)));
});
ui.horizontal(|ui| {
ui.monospace(format!("WX: {}", ppu::dbg::wx(ppu)));
ui.monospace(format!("WY: {}", ppu::dbg::wy(ppu)));
});
ui.monospace(format!(
"Mode: {:?} {}",
ppu::dbg::mode(ppu),
ppu::dbg::dot(ppu)
))
});
});
ui.add_space(10.0);
let _ = ui.selectable_label(cpu::dbg::ime(cpu), "IME");
ui.horizontal(|ui| {
let irq = cpu.int_request();
ui.label("IRQ:");
let _ = ui.selectable_label(irq & 0b01 == 0x01, "VBlank");
let _ = ui.selectable_label(irq >> 1 & 0x01 == 0x01, "LCD STAT");
let _ = ui.selectable_label(irq >> 2 & 0x01 == 0x01, "Timer");
let _ = ui.selectable_label(irq >> 3 & 0x01 == 0x01, "Serial");
let _ = ui.selectable_label(irq >> 4 & 0x01 == 0x01, "Joypad");
});
ui.horizontal(|ui| {
let ie = cpu.int_enable();
// TODO: Reimplement this
// let r_len = ctx.fonts().glyph_width(egui::TextStyle::Body, 'R');
// let e_len = ctx.fonts().glyph_width(egui::TextStyle::Body, 'E');
// let q_len = ctx.fonts().glyph_width(egui::TextStyle::Body, 'Q');
ui.label("IE:");
// ui.add_space(q_len - (e_len - r_len));
let _ = ui.selectable_label(ie & 0b01 == 0x01, "VBlank");
let _ = ui.selectable_label(ie >> 1 & 0x01 == 0x01, "LCD STAT");
let _ = ui.selectable_label(ie >> 2 & 0x01 == 0x01, "Timer");
let _ = ui.selectable_label(ie >> 3 & 0x01 == 0x01, "Serial");
let _ = ui.selectable_label(ie >> 4 & 0x01 == 0x01, "Joypad");
});
ui.add_space(10.0);
ui.horizontal(|ui| {
let flags = cpu::dbg::flags(cpu);
let _ = ui.selectable_label((flags >> 7 & 0x01) == 0x01, "Zero");
let _ = ui.selectable_label((flags >> 6 & 0x01) == 0x01, "Negative");
let _ = ui.selectable_label((flags >> 5 & 0x01) == 0x01, "Half-Carry");
let _ = ui.selectable_label((flags >> 4 & 0x01) == 0x01, "Carry");
})
})
});
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmuMode {
Running,
StepFrame,
Stopped,
Step,
}
/// To avoid using an [Option<KeyboardInput>] to keep track of user input from winit,
/// we can use a "default" value. However, in order for this to work the chosen "default"
/// value must be an **unused** key, so that it is ignored by the emulator.
pub fn unused_key() -> KeyboardInput {
#![allow(deprecated)]
KeyboardInput {
scancode: Default::default(),
state: ElementState::Released,
virtual_keycode: Default::default(),
modifiers: Default::default(), // this argument is deprecated
}
}
fn build_window<T>(event_loop: &EventLoop<T>) -> Result<Window, OsError> {
use winit::window::WindowBuilder;
WindowBuilder::new()
.with_decorations(true)
.with_resizable(true)
.with_transparent(false)
.with_title(WINDOW_TITLE)
.with_inner_size(PhysicalSize {
width: EGUI_DIMENSIONS.0 as f32,
height: EGUI_DIMENSIONS.1 as f32,
})
.build(event_loop)
}
fn request_adapter(instance: &Instance, surface: &Surface) -> Option<Adapter> {
use wgpu::{PowerPreference, RequestAdapterOptions};
pollster::block_on(instance.request_adapter(&RequestAdapterOptions {
power_preference: PowerPreference::HighPerformance,
compatible_surface: Some(surface),
force_fallback_adapter: false, // TODO: What do I want to do with this?
}))
}
fn request_device(adapter: &Adapter) -> Result<(Device, Queue), RequestDeviceError> {
use wgpu::{DeviceDescriptor, Features, Limits};
pollster::block_on(adapter.request_device(
&DeviceDescriptor {
features: Features::default(),
limits: Limits::default(),
label: None,
},
None,
))
}
fn surface_config(window: &Window, capabilities: SurfaceCapabilities) -> SurfaceConfiguration {
use egui_wgpu_backend::wgpu::{PresentMode, TextureFormat, TextureUsages};
let size = window.inner_size();
SurfaceConfiguration {
usage: TextureUsages::RENDER_ATTACHMENT,
format: capabilities.formats[0],
width: size.width as u32,
height: size.height as u32,
present_mode: PresentMode::Fifo,
alpha_mode: capabilities.alpha_modes[0],
view_formats: vec![TextureFormat::Rgba8UnormSrgb],
}
}
fn platform(window: &Window) -> Platform {
use egui::FontDefinitions;
use egui_winit_platform::PlatformDescriptor;
let size = window.inner_size();
Platform::new(PlatformDescriptor {
physical_width: size.width as u32,
physical_height: size.height as u32,
scale_factor: window.scale_factor(),
font_definitions: FontDefinitions::default(),
..Default::default()
})
}
fn texture_size() -> Extent3d {
Extent3d {
width: GB_WIDTH as u32,
height: GB_HEIGHT as u32,
..Default::default()
}
}
fn create_texture(device: &Device, size: Extent3d) -> Texture {
use wgpu::{TextureDescriptor, TextureDimension, TextureFormat, TextureUsages};
device.create_texture(&TextureDescriptor {
size,
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Bgra8Unorm,
usage: TextureUsages::COPY_DST | TextureUsages::TEXTURE_BINDING,
label: Some("gb_pixel_buffer"),
view_formats: &[TextureFormat::Bgra8Unorm],
})
}
#[inline]
fn write_to_texture(
queue: &Queue,
texture: &Texture,
data: &[u8; GB_WIDTH * 4 * GB_HEIGHT],
size: Extent3d,
) {
use std::num::NonZeroU32;
use wgpu::{ImageCopyTexture, ImageDataLayout, Origin3d, TextureAspect};
queue.write_texture(
ImageCopyTexture {
texture,
mip_level: 0,
origin: Origin3d::ZERO,
aspect: TextureAspect::All,
},
data,
ImageDataLayout {
offset: 0,
bytes_per_row: NonZeroU32::new(4 * GB_WIDTH as u32),
rows_per_image: NonZeroU32::new(GB_HEIGHT as u32),
},
size,
);
}
fn expose_texture_to_egui(
render_pass: &mut RenderPass,
device: &Device,
view: &TextureView,
) -> TextureId {
render_pass.egui_texture_from_wgpu_texture(device, view, FILTER_MODE)
}
#[inline]
fn create_command_encoder(device: &Device) -> CommandEncoder {
use wgpu::CommandEncoderDescriptor;
device.create_command_encoder(&CommandEncoderDescriptor {
label: Some("encoder"),
})
}
#[inline]
fn create_screen_descriptor(window: &Window, config: &SurfaceConfiguration) -> ScreenDescriptor {
ScreenDescriptor {
physical_width: config.width,
physical_height: config.height,
scale_factor: window.scale_factor() as f32,
}
}
pub mod kbd {
use winit::event::{ElementState, KeyboardInput, VirtualKeyCode};
pub fn space_released(input: &KeyboardInput) -> bool {
let keycode = input.virtual_keycode;
matches!(input.state, ElementState::Released if keycode == Some(VirtualKeyCode::Space))
}
}

View File

@@ -17,11 +17,11 @@ impl Default for HighRam {
}
impl BusIo for HighRam {
fn write_byte(&mut self, addr: u16, byte: u8) {
self.buf[addr as usize - HIGH_RAM_START_ADDRESS] = byte;
}
fn read_byte(&self, addr: u16) -> u8 {
self.buf[addr as usize - HIGH_RAM_START_ADDRESS]
}
fn write_byte(&mut self, addr: u16, byte: u8) {
self.buf[addr as usize - HIGH_RAM_START_ADDRESS] = byte;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +1,20 @@
use bitfield::bitfield;
#[derive(Debug, Default)]
#[derive(Debug)]
pub(crate) struct Interrupt {
pub(crate) flag: InterruptFlag,
pub(crate) enable: InterruptEnable,
}
impl Default for Interrupt {
fn default() -> Self {
Self {
flag: InterruptFlag(0),
enable: InterruptEnable(0),
}
}
}
bitfield! {
pub struct InterruptEnable(u8);
impl Debug;
@@ -23,12 +32,6 @@ impl Clone for InterruptEnable {
}
}
impl Default for InterruptEnable {
fn default() -> Self {
Self(0)
}
}
impl From<u8> for InterruptEnable {
fn from(byte: u8) -> Self {
Self(byte)
@@ -58,12 +61,6 @@ impl Clone for InterruptFlag {
}
}
impl Default for InterruptFlag {
fn default() -> Self {
Self(0)
}
}
impl From<u8> for InterruptFlag {
fn from(byte: u8) -> Self {
Self(byte)

View File

@@ -1,5 +1,5 @@
use gilrs::{Button, Event as GamepadEvent, EventType as GamepadEventType};
use winit_input_helper::WinitInputHelper;
use winit::event::{ElementState, KeyboardInput, VirtualKeyCode};
#[derive(Debug)]
pub struct Joypad {
@@ -110,72 +110,39 @@ impl ButtonEvent {
}
}
pub fn handle_keyboard_input(pad: &mut Joypad, input: &WinitInputHelper) {
use winit::event::VirtualKeyCode;
// TODO: What do I have to do to get a match statement here?
#[inline]
pub(crate) fn handle_keyboard_input(pad: &mut Joypad, key: KeyboardInput) {
let state = &mut pad.ext;
let irq = &mut pad.interrupt;
if input.key_pressed(VirtualKeyCode::Down) {
state.dpad_down.update(true, irq);
}
if input.key_released(VirtualKeyCode::Down) {
state.dpad_down.update(false, irq);
}
if input.key_pressed(VirtualKeyCode::Up) {
state.dpad_up.update(true, irq);
}
if input.key_released(VirtualKeyCode::Up) {
state.dpad_up.update(false, irq);
}
if input.key_pressed(VirtualKeyCode::Left) {
state.dpad_left.update(true, irq);
}
if input.key_released(VirtualKeyCode::Left) {
state.dpad_left.update(false, irq);
}
if input.key_pressed(VirtualKeyCode::Right) {
state.dpad_right.update(true, irq);
}
if input.key_released(VirtualKeyCode::Right) {
state.dpad_right.update(false, irq);
}
if input.key_pressed(VirtualKeyCode::T) {
state.start.update(true, irq);
}
if input.key_released(VirtualKeyCode::T) {
state.start.update(false, irq);
}
if input.key_pressed(VirtualKeyCode::Y) {
state.select.update(true, irq);
}
if input.key_released(VirtualKeyCode::Y) {
state.select.update(false, irq);
}
if input.key_pressed(VirtualKeyCode::Z) {
state.south.update(true, irq);
}
if input.key_released(VirtualKeyCode::Z) {
state.south.update(false, irq);
}
if input.key_pressed(VirtualKeyCode::X) {
state.east.update(true, irq);
}
if input.key_released(VirtualKeyCode::X) {
state.east.update(false, irq);
match key.state {
ElementState::Pressed => match key.virtual_keycode {
Some(VirtualKeyCode::Down) => state.dpad_down.update(true, irq),
Some(VirtualKeyCode::Up) => state.dpad_up.update(true, irq),
Some(VirtualKeyCode::Left) => state.dpad_left.update(true, irq),
Some(VirtualKeyCode::Right) => state.dpad_right.update(true, irq),
Some(VirtualKeyCode::Return) => state.start.update(true, irq),
Some(VirtualKeyCode::RShift) => state.select.update(true, irq),
Some(VirtualKeyCode::Z) => state.south.update(true, irq),
Some(VirtualKeyCode::X) => state.east.update(true, irq),
None | Some(_) => {}
},
ElementState::Released => match key.virtual_keycode {
Some(VirtualKeyCode::Down) => state.dpad_down.update(false, irq),
Some(VirtualKeyCode::Up) => state.dpad_up.update(false, irq),
Some(VirtualKeyCode::Left) => state.dpad_left.update(false, irq),
Some(VirtualKeyCode::Right) => state.dpad_right.update(false, irq),
Some(VirtualKeyCode::Return) => state.start.update(false, irq),
Some(VirtualKeyCode::RShift) => state.select.update(false, irq),
Some(VirtualKeyCode::Z) => state.south.update(false, irq),
Some(VirtualKeyCode::X) => state.east.update(false, irq),
None | Some(_) => {}
},
}
}
pub fn handle_gamepad_input(pad: &mut Joypad, event: GamepadEvent) {
#[inline]
pub(crate) fn handle_gamepad_input(pad: &mut Joypad, event: GamepadEvent) {
use Button::*;
use GamepadEventType::*;

View File

@@ -1,5 +1,7 @@
pub use apu::gen::AudioSPSC;
pub use instruction::cycle::Cycle;
#![allow(clippy::derivable_impls)] // Will remove this if bitfield-rs allows default impls
pub use apu::gen::init as spsc_init;
pub type Cycle = u64;
pub const GB_WIDTH: usize = 160;
pub const GB_HEIGHT: usize = 144;
@@ -9,6 +11,7 @@ mod bus;
mod cartridge;
mod cpu;
pub mod emu;
pub mod gui;
mod high_ram;
mod instruction;
mod interrupt;

View File

@@ -1,156 +1,126 @@
use anyhow::{anyhow, Result};
use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg};
use gb::{AudioSPSC, Cycle, GB_HEIGHT, GB_WIDTH};
use std::path::PathBuf;
use std::time::Instant;
use clap::{arg, command, value_parser};
use gb::gui::{EmuMode, Gui};
use gb::{emu, gui};
use gilrs::Gilrs;
use pixels::{PixelsBuilder, SurfaceTexture};
use rodio::{OutputStream, Sink};
use std::time::{Duration, Instant};
use winit::dpi::LogicalSize;
use winit::event::{Event, VirtualKeyCode};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder};
use winit_input_helper::WinitInputHelper;
use tracing_subscriber::EnvFilter;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{EventLoop, EventLoopBuilder};
const WINDOW_SCALE: f64 = 2.0;
const AUDIO_ENABLED: bool = true;
fn main() -> Result<()> {
let app = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!());
let m = app
fn main() {
let m = command!()
.arg(
Arg::with_name("rom")
.value_name("ROM_FILE")
.takes_value(true)
.required(true)
.index(1)
.help("Path to the Game ROM"),
arg!(-b --boot <FILE> "path to boot ROM")
.required(false)
.value_parser(value_parser!(PathBuf)),
)
.arg(
Arg::with_name("boot")
.short("b")
.long("boot")
.value_name("FILE")
.takes_value(true)
.help("Path to Boot ROM"),
arg!([ROM_FILE] "path to game ROM")
.required(true)
.value_parser(value_parser!(PathBuf)),
)
.get_matches();
let rom_path = m
.value_of("rom")
.expect("Required value 'rom' was provided");
// Set up subscriber
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "gb=info");
}
let mut game_boy =
gb::emu::init(m.value_of("boot"), rom_path).expect("Initialize DMG-01 Emulator");
let rom_title = gb::emu::rom_title(&game_boy);
tracing_subscriber::fmt::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
let mut gamepad = Gilrs::new().expect("Initialize Controller Support");
// Initialize GUI
let event_loop = EventLoop::new();
let mut input = WinitInputHelper::new();
let window = create_window(&event_loop, rom_title)?;
let mut pixels = {
let size = window.inner_size();
let surface_texture = SurfaceTexture::new(size.width, size.height, &window);
PixelsBuilder::new(GB_WIDTH as u32, GB_HEIGHT as u32, surface_texture)
.enable_vsync(false)
.build()?
// Init CPU
let mut cpu = match m.get_one::<PathBuf>("boot") {
Some(path) => {
tracing::info!("User-provided boot ROM");
emu::from_boot_rom(path).expect("initialize emulator with custom boot rom")
}
None => {
tracing::info!("Built-in boot ROM");
Default::default()
}
};
// Load ROM if filepath was provided
if let Some(path) = m.get_one::<PathBuf>("ROM_FILE") {
tracing::info!("User-provided cartridge ROM");
emu::read_game_rom(&mut cpu, path).expect("read game rom from path");
}
emu::load_save(&mut cpu);
let rom_title = emu::rom_title(&cpu).to_string();
tracing::info!("Initialize Gamepad");
let mut gamepad = Gilrs::new().expect("Initialize Controller Support");
// Initialize Audio
let spsc: AudioSPSC<f32> = Default::default();
let (prod, cons) = spsc.init();
let (_stream, stream_handle) = OutputStream::try_default().expect("Initialized Audio");
let sink = Sink::try_new(&stream_handle)?;
sink.append(cons);
game_boy.apu_mut().attach_producer(prod);
std::thread::spawn(move || {
sink.sleep_until_end();
});
if AUDIO_ENABLED {
let (prod, cons) = gb::spsc_init();
let sink = {
let s = Sink::try_new(&stream_handle).expect("create sink from audio stream handle");
s.append(cons);
s.set_volume(0.1);
s
};
let mut start = Instant::now();
let frame_time = Duration::from_secs_f64(1.0 / 59.73); // 59.73 Hz on Host
let mut cycle_count: Cycle = Default::default();
emu::set_audio_prod(&mut cpu, prod);
tracing::info!("Spawn Audio Thread");
std::thread::spawn(move || {
sink.sleep_until_end();
});
}
// Set up state for the Immediate-mode GUI
let event_loop: EventLoop<Event<()>> = EventLoopBuilder::with_user_event().build();
let mut app = Gui::new(rom_title, &event_loop, &cpu);
let mut last_key = gui::unused_key(); // TODO: Fix this awful impl
// used for egui animations
let start_time = Instant::now();
event_loop.run(move |event, _, control_flow| {
if let Event::RedrawRequested(_) = event {
if pixels
.render()
.map_err(|e| anyhow!("pixels.render() failed: {}", e))
.is_err()
{
*control_flow = ControlFlow::Exit;
return;
app.handle_event(&event);
match event {
Event::MainEventsCleared => {
app.maybe_quit(&cpu, control_flow);
match app.mode {
EmuMode::Running => emu::run_frame(&mut cpu, &mut gamepad, last_key),
EmuMode::StepFrame if gui::kbd::space_released(&last_key) => {
emu::run_frame(&mut cpu, &mut gamepad, last_key)
}
EmuMode::Step if gui::kbd::space_released(&last_key) => {
emu::run(&mut cpu, &mut gamepad, last_key, 4);
}
_ => {}
};
// Input has been consumed, reset it
last_key = gui::unused_key();
app.request_redraw();
}
}
if input.update(&event) {
if input.key_pressed(VirtualKeyCode::Escape) || input.quit() {
*control_flow = ControlFlow::Exit;
return;
}
if let Some(size) = input.window_resized() {
pixels.resize_surface(size.width, size.height);
}
let mut diff = Instant::now() - start;
while diff.subsec_nanos() < frame_time.subsec_nanos() {
if cycle_count < gb::emu::CYCLES_IN_FRAME {
cycle_count += gb::emu::run_frame(&mut game_boy, &mut gamepad, &input);
}
diff = Instant::now() - start;
}
start = Instant::now();
if cycle_count >= gb::emu::CYCLES_IN_FRAME {
cycle_count %= gb::emu::CYCLES_IN_FRAME;
gb::emu::draw(game_boy.ppu(), pixels.get_frame());
window.request_redraw();
Event::RedrawRequested(..) => {
app.update_time(start_time.elapsed().as_secs_f64());
app.paint(&cpu);
}
Event::WindowEvent { event, .. } => match event {
WindowEvent::Resized(size) => app.resize(size),
WindowEvent::CloseRequested => emu::save_and_exit(&cpu, control_flow),
WindowEvent::KeyboardInput { input, .. } => last_key = input,
_ => {}
},
_ => {}
}
});
}
#[cfg(not(windows))]
fn create_window(event_loop: &EventLoop<()>, title: &str) -> Result<Window> {
let size = LogicalSize::new(
(GB_WIDTH as f64) * WINDOW_SCALE,
(GB_HEIGHT as f64) * WINDOW_SCALE,
);
Ok(WindowBuilder::new()
.with_title(title)
.with_inner_size(size)
.with_min_inner_size(size)
.with_resizable(true)
.with_decorations(true)
.with_transparent(false)
.build(event_loop)?)
}
#[cfg(windows)]
fn create_window(event_loop: &EventLoop<()>, title: &str) -> Result<Window> {
use winit::platform::windows::WindowBuilderExtWindows;
let size = LogicalSize::new(
(GB_WIDTH as f64) * WINDOW_SCALE,
(GB_HEIGHT as f64) * WINDOW_SCALE,
);
Ok(WindowBuilder::new()
.with_title(title)
.with_inner_size(size)
.with_min_inner_size(size)
.with_resizable(true)
.with_decorations(true)
.with_transparent(false)
.with_drag_and_drop(false)
.build(event_loop)?)
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
use crate::instruction::cycle::Cycle;
use crate::Cycle;
#[derive(Debug, Default)]
#[derive(Debug)]
pub(crate) struct DirectMemoryAccess {
pub(crate) state: DmaState,
cycle: Cycle,
@@ -8,6 +8,16 @@ pub(crate) struct DirectMemoryAccess {
pub(crate) start: DmaAddress,
}
impl Default for DirectMemoryAccess {
fn default() -> Self {
Self {
state: DmaState::Disabled,
cycle: Default::default(),
start: Default::default(),
}
}
}
impl DirectMemoryAccess {
pub(crate) fn tick(&mut self) -> Option<(u16, u16)> {
match self.state {
@@ -56,7 +66,7 @@ impl DirectMemoryAccess {
}
fn reset(&mut self) {
self.cycle = Cycle::new(0);
self.cycle = 0;
self.state = DmaState::Disabled;
self.start.0 = None;
}
@@ -69,12 +79,6 @@ pub(crate) enum DmaState {
Transferring,
}
impl Default for DmaState {
fn default() -> Self {
Self::Disabled
}
}
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct DmaAddress(Option<u16>);

View File

@@ -29,12 +29,6 @@ impl Clone for LCDStatus {
}
}
impl Default for LCDStatus {
fn default() -> Self {
Self(0x80) // bit 7 is always 1
}
}
impl From<LCDStatus> for u8 {
fn from(status: LCDStatus) -> Self {
status.0
@@ -67,12 +61,6 @@ impl From<PpuMode> for u8 {
}
}
impl Default for PpuMode {
fn default() -> Self {
Self::HBlank
}
}
bitfield! {
pub struct LCDControl(u8);
impl Debug;
@@ -93,12 +81,6 @@ impl Clone for LCDControl {
}
}
impl Default for LCDControl {
fn default() -> Self {
Self(0)
}
}
impl From<u8> for LCDControl {
fn from(byte: u8) -> Self {
Self(byte)
@@ -142,12 +124,6 @@ impl From<TileMapAddress> for u8 {
}
}
impl Default for TileMapAddress {
fn default() -> Self {
Self::X9800
}
}
#[derive(Debug, Clone, Copy)]
pub enum TileDataAddress {
X8800 = 0,
@@ -170,12 +146,6 @@ impl From<TileDataAddress> for u8 {
}
}
impl Default for TileDataAddress {
fn default() -> Self {
Self::X8800
}
}
#[derive(Debug, Clone, Copy)]
pub enum ObjectSize {
Eight = 0,
@@ -183,7 +153,7 @@ pub enum ObjectSize {
}
impl ObjectSize {
pub(crate) fn as_u8(&self) -> u8 {
pub(crate) fn size(&self) -> u8 {
use ObjectSize::*;
match self {
@@ -209,12 +179,6 @@ impl From<ObjectSize> for u8 {
}
}
impl Default for ObjectSize {
fn default() -> Self {
Self::Eight
}
}
bitfield! {
pub struct BackgroundPalette(u8);
impl Debug;
@@ -243,12 +207,6 @@ impl Clone for BackgroundPalette {
}
}
impl Default for BackgroundPalette {
fn default() -> Self {
Self(0)
}
}
impl From<u8> for BackgroundPalette {
fn from(byte: u8) -> Self {
Self(byte)
@@ -288,12 +246,6 @@ impl Clone for ObjectPalette {
}
}
impl Default for ObjectPalette {
fn default() -> Self {
Self(0)
}
}
impl From<u8> for ObjectPalette {
fn from(byte: u8) -> Self {
Self(byte)
@@ -361,24 +313,12 @@ impl From<ObjectFlags> for u8 {
}
}
impl Default for ObjectFlags {
fn default() -> Self {
Self(0)
}
}
#[derive(Debug, Clone, Copy)]
pub enum ObjectPaletteKind {
Zero = 0,
One = 1,
}
impl Default for ObjectPaletteKind {
fn default() -> Self {
Self::Zero
}
}
impl From<u8> for ObjectPaletteKind {
fn from(byte: u8) -> Self {
match byte & 0b01 {
@@ -395,7 +335,7 @@ impl From<ObjectPaletteKind> for u8 {
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum RenderPriority {
Object = 0,
BackgroundAndWindow = 1,
@@ -417,12 +357,6 @@ impl From<RenderPriority> for u8 {
}
}
impl Default for RenderPriority {
fn default() -> Self {
Self::Object
}
}
#[derive(Debug, Clone, Copy)]
pub enum GrayShade {
White = 0,
@@ -442,12 +376,6 @@ impl GrayShade {
}
}
impl Default for GrayShade {
fn default() -> Self {
Self::White
}
}
impl From<u8> for GrayShade {
fn from(byte: u8) -> Self {
match byte & 0b11 {

View File

@@ -1,6 +1,6 @@
use bitfield::bitfield;
#[derive(Debug, Default)]
#[derive(Debug)]
pub(crate) struct Serial {
/// 0xFF01 | SB - Serial Transfer Data
pub(crate) next: u8,
@@ -8,6 +8,15 @@ pub(crate) struct Serial {
pub(crate) ctrl: SerialControl,
}
impl Default for Serial {
fn default() -> Self {
Self {
next: Default::default(),
ctrl: SerialControl(0),
}
}
}
bitfield! {
pub struct SerialControl(u8);
impl Debug;
@@ -23,12 +32,6 @@ impl Clone for SerialControl {
}
}
impl Default for SerialControl {
fn default() -> Self {
Self(0)
}
}
impl From<u8> for SerialControl {
fn from(byte: u8) -> Self {
Self(byte)
@@ -47,12 +50,6 @@ enum ShiftClock {
Internal = 1,
}
impl Default for ShiftClock {
fn default() -> Self {
Self::External
}
}
impl From<u8> for ShiftClock {
fn from(byte: u8) -> Self {
match byte & 0b01 {
@@ -69,12 +66,6 @@ enum ClockSpeed {
Fast = 1,
}
impl Default for ClockSpeed {
fn default() -> Self {
Self::Normal
}
}
impl From<u8> for ClockSpeed {
fn from(byte: u8) -> Self {
match byte & 0b01 {

View File

@@ -21,19 +21,6 @@ impl Timer {
use State::*;
use TimerSpeed::*;
if let TIMAOverflow(step) | AbortedTIMAOverflow(step) = self.state {
if step < 4 {
self.state = TIMAOverflow(step + 1);
return;
}
if self.state == TIMAOverflow(step) {
self.counter = self.modulo;
self.interrupt = true;
}
self.state = Normal;
}
self.divider = self.divider.wrapping_add(1);
// Get Bit Position
@@ -55,6 +42,17 @@ impl Timer {
}
self.and_result = Some(new_result);
match self.state {
TIMAOverflow(_) | AbortedTIMAOverflow(_) => self.next(),
LoadTIMA => {
self.counter = self.modulo;
self.interrupt = true;
self.next();
}
Normal => {}
}
}
/// 0xFF05 | TIMA - Timer Counter
@@ -67,14 +65,12 @@ impl Timer {
use State::*;
match self.state {
Normal => self.counter = byte,
Normal | AbortedTIMAOverflow(_) => self.counter = byte,
TIMAOverflow(step) => {
if step < 4 {
self.counter = byte;
self.state = AbortedTIMAOverflow(step);
}
self.counter = byte;
self.state = AbortedTIMAOverflow(step);
}
AbortedTIMAOverflow(_) => self.counter = byte,
LoadTIMA => { /* Ignored */ }
}
}
@@ -94,12 +90,23 @@ impl Timer {
self.state = State::TIMAOverflow(0);
}
}
fn next(&mut self) {
use State::*;
self.state = match self.state {
Normal | LoadTIMA | AbortedTIMAOverflow(3) => Normal,
TIMAOverflow(3) => LoadTIMA,
AbortedTIMAOverflow(step) => AbortedTIMAOverflow(step + 1),
TIMAOverflow(step) => TIMAOverflow(step + 1),
}
}
}
impl Default for Timer {
fn default() -> Self {
Self {
ctrl: Default::default(),
ctrl: TimerControl(0),
counter: 0,
modulo: 0,
divider: 0,
@@ -150,12 +157,6 @@ impl Clone for TimerControl {
}
}
impl Default for TimerControl {
fn default() -> Self {
Self(0)
}
}
impl From<u8> for TimerControl {
fn from(byte: u8) -> Self {
Self(byte)
@@ -173,4 +174,5 @@ enum State {
TIMAOverflow(u8),
AbortedTIMAOverflow(u8),
Normal,
LoadTIMA,
}

View File

@@ -11,13 +11,13 @@ pub(crate) struct WorkRam {
}
impl BusIo for WorkRam {
fn write_byte(&mut self, addr: u16, byte: u8) {
self.bank[addr as usize - WORK_RAM_START_ADDRESS] = byte;
}
fn read_byte(&self, addr: u16) -> u8 {
self.bank[addr as usize - WORK_RAM_START_ADDRESS]
}
fn write_byte(&mut self, addr: u16, byte: u8) {
self.bank[addr as usize - WORK_RAM_START_ADDRESS] = byte;
}
}
impl Default for WorkRam {
@@ -42,11 +42,11 @@ impl Default for VariableWorkRam {
}
impl BusIo for VariableWorkRam {
fn write_byte(&mut self, addr: u16, byte: u8) {
self.buf[addr as usize - VARIABLE_WORK_RAM_START_ADDRESS] = byte;
}
fn read_byte(&self, addr: u16) -> u8 {
self.buf[addr as usize - VARIABLE_WORK_RAM_START_ADDRESS]
}
fn write_byte(&mut self, addr: u16, byte: u8) {
self.buf[addr as usize - VARIABLE_WORK_RAM_START_ADDRESS] = byte;
}
}