"Undocumented" Z80 index register instructions

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
Cosmium
Microbot
Posts: 156
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

"Undocumented" Z80 index register instructions

Post by Cosmium »

I was intrigued coming across the extra rotate, shift, set and reset instructions you get when you add the DD CB and FD CB byte prefixes to normally unused opcodes... I'm talking about the instructions you get that put the result of the memory operation in an 8 bit register 'for free' :o

like:
RL (IX+0)->A takes 23T

which I believe does the same as:
RL (IX+0)
LD A,(IX+0)
which takes 23+19 = 42T

so saving 3 bytes and 19T states - quite a saving.

I like the idea of the savings gained from these undocumented instructions but the index registers are hardly known for being fast anyway, and are somewhat bloated compared to the HL equivalent (though admittedly easier to code with when handling data structures).

I'd be interested if anyone's found any practical benefit for this type of undocumented instruction - like a piece of code designed around these instructions to take advantage of them - and do all Z80s support these instructions?
User avatar
Lethargeek
Manic Miner
Posts: 743
Joined: Wed Dec 11, 2019 6:47 am

Re: "Undocumented" Z80 index register instructions

Post by Lethargeek »

Cosmium wrote: Sun Apr 26, 2020 11:27 pm which I believe does the same as:
RL (IX+0)
LD A,(IX+0)
which takes 23+19 = 42T
not the same, as the value of A will be the original byte rotated even if IX was pointing to ROM, so it's more like "ld a,(ix+):rla:ld (ix+),a"

anyway, i've never once seen these used in the actual code and i've seen quite a lot of Spectrum game code (mostly gfx routines but nonetheless)
catmeows
Manic Miner
Posts: 718
Joined: Tue May 28, 2019 12:02 pm
Location: Prague

Re: "Undocumented" Z80 index register instructions

Post by catmeows »

I wonder if there is ani emulator that is counting statistics of decoded instructions.
Proud owner of Didaktik M
User avatar
djnzx48
Manic Miner
Posts: 730
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: "Undocumented" Z80 index register instructions

Post by djnzx48 »

I used one of those instructions to save a byte in a 64-column print routine. Since I was only operating on temporary strings, it didn't matter that the character byte in memory was corrupted.
User avatar
Cosmium
Microbot
Posts: 156
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: "Undocumented" Z80 index register instructions

Post by Cosmium »

Interesting. Hmm, so these instructions don't seem to offer as much practical benefit as first hoped.

Come to think of it, personally I don't think any of the "undocumented instructions" have been all that useful :| ..except for being able to reference IX,IY as their 8 bit halves.
User avatar
Joefish
Rick Dangerous
Posts: 2058
Joined: Tue Nov 14, 2017 10:26 am

Re: "Undocumented" Z80 index register instructions

Post by Joefish »

Interesting - never heard of this particular set of hidden instructions. Does the result always go into A?
User avatar
arkannoyed
Manic Miner
Posts: 438
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: "Undocumented" Z80 index register instructions

Post by arkannoyed »

I think they can be applied to all the 8 bit registers.
User avatar
Lethargeek
Manic Miner
Posts: 743
Joined: Wed Dec 11, 2019 6:47 am

Re: "Undocumented" Z80 index register instructions

Post by Lethargeek »

Cosmium wrote: Tue Apr 28, 2020 6:20 am Interesting. Hmm, so these instructions don't seem to offer as much practical benefit as first hoped.

Come to think of it, personally I don't think any of the "undocumented instructions" have been all that useful :| ..except for being able to reference IX,IY as their 8 bit halves.
SLL (i prefer SLI designation meaning "shift left and increment") is useful for shifting sprite masks and marking the end of bitstream

OUT(C),0 might be useful for beeper sound or for some tight border effects
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: "Undocumented" Z80 index register instructions

Post by Ast A. Moore »

Cosmium wrote: Tue Apr 28, 2020 6:20 am personally I don't think any of the "undocumented instructions" have been all that useful :| ..except for being able to reference IX,IY as their 8 bit halves.
SLL is quite useful, as well. So are (in rarer cases) IN (C) and OUT (C),0.
arkannoyed wrote: Tue Apr 28, 2020 9:02 am I think they can be applied to all the 8 bit registers.
Indeed. The only exceptions are, obviously, the index register halves themselves.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Turtle_Quality
Manic Miner
Posts: 506
Joined: Fri Dec 07, 2018 10:19 pm

Re: "Undocumented" Z80 index register instructions

Post by Turtle_Quality »

Here's a link documenting the undocumented

http://www.z80.info/z80undoc.htm

More info can be found here https://www.z80cpu.eu/mirrors/www.z80.info/, for example a table of opcodes timings that includes the undocumented instructions
Definition of loop : see loop
Post Reply