-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathLibQuickNES.cs
248 lines (240 loc) · 9.43 KB
/
LibQuickNES.cs
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
using System.Runtime.InteropServices;
using BizHawk.BizInvoke;
namespace BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES
{
public abstract class LibQuickNES
{
/// <summary>
/// create a new quicknes context
/// </summary>
/// <returns>NULL on failure</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_new();
/// <summary>
/// destroy a quicknes context
/// </summary>
/// <param name="e">context previously returned from qn_new()</param>
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_delete(IntPtr e);
/// <summary>
/// load an ines file
/// </summary>
/// <param name="e">context</param>
/// <param name="data">file</param>
/// <param name="length">length of file</param>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_loadines(IntPtr e, byte[] data, int length);
/// <summary>
/// set audio sample rate
/// </summary>
/// <param name="e">context</param>
/// <param name="rate">hz</param>
/// <returns>string error</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_set_sample_rate(IntPtr e, int rate);
/// <summary>
/// emulate a single frame
/// </summary>
/// <param name="e">context</param>
/// <param name="pad1">pad 1 input</param>
/// <param name="pad2">pad 2 input</param>
/// <returns>string error</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_emulate_frame(IntPtr e, uint pad1, uint pad2, byte arkanoidPos, byte arkanoidFire, uint controllerType);
/// <summary>
/// blit to rgb32
/// </summary>
/// <param name="e">Context</param>
/// <param name="dest">rgb32 256x240 packed</param>
/// <param name="colors">rgb32 colors, 512 of them</param>
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_blit(IntPtr e, int[] dest, int[] colors, int cropleft, int croptop, int cropright, int cropbottom);
/// <summary>
/// get quicknes's default palette
/// </summary>
/// <returns>1536 bytes suitable for qn_blit</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_get_default_colors();
/// <summary>
/// get number of times joypad was read in most recent frame
/// </summary>
/// <param name="e">context</param>
/// <returns>0 means lag</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract int qn_get_joypad_read_count(IntPtr e);
/// <summary>
/// get audio info for most recent frame
/// </summary>
/// <param name="e">context</param>
/// <param name="sample_count">number of samples actually created</param>
/// <param name="chan_count">1 for mono, 2 for stereo</param>
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_get_audio_info(IntPtr e, ref int sample_count, ref int chan_count);
/// <summary>
/// get audio for most recent frame. must not be called more than once per frame!
/// </summary>
/// <param name="e">context</param>
/// <param name="dest">sample buffer</param>
/// <param name="max_samples">length to read into sample buffer</param>
/// <returns>length actually read</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract int qn_read_audio(IntPtr e, short[] dest, int max_samples);
/// <summary>
/// reset the console
/// </summary>
/// <param name="e">context</param>
/// <param name="hard">true for powercycle, false for reset button</param>
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_reset(IntPtr e, bool hard);
/// <summary>
/// get the required byte size of a savestate
/// </summary>
/// <param name="e">context</param>
/// <param name="size">size is returned</param>
/// <returns>string error</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_state_size(IntPtr e, ref int size);
/// <summary>
/// save state to buffer
/// </summary>
/// <param name="e">context</param>
/// <param name="dest">buffer</param>
/// <param name="size">length of buffer</param>
/// <returns>string error</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_state_save(IntPtr e, byte[] dest, int size);
/// <summary>
/// load state from buffer
/// </summary>
/// <param name="e">context</param>
/// <param name="src">buffer</param>
/// <param name="size">length of buffer</param>
/// <returns>string error</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_state_load(IntPtr e, byte[] src, int size);
/// <summary>
/// query battery ram state
/// </summary>
/// <param name="e">context</param>
/// <returns>true if battery backup sram exists</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract bool qn_has_battery_ram(IntPtr e);
/// <summary>
/// query battery ram size
/// </summary>
/// <param name="e">context</param>
/// <param name="size">size is returned</param>
/// <returns>string error</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_battery_ram_size(IntPtr e, ref int size);
/// <summary>
/// save battery ram to buffer
/// </summary>
/// <param name="e">context</param>
/// <param name="dest">buffer</param>
/// <param name="size">size</param>
/// <returns>string error</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_battery_ram_save(IntPtr e, byte[] dest, int size);
/// <summary>
/// load battery ram from buffer
/// </summary>
/// <param name="e">context</param>
/// <param name="src">buffer</param>
/// <param name="size">size</param>
/// <returns>string error</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_battery_ram_load(IntPtr e, byte[] src, int size);
/// <summary>
/// clear battery ram
/// </summary>
/// <param name="e">context</param>
/// <returns>string error</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_battery_ram_clear(IntPtr e);
/// <summary>
/// set sprite limit; does not affect emulation
/// </summary>
/// <param name="e">context</param>
/// <param name="n">0 to hide, 8 for normal, 64 for all</param>
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_set_sprite_limit(IntPtr e, int n);
/// <summary>
/// get memory area for debugging
/// </summary>
/// <param name="e">Context</param>
[BizImport(CallingConvention.Cdecl)]
public abstract bool qn_get_memory_area(IntPtr e, int which, ref IntPtr data, ref int size, ref bool writable, ref IntPtr name);
/// <summary>
/// peek the system bus
/// </summary>
/// <param name="e">Context</param>
/// <param name="addr">0000:ffff, but non-ram/rom addresses won't work</param>
[BizImport(CallingConvention.Cdecl)]
public abstract byte qn_peek_prgbus(IntPtr e, int addr);
/// <summary>
/// poke the system bus
/// </summary>
/// <param name="e">Context</param>
/// <param name="addr">0000:ffff, but non-ram/rom addresses won't work</param>
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_poke_prgbus(IntPtr e, int addr, byte val);
/// <summary>
/// get internal registers
/// </summary>
/// <param name="e">Context</param>
/// <param name="dest">a, x, y, sp, pc, p</param>
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_get_cpuregs(IntPtr e, [Out] int[] dest);
/// <summary>
/// get the mapper that's loaded
/// </summary>
/// <param name="e">Context</param>
/// <param name="number">recieves mapper number</param>
/// <returns>mapper name</returns>
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_get_mapper(IntPtr e, ref int number);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void TraceCallback(IntPtr data);
/// <summary>
/// set a trace callback to be run on each cycle
/// </summary>
/// <param name="e">Context</param>
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_set_tracecb(IntPtr e, TraceCallback cb);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void input_cb();
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_set_input_callback(input_cb cb);
[BizImport(CallingConvention.Cdecl)]
public abstract byte qn_get_reg2000(IntPtr e);
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_get_palmem(IntPtr e);
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr qn_get_oammem(IntPtr e);
[BizImport(CallingConvention.Cdecl)]
public abstract byte qn_peek_ppu(IntPtr e, int addr);
[BizImport(CallingConvention.Cdecl)]
public abstract void qn_peek_ppubus(IntPtr e, byte[] dest);
/// <summary>
/// handle "string error" as returned by some quicknes functions
/// </summary>
public static void ThrowStringError(IntPtr p)
{
if (p == IntPtr.Zero)
return;
string s = Marshal.PtrToStringAnsi(p);
if (s == "Unsupported mapper"
|| s == "Not an iNES file" // Not worth making a new exception for the iNES error, they ultimately are the same problem
|| s == " truncated file" // This is a garbage rom not worth anyone's time but at least NesHawk handles these better, and these occur before the core has a chance to assess an unsupported mapper
)
{
throw new Common.UnsupportedGameException(CoreNames.QuickNes + " unsupported mapper");
}
else
{
throw new InvalidOperationException($"{nameof(LibQuickNES)} error: {s}");
}
}
}
}