OCaml interpeter on the nintendo 3DS

In the last few days I was trying to create an interpreter for the nintendo 3DS, yet I was unable to. I do not think it is impossible, but it is certainly a not an easy project.
My idea was to compile C code (since the most of ocaml library is written using c, and I can use c to compile 3DS apps), but I struggled to compile a basic example.

The concept is as follows, the program is booted up, it has no previous memory.
The user inputs a string, the c code gives it to the “ocaml” interpreter (which is C code as well), the ocaml interpreter evaluates it and returns back the output.

Known limitations:

  • Not all libraries/functionality are probably portable to the 3DS
  • It must be a version of ocaml before version 5. The 3DS has a 32bit arm processor, and the version 5 can be only ran on a 64bit machine.
  • Code has to be compiled with the 3DS library with devkitpro
export HOST=arm-none-eabi
export TARGET=arm-none-eabi
export PATH=/mnt/c/devkitPro/devkitARM/bin:$PATH

wget https://github.com/ocaml/ocaml/archive/refs/tags/4.14.0.tar.gz
tar xzf 4.14.0.tar.gz
cd ocaml-4.14.0

./configure --host=$HOST --target=$TARGET

make world
make install

This is my try at a script to create the libraries needed for compilation, but I am afraid that it will never compile since the arm-none-eabi option is not present.

I have tried different options, but so far, none of them succeeded.
I think the next step would be probably trying to replace the runtime functions of ocaml’s code with the 3DS ones, but that would take considerable effort to do so.

(This is a repost of the stackoverflow question, along with the one from gbatemp)

2 Likes

Continuing with my research, I have stumbled upon this thread: https://discuss.ocaml.org/t/cross-compiling-for-embedded-arm32-target-rp2040-cortex-m0/11585/2
where the users mention an arm processor (from a raspberry pi computer), but unfortunately not much code that I would be able to run was mentioned.
Does anyone have a guess on how to proceed?

After thinking more, I will need an evaluation file, which I might have to write myself and static libraries linked to this file. This evaluation file will be called from the main 3DS function to parse the string, and return an evaluation as another string, but I am still struggling with creating the static libraries.
In one very old stackoverflow post: https://stackoverflow.com/questions/9472371/cross-compiling-ocaml-apps-for-arm?rq=3 which mentions crosscompilation to ARM processor using qemu with debian, but:
A) nintendo 3ds probably does not use debian
B) the thread itself is from 12 years ago
C) unfortunately does not do the best job on how to proceed

Lastly I’ve found this thread: /questions/27061506/ocaml-for-arm-cortex-m4, which also deals with the implementation of ocaml on arm cpu. But, it counts on the program having an existing OS to function with.

I do not really want to install linux on the machine, I don’t think it would solve all that many problems. The current code that I have for the 3ds gets the input natively from the user and that’s where I would expect the output from the ocaml, but it is a bit complicated.

Is the next best path to try compiling bytecode for the 3ds?

Hi! I’m interested in this question, as I opened back in 2021 this question on adding an OCaml interpreter for a graphical calculator (from the Numworks company). Let me know if you have any news!

@Ventuaer I took a quick peek at the specs for 3DS (128MB RAM; ARMv6) and devkitARM (GCC-based cross toolchain with pacman-installable binaries and libs). That is very similar to the Android pre-NDK23 toolchain (typically 0.5-4GB RAM devices; ARMv7; GCC-based cross toolchain w/ prebuilt binaries and libs). It sounds quite doable; a good starting point is [ANN] Simplified Android cross-compiler with DkML. Then fork dkml-compiler and add your own variation of env/android-ndk-env-to-ocaml-configure-env.sh. A few pieces of caution:

  1. The ARMv7 OCaml assembly generation backend had a couple serious bugs; the patches are in the dkml-compiler project. You might need to patch ARMv6.
  2. devkitARM has no ability to do shared libraries. I don’t think that will be a major problem, but I don’t know. At minimum, you’ll need to comment out the parts of dkml-compiler that build shared libraries in your fork.
  3. 3DS has somewhat low memory (128MB). You should stay bytecode only (comment out the parts of dkml-compiler that build native code in your fork) and I suspect you may want to patch the memory constants in runtime/caml/config.h.
  4. Unclear why you need OCaml on 3DS, so you are really on your own for support, even if others could get this project completed. Run into SIGSEGV because of an ARMv6 asmgen bug? You attach gdb, find the culprit and modify the asmgen backend. Have a question about modding dkml-compiler or how ocaml’s ./configure works? You have to figure it out yourself. Etc.

All the best.

1 Like