information for ZX Spectrum emulator and tools

Lets talk about The Spectrum Emulator (inc some technical bits!)

Ok where to start, We shall very dangerously assume ( hey why not live life on the edge!) this is a wide ranging audience from those that know nothing about the internals of a computer or Sinclair Spectrum and those that know more, so chances are this is going to be a total mess and keep things quick and basic!... Anyway hope it gives you some idea of how it looks...

The Spectrum as we know was a 80's home micro computer and to emulate it we need to look at the main internal parts (based on the 48k Spectrum) are...

CPU (Central Processing Unit) Z80
This was a Zilog Z80 - the brain that crunches all the binary bits ( the 1s and 0s ) and acts out what is being asked (more often than not - if you're to believe us programmers!), We communicate with this chip through machine code which gives the instructions on what to do.

The timing cycles of the spectrum is known as T states, each instruction for the CPU can take a different amount of T states to complete so in order to emulate this at the correct time / speed we need to know the time for each instruction.

Also every 69888 Tstates an Interrupt can occur 50 times a second like refresh the screen, remembering the analogue TV's working off the mains here in the UK had a refresh rate of 50hz the electron beam would redraw the picture 50 times a second, in order for the spectrum screen to be stable then the same would apply the screen would need to be drawn 50 times a second thus the 50 * 69888 = 3494400 Tstate per second, CPU running at approximately 3.49Mhz.

Although the spectrum seems slow to us now as technology has speeded things up this is still a powerful processor and not much has really changed from this view, Lets just consider a particular instruction took 4 T states, that instruction could be run 17472 times before an interrupt, and 873600 times within 1 second! - pretty quick ? oh and don't forget this all needs to be emulated.

Writing code for our Z80 / Spectrum we access Registers, Flags, and Memory, these are places which show, store and manipulate the data, to be quick lets imagine this...

We use Hexadecimal numbering system through out...
Hex to Decimal think of Hex as a count above the numerical digits instead of counting to 10 we count to 9 then A, B, C, D E, F, then 10(hex) which is 16(decimal).

A spectrum byte can hold 8 bits (8 * 1s and 0s) which represents a number 0 - FF (hex) or 0 - 255 (decimal).

Simple example to help explain how it works together....
bits 128 64 32 16 8 4 2 1
binary
1
1
0
0
1
0
0
1
byte
C9

 

our byte value in this case would be 201 decimal or C9 hex - bits 128, 64, 8, and 1 are on, just add these together.

Now a little simple maths in Assembly Language- lets load the 'A' Register "the accumulator" (this register does the maths ) with our byte value C9

LD A, C9

Now lets add another C9 (remember that's 201 + 201 in decimal )
ADD A, C9

Remember the single byte register can only hold a maximum value of 255 or FF hex, as we have created a number larger than this the carry flag will be set (carry = 1) by the Z80 this will lets us know that a number was carried over in the last ADD operation we performed.

This code will now need to be converted to Machine code by an Assembler for the processor to understand it, once assembled the code is placed in memory as a specified location where we can run it.

ULA (uncommitted logic array) - Memory (inc. Screen)
In Sinclair Spectrum terms think of the ULA as a series of gates which have tasks to perform, Screen, Border, Sound, Keyboard, CPU all wanting space for example when the ULA is drawing to the screen and using the screen memory the CPU could be halted as each device must wait in turn to access the same area- the ULA in this case is given priority as the screen needs to be drawn at the correct rate 50hz and thus a "contended memory" delay is seen as the CPU waits, this again needs to be emulated, and the actual amount of delay in Tstates is dependant on memory address being accessed.

IO ( Input Output Ports )
These are the places where the outside would communicates with the computer, the inputs Keyboard, Joystick, Microphone, Tape audio, the outputs Sound, Printer. Again these address and functions need to be emulated.

File types
In order to load and save games we need to be able to read the file formats,Interpret the contents and set the corresponding memory, registers, flags, etc accordingly, for tzx we need to emulate the pulses/sound waves coming from the the cassette in order for the spectrum emulator to load them as a real spectrum would, FYI: the emulated sound from our tape deck is so accurate you could plug a real spectrum into your sound card and load the game in real time, the same code is being deployed to create a digital cassette deck, coming soon.

Deploying on Microsoft Windows and .net
This is really the tip of the iceberg, there's vastly more than this to a ZX Spectrum including plenty of undocumented parts, things that no official documentation details, but years of expertise from some dedicated guys have been solving it bit by bit ( all puns intended ), once you start to understand the Z80, Spectrum and its parts you need to understand the language and platform on which to write the emulator...

In order to have sound play back the framework we used .net has no low level method of playing sound we then had to drill deeper to the platform windows and call routines that would allow us to play each byte.
To achieve this in real time we have to build our own wave form from the bits set on the speaker of the spectrum (output) and keep it in time with the emulation, Every execute of the processor (each operation) we take a reading of the speaker value (on or off) and how long Tstates it took, we accumulate an average of these results until 79 Tstates have passed, we then add the average byte value to a memory buffer until we have 882 bytes and send it to the windows API to play it every 20 milliseconds / every interrupt .. remember 50hz/s speed ? = 20ms.

Well we need to get the audio stream 44100hz (cd quality) into the same time frame as 50hz so every interrupt we play approximately 882 bytes of the 44100 which happens every 20ms. 882 * 50(times a second) = 44100hz

This is all without mentioning the important part about emulation you need to understand the language your going to use to write the code of the emulator in!

Ok it is a mess but hope that helps show a little of what's involved! and I'm sure you could write this better with time focused on the new projects ( keep dropping back, some stuff you may find interesting! ) this will have to do for now.



Thanks for reading, please if you can help out do so - Enjoy ZXSpectrum4.net Team.


Share this page with your freinds



we need your help!

ZX Spectrum Cassette Database

In order to maximise the database we need to find as many cassettes as possible, from unknown ones to ones known but we are unable to locate.

If you can help us with any old cassettes you may have in the loft you no longer want or know someone that has then please contact us and give them to a good home, If we can't get there in person we can get them collected by courier - please don't throw them!

Don't worry if you have no old cassettes please register your copy of the emulator by doing so helps us obtain further hardware and software for the project, and plus gives you full functionality of the emulator.

Help by donating or registering

Register

A big "THANK YOU!" to everyone the has donated, time, money, knowledge, tapes and hardware we are truly grateful - ZXSpectrum4.net Team.

emulator

ZX Spectrum Emulator for Microsoft Windows, developed under Microsoft .net framework.

It emulates all known ZX Spectrum behaviours, timing and models 16k, 48k, 128k, +2, +3 (including 3 inch floppy disk drive and tape deck emulation), Sound, Border, Basic, Snapshot saving and more... supported file formats include .wav .tzx .sna .z80 .tap .dsk

GAMES!

We've now included a couple of games in different formats to help those non technical get started, for more help please visit the help section

Requirements

Microsoft Windows 8, 7, Vista, XP
( XP requires microsoft .net framework )

NO install required, and NO registry garbage.

Download the latest ZXSpectrum4.zip for free.
Latest Version 1.0.6313 Build: 35929 (37405)

Download
DOWNLOAD - learn more

If automatic setup fails use
manual download above
Download
Setup click and choose 'RUN '