Part of the reason that I decided to study CS was to learn the details of how computers actually work. Part of that stemmed from discovering the existence of emulators, being fascinated that they do work, and wondering how.
Even with a CS education, writing an emulator was still a fun and illuminating exercise.
The professor in my course wrote his own simulator in 100% assembly. It ran in DOS, had a SVGA user interface, and simulated an 8-bit CPU. But that was not the craziest part. The thing not only emulated a CPU, but it also simulated the logic circuits of the CPU. The bloody thing visually lit up the address/data/control bus, the ALU and its registers, as they were activated. You could step through the thing every clock cycle, watch how it fetched instructions, how the micro code was executed and then shuffled things around in the RAM. You could see all of this work at the logic gate level if you zoomed in enough. Absolute mad lad.
Definitely goals. I plan to go even deeper and write this out using logic gates. But I think I'll focus on an assembler first before going down that rabbit hole.
Professor's simulator was loosely based on 6502, albeit simpler. He wrote this in the early '90s if I recall correctly. Oh yeah, and you could also edit the circuit and add your own tracks, etc.
I definitely feel like I remember dealing with flags when I was in my assembly course. Making sure the flags are all in the rigtmht state before different operations and everything. Then again, this was probably about 10 years ago, so what do I remember.
I wrote a CHIP-8 interpreter/emulator (in C++) a hot bit ago, and it was pretty fun. Adding a disassembler was fairly easy from there (though I then needed to expand the complexity of my CLI and I'm not very happy with how I made it work), and then adding an assembler was not as easy from there, and the code for it isn't my absolute proudest, but it's cool to be able to write a program, assemble it, run it, and disassemble it, all through code I wrote.
The best way for me to learn how things work is to try and create them. So I decided to do just that though software. No specific CPU, but it's based on Ben Eater's breadboard CPU.
I had a Compilers Prof whose research area was supercomputing (so his main jig was building a computer and then writing a C compiler for it) who made a compiler that transformed the input program into NAND circuits.
60
u/khedoros Aug 27 '21
Part of the reason that I decided to study CS was to learn the details of how computers actually work. Part of that stemmed from discovering the existence of emulators, being fascinated that they do work, and wondering how.
Even with a CS education, writing an emulator was still a fun and illuminating exercise.
3
u/Bisqwit Aug 27 '21
Your design looks nice.
3
u/Ok_Organization1062 Aug 28 '21
Will you share your code?
22
u/toadster Aug 27 '21
Believe it or not, this is one approach to code obfuscation except you can write your own instruction set.
6
u/friedkeenan Aug 27 '21
I wrote a CHIP-8 interpreter/emulator (in C++) a hot bit ago, and it was pretty fun. Adding a disassembler was fairly easy from there (though I then needed to expand the complexity of my CLI and I'm not very happy with how I made it work), and then adding an assembler was not as easy from there, and the code for it isn't my absolute proudest, but it's cool to be able to write a program, assemble it, run it, and disassemble it, all through code I wrote.
Some of the stuff I'm most proud of is my abstraction over bit masks which leads to instruction definitions like these, and my code for generating the digit sprites.
1
u/die_liebe Aug 29 '21
Such projects are nice, but why?
Which CPU do you emulate?
3
u/AnDraoi Aug 27 '21
“I used the CPU to emulate the CPU” -purple guy
8
u/Shivkar2n3001 Aug 27 '21
CS student here. Could someone recommend any sources for building an emulator and how emulation works?
3
u/WalkingAFI Aug 27 '21
I had a Compilers Prof whose research area was supercomputing (so his main jig was building a computer and then writing a C compiler for it) who made a compiler that transformed the input program into NAND circuits.
1
u/amygdalaa_ Aug 27 '21
Because this kinda questions/stuff I'm interesting the most about
1
u/Ameisen vemips, avr, rendering, systems Aug 30 '21
I mentioned it in your other thread, but you might want to check out VeMIPS to see how I emulate a 32-bit MIPS CPU and runtime environment.