# How 6502 Overflow Works

The 6502 was a ubiquitous microprocessor back in the 80s, powering devices such as the Apple II and the Nintendo Entertainment System. [Ken] took an in depth look at a small part of the processor: the overflow circuit.

In order to do signed calculations, a microprocessor’s arithmetic logic unit (ALU) needs to be able to calculate when an overflow occurs. This happens when two numbers are added that cannot fit in a single byte. The result of a calculation will be incorrect, so the processor must inform the program that an overflow has occurred. This is done by setting an overflow flag.

[Ken] uses this example to first explain how the overflow circuit works in logic. He then looks at the gates and a transistor implementation of the logic. Finally, he walks us through the circuit on the actual 6502 die, showing how the circuit is constructed in silicon.

This is a great example to learn a bit more about how ALUs work and how integrated circuits get built.

## 16 thoughts on “How 6502 Overflow Works”

1. Counting Bits, Picking Nits says:

This brings back great memories.

As a teen in the early 80’s, I learned to program in BASIC on a ti99. And then machine language on an APPLE ][. And then assembly language on that Apple. Wow – Macros! Labels! Self-modifying code!

And then I acquired a copy of the Aztec C compiler for the Apple ][. That was an awesome dev environment, complete with a shell that supported redirction much like the Bourne shell, and source for some of the standard UNIX utilities.

(side note, all of that independent study hurt my grades, where it should have been some kind of AP credit)

And then, in University, they wanted me to pay to learn Pascal. WTF?

And now, Java garbage. It’s pretty much been going downhill since those Aztec C days ;-)

1. joecomputer says:

Just like the Simpsons meme with Grandpa walking into the brothel, hanging up his hat and then circling right out, I left my first computer science course in freshman year because the prof said, ok open your pascal books to….I also first cut my teeth on a TI99 4/a and then went to assembler, but on the 6809 chip. I have my masters and 17 years as an engineer now. I plan on teaching my son assembler using the 6502 though.

1. cantido says:

> And now, Java garbage. It’s pretty much been going downhill
> since those Aztec C days ;-)

*yawn* If you can’t adapt and use stuff that isn’t your prefered choice that’s your problem. Things have come a long way since these 8 bit micros were king.

> I left my first computer science course in freshman year because the prof said, ok open your pascal books to…

But the skills you could have learned via Pascal would have been applicable to most programming languages. If you know everything already why even turn up to the class?

1. Alex says:

Come on man, you even quoted the wink part. It was tongue-in-cheek.

2. OMG! For 25 years I thought I was the only one!

I learned Basic on ZX-81 in 1982, when I was in grade 7, primarily from the manual that Sir Clive Sinclair wrote, help from my Dad, and source code from Compute magazine. Later, I learned Z-80 Assembly, then learned more languages on various Atari computers. Then, in 1989 I walked out of my first-year computer science course because it was taught using Pascal!

Of course, I had made a huge mistake, and wound up doing a second degree, this time in Computer Science, and I have been a software developer since 2000. Walking out of that course because of Pascal cost me many years and \$\$.

2. Entropia says:

ALU stands for Arithmetic Logic Unit, not Algorithmic.

3. Ryan7777 says:

“This is a great example to learn a bit more about how ALUs work and how integrated circuits get built.”

in 1975….

Still relevent?

The logic, sure. But thats about it!

1. SavannahLion says:

I’m not sure where you’re coming from on this. Wasn’t there a post just recently that describes how big fabricators are using old techniques of yore for various reasons?

Understanding some of the basics of how underlying foundations work does translate to writing better code in higher languages.

2. Don't chu java me says:

Yeah sure, sure. Just code it in java and the compiler will take care of the details. And if it doesn’t run fast, just throw your hands up and blame the jit compiler or the jvm.

1. cantido says:

>blame the jit compiler or the jvm.

Or don’t be a baby and learn to use the profiling tools that are available?
You can write slow code in any language that’s why development environments have profilers etc.

4. FrankenPC says:

Mother of gawd…the Xeon has 55,200 times as many transistors as the 6502.

5. This is a great analysis! I’m glad he takes on the Overflow flag, as its behavior can be confusing sometimes — especially for emulator authors trying to get their 6502 cores to act correctly. ;-)

6. Ren says:

So, did [Ken] have a description and location of the various gates on the chip, or did he ascertain that from just looking at the artwork?

1. Ren, thanks for your question. I have a couple 6502 chip schematics that I’ve been studying in detail and I figured out the overflow circuit at the gate level from them. The visual6502 project has all the raw transistor locations and labels on many of the transistors, so I could fairly quickly narrow down the right part of the chip. Finding the location of each relevant transistor and mapping it to the gate was straightforward but a bit tedious. I originally planned to explain the entire ALU, but just this one circuit took plenty of time. Maybe in a future article.

7. NES says:

Actually my CPU is not the 6502, but a 6502 based microprocessor called “RP2A03”.

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.