
Z80, the 8-bit Number Cruncher (2011) - elvis70
http://www.andreadrian.de/oldcpu/Z80_number_cruncher.html
======
nickcw
Nice article!

Back in the day I wrote 10s of thousands of lines of Z80 assembler - it was my
first actual paid job after university.

You got to know each register - they have their own character - need a loop -
well you need B and DJNZ, need programmable IO, well you need C then, etc

I still have a soft spot for the Z80!

Reading that article I learnt some great techniques! The code I was writing
always used the alternate register bank for interrupt handling so you could
have really fast save and restore registers.

I never thought of using them to pass parameters and doing fast arithmetic -
very neat!

    
    
        ; ADD ROUTINE 32+32BIT=32BIT
        ; H'L'HL = H'L'HL + D'E'DE
        ; CHANGES FLAGS
        ;
        ADD32:
            ADD     HL,DE   ; 16-BIT ADD OF HL AND DE
            EXX
            ADC     HL,DE   ; 16-BIT ADD OF HL AND DE WITH CARRY
            EXX
            RET

------
klelatti
I loved writing Z80 assembly back in the 1980s but it was a bit of as shock to
find out from Ken Shirriff [1] that this 'number cruncher' had a 4 bit ALU.

[1] [http://www.righto.com/2013/09/the-z-80-has-4-bit-alu-
heres-h...](http://www.righto.com/2013/09/the-z-80-has-4-bit-alu-heres-how-
it.html)

EDIT: Just to be clear this is in no way intended to be a criticism of the
Z80. On the contrary it's a tribute to the ingenuity of Federico Faggin and
Masatoshi Shima that they were looking for ways to 'save' transistors in the
ALU in a way which no-one would notice, which presumably enabled them to add
more features elsewhere.

~~~
ncmncm
The Z80 is not above criticism. Its fancy loop instructions were slower than a
regular loop.

But the 4-bit ALU was the result of a very astute observation that
inefficiencies in the 8080 microachitecture would hide an extra ALU cycle.

------
Snitch-Thursday
Okay guys, this may be off-topic, not sure as I've not finished the article
yet.

What (wiki?)books can I purchase that would help me bridge my mental
understanding of computer circuitry from 'this is a circuit with a resistor
and a power source and a lightbulb, while this is a CPU that has wires to
memory and magic happens to make em talk' to 'I know the difference between
TTL logic and a microprocessor and can fuzzily trace where the electrical
signals go'?

I don't own but am aware of things called logic analyzers, 'progammers', and
the difference between a microcontroler and a microprocessor (ish, the former
is vaguely a system on a chip?), but I'm not sure where to start to fill in
the many gaps in the bits of knowledge I have so far.

Wikipedia feeds me articles about logic gates, programmable logic, and
Algorithmic Logical Units used in things like the Xerox Alto, but I don't know
if this means I should try for electrical engineering courses or something
simpler?

~~~
NwtnsMthd
I don't have a great answer for you but I'll give it a shot. It sounds like
you're trying to get from a basic understanding of analog circuits to
understanding the makeup of a simple processor (e.g. the Z80).

The general progression is:

    
    
      1. Circuit Theory (how fundamental components work)
    
      2. Semiconductors(diodes, transistors, op-amps [skip those for now])
    
      3. Digital Electronics (basic logic gates and boolean algebra)
    

Once you finish learning about digital electronics you should have the
fundamental knowledge to understand how a basic processor might operate. A
reasonably good place to start might be here.

