Live data from Hacker News

Write Your Own Virtual Machine (2022)

jmeiners.com

21–30 of 102 posts

Re: Write Your Own Virtual Machine (2022)

#21

Alas, 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 cou…

My 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.

I wrote a simple 6502 simulator. Just made it a byte code interpreter.

No cycle accurate anything. Simple I/O. Since I/O on the 6502 is all memory accesses, adding console is as simple as echoing whenever a character is stored in a particular memory address. No reason the simulate a VIA chip or UART or anything that low level.

Fun project. You can take it as deep as you like.

Re: Write Your Own Virtual Machine (2022)

#22

Alas, 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 cou…

The 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.)

Re: Write Your Own Virtual Machine (2022)

#23
To 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.

Re: Write Your Own Virtual Machine (2022)

#24

Earlier quoted context omitted.

My 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.

Back in China my first comp arch class used 8051, it was fun. I remember everyone getting a development board and playing with assembly.

8051-core SoCs seem to be found in tons of cheap Chinese electronics. MP3 players, toys, SD card/USB drive controllers, touchscreen drivers etc.

Re: Write Your Own Virtual Machine (2022)

#25

Earlier quoted context omitted.

Back in China my first comp arch class used 8051, it was fun. I remember everyone getting a development board and playing with assembly.

8051-core SoCs seem to be found in tons of cheap Chinese electronics. MP3 players, toys, SD card/USB drive controllers, touchscreen drivers etc.

Yeah, 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.

Re: Write Your Own Virtual Machine (2022)

#26
post #23

To 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.

I 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_Stat... describes BPF and eBPF repeatedly as "virtual machines", 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.

Re: Write Your Own Virtual Machine (2022)

#27
post #15

Alas, 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 cou…

Can 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 t…

I'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.

Re: Write Your Own Virtual Machine (2022)

#28
post #5

As 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 simp…

That sounds like a fun class! It sounds very similar to https://www.nand2tetris.org/ or Charles Petzold's book "Code".

or https://nandgame.com/

Re: Write Your Own Virtual Machine (2022)

#29

Alas, 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 cou…

I 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?

I 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.

Re: Write Your Own Virtual Machine (2022)

#30
post #23

To 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.

> To 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.

I don't think the distinction you're advocating for actually exists. 'Virtual machine' is commonly used for any software that executes machine- or bytecode, irrespective of the reason. This can include virtualisation, but you also commonly see the term used for language runtimes: e.g. Java's JVM, Ruby's YARV (Yet Another Ruby VM).

The one area you don't actually hear the term that often is in emulation, and this is in part because most modern emulators have tended away from emulating entire systems, and towards techniques like dynamic recompilation (dynarec) of emulated software.

Post reply on HN