Every time I return home from a conference, I have a ton of materials to read, things to watch, ideas to try.

Someone suggested a little experiment and so, here I am, writing a uxn emulator in uxn. I think it might be feasible to implement Varvara's Bicycle without making use of any of the more esoteric features of the system, so I can finally get rid of that extra chunck of code from the specs altogether.

Follow

Almost passing all the tests with my uxn emulator, written in uxn itself. It's very bizarre project, it makes me wonder if there were 6502 emulators written in 6502.

· · Web · 4 · 7 · 27

@neauoire Generally speaking, you don't emulate a CPU on a CPU of the same architecture because you could just run the instructions directly. But what makes most 6502 machines what they are are the peripheral ICs put around the 6502, and an emulator would need to emulate those...you know, implementing the NES PPU on C64 by using the C64 graphics hardware instead.

@roadriverrail yup, that's the next step, I'll just do a handful of devices. :)

@neauoire Yeah, the distinctions break down a bit, but "emulate CPU X on CPU X" is kinda how a VM works. You just direct-execute anything that doesn't require emulation and you trap-and-emulate everything that does. :goose_hacker:

@roadriverrail in this case the memory is all virtual, so I can't just run through the host opcodes, but in a normal situation it's what I would do :)

@neauoire Yeah, this is now a fun question in general. Most (all?) 6502 machines have no MMU and no memory protection; a classic VM can't therefore rely on a trap and would have to scan the binary for load/store ops known to interact with addresses reserved for peripheral ICs and "fix up" those to instead use emulation routines.

@roadriverrail @neauoire there's one exception to that: a JIT. there was an HP project, Dynamo - written up in this PDF - that got something like a 30% speedup by just recompiling the CPU's own code into a trace cache at runtime

@roadriverrail @neauoire somewhere you store compiled basic blocks (runs on instructions without branches) for execution (or more thorough optimisation) later. basic blocks can end up quite a bit longer than one might expect, because of things that are variables at compile time, but known at runtime (eg. constant parameters to a given function call), and that's where the gains come from - turns out a lot of conditionals can be collapsed at runtime, where the values are known just before the code depending on them is compiled

@millihertz @neauoire Ohhhkay, I get the trick now. It sounds like this relies on having some understanding of semantics at runtime? I'm trying to imagine how this works for hand-written 6502 machine code.

@patrick Oh wow! the vizualizer is soo good! that's kind of what I'm going for here, they've done an excellent job at it.

@neauoire someone did write an 8080 emulator for the 6502, which is pretty close. it took less than 1KB, which is pretty awesome

ed. PDF of article describing it is here

@millihertz thank you! I wish I could see the 6502 code for this one. :>

@neauoire i read a little later and found it - if i'd done so initially i'd have included it too... but it's in kind of a strange format, and hard to read - partly because it's printed on an old drum printer in all uppercase, and partly because the author wrote it by hand and retrofitted his own mnemonics for publication

https://www.pagetable.com/docs/8080sim/An%208080%20Simulator%20for%20the%206502,%20KIM-1%20Version.pdf

@millihertz AAAH! you're the best. thank you. Somehow I feel like @EdS would be into that

@neauoire ditto! here's Pagetable's coverage of it, wherein the author explains:

I wrote this by hand, pencil and paper assembly... and much of the simulator was written as I rode the bus to and from work… ;)

About the tools used to create this program:

Honestly, I don’t remember for certain… BUT… MORE THAN LIKELY, it is a FAKE – i.e., I probably just text-edited a listing in “assembler listing” format for the purpose of publishing the code and “looking professional”… ;)

@neauoire i'm sure someone has had a go at translating it into a more modern (and more machine readable) format, but i can't find it right now. but there's an Apple ][ port online too, to which the article also links, so it could probably be extracted from that

Sign in to participate in the conversation
Merveilles

Revel in the marvels of the universe. We are a collective of forward-thinking individuals who strive to better ourselves and our surroundings through constant creation. We express ourselves through music, art, games, and writing. We also put great value in play. A warm welcome to any like-minded people who feel these ideals resonate with them.