Boy.xs

filter: Statementstmt 80.1% 259 / 323 Branchbran 48.3% 86 / 178 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.8% 345 / 501 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/Core/random.c ↑ Summary src/Core/rewind.c »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include <string.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include "gb.h"
10 #include "bootroms.h"
11  
12 /* ------------------------------------------------------------------ *
13 * Same::Boy - an XS bridge over the vendored SameBoy Core.
14 *
15 * The Perl object is a blessed scalar holding a pointer to one sb_t,
16 * which owns a GB_gameboy_t (as its FIRST member, so a GB_gameboy_t* can
17 * be cast back to sb_t* inside callbacks), a framebuffer the Core renders
18 * into, and a growable capture buffer for APU audio samples. All of the
19 * emulator's behaviour lives here: the OO surface, string->enum mapping,
20 * file I/O and argument handling are done in C rather than in the .pm.
21 * ------------------------------------------------------------------ */
22  
23 /* Largest SameBoy screen (SGB with border) is 256x224. */
24 #define SB_FB_MAX (256 * 224)
25  
26 typedef struct {
27 GB_gameboy_t gb; /* MUST be first member */
28 uint32_t fb[SB_FB_MAX];
29 int16_t *audio; /* interleaved L,R int16 */
30 size_t audio_len; /* samples used (in int16 units) */
31 size_t audio_cap; /* samples allocated (in int16 units) */
32 int capture; /* whether to store APU samples */
33 char model[8]; /* model string passed to the constructor */
34 } sb_t;
35  
36 /* Extract the sb_t* from a blessed scalar reference. */
37 static sb_t *
38 343 sb_from_sv(pTHX_ SV *self)
39 {
40 343 ▶ if (!SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) 0 343 343
41 0 croak("Same::Boy: not a Same::Boy object");
42 343 return INT2PTR(sb_t *, SvIV(SvRV(self)));
43 }
44  
45 /* ASCII case-insensitive compare; b is a lower-case literal. */
46 static int
47 21 sb_ieq(const char *a, const char *b)
48 {
49 49 ▶ while (*a && *b) {
Branch: 2 branches
BranchTrueFalseTotal
while (*a && *b) { 44 5 49
while (*a && *b) { 44 0 44
50 44 unsigned char ca = (unsigned char)*a++;
51 44 unsigned char cb = (unsigned char)*b++;
52 44 ▶ if (ca >= 'A' && ca <= 'Z') ca += 32;
Branch: 2 branches
BranchTrueFalseTotal
if (ca >= 'A' && ca <= 'Z') ca += 32; 44 0 44
if (ca >= 'A' && ca <= 'Z') ca += 32; 0 44 44
53 44 ▶ if (ca != cb) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ca != cb) return 0; 16 28 44
54 }
55 5 ▶ return *a == 0 && *b == 0;
Branch: 2 branches
BranchTrueFalseTotal
return *a == 0 && *b == 0; 5 0 5
return *a == 0 && *b == 0; 5 0 5
56 }
57  
58 /* Lower-case src into dst (ASCII), NUL-terminated within dsz. */
59 static void
60 16 sb_strlower(const char *src, char *dst, size_t dsz)
61 {
62 16 size_t i = 0;
63 64 ▶ for (; src[i] && i + 1 < dsz; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; src[i] && i + 1 < dsz; i++) { 48 16 64
for (; src[i] && i + 1 < dsz; i++) { 48 0 48
64 48 unsigned char c = (unsigned char)src[i];
65 48 ▶ dst[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : (char)c;
Branch: 2 branches
BranchTrueFalseTotal
dst[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : (char)c; 48 0 48
dst[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : (char)c; 0 48 48
66 }
67 16 dst[i] = '\0';
68 16 }
69  
70 /* ---- string -> enum maps (croak on an unknown value) -------------------- */
71  
72 /* Takes an already-lower-cased model string. */
73 static int
74 16 sb_model_id(pTHX_ const char *m)
75 {
76 16 ▶ if (strEQ(m, "dmg")) return 0x002; /* GB_MODEL_DMG_B */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(m, "dmg")) return 0x002; /* GB_MODEL_DMG_B */ 3 13 16
77 13 ▶ if (strEQ(m, "mgb")) return 0x100; /* GB_MODEL_MGB */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(m, "mgb")) return 0x100; /* GB_MODEL_MGB */ 0 13 13
78 13 ▶ if (strEQ(m, "sgb")) return 0x004; /* GB_MODEL_SGB */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(m, "sgb")) return 0x004; /* GB_MODEL_SGB */ 0 13 13
79 13 ▶ if (strEQ(m, "sgb2")) return 0x101; /* GB_MODEL_SGB2 */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(m, "sgb2")) return 0x101; /* GB_MODEL_SGB2 */ 0 13 13
80 13 ▶ if (strEQ(m, "cgb")) return 0x205; /* GB_MODEL_CGB_E */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(m, "cgb")) return 0x205; /* GB_MODEL_CGB_E */ 13 0 13
81 0 croak("Same::Boy: unknown model '%s'", m);
82 return 0; /* not reached */
83 }
84  
85 static int
86 2 sb_key_index(pTHX_ const char *k)
87 {
88 2 ▶ if (sb_ieq(k, "right")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(k, "right")) return 0; 0 2 2
89 2 ▶ if (sb_ieq(k, "left")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(k, "left")) return 1; 0 2 2
90 2 ▶ if (sb_ieq(k, "up")) return 2;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(k, "up")) return 2; 0 2 2
91 2 ▶ if (sb_ieq(k, "down")) return 3;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(k, "down")) return 3; 0 2 2
92 2 ▶ if (sb_ieq(k, "a")) return 4;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(k, "a")) return 4; 2 0 2
93 0 ▶ if (sb_ieq(k, "b")) return 5;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(k, "b")) return 5; 0 0 0
94 0 ▶ if (sb_ieq(k, "select")) return 6;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(k, "select")) return 6; 0 0 0
95 0 ▶ if (sb_ieq(k, "start")) return 7;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(k, "start")) return 7; 0 0 0
96 0 croak("Same::Boy: unknown key '%s'", k);
97 return -1; /* not reached */
98 }
99  
100 static int
101 1 sb_color_correction(pTHX_ const char *mode)
102 {
103 1 ▶ if (sb_ieq(mode, "disabled")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "disabled")) return 0; 0 1 1
104 1 ▶ if (sb_ieq(mode, "correct_curves")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "correct_curves")) return 1; 0 1 1
105 1 ▶ if (sb_ieq(mode, "modern_balanced")) return 2;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "modern_balanced")) return 2; 1 0 1
106 0 ▶ if (sb_ieq(mode, "modern_boost_contrast")) return 3;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "modern_boost_contrast")) return 3; 0 0 0
107 0 ▶ if (sb_ieq(mode, "reduce_contrast")) return 4;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "reduce_contrast")) return 4; 0 0 0
108 0 ▶ if (sb_ieq(mode, "low_contrast")) return 5;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "low_contrast")) return 5; 0 0 0
109 0 ▶ if (sb_ieq(mode, "modern_accurate")) return 6;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "modern_accurate")) return 6; 0 0 0
110 0 croak("Same::Boy: unknown color correction '%s'", mode);
111 return -1; /* not reached */
112 }
113  
114 static int
115 1 sb_highpass_mode(pTHX_ const char *mode)
116 {
117 1 ▶ if (sb_ieq(mode, "off")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "off")) return 0; 0 1 1
118 1 ▶ if (sb_ieq(mode, "accurate")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "accurate")) return 1; 1 0 1
119 0 ▶ if (sb_ieq(mode, "remove_dc_offset")) return 2;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "remove_dc_offset")) return 2; 0 0 0
120 0 croak("Same::Boy: unknown highpass mode '%s'", mode);
121 return -1; /* not reached */
122 }
123  
124 static int
125 0 sb_rtc_mode_id(pTHX_ const char *mode)
126 {
127 0 ▶ if (sb_ieq(mode, "sync_to_host")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "sync_to_host")) return 0; 0 0 0
128 0 ▶ if (sb_ieq(mode, "accurate")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(mode, "accurate")) return 1; 0 0 0
129 0 croak("Same::Boy: unknown rtc mode '%s'", mode);
130 return -1; /* not reached */
131 }
132  
133 static int
134 2 sb_dmg_palette(pTHX_ const char *name)
135 {
136 2 ▶ if (sb_ieq(name, "grey")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(name, "grey")) return 0; 0 2 2
137 2 ▶ if (sb_ieq(name, "dmg")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(name, "dmg")) return 1; 1 1 2
138 1 ▶ if (sb_ieq(name, "mgb")) return 2;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(name, "mgb")) return 2; 0 1 1
139 1 ▶ if (sb_ieq(name, "gbl")) return 3;
Branch: 1 branch
BranchTrueFalseTotal
if (sb_ieq(name, "gbl")) return 3; 0 1 1
140 1 croak("Same::Boy: unknown palette '%s'", name);
141 return -1; /* not reached */
142 }
143  
144 /* ---- file helpers ------------------------------------------------------- */
145  
146 /* Read an entire file into a fresh mortal string SV; croak on failure. */
147 static SV *
148 2 sb_slurp(pTHX_ const char *path)
149 {
150 2 FILE *f = fopen(path, "rb");
151 long n;
152 size_t got;
153 SV *sv;
154  
155 2 ▶ if (!f)
Branch: 1 branch
BranchTrueFalseTotal
if (!f) 0 2 2
156 0 croak("Same::Boy: cannot read '%s': %s", path, strerror(errno));
157  
158 2 ▶ if (fseek(f, 0, SEEK_END) != 0 || (n = ftell(f)) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (fseek(f, 0, SEEK_END) != 0 || (n = ftell(f)) < 0) { 2 0 2
if (fseek(f, 0, SEEK_END) != 0 || (n = ftell(f)) < 0) { 0 2 2
159 0 fclose(f);
160 0 croak("Same::Boy: cannot read '%s': %s", path, strerror(errno));
161 }
162 2 fseek(f, 0, SEEK_SET);
163  
164 2 ▶ sv = newSV(n ? (STRLEN)n : 1);
Branch: 1 branch
BranchTrueFalseTotal
sv = newSV(n ? (STRLEN)n : 1); 2 0 2
165 2 SvPOK_on(sv);
166 2 got = fread(SvPVX(sv), 1, (size_t)n, f);
167 2 fclose(f);
168 2 SvCUR_set(sv, got);
169 2 SvPVX(sv)[got] = '\0';
170 2 return sv_2mortal(sv);
171 }
172  
173 /* Write the bytes of data to path; croak on failure. */
174 static void
175 2 sb_spew(pTHX_ const char *path, SV *data)
176 {
177 STRLEN len;
178 2 const char *buf = SvPV(data, len);
179 2 FILE *f = fopen(path, "wb");
180  
181 2 ▶ if (!f)
Branch: 1 branch
BranchTrueFalseTotal
if (!f) 0 2 2
182 0 croak("Same::Boy: cannot write '%s': %s", path, strerror(errno));
183 2 ▶ if (len && fwrite(buf, 1, len, f) != len) {
Branch: 2 branches
BranchTrueFalseTotal
if (len && fwrite(buf, 1, len, f) != len) { 2 0 2
if (len && fwrite(buf, 1, len, f) != len) { 0 2 2
184 0 fclose(f);
185 0 croak("Same::Boy: cannot write '%s': %s", path, strerror(errno));
186 }
187 2 fclose(f);
188 2 }
189  
190 /* ---- Core callbacks ----------------------------------------------------- */
191  
192 /* Core asks us to pack (r,g,b) into a pixel word; use 0x00RRGGBB. */
193 static uint32_t
194 1435 sb_rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
195 {
196 PERL_UNUSED_ARG(gb);
197 1435 return ((uint32_t)r << 16) | ((uint32_t)g << 8) | (uint32_t)b;
198 }
199  
200 static void
201 291 sb_vblank(GB_gameboy_t *gb, GB_vblank_type_t type)
202 {
203 PERL_UNUSED_ARG(gb);
204 PERL_UNUSED_ARG(type);
205 291 }
206  
207 /* APU sample callback: append one stereo frame to the capture buffer. */
208 static void
209 212183 sb_sample(GB_gameboy_t *gb, GB_sample_t *sample)
210 {
211 212183 sb_t *s = (sb_t *)gb; /* gb is the first member of sb_t */
212 212183 ▶ if (!s->capture) return; /* still emitted, just not stored */
Branch: 1 branch
BranchTrueFalseTotal
if (!s->capture) return; /* still emitted, just not stored */ 208491 3692 212183
213 3692 ▶ if (s->audio_len + 2 > s->audio_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->audio_len + 2 > s->audio_cap) { 1 3691 3692
214 1 ▶ size_t ncap = s->audio_cap ? s->audio_cap * 2 : 8192;
Branch: 1 branch
BranchTrueFalseTotal
size_t ncap = s->audio_cap ? s->audio_cap * 2 : 8192; 0 1 1
215 1 int16_t *n = (int16_t *)saferealloc(s->audio, ncap * sizeof(int16_t));
216 1 s->audio = n;
217 1 s->audio_cap = ncap;
218 }
219 3692 s->audio[s->audio_len++] = sample->left;
220 3692 s->audio[s->audio_len++] = sample->right;
221 }
222  
223 /* Allocate and initialise an sb_t for the given GB model. */
224 static sb_t *
225 16 sb_create(pTHX_ int model_id)
226 {
227 16 sb_t *s = (sb_t *)safecalloc(1, sizeof(sb_t));
228 size_t brlen;
229 const unsigned char *br;
230  
231 16 GB_init(&s->gb, (GB_model_t)model_id);
232 16 GB_set_rgb_encode_callback(&s->gb, sb_rgb_encode);
233 16 GB_set_vblank_callback(&s->gb, sb_vblank);
234 16 GB_set_pixels_output(&s->gb, s->fb);
235 /* Always install the sample callback so the APU emits (and thus resets)
236 * samples; capture is gated by s->capture so we don't store anything
237 * until the user enables audio. */
238 16 GB_set_sample_rate(&s->gb, 44100);
239 16 GB_apu_set_sample_callback(&s->gb, sb_sample);
240 /* Auto-load the matching open-source boot ROM. */
241 16 br = sb_default_bootrom(model_id, &brlen);
242 16 GB_load_boot_rom_from_buffer(&s->gb, br, brlen);
243 16 return s;
244 }
245  
246 MODULE = Same::Boy PACKAGE = Same::Boy
247  
248 PROTOTYPES: DISABLE
249  
250 BOOT:
251 {
252 8 HV *stash = gv_stashpv("Same::Boy", GV_ADD);
253 8 newCONSTSUB(stash, "KEY_RIGHT", newSViv(1 << 0));
254 8 newCONSTSUB(stash, "KEY_LEFT", newSViv(1 << 1));
255 8 newCONSTSUB(stash, "KEY_UP", newSViv(1 << 2));
256 8 newCONSTSUB(stash, "KEY_DOWN", newSViv(1 << 3));
257 8 newCONSTSUB(stash, "KEY_A", newSViv(1 << 4));
258 8 newCONSTSUB(stash, "KEY_B", newSViv(1 << 5));
259 8 newCONSTSUB(stash, "KEY_SELECT", newSViv(1 << 6));
260 8 newCONSTSUB(stash, "KEY_START", newSViv(1 << 7));
261 }
262  
263 # ---- construction -------------------------------------------------------
264  
265 SV *
266 new(class, ...)
267 SV *class
268 CODE:
269 {
270 int i;
271 char model_lc[8];
272 int model_id;
273 16 SV *rom_sv = NULL;
274 16 SV *boot_sv = NULL;
275 16 SV *rate_sv = NULL;
276 sb_t *s;
277 SV *obj;
278  
279 /* default model 'cgb' */
280 16 strcpy(model_lc, "cgb");
281  
282 45 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 29 16 45
283 29 const char *k = SvPV_nolen(ST(i));
284 29 SV *v = ST(i + 1);
285 45 ▶ if (strEQ(k, "model")) sb_strlower(SvPV_nolen(v), model_lc, sizeof(model_lc));
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "model")) sb_strlower(SvPV_nolen(v), model_lc, sizeof(model_lc)); 16 13 29
286 13 ▶ else if (strEQ(k, "rom")) rom_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "rom")) rom_sv = v; 13 0 13
287 0 ▶ else if (strEQ(k, "boot_rom")) boot_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "boot_rom")) boot_sv = v; 0 0 0
288 0 ▶ else if (strEQ(k, "sample_rate")) rate_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "sample_rate")) rate_sv = v; 0 0 0
289 }
290  
291 16 model_id = sb_model_id(aTHX_ model_lc);
292 16 s = sb_create(aTHX_ model_id);
293 16 strncpy(s->model, model_lc, sizeof(s->model) - 1);
294 16 s->model[sizeof(s->model) - 1] = '\0';
295  
296 /* boot ROM before ROM, matching the original constructor order */
297 16 ▶ if (boot_sv && SvOK(boot_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (boot_sv && SvOK(boot_sv)) { 0 16 16
if (boot_sv && SvOK(boot_sv)) { 0 0 0
298 STRLEN len;
299 0 SV *bytes = SvROK(boot_sv) ? SvRV(boot_sv)
300 0 ▶ : sb_slurp(aTHX_ SvPV_nolen(boot_sv));
Branch: 1 branch
BranchTrueFalseTotal
: sb_slurp(aTHX_ SvPV_nolen(boot_sv)); 0 0 0
301 0 const unsigned char *buf = (const unsigned char *)SvPV(bytes, len);
302 0 GB_load_boot_rom_from_buffer(&s->gb, buf, (size_t)len);
303 }
304 16 ▶ if (rom_sv && SvOK(rom_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rom_sv && SvOK(rom_sv)) { 13 3 16
if (rom_sv && SvOK(rom_sv)) { 13 0 13
305 STRLEN len;
306 13 SV *bytes = SvROK(rom_sv) ? SvRV(rom_sv)
307 13 ▶ : sb_slurp(aTHX_ SvPV_nolen(rom_sv));
Branch: 1 branch
BranchTrueFalseTotal
: sb_slurp(aTHX_ SvPV_nolen(rom_sv)); 13 0 13
308 13 const unsigned char *buf = (const unsigned char *)SvPV(bytes, len);
309 13 GB_load_rom_from_buffer(&s->gb, buf, (size_t)len);
310 }
311 16 ▶ if (rate_sv && SvTRUE(rate_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rate_sv && SvTRUE(rate_sv)) { 0 16 16
if (rate_sv && SvTRUE(rate_sv)) { 0 0 0
312 0 GB_set_sample_rate(&s->gb, (unsigned)SvUV(rate_sv));
313 0 s->capture = 1;
314 }
315  
316 16 obj = newSV(0);
317 16 sv_setiv(obj, PTR2IV(s));
318 16 RETVAL = sv_bless(newRV_noinc(obj), gv_stashsv(class, GV_ADD));
319 }
320 OUTPUT:
321 RETVAL
322  
323 SV *
324 model(self)
325 SV *self
326 CODE:
327 1 RETVAL = newSVpv(sb_from_sv(aTHX_ self)->model, 0);
328 OUTPUT:
329 RETVAL
330  
331 void
332 DESTROY(self)
333 SV *self
334 CODE:
335 {
336 sb_t *s;
337 16 ▶ if (SvROK(self) && (s = INT2PTR(sb_t *, SvIV(SvRV(self))))) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(self) && (s = INT2PTR(sb_t *, SvIV(SvRV(self))))) { 16 0 16
if (SvROK(self) && (s = INT2PTR(sb_t *, SvIV(SvRV(self))))) { 16 0 16
338 16 GB_free(&s->gb);
339 16 ▶ if (s->audio) safefree(s->audio);
Branch: 1 branch
BranchTrueFalseTotal
if (s->audio) safefree(s->audio); 1 15 16
340 16 safefree(s);
341 }
342 }
343  
344 # ---- ROM / boot ROM -----------------------------------------------------
345  
346 SV *
347 load_rom(self, src)
348 SV *self
349 SV *src
350 CODE:
351 {
352 3 sb_t *s = sb_from_sv(aTHX_ self);
353 STRLEN len;
354 3 ▶ SV *bytes = SvROK(src) ? SvRV(src) : sb_slurp(aTHX_ SvPV_nolen(src));
Branch: 1 branch
BranchTrueFalseTotal
SV *bytes = SvROK(src) ? SvRV(src) : sb_slurp(aTHX_ SvPV_nolen(src)); 3 0 3
355 3 const unsigned char *buf = (const unsigned char *)SvPV(bytes, len);
356 3 GB_load_rom_from_buffer(&s->gb, buf, (size_t)len);
357 3 RETVAL = SvREFCNT_inc(self);
358 }
359 OUTPUT:
360 RETVAL
361  
362 SV *
363 load_boot_rom(self, src)
364 SV *self
365 SV *src
366 CODE:
367 {
368 0 sb_t *s = sb_from_sv(aTHX_ self);
369 STRLEN len;
370 0 ▶ SV *bytes = SvROK(src) ? SvRV(src) : sb_slurp(aTHX_ SvPV_nolen(src));
Branch: 1 branch
BranchTrueFalseTotal
SV *bytes = SvROK(src) ? SvRV(src) : sb_slurp(aTHX_ SvPV_nolen(src)); 0 0 0
371 0 const unsigned char *buf = (const unsigned char *)SvPV(bytes, len);
372 0 GB_load_boot_rom_from_buffer(&s->gb, buf, (size_t)len);
373 0 RETVAL = SvREFCNT_inc(self);
374 }
375 OUTPUT:
376 RETVAL
377  
378 # ---- lifecycle / execution ----------------------------------------------
379  
380 SV *
381 reset(self)
382 SV *self
383 CODE:
384 {
385 0 sb_t *s = sb_from_sv(aTHX_ self);
386 0 GB_reset(&s->gb);
387 0 GB_set_pixels_output(&s->gb, s->fb);
388 0 RETVAL = SvREFCNT_inc(self);
389 }
390 OUTPUT:
391 RETVAL
392  
393 UV
394 run_frame(self)
395 SV *self
396 CODE:
397 291 RETVAL = (UV)GB_run_frame(&sb_from_sv(aTHX_ self)->gb);
398 OUTPUT:
399 RETVAL
400  
401 UV
402 run(self)
403 SV *self
404 CODE:
405 0 ▶ RETVAL = (UV)GB_run(&sb_from_sv(aTHX_ self)->gb);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)GB_run(&sb_from_sv(aTHX_ self)->gb); 0 0 0
406 OUTPUT:
407 RETVAL
408  
409 SV *
410 set_clock_multiplier(self, mult)
411 SV *self
412 double mult
413 CODE:
414 1 GB_set_clock_multiplier(&sb_from_sv(aTHX_ self)->gb, mult);
415 1 RETVAL = SvREFCNT_inc(self);
416 OUTPUT:
417 RETVAL
418  
419 SV *
420 set_turbo(self, on, no_frame_skip)
421 SV *self
422 bool on
423 bool no_frame_skip
424 CODE:
425 1 GB_set_turbo_mode(&sb_from_sv(aTHX_ self)->gb, on, no_frame_skip);
426 1 RETVAL = SvREFCNT_inc(self);
427 OUTPUT:
428 RETVAL
429  
430 SV *
431 set_rtc_mode(self, mode)
432 SV *self
433 const char *mode
434 CODE:
435 0 GB_set_rtc_mode(&sb_from_sv(aTHX_ self)->gb,
436 0 (GB_rtc_mode_t)sb_rtc_mode_id(aTHX_ mode));
437 0 RETVAL = SvREFCNT_inc(self);
438 OUTPUT:
439 RETVAL
440  
441 # ---- introspection ------------------------------------------------------
442  
443 int
444 is_cgb(self)
445 SV *self
446 CODE:
447 2 ▶ RETVAL = GB_is_cgb(&sb_from_sv(aTHX_ self)->gb) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = GB_is_cgb(&sb_from_sv(aTHX_ self)->gb) ? 1 : 0; 0 2 2
448 OUTPUT:
449 RETVAL
450  
451 int
452 is_sgb(self)
453 SV *self
454 CODE:
455 1 ▶ RETVAL = GB_is_sgb(&sb_from_sv(aTHX_ self)->gb) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = GB_is_sgb(&sb_from_sv(aTHX_ self)->gb) ? 1 : 0; 0 1 1
456 OUTPUT:
457 RETVAL
458  
459 SV *
460 rom_title(self)
461 SV *self
462 CODE:
463 {
464 char title[17];
465 1 memset(title, 0, sizeof(title));
466 1 GB_get_rom_title(&sb_from_sv(aTHX_ self)->gb, title);
467 1 RETVAL = newSVpv(title, 0);
468 }
469 OUTPUT:
470 RETVAL
471  
472 UV
473 rom_crc32(self)
474 SV *self
475 CODE:
476 1 ▶ RETVAL = (UV)GB_get_rom_crc32(&sb_from_sv(aTHX_ self)->gb);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)GB_get_rom_crc32(&sb_from_sv(aTHX_ self)->gb); 0 1 1
477 OUTPUT:
478 RETVAL
479  
480 # ---- screen -------------------------------------------------------------
481  
482 void
483 dimensions(self)
484 SV *self
485 PPCODE:
486 {
487 4 sb_t *s = sb_from_sv(aTHX_ self);
488 4 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 4 4
489 4 mPUSHu(GB_get_screen_width(&s->gb));
490 4 mPUSHu(GB_get_screen_height(&s->gb));
491 }
492  
493 SV *
494 pixels(self)
495 SV *self
496 CODE:
497 {
498 7 sb_t *s = sb_from_sv(aTHX_ self);
499 7 unsigned w = GB_get_screen_width(&s->gb);
500 7 unsigned h = GB_get_screen_height(&s->gb);
501 7 RETVAL = newSVpvn((const char *)s->fb,
502 (STRLEN)w * h * sizeof(uint32_t));
503 }
504 OUTPUT:
505 RETVAL
506  
507 # Canvas-ready pixels: tightly packed R,G,B,A (A=255) bytes, w*h*4 of them,
508 # suitable for a JS ImageData buffer.
509 SV *
510 pixels_rgba(self)
511 SV *self
512 CODE:
513 {
514 2 sb_t *s = sb_from_sv(aTHX_ self);
515 2 unsigned w = GB_get_screen_width(&s->gb);
516 2 unsigned h = GB_get_screen_height(&s->gb);
517 2 STRLEN n = (STRLEN)w * h;
518 STRLEN i;
519 unsigned char *out;
520 2 RETVAL = newSV(n * 4 + 1);
521 2 SvPOK_on(RETVAL);
522 2 SvCUR_set(RETVAL, n * 4);
523 2 out = (unsigned char *)SvPVX(RETVAL);
524 46082 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 46080 2 46082
525 46080 uint32_t px = s->fb[i]; /* 0x00RRGGBB */
526 46080 out[i*4+0] = (px >> 16) & 0xFF;
527 46080 out[i*4+1] = (px >> 8) & 0xFF;
528 46080 out[i*4+2] = px & 0xFF;
529 46080 out[i*4+3] = 0xFF;
530 }
531 2 SvPVX(RETVAL)[n * 4] = '\0';
532 }
533 OUTPUT:
534 RETVAL
535  
536 SV *
537 set_color_correction(self, mode)
538 SV *self
539 const char *mode
540 CODE:
541 1 GB_set_color_correction_mode(&sb_from_sv(aTHX_ self)->gb,
542 1 (GB_color_correction_mode_t)sb_color_correction(aTHX_ mode));
543 1 RETVAL = SvREFCNT_inc(self);
544 OUTPUT:
545 RETVAL
546  
547 SV *
548 set_dmg_palette(self, name)
549 SV *self
550 const char *name
551 CODE:
552 {
553 2 sb_t *s = sb_from_sv(aTHX_ self);
554 const GB_palette_t *p;
555 2 switch (sb_dmg_palette(aTHX_ name)) {
556 1 case 1: p = &GB_PALETTE_DMG; break;
557 0 case 2: p = &GB_PALETTE_MGB; break;
558 0 case 3: p = &GB_PALETTE_GBL; break;
559 0 default: p = &GB_PALETTE_GREY; break;
560 }
561 1 GB_set_palette(&s->gb, p);
562 1 RETVAL = SvREFCNT_inc(self);
563 }
564 OUTPUT:
565 RETVAL
566  
567 # ---- audio --------------------------------------------------------------
568  
569 SV *
570 set_sample_rate(self, rate)
571 SV *self
572 unsigned rate
573 CODE:
574 {
575 2 sb_t *s = sb_from_sv(aTHX_ self);
576 /* Callback stays installed (see sb_create); toggle capture and, when
577 * enabling, apply the requested rate. */
578 2 ▶ if (rate) {
Branch: 1 branch
BranchTrueFalseTotal
if (rate) { 1 1 2
579 1 GB_set_sample_rate(&s->gb, rate);
580 1 s->capture = 1;
581 }
582 else {
583 1 s->capture = 0;
584 }
585 2 RETVAL = SvREFCNT_inc(self);
586 }
587 OUTPUT:
588 RETVAL
589  
590 SV *
591 set_highpass_filter(self, mode)
592 SV *self
593 const char *mode
594 CODE:
595 1 GB_set_highpass_filter_mode(&sb_from_sv(aTHX_ self)->gb,
596 1 (GB_highpass_mode_t)sb_highpass_mode(aTHX_ mode));
597 1 RETVAL = SvREFCNT_inc(self);
598 OUTPUT:
599 RETVAL
600  
601 # Drain captured audio: return interleaved int16 L,R as a packed string
602 # and reset the capture buffer.
603 SV *
604 samples(self)
605 SV *self
606 CODE:
607 {
608 4 sb_t *s = sb_from_sv(aTHX_ self);
609 8 RETVAL = s->audio_len
610 1 ? newSVpvn((const char *)s->audio, s->audio_len * sizeof(int16_t))
611 4 ▶ : newSVpvn("", 0);
Branch: 1 branch
BranchTrueFalseTotal
: newSVpvn("", 0); 1 3 4
612 4 s->audio_len = 0;
613 }
614 OUTPUT:
615 RETVAL
616  
617 # ---- input --------------------------------------------------------------
618  
619 SV *
620 press(self, button)
621 SV *self
622 const char *button
623 CODE:
624 1 GB_set_key_state(&sb_from_sv(aTHX_ self)->gb,
625 1 (GB_key_t)sb_key_index(aTHX_ button), true);
626 1 RETVAL = SvREFCNT_inc(self);
627 OUTPUT:
628 RETVAL
629  
630 SV *
631 release(self, button)
632 SV *self
633 const char *button
634 CODE:
635 1 GB_set_key_state(&sb_from_sv(aTHX_ self)->gb,
636 1 (GB_key_t)sb_key_index(aTHX_ button), false);
637 1 RETVAL = SvREFCNT_inc(self);
638 OUTPUT:
639 RETVAL
640  
641 SV *
642 press_for_player(self, button, player)
643 SV *self
644 const char *button
645 unsigned player
646 CODE:
647 0 GB_set_key_state_for_player(&sb_from_sv(aTHX_ self)->gb,
648 0 (GB_key_t)sb_key_index(aTHX_ button), player, true);
649 0 RETVAL = SvREFCNT_inc(self);
650 OUTPUT:
651 RETVAL
652  
653 SV *
654 release_for_player(self, button, player)
655 SV *self
656 const char *button
657 unsigned player
658 CODE:
659 0 GB_set_key_state_for_player(&sb_from_sv(aTHX_ self)->gb,
660 0 (GB_key_t)sb_key_index(aTHX_ button), player, false);
661 0 RETVAL = SvREFCNT_inc(self);
662 OUTPUT:
663 RETVAL
664  
665 SV *
666 set_key_mask(self, mask)
667 SV *self
668 int mask
669 CODE:
670 0 GB_set_key_mask(&sb_from_sv(aTHX_ self)->gb, (GB_key_mask_t)mask);
671 0 RETVAL = SvREFCNT_inc(self);
672 OUTPUT:
673 RETVAL
674  
675 # ---- battery-backed save RAM --------------------------------------------
676  
677 SV *
678 save_battery(self)
679 SV *self
680 CODE:
681 {
682 2 sb_t *s = sb_from_sv(aTHX_ self);
683 2 int size = GB_save_battery_size(&s->gb);
684 2 ▶ if (size <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (size <= 0) { 1 1 2
685 1 RETVAL = &PL_sv_undef;
686 }
687 else {
688 1 RETVAL = newSV(size);
689 1 SvPOK_on(RETVAL);
690 1 SvCUR_set(RETVAL, size);
691 1 GB_save_battery_to_buffer(&s->gb,
692 1 (uint8_t *)SvPVX(RETVAL), (size_t)size);
693 1 *SvEND(RETVAL) = '\0';
694 }
695 }
696 OUTPUT:
697 RETVAL
698  
699 SV *
700 load_battery(self, src)
701 SV *self
702 SV *src
703 CODE:
704 {
705 0 sb_t *s = sb_from_sv(aTHX_ self);
706 STRLEN len;
707 0 ▶ SV *bytes = SvROK(src) ? SvRV(src) : src;
Branch: 1 branch
BranchTrueFalseTotal
SV *bytes = SvROK(src) ? SvRV(src) : src; 0 0 0
708 0 const unsigned char *buf = (const unsigned char *)SvPV(bytes, len);
709 0 GB_load_battery_from_buffer(&s->gb, buf, (size_t)len);
710 0 RETVAL = SvREFCNT_inc(self);
711 }
712 OUTPUT:
713 RETVAL
714  
715 SV *
716 save_battery_to_file(self, path)
717 SV *self
718 const char *path
719 CODE:
720 {
721 1 sb_t *s = sb_from_sv(aTHX_ self);
722 1 int size = GB_save_battery_size(&s->gb);
723 SV *data;
724 1 ▶ if (size <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (size <= 0) 0 1 1
725 0 croak("Same::Boy: no battery-backed RAM to save");
726 1 data = sv_2mortal(newSV(size));
727 1 SvPOK_on(data);
728 1 SvCUR_set(data, size);
729 1 GB_save_battery_to_buffer(&s->gb, (uint8_t *)SvPVX(data), (size_t)size);
730 1 *SvEND(data) = '\0';
731 1 sb_spew(aTHX_ path, data);
732 1 RETVAL = SvREFCNT_inc(self);
733 }
734 OUTPUT:
735 RETVAL
736  
737 SV *
738 load_battery_from_file(self, path)
739 SV *self
740 const char *path
741 CODE:
742 {
743 1 sb_t *s = sb_from_sv(aTHX_ self);
744 STRLEN len;
745 1 SV *bytes = sb_slurp(aTHX_ path);
746 1 const unsigned char *buf = (const unsigned char *)SvPV(bytes, len);
747 1 GB_load_battery_from_buffer(&s->gb, buf, (size_t)len);
748 1 RETVAL = SvREFCNT_inc(self);
749 }
750 OUTPUT:
751 RETVAL
752  
753 # ---- save states --------------------------------------------------------
754  
755 SV *
756 save_state(self)
757 SV *self
758 CODE:
759 {
760 1 sb_t *s = sb_from_sv(aTHX_ self);
761 1 size_t size = GB_get_save_state_size(&s->gb);
762 1 RETVAL = newSV(size);
763 1 SvPOK_on(RETVAL);
764 1 SvCUR_set(RETVAL, size);
765 1 GB_save_state_to_buffer(&s->gb, (uint8_t *)SvPVX(RETVAL));
766 1 *SvEND(RETVAL) = '\0';
767 }
768 OUTPUT:
769 RETVAL
770  
771 SV *
772 load_state(self, src)
773 SV *self
774 SV *src
775 CODE:
776 {
777 1 sb_t *s = sb_from_sv(aTHX_ self);
778 STRLEN len;
779 1 ▶ SV *bytes = SvROK(src) ? SvRV(src) : src;
Branch: 1 branch
BranchTrueFalseTotal
SV *bytes = SvROK(src) ? SvRV(src) : src; 0 1 1
780 1 const unsigned char *buf = (const unsigned char *)SvPV(bytes, len);
781 1 int rc = GB_load_state_from_buffer(&s->gb, buf, (size_t)len);
782 1 ▶ if (rc != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) 0 1 1
783 0 croak("Same::Boy: load_state failed (rc=%d)", rc);
784 1 RETVAL = SvREFCNT_inc(self);
785 }
786 OUTPUT:
787 RETVAL
788  
789 SV *
790 save_state_to_file(self, path)
791 SV *self
792 const char *path
793 CODE:
794 {
795 1 sb_t *s = sb_from_sv(aTHX_ self);
796 1 size_t size = GB_get_save_state_size(&s->gb);
797 1 SV *data = sv_2mortal(newSV(size));
798 1 SvPOK_on(data);
799 1 SvCUR_set(data, size);
800 1 GB_save_state_to_buffer(&s->gb, (uint8_t *)SvPVX(data));
801 1 *SvEND(data) = '\0';
802 1 sb_spew(aTHX_ path, data);
803 1 RETVAL = SvREFCNT_inc(self);
804 }
805 OUTPUT:
806 RETVAL
807  
808 SV *
809 load_state_from_file(self, path)
810 SV *self
811 const char *path
812 CODE:
813 {
814 1 sb_t *s = sb_from_sv(aTHX_ self);
815 STRLEN len;
816 1 SV *bytes = sb_slurp(aTHX_ path);
817 1 const unsigned char *buf = (const unsigned char *)SvPV(bytes, len);
818 1 int rc = GB_load_state_from_buffer(&s->gb, buf, (size_t)len);
819 1 ▶ if (rc != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) 0 1 1
820 0 croak("Same::Boy: load_state failed (rc=%d)", rc);
821 1 RETVAL = SvREFCNT_inc(self);
822 }
823 OUTPUT:
824 RETVAL
825  
826 # ---- rewind -------------------------------------------------------------
827  
828 SV *
829 set_rewind_length(self, seconds)
830 SV *self
831 double seconds
832 CODE:
833 1 GB_set_rewind_length(&sb_from_sv(aTHX_ self)->gb, seconds);
834 1 RETVAL = SvREFCNT_inc(self);
835 OUTPUT:
836 RETVAL
837  
838 int
839 rewind(self)
840 SV *self
841 CODE:
842 1 ▶ RETVAL = GB_rewind_pop(&sb_from_sv(aTHX_ self)->gb) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = GB_rewind_pop(&sb_from_sv(aTHX_ self)->gb) ? 1 : 0; 0 1 1
843 OUTPUT:
844 RETVAL
845  
846 # ---- cheats -------------------------------------------------------------
847  
848 SV *
849 set_cheats_enabled(self, enabled)
850 SV *self
851 bool enabled
852 CODE:
853 1 GB_set_cheats_enabled(&sb_from_sv(aTHX_ self)->gb, enabled);
854 1 RETVAL = SvREFCNT_inc(self);
855 OUTPUT:
856 RETVAL
857  
858 int
859 cheats_enabled(self)
860 SV *self
861 CODE:
862 2 ▶ RETVAL = GB_cheats_enabled(&sb_from_sv(aTHX_ self)->gb) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = GB_cheats_enabled(&sb_from_sv(aTHX_ self)->gb) ? 1 : 0; 0 2 2
863 OUTPUT:
864 RETVAL
865  
866 # Import a Game Genie or GameShark code; returns true on success.
867 # Optional named args: description => $str, enabled => $bool (default true).
868 int
869 import_cheat(self, code, ...)
870 SV *self
871 const char *code
872 CODE:
873 {
874 2 sb_t *s = sb_from_sv(aTHX_ self);
875 2 const char *desc = code;
876 2 bool enabled = true;
877 int i;
878 4 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 2 2 4
879 2 const char *k = SvPV_nolen(ST(i));
880 2 SV *v = ST(i + 1);
881 4 ▶ if (strEQ(k, "description")) desc = SvPV_nolen(v);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "description")) desc = SvPV_nolen(v); 2 0 2
882 0 ▶ else if (strEQ(k, "enabled")) enabled = SvTRUE(v) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "enabled")) enabled = SvTRUE(v) ? true : false; 0 0 0
883 }
884 2 ▶ RETVAL = GB_import_cheat(&s->gb, code, desc, enabled) != NULL ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = GB_import_cheat(&s->gb, code, desc, enabled) != NULL ? 1 : 0; 0 2 2
885 }
886 OUTPUT:
887 RETVAL
888  
889 SV *
890 remove_all_cheats(self)
891 SV *self
892 CODE:
893 1 GB_remove_all_cheats(&sb_from_sv(aTHX_ self)->gb);
894 1 RETVAL = SvREFCNT_inc(self);
895 OUTPUT:
896 RETVAL
« src/Core/random.c ↑ Summary src/Core/rewind.c »