Development Language

From NerdConsole
Jump to navigationJump to search

The NerdConsole PPU software is a complex piece of multi-threaded code that accepts information from the GPIO pins on a Raspberry Pi, manages the data that was received, and uses it to render graphics based on that information.

Java

Originally, it was written in Java because I'm very comfortable with Java and that seemed like a good reason to use it at the time. Most values used in NerdConsole's PPU registers are bytes that are used as multi-value bit fields. This in turn meant that as development continued I got deeper into the bit level manipulation of data. On its own that wasn't a problem, but I kept running into things that weren't working the way I expected. At its core, Java is a language that was designed to simply always used signed numeric values for everything (values that can be both positive and negative). As part of that design choice, they made sign related decisions for bitwise operations that caused an almost unimaginable number of bugs.

Worse, to ensure the unsigned nature of my values within the code, I was forced to promote most data values to the next larger one. For example, when I wanted an unsigned 8-bit value (0 to 255), but a byte in Java is signed (-128 to 127), so I instead used a signed 16-bit value (-32768 to 32767) so my desired range was covered. This meant that I was using twice the amount of memory for every value, was forced to be very careful about the values I was using, and often had to waste CPU cycles masking the value to ensure it stayed in the range I wanted now that I had a much larger than desired range available.

Back to the signed bitwise operations causing trouble, I remind you about how 2's compliment numbers are stored. A simple way to think about it is that all of the negative numbers have loads of "1 bits" at the top of the value.

For example

Value Bits
5 00000101
-5 11111011

If you do common bit shift operations, those 1's on the left do things to your result that are often very difficult to figure out and require a lot of parenthesis groups and forced casts to work around. Even with tons of test code, there were often problems that would slip through the cracks.

Eventually, I realized that despite how much I like using Java, I was constantly fighting the language itself as I tried to write a complex piece of code. Over time, this became more work on dealing with Java than I was spending coding features into the NerdConsole PPU that weren't done yet. I hit around 50,000 lines of code that mostly worked on the graphics side and didn't want to deal with the problems anymore. Something had to be done.

C++20

A long time ago I was fluent in C++ and it has every type of integer value I could possibly hope for without the need to force signed values to behave in a way they don't like. Unfortunately, the last time I used C++ was before they started naming it with the number at the end (before C++98). Experience from back when it was more of an extension of C often stuck together as C/C++, and before it was truly its own language. Since the last time I used it for hobby projects, it has changed dramatically and I'd forgotten how to intelligently manage my own memory thanks to garbage-collected languages.

With this new theory, I have officially arrived at the tricky point in this project... Do I spent the time and effort relearning C++ almost from scratch and with that start the NerdConsole PPU development over again too, or do I keep fighting a language that was designed to make this type of low level programming harder than it should be?

As it turns out, the decision wasn't as difficult as I'm making it sound. C++ is fast and will typically use far less memory than a JVM. It has powerful graphics and GPIO libraries available, and won't constantly get in my way. Relearning a language and rewriting a project is no small undertaking, and every time I want to so something that is simple and built-in to Java and isn't in C++, I spin off for a half an hour of research. Daunting, yes. Worth it, also yes.

So the code based PPU project has been started over, and since this is still a massive hobby project that only gets nights and weekends, progress is slow. Please be patient.