Kalan's Blog

Kalan 頭像照片,在淡水拍攝,淺藍背景

四零二曜日電子報上線啦!訂閱訂起來

Software Engineer / Taiwanese / Life in Fukuoka
This blog supports RSS feed (all content), you can click RSS icon or setup through third-party service. If there are special styles such as code syntax in the technical article, it is still recommended to browse to the original website for the best experience.

Current Theme light

我會把一些不成文的筆記或是最近的生活雜感放在短筆記,如果有興趣的話可以來看看唷!

Please notice that currenly most of posts are translated by AI automatically and might contain lots of confusion. I'll gradually translate the post ASAP

Starting from NAND Logic Gates - Turing Complete Game Review

In the past, when it comes to discussing computers based on NAND logic gates, the first thing that comes to mind is the nand2tetris course. This course teaches you how to build a functioning computer (simulated on an emulator) from the ground up.

Starting from the basic theory, you begin with hardware-level logic gates and various circuit design instructions. You write the necessary components, design assembly language and instruction sets, and finally write software-level programming languages, including compilers and assemblers. By completing nand2tetris, you will understand how a computer actually works.

Turing Completeness

By breaking down the tasks of a CPU, you can see that most operations involve computations such as arithmetic operations, bit shifting, matrix multiplication, and so on. So, how do we know if a computer can compute everything that is "computable"?

For example, in a game, you may encounter a full adder, which can perform addition operations. Subtraction can be achieved using the concept of two's complement and addition. But what about multiplication? Sine function? Cosine function?

We can keep adding new hardware circuits like multipliers, sine function generators, cosine function generators, but it's never-ending because there are so many formulas in the world, and it's impossible to implement all of them in hardware circuits.

Alan Turing proposed the theory of Turing completeness in his paper. In simple terms, if we can store the current execution instruction address (Program Counter) and obtain the next state (Register) based on the current instruction, we can achieve Turing completeness. Turing proved that such an architecture can solve all computable functions.

Visualizing Digital Logic

Recently, I discovered a game called Turing Complete on Steam, which is similar to nand2tetris (but only up to assembly language). It also starts with logic gates and gradually assembles a Turing complete computer.

This game visualizes the code writing process by allowing you to drag and drop logic gates as building blocks. It provides real-time interaction and convenient observation of truth table changes.

XOR gate

By simplifying the process of writing truth tables with drag-and-drop connections, it eliminates the need for pen and paper. You can switch between 1 and 0 (ON and OFF) by clicking with the mouse, eliminating the need for software installation and HDL description languages, and allowing players to focus on the core concepts.

A CPU can be broken down into countless NAND logic gates. Your goal is to complete levels starting from logic gates and gradually build a working computer.

Starting from logic gates, you combine various circuits including registers, memory, multiplexers, and demultiplexers. Through various functional levels, you become familiar with circuit characteristics. Once you complete a level, the game converts your assembled circuit into components and gives them to you. As you progress in the game, you will have more and more components. The circuits in later levels will become more complex, requiring more components and connections.

full adder

demux

Journey to Turing Completeness

A significant milestone in the game is achieving Turing completeness. After completing the "WORKING COMPUTER" level, you will realize that all the circuits you painstakingly assembled were for this moment. This level requires you to meet several conditions:

  • Determine which operation to perform based on the bit of the byte code: ADD, XOR, OR, etc.
  • Program Counter: Keep track of the program's execution position
  • Determine whether to execute a jump (go to a specified address) based on a certain bit: you need to implement six cases (greater than, less than, equal to, always, never)
  • 5 registers

Turing Complete

The game becomes even more challenging as you progress. It will ask you to use your assembled circuits to meet specific requirements in subsequent levels, such as:

  • Calculate mod 4: At the beginning, the game will guide you to use byte code and click manually.

    byte code

  • Calculate 2 * pi * r: Next, you can use the assembly editor to write your own instructions and create your instruction set.

assembly editor

  • Solve a maze: Write code to navigate through a maze.

If you genuinely engage with the game's challenges and complete the levels, I believe you can gain a deep understanding of computer organization. It can be supplemented with textbooks to enhance comprehension and even surpass the foundation of some engineers in certain areas.

Evolving Existing Circuits

Once you complete the game's designated levels using assembly language, more advanced elements will be introduced into the existing framework. One simple example is why the number of registers is limited, but the program allows for so many variables to exist. The answer is RAM. Therefore, the game will guide you to add RAM for addressing and achieve specific functionalities.

