Live data from Hacker News

I don't know how CPUs work so I simulated one in code (2019)

djharper.dev

41–49 of 49 posts

Re: I don't know how CPUs work so I simulated one in code (2019)

#41
Love to see people doing CPU projects!

I just recently wrote a JavaScript emulator [1] for a simple 8-bit CPU I had built with logic gates. Both were fun projects, but one took a weekend whereas the other took about 4 months. Having skimmed through the author's code, I totally understand why it took them so much longer to get their's working. It's implemented with gate-level simulated logic gates, which is an impressive achievement. It's so much easier to just emulate whole components like I did.

[1] https://naberhausj.com/miscellaneous/8-bit-computer/page.htm...

Re: I don't know how CPUs work so I simulated one in code (2019)

#42
post #34
post #8

Earlier quoted context omitted.

The game Silicon Zeroes ( https://store.steampowered.com/app/684270/Silicon_Zeroes/ ) teaches this. It starts out with components computing their outputs instantaneously, but then introduces the concept of microticks such that the output of a component is unstable until its inputs are stable enough for some time, so the clock speed must be adjusted according to the largest delay in all circuit paths. The game starts…

I found that Silicon Zeroes is a pretty crappy emulation of a Zachtronics game. IIRC each level has an intended solution and only gives you the exact parts to create that solution, even if a better solution would be possible with different parts you've used before.

The earlier levels are like that, but the later levels have a lot more leeway.

Re: I don't know how CPUs work so I simulated one in code (2019)

#43
It can be fun to look at the various "achievements" or things that are generally possible for determined people (no geniuses allowed).

CPU just happens to be one of them. Fascinating that such a seemingly complex marvel of innovation is just something that everyone can do.

Of course, there's lots of groundwork and you need to know "the trick" (eg. that you can use transistors to make computation machines)... but it's easily within your grasp.

Re: I don't know how CPUs work so I simulated one in code (2019)

#45

Earlier quoted context omitted.

Old minicomputers had toggle switches on the front panel where you could set the program counter, enter machine code instructions, and other things, as well as step the clock. https://thumbs.worthpoint.com/zoom/images2/1/0619/05/vintage...

Image no longer works. Reupload?

It's not my site. It's the front panel of a Texas Instruments model 980. It's from this page.

https://www.worthpoint.com/worthopedia/vintage-ti-980-ttl-ba...

Re: I don't know how CPUs work so I simulated one in code (2019)

#46
post #12

I was once shown a dos-based CPU simulator back in the mid/late 90s. From memory it showed instruction decode, execution, cache and memory. Unfortunately I've never been able to find it, because all the google results are about running DOS games and/or DOSBox.

It is probably on one of the SIMTEL shovelware cdroms. https://archive.org/details/SIMTEL_0692 A search engine won't help you, but a local llm might.

Good suggestion, thanks.

ChatGPT came up with what I thought was a halucination: "CPU Sim".

After some searching, I found a Facebook post[1] with a photo of it running. That looks mostly like what I recall. The screenshot is from a 1987 version.

I can also find references to a Windows version, but no copies of either. I thought there were more features to it, but perhaps I'm misremembering or I've also thinking of Mikrosim that Tomte mentions in the sibling comment.

[1] https://www.facebook.com/RMUserGroup/posts/pfbid0uVrCYMsWmJa...

Re: I don't know how CPUs work so I simulated one in code (2019)

#47
post #24

I was once shown a dos-based CPU simulator back in the mid/late 90s. From memory it showed instruction decode, execution, cache and memory. Unfortunately I've never been able to find it, because all the google results are about running DOS games and/or DOSBox.

Mikrosim maybe? The name is because it simulates microcode. That was a huge a-ha moment for me in school. https://en.wikipedia.org/wiki/MikroSim http://www.mikrocodesimulator.de/index_eng.php

Good find, thanks.

That one is looking more familiar the more screenshots I see of it, but I think I might be mixing memories of two programs - this, and "CPU Sim" which I mention in a reply to the sibling comment.

Re: I don't know how CPUs work so I simulated one in code (2019)

#48
post #4

So, what this project misses, which is quite hard to capture if you think of gates being just on off switches, is the fact that signals are not instantaneous, and everything runs in parallel. As the AND gate 4 gates up the chain switches the NOT gate 4 gates down the chain starts to send different and unstable signals which may or may not be interpreted as a 1 or 0 in the downstream gate. That's the reason computers…

I'm trying to think of how you could simulate the non-instantaneous/clocked aspect of it in code without using an HDL, so you could write a more accurate simulator in a familiar language. I'm thinking you'd want each gate to store its value on the current clock tick, then you'd have a RunTick() function or something which uses the previous values to calculate the new values on the current tick for everything, then stores those new values. I'm not sure how that'd work for an SR latch, a gated SR latch, or a D flip-flop, if you wanted to model those at a gate level; would you have to model those as primitives?

EDIT: After a bit of thinking and bouncing ideas off of ChatGPT, one approach might be to have an SR latch that gets initialized to some arbitrary valid output, which can then use that starting output to calculate future states. From there, you could build up more complicated elements.

Re: I don't know how CPUs work so I simulated one in code (2019)

#49

Earlier quoted context omitted.

The thing that got me into CS was building a CPU in Minecraft redstone, which is surprisingly good at being a logic simulator. You only got (back in my day) NOT (the torch) and OR gates (wiring next to each other), and everything was built out of them. Signal repeaters had delay, the gates had delay, so you naturally had to build a clock to synchronise it all. The main benefit that I enjoyed was that you could see th…

Shameless plug, I made this website which simulates Minecraft redstone as a hobby project many years ago: https://mordritch.com/mc_rss/ Make sure to press the "Play" button at the top for the simulator to actually run.

This is awesome, thanks a lot!
Post reply on HN