Difference between revisions of "ADC"

From GbdevWiki
Jump to: navigation, search
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Add With Carry'''
+
{{Opcode}}
----
+
== Add With Carry ==
 +
 
  
This opcode group adds the contents of register r (placeholder for specific implementations) and the carry flag to the contents of register A, to be stored in register A.
+
This opcode group adds the contents of the specified byte-valued input and the register F ''carry'' flag (Flag computed as a 1 or a 0) to the contents of register A, to be stored in register A.
 
The formula can be seen as A=A+r+CY. Since register A is an 8-bit register, you must make sure it wraps around appropriately to only contain an 8-bit value in the range of 0-0xFF (0-255).
 
The formula can be seen as A=A+r+CY. Since register A is an 8-bit register, you must make sure it wraps around appropriately to only contain an 8-bit value in the range of 0-0xFF (0-255).
  
 
The ADC operation code groups are known as follows:<br>
 
The ADC operation code groups are known as follows:<br>
1) ADC A, r<br>
+
1) ''ADC A, r''<br>
2) ADC A, n<br>
+
2) ''ADC A, n''<br>
3) ADC A, (HL)
+
3) ''ADC A, (HL)''
  
  
Line 19: Line 20:
 
The ''half-carry'' flag bit is set to 1 if the operation causes a mathematical carry from bit 3.
 
The ''half-carry'' flag bit is set to 1 if the operation causes a mathematical carry from bit 3.
  
 +
The ''half-carry'' flag can be tricky sometimes to set properly. For this operation it can be recommended that you use some form of the equation below.
 +
 +
''half-carry'' = ((lower nibble of CPU register A) + (lower nibble of the input register) + (carry flag (if set, put 0x1)) > 0xF) ? (boolean true) : (boolean false)
 +
 +
An alternative to this is:
 +
 +
''half-carry'' = ((A & operand) | ((A ^ operand) & ~(A + operand + carry))) & $08
 +
 +
where 'operand' is the register, or immediate byte specified by the instruction.
  
 
----
 
----
 
'''ADC A, r'''
 
'''ADC A, r'''
  
Machine Cycle Count: 1
+
Machine Cycle Count: 1<br>
<center><table style="border: 1px ridge black"><caption>OP Code Instruction Code Numbers:</caption>
+
''r'' is a CPU register as specified in the table below.
 +
 
 +
<center><table style="border: 4px ridge black"><caption>OP Code Instructions:</caption>
 
<tr><td>#</td><td>Register (in place of ''r'')</td></tr>
 
<tr><td>#</td><td>Register (in place of ''r'')</td></tr>
 
<tr><td>8F</td><td>A</td></tr>
 
<tr><td>8F</td><td>A</td></tr>
Line 42: Line 54:
 
OP Code #CE
 
OP Code #CE
  
''n'' is the value of the byte of memory stored at the current immediate value, where the program counter is the 16-bit memory address being accessed.
+
''n'' is the value of the byte of memory stored at a certain location, where the 16-bit address of the memory is the program counter. You must increment the program counter after the operation and wrap to a 16-bit length as appropriate.
  
  
Line 51: Line 63:
 
OP Code #8E
 
OP Code #8E
  
''(HL)'' is the value of the byte of memory stored at the location where the address is the 16-bit value of registers H and L, where the register H is the upper byte, and register L is the lower byte.
+
''(HL)'' is the value of the byte of memory stored at a certain location, where the address is the 16-bit value of registers H and L, being that register H is the upper byte, and register L is the lower byte.

Latest revision as of 11:09, 25 May 2012

This is an article about the GB-Z80 CPU.

Opcodes:

Arithmetic and logical: ADD - ADD (16-bit) including LD HL,SP+nn - ADC - AND - CP - CPL - DAA - DEC - DEC and INC (16-bit) - INC - SBC - SUB - OR - XOR
Conditional: CALL - JP - JR - RET
Load: LD (8-bit) - LD (16-bit)
Extended Set: BIT - RES - RL - RLC - RR - RRC - SET - SLA - SRA - SRL - SWAP

Add With Carry

This opcode group adds the contents of the specified byte-valued input and the register F carry flag (Flag computed as a 1 or a 0) to the contents of register A, to be stored in register A. The formula can be seen as A=A+r+CY. Since register A is an 8-bit register, you must make sure it wraps around appropriately to only contain an 8-bit value in the range of 0-0xFF (0-255).

The ADC operation code groups are known as follows:
1) ADC A, r
2) ADC A, n
3) ADC A, (HL)



Register F Flag Computation

The subtract flag bit is always reset to 0 for any ADC operation. The zero flag bit is set to 1 if the operation sets register A to 0, otherwise the zero flag is reset to 0. The carry flag bit is set to 1 if the operation causes a mathematical carry from bit 7. The half-carry flag bit is set to 1 if the operation causes a mathematical carry from bit 3.

The half-carry flag can be tricky sometimes to set properly. For this operation it can be recommended that you use some form of the equation below.

half-carry = ((lower nibble of CPU register A) + (lower nibble of the input register) + (carry flag (if set, put 0x1)) > 0xF) ? (boolean true) : (boolean false)

An alternative to this is:

half-carry = ((A & operand) | ((A ^ operand) & ~(A + operand + carry))) & $08

where 'operand' is the register, or immediate byte specified by the instruction.


ADC A, r

Machine Cycle Count: 1
r is a CPU register as specified in the table below.

OP Code Instructions:
#Register (in place of r)
8FA
88B
89C
8AD
8BE
8CH
8DL



ADC A, n

Machine Cycle Count: 2
OP Code #CE

n is the value of the byte of memory stored at a certain location, where the 16-bit address of the memory is the program counter. You must increment the program counter after the operation and wrap to a 16-bit length as appropriate.



ADC A, (HL)

Machine Cycle Count: 2
OP Code #8E

(HL) is the value of the byte of memory stored at a certain location, where the address is the 16-bit value of registers H and L, being that register H is the upper byte, and register L is the lower byte.