2018-01-07 01:15:55 +00:00
|
|
|
//
|
|
|
|
// KonamiSCC.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 06/01/2018.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2018 Thomas Harte. All rights reserved.
|
2018-01-07 01:15:55 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "KonamiSCC.hpp"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace Konami;
|
|
|
|
|
2022-07-16 18:41:04 +00:00
|
|
|
SCC::SCC(Concurrency::AsyncTaskQueue<false> &task_queue) :
|
2018-01-07 01:15:55 +00:00
|
|
|
task_queue_(task_queue) {}
|
|
|
|
|
2020-05-09 21:57:21 +00:00
|
|
|
bool SCC::is_zero_level() const {
|
2018-01-07 01:15:55 +00:00
|
|
|
return !(channel_enable_ & 0x1f);
|
|
|
|
}
|
|
|
|
|
2024-02-12 15:55:52 +00:00
|
|
|
template <Outputs::Speaker::Action action>
|
2024-11-30 22:21:00 +00:00
|
|
|
void SCC::apply_samples(const std::size_t number_of_samples, Outputs::Speaker::MonoSample *const target) {
|
2018-03-03 18:08:33 +00:00
|
|
|
if(is_zero_level()) {
|
2024-02-12 15:55:52 +00:00
|
|
|
Outputs::Speaker::fill<action>(target, target + number_of_samples, Outputs::Speaker::MonoSample());
|
2018-01-07 01:15:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-07 04:15:42 +00:00
|
|
|
std::size_t c = 0;
|
|
|
|
while((master_divider_&7) && c < number_of_samples) {
|
2024-02-12 15:55:52 +00:00
|
|
|
Outputs::Speaker::apply<action>(target[c], transient_output_level_);
|
2018-01-07 04:15:42 +00:00
|
|
|
master_divider_++;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(c < number_of_samples) {
|
|
|
|
for(int channel = 0; channel < 5; ++channel) {
|
|
|
|
if(channels_[channel].tone_counter) channels_[channel].tone_counter--;
|
|
|
|
else {
|
|
|
|
channels_[channel].offset = (channels_[channel].offset + 1) & 0x1f;
|
|
|
|
channels_[channel].tone_counter = channels_[channel].period;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
evaluate_output_volume();
|
|
|
|
|
|
|
|
for(int ic = 0; ic < 8 && c < number_of_samples; ++ic) {
|
2024-02-12 15:55:52 +00:00
|
|
|
Outputs::Speaker::apply<action>(target[c], transient_output_level_);
|
2018-01-07 04:15:42 +00:00
|
|
|
c++;
|
|
|
|
master_divider_++;
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 01:15:55 +00:00
|
|
|
}
|
2024-02-12 15:55:52 +00:00
|
|
|
template void SCC::apply_samples<Outputs::Speaker::Action::Mix>(std::size_t, Outputs::Speaker::MonoSample *);
|
|
|
|
template void SCC::apply_samples<Outputs::Speaker::Action::Store>(std::size_t, Outputs::Speaker::MonoSample *);
|
|
|
|
template void SCC::apply_samples<Outputs::Speaker::Action::Ignore>(std::size_t, Outputs::Speaker::MonoSample *);
|
2018-01-07 01:15:55 +00:00
|
|
|
|
2024-11-30 22:21:00 +00:00
|
|
|
void SCC::write(uint16_t address, const uint8_t value) {
|
2018-01-07 04:15:42 +00:00
|
|
|
address &= 0xff;
|
|
|
|
if(address < 0x80) ram_[address] = value;
|
|
|
|
|
2022-07-14 20:39:26 +00:00
|
|
|
task_queue_.enqueue([this, address, value] {
|
2018-01-07 04:15:42 +00:00
|
|
|
// Check for a write into waveform memory.
|
|
|
|
if(address < 0x80) {
|
|
|
|
waves_[address >> 5].samples[address & 0x1f] = value;
|
|
|
|
} else switch(address) {
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
case 0x80: case 0x82: case 0x84: case 0x86: case 0x88: {
|
|
|
|
int channel = (address - 0x80) >> 1;
|
|
|
|
channels_[channel].period = (channels_[channel].period & ~0xff) | value;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 0x81: case 0x83: case 0x85: case 0x87: case 0x89: {
|
|
|
|
int channel = (address - 0x80) >> 1;
|
|
|
|
channels_[channel].period = (channels_[channel].period & 0xff) | ((value & 0xf) << 8);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 0x8a: case 0x8b: case 0x8c: case 0x8d: case 0x8e:
|
2018-01-07 14:59:00 +00:00
|
|
|
channels_[address - 0x8a].amplitude = value & 0xf;
|
2018-01-07 04:15:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x8f:
|
|
|
|
channel_enable_ = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
evaluate_output_volume();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SCC::evaluate_output_volume() {
|
2018-03-09 18:23:18 +00:00
|
|
|
transient_output_level_ =
|
2020-05-10 03:00:39 +00:00
|
|
|
int16_t(
|
2018-03-09 18:23:18 +00:00
|
|
|
((
|
2020-05-10 03:00:39 +00:00
|
|
|
(channel_enable_ & 0x01) ? int8_t(waves_[0].samples[channels_[0].offset]) * channels_[0].amplitude : 0 +
|
|
|
|
(channel_enable_ & 0x02) ? int8_t(waves_[1].samples[channels_[1].offset]) * channels_[1].amplitude : 0 +
|
|
|
|
(channel_enable_ & 0x04) ? int8_t(waves_[2].samples[channels_[2].offset]) * channels_[2].amplitude : 0 +
|
|
|
|
(channel_enable_ & 0x08) ? int8_t(waves_[3].samples[channels_[3].offset]) * channels_[3].amplitude : 0 +
|
|
|
|
(channel_enable_ & 0x10) ? int8_t(waves_[3].samples[channels_[4].offset]) * channels_[4].amplitude : 0
|
2018-03-09 18:23:18 +00:00
|
|
|
) * master_volume_) / (255*15*5)
|
|
|
|
// Five channels, each with 8-bit samples and 4-bit volumes implies a natural range of 0 to 255*15*5.
|
2018-01-07 04:15:42 +00:00
|
|
|
);
|
2018-01-07 01:15:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 18:23:18 +00:00
|
|
|
void SCC::set_sample_volume_range(std::int16_t range) {
|
|
|
|
master_volume_ = range;
|
|
|
|
evaluate_output_volume();
|
|
|
|
}
|
|
|
|
|
2024-11-30 22:21:00 +00:00
|
|
|
uint8_t SCC::read(uint16_t address) const {
|
2018-01-07 04:15:42 +00:00
|
|
|
address &= 0xff;
|
|
|
|
if(address < 0x80) {
|
|
|
|
return ram_[address];
|
|
|
|
}
|
2018-01-07 01:15:55 +00:00
|
|
|
return 0xff;
|
|
|
|
}
|