The author details their process of compiling OCaml code to run on a TI-84 Plus CE calculator. They leveraged the calculator's existing C toolchain and the OCaml compiler's ability to output C code. After overcoming challenges like limited RAM and the absence of a dynamic linker, they successfully ran a simple "Hello, world!" program. The key innovations included statically linking the OCaml runtime and using a custom, minimized runtime configuration to fit within the calculator's memory constraints. This allowed for direct execution of OCaml bytecode on the calculator, offering a novel approach to programming these devices.
This blog post details the author's project of enabling OCaml program execution on a TI-84 Plus CE graphing calculator. The author was motivated by a desire to explore low-level programming and leverage the calculator's readily available hardware, as opposed to relying on emulators. The project faced several significant challenges.
Firstly, the TI-84 CE utilizes a Z80 processor, an architecture not directly supported by the OCaml compiler. To overcome this, the author opted to compile OCaml code into C code using the js_of_ocaml compiler, a somewhat unconventional choice driven by its relatively simple output compared to the standard OCaml compiler. This C code was then further compiled into Z80 assembly using SDCC, a Small Device C Compiler. This multi-stage compilation process added complexity to the project.
Secondly, the limited resources of the calculator, specifically its RAM, posed a constraint. The author addressed this by carefully configuring the js_of_ocaml compiler to minimize the generated C code's size and meticulously managing memory allocation. Strategies like using a custom allocator were explored and detailed in the blog post. Even with these optimizations, the resulting programs were still quite large, pushing the boundaries of the calculator's memory capacity.
Thirdly, integrating the compiled code into the calculator's operating system required understanding its internal workings. The author employed a technique of patching the calculator's OS to intercept a specific key combination, which would then trigger the execution of the OCaml program. This allowed for seamless integration without requiring a complete OS overhaul.
The post concludes by showcasing a successful "Hello, World!" example running on the calculator. This demonstration served as proof of concept, verifying the viability of running OCaml-derived programs on the TI-84 CE despite the numerous technical hurdles. The author acknowledges that the process is still somewhat cumbersome and highlights potential areas of future improvement, such as exploring alternative compilation strategies or directly targeting Z80 from OCaml. While not yet a fully polished solution, the project represents a significant step towards bringing a powerful functional programming language to a readily available and widely familiar platform.
Summary of Comments ( 1 )
https://news.ycombinator.com/item?id=44017560
Hacker News users generally expressed enthusiasm for the project of compiling OCaml to a TI-84 calculator. Several commenters praised the technical achievement, highlighting the challenges of working with the calculator's limited resources. Some discussed potential educational benefits, suggesting it could be a powerful tool for teaching functional programming. Others reminisced about their own calculator programming experiences and pondered the possibility of porting other languages. A few users inquired about practical aspects like performance and library support. There was also some discussion comparing the project to other calculator-based language implementations and exploring potential future enhancements.
The Hacker News post titled "Compiling OCaml to the TI-84 CE Calculator" has generated several interesting comments. Many users express excitement and admiration for the project. One commenter points out the impressive nature of getting a garbage-collected language like OCaml running on such limited hardware. They highlight the challenge of memory management in this constrained environment.
Several commenters reminisce about their own experiences with TI calculators and programming them in languages like TI-BASIC and Z80 assembly. They express a sense of nostalgia and appreciation for the ingenuity involved in pushing the limits of these devices. One commenter even shares a link to their old TI-83+ assembly programs.
There's discussion about the practical applications of running OCaml on a calculator, with some suggesting potential uses in education, particularly for teaching functional programming concepts. One commenter questions the practical value beyond the "cool factor" but acknowledges the technical achievement.
The technical details of the project are also discussed. One comment mentions using a custom bytecode interpreter, while another asks about the implementation of the garbage collector. A commenter also points out the potential for optimization by directly targeting the Z80 processor instead of using bytecode.
Some comments focus on the broader implications of the project. One commenter wonders about the possibility of porting other high-level languages to similar platforms, while another discusses the challenges and benefits of targeting resource-constrained devices. The project is seen as a demonstration of the power and flexibility of OCaml.
Finally, there's some lighthearted banter and jokes related to calculators and OCaml. One user jokingly suggests porting Doom to the calculator next.