Zeddy emulator for ZX Spectrum

Emulator and emulator development specific topics
User avatar
kokkiklhs
Posts: 103
Joined: Sat Jan 10, 2015 12:09 pm

Zeddy emulator for ZX Spectrum

Post by kokkiklhs »

Have seen a routine that allows loading of Zeddy programs on the Spectrum (haven't tested it, though), but I was wondering how come and after all those years there isn't a proper, usable and modern full-featured ZX81 emulator for the 48k Spectrum.
I am not a programmer, but I suppose that technically this would be rather simple (at least in theory)... :mrgreen:
Practically the only part to be emulated would be the ULA and its "special" functions and also the timing of the circuit, and I think that the 48k of memory SHOULD be more than enough for such a project, including soft-loading of the Zeddy ROM dump on the Speccy's RAM and also allowing a full 16k (perhaps HRG-capable, as well) free for the emulated Zeddy!

What do the specialists say? Is my idea very ambitious? Where are the coders? I have seen programming MIRACLES so far on the Speccy, but nobody seems to have taken seriously the option of fully emulating the good old Zeddy on this beloved machine! I imagine that this would be rather easy (not to say "piece of cake") for an experienced Spectrum coder, who also has good knowledge of the Zeddy architecture! Wouldn't this be fascinating? 8-)
Of course I stand corrected, in case I miss something here (i.e. there already exists a super-Zeddy emulator for the Spectrum that I'm not aware of, or this is simply impossible for special technical reasons)...
User avatar
chernandezba
Posts: 205
Joined: Tue Mar 11, 2014 4:30 pm

Re: Zeddy emulator for ZX Spectrum

Post by chernandezba »

This already exists:

http://www.fruitcake.plus.com/Sinclair/ ... w_ZX81.htm

Also emulators of the ZX80 and Jupiter ace for the speccy
----

ZEsarUX
ZX Second-Emulator And Released for UniX
https://github.com/chernandezba/zesarux
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Zeddy emulator for ZX Spectrum

Post by dr beep »

But that emulator needs either
1) a ZX 128 with paging on ROM
2) some hardware to make it run

My ZX81 emulator from 1997 (so almost 20 years old) runs on a 48K ZX Spectrum and doesn't need additional hardware.
Speed is slow, but the added games are changed to work better.

The emulator was solely programmed to make 3D Monster Maze playable on the ZX Spectrum.
http://www.worldofspectrum.org/infoseek ... id=0015646

File holds conversion program to alter ZX81 speed to ZX Spectrum speed AND
next program is the ZX81 emulator. Added with programs.

Hires emulated is too slow.
Paul Farrow's version can play some hires games, but def. not all.
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Zeddy emulator for ZX Spectrum

Post by dr beep »

There are 2 problems to be solved for a working emulator.

1) the ROM must be codeable
2) the screenlocation must be changed

With an eprom you can alter the ROM to an altered ZX81 ROM to run as a ZX81
and a 128K can swap screen to 49152 so you have a memoryblock fully usable as ZX81

Only thing to do: during intrupt, read keyboard and updat ZX81 screen to ZX Spectrum screen.

Due to matching memory locations you can run the code (not emulate) and have an almost full speed lowres zx81.
This was also possible on the SAM with paging only. So full software emulator was coded in 1996 for the SAM by me.

On a 48K you don't have 32K matching with original addresses.
My emulator runs on address 5E00 as start of ZX81 ROM.
The emulator really emulates all Z80 opcodes. They are determined and do correct emulation
i.e. LD A,(16384) will load from 16384+#5E00

It took me 2,5 years and 3 versions for this emulator.
First emulator ran at 3%
Second on 10%
Third on app. 33%

Considering the rules for emulation not too bad at all.
Speed is won by translating ROM routines with special Opcodes.

I can write an essay about the entire program.
User avatar
kokkiklhs
Posts: 103
Joined: Sat Jan 10, 2015 12:09 pm

Re: Zeddy emulator for ZX Spectrum

Post by kokkiklhs »

Wow, thank you for the answers!!
I already know about the existing emulators, but none of them seems to be a "complete solution", at least WITHOUT the addition of extra hardware and on a plain 48k machine... of course I am not a coder and I can't know all the difficulties that occur during programming, on the quest of max compatibility... :roll:
I was just wondering about the speed issues, since the Z80 is the same on both machines and the Zeddy was/is traditionally much slower than the Spectrum, in most cases.
Dr.Beep, when you find some time, could you please be a little more specific?? 8-)
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Zeddy emulator for ZX Spectrum

Post by dr beep »

Manual about the ZX81 emulator:

Press 1 and 0 for menu.
Possible old version on WoS

Edit: WoS version is final published version.
(Unpublished altered version with some hires emulation never released, nor will be)
Last edited by dr beep on Wed Nov 23, 2016 2:34 pm, edited 1 time in total.
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Zeddy emulator for ZX Spectrum

Post by dr beep »

Hope this will do.

Code: Select all

Emulation on a ZX Spectrum

Let me first state 3 base rules for an emulator:
1) memory must be enough for emulated machine and emulatorcode.
2) screenresolution must be higher or equal to emulated machine
3) CPU-speed must be about 10x the emulated machine

The first 2 rules apply to a ZX Spectrum and a ZX81.
There is more memory and the resolution is in pixels the same.
The third rule does not apply. ZX Spectrum CPU is 3,5Mhz and ZX81 2Mhz.
This rule is needed since emulation needs to do the following steps

Read opcode
Emulate opcode
Update programcounter.
Goto first step

