2016-06-05 14:51:07 +00:00
|
|
|
//
|
|
|
|
// 6560.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/06/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "6560.hpp"
|
|
|
|
|
|
|
|
using namespace MOS;
|
|
|
|
|
2016-08-06 18:33:24 +00:00
|
|
|
Speaker::Speaker() :
|
2016-06-14 01:49:59 +00:00
|
|
|
_volume(0),
|
|
|
|
_control_registers{0, 0, 0, 0},
|
|
|
|
_shift_registers{0, 0, 0, 0},
|
2016-06-14 22:19:06 +00:00
|
|
|
_counters{2, 1, 0, 0} // create a slight phase offset for the three channels
|
2016-08-21 22:13:31 +00:00
|
|
|
{}
|
2016-06-13 23:30:52 +00:00
|
|
|
|
2016-08-06 18:33:24 +00:00
|
|
|
void Speaker::set_volume(uint8_t volume)
|
2016-06-13 23:30:52 +00:00
|
|
|
{
|
2016-06-14 00:59:01 +00:00
|
|
|
_volume = volume;
|
2016-06-13 23:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 18:33:24 +00:00
|
|
|
void Speaker::set_control(int channel, uint8_t value)
|
2016-06-13 23:30:52 +00:00
|
|
|
{
|
2016-06-14 00:59:01 +00:00
|
|
|
_control_registers[channel] = value;
|
2016-06-13 23:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 21:50:32 +00:00
|
|
|
// Source: VICE. Not original.
|
|
|
|
static uint8_t noise_pattern[] = {
|
|
|
|
0x07, 0x1e, 0x1e, 0x1c, 0x1c, 0x3e, 0x3c, 0x38, 0x78, 0xf8, 0x7c, 0x1e, 0x1f, 0x8f, 0x07, 0x07,
|
|
|
|
0xc1, 0xc0, 0xe0, 0xf1, 0xe0, 0xf0, 0xe3, 0xe1, 0xc0, 0xe0, 0x78, 0x7e, 0x3c, 0x38, 0xe0, 0xe1,
|
|
|
|
0xc3, 0xc3, 0x87, 0x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3c, 0x38, 0x3f,
|
|
|
|
};
|
|
|
|
|
2016-08-19 17:35:34 +00:00
|
|
|
#define shift(r) _shift_registers[r] = (_shift_registers[r] << 1) | (((_shift_registers[r]^0x80)&_control_registers[r]) >> 7)
|
|
|
|
#define increment(r) _shift_registers[r] = (_shift_registers[r]+1)%8191
|
2016-08-20 20:54:43 +00:00
|
|
|
#define update(r, m, up) _counters[r]++; if((_counters[r] >> m) == 0x80) { up(r); _counters[r] = (unsigned int)(_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
|
|
|
|
2016-08-06 18:33:24 +00:00
|
|
|
void Speaker::get_samples(unsigned int number_of_samples, int16_t *target)
|
2016-06-13 23:30:52 +00:00
|
|
|
{
|
2016-06-14 00:59:01 +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?
|
2016-06-14 21:50:32 +00:00
|
|
|
target[c] = (
|
|
|
|
(_shift_registers[0]&1) +
|
|
|
|
(_shift_registers[1]&1) +
|
|
|
|
(_shift_registers[2]&1) +
|
|
|
|
((noise_pattern[_shift_registers[3] >> 3] >> (_shift_registers[3]&7))&(_control_registers[3] >> 7)&1)
|
2016-06-21 11:31:14 +00:00
|
|
|
) * _volume * 700 + _volume * 44;
|
2016-06-14 00:59:01 +00:00
|
|
|
}
|
2016-06-13 23:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 18:33:24 +00:00
|
|
|
void Speaker::skip_samples(unsigned int number_of_samples)
|
2016-06-13 23:30:52 +00:00
|
|
|
{
|
2016-06-14 00:59:01 +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-14 00:59:01 +00:00
|
|
|
}
|
2016-06-13 23:30:52 +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
|