melonDS - now also for macOS!


Yep.
If you want to test it, scroll down to the bottom of the post. I’ll be explaining about what needed to be changed for it to work.

This originally started as a little challenge. "It shouldn't be that hard," I thought. However, it wasn't as easy as I would have hoped, but I got there in the end.

- The JIT recompiler

Thanks Generic (aka RSDuck) for helping me out a lot here and guiding me!

Fastmem

It mapped memory using "memfd_create()" on Linux, which didn't exist on macOS. Instead, on macOS shm_open is used to create the fastmem memory.
macOS also didn't have "->gregs" in "uc_mcontext" and no "REG_RIP" either. This has to be changed to "->__ss.__rip" instead.
Then, it would crash with a "bus error" on attempting to load. This was caused because macOS returned "bus error" instead of "segmentation fault", so the signal handler couldn't handle it.
Note: fastmem was disabled because it caused all sorts of errors while trying to boot firmware or run games. If anyone manages to fix it, send a pull request!

The JIT itself

The JIT would build, but at link time it would complain about "ARM_Dispatch" and "ARM_Ret" being undefined. Apparently in the Mach-O format (used in macOS) global function names defined in assembly are required to be prepended by an underscore.
Then it would crash upon booting firmware or trying to load a game. This was caused by the line here which tried to reprotect some memory to make it executable. On macOS, new memory is now mmap'ed instead.

- The OpenGL renderer

macOS complained about not being able to find "GL/gl.h" and "GL/glext.h". These includes had to be changed to "OpenGL/gl3.h" and "OpenGL/gl3ext.h" on macOS and the OpenGL framework was linked.
Also the functions defined by the OpenGL macronator already existed on macOS, which caused "ambiguous reference" errors. The macronator was ifndef'd out.

- Direct Mode

Direct Mode used "AF_PACKET" to get the MAC address, which doesn't exist on macOS. "AF_LINK" was used instead.
The library names of libpcap had to be changed to "libpcap.A.dylib" and "libpcap.dylib" on macOS.

- Binding Keys

This was a simple fix. For some reason, macOS didn't give focus to the buttons in the key binding menu when they were pressed, which meant that they couldn't detect keys. I had to set the focus policy to Qt::StrongFocus to get them to accept focus.

- App Bundle

Now it built fine and it worked, but it came as a Unix executable, not a macOS app bundle. I had to add some lines in CMakeLists.txt to make it build an app bundle.
I also generated a macOS ".icns" icon file for melonDS, so now the icon showed up on the app bundle.

- No libslirp available in Homebrew

Homebrew (the package manager) didn't have libslirp in their repositories, so I created a pull request here which was merged.


Here are the downloads. If you find any issues, make sure you comment here and tell me so I can fix it!
You will have to install the appropriate libraries beforehand with the Homebrew Package Manager.
In Terminal paste the following command to install the required libraries.
brew install qt@5 sdl2 libslirp
NOTE: melonDS now ships macOS releases as of 0.9.1. We recommend you to use that instead.
melonDS 0.9 beta for macOS x86_64
To unzip the above download, you may need to use a program like The Unarchiver.
Joel says:
Dec 2nd 2020
I read that after I made the post. However, it still was a good thing to post the result of trying to run them. A lot of Mac users might not have tried melonDS before and not know about that.
Rayyan says:
Dec 3rd 2020
Joel:
Re - Building melonDS on ARM Macs

In the build directory, could you remove CMakeCache.txt, then run
cmake .. -DQt5_DIR=$(brew --prefix qt5)/lib/cmake/Qt5 -DENABLE_JIT=OFF

then try building with make?
Joel says:
Dec 3rd 2020
That leads to a lot of errors in the make command. I'll move the discussion to PMs through the forums instead of cluttering up the blog entry.
Joel says:
Dec 8th 2020
HoLLy says:
Nov 30th 2020
Does this include the new ARM Macs? I imagine there wouldn't be that much need for the JIT on those.

poudink says:
Nov 30th 2020
No, there very much would. An ARM64 JIT is still a JIT.


