Google-apper
Hovedmeny

Post a Comment On: Ken Shirriff's blog

"The Z-80 has a 4-bit ALU. Here's how it works."

23 Comments -

1 – 23 of 23
Anonymous Anonymous said...

There is an issue with the layout of the OR gate that takes V and the internal carry as inputs: I don't think it should be connected to op2.

September 6, 2013 at 9:47 AM

Anonymous Anonymous said...

(I meant op1, not op2, sorry)

September 6, 2013 at 9:48 AM

Blogger Ken Shirriff said...

Anonymous: yes, that line is positioned ambiguously; it should be connected to the top OR gate, not the bottom one. I have a hard time drawing multi-level gates with Eagle.

September 6, 2013 at 9:55 AM

Anonymous Anonymous said...

Can you explain why the op latches have bidirectional connections to the ALU low bus and ALU high bus? Do the values here need to get read out without going thru the ALU in some circumstances?

September 6, 2013 at 11:02 AM

Anonymous Anonymous said...

In your detailed schematic, the bit select logic seems wrong. You are showing bits 1 and 5, shouldn't it be bits 2 and 6?

September 8, 2013 at 9:04 AM

Blogger Ken Shirriff said...

Anonymous, I'm glad my schematic is getting studied in detail. I think the bit select logic on the far right is correct. The upper NOR gate is active if /i5 is 1, /i4 is 0, and /i3 is 1, i.e. the instruction is xx010xxx, which is 2. The lower NOR gate is active if /i5 is 0, /i4 is 0, and /i3 is 1, i.e. instruction xx110xxx, which is 6.

September 8, 2013 at 10:09 AM

Anonymous Anonymous said...

On bitslice 0 of the ALU, is Carry In set to S so that the XOR works correctly on that bit of the word?

(I guess it's S OR Cin from previous ALU operation).

September 9, 2013 at 5:16 AM

Blogger Ken Shirriff said...

I haven't figured out the carry-in circuit yet, but based on the 8085 Regarding the carry in: I haven't figured out that circuit yet. I expect it to be slightly complicated since it depends on the instruction (e.g. add vs add with carry need different inputs).

In the 8085 the carry-in also needs to be set appropriately for rotate left and rotate left through carry. Since the Z-80 does rotates differently, that won't be the case.

September 9, 2013 at 7:47 AM

Anonymous Steve Chamberlin said...

This is very cool - both how the 8-bit CPU actually has a 4-bit ALU, and the fact that you've reverse-engineered it all by staring at photos of the silicon. I'm in the midst of designing my own custom 4-bit CPU from 7400 logic, so it's interesting to see another relevant example.

September 9, 2013 at 6:30 PM

Anonymous Anonymous said...

I thought I knew the ins and outs of the venerable CPU, but you proved me wrong. Well done, nice work!

September 10, 2013 at 4:19 AM

Anonymous Anonymous said...

What about the 16-bits operations? Are they executed as four 4-bits operations?
R.

September 10, 2013 at 7:50 PM

Blogger Unknown said...

Ken,
I see it now, I misread the double inversion logic. This and the 8085 teardown are excellent. I am looking forward to implementing this in an FPGA, then optimizing it for performance instead of the original need for minimal gate count. BTW: I started designing PCs based on the NEC uPD780 more than thirty years ago, at NEC. The Z80 still is my favorite. No longer Anonymous.

September 12, 2013 at 3:25 PM

Anonymous Anonymous said...

Very interesting stuff, indeed.

BTW, did you find in the layout any of the transistor traps (fake, always on, depletion mode transistors intended to dificult reverse ingeeniering) Masatoshi Shima mentioned?

February 7, 2014 at 6:12 AM

Blogger Unknown said...

It doesn't surprise me that its a 4bit ALU:

I wrote this in 1989, I didn't have access to the silicon implementation but to keep the half carry it made sense that it should be so.

addr3toA: ; subroutine to add r3 to reg_A
extzv #0, #4, r3, r4 ; get lower nibble in r3
extzv #0, #4, reg_A, r6 ; get lower nibble of A in r6
addb2 r4, r6 ; add lower nibbles
cmpb r6, #^x0F
bleq no_H1 ; branch if no Half Carry
bisb2 #^x10, reg_F ; set H_flag
no_H1:
addb2 r3, reg_A ; do add set vax flags
movpsl r2
blbc r2, 31$
bisb2 #^x01, reg_F ; set C flag
31$: bbc #1, r2, 32$
bisb2 #^x04, reg_F ; set V flag
32$: bbc #2, r2, 33$
bisb2 #^x40, reg_F ; set Z flag
33$: bbc $3, r2, 34$
bisb2 #^x80, reg_F ; set S flag

October 6, 2014 at 5:18 PM

Anonymous Anonymous said...

Most of 8-bit CPUs have an 8-bit ALU, so I don't understand what is the avvantage to use a 4-bit ALU to perform 8-bits operations.

April 12, 2015 at 2:36 AM

Blogger gabrieloshiro said...

Hi Ken,

Good job with the reverse engineering. I really like your articles about the z80.

You stated that "Not shown in the block diagram are the simple circuits to compute parity, test for zero, and check if a 4-bit value is less than 10. These values are used to set the condition flags."

Can you please explain how the zero flag is generated? I suppose it is a NOR of all the bits. But since the ALU is 4-bits is the zero flag generated from the 8-bit result? or is it generated from each nibble and then put together, like if there was a "half"-zero flag.

Please keep writing articles about z80 :D Thank you

April 18, 2015 at 10:41 AM

Blogger Unknown said...

good job ! it allowed me to implement this ALU using minecraft redstone.
However, by doing this, I found a few mistakes :
1)the last gate should be a OR gate instead of a NOR.
2)to perform a OR computation, R=0 (and not 1 as mentionned), S=1 and V=1.

