-
Notifications
You must be signed in to change notification settings - Fork 109
/
PaddleReader.cxx
161 lines (130 loc) · 3.97 KB
/
PaddleReader.cxx
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
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2017 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#include <cmath>
#include "PaddleReader.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PaddleReader::PaddleReader()
{
reset(0);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PaddleReader::reset(double timestamp)
{
myU = 0;
myIsDumped = false;
myValue = 0;
myTimestamp = timestamp;
setConsoleTiming(ConsoleTiming::ntsc);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PaddleReader::vblank(uInt8 value, double timestamp)
{
bool oldIsDumped = myIsDumped;
if (value & 0x80) {
myIsDumped = true;
myU = 0;
myTimestamp = timestamp;
} else if (oldIsDumped) {
myIsDumped = false;
myTimestamp = timestamp;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt8 PaddleReader::inpt(double timestamp)
{
updateCharge(timestamp);
bool state = myIsDumped ? false : myU > myUThresh;
return state ? 0x80 : 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PaddleReader::update(double value, double timestamp, ConsoleTiming consoleTiming)
{
if (consoleTiming != myConsoleTiming) {
setConsoleTiming(consoleTiming);
}
if (value != myValue) {
myValue = value;
if (myValue < 0) {
// value < 0 signifies either maximum resistance OR analog input connected to
// ground (keyboard controllers). As we have no way to tell these apart we just
// assume ground and discharge.
myU = 0;
myTimestamp = timestamp;
} else {
updateCharge(timestamp);
}
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PaddleReader::setConsoleTiming(ConsoleTiming consoleTiming)
{
myConsoleTiming = consoleTiming;
myClockFreq = myConsoleTiming == ConsoleTiming::ntsc ? 60 * 228 * 262 : 50 * 228 * 312;
myUThresh = USUPP * (1. - exp(-TRIPPOINT_LINES * 228 / myClockFreq / (RPOT + R0) / C));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PaddleReader::updateCharge(double timestamp)
{
if (myIsDumped) return;
if (myValue >= 0)
myU = USUPP * (1 - (1 - myU / USUPP) *
exp(-(timestamp - myTimestamp) / (myValue * RPOT + R0) / C / myClockFreq));
myTimestamp = timestamp;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool PaddleReader::save(Serializer& out) const
{
try
{
out.putString(name());
out.putDouble(myUThresh);
out.putDouble(myU);
out.putDouble(myValue);
out.putDouble(myTimestamp);
out.putInt(int(myConsoleTiming));
out.putDouble(myClockFreq);
out.putBool(myIsDumped);
}
catch(...)
{
cerr << "ERROR: TIA_PaddleReader::save" << endl;
return false;
}
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool PaddleReader::load(Serializer& in)
{
try
{
if(in.getString() != name())
return false;
myUThresh = in.getDouble();
myU = in.getDouble();
myValue = in.getDouble();
myTimestamp = in.getDouble();
myConsoleTiming = ConsoleTiming(in.getInt());
myClockFreq = in.getDouble();
myIsDumped = in.getBool();
}
catch(...)
{
cerr << "ERROR: TIA_PaddleReader::load" << endl;
return false;
}
return true;
}