-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathnatfeats.c
212 lines (189 loc) · 3.97 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
/*
* natfeat.c - NatFeats API examples
*
* Copyright (c) 2014-2016, 2019 by Eero Tamminen
*
* NF initialization & calling is based on EmuTOS code,
* Copyright (c) 2001-2003 The EmuTOS development team
*
* This file is distributed under the GPL, version 2 or at your
* option any later version. See doc/license.txt for details.
*/
#if __GNUC__
# include <mint/osbind.h>
#else /* VBCC/AHCC/Pure-C */
# include <tos.h>
#endif
#include "natfeats.h"
#define MSG_NF_MISSING \
"\r\nStart Hatari with option:\r\n\t--natfeats yes\r\n"
/* NatFeats available & initialized */
static int nf_ok;
/* handles for NF features that may be used more frequently */
static long nfid_print, nfid_debugger, nfid_fastforward;
/* API documentation is in natfeats.h header */
int nf_init(void)
{
void *sup = (void*)Super(0);
nf_ok = detect_nf();
Super(sup);
if (nf_ok) {
/* initialize commonly used handles */
nfid_print = nf_id("NF_STDERR");
nfid_debugger = nf_id("NF_DEBUGGER");
nfid_fastforward = nf_id("NF_FASTFORWARD");
} else {
(void)Cconws("Native Features initialization failed!\r\n");
}
return nf_ok;
}
long nf_version(void)
{
long id;
if(nf_ok && (id = nf_id("NF_VERSION"))) {
return nf_call(id);
} else {
(void)Cconws("NF_VERSION unavailable!\r\n");
return 0;
}
}
static long getname(char *buf, long len, int subid)
{
long id;
if (nf_ok && (id = nf_id("NF_NAME"))) {
return nf_call(id | subid, buf, (long)len);
} else {
(void)Cconws("NF_NAME unavailable!\r\n");
return 0;
}
}
long nf_name(char *buf, long len)
{
return getname(buf, len, 0x0);
}
long nf_fullname(char *buf, long len)
{
return getname(buf, len, 0x1);
}
long nf_print(const char *text)
{
if (nfid_print) {
return nf_call(nfid_print, text);
} else {
(void)Cconws("NF_STDERR unavailable!\r\n");
return 0;
}
}
long nf_debugger(void)
{
if (nfid_debugger) {
return nf_call(nfid_debugger);
} else {
(void)Cconws("NF_DEBUGGER unavailable!\r\n");
return 0;
}
}
long nf_fastforward(long enabled)
{
if (nfid_fastforward) {
return nf_call(nfid_fastforward, enabled);
} else {
(void)Cconws("NF_FASTFORWARD unavailable!\r\n");
return 0;
}
}
static void halt_reset(int subid)
{
long id;
if(nf_ok && (id = nf_id("NF_SHUTDOWN"))) {
void *sup = (void*)Super(0);
/* needs to be called in supervisor mode */
nf_call(id | subid);
Super(sup);
} else {
(void)Cconws("NF_SHUTDOWN unavailable!\r\n");
}
}
void nf_shutdown(void)
{
halt_reset(0x0);
}
void nf_reset(void)
{
halt_reset(0x1);
}
void nf_reset_cold(void)
{
halt_reset(0x2);
}
void nf_poweroff(void)
{
halt_reset(0x3);
}
void nf_exit(long exitval)
{
long id;
if(nf_ok && (id = nf_id("NF_EXIT"))) {
nf_call(id, exitval);
} else {
/* NF_EXIT is Hatari specific, NF_SHUTDOWN isn't */
(void)Cconws("NF_EXIT unavailable, trying NF_SHUTDOWN...\r\n");
nf_shutdown();
}
}
#ifdef TEST
/* show emulator name */
static void nf_showname(void)
{
long chars;
char buffer[64];
chars = nf_fullname(buffer, (long)(sizeof(buffer)-2));
buffer[chars++] = '\n';
buffer[chars++] = '\0';
nf_print(buffer);
}
#if 1
# define nf_showversion(void)
#else
/* printf requires adding ahcstart.o & ahccstdi.lib to nf_ahcc.prj,
* but those need features not supported by Hatari's dummy TOS
* (used in automated tests).
*
* Seeing the output requires "--conout 2" Hatari option, as
* emulated display doesn't have time to refresh before exit.
*/
#include <stdio.h>
static void nf_showversion(void)
{
long version = nf_version();
printf("NF API version: 0x%x\n", version);
}
#endif
static int wait_key(void)
{
while (Cconis()) {
Cconin();
}
(void)Cconws("\r\n<press key>\r\n");
return Cconin();
}
int main()
{
long old_ff;
if (!nf_init()) {
(void)Cconws(MSG_NF_MISSING);
wait_key();
return 1;
}
old_ff = nf_fastforward(1);
nf_print("Emulator name:\n");
nf_showname();
nf_print("Invoking debugger...\n");
nf_debugger();
nf_print("Restoring fastforward & shutting down...\n");
nf_fastforward(old_ff);
nf_exit(0);
wait_key();
return 0;
}
#endif