Show HN: 8-bit CPU simulator in C
github.com
Show HN: 8-bit CPU simulator in C
1–10 of 16 posts
Re: Show HN: 8-bit CPU simulator in C
#2Re: Show HN: 8-bit CPU simulator in C
#3For what it'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.
In my own Z80 emulation work, I have leaned heavily on 'Yaze' and 'Dasm', with a few extra features and debugging of my own.
Re: Show HN: 8-bit CPU simulator in C
#4Is this a simulation of some theoretical 8-bit CPU, or is it a simulation of a real, existing 8-bit CPU? For what it'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. In my own Z80 emulation work, I have leaned heavily on 'Yaze' and 'Dasm', with a few extra features an…
Re: Show HN: 8-bit CPU simulator in C
#5Advice 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.
Re: Show HN: 8-bit CPU simulator in C
#6For 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.
Here's a fairly simple set that might be a reasonable place to start...
ALU: and, or, xor, complement, shift left/right, rotate left/right, add, subtract, multiply, divide, compare (or just use subtract and set flags for this one)
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.
Control flow: jump, branch (maybe depending on flags from ALU), call/return
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't necessary to have all of them just for completeness.
Re: Show HN: 8-bit CPU simulator in C
#7Re: Show HN: 8-bit CPU simulator in C
#8I didn'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.
Re: Show HN: 8-bit CPU simulator in C
#9I did something similar a year or so ago, a really fun and rewarding project https://djhworld.github.io/post/2019/05/21/i-dont-know-how-c... I didn'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.
Re: Show HN: 8-bit CPU simulator in C
#10Judging by the files and their contents, this is actually a hardcoded gate-level simulator , which is very different in structure from the traditional loop-with-a-switch emulation that most people are probably expecting. 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.