0BSD
Cursed
TL;DR: I wrote a simulator for an ISA with its own assembly language.
LC2K is a Fisher-Price instruction set architecture (ISA) we teach in the course EECS 370: Intro to Computer Organization at the University of Michigan. Part of project 1 is writing a simulator for LC2K in C. However, one day it randomly occurred to me that it is possible to simulate LC2K in LC2K.
If you haven't taken 370, the following will not make sense. In that case, just
open p1s.as
, admire its beauty, close the tab, and go on with your life.
To build this, you will need:
Obviously I cannot provide any of them.
Printing memory is slow, so you may want to comment out invocations to
printState
in simulator.c
except for the last one.
Then, just run ./test.sh <TESTCASE>.as
. The script does the following:
p1s.as
-> p1s.mc
)<TESTCASE>.as -> <TESTCASE.mc>
)p1s.mc
and <TESTCASE>.mc
into test.mc
so that p1s.mc
begins at address 0 and <TESTCASE>.mc
begins at address 256test.mc
Note that I put little thought into error handling in test.sh
.
The simulator takes ~1500 cycles to simulate one LC2K instruction.
Test case | Cycles (C) | Cycles (LC2K) | LC2K / C |
---|---|---|---|
spec.as (p1) | 17 | 26110 | 1536 |
fib(11) (p2r, mine) | 5679 | 8776469 | 1545 |
I believe that most of the cycles are spent on right-shifting the instructions. This can be optimized, but it does not mean I am going to. Cope.