RAM

Originally, when executing instructions, the circuits could only use r1, r2, and r3 as temporary registers for calculations or jumps. However, this can be cumbersome when writing code. Therefore, the game will ask you to implement the structure of OPCODE, argument1, argument2, and result. The program reads 4 bytes at a time, and you need to decode them so that the entire circuit can freely choose which registers to use for parameters and where to store the results.

With RAM and a more flexible circuit structure, the next step is to implement stack functionality, allowing our circuits to store a series of contexts. This forms the basis for implementing functions. This means the circuits will become even more complex.

Stack circuit

Once you reach this stage, a new feature called "scoring" will be unlocked. When designing circuits, it is ideal to perform as many operations simultaneously as possible because the more logic gates the circuit passes through, the more delay it incurs. The game calculates the number of NAND gates used and the delay, allowing you to optimize your circuits for better performance.

Most of the subsequent levels involve modifying the existing framework and using assembly language to complete more advanced mini-games.

Drawbacks

This game is still in development. Although it doesn't require a solid IT foundation, it may be helpful to have a general understanding of what logic gates do and some knowledge of binary before playing.

Additionally, the game lacks guidance and prompts. Many times, you have to spend time struggling, constantly experimenting, thinking, and improving your circuits. Especially in later stages, the circuits become more challenging, and it's difficult to experience the fun of the game. The game interface is also not very intuitive, making the process of connecting wires quite frustrating.

There were many instances where the game suddenly crashed while I was playing, especially in the later stages. I don't know if this issue will be addressed in the official release.

Can You Learn from Playing Games?

Another example worth mentioning is Nintendo Switch's "Learn Programming with Dr. Kawashima: Coding Adventure."

The game's tutorial design is excellent. It provides numerous examples to guide you through implementing various functionalities, showing you what is needed and allowing you to run the code and make corrections. The cycle of requirement, implementation, verification, and modification is similar to the actual development process. In the game, you don't need to write any code; all the functionalities are abstracted as "nodes" that you can drag and drop. The essence of programming lies not in the syntax itself, but in the underlying logical thinking. Moreover, the game's documentation for beginners is exceptionally well-written and serves as a model for developers.

Turinb Complete is a bit more challenging. It doesn't provide much hand-holding (although this may change in the official release), so you often have to figure things out on your own.

The benefit of gamification is that it eliminates unnecessary setup, language syntax understanding, and dives straight into the core concepts. The author visualizes the concepts of logic gates and truth tables as draggable blocks, making it much more intuitive than writing code or text.

Conclusion

This game allowed me to revisit the fundamentals of computer architecture and review nand2tetris. Although I had some exposure to similar courses in school, very few provided the opportunity to build a working computer from scratch. While most of the concepts were covered in school, actually connecting the circuits often led to challenges. Therefore, it was truly satisfying to successfully assemble the circuits.

In reality, it's not practical to use the circuits assembled in the game. In the real world, it's often not as simple as connecting the desired functionalities. However, for introductory learning purposes, this approach can help learners grasp the core concepts more quickly. I highly recommend this game for those interested in understanding how computers work.

There is a quote from the game's preview video that left a deep impression on me:

If you try to make such projects, unseen by others, as perfect as any human could, you'll develop skills that other professionals don't have.

However, after playing the game, I found that the learning curve is quite steep. The game provides minimal guidance and explanations, requiring you to figure things out on your own most of the time. This can lead to struggles and constant trial and error, especially as the circuits become more challenging. Therefore, it may not be suitable for those looking for pure entertainment.

I even think that in the 21st century, with the maturity of computer development, the combination of powerful computing capabilities and visual effects can exponentially enhance learning. When learning digital logic, constantly modifying input values to observe their relationship with outputs is much more convenient on a computer than on paper. With a single click, you can switch between 0 and 1, and the outputs can change instantly.

  • Reduced feedback cycle costs
  • Real-time observation of results

Just these two points allow us to surpass the learning efficiency of people decades ago.

References

This is a previous livestream, although not all the game content is covered, most of the levels can be found in the videos.

Prev

Misconceptions about NFTs

Next

Future Energy Shortage Concerns

If you found this article helpful, please consider buy me a drink ☕️ It'll make my ordinary day shine✨

Buy me a coffee