Well, not really. Testing under Rosetta 2 on my M1 MBP gives the same performance with and without the JIT turned on. That being said, MelonDS would not build on my system with JIT and QT Frontend errors. Waluigi was able to use my system to troubleshoot and has submitted a fix for jit but it'll take time to resolve the QT issues.
extherian says:
Dec 10th 2020
At long last, I can ditch DeSmuME for good. Never again will I have to put up with abuse on their forums because I dared to make a request that the developers didn't like. I thought this day would never come.
Rayyan says:
Dec 10th 2020
extherian: Thanks!
By any chance, have you tested it yet?
Joel says:
Dec 10th 2020
I'm curious about if they have tried it yet also. I encountered an issue with OpenGL staying at a 1x screen size late last night and would like to know if it does that on Intel Macs also.
kevincrans says:
Dec 23rd 2020
Excuse me, but if melonds was available for 32-bits mojave, it should run on the m1. I don't if metal or 32-bits are a concern. Now I wanna run dolphin on a powermac lol.
StLouisCPhT says:
Dec 23rd 2020
M1 Macs can only run 64-bit Apps, either as an Intel Only build through Rosetta 2 translation, Universal Build (Intel/Apple Silicon combo) or a purely Apple Silicon native build.
Rayyan says:
Dec 23rd 2020
Also melonDS was never available for 32-bit.
Teone says:
Jan 2nd 2021
I found an Error! (Using MacOS Catilina)

Process: melonDS [7847]
Path: /private/var/folders/*/melonDS.app/Contents/MacOS/melonDS
Identifier: net.kuribo64.melonDS
Version: 0.9 (0.9)
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Responsible: melonDS [7847]
User ID: 502

Date/Time: 2021-01-02 22:26:31.495 +1100
OS Version: Mac OS X 10.15.7 (19H15)
Report Version: 12
Anonymous UUID: 3912EE42-0BC3-AD11-DADF-D11AA1F8EE6E

Sleep/Wake UUID: F908EF64-7AF9-4F42-90D9-B27E4DB215A5

Time Awake Since Boot: 140000 seconds
Time Since Wake: 11000 seconds

System Integrity Protection: enabled

Notes: Translocated Process

Crashed Thread: 0

Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY

Termination Reason: DYLD, [0x1] Library missing

Application Specific Information:
dyld: launch, loading dependent libraries

Dyld Error Message:
Library not loaded: /usr/local/opt/sdl2/lib/libSDL2-2.0.0.dylib
Referenced from: /private/var/folders/*/melonDS.app/Contents/MacOS/melonDS
Reason: image not found

Binary Images:
0x10b4d9000 - 0x10b5f0fff +net.kuribo64.melonDS (0.9 - 0.9) <DDCC7775-CC41-3841-95BC-6A45691E88D4> /var/folders/*/melonDS.app/Contents/MacOS/melonDS
0x1129a1000 - 0x112a32f47 dyld (750.6) <1D318D60-C9B0-3511-BE9C-82AFD2EF930D> /usr/lib/dyld
0x7fff672f1000 - 0x7fff672f2fff libSystem.B.dylib (1281.100.1) <0A6C8BA1-30FD-3D10-83FD-FF29E221AFFE> /usr/lib/libSystem.B.dylib

Model: iMac16,2, BootROM 236.0.0.0.0, 4 processors, Quad-Core Intel Core i5, 2.8 GHz, 8 GB, SMC 2.32f21
Graphics: kHW_IntelIrisPro6200Item, Intel Iris Pro Graphics 6200, spdisplays_builtin
Memory Module: BANK 0/DIMM0, 4 GB, DDR3, 1867 MHz, 0x80AD, 0x483943434E4E4E424C54424C41522D4E5544
Memory Module: BANK 1/DIMM0, 4 GB, DDR3, 1867 MHz, 0x80AD, 0x483943434E4E4E424C54424C41522D4E5544
AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0x156), Broadcom BCM43xx 1.0 (7.77.111.1 AirPortDriverBrcmNIC-1615.2)
Bluetooth: Version 7.0.6f7, 3 services, 27 devices, 1 incoming serial ports
Network Service: Wi-Fi, AirPort, en1
Serial ATA Device: APPLE HDD HTS541010A9E662, 1 TB
USB Device: USB 3.0 Bus
USB Device: Bluetooth USB Host Controller
USB Device: FaceTime HD Camera (Built-in)
USB Device: USB Flash Drive
USB Device: Magic Keyboard
Thunderbolt Bus: iMac, Apple Inc., 27.4
Rayyan says:
Jan 2nd 2021
Teone: Install the libraries. I mention how to do this in the post.
Junior says:
Jan 10th 2021
After completing everything in the steps, when I try to load a rom it say DS arm9 bios was not found or could not be accessed. Check your emu settings.

Where do I go from here ?
Rayyan says:
Jan 11th 2021
Junior: You have to configure it in the Emu Settings -> DS tab.
Anyway, this is old.
You should be using the 0.9.1 release (the latest).
guywithm1 says:
Jan 16th 2021
Hello guys. I have an m1 Mac and could give you remote access to it if you need to do compiling or something. Just message me at olcaymi9t@gmail.com, because I'll probably not come online on this forum again.

Post a comment
Name:
DO NOT TOUCH