Recently, I implemented a
RISC-V
simulator and assembler in AWK:
awk-riscv. This started as a joke,
evolved into wanting to prove a point (namely, that AWK being a Turing-complete
language, can compute anything which is compute-able) , and ended up being a
lot of fun. I will provide a narrative overview of the
riscv.awk
program, and also discuss more broadly the basics of writing a CPU simulator.
Even if you don’t know AWK, but want to learn about CPU simulators, you’ll probably be able to follow this article. AWK is an interpreted, record-oriented language with a C-like syntax, and I think you will agree that most of the code is quite readable to anyone with experience in C-like languages. Writing a CPU simulator is a great learning experience, and I think every programmer should do it at least once.