Skip to content

Commit

Permalink
align with 1tcycle apu ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
2c2c committed Aug 22, 2024
1 parent 2181a87 commit 2c9abb4
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/apu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ pub const APU = struct {
self.channel_1.shadow_frequency = self.channel_1._frequency;
self.channel_1.sweep_timer = if (self.channel_1.nr10.sweep_pace == 0) 8 else self.channel_1.nr10.sweep_pace;
self.channel_1.sweep_enable = if (self.channel_1.nr10.sweep_pace > 0 or self.channel_1.nr10.sweep_step > 0) true else false;
self.channel_1.timer = (2048 - self.channel_1.frequency);
self.channel_1.timer = (2048 - self.channel_1.frequency) * 4;
}
},
0xFF15 => {},
Expand Down Expand Up @@ -588,7 +588,7 @@ pub const APU = struct {
self.channel_2.enabled = true;
self.channel_2.length_timer = 64 - @as(u16, self.channel_2.nr21.sound_length);
self.channel_2.volume = self.channel_2.nr22.env_initial_volume;
self.channel_2.timer = (2048 - freq);
self.channel_2.timer = (2048 - freq) * 4;
self.channel_2.envelope_timer = self.channel_2.nr22.env_sweep_pace;
}
},
Expand Down Expand Up @@ -621,7 +621,7 @@ pub const APU = struct {
// ?
self.channel_3.current_sample = 0;
self.channel_3.length_timer = 256 - @as(u16, self.channel_3.nr31.initial_length_timer);
self.channel_3.timer = (2048 - freq) / 2;
self.channel_3.timer = (2048 - freq) * 2;
}
},
0xFF20 => {
Expand All @@ -640,7 +640,7 @@ pub const APU = struct {
self.channel_4.nr44 = @bitCast(byte | 0b0011_1111);
if (self.channel_4.nr44.trigger) {
self.channel_4.enabled = true;
self.channel_4.timer = self.channel_4.freq() / 4;
self.channel_4.timer = self.channel_4.freq();
self.channel_4.length_timer = 64 - @as(u16, self.channel_4.nr41.initial_length_timer);
self.channel_4.volume = self.channel_4.nr42.env_initial_volume;
self.channel_4.envelope_timer = self.channel_4.nr42.env_sweep_pace;
Expand Down Expand Up @@ -748,7 +748,7 @@ const Channel1 = struct {
// log.debug("before self.duty_pos = {}", .{self.ch1_duty_pos});
self.timer -%= 1;
if (self.timer == 0) {
self.timer = (2048 - self.frequency);
self.timer = (2048 - self.frequency) * 4;
self.duty_pos = (self.duty_pos + 1) % 8;
}
// log.debug("after self.duty_pos = {}", .{self.ch1_duty_pos});
Expand All @@ -772,7 +772,7 @@ const Channel1 = struct {
if (apu.envelope_step and self.nr12.env_sweep_pace != 0) {
self.envelope_timer -%= 1;
if (self.envelope_timer == 0) {
self.envelope_timer = self.nr12.env_sweep_pace / 4;
self.envelope_timer = self.nr12.env_sweep_pace;
if (self.nr12.env_direction and self.volume != 0xF) {
self.volume += 1;
}
Expand Down Expand Up @@ -862,7 +862,7 @@ const Channel2 = struct {
}

const freq: u16 = @as(u16, self.nr24.period_high) << 8 | @as(u16, self.nr23.period_low);
const initial_freq = (2048 - freq);
const initial_freq = (2048 - freq) * 4;
self.timer -%= 1;
if (self.timer == 0) {
self.timer = initial_freq;
Expand Down Expand Up @@ -960,7 +960,7 @@ const Channel3 = struct {
}

const freq: u16 = @as(u16, self.nr34.period_high) << 8 | @as(u16, self.nr33.period_low);
const initial_freq = (2048 - freq) / 2;
const initial_freq = (2048 - freq) * 2;
// const initial_freq = (2048 - freq);
self.timer -%= 1;
if (self.timer == 0) {
Expand Down Expand Up @@ -1072,7 +1072,7 @@ const Channel4 = struct {

self.timer -%= 1;
if (self.timer == 0) {
self.timer = self.freq() / 4;
self.timer = self.freq();

const lsfr_bit0 = self.lsfr & 1;
const lsfr_bit1 = (self.lsfr >> 1) & 1;
Expand Down

0 comments on commit 2c9abb4

Please sign in to comment.