SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Emulator and emulator development specific topics
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by dr beep »

kokkiklhs wrote: Fri Jun 02, 2023 6:30 pm The only game I couldn't get to run though (neither with previous versions) is 81-tris and I can't see an obvious reason, seems like a "normal" 16k game to me...
81-tris, like my 1K hires games, has a special defined displaymethod.
It looks like an intrupt controlled game but the method is different from my CALCULUS MAZXIMUS.

We need to ask SALVACAM
User avatar
kokkiklhs
Posts: 103
Joined: Sat Jan 10, 2015 12:09 pm

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by kokkiklhs »

dr beep wrote: Fri Jun 02, 2023 9:47 pm In 2006 or so I had plans to code emulators on multiple machines.
Most wanted would be machines where the memory could be used on right addresses with banks like on the SAM.
By heart the enterprise can do this. With the right expertise from both sides we could make a good emulator.
I have already started such a discussion in the enterprise forum and yesterday I posted about your latest version and about your ZX80 emulator, to see if there's any interest! :mrgreen:
https://enterpriseforever.com/emulators ... ock-ep128/
I'm pretty sure that geco will respond as soon as he sees it! 8-)
dr beep wrote: Just report not working games.
ZX81 Kong by Paul Farrow loads but crashes soon after! I tried with interrupt ON and OFF settings.
No big deal, though, as the also excellent Zonkey Kong runs and plays well! :D
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by dr beep »

I woke up this morning with an idea how to alter the display in a way that is closer to the original hardware.

This might give a solution for the games with their own IX-intrupt display.

This is not a fix in 2 hours, but worth trying.
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by dr beep »

81-tris uses IY in a way that it is lucky that the code runs on the ZX81 at all.

IY is set to #0000 at #6262. The next command is ADD IY,SP

To solve the use of 81-tris would cost me too much speed in the emulation of IY
and the way this is coded is really dirty.

I suspect is compiled C or something like this.
This game will not run on SP-2-ZX81.
butchi
Posts: 38
Joined: Wed Jun 12, 2019 4:12 am

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by butchi »

dr beep wrote: Sat Jun 03, 2023 9:25 am I woke up this morning with an idea how to alter the display in a way that is closer to the original hardware.

This might give a solution for the games with their own IX-intrupt display.

This is not a fix in 2 hours, but worth trying.
Hi,
FYI
Interrupts
The Z80 has three interrupt modes, selected by the instructions IM 0, IM 1 and IM 2.

When an interrupt is due, which is signalled by the ULA taking the level-triggered /INT pin on the Z80 low, nothing happens until the last M-cycle of the instruction currently being executed. At that point, if interrupts are enabled (IFF1 is set) then interrupt processing will begin. For this purpose, HALT is effectively an infinite series of NOPs, and the repeated instructions (LDIR, etc) can be interrupted after each execution. Interrupt processing begins by resetting IFF1 and IFF2; this has two non-obvious consequences:

If a LD A,I or LD A,R (which copy IFF2 to the P/V flag) is interrupted, then the P/V flag is reset, even if interrupts were enabled beforehand.
If interrupts are disabled when a EI instruction is interrupted, then the interrupt will not occur until after the instruction following the EI, as when IFF1 is sampled during the one and only M-cycle of the EI, it will be reset.

On the 48K Spectrum, the ULA holds the /INT pin low for precisely 32 T-states. This pin is sampled during the last M-cycle of every instruction apart from repeated IX and IY prefixes (DD and FD). If the pin goes high again before it is sampled, no interrupt will occur. The /INT pin must be held low for at least 23 T-states, as some IX and IY instructions take 23 T-states. If the interrupt routine starts EI/NOP, this can cause a double interrupt, as the /INT pin will be sampled 19 (for IM 2)+4+4=27 T-states after being first sampled, when it may still be low.

In IM 1, the processor simply executes an RST 38 instruction if an interrupt is requested. This is the mode the Spectrum is initalised to. In this mode, the processor takes 13 T states to reach 0x0038: a 7 T state M1 cycle to acknowledge the interrupt and decrement SP, a 3 T state M2 cycle to write the high byte of PC onto the stack and decrement SP again, and finally a 3 T state M3 cycle to write the low byte onto the stack and to set PC to 0x0038.

The other mode that is commonly used on the Spectrum is IM 2. If an interrupt is requested, the processor first builds a 16-bit address by combining the I register (as the high byte) with whatever the interrupting device places on the data bus. The processor then fetches the 16-bit address at this interrupt table entry, and finally calls the subroutine at that address. Rodnay Zaks in his book 'Programming the Z80' states that only even bytes are allowed as low index byte, but that isn't true. The normal Spectrum contains no hardware to place a byte on the bus, and the bus will therefore always read FF (because the ULA also doesn't read the screen if it generates an interrupt), so the resulting index address is 256*I+255. However, some not-so-neat hardware devices put things on the data bus when they shouldn't, so later programs didn't assume the low index byte was FF. These programs contain a 257 byte table of equal bytes starting at 256*I, and the interrupt routine is placed at an address that is a multiple of 257. A useful, but not so much used trick on the Spectrum, is to make the table contain FF's (or use the ROM for this) and put a byte 18 hex, the opcode for JR, at FFFF. The first byte of the ROM is a DI, F3 hex, so the JR will jump to FFF4, where a long JP to the actual interrupt routine is put. In IM 2, it takes 19 cycles to get to the interrupt routine:

