Big Mess o’ Wires


An 8-bit home-built CPU

ADC $NNNN,X

Uh oh. I was cranking through the microcode implementation for BMOW’s instruction set, making good progress. I’d finished about two-thirds of the microcode, when I came to ADC absolute, X-indexed. That’s when the wheels came off the cart.

This instruction (add with carry) is supposed to take an absolute memory address, adjust the address by adding the contents of the X register, then add the value at the effective address to the accumulator, plus whatever value was already in the carry bit of the condition code register. Unfortunately, I don’t see how I can reasonably implement it with my current hardware design.

The reason that ADC $NNNN,X is problematic is that it modifies the condition codes (to test and propagate a carry from the low byte to the high byte of the effective address when adding X), but it also depends on the current value of the condition codes (for the carry flag). The effective address computation destroys the carry bit that’s needed for the add step. SBC $NNNN,X (subtract with carry, absolute, X-indexed) has the same problem.

I see two possible ways to fix this, neither one great:

  • Store the old condition codes somewhere before computing the effective address, then restore them for the add step. That would make this instruction unreasonably slow, however. It might not even fit within the 16-clock limitation for a single instruction.
  • Add a fifth pseudo-flag to the condition code register for handling the carry propagation during effective address computation. This flag would be invisible to the programmer, and only accessible at the microcode level. But that would give me 5 bits in my 4-bit condition code register, and also require compensatory changes in the control subsystem and microcode assembler.

Poop.  I’m half hoping that if I stare at it for a while, I’ll think of a better solution.

2 Comments so far

  1. David Cary April 25th, 2008 9:12 pm

    Can you simply add the carry bit to X at the beginning, *then* trash the carry bit while calculating the effective address, and then later add the value from RAM to X and calculate the correct carry bit?
    … seems to give correct result, *except* for the special case when C=1 and X=FF. Alas.

  2. Steve April 26th, 2008 7:15 am

    In this case, X is an offset for the effective address, not a value to be included in the addition. In pseudo-C, the desired result is A += *(ADDR + X) + CARRY.

Leave a reply. Comments may take a few minutes to appear.


Homebuilt CPUs WebRing

JavaScript by Qirien Dhaela

Join the ring?

David Brooks, designer of the Simplex-III homebrew computer, has founded the Homebuilt CPUs Web Ring. To join, drop Dave a line, mentioning your page's URL. It will then be added to the list. You'll need to copy this code fragment into your page.