Series Note

This blog is part of my ongoing #lab-build-cpu series, where I’m building a CPU emulator in Python and documenting the process. You can follow the labs on GitHub. The project was inspired by Matt Godbolt's lecture series on machine language and low-level computing.

Deliverable:

Deliverable v0.01.01: Browse the code at v0.01.01 tree or view the release page.

Objective:

Build a CPU emulator in Python with its own pseudo-assembly language as a hands-on way to learn CPUs from the inside out.

Work log

The v0.01.01 release of the CPU emulator includes:

  • CPU properties
  • a program load method
  • a current-state report method
  • a run method
  • the logic that defines the pseudo-assembly language

Takeaways:

  • Learning C: Getting elbow-deep into the innards of a CPU showed me that C is an extension of assembly language. Coming from python, I had trouble grasping the concept of C pointers. Now, I see, just like my LOAD_IDX and STORE_IDX operations, C pointers use some variation of mem[n] to access specific slots in memory.
  • Code construction: To structure the logic for the pseudo-assembly language, I identified potential errors for each operation, grouped them, and wrote helper functions to validate input or raise errors. I then called these functions directly in the code for each operation. I found this to be a powerful method for building well-structured code that can easily scale.
  • Documentation: Writing clear, comprehensive documentation is a chore, but, just like good code structure, it is fundamental to building programs that can scale. It's easy to build a small program.

    My challenge: can I build programs that move mountains?

Next Steps:

  • Set up the project structure including pyproject.toml.
  • Build tests to run with pytest.
  • Write two different programs in the pseudo-assembly language that build fibonacci series and compare performance.
  • Expand the pseudo-assembly language with flags, comparisons, conditional branching and additional capabilities.

This marks the starting point of the project. Future entries will track the CPU as it evolves, feature by feature, into a richer system.

Review all posts in this series

tags: #lab-build-cpu, #applied-learning, #computer-systems, #fundamentals

Building a CPU — Week 1: CPU and Pseudo-Assembly Language