How hard is it to write an emulator?

Struggling with Fuse or trying to find an emulator with a specific feature. Ask your questions here.
Post Reply
User avatar
Jbizzel
Dynamite Dan
Posts: 1537
Joined: Mon May 04, 2020 4:34 pm
Location: Hull
Contact:

How hard is it to write an emulator?

Post by Jbizzel »

...and how many hours of work is it for a skilled coder?

Like you're not an architect unless you designed your own house... Writing an emulator seems like a right of passage for a certain type of coder, I guess.

How much work does it actually involve? Specifically I'm wondering about the esp32 type stuff. As more 8 bit emulators seem to pop up all the time.

Which is super cool.

Hypothetically, how time consuming is it?

For arguments sake, someone has already written an emulator and made the source code available. It's written for windows or arm in Java. That would be a good starting point for your own work. But if targeting esp32, then that would be Arduino/c type language

But is it like 5 years of your life? Or a couple of weekends

It's a stupid question from someone clueless. Maybe there is a forum somewhere for all the guys that make emulators, but ive never seen it talked about.
User avatar
Guesser
Manic Miner
Posts: 641
Joined: Wed Nov 15, 2017 2:35 pm
Contact:

Re: How hard is it to write an emulator?

Post by Guesser »

how long for what, running enough code to get to the ROM © message, or being finished? Because the latter is "forever" based on the emulator authors I know :lol:

There's a very active emulation channel on the ZX Spectrum discord, where a handful of folk split hairs over irrelevant details of spectrum behaviour that don't affect real software all day! ;)
User avatar
Jbizzel
Dynamite Dan
Posts: 1537
Joined: Mon May 04, 2020 4:34 pm
Location: Hull
Contact:

Re: How hard is it to write an emulator?

Post by Jbizzel »

Ah, yes. I meant to say minimal viable product!!
User avatar
Mpk
Dynamite Dan
Posts: 1008
Joined: Tue Feb 09, 2021 8:10 am

Re: How hard is it to write an emulator?

Post by Mpk »

Obviously judging the CGC wasn't punishment enough...
User avatar
Jbizzel
Dynamite Dan
Posts: 1537
Joined: Mon May 04, 2020 4:34 pm
Location: Hull
Contact:

Re: How hard is it to write an emulator?

Post by Jbizzel »

Mpk wrote: Wed Oct 25, 2023 9:44 pm Obviously judging the CGC wasn't punishment enough...
I guess that the answer 😂😂

Maybe it's a case of: if you have to ask, you're not up to it
Ralf
Rick Dangerous
Posts: 2289
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: How hard is it to write an emulator?

Post by Ralf »

I don't have personal experience with emulator making but will share some thoughts anyway ;)

I suppose there is a long way from your first, rough and buggy but working emulation to correct and precise emulation.
Your first attempt could look this way:

- ignore the sound, make it silent

- ignore loading from the tape, just reading some basic snapshot

- ignore the way that Zx Spectrum creates display and just dump screen data from memory
in one step, independently of executing Z80 code. All multicolor effects will be lost but about 90%
of games will somehow work


Making your first rough version could take a few weeks indeed, while making a polished thing with lots
of features could take years, assuming you have your life and you write code in evenings in your free time.

Your amount of work also depends on using existing so called Z80 core or not.
There are some ready solutions like this: https://github.com/Konamiman/Z80dotNet

If you use a code like this, you can skip a lot of tedious work and focus on Spectrum specific things.

And one more thing - in the past you had to make optimisations and clever tricks so your emulator ran
at decent speed. Today it mostly doesn't matter. You may write very straightforward, not optimised code
and it will be fast enough.
User avatar
Bedazzle
Manic Miner
Posts: 305
Joined: Sun Mar 24, 2019 9:03 am

Re: How hard is it to write an emulator?

Post by Bedazzle »

Double that!

And add more: 128K is harder to emulate; more memory, fiddling with pages, AY.
And of course if programmer go for additional hardware (for example, disk system), it will be even more.
User avatar
SkoolKid
Manic Miner
Posts: 407
Joined: Wed Nov 15, 2017 3:07 pm

Re: How hard is it to write an emulator?

Post by SkoolKid »

This is not the same as writing a full-blown emulator, but for SkoolKit I coded a simulator for:
  • The Z80 instruction set.
  • The ZX Spectrum memory.
  • The ports relevant to loading tapes and saving game snapshots.
I did this in Python, and got the instruction set and memory simulator done in 9 days. (If I'd done it in C, it probably would have taken 9 years.)

Here's my ROM © message moment.

Since then it's just been a matter of stuff like making Skool Daze load in less than 15 minutes and other optimisations and fixes and whatnot.
SkoolKit - disassemble a game today
Pyskool - a remake of Skool Daze and Back to Skool
TheMartian
Microbot
Posts: 100
Joined: Wed Feb 03, 2021 5:18 am

Re: How hard is it to write an emulator?

Post by TheMartian »

Hi, emulator author here.

For me it took 4 months of relaxed work (evenings, weekends) to write a working Spectrum 48K which I think had sound, keyboard and TAP/TZX support in C++ using CMake for the builds, SFML for graphics/audio/input, and Boost::unit_test_framework for tests.

In these four months I wrote the entire Z80 (state machine, instructions) from scratch, then the ULA with the help of the ULA book and Chris Smith's blog, sound support using the beeper, keyboard tables, etc. The first game I loaded was The Trap Door, using the original TZX file, because I had already register R emulation.

Take another month or two maybe to have floating bus support and run Arkanoid and Sidewize. The 128K/+2 emulation was done I think in another couple months (PSG, memory banks, timings). It was nice being able to run La Abadía del Crimen.

Then a couple more months for the +2A/+3 models (roughly). (New memory banks, FDC, disk formats)

From that point the development was slower, because I started spending time fixing things and improving the accuracy of the emulation, rather than adding new stuff.

At some point, I don't remember when, I added Pentagon timings. Also I didn't add ROM traps for tape routines until almost a year into the development. I wanted to see if my emulator could run the Ultraviolet demo.

Using previous components and having read and understood docs and schematics, adding support for Amstrad CPC took 2 weeks. (CRTC, Gate Array, PIO, bank switching...)

The times I am mentioning are what I remember. My memory is very bad, so maybe if you want to check the commit timeline, the project is in here:

https://github.com/MartianGirl/SpecIde

At this moment, almost seven years since I began, I'm still adding stuff (very slowly these days, tough times) but all the ZX Spectrum official models are supported and it's accurate enough to play any game and run any demo. The Amstrad CPC models are still WIP, but are mostly okay for games.

My emulator runs in Windows/Linux or MacOS, so PCs. It also runs on a RPi 4B, but it hogs the CPU. For smaller ARM chips I guess it can be done in about 6 months, but I guess every programmer is different, so I don't know. I've got a STM32 board, so maybe some day I'll give it a try (don't hold your breath, though) and post my figures then.

Edit: Actually, I've been slower. The commit history shows the times. Got the Speccy in 6 months, the 128K in another 6 months, then the +2A.
Matt_B
Manic Miner
Posts: 659
Joined: Sun Nov 01, 2020 8:47 am

Re: How hard is it to write an emulator?

Post by Matt_B »

Jbizzel wrote: Wed Oct 25, 2023 9:35 pm Ah, yes. I meant to say minimal viable product!!
If you can play Manic Miner end to end on it, you've got an emulator. :D
User avatar
Eremus
Drutt
Posts: 17
Joined: Sun Feb 05, 2023 12:58 pm
Contact:

Re: How hard is it to write an emulator?

Post by Eremus »

Hi!

It's difficult to say I think. I can only tell you about my experience:

I started to work on ESPectrum about one and a half years ago. I never had written a line of code for Arduino, ESP32 or similar platforms and the last time I started a C IDE was about ten or more years ago. Now, ESPectrum is a very complete and precise emulator and there are lots of people enjoying it. How do I did this? Easy: with the help of and learning from people who did this before me :)

