-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathnatfeats.c
359 lines (323 loc) · 8.67 KB
/
natfeats.c
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Hatari - natfeats.c
*
* Copyright (C) 2012-2016, 2019 by Eero Tamminen
*
* This file is distributed under the GNU General Public License, version 2
* or at your option any later version. Read the file gpl.txt for details.
*
* natfeats.c - Hatari Native features identification and call forwarding,
* modeled after similar code in Aranym. See tests/natfeats/ for more info.
*/
const char Natfeats_fileid[] = "Hatari natfeats.c";
#include <stdio.h>
#include "main.h"
#include "version.h"
#include "configuration.h"
#include "stMemory.h"
#include "m68000.h"
#include "reset.h"
#include "natfeats.h"
#include "debugui.h"
#include "statusbar.h"
#include "nf_scsidrv.h"
#include "log.h"
/* maximum input string length */
#define NF_MAX_STRING 4096
/* whether to allow XBIOS(255) style
* Hatari command line parsing with "command" NF
*/
#define NF_COMMAND 0
/* TODO:
* - supervisor vs. user stack handling?
* - clipboard and hostfs native features?
*/
/* ----------------------------------
* Native Features shared with Aranym
*/
/**
* Check whether given string address is valid
* and whether strings is of "reasonable" length.
*
* Returns string length or zero for error.
*/
static int mem_string_ok(Uint32 addr)
{
const char *buf;
int i;
if (!STMemory_CheckAreaType(addr, 1, ABFLAG_RAM | ABFLAG_ROM)) {
/* invalid starting address -> error */
M68000_BusError(addr, BUS_ERROR_READ, BUS_ERROR_SIZE_BYTE, BUS_ERROR_ACCESS_DATA, 0);
return 0;
}
buf = (const char *)STMemory_STAddrToPointer(addr);
if (STMemory_CheckAreaType(addr, NF_MAX_STRING, ABFLAG_RAM | ABFLAG_ROM)) {
/* valid area -> return length */
for (i = 0; i < NF_MAX_STRING; i++) {
if (!buf[i]) {
return i;
}
}
return 0;
}
for (i = 0; i < NF_MAX_STRING; i++) {
if (!STMemory_CheckAreaType(addr, 1, ABFLAG_RAM | ABFLAG_ROM)) {
/* ends in invalid area -> error */
M68000_BusError(addr, BUS_ERROR_READ, BUS_ERROR_SIZE_BYTE, BUS_ERROR_ACCESS_DATA, 0);
return 0;
}
if (!buf[i]) {
return i;
}
}
return 0;
}
/**
* NF_NAME - emulator name
* Stack arguments are:
* - pointer to buffer for emulator name, and
* - uint32_t for its size
* subid == 1 -> emulator name includes also version information.
* Returns length of the name
*/
static bool nf_name(Uint32 stack, Uint32 subid, Uint32 *retval)
{
Uint32 ptr, len;
const char *str;
char *buf;
ptr = STMemory_ReadLong(stack);
len = STMemory_ReadLong(stack + SIZE_LONG);
LOG_TRACE(TRACE_NATFEATS, "NF_NAME[%d](0x%x, %d)\n", subid, ptr, len);
if ( !STMemory_CheckAreaType ( ptr, len, ABFLAG_RAM | ABFLAG_ROM ) ) {
M68000_BusError(ptr, BUS_ERROR_WRITE, BUS_ERROR_SIZE_BYTE, BUS_ERROR_ACCESS_DATA, 0);
return false;
}
if (subid == 1) {
str = PROG_NAME;
} else {
str = "Hatari";
}
buf = (char *)STMemory_STAddrToPointer ( ptr );
*retval = snprintf(buf, len, "%s", str);
return true;
}
/**
* NF_VERSION - NativeFeatures version
* returns version number
*/
static bool nf_version(Uint32 stack, Uint32 subid, Uint32 *retval)
{
LOG_TRACE(TRACE_NATFEATS, "NF_VERSION() -> 0x00010000\n");
*retval = 0x00010000;
return true;
}
/**
* NF_STDERR - print string to stderr
* Stack arguments are:
* - pointer to buffer containing the string
* Returns printed string length
*/
static bool nf_stderr(Uint32 stack, Uint32 subid, Uint32 *retval)
{
const char *str;
Uint32 ptr;
ptr = STMemory_ReadLong(stack);
LOG_TRACE(TRACE_NATFEATS, "NF_STDERR(0x%x)\n", ptr);
*retval = 0;
if (subid) {
/* unrecognized subid, nothing printed */
return true;
}
if (!mem_string_ok(ptr)) {
return false;
}
str = (const char *)STMemory_STAddrToPointer (ptr);
*retval = fprintf(stderr, "%s", str);
fflush(stderr);
return true;
}
/**
* NF_SHUTDOWN - reset or exit emulator
*
* Needs to be called from supervisor mode
*/
static bool nf_shutdown(Uint32 stack, Uint32 subid, Uint32 *retval)
{
const char *msg;
LOG_TRACE(TRACE_NATFEATS, "NF_SHUTDOWN[%d]()\n", subid);
switch (subid) {
case 1: /* warm reset */
msg = "warm reset";
Reset_Warm();
/* Some infos can change after 'reset' */
Statusbar_UpdateInfo();
break;
case 2: /* cold reset (clear all) */
msg = "cold reset";
Reset_Cold();
/* Some infos can change after 'reset' */
Statusbar_UpdateInfo();
break;
case 0: /* shutdown */
case 3: /* poweroff */
msg = "poweroff";
ConfigureParams.Log.bConfirmQuit = false;
Main_RequestQuit(0);
break;
default:
/* unrecognized subid -> no-op */
return true;
}
fprintf(stderr, "NatFeats: %s\n", msg);
return true;
}
/* ----------------------------------
* Native Features specific to Hatari
*/
/**
* NF_EXIT - exit emulator with given exit code
* Stack arguments are:
* - emulator's int32_t exit value
*/
static bool nf_exit(Uint32 stack, Uint32 subid, Uint32 *retval)
{
Sint32 exitval;
ConfigureParams.Log.bConfirmQuit = false;
exitval = STMemory_ReadLong(stack);
LOG_TRACE(TRACE_NATFEATS, "NF_EXIT(%d)\n", exitval);
Main_RequestQuit(exitval);
fprintf(stderr, "NatFeats: exit(%d)\n", exitval);
return true;
}
/**
* NF_DEBUGGER - invoke debugger
*/
static bool nf_debugger(Uint32 stack, Uint32 subid, Uint32 *retval)
{
LOG_TRACE(TRACE_NATFEATS, "NF_DEBUGGER()\n");
DebugUI(REASON_PROGRAM);
return true;
}
/**
* NF_FASTFORWARD - set fast forward state
* Stack arguments are:
* - state 0: off, >=1: on
* Returns previous fast-forward value
*/
static bool nf_fastforward(Uint32 stack, Uint32 subid, Uint32 *retval)
{
Uint32 val;
*retval = ConfigureParams.System.bFastForward;
if (subid) {
/* unrecognized sub id -> no-op */
val = *retval;
} else {
val = STMemory_ReadLong(stack);
}
LOG_TRACE(TRACE_NATFEATS, "NF_FASTFORWARD(%d -> %d)\n", *retval, val);
ConfigureParams.System.bFastForward = ( val ? true : false );
return true;
}
#if NF_COMMAND
/**
* NF_COMMAND - execute Hatari (cli / debugger) command
* Stack arguments are:
* - pointer to command string
*/
static bool nf_command(Uint32 stack, Uint32 subid, Uint32 *retval)
{
const char *buffer;
Uint32 ptr;
if (subid) {
/* unrecognized sub id -> no-op */
return true;
}
ptr = STMemory_ReadLong(stack);
if (!mem_string_ok(ptr)) {
return false;
}
buffer = (const char *)STMemory_STAddrToPointer(ptr);
LOG_TRACE(TRACE_NATFEATS, "NF_COMMAND(0x%x \"%s\")\n", ptr, buffer);
Control_ProcessBuffer(buffer);
return true;
}
#endif
/* ---------------------------- */
#define FEATNAME_MAX 16
static const struct {
const char *name; /* feature name */
bool super; /* should be called only in supervisor mode */
bool (*cb)(Uint32 stack, Uint32 subid, Uint32 *retval);
} features[] = {
#if NF_COMMAND
{ "NF_COMMAND", false, nf_command },
#endif
{ "NF_NAME", false, nf_name },
{ "NF_VERSION", false, nf_version },
{ "NF_STDERR", false, nf_stderr },
{ "NF_SHUTDOWN", true, nf_shutdown },
{ "NF_EXIT", false, nf_exit },
{ "NF_DEBUGGER", false, nf_debugger },
{ "NF_FASTFORWARD", false, nf_fastforward }
#if defined(__linux__)
,{ "NF_SCSIDRV", true, nf_scsidrv }
#endif
};
/* macros from Aranym */
#define ID_SHIFT 20
#define IDX2MASTERID(idx) (((idx)+1) << ID_SHIFT)
#define MASTERID2IDX(id) (((id) >> ID_SHIFT)-1)
#define MASKOUTMASTERID(id) ((id) & ((1L << ID_SHIFT)-1))
/**
* Set retval to internal ID for requested Native Feature,
* or zero if feature is unknown/unsupported.
*
* Return true if caller is to proceed normally,
* false if there was an exception.
*/
bool NatFeat_ID(Uint32 stack, Uint32 *retval)
{
const char *name;
Uint32 ptr;
int i;
ptr = STMemory_ReadLong(stack);
if ( !STMemory_CheckAreaType ( ptr, FEATNAME_MAX, ABFLAG_RAM | ABFLAG_ROM ) ) {
M68000_BusError(ptr, BUS_ERROR_READ, BUS_ERROR_SIZE_BYTE, BUS_ERROR_ACCESS_DATA, 0);
return false;
}
name = (const char *)STMemory_STAddrToPointer ( ptr );
LOG_TRACE(TRACE_NATFEATS, "NF ID(0x%x \"%s\")\n", ptr, name);
for (i = 0; i < ARRAY_SIZE(features); i++) {
if (strcmp(features[i].name, name) == 0) {
*retval = IDX2MASTERID(i);
return true;
}
}
/* unknown feature */
*retval = 0;
return true;
}
/**
* Do given Native Feature, if it is supported
* and set 'retval' accordingly.
*
* Return true if caller is to proceed normally,
* false if there was an exception.
*/
bool NatFeat_Call(Uint32 stack, bool super, Uint32 *retval)
{
Uint32 subid = STMemory_ReadLong(stack);
unsigned int idx = MASTERID2IDX(subid);
subid = MASKOUTMASTERID(subid);
if (idx >= ARRAY_SIZE(features)) {
LOG_TRACE(TRACE_NATFEATS, "ERROR: invalid NF ID %d requested\n", idx);
return true; /* undefined */
}
if (features[idx].super && !super) {
LOG_TRACE(TRACE_NATFEATS, "ERROR: NF function %d called without supervisor mode\n", idx);
M68000_Exception(8, M68000_EXC_SRC_CPU);
return false;
}
stack += SIZE_LONG;
return features[idx].cb(stack, subid, retval);
}