What was the SLL instruction supposed to do?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

What was the SLL instruction supposed to do?

Post by djnzx48 »

The SLL instruction, described in this article, is a well-known undocumented instruction that works by shifting a value to the left and setting the least significant bit to 1. But what was it originally intended to do?

It's commonly suggested that it was meant to be the left-shifting version of SRL - shifting one to the left and setting the rightmost bit to 0. However this doesn't seem right, as in that case it would just be a duplicate of SLA. Now while there are a few redundant instructions on the Z80, they usually only occur with prefixes or with certain combinations of registers, and it wouldn't make sense to have so many duplicates. So is this really true? Was SLL meant to do something else? Was it even supposed to be an instruction at all?
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Re: What was the SLL instruction supposed to do?

Post by Nomad »

Just a wild shot in the dark, but could it have something to do with the 8080 compatibility?
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: What was the SLL instruction supposed to do?

Post by djnzx48 »

Hmm, it could be possible. I haven't really looked at the 8080 instruction set before, but it looks quite primitive in comparison with the Z80. The only rotate/shift instructions seem to be RLC, RRC, RAL, RAR, ADD A, and ADC A, which are just the 8080 versions of RLCA, RRCA, RLA, RRA, ADD A, A, and ADC A, A, plus they all have that horrible Intel syntax as well. I'd guess that the new shift/rotate instructions are still using the old 4-bit ALU, but someone with more hardware knowledge than me would have to confirm this.
AndyC
Dynamite Dan
Posts: 1388
Joined: Mon Nov 13, 2017 5:12 am

Re: What was the SLL instruction supposed to do?

Post by AndyC »

It's not an 8080 thing. More likely it just "falls out" of the way the hardware decodes and implements instructions by providing the "missing" shift operation as it were. It was probably left out of the official documentation simply because it doesn't really have an obvious meaning, since a real SLL would indeed be the same as SLA.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: What was the SLL instruction supposed to do?

Post by djnzx48 »

So it was never intended to be an instruction then? I suppose that there wasn't enough room on the chip for any more 'useful' instructions to fit there anyway. And SLL as it stands is more useful than some of the possible alternatives.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: What was the SLL instruction supposed to do?

Post by Ast A. Moore »

djnzx48 wrote: Mon May 07, 2018 9:33 am Was SLL meant to do something else? Was it even supposed to be an instruction at all?
Perhaps. But perhaps it’s the result of a bug in the instruction decoding matrix. Some believe it was meant to be a shift left inverted arithmetic, but it doesn’t make much sense insofar as computer science is concerned. The SLL mnemonic is not consistent with the rest of the naming conventions, and probably comes from misreading the more logical (pardon the pun) SL1. Don’t get to hung up on the mnemonic itself. It’s an undocumented instruction, after all, and the various the names come from difference sources.

A brief explanation is perhaps in order. There are two types of shifts that the Z80 can do—arithmetic and logical. A logical shift is just that—a shift. The Z80 shuffles all the bits to the right and resets the most significant bit. An arithmetic shift must preserve Bit 7, which is the sign bit, however, so, the instructions were named appropriately: SRL (shift right logical) and SRA (shift right arithmetic).

When doing a left shift, the logic above doesn’t really apply, as any shift will be a logical shift, not an arithmetic shift (Bit 0 does not hold any particular significance other than indicating whether the value is odd or even). SLA (shift left arithmetic) preserves the sign bit by placing it into the carry flag and properly resets Bit 0. SLL does the same thing but sets Bit 1. There’s no point in calling it shift left logical, since it most certainly is not. SLIA (shift left inverted arithmetic) makes sense in that it’s a good descriptor, but, again, computationally speaking, it’s not a conventional operation.

Whatever the story behind it is, it is an equivalent of the operation Register = Register × 2 + 1, which is useful in many circumstances. (For example, you can test Bit 0 prior to shifting and use SLL or SLA depending on its status so as to preserve it.)

