Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Write your own Virtual CPU in C++ (megalomaniacbore.blogspot.com)
61 points by AlexeyBrin on Sept 27, 2014 | hide | past | favorite | 13 comments


I got this idea from a friend, but whenever I wanted to learn a new language, I would quickly hack out a 4001 emulator in that language. Its an interesting but quick challenge.

http://pastebin.com/YkhRsuih - C

http://pastebin.com/L79CeeGB - Haskell

http://pastebin.com/LybWwGzm - JS

http://pastebin.com/ZVDiRmP6 - MHS this language cant take input (its an educational toy language for a course)


As a beginner studying C, Java, Python and JS at the same time this is very interesting and educational. Thank you very much for posting these links.


I wish there were more people who thought about and understood these low-level basics; it feels like today, almost everyone who uses a computer is at such a far level of abstraction from the base hardware that they've lost the intuition for how computers behave the way they do and everything seems mysterious to them.

This book is highly recommended to anyone interested in how computers work; it starts off with very basic electronics and goes up to a full CPU, plus some software stuff near the end: http://www.charlespetzold.com/code/


My mips cpu which boots linux -

https://github.com/andrewchambers/cmips


I've written an (incomplete) 8086 emulator in C++ [1] as part of a bigger project [2]. This kind of thing is really fun to do and gives you a lot of insight of how CPUs work (as a black box, though).

[1] https://github.com/ggambetta/emulator-backed-remakes

[2] http://gabrielgambetta.com/remakes.html


You'll probably be interested in this: http://reassembler.blogspot.com/

This guy spent three years disassembling the Outrun arcade ROM, then 2 years re-implementing it in C++, then extending his engine with features like high fps and new game modes, and is now onto creating extender tools like track and time editors.

Along the way he found several new bugs and provides a fixed ROM which can be flashed to the arcade board. He's also got a project to develop an SoC which uses his engine to run an arcade cabinet, completely replacing the original board.

It's a phenomenal effort and an amazing blog to read back through.


A nice book I have found on the topic of virtual machines as they relate to programming languages is http://www.amazon.com/Compiler-Design-Machines-Reinhard-Wilh.... It is a pretty good book although the focus is less on emulating actual hardware and more on designing and implementing custom virtual machines as compilation targets for imperative, functional, and logic languages.

I kinda wish there were more meetups that focused on this kind of stuff instead of the latest and greatest frameworks. This kind of fundamental theory is applicable in any kind of application and framework setting.


I've done this in Racket. It was quite a fun little learning project. I've been meaning to dust off the core and add some more I/O hardware to it though.

https://github.com/jarcane/MicroMini if you're curious.


If this is something that tickle's your fancy, you should check out 'The Elements of Computing Systems' http://www.amazon.com/The-Elements-Computing-Systems-Princip....



Another one in JS that boots linux:

http://riscv.org/angel


It's orders of magnitude slower than Bellard's JSLinux (http://bellard.org/jslinux/) on my iPad. I haven't waited until the end.


That's largely (entirely?) because it's a 64-bit system whereas jslinux is 32-bit (JS does not natively support 64-bit ints).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: