-
Notifications
You must be signed in to change notification settings - Fork 71
/
emuthread.cpp
262 lines (209 loc) · 4.72 KB
/
emuthread.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
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
#include "emuthread.h"
#include <cassert>
#include <cerrno>
#include <cstdarg>
#include <chrono>
#include <QDir>
#include <QEventLoop>
#include <QTimer>
#ifdef Q_OS_WINDOWS
#include <time.h>
#include <windows.h>
#endif
#include "core/debug.h"
#include "core/emu.h"
#include "core/usblink_queue.h"
EmuThread emu_thread;
void gui_do_stuff(bool wait)
{
emu_thread.doStuff(wait);
}
void gui_debug_printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
gui_debug_vprintf(fmt, ap);
va_end(ap);
}
void gui_debug_vprintf(const char *fmt, va_list ap)
{
emu_thread.debugStr(QString::vasprintf(fmt, ap));
}
void gui_status_printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
emu_thread.statusMsg(QString::vasprintf(fmt, ap));
va_end(ap);
}
void gui_perror(const char *msg)
{
gui_debug_printf("%s: %s\n", msg, strerror(errno));
}
void gui_debugger_entered_or_left(bool entered)
{
emu_thread.debuggerEntered(entered);
}
static debug_input_cb debug_callback;
void gui_debugger_request_input(debug_input_cb callback)
{
debug_callback = callback;
emu_thread.debugInputRequested(callback != nullptr);
}
void gui_putchar(char c)
{
if(true)
emu_thread.serialChar(c);
else
putchar(c);
}
int gui_getchar()
{
return -1;
}
void gui_set_busy(bool busy)
{
emit emu_thread.isBusy(busy);
}
void gui_show_speed(double d)
{
emit emu_thread.speedChanged(d);
}
void gui_usblink_changed(bool state)
{
emit emu_thread.usblinkChanged(state);
}
void throttle_timer_off()
{
emu_thread.setTurboMode(true);
}
void throttle_timer_on()
{
emu_thread.setTurboMode(false);
}
void throttle_timer_wait(unsigned int usec)
{
emu_thread.throttleTimerWait(usec);
}
EmuThread::EmuThread(QObject *parent) :
QThread(parent)
{
// There can only be one instance, this global one.
assert(&emu_thread == this);
// Set default settings
debug_on_start = debug_on_warn = false;
}
//Called occasionally, only way to do something in the same thread the emulator runs in.
void EmuThread::doStuff(bool wait)
{
do
{
if(do_suspend)
{
bool success = emu_suspend(snapshot_path.c_str());
do_suspend = false;
emit suspended(success);
}
if(enter_debugger)
{
setPaused(false);
enter_debugger = false;
if(!in_debugger)
debugger(DBG_USER, 0);
}
if(is_paused && wait)
msleep(100);
} while(is_paused && wait);
}
void EmuThread::run()
{
setTerminationEnabled();
path_boot1 = QDir::toNativeSeparators(boot1).toStdString();
path_flash = QDir::toNativeSeparators(flash).toStdString();
bool do_reset = !do_resume;
bool success = emu_start(port_gdb, port_rdbg, do_resume ? snapshot_path.c_str() : nullptr);
if(do_resume)
emit resumed(success);
else
emit started(success);
do_resume = false;
if(success)
emu_loop(do_reset);
emit stopped();
}
void EmuThread::throttleTimerWait(unsigned int usec)
{
if(usec <= 1)
return;
#ifdef Q_OS_WINDOWS
// QThread::usleep uses Sleep, which may sleep up to ~32ms more!
// Use nanosleep inside a timeBeginPeriod/timeEndPeriod block for accuracy.
timeBeginPeriod(10);
struct timespec ts{};
ts.tv_nsec = usec * 1000;
nanosleep(&ts, nullptr);
timeEndPeriod(10);
#else
QThread::usleep(usec);
#endif
}
void EmuThread::setTurboMode(bool enabled)
{
turbo_mode = enabled;
emit turboModeChanged(enabled);
}
void EmuThread::toggleTurbo()
{
setTurboMode(!turbo_mode);
}
void EmuThread::enterDebugger()
{
enter_debugger = true;
}
void EmuThread::debuggerInput(QString str)
{
debug_input = str.toStdString();
debug_callback(debug_input.c_str());
}
void EmuThread::setPaused(bool paused)
{
this->is_paused = paused;
emit this->paused(paused);
}
bool EmuThread::stop()
{
if(!isRunning())
return true;
exiting = true;
setPaused(false);
do_suspend = false;
// Cause the cpu core to leave the loop and check for events
cycle_count_delta = 0;
if(!this->wait(200))
{
terminate();
if(!this->wait(200))
return false;
}
emu_cleanup();
return true;
}
void EmuThread::reset()
{
usblink_queue_reset();
cpu_events |= EVENT_RESET;
}
bool EmuThread::resume(QString path)
{
snapshot_path = QDir::toNativeSeparators(path).toStdString();
do_resume = true;
if(!stop())
return false;
start();
return true;
}
void EmuThread::suspend(QString path)
{
snapshot_path = QDir::toNativeSeparators(path).toStdString();
do_suspend = true;
}