2016-06-05 14:51:07 +00:00
|
|
|
//
|
|
|
|
// 6560.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/06/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-06-05 14:51:07 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "6560.hpp"
|
|
|
|
|
2017-11-10 03:04:49 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2018-04-12 02:00:42 +00:00
|
|
|
using namespace MOS::MOS6560;
|
2016-06-05 14:51:07 +00:00
|
|
|
|
2022-07-16 18:41:04 +00:00
|
|
|
AudioGenerator::AudioGenerator(Concurrency::AsyncTaskQueue<false> &audio_queue) :
|
2017-12-18 02:26:06 +00:00
|
|
|
audio_queue_(audio_queue) {}
|
|
|
|
|
|
|
|
|
2024-11-30 03:43:54 +00:00
|
|
|
void AudioGenerator::set_volume(const uint8_t volume) {
|
2022-07-14 20:39:26 +00:00
|
|
|
audio_queue_.enqueue([this, volume]() {
|
2020-02-15 04:39:08 +00:00
|
|
|
volume_ = int16_t(volume) * range_multiplier_;
|
2024-02-12 15:55:52 +00:00
|
|
|
dc_offset_ = volume_ >> 4;
|
2016-10-07 21:10:00 +00:00
|
|
|
});
|
2016-06-13 23:30:52 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 03:43:54 +00:00
|
|
|
void AudioGenerator::set_control(const int channel, const uint8_t value) {
|
2022-07-14 20:39:26 +00:00
|
|
|
audio_queue_.enqueue([this, channel, value]() {
|
2016-12-03 15:51:09 +00:00
|
|
|
control_registers_[channel] = value;
|
2016-10-07 21:10:00 +00:00
|
|
|
});
|
2016-06-13 23:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 21:50:32 +00:00
|
|
|
// Source: VICE. Not original.
|
2024-11-30 03:43:54 +00:00
|
|
|
constexpr uint8_t noise_pattern[] = {
|
2016-06-14 21:50:32 +00:00
|
|
|
0x07, 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0x0f, 0x0f, 0x0f, 0x0e, 0x1c, 0x70,
|
|
|
|
0xe1, 0xe0, 0x71, 0xc1, 0x83, 0x83, 0x87, 0x0f, 0x1e, 0x18, 0x78, 0x78, 0x7c, 0x3e, 0x1c, 0x38,
|
|
|
|
0xf0, 0xe1, 0xe0, 0x78, 0x70, 0x38, 0x3c, 0x3e, 0x1e, 0x3c, 0x1e, 0x1c, 0x70, 0x3c, 0x38, 0x3f,
|
|
|
|
};
|
|
|
|
|
2024-11-30 03:43:54 +00:00
|
|
|
#define shift(r) shift_registers_[r] = \
|
|
|
|
(shift_registers_[r] << 1) | (((shift_registers_[r]^0x80)&control_registers_[r]) >> 7)
|
2016-12-03 15:51:09 +00:00
|
|
|
#define increment(r) shift_registers_[r] = (shift_registers_[r]+1)%8191
|
2024-11-30 03:43:54 +00:00
|
|
|
#define update(r, m, up) \
|
|
|
|
counters_[r]++; if((counters_[r] >> m) == 0x80) { up(r); counters_[r] = unsigned(control_registers_[r]&0x7f) << m; }
|
|
|
|
// Note on slightly askew test: as far as I can make out, if the value in the register is 0x7f then what's supposed to
|
|
|
|
// happen is that the 0x7f is loaded, on the next clocked cycle the Vic spots a 0x7f, pumps the output, reloads, etc. No
|
|
|
|
// increment ever occurs. It's conditional. I don't really want two conditionals if I can avoid it so I'm incrementing
|
|
|
|
// regardless and testing against 0x80. The effect should be the same: loading with 0x7f means an output update every
|
|
|
|
// cycle, loading with 0x7e means every second cycle, etc.
|
2016-06-14 00:59:01 +00:00
|
|
|
|
2024-02-12 15:55:52 +00:00
|
|
|
template <Outputs::Speaker::Action action>
|
2024-11-30 03:43:54 +00:00
|
|
|
void AudioGenerator::apply_samples(const std::size_t number_of_samples, Outputs::Speaker::MonoSample *const target) {
|
2018-04-08 22:50:42 +00:00
|
|
|
for(unsigned int c = 0; c < number_of_samples; ++c) {
|
2016-06-14 21:50:32 +00:00
|
|
|
update(0, 2, shift);
|
|
|
|
update(1, 1, shift);
|
|
|
|
update(2, 0, shift);
|
|
|
|
update(3, 1, increment);
|
|
|
|
|
2016-06-21 11:31:14 +00:00
|
|
|
// this sums the output of all three sounds channels plus a DC offset for volume;
|
|
|
|
// TODO: what's the real ratio of this stuff?
|
2024-02-12 15:55:52 +00:00
|
|
|
const int16_t sample =
|
2016-12-03 15:51:09 +00:00
|
|
|
(shift_registers_[0]&1) +
|
|
|
|
(shift_registers_[1]&1) +
|
|
|
|
(shift_registers_[2]&1) +
|
2024-02-12 15:55:52 +00:00
|
|
|
((noise_pattern[shift_registers_[3] >> 3] >> (shift_registers_[3]&7))&(control_registers_[3] >> 7)&1);
|
2016-06-13 23:30:52 +00:00
|
|
|
|
2024-02-12 15:55:52 +00:00
|
|
|
Outputs::Speaker::apply<action>(
|
|
|
|
target[c],
|
|
|
|
Outputs::Speaker::MonoSample(
|
|
|
|
sample * volume_ + dc_offset_
|
|
|
|
));
|
2016-06-14 00:59:01 +00:00
|
|
|
}
|
2016-06-13 23:30:52 +00:00
|
|
|
}
|
2024-11-30 03:43:54 +00:00
|
|
|
template void AudioGenerator::apply_samples<Outputs::Speaker::Action::Mix>(
|
|
|
|
std::size_t, Outputs::Speaker::MonoSample *);
|
|
|
|
template void AudioGenerator::apply_samples<Outputs::Speaker::Action::Store>(
|
|
|
|
std::size_t, Outputs::Speaker::MonoSample *);
|
|
|
|
template void AudioGenerator::apply_samples<Outputs::Speaker::Action::Ignore>(
|
|
|
|
std::size_t, Outputs::Speaker::MonoSample *);
|
2016-06-14 01:49:59 +00:00
|
|
|
|
2018-03-09 18:23:18 +00:00
|
|
|
void AudioGenerator::set_sample_volume_range(std::int16_t range) {
|
2020-05-10 03:00:39 +00:00
|
|
|
range_multiplier_ = int16_t(range / 64);
|
2018-03-09 18:23:18 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 01:49:59 +00:00
|
|
|
#undef shift
|
2016-06-14 21:50:32 +00:00
|
|
|
#undef increment
|
2016-06-14 01:49:59 +00:00
|
|
|
#undef update
|