public void WriteCONTROL_REG(int control)
{
var gate_next = control & 0x01;
// The rate counter is never reset, thus there will be a delay before the
// envelope counter starts counting up (attack) or down (release).
// Gate bit on: Start attack, decay, sustain.
if (gate==0 && gate_next!=0)
{
state = State.ATTACK;
rate_period = rate_counter_period[attack];
// Switching to attack state unlocks the zero freeze.
hold_zero = false;
}
// Gate bit off: Start release.
else if (gate!=0 && gate_next==0)
{
state = State.RELEASE;
rate_period = rate_counter_period[release];
}
gate = gate_next;
}