-
Notifications
You must be signed in to change notification settings - Fork 3
/
CycleCounter.cpp
174 lines (148 loc) · 4.65 KB
/
CycleCounter.cpp
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
#include "CycleCounter.h"
#include <mutex>
#include <iostream>
#include <chrono>
#include "A2VideoManager.h"
#include "SoundManager.h"
#include "EventRecorder.h"
// below because "The declaration of a static data member in its class definition is not a definition"
CycleCounter* CycleCounter::s_instance;
static uint32_t m_cycle = 0; // Current cycle
static uint32_t m_cycle_alignments = 0; // Number of times we aligned the cycles to the VBL seen
static bool bIsHBL = true;
static bool bIsVBL = false;
static VideoRegion_e m_region; // We default to NTSC values unless we get a VBL in a wrong cycle
std::mutex mtx_cycle; // protect the cycle counter
uint32_t cycles_vblank;
uint32_t cycles_total;
size_t CycleCounter::GetCurrentTimeInMicroseconds() {
// Get the current time point from the high-resolution clock
auto now = std::chrono::high_resolution_clock::now();
// Convert the time point to a duration since epoch in microseconds
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
// Return the count of microseconds
return duration.count();
}
void CycleCounter::Initialize()
{
m_tstamp_init = GetCurrentTimeInMicroseconds();
// So we can render on startup, move the cycle counter to after HBlank
m_cycle = CYCLES_SC_HBL;
bIsHBL = false;
m_region = VideoRegion_e::NTSC;
cycles_total = CYCLES_TOTAL_NTSC;
cycles_vblank = cycles_total - CYCLES_SCREEN;
m_cycles_since_reset = 0;
}
void CycleCounter::Reset()
{
Initialize();
}
void CycleCounter::IncrementCycles(int inc, bool isVBL)
{
m_tstamp_cycle = GetCurrentTimeInMicroseconds() - m_tstamp_init;
m_cycle += inc;
m_cycle = (m_cycle % cycles_total);
m_cycles_since_reset += inc;
// Update VBL and region automatically with 0xC019
if (isVideoRegionDynamic && isVBL)
{
if (m_cycle < CYCLES_SCREEN)
{
m_cycle_alignments++;
// determine the region
if ((m_cycle_alignments > 1) && (m_cycle < (CYCLES_SCREEN - cycles_vblank)))
{
// If we've already done one cycle alignment at least, then if we're in the
// correct region we're certain that the VBL is beyond (CYCLES_SCREEN - cycles_vblank).
// Hence here we've demonstrated that we're in the wrong region and should switch region.
std::cout << " VBL in cycle: " << m_cycle << ". " << std::endl;
if (m_region == VideoRegion_e::NTSC)
SetVideoRegion(VideoRegion_e::PAL);
else
SetVideoRegion(VideoRegion_e::NTSC);
}
m_prev_vbl_start = m_cycle;
while (m_cycle < CYCLES_SCREEN) // move m_cycle to VBLANK start
{
bIsVBL = false;
bIsHBL = (GetByteXPos() < CYCLES_SC_HBL);
A2VideoManager::GetInstance()->BeamIsAtPosition(GetByteXPos(), GetScanline());
++m_cycle;
}
std::cout << "VBL Alignment " << m_cycle_alignments
<< ": " << m_prev_vbl_start << " ---> " << m_cycle << std::endl;
}
}
bIsVBL = (m_cycle >= CYCLES_SCREEN);
bIsHBL = (GetByteXPos() < CYCLES_SC_HBL);
A2VideoManager::GetInstance()->BeamIsAtPosition(GetByteXPos(), GetScanline());
}
const VideoRegion_e CycleCounter::GetVideoRegion()
{
return m_region;
}
void CycleCounter::SetVideoRegion(VideoRegion_e region)
{
if (region == m_region)
return;
switch (region) {
case VideoRegion_e::PAL:
m_region = VideoRegion_e::PAL;
cycles_total = CYCLES_TOTAL_PAL;
cycles_vblank = cycles_total - CYCLES_SCREEN;
SoundManager::GetInstance()->SetPAL(true);
EventRecorder::GetInstance()->SetPAL(true);
std::cout << "Switched to PAL." << std::endl;
break;
case VideoRegion_e::NTSC:
m_region = VideoRegion_e::NTSC;
cycles_total = CYCLES_TOTAL_NTSC;
cycles_vblank = cycles_total - CYCLES_SCREEN;
SoundManager::GetInstance()->SetPAL(false);
EventRecorder::GetInstance()->SetPAL(true);
std::cout << "Switched to NTSC." << std::endl;
break;
default:
std::cout << "ERROR: Unknown Region" << std::endl;
break;
}
m_prev_vbl_start = CYCLES_SCREEN;
m_cycle = CYCLES_SCREEN;
m_cycle_alignments = 0;
}
const bool CycleCounter::IsVBL()
{
return bIsVBL;
}
const bool CycleCounter::IsHBL()
{
return bIsHBL;
}
const bool CycleCounter::IsInBlank()
{
return (bIsVBL || bIsHBL);
}
const uint32_t CycleCounter::GetScanline()
{
return m_cycle / (CYCLES_SC_CONTENT + CYCLES_SC_HBL);
}
const uint32_t CycleCounter::GetByteXPos()
{
return m_cycle % (CYCLES_SC_CONTENT + CYCLES_SC_HBL);
}
const uint32_t CycleCounter::GetScreenCycles()
{
return CYCLES_SCREEN;
}
void CycleCounter::SetVBLStart(uint32_t _vblStart)
{
// Don't allow a VBL start that's after the screen cycles
if (_vblStart > CYCLES_SCREEN)
return;
m_prev_vbl_start = CYCLES_SCREEN;
// move the cycle to the requested change to the VBL
m_cycle = m_cycle + (_vblStart - CYCLES_SCREEN);
m_cycle_alignments = 0;
return;
}