To summarize, no, I don’t think it was meant to be an instruction. It’s the result of a happy circumstance of putting a prefix before an existing opcode. It was probably cheaper to leave the instruction decoding logic as is than to design an exception circuitry. Think of it as the floating bus trick on the Spectrum: technically it’s a design flaw/oversight, but it’s a flaw that can be taken advantage of in clever ways.
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
RMartins
Manic Miner
Posts: 776
Joined: Thu Nov 16, 2017 3:26 pm

Re: What was the SLL instruction supposed to do?

Post by RMartins »

I might be stretching my memory here, and going against what is stated in the Z80 manual (old one had lots of errors, around 20, or the new one, from 2016), but:

- SLA stands for Shift Left Arithmetic, which means that it should preserve the Sign bit, but the manual states it differently.
- SLL stands for Shift Left Logical, which means, that it will not consider the sign bit any different from the other bits.

So, following reasoning and not what is in the manual:

LD A, b10111111
SLA

Should result in A becoming b11111110 (0xFE) and not b01111110 (0x7E)

NOTE: You don't want to multiply by 2 a negative number (shift left once), and get a positive number, right ?
Because that is what will happen, if you input -48, assuming the manual is right.

You might want to take a look at this post (for SRA), division by 2 for negative numbers. (near the bottom of the page)
http://zx-dev-2015.proboards.com/thread ... all?page=5


LD A, b10111111
SLL

Should result in A becoming b01111110 (0x7E)

But I would have to try this, on a real CPU (not an emulator) to be 100% sure.
Last edited by RMartins on Tue May 08, 2018 9:27 pm, edited 1 time in total.
User avatar
Seven.FFF
Manic Miner
Posts: 736
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: What was the SLL instruction supposed to do?

Post by Seven.FFF »

Nice theory, but I think

LD A, b10111111
SLL

gives

b01111111.

As Ast says, don't pay too much attention to the unofficial name the opcode has ended up being given. SL1 is really a better name.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
User avatar
RMartins
Manic Miner
Posts: 776
Joined: Thu Nov 16, 2017 3:26 pm

Re: What was the SLL instruction supposed to do?

Post by RMartins »

This document, documents it, and I was apparently wrong :)

http://www.z80.info/zip/z80-documented.pdf
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: What was the SLL instruction supposed to do?

Post by djnzx48 »

I don't think I've ever seen the SL1 or SLIA notation in any of the magazines or literature of the time; I've always thought of them as being more modern mnemonics. The usual ones seem to be SLL (to complement SRL) or SLS (Shift Left Set or even Shift Left Silly).

Amazingly, the discovery of these instructions predates the Spectrum. For example, here's a reference to SLL in a magazine letter from October 1981. And here's the July 1981 article that it refers to, which instead of SLL, uses the bizarre notation DUPINC. How about incorporating that into modern assemblers? :lol:
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: What was the SLL instruction supposed to do?

Post by Ast A. Moore »

djnzx48 wrote: Wed May 09, 2018 7:27 am Amazingly, the discovery of these instructions predates the Spectrum.
Indeed. The Z80 was released in 1976 or thereabouts.
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
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: What was the SLL instruction supposed to do?

Post by djnzx48 »

Yeah, I knew that. It's just that the undocumented instructions didn't seem to be widespread on the Spectrum until years later, and there weren't many articles about them early on, so early discoveries like this must have just been 'forgotten' and had to be rediscovered for a new generation of programmers when the Spectrum came along. It's possible that the Z80 was mainly being used for embedded systems in its early days, so using these undocumented instructions could have been seen as a safety risk if future versions of the chip failed to support them.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: What was the SLL instruction supposed to do?

Post by Ast A. Moore »

djnzx48 wrote: Wed May 09, 2018 8:20 am the Z80 was mainly being used for embedded systems in its early days, so using these undocumented instructions could have been seen as a safety risk if future versions of the chip failed to support them.
True. Although I seem to remember bumping into quite a few of them while disassembling some early Spectrum titles. Granted, SLL is not nearly as widespread as some others.
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
RMartins
Manic Miner
Posts: 776
Joined: Thu Nov 16, 2017 3:26 pm

