Close

Add with Carry

A project log for Hardware Controlled 4 bit CPU

A hardware controlled 4 bit CPU based on lessons learnt from the TD4 and the CHUMP CPUs

agpcooperagp.cooper 12/17/2022 at 00:180 Comments

Add with Carry

To date I have used plain and simple ADD with my DIY CPUs. ADC (i.e. add with carry) is useful for multi byte (nibble) addition as the carry is automatic. The downside is that the humble counter does not work, it skips 0. This can be fixed of course.

Here is the old counter using ADD:

20    LOAD 0          ; CLEAR ACC
A0    STORE MEM[0]    ; SAVE TO MEM[0] 
AF    STORE MEM[F]    ; OUTPUT
01    ADD 1           ; INCREMENT ACC
82    JNC 2           ; JUMP ON NOT CARRY
00    ADD 0           ; CLEAR CARRY
82    JNC 2           ; JUMP UNCONDITIONALLY 2

On on overflow you need to clear the carry so that the next JNC is an unconditional jump. 

Here is the new counter using ADC:

00    ADC 0           ; CLEAR CARRY
20    LOAD 0          ; CLEAR ACC
A0    STORE MEM[0]    ; SAVE TO MEM[0]
AF    STORE MEM[F]    ; OUTPUT
01    ADC 1           ; INCREMENT ACC
82    JNC 3           ; REPEAT
00    ADC 0           ; CLEAR CARRY
82    JNC 2           ; JUMP UNCONDITIONAL 2

 So on overflow you need to both clear the carry and clear the accumulator.

Converting the ALU from ADD to ADC involves linking the Carry signal from the Control unit to the Carry In on the Adder in the ALU unit:

For completeness here is the Control unit:

The carry logic holds the carry (CY) until the next ADC instruction.

Multi-Nimble Arithmetic

Before moving on I tested an 8 bit counter using ADC:

00  ADC 0     ; CLEAR CARRY
20  LOAD 0    ; CLEAR ACC
A0  STORE 0   ; CLEAR MEM[0] // LOW NIMBLE
A1  STORE 1   ; CLEAR MEM[1] // HIGH NIMBLE
AF  STORE F   ; CLEAR OUTPUT // LOW NIMBLE
LOOP:
  C0  READ 0  ; PRESET ADDR TO MEM[0]
  30  LOAD 0  ; LOAD LOW NIMBLE
  01  ADC 1   ; INCR LOW NIMBLE
  A0  STORE 0 ; SAVE LOW NIMBLE
  AF  STORE F ; OUTPUT LOW NIMBLE
  C1  READ 1  ; PRESET ADDR TO MEM[1]
  31  LOAD 1  ; LOAD HIGH NIMBLE
  00  ADC 0   ; ADD CARRY TO HIGH NIMBLE
  A1  STORE 1 ; SAVE HIGH NIMBLE
JUMP LOOP:
00 ADC 0      ; CLEAR CARRY
85 JNC 5      ; UNCONDITIONAL JUMP

The ADC does not specifically need the JNC instruction.

The above code is 16 bytes long. A pretty strong justification for a 32 (or more) byte PROM system.

AlanX

Discussions