stevenvar / OMicroB

An OCaml generic virtual machine for microcontrollers
Other
138 stars 23 forks source link

Could we port OMicroB to the Numworks CPU (an ARMv7-M Cortex-M7 on 32bits) ? Step ONE in a larger project #36

Open Naereen opened 5 days ago

Naereen commented 5 days ago

Hi there everybody :smiley: !

Following the discussion started on #14 I'll explain what I would like to achieve, here. Sorry if this is lengthy, I prefer to explain clearly and in details.

As the title of the issue suggest, I would like to continue the work done by @Vertmo into porting the OMicroB project to Micro:bit, and port it now to another ARM32 processor, which is very close to the Micro:bit and thus it shouldn't be too hard. From what I understood, the Micro:bit is running on a Arm Cortex-M4 32 bit processor with FPU, with RAM 128KB and at 64MHz. Quite less than what the Numworks calculators offer, but in a very close setup!

About the Numworks

The Numworks calculators are open-source: their OS is Epsilon, written in C++ (and parts in C). Its CPU is an ARMv7-M Cortex-M7 on 32bits, with its core clocked at 216 MHz and 256K of Static RAM. There exists two other open-source fork OS, Upsilon and Omega. I'm aiming only at Epsilon, to aim at the largest possible audience.

By default on the main OS, there is already a MicroPython interpreter as well as a (very good) text editor. All that is written in C++ and in C. It works amazingly well, considering the limited hardware...

My goal(s)

My first goal: I would like to port OMicroB to the Numworks devices. That is, being able to write on my laptop, in OCaml, tiny code that could be compiled to bytecode, and then to the correct assembly language (then binary) for the CPU of the Numworks, using OMicroB. But because of the nature of the calculator environment, I don't want to flash the program to the Numworks. If I do that, I'm most certain that nothing will work. And even if it worked, that would overwrite the OS of the Numworks, that's not what I want. Instead, such assembly language or object (.arm_o file) could then be used to be embedded in an application for the Numworks (see below).

My dream goal: it is to have the same experience as the Python app (that is, a full text editor, interpreter + terminal) for the OCaml language. If not possible, most likely because the RAM of the Numworks is limited, then I could focus instead on the camllight language, or even on tinier OCaml-like languages (like Mini-Caml-Interpreter). My guess is that, if the language interpreter itself is written in pure C (like it is the case for Lua, see below, or for caml-light), this should be possible: all I would need is to have a execute_this_phrase(const char* line_of_code) function, available in the rest of the C code.

What I tried so far

It is possible to write an installable ("flashable") application (a .nwa file) either in C++, but also [in C](https://github.com/numworks/epsilon-sample-app-c and in Rust. Following the tutorial, I successfully wrote a tiny C application, which does nothing interesting yet, to try it out. I want to add at least one object file, compiled for the correct ARM32 bit architecture, to this C application, like what is done for the lua app (see below also).

I tried (for hours, all weekend long) to obtain on my laptop a compiler for OCaml that could produce object code for ARM 32bits, this way I hoped of being able to follow the official tutorial which explains how to include compiled OCaml code in a C app. I wanted to have a tiny C app for the Numworks, where a very simple basic function could be written in OCaml separately (on the laptop), and the called in the app. I tried all the projects I could find online, none of them worked for me (and I tried a lot). This lead me to be interested in OMicroB.

Yesterday, I tried using the omicrob -device microbit2 compiler, to produce a .arm_o object file, from a very tiny OCaml application, targeting an ARM32 processor. I encountered several issues, mainly that there are no print_... functions, thus making the whole process quite hard: if the OCaml code cannot print to the screen of the Numworks, it is quite pointless. Let's address this issue later. Then when I focused on a simpler program, I managed to compile it (and obtain a bunch of compiled file, one being the .arm_o object file). I tried to include it in my baby C app, and got a lot of compilation issues (from what I gathered, because of float-abi=hard vs float-abi=softfp differences, and because the main.c file declares a main() function and the compiled OCaml code also declares a main() function).

An app for Lua: a possible inspiration

And... There is also the lua application, which ports a full Lua 5.4.4 interpreter on the Numworks. It doesn't ship a text editor, and only loads and prints the result of one script, that has to be shipped with the app while installing it. It's not amazing in terms of user experience, but it's already great.

If that is not possible to ship a text editor + interpreter (+ terminal/toplevel?) on the Numworks, then at least I would be happy to be have what the Lua app provides, or even less.

Many thanks for reading all the way down. I'll be actively working on this for the next two weeks at least. Thanks if you can help!