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"
|
|
|
|
|
2017-11-10 03:04:49 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2016-06-05 14:51:07 +00:00
|
|
|
using namespace MOS;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Speaker::set_volume(uint8_t volume) {
|
2016-10-07 21:10:00 +00:00
|
|
|
enqueue([=]() {
|
2016-12-03 15:51:09 +00:00
|
|
|
volume_ = volume;
|
2016-10-07 21:10:00 +00:00
|
|
|
});
|
2016-06-13 23:30:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Speaker::set_control(int channel, uint8_t value) {
|
2016-10-07 21:10:00 +00:00
|
|
|
enqueue([=]() {
|
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.
|
|
|
|
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, 0xc7, 0x07, 0x1e, 0x1c, 0x1f, 0x0e, 0x0e, 0x1e, 0x0e, 0x0f, 0x0f, 0xc3, 0xc3,
|
|
|
|
0xf1, 0xe1, 0xe3,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f, 0x1f, 0x0f, 0x1f, 0x0f, 0x1f, 0x87, 0x87, 0x83, 0x87,
|
|
|
|
0x83, 0xe1, 0xe1, 0xf0, 0x78, 0xf1, 0xf0, 0x70, 0x38, 0x38, 0x70, 0xe0, 0xe3, 0xc0, 0xe0, 0xf8,
|
|
|
|
0x78, 0x78, 0xf8, 0x38, 0xf1, 0xe1, 0xe1, 0xc3, 0x87, 0x87, 0x0e, 0x1e, 0x1f, 0x0e, 0x0e, 0x0f,
|
|
|
|
0x0f, 0x87, 0xc3, 0x87, 0x07, 0x83, 0xc0, 0xf0, 0x38, 0x3c, 0x3c, 0x38, 0xf0, 0xfc, 0x3e, 0x1e,
|
|
|
|
0x1c, 0x1c, 0x38, 0x70, 0xf0, 0xf1, 0xe0, 0xf0, 0xe0, 0xe0, 0xf1, 0xe3, 0xe0, 0xe1, 0xf0, 0xf0,
|
|
|
|
0x78, 0x7c, 0x78, 0x3c, 0x78, 0x78, 0x38, 0x78, 0x78, 0x78, 0x78, 0x70, 0xe3, 0x83, 0x83, 0xe0,
|
|
|
|
0xc3, 0xc1, 0xe1, 0xc1, 0xc1, 0xc1, 0xe3, 0xc3, 0xc7, 0x1e, 0x0e, 0x1f, 0x1e, 0x1e, 0x0f, 0x0f,
|
|
|
|
0x0e, 0x0e, 0x0e, 0x07, 0x83, 0x87, 0x87, 0x0e, 0x07, 0x8f, 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,
|
|
|
|
};
|
|
|
|
|
2016-12-03 15:51:09 +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
|
2017-10-21 23:49:04 +00:00
|
|
|
#define update(r, m, up) counters_[r]++; if((counters_[r] >> m) == 0x80) { up(r); counters_[r] = static_cast<unsigned int>(control_registers_[r]&0x7f) << m; }
|
2016-08-20 20:54:43 +00:00
|
|
|
// 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
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Speaker::get_samples(unsigned int number_of_samples, int16_t *target) {
|
|
|
|
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] = (
|
2016-12-03 15:51:09 +00:00
|
|
|
(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)
|
|
|
|
) * volume_ * 700 + volume_ * 44;
|
2016-06-14 00:59:01 +00:00
|
|
|
}
|
2016-06-13 23:30:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Speaker::skip_samples(unsigned int number_of_samples) {
|
|
|
|
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
|