Modernizing C Arrays For Greater Memory Safety

Lately, there has been a push for people to stop using programming languages that don’t promote memory safety. But as we still haven’t seen the death of some languages that were born in the early 1960s, we don’t think there will be much success in replacing the tremendous amount of software that uses said “unsafe” languages.

That doesn’t mean it’s a hopeless cause, though. [Kees Cook] recently posted how modern C99 compilers offer features to help create safer arrays, and he outlines how you can take advantage of these features. Turns out, it is generally easy to do, and if you get errors, they probably point out unexpected behavior in your original code, so that’s a plus.

We don’t think there’s anything wrong with C and C++ if you use them as you should. Electrical outlets are useful until you stick a fork in one. So don’t stick a fork in one. We really liked the recent headline we saw from [Sarah Butcher]: “If you can’t write safe C++ code, it’s because you can’t write C++.” [Cook’s] post makes a similar argument.  C has advanced quite a bit and the fact that 30-year-old code doesn’t use these new features isn’t a good excuse to give up on C.

Continue reading “Modernizing C Arrays For Greater Memory Safety”

Create A Compiler Step-By-Step

While JavaScript might not be the ideal language to write a production compiler, you might enjoy the “Create Your Own Compiler” tutorial that does an annotated walkthrough of “The Super Tiny Compiler” and teaches you the basics of writing a compiler from scratch.

The super tiny compiler itself is about 200 lines of code. The source code is well, over 1,000 but that’s because of the literate programming comments. The fancy title comments are about half as large as the actual compiler.

