a "teaching device" CPU in Python
Goal
To demonstrate the major Operating Systems (OS) mechanisms on a very simple, software-implemented CPU.
Doubts
Q: Why on a software-implemented CPU? A: Because in order to understand what fundamental problems need to be tackled in a computer system - and more specifically which of those problems' solutions are part of the OS - one needs to understand how those problems come about. So we create a CPU ourselves to see those problems emerge and then we solve them.
We do the CPU in SW, because creating a working CPU has the lowest requirements when created as SW.
Stages
We're going to develop the CPU step by step. This are our steps that we want to reach one by one:
-
basic CPU:
- has an accumulator ACC
- has RAM
- has a Programm Counter PC
- can fetch operations from memory
- can load a word from memory to ACC
- can store the ACC to memory
-
collaborative multitasking
ATTENTION when working on this repo
Until this project is somewhat stable I intend to keep on rewriting its git history. Justification: the goal here is for the commits to reflect clear logical progress and not the historic tribulations of its development.
That means that until this project is stable you can not expect to be able to pull from this repo without major conflicts.
Understanding what's in this repo and how to run the cpu.py
-
cpu.py
- this is a minimal virtual CPU written in Python. It has a memory, but the memory is empty. Its memory needs to be filled with machine code and data in order to do something useful. This CPU has the "Tpo" instruction set (just a random name).
-
program.asm
- this is example assembler code that solves the above stated
problems:
- it contains a minimal "OS"
- it contains to processes or "programs"
- it contains an interrupt handler to do context switching between processes
- this is example assembler code that solves the above stated
problems:
-
program.lst
- this is the generated machine code for the given cpu from
program.asm
. This code has been generated with [kasm](https://github.com/tpo/kasm-generic/ for the "Tpo" CPU instruction set.
- this is the generated machine code for the given cpu from
-
lst_to_commented_bytes.py
- this will transform
program.list
into a python array along with comments, so that it can be used by thecpu.py
- this will transform
-
cpu_with_program.py
- this is the cpu.py code coupled with "memory" generated fromprogram.lst
. That is this is thecpu.py
with code and data fromprogram.asm
running on it. -
run_program
- combines all the necessary steps and executes
program.asm
on thecpu.py
- calls
kasm.py
to generateprogram.lst
fromprogram.asm
- calls
lst_to_commented_bytes.py
to generate a pythonmemory
array fromprogram.lst
- combines
cpu.py
and the generatedmemory
array from the previous step into acpu_with_program.py
executable - executes
cpu_with_program.py
- calls
- combines all the necessary steps and executes