Skip to content

Commit

Permalink
Derive serde traits for all gameboy state types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanabenki committed Aug 1, 2023
1 parent f144d68 commit 507b058
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 77 deletions.
48 changes: 38 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ rust-version = "1.66"
[dependencies]
pixels = "0.13.0"
winit = "0.28.6"
flagset = "0.4.3"
flagset = { version = "0.4.3", features = ["serde"] }
enum_dispatch = "0.3.12"
color-eyre = "0.6.2"
clap = { version = "4.3.19", features = ["derive"] }
cpal = "0.15.2"
ringbuf = "0.3.3"
blip_buf = "0.1.4"
serde = { version = "1.0.180", features = ["derive"] }
serde-big-array = "0.5.1"
38 changes: 20 additions & 18 deletions src/gameboy/apu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::mem;

#[derive(Debug, Default)]
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Default)]
struct ChannelToggles {
channel_1: bool,
channel_2: bool,
Expand Down Expand Up @@ -30,7 +32,7 @@ impl ChannelToggles {
}
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct SoundEnable {
channels: ChannelToggles,
all: bool,
Expand All @@ -43,7 +45,7 @@ impl SoundEnable {
}
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct SoundPanning {
left: ChannelToggles,
right: ChannelToggles,
Expand All @@ -60,7 +62,7 @@ impl SoundPanning {
}
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct MasterVolVinPan {
left_volume: u8,
right_volume: u8,
Expand All @@ -83,14 +85,14 @@ impl MasterVolVinPan {
}
}

#[derive(Debug, Copy, Clone, Default)]
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Default)]
enum SweepOp {
#[default]
Increase = 0,
Decrease = 1,
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct Sweep {
pace: u8,
op: SweepOp,
Expand All @@ -112,7 +114,7 @@ impl Sweep {
}
}

#[derive(Debug, Clone, Copy, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default)]
enum WaveDuty {
#[default]
W0 = 0,
Expand All @@ -136,7 +138,7 @@ impl WaveDuty {
}
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct WaveDutyTimerLen {
wave_duty: WaveDuty,
init_len_timer: u8,
Expand All @@ -161,14 +163,14 @@ impl WaveDutyTimerLen {
}
}

#[derive(Debug, Clone, Copy, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default)]
enum EnvelopeDir {
#[default]
Decrease = 0,
Increase = 1,
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct VolumeEnvelope {
initial: u8,
direction: EnvelopeDir,
Expand All @@ -190,7 +192,7 @@ impl VolumeEnvelope {
}
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct WavelenCtrl {
trigger: bool,
sound_len_enable: bool,
Expand All @@ -209,7 +211,7 @@ impl WavelenCtrl {
}
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct Channel1 {
sweep: Sweep,
wave_duty_timer_len: WaveDutyTimerLen,
Expand All @@ -218,15 +220,15 @@ struct Channel1 {
wavelen_high_ctrl: WavelenCtrl,
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct Channel2 {
wave_duty_timer_len: WaveDutyTimerLen,
vol_env: VolumeEnvelope,
wavelen_low: u8,
wavelen_high_ctrl: WavelenCtrl,
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct Channel3 {
enable: bool,
len_timer: u8,
Expand All @@ -236,14 +238,14 @@ struct Channel3 {
wave_pattern: [u8; 16],
}

#[derive(Debug, Default, Clone, Copy)]
#[derive(Serialize, Deserialize, Debug, Default, Clone, Copy)]
enum LfsrWidth {
#[default]
B15 = 0,
B7 = 1,
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct FreqRand {
clock_shift: u8,
lfsr_width: LfsrWidth,
Expand All @@ -265,7 +267,7 @@ impl FreqRand {
}
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
struct Channel4 {
len_timer: u8,
vol_env: VolumeEnvelope,
Expand All @@ -274,7 +276,7 @@ struct Channel4 {
sound_len_enable: bool,
}

#[derive(Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Apu {
left_samples: [i16; 6],
right_samples: [i16; 6],
Expand Down
6 changes: 4 additions & 2 deletions src/gameboy/cartridge/mbc1.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use serde::{Deserialize, Serialize};

use super::{MapperOps, RAM_BANK_SIZE, ROM_BANK_SIZE};

#[derive(Debug)]
#[derive(Serialize, Deserialize, Debug)]
enum BankMode {
Rom,
Ram,
}

#[derive(Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Mbc1 {
has_ram: bool,
has_battery: bool,
Expand Down
8 changes: 4 additions & 4 deletions src/gameboy/cartridge/mbc2.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use serde::{Deserialize, Serialize};

use super::{
MapperOps, HIGH_BANK_END, HIGH_BANK_START, LOW_BANK_END, LOW_BANK_START, ROM_BANK_SIZE,
};

#[derive(Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Mbc2 {
has_battery: bool,
ram_enabled: bool,
_rom_bank_count: u16,
rom_bank: u8,
}

impl Mbc2 {
pub const RAM_SIZE: usize = 0x200;
const RAM_ADDR_MASK: u16 = 0x1FF;

pub const fn new(rom_bank_count: u16, has_battery: bool) -> Self {
pub const fn new(has_battery: bool) -> Self {
Self {
has_battery,
ram_enabled: false,
_rom_bank_count: rom_bank_count,
rom_bank: 1,
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/gameboy/cartridge/mbc3.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use serde::{Deserialize, Serialize};

use crate::gameboy::Gameboy;

use super::{
MapperOps, HIGH_BANK_END, HIGH_BANK_START, LOW_BANK_END, LOW_BANK_START, RAM_BANK_SIZE,
ROM_BANK_SIZE,
};

#[derive(Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Mbc3 {
has_rtc: bool,
has_ram: bool,
Expand All @@ -21,7 +23,7 @@ pub struct Mbc3 {
}

// TODO: Fetch current time and save/restore once save are implemented
#[derive(Default, Debug, Clone, Copy)]
#[derive(Serialize, Deserialize, Default, Debug, Clone, Copy)]
struct RtcRegisters {
seconds: u8,
minutes: u8,
Expand Down
4 changes: 3 additions & 1 deletion src/gameboy/cartridge/mbc5.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use serde::{Deserialize, Serialize};

use super::{
MapperOps, HIGH_BANK_END, HIGH_BANK_START, LOW_BANK_END, LOW_BANK_START, RAM_BANK_SIZE,
ROM_BANK_SIZE,
};

// TODO: Rumble with controller.
#[derive(Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Mbc5 {
_has_rumble: bool,
has_ram: bool,
Expand Down
Loading

0 comments on commit 507b058

Please sign in to comment.