Live data from Hacker News

How a CPU works: Bare metal C on my RISC-V toy CPU

florian.noeding.com

21–30 of 39 posts

Re: How a CPU works: Bare metal C on my RISC-V toy CPU

#21
post #2

I always wanted to understand how a CPU works, how it transitions from one instruction to the next and makes a computer work. So I thought: let's implement one and run a C program on it.

The textbooks by Hennessy and Patterson are the definitive ones.

Re: How a CPU works: Bare metal C on my RISC-V toy CPU

#22
post #2

I always wanted to understand how a CPU works, how it transitions from one instruction to the next and makes a computer work. So I thought: let's implement one and run a C program on it.

The textbooks by Hennessy and Patterson are the definitive ones. I think every programmer would be very well served reading them. There's older editions available online for free that are perfectly adequate for understanding the big picture.

Re: How a CPU works: Bare metal C on my RISC-V toy CPU

#23

This gets brought up every time the topic shows up but https://www.nand2tetris.org is a course that abstracts how a computer works, and is worth checking out

Is there a version of something like this that’s purely software? Like, emulate gates and keep building bigger pieces until you have a function that’s a CPU. Etc. I guess I’m thinking a bit more lower level than an emulator where you implement an opcode using normal capabilities of your language of choice. I guess TFA is pretty close to this in ways, too.

I have been building a CPU using: https://github.com/hneemann/Digital

Much faster than Logisim, UI a little clunky, but my CPU runs around 0.5Mhz and it has very nice peripherals like Telnet, graphics ram, VGA etc

Terrible name that is hard to google, but great tool.

Re: How a CPU works: Bare metal C on my RISC-V toy CPU

#24
post #4

This is a really fantastic read! I wonder if the author might consider including links to the git source for the entire project?

Thank you!

I'll think about sharing the code. Large code parts - while working - did not pass my quality bar, since I did not have enough time to clean and refactor while learning. But maybe that's fine. Hmm.

Re: How a CPU works: Bare metal C on my RISC-V toy CPU

#27
post #11
post #6

Earlier quoted context omitted.

Check out some videos by Ben Eater on Youtube

Ben Eater routinely makes me feel ashamed of how awful my breadboard wiring looks. Fantastic videos.

To be fair, all the wires he uses are pre-cut / pre-shaped for the video. The videos are meticulously planned and marvelously executed. Normally hobby projects do not have nearly as much thought put into them.

Re: How a CPU works: Bare metal C on my RISC-V toy CPU

#28
post #4

This is a really fantastic read! I wonder if the author might consider including links to the git source for the entire project?

Thank you! I'll think about sharing the code. Large code parts - while working - did not pass my quality bar, since I did not have enough time to clean and refactor while learning. But maybe that's fine. Hmm.

>"But maybe that's fine."

Oh I think it's more than fine :)

Your goal wasn't to write a C++ or Python app it's was to create almost everything from first principles. I also don't think anyone would doubt your abilities after reading this post. You could link to the article in the repo. I think it's speaks for itself. My first thought when I read the article was "Wow, this is great." I've read it twice now. Thank you for sharing.

Re: How a CPU works: Bare metal C on my RISC-V toy CPU

#29

This gets brought up every time the topic shows up but https://www.nand2tetris.org is a course that abstracts how a computer works, and is worth checking out

Is there a version of something like this that’s purely software? Like, emulate gates and keep building bigger pieces until you have a function that’s a CPU. Etc. I guess I’m thinking a bit more lower level than an emulator where you implement an opcode using normal capabilities of your language of choice. I guess TFA is pretty close to this in ways, too.

In college, we wrote a CPU simulation in C, for a simple architecture called "little computer". You could call it an emulator, but the whole idea was to faithfully implement all the pipelining and register renaming and stuff. A main loop iteration was equivalent to a clock cycle.

Re: How a CPU works: Bare metal C on my RISC-V toy CPU

#30

This gets brought up every time the topic shows up but https://www.nand2tetris.org is a course that abstracts how a computer works, and is worth checking out

Is there a version of something like this that’s purely software? Like, emulate gates and keep building bigger pieces until you have a function that’s a CPU. Etc. I guess I’m thinking a bit more lower level than an emulator where you implement an opcode using normal capabilities of your language of choice. I guess TFA is pretty close to this in ways, too.

The great Niklaus Wirth wrote a book which you might want to check out: Digital Circuit Design for Computer Science Students: An Introductory Textbook.
Post reply on HN