To be fair, I didn't started from scratch. I took David Crespo's code (https://github.com/dcrespo3d/ZX-ESPectrum-Wiimote), learn how it was done and started solving bugs and making little improvements to its code.

Later, when Arduino framework didn't was enough, I moved the project to another framework (ESP-IDF) and started the actual ESPectrum project.

In the following months, while I learned more and more about Speccy and from other authors (and discovered the ZX Spectrum discord channel) the emulator improved slowly at first but faster as I knew more and more.

My advice is to take a nice running emulator with source code available, start learning from the code and, at the same time, read all you can about the platform you want to emulate.

Our emulator is coded for ESP32 and you have the full sources (GPL-3.0) of our project at https://github.com/EremusOne/ZX-ESPectrum-IDF

You have also SoftSpectrum48 from Magnus Krook, a very good emulator with full source at https://softspectrum48.weebly.com/

If you have time, you can code things like the Z80 core or AY emulation. It's interesting and you'll learn a lot doing it but you can also use existing cores or libraries. While you manage to integrate in your code, you'll be learning also.

Bear in mind, also, the platform and it's power could influence the develop time. I've wasted (and still do) a lot of time learning about ESP32 platform and optimizing things for the emulator be able to run at full speed in any situation.
catmeows
Manic Miner
Posts: 718
Joined: Tue May 28, 2019 12:02 pm
Location: Prague

Re: How hard is it to write an emulator?

Post by catmeows »

Well, it really depends how much accurate emulator should be and what should be supported. More general emulator is more work. I have experience with cycle perfect 6809 core written in Java, it took about two weeks of evening work. But I think cpu core is not the hardest thing, the real problem is making the emulator core - here I mean cpu, bus, support chips - to be fully expandable.
In real world, if you take a computer, connect its system bus to a well designed hardware, it should work. Making your emulator so general that you can easily plug in dozen(s) devices is quite hard. One of the problems with peripherals is that you are leaving world of 'ones' and 'zeroes' and entering into world of rising / falling edges and in the end you are going to implement awfuly lot of state machines.
The computer I write emulator for is using single 6821 to communicate with world and the keyboard is realized through two additional chips - line decoder 74LS131 and shift register CD4021. I'm not going to completely emulate neither 6021 nor 74LS131 nor CD4021 but only functions that are given by the way chips are connected to each other (i.e. some pins are connected to ground or not connected at all). It saves me lot of work but there is also risk I will overlook some edge case. And probably every emulator author is going to make similar decisions, lot of them.
Similar situation is with supported files. It is easier to simply support snapshots and possibly a simplified tape image like TAP than support of exact image of tape (TZX, PZX) or audio file.
It is not like each single thing is particulary hard but for each thing you need to decide how much you want to be accurate and accuracy needs more work and also more cpu power on host machine. Meanwhile your code base grows and any simplification in design you thought was adequate when you started to write your emulator can backfire some months later when you want to add support for some exotic hardware configuration.

IMHO, writing emulator of naked Spectrum 48K is quite easy. Writing something like FUSE or SPIN or other matured emulators is quite hard and will consume lot of work.
Proud owner of Didaktik M
TheMartian
Microbot
Posts: 100
Joined: Wed Feb 03, 2021 5:18 am

Re: How hard is it to write an emulator?

Post by TheMartian »

catmeows wrote: Thu Oct 26, 2023 8:27 am Meanwhile your code base grows and any simplification in design you thought was adequate when you started to write your emulator can backfire some months later when you want to add support for some exotic hardware configuration.
This. The shock when I found I needed both edges of the Z80 to calculate contention correctly. (Because I'm calculating contention, which is slow, and not relying on the known contention pattern for the Speccy, which is faster, because I didn't write an emulator but documentation.)

Thank heavens for unit tests.
User avatar
Lethargeek
Manic Miner
Posts: 743
Joined: Wed Dec 11, 2019 6:47 am

Re: How hard is it to write an emulator?

Post by Lethargeek »

Ralf wrote: Wed Oct 25, 2023 10:15 pm I suppose there is a long way from your first, rough and buggy but working emulation to correct and precise emulation.
Your first attempt could look this way:

- ignore the sound, make it silent

- ignore loading from the tape, just reading some basic snapshot

- ignore the way that Zx Spectrum creates display and just dump screen data from memory
in one step, independently of executing Z80 code. All multicolor effects will be lost but about 90%
of games will somehow work


Making your first rough version could take a few weeks indeed,
for just this - one evening (if you take a ready working z80 emu core form the github and have basic experience with some gfx lib like SDL/SFML/etc)

...then you start fixing bugs and adding more bugs :mrgreen:
Timmy
Manic Miner
Posts: 230
Joined: Sat Apr 23, 2022 7:13 pm
Location: The Netherlands

Re: How hard is it to write an emulator?

Post by Timmy »

Jbizzel wrote: Wed Oct 25, 2023 9:13 pm ...and how many hours of work is it for a skilled coder?
I haven't made one before, but I guess it would probably take me many weekends. It really depends on where you start from (i.e. what parts are you not writing and use existing code from other people before you), and how accurate you want it to be.

The hard part for me is mostly finding the time to do it, because in that same time I could also make games instead.
But is it like 5 years of your life? Or a couple of weekends
Probably both. :)

But I guess the answer is different for everyone.

And if you really want to look for the most correct answer, then I suggest you to START WRITING NOW, and let us know when you finish it. :dance
User avatar
Seven.FFF
Manic Miner
Posts: 744
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: How hard is it to write an emulator?

Post by Seven.FFF »

A lot of people end up writing two emulators. One that loads and plays manic miner, and is very quick and enjoyable to write because the Spectrum is super easy to emulate.

Then they start trying to make game X work, and game Y work, and find they need to emulate lots of technical machine-specific internals. And that gets harder and harder to shoe into the simple version, so they start again with a better design. And it becomes a long term labour which is never done, because people still keep discoving subtle new hardware behavioural quirks, even in 2023!
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
Timmy
Manic Miner
Posts: 230
Joined: Sat Apr 23, 2022 7:13 pm
Location: The Netherlands

Re: How hard is it to write an emulator?

Post by Timmy »

Seven.FFF wrote: Thu Oct 26, 2023 5:39 pm Then they start trying to make game X work, and game Y work, and find they need to emulate lots of technical machine-specific internals. And that gets harder and harder to shoe into the simple version, so they start again with a better design. And it becomes a long term labour which is never done, because people still keep discoving subtle new hardware behavioural quirks, even in 2023!
Agree, and this also causes a dilemma when I decided to make games. Because of so many "first emulators" and others that are close to each other but not complete, it's always a balance to write something that only works for the advanced emulators or something that works fine for most emulators.

Do you want multicolour, border effects, perfect sprite handling, multiple keypresses, BCD, 128k support and other gotchas? 8-)

Still, the Spectrum is probably one of the easiest to emulate, because it doesn't have that many extra components.
Dr beep
Manic Miner
Posts: 381
Joined: Mon Oct 01, 2018 8:53 pm

Re: How hard is it to write an emulator?

Post by Dr beep »

Matt_B wrote: Thu Oct 26, 2023 12:19 am If you can play Manic Miner end to end on it, you've got an emulator. :D
The only reason I coded my zx81 emulator on the zx spectrum in the 90s was to play 3D Monster Maze.
Patrik Rak
Microbot
Posts: 117
Joined: Mon Apr 13, 2020 3:07 pm

Re: How hard is it to write an emulator?

Post by Patrik Rak »

Jbizzel wrote: Wed Oct 25, 2023 9:13 pm ...and how many hours of work is it for a skilled coder?
I had mine running on the first weekend. First properly usable public release in few months. Then after 15 years it's pretty complete. YMMV, though. :D

Patrik
User avatar
Woodster
Drutt
Posts: 48
Joined: Mon Nov 13, 2017 12:17 pm

Re: How hard is it to write an emulator?

Post by Woodster »

Jbizzel wrote: Wed Oct 25, 2023 9:13 pm But is it like 5 years of your life? Or a couple of weekends
Started my emulator over 20 years ago. Found and fixed a nasty bug saving to TAP files last night...
stevojohn
Drutt
Posts: 2
Joined: Tue Oct 31, 2023 2:44 pm

Re: How hard is it to write an emulator?

Post by stevojohn »

Here's the commit history for mine: https://github.com/stevehjohn/Zen/commi ... decc58+909

If I recall, it was probably a couple of months spare time work to get something running.

The rest was bells and whistle (sound and proper display emulation), then tinkering and adding features.
chernandezba
Microbot
Posts: 168
Joined: Tue Nov 28, 2017 7:39 am

Re: How hard is it to write an emulator?

Post by chernandezba »

Hi there

It took me two weeks (spending 3 or 4 hours every day) to get and initial "Sinclair research ltd" copyright message and most of the ROM code working on ZEsarUX, using a text-only driver and no sound
But I already knew how to create an emulator because it was my second emulator ;)
Post Reply