forked from ramapcsx2/gbs-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframesync.h
350 lines (301 loc) · 10.4 KB
/
framesync.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#ifndef FRAMESYNC_H_
#define FRAMESYNC_H_
// fast digitalRead()
#if defined(ESP8266)
#define digitalRead(x) ((GPIO_REG_READ(GPIO_IN_ADDRESS) >> x) & 1)
#ifndef DEBUG_IN_PIN
#define DEBUG_IN_PIN D6
#endif
#else // Arduino
// fastest, but non portable (Uno pin 11 = PB3, Mega2560 pin 11 = PB5)
//#define digitalRead(x) bitRead(PINB, 3)
#include "fastpin.h"
#define digitalRead(x) fastRead<x>()
// no define for DEBUG_IN_PIN
#endif
//#define FS_DEBUG
volatile uint32_t stopTime, startTime;
void ICACHE_RAM_ATTR risingEdgeISR_measure() {
noInterrupts();
stopTime = ESP.getCycleCount();
detachInterrupt(DEBUG_IN_PIN);
interrupts();
}
void ICACHE_RAM_ATTR risingEdgeISR_prepare() {
noInterrupts();
startTime = ESP.getCycleCount();
detachInterrupt(DEBUG_IN_PIN);
attachInterrupt(DEBUG_IN_PIN, risingEdgeISR_measure, RISING);
interrupts();
}
template <class GBS, class Attrs>
class FrameSyncManager {
private:
typedef typename GBS::STATUS_VDS_VERT_COUNT VERT_COUNT;
typedef typename GBS::VDS_HSYNC_RST HSYNC_RST;
typedef typename GBS::VDS_VSYNC_RST VSYNC_RST;
typedef typename GBS::VDS_VS_ST VSST;
typedef typename GBS::VDS_VS_SP VSSP;
typedef typename GBS::template Tie<VSYNC_RST, VSST, VSSP> VRST_SST_SSP;
static const uint8_t debugInPin = Attrs::debugInPin;
static const int16_t syncCorrection = Attrs::syncCorrection;
static const int32_t syncTargetPhase = Attrs::syncTargetPhase;
static bool syncLockReady;
static int16_t syncLastCorrection;
// Sample vsync start and stop times from debug pin.
static bool vsyncOutputSample(uint32_t *start, uint32_t *stop) {
startTime = 0; stopTime = 0;
ESP.wdtDisable();
attachInterrupt(DEBUG_IN_PIN, risingEdgeISR_prepare, RISING);
// PAL50: 20ms, worst case 2 fields, sometimes needed when tuning VDS clock
for (uint32_t i = 0; i < 1500; i++)
{
if (stopTime > 0) {
break;
}
if ((i % 100) == 0) {
ESP.wdtFeed();
}
delayMicroseconds(100);
}
*start = startTime;
*stop = stopTime;
ESP.wdtEnable(0);
if ((*start > *stop) || *stop == 0 || *start == 0) {
// ESP.getCycleCount() overflow oder no pulse, just fail this round
return false;
}
#ifdef FS_DEBUG
int32 tstC = *stop - *start;
static int32_t tstP = tstC;
Serial.print(" out jitter: ");
Serial.println(abs(tstC - tstP));
tstP = tstC;
#endif
return true;
}
// Sample input and output vsync periods and their phase
// difference in microseconds
static bool vsyncPeriodAndPhase(int32_t *periodInput, int32_t *periodOutput, int32_t *phase) {
uint32_t inStart, inStop, outStart, outStop;
uint32_t inPeriod, outPeriod, diff;
uint8_t debugRegBackup = GBS::TEST_BUS_SEL::read();
// 0x0 = IF (t1t28t3)
GBS::TEST_BUS_SEL::write(0x0);
if (!vsyncInputSample(&inStart, &inStop)) {
return false;
}
// 0x2 = VDS (t3t50t4) // selected test measures VDS vblank (VB ST/SP)
GBS::TEST_BUS_SEL::write(0x2);
inPeriod = (inStop - inStart); //>> 1;
if (!vsyncOutputSample(&outStart, &outStop)) {
return false;
}
outPeriod = (outStop - outStart); //>> 1;
// to decide: where to leave test bus sel ?
GBS::TEST_BUS_SEL::write(debugRegBackup);
diff = (outStart - inStart) % inPeriod;
if (periodInput)
*periodInput = inPeriod;
if (periodOutput)
*periodOutput = outPeriod;
if (phase)
*phase = (diff < inPeriod) ? diff : diff - inPeriod;
#ifdef FS_DEBUG
Serial.print(" inPeriod: "); Serial.println(inPeriod);
Serial.print("outPeriod: "); Serial.println(outPeriod);
#endif
return true;
}
static bool sampleVsyncPeriods(uint32_t *input, uint32_t *output) {
int32_t inPeriod, outPeriod;
if (!vsyncPeriodAndPhase(&inPeriod, &outPeriod, NULL))
return false;
*input = inPeriod;
*output = outPeriod;
return true;
}
// Find the largest htotal that makes output frame time less than
// the input.
// update: has to be less for the soft frame time lock to work (but not for hard frame lock (3_1A 4))
static bool findBestHTotal(uint32_t &bestHtotal) {
uint16_t inHtotal = HSYNC_RST::read();
uint32_t inPeriod, outPeriod;
if (inHtotal == 0) { return false; } // safety
if (!sampleVsyncPeriods(&inPeriod, &outPeriod)) { return false; }
// large htotal can push intermediates to 33 bits
bestHtotal = (uint64_t)(inHtotal * (uint64_t)inPeriod) / (uint64_t)outPeriod;
if (bestHtotal == (inHtotal + 1)) { bestHtotal -= 1; } // works well
if (bestHtotal == (inHtotal - 1)) { bestHtotal += 1; } // same (outPeriod very slightly larger like this doesn't cause the vertical bar)
#ifdef FS_DEBUG
if (bestHtotal != inHtotal) {
Serial.print(" wants new htotal, oldbest: "); Serial.print(inHtotal);
Serial.print(" newbest: "); Serial.println(bestHtotal);
}
#endif
return true;
}
public:
// sets syncLockReady = false, which in turn starts a new findBestHtotal run in loop()
static void reset() {
syncLockReady = false;
syncLastCorrection = 0;
}
static void resetWithoutRecalculation() {
syncLockReady = true;
syncLastCorrection = 0;
}
static uint16_t init() {
uint32_t bestHTotal = 0;
// Adjust output horizontal sync timing so that the overall
// frame time is as close to the input as possible while still
// being less. Increasing the vertical frame size slightly
// should then push the output frame time to being larger than
// the input.
if (!findBestHTotal(bestHTotal)) {
return 0;
}
syncLockReady = true;
return (uint16_t)bestHTotal;
}
static uint32_t getPulseTicks() {
uint32_t inStart, inStop;
if (!vsyncInputSample(&inStart, &inStop)) {
return 0;
}
return inStop - inStart;
}
static bool ready(void) {
return syncLockReady;
}
static int16_t getSyncLastCorrection() {
return syncLastCorrection;
}
// Sample vsync start and stop times from debug pin.
static bool vsyncInputSample(uint32_t *start, uint32_t *stop) {
startTime = 0; stopTime = 0;
ESP.wdtDisable();
attachInterrupt(DEBUG_IN_PIN, risingEdgeISR_prepare, RISING);
// PAL50: 20ms, worst case 2 fields, sometimes needed when tuning VDS clock
for (uint32_t i = 0; i < 1500; i++)
{
if (stopTime > 0) {
break;
}
if ((i % 100) == 0) {
ESP.wdtFeed();
}
delayMicroseconds(100);
}
*start = startTime;
*stop = stopTime;
ESP.wdtEnable(0);
if ((*start > *stop) || *stop == 0 || *start == 0) {
// ESP.getCycleCount() overflow oder no pulse, just fail this round
return false;
}
#ifdef FS_DEBUG
int32 tstC = *stop - *start;
static int32_t tstP = tstC;
Serial.print(" in jitter: ");
Serial.println(abs(tstC - tstP));
tstP = tstC;
#endif
return true;
}
// Perform vsync phase locking. This is accomplished by measuring
// the period and phase offset of the input and output vsync
// signals and adjusting the frame size (and thus the output vsync
// frequency) to bring the phase offset closer to the desired
// value.
static bool run(uint8_t frameTimeLockMethod) {
int32_t period;
int32_t phase;
int32_t target;
int16_t correction;
uint16_t thisHtotal = HSYNC_RST::read();
static int16_t prevHtotal = thisHtotal;
if (!syncLockReady)
return false;
if (prevHtotal != thisHtotal) {
if (syncLastCorrection != 0) {
#ifdef FS_DEBUG
Serial.println("reset with restore >");
#endif
uint16_t vtotal = 0, vsst = 0, vssp = 0;
uint16_t currentLineNumber, earlyFrameBoundary;
uint16_t timeout = 10;
VRST_SST_SSP::read(vtotal, vsst, vssp);
earlyFrameBoundary = vtotal / 4;
vtotal -= syncLastCorrection;
if (frameTimeLockMethod == 1) { // moves VS position
vsst -= syncLastCorrection;
vssp -= syncLastCorrection;
}
// else it is method 0: leaves VS position alone
do {
// wait for next frame start + 20 lines for stability
currentLineNumber = GBS::STATUS_VDS_VERT_COUNT::read();
} while ((currentLineNumber > earlyFrameBoundary || currentLineNumber < 20) && --timeout > 0);
VRST_SST_SSP::write(vtotal, vsst, vssp);
#ifdef FS_DEBUG
Serial.print(" vtotal now: "); Serial.println(vtotal);
#endif
}
else {
#ifdef FS_DEBUG
uint16_t vtotal = 0, vsst = 0, vssp = 0;
VRST_SST_SSP::read(vtotal, vsst, vssp);
Serial.print("reset without restore > vtotal now: "); Serial.println(vtotal);
#endif
}
reset(); // sets syncLockReady = false, which in turn starts a new findBestHtotal run in loop()
prevHtotal = thisHtotal;
return true;
}
if (!vsyncPeriodAndPhase(&period, NULL, &phase))
return false;
target = (syncTargetPhase * period) / 360; // -300; //debug
if (phase > target)
correction = 0;
else
correction = syncCorrection;
#ifdef FS_DEBUG
if (correction || syncLastCorrection) {
Serial.print("Correction: "); Serial.print(correction);
Serial.print(" syncLastCorrection: "); Serial.println(syncLastCorrection);
}
#endif
int16_t delta = correction - syncLastCorrection;
uint16_t vtotal = 0, vsst = 0, vssp = 0;
uint16_t currentLineNumber, earlyFrameBoundary;
uint16_t timeout = 10; // this routine usually finishes on first or second attempt
VRST_SST_SSP::read(vtotal, vsst, vssp);
earlyFrameBoundary = vtotal / 4;
vtotal += delta;
if (frameTimeLockMethod == 1) { // moves VS position
vsst += delta;
vssp += delta;
}
// else it is method 0: leaves VS position alone
do {
// wait for next frame start + 20 lines for stability
currentLineNumber = GBS::STATUS_VDS_VERT_COUNT::read();
} while ((currentLineNumber > earlyFrameBoundary || currentLineNumber < 20) && --timeout > 0);
VRST_SST_SSP::write(vtotal, vsst, vssp);
syncLastCorrection = correction;
prevHtotal = thisHtotal;
#ifdef FS_DEBUG
VRST_SST_SSP::read(vtotal, vsst, vssp);
Serial.print("thisHtotal: "); Serial.print(thisHtotal);
Serial.print(" prevHtotal: "); Serial.print(prevHtotal);
Serial.print(" vtotal: "); Serial.println(vtotal);
#endif
return true;
}
};
template <class GBS, class Attrs>
int16_t FrameSyncManager<GBS, Attrs>::syncLastCorrection;
template <class GBS, class Attrs>
bool FrameSyncManager<GBS, Attrs>::syncLockReady;
#endif