6 ms·
Write Your Own Virtual Machine (2022)
- saithound 2y agoAlas, educational architectures like the Brookshear Machine and Little Computer look nothing like real ones, making them worse than useless: in my experience students who take courses using these often end up with a more distorted understanding of computers than those who take no classes at all. Most people who want to learn a bit about how their machines work would be better served by taking an operating systems course. Same goes here: if you only have time for a short tutorial, I recommend "Writing my own bootloader" instead. [1] (This is not meant to say that the Write your own VM tutorial is a bad tutorial; only that, in my experience, most people who'd do it would be best served by a different subject) [1] https://dev.to/frosnerd/writing-my-own-boot-loader-3mld https://dev.to/frosnerd/writing-my-own-boot-loader-3mld
- markus_zhang 2y agoI just did the LC-3 one and look forward to learning a bit about dynamic recompilation using LC-3 as an (inappropriate) target machine in the current project. Can you please elaborate why it is bad to use LC-3 for studying computer architecture? I do understand it is completely different from real hardware, and too simplified. But from the perspective of writing CPU emulators, is it bad for me?
- saithound 2y agoI answered a similar question by anyfoo downthread, the point about implementing a PIT should explain what I mean, and why I think that emulating a simple real architecture might be more useful while requiring similar effort.
- c0wb0yc0d3r 2y agoLink in case the order changes. https://news.ycombinator.com/item?id=42519788 https://news.ycombinator.com/item?id=42519788
- anyfoo 2y agoAgain, you are coming at this from a software engineering direction, not a computer architecture/engineering one.
- saithound 2y agoSince I have the exact same thought (that you are coming at this from a software engineering direction, not a computer architecture/engineering one), this is unlikely to be further productive :( You say you want students to be able to write emulators/simulators etc. (you list a bunch of software.engineering tasks). I say writing an LC-3 emulator won't give you any transferable skills for writing, say, a Gameboy Emulator (still a 30yo system), because these educational arhitectures are bad and abstract away precisely the things that you have to get right to do that, leaving you with something trivial to implement: a C array for "memory" and a few bitwise operations. Since we're largely looking at the same things and coming to orthogonal conclusions, I suggest we agree to disagree on this one.
- liontwist 2y agoThis article would directly apply to NES or game boy emulation. Yes, there are fewer instructions.
- anyfoo 2y agoWhen studying computer architecture, I think the implementation details of a real architecture, even a simple one, might actually slow you down if your goal is to learn about dynamic recompilation. The commenter you replied to seems to be coming from a software engineering perspective, i.e. learning how to program a computer (which someone interested in computer architecture probably knows about already).
- deleted 2y ago[deleted]
- userbinator 2y agoMy recommendation is to use an old 8-bit architecture like 6502 or Z80. Apparently a lot of CS courses in India still use the 8086/8088 too.
- markus_zhang 2y agoBack in China my first comp arch class used 8051, it was fun. I remember everyone getting a development board and playing with assembly.
- userbinator 2y ago8051-core SoCs seem to be found in tons of cheap Chinese electronics. MP3 players, toys, SD card/USB drive controllers, touchscreen drivers etc.
- kragen 2y agoYeah, the 8051 family is still pretty popular, unlike, for example, the 68HC11/08/12 etc.. I think it's not as popular as the PIC, ARM, AVR, or even MSP430, but it seems to be more popular than STM8, M16C, SH-4, MAXQ, MIPS, or 8086. I'm not sure how to assess the Z80 (8080 variant), Z8, RL78, and RX families, all of which seem to be showing disturbing signs of life. Fortunately the 8048 seems to be dead.
- userbinator 2y agoI think it's not as popular as the PIC, ARM, AVR, or even MSP430 Far more 8051 cores are shipped than all of those combined, excluding ARM.
- kragen 2y agoEven today? That's plausible, but I'd like to know what leads you to believe it. I was mostly looking at Digi-Key stock numbers, which are at best a very imperfect measure.
- 2y ago
- PaulHoule 2y agoI think of Knuth's old MIX which was a decimal machine which might have gotten built in the 1960s but which nobody has built since the 1970s. A system like that can teach you many fundamentals but not the tricks here https://en.wikipedia.org/wiki/Hacker%27s_Delight https://en.wikipedia.org/wiki/Hacker%27s_Delight which mainly depend on conventional numeric representations.
- kragen 2y agoMIX wasn't decimal, but rather abstract over the choice of base, a decision Knuth changed in later volumes of the series, even before switching to MMIX. There have been decimal and hybrid computers built since the 01970s (especially for pocket calculators, but new decimal instructions were still being added to Intel's lineup in the 8086 with AAM and AAD, and I think decimal continued to be a consideration for the AS/400 into the 90s), but MIX is really kind of an 01950s design, with: - word-addressed memory - five bytes per word (plus a sign bit) - one word per instruction - six bits per byte (in binary realizations) - possible decimal - a single accumulator - sign-magnitude - no stack, and - a nonrecursive subroutine call convention relying on self-modifying code. You can find one or another of these characteristics in machines designed since 01960, but the combination looked old-fashioned already when it was introduced.
- anyfoo 2y agoCan you elaborate what you don’t like about this one, LC-3, in particular? I’m not familiar with it, but just had a look at it on Wikipedia. After your comic, I was expecting something weird, but upon a quick glance, it doesn’t seem too jarring. A bit like a mixture of s/360, some x86, and a tiny bit of ARM (or other RISCy architectures). With lots of omissions and some weirdness of course, but the goal seems to be to quickly come to a working implementation. I’m curious what exactly you think makes it “worse than useless” for teaching.
- saithound 2y agoI'm not talking about the instruction set, or teaching basic assembly (probably anything except Malbolge is suitable for that). Let's look at just one thing every programmer has to deal with, memory. On an LC-3, the address space is exactly 64KiB. There is no concept of missing memory, all addresses are assumed to exist, no memory detection is needed or possible, and memory mapped IO uses fixed addresses. There are no memory management capabilities on the LC-3, no MMU, no paging, no segmentation. In turn there are no memory-related exceptions, page faults or protection faults. When an x86 machine boots with 1MB of RAM, the 4GB address space still exists in full, but accessing certain addresses will cause bus timeouts, crashes. One must track and manage available memory. There's a BIOS, and manually probing memory locations may trash its critical structures. There's INT 0x15. I picked memory arbitrarily but you run into the same limitations no matter what you pick. Would a students who was educated on LC-3 know how a computer keeps time? Of course not, there's no PIT, there's no CMOS clock. Would they have thought about caches? Nope. Oh, but wouldn't a student who implements a timer emulation extension for LC-3 learn more about timers than somebody who just learned to use an x86 PIT? Alas, no. There are 20 equally easy and reasonable mathematical ways to implement a timer abstraction. A good 15 of these are physically impossible on real hardware, out of the remaining 5 two would be prohibiitively expensive due to electrical engineering reasons, one has never been implemented in real hardware due to historical accidents, and two are designs that are actually in use. So to write timer emulation that teaches you anything at all about how actual timers work, you'll have to look at and understand a real architecture anyway. That's why educational architectures are so contraproductive. They abstract away exactly the things that make modern computers modern computers. One comes away with fundamentally wrong ideas about what computers do and how they actually work, or could work. It's like learning to drive in GTA: in principle, there could be plenty of skills that transfer to the real thing, but in practice you'll prefer to teach how to drive to the person who didn't play GTA at all.
- hayley-patton 2y agoThe LC-3 has pretty odd addressing modes - in particular, you can do a doubly indirect load through a PC-relative word in the middle. But you still have to generate subtraction from negation, and negation from NOT and ADD ,,#-1. (I suppose NOT d,s = XOR d,s,#-1 would be a better use of the limited instruction encoding space too.)
- _yb2s 2y agoAs a teenager I took an intro CS class at a community college, and the instructor had us design a simple cpu instruction set, and write our own VM and assembler that worked and let me write and run assembly programs. It was shockingly easy, and it was amazing how much is demystified computers for me. I feel like one could learn every level of computing this way- from designing a real cpu for a FPGA, to writing a simple OS and programs that run on it. This stuff is all shockingly simple if you just want it to work and don’t need all of the extra performance and security modern computing needs.
- tralarpa 2y agoThat's what the nand2tetris course does, I think (I only looked at the first lessons)
- spit2wind 2y agoThat sounds like a fun class! It sounds very similar to https://www.nand2tetris.org/ https://www.nand2tetris.org/ or Charles Petzold's book "Code".
- NooneAtAll3 2y agoor https://nandgame.com/ https://nandgame.com/
- dimava 2y agoOr Turing Complete [0] the game [0] https://store.steampowered.com/app/1444480/Turing_Complete/ https://store.steampowered.com/app/1444480/Turing_Complete/
- markus_zhang 2y agoI think once you move from early fantasy CPUs to early CPUs in production such as 80286, the complexity immediately moves up drastically. IIRC it involves at least memory segmentation, protected mode (MMU).
- 2y ago
- ajross 2y agoTo be That Guy: this is an emulator, not a VM. While the term can apply in a descriptive sense, and in the pre-hardware-virtualization past there was some ambiguity, the overwhelmingly common usage of "Virtual Machine" in the modern world refers to an environment making use of hardware virtualization features like VT-x et. al.
- kragen 2y agoI don't think that terminology is "overwhelmingly common", and I'd argue that it isn't even entirely correct. The JVM is widely deployed, the Ethereum VM is called the "EVM", https://www.linuxfoundation.org/hubfs/LF%20Research/The_State_of_eBPF_010824.pdf?hsLang=en https://www.linuxfoundation.org/hubfs/LF%20Research/The_Stat... describes BPF and eBPF repeatedly as "virtual machines", https://webassembly.org/ https://webassembly.org/ begins by saying, "WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine," etc. "Virtual machine" is still the most common term for fictional machines. (Myself, I think I'd prefer "fictive machine", "fictious machine", "imaginary computer", or "fantastic automaton", but I doubt these terms will gain adoption.) You can't always use the term "emulator" instead of "virtual machine", because while you could say wasmtime was an "emulator", you can't correctly say that WebAssembly itself is an "emulator". Rather, WebAssembly is the virtual machine which wasmtime emulates. It's also common to call emulators "virtual machines". (The Wasmtime web site mostly calls wasmtime a "runtime" and WebAssembly an "instruction format", FWIW.) And of course a running instance of an emulator is also a "virtual machine" in a different sense. I think it's also reasonable to use "virtual machine" in the way you are describing, and it has some overlap with this last sense of "virtual machine". Perhaps in your current environment that is the overwhelmingly most common usage, but that is definitely not true elsewhere.
- dataflow 2y agoDisagree with both of you. JVM was always a misnomer, it just stuck because that's just the name they stuck with. Putting "virtual machine" in its name in defiance of the meaning doesn't suddenly make it a virtual machine any more than calling your dog "my son" makes your dog your son. And virtual machines have used software virtialization for the longest time, they aren't always hardware based. Really, a virtual machine is literally what it says on the tin: something that isn't a physical computer (machine), but presents itself as one. You know, with a virtual CPU or a virtual disk or a virtual keyboard etc., whatever a computer would have. If it doesn't present itself as a machine, it's not a virtual machine. Calling JVM a virtual machine just because it interprets some bytecode the same way a machine does is like calling me a virtual doctor just because I read text the same way a doctor does.
- rramadass 2y agoSome books that i was pointed to; 1) Virtual Machines: Versatile Platforms for Systems and Processes by Smith and Nair - Seems to be a comprehensive subject overview book. 2) Virtual Machines by Iain Craig - Seems like a more hands-on book with languages and VMs. 3) Virtual Machine Design and Implementation in C/C++ by Bill Blunden - Seems like an hands-on implementation. If somebody who has read any of the above can add some comments that would be helpful to everybody.
- kragen 2y agoI'm not convinced that the subject is narrow enough to overview in a single book. The considerations involved in writing a Nintendo emulator, a hypervisor using VT-x, a conventional multitasking operating system, an interpreter for a new scripting language, a SQL query optimizer, a regular expression matcher, a security monitor for untrusted player code running on a game server, etc., would seem to have very little overlap. But these are all "virtual machines". There's even a stack-based virtual machine in the terminfo format for specifying character-cell terminal escape sequences! In a deep sense, virtual machines are what make computers computers, as we use the term today. Turing's 01936 paper about the Entscheidungsproblem hinged on virtual machines being able to emulate one another.
- rramadass 2y agoYou are thinking about "Virtual Machines" in a more philosophical/abstract sense. Here the definition is in a much more narrower and conventional sense. Surprisingly i can't find too many books on this subject. The Iain Craig book looks pretty interesting since it seems to link language to the VM, so maybe one can learn both language design and a VM to run it on. Other than the above i can only think of studying "QEMU Internals" for more knowledge - https://airbus-seclab.github.io/qemu_blog/ https://airbus-seclab.github.io/qemu_blog/
- kragen 2y agoI'm mostly talking about the sense of the term used in the title of the article we're commenting on, not a more philosophical sense, though I did draw the connection in my second paragraph. It's hard to get less philosophical than terminfo!
- mystified5016 2y agoAfter watching Ben Eater's breadboard CPU series, I want nothing more than to design and emulate my own CPU. I just wish I could find the time to sit down and design the damn thing.