mirror of
https://github.com/TomHarte/CLK.git
synced 2025-01-11 08:30:55 +00:00
Made some minor attempt at proper sync response. I think I've gone way off piste and overcomplicated it.
This commit is contained in:
parent
4a1e9fe2a8
commit
4e4c082a05
@ -86,7 +86,7 @@ void Machine::output_state(OutputState state, uint8_t *pixel)
|
||||
_outputBuffer[3] = 0xff;
|
||||
_crt->output_level(_lastOutputStateDuration, "Atari2600");
|
||||
} break;
|
||||
case OutputState::Sync: _crt->output_sync(_lastOutputStateDuration); break;
|
||||
case OutputState::Sync: _crt->output_sync(_lastOutputStateDuration); break;
|
||||
case OutputState::Pixel: _crt->output_data(_lastOutputStateDuration, "Atari2600"); break;
|
||||
}
|
||||
_lastOutputStateDuration = 0;
|
||||
|
150
Outputs/CRT.cpp
150
Outputs/CRT.cpp
@ -12,6 +12,9 @@
|
||||
static const int bufferWidth = 512;
|
||||
static const int bufferHeight = 512;
|
||||
|
||||
static const int syncCapacityLineChargeThreshold = 3;
|
||||
static const int millisecondsHorizontalRetraceTime = 16;
|
||||
|
||||
using namespace Outputs;
|
||||
|
||||
CRT::CRT(int cycles_per_line, int number_of_buffers, ...)
|
||||
@ -33,6 +36,15 @@ CRT::CRT(int cycles_per_line, int number_of_buffers, ...)
|
||||
va_end(va);
|
||||
|
||||
_write_allocation_pointer = 0;
|
||||
_cycles_per_line = cycles_per_line;
|
||||
_expected_next_hsync = cycles_per_line;
|
||||
_hsync_error_window = cycles_per_line / 10;
|
||||
_horizontal_counter = 0;
|
||||
_sync_capacitor_charge_level = 0;
|
||||
|
||||
_hretrace_counter = -1;
|
||||
_is_in_sync = false;
|
||||
_vsync_is_proposed = false;
|
||||
}
|
||||
|
||||
CRT::~CRT()
|
||||
@ -45,23 +57,151 @@ CRT::~CRT()
|
||||
delete[] _buffers;
|
||||
}
|
||||
|
||||
#pragma mark - Sync decisions
|
||||
|
||||
#define hretrace_period() ((millisecondsHorizontalRetraceTime * _cycles_per_line) >> 6)
|
||||
|
||||
void CRT::propose_hsync()
|
||||
{
|
||||
if (_horizontal_counter >= _expected_next_hsync - _hsync_error_window)
|
||||
{
|
||||
_expected_next_hsync = (_horizontal_counter + _expected_next_hsync) >> 1;
|
||||
do_hsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("r %d\n", _horizontal_counter);
|
||||
}
|
||||
}
|
||||
|
||||
void CRT::charge_vsync(int number_of_cycles)
|
||||
{
|
||||
// will we start indicating hsync during this charge?
|
||||
const int final_capacitor_charge_level = _sync_capacitor_charge_level + number_of_cycles;
|
||||
const int required_capacitor_charge_level = syncCapacityLineChargeThreshold*_cycles_per_line;
|
||||
if(_sync_capacitor_charge_level < required_capacitor_charge_level && final_capacitor_charge_level >= required_capacitor_charge_level)
|
||||
{
|
||||
const int cycles_until_vsync_starts = required_capacitor_charge_level - _sync_capacitor_charge_level;
|
||||
run_line_for_cycles(cycles_until_vsync_starts);
|
||||
_vsync_is_proposed = true;
|
||||
run_line_for_cycles(number_of_cycles - cycles_until_vsync_starts);
|
||||
}
|
||||
else
|
||||
{
|
||||
run_line_for_cycles(number_of_cycles);
|
||||
}
|
||||
_sync_capacitor_charge_level += number_of_cycles;
|
||||
}
|
||||
|
||||
void CRT::drain_vsync(int number_of_cycles)
|
||||
{
|
||||
// will we stop indicating hsync during this charge?
|
||||
const int required_capacitor_charge_level = syncCapacityLineChargeThreshold*_cycles_per_line;
|
||||
if(_sync_capacitor_charge_level >= required_capacitor_charge_level && _sync_capacitor_charge_level - number_of_cycles < required_capacitor_charge_level)
|
||||
{
|
||||
const int cycles_until_vsync_ends = _sync_capacitor_charge_level - required_capacitor_charge_level;
|
||||
run_line_for_cycles(cycles_until_vsync_ends);
|
||||
_vsync_is_proposed = false;
|
||||
run_line_for_cycles(number_of_cycles - cycles_until_vsync_ends);
|
||||
}
|
||||
else
|
||||
{
|
||||
run_line_for_cycles(number_of_cycles);
|
||||
}
|
||||
_sync_capacitor_charge_level = std::max(0, _sync_capacitor_charge_level - number_of_cycles);
|
||||
}
|
||||
|
||||
void CRT::run_line_for_cycles(int number_of_cycles)
|
||||
{
|
||||
// we're guaranteed not to see any vertical sync events during this run_for_cycles;
|
||||
// will we see a horizontal?
|
||||
|
||||
if(!_hretrace_counter)
|
||||
{
|
||||
const int end_counter = _horizontal_counter + number_of_cycles;
|
||||
const int last_allowed_retrace_time = _expected_next_hsync + _hsync_error_window;
|
||||
if(end_counter >= last_allowed_retrace_time)
|
||||
{
|
||||
// there'll be a forced retrace, and we didn't detect a sync pulse so we'll
|
||||
// push back towards the default period
|
||||
const int cycles_before_retrace = end_counter - last_allowed_retrace_time;
|
||||
run_hline_for_cycles(cycles_before_retrace);
|
||||
do_hsync();
|
||||
_hretrace_counter = hretrace_period();
|
||||
_expected_next_hsync = (_expected_next_hsync + _cycles_per_line) >> 1;
|
||||
run_hline_for_cycles(number_of_cycles - cycles_before_retrace);
|
||||
}
|
||||
else
|
||||
{
|
||||
// we'll just output, no big deal
|
||||
run_hline_for_cycles(number_of_cycles);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_hretrace_counter - number_of_cycles < 0)
|
||||
{
|
||||
// we'll fully retrace and exit
|
||||
number_of_cycles -= _hretrace_counter;
|
||||
run_hline_for_cycles(number_of_cycles - _hretrace_counter);
|
||||
_hretrace_counter = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we'll spend this whole period retracing
|
||||
_hretrace_counter -= number_of_cycles;
|
||||
}
|
||||
|
||||
_hretrace_counter = std::max(0, _hretrace_counter - number_of_cycles);
|
||||
}
|
||||
}
|
||||
|
||||
void CRT::run_hline_for_cycles(int number_of_cycles)
|
||||
{
|
||||
_horizontal_counter += number_of_cycles;
|
||||
}
|
||||
|
||||
void CRT::do_hsync()
|
||||
{
|
||||
printf("%d\n", _horizontal_counter);
|
||||
_hretrace_counter = hretrace_period();
|
||||
_horizontal_counter = 0;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - stream feeding methods
|
||||
|
||||
void CRT::output_sync(int number_of_cycles)
|
||||
{
|
||||
// horizontal sync is edge triggered; vertical is integrated
|
||||
_syncCapacitorChargeLevel += number_of_cycles;
|
||||
// printf("[%d]\n", number_of_cycles);
|
||||
//
|
||||
// if(number_of_cycles > 16)
|
||||
// {
|
||||
// printf("!!!\n");
|
||||
// }
|
||||
|
||||
// horizontal sync is edge triggered
|
||||
if(!_is_in_sync)
|
||||
{
|
||||
_is_in_sync = true;
|
||||
propose_hsync();
|
||||
}
|
||||
charge_vsync(number_of_cycles);
|
||||
}
|
||||
|
||||
void CRT::output_level(int number_of_cycles, std::string type)
|
||||
{
|
||||
_syncCapacitorChargeLevel -= number_of_cycles;
|
||||
_is_in_sync = false;
|
||||
drain_vsync(number_of_cycles);
|
||||
}
|
||||
|
||||
void CRT::output_data(int number_of_cycles, std::string type)
|
||||
{
|
||||
_syncCapacitorChargeLevel -= number_of_cycles;
|
||||
_is_in_sync = false;
|
||||
drain_vsync(number_of_cycles);
|
||||
}
|
||||
|
||||
#pragma mark - Buffer supplying
|
||||
#pragma mark - Buffer supply
|
||||
|
||||
void CRT::allocate_write_area(int required_length)
|
||||
{
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <forward_list>
|
||||
|
||||
namespace Outputs {
|
||||
|
||||
@ -38,7 +39,7 @@ class CRT {
|
||||
|
||||
class CRTDelegate {
|
||||
public:
|
||||
void crt_did_start_vertical_retrace_with_runs(CRTRun *runs, int number_of_runs);
|
||||
void crt_did_start_vertical_retrace_with_runs(std::forward_list<CRTRun> *runs, int runs_to_draw);
|
||||
};
|
||||
void set_crt_delegate(CRTDelegate *);
|
||||
|
||||
@ -48,7 +49,7 @@ class CRT {
|
||||
private:
|
||||
CRTDelegate *_delegate;
|
||||
|
||||
int _syncCapacitorChargeLevel;
|
||||
int _sync_capacitor_charge_level;
|
||||
float _horizontalOffset, _verticalOffset;
|
||||
|
||||
uint8_t **_buffers;
|
||||
@ -56,6 +57,22 @@ class CRT {
|
||||
int _numberOfBuffers;
|
||||
|
||||
int _write_allocation_pointer, _write_target_pointer;
|
||||
|
||||
std::forward_list<CRTRun> _runs;
|
||||
|
||||
void propose_hsync();
|
||||
void charge_vsync(int number_of_cycles);
|
||||
void drain_vsync(int number_of_cycles);
|
||||
void run_line_for_cycles(int number_of_cycles);
|
||||
void run_hline_for_cycles(int number_of_cycles);
|
||||
void do_hsync();
|
||||
void do_vsync();
|
||||
|
||||
int _horizontal_counter, _expected_next_hsync, _hsync_error_window, _hretrace_counter;
|
||||
|
||||
int _cycles_per_line;
|
||||
bool _is_in_sync, _vsync_is_proposed;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user