forked from nspire-emus/firebird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emuthread.cpp
198 lines (159 loc) · 3.3 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
#include "emuthread.h"
#include <cassert>
#include <cerrno>
#include <cstdarg>
#include <chrono>
#include <QEventLoop>
#include <QTimer>
#include "core/debug.h"
#include "core/emu.h"
#include "core/usblink_queue.h"
#include "mainwindow.h"
EmuThread *emu_thread = nullptr;
void gui_do_stuff()
{
emu_thread->doStuff();
}
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)
{
QString str;
str.vsprintf(fmt, ap);
emu_thread->debugStr(str);
}
void gui_status_printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
QString str;
str.vsprintf(fmt, ap);
emu_thread->statusMsg(str);
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);
}
char *gui_debug_prompt()
{
#ifdef MOBILE_UI
return const_cast<char*>("c");
#else
QEventLoop ev;
ev.connect(main_window, SIGNAL(debuggerCommand()), &ev, SLOT(quit()));
ev.exec();
return main_window->debug_command.data();
#endif
}
void gui_putchar(char c)
{
if(true)
emu_thread->serialChar(c);
else
putchar(c);
}
int gui_getchar()
{
return -1;
}
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()
{
emu_thread->throttleTimerWait();
}
EmuThread::EmuThread(QObject *parent) :
QThread(parent)
{
assert(emu_thread == nullptr);
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()
{
if(enter_debugger)
{
enter_debugger = false;
debugger(DBG_USER, 0);
}
while(paused)
msleep(100);
}
void EmuThread::run()
{
setTerminationEnabled();
path_boot1 = boot1.c_str();
path_flash = flash.c_str();
int ret = emulate(port_gdb, port_rdbg);
emit exited(ret);
}
void EmuThread::throttleTimerWait()
{
unsigned int now = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
unsigned int throttle = throttle_delay * 1000;
unsigned int left = throttle - (now % throttle);
if(left > 0)
QThread::usleep(left);
}
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::setPaused(bool paused)
{
this->paused = paused;
}
bool EmuThread::stop()
{
exiting = true;
paused = false;
setTurboMode(true);
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;
}