Posted on

Understand your program by having a mental picture

Who said programming should be boring? In this edition, we will bring back the good old times with retro gaming in Rust! Moreover, this newsletter showcases a visualization tool for a better understanding of Rust programs in terms of borrowing, heap, and stack. Grab some coffee and let’s dive in…

Nintendo Emulator in Rust

An emulator is a software program that is designed to emulate the behavior of the Nintendo Entertainment System (NES), a popular video game console that was first released in the mid-1980s. The purpose of an emulator is to allow you to play Nintendo games on your computer or e.g. your Raspberry Pico without the need for the original console.

When you run an NES emulator, written in Rust, on your computer or other devices, it creates a virtual environment that mimics the hardware of the original NES console. The emulator includes software that emulates the various components of the console, such as the CPU, and the graphics processing unit. The emulator also includes software that can read and interpret the code of NES game cartridges, which you can download here and play in the NES wasm emulator in your browser.

Once the emulator is up and running, you can load the virtual game cartridges into the emulator, and it will execute the code of the game as if it were being played on the original console. The emulator will output the video that the game would generate on the original hardware, allowing you to play the game on your computer.

Overall, an NES emulator is a powerful tool that allows users to enjoy classic NES games on modern hardware, preserving the experience of playing these games for future generations.

Super Mario

Memory management: what the hack is a Stack and Heap?

Stack memory
The stack and the heap are two areas of memory that a program can use to store data.

The stack is a memory region that is used to keep track of function calls and local variables. It works like a stack of plates: you add a new plate (or item) to the top of the stack, and when you are done with it, you remove it from the top. This order is called Last In First Out (LIFO). The simplicity makes it fast because it can be managed easily and either lives in RAM (fast) or cache (faster) of your CPU. Moreover, the stack is limited in size

The heap, on the other hand, is a larger and more flexible area of memory that can be used to store data that is too large to fit on the stack or needs to be dynamically allocated during the program’s execution (e.g. mutable vectors or Boxed variables).

Overall, the stack is faster and more efficient, but it has less memory available and is less flexible. The heap is slower and more complex, but it can be used to store larger amounts of data and is more flexible. Knowing when to use each of these memory regions is an important part of writing efficient and effective computer programs. To understand the choices that are made on memory management in Rust (e.g. ownership), knowing what a Stack and Heap are is a must.

Some nice reading material:

Understanding code with help of a visualization tool

The Rust borrow checker is a key component of the Rust programming language that enforces ownership and borrowing rules at compile-time, ensuring that programs are free from certain kinds of memory errors such as data races and null pointer dereferences. The borrow checker analyzes a Rust program’s use of mutable and immutable references to ensure that they are used correctly and that the lifetime of the references is valid throughout the program’s execution. By enforcing these rules, the borrow checker helps Rust programs achieve memory safety and prevents common programming errors that can lead to crashes or security vulnerabilities.

To get a better understanding of this topic Aquascope can be a very useful tool.

“Aquascope is a tool that generates interactive visualizations of Rust programs. These visualizations show how Rust’s borrow checker “thinks” about a program, and how a Rust program actually executes.”

Go ahead and give it a try!

Aquascope visualization tool borrow checker

Enjoy your Sunday, and have a great week ahead.

Thanks for reading!
Bob Peters

Feel free to connect with me on LinkedIn