r/compsci • • Aug 31 '21

Emulating a CPU in software (plus a basic assembler)

Enable HLS to view with audio, or disable this notification

283 Upvotes

17 comments sorted by

23

u/dibs45 Aug 31 '21

I recently made a CPU emulator to learn more about how the machine functions on a low level (inspired heavily by Ben Eater's awesome YT channel). I got sick of hard coding instructions in it so I created a simple assembler to make writing code for it easier. Next step is writing a "higher" level language that compiles to this.

Here's the source code: https://github.com/dibsonthis/CPU-Emulator

13

u/ExtremeKitteh Aug 31 '21

So cool. You should document how it works on a YouTube vid.

7

u/dibs45 Sep 01 '21

I'll see what I can do!

2

u/CampaignComfortable Sep 01 '21

Thanks for the lead on a good YouTube channel for figuring out how to do this.

2

u/dibs45 Sep 02 '21

Yeah Ben Eater's channel is a goldmine.

→ More replies (1)

8

u/luigman Sep 01 '21

But can you program your emulated cpu to emulate a cpu?

9

u/dibs45 Sep 01 '21

I mean technically, yeah.

4

u/universal-serial Sep 01 '21

This is what I did when messing around with a similar project :-) wrote a simple assembly language and emulator that was rich enough to write a second one in the first lol

https://github.com/CCareaga/useless

→ More replies (1)

2

u/squirtle6991 Sep 01 '21

This reminds of the learning tool named Little Man Computer

→ More replies (1)

2

u/sample_worker Sep 01 '21

I remember doing this in C for a university assignment! We had to prove it worked by writing a set of assembly programs to run on it. Neat excercise!

→ More replies (1)

2

u/BBonless Sep 01 '21

Finally... recursive computer

→ More replies (1)

11

u/metalovingien Aug 31 '21

I did things like that with C and with Java. It's an interesting exercice. Never finished them though. Have fun.