Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

Building a Hack Computer: From Logic Gates to CPU Control in COMP1027

A step-by-step tutorial covering elementary logic gates, combinatorial circuits, and the Hack computer architecture for COMP1027 coursework 1, with real-world baking and gaming analogies.

COMP1027 coursework 1 elementary logic gates Boolean algebra simplification HDL chip implementation MyALU design Hack computer architecture assembly programming Hack power function assembly factorial assembly Hack CPU control unit memory chip Hack nand2tetris chapter 5 CPU instruction decode combinatorial circuits sequential circuits student programming help

Introduction: From Tessa's Café to the Hack Computer

In the spring of 2026, as AI-powered apps and esports tournaments dominate headlines, the fundamentals of computer architecture remain as crucial as ever. This tutorial guides you through COMP1027 Coursework 1, focusing on three parts: elementary logic gates, combinatorial and sequential circuits, and the Hack computer. Just as Tessa, a talented baker, must prioritize location, furniture, and menu to open her café, you'll learn to prioritize circuit design using Boolean algebra. Whether you're into gaming, finance, or AI, understanding how a CPU executes instructions is key to building efficient systems.

Part 1: Elementary Logic Gates – Tessa's Decision Chip

Part 1 introduces you to logic gates using a relatable scenario: Tessa wants to open a café in 2025. She needs a location (L), furniture (F), and a menu (M). She'll be happy if she fulfills two or three conditions, specifically (L and F) or (L and M). This translates to a Boolean expression: Output = (L · F) + (L · M). Using Boolean algebra identities, we simplify: L · (F + M). You'll implement this as a 1-bit chip in HDL, creating a Tessa.hdl file. This exercise mirrors how AI decision-making apps prioritize tasks—like a gaming AI choosing actions based on multiple inputs.

Truth Table and Simplification

Complete the truth table for three inputs (L, F, M) and output. For example, when L=1, F=1, M=0, output=1. Then write the pre-simplified expression: L·F·M' + L·F·M + L·F'·M = L·F + L·M. Simplify to L·(F+M). This is exactly how you'd optimize a circuit for a smart home app that controls lights based on occupancy and time.

Part 2: Combinatorial & Sequential Circuits – MyALU

Part 2 builds an Arithmetic Logic Unit (ALU) with 5 control bits (C4..C0) performing 20 functions, from 0 to X XOR Y. You'll complete the truth table in Table 1, then implement MyALU.hdl. For instance, when decimal value is 5, C4..C0 = 00101, output = Y. This ALU is like the core of a gaming console's CPU, processing player inputs to render graphics. Sequential circuits (flip-flops) will be added later for memory.

Creating Test Files

You must create your own .tst and .cmp files to verify your ALU. For example, test X=3, Y=5, control=5 (Y output) should give 5. This step ensures your chip works like a finance app's calculator—accurate and fast.

Part 3: The Hack Computer – Assembly and CPU

Part 3 culminates in building the Hack computer. You'll write two assembly programs: Power.asm and Factorial.asm, then implement Memory and CPU chips.

Section 3.1: Assembly Code – Power and Factorial

Power.asm: Calculate m^e. User inputs m in RAM[0], e in RAM[1]. Store result in RAM[2]. If e=0, store 1. Use a loop that multiplies m by itself e times. This is like calculating compound interest in a finance app.

Factorial.asm: Calculate n! where n in RAM[0], result in RAM[1]. Use a loop multiplying decreasing numbers. For n=5, output 120. This mirrors computing permutations in esports team matchups.

Section 3.2: The Hack Computer – Memory and CPU

Memory Chip: Implement a 16K RAM, Screen, and Keyboard. The memory chip is like a smartphone's RAM—fast access for apps. Use built-in parts from nand2tetris.

CPU Chip: Implement instruction fetch and execute. Decode A-instructions (load address) and C-instructions (compute and jump). The control unit manages data flow. This is similar to how a gaming CPU handles AI pathfinding.

Practical Tips for Success

  • Divide and conquer: Work in mini-groups as suggested. One person focuses on logic gates, another on ALU, and a third on assembly.
  • Use provided skeletons: Start with .hdl and .asm files from CW1-Files.zip. Modify only the required sections.
  • Test incrementally: Run .tst files after each chip. For example, test Tessa.hdl with all input combinations.
  • Refer to nand2tetris Chapter 5: It explains CPU implementation in detail, including control logic.

Connecting to Current Trends

In 2026, AI apps like ChatGPT rely on efficient logic circuits for inference. Esports tournaments use factorial calculations for bracket seeding. Finance apps use power functions for investment growth. By mastering these fundamentals, you're building skills for modern tech careers.

Conclusion

This tutorial covered COMP1027 Coursework 1 from Tessa's logic gates to the Hack CPU. You've learned Boolean simplification, ALU design, and assembly programming. Now, submit your files: Tessa.hdl, MyALU.hdl, Power.asm, Factorial.asm, Memory.hdl, and CPU.hdl. Good luck!