The compiler’s goal is to take Lisp-style functions and convert them to equivalent C-style function calls. For example: (add 5 (subtract 3 1) would become add(5,subtract(3,1)).

Of course, there are several shortcut methods you could use to do this pretty easily, but the compiler uses a structure like most full-blown modern compilers. There is a parser, an abstract representation phase, and code generation.

Continue reading “Create A Compiler Step-By-Step”

Two Esoteric Programming Languages, One Interpreter

Many of you will have heard of the esoteric programming language Brainf**k_. It’s an example language that’s nearly impossible to use because it’s too simple. It’s basically a Turing computer in code – you can essentially put characters into an array, read them out, increment, decrement, and branch. The rest is up to you. Good luck!

What could be worse? Befunge, a language that parses code not just left-to-right or top-to-bottom, but in any direction depending on the use of ^, v, >, and <. (We love the way that GOTO 10 looks like a garden path in the example.)

Uniting the two, [rsheldiii] brings us BrainFunge, a Brainf**k_ interpreter written in Befunge. And surprisingly, the resulting write-up sheds enough light on both of the esoteric programming languages that they make a little bit of sense. If you try to read along, you’ll definitely be helped out by Esolang Park, which was new to us, and accommodates the non-traditional parsing while displaying the contents of the stack.

If you get a taste of the esoteric, and you find that you’d like a little more, we have a great survey of some of the oddest for you. After cutting your teeth on Befunge, for example, we bet you’ll be ready for Piet.

A Primer For The Homebrew Game Boy Advance Scene

As video game systems pass into antiquity, some of them turn out to make excellent platforms for homebrew gaming. Not only does modern technology make it easier to interact with systems that are now comparatively underpowered and simpler, but the documentation available for older systems is often readily available as well, giving the community lots of options for exploration and creativity. The Game Boy Advance is becoming a popular platform for these sorts of independent game development, and this video shows exactly how you can get started too.

This tutorial starts with some explanation of how the GBA works. It offered developers several modes for the display, so this is the first choice a programmer must make when designing the game. From there it has a brief explanation of how to compile programs for the GBA and execute them, then it dives into actually writing the games themselves. There are a few examples that [3DSage] demonstrates here including examples for checking the operation of the code and hardware, some simple games, and also a detailed explanation the framebuffers and other hardware and software available when developing games for this console.

While the video is only 10 minutes long, we recommend watching it at three-quarters or half speed. It’s incredibly information-dense and anyone following along will likely need to pause several times. That being said, it’s an excellent primer for developing games for this platform and in general, especially since emulators are readily available so the original hardware isn’t needed. If you’d like to build something from an even more bygone era than the early 2000s, though, take a look at this tutorial for developing games on arcade cabinets.

Continue reading “A Primer For The Homebrew Game Boy Advance Scene”

Blinking An Arduino LED, In Julia

The Julia programming language is a horrible fit for a no-frills microcontroller like the ATMega328p that lies within the classic Arduino, but that didn’t stop [Sukera] from trying, and succeeding.

All of the features that make Julia a cool programming language for your big computer make it an awful choice for the Arduino. It’s designed for interactivity, is dynamically typed, and leans heavily on its garbage collection; each of these features alone would tax the Mega to the breaking point. But in its favor, it is a compiled language that is based on LLVM, and LLVM has an AVR backend for C. Should just be a simple matter of stubbing out some of the overhead, recompiling LLVM to add an AVR target for Julia, and then fixing up all the other loose ends, right?

Well, it turns out it almost was. Leaning heavily on the flexibility of LLVM, [Sukera] manages to turn off all the language features that aren’t needed, and after some small hurdles like the usual problems with volatile and atomic variables, manages to blink an LED slowly. Huzzah. We love [Sukera’s] wry “Now THAT is what I call two days well spent!” after it’s all done, but seriously, this is the first time we’ve every seen even super-rudimentary Julia code running on an 8-bit microcontroller, so there are definitely some kudos due here.

By the time that Julia is wedged into the AVR, a lot of what makes it appealing on the big computers is missing on the micro, so we don’t really see people picking it over straight C, which has a much more developed ecosystem. But still, it’s great to see what it takes to get a language designed around a runtime and garbage collection up and running on our favorite mini micro.

Thanks [Joel] for the tip!

Free Your Pi With This Bare Metal Programming Environment

[Rene Strange] has graced these fair pages a short while ago with a sweet Raspberry Pi software based poly synth, with a tantalising reference to it being a bare metal application. So now, we’ll look into circle, the bare metal programming environment that it is based upon. The platform consists of a large set of C++ classes to access the hardware as well as perform tasks such as task creation and scheduling in the cooperative multitasking, multicore environment. Supporting all Raspberry Pi boards from version 2 onwards (not including the Pico!) in both 32-bit and 64-bit flavours, the environment is pretty complete. Classes are provided for USB, networking, FatFS, as well as more mundane tasks such as dealing with interrupts. On top of these classes there are a pile of application-specific libraries, covering functions such as display interfacing, GUIs using a variety of frameworks, and some more esoteric applications such as interfacing to a Pico, and even sending the system log to a remote web browser!

Classes and libraries however, don’t always help by themselves, which is where the 42 (yes, we know) code examples come in very handy. They’ve provided example applications for some fun stuff like drawing Mandelbrot fractals to the display, as well as some more mundane tasks that we have to deal with such as getting that pesky DMA controller to play nice with the SPI hardware. All-in-all, this looks like a great set of tools for taking full advantage of some fairly beefy hardware for your next embedded project that needs plenty of resources, but not all that unnecessary operating system stuff.

Perhaps not quite as complete as circle, but we’ve seen a fair few Raspberry Pi Bare metal projects over the years, like the Nerdsynth, based on the PiZero, and this neat little bare metal assembly language clone of starfox.

Thanks [Ruhan] for the tip!

Header: Aryan Patidar, CC BY 4.0/Evan-Amos, Public domain.

Flashing TI Chips With An ESP

Texas Instruments is best known to the general public for building obsolete calculators and selling them at extraordinary prices to students, but they also build some interesting (and reasonably-priced) microcontrollers as well. While not as ubiquitous as Atmel and the Arduino platform, they can still be found in plenty of consumer electronics and reprogrammed, and [Aaron] aka [atc1441] demonstrates how to modify them with an ESP32 as an intermediary.

Specifically, the TI chips in this build revolve around the 8051-core  microcontrollers, which [Aaron] has found in small e-paper price tags and other RF hardware. He’s using an ESP32 to reprogram the TI chips, and leveraging a web server on the ESP in order to be able to re-flash them over WiFi. Some of the e-paper displays have built-in header pins which makes connecting them to the ESP fairly easy, and once that’s out of the way [Aaron] also provides an entire software library for interacting with these microcontrollers through the browser interface.

Right now the project supports the CC2430, CC2510 and CC1110 variants, but [Aaron] plans to add support for more in the future. It’s a fairly comprehensive build, and much better than buying the proprietary TI programmer, so if you have some of these e-paper displays laying around the barrier to entry has been dramatically lowered. If you don’t have this specific type of display laying around, we’ve seen similar teardowns and repurposing of other e-paper devices in the past as well.

Continue reading “Flashing TI Chips With An ESP”