So, this talk regards my translation of the classic reverse engineering work of Paul Corby and Sebastian Lalonde in a book they called Voyage au Centre de l'HP28C-S. They took apart the Hewlett-Packard 28 graphing calculator, which is the predecessor to the 48, the 49, and the 50 that all RPN junkies use, and they documented everything about it. So, they started with nothing more than a physical unit and documentation for the HP17, which was an unrelated machine that happened to use the same CPU. And from that, they dumped out the memory through a piezo buzzer. So, they dumped ones and zeros out, one full second of buzzing for a one, one full second of silence for a zero, recorded this to tape, fed the tapes into an Apple IIe, and then with this memory dump, they started to record the data for the HP17, and then they started looking through it and trying to recognize things. So, for example, they'd look for 314159, and that would be pi. And then seeing pi, they could try and go backward and figure out how all floats work. And then they began to inject things into memory and create new objects and mess with the operating system. And they documented this perfectly in a long and very technically detailed book. It's divided into two parts. It's divided into three sections. The first describes the software within the computer, or the HP28 graphing calculator, rather. And this software is described such that you can write your own machine language code. But the problem comes up that there's no assembler available for the platform. So, the appendices contain tables, and you can actually, using pen and paper, write your assembly language in the left column, and then you use the tables to convert everything over, to the individual hexadecimal entries which form the program. So, I'll be discussing the contents of the book. But because it's so technically dated, being like a hack that was performed 18 years ago, I'm focusing on the oddity of the hardware, and how impressive it is that they came up with all of this, using nothing more than an Apple IIe and a bit of ingenuity. Disclaimer, this is not my original work. All I did was translate. And my French is not terribly good. So, this is the HP28. It is a 4-bit architecture, with registers as wide as 64 bits. It's capable of doing algebra and calculus, symbolically. And it was released in 1986. Within software, we'll be discussing how objects work within the operating system. You don't have C compilers. So, things aren't done in terms of functions and returns. And they're not held as primitives within registers wherever possible. You have an entry prefix, and some other stuff that we'll describe. Then, I.O. has to be performed. And we'll discuss how that's implemented in the hardware, followed by the contents of RAM, and different tables that are used in RAM. How video processing works. This was before video cards were available. And... As there was no assembler openly available for it, we'll discuss how they initially broke into machine language. How they first wrote programs for it, and how they instruct the reader of the book to write programs for it. Then, there are suggestions for further discoveries and resources. There are three models, the 1BB and the 1CC, which are both labeled as the 28C. These differ not in hardware, but in firmware. So, entry points are different. You had the firmware compiled at HP, using whatever tools were private to that organization and unavailable to the authors of this book. So, in a given compile, you wind up with subroutines at particular addresses. They have to be found for every different firmware. Otherwise, they'll jump to a location that means something entirely different from what you expect, and your program crashes. The 28S only had a single revision. So, if you have a 28S, it's much easier to figure out which opcodes to type in. It used the HP Saturn, which was a chip that they designed in-house, and it was documented in the HP 71B. The 71B was interesting because they published their complete internal documentation for it. You can download from the HP Museum every single page of documentation that the original designers had access to. It has thousands of pages. It has schematic diagrams. It has the source code to the firmware of the 71B. It has all of this work, and it's freely published. Similar documents for the HP 28 are unavailable. I'm trying to find them. If you have any contacts with the Hewlett-Packard Archival Department, I would appreciate being put in touch. It's also interesting because it's nibble-wise a little Indian. So, instead of having... On a PC, you have bite-wise little Indians, so all of the bytes are backward, and to read it, you have to take those bytes and then flip them around in pairs. So, ABCD would become CDAB. Here, it's written backward as you would read it on paper in hexadecimal. You have four different memory layouts, even though you only have two different major revisions. So, they loaded the same firmware onto two units which defer slightly in hardware. So, the entry points are the same, but you can have extended memory. Among the 28C, the RAM grows. Everything else remains at rather fixed addresses. But with the 28S, there are many features that change. The row waveform driver no longer exists. The screen timer was moved from near the bottom of the screen. There's a lot of memory to near the top. And no program originally designed for any of the 28Cs would ever work on the 28S. The registers are far from the homogenous 32 general-purpose ones that we now expect from working with RISC systems. So, you've got ones of 12, 16, 1, 4, 16, 20, 64 bits in width. So, for example, you have a return stack, which is 8 levels deep and 20 bits wide. The address space is 20 bits. 20 is not evenly divisible by 8. So, every pointer on this system is not 8 bytes as you would expect, but instead it's 5 nibbles. You've got an entire register just for carrying that contains nothing else. And then you have separate hardware and software status registers. You have safeguard registers, which you can copy data into and out of, but you cannot do anything else with. So, you can't make these the source or destination of an addition. They're only used for backing things up before a quick function call or subroutine. All pointers are 20 bits wide, of course. That's the address space. So, of course, everything is in nibbles or quartets, as it's been translated, quartet being the more formal term. So, as a byte goes from 0 to 255, these nibbles go from 0 to F, or often they'll go from 0 to 9. To ease debugging, they use binary-coded decimal in hardware. All floats are held in binary-coded decimal so that it makes the same rounding mistakes that you would with pen and paper. And that way, when you calculate it with the calculator and you do it again on pen and paper, you wind up with the same value. You have no problem representing one-fifth, which is very difficult to represent in binary float. And it's nibble-wise little-endian. So, for the number 1, 2, 3, 4, we find that in big-endian, it would read just as it's written. This would be on, say, a PowerPC machine. Now, little-endian on, say, an MSP430, in which I do most of my work, the less significant byte comes first, followed by the more significant one. So, if you have a pointer and you only want the least significant byte, so you want to find it modulus 256, all you have to do is pretend that it's a pointer to a byte. But on the HP28, they went one step further, and it's nibble-wise little-endian, so it's written exactly backward of how you would write it as a human being. Now, every object has a prologue, which is five nibbles and specifies the type. The actual values in this table aren't important to know, but it's important that they all begin with similar prefix. You have 02991, 02933. And these define different objects within the system. It's not object-oriented in the modern sense of inheritance, but every value on the stack has this prefix code, has a type specified with it, and this is all carried together. If you store it to a variable, you are applying a type to that variable, and that type carries over. Types include short integers, reals, extended reals, complex numbers, extended complex numbers, arrays, two types which the authors couldn't discern but found by disassembling them. You can describe an algebraic expression as an object and then run it through the stack and add to it and subtract from it just as you would an integer by calling the same routines. So you can call an add routine and not know what the inputs are. The prefix is also used to imply the length. So given an entry point of an object, you don't know where it ends until you use a lookup table with the... the object prolog. Now short integers prolog is 0, 2, 9, 1, 1. This is of course written backward because we're nibble-wise Lelandian. So you find 1, 1, 9, 2, 0 as the prefix, and then the number follows. So in the case of 0, it's all zeros. But in the case of 1, 2, 3, 4, 5, it's of course written nibble-wise and backward. A real number is expressed, of course, with an exponent, a mantissa, and a sign. So we have the prolog, which of course is backward, followed by an exponent, say 5. This has to be padded to fit the specified width. So we find 5, 0, 0. The mantissa is pi, so 3.14159. That's actually rendered backward and in binary coded decimal. So you... looking in memory without... doing conversions will recognize the same constant that you learned in elementary school. And then this is followed by a sign, which even though it could possibly have one of 16 values, is either a 0 or a 1, 0 being positive. Now all operations in this calculator are performed using reverse Polish notation. So instead of typing in 3 plus 5 times 6 and then having the order of operations cause the multiplication to be performed before the addition, instead everything occurs as it's seen. Wherever you see an integer or a float or whatever, a constant value, that is pushed onto the stack. Whenever you see an operator, its inputs are popped off of the stack, run through the operator, and then the result is pushed back. This takes a long time to get used to, but once you're used to it, you'll never want to go back, because it allows you to work with a series of numbers and flip them around, and instead of seeing a complicated version history of what you know you typed in above the command line, you'll instead see the values that are available to you. This also allows you to completely avoid grouping symbols. You never need any parentheses unless you're trying to treat code as an object. And it's trivially easy to interpret. So it takes no effort or code space to build an interpreter for this language. Now given an algebraic expression like 1 plus 10 times 3, we know that the multiplication happens first. And we can draw this little inheritance tree of it. So we see that the addition takes the 1 and the result of the multiplication as its inputs, and then the multiplication takes 10 and 3 as its inputs. Well, this can be rewritten. You can put every value between the operator that applies to it. So 10 3 times, as soon as the times hits, it multiplies those two together, and all we see on the calculator is 30. And then 1 plus, as soon as you get the plus sign, it pops the 30 and the 1, and it returns 31 to the stack. Viewed differently, if you have input, 10 3 times 1 plus. The 10 is pushed onto the stack. The 3 is then pushed onto the stack. As soon as the multiplication hits, they're combined into 30. As soon as the 1 hits, 1 is pushed on top. And then the addition combines them. Now, despite doing everything in reverse Polish notation, the calculator still has an algebraic type. So inside of quotation marks, you can say pi times 10 to the fifth power. And it interprets that properly. But when it's stored in memory, you have your entry prologue, and then you have the system in reverse Polish notation. So everything is rearranged, and inside of memory, you'll see 10 5 power pi times. And of course, this seems difficult if you're unfamiliar with it, but it's really as easy as learning your multiplication tables or all of that stuff that we now take for granted, but that we struggled with when we first saw it in elementary school or middle school or wherever. And then once you get used to this, you never want to go back. It's the most efficient way to use a graphing calculator. UNIX has a command line calculator that uses a similar interface. And many of us that know RPN will use it out of habit because it's so much more work to use algebraic notation. And then, of course, within this, pi isn't just a letter. It is the pi object that we saw before. As a real number. You'll see it's prolog of 0, 9, 2, 3, 3 at the beginning. Red backward, of course. Followed by the exponent, which in this case is a 0, and then the mantissa and the sign. Now, IO is performed for infrared input and output, even though the input diode never shipped on the device. Although it had all of the hardware to do input and output and backing up files and that sort of stuff, this was never supported by the software. And it was never supported by the product after release. You could use the output to print to an infrared printer, a little thermal printer that would give you receipt paper. But you couldn't back anything up to a computer. You couldn't receive anything from a computer. And as all software was stored, all user-added software was stored in RAM, as soon as your battery died or your program crashed, you lost everything. Every program you had ever written, you would have to type back in. And that's just how things were in 1986. But by 1990, they started changing things. And so they recognized that there was space in the board for a photo transistor right next to the diode. And they started poking memory, and they found the addresses for it. And near this piece of memory, they also found timers. They found control for the contrast. They found screen bits, so they could write a bit-mapped image to a particular spot in memory. They could even change how physical rows were mapped to logical rows. So if they had a bit-map and they wanted to scroll it very quickly, they could scroll it not by copying bits around, but rather by copying the... by moving the rows around. They could even overclock one of the calculators in software only. And all of these features were hidden from the original users. You could not do this when the device was manufactured, and none of the official documents admitted that such features existed. This is the printer that you could purchase, the HP 82240. You can see a graph being printed on the right, and then above that, some records. The row driver waveform was used to map... the mapped lines, as I mentioned earlier. And for scrolling, you can map... or you can swap these line mappings instead of the actual data. If you're doing sprites, you can actually hide half of the screen and then swap it in, just as I believe Mode X did on DOS video game programming. So you have off-screen memory, and then you swap that on-screen. And because you're just swapping memory and not actually moving memory, you don't have to read it all in and then write it back out. This is initialized at boot to the proper settings, and then it's interpreted by a software video driver. So in the 28C, an interrupt will fire, and when it fires, its handler will actually update the physical row that needs to be shown to the LCD lines at that moment in time. The row driver waveform looks like this. I've covered the ones in red circles. You can see that the first physical line comes initially, followed by the first logical line, and that they're on opposite ends. Sorry. They run backward and then to each other. So the first and the 32nd line are adjacent, followed by the second and the 31st and the third and the 30th. So the screen is actually updated from the top and the bottom by alternate lines working inward until they meet in the center. Wherever you see a red dot over a bit, that's the logical line that gets mapped to a particular physical line. So for example, if we changed the red dot on the second row to the very first bit, the one on the far left, the first logical line would be shown on two physical lines, both at the top and at the bottom of the screen. Setting more than one of these bits causes the screen to scramble, I believe. You could do a magnifying glass with that. So you could make the top half of the screen grow to fill the entire screen and all sorts of neat little tricks. RAM itself contains not only objects and variables and that sort of stuff, but it also holds sort of virtual I.O. addresses. So these are polled by a background process. And then if you write something to them, the polling process recognizes that it's not the value that it was before, and it then carries out an action based upon it. Or it will write to it in the background and then you read from it. For example, there's a keyboard buffer. So when you press a key, an interrupt fires, writes that to the next key slot on the keyboard buffer, and then any user software can just watch it magically appear because it's happening in the background, interrupting the foreground process. But at the same time, it's really just RAM. This doesn't physically connect to any I.O. addresses. The flags, the command line stacks, and the temporary environment are all stored here. The keyboard buffer works sort of like a clock. You've got a circular buffer. There's a variable that marks the start of unhandled keys and the end of unhandled keys. And it's... As keys are handled, these two advance, and the two hands are sort of chasing one another. So the characters on the right side of the display, those to be treated, have not yet been handled by the operating system. Now, key end is swinging around as new characters get entered into the waiting buffer, and key start is swinging around in the same direction as keys are handled by the operating system. If things slow down to the point where one crosses the other, then you lose key events. But the system still recovers itself. And at the same time, if the key end advances past key start, it then ceases to advance, knowing that everything has been handled. Doing this in a circular buffer makes sense, because you can just do a modulus operation on a pointer and then increment it and never have to care about resetting it or decrementing anything. You need no conditionals. To access machine language, there is a machine language program type, a 02C96, but it's not creatable by the user. So you have to bootstrap it, first using a program called ASS to compose an object. This tricks the operating system into applying a particular object type before some given hex digits. And then after that, they had the LASS operation, which was a short little program that signed the object type of machine language program. Now, it takes as input a machine language string. This is not an assembler in the sense of a PC or a workstation or a UNIX. You cannot give it a human-readable program. You have to give it individual nibbles in hexadecimal and nothing else. Every single character is one nibble. And it takes this machine language string and compiles it to a machine language object, which you can then execute. So you push the string to the stack, you run LASS, and then you store the resultant object to a variable, and then you can execute that variable as a program. When you do this, your raw machine code is running on the device. If you make any mistake that will cause the device to crash, you quite likely lose all of memory and have to start from scratch. This is an example of a program. You would have to type in 76C2091C7069C20 and so forth. All of this. And if you made one typo, that might be enough to crash your calculator. There's no debugger that allows you to single-step it. At the time, there was no emulator. You're on your own. Further, some of the addresses here are unique to the particular revision of the model of the calculator that you're running. And so when you actually find the listing in the book, it has this with holes in it, and you have to fill in those holes with bytes from a table in the appendix. You as a user have to assemble your own program just to type it into your calculator. So they suggested finding further things by searching ROM, and they included string tables for things like errors. So you could look up the particular error code and then search ROM for that error. And they suggested two ways of doing it as a sort of sign of the times. The first was to write a disassembler that... And by disassembler, they mean a program which gives you hexadecimal values from memory. So a memory spy. They don't really mean a disassembler in the modern sense. You could write one on the calculator, and then you could sort of scroll through memory, and it would show you the address on the left and the bytes on the right, and then you with pen and paper could do your analysis. They also suggested dumping it to a workstation using I.O. that'll be described later. The latter method was the one that they used for most of their research, but they knew so little about the hardware when they started, none of it being documented and none of this having been done before, that they had to... They had to write a short snippet of machine code to spit out to the only I.O. address they knew, that being the piezobuzzer. You get a full second of buzzing for a one, full second of silence for a zero. All of this has to be recorded to audio cassette and then fed into an Apple IIe. And then you've got problems changing tapes or floppy disks. It takes days to dump it, so... I mean, for days this thing was buzzing in poor Sebastian's dorm room. They then identify useful features like useful routines in memory, like saving and loading registers, reserving room in the heap, performing garbage collection. While this isn't object-oriented in the modern sense, it did have object creation and deletion in the modern sense, and you did have a garbage collector, similar to what you would have in Java. They also identified entry points for things like a function that shows a too-few memory error or a beep. So if you know that something is causing you to run out of memory, you can search for the call to that function and then identify what's happening. They do flowcharts for routines that they reverse-engineered. So when you call the saveRegisters function, it reserves an address in memory, then it makes sure that there's room available. If not, it attempts to garbage collect, and it only makes one attempt. Failing that, it'll continue on. If it runs out, it does the memory error. And they keep in mind that when they started, they knew nothing of what was inside this machine. They didn't have a floppy disk drive. They didn't have a debugger. They didn't have access to memory until they created it themselves. And they built all the way up to understanding how garbage collection was performed on the machine, understanding all of these things and all of these different routines that were essential to the operating system. And then instead of keeping it to themselves or doing a security exploit and publishing that, instead they documented it. And they documented it all to the point where you as a mere mortal could go through and do the same thing. And then as you start doing the simple stuff, you work your way up to the more advanced stuff. So the second part of the book discusses the hardware of the device. How to modify it, how to add new features that didn't exist as it was initially manufactured. They begin with an exterior description, then opening the machine and so forth. This is what the calculator looks like when it's opened. As you remember from the first slide, it has a keyboard on the left and the right with letters being on the left and numbers being on the right. So you can fold the letters behind the screen if you've no need for them. All of the electronics sit beneath the screen on the top right face, and then the batteries slide into the case to the side behind it. There are no screws. They have plastic pins which are glued into one another. So you have to either drill these out or physically break them. And even after 20 years of aging, they're not too keen to fall apart. So in opening this device, you're going to break a lot of it. Once the calculator is opened and you're looking inside of the board, you'll note that there are two ROM chips. On the bottom right, where all of those lines are labeled, that's the memory bus. There's B0, 1, 2, and 3 because the fundamental unit of addressing is a nibble, so they only have to run four data lines. At the top, you'll see a diode symbol for output, and you'll see two empty pads to the right of it. That's initial supportive input that was never physically attached during manufacturing. And then you'll note a large capacitor with a resistance capacitance circuit beneath it, which is used for timing. If you want to overclock this older model, you modify the inductor and the capacitor, and in that way, you can change the clock. So they first describe an external power feed so that you don't have to keep buying expensive end-cell batteries, and they do this to get the user familiar with soldering and such. Immediately after that, they go into overclocking. By replacing a capacitor and inductor, you can speed the thing up, you can slow it down. And this is necessary in the 28C. In the 28S, you just run a program to do it. But the more interesting hardware modification was a replacement of the RAM of the device by actually cutting the traces of the memory bus. So they cut these gold traces on the circuit board, and then they purchased memory upgrades for the HP-71, the architectural ancestor of this device, for which they had full documentation. They then patch in this memory module. You can see from the pricing that 64 kilobytes would cost $150 to $300 at the time that this book was published, which was four years after the calculator was first released. This was never supported by the manufacturer. They knew at this point in history. The I.O. ports were modified. It had ridiculously short range, and there was no receiving phototransistor. So they got the idea that they could patch in an opto-isolator. So you've got a single chip with an infrared LED and a phototransistor embedded in silicon, so the light never escapes. There's never any optical interference. And then they just ran the lines through it in series with the original phototransistor and photodiode so that you can have a physical cable to connect between two devices. They also did the same thing involving the buzzer. So when the buzzer fires, it turns on an LED. And they used this to connect to the Apple IIe, the Commodore, many different minicomputers that you had available at the time. They came up with cartridges so that you could replace what was plugged into the infrared ports on the fly. They have schematics included for connecting this to an Apple IIe joystick to connect the two calculators to one another. They have voltage-level converters so you can run it to RS-232 and take it into your PC. They suggested adding a joystick, motor control, a robot, and a plotter. And most interesting, they suggested a telephone composer. They figured that you could make clicks with it that would work as a sort of rotary telephone. They never got around to freaking with this, but it would be interesting to see if it were possible. Then the book ends with appendices. And the appendices take a full third of the book. They begin by describing machine language in absolute detail. This is what you would expect in a manufacturer data sheet as opposed to the prose that forms the first two sections of the book. They describe the Saturn microprocessor. They describe its instructions. They have tables of error codes and objects. And entry points. And for each of these, they have to have it for every single revision. They had to disassemble the code of not one device, but of three or four devices. And then they include a small library of programs. So if you want to, say, invert the video of your screen, if you want to overclock your calculator, they include the original source code in their own notation. Then they translate that to machine language for you and include the information that you need to build that long series of hex that I showed you to type in. So after this in history, many events happened. This book was published in 1990. Since then, Hewlett-Packard came out with the much more famous 48 series. This supported memory expansion off the bat, which was first introduced by Voyage aux Cintres. They include infrared input and output. Also introduced in this book for the first time. They add a serial port. Also introduced in this book for the first time. And eventually the community wrote a program called MetaKernel, which was a replacement operating system that stayed on an external memory card. Sort of like Linux, but for a graphing calculator. And they jealously worked on this. They'd spend days on it, and you could actually go into a particular electronic shop in Paris and purchase a memory card with this kernel loaded on it. Later on, emulators were written. Christoph Geislink writes one called Emu28, which can emulate the HP28, 28C, and he has another for the 28S. So you can run this calculator today, and you can do it on your PC. He was able to build this emulator using the assistance of the book, even though he does not read French, by reading the pictures. Because there's so many diagrams in this, and so many of them contain useful addresses, that he was able to figure out his remaining questions about how the hardware functioned by looking at the diagrams in the tables alone. Then the HP49 series came out, including native support for algebra and all sorts of things that are necessary in the modern market. It has an SD card slot. My HP49 has two gigabytes of storage. Not that I'll ever need it, but it's certainly helpful to be able to go beyond the original memory. It supports a USB port, which is powered by a USB-to-serial chip. Infrared, serial, and all of that stuff. It has an ARM CPU, which emulates the original Saturn. After the very first HP49, they could no longer justify fabbing their own chip for this, but it was impossible for them to rewrite the software on their modern budget. So they built an emulator, and the official device that you buy today is an emulator of the classic device that you would have purchased a decade ago. It being ARM, though, some enterprising folks ported GCC. So you can compile a C program on your workstation, write it to an SD card, and copy it over. Then execute it. And it has full access to everything in the ARM device, as well as access by proxy to things within the emulated Saturn device. And then MetaKernel became the official operating system. So when you turn on a modern HP49 or 50G, there's a splash screen for MetaKernel, the image of which looks more like something that you'd find at DEF CON than something that you would find in Hewlett-Packard. And that's because they took this hacker operating system and they made it their official one, being unable to reproduce all of its features. TI calculator hacking came into vogue, and overclocking was performed by the same method, optionally cutting off a capacitor. And the next step will be the Texas Instruments Inspire calculator. It's completely locked down. DRM on everything. You cannot run any machine code. You cannot run any games. No ROM dump is available. And serious work is just beginning on this. So it's a sort of next step in this chain that stretches back to 1990. So I hope you've enjoyed a little history lesson. It's a departure from the MSP430 stuff that I usually speak about. But do you have any questions? Yes? It's on your DEF CON CD. No. I've finished 60% to 70% of it. It's not complete, and it's rather rough, but it's typeset in LaTeX. You get a bilingual PDF containing both a copy of the French and the English translation for each paragraph. If you natively speak French, or like myself, you can suffer through it, the original documentation is available at corbi.fr. That's C-O-U-R-B-I-S. Any other questions? Yes, again? Why are there so many French hackers on the HP Calculators? Because this book first came out in French. They had a head start. And then the authors later came out with a book on the HP-48. So because this work was originally done in French, and it took so long to be translated into English, a good 20 years or so, it set us behind here in the States. And many of the good HP Calculators, are from outside of France. But you're right in that the majority are French and then German. Any other questions? If I can't see you, just speak out. All right, well, thank you for your time. I'll be in room 115 by the vendor area for any one-on-one questions. Thank you.