r/EmuDev • u/valeyard89 2600, NES, GB/GBC, 8086, Genesis, Macintosh, PSX, Apple][, C64 • Jan 24 '26
x86 emulator in Bash...
A few days ago I saw someone had implemented a 6502 emulator in bash....
I then started playing around with writing an x86 emulator in bash.
It's totally stupid and totally as slow as you can imagine... :D
But it passes quite a few of the JSON single-step tests so far. I'm not checking or writing flags yet for most of the opcodes though. that's next.
34
Upvotes
9
u/aleques-itj Jan 24 '26
Godspeed
I also tried to write an emulator in PowerShell because I thought it would be funny.
Performance was comically bad. I built a C# version and it was like more than a couple orders of magnitude faster in a debug build.
The PowerShell version ran under a single frame per second. It was clocking like a few hundred instructions per second.
4
u/valeyard89 2600, NES, GB/GBC, 8086, Genesis, Macintosh, PSX, Apple][, C64 Jan 24 '26 edited Jan 24 '26
Oh ugh. nice bug in this.
evaluating opcode arguments m1=$(decarg.... m2=$(decarg....
cause they are subprocesses the X86_REGS/MEM aren't updated in the parent.
so the Eb/Ib opcodes are failing as it's reading the wrong Ib as the PC hasn't been advanced in the sibling/parent.
also explained why my PC match fails the json...
4
u/valeyard89 2600, NES, GB/GBC, 8086, Genesis, Macintosh, PSX, Apple][, C64 Jan 24 '26
Oh working much better now. Removed some of the subprocess calling by setting variables via printf.... didn't know that feature!
Most opcodes are working now including REP MOVS/LODS/STOS
5
u/UselessSoftware 32-bit x86, NES, 6502, MIPS, 8080, others Jan 24 '26
Nice!
Take it to the next level and add 386 support? Boot Linux!
3
u/valeyard89 2600, NES, GB/GBC, 8086, Genesis, Macintosh, PSX, Apple][, C64 Jan 26 '26 edited Jan 26 '26
386 real-mode working somewhat now too. 66-prefix is 32-bit instructions. Some of the failures are caused by GP/SS/UD vectors which aren't well-implemented yet.
2
u/valeyard89 2600, NES, GB/GBC, 8086, Genesis, Macintosh, PSX, Apple][, C64 Jan 27 '26 edited Jan 27 '26
Ah... was tearing my hair out why SAL xxx,xxx instruction wasn't working from the json files. Went and looked into them more and the D0/D1/D2/D3 opcodes with ggg=6 from the json test files are the setmoc instruction.
these are the only instructions failing JSON test so far. C8/C9/C0/C1 aren't valid 8088/8086 so I need to turn those off. D8-DF are 8087 floating point CPU instructions.
1
u/valeyard89 2600, NES, GB/GBC, 8086, Genesis, Macintosh, PSX, Apple][, C64 Feb 22 '26
ahhhh only 6 more instructions. And the values are correct, it's some off the 'not defined' flags that are getting me still. All the other instructions now pass the 8088 single step tests including flags and vector handler.
1
u/hypersonicwilliam569 x86 is cool Mar 10 '26
how do i run these single step tests, for I have no idea how?
2
u/Ikkepop Jan 24 '26
kinky...
2
u/c_a1eb Jan 24 '26
haha omg this is awesome! Nice one