[https://www.allaboutcircuits.com/textbook/](https://www.allaboutcircuits.com/textbook/)

Read Volume 1, Volume 3 (skip chapters 7-13), and Volume 4. That should get
you up to speed reasonably quickly.

~~~
Snitch-Thursday
Thanks for this, I was hoping to get a sketch of how I should orient my big-
picture roadmap to understand digital electronics from the old 'electric light
circuit kit' i used to have. While I can't claim that I'll complete this stuff
quickly as the sibling reply mentions, I will certainly look into it.

~~~
pkaye
Its about 4 courses (semester long) worth of studying I would consider
essential. One circuits analysis course, two digital design courses, and
computer architecture. By the end you should be able to build a simple RISC
cpu on an FPGA.

------
astatine
I had built (with a couple of other people) a TCP/IP stack for the Z80 in the
late 90s. And a small HTTP server to go with it. It had a weird CGI like
interface to call some subroutine for a path. The intention was to use it in
some kind of industrial equipment where users could _directly_ point their
browser to this HTTP server and get status updates. A super simple IOT, if you
will. This was actually for Zilog itself who were positioning the (already
faded) chip for embedded networking.

It was perfect for such applications!

Good memories.

~~~
jhallenworld
Dataprobe's original "iBoot" worked like this:

[http://dataprobe.com/iboot-hub/](http://dataprobe.com/iboot-hub/)

It had a built-in web server running on an 8051! It interfaced with an
Ethernet chip using a parallel FIFO interface- maybe NE2000 based chip? I
don't remember..

~~~
astatine
I had a similar parallel interface to an Ethernet daughterboard. I too don't
recollect the make. But there was a serial PPP based option as well - it made
perfect sense when main stream modems were anyway 9600 baud or lesser.

------
mark-r
One trick I used a lot with the Z80 is the conditional return instructions.
Instead of doing a jump, structure your functions so you could return early on
one of the condition flags. The jump instructions were 3 bytes, the return
instructions were only 1.

~~~
sedatk
It was only useful when stack wasn’t involved I presume?

~~~
rusk
Not sure about Z80 but stack is a bit of a luxury on 6502 ...

~~~
gp2000
The Z-80 stack pointer (SP) is a full 16 bit register. The stack can be
anywhere in memory and be as large as needed.

The 6502 has only an 8 bit stack pointer which points into page 1 only
(addresses 0x100 .. 0x1FF).

------
hsitz
The Z80 was the chip in the original Radio Shack TRS-80 computers. I got one
back in 1979, when I was 15 years old. I had a book on the Z80, the text in
this article brings back memories, and figured out how to do some simple
machine language programming. Mostly simple graphics programs, storing values
to the TRS-80's display memory buffer. Eventually I got the TRS-80 Assembler
that made things a bit easier. I can't remember now how I actually input the
machine language hexadecimal without using that assembler, but I'm sure I did.
Or maybe not, my memory is sometimes not so reliable on these things, even
when I feel certain.

I can hardly imagine how different things must be now for young kids growing
up in internet era, where everything is at your fingertips. Information was
much more difficult to get back in the old days, traveled much more slowly.
But, still, kids had the energy to figure things out.

~~~
java-man
Now, imagine the thrill of holding an original Intel 8080 data book behind the
Iron Curtain. With impeccably drawn diagrams, on high quality paper, in stark
contrast with anything produced by the Soviet industry.

------
jhallenworld
8088 was only three years later than z80 and it was much better than it. But
cost also matters.. I just looked at 1982 Byte Magazine at the "JDR
Microdevices" ad in the back:

    
    
        Z80A   $6.00
        Z80B   $18.95
        6502   $6.95
        6502A  $12.95
        6809   $24.95
        8088   $39.95

~~~
Someone
“Only three years” is two doublings according to Moore’s law, and that law
held fairly strongly at the time (transistor counts, according to
[https://en.wikipedia.org/wiki/Transistor_count](https://en.wikipedia.org/wiki/Transistor_count):
8,500 (“6,813 without depletion mode pull-up transistors”) vs 29,500), so of
course it was much better.

~~~
klelatti
For me the it's disappointing how small a step forward the 8088 was from the
Z80 given the increase in the number of transistors.

It came a few years later but the ARM1 which used 25,000 transistors and was
built on roughly the same process size [1] and was much faster than the
8086/8088 shows what might have been had Intel gone down a RISC route.

[1]
[https://en.wikichip.org/wiki/acorn/microarchitectures/arm1](https://en.wikichip.org/wiki/acorn/microarchitectures/arm1)

~~~
justin66
> It came a few years later but the ARM1 which used 25,000 transistors and was
> built on roughly the same process size [1] and was much faster than the
> 8086/8088 shows what might have been had Intel gone down a RISC route.

The 8086 the 8088 was based on was released in 1978. The ARM1 was released in
1985. That was an _eternity._

~~~
klelatti
It was a long time but the ARM1 didn't benefit because of the development of
Moore's law over that period - if you look at the process, die size and clock
speed it's all comparable with the 8086 - the ARM1 was built with very mature
(and cheap) technology.

As has been said elsewhere the ARM1 wouldn't have worked commercially in 1978
because of code density and bus width.

~~~
justin66
Well, okay, but it seems more significant to me that the ARM1 did not exist in
the late seventies - which would have been really great - because _the field
advanced._ The original comment struck me as a bit strange because it implies
that Intel just made a single bad call, or something.

~~~
klelatti
Very much agree - I was (not very effectively) trying to make the point that
its not just about transistor counts and that what you do with those
transistors - the field advancing as you say - matters a lot too. Credit to
Hennessey etc who developed the original RISC concept.

I do think though that if you look back at the history Intel put huge
resources into the iAPX432 which was hugely overambitious and they had to rely
on the fallback of the 8086/88\. There is probably a reasonable case that if
they had hadn't split their resources we might have had something better than
the 8086.

------
tyingq
You can still buy new TI calculators powered by a Z80. Pretty long-lived
platform.

~~~
NanoWar
They are pretty amazing, because you can start with Ti-Basic and once you're
hooked you will want to know all about writing assembly. Doing that with the
rich ecosystem/libraries and community available is much easier than doing the
same with just the processor alone. You can load your games (presumably) with
USB and have cursor keys right below the screen yay!

~~~
tyingq
Unfortunately, TI did make ASM much harder to do earlier this year. There is a
jailbreak for some OS versions:
[https://linustechtips.com/main/topic/1198899-texas-
instrumen...](https://linustechtips.com/main/topic/1198899-texas-instruments-
bans-all-asm-programs-games-on-ti-84-plus-ce-
calculators/?tab=comments#comment-13635333)

------
s_gourichon
Interesting article.

Indeed the Z80 was used in the Amstrad CPC series. There, the firmware used
the alternate register set for bookkeeping, so you could use them only in a
code region surrounded with interrupt dis/enabling and saving/restoring them.
The floating point format on the CPC is 40 bits, probably one of the formats
mentioned in the article.

A few people still create prod for the Z80 every year, there are already 10
productions registered in 2020 on
[http://www.pouet.net/prodlist.php?platform%5B%5D=Amstrad+CPC...](http://www.pouet.net/prodlist.php?platform%5B%5D=Amstrad+CPC&page=1)

The CPC had a huge graphics memory for the time: 16k, making nice-looking
games in paper catalog but very limited full-screen animation without piles of
hacks.

Lots of actual number crunching for 3D graphics in thi old prod "All in 3D"
[https://www.pouet.net/prod.php?which=14667](https://www.pouet.net/prod.php?which=14667)
(no video, you have to run it e.g. in an emulatoe).

For an exceptional result of Z80 performing number crunching and graphical
hacks, see this nice demo from 2017 "Amstrad CPC demo : Logon's run - 3D meets
the aging bits"
[https://www.youtube.com/watch?v=22wSm4y27Wk](https://www.youtube.com/watch?v=22wSm4y27Wk)
. Or "Batman Forever" demo of 2011
[https://www.youtube.com/watch?v=_syHewDu5lc](https://www.youtube.com/watch?v=_syHewDu5lc)

I personally made a production this year
[https://gourichon.org/cpcitor/justget9/beta/](https://gourichon.org/cpcitor/justget9/beta/)
a puzzle game with smooth animation, which got awarded at a local contest
[https://www.facebook.com/groups/1120607071477318/permalink/1...](https://www.facebook.com/groups/1120607071477318/permalink/1526248760913145/)

------
elvis70
I noticed an error in the "Z80 IF-ELSE" section. It says that the overflow
flag is the equivalent of the carry flag for signed numbers. But this is
wrong. The overflow flag indicates that the result of the signed operation is
wrong. In fact, it is the sign and zero flags that should be tested. In case
of overflow, the sign must be reversed.

See this document about the carry and overflow flags:
[http://teaching.idallen.com/dat2343/11w/notes/040_overflow.t...](http://teaching.idallen.com/dat2343/11w/notes/040_overflow.txt)

The code for comparing HL >= DE should be: (Replaced JP PO by JP M + inversion
of the sign if overflow)

    
    
      ;==================================================
      ; IF-ELSE CODE FRAGMENT
      ;
              LD      HL,-2000        ; HL = -2000;
              LD      DE,-1000        ; DE = -1000;
    
              AND     A               ; if (HL >= DE) {
              SBC     HL,DE
              JP      PO,NOOV
              LD      A,H             ; INVERSE SIGN BIT IF OVERFLOW
              XOR     80H             ;
      NOOV:
              JP      M,ELSE
              LD      A,1             ;   A = 1;
              JR      ENDIF
      ELSE:                           ; } else {
              LD      A,0             ;   A = 0;
      ENDIF:                          ; }
              HALT
      
              END

------
klelatti
For anyone interested in Z80 assembly, the ZX Spectrum ROM disassembly,
recently discussed on HN [1] is a treasure trove of annotated Z80 code
including a full set of floating point routines.

[1]
[https://news.ycombinator.com/item?id=23757449](https://news.ycombinator.com/item?id=23757449)

------
ralphc
1977 me had to choose between a TRS-80 and an Apple ][. Apple had the games &
color but man, the Z80 was so much nicer for assembly programming than the
6502. Z80 assembly and TRS-80 BASIC got me started on a lifelong career and
passion.

------
speeder
People make low bit general purpose CPUs with modern processes? Or devices
that use such CPUs still use ones with ancient designs?

~~~
kevin_thibedeau
Most moderate complexity ICs have a microcontroller inside. Depending on
performance needs an 8-bitter is sufficient. These will typically be a clone
of an established architecture so that tooling is readily available.

~~~
speeder
Well... exactly.

Thing is, since I am a game-dev: it would be cool to have a cheap console or
portable console that runs at quite high speeds.

For example what would be possible if I had a Z80 that ran at 2ghz?

~~~
PopePompus
Running an old 8-bit design at a very high clock speed would probably consume
more power than a slower clocked ARM chip which could process as fast or
faster due to the higher complexity of the chip. Using an old 8-bit design
only makes sense when the computing requirements are extremely modest.

------
ronald_petty
I just found a book at an electronics store yesterday for the Z80. I believe
it was copyright 1980. Nice post.

