2016-06-05 14:51:07 +00:00
|
|
|
//
|
|
|
|
// 6560.hpp
|
|
|
|
// 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
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2016-06-05 14:51:07 +00:00
|
|
|
|
2017-07-26 00:20:55 +00:00
|
|
|
#include "../../ClockReceiver/ClockReceiver.hpp"
|
2017-12-18 02:26:06 +00:00
|
|
|
#include "../../Concurrency/AsyncTaskQueue.hpp"
|
|
|
|
#include "../../Outputs/CRT/CRT.hpp"
|
2017-12-19 02:39:23 +00:00
|
|
|
#include "../../Outputs/Speaker/Implementation/LowpassSpeaker.hpp"
|
|
|
|
#include "../../Outputs/Speaker/Implementation/SampleSource.hpp"
|
2016-06-05 14:51:07 +00:00
|
|
|
|
2023-05-10 21:02:18 +00:00
|
|
|
namespace MOS::MOS6560 {
|
2016-06-05 14:51:07 +00:00
|
|
|
|
2016-08-06 18:33:24 +00:00
|
|
|
// audio state
|
2024-02-02 02:47:44 +00:00
|
|
|
class AudioGenerator: public ::Outputs::Speaker::SampleSource<AudioGenerator> {
|
2016-08-06 18:33:24 +00:00
|
|
|
public:
|
2022-07-16 18:41:04 +00:00
|
|
|
AudioGenerator(Concurrency::AsyncTaskQueue<false> &audio_queue);
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2016-08-06 18:33:24 +00:00
|
|
|
void set_volume(uint8_t volume);
|
|
|
|
void set_control(int channel, uint8_t value);
|
|
|
|
|
2018-03-09 18:23:18 +00:00
|
|
|
// For ::SampleSource.
|
2017-12-18 02:26:06 +00:00
|
|
|
void get_samples(std::size_t number_of_samples, int16_t *target);
|
|
|
|
void skip_samples(std::size_t number_of_samples);
|
2018-03-09 18:23:18 +00:00
|
|
|
void set_sample_volume_range(std::int16_t range);
|
2024-02-02 02:29:00 +00:00
|
|
|
static constexpr bool is_stereo = false;
|
2016-08-06 18:33:24 +00:00
|
|
|
|
|
|
|
private:
|
2022-07-16 18:41:04 +00:00
|
|
|
Concurrency::AsyncTaskQueue<false> &audio_queue_;
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2018-11-24 03:32:32 +00:00
|
|
|
unsigned int counters_[4] = {2, 1, 0, 0}; // create a slight phase offset for the three channels
|
2017-11-11 03:05:35 +00:00
|
|
|
unsigned int shift_registers_[4] = {0, 0, 0, 0};
|
|
|
|
uint8_t control_registers_[4] = {0, 0, 0, 0};
|
2018-04-06 01:04:46 +00:00
|
|
|
int16_t volume_ = 0;
|
|
|
|
int16_t range_multiplier_ = 1;
|
2016-08-06 18:33:24 +00:00
|
|
|
};
|
|
|
|
|
2018-04-12 02:00:42 +00:00
|
|
|
struct BusHandler {
|
2020-05-30 04:37:06 +00:00
|
|
|
void perform_read([[maybe_unused]] uint16_t address, [[maybe_unused]] uint8_t *pixel_data, [[maybe_unused]] uint8_t *colour_data) {
|
2018-04-12 02:00:42 +00:00
|
|
|
*pixel_data = 0xff;
|
|
|
|
*colour_data = 0xff;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class OutputMode {
|
|
|
|
PAL, NTSC
|
|
|
|
};
|
|
|
|
|
2016-06-15 00:00:16 +00:00
|
|
|
/*!
|
2016-06-19 22:11:37 +00:00
|
|
|
The 6560 Video Interface Chip ('VIC') is a video and audio output chip; it therefore vends both a @c CRT and a @c Speaker.
|
2016-06-13 23:30:52 +00:00
|
|
|
|
2016-06-19 22:11:37 +00:00
|
|
|
To run the VIC for a cycle, the caller should call @c get_address, make the requested bus access
|
2016-06-15 00:00:16 +00:00
|
|
|
and call @c set_graphics_value with the result.
|
2016-06-13 23:30:52 +00:00
|
|
|
|
2020-01-05 18:40:02 +00:00
|
|
|
@c write and @c read provide register access.
|
2016-06-15 00:00:16 +00:00
|
|
|
*/
|
2018-04-12 02:00:42 +00:00
|
|
|
template <class BusHandler> class MOS6560 {
|
2016-06-05 14:51:07 +00:00
|
|
|
public:
|
2018-04-12 02:00:42 +00:00
|
|
|
MOS6560(BusHandler &bus_handler) :
|
|
|
|
bus_handler_(bus_handler),
|
2019-01-06 21:32:13 +00:00
|
|
|
crt_(65*4, 1, Outputs::Display::Type::NTSC60, Outputs::Display::InputDataType::Luminance8Phase8),
|
2017-12-18 02:26:06 +00:00
|
|
|
audio_generator_(audio_queue_),
|
|
|
|
speaker_(audio_generator_)
|
|
|
|
{
|
2018-03-30 22:24:18 +00:00
|
|
|
// default to s-video output
|
2018-11-29 01:53:33 +00:00
|
|
|
crt_.set_display_type(Outputs::Display::DisplayType::SVideo);
|
2018-03-30 22:24:18 +00:00
|
|
|
|
2016-08-05 23:13:49 +00:00
|
|
|
// default to NTSC
|
|
|
|
set_output_mode(OutputMode::NTSC);
|
2016-08-19 17:35:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 01:59:19 +00:00
|
|
|
~MOS6560() {
|
|
|
|
audio_queue_.flush();
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void set_clock_rate(double clock_rate) {
|
2020-05-10 03:00:39 +00:00
|
|
|
speaker_.set_input_rate(float(clock_rate / 4.0));
|
2016-08-05 23:13:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 18:14:45 +00:00
|
|
|
void set_scan_target(Outputs::Display::ScanTarget *scan_target) { crt_.set_scan_target(scan_target); }
|
2020-01-22 03:28:25 +00:00
|
|
|
Outputs::Display::ScanStatus get_scaled_scan_status() const { return crt_.get_scaled_scan_status() / 4.0f; }
|
2023-05-12 18:14:45 +00:00
|
|
|
void set_display_type(Outputs::Display::DisplayType display_type) { crt_.set_display_type(display_type); }
|
|
|
|
Outputs::Display::DisplayType get_display_type() const { return crt_.get_display_type(); }
|
|
|
|
Outputs::Speaker::Speaker *get_speaker() { return &speaker_; }
|
2017-12-18 02:43:08 +00:00
|
|
|
|
|
|
|
void set_high_frequency_cutoff(float cutoff) {
|
|
|
|
speaker_.set_high_frequency_cutoff(cutoff);
|
|
|
|
}
|
2016-06-05 14:51:07 +00:00
|
|
|
|
2016-06-23 11:32:24 +00:00
|
|
|
/*!
|
|
|
|
Sets the output mode to either PAL or NTSC.
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
void set_output_mode(OutputMode output_mode) {
|
2016-12-03 15:51:09 +00:00
|
|
|
output_mode_ = output_mode;
|
2017-05-13 18:29:36 +00:00
|
|
|
|
2018-05-13 19:43:03 +00:00
|
|
|
// Luminances are encoded trivially: on a 0-255 scale.
|
2017-05-10 01:22:01 +00:00
|
|
|
const uint8_t luminances[16] = {
|
2018-04-11 03:05:18 +00:00
|
|
|
0, 255, 64, 192,
|
|
|
|
128, 128, 64, 192,
|
|
|
|
128, 192, 128, 255,
|
|
|
|
192, 192, 128, 255
|
2016-08-05 23:13:49 +00:00
|
|
|
};
|
2017-05-13 18:29:36 +00:00
|
|
|
|
2018-05-13 19:43:03 +00:00
|
|
|
// Chrominances are encoded such that 0-128 is a complete revolution of phase;
|
2017-05-13 18:29:36 +00:00
|
|
|
// anything above 191 disables the colour subcarrier. Phase is relative to the
|
2019-02-26 00:28:52 +00:00
|
|
|
// colour burst, so 0 is green (NTSC) or blue/violet (PAL).
|
2017-05-10 01:22:01 +00:00
|
|
|
const uint8_t pal_chrominances[16] = {
|
2019-02-26 00:28:52 +00:00
|
|
|
255, 255, 90, 20,
|
|
|
|
96, 42, 8, 72,
|
|
|
|
84, 90, 90, 20,
|
|
|
|
96, 42, 8, 72,
|
2016-08-05 23:13:49 +00:00
|
|
|
};
|
2017-05-06 23:53:24 +00:00
|
|
|
const uint8_t ntsc_chrominances[16] = {
|
2018-06-01 01:40:46 +00:00
|
|
|
255, 255, 121, 57,
|
|
|
|
103, 42, 80, 16,
|
|
|
|
0, 9, 121, 57,
|
|
|
|
103, 42, 80, 16,
|
2016-08-05 23:13:49 +00:00
|
|
|
};
|
2017-05-06 23:53:24 +00:00
|
|
|
const uint8_t *chrominances;
|
2018-11-04 01:54:25 +00:00
|
|
|
Outputs::Display::Type display_type;
|
2016-08-05 23:13:49 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(output_mode) {
|
2017-11-12 21:41:09 +00:00
|
|
|
default:
|
2016-08-05 23:13:49 +00:00
|
|
|
chrominances = pal_chrominances;
|
2018-11-04 01:54:25 +00:00
|
|
|
display_type = Outputs::Display::Type::PAL50;
|
2016-12-03 15:51:09 +00:00
|
|
|
timing_.cycles_per_line = 71;
|
2018-04-13 01:12:09 +00:00
|
|
|
timing_.line_counter_increment_offset = 4;
|
|
|
|
timing_.final_line_increment_position = timing_.cycles_per_line - timing_.line_counter_increment_offset;
|
2016-12-03 15:51:09 +00:00
|
|
|
timing_.lines_per_progressive_field = 312;
|
|
|
|
timing_.supports_interlacing = false;
|
2016-08-05 23:13:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OutputMode::NTSC:
|
|
|
|
chrominances = ntsc_chrominances;
|
2018-11-04 01:54:25 +00:00
|
|
|
display_type = Outputs::Display::Type::NTSC60;
|
2016-12-03 15:51:09 +00:00
|
|
|
timing_.cycles_per_line = 65;
|
2018-04-11 12:00:37 +00:00
|
|
|
timing_.line_counter_increment_offset = 40;
|
|
|
|
timing_.final_line_increment_position = 58;
|
2016-12-03 15:51:09 +00:00
|
|
|
timing_.lines_per_progressive_field = 261;
|
|
|
|
timing_.supports_interlacing = true;
|
2016-08-05 23:13:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-23 03:43:42 +00:00
|
|
|
crt_.set_new_display_type(timing_.cycles_per_line*4, display_type);
|
2018-04-06 01:25:19 +00:00
|
|
|
|
|
|
|
switch(output_mode) {
|
|
|
|
case OutputMode::PAL:
|
2018-11-23 03:43:42 +00:00
|
|
|
crt_.set_visible_area(Outputs::Display::Rect(0.1f, 0.07f, 0.9f, 0.9f));
|
2018-04-06 01:25:19 +00:00
|
|
|
break;
|
|
|
|
case OutputMode::NTSC:
|
2018-11-23 03:43:42 +00:00
|
|
|
crt_.set_visible_area(Outputs::Display::Rect(0.05f, 0.05f, 0.9f, 0.9f));
|
2018-04-06 01:25:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-09-11 21:09:00 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
for(int c = 0; c < 16; c++) {
|
2017-10-22 02:30:15 +00:00
|
|
|
uint8_t *colour = reinterpret_cast<uint8_t *>(&colours_[c]);
|
2017-05-10 01:22:01 +00:00
|
|
|
colour[0] = luminances[c];
|
2017-05-13 18:29:36 +00:00
|
|
|
colour[1] = chrominances[c];
|
2016-08-05 23:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-23 11:32:24 +00:00
|
|
|
|
2016-08-10 00:41:05 +00:00
|
|
|
/*!
|
|
|
|
Runs for cycles. Derr.
|
|
|
|
*/
|
2017-07-28 02:05:29 +00:00
|
|
|
inline void run_for(const Cycles cycles) {
|
2016-08-10 01:10:53 +00:00
|
|
|
// keep track of the amount of time since the speaker was updated; lazy updates are applied
|
2017-07-25 11:15:31 +00:00
|
|
|
cycles_since_speaker_update_ += cycles;
|
2016-08-10 01:10:53 +00:00
|
|
|
|
2019-10-30 02:36:29 +00:00
|
|
|
auto number_of_cycles = cycles.as_integral();
|
2017-03-26 18:34:47 +00:00
|
|
|
while(number_of_cycles--) {
|
2016-08-10 01:10:53 +00:00
|
|
|
// keep an old copy of the vertical count because that test is a cycle later than the actual changes
|
2016-12-03 15:51:09 +00:00
|
|
|
int previous_vertical_counter = vertical_counter_;
|
2016-08-10 01:10:53 +00:00
|
|
|
|
|
|
|
// keep track of internal time relative to this scanline
|
2016-12-03 15:51:09 +00:00
|
|
|
horizontal_counter_++;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(horizontal_counter_ == timing_.cycles_per_line) {
|
|
|
|
if(horizontal_drawing_latch_) {
|
2016-12-03 15:51:09 +00:00
|
|
|
current_character_row_++;
|
2016-08-10 01:10:53 +00:00
|
|
|
if(
|
2016-12-03 15:51:09 +00:00
|
|
|
(current_character_row_ == 16) ||
|
|
|
|
(current_character_row_ == 8 && !registers_.tall_characters)
|
2016-08-10 01:10:53 +00:00
|
|
|
) {
|
2016-12-03 15:51:09 +00:00
|
|
|
current_character_row_ = 0;
|
|
|
|
current_row_++;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
pixel_line_cycle_ = -1;
|
|
|
|
columns_this_line_ = -1;
|
|
|
|
column_counter_ = -1;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
horizontal_counter_ = 0;
|
|
|
|
if(output_mode_ == OutputMode::PAL) is_odd_line_ ^= true;
|
|
|
|
horizontal_drawing_latch_ = false;
|
2016-08-10 01:10:53 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
vertical_counter_ ++;
|
2018-04-08 20:18:41 +00:00
|
|
|
if(vertical_counter_ == lines_this_field()) {
|
2016-12-03 15:51:09 +00:00
|
|
|
vertical_counter_ = 0;
|
|
|
|
|
|
|
|
if(output_mode_ == OutputMode::NTSC) is_odd_frame_ ^= true;
|
|
|
|
current_row_ = 0;
|
|
|
|
rows_this_field_ = -1;
|
|
|
|
vertical_drawing_latch_ = false;
|
|
|
|
base_video_matrix_address_counter_ = 0;
|
|
|
|
current_character_row_ = 0;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for vertical starting events
|
2016-12-03 15:51:09 +00:00
|
|
|
vertical_drawing_latch_ |= registers_.first_row_location == (previous_vertical_counter >> 1);
|
|
|
|
horizontal_drawing_latch_ |= vertical_drawing_latch_ && (horizontal_counter_ == registers_.first_column_location);
|
2016-08-10 01:10:53 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
if(pixel_line_cycle_ >= 0) pixel_line_cycle_++;
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(pixel_line_cycle_) {
|
2016-08-10 01:10:53 +00:00
|
|
|
case -1:
|
2017-03-26 18:34:47 +00:00
|
|
|
if(horizontal_drawing_latch_) {
|
2016-12-03 15:51:09 +00:00
|
|
|
pixel_line_cycle_ = 0;
|
|
|
|
video_matrix_address_counter_ = base_video_matrix_address_counter_;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-12-03 15:51:09 +00:00
|
|
|
case 1: columns_this_line_ = registers_.number_of_columns; break;
|
|
|
|
case 2: if(rows_this_field_ < 0) rows_this_field_ = registers_.number_of_rows; break;
|
|
|
|
case 3: if(current_row_ < rows_this_field_) column_counter_ = 0; break;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t fetch_address = 0x1c;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(column_counter_ >= 0 && column_counter_ < columns_this_line_*2) {
|
|
|
|
if(column_counter_&1) {
|
2016-12-03 15:51:09 +00:00
|
|
|
fetch_address = registers_.character_cell_start_address + (character_code_*(registers_.tall_characters ? 16 : 8)) + current_character_row_;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2020-05-10 03:00:39 +00:00
|
|
|
fetch_address = uint16_t(registers_.video_matrix_start_address + video_matrix_address_counter_);
|
2016-12-03 15:51:09 +00:00
|
|
|
video_matrix_address_counter_++;
|
2016-08-10 01:10:53 +00:00
|
|
|
if(
|
2016-12-03 15:51:09 +00:00
|
|
|
(current_character_row_ == 15) ||
|
|
|
|
(current_character_row_ == 7 && !registers_.tall_characters)
|
2016-08-10 01:10:53 +00:00
|
|
|
) {
|
2016-12-03 15:51:09 +00:00
|
|
|
base_video_matrix_address_counter_ = video_matrix_address_counter_;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch_address &= 0x3fff;
|
|
|
|
|
2016-08-10 00:41:05 +00:00
|
|
|
uint8_t pixel_data;
|
|
|
|
uint8_t colour_data;
|
2018-04-12 02:00:42 +00:00
|
|
|
bus_handler_.perform_read(fetch_address, &pixel_data, &colour_data);
|
2016-08-10 01:10:53 +00:00
|
|
|
|
|
|
|
// TODO: there should be a further two-cycle delay on pixels being output; the reverse bit should
|
|
|
|
// divide the byte it is set for 3:1 and then continue as usual.
|
|
|
|
|
|
|
|
// determine output state; colour burst and sync timing are currently a guess
|
2016-12-03 15:51:09 +00:00
|
|
|
if(horizontal_counter_ > timing_.cycles_per_line-4) this_state_ = State::ColourBurst;
|
|
|
|
else if(horizontal_counter_ > timing_.cycles_per_line-7) this_state_ = State::Sync;
|
2017-03-26 18:34:47 +00:00
|
|
|
else {
|
2016-12-03 15:51:09 +00:00
|
|
|
this_state_ = (column_counter_ >= 0 && column_counter_ < columns_this_line_*2) ? State::Pixels : State::Border;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// apply vertical sync
|
|
|
|
if(
|
2018-04-11 12:00:37 +00:00
|
|
|
(vertical_counter_ < 3 && is_odd_frame()) ||
|
2016-12-03 15:51:09 +00:00
|
|
|
(registers_.interlaced &&
|
2016-08-10 01:10:53 +00:00
|
|
|
(
|
2016-12-03 15:51:09 +00:00
|
|
|
(vertical_counter_ == 0 && horizontal_counter_ > 32) ||
|
|
|
|
(vertical_counter_ == 1) || (vertical_counter_ == 2) ||
|
|
|
|
(vertical_counter_ == 3 && horizontal_counter_ <= 32)
|
2016-08-10 01:10:53 +00:00
|
|
|
)
|
|
|
|
))
|
2016-12-03 15:51:09 +00:00
|
|
|
this_state_ = State::Sync;
|
2016-08-10 01:10:53 +00:00
|
|
|
|
|
|
|
// update the CRT
|
2017-03-26 18:34:47 +00:00
|
|
|
if(this_state_ != output_state_) {
|
|
|
|
switch(output_state_) {
|
2018-11-23 03:43:42 +00:00
|
|
|
case State::Sync: crt_.output_sync(cycles_in_state_ * 4); break;
|
|
|
|
case State::ColourBurst: crt_.output_colour_burst(cycles_in_state_ * 4, (is_odd_frame_ || is_odd_line_) ? 128 : 0); break;
|
2023-12-07 16:04:18 +00:00
|
|
|
case State::Border: crt_.output_level<uint16_t>(cycles_in_state_ * 4, registers_.borderColour); break;
|
2018-11-23 03:43:42 +00:00
|
|
|
case State::Pixels: crt_.output_data(cycles_in_state_ * 4); break;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
2016-12-03 15:51:09 +00:00
|
|
|
output_state_ = this_state_;
|
|
|
|
cycles_in_state_ = 0;
|
2016-08-10 01:10:53 +00:00
|
|
|
|
|
|
|
pixel_pointer = nullptr;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(output_state_ == State::Pixels) {
|
2018-11-23 03:43:42 +00:00
|
|
|
pixel_pointer = reinterpret_cast<uint16_t *>(crt_.begin_data(260));
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-03 15:51:09 +00:00
|
|
|
cycles_in_state_++;
|
2016-08-10 01:10:53 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(this_state_ == State::Pixels) {
|
2018-04-13 01:12:09 +00:00
|
|
|
// TODO: palette changes can happen within half-characters; the below needs to be divided.
|
|
|
|
// Also: a perfect opportunity to rearrange this inner loop for no longer needing to be
|
|
|
|
// two parts with a cooperative owner?
|
2017-03-26 18:34:47 +00:00
|
|
|
if(column_counter_&1) {
|
2016-12-03 15:51:09 +00:00
|
|
|
character_value_ = pixel_data;
|
2016-08-10 01:10:53 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(pixel_pointer) {
|
2017-05-10 01:22:01 +00:00
|
|
|
uint16_t cell_colour = colours_[character_colour_ & 0x7];
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!(character_colour_&0x8)) {
|
2017-05-10 01:22:01 +00:00
|
|
|
uint16_t colours[2];
|
2017-03-26 18:34:47 +00:00
|
|
|
if(registers_.invertedCells) {
|
2016-08-10 01:10:53 +00:00
|
|
|
colours[0] = cell_colour;
|
2016-12-03 15:51:09 +00:00
|
|
|
colours[1] = registers_.backgroundColour;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-03 15:51:09 +00:00
|
|
|
colours[0] = registers_.backgroundColour;
|
2016-08-10 01:10:53 +00:00
|
|
|
colours[1] = cell_colour;
|
|
|
|
}
|
2016-12-03 15:51:09 +00:00
|
|
|
pixel_pointer[0] = colours[(character_value_ >> 7)&1];
|
|
|
|
pixel_pointer[1] = colours[(character_value_ >> 6)&1];
|
|
|
|
pixel_pointer[2] = colours[(character_value_ >> 5)&1];
|
|
|
|
pixel_pointer[3] = colours[(character_value_ >> 4)&1];
|
|
|
|
pixel_pointer[4] = colours[(character_value_ >> 3)&1];
|
|
|
|
pixel_pointer[5] = colours[(character_value_ >> 2)&1];
|
|
|
|
pixel_pointer[6] = colours[(character_value_ >> 1)&1];
|
|
|
|
pixel_pointer[7] = colours[(character_value_ >> 0)&1];
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2017-05-10 01:22:01 +00:00
|
|
|
uint16_t colours[4] = {registers_.backgroundColour, registers_.borderColour, cell_colour, registers_.auxiliary_colour};
|
2016-08-10 01:10:53 +00:00
|
|
|
pixel_pointer[0] =
|
2016-12-03 15:51:09 +00:00
|
|
|
pixel_pointer[1] = colours[(character_value_ >> 6)&3];
|
2016-08-10 01:10:53 +00:00
|
|
|
pixel_pointer[2] =
|
2016-12-03 15:51:09 +00:00
|
|
|
pixel_pointer[3] = colours[(character_value_ >> 4)&3];
|
2016-08-10 01:10:53 +00:00
|
|
|
pixel_pointer[4] =
|
2016-12-03 15:51:09 +00:00
|
|
|
pixel_pointer[5] = colours[(character_value_ >> 2)&3];
|
2016-08-10 01:10:53 +00:00
|
|
|
pixel_pointer[6] =
|
2016-12-03 15:51:09 +00:00
|
|
|
pixel_pointer[7] = colours[(character_value_ >> 0)&3];
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
2017-05-13 18:29:36 +00:00
|
|
|
|
2016-08-10 01:10:53 +00:00
|
|
|
pixel_pointer += 8;
|
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-03 15:51:09 +00:00
|
|
|
character_code_ = pixel_data;
|
|
|
|
character_colour_ = colour_data;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
2018-04-13 01:12:09 +00:00
|
|
|
}
|
2016-08-10 01:10:53 +00:00
|
|
|
|
2018-04-13 01:12:09 +00:00
|
|
|
// Keep counting columns even if sync or the colour burst have interceded.
|
|
|
|
if(column_counter_ >= 0 && column_counter_ < columns_this_line_*2) {
|
2016-12-03 15:51:09 +00:00
|
|
|
column_counter_++;
|
2016-08-10 01:10:53 +00:00
|
|
|
}
|
2016-08-10 00:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Causes the 6560 to flush as much pending CRT and speaker communications as possible.
|
|
|
|
*/
|
2017-12-18 02:26:06 +00:00
|
|
|
inline void flush() {
|
|
|
|
update_audio();
|
|
|
|
audio_queue_.perform();
|
|
|
|
}
|
2016-08-10 00:41:05 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Writes to a 6560 register.
|
|
|
|
*/
|
2020-01-05 18:40:02 +00:00
|
|
|
void write(int address, uint8_t value) {
|
2016-08-10 00:41:05 +00:00
|
|
|
address &= 0xf;
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.direct_values[address] = value;
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(address) {
|
2016-08-10 00:41:05 +00:00
|
|
|
case 0x0:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interlaced = !!(value&0x80) && timing_.supports_interlacing;
|
|
|
|
registers_.first_column_location = value & 0x7f;
|
2016-08-10 00:41:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x1:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.first_row_location = value;
|
2016-08-10 00:41:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x2:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.number_of_columns = value & 0x7f;
|
2020-05-10 03:00:39 +00:00
|
|
|
registers_.video_matrix_start_address = uint16_t((registers_.video_matrix_start_address & 0x3c00) | ((value & 0x80) << 2));
|
2016-08-10 00:41:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x3:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.number_of_rows = (value >> 1)&0x3f;
|
|
|
|
registers_.tall_characters = !!(value&0x01);
|
2016-08-10 00:41:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x5:
|
2020-05-10 03:00:39 +00:00
|
|
|
registers_.character_cell_start_address = uint16_t((value & 0x0f) << 10);
|
|
|
|
registers_.video_matrix_start_address = uint16_t((registers_.video_matrix_start_address & 0x0200) | ((value & 0xf0) << 6));
|
2016-08-10 00:41:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xa:
|
|
|
|
case 0xb:
|
|
|
|
case 0xc:
|
|
|
|
case 0xd:
|
|
|
|
update_audio();
|
2017-12-18 02:26:06 +00:00
|
|
|
audio_generator_.set_control(address - 0xa, value);
|
2016-08-10 00:41:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xe:
|
|
|
|
update_audio();
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.auxiliary_colour = colours_[value >> 4];
|
2017-12-18 02:26:06 +00:00
|
|
|
audio_generator_.set_volume(value & 0xf);
|
2016-08-10 00:41:05 +00:00
|
|
|
break;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
case 0xf: {
|
2017-05-10 01:22:01 +00:00
|
|
|
uint16_t new_border_colour = colours_[value & 0x07];
|
2017-03-26 18:34:47 +00:00
|
|
|
if(this_state_ == State::Border && new_border_colour != registers_.borderColour) {
|
2023-12-07 16:04:18 +00:00
|
|
|
crt_.output_level<uint16_t>(cycles_in_state_ * 4, registers_.borderColour);
|
2016-12-03 15:51:09 +00:00
|
|
|
cycles_in_state_ = 0;
|
2016-08-10 00:41:05 +00:00
|
|
|
}
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.invertedCells = !((value >> 3)&1);
|
|
|
|
registers_.borderColour = new_border_colour;
|
|
|
|
registers_.backgroundColour = colours_[value >> 4];
|
2016-08-10 00:41:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// TODO: the lightpen, etc
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Reads from a 6560 register.
|
|
|
|
*/
|
2020-05-10 01:22:51 +00:00
|
|
|
uint8_t read(int address) const {
|
2016-08-10 00:41:05 +00:00
|
|
|
address &= 0xf;
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(address) {
|
2016-12-03 15:51:09 +00:00
|
|
|
default: return registers_.direct_values[address];
|
2020-05-10 01:22:51 +00:00
|
|
|
case 0x03: return uint8_t(raster_value() << 7) | (registers_.direct_values[3] & 0x7f);
|
2018-04-08 20:18:41 +00:00
|
|
|
case 0x04: return (raster_value() >> 1) & 0xff;
|
2016-08-10 00:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-12 02:00:42 +00:00
|
|
|
BusHandler &bus_handler_;
|
2018-11-23 03:43:42 +00:00
|
|
|
Outputs::CRT::CRT crt_;
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2022-07-16 18:41:04 +00:00
|
|
|
Concurrency::AsyncTaskQueue<false> audio_queue_;
|
2017-12-18 02:26:06 +00:00
|
|
|
AudioGenerator audio_generator_;
|
2021-11-21 20:37:29 +00:00
|
|
|
Outputs::Speaker::PullLowpass<AudioGenerator> speaker_;
|
2016-08-10 00:41:05 +00:00
|
|
|
|
2017-07-25 11:15:31 +00:00
|
|
|
Cycles cycles_since_speaker_update_;
|
2017-03-26 18:34:47 +00:00
|
|
|
void update_audio() {
|
2017-12-18 02:26:06 +00:00
|
|
|
speaker_.run_for(audio_queue_, Cycles(cycles_since_speaker_update_.divide(Cycles(4))));
|
2016-08-10 00:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// register state
|
|
|
|
struct {
|
2018-09-11 02:21:03 +00:00
|
|
|
bool interlaced = false, tall_characters = false;
|
2020-09-16 21:24:18 +00:00
|
|
|
uint8_t first_column_location = 0, first_row_location = 0;
|
|
|
|
uint8_t number_of_columns = 0, number_of_rows = 0;
|
|
|
|
uint16_t character_cell_start_address = 0, video_matrix_start_address = 0;
|
|
|
|
uint16_t backgroundColour = 0, borderColour = 0, auxiliary_colour = 0;
|
2018-09-11 02:21:03 +00:00
|
|
|
bool invertedCells = false;
|
2016-08-10 00:41:05 +00:00
|
|
|
|
2020-09-16 21:24:18 +00:00
|
|
|
uint8_t direct_values[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
2016-12-03 15:51:09 +00:00
|
|
|
} registers_;
|
2016-08-10 00:41:05 +00:00
|
|
|
|
|
|
|
// output state
|
|
|
|
enum State {
|
|
|
|
Sync, ColourBurst, Border, Pixels
|
2020-09-16 21:24:18 +00:00
|
|
|
} this_state_ = State::Sync, output_state_ = State::Sync;
|
|
|
|
int cycles_in_state_ = 0;
|
2016-08-10 00:41:05 +00:00
|
|
|
|
|
|
|
// counters that cover an entire field
|
2018-04-08 22:51:20 +00:00
|
|
|
int horizontal_counter_ = 0, vertical_counter_ = 0;
|
2020-05-10 01:22:51 +00:00
|
|
|
int lines_this_field() const {
|
2018-04-11 12:00:37 +00:00
|
|
|
// Necessary knowledge here: only the NTSC 6560 supports interlaced video.
|
2018-04-08 20:18:41 +00:00
|
|
|
return registers_.interlaced ? (is_odd_frame_ ? 262 : 263) : timing_.lines_per_progressive_field;
|
|
|
|
}
|
2020-05-10 01:22:51 +00:00
|
|
|
int raster_value() const {
|
2018-04-11 03:05:18 +00:00
|
|
|
const int bonus_line = (horizontal_counter_ + timing_.line_counter_increment_offset) / timing_.cycles_per_line;
|
2018-04-11 12:00:37 +00:00
|
|
|
const int line = vertical_counter_ + bonus_line;
|
|
|
|
const int final_line = lines_this_field();
|
|
|
|
|
|
|
|
if(line < final_line)
|
|
|
|
return line;
|
|
|
|
|
|
|
|
if(is_odd_frame()) {
|
|
|
|
return (horizontal_counter_ >= timing_.final_line_increment_position) ? 0 : final_line - 1;
|
|
|
|
} else {
|
|
|
|
return line % final_line;
|
|
|
|
}
|
2018-04-11 03:05:18 +00:00
|
|
|
// Cf. http://www.sleepingelephant.com/ipw-web/bulletin/bb/viewtopic.php?f=14&t=7237&start=15#p80737
|
2018-04-08 20:18:41 +00:00
|
|
|
}
|
2020-05-10 01:22:51 +00:00
|
|
|
bool is_odd_frame() const {
|
2018-04-11 12:00:37 +00:00
|
|
|
return is_odd_frame_ || !registers_.interlaced;
|
|
|
|
}
|
2016-08-10 00:41:05 +00:00
|
|
|
|
|
|
|
// latches dictating start and length of drawing
|
2018-09-11 02:21:03 +00:00
|
|
|
bool vertical_drawing_latch_ = false, horizontal_drawing_latch_ = false;
|
2020-09-16 21:24:18 +00:00
|
|
|
int rows_this_field_ = 0, columns_this_line_ = 0;
|
2016-08-10 00:41:05 +00:00
|
|
|
|
|
|
|
// current drawing position counter
|
2020-09-16 21:24:18 +00:00
|
|
|
int pixel_line_cycle_ = 0, column_counter_ = 0;
|
|
|
|
int current_row_ = 0;
|
|
|
|
uint16_t current_character_row_ = 0;
|
|
|
|
uint16_t video_matrix_address_counter_ = 0, base_video_matrix_address_counter_ = 0;
|
2016-08-10 00:41:05 +00:00
|
|
|
|
|
|
|
// data latched from the bus
|
2020-09-16 21:24:18 +00:00
|
|
|
uint8_t character_code_ = 0, character_colour_ = 0, character_value_ = 0;
|
2016-08-10 00:41:05 +00:00
|
|
|
|
2017-11-11 03:05:35 +00:00
|
|
|
bool is_odd_frame_ = false, is_odd_line_ = false;
|
2016-08-10 00:41:05 +00:00
|
|
|
|
|
|
|
// lookup table from 6560 colour index to appropriate PAL/NTSC value
|
2020-09-16 21:24:18 +00:00
|
|
|
uint16_t colours_[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
2016-08-10 00:41:05 +00:00
|
|
|
|
2020-09-16 21:24:18 +00:00
|
|
|
uint16_t *pixel_pointer = nullptr;
|
2016-08-10 00:41:05 +00:00
|
|
|
|
|
|
|
struct {
|
2020-09-16 21:24:18 +00:00
|
|
|
int cycles_per_line = 0;
|
|
|
|
int line_counter_increment_offset = 0;
|
|
|
|
int final_line_increment_position = 0;
|
|
|
|
int lines_per_progressive_field = 0;
|
|
|
|
bool supports_interlacing = 0;
|
2016-12-03 15:51:09 +00:00
|
|
|
} timing_;
|
2020-09-16 21:24:18 +00:00
|
|
|
OutputMode output_mode_ = OutputMode::NTSC;
|
2016-06-05 14:51:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|