-
Notifications
You must be signed in to change notification settings - Fork 0
/
natfeats.c
191 lines (164 loc) · 3.61 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
/*
* natfeat.c - NatFeats API examples
*
* Copyright (c) 2014 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.
*/
#ifdef __cplusplus
extern "C" {
#endif
#if __GNUC__
# include <mint/osbind.h>
#else /* VBCC/AHCC/Pure-C */
# include <tos.h>
#endif
#include "natfeats.h"
#include <stdarg.h>
#include <stdlib.h>
/* NatFeats available & initialized */
static int nf_ok;
/* handles for NF features that may be used more frequently */
static long nfid_print;
static long nfid_debugger;
static long 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_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;
}
}
void nf_shutdown(void)
{
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);
Super(sup);
} else {
(void) Cconws("NF_SHUTDOWN unavailable!\r\n");
}
}
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();
}
}
void nf_putchar(int c)
{
char s[2];
s[0] = (char) c;
s[1] = '\0';
nf_print(s);
}
extern int doprnt(int (*)(int, void *), char *, const char *, va_list);
int nf_printf(const char *fmt, ...)
{
int ret;
va_list va;
va_start(va, fmt);
if (! nf_ok)
nf_init();
ret = doprnt((int (*)(int, void *)) nf_putchar, NULL, fmt, va);
va_end(va);
return ret;
}
#ifdef TEST
/* show emulator name */
static void nf_showname(void)
{
long id;
if (nf_ok && (id = nf_id("NF_NAME"))) {
long chars;
char buffer[64];
id |= 0x0001; /* name + version */
chars = nf_call(id, buffer, sizeof(buffer)-2);
buffer[chars++] = '\n';
buffer[chars++] = '\0';
nf_print(buffer);
} else {
(void) Cconws("NF_NAME unavailable!\r\n");
}
}
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()) {
wait_key();
return 1;
}
old_ff = nf_fastforward(1);
nf_print("Emulator name:\n");
nf_showname();
nf_print("Shutting down...\n");
nf_fastforward(old_ff);
nf_exit(0);
wait_key();
return 0;
}
#endif
#ifdef __cplusplus
}
#endif