1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-10-31 18:04:37 +00:00
CLK/Components/AY38910/AY38910.hpp

150 lines
4.0 KiB
C++
Raw Normal View History

//
// AY-3-8910.hpp
// Clock Signal
//
// Created by Thomas Harte on 14/10/2016.
// Copyright 2016 Thomas Harte. All rights reserved.
//
#ifndef AY_3_8910_hpp
#define AY_3_8910_hpp
#include "../../Outputs/Speaker/Implementation/SampleSource.hpp"
#include "../../Concurrency/AsyncTaskQueue.hpp"
namespace GI {
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.
*/
class PortHandler {
public:
2017-08-16 13:14:56 +00:00
/*!
Requests the current input on an AY port.
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.
*/
virtual uint8_t get_port_input(bool port_b) {
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.
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
*/
virtual void set_port_output(bool port_b, uint8_t value) {}
};
2017-08-16 13:14:56 +00:00
/*!
Names the control lines used as input to the AY, which uses CP1600 bus semantics.
*/
enum ControlLines {
BC1 = (1 << 0),
BC2 = (1 << 1),
BDIR = (1 << 2)
};
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.
*/
class AY38910: public ::Outputs::Speaker::SampleSource {
public:
2016-10-20 02:47:44 +00:00
/// Creates a new AY38910.
AY38910(Personality, Concurrency::DeferringAsyncTaskQueue &);
2016-10-20 02:47:44 +00:00
/// Sets the value the AY would read from its data lines if it were not outputting.
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.
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.
void set_control_lines(ControlLines control_lines);
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);
/*!
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.
*/
void set_port_handler(PortHandler *);
// to satisfy ::Outputs::Speaker (included via ::Outputs::Filter.
void get_samples(std::size_t number_of_samples, int16_t *target);
bool is_zero_level();
void set_sample_volume_range(std::int16_t range);
2016-10-20 02:47:44 +00:00
private:
Concurrency::DeferringAsyncTaskQueue &task_queue_;
int selected_register_ = 0;
uint8_t registers_[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t output_registers_[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int master_divider_ = 0;
2016-10-24 00:42:49 +00:00
int tone_periods_[3] = {0, 0, 0};
int tone_counters_[3] = {0, 0, 0};
int tone_outputs_[3] = {0, 0, 0};
int noise_period_ = 0;
int noise_counter_ = 0;
int noise_shift_register_ = 0xffff;
int noise_output_ = 0;
int envelope_period_ = 0;
int envelope_divider_ = 0;
int envelope_position_ = 0, envelope_position_mask_ = 0;
int envelope_shapes_[16][64];
int envelope_overflow_masks_[16];
int volumes_[32];
2016-10-24 00:42:49 +00:00
enum ControlState {
Inactive,
LatchAddress,
Read,
Write
} control_state_;
void select_register(uint8_t r);
void set_register_value(uint8_t value);
uint8_t get_register_value();
uint8_t data_input_, data_output_;
int16_t output_volume_;
void evaluate_output_volume();
void update_bus();
PortHandler *port_handler_ = nullptr;
void set_port_output(bool port_b);
};
}
}
#endif /* AY_3_8910_hpp */