After years of optimizing my emulation routine I have found that the fastest
program to do only determine each opcode on a ZX Spectrum needs 26 Tstates. 
Since opcodes need to be done in 4 to 19 Tstates you now already know 
why the processor needs to be faster.

Mainprogram to determine each opcode
LD A,(DE) ; 7 DE= programcounter, fetch opcode
LD L,A    ; 4 set lowbyte of opcode address
LD H,B    ; 4 fixed value to highbyte table
LD H,(HL) ; 7 get highbyte of opcode address
JP (HL)   ; 4 go to opcode routine

In addition to this you need the time for the emulation and updating the counter.
The table needs specific values to work.
This program is used in other emulators too. 
(i.e. Galaksian-6502-emulator on the SAM-coupe)

At the start it was obvious that the emulator could never reach full speed.
After many optimization in the code app. 33% is reached.
This is done by building extra opcodes in the ROM for non existing opcodes in the ED-range.
When such a code was met the emulator goes to translated ROM routines that run on full speed.
Not the entire ROM but effective parts are translated (ie CLS).
In addition to the speed the screen is not updated on every intrupt.
Also a check on altered lowres screenpositions is done. Only those are updated.
This also tells why hires is not possible. Only the lowres is checked.

The emulator on the ZX Spectrum has 32K memory from #5E00 to #D000
#5E00 ZX81 ROM
#9E00 ZX81 screen and RAM

From #D000 and on the emulator is coded.
The 8K-16K not used on the ZX81 is used for 'translated' ROM 
The ROM itself is also altered when the screendisplay is called (uppermemory jump).
This is discarted. Since a FP-flaw will override address #0000 and it is in RAM now
the menu is the only way to reset correct. Although RST 0 knows of the bug and will
adjust as well.

To get more speed in 3D Monster Maze the program is altered.
A BASIC delay loop is skipped. 
A game like RED ANTS can be played in a faster mode.  


The ZX81 emulator is not the only emulator on the ZX Spectrum.
The Videopac is also emulated on the ZX Spectrum.
http://www.worldofspectrum.org/infoseekid.cgi?id=0015647

Since the videopac has a whole different processor the opcodes had to be
fully emulated on the Z80. The ZX81 can execute the same opcode. It only needs
to check if memory is used. This emulator uses the principle to read the original
code, translate 8048-code to Z80-code and on second entry directly run the Z80 code.
This is possible since most programs on a Videopac max. has 4K ROM of code.
Still not all games can be played due to perfect emulation vs. speed/memoryuse
The games available fill the balance of emulation and speed.
Each ROM has additional data for setting screenupdates. If less is possible it will
do. The translating of the ROM is best shown in BOWLING. You see the first through is slower
than the second trough. Colour and sound is not emulated.

Manual
E-cursor is game reset
DELETE (Shift0) is game delete and load next
When you define the game to be loaded it will show on the screen otherwise it will stay empty.
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Zeddy emulator for ZX Spectrum

Post by dr beep »

kokkiklhs wrote:Wow, thank you for the answers!!
I already know about the existing emulators, but none of them seems to be a "complete solution", at least WITHOUT the addition of extra hardware and on a plain 48k machine... of course I am not a coder and I can't know all the difficulties that occur during programming, on the quest of max compatibility... :roll:
I was just wondering about the speed issues, since the Z80 is the same on both machines and the Zeddy was/is traditionally much slower than the Spectrum, in most cases.
Dr.Beep, when you find some time, could you please be a little more specific?? 8-)
I tried to built an interface with ROM and correct memoryread on a 48K.
It failed, but Paul had the same idea and worked.
Only difference. His interface had faster IC's.
User avatar
kokkiklhs
Posts: 103
Joined: Sat Jan 10, 2015 12:09 pm

Re: Zeddy emulator for ZX Spectrum

Post by kokkiklhs »

Thanks a lot for your time dr.beep explaining all these, the info so far is quite illuminating! :)
I just thought that it would be much simpler for a pure software cycle-exact emulation between the two machines... :roll:
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Zeddy emulator for ZX Spectrum

Post by dr beep »

kokkiklhs wrote:Thanks a lot for your time dr.beep explaining all these, the info so far is quite illuminating! :)
I just thought that it would be much simpler for a pure software cycle-exact emulation between the two machines... :roll:
The interfaces from Paul Farrow do not emulate, but run the original code on original addresses.
Some minor changes have been made in the (EP)ROM to initialize intrupts and intruptcode is set.

Full emulation needs finding out which opcode it is and then emulate it.
Only advantage from a ZX81 is that a lot of opcode can run as they are since flags will be set automatically.
The emulator holds emulator registers in main registers and ZX81-registers in alternate registers.

So emulating LD A,B and LD H,(HL) is done like this

Code: Select all

byte3  INC DE
byte2  INC DE
byte1  INC DE 
       LD A,(DE)
       LD L,A
       LD H,B
       LD H,(HL)
       JP (HL)

LDAB   EX AF,AF' ; get ZX81 AF 
       EXX       ; get ZX81 BC DE HL 
       LD A,B    ; do opcode, 
       EXX       ; get emulator BC DE HL
       EX AF,AF' ; get emulator AF
       JP byte1  ; increase programcounter

LDHIHL LD A,C    ; #5e, fixed
       EXX       ; get ZX81 BC DE HL    
       ADD A,H   
       LD H,A    ; point to correct memory
       LD H,(HL) ; get right content
       EXX       ; get emulator registers
       JP byte1  ; increase programcounter
and so are all opcodes emulated.
Post Reply