1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-12-19 14:18:05 +00:00

Build in initial prescaler.

This commit is contained in:
Thomas Harte
2025-11-11 12:21:49 -05:00
parent b9f5802c89
commit b0b82782ad
2 changed files with 27 additions and 6 deletions

View File

@@ -47,10 +47,12 @@ void SID::write(const Numeric::SizedInt<5> address, const uint8_t value) {
case 0x05: case 0x0c: case 0x13:
adsr().attack = value >> 4;
adsr().decay = value;
adsr().set_phase(adsr().phase);
break;
case 0x06: case 0x0d: case 0x14:
adsr().sustain = value >> 4;
adsr().release = value;
adsr().set_phase(adsr().phase);
break;
}
});
@@ -82,8 +84,6 @@ void SID::apply_samples(const std::size_t number_of_samples, Outputs::Speaker::M
voices_[1].synchronise(voices_[0]);
voices_[2].synchronise(voices_[1]);
// TODO: advance ADSR.
// TODO: inspect enabled wave types (and volumes) to complete digital path.
// TODO: apply filter.

View File

@@ -87,13 +87,27 @@ private:
Numeric::SizedInt<4> release;
// State.
Numeric::SizedInt<8> envelope;
Numeric::SizedInt<15> rate_counter;
enum class Phase {
Attack,
DecayAndHold,
Release,
} phase_ = Phase::Attack;
} phase = Phase::Release;
Numeric::SizedInt<15> rate_counter;
Numeric::SizedInt<15> rate_counter_target;
void set_phase(const Phase new_phase) {
static constexpr uint16_t rate_prescaler[] = {
9, 32, 63, 95, 149, 220, 267, 313, 392, 977, 1954, 3126, 3907, 11720, 19532, 31251
};
static_assert(sizeof(rate_prescaler) / sizeof(*rate_prescaler) == 16);
phase = new_phase;
switch(phase) {
case Phase::Attack: rate_counter_target = rate_prescaler[attack.get()]; break;
case Phase::DecayAndHold: rate_counter_target = rate_prescaler[decay.get()]; break;
case Phase::Release: rate_counter_target = rate_prescaler[release.get()]; break;
}
}
} adsr;
Numeric::SizedInt<8> control;
@@ -119,7 +133,14 @@ private:
}
}
// TODO: ADSR.
// ADSR.
++ adsr.rate_counter;
if(adsr.rate_counter == adsr.rate_counter_target) {
adsr.rate_counter = 0;
// TODO: second prescaler?
}
}
void synchronise(const Voice &prior) {