forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsolehook.cpp
More file actions
245 lines (223 loc) · 5.85 KB
/
consolehook.cpp
File metadata and controls
245 lines (223 loc) · 5.85 KB
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
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "execlib.h"
#include "disk.h"
#include "rommgr.h"
#include "uae.h"
#include "threaddep/thread.h"
#include "fsdb.h"
#include "custom.h"
#include "consolehook.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
extern int consoleopen;
#define CONSOLEHOOK_DEBUG 0
static uaecptr beginio;
static uae_sem_t console_in_sem;
static uae_atomic console_in_init;
static uae_atomic console_thread_started;
static uae_atomic console_stop;
static unsigned char console_in_buf[8192];
static volatile uae_u32 console_in_rp;
static volatile uae_u32 console_in_wp;
static void console_in_init_once(void)
{
if (console_in_init)
return;
console_in_init = 1;
uae_sem_init(&console_in_sem, 0, 1);
console_in_rp = console_in_wp = 0;
}
static uae_u32 console_in_avail_nolock(void)
{
return (console_in_wp - console_in_rp) & (sizeof console_in_buf - 1);
}
static void console_in_push(unsigned char b)
{
uae_sem_wait(&console_in_sem);
uae_u32 next = (console_in_wp + 1) & (sizeof console_in_buf - 1);
if (next != console_in_rp) {
console_in_buf[console_in_wp] = b;
console_in_wp = next;
}
uae_sem_post(&console_in_sem);
#if CONSOLEHOOK_DEBUG
write_log(_T("[consolehook] push %02X avail=%u\n"), b, console_in_avail_nolock());
#endif
}
static uae_u32 console_in_pop(unsigned char *dst, uae_u32 maxlen)
{
uae_u32 got = 0;
uae_sem_wait(&console_in_sem);
while (got < maxlen && console_in_rp != console_in_wp) {
dst[got++] = console_in_buf[console_in_rp];
console_in_rp = (console_in_rp + 1) & (sizeof console_in_buf - 1);
}
uae_sem_post(&console_in_sem);
return got;
}
/* CMD_READ must not block: it is called on the emulation thread. */
void consolehook_shutdown(void)
{
console_stop = 1;
}
void consolehook_config (struct uae_prefs *p, const TCHAR *custom_path)
{
struct uaedev_config_info ci = { 0 };
int roms[8];
roms[0] = 11;
roms[1] = 15;
roms[2] = 276;
roms[3] = 281;
roms[4] = 286;
roms[5] = 291;
roms[6] = 304;
roms[7] = -1;
consoleopen = 0;
default_prefs (p, true, 0);
//p->headless = 1;
p->produce_sound = 0;
p->gfx_resolution = 0;
p->gfx_vresolution = 0;
p->gfx_iscanlines = 0;
p->gfx_pscanlines = 0;
p->gfx_framerate = 10;
p->immediate_blits = 1;
p->collision_level = 0;
configure_rom (p, roms, 0);
p->cpu_model = 68020;
p->fpu_model = 68882;
p->chipset_mask = CSMASK_AGA | CSMASK_ECS_AGNUS | CSMASK_ECS_DENISE;
p->cpu_compatible = p->address_space_24 = false;
p->m68k_speed = -1;
#ifdef JIT
p->cachesize = 16384;
#else
p->cachesize = 0;
#endif
p->chipmem.size = 0x200000;
p->fastmem[0].size = 0x00800000;
p->bogomem.size = 0;
p->nr_floppies = 1;
p->floppyslots[1].dfxtype = DRV_NONE;
p->floppy_speed = 0;
p->start_gui = 0;
p->gfx_monitor[0].gfx_size_win.width = 320;
p->gfx_monitor[0].gfx_size_win.height = 256;
p->turbo_emulation = 0;
//p->win32_automount_drives = 2;
//p->win32_automount_cddrives = 2;
p->cs_compatible = CP_A1200;
built_in_chipset_prefs (p);
uci_set_defaults(&ci, false);
if (custom_path && custom_path[0]) {
// Use the custom path provided via command line
my_canonicalize_path(custom_path, ci.rootdir, MAX_DPATH);
} else {
// Use current working directory as before
TCHAR cwd[MAX_DPATH];
cwd[0] = 0;
#ifdef _WIN32
GetCurrentDirectory(MAX_DPATH, cwd);
#else
getcwd(cwd, MAX_DPATH);
#endif
if (cwd[0])
my_canonicalize_path(cwd, ci.rootdir, MAX_DPATH);
else
_tcscpy(ci.rootdir, _T("."));
}
_tcscpy (ci.volname, _T("CLIBOOT"));
_tcscpy (ci.devname, _T("DH0"));
ci.bootpri = 15;
ci.type = UAEDEV_DIR;
add_filesys_config (p, -1, &ci);
}
static void console_thread (void *v)
{
uae_set_thread_priority (NULL, 1);
console_in_init_once();
set_console_input_mode(0);
for (;;) {
if (console_stop)
break;
TCHAR wc = console_getch ();
if (!wc)
continue;
char c = (char)wc;
/* Normalize host console keys to what Amiga console.device expects. */
if (c == '\r')
c = '\n';
console_in_push((unsigned char)c);
}
set_console_input_mode(1);
console_thread_started = 0;
}
int consolehook_activate (void)
{
return console_emulation;
}
void consolehook_ret(TrapContext *ctx, uaecptr condev, uaecptr oldbeginio)
{
beginio = oldbeginio;
write_log (_T("console.device at %08X\n"), condev);
open_console();
console_in_init_once();
if (!console_thread_started) {
console_thread_started = 1;
console_stop = 0;
uae_start_thread (_T("consolereader"), console_thread, NULL, NULL);
}
}
uaecptr consolehook_beginio(TrapContext *ctx, uaecptr request)
{
uae_u32 io_data = trap_get_long(ctx, request + 40); // 0x28
uae_u32 io_length = trap_get_long(ctx, request + 36); // 0x24
(void)trap_get_long(ctx, request + 32); // io_actual (unused)
(void)trap_get_long(ctx, request + 44); // io_offset (unused)
uae_u16 cmd = trap_get_word(ctx, request + 28);
if (cmd == CMD_WRITE) {
open_console();
int len = (int)io_length;
if (len < 0)
len = 0;
if (len > 1024 * 1024)
len = 1024 * 1024;
if (len > 0 && io_data) {
char *dbuf = xmalloc(char, len);
if (!dbuf)
return beginio;
trap_get_bytes(ctx, dbuf, io_data, len);
for (int i = 0; i < len; i++) {
TCHAR ch = (TCHAR)dbuf[i];
if (ch == '\n')
console_out_f(_T("\r"));
console_out_f(_T("%c"), ch);
}
console_flush();
xfree(dbuf);
}
} else if (cmd == CMD_READ) {
console_in_init_once();
uae_u32 maxlen = io_length;
if ((uae_s32)maxlen < 0)
maxlen = 0;
if (maxlen > 4096)
maxlen = 4096;
unsigned char tmp[4096];
uae_u32 got = 0;
if (maxlen && io_data) // Fix: check io_data (buf) is not NULL before use
got = console_in_pop(tmp, maxlen);
#if CONSOLEHOOK_DEBUG
write_log(_T("[consolehook] CMD_READ max=%u got=%u\n"), maxlen, got);
#endif
if (got && io_data) // Fix: check io_data (buf) is not NULL before use
trap_put_bytes(ctx, tmp, io_data, got);
trap_put_long(ctx, request + 32, got); // io_actual
}
return beginio;
}