How 6502 Overflow Works

6502 Overflow Circuit

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. 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. 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. > 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?

    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 $$.

    1. 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. 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. >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.

    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.

Leave a Reply to NESCancel reply

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.