Compare commits

...

336 Commits

Author SHA1 Message Date
Lior Halphon
b7f03dea8d Add CGB-A support 2022-01-05 21:55:46 +02:00
Lior Halphon
ab1d4cd26e More DMA write conflicts 2022-01-05 21:40:10 +02:00
Lior Halphon
5e7bb0f891 DMA write conflict revision differences 2022-01-04 19:59:46 +02:00
Lior Halphon
79ec22b28e Clang hates this 2022-01-03 17:55:00 +02:00
Lior Halphon
5c17c0ec3b unreachable fun 2022-01-03 17:17:35 +02:00
Lior Halphon
cd16431699 OMA DMA improvements (WIP) 2022-01-03 16:51:45 +02:00
Lior Halphon
81e45b00b9 Minor Cocoa bug fixes 2022-01-03 16:51:24 +02:00
Lior Halphon
c7298ae5a6 Fix a silly underflow 2022-01-03 16:51:05 +02:00
Lior Halphon
8e675295a8 Enable LTO in Windows builds, fix missing MGB boot in SDL 2021-12-31 00:40:34 +02:00
Lior Halphon
bc073e3d09 Expose PC 2021-12-30 23:53:24 +02:00
Lior Halphon
52c5610528 Cocoa audio driver goes brrrr 2021-12-29 17:21:06 +02:00
Lior Halphon
38eafeb0cf Never deadlock ever again thanks 2021-12-29 17:03:44 +02:00
Lior Halphon
97652b7460 Cocoa audio bugfix 2021-12-29 16:53:28 +02:00
Lior Halphon
59c315a5dd Minor free optimization 2021-12-29 16:33:04 +02:00
Lior Halphon
b72c2ea225 DMG palette getter 2021-12-29 13:08:46 +02:00
Lior Halphon
769aac93c0 Lazy APU, extra ~17% speed up 2021-12-29 00:48:44 +02:00
Lior Halphon
db50462710 More accurate fix 2021-12-26 23:24:08 +02:00
Lior Halphon
59dfb1a85a It's not verified because it's wrong 2021-12-26 21:57:40 +02:00
Lior Halphon
6ffe924637 This is probably better but still wrong 2021-12-26 21:43:54 +02:00
Lior Halphon
d178ece909 Disabled an incorrectly emulated portion of the TILE_SET glitch 2021-12-26 19:57:18 +02:00
Lior Halphon
6e7ba7589c Fixed blurred unfiltered screenshots 2021-12-26 18:38:08 +02:00
Lior Halphon
66f7babe86 Cache the clock rate 2021-12-26 15:50:24 +02:00
Lior Halphon
c53d99dbc4 Abolished slow double use 2021-12-26 15:20:46 +02:00
Lior Halphon
c5f6be1e64 Several likely/unlikely optimization, saving on a memset 2021-12-26 02:38:54 +02:00
Lior Halphon
69de3f0fae Implement a PPU fast path, up to 34% performance boost 2021-12-26 01:47:59 +02:00
Lior Halphon
f3277ab8d3 Sorry C++ users 2021-12-20 18:59:51 +02:00
Lior Halphon
e9906e44cd Sure, why not 2021-12-19 21:46:22 +02:00
Lior Halphon
f866441481 Improved emulation of channel 3 wave RAM read glitch 2021-12-19 19:27:40 +02:00
Lior Halphon
e9629407a5 Fix potential alignment issues 2021-12-19 00:54:29 +02:00
Lior Halphon
cdc3321c36 Add an API to allow illegal inputs 2021-12-19 00:28:24 +02:00
Lior Halphon
eaccd792ed Fixes to safe reads, closes #422 2021-12-18 14:56:33 +02:00
Lior Halphon
5127cb0022 Direct access to registers (#422) 2021-12-18 14:51:14 +02:00
Lior Halphon
c63ddbe771 Lag frame detection API (#422) 2021-12-18 01:25:06 +02:00
Lior Halphon
c3d9141b7c Replace the term sprite with object for consistency 2021-12-17 21:16:23 +02:00
Lior Halphon
c1ae129ed4 Allow hiding background/object "layers" (#422) 2021-12-17 21:12:26 +02:00
Lior Halphon
f78fac12c2 Fixed several issues involving LY change timing, as well as an LYC issue in models prior to CGB-D 2021-12-14 20:27:38 +02:00
Lior Halphon
7e5e672988 RTC speed multiplier, for TAS syncing (#422) 2021-12-11 02:51:21 +02:00
Lior Halphon
a30247cf16 LCD line callback (for #422) 2021-12-10 19:49:52 +02:00
Lior Halphon
7508ddb0cf Execute callback (for #422) 2021-12-10 19:42:47 +02:00
Lior Halphon
e087bd5218 The GBS visualizer should use custom color palettes 2021-12-10 02:06:12 +02:00
Lior Halphon
9e57201b08 Accurate IF clear timing 2021-12-05 16:18:54 +02:00
Lior Halphon
25e3414974 Redesigned vblank callback scheduling scheme, should be more regular and less prune to various sorts of frontend DOS 2021-12-04 15:04:46 +02:00
Lior Halphon
4b3c77bfa5 oops 2021-12-02 11:54:26 +02:00
Lior Halphon
8660e20eeb New inputs API 2021-12-02 11:23:44 +02:00
Lior Halphon
b770bbea2e Fix save state issue that caused vblank callbacks timings to differ 2021-12-02 11:21:12 +02:00
Lior Halphon
486f8a2c10
Merge pull request #420 from SnowyMouse/cgb_mode
Add GB_is_cgb_in_cgb_mode
2021-11-26 14:10:35 +02:00
Lior Halphon
06b744259b Add memory write callback, optimize memory access with likely/unlikely 2021-11-26 14:09:41 +02:00
Lior Halphon
bdbe02b043 Add a safe memory read API 2021-11-26 13:54:28 +02:00
Lior Halphon
33090a5cc0 Fix an oops from the last commit 2021-11-26 13:38:52 +02:00
Snowy
d0a9d2f72a Add GB_is_cgb_in_cgb_mode 2021-11-25 17:16:11 -06:00
Lior Halphon
f1e5e04198 ...even when timekeeping is disabled 2021-11-25 21:46:51 +02:00
Lior Halphon
d0d39015ee Let update_input_hint_callback get called during turbo 2021-11-25 21:17:49 +02:00
Lior Halphon
f08f16346e Fix #293 2021-11-24 23:13:52 +02:00
Lior Halphon
d94c8b9125 Switch Pro Controller motion controls 2021-11-22 23:29:10 +02:00
Lior Halphon
d15eaf4134 Mouse controls for MBC7 2021-11-14 21:43:31 +02:00
Lior Halphon
ae930472f0 Units info 2021-11-14 13:18:58 +02:00
Lior Halphon
7a78649e21 Implement motion controls in JoyKit, implement accel/gyro in DualSense and DualShock 4, implement motion controls in Cocoa 2021-11-13 19:23:45 +02:00
Lior Halphon
06ce30d3a8 Map joysticks to motion controls 2021-11-12 18:10:03 +02:00
Lior Halphon
c6f39bc60b Initial MBC7 support 2021-11-12 17:44:51 +02:00
Lior Halphon
02f55d12d3 Maybe one day GCC will stop being shit at handling __attribute__s 2021-11-07 14:13:52 +02:00
Lior Halphon
1650820edb Clean up endian-related code 2021-11-07 13:57:43 +02:00
Lior Halphon
18e7a3f4fa Cleanup, better symbol handling, improves LTO 2021-11-07 13:39:18 +02:00
Lior Halphon
fbf1bb7f98 Save state compatibility breaking cleanup 2021-11-07 12:56:46 +02:00
Lior Halphon
5565c2540b Register name and info update 2021-11-06 13:34:34 +02:00
Lior Halphon
4a7afb246d Fix some oopsies 2021-11-05 21:45:54 +02:00
Lior Halphon
178860e715 Custom palette and editor 2021-11-05 19:07:27 +02:00
Lior Halphon
f237b1e9b9 CGB-0 support 2021-11-04 00:35:44 +02:00
Lior Halphon
6cd13be624 Add CGB-B support 2021-10-30 20:58:57 +03:00
Lior Halphon
b54365fc40 Merge branch 'v0.14.x' 2021-10-30 18:40:27 +03:00
Lior Halphon
72b6d6c532 Merge branch 'master' of https://github.com/LIJI32/SameBoy 2021-10-30 18:31:47 +03:00
Lior Halphon
43831d0bc1 Update version to 0.14.7 2021-10-30 16:10:26 +03:00
Lior Halphon
0f6a0186cd Cherry picking conflicts 2021-10-30 16:09:13 +03:00
Lior Halphon
deb037d87d Detect missing ANSI support on Windows 2021-10-30 16:08:35 +03:00
Lior Halphon
4498d16bed Improved sanitation for save states for better security and stability 2021-10-30 16:08:35 +03:00
Lior Halphon
8d319c65c2 Use a monospaced font in the palette viewer 2021-10-30 16:08:35 +03:00
Snowy
0d7cc66ffd Change y to a signed value 2021-10-30 16:08:35 +03:00
Lior Halphon
0dcd233cbb Writes to SVBK should work before the boot ROM is disabled 2021-10-30 16:08:35 +03:00
Lior Halphon
fd8c9bba5d Detect missing ANSI support on Windows 2021-10-30 16:03:33 +03:00
Lior Halphon
1f7b20251b Improved sanitation for save states for better security and stability 2021-10-30 16:03:13 +03:00
Lior Halphon
93ef8d7db8
Merge pull request #411 from SnowyMouse/sprite_h
Fix partially offscreen sprites not being returned in GB_get_oam_info
2021-10-27 01:44:26 +03:00
Lior Halphon
739a9eb2bf Use a monospaced font in the palette viewer 2021-10-27 01:43:36 +03:00
Lior Halphon
e6c4ceaf5a Add CGB-D support 2021-10-27 01:40:28 +03:00
Snowy
2ec573c84a Change y to a signed value 2021-10-24 11:15:28 -05:00
Lior Halphon
5b9746084d Writes to SVBK should work before the boot ROM is disabled 2021-10-23 23:51:48 +03:00
Lior Halphon
18007f0e53 MGB support 2021-10-23 23:28:54 +03:00
Lior Halphon
5808d4485f Drop BOOLs 2021-10-23 13:36:58 +03:00
Lior Halphon
fc10a90dec Screenshots in the Cocoa frontend 2021-10-23 13:26:44 +03:00
Lior Halphon
3f954f1d0c Update version 2021-10-20 23:37:16 +03:00
Lior Halphon
0e6b9da42d Merge branch 'master' of https://github.com/LIJI32/SameBoy 2021-10-20 23:36:48 +03:00
Lior Halphon
1376c386a2 Slightly altered merge of #408 2021-10-20 21:49:02 +03:00
Lior Halphon
2f2e469790
Merge pull request #407 from SnowyMouse/master
Handle missing background.bmp
2021-10-20 20:34:13 +03:00
Lior Halphon
e04dcd90d6 Fix APU command 2021-10-19 01:56:10 +03:00
Lior Halphon
94776fcf8c Better (But imperfect) emulation of the wave RAM address bug glitch 2021-10-19 01:53:24 +03:00
Lior Halphon
de16ab5d08 Why was this under APU 2021-10-17 20:05:49 +03:00
Lior Halphon
886363b398 Now this glitch makes more sense 2021-10-17 12:52:08 +03:00
Lior Halphon
7ef198ec50 More accurate channel 3 restarts 2021-10-17 02:06:33 +03:00
Lior Halphon
f1b8164613 Force the user selected model on reset (which can change by a save state) 2021-10-17 02:06:13 +03:00
Lior Halphon
40fc477b56 Fix an oopsie 2021-10-10 20:05:57 +03:00
Lior Halphon
4ce643d5eb Fix timer bug; fixes #409 2021-10-10 16:53:07 +03:00
Lior Halphon
01f80a3f3e Adjust border fade delay to match SGB2 2021-10-10 14:24:14 +03:00
Lior Halphon
8f9365251d Timing adjustments 2021-10-10 13:30:30 +03:00
Lior Halphon
b580e63c37 Fix 7-part SGB commands 2021-10-10 02:55:12 +03:00
Lior Halphon
d263a8f6f8 Fix sanity 2021-10-09 23:12:49 +03:00
Lior Halphon
46e1b79b63 Madden 96 needs even more time 2021-10-09 23:07:15 +03:00
Lior Halphon
6b947c46bc Correct blank image detection on SGB 2021-10-09 22:41:43 +03:00
Lior Halphon
893d7d162a Don't try to render tiles 100-3FF 2021-10-09 22:34:43 +03:00
Lior Halphon
85da5b64d3 Some games like racing the SGB 2021-10-09 22:02:40 +03:00
Lior Halphon
a7c8b702da Some games like to race with the SGB border fade 2021-10-09 21:08:17 +03:00
Lior Halphon
f1761340fc Fix ATTR_SET command 2021-10-09 18:45:08 +03:00
Lior Halphon
3b1094058b Add SGB and CGB flags to the tester 2021-10-09 15:57:15 +03:00
Lior Halphon
004b004f98 Fix inverted key buttons in the SDL menu, fixes #401 2021-10-09 15:27:18 +03:00
Lior Halphon
191f7cee02 Improved emulation of SGB multiplayer, fixes #405 2021-10-09 14:52:28 +03:00
Lior Halphon
164a870189 Cleanup uses of gb->registers 2021-10-09 14:09:51 +03:00
Lior Halphon
24af1c5a31 Newly discovered OAM corruption trigger 2021-10-09 14:05:29 +03:00
Lior Halphon
c05c3c2abd Improved accuracy of mid-line SCX writes 2021-10-08 19:36:05 +03:00
Lior Halphon
d4999fbbdb Give APU tests more time 2021-10-07 18:30:09 +03:00
Lior Halphon
ef053ff113 APU regression fix 2021-10-07 18:25:54 +03:00
Lior Halphon
d1caeafe5e Better handling of tiny ROMs 2021-10-05 19:53:43 +03:00
Lior Halphon
9a957674d9 Fix broken action 2021-10-05 19:53:19 +03:00
Lior Halphon
345e51647f API issue – RTC data should not be wiped after GB_reset 2021-09-30 21:23:52 +03:00
Lior Halphon
8068ff41fb Fix potential future compatibility issue, update workflow environments 2021-09-29 21:57:39 +03:00
Snowy
20d580881a Handle missing background.bmp 2021-09-26 23:08:13 -05:00
Lior Halphon
1050a7a533 More size changes for Big Bloat 2021-09-26 00:10:19 +03:00
Lior Halphon
6e2abe23ef Merge branch 'master' of https://github.com/LIJI32/SameBoy 2021-09-25 21:55:29 +03:00
Lior Halphon
7aca04f4c4 Things need to be slightly bigger in Big Sur 2021-09-25 21:55:12 +03:00
Lior Halphon
24a7467735
Merge pull request #406 from Talkashie/master
Fixed a typo in the debugger
2021-09-24 20:33:56 +03:00
Lior Halphon
84c5f8b92d Fix #402 2021-09-22 20:53:04 +03:00
Lior Halphon
51be70275d Fix broken wave RAM initialization in the boot ROM 2021-09-22 17:59:20 +03:00
Talkashie
2dc0e14d0a Update debugger.c
Fixed a typo
2021-09-13 21:20:19 -05:00
Lior Halphon
c25c94d142 Minor improvements to console behavior on startup 2021-09-10 14:17:07 +03:00
Lior Halphon
c5d91fc448 New console readline-like interface for the SDL port 2021-09-09 00:13:09 +03:00
Lior Halphon
336bc65dbf Remove Cocoa spam from the SDL port 2021-09-09 00:11:49 +03:00
Lior Halphon
d8a9f12a4f Smooth scrolling in the SDL port on macOS 2021-09-06 15:21:02 +03:00
Lior Halphon
b27bd4eed7 Remove redundant condition 2021-09-04 18:15:22 +03:00
Lior Halphon
e5454a39b7 Block wave RAM writes on the AGB 2021-09-04 14:18:46 +03:00
Lior Halphon
0ca1ee6a35 Minor APU clean ups 2021-09-02 01:29:38 +03:00
Lior Halphon
eefaac3d04 NR32 was treated as the wrong value on APU reset 2021-08-31 23:28:05 +03:00
Lior Halphon
3c6d094763 Update version to 0.14.5 2021-08-01 16:19:40 +03:00
Lior Halphon
50bf8c4919 Fixed a bug that sometimes prevented GBS files from being opened 2021-08-01 15:11:33 +03:00
Lior Halphon
de500cd397 Update version to 0.14.4 2021-07-30 19:09:18 +03:00
Lior Halphon
c459058156 That was silly 2021-07-30 14:10:04 +03:00
Lior Halphon
48397683b8
Merge pull request #389 from tommitytom/master
Fix Windows build in clang-cl
2021-07-30 13:50:26 +03:00
Lior Halphon
0b2411ecc6 Merge branch 'libretro-core-options' 2021-07-29 23:29:47 +03:00
Lior Halphon
c7e8d7fa13 Minor cleanup 2021-07-29 23:19:13 +03:00
Lior Halphon
6138833b28 Style fixes 2021-07-29 23:03:36 +03:00
Lior Halphon
690a263648 Major improvements to JoyKit, fixing Xbox and 8BitDo controllers as well as analog mappings in PS controllers in some situations 2021-07-29 22:43:55 +03:00
Lior Halphon
0ff882f3bc Actually do what the previous commit claimed to do 2021-07-28 00:47:19 +03:00
Lior Halphon
b454ee28db Fix an issue where SameBoot gave DMG games the wrong palette and needlessly drew the DMG boot tilemap 2021-07-27 22:18:28 +03:00
Lior Halphon
4d1a28f1d1 Improved OAM bug accuracy in several read edge cases 2021-07-25 16:34:34 +03:00
Lior Halphon
1d7692cff5 Fix blurry VRAM viewer grid lines 2021-07-11 23:12:46 +03:00
Lior Halphon
a5325d3374 Improved ticks command, more accurate speed switch timings, better odd-mode warnings 2021-07-11 21:49:58 +03:00
Lior Halphon
6f6f72dcbd More accurate emulation of STOP 2021-07-11 12:11:12 +03:00
Lior Halphon
efb644bc72 MBC5 RAM enable is 8 bit 2021-07-10 15:02:15 +03:00
Ryunam
75ec1c0334 [Libretro] Fix small typo in palette description 2021-06-27 11:22:27 +02:00
Ryunam
e1453f1961 [Libretro] Upgrade Core Options to v1.3 2021-06-26 23:40:22 +02:00
Lior Halphon
278224299f Fixed double->single speed switch causing misaligned CPU timing 2021-06-26 13:55:34 +03:00
Lior Halphon
94add1d172 Add "Harsh Reality" color correction mode 2021-06-25 19:57:56 +03:00
Lior Halphon
a2d34c9bd9 Add -s/--stop-debugger flag to SDL, closes #392 2021-06-25 17:12:05 +03:00
Lior Halphon
ceacc226bc Fixed Switch Pro Controller in USB mode 2021-06-23 21:21:53 +03:00
Lior Halphon
23e8cc58c5 Vblank should occur 1 T-cycle later 2021-06-19 02:14:16 +03:00
Lior Halphon
b4709fd66b Disabled an accuracy-improvement-attempt that caused audio regressions until the proper behavior is well understood, fixes #390 2021-06-18 01:36:29 +03:00
Lior Halphon
339613263c Fixed a bug that prevented STAT interrupt blocking from functioning correctly in the transition to VBlank while the OAM interrupt was disabled 2021-06-18 01:20:05 +03:00
Tom Yaxley
a12ec3c8c8 Fix Windows build in clang-cl 2021-06-06 12:04:47 +10:00
Lior Halphon
7a6ae2d951 Improved DualSense LEDs, fix several analog controls issues 2021-06-04 22:21:41 +03:00
Lior Halphon
e71d3a7d3c First-tier support for DualSense controllers with rumble and LED support 2021-06-04 18:17:14 +03:00
Lior Halphon
b444ecd1ee Fix configuration of analog shoulder buttons for analog turbo/slow motion when using a PS5 controller 2021-06-01 00:46:06 +03:00
Lior Halphon
1e5e236e84 Correct default mapping for PS5 and PS4 controllers 2021-06-01 00:33:25 +03:00
Lior Halphon
ebb0cb5e81 Added optional OSD (SDL) 2021-05-30 23:39:59 +03:00
Lior Halphon
3ed18a76da Added optional OSD (Cocoa) 2021-05-30 20:55:04 +03:00
Lior Halphon
033f025851 Added volume control to the Cocoa port 2021-05-21 18:12:29 +03:00
Lior Halphon
75d3470d55 That code made very little sense 2021-05-19 00:15:02 +03:00
Lior Halphon
fcbbecea17 Fix #386 2021-05-18 20:21:21 +03:00
Lior Halphon
2afeb7dee3 Place a cap on the GBS file size 2021-05-17 17:11:41 +03:00
Lior Halphon
ea67a7e3f0
Merge pull request #379 from jprjr/gbs-buffer
gbs: function to load from memory buffer
2021-05-17 16:53:12 +03:00
Lior Halphon
9b2dfe7ae2
Style fixes 2021-05-17 16:52:55 +03:00
Lior Halphon
e9ab7fa7df
Merge pull request #382 from Mailaender/patch-1
Fixed the desktop categories
2021-05-17 16:44:53 +03:00
Lior Halphon
c944142b36 Fall back to .snX if no .sX save state found 2021-05-07 00:33:04 +03:00
Lior Halphon
a4a8ad00d5 Display usage on invalid options 2021-05-06 00:26:45 +03:00
Lior Halphon
0dff3ef144 A flag to disable OpenGL, better and more stable handling of no-OpenGL mode 2021-05-06 00:23:46 +03:00
Lior Halphon
1d0366052d Updater support 2021-04-25 22:28:24 +03:00
Matthias Mailänder
898ef2c981
Fix the desktop categories. 2021-04-23 20:43:34 +02:00
Lior Halphon
ea05a0c765 Don't save 0x6000 for MBC3 in BESS 2021-04-23 21:05:33 +03:00
Lior Halphon
ac5b0aca2c RTC accuracy fix 2021-04-23 21:01:17 +03:00
John Regan
0e8d8effdf gbs: function to load from memory buffer 2021-04-20 08:38:53 -04:00
Lior Halphon
a2d3b8c174 Support for non-standard GBS files with a loading address at 0 2021-04-19 20:58:27 +03:00
Lior Halphon
c29edc1963 Handle loading errors 2021-04-19 20:57:28 +03:00
Lior Halphon
2971b17701 Add support for ugetab's GBS extensions, fixes #377 2021-04-19 00:32:10 +03:00
Lior Halphon
5f2e893828 Allow GBS files with loading addresses 0x6E-0x3FF, fixes #376 2021-04-19 00:08:21 +03:00
Lior Halphon
d9b9385eb4 Typo fix 2021-04-17 18:13:19 +03:00
Lior Halphon
939817df73 Update version, finalize BESS 1.0 2021-04-17 16:59:22 +03:00
Lior Halphon
e8158be454 Merge branch 'bess' into gbs 2021-04-17 16:57:05 +03:00
Lior Halphon
9fcdc082d2 Fix an SDL crash, minor tweak to BESS SGB 2021-04-17 16:37:55 +03:00
Lior Halphon
817c4a7752 Merge branch 'bess' into gbs 2021-04-16 16:35:21 +03:00
Lior Halphon
87a2d48675 Redo TPP1 saving, fix RTC and HUC3 in BESS 2021-04-16 00:35:54 +03:00
Lior Halphon
f0a6488546 Added optional INFO block 2021-04-15 21:57:38 +03:00
Lior Halphon
2078c2a8fb Use semantic popup icons instead of always using error 2021-04-15 02:42:31 +03:00
Lior Halphon
98a39ae49a ATTR_CHR does not seem to wrap around screen (only lines/columns) 2021-04-14 23:39:07 +03:00
Lior Halphon
b325148544 Update and clarify specification 2021-04-14 23:37:00 +03:00
Lior Halphon
ba6e22dfc0 Merge branch 'bess' into gbs 2021-04-14 16:44:51 +03:00
Lior Halphon
dd86077410 Use the older, more available API 2021-04-14 15:24:06 +03:00
Lior Halphon
8a84a5897e Allow drag&drop of state files 2021-04-14 15:20:01 +03:00
Lior Halphon
c1509b6339 KEY0 info 2021-04-13 23:34:49 +03:00
Lior Halphon
6f0b640702 More clarifications 2021-04-13 22:32:45 +03:00
Lior Halphon
79f109b463 Clarify MBC block 2021-04-13 22:08:25 +03:00
Lior Halphon
43fb86320e Hard fail on unexpected SGB blocks 2021-04-13 22:05:13 +03:00
Lior Halphon
0af4f1fa4d Clarify SGB multiplayer, handle count = 0 2021-04-13 21:33:13 +03:00
Lior Halphon
24915e41eb TPP1 in BESS 2021-04-13 20:56:09 +03:00
Lior Halphon
976f5e4d02 Merge branch 'master' into bess 2021-04-13 20:50:29 +03:00
Lior Halphon
fada772cb1 Don't use BESS for internal in-memory saves 2021-04-13 20:35:07 +03:00
Lior Halphon
dfdbff7304 Allow writes to the $a000-$bfff range in the MBC block 2021-04-13 16:01:44 +03:00
Lior Halphon
6ee488688b Update spec 2021-04-13 01:11:06 +03:00
Lior Halphon
a3a73602fc ATF is only 0xFD2 bytes, not 0xFE0 2021-04-13 01:09:29 +03:00
Lior Halphon
5b993ed775 Add HuC3 to BESS 2021-04-12 23:36:42 +03:00
Lior Halphon
4346b063f5 Wording 2021-04-12 22:48:05 +03:00
Lior Halphon
9c1889f450 Actually update spec 2021-04-12 22:43:23 +03:00
Lior Halphon
9a1f962281 Spec update 2021-04-12 22:39:13 +03:00
Lior Halphon
251dd15ff9 Fixed a bug where the screen would not redraw when certain controllers are rumbling in specific strengths in the Cocoa port 2021-04-11 23:36:42 +03:00
Lior Halphon
80f422d0ca Respect TPP1 feature flags for rumble and RTC 2021-04-11 23:16:31 +03:00
Lior Halphon
763de9d2e0 Fix Rumble support in TPP1 2021-04-11 22:52:34 +03:00
Lior Halphon
42471095e4 Normalize invalid weekdays only after a $11 command 2021-04-11 22:38:25 +03:00
Lior Halphon
0c5e15b49d Correct emulation of count overflow in ATTR_CHR, fixes #372 2021-04-11 02:38:58 +03:00
Lior Halphon
f24489b983 TPP1 support 2021-04-10 23:56:41 +03:00
Lior Halphon
44c75ae7be Remove commented out code 2021-04-10 18:43:24 +03:00
Lior Halphon
ad05eb6d0a GCC build fix 2021-04-10 16:15:40 +03:00
Lior Halphon
955ef59140 Merge branch 'bess' into gbs 2021-04-10 16:12:35 +03:00
Lior Halphon
f89c80caa5 Merge branch 'master' into bess 2021-04-10 16:12:03 +03:00
Lior Halphon
d0bbf383d6 Another cheat bugfix 2021-04-10 16:10:23 +03:00
Lior Halphon
6ddfcc9725 Added visualizer to the GBS player, various GBS UI improvements 2021-04-10 16:10:10 +03:00
Lior Halphon
77384a5f6a Merge branch 'bess' into gbs 2021-04-09 23:12:03 +03:00
Lior Halphon
20ffa27dd4 Forgot to commit the document update 2021-04-07 21:45:43 +03:00
Lior Halphon
1c31812ffd BESS format updates 2021-04-06 01:02:49 +03:00
Lior Halphon
7a558492b6 Merge branch 'master' into bess 2021-04-05 23:10:00 +03:00
Lior Halphon
e6fa2336da Fix a potential crash/corruption when modifying cheats 2021-04-05 23:09:32 +03:00
Lior Halphon
f67d3947d6 UI Updates 2021-04-05 23:08:43 +03:00
Lior Halphon
bb3a73ff88 Cocoa GBS Player 2021-04-03 01:29:43 +03:00
Lior Halphon
9996c7b4a2 Add GBS APIs 2021-04-03 01:29:06 +03:00
Lior Halphon
39c71b40e7 Fix memory leak 2021-04-02 19:07:28 +03:00
Lior Halphon
328e2d9e40 Merge branch 'master' into gbs 2021-04-02 19:06:15 +03:00
Lior Halphon
6b8eb8063a Fix a bug where SameBoy would start in "faux turbo mode" 2021-04-02 02:54:14 +03:00
Lior Halphon
b7348b5478 Add BESS format documentation 2021-04-01 00:16:28 +03:00
Lior Halphon
e460b0a7b4 Change the border format to SNES-style 2021-03-31 00:54:55 +03:00
Lior Halphon
9314bcf98d
Merge pull request #371 from Jan200101/patch-1
Correct usage of PREFIX with DATA_DIR
2021-03-30 20:45:21 +03:00
Jan
48ec3e6413
Correct usage of PREFIX with DATA_DIR
slipped through in #370
DATA_DIR is not relative to PREFIX so having it is problematic
2021-03-30 17:29:55 +02:00
Lior Halphon
8adaba237e SGB support in BESS, BE fixes, changes to SGB save state format on BE machines 2021-03-29 02:47:57 +03:00
Lior Halphon
b9030bb2d0
Merge pull request #370 from Jan200101/patch-1
replace PREFIX/share/sameboy with DATA_DIR
2021-03-25 20:57:30 +02:00
Jan
aca2fd04b1
replace PREFIX/share/sameboy with DATA_DIR 2021-03-25 19:17:45 +01:00
Lior Halphon
659f954028 RTC support 2021-03-25 00:07:37 +02:00
Lior Halphon
a52302f2f6 Make NAME come before CORE 2021-03-25 00:07:37 +02:00
Lior Halphon
75bc1e9a86 Initial BESS support, no SGB nor RTC yet 2021-03-25 00:07:37 +02:00
Lior Halphon
925bd863c0 Better errnos 2021-03-22 00:18:49 +02:00
Lior Halphon
da1003263f Redo save states to remove severe code duplication between buffers and files 2021-03-21 20:32:30 +02:00
Lior Halphon
ad54dc57b0 Improved time syncing when turning the LCD on and off, fixes #193 2021-03-21 15:15:04 +02:00
Lior Halphon
5c1b89e82d Update version 2021-03-01 23:27:40 +02:00
Lior Halphon
5a966bba91 Register ISX files on FreeDesktop 2021-03-01 23:21:07 +02:00
Lior Halphon
f50d9310a7 This shouldn't have been here 2021-03-01 23:00:11 +02:00
Lior Halphon
4d67fa8e80 Close all related windows when closing a document 2021-03-01 22:58:52 +02:00
Lior Halphon
ea97c1dc0b Fix an APU regression that caused some games in DMG mode to play in the wrong pitch 2021-03-01 21:44:54 +02:00
Lior Halphon
d2ed1343e5 Add missing mkdir 2021-02-28 20:41:58 +02:00
Lior Halphon
c6ea57209f
Merge pull request #356 from RobLoach/libretro-updates
libretro: Sync updates from libretro
2021-02-28 20:14:15 +02:00
Rob Loach
f21fd33cc3
libretro: Remove APP_STL 2021-02-28 13:13:40 -05:00
Rob Loach
975d379d76
libretro: Remove empty CFLAGS 2021-02-28 13:13:12 -05:00
Lior Halphon
2d593a95e3 Update version to 0.14.1 2021-02-28 17:15:19 +02:00
Lior Halphon
57080c48bc No need for -1 2021-02-28 16:50:46 +02:00
Rob Loach
6a995bfe10
libretro: Sync updates from libretro 2021-02-28 09:45:18 -05:00
Lior Halphon
ec7d756e3b
Merge pull request #351 from NieDzejkob/symbol-off-by-one
Fix off-by-one in symbol search
2021-02-28 15:27:50 +02:00
Lior Halphon
0fbd714d4a
Merge pull request #352 from NieDzejkob/value-to-string-oob
value_to_string: use snprintf
2021-02-28 15:23:29 +02:00
Lior Halphon
81bfea9ba2
Coding style, ensuring string termination. 2021-02-28 15:23:14 +02:00
Lior Halphon
d2eb8e0996 Addresses issues mentioned by #355 2021-02-28 15:17:00 +02:00
Lior Halphon
5cc2dcc864 Fix #353 better 2021-02-28 14:55:30 +02:00
Lior Halphon
d50514ede9 Fix #353 2021-02-28 14:51:58 +02:00
Lior Halphon
5cd920d363 Merge branch 'master' of https://github.com/LIJI32/SameBoy 2021-02-28 03:41:13 +02:00
Lior Halphon
1fdb4f09b4 Fix a sweep regression in DMG/SGB mode and CGB-C mode 2021-02-28 03:40:58 +02:00
Jakub Kądziołka
c9665d0449
value_to_string: use snprintf
Currently, value_to_string and debugger_value_to_string use an
error-prone calculation to avoid overflow. This was once adjusted
already, and one of the codepaths is still vulnerable. Put this in a
symfile:

    01:5678 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

and execute `p 1:$5679`. On Linux, the canary terminates the process.
2021-02-27 19:33:31 +01:00
Lior Halphon
ec2661ac03
Merge pull request #350 from NieDzejkob/destdir-mkdir
Fix DESTDIR installation
2021-02-27 20:29:22 +02:00
Jakub Kądziołka
e8bfc4050e
Fix off-by-one in symbol search
Before this commit, printing an address that's after every symbol in a
bank would not show it relative to the last symbol.
2021-02-27 19:29:06 +01:00
Jakub Kądziołka
0a4cb8148f
Fix DESTDIR installation
Installing into a fresh DESTDIR would error out due to non-existent bin
directory
2021-02-27 18:33:22 +01:00
Lior Halphon
54d733f356
Merge pull request #349 from NieDzejkob/memmove-fix
Use memmove for overlapping copy
2021-02-27 19:31:06 +02:00
Jakub Kądziołka
1dae345b24
Use memmove for overlapping copy 2021-02-27 18:29:59 +01:00
Lior Halphon
e57b5dd57e Update version and copyright date 2021-02-27 19:08:32 +02:00
Lior Halphon
a59cd856c7 Fix make install issues 2021-02-27 18:39:14 +02:00
Lior Halphon
cd2e4b3cef Fixes and improvements to XDG installation 2021-02-27 17:34:11 +02:00
Lior Halphon
3c0f4d458d Update Windows icon 2021-02-27 15:51:56 +02:00
Lior Halphon
aebc11744c Update readme 2021-02-27 15:37:40 +02:00
Lior Halphon
2b263937da Allow make install under FreeDesktop 2021-02-27 14:32:07 +02:00
Lior Halphon
0a983b788e Update icon 2021-02-27 04:13:31 +02:00
Lior Halphon
ce44773caa Make the printer not deadlock after a sudden termination 2021-02-26 16:40:35 +02:00
Lior Halphon
cb721dae5d Make the automation use the accurate RTC mode 2021-02-26 01:09:40 +02:00
Lior Halphon
34b0404ffa Add RTC setting to libretro 2021-02-26 01:07:46 +02:00
Lior Halphon
bae91cdb1d Add RTC option to the SDL port, fix a bug where rewind setting didn't update 2021-02-26 01:04:24 +02:00
Lior Halphon
72cb391612 Slightly improve MBC3 accuracy 2021-02-26 00:52:18 +02:00
Lior Halphon
71c6fa45e0 Accurate RTC emulation 2021-02-26 00:40:18 +02:00
Lior Halphon
a13469c4e2 Fix PAL SGB in the Cocoa port 2021-02-25 22:42:02 +02:00
Lior Halphon
e08df2a089 Add accurate RTC emulation mode 2021-02-25 22:12:14 +02:00
Lior Halphon
807712b9c2 Allow creating sav files from the tester (Fixes #311) 2021-02-25 18:04:52 +02:00
Lior Halphon
9fa564f97c Fix #336 2021-02-25 17:12:01 +02:00
Lior Halphon
6ec4583aa0 Tell GCC to calm down 2021-02-25 15:52:48 +02:00
Lior Halphon
fa5420136e I hate the audio thread 2021-02-25 15:43:52 +02:00
Lior Halphon
4c05ebcea6 Redo the volume envelope with better timings, locking emulation and zombie mode edge cases. Fixes #344 2021-02-25 15:43:38 +02:00
Lior Halphon
8809d8ac2f More correct emulation of manual clocking of channels 1 and 2 2021-02-22 15:27:36 +02:00
Lior Halphon
71c88323b7 Rename UNROLL to unrolled (unrolled for) 2021-02-22 14:45:30 +02:00
Lior Halphon
9da0449797 Merge branch 'master' of https://github.com/LIJI32/SameBoy 2021-02-22 13:49:08 +02:00
Lior Halphon
393269ae1f Emulate volume envelope locking 2021-02-22 13:48:56 +02:00
Lior Halphon
d50fdc52ea Further accuracy improvements to the audio envelope 2021-02-22 01:58:43 +02:00
Lior Halphon
6d2d88648e Improved emulation of the volume envelope 2021-02-22 01:10:14 +02:00
Lior Halphon
759ff1927e
Merge pull request #345 from jkotlinski/fail-nicely-on-missing-window
exit with error message instead of crash when a window cannot be opened
2021-02-14 02:33:58 +02:00
Johan Kotlinski
1a87c452b7 exit with error message instead of crash when a window cannot be opened 2021-02-14 01:31:49 +01:00
Lior Halphon
e7a5be70c2 Merge branch 'master' of https://github.com/LIJI32/SameBoy 2021-02-13 23:00:30 +02:00
Lior Halphon
95051d1c1c Improved emulation of the NRx2 write glitch (Zombie mode) on models prior to CGB-D 2021-02-13 23:00:20 +02:00
Lior Halphon
d343f0c969
Merge pull request #330 from jverkoey/array
Add type annotations to GBImageView's grid arrays.
2021-02-13 14:38:07 +02:00
Lior Halphon
3316954d14
Merge pull request #326 from jverkoey/deprecations
Resolve various deprecation warnings.
2021-02-13 14:15:48 +02:00
Lior Halphon
8ad08c1b35 Fix more audio deadlocks 2021-02-01 23:11:42 +02:00
Lior Halphon
301c0f41c2 Merge branch 'master' of https://github.com/LIJI32/SameBoy 2021-01-31 19:17:56 +02:00
Lior Halphon
6798b1f11a Use a slider for temperature in the SDL GUI 2021-01-31 19:17:48 +02:00
Lior Halphon
2d76698279 Emulation of NR43 bit 3 glitch on CGB-C and older 2021-01-31 19:17:26 +02:00
Lior Halphon
d67580c964 Oops, that was reversed 2021-01-31 17:16:59 +02:00
Lior Halphon
fbfa20a2cd
Merge pull request #337 from phobos2390/fix_for_signed_array_index
Fix for tolower extension signed char issue
2021-01-27 22:04:15 +02:00
phobos2390
bbfd16f63d Fix for tolower extension signed char issue 2021-01-25 23:37:46 -07:00
Lior Halphon
ef9671010b More NR43 obscurities 2021-01-24 20:57:46 +02:00
Lior Halphon
8e1e889ce0 Add a TODO 2021-01-16 15:31:09 +02:00
Lior Halphon
aa421258b8 Update the model enum so comparisons work correctly for SGB PAL and no-SFC SGBs 2021-01-16 14:51:06 +02:00
Lior Halphon
13a1e9d332 Timing fix 2021-01-16 14:43:32 +02:00
Lior Halphon
0056cc2d61 Revert "Further NR43 write glitch emulation" for now
This reverts commit e384707615.
2021-01-16 14:42:13 +02:00
Jeff Verkoeyen
557f554270 [Sameboy] Add type annotations to GBImageView's grid arrays. 2021-01-15 13:21:28 -05:00
Jeff Verkoeyen
8f91533a9a Revert nil check changes. 2021-01-15 12:49:24 -05:00
Lior Halphon
931045fd9b
Merge pull request #325 from jverkoey/master
Annotate all Cocoa properties as nonatomic.
2021-01-15 14:15:09 +02:00
Lior Halphon
0b8ee0585a
Merge pull request #329 from jverkoey/spritebug
Fix broken sprite rendering in the VRAM viewer due to mis-calculation of image data size.
2021-01-15 13:39:07 +02:00
Jeff Verkoeyen
1707c8818a Fix broken sprite rendering in the VRAM viewer due to mis-calculation of image data size. 2021-01-15 00:41:21 -05:00
Jeff Verkoeyen
60f226321d Resolve various deprecation warnings. 2021-01-13 14:52:18 -05:00
Jeff Verkoeyen
6dca01ad27 Annotate properties as nonatomic. 2021-01-13 14:12:34 -05:00
Lior Halphon
e384707615 Further NR43 write glitch emulation 2021-01-10 17:20:25 +02:00
Lior Halphon
07e76a4ecf Oh boy, looks like my CGB-B is unique 2021-01-09 23:28:30 +02:00
Lior Halphon
1b3f52e8c0 Improved emulation of NR43 writes on different revisions 2021-01-09 21:21:22 +02:00
Lior Halphon
2aa171e0ea Better sample alignment on pre-CGB-D models 2021-01-09 16:26:56 +02:00
Lior Halphon
96736fe7c5 Fix false positives in odd-mode detection 2021-01-09 00:59:12 +02:00
Lior Halphon
c496797fce Merge branch 'master' of https://github.com/LIJI32/SameBoy 2021-01-09 00:31:26 +02:00
Lior Halphon
c0582fd994 More accurate emulation of NR10 writes 2021-01-09 00:31:16 +02:00
161 changed files with 12724 additions and 3030 deletions

View File

@ -1,10 +1,10 @@
set -e
./build/bin/tester/sameboy_tester --jobs 5 \
--length 40 .github/actions/cgb_sound.gb \
--length 45 .github/actions/cgb_sound.gb \
--length 10 .github/actions/cgb-acid2.gbc \
--length 10 .github/actions/dmg-acid2.gb \
--dmg --length 40 .github/actions/dmg_sound-2.gb \
--dmg --length 45 .github/actions/dmg_sound-2.gb \
--dmg --length 20 .github/actions/oam_bug-2.gb
mv .github/actions/dmg{,-mode}-acid2.bmp
@ -15,18 +15,18 @@ mv .github/actions/dmg{,-mode}-acid2.bmp
set +e
FAILED_TESTS=`
shasum .github/actions/*.bmp | grep -q -E -v \(\
44ce0c7d49254df0637849c9155080ac7dc3ef3d\ \ .github/actions/cgb-acid2.bmp\|\
shasum .github/actions/*.bmp | grep -E -v \(\
5283564df0cf5bb78a7a90aff026c1a4692fd39e\ \ .github/actions/cgb-acid2.bmp\|\
dbcc438dcea13b5d1b80c5cd06bda2592cc5d9e0\ \ .github/actions/cgb_sound.bmp\|\
0caadf9634e40247ae9c15ff71992e8f77bbf89e\ \ .github/actions/dmg-acid2.bmp\|\
c50daed36c57a8170ff362042694786676350997\ \ .github/actions/dmg-mode-acid2.bmp\|\
a732077f98f43d9231453b1764d9f797a836924d\ \ .github/actions/dmg-mode-acid2.bmp\|\
c9e944b7e01078bdeba1819bc2fa9372b111f52d\ \ .github/actions/dmg_sound-2.bmp\|\
f0172cc91867d3343fbd113a2bb98100074be0de\ \ .github/actions/oam_bug-2.bmp\
\)`
if [ -n "$FAILED_TESTS" ] ; then
echo "Failed the following tests:"
echo $FAILED_TESTS | tr " " "\n" | grep -q -o -E "[^/]+\.bmp" | sed s/.bmp// | sort
echo $FAILED_TESTS | tr " " "\n" | grep -o -E "[^/]+\.bmp" | sed s/.bmp// | sort
exit 1
fi

View File

@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, ubuntu-16.04]
os: [macos-latest, ubuntu-latest, ubuntu-18.04]
cc: [gcc, clang]
include:
- os: macos-latest
@ -33,4 +33,4 @@ jobs:
uses: actions/upload-artifact@v1
with:
name: sameboy-canary-${{ matrix.os }}-${{ matrix.cc }}
path: build/bin
path: build/bin

217
BESS.md Normal file
View File

@ -0,0 +1,217 @@
# BESS Best Effort Save State 1.0
## Motivation
BESS is a save state format specification designed to allow different emulators, as well as majorly different versions of the same emulator, to import save states from other BESS-compliant save states. BESS works by appending additional, implementation-agnostic information about the emulation state. This allows a single save state file to be read as both a fully-featured, implementation specific save state which includes detailed timing information; and as a portable "best effort" save state that represents a state accurately enough to be restored in casual use-cases.
## Specification
Every integer used in the BESS specification is stored in Little Endian encoding.
### BESS footer
BESS works by appending a detectable footer at the end of an existing save state format. The footer uses the following format:
| Offset from end of file | Content |
|-------------------------|-------------------------------------------------------|
| -8 | Offset to the first BESS Block, from the file's start |
| -4 | The ASCII string 'BESS' |
### BESS blocks
BESS uses a block format where each block contains the following header:
| Offset | Content |
|--------|---------------------------------------|
| 0 | A four-letter ASCII identifier |
| 4 | Length of the block, excluding header |
Every block is followed by another block, until the END block is reached. If an implementation encounters an unsupported block, it should be completely ignored (Should not have any effect and should not trigger a failure).
#### NAME block
The NAME block uses the `'NAME'` identifier, and is an optional block that contains the name of the emulator that created this save state. While optional, it is highly recommended to be included in every implementation it allows the user to know which emulator and version is compatible with the native save state format contained in this file. When used, this block should come first.
The length of the NAME block is variable, and it only contains the name and version of the originating emulator in ASCII.
#### INFO block
The INFO block uses the `'INFO'` identifier, and is an optional block that contains information about the ROM this save state originates from. When used, this block should come before `CORE` but after `NAME`. This block is 0x12 bytes long, and it follows this structure:
| Offset | Content |
|--------|--------------------------------------------------|
| 0x00 | Bytes 0x134-0x143 from the ROM (Title) |
| 0x10 | Bytes 0x14E-0x14F from the ROM (Global checksum) |
#### CORE block
The CORE block uses the `'CORE'` identifier, and is a required block that contains both core state information, as well as basic information about the BESS version used. This block must be the first block, unless the `NAME` or `INFO` blocks exist then it must come directly after them. An implementation should not enforce block order on blocks unknown to it for future compatibility.
The length of the CORE block is 0xD0 bytes, but implementations are expected to ignore any excess bytes. Following the BESS block header, the structure is as follows:
| Offset | Content |
|--------|----------------------------------------|
| 0x00 | Major BESS version as a 16-bit integer |
| 0x02 | Minor BESS version as a 16-bit integer |
Both major and minor versions should be 1. Implementations are expected to reject incompatible majors, but still attempt to read newer minor versions.
| Offset | Content |
|--------|----------------------------------------|
| 0x04 | A four-character ASCII model identifier |
BESS uses a four-character string to identify Game Boy models:
* The first letter represents mutually-incompatible families of models and is required. The allowed values are `'G'` for the original Game Boy family, `'S'` for the Super Game Boy family, and `'C'` for the Game Boy Color and Advance family.
* The second letter represents a specific model within the family, and is optional (If an implementation does not distinguish between specific models in a family, a space character may be used). The allowed values for family G are `'D'` for DMG and `'M'` for MGB; the allowed values for family S are `'N'` for NTSC, `'P'` for PAL, and `'2'` for SGB2; and the allowed values for family C are `'C'` for CGB, and `'A'` for the various GBA line models.
* The third letter represents a specific CPU revision within a model, and is optional (If an implementation does not distinguish between revisions, a space character may be used). The allowed values for model GD (DMG) are `'0'` and `'A'`, through `'C'`; the allowed values for model CC (CGB) are `'0'` and `'A'`, through `'E'`; the allowed values for model CA (AGB, AGS, GBP) are `'0'`, `'A'` and `'B'`; and for every other model this value must be a space character.
* The last character is used for padding and must be a space character.
For example; `'GD '` represents a DMG of an unspecified revision, `'S '` represents some model of the SGB family, and `'CCE '` represent a CGB using CPU revision E.
| Offset | Content |
|--------|--------------------------------------------------------|
| 0x08 | The value of the PC register |
| 0x0A | The value of the AF register |
| 0x0C | The value of the BC register |
| 0x0E | The value of the DE register |
| 0x10 | The value of the HL register |
| 0x12 | The value of the SP register |
| 0x14 | The value of IME (0 or 1) |
| 0x15 | The value of the IE register |
| 0x16 | Execution state (0 = running; 1 = halted; 2 = stopped) |
| 0x17 | Reserved, must be 0 |
| 0x18 | The values of every memory-mapped register (128 bytes) |
The values of memory-mapped registers should be written 'as-is' to memory as if the actual ROM wrote them, with the following exceptions and note:
* Unused registers have Don't-Care values which should be ignored
* Unused register bits have Don't-Care values which should be ignored
* If the model is CGB or newer, the value of KEY0 (FF4C) must be valid as it determines DMG mode
* Bit 2 determines DMG mode. A value of 0x04 usually denotes DMG mode, while a value of `0x80` usually denotes CGB mode.
* Object priority is derived from KEY0 (FF4C) instead of OPRI (FF6C) because OPRI can be modified after booting, but only the value of OPRI during boot ROM execution takes effect
* If a register doesn't exist on the emulated model (For example, KEY0 (FF4C) on a DMG), its value should be ignored.
* BANK (FF50) should be 0 if the boot ROM is still mapped, and 1 otherwise, and must be valid.
* Implementations should not start a serial transfer when writing the value of SB
* Similarly, no value of NRx4 should trigger a sound pulse on save state load
* And similarly again, implementations should not trigger DMA transfers when writing the values of DMA or HDMA5
* The value store for DIV will be used to set the internal divisor to `DIV << 8`
* Implementation should apply care when ordering the write operations (For example, writes to NR52 must come before writes to the other APU registers)
| Offset | Content |
|--------|--------------------------------------------------------------------|
| 0x98 | The size of RAM (32-bit integer) |
| 0x9C | The offset of RAM from file start (32-bit integer) |
| 0xA0 | The size of VRAM (32-bit integer) |
| 0xA4 | The offset of VRAM from file start (32-bit integer) |
| 0xA8 | The size of MBC RAM (32-bit integer) |
| 0xAC | The offset of MBC RAM from file start (32-bit integer) |
| 0xB0 | The size of OAM (=0xA0, 32-bit integer) |
| 0xB4 | The offset of OAM from file start (32-bit integer) |
| 0xB8 | The size of HRAM (=0x7F, 32-bit integer) |
| 0xBC | The offset of HRAM from file start (32-bit integer) |
| 0xC0 | The size of background palettes (=0x40 or 0, 32-bit integer) |
| 0xC4 | The offset of background palettes from file start (32-bit integer) |
| 0xC8 | The size of object palettes (=0x40 or 0, 32-bit integer) |
| 0xCC | The offset of object palettes from file start (32-bit integer) |
The contents of large buffers are stored outside of BESS structure so data from an implementation's native save state format can be reused. The offsets are absolute offsets from the save state file's start. Background and object palette sizes must be 0 for models prior to Game Boy Color.
An implementation needs handle size mismatches gracefully. For example, if too large MBC RAM size is specified, the superfluous data should be ignored. On the other hand, if a too small VRAM size is specified (For example, if it's a save state from an emulator emulating a CGB in DMG mode, and it didn't save the second CGB VRAM bank), the implementation is expected to set that extra bank to all zeros.
#### XOAM block
The XOAM block uses the `'XOAM'` identifier, and is an optional block that contains the data of extra OAM (addresses `0xFEA0-0xFEFF`). This block length must be `0x60`. Implementations that do not emulate this extra range are free to ignore the excess bytes, and to not create this block.
#### MBC block
The MBC block uses the `'MBC '` identifier, and is an optional block that is only used when saving states of ROMs that use an MBC. The length of this block is variable and must be divisible by 3.
This block contains an MBC-specific number of 3-byte-long pairs that represent the values of each MBC register. For example, for MBC5 the contents would look like:
| Offset | Content |
|--------|---------------------------------------|
| 0x0 | The value 0x0000 as a 16-bit integer |
| 0x2 | 0x0A if RAM is enabled, 0 otherwise |
| 0x3 | The value 0x2000 as a 16-bit integer |
| 0x5 | The lower 8 bits of the ROM bank |
| 0x6 | The value 0x3000 as a 16-bit integer |
| 0x8 | The bit 9 of the ROM bank |
| 0x9 | The value 0x4000 as a 16-bit integer |
| 0xB | The current RAM bank |
An implementation should parse this block as a series of writes to be made. Values outside the `0x0000-0x7FFF` and `0xA000-0xBFFF` ranges are not allowed. Implementations must perform the writes in order (i.e. not reverse, sorted, or any other transformation on their order)
#### RTC block
The RTC block uses the `'RTC '` identifier, and is an optional block that is used while emulating an MBC3 with an RTC. The contents of this block are identical to 64-bit RTC saves from VBA, which are also used by SameBoy and different emulators such as BGB.
The length of this block is 0x30 bytes long and it follows the following structure:
| Offset | Content |
|--------|------------------------------------------------------------------------|
| 0x00 | Current seconds (1 byte), followed by 3 bytes of padding |
| 0x04 | Current minutes (1 byte), followed by 3 bytes of padding |
| 0x08 | Current hours (1 byte), followed by 3 bytes of padding |
| 0x0C | Current days (1 byte), followed by 3 bytes of padding |
| 0x10 | Current high/overflow/running (1 byte), followed by 3 bytes of padding |
| 0x14 | Latched seconds (1 byte), followed by 3 bytes of padding |
| 0x18 | Latched minutes (1 byte), followed by 3 bytes of padding |
| 0x1C | Latched hours (1 byte), followed by 3 bytes of padding |
| 0x20 | Latched days (1 byte), followed by 3 bytes of padding |
| 0x24 | Latched high/overflow/running (1 byte), followed by 3 bytes of padding |
| 0x28 | UNIX timestamp at the time of the save state (64-bit) |
#### HUC3 block
The HUC3 block uses the `'HUC3'` identifier, and is an optional block that is used while emulating an HuC3 cartridge to store RTC and alarm information. The contents of this block are identical to HuC3 RTC saves from SameBoy.
The length of this block is 0x11 bytes long and it follows the following structure:
| Offset | Content |
|--------|-------------------------------------------------------|
| 0x00 | UNIX timestamp at the time of the save state (64-bit) |
| 0x08 | RTC minutes (16-bit) |
| 0x0A | RTC days (16-bit) |
| 0x0C | Scheduled alarm time minutes (16-bit) |
| 0x0E | Scheduled alarm time days (16-bit) |
| 0x10 | Alarm enabled flag (8-bits, either 0 or 1) |
#### SGB block
The SGB block uses the `'SGB '` identifier, and is an optional block that is only used while emulating an SGB or SGB2 *and* SGB commands enabled. Implementations must not save this block on other models or when SGB commands are disabled, and should assume SGB commands are disabled if this block is missing.
The length of this block is 0x39 bytes, but implementations should allow and ignore excess data in this block for extensions. The block follows the following structure:
| Offset | Content |
|--------|--------------------------------------------------------------------------------------------------------------------------|
| 0x00 | The size of the border tile data (=0x2000, 32-bit integer) |
| 0x04 | The offset of the border tile data (SNES tile format, 32-bit integer) |
| 0x08 | The size of the border tilemap (=0x800, 32-bit integer) |
| 0x0C | The offset of the border tilemap (LE 16-bit sequences, 32-bit integer) |
| 0x10 | The size of the border palettes (=0x80, 32-bit integer) |
| 0x14 | The offset of the border palettes (LE 16-bit sequences, 32-bit integer) |
| 0x18 | The size of active colorization palettes (=0x20, 32-bit integer) |
| 0x1C | The offset of the active colorization palettes (LE 16-bit sequences, 32-bit integer) |
| 0x20 | The size of RAM colorization palettes (=0x1000, 32-bit integer) |
| 0x24 | The offset of the RAM colorization palettes (LE 16-bit sequences, 32-bit integer) |
| 0x28 | The size of the attribute map (=0x168, 32-bit integer) |
| 0x2C | The offset of the attribute map (32-bit integer) |
| 0x30 | The size of the attribute files (=0xfd2, 32-bit integer) |
| 0x34 | The offset of the attribute files (32-bit integer) |
| 0x38 | Multiplayer status (1 byte); high nibble is player count (1, 2 or 4), low nibble is current player (Where Player 1 is 0) |
If only some of the size-offset pairs are available (for example, partial HLE SGB implementation), missing fields are allowed to have 0 as their size, and implementations are expected to fall back to a sane default.
#### END block
The END block uses the `'END '` identifier, and is a required block that marks the end of BESS data. Naturally, it must be the last block. The length of the END block must be 0.
## Validation and Failures
Other than previously specified required fail conditions, an implementation is free to decide what format errors should abort the loading of a save file. Structural errors (e.g. a block with an invalid length, a file offset that is outside the file's range, or a missing END block) should be considered as irrecoverable errors. Other errors that are considered fatal by SameBoy's implementation:
* Duplicate CORE block
* A known block, other than NAME, appearing before CORE
* An invalid length for the XOAM, RTC, SGB or HUC3 blocks
* An invalid length of MBC (not a multiple of 3)
* A write outside the $0000-$7FFF and $A000-$BFFF ranges in the MBC block
* An SGB block on a save state targeting another model
* An END block with non-zero length

2
BootROMs/cgb0_boot.asm Normal file
View File

@ -0,0 +1,2 @@
CGB0 EQU 1
include "cgb_boot.asm"

View File

@ -23,13 +23,16 @@ Start:
dec c
jr nz, .clearOAMLoop
IF !DEF(CGB0)
; Init waveform
ld c, $10
ld hl, $FF30
.waveformLoop
ldi [hl], a
cpl
dec c
jr nz, .waveformLoop
ENDC
; Clear chosen input palette
ldh [InputPalette], a
@ -44,7 +47,6 @@ Start:
ldh [$25], a
ld a, $77
ldh [$24], a
ld hl, $FF30
; Init BG palette
ld a, $fc
@ -190,10 +192,9 @@ ENDC
IF !DEF(FAST)
call DoIntroAnimation
ld a, 45
ld a, 48 ; frames to wait after playing the chime
ldh [WaitLoopCounter], a
; Wait ~0.75 seconds
ld b, a
ld b, 4 ; frames to wait before playing the chime
call WaitBFrames
; Play first sound
@ -271,7 +272,7 @@ TitleChecksums:
db $A2 ; STAR WARS-NOA
db $49 ;
db $4E ; WAVERACE
db $43 | $80 ;
db $43 ;
db $68 ; LOLO2
db $E0 ; YOSHI'S COOKIE
db $8B ; MYSTIC QUEST
@ -330,7 +331,7 @@ ChecksumsEnd:
PalettePerChecksum:
palette_index: MACRO ; palette, flags
db ((\1) * 3) | (\2) ; | $80 means game requires DMG boot tilemap
db ((\1)) | (\2) ; | $80 means game requires DMG boot tilemap
ENDM
palette_index 0, 0 ; Default Palette
palette_index 4, 0 ; ALLEY WAY
@ -374,7 +375,7 @@ ENDM
palette_index 45, 0 ; STAR WARS-NOA
palette_index 36, 0 ;
palette_index 38, 0 ; WAVERACE
palette_index 26, 0 ;
palette_index 26, $80 ;
palette_index 42, 0 ; LOLO2
palette_index 30, 0 ; YOSHI'S COOKIE
palette_index 41, 0 ; MYSTIC QUEST
@ -475,7 +476,7 @@ ENDM
palette_comb 17, 4, 13
raw_palette_comb 28 * 4 - 1, 0 * 4, 14 * 4
raw_palette_comb 28 * 4 - 1, 4 * 4, 15 * 4
palette_comb 19, 22, 9
raw_palette_comb 19 * 4, 23 * 4 - 1, 9 * 4
palette_comb 16, 28, 10
palette_comb 4, 23, 28
palette_comb 17, 22, 2
@ -918,7 +919,10 @@ EmulateDMG:
call GetPaletteIndex
bit 7, a
call nz, LoadDMGTilemap
and $7F
res 7, a
ld b, a
add b
add b
ld b, a
ldh a, [InputPalette]
and a
@ -978,7 +982,7 @@ GetPaletteIndex:
; We might have a match, Do duplicate/4th letter check
ld a, l
sub FirstChecksumWithDuplicate - TitleChecksums
sub FirstChecksumWithDuplicate - TitleChecksums + 1
jr c, .match ; Does not have a duplicate, must be a match!
; Has a duplicate; check 4th letter
push hl
@ -1184,7 +1188,7 @@ ChangeAnimationPalette:
call WaitFrame
call LoadPalettesFromHRAM
; Delay the wait loop while the user is selecting a palette
ld a, 45
ld a, 48
ldh [WaitLoopCounter], a
pop de
pop bc

View File

@ -115,7 +115,11 @@ Start:
call WaitBFrames
; Set registers to match the original DMG boot
IF DEF(MGB)
ld hl, $FFB0
ELSE
ld hl, $01B0
ENDC
push hl
pop af
ld hl, $014D

2
BootROMs/mgb_boot.asm Normal file
View File

@ -0,0 +1,2 @@
MGB EQU 1
include "dmg_boot.asm"

View File

@ -1,16 +1,25 @@
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate, NSMenuDelegate>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate, NSMenuDelegate, WebUIDelegate, WebPolicyDelegate, WebFrameLoadDelegate>
@property IBOutlet NSWindow *preferencesWindow;
@property (strong) IBOutlet NSView *graphicsTab;
@property (strong) IBOutlet NSView *emulationTab;
@property (strong) IBOutlet NSView *audioTab;
@property (strong) IBOutlet NSView *controlsTab;
@property (nonatomic, strong) IBOutlet NSWindow *preferencesWindow;
@property (nonatomic, strong) IBOutlet NSView *graphicsTab;
@property (nonatomic, strong) IBOutlet NSView *emulationTab;
@property (nonatomic, strong) IBOutlet NSView *audioTab;
@property (nonatomic, strong) IBOutlet NSView *controlsTab;
@property (nonatomic, strong) IBOutlet NSView *updatesTab;
- (IBAction)showPreferences: (id) sender;
- (IBAction)toggleDeveloperMode:(id)sender;
- (IBAction)switchPreferencesTab:(id)sender;
@property (weak) IBOutlet NSMenuItem *linkCableMenuItem;
@property (nonatomic, weak) IBOutlet NSMenuItem *linkCableMenuItem;
@property (nonatomic, strong) IBOutlet NSWindow *updateWindow;
@property (nonatomic, strong) IBOutlet WebView *updateChanges;
@property (nonatomic, strong) IBOutlet NSProgressIndicator *updatesSpinner;
@property (strong) IBOutlet NSButton *updatesButton;
@property (strong) IBOutlet NSTextField *updateProgressLabel;
@property (strong) IBOutlet NSButton *updateProgressButton;
@property (strong) IBOutlet NSWindow *updateProgressWindow;
@property (strong) IBOutlet NSProgressIndicator *updateProgressSpinner;
@end

View File

@ -4,11 +4,33 @@
#include <Core/gb.h>
#import <Carbon/Carbon.h>
#import <JoyKit/JoyKit.h>
#import <WebKit/WebKit.h>
#define UPDATE_SERVER "https://sameboy.github.io"
static uint32_t color_to_int(NSColor *color)
{
color = [color colorUsingColorSpace:[NSColorSpace deviceRGBColorSpace]];
return (((unsigned)(color.redComponent * 0xFF)) << 16) |
(((unsigned)(color.greenComponent * 0xFF)) << 8) |
((unsigned)(color.blueComponent * 0xFF));
}
@implementation AppDelegate
{
NSWindow *preferences_window;
NSArray<NSView *> *preferences_tabs;
NSString *_lastVersion;
NSString *_updateURL;
NSURLSessionDownloadTask *_updateTask;
enum {
UPDATE_DOWNLOADING,
UPDATE_EXTRACTING,
UPDATE_WAIT_INSTALL,
UPDATE_INSTALLING,
UPDATE_FAILED,
} _updateState;
NSString *_downloadDirectory;
}
- (void) applicationDidFinishLaunching:(NSNotification *)notification
@ -44,6 +66,11 @@
@"GBCGBModel": @(GB_MODEL_CGB_E),
@"GBSGBModel": @(GB_MODEL_SGB2),
@"GBRumbleMode": @(GB_RUMBLE_CARTRIDGE_ONLY),
@"GBVolume": @(1.0),
@"GBMBC7JoystickOverride": @NO,
@"GBMBC7AllowMouse": @YES,
}];
[JOYController startOnRunLoop:[NSRunLoop currentRunLoop] withOptions:@{
@ -54,6 +81,16 @@
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBNotificationsUsed"]) {
[NSUserNotificationCenter defaultUserNotificationCenter].delegate = self;
}
[self askAutoUpdates];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBAutoUpdatesEnabled"]) {
[self checkForUpdates];
}
if ([[NSProcessInfo processInfo].arguments containsObject:@"--update-launch"]) {
[NSApp activateIgnoringOtherApps:true];
}
}
- (IBAction)toggleDeveloperMode:(id)sender
@ -72,7 +109,7 @@
NSRect new = [_preferencesWindow frameRectForContentRect:tab.frame];
new.origin.x = old.origin.x;
new.origin.y = old.origin.y + (old.size.height - new.size.height);
[_preferencesWindow setFrame:new display:YES animate:_preferencesWindow.visible];
[_preferencesWindow setFrame:new display:true animate:_preferencesWindow.visible];
[_preferencesWindow.contentView addSubview:tab];
}
@ -111,22 +148,303 @@
[[NSBundle mainBundle] loadNibNamed:@"Preferences" owner:self topLevelObjects:&objects];
NSToolbarItem *first_toolbar_item = [_preferencesWindow.toolbar.items firstObject];
_preferencesWindow.toolbar.selectedItemIdentifier = [first_toolbar_item itemIdentifier];
preferences_tabs = @[self.emulationTab, self.graphicsTab, self.audioTab, self.controlsTab];
preferences_tabs = @[self.emulationTab, self.graphicsTab, self.audioTab, self.controlsTab, self.updatesTab];
[self switchPreferencesTab:first_toolbar_item];
[_preferencesWindow center];
#ifndef UPDATE_SUPPORT
[_preferencesWindow.toolbar removeItemAtIndex:4];
#endif
}
[_preferencesWindow makeKeyAndOrderFront:self];
}
- (BOOL)applicationOpenUntitledFile:(NSApplication *)sender
{
[self askAutoUpdates];
/* Bring an existing panel to the foreground */
for (NSWindow *window in [[NSApplication sharedApplication] windows]) {
if ([window isKindOfClass:[NSOpenPanel class]]) {
[(NSOpenPanel *)window makeKeyAndOrderFront:nil];
return true;
}
}
[[NSDocumentController sharedDocumentController] openDocument:self];
return YES;
return true;
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfFile:notification.identifier display:YES];
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfFile:notification.identifier display:true];
}
- (void)updateFound
{
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@UPDATE_SERVER "/raw_changes"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSColor *linkColor = [NSColor colorWithRed:0.125 green:0.325 blue:1.0 alpha:1.0];
if (@available(macOS 10.10, *)) {
linkColor = [NSColor linkColor];
}
NSString *changes = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSRange cutoffRange = [changes rangeOfString:@"<!--(" GB_VERSION ")-->"];
if (cutoffRange.location != NSNotFound) {
changes = [changes substringToIndex:cutoffRange.location];
}
NSString *html = [NSString stringWithFormat:@"<!DOCTYPE html><html><head><title></title>"
"<style>html {background-color:transparent; color: #%06x; line-height:1.5} a:link, a:visited{color:#%06x; text-decoration:none}</style>"
"</head><body>%@</body></html>",
color_to_int([NSColor textColor]),
color_to_int(linkColor),
changes];
if ([(NSHTTPURLResponse *)response statusCode] == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *objects;
[[NSBundle mainBundle] loadNibNamed:@"UpdateWindow" owner:self topLevelObjects:&objects];
self.updateChanges.preferences.standardFontFamily = [NSFont systemFontOfSize:0].familyName;
self.updateChanges.preferences.fixedFontFamily = @"Menlo";
self.updateChanges.drawsBackground = false;
[self.updateChanges.mainFrame loadHTMLString:html baseURL:nil];
});
}
}] resume];
}
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItems
{
// Disable reload context menu
if ([defaultMenuItems count] <= 2) {
return nil;
}
return defaultMenuItems;
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sender.mainFrame.frameView.documentView.enclosingScrollView.drawsBackground = true;
sender.mainFrame.frameView.documentView.enclosingScrollView.backgroundColor = [NSColor textBackgroundColor];
sender.policyDelegate = self;
[self.updateWindow center];
[self.updateWindow makeKeyAndOrderFront:nil];
});
}
- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
{
[listener ignore];
[[NSWorkspace sharedWorkspace] openURL:[request URL]];
}
- (void)checkForUpdates
{
#ifdef UPDATE_SUPPORT
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@UPDATE_SERVER "/latest_version"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.updatesSpinner stopAnimation:nil];
[self.updatesButton setEnabled:true];
});
if ([(NSHTTPURLResponse *)response statusCode] == 200) {
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray <NSString *> *components = [string componentsSeparatedByString:@"|"];
if (components.count != 2) return;
_lastVersion = components[0];
_updateURL = components[1];
if (![@GB_VERSION isEqualToString:_lastVersion] &&
![[[NSUserDefaults standardUserDefaults] stringForKey:@"GBSkippedVersion"] isEqualToString:_lastVersion]) {
[self updateFound];
}
}
}] resume];
#endif
}
- (IBAction)userCheckForUpdates:(id)sender
{
if (self.updateWindow) {
[self.updateWindow makeKeyAndOrderFront:sender];
}
else {
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"GBSkippedVersion"];
[self checkForUpdates];
[sender setEnabled:false];
[self.updatesSpinner startAnimation:sender];
}
}
- (void)askAutoUpdates
{
#ifdef UPDATE_SUPPORT
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"GBAskedAutoUpdates"]) {
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = @"Should SameBoy check for updates when launched?";
alert.informativeText = @"SameBoy is frequently updated with new features, accuracy improvements, and bug fixes. This setting can always be changed in the preferences window.";
[alert addButtonWithTitle:@"Check on Launch"];
[alert addButtonWithTitle:@"Don't Check on Launch"];
[[NSUserDefaults standardUserDefaults] setBool:[alert runModal] == NSAlertFirstButtonReturn forKey:@"GBAutoUpdatesEnabled"];
[[NSUserDefaults standardUserDefaults] setBool:true forKey:@"GBAskedAutoUpdates"];
}
#endif
}
- (IBAction)skipVersion:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:_lastVersion forKey:@"GBSkippedVersion"];
[self.updateWindow performClose:sender];
}
- (IBAction)installUpdate:(id)sender
{
[self.updateProgressSpinner startAnimation:nil];
self.updateProgressButton.title = @"Cancel";
self.updateProgressButton.enabled = true;
self.updateProgressLabel.stringValue = @"Downloading update...";
_updateState = UPDATE_DOWNLOADING;
_updateTask = [[NSURLSession sharedSession] downloadTaskWithURL: [NSURL URLWithString:_updateURL] completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
_updateTask = nil;
dispatch_sync(dispatch_get_main_queue(), ^{
self.updateProgressButton.enabled = false;
self.updateProgressLabel.stringValue = @"Extracting update...";
_updateState = UPDATE_EXTRACTING;
});
_downloadDirectory = [[[NSFileManager defaultManager] URLForDirectory:NSItemReplacementDirectory
inDomain:NSUserDomainMask
appropriateForURL:[[NSBundle mainBundle] bundleURL]
create:true
error:nil] path];
NSTask *unzipTask;
if (!_downloadDirectory) {
dispatch_sync(dispatch_get_main_queue(), ^{
self.updateProgressButton.enabled = false;
self.updateProgressLabel.stringValue = @"Failed to extract update.";
_updateState = UPDATE_FAILED;
self.updateProgressButton.title = @"Close";
self.updateProgressButton.enabled = true;
[self.updateProgressSpinner stopAnimation:nil];
});
return;
}
unzipTask = [[NSTask alloc] init];
unzipTask.launchPath = @"/usr/bin/unzip";
unzipTask.arguments = @[location.path, @"-d", _downloadDirectory];
[unzipTask launch];
[unzipTask waitUntilExit];
if (unzipTask.terminationStatus != 0 || unzipTask.terminationReason != NSTaskTerminationReasonExit) {
[[NSFileManager defaultManager] removeItemAtPath:_downloadDirectory error:nil];
dispatch_sync(dispatch_get_main_queue(), ^{
self.updateProgressButton.enabled = false;
self.updateProgressLabel.stringValue = @"Failed to extract update.";
_updateState = UPDATE_FAILED;
self.updateProgressButton.title = @"Close";
self.updateProgressButton.enabled = true;
[self.updateProgressSpinner stopAnimation:nil];
});
return;
}
dispatch_sync(dispatch_get_main_queue(), ^{
self.updateProgressButton.enabled = false;
self.updateProgressLabel.stringValue = @"Update ready, save your game progress and click Install.";
_updateState = UPDATE_WAIT_INSTALL;
self.updateProgressButton.title = @"Install";
self.updateProgressButton.enabled = true;
[self.updateProgressSpinner stopAnimation:nil];
});
}];
[_updateTask resume];
self.updateProgressWindow.preventsApplicationTerminationWhenModal = false;
[self.updateWindow beginSheet:self.updateProgressWindow completionHandler:^(NSModalResponse returnCode) {
[self.updateWindow close];
}];
}
- (void)performUpgrade
{
self.updateProgressButton.enabled = false;
self.updateProgressLabel.stringValue = @"Instaling update...";
_updateState = UPDATE_INSTALLING;
self.updateProgressButton.enabled = false;
[self.updateProgressSpinner startAnimation:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *executablePath = [[NSBundle mainBundle] executablePath];
NSString *contentsPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents"];
NSString *contentsTempPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"TempContents"];
NSString *updateContentsPath = [_downloadDirectory stringByAppendingPathComponent:@"SameBoy.app/Contents"];
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:contentsPath toPath:contentsTempPath error:&error];
if (error) {
[[NSFileManager defaultManager] removeItemAtPath:_downloadDirectory error:nil];
_downloadDirectory = nil;
dispatch_sync(dispatch_get_main_queue(), ^{
self.updateProgressButton.enabled = false;
self.updateProgressLabel.stringValue = @"Failed to install update.";
_updateState = UPDATE_FAILED;
self.updateProgressButton.title = @"Close";
self.updateProgressButton.enabled = true;
[self.updateProgressSpinner stopAnimation:nil];
});
return;
}
[[NSFileManager defaultManager] moveItemAtPath:updateContentsPath toPath:contentsPath error:&error];
if (error) {
[[NSFileManager defaultManager] moveItemAtPath:contentsTempPath toPath:contentsPath error:nil];
[[NSFileManager defaultManager] removeItemAtPath:_downloadDirectory error:nil];
_downloadDirectory = nil;
dispatch_sync(dispatch_get_main_queue(), ^{
self.updateProgressButton.enabled = false;
self.updateProgressLabel.stringValue = @"Failed to install update.";
_updateState = UPDATE_FAILED;
self.updateProgressButton.title = @"Close";
self.updateProgressButton.enabled = true;
[self.updateProgressSpinner stopAnimation:nil];
});
return;
}
[[NSFileManager defaultManager] removeItemAtPath:_downloadDirectory error:nil];
[[NSFileManager defaultManager] removeItemAtPath:contentsTempPath error:nil];
_downloadDirectory = nil;
atexit_b(^{
execl(executablePath.UTF8String, executablePath.UTF8String, "--update-launch", NULL);
});
dispatch_async(dispatch_get_main_queue(), ^{
[NSApp terminate:nil];
});
});
}
- (IBAction)updateAction:(id)sender
{
switch (_updateState) {
case UPDATE_DOWNLOADING:
[_updateTask cancelByProducingResumeData:nil];
_updateTask = nil;
[self.updateProgressWindow close];
break;
case UPDATE_WAIT_INSTALL:
[self performUpgrade];
break;
case UPDATE_EXTRACTING:
case UPDATE_INSTALLING:
break;
case UPDATE_FAILED:
[self.updateProgressWindow close];
break;
}
}
- (void)dealloc
{
if (_downloadDirectory) {
[[NSFileManager defaultManager] removeItemAtPath:_downloadDirectory error:nil];
}
}
- (IBAction)nop:(id)sender

Binary file not shown.

View File

@ -18,7 +18,7 @@ typedef NS_ENUM(NSInteger, NSWindowToolbarStyle) {
} API_AVAILABLE(macos(11.0));
@interface NSWindow (toolbarStyle)
@property NSWindowToolbarStyle toolbarStyle API_AVAILABLE(macos(11.0));
@property (nonatomic) NSWindowToolbarStyle toolbarStyle API_AVAILABLE(macos(11.0));
@end
@interface NSImage (SFSymbols)

View File

@ -2,47 +2,60 @@
#include "GBView.h"
#include "GBImageView.h"
#include "GBSplitView.h"
#include "GBVisualizerView.h"
#include "GBOSDView.h"
@class GBCheatWindowController;
@interface Document : NSDocument <NSWindowDelegate, GBImageViewDelegate, NSTableViewDataSource, NSTableViewDelegate, NSSplitViewDelegate>
@property (readonly) GB_gameboy_t *gb;
@property (strong) IBOutlet GBView *view;
@property (strong) IBOutlet NSTextView *consoleOutput;
@property (strong) IBOutlet NSPanel *consoleWindow;
@property (strong) IBOutlet NSTextField *consoleInput;
@property (strong) IBOutlet NSWindow *mainWindow;
@property (strong) IBOutlet NSView *memoryView;
@property (strong) IBOutlet NSPanel *memoryWindow;
@property (readonly) GB_gameboy_t *gameboy;
@property (strong) IBOutlet NSTextField *memoryBankInput;
@property (strong) IBOutlet NSToolbarItem *memoryBankItem;
@property (strong) IBOutlet GBImageView *tilesetImageView;
@property (strong) IBOutlet NSPopUpButton *tilesetPaletteButton;
@property (strong) IBOutlet GBImageView *tilemapImageView;
@property (strong) IBOutlet NSPopUpButton *tilemapPaletteButton;
@property (strong) IBOutlet NSPopUpButton *tilemapMapButton;
@property (strong) IBOutlet NSPopUpButton *TilemapSetButton;
@property (strong) IBOutlet NSButton *gridButton;
@property (strong) IBOutlet NSTabView *vramTabView;
@property (strong) IBOutlet NSPanel *vramWindow;
@property (strong) IBOutlet NSTextField *vramStatusLabel;
@property (strong) IBOutlet NSTableView *paletteTableView;
@property (strong) IBOutlet NSTableView *spritesTableView;
@property (strong) IBOutlet NSPanel *printerFeedWindow;
@property (strong) IBOutlet NSImageView *feedImageView;
@property (strong) IBOutlet NSTextView *debuggerSideViewInput;
@property (strong) IBOutlet NSTextView *debuggerSideView;
@property (strong) IBOutlet GBSplitView *debuggerSplitView;
@property (strong) IBOutlet NSBox *debuggerVerticalLine;
@property (strong) IBOutlet NSPanel *cheatsWindow;
@property (strong) IBOutlet GBCheatWindowController *cheatWindowController;
@property (readonly) Document *partner;
@property (readonly) bool isSlave;
@property (nonatomic, readonly) GB_gameboy_t *gb;
@property (nonatomic, strong) IBOutlet GBView *view;
@property (nonatomic, strong) IBOutlet NSTextView *consoleOutput;
@property (nonatomic, strong) IBOutlet NSPanel *consoleWindow;
@property (nonatomic, strong) IBOutlet NSTextField *consoleInput;
@property (nonatomic, strong) IBOutlet NSWindow *mainWindow;
@property (nonatomic, strong) IBOutlet NSView *memoryView;
@property (nonatomic, strong) IBOutlet NSPanel *memoryWindow;
@property (nonatomic, readonly) GB_gameboy_t *gameboy;
@property (nonatomic, strong) IBOutlet NSTextField *memoryBankInput;
@property (nonatomic, strong) IBOutlet NSToolbarItem *memoryBankItem;
@property (nonatomic, strong) IBOutlet GBImageView *tilesetImageView;
@property (nonatomic, strong) IBOutlet NSPopUpButton *tilesetPaletteButton;
@property (nonatomic, strong) IBOutlet GBImageView *tilemapImageView;
@property (nonatomic, strong) IBOutlet NSPopUpButton *tilemapPaletteButton;
@property (nonatomic, strong) IBOutlet NSPopUpButton *tilemapMapButton;
@property (nonatomic, strong) IBOutlet NSPopUpButton *TilemapSetButton;
@property (nonatomic, strong) IBOutlet NSButton *gridButton;
@property (nonatomic, strong) IBOutlet NSTabView *vramTabView;
@property (nonatomic, strong) IBOutlet NSPanel *vramWindow;
@property (nonatomic, strong) IBOutlet NSTextField *vramStatusLabel;
@property (nonatomic, strong) IBOutlet NSTableView *paletteTableView;
@property (nonatomic, strong) IBOutlet NSTableView *objectsTableView;
@property (nonatomic, strong) IBOutlet NSPanel *printerFeedWindow;
@property (nonatomic, strong) IBOutlet NSImageView *feedImageView;
@property (nonatomic, strong) IBOutlet NSTextView *debuggerSideViewInput;
@property (nonatomic, strong) IBOutlet NSTextView *debuggerSideView;
@property (nonatomic, strong) IBOutlet GBSplitView *debuggerSplitView;
@property (nonatomic, strong) IBOutlet NSBox *debuggerVerticalLine;
@property (nonatomic, strong) IBOutlet NSPanel *cheatsWindow;
@property (nonatomic, strong) IBOutlet GBCheatWindowController *cheatWindowController;
@property (nonatomic, readonly) Document *partner;
@property (nonatomic, readonly) bool isSlave;
@property (strong) IBOutlet NSView *gbsPlayerView;
@property (strong) IBOutlet NSTextField *gbsTitle;
@property (strong) IBOutlet NSTextField *gbsAuthor;
@property (strong) IBOutlet NSTextField *gbsCopyright;
@property (strong) IBOutlet NSPopUpButton *gbsTracks;
@property (strong) IBOutlet NSButton *gbsPlayPauseButton;
@property (strong) IBOutlet NSButton *gbsRewindButton;
@property (strong) IBOutlet NSSegmentedControl *gbsNextPrevButton;
@property (strong) IBOutlet GBVisualizerView *gbsVisualizer;
@property (strong) IBOutlet GBOSDView *osdView;
-(uint8_t) readMemory:(uint16_t) addr;
-(void) writeMemory:(uint16_t) addr value:(uint8_t)value;
-(void) performAtomicBlock: (void (^)())block;
-(void) connectLinkCable:(NSMenuItem *)sender;
-(int)loadStateFile:(const char *)path noErrorOnNotFound:(bool)noErrorOnFileNotFound;
@end

View File

@ -10,6 +10,9 @@
#include "GBCheatWindowController.h"
#include "GBTerminalTextFieldCell.h"
#include "BigSurToolbar.h"
#import "GBPaletteEditorController.h"
#define GB_MODEL_PAL_BIT_OLD 0x1000
/* Todo: The general Objective-C coding style conflicts with SameBoy's. This file needs a cleanup. */
/* Todo: Split into category files! This is so messy!!! */
@ -20,6 +23,7 @@ enum model {
MODEL_CGB,
MODEL_AGB,
MODEL_SGB,
MODEL_MGB,
};
@interface Document ()
@ -65,6 +69,7 @@ enum model {
size_t audioBufferSize;
size_t audioBufferPosition;
size_t audioBufferNeeded;
double _volume;
bool borderModeChanged;
@ -208,6 +213,7 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
debugger_input_queue = [[NSMutableArray alloc] init];
console_output_lock = [[NSRecursiveLock alloc] init];
audioLock = [[NSCondition alloc] init];
_volume = [[NSUserDefaults standardUserDefaults] doubleForKey:@"GBVolume"];
}
return self;
}
@ -237,8 +243,16 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
case MODEL_CGB:
return (GB_model_t)[[NSUserDefaults standardUserDefaults] integerForKey:@"GBCGBModel"];
case MODEL_SGB:
return (GB_model_t)[[NSUserDefaults standardUserDefaults] integerForKey:@"GBSGBModel"];
case MODEL_SGB: {
GB_model_t model = (GB_model_t)[[NSUserDefaults standardUserDefaults] integerForKey:@"GBSGBModel"];
if (model == (GB_MODEL_SGB | GB_MODEL_PAL_BIT_OLD)) {
model = GB_MODEL_SGB_PAL;
}
return model;
}
case MODEL_MGB:
return GB_MODEL_MGB;
case MODEL_AGB:
return GB_MODEL_AGB;
@ -247,23 +261,7 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
- (void) updatePalette
{
switch ([[NSUserDefaults standardUserDefaults] integerForKey:@"GBColorPalette"]) {
case 1:
GB_set_palette(&gb, &GB_PALETTE_DMG);
break;
case 2:
GB_set_palette(&gb, &GB_PALETTE_MGB);
break;
case 3:
GB_set_palette(&gb, &GB_PALETTE_GBL);
break;
default:
GB_set_palette(&gb, &GB_PALETTE_GREY);
break;
}
GB_set_palette(&gb, [GBPaletteEditorController userPalette]);
}
- (void) updateBorderMode
@ -295,6 +293,7 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
GB_set_camera_update_request_callback(&gb, cameraRequestUpdate);
GB_set_highpass_filter_mode(&gb, (GB_highpass_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBHighpassFilter"]);
GB_set_rewind_length(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRewindLength"]);
GB_set_rtc_mode(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRTCMode"]);
GB_apu_set_sample_callback(&gb, audioCallback);
GB_set_rumble_callback(&gb, rumbleCallback);
GB_set_infrared_callback(&gb, infraredStateChanged);
@ -308,10 +307,16 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
self.mainWindow.contentView.bounds.size.width < GB_get_screen_height(&gb)) {
[self.mainWindow zoom:nil];
}
self.osdView.usesSGBScale = GB_get_screen_width(&gb) == 256;
}
- (void) vblank
{
if (_gbsVisualizer) {
dispatch_async(dispatch_get_main_queue(), ^{
[_gbsVisualizer setNeedsDisplay:true];
});
}
[self.view flip];
if (borderModeChanged) {
dispatch_sync(dispatch_get_main_queue(), ^{
@ -327,19 +332,23 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
GB_set_pixels_output(&gb, self.view.pixels);
if (self.vramWindow.isVisible) {
dispatch_async(dispatch_get_main_queue(), ^{
self.view.mouseHidingEnabled = (self.mainWindow.styleMask & NSFullScreenWindowMask) != 0;
self.view.mouseHidingEnabled = (self.mainWindow.styleMask & NSWindowStyleMaskFullScreen) != 0;
[self reloadVRAMData: nil];
});
}
if (self.view.isRewinding) {
rewind = true;
[self.osdView displayText:@"Rewinding..."];
}
}
- (void)gotNewSample:(GB_sample_t *)sample
{
if (_gbsVisualizer) {
[_gbsVisualizer addSample:sample];
}
[audioLock lock];
if (self.audioClient.isPlaying) {
if (_audioClient.isPlaying) {
if (audioBufferPosition == audioBufferSize) {
if (audioBufferSize >= 0x4000) {
audioBufferPosition = 0;
@ -355,6 +364,10 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
}
audioBuffer = realloc(audioBuffer, sizeof(*sample) * audioBufferSize);
}
if (_volume != 1) {
sample->left *= _volume;
sample->right *= _volume;
}
audioBuffer[audioBufferPosition++] = *sample;
}
if (audioBufferPosition == audioBufferNeeded) {
@ -373,21 +386,27 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
{
GB_set_pixels_output(&gb, self.view.pixels);
GB_set_sample_rate(&gb, 96000);
self.audioClient = [[GBAudioClient alloc] initWithRendererBlock:^(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer) {
_audioClient = [[GBAudioClient alloc] initWithRendererBlock:^(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer) {
[audioLock lock];
if (audioBufferPosition < nFrames) {
audioBufferNeeded = nFrames;
[audioLock wait];
[audioLock waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.125]];
}
if (stopping) {
if (stopping || GB_debugger_is_stopped(&gb)) {
memset(buffer, 0, nFrames * sizeof(*buffer));
[audioLock unlock];
return;
}
if (audioBufferPosition >= nFrames && audioBufferPosition < nFrames + 4800) {
if (audioBufferPosition < nFrames) {
// Not enough audio
memset(buffer, 0, (nFrames - audioBufferPosition) * sizeof(*buffer));
memcpy(buffer, audioBuffer, audioBufferPosition * sizeof(*buffer));
audioBufferPosition = 0;
}
else if (audioBufferPosition < nFrames + 4800) {
memcpy(buffer, audioBuffer, nFrames * sizeof(*buffer));
memmove(audioBuffer, audioBuffer + nFrames, (audioBufferPosition - nFrames) * sizeof(*buffer));
audioBufferPosition = audioBufferPosition - nFrames;
@ -399,23 +418,23 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on)
[audioLock unlock];
} andSampleRate:96000];
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"Mute"]) {
[self.audioClient start];
[_audioClient start];
}
hex_timer = [NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(reloadMemoryView) userInfo:nil repeats:YES];
hex_timer = [NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(reloadMemoryView) userInfo:nil repeats:true];
[[NSRunLoop mainRunLoop] addTimer:hex_timer forMode:NSDefaultRunLoopMode];
/* Clear pending alarms, don't play alarms while playing */
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBNotificationsUsed"]) {
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
for (NSUserNotification *notification in [center scheduledNotifications]) {
if ([notification.identifier isEqualToString:self.fileName]) {
if ([notification.identifier isEqualToString:self.fileURL.path]) {
[center removeScheduledNotification:notification];
break;
}
}
for (NSUserNotification *notification in [center deliveredNotifications]) {
if ([notification.identifier isEqualToString:self.fileName]) {
if ([notification.identifier isEqualToString:self.fileURL.path]) {
[center removeDeliveredNotification:notification];
break;
}
@ -479,24 +498,24 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
audioBufferPosition = audioBufferNeeded;
[audioLock signal];
[audioLock unlock];
[self.audioClient stop];
self.audioClient = nil;
self.view.mouseHidingEnabled = NO;
GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]);
GB_save_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]);
[_audioClient stop];
_audioClient = nil;
self.view.mouseHidingEnabled = false;
GB_save_battery(&gb, [[[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"sav"].path UTF8String]);
GB_save_cheats(&gb, [[[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"cht"].path UTF8String]);
unsigned time_to_alarm = GB_time_to_alarm(&gb);
if (time_to_alarm) {
[NSUserNotificationCenter defaultUserNotificationCenter].delegate = (id)[NSApp delegate];
NSUserNotification *notification = [[NSUserNotification alloc] init];
NSString *friendlyName = [[self.fileName lastPathComponent] stringByDeletingPathExtension];
NSString *friendlyName = [[self.fileURL lastPathComponent] stringByDeletingPathExtension];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\([^)]+\\)|\\[[^\\]]+\\]" options:0 error:nil];
friendlyName = [regex stringByReplacingMatchesInString:friendlyName options:0 range:NSMakeRange(0, [friendlyName length]) withTemplate:@""];
friendlyName = [friendlyName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
notification.title = [NSString stringWithFormat:@"%@ Played an Alarm", friendlyName];
notification.informativeText = [NSString stringWithFormat:@"%@ requested your attention by playing a scheduled alarm", friendlyName];
notification.identifier = self.fileName;
notification.identifier = self.fileURL.path;
notification.deliveryDate = [NSDate dateWithTimeIntervalSinceNow:time_to_alarm];
notification.soundName = NSUserNotificationDefaultSoundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];
@ -507,7 +526,8 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (void) start
{
self.view.mouseHidingEnabled = (self.mainWindow.styleMask & NSFullScreenWindowMask) != 0;
self.gbsPlayPauseButton.state = true;
self.view.mouseHidingEnabled = (self.mainWindow.styleMask & NSWindowStyleMaskFullScreen) != 0;
if (master) {
[master start];
return;
@ -518,6 +538,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (void) stop
{
self.gbsPlayPauseButton.state = false;
if (master) {
if (!master->running) return;
GB_debugger_set_disabled(&gb, true);
@ -549,12 +570,12 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (void) loadBootROM: (GB_boot_rom_t)type
{
static NSString *const names[] = {
[GB_BOOT_ROM_DMG0] = @"dmg0_boot",
[GB_BOOT_ROM_DMG_0] = @"dmg0_boot",
[GB_BOOT_ROM_DMG] = @"dmg_boot",
[GB_BOOT_ROM_MGB] = @"mgb_boot",
[GB_BOOT_ROM_SGB] = @"sgb_boot",
[GB_BOOT_ROM_SGB2] = @"sgb2_boot",
[GB_BOOT_ROM_CGB0] = @"cgb0_boot",
[GB_BOOT_ROM_CGB_0] = @"cgb0_boot",
[GB_BOOT_ROM_CGB] = @"cgb_boot",
[GB_BOOT_ROM_AGB] = @"agb_boot",
};
@ -570,12 +591,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
current_model = (enum model)[sender tag];
}
if (!modelsChanging && [sender tag] == MODEL_NONE) {
GB_reset(&gb);
}
else {
GB_switch_model_and_reset(&gb, [self internalModel]);
}
GB_switch_model_and_reset(&gb, [self internalModel]);
if (old_width != GB_get_screen_width(&gb)) {
[self.view screenSizeChanged];
@ -588,6 +604,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
[[NSUserDefaults standardUserDefaults] setBool:current_model == MODEL_DMG forKey:@"EmulateDMG"];
[[NSUserDefaults standardUserDefaults] setBool:current_model == MODEL_SGB forKey:@"EmulateSGB"];
[[NSUserDefaults standardUserDefaults] setBool:current_model == MODEL_AGB forKey:@"EmulateAGB"];
[[NSUserDefaults standardUserDefaults] setBool:current_model == MODEL_MGB forKey:@"EmulateMGB"];
}
/* Reload the ROM, SAV and SYM files */
@ -600,6 +617,10 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
[(GBMemoryByteArray *)(hex_controller.byteArray) setSelectedBank:0];
[self hexUpdateBank:self.memoryBankInput ignoreErrors:true];
}
char title[17];
GB_get_rom_title(&gb, title);
[self.osdView displayText:[NSString stringWithFormat:@"SameBoy v" GB_VERSION "\n%s\n%08X", title, GB_get_rom_crc32(&gb)]];
}
- (IBAction)togglePause:(id)sender
@ -662,12 +683,16 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
window_frame.size.width);
window_frame.size.height = MAX([[NSUserDefaults standardUserDefaults] integerForKey:@"LastWindowHeight"],
window_frame.size.height);
[self.mainWindow setFrame:window_frame display:YES];
[self.mainWindow setFrame:window_frame display:true];
self.vramStatusLabel.cell.backgroundStyle = NSBackgroundStyleRaised;
NSUInteger height_diff = self.vramWindow.frame.size.height - self.vramWindow.contentView.frame.size.height;
CGRect vram_window_rect = self.vramWindow.frame;
vram_window_rect.size.height = 384 + height_diff + 48;
[self.vramWindow setFrame:vram_window_rect display:true animate:false];
self.consoleWindow.title = [NSString stringWithFormat:@"Debug Console %@", [[self.fileURL path] lastPathComponent]];
self.consoleWindow.title = [NSString stringWithFormat:@"Debug Console %@", [self.fileURL.path lastPathComponent]];
self.debuggerSplitView.dividerColor = [NSColor clearColor];
if (@available(macOS 11.0, *)) {
self.memoryWindow.toolbarStyle = NSWindowToolbarStyleExpanded;
@ -726,6 +751,12 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
name:@"GBRewindLengthChanged"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateRTCMode)
name:@"GBRTCModeChanged"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dmgModelChanged)
name:@"GBDMGModelChanged"
@ -741,22 +772,37 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
name:@"GBCGBModelChanged"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateVolume)
name:@"GBVolumeChanged"
object:nil];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateDMG"]) {
current_model = MODEL_DMG;
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateSGB"]) {
current_model = MODEL_SGB;
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateMGB"]) {
current_model = MODEL_MGB;
}
else {
current_model = [[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateAGB"]? MODEL_AGB : MODEL_CGB;
}
[self initCommon];
self.view.gb = &gb;
self.view.osdView = _osdView;
[self.view screenSizeChanged];
[self loadROM];
[self reset:nil];
if ([self loadROM]) {
_mainWindow.alphaValue = 0; // Hack hack ugly hack
dispatch_async(dispatch_get_main_queue(), ^{
[self close];
});
}
else {
[self reset:nil];
}
}
- (void) initMemoryView
@ -807,7 +853,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
+ (BOOL)autosavesInPlace
{
return YES;
return true;
}
- (NSString *)windowNibName
@ -819,40 +865,134 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type
{
return YES;
return true;
}
- (void) loadROM
- (IBAction)changeGBSTrack:(id)sender
{
NSString *rom_warnings = [self captureOutputForBlock:^{
GB_debugger_clear_symbols(&gb);
if ([[self.fileType pathExtension] isEqualToString:@"isx"]) {
GB_load_isx(&gb, [self.fileName UTF8String]);
GB_load_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"ram"] UTF8String]);
if (!running) {
[self start];
}
[self performAtomicBlock:^{
GB_gbs_switch_track(&gb, self.gbsTracks.indexOfSelectedItem);
}];
}
- (IBAction)gbsNextPrevPushed:(id)sender
{
if (self.gbsNextPrevButton.selectedSegment == 0) {
// Previous
if (self.gbsTracks.indexOfSelectedItem == 0) {
[self.gbsTracks selectItemAtIndex:self.gbsTracks.numberOfItems - 1];
}
else {
GB_load_rom(&gb, [self.fileName UTF8String]);
[self.gbsTracks selectItemAtIndex:self.gbsTracks.indexOfSelectedItem - 1];
}
GB_load_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]);
GB_load_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]);
}
else {
// Next
if (self.gbsTracks.indexOfSelectedItem == self.gbsTracks.numberOfItems - 1) {
[self.gbsTracks selectItemAtIndex: 0];
}
else {
[self.gbsTracks selectItemAtIndex:self.gbsTracks.indexOfSelectedItem + 1];
}
}
[self changeGBSTrack:sender];
}
- (void)prepareGBSInterface: (GB_gbs_info_t *)info
{
GB_set_rendering_disabled(&gb, true);
_view = nil;
for (NSView *view in [_mainWindow.contentView.subviews copy]) {
[view removeFromSuperview];
}
[[NSBundle mainBundle] loadNibNamed:@"GBS" owner:self topLevelObjects:nil];
[_mainWindow setContentSize:self.gbsPlayerView.bounds.size];
_mainWindow.styleMask &= ~NSWindowStyleMaskResizable;
dispatch_async(dispatch_get_main_queue(), ^{ // Cocoa is weird, no clue why it's needed
[_mainWindow standardWindowButton:NSWindowZoomButton].enabled = false;
});
[_mainWindow.contentView addSubview:self.gbsPlayerView];
_mainWindow.movableByWindowBackground = true;
[_mainWindow setContentBorderThickness:24 forEdge:NSRectEdgeMinY];
self.gbsTitle.stringValue = [NSString stringWithCString:info->title encoding:NSISOLatin1StringEncoding] ?: @"GBS Player";
self.gbsAuthor.stringValue = [NSString stringWithCString:info->author encoding:NSISOLatin1StringEncoding] ?: @"Unknown Composer";
NSString *copyright = [NSString stringWithCString:info->copyright encoding:NSISOLatin1StringEncoding];
if (copyright) {
copyright = [@"©" stringByAppendingString:copyright];
}
self.gbsCopyright.stringValue = copyright ?: @"Missing copyright information";
for (unsigned i = 0; i < info->track_count; i++) {
[self.gbsTracks addItemWithTitle:[NSString stringWithFormat:@"Track %u", i + 1]];
}
[self.gbsTracks selectItemAtIndex:info->first_track];
self.gbsPlayPauseButton.image.template = true;
self.gbsPlayPauseButton.alternateImage.template = true;
self.gbsRewindButton.image.template = true;
for (unsigned i = 0; i < 2; i++) {
[self.gbsNextPrevButton imageForSegment:i].template = true;
}
if (!_audioClient.isPlaying) {
[_audioClient start];
}
if (@available(macOS 10.10, *)) {
_mainWindow.titlebarAppearsTransparent = true;
}
}
- (int) loadROM
{
__block int ret = 0;
NSString *rom_warnings = [self captureOutputForBlock:^{
GB_debugger_clear_symbols(&gb);
if ([[[self.fileType pathExtension] lowercaseString] isEqualToString:@"isx"]) {
ret = GB_load_isx(&gb, self.fileURL.path.UTF8String);
GB_load_battery(&gb, [[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"ram"].path.UTF8String);
}
else if ([[[self.fileType pathExtension] lowercaseString] isEqualToString:@"gbs"]) {
__block GB_gbs_info_t info;
ret = GB_load_gbs(&gb, self.fileURL.path.UTF8String, &info);
[self prepareGBSInterface:&info];
}
else {
ret = GB_load_rom(&gb, [self.fileURL.path UTF8String]);
}
GB_load_battery(&gb, [[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"sav"].path.UTF8String);
GB_load_cheats(&gb, [[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"cht"].path.UTF8String);
[self.cheatWindowController cheatsUpdated];
GB_debugger_load_symbol_file(&gb, [[[NSBundle mainBundle] pathForResource:@"registers" ofType:@"sym"] UTF8String]);
GB_debugger_load_symbol_file(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sym"] UTF8String]);
GB_debugger_load_symbol_file(&gb, [[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"sym"].path.UTF8String);
}];
if (rom_warnings && !rom_warning_issued) {
if (ret) {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:rom_warnings?: @"Could not load ROM"];
[alert setAlertStyle:NSAlertStyleCritical];
[alert runModal];
}
else if (rom_warnings && !rom_warning_issued) {
rom_warning_issued = true;
[GBWarningPopover popoverWithContents:rom_warnings onWindow:self.mainWindow];
}
return ret;
}
- (void)close
{
[self disconnectLinkCable];
[[NSUserDefaults standardUserDefaults] setInteger:self.mainWindow.frame.size.width forKey:@"LastWindowWidth"];
[[NSUserDefaults standardUserDefaults] setInteger:self.mainWindow.frame.size.height forKey:@"LastWindowHeight"];
if (!self.gbsPlayerView) {
[[NSUserDefaults standardUserDefaults] setInteger:self.mainWindow.frame.size.width forKey:@"LastWindowWidth"];
[[NSUserDefaults standardUserDefaults] setInteger:self.mainWindow.frame.size.height forKey:@"LastWindowHeight"];
}
[self stop];
[self.consoleWindow close];
[self.memoryWindow close];
[self.vramWindow close];
[self.printerFeedWindow close];
[self.cheatsWindow close];
[super close];
}
@ -867,19 +1007,22 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (IBAction)mute:(id)sender
{
if (self.audioClient.isPlaying) {
[self.audioClient stop];
if (_audioClient.isPlaying) {
[_audioClient stop];
}
else {
[self.audioClient start];
[_audioClient start];
if (_volume == 0) {
[GBWarningPopover popoverWithContents:@"Warning: Volume is set to to zero in the preferences panel" onWindow:self.mainWindow];
}
}
[[NSUserDefaults standardUserDefaults] setBool:!self.audioClient.isPlaying forKey:@"Mute"];
[[NSUserDefaults standardUserDefaults] setBool:!_audioClient.isPlaying forKey:@"Mute"];
}
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem
{
if ([anItem action] == @selector(mute:)) {
[(NSMenuItem *)anItem setState:!self.audioClient.isPlaying];
[(NSMenuItem *)anItem setState:!_audioClient.isPlaying];
}
else if ([anItem action] == @selector(togglePause:)) {
if (master) {
@ -912,6 +1055,13 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
else if ([anItem action] == @selector(toggleCheats:)) {
[(NSMenuItem*)anItem setState:GB_cheats_enabled(&gb)];
}
else if ([anItem action] == @selector(toggleDisplayBackground:)) {
[(NSMenuItem*)anItem setState:!GB_is_background_rendering_disabled(&gb)];
}
else if ([anItem action] == @selector(toggleDisplayObjects:)) {
[(NSMenuItem*)anItem setState:!GB_is_object_rendering_disabled(&gb)];
}
return [super validateUserInterfaceItem:anItem];
}
@ -925,7 +1075,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (void) windowWillExitFullScreen:(NSNotification *)notification
{
fullScreen = false;
self.view.mouseHidingEnabled = NO;
self.view.mouseHidingEnabled = false;
}
- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)newFrame
@ -1020,14 +1170,14 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
}
if (![console_output_timer isValid]) {
console_output_timer = [NSTimer timerWithTimeInterval:(NSTimeInterval)0.05 target:self selector:@selector(appendPendingOutput) userInfo:nil repeats:NO];
console_output_timer = [NSTimer timerWithTimeInterval:(NSTimeInterval)0.05 target:self selector:@selector(appendPendingOutput) userInfo:nil repeats:false];
[[NSRunLoop mainRunLoop] addTimer:console_output_timer forMode:NSDefaultRunLoopMode];
}
[console_output_lock unlock];
/* Make sure mouse is not hidden while debugging */
self.view.mouseHidingEnabled = NO;
self.view.mouseHidingEnabled = false;
}
- (IBAction)showConsoleWindow:(id)sender
@ -1105,6 +1255,9 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (char *) getDebuggerInput
{
[audioLock lock];
[audioLock signal];
[audioLock unlock];
[self updateSideView];
[self log:">"];
in_sync_input = true;
@ -1144,28 +1297,47 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
{
bool __block success = false;
[self performAtomicBlock:^{
success = GB_save_state(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:[NSString stringWithFormat:@"s%ld", (long)[sender tag] ]] UTF8String]) == 0;
success = GB_save_state(&gb, [[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:[NSString stringWithFormat:@"s%ld", (long)[sender tag] ]].path.UTF8String) == 0;
}];
if (!success) {
[GBWarningPopover popoverWithContents:@"Failed to write save state." onWindow:self.mainWindow];
NSBeep();
}
else {
[self.osdView displayText:@"State saved"];
}
}
- (int)loadStateFile:(const char *)path noErrorOnNotFound:(bool)noErrorOnFileNotFound;
{
int __block result = false;
NSString *error =
[self captureOutputForBlock:^{
result = GB_load_state(&gb, path);
}];
if (result == ENOENT && noErrorOnFileNotFound) {
return ENOENT;
}
if (result) {
NSBeep();
}
else {
[self.osdView displayText:@"State loaded"];
}
if (error) {
[GBWarningPopover popoverWithContents:error onWindow:self.mainWindow];
}
return result;
}
- (IBAction)loadState:(id)sender
{
bool __block success = false;
NSString *error =
[self captureOutputForBlock:^{
success = GB_load_state(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:[NSString stringWithFormat:@"s%ld", (long)[sender tag] ]] UTF8String]) == 0;
}];
if (!success) {
if (error) {
[GBWarningPopover popoverWithContents:error onWindow:self.mainWindow];
}
NSBeep();
int ret = [self loadStateFile:[[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:[NSString stringWithFormat:@"s%ld", (long)[sender tag]]].path.UTF8String noErrorOnNotFound:true];
if (ret == ENOENT) {
[self loadStateFile:[[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:[NSString stringWithFormat:@"sn%ld", (long)[sender tag]]].path.UTF8String noErrorOnNotFound:false];
}
}
@ -1182,7 +1354,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (uint8_t) readMemory:(uint16_t)addr
{
while (!GB_is_inited(&gb));
return GB_read_memory(&gb, addr);
return GB_safe_read_memory(&gb, addr);
}
- (void) writeMemory:(uint16_t)addr value:(uint8_t)value
@ -1232,7 +1404,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
bitmapInfo,
provider,
NULL,
YES,
true,
renderingIntent);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
@ -1253,6 +1425,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (IBAction) reloadVRAMData: (id) sender
{
if (self.vramWindow.isVisible) {
uint8_t *io_regs = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_IO, NULL, NULL);
switch ([self.vramTabView.tabViewItems indexOfObject:self.vramTabView.selectedTabViewItem]) {
case 0:
/* Tileset */
@ -1291,8 +1464,8 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
(GB_map_type_t) self.tilemapMapButton.indexOfSelectedItem,
(GB_tileset_type_t) self.TilemapSetButton.indexOfSelectedItem);
self.tilemapImageView.scrollRect = NSMakeRect(GB_read_memory(&gb, 0xFF00 | GB_IO_SCX),
GB_read_memory(&gb, 0xFF00 | GB_IO_SCY),
self.tilemapImageView.scrollRect = NSMakeRect(io_regs[GB_IO_SCX],
io_regs[GB_IO_SCY],
160, 144);
self.tilemapImageView.image = [Document imageFromData:data width:256 height:256 scale:1.0];
self.tilemapImageView.layer.magnificationFilter = kCAFilterNearest;
@ -1306,7 +1479,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
dispatch_async(dispatch_get_main_queue(), ^{
if (!oamUpdating) {
oamUpdating = true;
[self.spritesTableView reloadData];
[self.objectsTableView reloadData];
oamUpdating = false;
}
});
@ -1440,7 +1613,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
}
[self.memoryBankInput setStringValue:[NSString stringWithFormat:@"$%x", byteArray.selectedBank]];
[hex_controller reloadData];
[self.memoryView setNeedsDisplay:YES];
[self.memoryView setNeedsDisplay:true];
}
- (GB_gameboy_t *) gameboy
@ -1450,12 +1623,12 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
+ (BOOL)canConcurrentlyReadDocumentsOfType:(NSString *)typeName
{
return YES;
return true;
}
- (void)cameraRequestUpdate
{
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@try {
if (!cameraSession) {
if (@available(macOS 10.14, *)) {
@ -1591,14 +1764,14 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
window_rect.size.height = 512 + height_diff + 48;
break;
case 3:
window_rect.size.height = 20 * 16 + height_diff + 24;
window_rect.size.height = 20 * 16 + height_diff + 34;
break;
default:
break;
}
window_rect.origin.y -= window_rect.size.height;
[self.vramWindow setFrame:window_rect display:YES animate:YES];
[self.vramWindow setFrame:window_rect display:true animate:true];
}
- (void)mouseDidLeaveImageView:(GBImageView *)view
@ -1669,7 +1842,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
if (tableView == self.paletteTableView) {
return 16; /* 8 BG palettes, 8 OBJ palettes*/
}
else if (tableView == self.spritesTableView) {
else if (tableView == self.objectsTableView) {
return oamCount;
}
return 0;
@ -1688,12 +1861,12 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
uint16_t index = columnIndex - 1 + (row & 7) * 4;
return @((palette_data[(index << 1) + 1] << 8) | palette_data[(index << 1)]);
}
else if (tableView == self.spritesTableView) {
else if (tableView == self.objectsTableView) {
switch (columnIndex) {
case 0:
return [Document imageFromData:[NSData dataWithBytesNoCopy:oamInfo[row].image
length:64 * 4
freeWhenDone:NO]
length:64 * 4 * 2
freeWhenDone:false]
width:8
height:oamHeight
scale:16.0/oamHeight];
@ -1722,7 +1895,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
oamInfo[row].flags & 0x20? 'X' : '-',
oamInfo[row].flags & 0x10? 1 : 0];
case 7:
return oamInfo[row].obscured_by_line_limit? @"Dropped: Too many sprites in line": @"";
return oamInfo[row].obscured_by_line_limit? @"Dropped: Too many objects in line": @"";
}
}
@ -1731,12 +1904,12 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
{
return tableView == self.spritesTableView;
return tableView == self.objectsTableView;
}
- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
return NO;
return false;
}
- (IBAction)showVRAMViewer:(id)sender
@ -1766,7 +1939,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
frame.size = self.feedImageView.image.size;
[self.printerFeedWindow setContentMaxSize:frame.size];
frame.size.height += self.printerFeedWindow.frame.size.height - self.printerFeedWindow.contentView.frame.size.height;
[self.printerFeedWindow setFrame:frame display:NO animate: self.printerFeedWindow.isVisible];
[self.printerFeedWindow setFrame:frame display:false animate: self.printerFeedWindow.isVisible];
[self.printerFeedWindow orderFront:NULL];
});
@ -1786,19 +1959,19 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
{
bool shouldResume = running;
[self stop];
NSSavePanel * savePanel = [NSSavePanel savePanel];
NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setAllowedFileTypes:@[@"png"]];
[savePanel beginSheetModalForWindow:self.printerFeedWindow completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
if (result == NSModalResponseOK) {
[savePanel orderOut:self];
CGImageRef cgRef = [self.feedImageView.image CGImageForProposedRect:NULL
context:nil
hints:nil];
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef];
[imageRep setSize:(NSSize){160, self.feedImageView.image.size.height / 2}];
NSData *data = [imageRep representationUsingType:NSPNGFileType properties:@{}];
[data writeToURL:savePanel.URL atomically:NO];
[self.printerFeedWindow setIsVisible:NO];
NSData *data = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
[data writeToURL:savePanel.URL atomically:false];
[self.printerFeedWindow setIsVisible:false];
}
if (shouldResume) {
[self start];
@ -1833,6 +2006,11 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
}];
}
- (void) updateVolume
{
_volume = [[NSUserDefaults standardUserDefaults] doubleForKey:@"GBVolume"];
}
- (void) updateHighpassFilter
{
if (GB_is_inited(&gb)) {
@ -1875,6 +2053,13 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
}];
}
- (void) updateRTCMode
{
if (GB_is_inited(&gb)) {
GB_set_rtc_mode(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRTCMode"]);
}
}
- (void)dmgModelChanged
{
modelsChanging = true;
@ -1912,9 +2097,9 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (BOOL)splitView:(GBSplitView *)splitView canCollapseSubview:(NSView *)subview;
{
if ([[splitView arrangedSubviews] lastObject] == subview) {
return YES;
return true;
}
return NO;
return false;
}
- (CGFloat)splitView:(GBSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex
@ -1930,9 +2115,9 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
- (BOOL)splitView:(GBSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)view
{
if ([[splitView arrangedSubviews] lastObject] == view) {
return NO;
return false;
}
return YES;
return true;
}
- (void)splitViewDidResizeSubviews:(NSNotification *)notification
@ -2036,4 +2221,125 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency)
{
return &gb;
}
- (NSImage *)takeScreenshot
{
NSImage *ret = nil;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBFilterScreenshots"]) {
ret = [_view renderToImage];
[ret lockFocus];
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0,
ret.size.width, ret.size.height)];
[ret unlockFocus];
ret = [[NSImage alloc] initWithSize:ret.size];
[ret addRepresentation:bitmapRep];
}
if (!ret) {
ret = [Document imageFromData:[NSData dataWithBytesNoCopy:_view.currentBuffer
length:GB_get_screen_width(&gb) * GB_get_screen_height(&gb) * 4
freeWhenDone:false]
width:GB_get_screen_width(&gb)
height:GB_get_screen_height(&gb)
scale:1.0];
}
return ret;
}
- (NSString *)screenshotFilename
{
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterMediumStyle;
return [[NSString stringWithFormat:@"%@ %@.png",
self.fileURL.lastPathComponent.stringByDeletingPathExtension,
[dateFormatter stringFromDate:date]] stringByReplacingOccurrencesOfString:@":" withString:@"."]; // Gotta love Mac OS Classic
}
- (IBAction)saveScreenshot:(id)sender
{
NSString *folder = [[NSUserDefaults standardUserDefaults] stringForKey:@"GBScreenshotFolder"];
BOOL isDirectory = false;
if (folder) {
[[NSFileManager defaultManager] fileExistsAtPath:folder isDirectory:&isDirectory];
}
if (!folder) {
bool shouldResume = running;
[self stop];
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
openPanel.canChooseFiles = false;
openPanel.canChooseDirectories = true;
openPanel.message = @"Choose a folder for screenshots";
[openPanel beginSheetModalForWindow:self.mainWindow completionHandler:^(NSInteger result) {
if (result == NSModalResponseOK) {
[[NSUserDefaults standardUserDefaults] setObject:openPanel.URL.path
forKey:@"GBScreenshotFolder"];
[self saveScreenshot:sender];
}
if (shouldResume) {
[self start];
}
}];
return;
}
NSImage *image = [self takeScreenshot];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterMediumStyle;
NSString *filename = [self screenshotFilename];
filename = [folder stringByAppendingPathComponent:filename];
unsigned i = 2;
while ([[NSFileManager defaultManager] fileExistsAtPath:filename]) {
filename = [[filename stringByDeletingPathExtension] stringByAppendingFormat:@" %d.png", i++];
}
NSBitmapImageRep *imageRep = (NSBitmapImageRep *)image.representations.firstObject;
NSData *data = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
[data writeToFile:filename atomically:false];
[self.osdView displayText:@"Screenshot saved"];
}
- (IBAction)saveScreenshotAs:(id)sender
{
bool shouldResume = running;
[self stop];
NSImage *image = [self takeScreenshot];
NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setNameFieldStringValue:[self screenshotFilename]];
[savePanel beginSheetModalForWindow:self.mainWindow completionHandler:^(NSInteger result) {
if (result == NSModalResponseOK) {
[savePanel orderOut:self];
NSBitmapImageRep *imageRep = (NSBitmapImageRep *)image.representations.firstObject;
NSData *data = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
[data writeToURL:savePanel.URL atomically:false];
[[NSUserDefaults standardUserDefaults] setObject:savePanel.URL.path.stringByDeletingLastPathComponent
forKey:@"GBScreenshotFolder"];
}
if (shouldResume) {
[self start];
}
}];
[self.osdView displayText:@"Screenshot saved"];
}
- (IBAction)copyScreenshot:(id)sender
{
NSImage *image = [self takeScreenshot];
[[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] writeObjects:@[image]];
[self.osdView displayText:@"Screenshot copied"];
}
- (IBAction)toggleDisplayBackground:(id)sender
{
GB_set_background_rendering_disabled(&gb, !GB_is_background_rendering_disabled(&gb));
}
- (IBAction)toggleDisplayObjects:(id)sender
{
GB_set_object_rendering_disabled(&gb, !GB_is_object_rendering_disabled(&gb));
}
@end

View File

@ -25,9 +25,10 @@
<outlet property="memoryBankItem" destination="bWC-FW-IYP" id="Lf2-dh-z32"/>
<outlet property="memoryView" destination="8hr-8o-3rN" id="fF0-rh-8ND"/>
<outlet property="memoryWindow" destination="mRm-dL-mCj" id="VPR-lu-vtI"/>
<outlet property="objectsTableView" destination="TOc-XJ-w9w" id="O4R-4Z-9hU"/>
<outlet property="osdView" destination="MX4-l2-7NE" id="Am7-fq-uvu"/>
<outlet property="paletteTableView" destination="gfC-d3-dmq" id="fTC-eL-Qg3"/>
<outlet property="printerFeedWindow" destination="NdE-0B-WCf" id="yVK-cS-NOJ"/>
<outlet property="spritesTableView" destination="TOc-XJ-w9w" id="O4R-4Z-9hU"/>
<outlet property="tilemapImageView" destination="LlK-tV-bjv" id="nSY-Xd-BjZ"/>
<outlet property="tilemapMapButton" destination="YIJ-Qc-SIZ" id="BB7-Gg-7XP"/>
<outlet property="tilemapPaletteButton" destination="loB-0k-Qff" id="2Or-7l-6vn"/>
@ -65,6 +66,10 @@
</view>
</subviews>
</customView>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="MX4-l2-7NE" customClass="GBOSDView">
<rect key="frame" x="0.0" y="0.0" width="160" height="144"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</customView>
</subviews>
</view>
<connections>
@ -497,7 +502,7 @@
</subviews>
</view>
</tabViewItem>
<tabViewItem label="Sprites" identifier="" id="a08-eg-Maw">
<tabViewItem label="Objects" identifier="" id="a08-eg-Maw">
<view key="view" id="EiO-p0-3xn">
<rect key="frame" x="0.0" y="0.0" width="512" height="408"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
@ -765,7 +770,7 @@
<segments>
<segment label="Tileset" selected="YES"/>
<segment label="Tilemap" tag="1"/>
<segment label="Sprites"/>
<segment label="Objects"/>
<segment label="Palettes"/>
</segments>
</segmentedCell>
@ -978,7 +983,7 @@
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" heightSizable="YES"/>
<clipView key="contentView" id="mzf-yu-RID">
<rect key="frame" x="1" y="0.0" width="398" height="274"/>
<autoresizingMask key="autoresizingMask"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="none" alternatingRowBackgroundColors="YES" columnReordering="NO" columnResizing="NO" multipleSelection="NO" emptySelection="NO" autosaveColumns="NO" typeSelect="NO" headerView="pvX-uJ-qK5" id="tA3-8T-bxb">
<rect key="frame" x="0.0" y="0.0" width="398" height="249"/>
@ -999,7 +1004,7 @@
</buttonCell>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
</tableColumn>
<tableColumn width="50" minWidth="40" maxWidth="1000" id="9DZ-oW-Scx">
<tableColumn width="60" minWidth="40" maxWidth="1000" id="9DZ-oW-Scx">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Enabled">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
@ -1024,7 +1029,7 @@
</textFieldCell>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
</tableColumn>
<tableColumn editable="NO" width="144" minWidth="40" maxWidth="1000" id="ACq-gU-K36">
<tableColumn editable="NO" width="134" minWidth="40" maxWidth="1000" id="ACq-gU-K36">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Action">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>

View File

@ -2,9 +2,9 @@
#import <Core/gb.h>
@interface GBAudioClient : NSObject
@property (strong) void (^renderBlock)(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer);
@property (readonly) UInt32 rate;
@property (readonly, getter=isPlaying) bool playing;
@property (nonatomic, strong) void (^renderBlock)(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer);
@property (nonatomic, readonly) UInt32 rate;
@property (nonatomic, readonly, getter=isPlaying) bool playing;
-(void) start;
-(void) stop;
-(id) initWithRendererBlock:(void (^)(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer)) block

View File

@ -90,7 +90,7 @@ static OSStatus render(
{
OSErr err = AudioOutputUnitStart(audioUnit);
NSAssert1(err == noErr, @"Error starting unit: %hd", err);
_playing = YES;
_playing = true;
}
@ -98,7 +98,7 @@ static OSStatus render(
-(void) stop
{
AudioOutputUnitStop(audioUnit);
_playing = NO;
_playing = false;
}
-(void) dealloc
@ -108,4 +108,4 @@ static OSStatus render(
AudioComponentInstanceDispose(audioUnit);
}
@end
@end

View File

@ -5,12 +5,12 @@
- (void)awakeFromNib
{
self.wantsLayer = YES;
self.wantsLayer = true;
}
- (BOOL)wantsUpdateLayer
{
return YES;
return true;
}
- (void)updateLayer

View File

@ -1,5 +1,5 @@
#import <Cocoa/Cocoa.h>
@interface GBCheatTextFieldCell : NSTextFieldCell
@property bool usesAddressFormat;
@property (nonatomic) bool usesAddressFormat;
@end

View File

@ -114,7 +114,7 @@
return _fieldEditor;
}
_fieldEditor = [[GBCheatTextView alloc] initWithFrame:controlView.frame];
_fieldEditor.fieldEditor = YES;
_fieldEditor.fieldEditor = true;
_fieldEditor.usesAddressFormat = self.usesAddressFormat;
return _fieldEditor;
}

View File

@ -3,15 +3,14 @@
#import "Document.h"
@interface GBCheatWindowController : NSObject <NSTableViewDelegate, NSTableViewDataSource, NSTextFieldDelegate>
@property (weak) IBOutlet NSTableView *cheatsTable;
@property (weak) IBOutlet NSTextField *addressField;
@property (weak) IBOutlet NSTextField *valueField;
@property (weak) IBOutlet NSTextField *oldValueField;
@property (weak) IBOutlet NSButton *oldValueCheckbox;
@property (weak) IBOutlet NSTextField *descriptionField;
@property (weak) IBOutlet NSTextField *importCodeField;
@property (weak) IBOutlet NSTextField *importDescriptionField;
@property (weak) IBOutlet Document *document;
@property (nonatomic, weak) IBOutlet NSTableView *cheatsTable;
@property (nonatomic, weak) IBOutlet NSTextField *addressField;
@property (nonatomic, weak) IBOutlet NSTextField *valueField;
@property (nonatomic, weak) IBOutlet NSTextField *oldValueField;
@property (nonatomic, weak) IBOutlet NSButton *oldValueCheckbox;
@property (nonatomic, weak) IBOutlet NSTextField *descriptionField;
@property (nonatomic, weak) IBOutlet NSTextField *importCodeField;
@property (nonatomic, weak) IBOutlet NSTextField *importDescriptionField;
@property (nonatomic, weak) IBOutlet Document *document;
- (void)cheatsUpdated;
@end

View File

@ -52,7 +52,7 @@
if (row >= cheatCount) {
switch (columnIndex) {
case 0:
return @(YES);
return @YES;
case 1:
return @NO;
@ -67,7 +67,7 @@
switch (columnIndex) {
case 0:
return @(NO);
return @NO;
case 1:
return @(cheats[row]->enabled);

View File

@ -13,13 +13,13 @@ static inline double scale_channel(uint8_t x)
- (void)setObjectValue:(id)objectValue
{
_integerValue = [objectValue integerValue];
uint8_t r = _integerValue & 0x1F,
g = (_integerValue >> 5) & 0x1F,
b = (_integerValue >> 10) & 0x1F;
super.objectValue = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"$%04x", (uint16_t)(_integerValue & 0x7FFF)] attributes:@{
NSForegroundColorAttributeName: r * 3 + g * 4 + b * 2 > 120? [NSColor blackColor] : [NSColor whiteColor]
NSForegroundColorAttributeName: r * 3 + g * 4 + b * 2 > 120? [NSColor blackColor] : [NSColor whiteColor],
NSFontAttributeName: [NSFont userFixedPitchFontOfSize:12]
}];
}
@ -36,13 +36,14 @@ static inline double scale_channel(uint8_t x)
- (NSColor *) backgroundColor
{
/* Todo: color correction */
uint16_t color = self.integerValue;
return [NSColor colorWithRed:scale_channel(color) green:scale_channel(color >> 5) blue:scale_channel(color >> 10) alpha:1.0];
}
- (BOOL)drawsBackground
{
return YES;
return true;
}
@end

9
Cocoa/GBHueSliderCell.h Normal file
View File

@ -0,0 +1,9 @@
#import <Cocoa/Cocoa.h>
@interface NSSlider (GBHueSlider)
-(NSColor *)colorValue;
@end
@interface GBHueSliderCell : NSSliderCell
-(NSColor *)colorValue;
@end

113
Cocoa/GBHueSliderCell.m Normal file
View File

@ -0,0 +1,113 @@
#import "GBHueSliderCell.h"
@interface NSSliderCell(privateAPI)
- (double)_normalizedDoubleValue;
@end
@implementation GBHueSliderCell
{
bool _drawingTrack;
}
-(NSColor *)colorValue
{
double hue = self.doubleValue / 360.0;
double r = 0, g = 0, b =0 ;
double t = fmod(hue * 6, 1);
switch ((int)(hue * 6) % 6) {
case 0:
r = 1;
g = t;
break;
case 1:
r = 1 - t;
g = 1;
break;
case 2:
g = 1;
b = t;
break;
case 3:
g = 1 - t;
b = 1;
break;
case 4:
b = 1;
r = t;
break;
case 5:
b = 1 - t;
r = 1;
break;
}
return [NSColor colorWithRed:r green:g blue:b alpha:1.0];
}
-(void)drawKnob:(NSRect)knobRect
{
[super drawKnob:knobRect];
NSRect peekRect = knobRect;
peekRect.size.width /= 2;
peekRect.size.height = peekRect.size.width;
peekRect.origin.x += peekRect.size.width / 2;
peekRect.origin.y += peekRect.size.height / 2;
NSColor *color = self.colorValue;
if (!self.enabled) {
color = [color colorWithAlphaComponent:0.5];
}
[color setFill];
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:peekRect];
[path fill];
[[NSColor colorWithWhite:0 alpha:0.25] setStroke];
[path setLineWidth:0.5];
[path stroke];
}
-(double)_normalizedDoubleValue
{
if (_drawingTrack) return 0;
return [super _normalizedDoubleValue];
}
-(void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped
{
if (!self.enabled) {
[super drawBarInside:rect flipped:flipped];
return;
}
_drawingTrack = true;
[super drawBarInside:rect flipped:flipped];
_drawingTrack = false;
NSGradient *gradient = [[NSGradient alloc] initWithColors:@[
[NSColor redColor],
[NSColor yellowColor],
[NSColor greenColor],
[NSColor cyanColor],
[NSColor blueColor],
[NSColor magentaColor],
[NSColor redColor],
]];
rect.origin.y += rect.size.height / 2 - 0.5;
rect.size.height = 1;
rect.size.width -= 2;
rect.origin.x += 1;
[[NSColor redColor] set];
NSRectFill(rect);
rect.size.width -= self.knobThickness + 2;
rect.origin.x += self.knobThickness / 2 - 1;
[gradient drawInRect:rect angle:0];
}
@end
@implementation NSSlider (GBHueSlider)
- (NSColor *)colorValue
{
return ((GBHueSliderCell *)self.cell).colorValue;
}
@end

View File

@ -3,17 +3,17 @@
@protocol GBImageViewDelegate;
@interface GBImageViewGridConfiguration : NSObject
@property NSColor *color;
@property NSUInteger size;
@property (nonatomic, strong) NSColor *color;
@property (nonatomic) NSUInteger size;
- (instancetype) initWithColor: (NSColor *) color size: (NSUInteger) size;
@end
@interface GBImageView : NSImageView
@property (nonatomic) NSArray *horizontalGrids;
@property (nonatomic) NSArray *verticalGrids;
@property (nonatomic, strong) NSArray<GBImageViewGridConfiguration *> *horizontalGrids;
@property (nonatomic, strong) NSArray<GBImageViewGridConfiguration *> *verticalGrids;
@property (nonatomic) bool displayScrollRect;
@property NSRect scrollRect;
@property (weak) IBOutlet id<GBImageViewDelegate> delegate;
@property (nonatomic, weak) IBOutlet id<GBImageViewDelegate> delegate;
@end
@protocol GBImageViewDelegate <NSObject>

View File

@ -12,6 +12,6 @@ typedef enum {
@interface GBMemoryByteArray : HFByteArray
- (instancetype) initWithDocument:(Document *)document;
@property uint16_t selectedBank;
@property GB_memory_mode_t mode;
@property (nonatomic) uint16_t selectedBank;
@property (nonatomic) GB_memory_mode_t mode;
@end

6
Cocoa/GBOSDView.h Normal file
View File

@ -0,0 +1,6 @@
#import <Cocoa/Cocoa.h>
@interface GBOSDView : NSView
@property bool usesSGBScale;
- (void)displayText:(NSString *)text;
@end

104
Cocoa/GBOSDView.m Normal file
View File

@ -0,0 +1,104 @@
#import "GBOSDView.h"
@implementation GBOSDView
{
bool _usesSGBScale;
NSString *_text;
double _animation;
NSTimer *_timer;
}
- (void)setUsesSGBScale:(bool)usesSGBScale
{
_usesSGBScale = usesSGBScale;
[self setNeedsDisplay:true];
}
- (bool)usesSGBScale
{
return _usesSGBScale;
}
- (void)displayText:(NSString *)text
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"GBOSDEnabled"]) return;
dispatch_async(dispatch_get_main_queue(), ^{
if (![_text isEqualToString:text]) {
[self setNeedsDisplay:true];
}
_text = text;
self.alphaValue = 1.0;
_animation = 2.5;
// Longer strings should appear longer
if ([_text rangeOfString:@"\n"].location != NSNotFound) {
_animation += 4;
}
[_timer invalidate];
self.hidden = false;
_timer = [NSTimer scheduledTimerWithTimeInterval:0.025 target:self selector:@selector(animate) userInfo:nil repeats:true];
});
}
- (void)animate
{
_animation -= 0.1;
if (_animation < 1.0) {
self.alphaValue = _animation;
};
if (_animation == 0) {
self.hidden = true;
[_timer invalidate];
_text = nil;
}
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
if (!_text.length) return;
double fontSize = 8;
NSSize size = self.frame.size;
if (_usesSGBScale) {
fontSize *= MIN(size.width / 256, size.height / 224);
}
else {
fontSize *= MIN(size.width / 160, size.height / 144);
}
NSFont *font = [NSFont boldSystemFontOfSize:fontSize];
/* The built in stroke attribute uses an inside stroke, which is typographically terrible.
We'll use a naïve manual stroke instead which looks better. */
NSDictionary *attributes = @{
NSFontAttributeName: font,
NSForegroundColorAttributeName: [NSColor blackColor],
};
NSAttributedString *text = [[NSAttributedString alloc] initWithString:_text attributes:attributes];
[text drawAtPoint:NSMakePoint(fontSize + 1, fontSize + 0)];
[text drawAtPoint:NSMakePoint(fontSize - 1, fontSize + 0)];
[text drawAtPoint:NSMakePoint(fontSize + 0, fontSize + 1)];
[text drawAtPoint:NSMakePoint(fontSize + 0, fontSize - 1)];
// The uses of sqrt(2)/2, which is more correct, results in severe ugly-looking rounding errors
if (self.window.screen.backingScaleFactor > 1) {
[text drawAtPoint:NSMakePoint(fontSize + 0.5, fontSize + 0.5)];
[text drawAtPoint:NSMakePoint(fontSize - 0.5, fontSize + 0.5)];
[text drawAtPoint:NSMakePoint(fontSize - 0.5, fontSize - 0.5)];
[text drawAtPoint:NSMakePoint(fontSize + 0.5, fontSize - 0.5)];
}
attributes = @{
NSFontAttributeName: font,
NSForegroundColorAttributeName: [NSColor whiteColor],
};
text = [[NSAttributedString alloc] initWithString:_text attributes:attributes];
[text drawAtPoint:NSMakePoint(fontSize, fontSize)];
}
@end

View File

@ -2,5 +2,5 @@
#import "GBGLShader.h"
@interface GBOpenGLView : NSOpenGLView
@property GBGLShader *shader;
@property (nonatomic) GBGLShader *shader;
@end

View File

@ -34,6 +34,6 @@
- (void) filterChanged
{
self.shader = nil;
[self setNeedsDisplay:YES];
[self setNeedsDisplay:true];
}
@end

View File

@ -0,0 +1,18 @@
#import <AppKit/AppKit.h>
#import <Core/gb.h>
@interface GBPaletteEditorController : NSObject<NSTableViewDataSource, NSTableViewDelegate>
@property (weak) IBOutlet NSColorWell *colorWell0;
@property (weak) IBOutlet NSColorWell *colorWell1;
@property (weak) IBOutlet NSColorWell *colorWell2;
@property (weak) IBOutlet NSColorWell *colorWell3;
@property (weak) IBOutlet NSColorWell *colorWell4;
@property (weak) IBOutlet NSButton *disableLCDColorCheckbox;
@property (weak) IBOutlet NSButton *manualModeCheckbox;
@property (weak) IBOutlet NSSlider *brightnessSlider;
@property (weak) IBOutlet NSSlider *hueSlider;
@property (weak) IBOutlet NSSlider *hueStrengthSlider;
@property (weak) IBOutlet NSTableView *themesList;
@property (weak) IBOutlet NSMenu *menu;
+ (const GB_palette_t *)userPalette;
@end

View File

@ -0,0 +1,378 @@
#import "GBPaletteEditorController.h"
#import "GBHueSliderCell.h"
#import <Core/gb.h>
#define MAGIC 'SBPL'
typedef struct __attribute__ ((packed)) {
uint32_t magic;
bool manual:1;
bool disabled_lcd_color:1;
unsigned padding:6;
struct GB_color_s colors[5];
int32_t brightness_bias;
uint32_t hue_bias;
uint32_t hue_bias_strength;
} theme_t;
static double blend(double from, double to, double position)
{
return from * (1 - position) + to * position;
}
@implementation NSColor (GBColor)
- (struct GB_color_s)gbColor
{
NSColor *sRGB = [self colorUsingColorSpace:[NSColorSpace deviceRGBColorSpace]];
return (struct GB_color_s){round(sRGB.redComponent * 255), round(sRGB.greenComponent * 255), round(sRGB.blueComponent * 255)};
}
- (uint32_t)intValue
{
struct GB_color_s color = self.gbColor;
return (color.r << 0) | (color.g << 8) | (color.b << 16) | 0xFF000000;
}
@end
@implementation GBPaletteEditorController
- (NSArray<NSColorWell *> *)colorWells
{
return @[_colorWell0, _colorWell1, _colorWell2, _colorWell3, _colorWell4];
}
- (void)updateEnabledControls
{
if (self.manualModeCheckbox.state) {
_brightnessSlider.enabled = false;
_hueSlider.enabled = false;
_hueStrengthSlider.enabled = false;
_colorWell1.enabled = true;
_colorWell2.enabled = true;
_colorWell3.enabled = true;
if (!(_colorWell4.enabled = self.disableLCDColorCheckbox.state)) {
_colorWell4.color = _colorWell3.color;
}
}
else {
_colorWell1.enabled = false;
_colorWell2.enabled = false;
_colorWell3.enabled = false;
_colorWell4.enabled = true;
_brightnessSlider.enabled = true;
_hueSlider.enabled = true;
_hueStrengthSlider.enabled = true;
[self updateAutoColors];
}
}
- (NSColor *)autoColorAtPositon:(double)position
{
NSColor *first = [_colorWell0.color colorUsingColorSpace:[NSColorSpace deviceRGBColorSpace]];
NSColor *second = [_colorWell4.color colorUsingColorSpace:[NSColorSpace deviceRGBColorSpace]];
double brightness = 1 / pow(4, (_brightnessSlider.doubleValue - 128) / 128.0);
position = pow(position, brightness);
NSColor *hue = _hueSlider.colorValue;
double bias = _hueStrengthSlider.doubleValue / 256.0;
double red = 1 / pow(4, (hue.redComponent * 2 - 1) * bias);
double green = 1 / pow(4, (hue.greenComponent * 2 - 1) * bias);
double blue = 1 / pow(4, (hue.blueComponent * 2 - 1) * bias);
NSColor *ret = [NSColor colorWithRed:blend(first.redComponent, second.redComponent, pow(position, red))
green:blend(first.greenComponent, second.greenComponent, pow(position, green))
blue:blend(first.blueComponent, second.blueComponent, pow(position, blue))
alpha:1.0];
return ret;
}
- (IBAction)updateAutoColors:(id)sender
{
if (!self.manualModeCheckbox.state) {
[self updateAutoColors];
}
else {
[self savePalette:sender];
}
}
- (void)updateAutoColors
{
if (_disableLCDColorCheckbox.state) {
_colorWell1.color = [self autoColorAtPositon:8 / 25.0];
_colorWell2.color = [self autoColorAtPositon:16 / 25.0];
_colorWell3.color = [self autoColorAtPositon:24 / 25.0];
}
else {
_colorWell1.color = [self autoColorAtPositon:1 / 3.0];
_colorWell2.color = [self autoColorAtPositon:2 / 3.0];
_colorWell3.color = _colorWell4.color;
}
[self savePalette:nil];
}
- (IBAction)disabledLCDColorCheckboxChanged:(id)sender
{
[self updateEnabledControls];
}
- (IBAction)manualModeChanged:(id)sender
{
[self updateEnabledControls];
}
- (IBAction)updateColor4:(id)sender
{
if (!self.disableLCDColorCheckbox.state) {
self.colorWell4.color = self.colorWell3.color;
}
[self savePalette:self];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *themes = [defaults dictionaryForKey:@"GBThemes"];
if (themes.count == 0) {
[defaults setObject:@"Untitled Palette" forKey:@"GBCurrentTheme"];
[self savePalette:nil];
return 1;
}
return themes.count;
}
-(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *oldName = [self tableView:tableView objectValueForTableColumn:tableColumn row:row];
if ([oldName isEqualToString:object]) {
return;
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *themes = [[defaults dictionaryForKey:@"GBThemes"] ?: @{} mutableCopy];
NSString *newName = object;
unsigned i = 2;
if (!newName.length) {
newName = @"Untitled Palette";
}
while (themes[newName]) {
newName = [NSString stringWithFormat:@"%@ %d", object, i];
}
themes[newName] = themes[oldName];
[themes removeObjectForKey:oldName];
if ([oldName isEqualToString:[defaults stringForKey:@"GBCurrentTheme"]]) {
[defaults setObject:newName forKey:@"GBCurrentTheme"];
}
[defaults setObject:themes forKey:@"GBThemes"];
[tableView reloadData];
[self awakeFromNib];
}
- (IBAction)deleteTheme:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *name = [defaults stringForKey:@"GBCurrentTheme"];
NSMutableDictionary *themes = [[defaults dictionaryForKey:@"GBThemes"] ?: @{} mutableCopy];
[themes removeObjectForKey:name];
[defaults setObject:themes forKey:@"GBThemes"];
[_themesList reloadData];
[self awakeFromNib];
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
NSString *name = [self tableView:nil objectValueForTableColumn:nil row:_themesList.selectedRow];
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@"GBCurrentTheme"];
[self loadPalette];
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBColorPaletteChanged" object:nil];
}
- (void)tableViewSelectionIsChanging:(NSNotification *)notification
{
[self tableViewSelectionDidChange:notification];
}
- (void)awakeFromNib
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *themes = [defaults dictionaryForKey:@"GBThemes"];
NSString *theme = [defaults stringForKey:@"GBCurrentTheme"];
if (theme && themes[theme]) {
unsigned index = [[themes.allKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] indexOfObject:theme];
[_themesList selectRowIndexes:[NSIndexSet indexSetWithIndex:index] byExtendingSelection:false];
}
else {
[_themesList selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:false];
}
[self tableViewSelectionDidChange:nil];
}
- (IBAction)addTheme:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *themes = [defaults dictionaryForKey:@"GBThemes"];
NSString *newName = @"Untitled Palette";
unsigned i = 2;
while (themes[newName]) {
newName = [NSString stringWithFormat:@"Untitled Palette %d", i++];
}
[defaults setObject:newName forKey:@"GBCurrentTheme"];
[self savePalette:sender];
[_themesList reloadData];
[self awakeFromNib];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *themes = [defaults dictionaryForKey:@"GBThemes"];
return [themes.allKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)][row];
}
- (void)loadPalette
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *theme = [defaults dictionaryForKey:@"GBThemes"][[defaults stringForKey:@"GBCurrentTheme"]];
NSArray *colors = theme[@"Colors"];
if (colors.count == 5) {
unsigned i = 0;
for (NSNumber *color in colors) {
uint32_t c = [color unsignedIntValue];
self.colorWells[i++].color = [NSColor colorWithRed:(c & 0xFF) / 255.0
green:((c >> 8) & 0xFF) / 255.0
blue:((c >> 16) & 0xFF) / 255.0
alpha:1.0];
}
}
_disableLCDColorCheckbox.state = [theme[@"DisabledLCDColor"] boolValue];
_manualModeCheckbox.state = [theme[@"Manual"] boolValue];
_brightnessSlider.doubleValue = [theme[@"BrightnessBias"] doubleValue] * 128 + 128;
_hueSlider.doubleValue = [theme[@"HueBias"] doubleValue] * 360;
_hueStrengthSlider.doubleValue = [theme[@"HueBiasStrength"] doubleValue] * 256;
[self updateEnabledControls];
}
- (IBAction)savePalette:(id)sender
{
NSDictionary *theme = @{
@"Colors":
@[@(_colorWell0.color.intValue),
@(_colorWell1.color.intValue),
@(_colorWell2.color.intValue),
@(_colorWell3.color.intValue),
@(_colorWell4.color.intValue)],
@"DisabledLCDColor": _disableLCDColorCheckbox.state? @YES : @NO,
@"Manual": _manualModeCheckbox.state? @YES : @NO,
@"BrightnessBias": @((_brightnessSlider.doubleValue - 128) / 128.0),
@"HueBias": @(_hueSlider.doubleValue / 360.0),
@"HueBiasStrength": @(_hueStrengthSlider.doubleValue / 256.0)
};
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *themes = [[defaults dictionaryForKey:@"GBThemes"] ?: @{} mutableCopy];
themes[[defaults stringForKey:@"GBCurrentTheme"]] = theme;
[defaults setObject:themes forKey:@"GBThemes"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBColorPaletteChanged" object:nil];
}
+ (const GB_palette_t *)userPalette
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
switch ([defaults integerForKey:@"GBColorPalette"]) {
case 1: return &GB_PALETTE_DMG;
case 2: return &GB_PALETTE_MGB;
case 3: return &GB_PALETTE_GBL;
default: return &GB_PALETTE_GREY;
case -1: {
static GB_palette_t customPalette;
NSArray *colors = [defaults dictionaryForKey:@"GBThemes"][[defaults stringForKey:@"GBCurrentTheme"]][@"Colors"];
if (colors.count == 5) {
unsigned i = 0;
for (NSNumber *color in colors) {
uint32_t c = [color unsignedIntValue];
customPalette.colors[i++] = (struct GB_color_s) {c, c >> 8, c >> 16};
}
}
return &customPalette;
}
}
}
- (IBAction)export:(id)sender
{
NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setAllowedFileTypes:@[@"sbp"]];
savePanel.nameFieldStringValue = [NSString stringWithFormat:@"%@.sbp", [[NSUserDefaults standardUserDefaults] stringForKey:@"GBCurrentTheme"]];
if ([savePanel runModal] == NSModalResponseOK) {
theme_t theme = {0,};
theme.magic = MAGIC;
theme.manual = _manualModeCheckbox.state;
theme.disabled_lcd_color = _disableLCDColorCheckbox.state;
unsigned i = 0;
for (NSColorWell *well in self.colorWells) {
theme.colors[i++] = well.color.gbColor;
}
theme.brightness_bias = (_brightnessSlider.doubleValue - 128) * (0x40000000 / 128);
theme.hue_bias = round(_hueSlider.doubleValue * (0x80000000 / 360.0));
theme.hue_bias_strength = (_hueStrengthSlider.doubleValue) * (0x80000000 / 256);
size_t size = sizeof(theme);
if (theme.manual) {
size = theme.disabled_lcd_color? 5 + 5 * sizeof(theme.colors[0]) : 5 + 4 * sizeof(theme.colors[0]);
}
[[NSData dataWithBytes:&theme length:size] writeToURL:savePanel.URL atomically:false];
}
}
- (IBAction)import:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowedFileTypes:@[@"sbp"]];
if ([openPanel runModal] == NSModalResponseOK) {
NSData *data = [NSData dataWithContentsOfURL:openPanel.URL];
theme_t theme = {0,};
memcpy(&theme, data.bytes, MIN(sizeof(theme), data.length));
if (theme.magic != MAGIC) {
NSBeep();
return;
}
_manualModeCheckbox.state = theme.manual;
_disableLCDColorCheckbox.state = theme.disabled_lcd_color;
unsigned i = 0;
for (NSColorWell *well in self.colorWells) {
well.color = [NSColor colorWithRed:theme.colors[i].r / 255.0
green:theme.colors[i].g / 255.0
blue:theme.colors[i].b / 255.0
alpha:1.0];
i++;
}
if (!theme.disabled_lcd_color) {
_colorWell4.color = _colorWell3.color;
}
_brightnessSlider.doubleValue = theme.brightness_bias / (0x40000000 / 128.0) + 128;
_hueSlider.doubleValue = theme.hue_bias / (0x80000000 / 360.0);
_hueStrengthSlider.doubleValue = theme.hue_bias_strength / (0x80000000 / 256.0);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *themes = [defaults dictionaryForKey:@"GBThemes"];
NSString *baseName = openPanel.URL.lastPathComponent.stringByDeletingPathExtension;
NSString *newName = baseName;
i = 2;
while (themes[newName]) {
newName = [NSString stringWithFormat:@"%@ %d", baseName, i++];
}
[defaults setObject:newName forKey:@"GBCurrentTheme"];
[self savePalette:sender];
[self awakeFromNib];
}
}
- (IBAction)done:(NSButton *)sender
{
[sender.window.sheetParent endSheet:sender.window];
}
- (instancetype)init
{
static id singleton = nil;
if (singleton) return singleton;
return (singleton = [super init]);
}
@end

View File

@ -1,28 +1,37 @@
#import <Cocoa/Cocoa.h>
#import <JoyKit/JoyKit.h>
#import "GBPaletteEditorController.h"
@interface GBPreferencesWindow : NSWindow <NSTableViewDelegate, NSTableViewDataSource, JOYListener>
@property IBOutlet NSTableView *controlsTableView;
@property IBOutlet NSPopUpButton *graphicsFilterPopupButton;
@property (strong) IBOutlet NSButton *analogControlsCheckbox;
@property (strong) IBOutlet NSButton *aspectRatioCheckbox;
@property (strong) IBOutlet NSPopUpButton *highpassFilterPopupButton;
@property (strong) IBOutlet NSPopUpButton *colorCorrectionPopupButton;
@property (strong) IBOutlet NSPopUpButton *frameBlendingModePopupButton;
@property (strong) IBOutlet NSPopUpButton *colorPalettePopupButton;
@property (strong) IBOutlet NSPopUpButton *displayBorderPopupButton;
@property (strong) IBOutlet NSPopUpButton *rewindPopupButton;
@property (strong) IBOutlet NSButton *configureJoypadButton;
@property (strong) IBOutlet NSButton *skipButton;
@property (strong) IBOutlet NSMenuItem *bootROMsFolderItem;
@property (strong) IBOutlet NSPopUpButtonCell *bootROMsButton;
@property (strong) IBOutlet NSPopUpButton *rumbleModePopupButton;
@property (weak) IBOutlet NSSlider *temperatureSlider;
@property (weak) IBOutlet NSSlider *interferenceSlider;
@property (weak) IBOutlet NSPopUpButton *dmgPopupButton;
@property (weak) IBOutlet NSPopUpButton *sgbPopupButton;
@property (weak) IBOutlet NSPopUpButton *cgbPopupButton;
@property (weak) IBOutlet NSPopUpButton *preferredJoypadButton;
@property (weak) IBOutlet NSPopUpButton *playerListButton;
@property (nonatomic, strong) IBOutlet NSTableView *controlsTableView;
@property (nonatomic, strong) IBOutlet NSPopUpButton *graphicsFilterPopupButton;
@property (nonatomic, strong) IBOutlet NSButton *analogControlsCheckbox;
@property (nonatomic, strong) IBOutlet NSButton *aspectRatioCheckbox;
@property (nonatomic, strong) IBOutlet NSPopUpButton *highpassFilterPopupButton;
@property (nonatomic, strong) IBOutlet NSPopUpButton *colorCorrectionPopupButton;
@property (nonatomic, strong) IBOutlet NSPopUpButton *frameBlendingModePopupButton;
@property (nonatomic, strong) IBOutlet NSPopUpButton *colorPalettePopupButton;
@property (nonatomic, strong) IBOutlet NSPopUpButton *displayBorderPopupButton;
@property (nonatomic, strong) IBOutlet NSPopUpButton *rewindPopupButton;
@property (nonatomic, strong) IBOutlet NSPopUpButton *rtcPopupButton;
@property (nonatomic, strong) IBOutlet NSButton *configureJoypadButton;
@property (nonatomic, strong) IBOutlet NSButton *skipButton;
@property (nonatomic, strong) IBOutlet NSMenuItem *bootROMsFolderItem;
@property (nonatomic, strong) IBOutlet NSPopUpButtonCell *bootROMsButton;
@property (nonatomic, strong) IBOutlet NSPopUpButton *rumbleModePopupButton;
@property (nonatomic, weak) IBOutlet NSSlider *temperatureSlider;
@property (nonatomic, weak) IBOutlet NSSlider *interferenceSlider;
@property (nonatomic, weak) IBOutlet NSPopUpButton *dmgPopupButton;
@property (nonatomic, weak) IBOutlet NSPopUpButton *sgbPopupButton;
@property (nonatomic, weak) IBOutlet NSPopUpButton *cgbPopupButton;
@property (nonatomic, weak) IBOutlet NSPopUpButton *preferredJoypadButton;
@property (nonatomic, weak) IBOutlet NSPopUpButton *playerListButton;
@property (nonatomic, weak) IBOutlet NSButton *autoUpdatesCheckbox;
@property (weak) IBOutlet NSSlider *volumeSlider;
@property (weak) IBOutlet NSButton *OSDCheckbox;
@property (weak) IBOutlet NSButton *screenshotFilterCheckbox;
@property (weak) IBOutlet GBPaletteEditorController *paletteEditorController;
@property (strong) IBOutlet NSWindow *paletteEditor;
@property (weak) IBOutlet NSButton *joystickMBC7Checkbox;
@property (weak) IBOutlet NSButton *mouseMBC7Checkbox;
@end

View File

@ -2,6 +2,7 @@
#import "NSString+StringForKey.h"
#import "GBButtons.h"
#import "BigSurToolbar.h"
#import "GBViewMetal.h"
#import <Carbon/Carbon.h>
@implementation GBPreferencesWindow
@ -19,6 +20,7 @@
NSPopUpButton *_colorPalettePopupButton;
NSPopUpButton *_displayBorderPopupButton;
NSPopUpButton *_rewindPopupButton;
NSPopUpButton *_rtcPopupButton;
NSButton *_aspectRatioCheckbox;
NSButton *_analogControlsCheckbox;
NSEventModifierFlags previousModifiers;
@ -28,6 +30,12 @@
NSPopUpButton *_rumbleModePopupButton;
NSSlider *_temperatureSlider;
NSSlider *_interferenceSlider;
NSSlider *_volumeSlider;
NSButton *_autoUpdatesCheckbox;
NSButton *_OSDCheckbox;
NSButton *_screenshotFilterCheckbox;
NSButton *_joystickMBC7Checkbox;
NSButton *_mouseMBC7Checkbox;
}
+ (NSArray *)filterList
@ -63,8 +71,8 @@
- (void)close
{
joystick_configuration_state = -1;
[self.configureJoypadButton setEnabled:YES];
[self.skipButton setEnabled:NO];
[self.configureJoypadButton setEnabled:true];
[self.skipButton setEnabled:false];
[self.configureJoypadButton setTitle:@"Configure Controller"];
[super close];
}
@ -121,6 +129,17 @@
return _interferenceSlider;
}
- (void)setVolumeSlider:(NSSlider *)volumeSlider
{
_volumeSlider = volumeSlider;
[volumeSlider setDoubleValue:[[NSUserDefaults standardUserDefaults] doubleForKey:@"GBVolume"] * 256];
}
- (NSSlider *)volumeSlider
{
return _volumeSlider;
}
- (void)setFrameBlendingModePopupButton:(NSPopUpButton *)frameBlendingModePopupButton
{
_frameBlendingModePopupButton = frameBlendingModePopupButton;
@ -136,8 +155,14 @@
- (void)setColorPalettePopupButton:(NSPopUpButton *)colorPalettePopupButton
{
_colorPalettePopupButton = colorPalettePopupButton;
[self updatePalettesMenu];
NSInteger mode = [[NSUserDefaults standardUserDefaults] integerForKey:@"GBColorPalette"];
[_colorPalettePopupButton selectItemAtIndex:mode];
if (mode >= 0) {
[_colorPalettePopupButton selectItemWithTag:mode];
}
else {
[_colorPalettePopupButton selectItemWithTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"GBCurrentTheme"] ?: @""];
}
}
- (NSPopUpButton *)colorPalettePopupButton
@ -181,6 +206,18 @@
return _rewindPopupButton;
}
- (NSPopUpButton *)rtcPopupButton
{
return _rtcPopupButton;
}
- (void)setRtcPopupButton:(NSPopUpButton *)rtcPopupButton
{
_rtcPopupButton = rtcPopupButton;
NSInteger mode = [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRTCMode"];
[_rtcPopupButton selectItemAtIndex:mode];
}
- (void)setHighpassFilterPopupButton:(NSPopUpButton *)highpassFilterPopupButton
{
_highpassFilterPopupButton = highpassFilterPopupButton;
@ -239,12 +276,12 @@
dispatch_async(dispatch_get_main_queue(), ^{
is_button_being_modified = true;
button_being_modified = row;
tableView.enabled = NO;
self.playerListButton.enabled = NO;
tableView.enabled = false;
self.playerListButton.enabled = false;
[tableView reloadData];
[self makeFirstResponder:self];
});
return NO;
return false;
}
-(void)keyDown:(NSEvent *)theEvent
@ -260,8 +297,8 @@
[[NSUserDefaults standardUserDefaults] setInteger:theEvent.keyCode
forKey:button_to_preference_name(button_being_modified, self.playerListButton.selectedTag)];
self.controlsTableView.enabled = YES;
self.playerListButton.enabled = YES;
self.controlsTableView.enabled = true;
self.playerListButton.enabled = true;
[self.controlsTableView reloadData];
[self makeFirstResponder:self.controlsTableView];
}
@ -289,6 +326,19 @@
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBHighpassFilterChanged" object:nil];
}
- (IBAction)changeMBC7JoystickOverride:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool: [(NSButton *)sender state] == NSOnState
forKey:@"GBMBC7JoystickOverride"];
}
- (IBAction)changeMBC7AllowMouse:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool: [(NSButton *)sender state] == NSOnState
forKey:@"GBMBC7AllowMouse"];
}
- (IBAction)changeAnalogControls:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool: [(NSButton *)sender state] == NSOnState
@ -316,12 +366,18 @@
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBLightTemperatureChanged" object:nil];
}
- (IBAction)volumeTemperatureChanged:(id)sender
- (IBAction)interferenceVolumeChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:@([sender doubleValue] / 256.0)
forKey:@"GBInterferenceVolume"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBInterferenceVolumeChanged" object:nil];
}
- (IBAction)volumeChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:@([sender doubleValue] / 256.0)
forKey:@"GBVolume"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBVolumeChanged" object:nil];
}
- (IBAction)franeBlendingModeChanged:(id)sender
@ -332,10 +388,51 @@
}
- (void)updatePalettesMenu
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *themes = [defaults dictionaryForKey:@"GBThemes"];
NSMenu *menu = _colorPalettePopupButton.menu;
while (menu.itemArray.count != 4) {
[menu removeItemAtIndex:4];
}
[menu addItem:[NSMenuItem separatorItem]];
for (NSString *name in [themes.allKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]) {
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:name action:nil keyEquivalent:@""];
item.tag = -2;
[menu addItem:item];
}
if (themes) {
[menu addItem:[NSMenuItem separatorItem]];
}
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Custom…" action:nil keyEquivalent:@""];
item.tag = -1;
[menu addItem:item];
}
- (IBAction)colorPaletteChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:@([sender indexOfSelectedItem])
forKey:@"GBColorPalette"];
signed tag = [sender selectedItem].tag;
if (tag == -2) {
[[NSUserDefaults standardUserDefaults] setObject:@(-1)
forKey:@"GBColorPalette"];
[[NSUserDefaults standardUserDefaults] setObject:[sender selectedItem].title
forKey:@"GBCurrentTheme"];
}
else if (tag == -1) {
[[NSUserDefaults standardUserDefaults] setObject:@(-1)
forKey:@"GBColorPalette"];
[_paletteEditorController awakeFromNib];
[self beginSheet:_paletteEditor completionHandler:^(NSModalResponse returnCode) {
[self updatePalettesMenu];
[_colorPalettePopupButton selectItemWithTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"GBCurrentTheme"] ?: @""];
}];
}
else {
[[NSUserDefaults standardUserDefaults] setObject:@([sender selectedItem].tag)
forKey:@"GBColorPalette"];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBColorPaletteChanged" object:nil];
}
@ -360,10 +457,24 @@
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBRewindLengthChanged" object:nil];
}
- (IBAction)rtcModeChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:@([sender indexOfSelectedItem])
forKey:@"GBRTCMode"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"GBRTCModeChanged" object:nil];
}
- (IBAction)changeAutoUpdates:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool: [(NSButton *)sender state] == NSOnState
forKey:@"GBAutoUpdatesEnabled"];
}
- (IBAction) configureJoypad:(id)sender
{
[self.configureJoypadButton setEnabled:NO];
[self.skipButton setEnabled:YES];
[self.configureJoypadButton setEnabled:false];
[self.skipButton setEnabled:true];
joystick_being_configured = nil;
[self advanceConfigurationStateMachine];
}
@ -384,8 +495,8 @@
}
else {
joystick_configuration_state = -1;
[self.configureJoypadButton setEnabled:YES];
[self.skipButton setEnabled:NO];
[self.configureJoypadButton setEnabled:true];
[self.skipButton setEnabled:false];
[self.configureJoypadButton setTitle:@"Configure Joypad"];
}
}
@ -432,30 +543,36 @@
static const unsigned gb_to_joykit[] = {
[GBRight]=JOYButtonUsageDPadRight,
[GBLeft]=JOYButtonUsageDPadLeft,
[GBUp]=JOYButtonUsageDPadUp,
[GBDown]=JOYButtonUsageDPadDown,
[GBA]=JOYButtonUsageA,
[GBB]=JOYButtonUsageB,
[GBSelect]=JOYButtonUsageSelect,
[GBStart]=JOYButtonUsageStart,
[GBTurbo]=JOYButtonUsageL1,
[GBRewind]=JOYButtonUsageL2,
[GBUnderclock]=JOYButtonUsageR1,
[GBRight] = JOYButtonUsageDPadRight,
[GBLeft] = JOYButtonUsageDPadLeft,
[GBUp] = JOYButtonUsageDPadUp,
[GBDown] = JOYButtonUsageDPadDown,
[GBA] = JOYButtonUsageA,
[GBB] = JOYButtonUsageB,
[GBSelect] = JOYButtonUsageSelect,
[GBStart] = JOYButtonUsageStart,
[GBTurbo] = JOYButtonUsageL1,
[GBRewind] = JOYButtonUsageL2,
[GBUnderclock] = JOYButtonUsageR1,
};
if (joystick_configuration_state == GBUnderclock) {
mapping[@"AnalogUnderclock"] = nil;
double max = 0;
for (JOYAxis *axis in controller.axes) {
if (axis.value > 0.5) {
if ((axis.value > 0.5 || (axis.equivalentButtonUsage == button.usage)) && axis.value >= max) {
mapping[@"AnalogUnderclock"] = @(axis.uniqueID);
break;
}
}
}
if (joystick_configuration_state == GBTurbo) {
mapping[@"AnalogTurbo"] = nil;
double max = 0;
for (JOYAxis *axis in controller.axes) {
if (axis.value > 0.5) {
if ((axis.value > 0.5 || (axis.equivalentButtonUsage == button.usage)) && axis.value >= max) {
max = axis.value;
mapping[@"AnalogTurbo"] = @(axis.uniqueID);
}
}
@ -470,6 +587,28 @@
[self advanceConfigurationStateMachine];
}
- (NSButton *)joystickMBC7Checkbox
{
return _joystickMBC7Checkbox;
}
- (void)setJoystickMBC7Checkbox:(NSButton *)joystickMBC7Checkbox
{
_joystickMBC7Checkbox = joystickMBC7Checkbox;
[_joystickMBC7Checkbox setState: [[NSUserDefaults standardUserDefaults] boolForKey:@"GBMBC7JoystickOverride"]];
}
- (NSButton *)mouseMBC7Checkbox
{
return _mouseMBC7Checkbox;
}
- (void)setMouseMBC7Checkbox:(NSButton *)mouseMBC7Checkbox
{
_mouseMBC7Checkbox = mouseMBC7Checkbox;
[_mouseMBC7Checkbox setState: [[NSUserDefaults standardUserDefaults] boolForKey:@"GBMBC7AllowMouse"]];
}
- (NSButton *)analogControlsCheckbox
{
return _analogControlsCheckbox;
@ -510,8 +649,8 @@
- (IBAction)selectOtherBootROMFolder:(id)sender
{
NSOpenPanel *panel = [[NSOpenPanel alloc] init];
[panel setCanChooseDirectories:YES];
[panel setCanChooseFiles:NO];
[panel setCanChooseDirectories:true];
[panel setCanChooseFiles:false];
[panel setPrompt:@"Select"];
[panel setDirectoryURL:[[NSUserDefaults standardUserDefaults] URLForKey:@"GBBootROMsFolder"]];
[panel beginSheetModalForWindow:self completionHandler:^(NSModalResponse result) {
@ -535,12 +674,12 @@
[self.bootROMsFolderItem setTitle:[url lastPathComponent]];
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:[url path]];
[icon setSize:NSMakeSize(16, 16)];
[self.bootROMsFolderItem setHidden:NO];
[self.bootROMsFolderItem setHidden:false];
[self.bootROMsFolderItem setImage:icon];
[self.bootROMsButton selectItemAtIndex:1];
}
else {
[self.bootROMsFolderItem setHidden:YES];
[self.bootROMsFolderItem setHidden:true];
[self.bootROMsButton selectItemAtIndex:0];
}
}
@ -684,4 +823,56 @@
}
[[NSUserDefaults standardUserDefaults] setObject:default_joypads forKey:@"JoyKitDefaultControllers"];
}
- (NSButton *)autoUpdatesCheckbox
{
return _autoUpdatesCheckbox;
}
- (void)setAutoUpdatesCheckbox:(NSButton *)autoUpdatesCheckbox
{
_autoUpdatesCheckbox = autoUpdatesCheckbox;
[_autoUpdatesCheckbox setState: [[NSUserDefaults standardUserDefaults] boolForKey:@"GBAutoUpdatesEnabled"]];
}
- (NSButton *)OSDCheckbox
{
return _OSDCheckbox;
}
- (void)setOSDCheckbox:(NSButton *)OSDCheckbox
{
_OSDCheckbox = OSDCheckbox;
[_OSDCheckbox setState: [[NSUserDefaults standardUserDefaults] boolForKey:@"GBOSDEnabled"]];
}
- (IBAction)changeOSDEnabled:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[(NSButton *)sender state] == NSOnState
forKey:@"GBOSDEnabled"];
}
- (IBAction)changeFilterScreenshots:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[(NSButton *)sender state] == NSOnState
forKey:@"GBFilterScreenshots"];
}
- (NSButton *)screenshotFilterCheckbox
{
return _screenshotFilterCheckbox;
}
- (void)setScreenshotFilterCheckbox:(NSButton *)screenshotFilterCheckbox
{
_screenshotFilterCheckbox = screenshotFilterCheckbox;
if (![GBViewMetal isSupported]) {
[_screenshotFilterCheckbox setEnabled:false];
}
else {
[_screenshotFilterCheckbox setState: [[NSUserDefaults standardUserDefaults] boolForKey:@"GBFilterScreenshots"]];
}
}
@end

128
Cocoa/GBS.xib Normal file
View File

@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14868" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14868"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="Document">
<connections>
<outlet property="gbsAuthor" destination="gaD-ZH-Beh" id="2i7-BD-bJ2"/>
<outlet property="gbsCopyright" destination="2dl-dH-E3J" id="LnT-Vb-pN6"/>
<outlet property="gbsNextPrevButton" destination="SRS-M5-VVL" id="YEN-01-wRX"/>
<outlet property="gbsPlayPauseButton" destination="qxJ-pH-d0y" id="qk8-8I-9u5"/>
<outlet property="gbsPlayerView" destination="c22-O7-iKe" id="A1w-e5-EQE"/>
<outlet property="gbsRewindButton" destination="0yD-Sp-Ilo" id="FgR-xd-JW5"/>
<outlet property="gbsTitle" destination="H3v-X3-48q" id="DCl-wL-oy8"/>
<outlet property="gbsTracks" destination="I1T-VS-Vse" id="Vk4-GP-RjB"/>
<outlet property="gbsVisualizer" destination="Q3o-bK-DIN" id="1YC-C5-Je6"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="c22-O7-iKe">
<rect key="frame" x="0.0" y="0.0" width="332" height="221"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" id="H3v-X3-48q">
<rect key="frame" x="18" y="192" width="296" height="19"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="center" title="Title" id="BwZ-Zj-sP6">
<font key="font" metaFont="systemBold" size="16"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" id="gaD-ZH-Beh">
<rect key="frame" x="18" y="166" width="296" height="16"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="center" title="Author" id="IgT-r1-T38">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<button focusRingType="none" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="qxJ-pH-d0y">
<rect key="frame" x="61.5" y="127" width="39" height="23"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" image="Play" imagePosition="only" alignment="center" alternateImage="Pause" state="on" borderStyle="border" focusRingType="none" inset="2" id="3ZK-br-UrS">
<behavior key="behavior" pushIn="YES" changeContents="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="togglePause:" target="-2" id="AUe-I7-nOK"/>
</connections>
</button>
<button focusRingType="none" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="0yD-Sp-Ilo">
<rect key="frame" x="19.5" y="127" width="38" height="23"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" image="Rewind" imagePosition="only" alignment="center" state="on" borderStyle="border" focusRingType="none" inset="2" id="ZIF-TP-Fqn">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="changeGBSTrack:" target="-2" id="jug-AS-bW7"/>
</connections>
</button>
<popUpButton focusRingType="none" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="I1T-VS-Vse">
<rect key="frame" x="106" y="127" width="131" height="23"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" alignment="left" lineBreakMode="truncatingTail" borderStyle="border" focusRingType="none" imageScaling="proportionallyDown" inset="2" id="YJh-dI-A5D">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
<menu key="menu" id="Knp-Ok-Pb4"/>
</popUpButtonCell>
<connections>
<action selector="changeGBSTrack:" target="-2" id="HET-AT-CfQ"/>
</connections>
</popUpButton>
<segmentedControl verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="SRS-M5-VVL">
<rect key="frame" x="240.5" y="127" width="72" height="23"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<segmentedCell key="cell" borderStyle="border" alignment="left" style="texturedRounded" trackingMode="momentary" id="cmq-I8-cFL">
<font key="font" metaFont="system"/>
<segments>
<segment toolTip="Previous Track" image="Previous" width="33"/>
<segment toolTip="Next Track" image="Next" width="32" tag="1"/>
</segments>
</segmentedCell>
<connections>
<action selector="gbsNextPrevPushed:" target="-2" id="roN-Iy-tDQ"/>
</connections>
</segmentedControl>
<box verticalHuggingPriority="750" fixedFrame="YES" boxType="separator" translatesAutoresizingMaskIntoConstraints="NO" id="b9A-cd-ias">
<rect key="frame" x="0.0" y="117" width="332" height="5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</box>
<customView appearanceType="darkAqua" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="tRy-Gw-IaG" customClass="GBOptionalVisualEffectView">
<rect key="frame" x="0.0" y="24" width="332" height="95"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Q3o-bK-DIN" customClass="GBVisualizerView">
<rect key="frame" x="0.0" y="0.0" width="332" height="95"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</customView>
</subviews>
</customView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" id="2dl-dH-E3J">
<rect key="frame" x="18" y="5" width="296" height="14"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<textFieldCell key="cell" controlSize="small" lineBreakMode="clipping" alignment="center" title="Copyright" id="nM9-oF-OV9">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<point key="canvasLocation" x="67" y="292.5"/>
</customView>
</objects>
<resources>
<image name="Next" width="16" height="10"/>
<image name="Pause" width="10" height="10"/>
<image name="Play" width="10" height="10"/>
<image name="Previous" width="16" height="10"/>
<image name="Rewind" width="10" height="10"/>
</resources>
</document>

View File

@ -8,7 +8,7 @@
- (void)setDividerColor:(NSColor *)color
{
_dividerColor = color;
[self setNeedsDisplay:YES];
[self setNeedsDisplay:true];
}
- (NSColor *)dividerColor

View File

@ -2,5 +2,5 @@
#include <Core/gb.h>
@interface GBTerminalTextFieldCell : NSTextFieldCell
@property GB_gameboy_t *gb;
@property (nonatomic) GB_gameboy_t *gb;
@end

View File

@ -17,7 +17,7 @@
return field_editor;
}
field_editor = [[GBTerminalTextView alloc] init];
[field_editor setFieldEditor:YES];
[field_editor setFieldEditor:true];
field_editor.gb = self.gb;
return field_editor;
}
@ -109,7 +109,7 @@
[self updateReverseSearch];
}
else {
[self setNeedsDisplay:YES];
[self setNeedsDisplay:true];
reverse_search_mode = true;
}

View File

@ -1,6 +1,7 @@
#import <Cocoa/Cocoa.h>
#include <Core/gb.h>
#import <JoyKit/JoyKit.h>
#import "GBOSDView.h"
@class Document;
typedef enum {
@ -14,15 +15,17 @@ typedef enum {
@interface GBView : NSView<JOYListener>
- (void) flip;
- (uint32_t *) pixels;
@property (weak) IBOutlet Document *document;
@property GB_gameboy_t *gb;
@property (nonatomic, weak) IBOutlet Document *document;
@property (nonatomic) GB_gameboy_t *gb;
@property (nonatomic) GB_frame_blending_mode_t frameBlendingMode;
@property (getter=isMouseHidingEnabled) BOOL mouseHidingEnabled;
@property bool isRewinding;
@property NSView *internalView;
@property (nonatomic, getter=isMouseHidingEnabled) bool mouseHidingEnabled;
@property (nonatomic) bool isRewinding;
@property (nonatomic, strong) NSView *internalView;
@property (weak) GBOSDView *osdView;
- (void) createInternalView;
- (uint32_t *)currentBuffer;
- (uint32_t *)previousBuffer;
- (void)screenSizeChanged;
- (void)setRumble: (double)amp;
- (NSImage *)renderToImage;
@end

View File

@ -106,9 +106,9 @@ static const uint8_t workboy_vk_to_key[] = {
{
uint32_t *image_buffers[3];
unsigned char current_buffer;
BOOL mouse_hidden;
bool mouse_hidden;
NSTrackingArea *tracking_area;
BOOL _mouseHidingEnabled;
bool _mouseHidingEnabled;
bool axisActive[2];
bool underclockKeyDown;
double clockMultiplier;
@ -117,6 +117,8 @@ static const uint8_t workboy_vk_to_key[] = {
NSEventModifierFlags previousModifiers;
JOYController *lastController;
GB_frame_blending_mode_t _frameBlendingMode;
bool _turbo;
bool _mouseControlEnabled;
}
+ (instancetype)alloc
@ -142,9 +144,11 @@ static const uint8_t workboy_vk_to_key[] = {
- (void) _init
{
[self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil];
tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){}
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseMoved
owner:self
userInfo:nil];
[self addTrackingArea:tracking_area];
@ -153,6 +157,7 @@ static const uint8_t workboy_vk_to_key[] = {
[self addSubview:self.internalView];
self.internalView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[JOYController registerListener:self];
_mouseControlEnabled = true;
}
- (void)screenSizeChanged
@ -180,7 +185,7 @@ static const uint8_t workboy_vk_to_key[] = {
- (void) setFrameBlendingMode:(GB_frame_blending_mode_t)frameBlendingMode
{
_frameBlendingMode = frameBlendingMode;
[self setNeedsDisplay:YES];
[self setNeedsDisplay:true];
}
@ -257,6 +262,7 @@ static const uint8_t workboy_vk_to_key[] = {
- (void) flip
{
if (analogClockMultiplierValid && [[NSUserDefaults standardUserDefaults] boolForKey:@"GBAnalogControls"]) {
clockMultiplier = 1.0;
GB_set_clock_multiplier(_gb, analogClockMultiplier);
if (self.document.partner) {
GB_set_clock_multiplier(self.document.partner.gb, analogClockMultiplier);
@ -264,6 +270,12 @@ static const uint8_t workboy_vk_to_key[] = {
if (analogClockMultiplier == 1.0) {
analogClockMultiplierValid = false;
}
if (analogClockMultiplier < 2.0 && analogClockMultiplier > 1.0) {
GB_set_turbo_mode(_gb, false, false);
if (self.document.partner) {
GB_set_turbo_mode(self.document.partner.gb, false, false);
}
}
}
else {
if (underclockKeyDown && clockMultiplier > 0.5) {
@ -281,6 +293,14 @@ static const uint8_t workboy_vk_to_key[] = {
}
}
}
if ((!analogClockMultiplierValid && clockMultiplier > 1) ||
_turbo || (analogClockMultiplierValid && analogClockMultiplier > 1)) {
[self.osdView displayText:@"Fast forwarding..."];
}
else if ((!analogClockMultiplierValid && clockMultiplier < 1) ||
(analogClockMultiplierValid && analogClockMultiplier < 1)) {
[self.osdView displayText:@"Slow motion..."];
}
current_buffer = (current_buffer + 1) % self.numberOfBuffers;
}
@ -327,6 +347,7 @@ static const uint8_t workboy_vk_to_key[] = {
else {
GB_set_turbo_mode(_gb, true, self.isRewinding);
}
_turbo = true;
analogClockMultiplierValid = false;
break;
@ -334,6 +355,7 @@ static const uint8_t workboy_vk_to_key[] = {
if (!self.document.partner) {
self.isRewinding = true;
GB_set_turbo_mode(_gb, false, false);
_turbo = false;
}
break;
@ -399,6 +421,7 @@ static const uint8_t workboy_vk_to_key[] = {
else {
GB_set_turbo_mode(_gb, false, false);
}
_turbo = false;
analogClockMultiplierValid = false;
break;
@ -438,8 +461,22 @@ static const uint8_t workboy_vk_to_key[] = {
[lastController setRumbleAmplitude:amp];
}
- (bool)shouldControllerUseJoystickForMotion:(JOYController *)controller
{
if (!_gb) return false;
if (!GB_has_accelerometer(_gb)) return false;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBMBC7JoystickOverride"]) return true;
for (JOYAxes3D *axes in controller.axes3D) {
if (axes.usage == JOYAxes3DUsageOrientation || axes.usage == JOYAxes3DUsageAcceleration) {
return false;
}
}
return true;
}
- (void)controller:(JOYController *)controller movedAxis:(JOYAxis *)axis
{
if (!_gb) return;
if (![self.window isMainWindow]) return;
NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"][controller.uniqueID];
@ -449,20 +486,55 @@ static const uint8_t workboy_vk_to_key[] = {
if ((axis.usage == JOYAxisUsageR1 && !mapping) ||
axis.uniqueID == [mapping[@"AnalogUnderclock"] unsignedLongValue]){
analogClockMultiplier = MIN(MAX(1 - axis.value + 0.2, 1.0 / 3), 1.0);
analogClockMultiplier = MIN(MAX(1 - axis.value + 0.05, 1.0 / 3), 1.0);
analogClockMultiplierValid = true;
}
else if ((axis.usage == JOYAxisUsageL1 && !mapping) ||
axis.uniqueID == [mapping[@"AnalogTurbo"] unsignedLongValue]){
analogClockMultiplier = MIN(MAX(axis.value * 3 + 0.8, 1.0), 3.0);
analogClockMultiplier = MIN(MAX(axis.value * 3 + 0.95, 1.0), 3.0);
analogClockMultiplierValid = true;
}
}
- (void)controller:(JOYController *)controller movedAxes2D:(JOYAxes2D *)axes
{
if (!_gb) return;
if ([self shouldControllerUseJoystickForMotion:controller]) {
if (!self.mouseControlsActive) {
GB_set_accelerometer_values(_gb, -axes.value.x, -axes.value.y);
}
}
}
- (void)controller:(JOYController *)controller movedAxes3D:(JOYAxes3D *)axes
{
if (!_gb) return;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBMBC7JoystickOverride"]) return;
if (self.mouseControlsActive) return;
if (axes.usage == JOYAxes3DUsageOrientation) {
for (JOYAxes3D *axes in controller.axes3D) {
// Only use orientation if there's no acceleration axes
if (axes.usage == JOYAxes3DUsageAcceleration) {
return;
}
}
JOYPoint3D point = axes.normalizedValue;
GB_set_accelerometer_values(_gb, point.x, point.z);
}
else if (axes.usage == JOYAxes3DUsageAcceleration) {
JOYPoint3D point = axes.gUnitsValue;
GB_set_accelerometer_values(_gb, point.x, point.z);
}
}
- (void)controller:(JOYController *)controller buttonChangedState:(JOYButton *)button
{
if (!_gb) return;
if (![self.window isMainWindow]) return;
_mouseControlEnabled = false;
if (button.type == JOYButtonTypeAxes2DEmulated && [self shouldControllerUseJoystickForMotion:controller]) return;
unsigned player_count = GB_get_player_count(_gb);
if (self.document.partner) {
@ -481,7 +553,7 @@ static const uint8_t workboy_vk_to_key[] = {
continue;
}
dispatch_async(dispatch_get_main_queue(), ^{
[controller setPlayerLEDs:1 << player];
[controller setPlayerLEDs:[controller LEDMaskForPlayer:player]];
});
NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"][controller.uniqueID];
if (!mapping) {
@ -532,17 +604,22 @@ static const uint8_t workboy_vk_to_key[] = {
else {
GB_set_turbo_mode(_gb, false, false);
}
_turbo = false;
}
break;
}
case JOYButtonUsageL1: {
if (self.document.isSlave) {
GB_set_turbo_mode(self.document.partner.gb, button.isPressed, false); break;
}
else {
GB_set_turbo_mode(_gb, button.isPressed, button.isPressed && self.isRewinding); break;
if (!analogClockMultiplierValid || analogClockMultiplier == 1.0 || !button.isPressed) {
if (self.document.isSlave) {
GB_set_turbo_mode(self.document.partner.gb, button.isPressed, false);
}
else {
GB_set_turbo_mode(_gb, button.isPressed, button.isPressed && self.isRewinding);
}
_turbo = button.isPressed;
}
break;
}
case JOYButtonUsageR1: underclockKeyDown = button.isPressed; break;
@ -559,14 +636,21 @@ static const uint8_t workboy_vk_to_key[] = {
- (BOOL)acceptsFirstResponder
{
return YES;
return true;
}
- (bool)mouseControlsActive
{
return _gb && GB_is_inited(_gb) && GB_has_accelerometer(_gb) &&
_mouseControlEnabled && [[NSUserDefaults standardUserDefaults] boolForKey:@"GBMBC7AllowMouse"];
}
- (void)mouseEntered:(NSEvent *)theEvent
{
if (!mouse_hidden) {
mouse_hidden = true;
if (_mouseHidingEnabled) {
if (_mouseHidingEnabled &&
!self.mouseControlsActive) {
[NSCursor hide];
}
}
@ -584,7 +668,47 @@ static const uint8_t workboy_vk_to_key[] = {
[super mouseExited:theEvent];
}
- (void)setMouseHidingEnabled:(BOOL)mouseHidingEnabled
- (void)mouseDown:(NSEvent *)event
{
_mouseControlEnabled = true;
if (self.mouseControlsActive) {
if (event.type == NSEventTypeLeftMouseDown) {
GB_set_key_state(_gb, GB_KEY_A, true);
}
}
}
- (void)mouseUp:(NSEvent *)event
{
if (self.mouseControlsActive) {
if (event.type == NSEventTypeLeftMouseUp) {
GB_set_key_state(_gb, GB_KEY_A, false);
}
}
}
- (void)mouseMoved:(NSEvent *)event
{
if (self.mouseControlsActive) {
NSPoint point = [self convertPoint:[event locationInWindow] toView:nil];
point.x /= self.frame.size.width;
point.x *= 2;
point.x -= 1;
point.y /= self.frame.size.height;
point.y *= 2;
point.y -= 1;
if (GB_get_screen_width(_gb) != 160) { // has border
point.x *= 256 / 160.0;
point.y *= 224 / 114.0;
}
GB_set_accelerometer_values(_gb, -point.x, point.y);
}
}
- (void)setMouseHidingEnabled:(bool)mouseHidingEnabled
{
if (mouseHidingEnabled == _mouseHidingEnabled) return;
@ -599,7 +723,7 @@ static const uint8_t workboy_vk_to_key[] = {
}
}
- (BOOL)isMouseHidingEnabled
- (bool)isMouseHidingEnabled
{
return _mouseHidingEnabled;
}
@ -626,4 +750,35 @@ static const uint8_t workboy_vk_to_key[] = {
return image_buffers[(current_buffer + 2) % self.numberOfBuffers];
}
-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSURLPboardType] ) {
NSURL *fileURL = [NSURL URLFromPasteboard:pboard];
if (GB_is_save_state(fileURL.fileSystemRepresentation)) {
return NSDragOperationGeneric;
}
}
return NSDragOperationNone;
}
-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSURLPboardType] ) {
NSURL *fileURL = [NSURL URLFromPasteboard:pboard];
return [_document loadStateFile:fileURL.fileSystemRepresentation noErrorOnNotFound:false];
}
return false;
}
- (NSImage *)renderToImage;
{
/* Not going to support this on OpenGL, OpenGL is too much of a terrible API for me
to bother figuring out how the hell something so trivial can be done. */
return nil;
}
@end

View File

@ -19,7 +19,7 @@
NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
self.internalView = [[GBOpenGLView alloc] initWithFrame:self.frame pixelFormat:pf];
((GBOpenGLView *)self.internalView).wantsBestResolutionOpenGLSurface = YES;
((GBOpenGLView *)self.internalView).wantsBestResolutionOpenGLSurface = true;
((GBOpenGLView *)self.internalView).openGLContext = context;
}
@ -27,8 +27,8 @@
{
[super flip];
dispatch_async(dispatch_get_main_queue(), ^{
[self.internalView setNeedsDisplay:YES];
[self setNeedsDisplay:YES];
[self.internalView setNeedsDisplay:true];
[self setNeedsDisplay:true];
});
}

View File

@ -1,3 +1,4 @@
#import <CoreImage/CoreImage.h>
#import "GBViewMetal.h"
#pragma clang diagnostic ignored "-Wpartial-availability"
@ -51,8 +52,9 @@ static const vector_float2 rect[] =
MTKView *view = [[MTKView alloc] initWithFrame:self.frame device:(device = MTLCreateSystemDefaultDevice())];
view.delegate = self;
self.internalView = view;
view.paused = YES;
view.enableSetNeedsDisplay = YES;
view.paused = true;
view.enableSetNeedsDisplay = true;
view.framebufferOnly = false;
vertices = [device newBufferWithBytes:rect
length:sizeof(rect)
@ -92,7 +94,7 @@ static const vector_float2 rect[] =
withString:scaler_source];
MTLCompileOptions *options = [[MTLCompileOptions alloc] init];
options.fastMathEnabled = YES;
options.fastMathEnabled = true;
id<MTLLibrary> library = [device newLibraryWithSource:shader_source
options:options
error:&error];
@ -123,7 +125,7 @@ static const vector_float2 rect[] =
command_queue = [device newCommandQueue];
}
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size
- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size
{
output_resolution = (vector_float2){size.width, size.height};
dispatch_async(dispatch_get_main_queue(), ^{
@ -131,7 +133,7 @@ static const vector_float2 rect[] =
});
}
- (void)drawInMTKView:(nonnull MTKView *)view
- (void)drawInMTKView:(MTKView *)view
{
if (!(view.window.occlusionState & NSWindowOcclusionStateVisible)) return;
if (!self.gb) return;
@ -208,8 +210,23 @@ static const vector_float2 rect[] =
{
[super flip];
dispatch_async(dispatch_get_main_queue(), ^{
[(MTKView *)self.internalView setNeedsDisplay:YES];
[(MTKView *)self.internalView setNeedsDisplay:true];
});
}
- (NSImage *)renderToImage
{
CIImage *ciImage = [CIImage imageWithMTLTexture:[[(MTKView *)self.internalView currentDrawable] texture]
options:@{
kCIImageColorSpace: (__bridge_transfer id)CGColorSpaceCreateDeviceRGB()
}];
ciImage = [ciImage imageByApplyingTransform:CGAffineTransformTranslate(CGAffineTransformMakeScale(1, -1),
0, ciImage.extent.size.height)];
CIContext *context = [CIContext context];
CGImageRef cgImage = [context createCGImage:ciImage fromRect:ciImage.extent];
NSImage *ret = [[NSImage alloc] initWithCGImage:cgImage size:self.internalView.bounds.size];
CGImageRelease(cgImage);
return ret;
}
@end

6
Cocoa/GBVisualizerView.h Normal file
View File

@ -0,0 +1,6 @@
#import <Cocoa/Cocoa.h>
#include <Core/gb.h>
@interface GBVisualizerView : NSView
- (void)addSample:(GB_sample_t *)sample;
@end

71
Cocoa/GBVisualizerView.m Normal file
View File

@ -0,0 +1,71 @@
#import "GBVisualizerView.h"
#import "GBPaletteEditorController.h"
#include <Core/gb.h>
#define SAMPLE_COUNT 1024
static NSColor *color_to_effect_color(typeof(GB_PALETTE_DMG.colors[0]) color)
{
if (@available(macOS 10.10, *)) {
double tint = MAX(color.r, MAX(color.g, color.b)) + 64;
return [NSColor colorWithRed:color.r / tint
green:color.g / tint
blue:color.b / tint
alpha:tint/(255 + 64)];
}
return [NSColor colorWithRed:color.r / 255.0
green:color.g / 255.0
blue:color.b / 255.0
alpha:1.0];
}
@implementation GBVisualizerView
{
GB_sample_t _samples[SAMPLE_COUNT];
size_t _position;
}
- (void)drawRect:(NSRect)dirtyRect
{
const GB_palette_t *palette = [GBPaletteEditorController userPalette];
NSSize size = self.bounds.size;
[color_to_effect_color(palette->colors[0]) setFill];
NSRectFill(self.bounds);
NSBezierPath *line = [NSBezierPath bezierPath];
[line moveToPoint:NSMakePoint(0, size.height / 2)];
for (unsigned i = 0; i < SAMPLE_COUNT; i++) {
GB_sample_t *sample = _samples + ((i + _position) % SAMPLE_COUNT);
double volume = ((signed)sample->left + (signed)sample->right) / 32768.0;
[line lineToPoint:NSMakePoint(size.width * (i + 0.5) / SAMPLE_COUNT,
(volume + 1) * size.height / 2)];
}
[line lineToPoint:NSMakePoint(size.width, size.height / 2)];
[line setLineWidth:1.0];
[color_to_effect_color(palette->colors[2]) setFill];
[line fill];
[color_to_effect_color(palette->colors[1]) setFill];
NSRectFill(NSMakeRect(0, size.height / 2 - 0.5, size.width, 1));
[color_to_effect_color(palette->colors[3]) setStroke];
[line stroke];
[super drawRect:dirtyRect];
}
- (void)addSample:(GB_sample_t *)sample
{
_samples[_position++] = *sample;
if (_position == SAMPLE_COUNT) {
_position = 0;
}
}
@end

View File

@ -10,7 +10,7 @@ static GBWarningPopover *lastPopover;
lastPopover = [[self alloc] init];
[lastPopover setBehavior:NSPopoverBehaviorApplicationDefined];
[lastPopover setAnimates:YES];
[lastPopover setAnimates:true];
lastPopover.contentViewController = [[NSViewController alloc] initWithNibName:@"PopoverView" bundle:nil];
NSTextField *field = (NSTextField *)lastPopover.contentViewController.view;
[field setStringValue:contents];
@ -20,7 +20,7 @@ static GBWarningPopover *lastPopover;
[lastPopover setContentSize:textSize];
if (!view.window.isVisible) {
[view.window setIsVisible:YES];
[view.window setIsVisible:true];
}
[lastPopover showRelativeToRect:view.bounds

View File

@ -51,7 +51,7 @@
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>gbc</string>
<string>isx</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>ColorCartridge</string>
@ -68,6 +68,26 @@
<key>NSDocumentClass</key>
<string>Document</string>
</dict>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>gbs</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>ColorCartridge</string>
<key>CFBundleTypeName</key>
<string>Game Boy Sound File</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSItemContentTypes</key>
<array>
<string>com.github.liji32.sameboy.gbs</string>
</array>
<key>LSTypeIsPackage</key>
<integer>0</integer>
<key>NSDocumentClass</key>
<string>Document</string>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>SameBoy</string>
@ -92,7 +112,7 @@
<key>LSMinimumSystemVersion</key>
<string>10.9</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2015-2020 Lior Halphon</string>
<string>Copyright © 2015-2021 Lior Halphon</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
@ -156,6 +176,25 @@
</array>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeDescription</key>
<string>Game Boy Sound File</string>
<key>UTTypeIconFile</key>
<string>ColorCartridge</string>
<key>UTTypeIdentifier</key>
<string>com.github.liji32.sameboy.gbs</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>gbs</string>
</array>
</dict>
</dict>
</array>
<key>NSCameraUsageDescription</key>
<string>SameBoy needs to access your camera to emulate the Game Boy Camera</string>

View File

@ -30,7 +30,7 @@
<h1>SameBoy</h1>
<h2>MIT License</h2>
<h3>Copyright © 2015-2020 Lior Halphon</h3>
<h3>Copyright © 2015-2021 Lior Halphon</h3>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -316,12 +316,35 @@
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="5GS-tt-E0a"/>
<menuItem title="Save Screenshot" keyEquivalent="s" id="0J3-yf-iXs">
<connections>
<action selector="saveScreenshot:" target="-1" id="gJd-ml-J8p"/>
</connections>
</menuItem>
<menuItem title="Save Screenshot As…" alternate="YES" keyEquivalent="s" id="98X-Fp-Uny">
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
<connections>
<action selector="saveScreenshotAs:" target="-1" id="Cxc-Gx-ql1"/>
</connections>
</menuItem>
<menuItem title="Copy Screenshot" keyEquivalent="S" id="vbX-pB-QC8">
<connections>
<action selector="copyScreenshot:" target="-1" id="XJC-EB-HNl"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="zk7-gf-LXN"/>
<menuItem title="Game Boy" tag="1" id="g7C-LA-VAr">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="reset:" target="-1" id="rxG-cz-s1S"/>
</connections>
</menuItem>
<menuItem title="Game Boy Pocket/Light" tag="5" id="1bM-CT-hoW">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="reset:" target="-1" id="U7l-BM-kB1"/>
</connections>
</menuItem>
<menuItem title="Super Game Boy" tag="4" id="vc7-yy-ARW">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
@ -433,6 +456,19 @@
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="M6n-8G-LZS"/>
<menuItem title="Show Background and Window" state="on" id="yfD-Qd-zoz">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="toggleDisplayBackground:" target="-1" id="p5b-1n-SPR"/>
</connections>
</menuItem>
<menuItem title="Show Objects" state="on" id="OWx-a0-vQk">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="toggleDisplayObjects:" target="-1" id="8ie-ey-739"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="afI-BR-65k"/>
<menuItem title="Show Memory" id="UIa-n7-LSa">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>

BIN
Cocoa/Next.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Cocoa/Next@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
Cocoa/Pause.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Cocoa/Pause@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
Cocoa/Play.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
Cocoa/Play@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

File diff suppressed because it is too large Load Diff

BIN
Cocoa/Previous.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Cocoa/Previous@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

BIN
Cocoa/Rewind.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Cocoa/Rewind@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

139
Cocoa/UpdateWindow.xib Normal file
View File

@ -0,0 +1,139 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14868" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14868"/>
<plugIn identifier="com.apple.WebKitIBPlugin" version="14868"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="AppDelegate">
<connections>
<outlet property="updateChanges" destination="ATt-VM-cb5" id="hJj-Nd-FBv"/>
<outlet property="updateProgressButton" destination="7nO-AA-WmG" id="wTa-9l-cOG"/>
<outlet property="updateProgressLabel" destination="wIm-GX-c6B" id="URp-JG-6wR"/>
<outlet property="updateProgressSpinner" destination="fqq-Nb-THz" id="4vC-m5-ysO"/>
<outlet property="updateProgressWindow" destination="2Gy-QG-FoA" id="RXw-50-DQh"/>
<outlet property="updateWindow" destination="QvC-M9-y7g" id="iwP-kC-tmG"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Update Available" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="480" height="360"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1417"/>
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="480" height="360"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="xZt-UI-Wl2">
<rect key="frame" x="338" y="13" width="128" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxY="YES"/>
<buttonCell key="cell" type="push" title="Install Update" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Mav-rY-sJo">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<string key="keyEquivalent" base64-UTF8="YES">
DQ
</string>
</buttonCell>
<connections>
<action selector="installUpdate:" target="-2" id="jJc-CY-4vz"/>
</connections>
</button>
<button verticalHuggingPriority="750" id="b3S-YN-iDZ">
<rect key="frame" x="242" y="13" width="96" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxY="YES"/>
<buttonCell key="cell" type="push" title="Not Now" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="QsJ-cv-hwF">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="performClose:" target="QvC-M9-y7g" id="8qO-ac-k7U"/>
</connections>
</button>
<button verticalHuggingPriority="750" id="uOb-4Q-S8o">
<rect key="frame" x="14" y="13" width="128" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<buttonCell key="cell" type="push" title="Skip Version" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Ai5-6n-dvH">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="skipVersion:" target="-2" id="Jf8-Qe-6X0"/>
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Swh-S6-6XA">
<rect key="frame" x="18" y="313" width="444" height="27"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="A new version of SameBoy is available with the following changes:" id="WsO-pC-VO7">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<box verticalHuggingPriority="750" fixedFrame="YES" boxType="separator" translatesAutoresizingMaskIntoConstraints="NO" id="9Tu-Q3-l40">
<rect key="frame" x="0.0" y="302" width="480" height="5"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
</box>
<box verticalHuggingPriority="750" fixedFrame="YES" boxType="separator" translatesAutoresizingMaskIntoConstraints="NO" id="dva-Ay-nnl">
<rect key="frame" x="0.0" y="58" width="480" height="4"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
</box>
<webView maintainsBackForwardList="NO" id="ATt-VM-cb5">
<rect key="frame" x="0.0" y="61" width="480" height="243"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<webPreferences key="preferences" defaultFontSize="13" defaultFixedFontSize="13" minimumFontSize="0" plugInsEnabled="NO" javaEnabled="NO" javaScriptEnabled="NO" javaScriptCanOpenWindowsAutomatically="NO" loadsImagesAutomatically="NO" allowsAnimatedImages="NO" allowsAnimatedImageLooping="NO">
<nil key="identifier"/>
</webPreferences>
<connections>
<outlet property="UIDelegate" destination="-2" id="xQ1-eY-1hu"/>
<outlet property="frameLoadDelegate" destination="-2" id="BOf-df-5LR"/>
</connections>
</webView>
</subviews>
</view>
<point key="canvasLocation" x="217" y="267"/>
</window>
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" id="2Gy-QG-FoA">
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="283" y="305" width="512" height="61"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1417"/>
<view key="contentView" id="jjT-Z5-15Q">
<rect key="frame" x="0.0" y="0.0" width="512" height="61"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<progressIndicator wantsLayer="YES" fixedFrame="YES" maxValue="100" displayedWhenStopped="NO" indeterminate="YES" style="spinning" translatesAutoresizingMaskIntoConstraints="NO" id="fqq-Nb-THz">
<rect key="frame" x="20" y="15" width="32" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</progressIndicator>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="7nO-AA-WmG">
<rect key="frame" x="417" y="13" width="82" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Cancel" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="6l6-qX-gsr">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<string key="keyEquivalent" base64-UTF8="YES">
Gw
</string>
</buttonCell>
<connections>
<action selector="updateAction:" target="-2" id="geO-Gk-xrs"/>
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="wIm-GX-c6B">
<rect key="frame" x="58" y="15" width="359" height="24"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Downloading update..." id="qmF-X1-v5B">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
</view>
<point key="canvasLocation" x="11" y="-203.5"/>
</window>
</objects>
</document>

File diff suppressed because it is too large Load Diff

View File

@ -3,8 +3,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include "gb_struct_def.h"
#include "defs.h"
#ifdef GB_INTERNAL
/* Speed = 1 / Length (in seconds) */
@ -46,12 +45,19 @@ enum GB_CHANNELS {
GB_N_CHANNELS
};
typedef struct
{
bool locked:1;
bool clock:1; // Represents FOSY on channel 4
unsigned padding:6;
} GB_envelope_clock_t;
typedef void (*GB_sample_callback_t)(GB_gameboy_t *gb, GB_sample_t *sample);
typedef struct
{
bool global_enable;
uint8_t apu_cycles;
uint16_t apu_cycles;
uint8_t samples[GB_N_CHANNELS];
bool is_active[GB_N_CHANNELS];
@ -67,20 +73,21 @@ typedef struct
uint16_t sweep_length_addend;
uint16_t shadow_sweep_sample_length;
bool unshifted_sweep;
GB_PADDING(bool, sweep_decreasing);
bool enable_zombie_calculate_stepping;
uint8_t channel_1_restart_hold;
uint16_t channel1_completed_addend;
struct {
uint16_t pulse_length; // Reloaded from NRX1 (xorred), in 256Hz DIV ticks
uint8_t current_volume; // Reloaded from NRX2
uint8_t volume_countdown; // Reloaded from NRX2
uint8_t current_sample_index; /* For save state compatibility,
highest bit is reused (See NR14/NR24's
write code)*/
uint8_t current_sample_index;
bool sample_surpressed;
uint16_t sample_countdown; // in APU ticks (Reloaded from sample_length, xorred $7FF)
uint16_t sample_length; // From NRX3, NRX4, in APU ticks
bool length_enabled; // NRX4
GB_envelope_clock_t envelope_clock;
} square_channels[2];
struct {
@ -92,10 +99,10 @@ typedef struct
uint16_t sample_countdown; // in APU ticks (Reloaded from sample_length, xorred $7FF)
uint8_t current_sample_index;
uint8_t current_sample; // Current sample before shifting.
int8_t wave_form[32];
uint8_t current_sample_byte; // Current sample byte.
bool wave_form_just_read;
bool pulsed;
uint8_t bugged_read_countdown;
} wave_channel;
struct {
@ -106,25 +113,24 @@ typedef struct
bool narrow;
uint8_t counter_countdown; // Counts from 0-7 to 0 to tick counter (Scaled from 512KHz to 2MHz)
uint8_t __padding;
uint16_t counter; // A bit from this 14-bit register ticks LFSR
bool length_enabled; // NR44
uint8_t alignment; // If (NR43 & 7) != 0, samples are aligned to 512KHz clock instead of
// 1MHz. This variable keeps track of the alignment.
bool current_lfsr_sample;
int8_t delta;
bool countdown_reloaded;
uint8_t dmg_delayed_start;
GB_envelope_clock_t envelope_clock;
} noise_channel;
#define GB_SKIP_DIV_EVENT_INACTIVE 0
#define GB_SKIP_DIV_EVENT_SKIPPED 1
#define GB_SKIP_DIV_EVENT_SKIP 2
uint8_t skip_div_event;
bool current_lfsr_sample;
enum {
GB_SKIP_DIV_EVENT_INACTIVE,
GB_SKIP_DIV_EVENT_SKIPPED,
GB_SKIP_DIV_EVENT_SKIP,
} skip_div_event:8;
uint8_t pcm_mask[2]; // For CGB-0 to CGB-C PCM read glitch
uint8_t channel_1_restart_hold;
int8_t channel_4_delta;
bool channel_4_countdown_reloaded;
uint8_t channel_4_dmg_delayed_start;
} GB_apu_t;
typedef enum {
@ -137,8 +143,7 @@ typedef enum {
typedef struct {
unsigned sample_rate;
double sample_cycles; // In 8 MHz units
double cycles_per_sample;
unsigned sample_cycles; // Counts by sample_rate until it reaches the clock frequency
// Samples are NOT normalized to MAX_CH_AMP * 4 at this stage!
unsigned cycles_since_render;
@ -153,7 +158,6 @@ typedef struct {
GB_sample_callback_t sample_callback;
bool rate_set_in_clocks;
double interference_volume;
double interference_highpass;
} GB_apu_output_t;
@ -165,14 +169,13 @@ void GB_set_interference_volume(GB_gameboy_t *gb, double volume);
void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback);
#ifdef GB_INTERNAL
bool GB_apu_is_DAC_enabled(GB_gameboy_t *gb, unsigned index);
void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value);
uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg);
void GB_apu_div_event(GB_gameboy_t *gb);
void GB_apu_init(GB_gameboy_t *gb);
void GB_apu_run(GB_gameboy_t *gb);
void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb);
void GB_borrow_sgb_border(GB_gameboy_t *gb);
internal bool GB_apu_is_DAC_enabled(GB_gameboy_t *gb, unsigned index);
internal void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value);
internal uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg);
internal void GB_apu_div_event(GB_gameboy_t *gb);
internal void GB_apu_div_secondary_event(GB_gameboy_t *gb);
internal void GB_apu_init(GB_gameboy_t *gb);
internal void GB_apu_run(GB_gameboy_t *gb, bool force);
#endif
#endif /* apu_h */

View File

@ -1,7 +1,7 @@
#ifndef camera_h
#define camera_h
#include <stdint.h>
#include "gb_struct_def.h"
#include "defs.h"
typedef uint8_t (*GB_camera_get_pixel_callback_t)(GB_gameboy_t *gb, uint8_t x, uint8_t y);
typedef void (*GB_camera_update_request_callback_t)(GB_gameboy_t *gb);

View File

@ -32,10 +32,11 @@ static uint16_t bank_for_addr(GB_gameboy_t *gb, uint16_t addr)
void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value)
{
if (!gb->cheat_enabled) return;
if (!gb->boot_rom_finished) return;
if (likely(!gb->cheat_enabled)) return;
if (likely(gb->cheat_count == 0)) return; // Optimization
if (unlikely(!gb->boot_rom_finished)) return;
const GB_cheat_hash_t *hash = gb->cheat_hash[hash_addr(address)];
if (hash) {
if (unlikely(hash)) {
for (unsigned i = 0; i < hash->size; i++) {
GB_cheat_t *cheat = hash->cheats[i];
if (cheat->address == address && cheat->enabled && (!cheat->use_old_value || cheat->old_value == *value)) {
@ -109,7 +110,7 @@ void GB_remove_cheat(GB_gameboy_t *gb, const GB_cheat_t *cheat)
GB_cheat_hash_t **hash = &gb->cheat_hash[hash_addr(cheat->address)];
for (unsigned i = 0; i < (*hash)->size; i++) {
if ((*hash)->cheats[i] == cheat) {
(*hash)->cheats[i] = (*hash)->cheats[(*hash)->size--];
(*hash)->cheats[i] = (*hash)->cheats[--(*hash)->size];
if ((*hash)->size == 0) {
free(*hash);
*hash = NULL;
@ -200,7 +201,7 @@ void GB_update_cheat(GB_gameboy_t *gb, const GB_cheat_t *_cheat, const char *des
GB_cheat_hash_t **hash = &gb->cheat_hash[hash_addr(cheat->address)];
for (unsigned i = 0; i < (*hash)->size; i++) {
if ((*hash)->cheats[i] == cheat) {
(*hash)->cheats[i] = (*hash)->cheats[(*hash)->size--];
(*hash)->cheats[i] = (*hash)->cheats[--(*hash)->size];
if ((*hash)->size == 0) {
free(*hash);
*hash = NULL;
@ -250,7 +251,7 @@ void GB_load_cheats(GB_gameboy_t *gb, const char *path)
uint32_t struct_size = 0;
fread(&magic, sizeof(magic), 1, f);
fread(&struct_size, sizeof(struct_size), 1, f);
if (magic != CHEAT_MAGIC && magic != __builtin_bswap32(CHEAT_MAGIC)) {
if (magic != LE32(CHEAT_MAGIC) && magic != BE32(CHEAT_MAGIC)) {
GB_log(gb, "The file is not a SameBoy cheat database");
return;
}
@ -267,7 +268,7 @@ void GB_load_cheats(GB_gameboy_t *gb, const char *path)
GB_cheat_t cheat;
while (fread(&cheat, sizeof(cheat), 1, f)) {
if (magic == __builtin_bswap32(CHEAT_MAGIC)) {
if (magic != CHEAT_MAGIC) {
cheat.address = __builtin_bswap16(cheat.address);
cheat.bank = __builtin_bswap16(cheat.bank);
}

View File

@ -1,6 +1,6 @@
#ifndef cheats_h
#define cheats_h
#include "gb_struct_def.h"
#include "defs.h"
#define GB_CHEAT_ANY_BANK 0xFFFF
@ -20,7 +20,7 @@ int GB_save_cheats(GB_gameboy_t *gb, const char *path);
#ifdef GB_DISABLE_CHEATS
#define GB_apply_cheat(...)
#else
void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value);
internal void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value);
#endif
#endif

View File

@ -131,30 +131,25 @@ static const char *value_to_string(GB_gameboy_t *gb, uint16_t value, bool prefer
symbol = NULL;
}
/* Avoid overflow */
if (symbol && strlen(symbol->name) >= 240) {
symbol = NULL;
}
if (!symbol) {
sprintf(output, "$%04x", value);
snprintf(output, sizeof(output), "$%04x", value);
}
else if (symbol->addr == value) {
if (prefer_name) {
sprintf(output, "%s ($%04x)", symbol->name, value);
snprintf(output, sizeof(output), "%s ($%04x)", symbol->name, value);
}
else {
sprintf(output, "$%04x (%s)", value, symbol->name);
snprintf(output, sizeof(output), "$%04x (%s)", value, symbol->name);
}
}
else {
if (prefer_name) {
sprintf(output, "%s+$%03x ($%04x)", symbol->name, value - symbol->addr, value);
snprintf(output, sizeof(output), "%s+$%03x ($%04x)", symbol->name, value - symbol->addr, value);
}
else {
sprintf(output, "$%04x (%s+$%03x)", value, symbol->name, value - symbol->addr);
snprintf(output, sizeof(output), "$%04x (%s+$%03x)", value, symbol->name, value - symbol->addr);
}
}
return output;
@ -171,30 +166,25 @@ static const char *debugger_value_to_string(GB_gameboy_t *gb, value_t value, boo
symbol = NULL;
}
/* Avoid overflow */
if (symbol && strlen(symbol->name) >= 240) {
symbol = NULL;
}
if (!symbol) {
sprintf(output, "$%02x:$%04x", value.bank, value.value);
snprintf(output, sizeof(output), "$%02x:$%04x", value.bank, value.value);
}
else if (symbol->addr == value.value) {
if (prefer_name) {
sprintf(output, "%s ($%02x:$%04x)", symbol->name, value.bank, value.value);
snprintf(output, sizeof(output), "%s ($%02x:$%04x)", symbol->name, value.bank, value.value);
}
else {
sprintf(output, "$%02x:$%04x (%s)", value.bank, value.value, symbol->name);
snprintf(output, sizeof(output), "$%02x:$%04x (%s)", value.bank, value.value, symbol->name);
}
}
else {
if (prefer_name) {
sprintf(output, "%s+$%03x ($%02x:$%04x)", symbol->name, value.value - symbol->addr, value.bank, value.value);
snprintf(output, sizeof(output), "%s+$%03x ($%02x:$%04x)", symbol->name, value.value - symbol->addr, value.bank, value.value);
}
else {
sprintf(output, "$%02x:$%04x (%s+$%03x)", value.bank, value.value, symbol->name, value.value - symbol->addr);
snprintf(output, sizeof(output), "$%02x:$%04x (%s+$%03x)", value.bank, value.value, symbol->name, value.value - symbol->addr);
}
}
return output;
@ -438,23 +428,23 @@ static lvalue_t debugger_evaluate_lvalue(GB_gameboy_t *gb, const char *string,
if (string[0] != '$' && (string[0] < '0' || string[0] > '9')) {
if (length == 1) {
switch (string[0]) {
case 'a': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->registers[GB_REGISTER_AF]};
case 'f': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->registers[GB_REGISTER_AF]};
case 'b': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->registers[GB_REGISTER_BC]};
case 'c': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->registers[GB_REGISTER_BC]};
case 'd': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->registers[GB_REGISTER_DE]};
case 'e': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->registers[GB_REGISTER_DE]};
case 'h': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->registers[GB_REGISTER_HL]};
case 'l': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->registers[GB_REGISTER_HL]};
case 'a': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->af};
case 'f': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->af};
case 'b': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->bc};
case 'c': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->bc};
case 'd': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->de};
case 'e': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->de};
case 'h': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->hl};
case 'l': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->hl};
}
}
else if (length == 2) {
switch (string[0]) {
case 'a': if (string[1] == 'f') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_AF]};
case 'b': if (string[1] == 'c') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_BC]};
case 'd': if (string[1] == 'e') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_DE]};
case 'h': if (string[1] == 'l') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_HL]};
case 's': if (string[1] == 'p') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_SP]};
case 'a': if (string[1] == 'f') return (lvalue_t){LVALUE_REG16, .register_address = &gb->af};
case 'b': if (string[1] == 'c') return (lvalue_t){LVALUE_REG16, .register_address = &gb->bc};
case 'd': if (string[1] == 'e') return (lvalue_t){LVALUE_REG16, .register_address = &gb->de};
case 'h': if (string[1] == 'l') return (lvalue_t){LVALUE_REG16, .register_address = &gb->hl};
case 's': if (string[1] == 'p') return (lvalue_t){LVALUE_REG16, .register_address = &gb->sp};
case 'p': if (string[1] == 'c') return (lvalue_t){LVALUE_REG16, .register_address = &gb->pc};
}
}
@ -616,23 +606,23 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string,
if (string[0] != '$' && (string[0] < '0' || string[0] > '9')) {
if (length == 1) {
switch (string[0]) {
case 'a': ret = VALUE_16(gb->registers[GB_REGISTER_AF] >> 8); goto exit;
case 'f': ret = VALUE_16(gb->registers[GB_REGISTER_AF] & 0xFF); goto exit;
case 'b': ret = VALUE_16(gb->registers[GB_REGISTER_BC] >> 8); goto exit;
case 'c': ret = VALUE_16(gb->registers[GB_REGISTER_BC] & 0xFF); goto exit;
case 'd': ret = VALUE_16(gb->registers[GB_REGISTER_DE] >> 8); goto exit;
case 'e': ret = VALUE_16(gb->registers[GB_REGISTER_DE] & 0xFF); goto exit;
case 'h': ret = VALUE_16(gb->registers[GB_REGISTER_HL] >> 8); goto exit;
case 'l': ret = VALUE_16(gb->registers[GB_REGISTER_HL] & 0xFF); goto exit;
case 'a': ret = VALUE_16(gb->af >> 8); goto exit;
case 'f': ret = VALUE_16(gb->af & 0xFF); goto exit;
case 'b': ret = VALUE_16(gb->bc >> 8); goto exit;
case 'c': ret = VALUE_16(gb->bc & 0xFF); goto exit;
case 'd': ret = VALUE_16(gb->de >> 8); goto exit;
case 'e': ret = VALUE_16(gb->de & 0xFF); goto exit;
case 'h': ret = VALUE_16(gb->hl >> 8); goto exit;
case 'l': ret = VALUE_16(gb->hl & 0xFF); goto exit;
}
}
else if (length == 2) {
switch (string[0]) {
case 'a': if (string[1] == 'f') {ret = VALUE_16(gb->registers[GB_REGISTER_AF]); goto exit;}
case 'b': if (string[1] == 'c') {ret = VALUE_16(gb->registers[GB_REGISTER_BC]); goto exit;}
case 'd': if (string[1] == 'e') {ret = VALUE_16(gb->registers[GB_REGISTER_DE]); goto exit;}
case 'h': if (string[1] == 'l') {ret = VALUE_16(gb->registers[GB_REGISTER_HL]); goto exit;}
case 's': if (string[1] == 'p') {ret = VALUE_16(gb->registers[GB_REGISTER_SP]); goto exit;}
case 'a': if (string[1] == 'f') {ret = VALUE_16(gb->af); goto exit;}
case 'b': if (string[1] == 'c') {ret = VALUE_16(gb->bc); goto exit;}
case 'd': if (string[1] == 'e') {ret = VALUE_16(gb->de); goto exit;}
case 'h': if (string[1] == 'l') {ret = VALUE_16(gb->hl); goto exit;}
case 's': if (string[1] == 'p') {ret = VALUE_16(gb->sp); goto exit;}
case 'p': if (string[1] == 'c') {ret = (value_t){true, bank_for_addr(gb, gb->pc), gb->pc}; goto exit;}
}
}
@ -821,15 +811,15 @@ static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const
}
GB_log(gb, "AF = $%04x (%c%c%c%c)\n", gb->registers[GB_REGISTER_AF], /* AF can't really be an address */
GB_log(gb, "AF = $%04x (%c%c%c%c)\n", gb->af, /* AF can't really be an address */
(gb->f & GB_CARRY_FLAG)? 'C' : '-',
(gb->f & GB_HALF_CARRY_FLAG)? 'H' : '-',
(gb->f & GB_SUBTRACT_FLAG)? 'N' : '-',
(gb->f & GB_ZERO_FLAG)? 'Z' : '-');
GB_log(gb, "BC = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_BC], false));
GB_log(gb, "DE = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_DE], false));
GB_log(gb, "HL = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_HL], false));
GB_log(gb, "SP = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_SP], false));
GB_log(gb, "BC = %s\n", value_to_string(gb, gb->bc, false));
GB_log(gb, "DE = %s\n", value_to_string(gb, gb->de, false));
GB_log(gb, "HL = %s\n", value_to_string(gb, gb->hl, false));
GB_log(gb, "SP = %s\n", value_to_string(gb, gb->sp, false));
GB_log(gb, "PC = %s\n", value_to_string(gb, gb->pc, false));
GB_log(gb, "IME = %s\n", gb->ime? "Enabled" : "Disabled");
return true;
@ -1461,7 +1451,7 @@ static bool examine(GB_gameboy_t *gb, char *arguments, char *modifiers, const de
while (count) {
GB_log(gb, "%02x:%04x: ", addr.bank, addr.value);
for (unsigned i = 0; i < 16 && count; i++) {
GB_log(gb, "%02x ", GB_read_memory(gb, addr.value + i));
GB_log(gb, "%02x ", GB_safe_read_memory(gb, addr.value + i));
count--;
}
addr.value += 16;
@ -1474,7 +1464,7 @@ static bool examine(GB_gameboy_t *gb, char *arguments, char *modifiers, const de
while (count) {
GB_log(gb, "%04x: ", addr.value);
for (unsigned i = 0; i < 16 && count; i++) {
GB_log(gb, "%02x ", GB_read_memory(gb, addr.value + i));
GB_log(gb, "%02x ", GB_safe_read_memory(gb, addr.value + i));
count--;
}
addr.value += 16;
@ -1533,7 +1523,9 @@ static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg
const GB_cartridge_t *cartridge = gb->cartridge_type;
if (cartridge->has_ram) {
GB_log(gb, "Cartridge includes%s RAM: $%x bytes\n", cartridge->has_battery? " battery-backed": "", gb->mbc_ram_size);
bool has_battery = gb->cartridge_type->has_battery &&
(gb->cartridge_type->mbc_type != GB_TPP1 || (gb->rom[0x153] & 8));
GB_log(gb, "Cartridge includes%s RAM: $%x bytes\n", has_battery? " battery-backed": "", gb->mbc_ram_size);
}
else {
GB_log(gb, "No cartridge RAM\n");
@ -1549,6 +1541,7 @@ static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg
[GB_MBC2] = "MBC2",
[GB_MBC3] = "MBC3",
[GB_MBC5] = "MBC5",
[GB_MBC7] = "MBC7",
[GB_HUC1] = "HUC-1",
[GB_HUC3] = "HUC-3",
};
@ -1558,7 +1551,7 @@ static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg
if (cartridge->has_ram) {
GB_log(gb, "Current mapped RAM bank: %x\n", gb->mbc_ram_bank);
if (gb->cartridge_type->mbc_type != GB_HUC1) {
GB_log(gb, "RAM is curently %s\n", gb->mbc_ram_enable? "enabled" : "disabled");
GB_log(gb, "RAM is currently %s\n", gb->mbc_ram_enable? "enabled" : "disabled");
}
}
if (cartridge->mbc_type == GB_MBC1 && gb->mbc1_wiring == GB_STANDARD_MBC1_WIRING) {
@ -1575,7 +1568,8 @@ static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg
GB_log(gb, "No MBC\n");
}
if (cartridge->has_rumble) {
if (gb->cartridge_type->has_rumble &&
(gb->cartridge_type->mbc_type != GB_TPP1 || (gb->rom[0x153] & 1))) {
GB_log(gb, "Cart contains a Rumble Pak\n");
}
@ -1613,8 +1607,12 @@ static bool ticks(GB_gameboy_t *gb, char *arguments, char *modifiers, const debu
return true;
}
GB_log(gb, "Ticks: %llu. (Resetting)\n", (unsigned long long)gb->debugger_ticks);
GB_log(gb, "T-cycles: %llu\n", (unsigned long long)gb->debugger_ticks);
GB_log(gb, "M-cycles: %llu\n", (unsigned long long)gb->debugger_ticks / 4);
GB_log(gb, "Absolute 8MHz ticks: %llu\n", (unsigned long long)gb->absolute_debugger_ticks);
GB_log(gb, "Tick count reset.\n");
gb->debugger_ticks = 0;
gb->absolute_debugger_ticks = 0;
return true;
}
@ -1641,9 +1639,9 @@ static bool palettes(GB_gameboy_t *gb, char *arguments, char *modifiers, const d
}
}
GB_log(gb, "Sprites palettes: \n");
GB_log(gb, "Object palettes: \n");
for (unsigned i = 0; i < 32; i++) {
GB_log(gb, "%04x ", ((uint16_t *)&gb->sprite_palettes_data)[i]);
GB_log(gb, "%04x ", ((uint16_t *)&gb->object_palettes_data)[i]);
if (i % 4 == 3) {
GB_log(gb, "\n");
}
@ -1661,7 +1659,7 @@ static bool lcd(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg
}
GB_log(gb, "LCDC:\n");
GB_log(gb, " LCD enabled: %s\n",(gb->io_registers[GB_IO_LCDC] & 128)? "Enabled" : "Disabled");
GB_log(gb, " %s: %s\n", (gb->cgb_mode? "Sprite priority flags" : "Background and Window"),
GB_log(gb, " %s: %s\n", (gb->cgb_mode? "Object priority flags" : "Background and Window"),
(gb->io_registers[GB_IO_LCDC] & 1)? "Enabled" : "Disabled");
GB_log(gb, " Objects: %s\n", (gb->io_registers[GB_IO_LCDC] & 2)? "Enabled" : "Disabled");
GB_log(gb, " Object size: %s\n", (gb->io_registers[GB_IO_LCDC] & 4)? "8x16" : "8x8");
@ -1774,8 +1772,8 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg
GB_log(gb, " Duty cycle %s%% (%s), current index %u/8%s\n",
duty > 3? "" : (const char *[]){"12.5", " 25", " 50", " 75"}[duty],
duty > 3? "" : (const char *[]){"_______-", "-______-", "-____---", "_------_"}[duty],
gb->apu.square_channels[channel].current_sample_index & 0x7f,
gb->apu.square_channels[channel].current_sample_index >> 7 ? " (suppressed)" : "");
gb->apu.square_channels[channel].current_sample_index,
gb->apu.square_channels[channel].sample_surpressed ? " (suppressed)" : "");
if (channel == GB_SQUARE_1) {
GB_log(gb, " Frequency sweep %s and %s\n",
@ -1800,8 +1798,9 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg
GB_log(gb, "\nCH3:\n");
GB_log(gb, " Wave:");
for (uint8_t i = 0; i < 32; i++) {
GB_log(gb, "%s%X", i%4?"":" ", gb->apu.wave_channel.wave_form[i]);
for (uint8_t i = 0; i < 16; i++) {
GB_log(gb, "%s%X", i % 2? "" : " ", gb->io_registers[GB_IO_WAV_START + i] >> 4);
GB_log(gb, "%X", gb->io_registers[GB_IO_WAV_START + i] & 0xF);
}
GB_log(gb, "\n");
GB_log(gb, " Current position: %u\n", gb->apu.wave_channel.current_sample_index);
@ -1883,11 +1882,14 @@ static bool wave(GB_gameboy_t *gb, char *arguments, char *modifiers, const debug
for (int8_t cur_val = 0xf & mask; cur_val >= 0; cur_val -= shift_amount) {
for (uint8_t i = 0; i < 32; i++) {
if ((gb->apu.wave_channel.wave_form[i] & mask) == cur_val) {
GB_log(gb, "%X", gb->apu.wave_channel.wave_form[i]);
uint8_t sample = i & 1?
(gb->io_registers[GB_IO_WAV_START + i / 2] & 0xF) :
(gb->io_registers[GB_IO_WAV_START + i / 2] >> 4);
if ((sample & mask) == cur_val) {
GB_log(gb, "%X", sample);
}
else {
GB_log(gb, "%c", i%4 == 2 ? '-' : ' ');
GB_log(gb, "%c", i % 4 == 2 ? '-' : ' ');
}
}
GB_log(gb, "\n");
@ -1909,7 +1911,7 @@ static bool undo(GB_gameboy_t *gb, char *arguments, char *modifiers, const debug
return true;
}
uint16_t pc = gb->pc;
GB_load_state_from_buffer(gb, gb->undo_state, GB_get_save_state_size(gb));
GB_load_state_from_buffer(gb, gb->undo_state, GB_get_save_state_size_no_bess(gb));
GB_log(gb, "Reverted a \"%s\" command.\n", gb->undo_label);
if (pc != gb->pc) {
GB_cpu_disassemble(gb, gb->pc, 5);
@ -2042,7 +2044,7 @@ void GB_debugger_call_hook(GB_gameboy_t *gb, uint16_t call_addr)
gb->debug_stopped = true;
}
else {
gb->sp_for_call_depth[gb->debug_call_depth] = gb->registers[GB_REGISTER_SP];
gb->sp_for_call_depth[gb->debug_call_depth] = gb->sp;
gb->addr_for_call_depth[gb->debug_call_depth] = gb->pc;
}
}
@ -2050,7 +2052,7 @@ void GB_debugger_call_hook(GB_gameboy_t *gb, uint16_t call_addr)
if (gb->backtrace_size < sizeof(gb->backtrace_sps) / sizeof(gb->backtrace_sps[0])) {
while (gb->backtrace_size) {
if (gb->backtrace_sps[gb->backtrace_size - 1] < gb->registers[GB_REGISTER_SP]) {
if (gb->backtrace_sps[gb->backtrace_size - 1] < gb->sp) {
gb->backtrace_size--;
}
else {
@ -2058,7 +2060,7 @@ void GB_debugger_call_hook(GB_gameboy_t *gb, uint16_t call_addr)
}
}
gb->backtrace_sps[gb->backtrace_size] = gb->registers[GB_REGISTER_SP];
gb->backtrace_sps[gb->backtrace_size] = gb->sp;
gb->backtrace_returns[gb->backtrace_size].bank = bank_for_addr(gb, call_addr);
gb->backtrace_returns[gb->backtrace_size].addr = call_addr;
gb->backtrace_size++;
@ -2079,9 +2081,9 @@ void GB_debugger_ret_hook(GB_gameboy_t *gb)
gb->debug_stopped = true;
}
else {
if (gb->registers[GB_REGISTER_SP] != gb->sp_for_call_depth[gb->debug_call_depth]) {
if (gb->sp != gb->sp_for_call_depth[gb->debug_call_depth]) {
GB_log(gb, "Stack leak detected for function %s!\n", value_to_string(gb, gb->addr_for_call_depth[gb->debug_call_depth], true));
GB_log(gb, "SP is $%04x, should be $%04x.\n", gb->registers[GB_REGISTER_SP],
GB_log(gb, "SP is $%04x, should be $%04x.\n", gb->sp,
gb->sp_for_call_depth[gb->debug_call_depth]);
gb->debug_stopped = true;
}
@ -2089,7 +2091,7 @@ void GB_debugger_ret_hook(GB_gameboy_t *gb)
}
while (gb->backtrace_size) {
if (gb->backtrace_sps[gb->backtrace_size - 1] <= gb->registers[GB_REGISTER_SP]) {
if (gb->backtrace_sps[gb->backtrace_size - 1] <= gb->sp) {
gb->backtrace_size--;
}
else {
@ -2194,6 +2196,9 @@ bool GB_debugger_execute_command(GB_gameboy_t *gb, char *input)
if (!input[0]) {
return true;
}
GB_display_sync(gb);
GB_apu_run(gb, true);
char *command_string = input;
char *arguments = strchr(input, ' ');
@ -2215,8 +2220,8 @@ bool GB_debugger_execute_command(GB_gameboy_t *gb, char *input)
const debugger_command_t *command = find_command(command_string);
if (command) {
uint8_t *old_state = malloc(GB_get_save_state_size(gb));
GB_save_state_to_buffer(gb, old_state);
uint8_t *old_state = malloc(GB_get_save_state_size_no_bess(gb));
GB_save_state_to_buffer_no_bess(gb, old_state);
bool ret = command->implementation(gb, arguments, modifiers, command);
if (!ret) { // Command continues, save state in any case
free(gb->undo_state);
@ -2224,9 +2229,9 @@ bool GB_debugger_execute_command(GB_gameboy_t *gb, char *input)
gb->undo_label = command->command;
}
else {
uint8_t *new_state = malloc(GB_get_save_state_size(gb));
GB_save_state_to_buffer(gb, new_state);
if (memcmp(new_state, old_state, GB_get_save_state_size(gb)) != 0) {
uint8_t *new_state = malloc(GB_get_save_state_size_no_bess(gb));
GB_save_state_to_buffer_no_bess(gb, new_state);
if (memcmp(new_state, old_state, GB_get_save_state_size_no_bess(gb)) != 0) {
// State changed, save the old state as the new undo state
free(gb->undo_state);
gb->undo_state = old_state;
@ -2246,7 +2251,6 @@ bool GB_debugger_execute_command(GB_gameboy_t *gb, char *input)
}
}
/* Returns true if debugger waits for more commands */
char *GB_debugger_complete_substring(GB_gameboy_t *gb, char *input, uintptr_t *context)
{
char *command_string = input;
@ -2316,8 +2320,8 @@ void GB_debugger_run(GB_gameboy_t *gb)
if (gb->debug_disable) return;
if (!gb->undo_state) {
gb->undo_state = malloc(GB_get_save_state_size(gb));
GB_save_state_to_buffer(gb, gb->undo_state);
gb->undo_state = malloc(GB_get_save_state_size_no_bess(gb));
GB_save_state_to_buffer_no_bess(gb, gb->undo_state);
}
char *input = NULL;
@ -2365,9 +2369,9 @@ next_command:
}
else if (jump_to_result == JUMP_TO_NONTRIVIAL) {
if (!gb->nontrivial_jump_state) {
gb->nontrivial_jump_state = malloc(GB_get_save_state_size(gb));
gb->nontrivial_jump_state = malloc(GB_get_save_state_size_no_bess(gb));
}
GB_save_state_to_buffer(gb, gb->nontrivial_jump_state);
GB_save_state_to_buffer_no_bess(gb, gb->nontrivial_jump_state);
gb->non_trivial_jump_breakpoint_occured = false;
should_delete_state = false;
}
@ -2541,7 +2545,7 @@ static bool is_in_trivial_memory(uint16_t addr)
return false;
}
typedef uint16_t GB_opcode_address_getter_t(GB_gameboy_t *gb, uint8_t opcode);
typedef uint16_t opcode_address_getter_t(GB_gameboy_t *gb, uint8_t opcode);
uint16_t trivial_1(GB_gameboy_t *gb, uint8_t opcode)
{
@ -2567,13 +2571,13 @@ static bool condition_code(GB_gameboy_t *gb, uint8_t opcode)
{
switch ((opcode >> 3) & 0x3) {
case 0:
return !(gb->registers[GB_REGISTER_AF] & GB_ZERO_FLAG);
return !(gb->af & GB_ZERO_FLAG);
case 1:
return (gb->registers[GB_REGISTER_AF] & GB_ZERO_FLAG);
return (gb->af & GB_ZERO_FLAG);
case 2:
return !(gb->registers[GB_REGISTER_AF] & GB_CARRY_FLAG);
return !(gb->af & GB_CARRY_FLAG);
case 3:
return (gb->registers[GB_REGISTER_AF] & GB_CARRY_FLAG);
return (gb->af & GB_CARRY_FLAG);
}
return false;
@ -2590,8 +2594,8 @@ static uint16_t jr_cc_r8(GB_gameboy_t *gb, uint8_t opcode)
static uint16_t ret(GB_gameboy_t *gb, uint8_t opcode)
{
return GB_read_memory(gb, gb->registers[GB_REGISTER_SP]) |
(GB_read_memory(gb, gb->registers[GB_REGISTER_SP] + 1) << 8);
return GB_read_memory(gb, gb->sp) |
(GB_read_memory(gb, gb->sp + 1) << 8);
}
@ -2631,7 +2635,7 @@ static uint16_t jp_hl(GB_gameboy_t *gb, uint8_t opcode)
return gb->hl;
}
static GB_opcode_address_getter_t *opcodes[256] = {
static opcode_address_getter_t *opcodes[256] = {
/* X0 X1 X2 X3 X4 X5 X6 X7 */
/* X8 X9 Xa Xb Xc Xd Xe Xf */
trivial_1, trivial_3, trivial_1, trivial_1, trivial_1, trivial_1, trivial_2, trivial_1, /* 0X */
@ -2673,7 +2677,7 @@ static jump_to_return_t test_jump_to_breakpoints(GB_gameboy_t *gb, uint16_t *add
if (!gb->has_jump_to_breakpoints) return JUMP_TO_NONE;
if (!is_in_trivial_memory(gb->pc) || !is_in_trivial_memory(gb->pc + 2) ||
!is_in_trivial_memory(gb->registers[GB_REGISTER_SP]) || !is_in_trivial_memory(gb->registers[GB_REGISTER_SP] + 1)) {
!is_in_trivial_memory(gb->sp) || !is_in_trivial_memory(gb->sp + 1)) {
return JUMP_TO_NONTRIVIAL;
}
@ -2709,7 +2713,7 @@ static jump_to_return_t test_jump_to_breakpoints(GB_gameboy_t *gb, uint16_t *add
return JUMP_TO_NONE;
}
GB_opcode_address_getter_t *getter = opcodes[opcode];
opcode_address_getter_t *getter = opcodes[opcode];
if (!getter) {
gb->n_watchpoints = n_watchpoints;
return JUMP_TO_NONE;

View File

@ -2,7 +2,7 @@
#define debugger_h
#include <stdbool.h>
#include <stdint.h>
#include "gb_struct_def.h"
#include "defs.h"
#include "symbol_hash.h"
@ -17,14 +17,14 @@
#define GB_debugger_add_symbol(gb, bank, address, symbol) ((void)bank, (void)address, (void)symbol)
#else
void GB_debugger_run(GB_gameboy_t *gb);
void GB_debugger_handle_async_commands(GB_gameboy_t *gb);
void GB_debugger_call_hook(GB_gameboy_t *gb, uint16_t call_addr);
void GB_debugger_ret_hook(GB_gameboy_t *gb);
void GB_debugger_test_write_watchpoint(GB_gameboy_t *gb, uint16_t addr, uint8_t value);
void GB_debugger_test_read_watchpoint(GB_gameboy_t *gb, uint16_t addr);
const GB_bank_symbol_t *GB_debugger_find_symbol(GB_gameboy_t *gb, uint16_t addr);
void GB_debugger_add_symbol(GB_gameboy_t *gb, uint16_t bank, uint16_t address, const char *symbol);
internal void GB_debugger_run(GB_gameboy_t *gb);
internal void GB_debugger_handle_async_commands(GB_gameboy_t *gb);
internal void GB_debugger_call_hook(GB_gameboy_t *gb, uint16_t call_addr);
internal void GB_debugger_ret_hook(GB_gameboy_t *gb);
internal void GB_debugger_test_write_watchpoint(GB_gameboy_t *gb, uint16_t addr, uint8_t value);
internal void GB_debugger_test_read_watchpoint(GB_gameboy_t *gb, uint16_t addr);
internal const GB_bank_symbol_t *GB_debugger_find_symbol(GB_gameboy_t *gb, uint16_t addr);
internal void GB_debugger_add_symbol(GB_gameboy_t *gb, uint16_t bank, uint16_t address, const char *symbol);
#endif /* GB_DISABLE_DEBUGGER */
#endif

46
Core/defs.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef defs_h
#define defs_h
#ifdef GB_INTERNAL
// "Keyword" definitions
#define likely(x) __builtin_expect((bool)(x), 1)
#define unlikely(x) __builtin_expect((bool)(x), 0)
#define internal __attribute__((visibility("internal")))
#if __clang__
#define unrolled _Pragma("unroll")
#elif __GNUC__ >= 8
#define unrolled _Pragma("GCC unroll 8")
#else
#define unrolled
#endif
#define unreachable() __builtin_unreachable();
#define nodefault default: unreachable()
#ifdef GB_BIG_ENDIAN
#define LE16(x) __builtin_bswap16(x)
#define LE32(x) __builtin_bswap32(x)
#define LE64(x) __builtin_bswap64(x)
#define BE16(x) (x)
#define BE32(x) (x)
#define BE64(x) (x)
#else
#define LE16(x) (x)
#define LE32(x) (x)
#define LE64(x) (x)
#define BE16(x) __builtin_bswap16(x)
#define BE32(x) __builtin_bswap32(x)
#define BE64(x) __builtin_bswap64(x)
#endif
#endif
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
#define __builtin_bswap16(x) ({ typeof(x) _x = (x); _x >> 8 | _x << 8; })
#endif
struct GB_gameboy_s;
typedef struct GB_gameboy_s GB_gameboy_t;
#endif

File diff suppressed because it is too large Load Diff

View File

@ -6,13 +6,14 @@
#include <stdint.h>
#ifdef GB_INTERNAL
void GB_display_run(GB_gameboy_t *gb, uint8_t cycles);
void GB_palette_changed(GB_gameboy_t *gb, bool background_palette, uint8_t index);
void GB_STAT_update(GB_gameboy_t *gb);
void GB_lcd_off(GB_gameboy_t *gb);
internal void GB_display_run(GB_gameboy_t *gb, unsigned cycles, bool force);
internal void GB_palette_changed(GB_gameboy_t *gb, bool background_palette, uint8_t index);
internal void GB_STAT_update(GB_gameboy_t *gb);
internal void GB_lcd_off(GB_gameboy_t *gb);
internal void GB_display_vblank(GB_gameboy_t *gb);
#define GB_display_sync(gb) GB_display_run(gb, 0, true)
enum {
GB_OBJECT_PRIORITY_UNDEFINED, // For save state compatibility
GB_OBJECT_PRIORITY_X,
GB_OBJECT_PRIORITY_INDEX,
};
@ -51,13 +52,21 @@ typedef enum {
GB_COLOR_CORRECTION_EMULATE_HARDWARE,
GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS,
GB_COLOR_CORRECTION_REDUCE_CONTRAST,
GB_COLOR_CORRECTION_LOW_CONTRAST,
} GB_color_correction_mode_t;
void GB_draw_tileset(GB_gameboy_t *gb, uint32_t *dest, GB_palette_type_t palette_type, uint8_t palette_index);
void GB_draw_tilemap(GB_gameboy_t *gb, uint32_t *dest, GB_palette_type_t palette_type, uint8_t palette_index, GB_map_type_t map_type, GB_tileset_type_t tileset_type);
uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *sprite_height);
uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *object_height);
uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color, bool for_border);
void GB_set_color_correction_mode(GB_gameboy_t *gb, GB_color_correction_mode_t mode);
void GB_set_light_temperature(GB_gameboy_t *gb, double temperature);
bool GB_is_odd_frame(GB_gameboy_t *gb);
void GB_set_object_rendering_disabled(GB_gameboy_t *gb, bool disabled);
void GB_set_background_rendering_disabled(GB_gameboy_t *gb, bool disabled);
bool GB_is_object_rendering_disabled(GB_gameboy_t *gb);
bool GB_is_background_rendering_disabled(GB_gameboy_t *gb);
#endif /* display_h */

744
Core/gb.c

File diff suppressed because it is too large Load Diff

295
Core/gb.h
View File

@ -3,9 +3,10 @@
#define typeof __typeof__
#include <stdbool.h>
#include <stdint.h>
#include <stdalign.h>
#include <time.h>
#include "gb_struct_def.h"
#include "defs.h"
#include "save_state.h"
#include "apu.h"
@ -24,26 +25,16 @@
#include "cheats.h"
#include "rumble.h"
#include "workboy.h"
#include "random.h"
#define GB_STRUCT_VERSION 13
#define GB_STRUCT_VERSION 14
#define GB_MODEL_FAMILY_MASK 0xF00
#define GB_MODEL_DMG_FAMILY 0x000
#define GB_MODEL_MGB_FAMILY 0x100
#define GB_MODEL_CGB_FAMILY 0x200
#define GB_MODEL_PAL_BIT 0x1000
#define GB_MODEL_NO_SFC_BIT 0x2000
#ifdef GB_INTERNAL
#if __clang__
#define UNROLL _Pragma("unroll")
#elif __GNUC__ >= 8
#define UNROLL _Pragma("GCC unroll 8")
#else
#define UNROLL
#endif
#endif
#define GB_MODEL_PAL_BIT 0x40
#define GB_MODEL_NO_SFC_BIT 0x80
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define GB_BIG_ENDIAN
@ -53,12 +44,9 @@
#error Unable to detect endianess
#endif
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
#define __builtin_bswap16(x) ({ typeof(x) _x = (x); _x >> 8 | _x << 8; })
#endif
typedef struct {
struct {
struct GB_color_s {
uint8_t r, g, b;
} colors[5];
} GB_palette_t;
@ -76,9 +64,24 @@ typedef union {
uint8_t days;
uint8_t high;
};
struct {
uint8_t seconds;
uint8_t minutes;
uint8_t hours:5;
uint8_t weekday:3;
uint8_t weeks;
} tpp1;
uint8_t data[5];
} GB_rtc_time_t;
typedef struct __attribute__((packed)) {
uint64_t last_rtc_second;
uint16_t minutes;
uint16_t days;
uint16_t alarm_minutes, alarm_days;
uint8_t alarm_enabled;
} GB_huc3_rtc_time_t;
typedef enum {
// GB_MODEL_DMG_0 = 0x000,
// GB_MODEL_DMG_A = 0x001,
@ -90,14 +93,14 @@ typedef enum {
GB_MODEL_SGB_NTSC_NO_SFC = GB_MODEL_SGB | GB_MODEL_NO_SFC_BIT,
GB_MODEL_SGB_NO_SFC = GB_MODEL_SGB_NTSC_NO_SFC,
GB_MODEL_SGB_PAL_NO_SFC = GB_MODEL_SGB | GB_MODEL_NO_SFC_BIT | GB_MODEL_PAL_BIT,
// GB_MODEL_MGB = 0x100,
GB_MODEL_MGB = 0x100,
GB_MODEL_SGB2 = 0x101,
GB_MODEL_SGB2_NO_SFC = GB_MODEL_SGB2 | GB_MODEL_NO_SFC_BIT,
// GB_MODEL_CGB_0 = 0x200,
// GB_MODEL_CGB_A = 0x201,
// GB_MODEL_CGB_B = 0x202,
GB_MODEL_CGB_0 = 0x200,
GB_MODEL_CGB_A = 0x201,
GB_MODEL_CGB_B = 0x202,
GB_MODEL_CGB_C = 0x203,
// GB_MODEL_CGB_D = 0x204,
GB_MODEL_CGB_D = 0x204,
GB_MODEL_CGB_E = 0x205,
GB_MODEL_AGB = 0x206,
} GB_model_t;
@ -108,6 +111,7 @@ enum {
GB_REGISTER_DE,
GB_REGISTER_HL,
GB_REGISTER_SP,
GB_REGISTER_PC,
GB_REGISTERS_16_BIT /* Count */
};
@ -186,10 +190,7 @@ enum {
GB_IO_OBP1 = 0x49, // Object Palette 1 Data (R/W) - Non CGB Mode Only
GB_IO_WY = 0x4a, // Window Y Position (R/W)
GB_IO_WX = 0x4b, // Window X Position minus 7 (R/W)
// Has some undocumented compatibility flags written at boot.
// Unfortunately it is not readable or writable after boot has finished, so research of this
// register is quite limited. The value written to this register, however, can be controlled
// in some cases.
// Controls DMG mode and PGB mode
GB_IO_KEY0 = 0x4c,
/* General CGB features */
@ -215,20 +216,19 @@ enum {
/* CGB Paletts */
GB_IO_BGPI = 0x68, // CGB Mode Only - Background Palette Index
GB_IO_BGPD = 0x69, // CGB Mode Only - Background Palette Data
GB_IO_OBPI = 0x6a, // CGB Mode Only - Sprite Palette Index
GB_IO_OBPD = 0x6b, // CGB Mode Only - Sprite Palette Data
GB_IO_OBPI = 0x6a, // CGB Mode Only - Object Palette Index
GB_IO_OBPD = 0x6b, // CGB Mode Only - Object Palette Data
GB_IO_OPRI = 0x6c, // Affects object priority (X based or index based)
/* Missing */
GB_IO_SVBK = 0x70, // CGB Mode Only - WRAM Bank
GB_IO_UNKNOWN2 = 0x72, // (00h) - Bit 0-7 (Read/Write)
GB_IO_UNKNOWN3 = 0x73, // (00h) - Bit 0-7 (Read/Write)
GB_IO_UNKNOWN4 = 0x74, // (00h) - Bit 0-7 (Read/Write) - CGB Mode Only
GB_IO_PSWX = 0x72, // X position of the palette switching window
GB_IO_PSWY = 0x73, // Y position of the palette switching window
GB_IO_PSW = 0x74, // Key combo to trigger the palette switching window
GB_IO_UNKNOWN5 = 0x75, // (8Fh) - Bit 4-6 (Read/Write)
GB_IO_PCM_12 = 0x76, // Channels 1 and 2 amplitudes
GB_IO_PCM_34 = 0x77, // Channels 3 and 4 amplitudes
GB_IO_UNKNOWN8 = 0x7F, // Unknown, write only
GB_IO_PCM12 = 0x76, // Channels 1 and 2 amplitudes
GB_IO_PCM34 = 0x77, // Channels 3 and 4 amplitudes
};
typedef enum {
@ -239,12 +239,12 @@ typedef enum {
} GB_log_attributes;
typedef enum {
GB_BOOT_ROM_DMG0,
GB_BOOT_ROM_DMG_0,
GB_BOOT_ROM_DMG,
GB_BOOT_ROM_MGB,
GB_BOOT_ROM_SGB,
GB_BOOT_ROM_SGB2,
GB_BOOT_ROM_CGB0,
GB_BOOT_ROM_CGB_0,
GB_BOOT_ROM_CGB,
GB_BOOT_ROM_AGB,
} GB_boot_rom_t;
@ -255,7 +255,6 @@ typedef enum {
#define SGB_NTSC_FREQUENCY (21477272 / 5)
#define SGB_PAL_FREQUENCY (21281370 / 5)
#define DIV_CYCLES (0x100)
#define INTERNAL_DIV_CYCLES (0x40000)
#if !defined(MIN)
#define MIN(A, B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
@ -281,14 +280,17 @@ typedef void (*GB_icd_hreset_callback_t)(GB_gameboy_t *gb);
typedef void (*GB_icd_vreset_callback_t)(GB_gameboy_t *gb);
typedef void (*GB_boot_rom_load_callback_t)(GB_gameboy_t *gb, GB_boot_rom_t type);
typedef void (*GB_execution_callback_t)(GB_gameboy_t *gb, uint16_t address, uint8_t opcode);
typedef void (*GB_lcd_line_callback_t)(GB_gameboy_t *gb, uint8_t line);
struct GB_breakpoint_s;
struct GB_watchpoint_s;
typedef struct {
uint8_t pixel; // Color, 0-3
uint8_t palette; // Palette, 0 - 7 (CGB); 0-1 in DMG (or just 0 for BG)
uint8_t priority; // Sprite priority 0 in DMG, OAM index in CGB
bool bg_priority; // For sprite FIFO the BG priority bit. For the BG FIFO  the CGB attributes priority bit
uint8_t priority; // Object priority 0 in DMG, OAM index in CGB
bool bg_priority; // For object FIFO the BG priority bit. For the BG FIFO  the CGB attributes priority bit
} GB_fifo_item_t;
#define GB_FIFO_LENGTH 16
@ -298,6 +300,55 @@ typedef struct {
uint8_t write_end;
} GB_fifo_t;
typedef struct {
uint32_t magic;
uint8_t track_count;
uint8_t first_track;
uint16_t load_address;
uint16_t init_address;
uint16_t play_address;
uint16_t sp;
uint8_t TMA;
uint8_t TAC;
char title[32];
char author[32];
char copyright[32];
} GB_gbs_header_t;
typedef struct {
uint8_t track_count;
uint8_t first_track;
char title[33];
char author[33];
char copyright[33];
} GB_gbs_info_t;
/* Duplicated so it can remain anonymous in GB_gameboy_t */
typedef union {
uint16_t registers[GB_REGISTERS_16_BIT];
struct {
uint16_t af,
bc,
de,
hl,
sp,
pc;
};
struct {
#ifdef GB_BIG_ENDIAN
uint8_t a, f,
b, c,
d, e,
h, l;
#else
uint8_t f, a,
c, b,
e, d,
l, h;
#endif
};
} GB_registers_t;
/* When state saving, each section is dumped independently of other sections.
This allows adding data to the end of the section without worrying about future compatibility.
Some other changes might be "safe" as well.
@ -321,31 +372,30 @@ struct GB_gameboy_internal_s {
GB_SECTION(core_state,
/* Registers */
uint16_t pc;
union {
uint16_t registers[GB_REGISTERS_16_BIT];
struct {
uint16_t af,
bc,
de,
hl,
sp;
};
struct {
union {
uint16_t registers[GB_REGISTERS_16_BIT];
struct {
uint16_t af,
bc,
de,
hl,
sp,
pc;
};
struct {
#ifdef GB_BIG_ENDIAN
uint8_t a, f,
b, c,
d, e,
h, l;
uint8_t a, f,
b, c,
d, e,
h, l;
#else
uint8_t f, a,
c, b,
e, d,
l, h;
uint8_t f, a,
c, b,
e, d,
l, h;
#endif
};
};
};
};
uint8_t ime;
uint8_t interrupt_enable;
uint8_t cgb_ram_bank;
@ -370,6 +420,7 @@ struct GB_gameboy_internal_s {
int32_t ir_sensor;
bool effective_ir_input;
uint16_t address_bus;
);
/* DMA and HDMA */
@ -385,6 +436,8 @@ struct GB_gameboy_internal_s {
uint16_t dma_current_src;
int16_t dma_cycles;
bool is_dma_restarting;
uint8_t dma_and_pattern;
bool dma_skip_write;
uint8_t last_opcode_read; /* Required to emulte HDMA reads from Exxx */
bool hdma_starting;
);
@ -409,6 +462,7 @@ struct GB_gameboy_internal_s {
struct {
uint8_t rom_bank:8;
uint8_t ram_bank:3;
bool rtc_mapped:1;
} mbc3;
struct {
@ -416,6 +470,22 @@ struct GB_gameboy_internal_s {
uint8_t rom_bank_high:1;
uint8_t ram_bank:4;
} mbc5;
struct {
uint8_t rom_bank;
uint16_t x_latch;
uint16_t y_latch;
bool latch_ready:1;
bool eeprom_do:1;
bool eeprom_di:1;
bool eeprom_clk:1;
bool eeprom_cs:1;
uint16_t eeprom_command:11;
uint16_t read_bits;
uint8_t bits_countdown:5;
bool secondary_ram_enable:1;
bool eeprom_write_enabled:1;
} mbc7;
struct {
uint8_t bank_low:6;
@ -428,23 +498,27 @@ struct GB_gameboy_internal_s {
uint8_t rom_bank:7;
uint8_t padding:1;
uint8_t ram_bank:4;
uint8_t mode;
uint8_t access_index;
uint16_t minutes, days;
uint16_t alarm_minutes, alarm_days;
bool alarm_enabled;
uint8_t read;
uint8_t access_flags;
} huc3;
struct {
uint16_t rom_bank;
uint8_t ram_bank;
uint8_t mode;
} tpp1;
};
uint16_t mbc_rom0_bank; /* For some MBC1 wirings. */
bool camera_registers_mapped;
uint8_t camera_registers[0x36];
bool rumble_state;
uint8_t rumble_strength;
bool cart_ir;
// TODO: move to huc3/mbc3 struct when breaking save compat
uint8_t huc3_mode;
uint8_t huc3_access_index;
uint16_t huc3_minutes, huc3_days;
uint16_t huc3_alarm_minutes, huc3_alarm_days;
bool huc3_alarm_enabled;
uint8_t huc3_read;
uint8_t huc3_access_flags;
bool mbc3_rtc_mapped;
);
@ -464,6 +538,14 @@ struct GB_gameboy_internal_s {
uint16_t serial_length;
uint8_t double_speed_alignment;
uint8_t serial_count;
int32_t speed_switch_halt_countdown;
uint8_t speed_switch_countdown; // To compensate for the lack of pipeline emulation
uint8_t speed_switch_freeze; // Solely for realigning the PPU, should be removed when the odd modes are implemented
/* For timing of the vblank callback */
uint32_t cycles_since_vblank_callback;
bool lcd_disabled_outside_of_vblank;
int32_t allowed_pending_cycles;
uint16_t mode3_batching_length;
);
/* APU */
@ -475,16 +557,17 @@ struct GB_gameboy_internal_s {
GB_SECTION(rtc,
GB_rtc_time_t rtc_real, rtc_latched;
uint64_t last_rtc_second;
bool rtc_latch;
uint32_t rtc_cycles;
uint8_t tpp1_mr4;
);
/* Video Display */
GB_SECTION(video,
uint32_t vram_size; // Different between CGB and DMG
uint8_t cgb_vram_bank;
bool cgb_vram_bank;
uint8_t oam[0xA0];
uint8_t background_palettes_data[0x40];
uint8_t sprite_palettes_data[0x40];
uint8_t object_palettes_data[0x40];
uint8_t position_in_line;
bool stat_interrupt_line;
uint8_t effective_scx;
@ -509,7 +592,6 @@ struct GB_gameboy_internal_s {
uint8_t current_line;
uint16_t ly_for_comparison;
GB_fifo_t bg_fifo, oam_fifo;
uint8_t fetcher_x;
uint8_t fetcher_y;
uint16_t cycles_for_line;
uint8_t current_tile;
@ -524,12 +606,11 @@ struct GB_gameboy_internal_s {
uint8_t n_visible_objs;
uint8_t oam_search_index;
uint8_t accessed_oam_row;
uint8_t extra_penalty_for_sprite_at_0;
uint8_t extra_penalty_for_object_at_0;
uint8_t mode_for_interrupt;
bool lyc_interrupt_line;
bool cgb_palettes_blocked;
uint8_t current_lcd_line; // The LCD can go out of sync since the vsync signal is skipped in some cases.
uint32_t cycles_in_stop_mode;
uint8_t object_priority;
bool oam_ppu_blocked;
bool vram_ppu_blocked;
@ -570,19 +651,28 @@ struct GB_gameboy_internal_s {
/* I/O */
uint32_t *screen;
uint32_t background_palettes_rgb[0x20];
uint32_t sprite_palettes_rgb[0x20];
uint32_t object_palettes_rgb[0x20];
const GB_palette_t *dmg_palette;
GB_color_correction_mode_t color_correction_mode;
double light_temperature;
bool keys[4][GB_KEY_MAX];
double accelerometer_x, accelerometer_y;
GB_border_mode_t border_mode;
GB_sgb_border_t borrowed_border;
bool tried_loading_sgb_border;
bool has_sgb_border;
bool objects_disabled;
bool background_disabled;
bool joyp_accessed;
bool illegal_inputs_allowed;
/* Timing */
uint64_t last_sync;
uint64_t cycles_since_last_sync; // In 8MHz units
GB_rtc_mode_t rtc_mode;
uint32_t rtc_second_length;
uint32_t clock_rate;
uint32_t unmultiplied_clock_rate;
/* Audio */
GB_apu_output_t apu_output;
@ -606,10 +696,13 @@ struct GB_gameboy_internal_s {
GB_icd_vreset_callback_t icd_hreset_callback;
GB_icd_vreset_callback_t icd_vreset_callback;
GB_read_memory_callback_t read_memory_callback;
GB_write_memory_callback_t write_memory_callback;
GB_boot_rom_load_callback_t boot_rom_load_callback;
GB_print_image_callback_t printer_callback;
GB_workboy_set_time_callback workboy_set_time_callback;
GB_workboy_get_time_callback workboy_get_time_callback;
GB_execution_callback_t execution_callback;
GB_lcd_line_callback_t lcd_line_callback;
/*** Debugger ***/
volatile bool debug_stopped, debug_disable;
@ -646,6 +739,7 @@ struct GB_gameboy_internal_s {
/* Ticks command */
uint64_t debugger_ticks;
uint64_t absolute_debugger_ticks;
/* Undo */
uint8_t *undo_state;
@ -689,12 +783,15 @@ struct GB_gameboy_internal_s {
/* Temporary state */
bool wx_just_changed;
bool tile_sel_glitch;
bool disable_oam_corruption; // For safe memory reads
GB_gbs_header_t gbs_header;
);
};
#ifndef GB_INTERNAL
struct GB_gameboy_s {
char __internal[sizeof(struct GB_gameboy_internal_s)];
alignas(struct GB_gameboy_internal_s) uint8_t __internal[sizeof(struct GB_gameboy_internal_s)];
};
#endif
@ -707,7 +804,8 @@ __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
void GB_init(GB_gameboy_t *gb, GB_model_t model);
bool GB_is_inited(GB_gameboy_t *gb);
bool GB_is_cgb(GB_gameboy_t *gb);
bool GB_is_cgb(const GB_gameboy_t *gb);
bool GB_is_cgb_in_cgb_mode(GB_gameboy_t *gb);
bool GB_is_sgb(GB_gameboy_t *gb); // Returns true if the model is SGB or SGB2
bool GB_is_hle_sgb(GB_gameboy_t *gb); // Returns true if the model is SGB or SGB2 and the SFC/SNES side is HLE'd
GB_model_t GB_get_model(GB_gameboy_t *gb);
@ -737,18 +835,20 @@ typedef enum {
/* Returns a mutable pointer to various hardware memories. If that memory is banked, the current bank
is returned at *bank, even if only a portion of the memory is banked. */
void *GB_get_direct_access(GB_gameboy_t *gb, GB_direct_access_t access, size_t *size, uint16_t *bank);
GB_registers_t *GB_get_registers(GB_gameboy_t *gb);
void *GB_get_user_data(GB_gameboy_t *gb);
void GB_set_user_data(GB_gameboy_t *gb, void *data);
int GB_load_boot_rom(GB_gameboy_t *gb, const char *path);
void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, size_t size);
int GB_load_rom(GB_gameboy_t *gb, const char *path);
void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size);
int GB_load_isx(GB_gameboy_t *gb, const char *path);
int GB_load_gbs_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size, GB_gbs_info_t *info);
int GB_load_gbs(GB_gameboy_t *gb, const char *path, GB_gbs_info_t *info);
void GB_gbs_switch_track(GB_gameboy_t *gb, uint8_t track);
int GB_save_battery_size(GB_gameboy_t *gb);
int GB_save_battery_to_buffer(GB_gameboy_t *gb, uint8_t *buffer, size_t size);
int GB_save_battery(GB_gameboy_t *gb, const char *path);
@ -778,7 +878,11 @@ void GB_set_update_input_hint_callback(GB_gameboy_t *gb, GB_update_input_hint_ca
/* Called when a new boot ROM is needed. The callback should call GB_load_boot_rom or GB_load_boot_rom_from_buffer */
void GB_set_boot_rom_load_callback(GB_gameboy_t *gb, GB_boot_rom_load_callback_t callback);
void GB_set_execution_callback(GB_gameboy_t *gb, GB_execution_callback_t callback);
void GB_set_lcd_line_callback(GB_gameboy_t *gb, GB_lcd_line_callback_t callback);
void GB_set_palette(GB_gameboy_t *gb, const GB_palette_t *palette);
const GB_palette_t *GB_get_palette(GB_gameboy_t *gb);
/* These APIs are used when using internal clock */
void GB_set_serial_transfer_bit_start_callback(GB_gameboy_t *gb, GB_serial_transfer_bit_start_callback_t callback);
@ -792,6 +896,12 @@ void GB_disconnect_serial(GB_gameboy_t *gb);
/* For cartridges with an alarm clock */
unsigned GB_time_to_alarm(GB_gameboy_t *gb); // 0 if no alarm
/* For cartridges motion controls */
bool GB_has_accelerometer(GB_gameboy_t *gb);
// In units of g (gravity's acceleration).
// Values within ±4 recommended
void GB_set_accelerometer_values(GB_gameboy_t *gb, double x, double y);
/* For integration with SFC/SNES emulators */
void GB_set_joyp_write_callback(GB_gameboy_t *gb, GB_joyp_write_callback_t callback);
@ -800,6 +910,7 @@ void GB_set_icd_hreset_callback(GB_gameboy_t *gb, GB_icd_hreset_callback_t callb
void GB_set_icd_vreset_callback(GB_gameboy_t *gb, GB_icd_vreset_callback_t callback);
uint32_t GB_get_clock_rate(GB_gameboy_t *gb);
uint32_t GB_get_unmultiplied_clock_rate(GB_gameboy_t *gb);
void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier);
unsigned GB_get_screen_width(GB_gameboy_t *gb);
@ -807,4 +918,14 @@ unsigned GB_get_screen_height(GB_gameboy_t *gb);
double GB_get_usual_frame_rate(GB_gameboy_t *gb);
unsigned GB_get_player_count(GB_gameboy_t *gb);
/* Handy ROM info APIs */
// `title` must be at least 17 bytes in size
void GB_get_rom_title(GB_gameboy_t *gb, char *title);
uint32_t GB_get_rom_crc32(GB_gameboy_t *gb);
#ifdef GB_INTERNAL
internal void GB_borrow_sgb_border(GB_gameboy_t *gb);
internal void GB_update_clock_rate(GB_gameboy_t *gb);
#endif
#endif /* GB_h */

View File

@ -1,5 +0,0 @@
#ifndef gb_struct_def_h
#define gb_struct_def_h
struct GB_gameboy_s;
typedef struct GB_gameboy_s GB_gameboy_t;
#endif

View File

@ -0,0 +1,477 @@
static const uint16_t palette[] = {
0x0000, 0x0000, 0x0011, 0x001A, 0x39CE, 0x6B5A, 0x739C, 0x5265,
0x3DC5, 0x2924, 0x18A4, 0x20E6, 0x2D49, 0x1484, 0x5694, 0x20EC,
};
static const uint16_t tilemap[] = {
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x0010, 0x0011, 0x0012, 0x0012, 0x0012, 0x0012, 0x0012,
0x0012, 0x0012, 0x0012, 0x0012, 0x0012, 0x0012, 0x0012, 0x0012,
0x0012, 0x0012, 0x0012, 0x0012, 0x0012, 0x0012, 0x0012, 0x0012,
0x0012, 0x0012, 0x0012, 0x0012, 0x0012, 0x4011, 0x4010, 0x000F,
0x000F, 0x0013, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014,
0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014,
0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014,
0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x4013, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0016, 0x0017, 0x0017,
0x0017, 0x0017, 0x0017, 0x0017, 0x0017, 0x0017, 0x0017, 0x0017,
0x0017, 0x0017, 0x0017, 0x0017, 0x0017, 0x0017, 0x0017, 0x0017,
0x0017, 0x0017, 0x4016, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0019, 0x001A, 0x4019, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x8019, 0x001B, 0xC019, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x0018, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4018, 0x0014, 0x0014, 0x0014, 0x4015, 0x000F,
0x000F, 0x0015, 0x0014, 0x0014, 0x0014, 0x001C, 0x001D, 0x001D,
0x001D, 0x001D, 0x001D, 0x001D, 0x001D, 0x001D, 0x001D, 0x001D,
0x001D, 0x001D, 0x001D, 0x001D, 0x001D, 0x001D, 0x001D, 0x001D,
0x001D, 0x001D, 0x401C, 0x0014, 0x0014, 0x0014, 0x001E, 0x000F,
0x000F, 0x0015, 0x0014, 0x001F, 0x0020, 0x0021, 0x0022, 0x0023,
0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B,
0x002C, 0x002D, 0x002E, 0x002F, 0x0014, 0x0014, 0x0014, 0x0014,
0x0014, 0x0014, 0x0014, 0x0014, 0x0014, 0x0030, 0x0031, 0x000F,
0x000F, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038,
0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, 0x0040,
0x0041, 0x0042, 0x0043, 0x0044, 0x0014, 0x0014, 0x0014, 0x0014,
0x0014, 0x0014, 0x0014, 0x0014, 0x0045, 0x0046, 0x000F, 0x000F,
0x000F, 0x0047, 0x0048, 0x0049, 0x0049, 0x0049, 0x0049, 0x0049,
0x0049, 0x0049, 0x0049, 0x0049, 0x0049, 0x0049, 0x0049, 0x0049,
0x0049, 0x0049, 0x0049, 0x0049, 0x0049, 0x0049, 0x0049, 0x0049,
0x0049, 0x0049, 0x004A, 0x004B, 0x004C, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F, 0x000F,
};
const uint8_t tiles[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x3D,
0x42, 0x7F, 0x81, 0xFF, 0x01, 0xFD, 0x01,
0xFD, 0x01, 0xFF, 0x03, 0xFF, 0x03, 0xFF,
0xFF, 0xBC, 0x7F, 0xFD, 0xFE, 0xFD, 0xFE,
0xFD, 0xFE, 0xFD, 0xFE, 0xFF, 0xFC, 0xFF,
0xFC, 0xFF, 0x00, 0xBF, 0x41, 0xFE, 0xC0,
0xBF, 0xC1, 0xFF, 0x81, 0x7D, 0x03, 0x7F,
0x01, 0x7F, 0x01, 0xFF, 0xFF, 0x3E, 0xFF,
0xBE, 0x7F, 0xBF, 0x7E, 0xFF, 0x7E, 0x7D,
0xFE, 0x7D, 0xFE, 0x7D, 0xFE, 0xFF, 0x00,
0xFF, 0x00, 0xBF, 0x83, 0xBF, 0x87, 0xFC,
0x8D, 0xED, 0x8E, 0xDB, 0xF8, 0xBF, 0xD8,
0xFF, 0xFF, 0x3E, 0xFF, 0xBB, 0x7C, 0xB7,
0x78, 0xAC, 0x73, 0xAD, 0x73, 0x9B, 0x67,
0x9B, 0x67, 0xFF, 0x00, 0xB7, 0x08, 0xFF,
0xF8, 0x3F, 0x38, 0xFF, 0x08, 0xFE, 0x01,
0x87, 0x00, 0xFB, 0x78, 0xFF, 0xFF, 0x07,
0xFF, 0xFB, 0x07, 0x3B, 0xC7, 0xE7, 0xFF,
0xFE, 0xFF, 0x82, 0xFF, 0xFA, 0x87, 0xFF,
0x00, 0xFE, 0x81, 0x5F, 0x40, 0xDE, 0xC0,
0xFE, 0xC0, 0xE0, 0xDE, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0x1E, 0xFF, 0x5E, 0xBF,
0xDE, 0x3F, 0xDE, 0x3F, 0xC0, 0x3F, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xCF, 0x30,
0xDF, 0xEF, 0xFF, 0xCF, 0xFF, 0xC1, 0xBD,
0x81, 0xBD, 0x81, 0xFF, 0x83, 0xFF, 0xFF,
0x00, 0xFF, 0xCF, 0x30, 0xEF, 0x30, 0xFD,
0x3E, 0xBD, 0x7E, 0xBD, 0x7E, 0xBF, 0x7C,
0xFF, 0x00, 0xFF, 0x08, 0xF7, 0xF0, 0xFF,
0xF0, 0xBF, 0xC0, 0xFF, 0x80, 0x7F, 0x00,
0x7F, 0x00, 0xFF, 0xFF, 0x07, 0xFF, 0xF7,
0x0F, 0xF7, 0x0F, 0xBF, 0x7F, 0xFF, 0x7F,
0x7F, 0xFF, 0x7F, 0xFF, 0xFB, 0x07, 0xFF,
0x03, 0xFF, 0x03, 0xFB, 0x03, 0xFB, 0x03,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFB,
0xFC, 0xFB, 0xFC, 0xFB, 0xFC, 0xFB, 0xFC,
0xFB, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFD, 0x01, 0xFD, 0x81, 0xFF, 0x0B,
0xF7, 0xF3, 0xFB, 0xF7, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0x7D, 0xFE, 0x7D, 0xFE,
0x07, 0xFC, 0xF7, 0x0C, 0xF3, 0x0C, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x98,
0x7B, 0x38, 0x7C, 0x1D, 0xFF, 0x0F, 0xFB,
0x0B, 0xFD, 0x03, 0xFF, 0x00, 0xFF, 0x00,
0xDB, 0x67, 0x5B, 0xE7, 0x7C, 0xE3, 0x6F,
0xF0, 0x73, 0xFC, 0xFC, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0x9E, 0x18, 0xFE, 0x3C, 0x5A,
0xDC, 0xFF, 0xF9, 0xED, 0xE3, 0xBF, 0xC0,
0xFF, 0x00, 0xFF, 0x00, 0x9A, 0xE7, 0xDA,
0xE7, 0x1A, 0xE7, 0xFF, 0x06, 0xE5, 0x1E,
0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
0xFD, 0xBF, 0x81, 0xBF, 0x81, 0xBD, 0x81,
0xFD, 0x81, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xC1, 0x3E, 0xBD, 0x7E, 0xBD, 0x7E,
0xBD, 0x7E, 0xBD, 0x7E, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x83, 0xBB, 0xC7,
0xFF, 0x83, 0xFF, 0x83, 0x7B, 0x03, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xBF, 0x7C,
0xBB, 0x7C, 0xFB, 0x7C, 0xFB, 0x7C, 0x7B,
0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x80, 0x7F,
0x80, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F,
0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFE, 0x00, 0xF9, 0x00,
0xF7, 0x00, 0xEE, 0x00, 0xDD, 0x04, 0xDF,
0x04, 0xBF, 0x08, 0xFF, 0x00, 0xFF, 0x01,
0xFF, 0x07, 0xFF, 0x0F, 0xFF, 0x1F, 0xFB,
0x3F, 0xFB, 0x3F, 0xF7, 0x7F, 0x80, 0x00,
0x7F, 0x00, 0xFF, 0x00, 0x80, 0x00, 0x7F,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBF,
0x08, 0xFF, 0x10, 0xFF, 0x10, 0xFF, 0x10,
0xFF, 0x10, 0xFF, 0x10, 0xFF, 0x10, 0xFF,
0x10, 0xF7, 0x7F, 0xEF, 0x7F, 0xEF, 0x7F,
0xEF, 0x7F, 0xEF, 0x7F, 0xEF, 0x7F, 0xEF,
0x7F, 0xEF, 0x7F, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x10, 0xFF, 0x10, 0xFF, 0x10, 0xFF,
0x10, 0xFF, 0x10, 0xFF, 0x10, 0xFF, 0x10,
0xFF, 0x10, 0xEF, 0x7F, 0xEF, 0x7F, 0xEF,
0x7F, 0xEF, 0x7F, 0xEF, 0x7F, 0xEF, 0x7F,
0xEF, 0x7F, 0xEF, 0x7F, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
0xFE, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
0xFE, 0xFE, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x01,
0xFF, 0x01, 0xFF, 0x01, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xC3, 0x7E,
0xBD, 0xFF, 0x66, 0xFF, 0x7E, 0xFF, 0x7E,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xC3, 0xFF, 0x81, 0xC3, 0x18, 0x81, 0x00,
0x00, 0x00, 0x00, 0x7E, 0xFF, 0x3C, 0xFF,
0x00, 0x7E, 0x81, 0xBD, 0xC3, 0x42, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00,
0x00, 0x00, 0x81, 0x00, 0xC3, 0x81, 0xFF,
0xC3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x01, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0x08, 0xFD, 0x12, 0xFD, 0x12,
0xFD, 0x12, 0xFD, 0x12, 0xFB, 0x24, 0xFB,
0x24, 0xFB, 0x24, 0xF7, 0xFE, 0xEF, 0xFC,
0xEF, 0xFC, 0xEF, 0xFC, 0xEF, 0xFC, 0xDF,
0xF8, 0xDF, 0xF8, 0xDF, 0xF8, 0xFF, 0x00,
0xF0, 0x1E, 0xC0, 0x3F, 0x8D, 0x72, 0x0E,
0xF3, 0x8F, 0xF0, 0x01, 0xFC, 0xA0, 0x1E,
0xFF, 0xFF, 0xEF, 0xFF, 0xFF, 0xE0, 0xFF,
0xC0, 0x7C, 0x9F, 0x7F, 0x9F, 0x7F, 0x87,
0xFF, 0xC0, 0xFF, 0x00, 0xFC, 0x00, 0x78,
0x87, 0x78, 0x87, 0xF0, 0x07, 0xF8, 0x07,
0xE2, 0x0F, 0xE2, 0x1C, 0xFF, 0xFF, 0xFF,
0xFE, 0xFB, 0xFC, 0x7F, 0xFC, 0xFF, 0xF8,
0xFF, 0xF2, 0xFD, 0xF3, 0xFF, 0xE6, 0xFF,
0x00, 0x7C, 0x02, 0xFC, 0x01, 0x3C, 0xC3,
0x3C, 0x83, 0x3E, 0xC1, 0x3C, 0xC3, 0x18,
0xC7, 0xFF, 0xFF, 0xFD, 0xFE, 0xFF, 0x7C,
0xBF, 0x7E, 0xFF, 0x3E, 0xFF, 0x7C, 0xFF,
0x3C, 0xFB, 0x3C, 0xFF, 0x00, 0x1E, 0x01,
0x1E, 0xE1, 0x1E, 0xE3, 0x1C, 0xF3, 0x0C,
0xE7, 0x08, 0xF7, 0x19, 0x6F, 0xFF, 0xFF,
0xFE, 0x3F, 0xFF, 0x3F, 0xFD, 0x1E, 0xEF,
0x1E, 0xFB, 0x8C, 0xFF, 0xDD, 0xF6, 0x49,
0xFF, 0x00, 0x18, 0x14, 0x08, 0xE3, 0x08,
0xE3, 0x18, 0xF7, 0x1D, 0xE2, 0x18, 0xE7,
0xB8, 0x47, 0xFF, 0xFF, 0xEB, 0x1C, 0xFF,
0x0C, 0xFF, 0x18, 0xEF, 0x1C, 0xFF, 0x98,
0xFF, 0x98, 0xFF, 0x18, 0xFF, 0x00, 0x06,
0x05, 0x02, 0xF8, 0x02, 0xF9, 0xFE, 0x01,
0xFF, 0x00, 0x06, 0xF9, 0x04, 0xF3, 0xFF,
0xFF, 0xFA, 0x07, 0xFF, 0x02, 0xFF, 0x07,
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0x0E, 0xFD,
0x06, 0xFF, 0x00, 0x03, 0x00, 0x01, 0xFF,
0x30, 0xCF, 0x70, 0x8F, 0x30, 0xC6, 0x21,
0xDC, 0x01, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F,
0xFE, 0x01, 0xFF, 0x01, 0xF7, 0x39, 0xFF,
0x79, 0xFF, 0x01, 0xFF, 0x03, 0xFF, 0x00,
0xF8, 0x01, 0xF0, 0x1F, 0xE1, 0x3E, 0x83,
0x78, 0x87, 0x30, 0xCF, 0x30, 0x8F, 0x70,
0xFF, 0xFF, 0xFF, 0xFD, 0xEF, 0xF8, 0xDF,
0xE0, 0xBF, 0xC3, 0xFF, 0x87, 0xFF, 0x8F,
0xFF, 0x9F, 0xFF, 0x00, 0x3C, 0xA0, 0x0C,
0xE1, 0x07, 0xF0, 0x86, 0x38, 0xC7, 0x3C,
0xC3, 0x18, 0xC7, 0x3C, 0xFF, 0xFF, 0xDF,
0xBE, 0xFF, 0x0C, 0xFF, 0x06, 0xFF, 0x87,
0xFB, 0xE7, 0xFF, 0xC7, 0xFB, 0xE7, 0xFF,
0x00, 0x7C, 0x40, 0x78, 0x83, 0x39, 0xEE,
0x19, 0xE7, 0x81, 0x7C, 0x03, 0x78, 0xC7,
0x38, 0xFF, 0xFF, 0xBF, 0x7E, 0xFF, 0x38,
0xD7, 0x38, 0xFE, 0x31, 0xFF, 0x13, 0xFF,
0x83, 0xFF, 0x8F, 0xFF, 0x00, 0x3C, 0x43,
0x7C, 0x83, 0xFC, 0x03, 0xFC, 0x03, 0xFC,
0x03, 0xFD, 0x02, 0xFC, 0x03, 0xFF, 0xFF,
0xBF, 0x7C, 0xFF, 0xFC, 0xFF, 0xFC, 0xFF,
0xFC, 0xFF, 0xFC, 0xFF, 0xFC, 0xFF, 0xFD,
0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x04,
0xA3, 0xFD, 0xB6, 0x8C, 0x3A, 0x8B, 0x7C,
0x99, 0x62, 0xFF, 0xFF, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0xF8, 0x4B, 0xFC, 0xF7, 0xCC,
0xF3, 0xCD, 0xFF, 0xCB, 0xFF, 0x00, 0x00,
0xFF, 0x00, 0xFF, 0x90, 0x3F, 0xD8, 0x46,
0x09, 0xF6, 0x0D, 0xF1, 0x12, 0xF4, 0xFF,
0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xCF, 0x78,
0xBF, 0xD9, 0x7F, 0x9F, 0xFE, 0x9B, 0xEF,
0x9B, 0xFF, 0x00, 0x00, 0xFC, 0x02, 0xFC,
0x46, 0x59, 0x13, 0xAC, 0x82, 0x68, 0x07,
0xFC, 0x14, 0xE8, 0xFF, 0xFF, 0xFF, 0x03,
0xFF, 0x02, 0xBF, 0x73, 0x5F, 0xB6, 0xFF,
0x23, 0xFB, 0x07, 0xFF, 0x26, 0xFF, 0x00,
0x00, 0xFF, 0x00, 0xFF, 0x03, 0x9F, 0x21,
0x55, 0x48, 0xB7, 0x8F, 0x60, 0x80, 0x6F,
0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFC,
0x27, 0xBA, 0xCD, 0x7F, 0x9D, 0xFF, 0x8F,
0xF7, 0xD8, 0xFF, 0x00, 0x00, 0xFF, 0x0C,
0xF3, 0x18, 0xF3, 0xD8, 0x2F, 0x90, 0x67,
0xB0, 0x4F, 0x10, 0xEF, 0xFF, 0xFF, 0xFF,
0x08, 0xFF, 0x18, 0xEF, 0xBC, 0xF7, 0xBC,
0xFF, 0xD0, 0xFF, 0xD8, 0xFF, 0x30, 0xFF,
0x00, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80,
0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, 0x7F,
0x80, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0x7F,
0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF,
0x7F, 0xFF, 0x7F, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x01, 0xFF,
0x02, 0xFF, 0x04, 0xFF, 0x08, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
0xFF, 0xFD, 0xFF, 0xFB, 0xFF, 0xF7, 0xFF,
0xF7, 0x48, 0xF7, 0x48, 0xEF, 0x90, 0xEF,
0x90, 0xDF, 0x20, 0xBF, 0x40, 0xBF, 0x40,
0x7F, 0x80, 0xBF, 0xF0, 0xBF, 0xF0, 0x7F,
0xE0, 0x7F, 0xE0, 0xFF, 0xC0, 0xFF, 0x80,
0xFF, 0x80, 0xFF, 0x00, 0xFF, 0x10, 0xFF,
0x10, 0xFF, 0x10, 0xFF, 0x10, 0xFF, 0x10,
0xFF, 0x10, 0xFF, 0x10, 0xBF, 0x48, 0xEF,
0x7F, 0xEF, 0x7F, 0xEF, 0x7F, 0xEF, 0x7F,
0xEF, 0x7F, 0xEF, 0x7F, 0xEF, 0x7F, 0xF7,
0x3F, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFE, 0x00, 0xFE, 0x01, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0x0F,
0xF8, 0x07, 0x00, 0x57, 0x01, 0xFF, 0x05,
0xF8, 0x87, 0x48, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0xF8, 0xFF, 0xFC, 0xEF, 0x99, 0xFE,
0x01, 0xFF, 0x83, 0xB7, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xC0, 0x3F, 0x80, 0x7F, 0x80,
0x7F, 0x0F, 0x70, 0x8F, 0x70, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xE7, 0xBF,
0xC0, 0xFF, 0xCF, 0xFF, 0x9F, 0xEF, 0x1F,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x18,
0xE3, 0x18, 0xE3, 0x98, 0x77, 0x08, 0x67,
0x1D, 0xE2, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x3C, 0xFF, 0x18, 0xEF, 0x1C,
0xFF, 0x0C, 0x7F, 0x88, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0x01, 0x3E, 0xC2, 0xFF,
0xC2, 0x3C, 0xE2, 0x18, 0xC6, 0x39, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x8B,
0x3C, 0xC3, 0xFF, 0xC7, 0xFF, 0xC6, 0xFF,
0xEF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x18, 0xEF, 0x10, 0xE6, 0x10, 0xC6, 0x30,
0xEF, 0x30, 0xCF, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xF7, 0x39, 0xFF, 0x39, 0xFF,
0x10, 0xDF, 0x38, 0xFF, 0x38, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x0C, 0x09, 0xFC,
0x01, 0x0C, 0x0B, 0x04, 0xFB, 0x06, 0xF1,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xF7,
0x0E, 0xFF, 0xFC, 0xF7, 0x0E, 0xFF, 0x0E,
0xFF, 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0xCE, 0x70, 0xCF, 0x70, 0xFE,
0x01, 0xFE, 0x0B, 0xF0, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x69, 0xB7, 0x79,
0x8F, 0x79, 0xFF, 0x03, 0xFF, 0x03, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x8F, 0x70,
0x87, 0x78, 0x88, 0x72, 0x80, 0x7F, 0xC0,
0x3F, 0xFC, 0x05, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x9F, 0xF7, 0x8F, 0xFD, 0xC7, 0xBF,
0xC0, 0xDF, 0xF0, 0xFA, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xE7, 0x18, 0x87, 0x38, 0x17,
0xE8, 0x1F, 0xF0, 0x3F, 0xC0, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xC7, 0xFF,
0x8F, 0xF7, 0x8F, 0xEF, 0x1F, 0xFF, 0x7F,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x87,
0x78, 0x8F, 0x70, 0xDF, 0x20, 0x8F, 0x70,
0x8F, 0x70, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xF7, 0xCF, 0xFF, 0xCF, 0xFF, 0x8F,
0xFF, 0x9F, 0xFF, 0x9F, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFC, 0x02, 0xFD, 0x03,
0xFD, 0x02, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFC,
0xFE, 0xFD, 0xFF, 0xFD, 0xFF, 0xFD, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x9F, 0x30, 0xA0, 0x9E, 0x00, 0x7F, 0x00,
0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xEF, 0xF9, 0x6F, 0xF8, 0xFF,
0x00, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x90, 0xCF, 0xE1,
0x1E, 0x40, 0xBF, 0x00, 0xFF, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7C,
0xDB, 0xFF, 0xF3, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x3D, 0x82, 0x86, 0x79, 0x80, 0x7F,
0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0xA6, 0xBF, 0xEC,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x88, 0x27,
0xD6, 0xA0, 0x00, 0xFF, 0x00, 0xFF, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFE, 0xDF, 0x7F, 0xCE, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0x18, 0x47, 0x10, 0xC7, 0x00,
0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x10, 0xFF,
0x18, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
0x80, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F,
0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x01, 0xFF,
0x02, 0xFF, 0x0C, 0xFF, 0x10, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
0xFF, 0xFD, 0xFF, 0xF3, 0xFF, 0xEF, 0xFF,
0xFE, 0x11, 0xFD, 0x22, 0xFB, 0x44, 0xF7,
0x88, 0xEF, 0x10, 0xDF, 0x20, 0xBF, 0x40,
0x7F, 0x80, 0xEF, 0xFE, 0xDF, 0xFC, 0xBF,
0xF8, 0x7F, 0xF0, 0xFF, 0xE0, 0xFF, 0xC0,
0xFF, 0x80, 0xFF, 0x00, 0xBF, 0x48, 0xDF,
0x24, 0xDF, 0x22, 0xEF, 0x11, 0xF7, 0x08,
0xF9, 0x06, 0xFE, 0x01, 0xFF, 0x00, 0xF7,
0x3F, 0xFB, 0x1F, 0xFD, 0x1F, 0xFE, 0x0F,
0xFF, 0x07, 0xFF, 0x01, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x80, 0xFF, 0x7F, 0xFF, 0x00, 0x7F,
0x80, 0x80, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0x7F, 0xFF, 0x80, 0xFF, 0xFF,
0xFF, 0xFF, 0x7F, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x03, 0xFF, 0xFC, 0xFF, 0x00,
0xFC, 0x03, 0x03, 0xFC, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFC, 0xFF, 0x03, 0xFF,
0xFF, 0xFF, 0xFF, 0xFC, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x03, 0xFF, 0x1C, 0xFF, 0xE0,
0xFC, 0x03, 0xE3, 0x1C, 0x1F, 0xE0, 0xFF,
0x00, 0xFF, 0xFF, 0xFC, 0xFF, 0xE3, 0xFF,
0x1F, 0xFF, 0xFF, 0xFC, 0xFF, 0xE0, 0xFF,
0x00, 0xFF, 0x00, 0xFC, 0xE3, 0xF3, 0x0C,
0xEF, 0x10, 0x1F, 0xE0, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0x1F, 0xFC,
0xFF, 0xF0, 0xFF, 0xE0, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
};

View File

@ -12,7 +12,7 @@ void GB_update_joyp(GB_gameboy_t *gb)
previous_state = gb->io_registers[GB_IO_JOYP] & 0xF;
key_selection = (gb->io_registers[GB_IO_JOYP] >> 4) & 3;
gb->io_registers[GB_IO_JOYP] &= 0xF0;
uint8_t current_player = gb->sgb? (gb->sgb->current_player & (gb->sgb->player_count - 1) & 3) : 0;
uint8_t current_player = gb->sgb? gb->sgb->current_player : 0;
switch (key_selection) {
case 3:
if (gb->sgb && gb->sgb->player_count > 1) {
@ -30,11 +30,13 @@ void GB_update_joyp(GB_gameboy_t *gb)
gb->io_registers[GB_IO_JOYP] |= (!gb->keys[current_player][i]) << i;
}
/* Forbid pressing two opposing keys, this breaks a lot of games; even if it's somewhat possible. */
if (!(gb->io_registers[GB_IO_JOYP] & 1)) {
gb->io_registers[GB_IO_JOYP] |= 2;
}
if (!(gb->io_registers[GB_IO_JOYP] & 4)) {
gb->io_registers[GB_IO_JOYP] |= 8;
if (likely(!gb->illegal_inputs_allowed)) {
if (!(gb->io_registers[GB_IO_JOYP] & 1)) {
gb->io_registers[GB_IO_JOYP] |= 2;
}
if (!(gb->io_registers[GB_IO_JOYP] & 4)) {
gb->io_registers[GB_IO_JOYP] |= 8;
}
}
break;
@ -51,8 +53,7 @@ void GB_update_joyp(GB_gameboy_t *gb)
}
break;
default:
break;
nodefault;
}
/* Todo: This assumes the keys *always* bounce, which is incorrect when emulating an SGB */
@ -90,3 +91,48 @@ void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned play
gb->keys[player][index] = pressed;
GB_update_joyp(gb);
}
void GB_set_key_mask(GB_gameboy_t *gb, GB_key_mask_t mask)
{
memset(gb->keys, 0, sizeof(gb->keys));
bool *key = &gb->keys[0][0];
while (mask) {
if (mask & 1) {
*key = true;
}
mask >>= 1;
key++;
}
GB_update_joyp(gb);
}
void GB_set_key_mask_for_player(GB_gameboy_t *gb, GB_key_mask_t mask, unsigned player)
{
memset(gb->keys[player], 0, sizeof(gb->keys[player]));
bool *key = gb->keys[player];
while (mask) {
if (mask & 1) {
*key = true;
}
mask >>= 1;
key++;
}
GB_update_joyp(gb);
}
bool GB_get_joyp_accessed(GB_gameboy_t *gb)
{
return gb->joyp_accessed;
}
void GB_clear_joyp_accessed(GB_gameboy_t *gb)
{
gb->joyp_accessed = false;
}
void GB_set_allow_illegal_inputs(GB_gameboy_t *gb, bool allow)
{
gb->illegal_inputs_allowed = allow;
}

View File

@ -1,6 +1,6 @@
#ifndef joypad_h
#define joypad_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdbool.h>
typedef enum {
@ -15,11 +15,31 @@ typedef enum {
GB_KEY_MAX
} GB_key_t;
typedef enum {
GB_KEY_RIGHT_MASK = 1 << GB_KEY_RIGHT,
GB_KEY_LEFT_MASK = 1 << GB_KEY_LEFT,
GB_KEY_UP_MASK = 1 << GB_KEY_UP,
GB_KEY_DOWN_MASK = 1 << GB_KEY_DOWN,
GB_KEY_A_MASK = 1 << GB_KEY_A,
GB_KEY_B_MASK = 1 << GB_KEY_B,
GB_KEY_SELECT_MASK = 1 << GB_KEY_SELECT,
GB_KEY_START_MASK = 1 << GB_KEY_START,
} GB_key_mask_t;
// For example, for player 2's (0-based; logical player 3) A button, use GB_MASK_FOR_PLAYER(GB_KEY_A_MASK, 2)
#define GB_MASK_FOR_PLAYER(mask, player) ((x) << (player * 8))
void GB_set_key_state(GB_gameboy_t *gb, GB_key_t index, bool pressed);
void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned player, bool pressed);
void GB_set_key_mask(GB_gameboy_t *gb, GB_key_mask_t mask);
void GB_set_key_mask_for_player(GB_gameboy_t *gb, GB_key_mask_t mask, unsigned player);
void GB_icd_set_joyp(GB_gameboy_t *gb, uint8_t value);
bool GB_get_joyp_accessed(GB_gameboy_t *gb);
void GB_clear_joyp_accessed(GB_gameboy_t *gb);
void GB_set_allow_illegal_inputs(GB_gameboy_t *gb, bool allow);
#ifdef GB_INTERNAL
void GB_update_joyp(GB_gameboy_t *gb);
internal void GB_update_joyp(GB_gameboy_t *gb);
#endif
#endif /* joypad_h */

View File

@ -34,6 +34,8 @@ const GB_cartridge_t GB_cart_defs[256] = {
{ GB_MBC5 , GB_STANDARD_MBC, false, false, false, true }, // 1Ch MBC5+RUMBLE
{ GB_MBC5 , GB_STANDARD_MBC, true , false, false, true }, // 1Dh MBC5+RUMBLE+RAM
{ GB_MBC5 , GB_STANDARD_MBC, true , true , false, true }, // 1Eh MBC5+RUMBLE+RAM+BATTERY
[0x22] =
{ GB_MBC7 , GB_STANDARD_MBC, true, true, false, false}, // 22h MBC7+ACCEL+EEPROM
[0xFC] =
{ GB_MBC5 , GB_CAMERA , true , true , false, false}, // FCh POCKET CAMERA
{ GB_NO_MBC, GB_STANDARD_MBC, false, false, false, false}, // FDh BANDAI TAMA5 (Todo: Not supported)
@ -75,6 +77,7 @@ void GB_update_mbc_mappings(GB_gameboy_t *gb)
gb->mbc_rom_bank++;
}
break;
nodefault;
}
break;
case GB_MBC2:
@ -97,6 +100,9 @@ void GB_update_mbc_mappings(GB_gameboy_t *gb)
gb->mbc_rom_bank = gb->mbc5.rom_bank_low | (gb->mbc5.rom_bank_high << 8);
gb->mbc_ram_bank = gb->mbc5.ram_bank;
break;
case GB_MBC7:
gb->mbc_rom_bank = gb->mbc7.rom_bank;
break;
case GB_HUC1:
if (gb->huc1.mode == 0) {
gb->mbc_rom_bank = gb->huc1.bank_low | (gb->mbc1.bank_high << 6);
@ -111,12 +117,25 @@ void GB_update_mbc_mappings(GB_gameboy_t *gb)
gb->mbc_rom_bank = gb->huc3.rom_bank;
gb->mbc_ram_bank = gb->huc3.ram_bank;
break;
case GB_TPP1:
gb->mbc_rom_bank = gb->tpp1.rom_bank;
gb->mbc_ram_bank = gb->tpp1.ram_bank;
gb->mbc_ram_enable = (gb->tpp1.mode == 2) || (gb->tpp1.mode == 3);
break;
nodefault;
}
}
void GB_configure_cart(GB_gameboy_t *gb)
{
gb->cartridge_type = &GB_cart_defs[gb->rom[0x147]];
if (gb->rom[0x147] == 0xbc &&
gb->rom[0x149] == 0xc1 &&
gb->rom[0x14a] == 0x65) {
static const GB_cartridge_t tpp1 = {GB_TPP1, GB_STANDARD_MBC, true, true, true, true};
gb->cartridge_type = &tpp1;
gb->tpp1.rom_bank = 1;
}
if (gb->rom[0x147] == 0 && gb->rom_size > 0x8000) {
GB_log(gb, "ROM header reports no MBC, but file size is over 32Kb. Assuming cartridge uses MBC3.\n");
@ -125,11 +144,25 @@ void GB_configure_cart(GB_gameboy_t *gb)
else if (gb->rom[0x147] != 0 && memcmp(gb->cartridge_type, &GB_cart_defs[0], sizeof(GB_cart_defs[0])) == 0) {
GB_log(gb, "Cartridge type %02x is not yet supported.\n", gb->rom[0x147]);
}
if (gb->mbc_ram) {
free(gb->mbc_ram);
gb->mbc_ram = NULL;
gb->mbc_ram_size = 0;
}
if (gb->cartridge_type->has_ram) {
if (gb->cartridge_type->mbc_type == GB_MBC2) {
gb->mbc_ram_size = 0x200;
}
else if (gb->cartridge_type->mbc_type == GB_MBC7) {
gb->mbc_ram_size = 0x100;
}
else if (gb->cartridge_type->mbc_type == GB_TPP1) {
if (gb->rom[0x152] >= 1 && gb->rom[0x152] <= 9) {
gb->mbc_ram_size = 0x2000 << (gb->rom[0x152] - 1);
}
}
else {
static const unsigned ram_sizes[256] = {0, 0x800, 0x2000, 0x8000, 0x20000, 0x10000};
gb->mbc_ram_size = ram_sizes[gb->rom[0x149]];
@ -139,7 +172,7 @@ void GB_configure_cart(GB_gameboy_t *gb)
gb->mbc_ram = malloc(gb->mbc_ram_size);
}
/* Todo: Some games assume unintialized MBC RAM is 0xFF. It this true for all cartridges types? */
/* Todo: Some games assume unintialized MBC RAM is 0xFF. It this true for all cartridge types? */
memset(gb->mbc_ram, 0xFF, gb->mbc_ram_size);
}
@ -164,4 +197,12 @@ void GB_configure_cart(GB_gameboy_t *gb)
if (gb->cartridge_type->mbc_type == GB_MBC5) {
gb->mbc5.rom_bank_low = 1;
}
/* Initial MBC7 state */
if (gb->cartridge_type->mbc_type == GB_MBC7) {
gb->mbc7.x_latch = gb->mbc7.y_latch = 0x8000;
gb->mbc7.latch_ready = true;
gb->mbc7.read_bits = -1;
gb->mbc7.eeprom_do = true;
}
}

View File

@ -1,6 +1,6 @@
#ifndef MBC_h
#define MBC_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdbool.h>
typedef struct {
@ -10,8 +10,10 @@ typedef struct {
GB_MBC2,
GB_MBC3,
GB_MBC5,
GB_MBC7,
GB_HUC1,
GB_HUC3,
GB_TPP1,
} mbc_type;
enum {
GB_STANDARD_MBC,
@ -25,8 +27,8 @@ typedef struct {
#ifdef GB_INTERNAL
extern const GB_cartridge_t GB_cart_defs[256];
void GB_update_mbc_mappings(GB_gameboy_t *gb);
void GB_configure_cart(GB_gameboy_t *gb);
internal void GB_update_mbc_mappings(GB_gameboy_t *gb);
internal void GB_configure_cart(GB_gameboy_t *gb);
#endif
#endif /* MBC_h */

File diff suppressed because it is too large Load Diff

View File

@ -1,18 +1,20 @@
#ifndef memory_h
#define memory_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdint.h>
typedef uint8_t (*GB_read_memory_callback_t)(GB_gameboy_t *gb, uint16_t addr, uint8_t data);
typedef bool (*GB_write_memory_callback_t)(GB_gameboy_t *gb, uint16_t addr, uint8_t data); // Return false to prevent the write
void GB_set_read_memory_callback(GB_gameboy_t *gb, GB_read_memory_callback_t callback);
void GB_set_write_memory_callback(GB_gameboy_t *gb, GB_write_memory_callback_t callback);
uint8_t GB_read_memory(GB_gameboy_t *gb, uint16_t addr);
uint8_t GB_safe_read_memory(GB_gameboy_t *gb, uint16_t addr); // Without side effects
void GB_write_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value);
#ifdef GB_INTERNAL
void GB_dma_run(GB_gameboy_t *gb);
void GB_hdma_run(GB_gameboy_t *gb);
void GB_trigger_oam_bug(GB_gameboy_t *gb, uint16_t address);
void GB_trigger_oam_bug_read_increase(GB_gameboy_t *gb, uint16_t address);
internal void GB_dma_run(GB_gameboy_t *gb);
internal void GB_hdma_run(GB_gameboy_t *gb);
internal void GB_trigger_oam_bug(GB_gameboy_t *gb, uint16_t address);
#endif
#endif /* memory_h */

View File

@ -11,7 +11,6 @@
static void handle_command(GB_gameboy_t *gb)
{
switch (gb->printer.command_id) {
case GB_PRINTER_INIT_COMMAND:
gb->printer.status = 0;
@ -71,7 +70,7 @@ static void handle_command(GB_gameboy_t *gb)
}
static void byte_reieve_completed(GB_gameboy_t *gb, uint8_t byte_received)
static void byte_recieve_completed(GB_gameboy_t *gb, uint8_t byte_received)
{
gb->printer.byte_to_send = 0;
switch (gb->printer.command_state) {
@ -189,11 +188,16 @@ static void byte_reieve_completed(GB_gameboy_t *gb, uint8_t byte_received)
static void serial_start(GB_gameboy_t *gb, bool bit_received)
{
if (gb->printer.idle_time > GB_get_unmultiplied_clock_rate(gb)) {
gb->printer.command_state = GB_PRINTER_COMMAND_MAGIC1;
gb->printer.bits_received = 0;
}
gb->printer.idle_time = 0;
gb->printer.byte_being_received <<= 1;
gb->printer.byte_being_received |= bit_received;
gb->printer.bits_received++;
if (gb->printer.bits_received == 8) {
byte_reieve_completed(gb, gb->printer.byte_being_received);
byte_recieve_completed(gb, gb->printer.byte_being_received);
gb->printer.bits_received = 0;
gb->printer.byte_being_received = 0;
}

View File

@ -2,7 +2,7 @@
#define printer_h
#include <stdint.h>
#include <stdbool.h>
#include "gb_struct_def.h"
#include "defs.h"
#define GB_PRINTER_MAX_COMMAND_LENGTH 0x280
#define GB_PRINTER_DATA_SIZE 0x280
@ -48,8 +48,7 @@ typedef struct
uint8_t image[160 * 200];
uint16_t image_offset;
/* TODO: Delete me. */
uint64_t padding;
uint64_t idle_time;
uint8_t compression_run_lenth;
bool compression_run_is_compressed;

View File

@ -108,7 +108,7 @@ static void state_decompress(const uint8_t *prev, uint8_t *data, uint8_t *dest,
void GB_rewind_push(GB_gameboy_t *gb)
{
const size_t save_size = GB_get_save_state_size(gb);
const size_t save_size = GB_get_save_state_size_no_bess(gb);
if (!gb->rewind_sequences) {
if (gb->rewind_buffer_length) {
gb->rewind_sequences = malloc(sizeof(*gb->rewind_sequences) * gb->rewind_buffer_length);
@ -140,11 +140,11 @@ void GB_rewind_push(GB_gameboy_t *gb)
if (!gb->rewind_sequences[gb->rewind_pos].key_state) {
gb->rewind_sequences[gb->rewind_pos].key_state = malloc(save_size);
GB_save_state_to_buffer(gb, gb->rewind_sequences[gb->rewind_pos].key_state);
GB_save_state_to_buffer_no_bess(gb, gb->rewind_sequences[gb->rewind_pos].key_state);
}
else {
uint8_t *save_state = malloc(save_size);
GB_save_state_to_buffer(gb, save_state);
GB_save_state_to_buffer_no_bess(gb, save_state);
gb->rewind_sequences[gb->rewind_pos].compressed_states[gb->rewind_sequences[gb->rewind_pos].pos++] =
state_compress(gb->rewind_sequences[gb->rewind_pos].key_state, save_state, save_size);
free(save_state);
@ -158,7 +158,7 @@ bool GB_rewind_pop(GB_gameboy_t *gb)
return false;
}
const size_t save_size = GB_get_save_state_size(gb);
const size_t save_size = GB_get_save_state_size_no_bess(gb);
if (gb->rewind_sequences[gb->rewind_pos].pos == 0) {
GB_load_state_from_buffer(gb, gb->rewind_sequences[gb->rewind_pos].key_state, save_size);
free(gb->rewind_sequences[gb->rewind_pos].key_state);

View File

@ -2,11 +2,11 @@
#define rewind_h
#include <stdbool.h>
#include "gb_struct_def.h"
#include "defs.h"
#ifdef GB_INTERNAL
void GB_rewind_push(GB_gameboy_t *gb);
void GB_rewind_free(GB_gameboy_t *gb);
internal void GB_rewind_push(GB_gameboy_t *gb);
internal void GB_rewind_free(GB_gameboy_t *gb);
#endif
bool GB_rewind_pop(GB_gameboy_t *gb);
void GB_set_rewind_length(GB_gameboy_t *gb, double seconds);

View File

@ -15,7 +15,8 @@ void GB_handle_rumble(GB_gameboy_t *gb)
if (gb->rumble_mode == GB_RUMBLE_DISABLED) {
return;
}
if (gb->cartridge_type->has_rumble) {
if (gb->cartridge_type->has_rumble &&
(gb->cartridge_type->mbc_type != GB_TPP1 || (gb->rom[0x153] & 1))) {
if (gb->rumble_on_cycles + gb->rumble_off_cycles) {
gb->rumble_callback(gb, gb->rumble_on_cycles / (double)(gb->rumble_on_cycles + gb->rumble_off_cycles));
gb->rumble_on_cycles = gb->rumble_off_cycles = 0;

View File

@ -1,7 +1,7 @@
#ifndef rumble_h
#define rumble_h
#include "gb_struct_def.h"
#include "defs.h"
typedef enum {
GB_RUMBLE_DISABLED,
@ -10,7 +10,7 @@ typedef enum {
} GB_rumble_mode_t;
#ifdef GB_INTERNAL
void GB_handle_rumble(GB_gameboy_t *gb);
internal void GB_handle_rumble(GB_gameboy_t *gb);
#endif
void GB_set_rumble_mode(GB_gameboy_t *gb, GB_rumble_mode_t mode);

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,7 @@
as anonymous enums inside unions */
#define GB_SECTION(name, ...) __attribute__ ((aligned (8))) __VA_ARGS__
#else
#define GB_SECTION(name, ...) __attribute__ ((aligned (8))) union {uint8_t name##_section_start; struct {__VA_ARGS__};}; uint8_t name##_section_end[0]
#define GB_SECTION(name, ...) union __attribute__ ((aligned (8))) {uint8_t name##_section_start; struct {__VA_ARGS__};}; uint8_t name##_section_end[0]
#define GB_SECTION_OFFSET(name) (offsetof(GB_gameboy_t, name##_section_start))
#define GB_SECTION_SIZE(name) (offsetof(GB_gameboy_t, name##_section_end) - offsetof(GB_gameboy_t, name##_section_start))
#define GB_GET_SECTION(gb, name) ((void*)&((gb)->name##_section_start))
@ -27,4 +27,17 @@ void GB_save_state_to_buffer(GB_gameboy_t *gb, uint8_t *buffer);
int GB_load_state(GB_gameboy_t *gb, const char *path);
int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t length);
bool GB_is_save_state(const char *path);
#ifdef GB_INTERNAL
static inline uint32_t state_magic(void)
{
if (sizeof(bool) == 1) return 'SAME';
return 'S4ME';
}
/* For internal in-memory save states (rewind, debugger) that do not need BESS */
internal size_t GB_get_save_state_size_no_bess(GB_gameboy_t *gb);
internal void GB_save_state_to_buffer_no_bess(GB_gameboy_t *gb, uint8_t *buffer);
#endif
#endif /* save_state_h */

View File

@ -7,8 +7,6 @@
#define M_PI 3.14159265358979323846
#endif
#define INTRO_ANIMATION_LENGTH 200
enum {
PAL01 = 0x00,
PAL23 = 0x01,
@ -49,14 +47,14 @@ static inline void pal_command(GB_gameboy_t *gb, unsigned first, unsigned second
{
gb->sgb->effective_palettes[0] = gb->sgb->effective_palettes[4] =
gb->sgb->effective_palettes[8] = gb->sgb->effective_palettes[12] =
gb->sgb->command[1] | (gb->sgb->command[2] << 8);
*(uint16_t *)&gb->sgb->command[1];
for (unsigned i = 0; i < 3; i++) {
gb->sgb->effective_palettes[first * 4 + i + 1] = gb->sgb->command[3 + i * 2] | (gb->sgb->command[4 + i * 2] << 8);
gb->sgb->effective_palettes[first * 4 + i + 1] = *(uint16_t *)&gb->sgb->command[3 + i * 2];
}
for (unsigned i = 0; i < 3; i++) {
gb->sgb->effective_palettes[second * 4 + i + 1] = gb->sgb->command[9 + i * 2] | (gb->sgb->command[10 + i * 2] << 8);
gb->sgb->effective_palettes[second * 4 + i + 1] = *(uint16_t *)&gb->sgb->command[9 + i * 2];
}
}
@ -172,10 +170,10 @@ static void command_ready(GB_gameboy_t *gb)
gb->sgb->disable_commands = true;
for (unsigned i = 0; i < sizeof(palette_assignments) / sizeof(palette_assignments[0]); i++) {
if (memcmp(palette_assignments[i].name, &gb->sgb->received_header[0x30], sizeof(palette_assignments[i].name)) == 0) {
gb->sgb->effective_palettes[0] = built_in_palettes[palette_assignments[i].palette_index * 4 - 4];
gb->sgb->effective_palettes[1] = built_in_palettes[palette_assignments[i].palette_index * 4 + 1 - 4];
gb->sgb->effective_palettes[2] = built_in_palettes[palette_assignments[i].palette_index * 4 + 2 - 4];
gb->sgb->effective_palettes[3] = built_in_palettes[palette_assignments[i].palette_index * 4 + 3 - 4];
gb->sgb->effective_palettes[0] = LE16(built_in_palettes[palette_assignments[i].palette_index * 4 - 4]);
gb->sgb->effective_palettes[1] = LE16(built_in_palettes[palette_assignments[i].palette_index * 4 + 1 - 4]);
gb->sgb->effective_palettes[2] = LE16(built_in_palettes[palette_assignments[i].palette_index * 4 + 2 - 4]);
gb->sgb->effective_palettes[3] = LE16(built_in_palettes[palette_assignments[i].palette_index * 4 + 3 - 4]);
break;
}
}
@ -264,12 +262,10 @@ static void command_ready(GB_gameboy_t *gb)
} *command = (void *)(gb->sgb->command + 1);
uint16_t count = command->length;
#ifdef GB_BIG_ENDIAN
count = __builtin_bswap16(count);
#endif
count = LE16(count);
uint8_t x = command->x;
uint8_t y = command->y;
if (x >= 20 || y >= 18 || (count + 3) / 4 > sizeof(gb->sgb->command) - sizeof(*command) - 1) {
if (x >= 20 || y >= 18) {
/* TODO: Verify with the SFC BIOS */
break;
}
@ -283,7 +279,7 @@ static void command_ready(GB_gameboy_t *gb)
x++;
y = 0;
if (x == 20) {
x = 0;
break;
}
}
}
@ -293,7 +289,7 @@ static void command_ready(GB_gameboy_t *gb)
y++;
x = 0;
if (y == 18) {
y = 0;
break;
}
}
}
@ -377,39 +373,36 @@ static void command_ready(GB_gameboy_t *gb)
}
break;
case PAL_TRN:
gb->sgb->vram_transfer_countdown = 2;
gb->sgb->vram_transfer_countdown = 3;
gb->sgb->transfer_dest = TRANSFER_PALETTES;
break;
case DATA_SND:
// Not supported, but used by almost all SGB games for hot patching, so let's mute the warning for this
break;
case MLT_REQ:
if (gb->sgb->player_count == 1) {
gb->sgb->current_player = 0;
}
gb->sgb->player_count = (gb->sgb->command[1] & 3) + 1; /* Todo: When breaking save state comaptibility,
fix this to be 0 based. */
if (gb->sgb->player_count == 3) {
gb->sgb->current_player++;
gb->sgb->player_count++;
}
gb->sgb->mlt_lock = true;
gb->sgb->current_player &= (gb->sgb->player_count - 1);
break;
case CHR_TRN:
gb->sgb->vram_transfer_countdown = 2;
gb->sgb->vram_transfer_countdown = 3;
gb->sgb->transfer_dest = (gb->sgb->command[1] & 1)? TRANSFER_HIGH_TILES : TRANSFER_LOW_TILES;
break;
case PCT_TRN:
gb->sgb->vram_transfer_countdown = 2;
gb->sgb->vram_transfer_countdown = 3;
gb->sgb->transfer_dest = TRANSFER_BORDER_DATA;
break;
case ATTR_TRN:
gb->sgb->vram_transfer_countdown = 2;
gb->sgb->vram_transfer_countdown = 3;
gb->sgb->transfer_dest = TRANSFER_ATTRIBUTES;
break;
case ATTR_SET:
load_attribute_file(gb, gb->sgb->command[0] & 0x3F);
load_attribute_file(gb, gb->sgb->command[1] & 0x3F);
if (gb->sgb->command[0] & 0x40) {
if (gb->sgb->command[1] & 0x40) {
gb->sgb->mask_mode = MASK_DISABLED;
}
break;
@ -439,27 +432,22 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value)
return;
}
if (gb->sgb->disable_commands) return;
if (gb->sgb->command_write_index >= sizeof(gb->sgb->command) * 8) {
return;
}
uint16_t command_size = (gb->sgb->command[0] & 7 ?: 1) * SGB_PACKET_SIZE * 8;
if ((gb->sgb->command[0] & 0xF1) == 0xF1) {
command_size = SGB_PACKET_SIZE * 8;
}
if ((value & 0x20) == 0 && (gb->io_registers[GB_IO_JOYP] & 0x20) != 0) {
gb->sgb->mlt_lock ^= true;
if ((value & 0x20) != 0 && (gb->io_registers[GB_IO_JOYP] & 0x20) == 0) {
if ((gb->sgb->player_count & 1) == 0) {
gb->sgb->current_player++;
gb->sgb->current_player &= (gb->sgb->player_count - 1);
}
}
switch ((value >> 4) & 3) {
case 3:
gb->sgb->ready_for_pulse = true;
if ((gb->sgb->player_count & 1) == 0 && !gb->sgb->mlt_lock) {
gb->sgb->current_player++;
gb->sgb->current_player &= 3;
gb->sgb->mlt_lock = true;
}
break;
case 2: // Zero
@ -475,10 +463,12 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value)
gb->sgb->ready_for_stop = false;
}
else {
gb->sgb->command_write_index++;
gb->sgb->ready_for_pulse = false;
if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) == 0) {
gb->sgb->ready_for_stop = true;
if (gb->sgb->command_write_index < sizeof(gb->sgb->command) * 8) {
gb->sgb->command_write_index++;
gb->sgb->ready_for_pulse = false;
if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) == 0) {
gb->sgb->ready_for_stop = true;
}
}
}
break;
@ -492,11 +482,13 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value)
memset(gb->sgb->command, 0, sizeof(gb->sgb->command));
}
else {
gb->sgb->command[gb->sgb->command_write_index / 8] |= 1 << (gb->sgb->command_write_index & 7);
gb->sgb->command_write_index++;
gb->sgb->ready_for_pulse = false;
if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) == 0) {
gb->sgb->ready_for_stop = true;
if (gb->sgb->command_write_index < sizeof(gb->sgb->command) * 8) {
gb->sgb->command[gb->sgb->command_write_index / 8] |= 1 << (gb->sgb->command_write_index & 7);
gb->sgb->command_write_index++;
gb->sgb->ready_for_pulse = false;
if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) == 0) {
gb->sgb->ready_for_stop = true;
}
}
}
break;
@ -539,7 +531,6 @@ static uint32_t convert_rgb15_with_fade(GB_gameboy_t *gb, uint16_t color, uint8_
return GB_convert_rgb15(gb, color, false);
}
#include <stdio.h>
static void render_boot_animation (GB_gameboy_t *gb)
{
#include "graphics/sgb_animation_logo.inc"
@ -556,8 +547,8 @@ static void render_boot_animation (GB_gameboy_t *gb)
else if (gb->sgb->intro_animation < 80) {
fade_blue = 80 - gb->sgb->intro_animation;
}
else if (gb->sgb->intro_animation > INTRO_ANIMATION_LENGTH - 32) {
fade_red = fade_blue = gb->sgb->intro_animation - INTRO_ANIMATION_LENGTH + 32;
else if (gb->sgb->intro_animation > GB_SGB_INTRO_ANIMATION_LENGTH - 32) {
fade_red = fade_blue = gb->sgb->intro_animation - GB_SGB_INTRO_ANIMATION_LENGTH + 32;
}
uint32_t colors[] = {
convert_rgb15(gb, 0),
@ -607,74 +598,73 @@ void GB_sgb_render(GB_gameboy_t *gb)
render_jingle(gb, gb->apu_output.sample_rate / GB_get_usual_frame_rate(gb));
}
if (gb->sgb->intro_animation < INTRO_ANIMATION_LENGTH) gb->sgb->intro_animation++;
if (gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) gb->sgb->intro_animation++;
if (gb->sgb->vram_transfer_countdown) {
if (--gb->sgb->vram_transfer_countdown == 0) {
if (gb->sgb->transfer_dest == TRANSFER_LOW_TILES || gb->sgb->transfer_dest == TRANSFER_HIGH_TILES) {
uint8_t *base = &gb->sgb->pending_border.tiles[gb->sgb->transfer_dest == TRANSFER_HIGH_TILES ? 0x80 * 8 * 8 : 0];
for (unsigned tile = 0; tile < 0x80; tile++) {
unsigned tile_x = (tile % 10) * 16;
unsigned tile_y = (tile / 10) * 8;
for (unsigned y = 0; y < 0x8; y++) {
for (unsigned x = 0; x < 0x8; x++) {
base[tile * 8 * 8 + y * 8 + x] = gb->sgb->screen_buffer[(tile_x + x) + (tile_y + y) * 160] +
gb->sgb->screen_buffer[(tile_x + x + 8) + (tile_y + y) * 160] * 4;
}
}
}
unsigned size = 0;
uint16_t *data = NULL;
switch (gb->sgb->transfer_dest) {
case TRANSFER_LOW_TILES:
size = 0x100;
data = (uint16_t *)gb->sgb->pending_border.tiles;
break;
case TRANSFER_HIGH_TILES:
size = 0x100;
data = (uint16_t *)gb->sgb->pending_border.tiles + 0x800;
break;
case TRANSFER_PALETTES:
size = 0x100;
data = gb->sgb->ram_palettes;
break;
case TRANSFER_BORDER_DATA:
size = 0x88;
data = gb->sgb->pending_border.raw_data;
break;
case TRANSFER_ATTRIBUTES:
size = 0xFE;
data = (uint16_t *)gb->sgb->attribute_files;
break;
default:
return; // Corrupt state?
}
else {
unsigned size = 0;
uint16_t *data = NULL;
switch (gb->sgb->transfer_dest) {
case TRANSFER_PALETTES:
size = 0x100;
data = gb->sgb->ram_palettes;
break;
case TRANSFER_BORDER_DATA:
size = 0x88;
data = gb->sgb->pending_border.raw_data;
break;
case TRANSFER_ATTRIBUTES:
size = 0xFE;
data = (uint16_t *)gb->sgb->attribute_files;
break;
default:
return; // Corrupt state?
}
for (unsigned tile = 0; tile < size; tile++) {
unsigned tile_x = (tile % 20) * 8;
unsigned tile_y = (tile / 20) * 8;
for (unsigned y = 0; y < 0x8; y++) {
static const uint16_t pixel_to_bits[4] = {0x0000, 0x0080, 0x8000, 0x8080};
*data = 0;
for (unsigned x = 0; x < 8; x++) {
*data |= pixel_to_bits[gb->sgb->screen_buffer[(tile_x + x) + (tile_y + y) * 160] & 3] >> x;
}
#ifdef GB_BIG_ENDIAN
if (gb->sgb->transfer_dest == TRANSFER_ATTRIBUTES) {
*data = __builtin_bswap16(*data);
}
#endif
data++;
for (unsigned tile = 0; tile < size; tile++) {
unsigned tile_x = (tile % 20) * 8;
unsigned tile_y = (tile / 20) * 8;
for (unsigned y = 0; y < 0x8; y++) {
static const uint16_t pixel_to_bits[4] = {0x0000, 0x0080, 0x8000, 0x8080};
*data = 0;
for (unsigned x = 0; x < 8; x++) {
*data |= pixel_to_bits[gb->sgb->screen_buffer[(tile_x + x) + (tile_y + y) * 160] & 3] >> x;
}
*data = LE16(*data);
data++;
}
if (gb->sgb->transfer_dest == TRANSFER_BORDER_DATA) {
gb->sgb->border_animation = 64;
}
}
if (gb->sgb->transfer_dest == TRANSFER_BORDER_DATA) {
gb->sgb->border_animation = 105; // Measured on an SGB2, but might be off by ±2
}
}
}
if (!gb->screen || !gb->rgb_encode_callback || gb->disable_rendering) return;
if (!gb->screen || !gb->rgb_encode_callback || gb->disable_rendering) {
if (gb->sgb->border_animation > 32) {
gb->sgb->border_animation--;
}
else if (gb->sgb->border_animation != 0) {
gb->sgb->border_animation--;
}
if (gb->sgb->border_animation == 32) {
memcpy(&gb->sgb->border, &gb->sgb->pending_border, sizeof(gb->sgb->border));
}
return;
}
uint32_t colors[4 * 4];
for (unsigned i = 0; i < 4 * 4; i++) {
colors[i] = convert_rgb15(gb, gb->sgb->effective_palettes[i]);
colors[i] = convert_rgb15(gb, LE16(gb->sgb->effective_palettes[i]));
}
if (gb->sgb->mask_mode != MASK_FREEZE) {
@ -683,7 +673,7 @@ void GB_sgb_render(GB_gameboy_t *gb)
sizeof(gb->sgb->effective_screen_buffer));
}
if (gb->sgb->intro_animation < INTRO_ANIMATION_LENGTH) {
if (gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) {
render_boot_animation(gb);
}
else {
@ -735,21 +725,24 @@ void GB_sgb_render(GB_gameboy_t *gb)
}
uint32_t border_colors[16 * 4];
if (gb->sgb->border_animation == 0 || gb->sgb->intro_animation < INTRO_ANIMATION_LENGTH) {
if (gb->sgb->border_animation == 0 || gb->sgb->border_animation > 64 || gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) {
if (gb->sgb->border_animation != 0) {
gb->sgb->border_animation--;
}
for (unsigned i = 0; i < 16 * 4; i++) {
border_colors[i] = convert_rgb15(gb, gb->sgb->border.palette[i]);
border_colors[i] = convert_rgb15(gb, LE16(gb->sgb->border.palette[i]));
}
}
else if (gb->sgb->border_animation > 32) {
gb->sgb->border_animation--;
for (unsigned i = 0; i < 16 * 4; i++) {
border_colors[i] = convert_rgb15_with_fade(gb, gb->sgb->border.palette[i], 64 - gb->sgb->border_animation);
border_colors[i] = convert_rgb15_with_fade(gb, LE16(gb->sgb->border.palette[i]), 64 - gb->sgb->border_animation);
}
}
else {
gb->sgb->border_animation--;
for (unsigned i = 0; i < 16 * 4; i++) {
border_colors[i] = convert_rgb15_with_fade(gb, gb->sgb->border.palette[i], gb->sgb->border_animation);
border_colors[i] = convert_rgb15_with_fade(gb, LE16(gb->sgb->border.palette[i]), gb->sgb->border_animation);
}
}
@ -767,13 +760,20 @@ void GB_sgb_render(GB_gameboy_t *gb)
else if (gb->border_mode == GB_BORDER_NEVER) {
continue;
}
uint16_t tile = gb->sgb->border.map[tile_x + tile_y * 32];
uint8_t flip_x = (tile & 0x4000)? 0x7 : 0;
uint8_t flip_y = (tile & 0x8000)? 0x7 : 0;
uint16_t tile = LE16(gb->sgb->border.map[tile_x + tile_y * 32]);
if (tile & 0x300) continue; // Unused tile
uint8_t flip_x = (tile & 0x4000)? 0:7;
uint8_t flip_y = (tile & 0x8000)? 7:0;
uint8_t palette = (tile >> 10) & 3;
for (unsigned y = 0; y < 8; y++) {
unsigned base = (tile & 0xFF) * 32 + (y ^ flip_y) * 2;
for (unsigned x = 0; x < 8; x++) {
uint8_t color = gb->sgb->border.tiles[(tile & 0xFF) * 64 + (x ^ flip_x) + (y ^ flip_y) * 8] & 0xF;
uint8_t bit = 1 << (x ^ flip_x);
uint8_t color = ((gb->sgb->border.tiles[base] & bit) ? 1: 0) |
((gb->sgb->border.tiles[base + 1] & bit) ? 2: 0) |
((gb->sgb->border.tiles[base + 16] & bit) ? 4: 0) |
((gb->sgb->border.tiles[base + 17] & bit) ? 8: 0);
uint32_t *output = gb->screen;
if (gb->border_mode == GB_BORDER_NEVER) {
output += (tile_x - 6) * 8 + x + ((tile_y - 5) * 8 + y) * 160;
@ -798,22 +798,19 @@ void GB_sgb_load_default_data(GB_gameboy_t *gb)
{
#include "graphics/sgb_border.inc"
#ifdef GB_BIG_ENDIAN
for (unsigned i = 0; i < sizeof(tilemap) / 2; i++) {
gb->sgb->border.map[i] = LE16(tilemap[i]);
}
for (unsigned i = 0; i < sizeof(palette) / 2; i++) {
gb->sgb->border.palette[i] = LE16(palette[i]);
}
#else
memcpy(gb->sgb->border.map, tilemap, sizeof(tilemap));
memcpy(gb->sgb->border.palette, palette, sizeof(palette));
/* Expand tileset */
for (unsigned tile = 0; tile < sizeof(tiles) / 32; tile++) {
for (unsigned y = 0; y < 8; y++) {
for (unsigned x = 0; x < 8; x++) {
gb->sgb->border.tiles[tile * 8 * 8 + y * 8 + x] =
(tiles[tile * 32 + y * 2 + 0] & (1 << (7 ^ x)) ? 1 : 0) |
(tiles[tile * 32 + y * 2 + 1] & (1 << (7 ^ x)) ? 2 : 0) |
(tiles[tile * 32 + y * 2 + 16] & (1 << (7 ^ x)) ? 4 : 0) |
(tiles[tile * 32 + y * 2 + 17] & (1 << (7 ^ x)) ? 8 : 0);
}
}
}
#endif
memcpy(gb->sgb->border.tiles, tiles, sizeof(tiles));
if (gb->model != GB_MODEL_SGB2) {
/* Delete the "2" */
@ -825,10 +822,10 @@ void GB_sgb_load_default_data(GB_gameboy_t *gb)
/* Re-center */
memmove(&gb->sgb->border.map[25 * 32 + 1], &gb->sgb->border.map[25 * 32], (32 * 3 - 1) * sizeof(gb->sgb->border.map[0]));
}
gb->sgb->effective_palettes[0] = built_in_palettes[0];
gb->sgb->effective_palettes[1] = built_in_palettes[1];
gb->sgb->effective_palettes[2] = built_in_palettes[2];
gb->sgb->effective_palettes[3] = built_in_palettes[3];
gb->sgb->effective_palettes[0] = LE16(built_in_palettes[0]);
gb->sgb->effective_palettes[1] = LE16(built_in_palettes[1]);
gb->sgb->effective_palettes[2] = LE16(built_in_palettes[2]);
gb->sgb->effective_palettes[3] = LE16(built_in_palettes[3]);
}
static double fm_synth(double phase)
@ -874,7 +871,7 @@ static void render_jingle(GB_gameboy_t *gb, size_t count)
return;
}
if (gb->sgb->intro_animation >= INTRO_ANIMATION_LENGTH) return;
if (gb->sgb->intro_animation >= GB_SGB_INTRO_ANIMATION_LENGTH) return;
signed jingle_stage = (gb->sgb->intro_animation - 64) / 3;
double sweep_cutoff_ratio = 2000.0 * pow(2, gb->sgb->intro_animation / 20.0) / gb->apu_output.sample_rate;
@ -892,7 +889,7 @@ static void render_jingle(GB_gameboy_t *gb, size_t count)
gb->sgb_intro_jingle_phases[f] += frequencies[f] / gb->apu_output.sample_rate;
}
if (gb->sgb->intro_animation > 100) {
sample *= pow((INTRO_ANIMATION_LENGTH - gb->sgb->intro_animation) / (INTRO_ANIMATION_LENGTH - 100.0), 3);
sample *= pow((GB_SGB_INTRO_ANIMATION_LENGTH - gb->sgb->intro_animation) / (GB_SGB_INTRO_ANIMATION_LENGTH - 100.0), 3);
}
if (gb->sgb->intro_animation < 120) {

View File

@ -1,12 +1,12 @@
#ifndef sgb_h
#define sgb_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdint.h>
#include <stdbool.h>
typedef struct GB_sgb_s GB_sgb_t;
typedef struct {
uint8_t tiles[0x100 * 8 * 8]; /* High nibble not used*/
uint8_t tiles[0x100 * 8 * 4];
union {
struct {
uint16_t map[32 * 32];
@ -17,6 +17,8 @@ typedef struct {
} GB_sgb_border_t;
#ifdef GB_INTERNAL
#define GB_SGB_INTRO_ANIMATION_LENGTH 200
struct GB_sgb_s {
uint8_t command[16 * 7];
uint16_t command_write_index;
@ -46,21 +48,19 @@ struct GB_sgb_s {
uint16_t effective_palettes[4 * 4];
uint16_t ram_palettes[4 * 512];
uint8_t attribute_map[20 * 18];
uint8_t attribute_files[0xFE0];
uint8_t attribute_files[0xFD2];
uint8_t attribute_files_padding[0xFE0 - 0xFD2];
/* Intro */
int16_t intro_animation;
/* GB Header */
uint8_t received_header[0x54];
/* Multiplayer (cont) */
bool mlt_lock;
};
void GB_sgb_write(GB_gameboy_t *gb, uint8_t value);
void GB_sgb_render(GB_gameboy_t *gb);
void GB_sgb_load_default_data(GB_gameboy_t *gb);
internal void GB_sgb_write(GB_gameboy_t *gb, uint8_t value);
internal void GB_sgb_render(GB_gameboy_t *gb);
internal void GB_sgb_load_default_data(GB_gameboy_t *gb);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,11 @@
#ifndef sm83_cpu_h
#define sm83_cpu_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdint.h>
void GB_cpu_disassemble(GB_gameboy_t *gb, uint16_t pc, uint16_t count);
#ifdef GB_INTERNAL
void GB_cpu_run(GB_gameboy_t *gb);
internal void GB_cpu_run(GB_gameboy_t *gb);
#endif
#endif /* sm83_cpu_h */

View File

@ -2,8 +2,9 @@
#include <stdbool.h>
#include "gb.h"
#define GB_read_memory GB_safe_read_memory
typedef void GB_opcode_t(GB_gameboy_t *gb, uint8_t opcode, uint16_t *pc);
typedef void opcode_t(GB_gameboy_t *gb, uint8_t opcode, uint16_t *pc);
static void ill(GB_gameboy_t *gb, uint8_t opcode, uint16_t *pc)
{
@ -716,7 +717,7 @@ static void cb_prefix(GB_gameboy_t *gb, uint8_t opcode, uint16_t *pc)
}
}
static GB_opcode_t *opcodes[256] = {
static opcode_t *opcodes[256] = {
/* X0 X1 X2 X3 X4 X5 X6 X7 */
/* X8 X9 Xa Xb Xc Xd Xe Xf */
nop, ld_rr_d16, ld_drr_a, inc_rr, inc_hr, dec_hr, ld_hr_d8, rlca, /* 0X */

View File

@ -4,7 +4,7 @@
#include <string.h>
#include <sys/types.h>
static size_t GB_map_find_symbol_index(GB_symbol_map_t *map, uint16_t addr)
static size_t map_find_symbol_index(GB_symbol_map_t *map, uint16_t addr)
{
if (!map->symbols) {
return 0;
@ -26,7 +26,7 @@ static size_t GB_map_find_symbol_index(GB_symbol_map_t *map, uint16_t addr)
GB_bank_symbol_t *GB_map_add_symbol(GB_symbol_map_t *map, uint16_t addr, const char *name)
{
size_t index = GB_map_find_symbol_index(map, addr);
size_t index = map_find_symbol_index(map, addr);
map->symbols = realloc(map->symbols, (map->n_symbols + 1) * sizeof(map->symbols[0]));
memmove(&map->symbols[index + 1], &map->symbols[index], (map->n_symbols - index) * sizeof(map->symbols[0]));
@ -39,8 +39,8 @@ GB_bank_symbol_t *GB_map_add_symbol(GB_symbol_map_t *map, uint16_t addr, const c
const GB_bank_symbol_t *GB_map_find_symbol(GB_symbol_map_t *map, uint16_t addr)
{
if (!map) return NULL;
size_t index = GB_map_find_symbol_index(map, addr);
if (index < map->n_symbols && map->symbols[index].addr != addr) {
size_t index = map_find_symbol_index(map, addr);
if (index >= map->n_symbols || map->symbols[index].addr != addr) {
index--;
}
if (index < map->n_symbols) {

View File

@ -27,12 +27,12 @@ typedef struct {
} GB_reversed_symbol_map_t;
#ifdef GB_INTERNAL
void GB_reversed_map_add_symbol(GB_reversed_symbol_map_t *map, uint16_t bank, GB_bank_symbol_t *symbol);
const GB_symbol_t *GB_reversed_map_find_symbol(GB_reversed_symbol_map_t *map, const char *name);
GB_bank_symbol_t *GB_map_add_symbol(GB_symbol_map_t *map, uint16_t addr, const char *name);
const GB_bank_symbol_t *GB_map_find_symbol(GB_symbol_map_t *map, uint16_t addr);
GB_symbol_map_t *GB_map_alloc(void);
void GB_map_free(GB_symbol_map_t *map);
internal void GB_reversed_map_add_symbol(GB_reversed_symbol_map_t *map, uint16_t bank, GB_bank_symbol_t *symbol);
internal const GB_symbol_t *GB_reversed_map_find_symbol(GB_reversed_symbol_map_t *map, const char *name);
internal GB_bank_symbol_t *GB_map_add_symbol(GB_symbol_map_t *map, uint16_t addr, const char *name);
internal const GB_bank_symbol_t *GB_map_find_symbol(GB_symbol_map_t *map, uint16_t addr);
internal GB_symbol_map_t *GB_map_alloc(void);
internal void GB_map_free(GB_symbol_map_t *map);
#endif
#endif /* symbol_hash_h */

View File

@ -8,7 +8,7 @@
#include <sys/time.h>
#endif
static const unsigned GB_TAC_TRIGGER_BITS[] = {512, 8, 32, 128};
static const unsigned TAC_TRIGGER_BITS[] = {512, 8, 32, 128};
#ifndef GB_DISABLE_TIMEKEEPING
static int64_t get_nanoseconds(void)
@ -54,21 +54,30 @@ bool GB_timing_sync_turbo(GB_gameboy_t *gb)
void GB_timing_sync(GB_gameboy_t *gb)
{
if (gb->turbo) {
gb->cycles_since_last_sync = 0;
return;
}
/* Prevent syncing if not enough time has passed.*/
if (gb->cycles_since_last_sync < LCDC_PERIOD / 3) return;
if (gb->turbo) {
gb->cycles_since_last_sync = 0;
if (gb->update_input_hint_callback) {
gb->update_input_hint_callback(gb);
}
return;
}
uint64_t target_nanoseconds = gb->cycles_since_last_sync * 1000000000LL / 2 / GB_get_clock_rate(gb); /* / 2 because we use 8MHz units */
int64_t nanoseconds = get_nanoseconds();
int64_t time_to_sleep = target_nanoseconds + gb->last_sync - nanoseconds;
if (time_to_sleep > 0 && time_to_sleep < LCDC_PERIOD * 1000000000LL / GB_get_clock_rate(gb)) {
if (time_to_sleep > 0 && time_to_sleep < LCDC_PERIOD * 1200000000LL / GB_get_clock_rate(gb)) { // +20% to be more forgiving
nsleep(time_to_sleep);
gb->last_sync += target_nanoseconds;
}
else {
if (time_to_sleep < 0 && -time_to_sleep < LCDC_PERIOD * 1200000000LL / GB_get_clock_rate(gb)) {
// We're running a bit too slow, but the difference is small enough,
// just skip this sync and let it even out
return;
}
gb->last_sync = nanoseconds;
}
@ -86,6 +95,14 @@ bool GB_timing_sync_turbo(GB_gameboy_t *gb)
void GB_timing_sync(GB_gameboy_t *gb)
{
if (gb->cycles_since_last_sync < LCDC_PERIOD / 3) return;
gb->cycles_since_last_sync = 0;
gb->cycles_since_last_sync = 0;
if (gb->update_input_hint_callback) {
gb->update_input_hint_callback(gb);
}
return;
}
#endif
@ -94,9 +111,9 @@ void GB_timing_sync(GB_gameboy_t *gb)
#define IR_THRESHOLD 19900
#define IR_MAX IR_THRESHOLD * 2 + IR_DECAY
static void GB_ir_run(GB_gameboy_t *gb, uint32_t cycles)
static void ir_run(GB_gameboy_t *gb, uint32_t cycles)
{
if (gb->model == GB_MODEL_AGB) return;
if ((gb->model == GB_MODEL_AGB || !gb->cgb_mode) && gb->cartridge_type->mbc_type != GB_HUC1 && gb->cartridge_type->mbc_type != GB_HUC3) return;
if (gb->infrared_input || gb->cart_ir || (gb->io_registers[GB_IO_RP] & 1)) {
gb->ir_sensor += cycles;
if (gb->ir_sensor > IR_MAX) {
@ -137,24 +154,29 @@ static void increase_tima(GB_gameboy_t *gb)
}
}
static void GB_set_internal_div_counter(GB_gameboy_t *gb, uint32_t value)
void GB_set_internal_div_counter(GB_gameboy_t *gb, uint16_t value)
{
/* TIMA increases when a specific high-bit becomes a low-bit. */
value &= INTERNAL_DIV_CYCLES - 1;
uint32_t triggers = gb->div_counter & ~value;
if ((gb->io_registers[GB_IO_TAC] & 4) && (triggers & GB_TAC_TRIGGER_BITS[gb->io_registers[GB_IO_TAC] & 3])) {
uint16_t triggers = gb->div_counter & ~value;
if ((gb->io_registers[GB_IO_TAC] & 4) && (triggers & TAC_TRIGGER_BITS[gb->io_registers[GB_IO_TAC] & 3])) {
increase_tima(gb);
}
/* TODO: Can switching to double speed mode trigger an event? */
if (triggers & (gb->cgb_double_speed? 0x2000 : 0x1000)) {
GB_apu_run(gb);
uint16_t apu_bit = gb->cgb_double_speed? 0x2000 : 0x1000;
if (triggers & apu_bit) {
GB_apu_div_event(gb);
}
else {
uint16_t secondary_triggers = ~gb->div_counter & value;
if (secondary_triggers & apu_bit) {
GB_apu_div_secondary_event(gb);
}
}
gb->div_counter = value;
}
static void GB_timers_run(GB_gameboy_t *gb, uint8_t cycles)
static void timers_run(GB_gameboy_t *gb, uint8_t cycles)
{
if (gb->stopped) {
if (GB_is_cgb(gb)) {
@ -166,11 +188,8 @@ static void GB_timers_run(GB_gameboy_t *gb, uint8_t cycles)
GB_STATE_MACHINE(gb, div, cycles, 1) {
GB_STATE(gb, div, 1);
GB_STATE(gb, div, 2);
GB_STATE(gb, div, 3);
}
GB_set_internal_div_counter(gb, 0);
main:
GB_SLEEP(gb, div, 1, 3);
while (true) {
advance_tima_state_machine(gb);
@ -178,19 +197,14 @@ main:
gb->apu.apu_cycles += 4 << !gb->cgb_double_speed;
GB_SLEEP(gb, div, 2, 4);
}
/* Todo: This is ugly to allow compatibility with 0.11 save states. Fix me when breaking save compatibility */
{
div3:
/* Compensate for lack of prefetch emulation, as well as DIV's internal initial value */
GB_set_internal_div_counter(gb, 8);
goto main;
}
}
static void advance_serial(GB_gameboy_t *gb, uint8_t cycles)
{
if (gb->serial_length == 0) {
if (unlikely(gb->printer_callback && (gb->printer.command_state || gb->printer.bits_received))) {
gb->printer.idle_time += cycles;
}
if (likely(gb->serial_length == 0)) {
gb->serial_cycles += cycles;
return;
}
@ -232,46 +246,196 @@ static void advance_serial(GB_gameboy_t *gb, uint8_t cycles)
}
void GB_set_rtc_mode(GB_gameboy_t *gb, GB_rtc_mode_t mode)
{
if (gb->rtc_mode != mode) {
gb->rtc_mode = mode;
gb->rtc_cycles = 0;
gb->last_rtc_second = time(NULL);
}
}
void GB_set_rtc_multiplier(GB_gameboy_t *gb, double multiplier)
{
if (multiplier == 1) {
gb->rtc_second_length = 0;
return;
}
gb->rtc_second_length = GB_get_unmultiplied_clock_rate(gb) * 2 * multiplier;
}
static void rtc_run(GB_gameboy_t *gb, uint8_t cycles)
{
if (likely(gb->cartridge_type->mbc_type != GB_HUC3 && !gb->cartridge_type->has_rtc)) return;
gb->rtc_cycles += cycles;
time_t current_time = 0;
uint32_t rtc_second_length = unlikely(gb->rtc_second_length)? gb->rtc_second_length : GB_get_unmultiplied_clock_rate(gb) * 2;
switch (gb->rtc_mode) {
case GB_RTC_MODE_SYNC_TO_HOST:
// Sync in a 1/32s resolution
if (gb->rtc_cycles < GB_get_unmultiplied_clock_rate(gb) / 16) return;
gb->rtc_cycles -= GB_get_unmultiplied_clock_rate(gb) / 16;
current_time = time(NULL);
break;
case GB_RTC_MODE_ACCURATE:
if (gb->cartridge_type->mbc_type != GB_HUC3 && (gb->rtc_real.high & 0x40)) {
gb->rtc_cycles -= cycles;
return;
}
if (gb->rtc_cycles < rtc_second_length) return;
gb->rtc_cycles -= rtc_second_length;
current_time = gb->last_rtc_second + 1;
break;
}
if (gb->cartridge_type->mbc_type == GB_HUC3) {
while (gb->last_rtc_second / 60 < current_time / 60) {
gb->last_rtc_second += 60;
gb->huc3.minutes++;
if (gb->huc3.minutes == 60 * 24) {
gb->huc3.days++;
gb->huc3.minutes = 0;
}
}
return;
}
bool running = false;
if (gb->cartridge_type->mbc_type == GB_TPP1) {
running = gb->tpp1_mr4 & 0x4;
}
else {
running = (gb->rtc_real.high & 0x40) == 0;
}
if (running) { /* is timer running? */
while (gb->last_rtc_second + 60 * 60 * 24 < current_time) {
gb->last_rtc_second += 60 * 60 * 24;
if (gb->cartridge_type->mbc_type == GB_TPP1) {
if (++gb->rtc_real.tpp1.weekday == 7) {
gb->rtc_real.tpp1.weekday = 0;
if (++gb->rtc_real.tpp1.weeks == 0) {
gb->tpp1_mr4 |= 8; /* Overflow bit */
}
}
}
else if (++gb->rtc_real.days == 0) {
if (gb->rtc_real.high & 1) { /* Bit 8 of days*/
gb->rtc_real.high |= 0x80; /* Overflow bit */
}
gb->rtc_real.high ^= 1;
}
}
while (gb->last_rtc_second < current_time) {
gb->last_rtc_second++;
if (++gb->rtc_real.seconds != 60) continue;
gb->rtc_real.seconds = 0;
if (++gb->rtc_real.minutes != 60) continue;
gb->rtc_real.minutes = 0;
if (gb->cartridge_type->mbc_type == GB_TPP1) {
if (++gb->rtc_real.tpp1.hours != 24) continue;
gb->rtc_real.tpp1.hours = 0;
if (++gb->rtc_real.tpp1.weekday != 7) continue;
gb->rtc_real.tpp1.weekday = 0;
if (++gb->rtc_real.tpp1.weeks == 0) {
gb->tpp1_mr4 |= 8; /* Overflow bit */
}
}
else {
if (++gb->rtc_real.hours != 24) continue;
gb->rtc_real.hours = 0;
if (++gb->rtc_real.days != 0) continue;
if (gb->rtc_real.high & 1) { /* Bit 8 of days*/
gb->rtc_real.high |= 0x80; /* Overflow bit */
}
gb->rtc_real.high ^= 1;
}
}
}
}
void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles)
{
if (unlikely(gb->speed_switch_countdown)) {
if (gb->speed_switch_countdown == cycles) {
gb->cgb_double_speed ^= true;
gb->speed_switch_countdown = 0;
}
else if (gb->speed_switch_countdown > cycles) {
gb->speed_switch_countdown -= cycles;
}
else {
uint8_t old_cycles = gb->speed_switch_countdown;
cycles -= old_cycles;
gb->speed_switch_countdown = 0;
GB_advance_cycles(gb, old_cycles);
gb->cgb_double_speed ^= true;
}
}
gb->apu.pcm_mask[0] = gb->apu.pcm_mask[1] = 0xFF; // Sort of hacky, but too many cross-component interactions to do it right
// Affected by speed boost
gb->dma_cycles += cycles;
GB_timers_run(gb, cycles);
if (!gb->stopped) {
timers_run(gb, cycles);
if (unlikely(!gb->stopped)) {
advance_serial(gb, cycles); // TODO: Verify what happens in STOP mode
}
if (unlikely(gb->speed_switch_halt_countdown)) {
gb->speed_switch_halt_countdown -= cycles;
if (gb->speed_switch_halt_countdown <= 0) {
gb->speed_switch_halt_countdown = 0;
gb->halted = false;
}
}
gb->debugger_ticks += cycles;
if (gb->speed_switch_freeze) {
if (gb->speed_switch_freeze >= cycles) {
gb->speed_switch_freeze -= cycles;
return;
}
cycles -= gb->speed_switch_freeze;
gb->speed_switch_freeze = 0;
}
if (!gb->cgb_double_speed) {
if (unlikely(!gb->cgb_double_speed)) {
cycles <<= 1;
}
gb->absolute_debugger_ticks += cycles;
// Not affected by speed boost
if (gb->io_registers[GB_IO_LCDC] & 0x80) {
if (likely(gb->io_registers[GB_IO_LCDC] & 0x80)) {
gb->double_speed_alignment += cycles;
}
gb->hdma_cycles += cycles;
gb->apu_output.sample_cycles += cycles;
gb->apu_output.sample_cycles += cycles * gb->apu_output.sample_rate;
gb->cycles_since_last_sync += cycles;
gb->cycles_since_run += cycles;
if (gb->rumble_state) {
gb->rumble_on_cycles++;
}
else {
gb->rumble_off_cycles++;
}
if (!gb->stopped) { // TODO: Verify what happens in STOP mode
gb->rumble_on_cycles += gb->rumble_strength & 3;
gb->rumble_off_cycles += (gb->rumble_strength & 3) ^ 3;
if (unlikely(!gb->stopped)) { // TODO: Verify what happens in STOP mode
GB_dma_run(gb);
GB_hdma_run(gb);
}
GB_apu_run(gb);
GB_display_run(gb, cycles);
GB_ir_run(gb, cycles);
GB_apu_run(gb, false);
GB_display_run(gb, cycles, false);
ir_run(gb, cycles);
rtc_run(gb, cycles);
}
/*
@ -284,63 +448,14 @@ void GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac)
/* Glitch only happens when old_tac is enabled. */
if (!(old_tac & 4)) return;
unsigned old_clocks = GB_TAC_TRIGGER_BITS[old_tac & 3];
unsigned new_clocks = GB_TAC_TRIGGER_BITS[new_tac & 3];
unsigned old_clocks = TAC_TRIGGER_BITS[old_tac & 3];
unsigned new_clocks = TAC_TRIGGER_BITS[new_tac & 3];
/* The bit used for overflow testing must have been 1 */
if (gb->div_counter & old_clocks) {
/* And now either the timer must be disabled, or the new bit used for overflow testing be 0. */
if (!(new_tac & 4) || gb->div_counter & new_clocks) {
if (!(new_tac & 4) || !(gb->div_counter & new_clocks)) {
increase_tima(gb);
}
}
}
void GB_rtc_run(GB_gameboy_t *gb)
{
if (gb->cartridge_type->mbc_type == GB_HUC3) {
time_t current_time = time(NULL);
while (gb->last_rtc_second / 60 < current_time / 60) {
gb->last_rtc_second += 60;
gb->huc3_minutes++;
if (gb->huc3_minutes == 60 * 24) {
gb->huc3_days++;
gb->huc3_minutes = 0;
}
}
return;
}
if ((gb->rtc_real.high & 0x40) == 0) { /* is timer running? */
time_t current_time = time(NULL);
while (gb->last_rtc_second + 60 * 60 * 24 < current_time) {
gb->last_rtc_second += 60 * 60 * 24;
if (++gb->rtc_real.days == 0) {
if (gb->rtc_real.high & 1) { /* Bit 8 of days*/
gb->rtc_real.high |= 0x80; /* Overflow bit */
}
gb->rtc_real.high ^= 1;
}
}
while (gb->last_rtc_second < current_time) {
gb->last_rtc_second++;
if (++gb->rtc_real.seconds == 60) {
gb->rtc_real.seconds = 0;
if (++gb->rtc_real.minutes == 60) {
gb->rtc_real.minutes = 0;
if (++gb->rtc_real.hours == 24) {
gb->rtc_real.hours = 0;
if (++gb->rtc_real.days == 0) {
if (gb->rtc_real.high & 1) { /* Bit 8 of days*/
gb->rtc_real.high |= 0x80; /* Overflow bit */
}
gb->rtc_real.high ^= 1;
}
}
}
}
}
}
}

View File

@ -1,14 +1,24 @@
#ifndef timing_h
#define timing_h
#include "gb_struct_def.h"
#include "defs.h"
typedef enum {
GB_RTC_MODE_SYNC_TO_HOST,
GB_RTC_MODE_ACCURATE,
} GB_rtc_mode_t;
/* RTC emulation mode */
void GB_set_rtc_mode(GB_gameboy_t *gb, GB_rtc_mode_t mode);
/* Speed multiplier for the RTC, mostly for TAS syncing */
void GB_set_rtc_multiplier(GB_gameboy_t *gb, double multiplier);
#ifdef GB_INTERNAL
void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles);
void GB_rtc_run(GB_gameboy_t *gb);
void GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac);
bool GB_timing_sync_turbo(GB_gameboy_t *gb); /* Returns true if should skip frame */
void GB_timing_sync(GB_gameboy_t *gb);
internal void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles);
internal void GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac);
internal bool GB_timing_sync_turbo(GB_gameboy_t *gb); /* Returns true if should skip frame */
internal void GB_timing_sync(GB_gameboy_t *gb);
internal void GB_set_internal_div_counter(GB_gameboy_t *gb, uint16_t value);
enum {
GB_TIMA_RUNNING = 0,
GB_TIMA_RELOADING = 1,
@ -18,13 +28,23 @@ enum {
#define GB_SLEEP(gb, unit, state, cycles) do {\
(gb)->unit##_cycles -= (cycles) * __state_machine_divisor; \
if ((gb)->unit##_cycles <= 0) {\
if (unlikely((gb)->unit##_cycles <= 0)) {\
(gb)->unit##_state = state;\
return;\
unit##state:; \
}\
} while (0)
#define GB_BATCHPOINT(gb, unit, state, cycles) do {\
unit##state:; \
if (likely(__state_machine_allow_batching && (gb)->unit##_cycles < (cycles * 2))) {\
(gb)->unit##_state = state;\
return;\
}\
} while (0)
#define GB_BATCHED_CYCLES(gb, unit) ((gb)->unit##_cycles / __state_machine_divisor)
#define GB_STATE_MACHINE(gb, unit, cycles, divisor) \
static const int __state_machine_divisor = divisor;\
(gb)->unit##_cycles += cycles; \
@ -34,6 +54,10 @@ if ((gb)->unit##_cycles <= 0) {\
switch ((gb)->unit##_state)
#endif
#define GB_BATCHABLE_STATE_MACHINE(gb, unit, cycles, divisor, allow_batching) \
const bool __state_machine_allow_batching = (allow_batching); \
GB_STATE_MACHINE(gb, unit, cycles, divisor)
#define GB_STATE(gb, unit, state) case state: goto unit##state
#define GB_UNIT(unit) int32_t unit##_cycles, unit##_state

View File

@ -3,7 +3,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include "gb_struct_def.h"
#include "defs.h"
typedef struct {

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Some files were not shown because too many files have changed in this diff Show More