2016-10-15 01:18:03 +00:00
|
|
|
//
|
|
|
|
// AY-3-8910.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/10/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AY38910.hpp"
|
|
|
|
|
|
|
|
using namespace GI;
|
|
|
|
|
2016-10-20 01:13:22 +00:00
|
|
|
AY38910::AY38910() :
|
|
|
|
_selected_register(0),
|
2016-10-24 00:42:49 +00:00
|
|
|
_tone_counters{0, 0, 0}, _tone_periods{0, 0, 0}, _tone_outputs{0, 0, 0},
|
|
|
|
_noise_shift_register(0xffff), _noise_period(0), _noise_counter(0), _noise_output(0),
|
2016-10-20 02:12:51 +00:00
|
|
|
_envelope_divider(0), _envelope_period(0), _envelope_position(0),
|
2016-10-31 02:51:08 +00:00
|
|
|
_master_divider(0),
|
2016-10-20 02:12:51 +00:00
|
|
|
_output_registers{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
2016-10-15 01:18:03 +00:00
|
|
|
{
|
2016-10-16 01:04:21 +00:00
|
|
|
_output_registers[8] = _output_registers[9] = _output_registers[10] = 0;
|
2016-10-20 01:13:22 +00:00
|
|
|
|
|
|
|
// set up envelope lookup tables
|
|
|
|
for(int c = 0; c < 16; c++)
|
|
|
|
{
|
|
|
|
for(int p = 0; p < 32; p++)
|
|
|
|
{
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case 0: case 1: case 2: case 3: case 9:
|
|
|
|
_envelope_shapes[c][p] = (p < 16) ? (p^0xf) : 0;
|
|
|
|
_envelope_overflow_masks[c] = 0x1f;
|
|
|
|
break;
|
|
|
|
case 4: case 5: case 6: case 7: case 15:
|
|
|
|
_envelope_shapes[c][p] = (p < 16) ? p : 0;
|
|
|
|
_envelope_overflow_masks[c] = 0x1f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
_envelope_shapes[c][p] = (p & 0xf) ^ 0xf;
|
|
|
|
_envelope_overflow_masks[c] = 0x00;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
_envelope_shapes[c][p] = (p & 0xf);
|
|
|
|
_envelope_overflow_masks[c] = 0x00;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 10:
|
|
|
|
_envelope_shapes[c][p] = (p & 0xf) ^ ((p < 16) ? 0xf : 0x0);
|
|
|
|
_envelope_overflow_masks[c] = 0x00;
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
_envelope_shapes[c][p] = (p & 0xf) ^ ((p < 16) ? 0x0 : 0xf);
|
|
|
|
_envelope_overflow_masks[c] = 0x00;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
_envelope_shapes[c][p] = (p < 16) ? (p^0xf) : 0xf;
|
|
|
|
_envelope_overflow_masks[c] = 0x1f;
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
_envelope_shapes[c][p] = (p < 16) ? p : 0xf;
|
|
|
|
_envelope_overflow_masks[c] = 0x1f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-20 03:07:51 +00:00
|
|
|
|
|
|
|
// set up volume lookup table
|
|
|
|
float max_volume = 8192;
|
|
|
|
float root_two = sqrtf(2.0f);
|
|
|
|
for(int v = 0; v < 16; v++)
|
|
|
|
{
|
|
|
|
_volumes[v] = (int)(max_volume / powf(root_two, (float)(v ^ 0xf)));
|
|
|
|
}
|
|
|
|
_volumes[0] = 0;
|
2016-10-15 01:18:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 21:45:39 +00:00
|
|
|
void AY38910::set_clock_rate(double clock_rate)
|
|
|
|
{
|
|
|
|
set_input_rate((float)clock_rate);
|
|
|
|
}
|
|
|
|
|
2016-10-15 01:18:03 +00:00
|
|
|
void AY38910::get_samples(unsigned int number_of_samples, int16_t *target)
|
|
|
|
{
|
2016-10-24 00:32:48 +00:00
|
|
|
int c = 0;
|
|
|
|
while((_master_divider&15) && c < number_of_samples)
|
|
|
|
{
|
|
|
|
target[c] = _output_volume;
|
|
|
|
_master_divider++;
|
|
|
|
c++;
|
|
|
|
}
|
2016-10-20 01:43:18 +00:00
|
|
|
|
2016-10-24 00:32:48 +00:00
|
|
|
while(c < number_of_samples)
|
2016-10-22 00:05:38 +00:00
|
|
|
{
|
2016-10-24 00:32:48 +00:00
|
|
|
#define step_channel(c) \
|
|
|
|
if(_tone_counters[c]) _tone_counters[c]--;\
|
|
|
|
else\
|
|
|
|
{\
|
|
|
|
_tone_outputs[c] ^= 1;\
|
2016-10-24 00:42:49 +00:00
|
|
|
_tone_counters[c] = _tone_periods[c];\
|
2016-10-24 00:32:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 01:43:18 +00:00
|
|
|
// update the tone channels
|
|
|
|
step_channel(0);
|
|
|
|
step_channel(1);
|
|
|
|
step_channel(2);
|
|
|
|
|
2016-10-22 00:05:38 +00:00
|
|
|
#undef step_channel
|
|
|
|
|
2016-10-20 01:51:35 +00:00
|
|
|
// ... the noise generator. This recomputes the new bit repeatedly but harmlessly, only shifting
|
|
|
|
// it into the official 17 upon divider underflow.
|
2016-10-24 00:42:49 +00:00
|
|
|
if(_noise_counter) _noise_counter--;
|
2016-10-22 00:05:38 +00:00
|
|
|
else
|
|
|
|
{
|
2016-10-24 00:42:49 +00:00
|
|
|
_noise_counter = _noise_period;
|
2016-10-22 00:05:38 +00:00
|
|
|
_noise_output ^= _noise_shift_register&1;
|
|
|
|
_noise_shift_register |= ((_noise_shift_register ^ (_noise_shift_register >> 3))&1) << 17;
|
|
|
|
_noise_shift_register >>= 1;
|
|
|
|
}
|
2016-10-20 01:13:22 +00:00
|
|
|
|
2016-10-20 01:51:35 +00:00
|
|
|
// ... and the envelope generator. Table based for pattern lookup, with a 'refill' step — a way of
|
|
|
|
// implementing non-repeating patterns by locking them to table position 0x1f.
|
2016-10-22 00:05:38 +00:00
|
|
|
if(_envelope_divider) _envelope_divider--;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_envelope_divider = _envelope_period;
|
|
|
|
_envelope_position ++;
|
|
|
|
if(_envelope_position == 32) _envelope_position = _envelope_overflow_masks[_output_registers[13]];
|
|
|
|
}
|
2016-10-20 01:13:22 +00:00
|
|
|
|
2016-10-22 00:05:38 +00:00
|
|
|
evaluate_output_volume();
|
2016-10-20 01:43:18 +00:00
|
|
|
|
2016-10-24 00:32:48 +00:00
|
|
|
for(int ic = 0; ic < 16 && c < number_of_samples; ic++)
|
2016-10-22 00:05:38 +00:00
|
|
|
{
|
2016-10-24 00:32:48 +00:00
|
|
|
target[c] = _output_volume;
|
2016-10-22 00:05:38 +00:00
|
|
|
c++;
|
2016-10-24 00:32:48 +00:00
|
|
|
_master_divider++;
|
2016-10-22 00:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-31 02:51:08 +00:00
|
|
|
|
|
|
|
_master_divider &= 15;
|
2016-10-22 00:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AY38910::evaluate_output_volume()
|
|
|
|
{
|
|
|
|
int envelope_volume = _envelope_shapes[_output_registers[13]][_envelope_position];
|
|
|
|
|
|
|
|
// The output level for a channel is:
|
|
|
|
// 1 if neither tone nor noise is enabled;
|
|
|
|
// 0 if either tone or noise is enabled and its value is low.
|
|
|
|
// (which is implemented here with reverse logic, assuming _channel_output and _noise_output are already inverted)
|
2016-10-20 01:43:18 +00:00
|
|
|
#define level(c, tb, nb) \
|
2016-10-24 00:32:48 +00:00
|
|
|
(((((_output_registers[7] >> tb)&1)^1) & _tone_outputs[c]) | ((((_output_registers[7] >> nb)&1)^1) & _noise_output)) ^ 1
|
2016-10-19 02:20:12 +00:00
|
|
|
|
2016-10-22 00:05:38 +00:00
|
|
|
int channel_levels[3] = {
|
|
|
|
level(0, 0, 3),
|
|
|
|
level(1, 1, 4),
|
|
|
|
level(2, 2, 5),
|
|
|
|
};
|
2016-10-20 01:43:18 +00:00
|
|
|
#undef level
|
|
|
|
|
2016-10-20 01:51:35 +00:00
|
|
|
// Channel volume is a simple selection: if the bit at 0x10 is set, use the envelope volume; otherwise use the lower four bits
|
2016-10-20 01:43:18 +00:00
|
|
|
#define channel_volume(c) \
|
2016-10-20 02:14:05 +00:00
|
|
|
((_output_registers[c] >> 4)&1) * envelope_volume + (((_output_registers[c] >> 4)&1)^1) * (_output_registers[c]&0xf)
|
2016-10-20 01:13:22 +00:00
|
|
|
|
2016-10-22 00:05:38 +00:00
|
|
|
int volumes[3] = {
|
|
|
|
channel_volume(8),
|
|
|
|
channel_volume(9),
|
|
|
|
channel_volume(10)
|
|
|
|
};
|
2016-10-20 01:43:18 +00:00
|
|
|
#undef channel_volume
|
2016-10-19 02:20:12 +00:00
|
|
|
|
2016-10-22 00:05:38 +00:00
|
|
|
// Mix additively.
|
|
|
|
_output_volume = (int16_t)(
|
|
|
|
_volumes[volumes[0]] * channel_levels[0] +
|
|
|
|
_volumes[volumes[1]] * channel_levels[1] +
|
|
|
|
_volumes[volumes[2]] * channel_levels[2]
|
|
|
|
);
|
2016-10-15 01:18:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 01:35:15 +00:00
|
|
|
void AY38910::select_register(uint8_t r)
|
|
|
|
{
|
2016-10-15 01:44:15 +00:00
|
|
|
_selected_register = r & 0xf;
|
2016-10-15 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AY38910::set_register_value(uint8_t value)
|
|
|
|
{
|
2016-10-15 01:44:15 +00:00
|
|
|
_registers[_selected_register] = value;
|
2016-10-19 02:20:12 +00:00
|
|
|
if(_selected_register < 14)
|
2016-10-15 21:45:39 +00:00
|
|
|
{
|
|
|
|
int selected_register = _selected_register;
|
|
|
|
enqueue([=] () {
|
2016-10-20 01:13:22 +00:00
|
|
|
uint8_t masked_value = value;
|
2016-10-15 21:45:39 +00:00
|
|
|
switch(selected_register)
|
|
|
|
{
|
|
|
|
case 0: case 2: case 4:
|
|
|
|
case 1: case 3: case 5:
|
2016-10-22 02:12:44 +00:00
|
|
|
{
|
|
|
|
int channel = selected_register >> 1;
|
2016-10-22 02:16:44 +00:00
|
|
|
|
2016-10-22 02:12:44 +00:00
|
|
|
if(selected_register & 1)
|
2016-10-24 00:42:49 +00:00
|
|
|
_tone_periods[channel] = (_tone_periods[channel] & 0xff) | (uint16_t)((value&0xf) << 8);
|
2016-10-22 02:12:44 +00:00
|
|
|
else
|
2016-10-24 00:42:49 +00:00
|
|
|
_tone_periods[channel] = (_tone_periods[channel] & ~0xff) | value;
|
|
|
|
_tone_counters[channel] = _tone_periods[channel];
|
2016-10-22 02:12:44 +00:00
|
|
|
}
|
2016-10-15 21:45:39 +00:00
|
|
|
break;
|
|
|
|
|
2016-10-22 00:07:14 +00:00
|
|
|
case 6:
|
2016-10-24 00:42:49 +00:00
|
|
|
_noise_period = value & 0x1f;
|
|
|
|
_noise_counter = _noise_period;
|
2016-10-22 00:07:14 +00:00
|
|
|
break;
|
|
|
|
|
2016-10-15 21:45:39 +00:00
|
|
|
case 11:
|
|
|
|
_envelope_period = (_envelope_period & ~0xff) | value;
|
2016-10-22 00:05:38 +00:00
|
|
|
_envelope_divider = _envelope_period;
|
2016-10-15 21:45:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 12:
|
2016-10-20 01:13:22 +00:00
|
|
|
_envelope_period = (_envelope_period & 0xff) | (int)(value << 8);
|
2016-10-22 00:05:38 +00:00
|
|
|
_envelope_divider = _envelope_period;
|
2016-10-20 01:13:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 13:
|
|
|
|
masked_value &= 0xf;
|
|
|
|
_envelope_position = 0;
|
2016-10-15 21:45:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-10-20 01:13:22 +00:00
|
|
|
_output_registers[selected_register] = masked_value;
|
2016-10-22 00:05:38 +00:00
|
|
|
evaluate_output_volume();
|
2016-10-15 21:45:39 +00:00
|
|
|
});
|
|
|
|
}
|
2016-10-15 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AY38910::get_register_value()
|
|
|
|
{
|
2016-10-15 01:44:15 +00:00
|
|
|
return _registers[_selected_register];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AY38910::get_port_output(bool port_b)
|
|
|
|
{
|
|
|
|
return _registers[port_b ? 15 : 14];
|
2016-10-15 01:35:15 +00:00
|
|
|
}
|
2016-10-18 23:32:15 +00:00
|
|
|
|
|
|
|
void AY38910::set_data_input(uint8_t r)
|
|
|
|
{
|
|
|
|
_data_input = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AY38910::get_data_output()
|
|
|
|
{
|
|
|
|
return _data_output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AY38910::set_control_lines(ControlLines control_lines)
|
|
|
|
{
|
|
|
|
ControlState new_state;
|
|
|
|
switch((int)control_lines)
|
|
|
|
{
|
2016-10-20 01:43:18 +00:00
|
|
|
default: new_state = Inactive; break;
|
2016-10-18 23:32:15 +00:00
|
|
|
|
|
|
|
case (int)(BCDIR | BC2 | BC1):
|
|
|
|
case BCDIR:
|
2016-10-20 01:43:18 +00:00
|
|
|
case BC1: new_state = LatchAddress; break;
|
2016-10-18 23:32:15 +00:00
|
|
|
|
|
|
|
case (int)(BC2 | BC1): new_state = Read; break;
|
|
|
|
case (int)(BCDIR | BC2): new_state = Write; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(new_state != _control_state)
|
|
|
|
{
|
|
|
|
_control_state = new_state;
|
|
|
|
switch(new_state)
|
|
|
|
{
|
|
|
|
default: break;
|
2016-10-20 01:43:18 +00:00
|
|
|
case LatchAddress: select_register(_data_input); break;
|
|
|
|
case Write: set_register_value(_data_input); break;
|
|
|
|
case Read: _data_output = get_register_value(); break;
|
2016-10-18 23:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|