logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Hacking-on-your-TI-calculator.md (10087B)


  1. ---
  2. date: 2014-02-25
  3. title: Hacking on your TI calculator
  4. layout: post
  5. tags: [KnightOS, kernel hacking]
  6. ---
  7. I've built the [KnightOS kernel](https://github.com/KnightOS/kernel), an open-source OS that runs on
  8. several TI calculator models, including the popular TI-83+ family, and recently the new TI-84+ Color
  9. Silver Edition. I have published some information on how to build your own operating sytsems for these
  10. devices, but I've learned a lot since then and I'm writing this blog post to include the lessons I've
  11. learned from other attempts.
  12. ## Prerequisites
  13. Coming into this, you should be comforable with z80 assembly. It's possible to write an OS for these
  14. devices in C (and perhaps other high-level languages), but proficiency in z80 assembly is still
  15. required. Additionally, I don't consider C a viable choice for osdev on these devices when you
  16. consider that the available compliers do not optimize the result very well, and these devices have
  17. very limited resources.
  18. You will also have to be comfortable (though not neccessarily expert-level) with these tools:
  19. * make
  20. * The assembler of your choice
  21. * The toolchain of your choice
  22. I'm going to gear this post from the perspective of a Linux user, but Windows users should be able to
  23. do fine with cygwin. If you're looking for a good assembler, I suggest
  24. [sass](https://github.com/KnightOS/sass), the assembler KnightOS uses. I built it myself to address
  25. the needs of the kernel, and it includes several nice features that make it easier to maintain such a
  26. large and complex codebase. Other good choices include
  27. [spasm](https://wabbit.codeplex.com/releases/view/45088) and
  28. [brass](https://code.google.com/p/brass-assembler/).
  29. For your toolchain, there are a few options, but I've built custom tools that work well for KnightOS
  30. and should fit into your project as well. You need to accomplish a couple of tasks:
  31. * [Create ROM files from assembler output](https://github.com/KnightOS/MakeROM)
  32. * [Create OS upgrades from ROM files](https://github.com/KnightOS/CreateUpgrade)
  33. You also need the [cryptographic signing keys](http://brandonw.net/calculators/keys/) for any of the
  34. calculators you intend to support. There are ways to get around using these (which you'll need to
  35. research for the TI-84+ CSE, for example) that you may want to look into. These keys will allow you
  36. to add a cryptographic signature on your OS upgrades that will make your calculator think it's an
  37. official Texas Instruments operating system, and you will be able to send it to the device. The
  38. CreateUpgrade tool linked above produces signed upgrade files for you, but if you choose to use other
  39. tools you may need to find a seperate signing tool.
  40. Additonally, if you target devices with a newer boot code, you'll have to reflash your boot code or
  41. use a tool like [UOSRECV](http://brandonw.net/calcstuff/uosrecv.zip) to send your OS to an actual
  42. device.
  43. ## What you're getting into
  44. You will be replacing everything on the calculator with your own system (though if you want to retain
  45. compatability with TIOS like [OS2](http://brandonw.net/calculators/OS2/) tried to, feel free). You'll
  46. need to do *everything*, including common things like providing your own multiplication functions, or
  47. drawing functions, or anything else. You'll also be responsible for initializing the calculator and
  48. all of the hardware you want to use (such as the LCD or keypad).
  49. That being said, you can take some code from projects like the KnightOS kernel to help you out. The
  50. KnightOS kernel is open sourced under the MIT license, which means you're free to take any code from
  51. it and use it in your own project. I also strongly suggest using it as a reference for when you get
  52. stuck.
  53. The advantage to taking on this task is that you can leverage the full potential of these devices.
  54. What you're building for is a 6/15 MHz z80 with 32K or more of RAM, plus plenty of Flash and all
  55. sorts of fun hardware. You can also build something that frees your device of proprietary code, if
  56. that is what you are interested in (though the proprietary boot code would remain - but that's a
  57. story for another day).
  58. If you plan on making a full blown operating systems that can run arbituary programs and handle all
  59. sorts of fun things, you'll want to make sure you have a strong understanding of programming in
  60. general, as well as solid algorithmic knowledge and low-level knowledge. If you don't know how to
  61. use pointers or bit math, or don't fully understand the details of the device, you may want to try
  62. again when you do. That being said, I didn't know a lot when I started KnightOS (as the community was
  63. happy to point out), and now I feel much more secure in my skills.
  64. ## Building the basic OS
  65. We'll build a simple OS here to get you started, including booting the thing up and showing a
  66. simple sprite on the screen. First, we'll create a simple Makefile. This OS will run on the
  67. TI-73, TI-83+, TI-83+ SE, TI-84+, TI-84+ SE, and TI-84+ CSE, as well as the French variations
  68. on these devices.
  69. [Grab this tarball](/demo_os.tar.gz) with the basic OS to get started. It looks like this:
  70. .
  71. ├── build
  72. │   ├── CreateUpgrade.exe
  73. │   ├── MakeROM.exe
  74. │   └── sass.exe
  75. ├── inc
  76. │   └── platforms.inc
  77. ├── Makefile
  78. └── src
  79. ├── 00
  80. │   ├── base.asm
  81. │   ├── boot.asm
  82. │   ├── display.asm
  83. │   └── header.asm
  84. └── boot
  85. └── base.asm
  86. If you grab this, run `make all` and you'll get a bunch of ROM files in the `bin` directory.
  87. I'll explain a little bit about how it works. The important file here is `boot.asm`, but I
  88. encourage you to read whatever else you feel like - especially the Makefile.
  89. ### Miscellaneous Files
  90. Here is the purpose of each file, save for boot.asm (which gets its own section later):
  91. * The makefile is like a script for building the OS. You should probably learn how these work
  92. if you don't already.
  93. * Everything in build/ is part of the suggested toolchain.
  94. * The inc folder can be #included to, and includes `platforms.inc`, which defines a bunch of
  95. useful constants for you.
  96. * `base.asm` is just a bunch of #include statements, for linking without a linker
  97. * `display.asm` has some useful display code I pulled out of KnightOS
  98. * `header.asm` contains the OS header and RST list
  99. ### boot.asm
  100. The real juciy stuff is boot.asm. This file initializes everything and draws a smiley face
  101. in the middle of the screen. Here's what it does (in order):
  102. 1. Disable interrupts
  103. 2. Set up memory mappings
  104. 3. Create a stack and set SP accordingly
  105. 4. Initialize the LCD (B&W or color)
  106. 5. Draw a smiley face
  107. I'm sure your OS will probably want to do more interesting things. The KnightOS kernel, for
  108. example, adds on top of this a bunch of kernel state initialization, filesystem initialization,
  109. and loads up a boot program.
  110. `boot.asm` is well-commented and I encourage you to read through it to get an idea of what
  111. needs to be done. The most complicated and annoying bit is the color LCD initialization, which is
  112. mostly in `display.asm`.
  113. I encourage you to spend some time playing with this. Bring in more things and try to build
  114. something simple. Remember, you have no bcalls here. You need to build everything yourself.
  115. ## Resources
  116. There are several things you might want to check out. The first and most obvious is
  117. [WikiTI](http://wikiti.brandonw.net/index.php?title=Calculator_Documentation). I don't use much
  118. here except for the documentation on I/O ports, and you'll find it useful, too.
  119. The rest of the resources here are links to code in the KnightOS kernel.
  120. The [interrupt handler](https://github.com/KnightOS/kernel/blob/master/src/00/interrupt.asm#L19)
  121. is a good reference for anyone wanting to work with interrupts to do things like handle the ON
  122. button, link activity, or timers. One good use case here (and what KnightOS uses it for) is
  123. preemptive multitasking. Note that you might want to use `exx` and `ex af, af'` instead of
  124. pushing all the registers like KnightOS does. Take special note of how we handle USB activity.
  125. You might want to consider offering some sort of color LCD compatabilty mode like KnightOS does.
  126. This allows you to treat it like a black & white screen. The relevant code is
  127. [here](https://github.com/KnightOS/kernel/blob/master/src/00/display-color.asm).
  128. If you want to interact with the keyboard, you'll probably want to reference the KnightOS
  129. keyboard code [here](https://github.com/KnightOS/kernel/blob/master/src/00/keyboard.asm). You
  130. might also consider working out an interrupt-based keyboard driver.
  131. If you'd like to manipulate Flash, you need to run most of it from RAM. You will probably want
  132. to reference the [KnightOS Flash driver](https://github.com/KnightOS/kernel/blob/master/src/00/flash.asm).
  133. ## Skipping to the good part
  134. It's entirely possible to avoid writing an entire system by yourself. If you want to dive right
  135. in and start immediately making something cool, you might consider grabbing the KnightOS kernel.
  136. Right off the bat, you'll get:
  137. * A tree-based filesystem
  138. * Multitasking and IPC
  139. * Memory management
  140. * A standard library (math, sorting, etc)
  141. * Library support
  142. * Hardware drivers for the keyboard, displays, etc
  143. * Color and monochrome graphics (and a compatability layer)
  144. * A font and text rendering
  145. * [Great documentation](http://www.knightos.org/documentation.html)
  146. * Full support for 9 calculator models
  147. The kernel is standalone and open-source, and it runs great without the KnightOS userspace.
  148. If you're interested in that, you can get started [on GitHub](https://github.com/KnightOS/kernel).
  149. We'd also love some contributors, if you want to help make the kernel even better.
  150. ## Closing thoughts
  151. I hope to see a few cool OSes come into being in the TI world. It's unfortunately sparse in that
  152. regard. If you run into any problems, feel free to drop by #knightos on irc.freenode.net, where
  153. I'm sure myself or someone else can help answer your questions. Good luck!