Re: What was the SLL instruction supposed to do?

Post by RMartins »

I read in a book somewhere, don't remember exactly where, that the first Z80 chips that were commercially available, didn't have a reliable SLL, and hence the reason, for Z80 not to document it.

How true this is, is up for grabs.
User avatar
RMartins
Manic Miner
Posts: 776
Joined: Thu Nov 16, 2017 3:26 pm

Re: What was the SLL instruction supposed to do?

Post by RMartins »

djnzx48 wrote: Wed May 09, 2018 7:27 am ... And here's the July 1981 article that it refers to, which instead of SLL, uses the bizarre notation DUPINC. How about incorporating that into modern assemblers? :lol:
Note the Editors note on that page:
These OpCodes are not tested during production of the Z80 chip by Zilog.
It is possible, in a small number of cases, that any of these opcodes may not work.
Taking into account that Zilog licensed the z80 core to several manufacturers and that there were several non official copies, these opcodes are likely not to work or work differently in Z80 from other manufacturers.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: What was the SLL instruction supposed to do?

Post by djnzx48 »

But if the presence of SLL was merely an implementation detail that wasn't meant to occur in the first place, then what would be the point of testing it?
User avatar
RMartins
Manic Miner
Posts: 776
Joined: Thu Nov 16, 2017 3:26 pm

Re: What was the SLL instruction supposed to do?

Post by RMartins »

A CPU is basically a state machine, that given a set of inputs generates some output and changes internal state.

When the documented instructions, do not cover all possible combinations, the curious human mind, will need to find out, what happens in the other cases :)
And the search for undocumented opcodes is born.

Most of these were probably meant as instructions, but some are just a side effect of how the state machine was built.

A good example are the prefixes, which were added, to increase the functionality, for example by swamping the HL register by IX or iY, among other possible examples. So changing just one of the referenced registers in an operation, provides a complete new set of instructions.

Some examples, are redundant, like "SLA A" and "ADD A, A".
On some systems/CPUs, the shift operation doesn't even exist, because it can be implemented with an ADD.
And for better or for worse, the add is more efficient (4T) than shift (6T).
But ADD only works with A register, where Shift works with any of the 8 bit registers.
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Re: What was the SLL instruction supposed to do?

Post by Nomad »

This is where FPGA projects tend to win big is you get all the undocumented features 'free' as everything from the gate logic level is implemented. As long as you exactly replicate what is on the die, you don't need to 'know' what is going on and implement it like with an emulator.

FPGA's are getting cheaper every year, when it hits the widget sweet spot I figure 8-bit fpga widgets are going to appear like roaches flooding the market :lol: It will be awesome.
User avatar
Seven.FFF
Manic Miner
Posts: 736
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: What was the SLL instruction supposed to do?

Post by Seven.FFF »

Except not really, because most FPGA cores model chip behaviour, not structure.

The T80 as used in the Next and Uno ZX core mimics behaviour. The A-Z80 as used in the Uno CPC core is derived from decapping and photographing, and mimics structure. But it takes up a lot more FPGA resources than the T80 does.

https://opencores.org/project/t80
https://opencores.org/project/a-z80

We will get there in a few more years, as entry-level FPGAs get bigger and more sophisticated.

Behaviour modelling designs are a lot closer to software emulators than most FPGA fanbois will admit.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Re: What was the SLL instruction supposed to do?

Post by Nomad »

True, I never liked behavior simulation projects. It's an emulator hiding inside an fpga :lol: You get all the disadvantages of an emulator.

The next 10-20 years though, gate level re-creations of cpus will be a reality in budget packages. Then comes the interesting question on whom will buy these widgets? because most of the original consumers of 8-bit tech are not going to be in a position to consume...
User avatar
Seven.FFF
Manic Miner
Posts: 736
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: What was the SLL instruction supposed to do?

Post by Seven.FFF »

We will be in our 90s, buying nostalgia-driven recreations of pacemakers and colostomy devices that we used in our 70s!
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
Post Reply