Skip to content

Commit 5e6ddbd

Browse files
remove redundant next_period member of pulsewave and wave_output channel
1 parent 3a688ed commit 5e6ddbd

2 files changed

Lines changed: 26 additions & 35 deletions

File tree

src/apu.cpp

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ void gb_pulsewave_channel_t::start() {
2525
if (this->length == 0) {
2626
this->length = 64;
2727
}
28-
this->curr_period = this->next_period;
2928
this->curr_volume = this->initial_volume;
3029
this->curr_env_dir = this->next_env_dir;
3130
this->curr_env_sweep_pace = this->next_env_sweep_pace;
@@ -44,8 +43,7 @@ void gb_pulsewave_channel_t::reset() {
4443
this->right_ch_on = false;
4544
this->phase = 0;
4645
this->counter = MAX_PERIOD;
47-
this->next_period = 0;
48-
this->curr_period = 0;
46+
this->period = 0;
4947
this->initial_length = 0;
5048
this->length = 0;
5149
this->length_enabled = false;
@@ -83,9 +81,9 @@ double gb_pulsewave_channel_t::samp_freq() {
8381
* ------------- Hz
8482
* 2048 - period
8583
*/
86-
GB_assert(this->curr_period < 2048);
84+
GB_assert(this->period < 2048);
8785

88-
return 1'048'576.0 / (2048 - this->curr_period);
86+
return 1'048'576.0 / (2048 - this->period);
8987
}
9088
double gb_pulsewave_channel_t::tone_freq() {
9189
/*
@@ -111,17 +109,17 @@ void gb_pulsewave_channel_t::period_sweep_tick() {
111109
if (this->period_sweep_ticks < this->curr_period_sweep_pace) return;
112110
this->period_sweep_ticks = 0;
113111

114-
int addend = (this->curr_period / (std::pow(2, this->period_sweep_step)));
112+
int addend = (this->period / (std::pow(2, this->period_sweep_step)));
115113
if (this->period_sweep_dir) {
116114
// Shouldn't be possible unless I have crazybonesitis
117-
GB_assert(this->curr_period >= addend);
115+
GB_assert(this->period >= addend);
118116
addend *= -1;
119117
}
120-
if ((this->curr_period + addend) > 0x7FF) {
118+
if ((this->period + addend) > 0x7FF) {
121119
this->stop();
122120
return;
123121
}
124-
this->curr_period += addend;
122+
this->period += addend;
125123
}
126124

127125
void gb_pulsewave_channel_t::env_sweep_tick() {
@@ -158,8 +156,8 @@ template <typename T> void set_NRx4(T ch, uint8_t apu_div, uint8_t val) {
158156
if ((!prev_length_enabled) && !falling_edge_bit(0, apu_div, (apu_div + 1))) {
159157
ch->len_tick();
160158
}
161-
ch->next_period &= 0x00FF;
162-
ch->next_period |= (val & 0b0000'0111) << 8;
159+
ch->period &= 0x00FF;
160+
ch->period |= (val & 0b0000'0111) << 8;
163161
if ((val >> 7) & 1) { // Trigger if this bit is high
164162
ch->start();
165163
/// From https://gbdev.io/pandocs/Audio_details.html#obscure-behavior:
@@ -204,8 +202,7 @@ str gb_pulsewave_channel_t::dbg_state_str() {
204202
show_field(length, "{}");
205203
show_field(phase, "{}");
206204
show_field(counter, "{}");
207-
show_field(next_period, "{}");
208-
show_field(curr_period, "{}");
205+
show_field(period, "{}");
209206
show_field(initial_volume, "{}");
210207
show_field(next_env_dir, "{}");
211208
show_field(next_env_sweep_pace, "{}");
@@ -235,7 +232,6 @@ void gb_wave_output_channel_t::start() {
235232
if (this->length == 0) {
236233
this->length = 256;
237234
}
238-
this->curr_period = this->next_period;
239235
}
240236

241237
void gb_wave_output_channel_t::stop() {
@@ -251,11 +247,10 @@ void gb_wave_output_channel_t::reset() {
251247
this->initial_length = 0;
252248
// TODO: Investigate when length timers aren't reset
253249
// (https://gbdev.io/pandocs/Audio_Registers.html#footnote-dmg_apu_off)
254-
this->length = 0;
255-
this->next_period = 0;
256-
this->curr_period = 0;
257-
this->counter = MAX_PERIOD;
258-
this->vol = GB_CH3_VOLUME_MUTE;
250+
this->length = 0;
251+
this->period = 0;
252+
this->counter = MAX_PERIOD;
253+
this->vol = GB_CH3_VOLUME_MUTE;
259254
}
260255

261256
void gb_wave_output_channel_t::len_tick() {
@@ -287,8 +282,7 @@ str gb_wave_output_channel_t::dbg_state_str() {
287282
show_field(initial_length, "{}");
288283
show_field(length, "{}");
289284
show_field(vol, "{}");
290-
show_field(next_period, "{}");
291-
show_field(curr_period, "{}");
285+
show_field(period, "{}");
292286
show_field(phase, "{}");
293287
show_field(counter, "{}");
294288
show_field(wave_pattern, "{}");
@@ -680,8 +674,8 @@ void gb_apu_t::write_io_reg(io_reg_addr_t reg, uint8_t val) {
680674
return;
681675
}
682676
case IO_NR13: {
683-
this->ch1.next_period &= 0xFF00;
684-
this->ch1.next_period |= (val & 0x00FF);
677+
this->ch1.period &= 0xFF00;
678+
this->ch1.period |= (val & 0x00FF);
685679
return;
686680
}
687681
case IO_NR14: {
@@ -711,8 +705,8 @@ void gb_apu_t::write_io_reg(io_reg_addr_t reg, uint8_t val) {
711705
return;
712706
}
713707
case IO_NR23: {
714-
this->ch2.next_period &= 0xFF00;
715-
this->ch2.next_period |= (val & 0x00FF);
708+
this->ch2.period &= 0xFF00;
709+
this->ch2.period |= (val & 0x00FF);
716710
return;
717711
}
718712
case IO_NR24: {
@@ -735,8 +729,8 @@ void gb_apu_t::write_io_reg(io_reg_addr_t reg, uint8_t val) {
735729
return;
736730
}
737731
case IO_NR33: {
738-
this->ch3.next_period &= 0xFF00;
739-
this->ch3.next_period |= (val & 0x00FF);
732+
this->ch3.period &= 0xFF00;
733+
this->ch3.period |= (val & 0x00FF);
740734
return;
741735
}
742736
case IO_NR34: {
@@ -832,7 +826,7 @@ void gb_apu_t::tick() {
832826
gb_pulsewave_channel_t &ch = this->ch1;
833827
ch.counter--;
834828
if (ch.counter == 0) {
835-
ch.counter = MAX_PERIOD - ch.curr_period;
829+
ch.counter = MAX_PERIOD - ch.period;
836830
ch.phase++;
837831
ch.phase %= 8;
838832
}
@@ -858,7 +852,7 @@ void gb_apu_t::tick() {
858852
gb_pulsewave_channel_t &ch = this->ch2;
859853
ch.counter--;
860854
if (ch.counter == 0) {
861-
ch.counter = MAX_PERIOD - ch.curr_period;
855+
ch.counter = MAX_PERIOD - ch.period;
862856
ch.phase++;
863857
ch.phase %= 8;
864858
}
@@ -886,8 +880,7 @@ void gb_apu_t::tick() {
886880
gb_wave_output_channel_t &ch = this->ch3;
887881
ch.counter -= 2;
888882
if (ch.counter <= 0) {
889-
ch.curr_period = ch.next_period;
890-
ch.counter += (MAX_PERIOD - ch.curr_period);
883+
ch.counter += (MAX_PERIOD - ch.period);
891884
ch.phase++;
892885
ch.phase %= 32;
893886
}

src/apu.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ typedef struct gb_pulsewave_channel {
7373
uint8_t length;
7474
uint8_t phase;
7575
uint16_t counter;
76-
uint16_t next_period;
77-
uint16_t curr_period;
76+
uint16_t period;
7877

7978
// From `NRx2`, these don't take effect until a next trigger.
8079
uint8_t initial_volume;
@@ -162,8 +161,7 @@ typedef struct gb_wave_output_channel {
162161

163162
gb_ch3_volume_t vol;
164163

165-
uint16_t next_period;
166-
uint16_t curr_period;
164+
uint16_t period;
167165
uint8_t phase;
168166
int32_t counter;
169167

0 commit comments

Comments
 (0)