forked from ramapcsx2/gbs-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframesync.h
372 lines (312 loc) · 10.2 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#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
#include <ESP8266WiFi.h>
// FS_DEBUG: full verbose debug over serial
// FS_DEBUG_LED: just blink LED (off = adjust phase, on = normal phase)
//#define FS_DEBUG
//#define FS_DEBUG_LED
volatile uint32_t stopTime, startTime;
volatile uint32_t armed;
void ICACHE_RAM_ATTR risingEdgeISR_measure() {
noInterrupts();
//stopTime = ESP.getCycleCount();
__asm__ __volatile__("rsr %0,ccount":"=a"(stopTime));
detachInterrupt(DEBUG_IN_PIN);
interrupts();
}
void ICACHE_RAM_ATTR risingEdgeISR_prepare() {
noInterrupts();
//startTime = ESP.getCycleCount();
__asm__ __volatile__("rsr %0,ccount":"=a"(startTime));
detachInterrupt(DEBUG_IN_PIN);
armed = 1;
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::template Tie<VSYNC_RST, VSST> VRST_SST;
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 uint8_t delayLock;
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; armed = 0;
yield(); ESP.wdtDisable();
attachInterrupt(DEBUG_IN_PIN, risingEdgeISR_prepare, RISING);
// typical: 300000 at 80MHz, 600000 at 160MHz
for (uint32_t i = 0; i < 3000000; i++)
{
if (armed) {
armed = 0;
delay(7);
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
}
if (stopTime > 0) {
break;
}
}
*start = startTime;
*stop = stopTime;
ESP.wdtEnable(0);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
if ((*start >= *stop) || *stop == 0 || *start == 0) {
// ESP.getCycleCount() overflow oder no pulse, just fail this round
return false;
}
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;
// calling code needs to ensure debug bus is ready to sample vperiod
if (!vsyncInputSample(&inStart, &inStop)) {
return false;
}
GBS::TEST_BUS_SEL::write(0x2); // 0x2 = VDS (t3t50t4) // measure VDS vblank (VB ST/SP)
inPeriod = (inStop - inStart); //>> 1;
if (!vsyncOutputSample(&outStart, &outStop)) {
return false;
}
outPeriod = (outStop - outStart); //>> 1;
diff = (outStart - inStart) % inPeriod;
if (periodInput)
*periodInput = inPeriod;
if (periodOutput)
*periodOutput = outPeriod;
if (phase)
*phase = (diff < inPeriod) ? diff : diff - inPeriod;
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 appropriate htotal that makes output frame time slightly more than the input.
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; }
if (inPeriod == 0 || outPeriod == 0) { return false; } // safety
// allow ~4 negative (inPeriod is < outPeriod) clock cycles jitter
if ((inPeriod > outPeriod ? inPeriod - outPeriod : outPeriod - inPeriod) <= 4) {
/*if (inPeriod >= outPeriod) {
Serial.print("inPeriod >= out: ");
Serial.println(inPeriod - outPeriod);
}
else {
Serial.print("inPeriod < out: ");
Serial.println(outPeriod - inPeriod);
}*/
bestHtotal = inHtotal;
}
else {
// large htotal can push intermediates to 33 bits
bestHtotal = (uint64_t)(inHtotal * (uint64_t)inPeriod) / (uint64_t)outPeriod;
}
// new 08.11.19: skip this step, IF period measurement should be stable enough to give repeatable results
//if (bestHtotal == (inHtotal + 1)) { bestHtotal -= 1; } // works well
//if (bestHtotal == (inHtotal - 1)) { bestHtotal += 1; } // check with SNES + vtotal = 1000 (1280x960)
#ifdef FS_DEBUG
if (bestHtotal != inHtotal) {
Serial.print(F(" wants new htotal, oldbest: ")); Serial.print(inHtotal);
Serial.print(F(" newbest: ")); Serial.println(bestHtotal);
Serial.print(F(" inPeriod: ")); Serial.print(inPeriod);
Serial.print(F(" outPeriod: ")); Serial.println(outPeriod);
}
#endif
return true;
}
public:
// sets syncLockReady = false, which in turn starts a new findBestHtotal run in loop()
static void reset(uint8_t frameTimeLockMethod) {
#ifdef FS_DEBUG
Serial.print("FS reset(), with correction: ");
#endif
if (syncLastCorrection != 0) {
#ifdef FS_DEBUG
Serial.println("Yes");
#endif
uint16_t vtotal = 0, vsst = 0;
VRST_SST::read(vtotal, vsst);
uint16_t timeout = 0;
vtotal -= syncLastCorrection;
if (frameTimeLockMethod == 0) { // moves VS position
vsst -= syncLastCorrection;
}
while ((GBS::STATUS_VDS_FIELD::read() == 1) && (++timeout < 400));
GBS::VDS_VS_ST::write(vsst);
timeout = 0;
while ((GBS::STATUS_VDS_FIELD::read() == 0) && (++timeout < 400));
GBS::VDS_VSYNC_RST::write(vtotal);
}
#ifdef FS_DEBUG
else {
Serial.println("No");
}
#endif
syncLockReady = false;
syncLastCorrection = 0;
delayLock = 0;
}
static void resetWithoutRecalculation() {
syncLockReady = false;
delayLock = 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;
delayLock = 0;
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;
}
static void cleanup() {
syncLastCorrection = 0; // the important bit
syncLockReady = 0;
delayLock = 0;
}
// Sample vsync start and stop times from debug pin.
static bool vsyncInputSample(uint32_t *start, uint32_t *stop) {
startTime = 0; stopTime = 0; armed = 0;
yield(); ESP.wdtDisable();
attachInterrupt(DEBUG_IN_PIN, risingEdgeISR_prepare, RISING);
// typical: 300000 at 80MHz, 600000 at 160MHz
for (uint32_t i = 0; i < 3000000; i++)
{
if (armed) {
armed = 0;
delay(7);
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
}
if (stopTime > 0) {
break;
}
}
*start = startTime;
*stop = stopTime;
ESP.wdtEnable(0);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
if ((*start >= *stop) || *stop == 0 || *start == 0) {
// ESP.getCycleCount() overflow oder no pulse, just fail this round
return false;
}
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;
if (!syncLockReady)
return false;
if (delayLock < 2) {
delayLock++;
return true;
}
if (!vsyncPeriodAndPhase(&period, NULL, &phase))
return false;
target = (syncTargetPhase * period) / 360;
if (phase > target)
correction = 0;
else
correction = syncCorrection;
#ifdef FS_DEBUG
Serial.printf("phase: %7d target: %7d", phase, target);
if (correction == syncLastCorrection) {
// terminate line if returning early
Serial.println();
}
#endif
#ifdef FS_DEBUG_LED
if (correction == 0) {
digitalWrite(LED_BUILTIN, LOW); // LED ON
}
else {
digitalWrite(LED_BUILTIN, HIGH); // LED OFF
}
#endif
// return early?
if (correction == syncLastCorrection) {
return true;
}
int16_t delta = correction - syncLastCorrection;
uint16_t vtotal = 0, vsst = 0;
uint16_t timeout = 0;
VRST_SST::read(vtotal, vsst);
vtotal += delta;
if (frameTimeLockMethod == 0) { // moves VS position
vsst += delta;
}
// else it is method 1: leaves VS position alone
while ((GBS::STATUS_VDS_FIELD::read() == 1) && (++timeout < 400));
GBS::VDS_VS_ST::write(vsst);
timeout = 0;
while ((GBS::STATUS_VDS_FIELD::read() == 0) && (++timeout < 400));
GBS::VDS_VSYNC_RST::write(vtotal);
syncLastCorrection = correction;
#ifdef FS_DEBUG
Serial.printf(" vtotal: %4d\n", vtotal);
#endif
return true;
}
};
template <class GBS, class Attrs>
int16_t FrameSyncManager<GBS, Attrs>::syncLastCorrection;
template <class GBS, class Attrs>
uint8_t FrameSyncManager<GBS, Attrs>::delayLock;
template <class GBS, class Attrs>
bool FrameSyncManager<GBS, Attrs>::syncLockReady;
#endif