December 10, 2016 at 5:44 AM

Blogger Gau_Veldt said...

It's been mentioned a few times that other CPUs such as the 6502 have 8-bit ALUs.

The 6502 ALU is actually a pair of 4-bit ALUs daisy chained together.

You could say the Z80's processing path that requires a second lap through its 4-bit ALU via latches to handle larger words (or three additional laps for a 16-bit word) is simply unrolled one lap in the 6502's silicon to achieve an 8-bit unit. The 6502 would face some additional challenges also were it to have had a monolithic 8-bit ALU since the paired 4-bit nybble ALUs provided an intermediary half-carry on the first nybble that permits a fast carry decider (multiplexed between binary/bcd carry) and fast decimal adjust on the low-order nybble for its patented BCD optimization to work the way it does. With a monolithic 8-bit design the decimal adjustment would have had to have been done in a separate stage to the full 8-bit add/sub output consuming additional cycles.

October 16, 2017 at 9:52 AM

Anonymous Anonymous said...

This post was old. I'm not sure if someone will read this and answer me.
Well, Your image schematics is very helpful
your pdf schematics is seems to be helpful.
But I don't understand it.
(Also I come to find just a logic gate schematic)
(i) how does ALU bus select its bit
(ii) What is that op amp with S gate there
(iii) Can you implement me some pin we will have and what does them do
Thx for all answer soon.

May 5, 2020 at 3:00 AM

Anonymous Anonymous said...

Well Unknown, I think its not mistake. Its base on silicon which have nor gate and have no or gate

May 5, 2020 at 3:03 AM

Anonymous Anonymous said...

> (ii) What is that op amp with S gate there

Maybe a “superbuffer”. I just read that term on http://www.righto.com/2020/10/how-bootstrap-load-made-historic-intel.html#fn:circuits , although that's on a different processor.

It seems to make sense because these appear everywhere a bus has to be driven by a regular gate.

October 24, 2020 at 12:40 PM

Anonymous TonyB said...

The 8080 can do 8-bit ALU operations in 1T and it would be strange if the Z80 is slower, as both were designed by the same people.

The big question for me is how much can the Z80 ALU do in 1T? Is it two 4-bit operations or only one?

P.S.
Ken says the WZ is invisible to the programmer. This is true for the 8080, however bits 5 & 3 of the Z80 W are copied to the same bits in F after BIT b,(HL). Also, CPI/CPD increment/decrement WZ, so that WZ[13:0] can be known for certain.

February 2, 2022 at 9:59 AM

Blogger Wolf said...

A Sinclair ZX81 owner thanks you many times!

February 5, 2022 at 11:10 AM

You can use some HTML tags, such as <b>, <i>, <a>

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot