The story of my ZX Spectrum emulator
How did it start?
I grew up in the 80's and was exposed to the revolution of personal computers. As a person interested in technology everything related to it set me on fire, especially electronics. I was only twelve when I made my first electronic circuit which was an astable multivibrator soldered together without any PCBs, just in the air. One year later the first 8-bit personal computers arrived and ultimately determined the rest of my life.
The computers became available for schools and for homes; however, at that time they still cost a fortune. The famous ones in my region were the Commodore family products: VIC-20, Plus/4 and 64, the Sinclair's series as: ZX81, ZX Spectrum and including but not limited to some other ones like the HT1080Z, the Pro Primo, the Enterprise, the TV Computer and so on. My family couldn't afford to buy a computer but it didn't prevent me from discovering this science using all of the available sources. I was learning the BASIC programming language from articles published in technical news and from books. Later on, my best friend's parents bought a ZX Spectrum so I was able to practice the programming when I visited him. I quickly outgrew the BASIC language and started to learn the Z80 assembly to write faster programs which were needed to generate wireframe 3D animations.
My elementary school also acquired several personal computers. The teachers decided that only those students who had excellent rating in math were able to attend computer courses organised by the institution. Unfortunately, I didn't match the expectations; therefore, the school didn't support my way at that time. As I mentioned before, this didn't prevent me. Moreover, I think it gave me more power to find my own way. As fate would have it, one year later I had the chance to visit one of the computer study groups in the school and prove my skills in programming had been learnt auto didactically so far. I was writing a simple arcade game within an hour using custom designed UDFs on a ZX Spectrum Plus. The math teacher who supervised the study group was absolutely enchanted by my skills and as an acknowledgement and compensation of the situation he requested the director to provide me with one of the school's personal computers based on my decision to be taken home at all the weekends and on holidays. I'll never forget this act because I think he seriously participated in forming my professional career. Thank you very much Mr. Géza Rácz.
I didn't go to a school related to computer studies after the elementary school. In the high school I acquired the electrician and electronic technician qualifications. I also served in the army for 8 years. Nonetheless, I've never given up writing software. Then finally, ten years later, I acquired the system programmer qualification and I've also got the degree of an IT engineer. I've been developing software now for more than 20 years as a professional software developer.
Thesis work
I'll always be thankful to the ZX Spectrum because I know that this technical miracle established my professional skills. I know the history, the circuit diagram and the individual solutions of this hardware, not to mention the ROM program and its details like the back of my hand. That was why I decided in 2000 that I choose the ZX Spectrum 48K emulator for the topic of my thesis work at the college. Actually, the emulator wasn't only a thesis topic, but the expression of my gratitude for this computer in the background.
I named the emulator SpEm. I found the source code in my archive few days ago and decided to make it open source. That was why I wrote this article, as well. The source code can be found here:
https://github.com/fox20io/spem
This program was written at the beginning of my software developer career. 20 years ago I mainly applied structured programming; however, some parts in object oriented programming can also be found there. The original source code was written in C++ using Visual Studio 6.0; however, the project has been updated to be buildable with the recent versions of Visual Studio. The target platform is Windows. The program uses several Microsoft specific resources like MFC, DirectDraw, DirectInput and DirectSound. Some components and functions like the sound and full screen modes aren't working anymore. Some DirectSound version incompatibility and lack of handling multiple monitors can be experienced because the technology has overrun the algorithms were applied decades ago. For instance, multiple monitors weren't usual in those days. It's clear that some refactors are definitely needed.
Features
The emulator supports the following features:
- 48KB of RAM
- Snapshot file format for loading existing ZX Spectrum programs and saving the current state of the machine into this format, as well.
- Resetting
- Built in Z80 disassembler and debugger
- Different zoom levels: 1x1, 2x2, Full screen mode (no longer works properly)
- Turning the screen border on or off
- Interlace or non-interlace video rendering
- Different speed options: real, synchronized to video and full speed
- Video speed test
- Keyboard assistant to help use the Spectrum's tokenized keyboard
- External ROM support
Some of the listed features sound weird nowadays. Let's see the screen border, interlace and sync to video options. The home computers in the 80's had to be connected to a standard television and they provided analogue RF signals to display information on the screen. It was a digital to analogue conversion. These computers weren't able to control how the generated video content is aligned for the analogue screen for different televisions, instead, they provided a smaller frame which could surely fit into the viewable area of the cathode ray tube. The outside area was named border and information wasn't able to be displayed within it. Technically the ZX Spectrum could only set the colour of this area.
Since the fantasy of programmers is endless, they figured out how to use the border for doing more interesting things. If the colour of the border was changed using proper timing the programmers would be able to draw desired effects in the border, as well. Using this approach, for example, a tricolour flag could be displayed on the screen which could occupy the entire surface of the television. This timing had to be well synchronized to the video frequency in order to make the effect perfect. That was the reason I added the interlace/non-interlace and video based sync options to SpEm.
You can find a dozen programs and games for the ZX Spectrum in a Snapshot file format which is also supported by the emulator. It's important to note that my Z80 core implementation supports only the public instruction set of the CPU; however, there are non-public instructions that can be applied and executed by the real Z80 CPU in machine code. Programs that use these instructions can cause the crash of SpEm.
Behold a couple of my favourite games from the 80's running in SpEm:
I also added a disassembler and debugger to the emulator. This feature was needed by me during the development of the CPU core for tracing the execution, watching the content of registers and flags, as well as to investigate problems. I thought that this feature could also be useful for others; therefore, this feature was kept in the final version, as well.
The Sinclair ZX home computer series introduced a fully tokenized keyboard which had to be learnt by the users prior to doing almost anything with the computer. This weird approach could cause unpleasant moments at the first time, but later on, especially for programmers it provided a very fast way for writing basic programs. This tokenized layout was also useful for the engineers to save ROM space; moreover, the programs occupied less space in the RAM in tokenized form instead of storing it as plain text. Remember that these computers used to have only 16KB of ROM and 48KB of RAM to provide their functionalities.
The emulator had to map the ZX Spectrum's tokenized keyboard layout to a standard PC keyboard. This difficult tokenized keyboard is very hard to use with the PC. Due to this fact, I added the Keyboard assistant function which can help the user in typing and finding the appropriate token and its counterpart on the PC keyboard.
Abstract
Nowadays, a ZX Spectrum emulator can be written in high level programming languages like JavaScript and can simply be run in web browsers or in mobile devices thanks to the very high speed CPUs and the enhanced technology. In 2000, resources were scarce. Usually PCs with 466 MHz Celeron processors were usual, so the only way to write an emulator was to use the C and C++ languages. However, later I saw a ZX Spectrum emulator had been written in Java which was the first sign of changes.
In the distance of more than 30 years, I think I was lucky to see and to be part of the revolution of personal computers. At that time, there was no internet, the information was very expensive, hard to access and was shared only in printed form. However, it didn't prevent people, including me, from learning and to get to know this field. For instance, how to manage the lack of resources, how to save more memory and running time. I think computer science is now in our veins.
I've been collecting the icons of this age. I've got a couple of ZX Spectrums and several Commodore 64s. If I had more time I would build my own version of these or assemble and revive my Pro Primo DIY kit from the 80's. I've started with dusting off and launching one of my creatures to the world.
I don't think I've revelead any secrets. There are still a lot of more creative things in my bag I'd like to share.
To be continued.














Comments
Post a Comment