9 ms·
Running a Unix-like OS on a home-built CPU with a home-built C compiler
- CyberRabbi 6y agoJapanese people are incredible. Such an advanced race of people. I have so much respect for them.
- fancyfredbot 6y agoReminds me a little of Wirth's Project Oberon
- badsectoracula 6y agoIndeed, while i was reading the blog post i had in my mind Wirth's Project Oberon which is also about implementing a custom CPU on FPGA and then a complete OS with its own compiler, tools, GUI, etc on it.
- duckfruit 6y agoJaw-droppingly impressive to have gone from nothing to a custom ISA, C compiler toolchain and a whole freaking OS with userland apps in six months! My favorite detail was that having spent all this time on extra credit, they only managed to complete their primary task an hour before the presentation - an unmistakable mark of a true hacker if there ever was one.
- unnouinceput 6y agoThey completed the primary task way before that. This was free time and they did it for fun. The fact that they re-implemented their initial task as a program in the OS itself was the cherry on top, to impress their teachers (and also to learn a heap load of stuff in the process).
- amelius 6y ago> to impress their teachers If I was that teacher, I would have flunked them for "doing something just because you can" and not using their capabilities for a more useful goal. And also because I'm tired of yet another toy CPU and toy OS that's just like all the others.
- saagarjha 6y agoI suspect you’re failing to understand the goal of being a teacher, which is to nurture your students. Toy operating systems are a time-tested way to learn.
- amelius 6y agoNo, I would have taught them the valuable lesson of not trying to invent the wheel. These students are obviously smarter than the average student, and therefore I would expect more of them, like inventing new OS concepts instead of mimicking age-old designs.
- deleted 6y ago[deleted]
- codegladiator 6y agonew doesn't come before knowing what exists and why, and to understand the latter making it is important. as a teacher if you fail to understand you should probably spend some time to read more why people study what exists in school and college for the better part of their life.
- Kuinox 6y agoYeah, a certain Linus should not have tried to invent the wheel.
- NobodyNada 6y agoWhen I was 12 or so, I was fascinated by a video [0] visualizing the memory architecture of the Nintendo Entertainment System. I started researching the technical details of the console in order to try to make sense of what I was watching, and within a few hours I had decided to try to write a NES emulator. Now, the NES emulation scene is extremely mature, and so I quickly came across a number of warnings essentially saying "the world has too may NES emulators, please don't write another one." I decided to go ahead anyway, and after spending about 2 years' worth of afternoons and weekends, the result was just another bad emulator. It was slow, buggy, incomplete, and the code was a mess. By your standards, I wasted 2 years of my life making "yet another toy" emulator instead of "using [my] capabilities for a more useful goal." But I had a ton of fun, and the learning experience was absolutely invaluable. I learned in depth how a computer actually works at the hardware level (even though the NES is much simpler than a modern computer, the concepts and skills transferred over very quickly). As a result, my practical programming knowledge today is vastly improved over where it would have been had I not "wasted" all those afternoons. Many of the career opportunities available to me so far have been directly attributable to the skills I gained working on that emulator. [0]: https://www.youtube.com/watch?v=xI3xZAn7r2A https://www.youtube.com/watch?v=xI3xZAn7r2A
- saagarjha 6y agoVery cool. I once taught a class similar in spirit to this, although it was aimed at first-year undergraduates with only a basic knowledge of programming, squashed into a single quarter, and designed on the fly, so we only got partway through implementing a C compiler. Instead of writing a full C compiler ourselves, we considered "cross-compiling" one by taking one that compiled to bytecode, and writing an emulator for that in assembly, taking that to be our first "compiler" and then progressively bootstrapping from there to a better compiler until it became feasible to start writing (or porting) a C-based OS.
- wrs 6y agoI’ve always wanted to do this. I helped my housemates implement a CPU with custom ISA in college (this was before FPGAs, so there were a lot of chips in that CPU) but that’s as close as I’ve gotten. I kind of feel like you can do something like this in college, or after you retire, but nowhere in between. :)
- ellis0n 6y agoWhy not? I made my first Phone OS (RTOS) on 8051 and 256KB ROM in college 20 years ago. Then I worked on several projects and my skills improved. Then I designed a processor architecture, made C-like programming language, created a compiler, an IDE, created a mobile OS and want to build a hardware chip in my startup https://animationcpu.com/ https://animationcpu.com/
- wrs 6y agoThat’s awesome! To be clear, I’ve been able to pick (or create) some fantastic “day jobs” where I got to do lots of things I wanted to do. For some reason it’s harder for me to focus on a big long-term “outside interest” project like this. But you’re exactly right...why the heck not?
- ChuckMcM 6y agoThis was an excellent read. Congrats to the team on getting their port of Xv6 up and running! I had the opportunity to help with a project to port UNIX to a data general NOVA computer as an undergrad and the combination of "non toy" problems and results you can see is really fun. We didn't have to write the compiler though! That is an amazing part of this story too. Given the availability of RISC-V descriptions and FPGA boards that will host it with memory, this sort of project should be within reach of anybody willing to invest about $150 in parts and a lot of labor. It would make for an interesting basis for a demo contest!
- Teknoman117 6y agoThat's a current project for me right now. When I took computer architecture in university, it was fairly light on hard details about things like superscalar and out of order designs. Sure there's the concepts, but there are so many interesting problems it didn't cover. E.g. how do you, at a hardware level, actually do the reservation stations for a out of order design. How do you actually implement a register file with enough read and write ports to satisfy such a design without taking up a whole die for it? I know there are a few Linux capable soft-core RISC-V designs out there (VexRisc, etc.) and microcontroller class ones (PicoRV32, etc.). If my goal was implement a system and it needed one of those things, sure, I'd use an off the shelf one. But I really want to understand how the CPUs work, and the best way to do that is doing it myself without looking at the answer key. Turns out register files are complicated and fascinating. I'd never come across "register file banking" in my architecture courses. Makes what I had to deal with in CUDA make a lot more sense now.
- ChickeNES 6y agoAnother detail I never see covered is the implementation of variable length instruction decoders. Most every book seems to assume a classic RISC design with 32-bit wide instructions, single-issue and non-microcoded. Are there any advanced undergraduate or graduate level books that cover details like this?
- a1369209993 6y ago
- nkozyra 6y agoVery interesting article. An ambitious project and I'm certain a great learning experience I see "cofee" written a bunch across the site is that some alternate spelling or just a typo?
- peterburkimsher 6y agoI did a course like this while on exchange programme to UCSB! In the ECE 152B Digital Design Methodologies class, teams of 2 people had to design and build a CPU from scratch. We had to choose all our own chips and wire them together by hand. We had an FPGA for the Control Unit, but had to design the bus, pipelining, stack, carry-borrow, all the way up to running a demonstration program. We did that in teams of 2 people, with only 1 semester. I was broke, so we chose the cheapest chips (I built MUXes out of NANDs). I realised we couldn't finish on time. I wandered the hallways, saw a project poster from a previous year, took some photos, reverse-engineered and reimplemented their design. We didn't have the FPGA code, but knowing the breadboard layout helped enormously. A few more nights in the lab, working to exhaustion and sleeping under the desk, and we passed the class. If you think industrial espionage only happens in other countries, that taught me that it happens in the US too. Arguably we made enough changes that it wasn't total plagiarism, but it did help to have a known-working design to build on. The most lasting memory of that project, though, was when it worked fine during debugging, but failed whenever we removed the test probes. We were using 2 breadboards, but no common GND. During programming, they were grounded together through the GND on the PC's USB port. Always check for a common ground!
- grishka 6y agoTo be honest, I'm kinda jealous. I do have a general understanding of how a CPU works, and I do understand what preemptive multitasking operating systems do, but my understanding is mostly abstract. And, fwiw, I studied most of this by myself out of sheer curiosity. I did graduate a university but the closest thing we had was using a custom CPU emulator, given by the teacher, to run programs, given by teacher, and tracing every step it takes. It was so tedious and boring (the thing ONLY accepted binary, too, hexadecimal would be too convenient I guess), I wrote my own emulator that took an assembly file as input and generated the paper as the output, you only had to add your name to it before printing and turning it in. It then took me 5 whole minutes to generate semester worth of those. Of course I got a passing grade. Still, no substantial understanding of CPU inner workings was gained during the process.
- m463 6y agoI was reading a book on early computing and I believe folks working on some of the original computers at MIT (?) would add their own instructions. They would just wire them in.
- mwcampbell 6y agoSteven Levy's book Hackers talks about this in chapter 5, "The Midnight Computer Wiring Society."
- vegetablepotpie 6y ago> Reinventing the wheel is generally said to be something to be avoided, but there’s quite a bit to learn from actually doing it. It made me realize that I didn’t understand it as well as I could implement it from scratch. I wrote a bitmap image library in C a few months ago and when I realized that I was in too deep I just kept going because I can never abandon a project I start. I felt bad when I came out the other end because of all the time I spent on it that I could have spent doing other things and because I could have just taken a library off the shelf. In doing the exercise I did realize that what appears to be a simple and straight forward image format, at first glance, is actually ambiguously defined and full of edge cases. There was no way for me to know that when I started.
- kovek 6y agoI like to call these discoveries as unknown unknowns. It’s when not only the answer/fact is not known, but even the existence of the question/fact is unknown. In this case, not only the fact that the format is ambiguous (the answer), but also the fact that there exists something interesting/unusual about the format (the question). For example, you could have learned the answer if you could have searched “is there something unusual about the format?”, or even “is the format actually ambiguous?”, but those ideas were not present! Active mentors can help with this a lot.
- ajsnigrutin 6y ago...but, you learned something new, and hopefully had at least some fun...
- swiley 6y agoI think a better way to phrase it is that reinventing the wheel has very little business value. But IMO you can learn a lot reinventing things badly and correct software has little business value anyway.
- a1369209993 6y agoAs the saying goes: we reinvent the wheel not because we need more wheels, but because we need more inventors.
- fomine3 6y agoThe author also did another project named Noah that's like WSL on macOS. https://news.ycombinator.com/item?id=18765184 https://news.ycombinator.com/item?id=18765184
- known 6y agoMade my day :)
- plastman 6y agoWOW. that's all I have got to say. I wish my country's universities had such educational quality, help guiding students properly and would encourage students to work on such projects.
- poxwole 6y agoWow!!!
- unionpivo 6y agoWhat would be the best FPGA entry point today, for hobbyist? I would prefer if tool chain works on linux ?
- orbifold 6y agoAll the major toolchains work on Linux. I personally liked Pynq http://www.pynq.io/ http://www.pynq.io/, it works fairly well and is beefy enough to do non-toy projects with it. For something totally barebones you can try https://www.latticesemi.com/Products/DevelopmentBoardsAndKits/iCE40HX8KBreakoutBoard.aspx https://www.latticesemi.com/Products/DevelopmentBoardsAndKit..., it has the advantage that it has a very nice open source tool flow available for it: http://www.clifford.at/icestorm/ http://www.clifford.at/icestorm/. I own both boards and I work professionally with EDA tools, honestly the biggest cliff is setting up tooling and processes around a given project. If you don't want to do everything from scratch you need to either rely on proprietary libraries, which will cost you a fortune and/or lock you into a very specific tool flow (Synopsis Designware libraries). The other alternative is to use one of the open source ecosystems like https://github.com/m-labs/migen https://github.com/m-labs/migen or tooling around Chisel or various scattered libraries of open-source components which are often hard to get to work together, but also this requires a massive amount of time investment to just get the synthesis / simulation flow working. The situation is slowly improving and there are various university research groups and corporations that have released larger amounts of open source code that one can use as a starting point: - https://github.com/openhwgroup/cva6 https://github.com/openhwgroup/cva6 (mostly done by ETHZ people initially, also see https://github.com/pulp-platform/pulpino https://github.com/pulp-platform/pulpino) - https://github.com/chipsalliance/Cores-SweRV-EL2 https://github.com/chipsalliance/Cores-SweRV-EL2 (one of several high quality open-source RISC-V cores by Western Digital) - https://github.com/chipsalliance/rocket-chip https://github.com/chipsalliance/rocket-chip (UCB by initial creators of RISC-V) - https://github.com/electronicvisions/nux https://github.com/electronicvisions/nux (POWER based embedded processor, that we have used in several neuromorphic chip tapeouts (silicon proven)) On the tooling side one great thing that happened is that the verilator open source simulator has gained a lot of traction. - https://github.com/verilator/verilator https://github.com/verilator/verilator On the hardware synthesis side some exciting developments are also happening, mostly driven by the desire of companies like Google to be vendor independent for their Deep learning accelerator designs and through funding by DARPA.
- varispeed 6y agoThese days there is a low barrier of entry to create "own" CPU. Just get your favourite FPGA ecosystem, configure ready made core and off you go. Route manufacturer's reference design in Eagle or Kicad add your peripherals then send the design to JLCPCB or other fab and job done. There is plenty of good soldering tutorials on YT.
- notdan 6y agoIf you want to do the lite version of this there is a book called “From NAND to Tetris” that is an instructional book for a similar class and walks through doing much of the same thing. It helped me connect the dots between “I know how a transistor works” and “I know how assembly language works” that I have never learned otherwise. Even if you just skim the book without doing the project you will likely learn some things. The Elements of Computing Systems: Building a Modern Computer from First Principles https://www.amazon.com/dp/0262640686/ref=cm_sw_r_cp_api_i_yH-EFb40311E0 https://www.amazon.com/dp/0262640686/ref=cm_sw_r_cp_api_i_yH...
- amcvitty 6y agoI’m doing this right now and it’s a lot of fun and very accessible. I saw there’s a highly rated Coursera version too (I prefer books to videos so I haven’t looked at it)
- shyam_joshi 6y ago> Reinventing the wheel is generally said to be something to be avoided, but there’s quite a bit to learn from actually doing it. It made me realize that I didn’t understand it as well as I could implement it from scratch. The paradox of any performance that seems effortless, is that tons of effort invariably went into producing that effect. Good going mate!