2015-07-19 17:36:27 +00:00
|
|
|
//
|
|
|
|
// CRT.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "CRT.hpp"
|
2017-11-10 03:04:49 +00:00
|
|
|
#include "Internals/CRTOpenGL.hpp"
|
2017-07-07 01:46:24 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cmath>
|
2016-04-27 01:41:39 +00:00
|
|
|
#include <algorithm>
|
2017-07-07 01:46:24 +00:00
|
|
|
#include <cassert>
|
2015-07-19 17:36:27 +00:00
|
|
|
|
2016-03-09 01:49:07 +00:00
|
|
|
using namespace Outputs::CRT;
|
2015-07-19 17:36:27 +00:00
|
|
|
|
2017-07-29 18:53:53 +00:00
|
|
|
void CRT::set_new_timing(unsigned int cycles_per_line, unsigned int height_of_display, ColourSpace colour_space, unsigned int colour_cycle_numerator, unsigned int colour_cycle_denominator, unsigned int vertical_sync_half_lines, bool should_alternate) {
|
2016-11-17 03:00:11 +00:00
|
|
|
openGL_output_builder_.set_colour_format(colour_space, colour_cycle_numerator, colour_cycle_denominator);
|
2016-02-07 22:32:38 +00:00
|
|
|
|
2015-08-19 13:09:00 +00:00
|
|
|
const unsigned int millisecondsHorizontalRetraceTime = 7; // source: Dictionary of Video and Television Technology, p. 234
|
|
|
|
const unsigned int scanlinesVerticalRetraceTime = 10; // source: ibid
|
2015-07-22 22:15:18 +00:00
|
|
|
|
2015-08-19 02:36:32 +00:00
|
|
|
// To quote:
|
|
|
|
//
|
|
|
|
// "retrace interval; The interval of time for the return of the blanked scanning beam of
|
|
|
|
// a TV picture tube or camera tube to the starting point of a line or field. It is about 7 µs
|
|
|
|
// for horizontal retrace and 500 to 750 µs for vertical retrace in NTSC and PAL TV."
|
|
|
|
|
2016-11-17 03:00:11 +00:00
|
|
|
time_multiplier_ = IntermediateBufferWidth / cycles_per_line;
|
2017-01-07 17:38:00 +00:00
|
|
|
phase_denominator_ = cycles_per_line * colour_cycle_denominator * time_multiplier_;
|
2016-12-10 18:42:34 +00:00
|
|
|
phase_numerator_ = 0;
|
2017-01-07 02:36:19 +00:00
|
|
|
colour_cycle_numerator_ = colour_cycle_numerator;
|
2016-12-10 18:42:34 +00:00
|
|
|
phase_alternates_ = should_alternate;
|
|
|
|
is_alernate_line_ &= phase_alternates_;
|
2017-03-07 00:15:33 +00:00
|
|
|
cycles_per_line_ = cycles_per_line;
|
2016-11-17 03:00:11 +00:00
|
|
|
unsigned int multiplied_cycles_per_line = cycles_per_line * time_multiplier_;
|
2015-07-21 03:18:56 +00:00
|
|
|
|
2017-07-31 01:19:42 +00:00
|
|
|
// allow sync to be detected (and acted upon) a line earlier than the specified requirement,
|
|
|
|
// as a simple way of avoiding not-quite-exact comparison issues while still being true enough to
|
|
|
|
// the gist for simple debugging
|
|
|
|
sync_capacitor_charge_threshold_ = ((vertical_sync_half_lines - 2) * cycles_per_line) >> 1;
|
2015-07-22 22:15:18 +00:00
|
|
|
|
2016-02-28 01:37:41 +00:00
|
|
|
// create the two flywheels
|
2016-11-17 03:00:11 +00:00
|
|
|
horizontal_flywheel_.reset(new Flywheel(multiplied_cycles_per_line, (millisecondsHorizontalRetraceTime * multiplied_cycles_per_line) >> 6, multiplied_cycles_per_line >> 6));
|
|
|
|
vertical_flywheel_.reset(new Flywheel(multiplied_cycles_per_line * height_of_display, scanlinesVerticalRetraceTime * multiplied_cycles_per_line, (multiplied_cycles_per_line * height_of_display) >> 3));
|
2016-02-12 04:43:08 +00:00
|
|
|
|
2016-02-28 01:37:41 +00:00
|
|
|
// figure out the divisor necessary to get the horizontal flywheel into a 16-bit range
|
2016-11-17 03:00:11 +00:00
|
|
|
unsigned int real_clock_scan_period = (multiplied_cycles_per_line * height_of_display) / (time_multiplier_ * common_output_divisor_);
|
2017-10-22 01:50:53 +00:00
|
|
|
vertical_flywheel_output_divider_ = static_cast<uint16_t>(ceilf(real_clock_scan_period / 65536.0f) * (time_multiplier_ * common_output_divisor_));
|
2016-03-09 03:40:23 +00:00
|
|
|
|
2016-11-17 03:00:11 +00:00
|
|
|
openGL_output_builder_.set_timing(cycles_per_line, multiplied_cycles_per_line, height_of_display, horizontal_flywheel_->get_scan_period(), vertical_flywheel_->get_scan_period(), vertical_flywheel_output_divider_);
|
2015-07-31 22:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void CRT::set_new_display_type(unsigned int cycles_per_line, DisplayType displayType) {
|
|
|
|
switch(displayType) {
|
2016-01-23 23:53:20 +00:00
|
|
|
case DisplayType::PAL50:
|
2017-07-29 18:53:53 +00:00
|
|
|
set_new_timing(cycles_per_line, 312, ColourSpace::YUV, 709379, 2500, 5, true); // i.e. 283.7516; 2.5 lines = vertical sync
|
2017-08-10 19:17:08 +00:00
|
|
|
set_input_gamma(2.8f);
|
2016-01-23 23:53:20 +00:00
|
|
|
break;
|
2015-07-27 03:50:43 +00:00
|
|
|
|
2016-01-23 23:53:20 +00:00
|
|
|
case DisplayType::NTSC60:
|
2017-07-29 18:53:53 +00:00
|
|
|
set_new_timing(cycles_per_line, 262, ColourSpace::YIQ, 455, 2, 6, false); // i.e. 227.5, 3 lines = vertical sync
|
2017-08-10 19:17:08 +00:00
|
|
|
set_input_gamma(2.2f);
|
2016-01-23 23:53:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:31:39 +00:00
|
|
|
void CRT::set_composite_function_type(CompositeSourceType type, float offset_of_first_sample) {
|
|
|
|
if(type == DiscreteFourSamplesPerCycle) {
|
2017-10-22 01:50:53 +00:00
|
|
|
colour_burst_phase_adjustment_ = static_cast<uint8_t>(offset_of_first_sample * 256.0f) & 63;
|
2017-05-17 00:31:39 +00:00
|
|
|
} else {
|
|
|
|
colour_burst_phase_adjustment_ = 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 19:17:08 +00:00
|
|
|
void CRT::set_input_gamma(float gamma) {
|
|
|
|
input_gamma_ = gamma;
|
|
|
|
update_gamma();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRT::set_output_gamma(float gamma) {
|
|
|
|
output_gamma_ = gamma;
|
|
|
|
update_gamma();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRT::update_gamma() {
|
2017-08-10 19:36:27 +00:00
|
|
|
float gamma_ratio = input_gamma_ / output_gamma_;
|
2017-08-10 19:17:08 +00:00
|
|
|
openGL_output_builder_.set_gamma(gamma_ratio);
|
|
|
|
}
|
|
|
|
|
2016-11-17 03:00:11 +00:00
|
|
|
CRT::CRT(unsigned int common_output_divisor, unsigned int buffer_depth) :
|
|
|
|
common_output_divisor_(common_output_divisor),
|
2017-10-18 02:34:10 +00:00
|
|
|
openGL_output_builder_(buffer_depth) {}
|
2016-11-17 03:00:11 +00:00
|
|
|
|
2017-07-29 18:53:53 +00:00
|
|
|
CRT::CRT( unsigned int cycles_per_line,
|
|
|
|
unsigned int common_output_divisor,
|
|
|
|
unsigned int height_of_display,
|
|
|
|
ColourSpace colour_space,
|
|
|
|
unsigned int colour_cycle_numerator, unsigned int colour_cycle_denominator,
|
|
|
|
unsigned int vertical_sync_half_lines,
|
|
|
|
bool should_alternate,
|
|
|
|
unsigned int buffer_depth) :
|
2017-03-26 18:34:47 +00:00
|
|
|
CRT(common_output_divisor, buffer_depth) {
|
2017-07-29 18:53:53 +00:00
|
|
|
set_new_timing(cycles_per_line, height_of_display, colour_space, colour_cycle_numerator, colour_cycle_denominator, vertical_sync_half_lines, should_alternate);
|
2016-01-23 23:53:20 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 03:00:11 +00:00
|
|
|
CRT::CRT(unsigned int cycles_per_line, unsigned int common_output_divisor, DisplayType displayType, unsigned int buffer_depth) :
|
2017-03-26 18:34:47 +00:00
|
|
|
CRT(common_output_divisor, buffer_depth) {
|
2016-03-09 03:40:23 +00:00
|
|
|
set_new_display_type(cycles_per_line, displayType);
|
2015-07-20 01:21:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Sync loop
|
2015-07-20 03:43:22 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Flywheel::SyncEvent CRT::get_next_vertical_sync_event(bool vsync_is_requested, unsigned int cycles_to_run_for, unsigned int *cycles_advanced) {
|
2016-11-17 03:00:11 +00:00
|
|
|
return vertical_flywheel_->get_next_event_in_period(vsync_is_requested, cycles_to_run_for, cycles_advanced);
|
2015-07-20 03:43:22 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Flywheel::SyncEvent CRT::get_next_horizontal_sync_event(bool hsync_is_requested, unsigned int cycles_to_run_for, unsigned int *cycles_advanced) {
|
2016-11-17 03:00:11 +00:00
|
|
|
return horizontal_flywheel_->get_next_event_in_period(hsync_is_requested, cycles_to_run_for, cycles_advanced);
|
2015-07-23 22:53:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-22 02:30:15 +00:00
|
|
|
#define output_x1() (*reinterpret_cast<uint16_t *>(&next_output_run[OutputVertexOffsetOfHorizontal + 0]))
|
|
|
|
#define output_x2() (*reinterpret_cast<uint16_t *>(&next_output_run[OutputVertexOffsetOfHorizontal + 2]))
|
|
|
|
#define output_position_y() (*reinterpret_cast<uint16_t *>(&next_output_run[OutputVertexOffsetOfVertical + 0]))
|
|
|
|
#define output_tex_y() (*reinterpret_cast<uint16_t *>(&next_output_run[OutputVertexOffsetOfVertical + 2]))
|
|
|
|
|
|
|
|
#define source_input_position_x1() (*reinterpret_cast<uint16_t *>(&next_run[SourceVertexOffsetOfInputStart + 0]))
|
|
|
|
#define source_input_position_y() (*reinterpret_cast<uint16_t *>(&next_run[SourceVertexOffsetOfInputStart + 2]))
|
|
|
|
#define source_input_position_x2() (*reinterpret_cast<uint16_t *>(&next_run[SourceVertexOffsetOfEnds + 0]))
|
|
|
|
#define source_output_position_x1() (*reinterpret_cast<uint16_t *>(&next_run[SourceVertexOffsetOfOutputStart + 0]))
|
|
|
|
#define source_output_position_y() (*reinterpret_cast<uint16_t *>(&next_run[SourceVertexOffsetOfOutputStart + 2]))
|
|
|
|
#define source_output_position_x2() (*reinterpret_cast<uint16_t *>(&next_run[SourceVertexOffsetOfEnds + 2]))
|
2016-05-10 23:50:12 +00:00
|
|
|
#define source_phase() next_run[SourceVertexOffsetOfPhaseTimeAndAmplitude + 0]
|
2017-07-07 01:29:08 +00:00
|
|
|
#define source_amplitude() next_run[SourceVertexOffsetOfPhaseTimeAndAmplitude + 1]
|
2016-03-05 22:55:18 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void CRT::advance_cycles(unsigned int number_of_cycles, bool hsync_requested, bool vsync_requested, const Scan::Type type) {
|
2016-12-06 23:48:30 +00:00
|
|
|
std::unique_lock<std::mutex> output_lock = openGL_output_builder_.get_output_lock();
|
2016-11-17 03:00:11 +00:00
|
|
|
number_of_cycles *= time_multiplier_;
|
2015-07-25 03:36:44 +00:00
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
bool is_output_run = ((type == Scan::Type::Level) || (type == Scan::Type::Data));
|
2015-07-21 03:18:56 +00:00
|
|
|
|
2015-07-21 01:43:00 +00:00
|
|
|
while(number_of_cycles) {
|
2015-07-21 20:37:39 +00:00
|
|
|
|
2015-08-16 20:08:29 +00:00
|
|
|
unsigned int time_until_vertical_sync_event, time_until_horizontal_sync_event;
|
2016-02-12 04:43:08 +00:00
|
|
|
Flywheel::SyncEvent next_vertical_sync_event = get_next_vertical_sync_event(vsync_requested, number_of_cycles, &time_until_vertical_sync_event);
|
|
|
|
Flywheel::SyncEvent next_horizontal_sync_event = get_next_horizontal_sync_event(hsync_requested, time_until_vertical_sync_event, &time_until_horizontal_sync_event);
|
2015-07-23 22:53:18 +00:00
|
|
|
|
2015-07-21 20:37:39 +00:00
|
|
|
// get the next sync event and its timing; hsync request is instantaneous (being edge triggered) so
|
|
|
|
// set it to false for the next run through this loop (if any)
|
2015-08-16 20:08:29 +00:00
|
|
|
unsigned int next_run_length = std::min(time_until_vertical_sync_event, time_until_horizontal_sync_event);
|
2016-12-10 18:42:34 +00:00
|
|
|
phase_numerator_ += next_run_length * colour_cycle_numerator_;
|
|
|
|
phase_numerator_ %= phase_denominator_;
|
2015-07-20 03:43:22 +00:00
|
|
|
|
2015-08-06 01:12:33 +00:00
|
|
|
hsync_requested = false;
|
|
|
|
vsync_requested = false;
|
2015-08-03 12:42:05 +00:00
|
|
|
|
2016-11-17 03:00:11 +00:00
|
|
|
bool is_output_segment = ((is_output_run && next_run_length) && !horizontal_flywheel_->is_in_retrace() && !vertical_flywheel_->is_in_retrace());
|
2016-03-05 22:55:18 +00:00
|
|
|
uint8_t *next_run = nullptr;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(is_output_segment && !openGL_output_builder_.composite_output_buffer_is_full()) {
|
2017-07-09 21:50:22 +00:00
|
|
|
bool did_retain_source_data = openGL_output_builder_.texture_builder.retain_latest();
|
|
|
|
if(did_retain_source_data) {
|
|
|
|
next_run = openGL_output_builder_.array_builder.get_input_storage(SourceVertexSize);
|
2017-07-09 23:11:38 +00:00
|
|
|
if(!next_run) {
|
|
|
|
openGL_output_builder_.texture_builder.discard_latest();
|
|
|
|
}
|
2017-07-09 21:50:22 +00:00
|
|
|
}
|
2016-03-05 22:55:18 +00:00
|
|
|
}
|
2015-08-02 18:25:21 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(next_run) {
|
2017-07-09 21:50:22 +00:00
|
|
|
// output_y and texture locations will be written later; we won't necessarily know what they are
|
|
|
|
// outside of the locked region
|
2017-10-22 01:50:53 +00:00
|
|
|
source_output_position_x1() = static_cast<uint16_t>(horizontal_flywheel_->get_current_output_position());
|
2017-01-11 13:18:00 +00:00
|
|
|
source_phase() = colour_burst_phase_;
|
2016-11-17 03:00:11 +00:00
|
|
|
source_amplitude() = colour_burst_amplitude_;
|
2015-07-25 03:29:45 +00:00
|
|
|
}
|
2015-07-23 00:33:20 +00:00
|
|
|
|
2016-02-12 04:43:08 +00:00
|
|
|
// decrement the number of cycles left to run for and increment the
|
|
|
|
// horizontal counter appropriately
|
|
|
|
number_of_cycles -= next_run_length;
|
2015-08-02 23:30:45 +00:00
|
|
|
|
2016-02-12 04:43:08 +00:00
|
|
|
// react to the incoming event...
|
2016-11-17 03:00:11 +00:00
|
|
|
horizontal_flywheel_->apply_event(next_run_length, (next_run_length == time_until_horizontal_sync_event) ? next_horizontal_sync_event : Flywheel::SyncEvent::None);
|
|
|
|
vertical_flywheel_->apply_event(next_run_length, (next_run_length == time_until_vertical_sync_event) ? next_vertical_sync_event : Flywheel::SyncEvent::None);
|
2015-07-23 00:33:20 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(next_run) {
|
2017-10-22 01:50:53 +00:00
|
|
|
source_output_position_x2() = static_cast<uint16_t>(horizontal_flywheel_->get_current_output_position());
|
2015-07-20 03:43:22 +00:00
|
|
|
}
|
2015-07-21 01:43:00 +00:00
|
|
|
|
2016-03-05 22:55:18 +00:00
|
|
|
// if this is horizontal retrace then advance the output line counter and bookend an output run
|
2016-05-03 01:05:58 +00:00
|
|
|
Flywheel::SyncEvent honoured_event = Flywheel::SyncEvent::None;
|
|
|
|
if(next_run_length == time_until_vertical_sync_event && next_vertical_sync_event != Flywheel::SyncEvent::None) honoured_event = next_vertical_sync_event;
|
|
|
|
if(next_run_length == time_until_horizontal_sync_event && next_horizontal_sync_event != Flywheel::SyncEvent::None) honoured_event = next_horizontal_sync_event;
|
|
|
|
bool needs_endpoint =
|
2016-11-17 03:00:11 +00:00
|
|
|
(honoured_event == Flywheel::SyncEvent::StartRetrace && is_writing_composite_run_) ||
|
|
|
|
(honoured_event == Flywheel::SyncEvent::EndRetrace && !horizontal_flywheel_->is_in_retrace() && !vertical_flywheel_->is_in_retrace());
|
2016-05-03 01:05:58 +00:00
|
|
|
|
2016-12-10 18:42:34 +00:00
|
|
|
if(next_run_length == time_until_horizontal_sync_event && next_horizontal_sync_event == Flywheel::SyncEvent::StartRetrace) is_alernate_line_ ^= phase_alternates_;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(needs_endpoint) {
|
2016-05-07 22:37:18 +00:00
|
|
|
if(
|
2016-11-17 04:26:04 +00:00
|
|
|
!openGL_output_builder_.array_builder.is_full() &&
|
2017-03-26 18:34:47 +00:00
|
|
|
!openGL_output_builder_.composite_output_buffer_is_full()) {
|
|
|
|
|
|
|
|
if(!is_writing_composite_run_) {
|
2017-10-22 01:50:53 +00:00
|
|
|
output_run_.x1 = static_cast<uint16_t>(horizontal_flywheel_->get_current_output_position());
|
|
|
|
output_run_.y = static_cast<uint16_t>(vertical_flywheel_->get_current_output_position() / vertical_flywheel_output_divider_);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-11-16 03:34:05 +00:00
|
|
|
// Get and write all those previously unwritten output ys
|
2016-12-03 23:19:12 +00:00
|
|
|
const uint16_t output_y = openGL_output_builder_.get_composite_output_y();
|
2016-11-16 03:34:05 +00:00
|
|
|
|
|
|
|
// Construct the output run
|
2017-07-22 00:46:08 +00:00
|
|
|
uint8_t *next_output_run = openGL_output_builder_.array_builder.get_output_storage(OutputVertexSize);
|
|
|
|
if(next_output_run) {
|
2016-11-17 05:37:53 +00:00
|
|
|
output_x1() = output_run_.x1;
|
|
|
|
output_position_y() = output_run_.y;
|
|
|
|
output_tex_y() = output_y;
|
2017-10-22 01:50:53 +00:00
|
|
|
output_x2() = static_cast<uint16_t>(horizontal_flywheel_->get_current_output_position());
|
2016-11-17 05:37:53 +00:00
|
|
|
}
|
2016-12-03 23:19:12 +00:00
|
|
|
openGL_output_builder_.array_builder.flush(
|
2017-11-11 20:28:40 +00:00
|
|
|
[=] (uint8_t *input_buffer, std::size_t input_size, uint8_t *output_buffer, std::size_t output_size) {
|
2016-12-06 12:24:07 +00:00
|
|
|
openGL_output_builder_.texture_builder.flush(
|
2017-11-11 20:28:40 +00:00
|
|
|
[=] (const std::vector<TextureBuilder::WriteArea> &write_areas, std::size_t number_of_write_areas) {
|
2018-01-15 23:37:09 +00:00
|
|
|
// assert(number_of_write_areas * SourceVertexSize == input_size);
|
|
|
|
if(number_of_write_areas * SourceVertexSize == input_size) {
|
|
|
|
for(std::size_t run = 0; run < number_of_write_areas; run++) {
|
|
|
|
*reinterpret_cast<uint16_t *>(&input_buffer[run * SourceVertexSize + SourceVertexOffsetOfInputStart + 0]) = write_areas[run].x;
|
|
|
|
*reinterpret_cast<uint16_t *>(&input_buffer[run * SourceVertexSize + SourceVertexOffsetOfInputStart + 2]) = write_areas[run].y;
|
|
|
|
*reinterpret_cast<uint16_t *>(&input_buffer[run * SourceVertexSize + SourceVertexOffsetOfEnds + 0]) = write_areas[run].x + write_areas[run].length;
|
|
|
|
}
|
2016-12-06 12:24:07 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-11 20:28:40 +00:00
|
|
|
for(std::size_t position = 0; position < input_size; position += SourceVertexSize) {
|
2017-10-22 02:30:15 +00:00
|
|
|
(*reinterpret_cast<uint16_t *>(&input_buffer[position + SourceVertexOffsetOfOutputStart + 2])) = output_y;
|
2016-12-03 23:19:12 +00:00
|
|
|
}
|
|
|
|
});
|
2017-02-06 23:27:44 +00:00
|
|
|
colour_burst_amplitude_ = 0;
|
2016-05-10 23:04:03 +00:00
|
|
|
}
|
2016-11-17 03:00:11 +00:00
|
|
|
is_writing_composite_run_ ^= true;
|
2016-05-05 23:52:05 +00:00
|
|
|
}
|
2016-05-03 01:05:58 +00:00
|
|
|
}
|
2016-03-06 02:18:28 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(next_run_length == time_until_horizontal_sync_event && next_horizontal_sync_event == Flywheel::SyncEvent::StartRetrace) {
|
2016-11-17 03:00:11 +00:00
|
|
|
openGL_output_builder_.increment_composite_output_y();
|
2016-03-05 22:55:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if this is vertical retrace then adcance a field
|
2017-03-26 18:34:47 +00:00
|
|
|
if(next_run_length == time_until_vertical_sync_event && next_vertical_sync_event == Flywheel::SyncEvent::EndRetrace) {
|
|
|
|
if(delegate_) {
|
2016-11-17 03:00:11 +00:00
|
|
|
frames_since_last_delegate_call_++;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(frames_since_last_delegate_call_ == 20) {
|
2017-01-09 00:49:21 +00:00
|
|
|
output_lock.unlock();
|
2016-11-17 03:00:11 +00:00
|
|
|
delegate_->crt_did_end_batch_of_frames(this, frames_since_last_delegate_call_, vertical_flywheel_->get_and_reset_number_of_surprises());
|
2017-01-09 00:49:21 +00:00
|
|
|
output_lock.lock();
|
2016-11-17 03:00:11 +00:00
|
|
|
frames_since_last_delegate_call_ = 0;
|
2016-04-24 22:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-20 03:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 11:47:47 +00:00
|
|
|
#undef output_x1
|
|
|
|
#undef output_x2
|
2016-03-05 22:55:18 +00:00
|
|
|
#undef output_position_y
|
|
|
|
#undef output_tex_y
|
|
|
|
|
2016-05-10 23:50:12 +00:00
|
|
|
#undef source_input_position_x1
|
|
|
|
#undef source_input_position_y
|
|
|
|
#undef source_input_position_x2
|
|
|
|
#undef source_output_position_x1
|
|
|
|
#undef source_output_position_y
|
|
|
|
#undef source_output_position_x2
|
|
|
|
#undef source_phase
|
|
|
|
#undef source_amplitude
|
|
|
|
#undef source_phase_time
|
2016-03-05 22:55:18 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - stream feeding methods
|
2015-07-20 03:43:22 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void CRT::output_scan(const Scan *const scan) {
|
2016-03-06 01:47:11 +00:00
|
|
|
// simplified colour burst logic: if it's within the back porch we'll take it
|
2017-03-26 18:34:47 +00:00
|
|
|
if(scan->type == Scan::Type::ColourBurst) {
|
|
|
|
if(!colour_burst_amplitude_ && horizontal_flywheel_->get_current_time() < (horizontal_flywheel_->get_standard_period() * 12) >> 6) {
|
2017-01-11 13:18:00 +00:00
|
|
|
unsigned int position_phase = (horizontal_flywheel_->get_current_time() * colour_cycle_numerator_ * 256) / phase_denominator_;
|
|
|
|
colour_burst_phase_ = (position_phase + scan->phase) & 255;
|
2016-11-17 03:00:11 +00:00
|
|
|
colour_burst_amplitude_ = scan->amplitude;
|
2017-01-11 13:18:00 +00:00
|
|
|
|
2017-05-17 00:31:39 +00:00
|
|
|
if(colour_burst_phase_adjustment_ != 0xff)
|
|
|
|
colour_burst_phase_ = (colour_burst_phase_ & ~63) + colour_burst_phase_adjustment_;
|
2016-03-06 01:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-29 21:33:52 +00:00
|
|
|
// TODO: inspect raw data for potential colour burst if required; the DPLL and some zero crossing logic
|
|
|
|
// will probably be sufficient but some test data would be helpful
|
|
|
|
|
|
|
|
// sync logic: mark whether this is currently sync and check for a leading edge
|
|
|
|
const bool this_is_sync = (scan->type == Scan::Type::Sync);
|
|
|
|
const bool is_leading_edge = (!is_receiving_sync_ && this_is_sync);
|
|
|
|
is_receiving_sync_ = this_is_sync;
|
2016-03-06 01:47:11 +00:00
|
|
|
|
2017-07-29 21:33:52 +00:00
|
|
|
// horizontal sync is recognised on any leading edge that is not 'near' the expected vertical sync;
|
|
|
|
// the second limb is to avoid slightly horizontal sync shifting from the common pattern of
|
|
|
|
// equalisation pulses as the inverse of ordinary horizontal sync
|
|
|
|
bool hsync_requested = is_leading_edge && !vertical_flywheel_->is_near_expected_sync();
|
|
|
|
|
|
|
|
if(this_is_sync) {
|
|
|
|
// if this is sync then either begin or continue a sync accumulation phase
|
|
|
|
is_accumulating_sync_ = true;
|
2017-07-29 22:25:04 +00:00
|
|
|
cycles_since_sync_ = 0;
|
2017-07-29 21:33:52 +00:00
|
|
|
} else {
|
|
|
|
// if this is not sync then check how long it has been since sync. If it's more than
|
|
|
|
// half a line then end sync accumulation and zero out the accumulating count
|
|
|
|
cycles_since_sync_ += scan->number_of_cycles;
|
2017-07-29 22:25:04 +00:00
|
|
|
if(cycles_since_sync_ > (cycles_per_line_ >> 2)) {
|
2017-07-29 21:33:52 +00:00
|
|
|
cycles_of_sync_ = 0;
|
|
|
|
is_accumulating_sync_ = false;
|
2017-07-29 22:25:04 +00:00
|
|
|
is_refusing_sync_ = false;
|
2017-07-29 21:33:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int number_of_cycles = scan->number_of_cycles;
|
|
|
|
bool vsync_requested = false;
|
|
|
|
|
|
|
|
// if sync is being accumulated then accumulate it; if it crosses the vertical sync threshold then
|
|
|
|
// divide this line at the crossing point and indicate vertical sync there
|
2017-07-29 22:25:04 +00:00
|
|
|
if(is_accumulating_sync_ && !is_refusing_sync_) {
|
2017-07-29 21:33:52 +00:00
|
|
|
cycles_of_sync_ += scan->number_of_cycles;
|
|
|
|
|
|
|
|
if(this_is_sync && cycles_of_sync_ >= sync_capacitor_charge_threshold_) {
|
2017-07-29 22:25:04 +00:00
|
|
|
unsigned int overshoot = std::min(cycles_of_sync_ - sync_capacitor_charge_threshold_, number_of_cycles);
|
|
|
|
if(overshoot) {
|
|
|
|
number_of_cycles -= overshoot;
|
|
|
|
advance_cycles(number_of_cycles, hsync_requested, false, scan->type);
|
|
|
|
hsync_requested = false;
|
|
|
|
number_of_cycles = overshoot;
|
|
|
|
}
|
2017-07-29 21:33:52 +00:00
|
|
|
|
2017-07-30 01:37:59 +00:00
|
|
|
is_refusing_sync_ = true;
|
2017-07-29 21:33:52 +00:00
|
|
|
vsync_requested = true;
|
|
|
|
}
|
|
|
|
}
|
2016-03-06 01:47:11 +00:00
|
|
|
|
2017-07-29 21:33:52 +00:00
|
|
|
advance_cycles(number_of_cycles, hsync_requested, vsync_requested, scan->type);
|
2016-01-23 22:44:34 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 20:37:39 +00:00
|
|
|
/*
|
|
|
|
These all merely channel into advance_cycles, supplying appropriate arguments
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
void CRT::output_sync(unsigned int number_of_cycles) {
|
2017-11-10 03:04:49 +00:00
|
|
|
Scan scan;
|
|
|
|
scan.type = Scan::Type::Sync;
|
|
|
|
scan.number_of_cycles = number_of_cycles;
|
2016-03-05 22:55:18 +00:00
|
|
|
output_scan(&scan);
|
2015-07-19 17:36:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void CRT::output_blank(unsigned int number_of_cycles) {
|
2017-11-10 03:04:49 +00:00
|
|
|
Scan scan;
|
|
|
|
scan.type = Scan::Type::Blank;
|
|
|
|
scan.number_of_cycles = number_of_cycles;
|
2016-03-05 22:55:18 +00:00
|
|
|
output_scan(&scan);
|
2015-07-19 17:36:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void CRT::output_level(unsigned int number_of_cycles) {
|
2017-07-08 02:25:05 +00:00
|
|
|
openGL_output_builder_.texture_builder.reduce_previous_allocation_to(1);
|
2017-11-10 03:04:49 +00:00
|
|
|
Scan scan;
|
|
|
|
scan.type = Scan::Type::Level;
|
|
|
|
scan.number_of_cycles = number_of_cycles;
|
2016-12-06 23:48:30 +00:00
|
|
|
output_scan(&scan);
|
2016-01-23 22:44:34 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void CRT::output_colour_burst(unsigned int number_of_cycles, uint8_t phase, uint8_t amplitude) {
|
2017-11-10 03:04:49 +00:00
|
|
|
Scan scan;
|
|
|
|
scan.type = Scan::Type::ColourBurst;
|
|
|
|
scan.number_of_cycles = number_of_cycles;
|
|
|
|
scan.phase = phase;
|
|
|
|
scan.amplitude = amplitude;
|
2016-03-05 22:55:18 +00:00
|
|
|
output_scan(&scan);
|
2015-07-21 01:43:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void CRT::output_default_colour_burst(unsigned int number_of_cycles) {
|
2017-10-22 01:50:53 +00:00
|
|
|
output_colour_burst(number_of_cycles, static_cast<uint8_t>((phase_numerator_ * 256) / phase_denominator_ + (is_alernate_line_ ? 128 : 0)));
|
2016-12-10 18:42:34 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void CRT::output_data(unsigned int number_of_cycles, unsigned int source_divider) {
|
2016-12-06 23:48:30 +00:00
|
|
|
openGL_output_builder_.texture_builder.reduce_previous_allocation_to(number_of_cycles / source_divider);
|
2017-11-10 03:04:49 +00:00
|
|
|
Scan scan;
|
|
|
|
scan.type = Scan::Type::Data;
|
|
|
|
scan.number_of_cycles = number_of_cycles;
|
2016-12-06 23:48:30 +00:00
|
|
|
output_scan(&scan);
|
2015-07-19 17:36:27 +00:00
|
|
|
}
|
2016-04-12 01:47:23 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Outputs::CRT::Rect CRT::get_rect_for_area(int first_line_after_sync, int number_of_lines, int first_cycle_after_sync, int number_of_cycles, float aspect_ratio) {
|
2016-11-17 03:00:11 +00:00
|
|
|
first_cycle_after_sync *= time_multiplier_;
|
|
|
|
number_of_cycles *= time_multiplier_;
|
2016-11-16 03:53:15 +00:00
|
|
|
|
|
|
|
first_line_after_sync -= 2;
|
|
|
|
number_of_lines += 4;
|
2016-04-12 01:47:23 +00:00
|
|
|
|
|
|
|
// determine prima facie x extent
|
2016-11-17 03:00:11 +00:00
|
|
|
unsigned int horizontal_period = horizontal_flywheel_->get_standard_period();
|
|
|
|
unsigned int horizontal_scan_period = horizontal_flywheel_->get_scan_period();
|
2016-04-12 01:47:23 +00:00
|
|
|
unsigned int horizontal_retrace_period = horizontal_period - horizontal_scan_period;
|
|
|
|
|
2016-06-14 11:29:35 +00:00
|
|
|
// make sure that the requested range is visible
|
2017-10-22 01:00:40 +00:00
|
|
|
if(static_cast<unsigned int>(first_cycle_after_sync) < horizontal_retrace_period) first_cycle_after_sync = static_cast<int>(horizontal_retrace_period);
|
|
|
|
if(static_cast<unsigned int>(first_cycle_after_sync + number_of_cycles) > horizontal_scan_period) number_of_cycles = static_cast<int>(horizontal_scan_period - static_cast<unsigned int>(first_cycle_after_sync));
|
2016-06-14 11:29:35 +00:00
|
|
|
|
2017-10-22 01:00:40 +00:00
|
|
|
float start_x = static_cast<float>(static_cast<unsigned int>(first_cycle_after_sync) - horizontal_retrace_period) / static_cast<float>(horizontal_scan_period);
|
2017-10-21 23:49:04 +00:00
|
|
|
float width = static_cast<float>(number_of_cycles) / static_cast<float>(horizontal_scan_period);
|
2016-04-12 01:47:23 +00:00
|
|
|
|
|
|
|
// determine prima facie y extent
|
2016-11-17 03:00:11 +00:00
|
|
|
unsigned int vertical_period = vertical_flywheel_->get_standard_period();
|
|
|
|
unsigned int vertical_scan_period = vertical_flywheel_->get_scan_period();
|
2016-04-12 01:47:23 +00:00
|
|
|
unsigned int vertical_retrace_period = vertical_period - vertical_scan_period;
|
2016-06-14 11:29:35 +00:00
|
|
|
|
|
|
|
// make sure that the requested range is visible
|
2017-10-22 01:00:40 +00:00
|
|
|
// if(static_cast<unsigned int>(first_line_after_sync) * horizontal_period < vertical_retrace_period)
|
2016-06-14 11:29:35 +00:00
|
|
|
// first_line_after_sync = (vertical_retrace_period + horizontal_period - 1) / horizontal_period;
|
|
|
|
// if((first_line_after_sync + number_of_lines) * horizontal_period > vertical_scan_period)
|
2017-10-22 01:00:40 +00:00
|
|
|
// number_of_lines = static_cast<int>(horizontal_scan_period - static_cast<unsigned int>(first_cycle_after_sync));
|
2016-06-14 11:29:35 +00:00
|
|
|
|
2017-10-22 01:00:40 +00:00
|
|
|
float start_y = static_cast<float>((static_cast<unsigned int>(first_line_after_sync) * horizontal_period) - vertical_retrace_period) / static_cast<float>(vertical_scan_period);
|
|
|
|
float height = static_cast<float>(static_cast<unsigned int>(number_of_lines) * horizontal_period) / vertical_scan_period;
|
2016-04-12 01:47:23 +00:00
|
|
|
|
|
|
|
// adjust to ensure aspect ratio is correct
|
|
|
|
float adjusted_aspect_ratio = (3.0f*aspect_ratio / 4.0f);
|
|
|
|
float ideal_width = height * adjusted_aspect_ratio;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(ideal_width > width) {
|
2016-04-12 01:47:23 +00:00
|
|
|
start_x -= (ideal_width - width) * 0.5f;
|
|
|
|
width = ideal_width;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-04-12 01:47:23 +00:00
|
|
|
float ideal_height = width / adjusted_aspect_ratio;
|
|
|
|
start_y -= (ideal_height - height) * 0.5f;
|
|
|
|
height = ideal_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Rect(start_x, start_y, width, height);
|
|
|
|
}
|