TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: 8-bit CPU simulator in C

67 pointsby rzachalmost 5 years ago

6 comments

userbinatoralmost 5 years ago
Judging by the files and their contents, this is actually a <i>hardcoded gate-level simulator</i>, which is very different in structure from the traditional loop-with-a-switch emulation that most people are probably expecting.<p>Advice for the author: if you use a netlist-based abstraction instead of hardcoding the circuit in code, you will be able to simplify the code, and make it much easier to modify and inspect.
评论 #23677352 未加载
评论 #23677075 未加载
rzachalmost 5 years ago
It&#x27;s not quite done. I&#x27;m still working on the memory components and the instruction set. Feel free to uncomment the testing functions in test.c to see the other components. Also feel free to give advice on ALU and instruction set design.
simonblackalmost 5 years ago
Is this a simulation of some theoretical 8-bit CPU, or is it a simulation of a real, existing 8-bit CPU?<p>For what it&#x27;s worth, there are many existing C-code emulations of actual 1970s 8-bit CPUs on the Net. (6800, 8080, Z80, 6502, etc, etc, etc.) Studying those may assist you in managing to get past the tricky bits.<p>In my own Z80 emulation work, I have leaned heavily on &#x27;Yaze&#x27; and &#x27;Dasm&#x27;, with a few extra features and debugging of my own.
评论 #23673770 未加载
评论 #23677417 未加载
ls65536almost 5 years ago
This is neat! I had some thoughts recently about creating something similar as well.<p>For a simple but still practical instruction set, you can probably sanely get away with about two dozen or so different instructions, but at the expense of having larger programs (by number of instructions) to accomplish the same tasks compared to a more featureful instruction set.<p>Here&#x27;s a fairly simple set that might be a reasonable place to start...<p>ALU: and, or, xor, complement, shift left&#x2F;right, rotate left&#x2F;right, add, subtract, multiply, divide, compare (or just use subtract and set flags for this one)<p>Memory: move (copy between registers), load and store (between memory and a register), maybe also push and pop for sanity if you want to provide explicit stack instructions.<p>Control flow: jump, branch (maybe depending on flags from ALU), call&#x2F;return<p>It might also be helpful to look at the 8-bit AVR instruction set (quite popular for small microcontrollers, including what you might find on many Arduino boards). It contains about a hundred or so instructions, with all of the above list present in one form or another (although many are just aliases for some other instruction with the same opcode but otherwise fixed parameters). In the case of AVR, where code size is often the limiting constraint, having a larger vocabulary of instructions is useful for expressing a program in the smallest amount of space possible, but it certainly isn&#x27;t necessary to have all of them just for completeness.
评论 #23677529 未加载
评论 #23685142 未加载
CGamesPlayalmost 5 years ago
Cool project! You might already know about this, but if you don&#x27;t, this is an excellent course on implementing an 8-bit computer, and might answer your questions about the instruction set. <a href="https:&#x2F;&#x2F;eater.net&#x2F;8bit" rel="nofollow">https:&#x2F;&#x2F;eater.net&#x2F;8bit</a>
djhworldalmost 5 years ago
I did something similar a year or so ago, a really fun and rewarding project <a href="https:&#x2F;&#x2F;djhworld.github.io&#x2F;post&#x2F;2019&#x2F;05&#x2F;21&#x2F;i-dont-know-how-cpus-work-so-i-simulated-one-in-code" rel="nofollow">https:&#x2F;&#x2F;djhworld.github.io&#x2F;post&#x2F;2019&#x2F;05&#x2F;21&#x2F;i-dont-know-how-c...</a><p>I didn&#x27;t go as far as writing my own OS, but I did develop some simple functions to render characters on a screen and accept keyboard input.
评论 #23677013 未加载