2016-10-15 01:18:03 +00:00
|
|
|
//
|
|
|
|
// AY-3-8910.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/10/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-10-15 01:18:03 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef AY_3_8910_hpp
|
|
|
|
#define AY_3_8910_hpp
|
|
|
|
|
2017-12-19 02:39:23 +00:00
|
|
|
#include "../../Outputs/Speaker/Implementation/SampleSource.hpp"
|
2017-12-18 02:26:06 +00:00
|
|
|
#include "../../Concurrency/AsyncTaskQueue.hpp"
|
2016-10-15 01:18:03 +00:00
|
|
|
|
2021-04-26 21:39:11 +00:00
|
|
|
#include "../../Reflection/Struct.hpp"
|
|
|
|
|
2016-10-15 01:18:03 +00:00
|
|
|
namespace GI {
|
2017-08-16 02:47:17 +00:00
|
|
|
namespace AY38910 {
|
|
|
|
|
2017-08-16 13:14:56 +00:00
|
|
|
/*!
|
|
|
|
A port handler provides all input for an AY's two 8-bit ports, and may optionally receive
|
|
|
|
active notification of changes in output.
|
|
|
|
|
|
|
|
Machines with an AY without ports or with nothing wired to them need not supply a port handler.
|
|
|
|
Machines that use the AY ports as output but for which polling for changes is acceptable can
|
|
|
|
instead use AY38910.get_port_output.
|
|
|
|
*/
|
2017-08-16 02:47:17 +00:00
|
|
|
class PortHandler {
|
|
|
|
public:
|
2017-08-16 13:14:56 +00:00
|
|
|
/*!
|
|
|
|
Requests the current input on an AY port.
|
2017-11-08 03:51:06 +00:00
|
|
|
|
2017-08-16 13:14:56 +00:00
|
|
|
@param port_b @c true if the input being queried is Port B. @c false if it is Port A.
|
|
|
|
*/
|
2020-05-30 04:37:06 +00:00
|
|
|
virtual uint8_t get_port_input([[maybe_unused]] bool port_b) {
|
2017-08-16 02:47:17 +00:00
|
|
|
return 0xff;
|
|
|
|
}
|
2017-08-16 13:14:56 +00:00
|
|
|
|
|
|
|
/*!
|
2019-10-17 03:19:42 +00:00
|
|
|
Sets the current output on an AY port.
|
2017-11-08 03:51:06 +00:00
|
|
|
|
2019-10-17 03:19:42 +00:00
|
|
|
@param port_b @c true if the output being posted is Port B. @c false if it is Port A.
|
|
|
|
@param value the value now being output.
|
2017-08-16 13:14:56 +00:00
|
|
|
*/
|
2020-05-30 04:37:06 +00:00
|
|
|
virtual void set_port_output([[maybe_unused]] bool port_b, [[maybe_unused]] uint8_t value) {}
|
2017-08-16 02:47:17 +00:00
|
|
|
};
|
|
|
|
|
2017-08-16 13:14:56 +00:00
|
|
|
/*!
|
|
|
|
Names the control lines used as input to the AY, which uses CP1600 bus semantics.
|
|
|
|
*/
|
2017-08-16 02:47:17 +00:00
|
|
|
enum ControlLines {
|
|
|
|
BC1 = (1 << 0),
|
|
|
|
BC2 = (1 << 1),
|
|
|
|
BDIR = (1 << 2)
|
|
|
|
};
|
2016-10-15 01:18:03 +00:00
|
|
|
|
2019-12-19 00:28:41 +00:00
|
|
|
enum class Personality {
|
|
|
|
/// Provides 16 volume levels to envelopes.
|
|
|
|
AY38910,
|
|
|
|
/// Provides 32 volume levels to envelopes.
|
|
|
|
YM2149F
|
|
|
|
};
|
|
|
|
|
2016-10-20 02:47:44 +00:00
|
|
|
/*!
|
|
|
|
Provides emulation of an AY-3-8910 / YM2149, which is a three-channel sound chip with a
|
|
|
|
noise generator and a volume envelope generator, which also provides two bidirectional
|
|
|
|
interface ports.
|
2020-02-15 23:09:17 +00:00
|
|
|
|
|
|
|
This AY has an attached mono or stereo mixer.
|
2016-10-20 02:47:44 +00:00
|
|
|
*/
|
2020-02-16 23:28:03 +00:00
|
|
|
template <bool is_stereo> class AY38910: public ::Outputs::Speaker::SampleSource {
|
2016-10-15 01:18:03 +00:00
|
|
|
public:
|
2016-10-20 02:47:44 +00:00
|
|
|
/// Creates a new AY38910.
|
2019-12-19 00:28:41 +00:00
|
|
|
AY38910(Personality, Concurrency::DeferringAsyncTaskQueue &);
|
2016-10-15 01:18:03 +00:00
|
|
|
|
2016-10-20 02:47:44 +00:00
|
|
|
/// Sets the value the AY would read from its data lines if it were not outputting.
|
2016-10-18 23:32:15 +00:00
|
|
|
void set_data_input(uint8_t r);
|
2016-10-20 02:47:44 +00:00
|
|
|
|
|
|
|
/// Gets the value that would appear on the data lines if only the AY is outputting.
|
2016-10-18 23:32:15 +00:00
|
|
|
uint8_t get_data_output();
|
2016-10-20 02:47:44 +00:00
|
|
|
|
2017-08-01 21:01:36 +00:00
|
|
|
/// Sets the current control line state, as a bit field.
|
2016-10-18 23:32:15 +00:00
|
|
|
void set_control_lines(ControlLines control_lines);
|
2016-10-15 01:18:03 +00:00
|
|
|
|
2016-10-20 02:47:44 +00:00
|
|
|
/*!
|
|
|
|
Gets the value that would appear on the requested interface port if it were in output mode.
|
|
|
|
@parameter port_b @c true to get the value for Port B, @c false to get the value for Port A.
|
|
|
|
*/
|
2016-10-15 01:44:15 +00:00
|
|
|
uint8_t get_port_output(bool port_b);
|
|
|
|
|
2017-08-01 22:04:51 +00:00
|
|
|
/*!
|
2017-08-16 02:47:17 +00:00
|
|
|
Sets the port handler, which will receive a call every time the AY either wants to sample
|
|
|
|
input or else declare new output. As a convenience, current port output can be obtained
|
|
|
|
without installing a port handler via get_port_output.
|
2017-08-01 22:04:51 +00:00
|
|
|
*/
|
2017-08-16 02:47:17 +00:00
|
|
|
void set_port_handler(PortHandler *);
|
2017-08-01 22:04:51 +00:00
|
|
|
|
2020-02-15 23:09:17 +00:00
|
|
|
/*!
|
|
|
|
Enables or disables stereo output; if stereo output is enabled then also sets the weight of each of the AY's
|
|
|
|
channels in each of the output channels.
|
|
|
|
|
|
|
|
If a_left_ = b_left = c_left = a_right = b_right = c_right = 1.0 then you'll get output that's effectively mono.
|
|
|
|
|
|
|
|
a_left = 0.0, a_right = 1.0 will make A full volume on the right output, and silent on the left.
|
|
|
|
|
|
|
|
a_left = 0.5, a_right = 0.5 will make A half volume on both outputs.
|
|
|
|
*/
|
2020-02-16 23:28:03 +00:00
|
|
|
void set_output_mixing(float a_left, float b_left, float c_left, float a_right = 1.0, float b_right = 1.0, float c_right = 1.0);
|
2020-02-15 23:09:17 +00:00
|
|
|
|
2019-03-06 01:20:26 +00:00
|
|
|
// to satisfy ::Outputs::Speaker (included via ::Outputs::Filter.
|
2017-12-18 02:26:06 +00:00
|
|
|
void get_samples(std::size_t number_of_samples, int16_t *target);
|
2020-05-09 21:57:21 +00:00
|
|
|
bool is_zero_level() const;
|
2018-03-09 18:23:18 +00:00
|
|
|
void set_sample_volume_range(std::int16_t range);
|
2020-02-16 23:28:03 +00:00
|
|
|
static constexpr bool get_is_stereo() { return is_stereo; }
|
2016-10-20 02:47:44 +00:00
|
|
|
|
2016-10-15 01:18:03 +00:00
|
|
|
private:
|
2017-12-18 02:26:06 +00:00
|
|
|
Concurrency::DeferringAsyncTaskQueue &task_queue_;
|
|
|
|
|
2017-11-11 03:31:27 +00:00
|
|
|
int selected_register_ = 0;
|
2018-06-01 23:48:42 +00:00
|
|
|
uint8_t registers_[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
2017-11-11 03:31:27 +00:00
|
|
|
uint8_t output_registers_[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
2016-10-15 01:18:03 +00:00
|
|
|
|
2017-11-11 03:31:27 +00:00
|
|
|
int master_divider_ = 0;
|
2016-10-24 00:42:49 +00:00
|
|
|
|
2017-11-11 03:31:27 +00:00
|
|
|
int tone_periods_[3] = {0, 0, 0};
|
|
|
|
int tone_counters_[3] = {0, 0, 0};
|
|
|
|
int tone_outputs_[3] = {0, 0, 0};
|
2016-10-20 01:13:22 +00:00
|
|
|
|
2017-11-11 03:31:27 +00:00
|
|
|
int noise_period_ = 0;
|
|
|
|
int noise_counter_ = 0;
|
|
|
|
int noise_shift_register_ = 0xffff;
|
|
|
|
int noise_output_ = 0;
|
2016-10-20 01:13:22 +00:00
|
|
|
|
2017-11-11 03:31:27 +00:00
|
|
|
int envelope_period_ = 0;
|
|
|
|
int envelope_divider_ = 0;
|
2019-12-19 03:03:02 +00:00
|
|
|
int envelope_position_ = 0, envelope_position_mask_ = 0;
|
|
|
|
int envelope_shapes_[16][64];
|
2016-12-03 15:51:09 +00:00
|
|
|
int envelope_overflow_masks_[16];
|
2016-10-18 23:32:15 +00:00
|
|
|
|
2019-12-19 03:03:02 +00:00
|
|
|
int volumes_[32];
|
2016-10-24 00:42:49 +00:00
|
|
|
|
2016-10-18 23:32:15 +00:00
|
|
|
enum ControlState {
|
|
|
|
Inactive,
|
|
|
|
LatchAddress,
|
|
|
|
Read,
|
|
|
|
Write
|
2016-12-03 15:51:09 +00:00
|
|
|
} control_state_;
|
2016-10-18 23:32:15 +00:00
|
|
|
|
|
|
|
void select_register(uint8_t r);
|
|
|
|
void set_register_value(uint8_t value);
|
|
|
|
uint8_t get_register_value();
|
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
uint8_t data_input_, data_output_;
|
2016-10-22 00:05:38 +00:00
|
|
|
|
2020-02-15 23:09:17 +00:00
|
|
|
uint32_t output_volume_;
|
2017-08-02 23:45:58 +00:00
|
|
|
|
2019-03-06 01:20:26 +00:00
|
|
|
void update_bus();
|
2017-11-11 03:31:27 +00:00
|
|
|
PortHandler *port_handler_ = nullptr;
|
2019-03-06 01:20:26 +00:00
|
|
|
void set_port_output(bool port_b);
|
2020-02-15 23:09:17 +00:00
|
|
|
|
2020-02-16 23:28:03 +00:00
|
|
|
void evaluate_output_volume();
|
2020-02-15 23:09:17 +00:00
|
|
|
|
|
|
|
// Output mixing control.
|
|
|
|
uint8_t a_left_ = 255, a_right_ = 255;
|
|
|
|
uint8_t b_left_ = 255, b_right_ = 255;
|
|
|
|
uint8_t c_left_ = 255, c_right_ = 255;
|
2021-04-26 21:39:11 +00:00
|
|
|
|
|
|
|
friend struct State;
|
2016-10-15 01:18:03 +00:00
|
|
|
};
|
|
|
|
|
2021-03-21 04:14:48 +00:00
|
|
|
/*!
|
|
|
|
Provides helper code, to provide something closer to the interface exposed by many
|
|
|
|
AY-deploying machines of the era.
|
|
|
|
*/
|
|
|
|
struct Utility {
|
2021-03-27 03:19:47 +00:00
|
|
|
template <typename AY> static void write(AY &ay, bool is_data_write, uint8_t data) {
|
|
|
|
ay.set_control_lines(GI::AY38910::ControlLines(GI::AY38910::BDIR | GI::AY38910::BC2 | (is_data_write ? 0 : GI::AY38910::BC1)));
|
|
|
|
ay.set_data_input(data);
|
2021-03-21 04:14:48 +00:00
|
|
|
ay.set_control_lines(GI::AY38910::ControlLines(0));
|
|
|
|
}
|
|
|
|
|
2021-03-27 03:19:47 +00:00
|
|
|
template <typename AY> static void select_register(AY &ay, uint8_t reg) {
|
|
|
|
write(ay, false, reg);
|
|
|
|
}
|
|
|
|
|
2021-03-21 04:14:48 +00:00
|
|
|
template <typename AY> static void write_data(AY &ay, uint8_t reg) {
|
2021-03-27 03:19:47 +00:00
|
|
|
write(ay, true, reg);
|
2021-03-21 04:14:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 03:19:47 +00:00
|
|
|
template <typename AY> static uint8_t read(AY &ay) {
|
2021-03-21 04:14:48 +00:00
|
|
|
ay.set_control_lines(GI::AY38910::ControlLines(GI::AY38910::BC2 | GI::AY38910::BC1));
|
|
|
|
const uint8_t result = ay.get_data_output();
|
|
|
|
ay.set_control_lines(GI::AY38910::ControlLines(0));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2020-02-16 23:28:03 +00:00
|
|
|
|
2021-04-26 21:39:11 +00:00
|
|
|
struct State: public Reflection::StructImpl<State> {
|
|
|
|
uint8_t registers[16]{};
|
2021-04-27 00:47:28 +00:00
|
|
|
uint8_t selected_register = 0;
|
2021-04-26 21:39:11 +00:00
|
|
|
|
|
|
|
// TODO: all audio-production thread state.
|
|
|
|
|
|
|
|
State() {
|
|
|
|
if(needs_declare()) {
|
|
|
|
DeclareField(registers);
|
2021-04-27 00:47:28 +00:00
|
|
|
DeclareField(selected_register);
|
2021-04-26 21:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AY> void apply(AY &target) {
|
|
|
|
// Establish emulator-thread state
|
|
|
|
for(uint8_t c = 0; c < 16; c++) {
|
|
|
|
target.select_register(c);
|
|
|
|
target.set_register_value(registers[c]);
|
|
|
|
}
|
2021-04-27 00:47:28 +00:00
|
|
|
target.select_register(selected_register);
|
2021-04-26 21:39:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-16 02:47:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-15 01:18:03 +00:00
|
|
|
|
|
|
|
#endif /* AY_3_8910_hpp */
|