

The Z-80 has a 4-bit ALU. Here's how it works. - kens
http://www.righto.com/2013/09/the-z-80-has-4-bit-alu-heres-how-it.html

======
pslam
CPUs of this era were normally multi-cycle for every instruction, but I never
expected in the Z-80 at least one cycle was because the ALU was only 4 bit.
Love the detailed analysis - and this is just the tip of the iceberg of that
site.

One thing I'm missing from this article is an approximate gate count.
Obviously going 4 bit was motivated by gate and area saving, but halving the
ALU size isn't going to halve the gate count or area, because it still needs
the same width bus and extra latches for the partial answer. Or was it
critical path? What kind of saving was it from an 8 bit ALU?

~~~
kens
I don't have a gate count (yet). You're right that the 4-bit ALU doesn't save
a lot of space overall. The Z-80 designer talks a bit about the 4-bit ALU [1]
but doesn't really explain the motivation. My guess was he was able to use two
cycles for the ALU without increasing the overall cycle count because memory
cycles were the bottleneck. If you can cut the ALU in half "for free", why
not? Hopefully as I continue analyzing the chip this will become clearer.

[1] See page 10 in
[http://archive.computerhistory.org/resources/access/text/Ora...](http://archive.computerhistory.org/resources/access/text/Oral_History/102658073.05.01.acc.pdf)

Note: if you're interested in Z-80 architecture, you seriously should read
that link.

~~~
ChuckMcM
One of the reasons I was told was that the circuit extended to 16 bits easily
(and was later used in the Z8000 as I recall) and doing decimal (BCD) math was
easier. DAA (decimal adjust accumulator) was driven by the half carry flag. In
'85 Intel wrote a Z80 emulator in 8086 machine code to try to win some
Japanese game console design win and the decimal arithmetic stuff[1] was a
PITA (and as it turned out not used a lot in games :-)

[1] The 8080 also had these decimal arithmetic hacks but it didn't have an
alternate set of registers to pull from.

~~~
jrabone
Whether to provide BCD optimisation always seemed to be a tricky engineering
decision; virtually nobody used the 6502 BCD instructions in the amateur home
microcomputer environment I was familiar with in the 80s, but it was clearly
considered to be important to the CPU manufacturers. Were there BCD benchmarks
back then? Was it considered a killer feature to make financial software
easier to write? Did Rockwell ever capitalise on that patent?

~~~
pwg
The Atari's ROM's contained a full (well, for the time) floating point library
implementation that used BCD floating point values.

The result was that the Atari's, without even trying, had more accurate
decimal math algorithms than other contemporary computers. Something to do on
the demo machines of the day in stores was to run this loop:

    
    
       10 let x = 100
       20 print x
       30 let x = x - 0.01
       40 goto 20
    

On an Atari this would accurately count down from 100 to zero with zero round
off errors. The exact same loop on an IBM PC after about 5 steps started
printing things like 99.94999999998 instead of 99.95.

Edit: formatting

~~~
rbanffy
I got some interesting results. MSX and Atari computed the results correctly.
On the TRS-80 Model I, wrong results started on the 12th iteration. Apple IIe
(AppleSoft), VIC-20 and PET started the wrong results on the 8th or so. This
has to do with the internal representation of floating-point numbers, of
course - the Apple II uses, IIRC, 5 bytes to represent a float while MSX uses,
again, IIRC (it's been a long time) 8.

------
rayiner
The Pentium 4 also had a 16-bit ALU, which computed a 16-bit operation on each
of the rising and falling edges of the clock to maintain 1-cycle latency.
www.cs.virginia.edu/~mc2zk/cs451/mco_P4.ppt‎.

~~~
stephengillie
_The requested URL /~mc2zk/cs451/mco_P4.pptâ€Ž was not found on this server._

I don't know who is mangling the URL (Chrome, Apache, MITM?) nor why it's
happening.

~~~
rayiner
[http://www.cs.virginia.edu/~mc2zk/cs451/mco_P4.ppt](http://www.cs.virginia.edu/~mc2zk/cs451/mco_P4.ppt)

Does that work?

I copied and pasted it from a Google search result (because otherwise the file
downloads without showing me the URL). Google of course has decided "copy link
to" shouldn't work.

~~~
nitrogen
_Google of course has decided "copy link to" shouldn't work._

There's an addon for that.

~~~
mpyne
It shouldn't _need_ an addon though. I'm often on Google's side on debates but
this is just ridiculous.

~~~
stephengillie
We should make our own browser where we can make those decisions!

------
acegopher
This is awesome. I'm going through the Elements of Computing Systems
book/course (a.k.a. From NAND to Tetris)
[http://www.nand2tetris.org/](http://www.nand2tetris.org/) and it's been great
in helping me understand how CPU's are constructed.

The course actually has you make a ALU from logic gates, so you understand at
a deep level just how it's done.

~~~
kens
That looks like a great course. I find it interesting how real processors
mostly use the same principles you learn in school, but then they throw in
clever tricks and optimizations that you never learn about. And every
processor I've looked at (6502, 8085, Z-80) has its own style.

------
jdmichal
This is also very useful for anyone who's ever worked with a Gameboy emulator.
It uses a processor very similar to the Z-80, including this particular ALU
setup.

~~~
xxpor
I was under the impression that the GameBoy used THE Z-80. Is that not the
case?

~~~
pdw
No, the Game Boy CPU was a custom design.
[http://realboyemulator.wordpress.com/2013/01/02/the-
nintendo...](http://realboyemulator.wordpress.com/2013/01/02/the-nintendo-
game-boy-part-2/)

------
beachstartup
in high school i learned z80 assembly to hack games on my ti-86 calculator.
it's a great chip to learn on.

i remember there was a cross-compiler and a software utility + serial cable
... after some googling:

[http://www.ticalc.org/programming/columns/86-asm/el-
helw/les...](http://www.ticalc.org/programming/columns/86-asm/el-
helw/lesson1.html)