M1: 7 T states: acknowledge interrupt and decrement SP
M2: 3 T states: write high byte and decrement SP
M3: 3 T states: write low byte
M4: 3 T states: read low byte from the interrupt vector
M5: 3 T states: read high byte and jump to interrupt routine

In interrupt mode 0, the processor executes the instruction that the interrupting device places on the data bus. On a standard Spectrum this will be the byte FF, coincidentally the opcode for RST 38. But for the same reasons as above, this is not really reliable. If there is a RST n on the data bus, it takes 12 cycles to get to 'n':

M1: 6 T states: acknowledge interrupt and decrement SP
M2: 3 T states: write high byte and decrement SP
M3: 3 T states: write low byte and jump to 'n'

With a CALL nnnn on the data bus, it takes 19 cycles:

M1: 6 T states: acknowledge interrupt
M2: 3 T states: read low byte of 'nnnn' from data bus
M3: 4 T states: read high byte of 'nnnn' and decrement SP
M4: 3 T states: write high byte of PC to the stack and decrement SP
M5: 3 T states: write low byte of PC and jump to 'nnnn'

When the /NMI pin goes low, an internal flip-flop in the Z80 is set to note that an NMI is pending; This flip-flop is sampled at the end of every instruction, apart from DD/FD and possibly EI/DI.

When an NMI occurs, IFF1 is reset, thereby disallowing further maskable interrupts, but IFF2 is left unchanged. This enables the NMI service routine to check whether the interrupted program had enabled or disabled maskable interrupts. The NMI routine should end with a RETN instruction, which in addition to the usual RET actions copies IFF2 to IFF1, thus restoring the interrupt state of the interrupted code.

When an NMI occurs, it takes 11 T states to get to 0x0066: a 5 T state M1 cycle to do an opcode read and decrement SP, a 3 T state M2 cycle to write the high byte of PC to the stack and decrement SP and finally a 3 T state M3 cycle to write the low byte of PC and jump to 0x0066.
. . . Rodnay Zaks 'Programming the Z80' cookbook has error(s)
User avatar
kokkiklhs
Posts: 103
Joined: Sat Jan 10, 2015 12:09 pm

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by kokkiklhs »

dr beep wrote: Sat Jun 03, 2023 8:24 pm 81-tris uses IY in a way that it is lucky that the code runs on the ZX81 at all.
---------------------------------
I suspect is compiled C or something like this.
This game will not run on SP-2-ZX81.
Hey, it doesn't matter, we still have Bob's excellent Domin8tr1s, which plays fine with the emulator and is probably the best tetris-like game on the Zeddy!! ;)
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by dr beep »

kokkiklhs wrote: Sun Jun 04, 2023 7:38 am Hey, it doesn't matter, we still have Bob's excellent Domin8tr1s, which plays fine with the emulator and is probably the best tetris-like game on the Zeddy!! ;)
Then you haven't played 1K LETRIS yet. :D
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by dr beep »

butchi wrote: Sun Jun 04, 2023 1:53 am
. . . Rodnay Zaks 'Programming the Z80' cookbook has error(s)
Good to know I never used Zaks book and went for William Tang.
User avatar
kokkiklhs
Posts: 103
Joined: Sat Jan 10, 2015 12:09 pm

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by kokkiklhs »

dr beep wrote: Sun Jun 04, 2023 8:59 am
kokkiklhs wrote: Sun Jun 04, 2023 7:38 am Hey, it doesn't matter, we still have Bob's excellent Domin8tr1s, which plays fine with the emulator and is probably the best tetris-like game on the Zeddy!! ;)
Then you haven't played 1K LETRIS yet. :D
No, I haven't... or may I have? :lol:
Not quite sure, in fact I've tried a couple more tetris-like games, but I don't remember the exact names and I also think that Domin8tr1s is the most nice among these I've seen so far! :)
salvacam
Posts: 50
Joined: Wed Jul 21, 2021 4:50 pm

Re: SP-2-ZX81, the new ZX81-emulator for the 48K ZX Spectrum

Post by salvacam »

dr beep wrote: Fri Jun 02, 2023 10:50 pm
kokkiklhs wrote: Fri Jun 02, 2023 6:30 pm The only game I couldn't get to run though (neither with previous versions) is 81-tris and I can't see an obvious reason, seems like a "normal" 16k game to me...
81-tris, like my 1K hires games, has a special defined displaymethod.
It looks like an intrupt controlled game but the method is different from my CALCULUS MAZXIMUS.

We need to ask SALVACAM
I don't know how I can help you, I use z88dk to program, to paint the blocks of the pieces I use the SetBlock function (in ASM), for the rest, instructions, markers,... I use the printf function (from z88dk).

All my games have the code available on my website, it is in my signature
My games

ZX81 hoy Blog (in Spanish) of new games and applications for the ZX81
Post Reply