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"
|
2016-02-14 01:52:23 +00:00
|
|
|
#include "CRTOpenGL.hpp"
|
2015-07-20 01:21:34 +00:00
|
|
|
#include <stdarg.h>
|
2015-07-27 03:50:43 +00:00
|
|
|
#include <math.h>
|
2016-04-27 01:41:39 +00:00
|
|
|
#include <algorithm>
|
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
|
|
|
|
2016-02-07 22:32:38 +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)
|
2015-07-19 17:36:27 +00:00
|
|
|
{
|
2016-03-09 03:40:23 +00:00
|
|
|
_openGL_output_builder->set_colour_format(colour_space, colour_cycle_numerator, colour_cycle_denominator);
|
2016-02-07 22:32:38 +00:00
|
|
|
|
2016-04-11 01:34:40 +00:00
|
|
|
const unsigned int syncCapacityLineChargeThreshold = 2;
|
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-04-29 02:12:59 +00:00
|
|
|
_time_multiplier = IntermediateBufferWidth / cycles_per_line;
|
2015-07-25 03:36:44 +00:00
|
|
|
|
2015-07-21 20:37:39 +00:00
|
|
|
// store fundamental display configuration properties
|
2016-02-12 04:43:08 +00:00
|
|
|
_height_of_display = height_of_display;
|
2015-07-25 03:36:44 +00:00
|
|
|
_cycles_per_line = cycles_per_line * _time_multiplier;
|
2015-07-21 03:18:56 +00:00
|
|
|
|
2015-07-21 20:37:39 +00:00
|
|
|
// generate timing values implied by the given arbuments
|
2016-04-11 01:34:40 +00:00
|
|
|
_sync_capacitor_charge_threshold = (int)(syncCapacityLineChargeThreshold * _cycles_per_line);
|
2015-07-22 22:15:18 +00:00
|
|
|
|
2016-02-28 01:37:41 +00:00
|
|
|
// create the two flywheels
|
2016-03-09 01:49:07 +00:00
|
|
|
_horizontal_flywheel = std::unique_ptr<Flywheel>(new Flywheel(_cycles_per_line, (millisecondsHorizontalRetraceTime * _cycles_per_line) >> 6));
|
|
|
|
_vertical_flywheel = std::unique_ptr<Flywheel>(new Flywheel(_cycles_per_line * height_of_display, scanlinesVerticalRetraceTime * _cycles_per_line));
|
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-02-28 03:39:01 +00:00
|
|
|
unsigned int real_clock_scan_period = (_cycles_per_line * height_of_display) / (_time_multiplier * _common_output_divisor);
|
|
|
|
_vertical_flywheel_output_divider = (uint16_t)(ceilf(real_clock_scan_period / 65536.0f) * (_time_multiplier * _common_output_divisor));
|
2016-03-09 03:40:23 +00:00
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
_openGL_output_builder->set_timing(cycles_per_line, _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
|
|
|
}
|
|
|
|
|
2016-01-23 23:53:20 +00:00
|
|
|
void CRT::set_new_display_type(unsigned int cycles_per_line, DisplayType displayType)
|
2015-07-31 22:04:33 +00:00
|
|
|
{
|
2016-01-23 23:53:20 +00:00
|
|
|
switch(displayType)
|
|
|
|
{
|
|
|
|
case DisplayType::PAL50:
|
2016-02-07 22:32:38 +00:00
|
|
|
set_new_timing(cycles_per_line, 312, ColourSpace::YUV, 1135, 4);
|
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:
|
2016-02-07 22:32:38 +00:00
|
|
|
set_new_timing(cycles_per_line, 262, ColourSpace::YIQ, 545, 2);
|
2016-01-23 23:53:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 03:39:01 +00:00
|
|
|
CRT::CRT(unsigned int common_output_divisor) :
|
2016-01-23 23:53:20 +00:00
|
|
|
_sync_capacitor_charge_level(0),
|
|
|
|
_is_receiving_sync(false),
|
2016-02-28 03:39:01 +00:00
|
|
|
_sync_period(0),
|
2016-03-05 22:55:18 +00:00
|
|
|
_common_output_divisor(common_output_divisor),
|
2016-04-24 22:36:22 +00:00
|
|
|
_is_writing_composite_run(false),
|
|
|
|
_delegate(nullptr),
|
2016-05-10 23:04:03 +00:00
|
|
|
_frames_since_last_delegate_call(0) {}
|
2016-02-14 01:52:23 +00:00
|
|
|
|
2016-03-19 21:37:55 +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 buffer_depth) : CRT(common_output_divisor)
|
2016-01-23 23:53:20 +00:00
|
|
|
{
|
2016-03-19 21:37:55 +00:00
|
|
|
_openGL_output_builder = std::unique_ptr<OpenGLOutputBuilder>(new OpenGLOutputBuilder(buffer_depth));
|
2016-03-09 03:40:23 +00:00
|
|
|
set_new_timing(cycles_per_line, height_of_display, colour_space, colour_cycle_numerator, colour_cycle_denominator);
|
2016-01-23 23:53:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 21:37:55 +00:00
|
|
|
CRT::CRT(unsigned int cycles_per_line, unsigned int common_output_divisor, DisplayType displayType, unsigned int buffer_depth) : CRT(common_output_divisor)
|
2016-01-23 23:53:20 +00:00
|
|
|
{
|
2016-03-19 21:37:55 +00:00
|
|
|
_openGL_output_builder = std::unique_ptr<OpenGLOutputBuilder>(new OpenGLOutputBuilder(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
|
|
|
}
|
|
|
|
|
2015-07-21 01:43:00 +00:00
|
|
|
#pragma mark - Sync loop
|
2015-07-20 03:43:22 +00:00
|
|
|
|
2016-02-12 04:43:08 +00:00
|
|
|
Flywheel::SyncEvent CRT::get_next_vertical_sync_event(bool vsync_is_requested, unsigned int cycles_to_run_for, unsigned int *cycles_advanced)
|
2015-07-20 03:43:22 +00:00
|
|
|
{
|
2016-02-12 04:43:08 +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
|
|
|
}
|
|
|
|
|
2016-02-12 04:43:08 +00:00
|
|
|
Flywheel::SyncEvent CRT::get_next_horizontal_sync_event(bool hsync_is_requested, unsigned int cycles_to_run_for, unsigned int *cycles_advanced)
|
2015-07-23 22:53:18 +00:00
|
|
|
{
|
2016-02-12 04:43:08 +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
|
|
|
}
|
|
|
|
|
2016-05-10 11:47:47 +00:00
|
|
|
#define output_x1() (*(uint16_t *)&next_run[OutputVertexOffsetOfHorizontal + 0])
|
|
|
|
#define output_x2() (*(uint16_t *)&next_run[OutputVertexOffsetOfHorizontal + 2])
|
|
|
|
#define output_position_y() (*(uint16_t *)&next_run[OutputVertexOffsetOfVertical + 0])
|
|
|
|
#define output_tex_y() (*(uint16_t *)&next_run[OutputVertexOffsetOfVertical + 2])
|
2016-03-09 01:49:07 +00:00
|
|
|
|
2016-05-10 23:50:12 +00:00
|
|
|
#define source_input_position_x1() (*(uint16_t *)&next_run[SourceVertexOffsetOfInputStart + 0])
|
|
|
|
#define source_input_position_y() (*(uint16_t *)&next_run[SourceVertexOffsetOfInputStart + 2])
|
|
|
|
#define source_input_position_x2() (*(uint16_t *)&next_run[SourceVertexOffsetOfEnds + 0])
|
|
|
|
#define source_output_position_x1() (*(uint16_t *)&next_run[SourceVertexOffsetOfOutputStart + 0])
|
|
|
|
#define source_output_position_y() (*(uint16_t *)&next_run[SourceVertexOffsetOfOutputStart + 2])
|
|
|
|
#define source_output_position_x2() (*(uint16_t *)&next_run[SourceVertexOffsetOfEnds + 2])
|
|
|
|
#define source_phase() next_run[SourceVertexOffsetOfPhaseTimeAndAmplitude + 0]
|
|
|
|
#define source_amplitude() next_run[SourceVertexOffsetOfPhaseTimeAndAmplitude + 2]
|
|
|
|
#define source_phase_time() next_run[SourceVertexOffsetOfPhaseTimeAndAmplitude + 1]
|
2016-03-05 22:55:18 +00:00
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divider, bool hsync_requested, bool vsync_requested, const bool vsync_charging, const Scan::Type type, uint16_t tex_x, uint16_t tex_y)
|
2015-07-20 03:43:22 +00:00
|
|
|
{
|
2015-07-25 03:36:44 +00:00
|
|
|
number_of_cycles *= _time_multiplier;
|
|
|
|
|
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);
|
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-03-05 22:55:18 +00:00
|
|
|
bool is_output_segment = ((is_output_run && next_run_length) && !_horizontal_flywheel->is_in_retrace() && !_vertical_flywheel->is_in_retrace());
|
|
|
|
uint8_t *next_run = nullptr;
|
2016-05-05 23:52:05 +00:00
|
|
|
if(is_output_segment && !_openGL_output_builder->composite_output_buffer_is_full())
|
2016-03-05 22:55:18 +00:00
|
|
|
{
|
2016-05-03 01:05:58 +00:00
|
|
|
next_run = _openGL_output_builder->get_next_source_run();
|
2016-03-05 22:55:18 +00:00
|
|
|
}
|
2015-08-02 18:25:21 +00:00
|
|
|
|
2015-07-25 03:29:45 +00:00
|
|
|
if(next_run)
|
2015-07-23 22:53:18 +00:00
|
|
|
{
|
2016-05-10 23:50:12 +00:00
|
|
|
source_input_position_x1() = tex_x;
|
|
|
|
source_input_position_y() = tex_y;
|
|
|
|
source_output_position_x1() = (uint16_t)_horizontal_flywheel->get_current_output_position();
|
|
|
|
source_output_position_y() = _openGL_output_builder->get_composite_output_y();
|
|
|
|
source_phase() = _colour_burst_phase;
|
|
|
|
source_amplitude() = _colour_burst_amplitude;
|
|
|
|
source_phase_time() = (uint8_t)_colour_burst_time; // assumption: burst was within the first 1/16 of the line
|
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
|
|
|
// either charge or deplete the vertical retrace capacitor (making sure it stops at 0)
|
2016-04-25 02:32:24 +00:00
|
|
|
if(vsync_charging)
|
2016-02-12 04:43:08 +00:00
|
|
|
_sync_capacitor_charge_level += next_run_length;
|
2015-07-25 03:29:45 +00:00
|
|
|
else
|
2016-02-12 04:43:08 +00:00
|
|
|
_sync_capacitor_charge_level = std::max(_sync_capacitor_charge_level - (int)next_run_length, 0);
|
2015-07-23 00:33:20 +00:00
|
|
|
|
2016-02-12 04:43:08 +00:00
|
|
|
// react to the incoming event...
|
|
|
|
_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
|
|
|
|
2015-07-25 03:29:45 +00:00
|
|
|
if(next_run)
|
|
|
|
{
|
2015-07-23 00:33:20 +00:00
|
|
|
// if this is a data run then advance the buffer pointer
|
2016-03-09 03:40:23 +00:00
|
|
|
if(type == Scan::Type::Data && source_divider) tex_x += next_run_length / (_time_multiplier * source_divider);
|
2015-07-23 00:33:20 +00:00
|
|
|
|
2016-05-10 23:50:12 +00:00
|
|
|
source_input_position_x2() = tex_x;
|
|
|
|
source_output_position_x2() = (uint16_t)_horizontal_flywheel->get_current_output_position();
|
2016-04-11 02:56:52 +00:00
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
_openGL_output_builder->complete_source_run();
|
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 =
|
|
|
|
(honoured_event == Flywheel::SyncEvent::StartRetrace && _is_writing_composite_run) ||
|
|
|
|
(honoured_event == Flywheel::SyncEvent::EndRetrace && !_horizontal_flywheel->is_in_retrace() && !_vertical_flywheel->is_in_retrace());
|
|
|
|
|
|
|
|
if(needs_endpoint)
|
2016-03-05 22:55:18 +00:00
|
|
|
{
|
2016-05-07 22:37:18 +00:00
|
|
|
if(
|
2016-05-10 11:23:47 +00:00
|
|
|
_openGL_output_builder->composite_output_run_has_room_for_vertex() &&
|
2016-05-09 00:30:59 +00:00
|
|
|
!_openGL_output_builder->composite_output_buffer_is_full())
|
2016-05-05 23:52:05 +00:00
|
|
|
{
|
2016-05-10 23:04:03 +00:00
|
|
|
if(!_is_writing_composite_run)
|
2016-05-07 22:37:18 +00:00
|
|
|
{
|
2016-05-10 23:04:03 +00:00
|
|
|
_output_run.x1 = (uint16_t)_horizontal_flywheel->get_current_output_position();
|
|
|
|
_output_run.y = (uint16_t)(_vertical_flywheel->get_current_output_position() / _vertical_flywheel_output_divider);
|
|
|
|
_output_run.tex_y = _openGL_output_builder->get_composite_output_y();
|
2016-05-07 22:37:18 +00:00
|
|
|
}
|
2016-05-10 23:04:03 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
uint8_t *next_run = _openGL_output_builder->get_next_output_run();
|
|
|
|
output_x1() = _output_run.x1;
|
|
|
|
output_position_y() = _output_run.y;
|
|
|
|
output_tex_y() = _output_run.tex_y;
|
|
|
|
output_x2() = (uint16_t)_horizontal_flywheel->get_current_output_position();
|
|
|
|
_openGL_output_builder->complete_output_run();
|
|
|
|
}
|
|
|
|
_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
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
if(next_run_length == time_until_horizontal_sync_event && next_horizontal_sync_event == Flywheel::SyncEvent::StartRetrace)
|
|
|
|
{
|
|
|
|
_openGL_output_builder->increment_composite_output_y();
|
2016-03-05 22:55:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if this is vertical retrace then adcance a field
|
2016-02-12 04:43:08 +00:00
|
|
|
if(next_run_length == time_until_vertical_sync_event && next_vertical_sync_event == Flywheel::SyncEvent::EndRetrace)
|
2015-07-23 22:53:18 +00:00
|
|
|
{
|
2016-04-24 22:36:22 +00:00
|
|
|
if(_delegate)
|
|
|
|
{
|
|
|
|
_frames_since_last_delegate_call++;
|
|
|
|
if(_frames_since_last_delegate_call == 100)
|
|
|
|
{
|
|
|
|
_delegate->crt_did_end_batch_of_frames(this, _frames_since_last_delegate_call, _vertical_flywheel->get_and_reset_number_of_surprises());
|
|
|
|
_frames_since_last_delegate_call = 0;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2015-07-20 03:43:22 +00:00
|
|
|
#pragma mark - stream feeding methods
|
|
|
|
|
2016-03-09 03:54:05 +00:00
|
|
|
void CRT::output_scan(const Scan *const scan)
|
2016-01-23 22:44:34 +00:00
|
|
|
{
|
2016-03-09 03:54:05 +00:00
|
|
|
const bool this_is_sync = (scan->type == Scan::Type::Sync);
|
|
|
|
const bool is_trailing_edge = (_is_receiving_sync && !this_is_sync);
|
2016-04-11 01:34:40 +00:00
|
|
|
const bool is_leading_edge = (!_is_receiving_sync && this_is_sync);
|
2016-01-23 22:44:34 +00:00
|
|
|
_is_receiving_sync = this_is_sync;
|
|
|
|
|
2016-04-15 00:30:45 +00:00
|
|
|
// This introduces a blackout period close to the expected vertical sync point in which horizontal syncs are not
|
|
|
|
// recognised, effectively causing the horizontal flywheel to freewheel during that period. This attempts to seek
|
|
|
|
// the problem that vertical sync otherwise often starts halfway through a scanline, which confuses the horizontal
|
|
|
|
// flywheel. I'm currently unclear whether this is an accurate solution to this problem.
|
|
|
|
const bool hsync_requested = is_leading_edge && !_vertical_flywheel->is_near_expected_sync();
|
2016-03-09 03:54:05 +00:00
|
|
|
const bool vsync_requested = is_trailing_edge && (_sync_capacitor_charge_level >= _sync_capacitor_charge_threshold);
|
|
|
|
|
2016-03-06 01:47:11 +00:00
|
|
|
// simplified colour burst logic: if it's within the back porch we'll take it
|
2016-03-09 03:40:23 +00:00
|
|
|
if(scan->type == Scan::Type::ColourBurst)
|
2016-03-06 01:47:11 +00:00
|
|
|
{
|
|
|
|
if(_horizontal_flywheel->get_current_time() < (_horizontal_flywheel->get_standard_period() * 12) >> 6)
|
|
|
|
{
|
2016-04-26 00:56:15 +00:00
|
|
|
_colour_burst_time = (uint16_t)_horizontal_flywheel->get_current_time();
|
2016-03-06 01:47:11 +00:00
|
|
|
_colour_burst_phase = scan->phase;
|
|
|
|
_colour_burst_amplitude = scan->amplitude;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: inspect raw data for potential colour burst if required
|
|
|
|
|
2016-02-21 01:29:40 +00:00
|
|
|
_sync_period = _is_receiving_sync ? (_sync_period + scan->number_of_cycles) : 0;
|
2016-01-23 22:49:25 +00:00
|
|
|
advance_cycles(scan->number_of_cycles, scan->source_divider, hsync_requested, vsync_requested, this_is_sync, scan->type, scan->tex_x, scan->tex_y);
|
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
|
|
|
|
*/
|
2015-08-16 20:08:29 +00:00
|
|
|
void CRT::output_sync(unsigned int number_of_cycles)
|
2015-07-19 17:36:27 +00:00
|
|
|
{
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->lock_output();
|
2016-03-05 22:55:18 +00:00
|
|
|
Scan scan{
|
2016-03-09 03:40:23 +00:00
|
|
|
.type = Scan::Type::Sync,
|
2016-03-05 22:55:18 +00:00
|
|
|
.number_of_cycles = number_of_cycles
|
|
|
|
};
|
|
|
|
output_scan(&scan);
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->unlock_output();
|
2015-07-19 17:36:27 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 20:08:29 +00:00
|
|
|
void CRT::output_blank(unsigned int number_of_cycles)
|
2015-07-19 17:36:27 +00:00
|
|
|
{
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->lock_output();
|
2016-03-05 22:55:18 +00:00
|
|
|
Scan scan {
|
2016-03-09 03:40:23 +00:00
|
|
|
.type = Scan::Type::Blank,
|
2016-03-05 22:55:18 +00:00
|
|
|
.number_of_cycles = number_of_cycles
|
|
|
|
};
|
|
|
|
output_scan(&scan);
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->unlock_output();
|
2015-07-19 17:36:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 00:25:30 +00:00
|
|
|
void CRT::output_level(unsigned int number_of_cycles)
|
2015-07-19 17:36:27 +00:00
|
|
|
{
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->lock_output();
|
2016-05-09 11:02:12 +00:00
|
|
|
if(!_openGL_output_builder->input_buffer_is_full())
|
|
|
|
{
|
|
|
|
Scan scan {
|
|
|
|
.type = Scan::Type::Level,
|
|
|
|
.number_of_cycles = number_of_cycles,
|
|
|
|
.tex_x = _openGL_output_builder->get_last_write_x_posititon(),
|
|
|
|
.tex_y = _openGL_output_builder->get_last_write_y_posititon()
|
|
|
|
};
|
|
|
|
output_scan(&scan);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Scan scan {
|
|
|
|
.type = Scan::Type::Blank,
|
|
|
|
.number_of_cycles = number_of_cycles
|
|
|
|
};
|
|
|
|
output_scan(&scan);
|
|
|
|
}
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->unlock_output();
|
2016-01-23 22:44:34 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 01:47:11 +00:00
|
|
|
void CRT::output_colour_burst(unsigned int number_of_cycles, uint8_t phase, uint8_t amplitude)
|
2016-01-23 22:44:34 +00:00
|
|
|
{
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->lock_output();
|
2016-03-05 22:55:18 +00:00
|
|
|
Scan scan {
|
2016-03-09 03:40:23 +00:00
|
|
|
.type = Scan::Type::ColourBurst,
|
2016-03-05 22:55:18 +00:00
|
|
|
.number_of_cycles = number_of_cycles,
|
|
|
|
.phase = phase,
|
2016-03-06 01:47:11 +00:00
|
|
|
.amplitude = amplitude
|
2016-03-05 22:55:18 +00:00
|
|
|
};
|
|
|
|
output_scan(&scan);
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->unlock_output();
|
2015-07-21 01:43:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:17:47 +00:00
|
|
|
void CRT::output_data(unsigned int number_of_cycles, unsigned int source_divider)
|
2015-07-21 01:43:00 +00:00
|
|
|
{
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->lock_output();
|
2016-05-09 11:02:12 +00:00
|
|
|
if(!_openGL_output_builder->input_buffer_is_full())
|
2016-05-04 11:39:45 +00:00
|
|
|
{
|
2016-05-09 11:02:12 +00:00
|
|
|
_openGL_output_builder->reduce_previous_allocation_to(number_of_cycles / source_divider);
|
2016-05-04 11:39:45 +00:00
|
|
|
Scan scan {
|
|
|
|
.type = Scan::Type::Data,
|
|
|
|
.number_of_cycles = number_of_cycles,
|
|
|
|
.tex_x = _openGL_output_builder->get_last_write_x_posititon(),
|
|
|
|
.tex_y = _openGL_output_builder->get_last_write_y_posititon(),
|
|
|
|
.source_divider = source_divider
|
|
|
|
};
|
|
|
|
output_scan(&scan);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-09 11:02:12 +00:00
|
|
|
Scan scan {
|
|
|
|
.type = Scan::Type::Blank,
|
|
|
|
.number_of_cycles = number_of_cycles
|
|
|
|
};
|
|
|
|
output_scan(&scan);
|
2016-05-04 11:39:45 +00:00
|
|
|
}
|
2016-05-09 10:58:26 +00:00
|
|
|
_openGL_output_builder->unlock_output();
|
2015-07-19 17:36:27 +00:00
|
|
|
}
|
2016-04-12 01:47:23 +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)
|
|
|
|
{
|
|
|
|
first_cycle_after_sync *= _time_multiplier;
|
|
|
|
number_of_cycles *= _time_multiplier;
|
2016-04-12 03:13:14 +00:00
|
|
|
number_of_lines++;
|
2016-04-12 01:47:23 +00:00
|
|
|
|
|
|
|
// determine prima facie x extent
|
|
|
|
unsigned int horizontal_period = _horizontal_flywheel->get_standard_period();
|
|
|
|
unsigned int horizontal_scan_period = _horizontal_flywheel->get_scan_period();
|
|
|
|
unsigned int horizontal_retrace_period = horizontal_period - horizontal_scan_period;
|
|
|
|
|
|
|
|
float start_x = (float)((unsigned)first_cycle_after_sync - horizontal_retrace_period) / (float)horizontal_scan_period;
|
|
|
|
float width = (float)number_of_cycles / (float)horizontal_scan_period;
|
|
|
|
|
|
|
|
// determine prima facie y extent
|
|
|
|
unsigned int vertical_period = _vertical_flywheel->get_standard_period();
|
|
|
|
unsigned int vertical_scan_period = _vertical_flywheel->get_scan_period();
|
|
|
|
unsigned int vertical_retrace_period = vertical_period - vertical_scan_period;
|
|
|
|
float start_y = (float)(((unsigned)first_line_after_sync * horizontal_period) - vertical_retrace_period) / (float)vertical_scan_period;
|
|
|
|
float height = (float)((unsigned)number_of_lines * horizontal_period) / vertical_scan_period;
|
|
|
|
|
|
|
|
// adjust to ensure aspect ratio is correct
|
|
|
|
float adjusted_aspect_ratio = (3.0f*aspect_ratio / 4.0f);
|
|
|
|
float ideal_width = height * adjusted_aspect_ratio;
|
|
|
|
if(ideal_width > width)
|
|
|
|
{
|
|
|
|
start_x -= (ideal_width - width) * 0.5f;
|
|
|
|
width = ideal_width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|