Skip to content

Commit

Permalink
Explain the DAA instruction algorithm (#1564)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 authored Nov 29, 2024
1 parent a27f704 commit a5d18d6
Showing 1 changed file with 66 additions and 4 deletions.
70 changes: 66 additions & 4 deletions man/gbz80.7
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,66 @@ Flags:
1
.El
.Ss DAA
Decimal Adjust Accumulator to get a correct BCD representation after an arithmetic instruction.
Decimal Adjust Accumulator.
.Pp
Designed to be used after performing an arithmetic instruction
.Pq Sy ADD , ADC , SUB , SBC
whose inputs were in Binary-Coded Decimal (BCD), adjusting the result to likewise be in BCD.
.Pp
The exact behavior of this instruction is as follows:
.Bl -tag -width Ds -offset indent
.It If the subtract flag Sy N No is set:
.Bl -enum -compact
.It
Initialize the adjustment to 0.
.It
If the half-carry flag
.Sy H
is set, then add
.Ad $6
to the adjustment.
.It
If the carry flag is set, then add
.Ad $60
to the adjustment.
.It
Subtract the adjustment from
.Sy A .
.It
Set the carry flag if borrow (i.e. if adjustment >
.Sy A ) .
.El
.It If the subtract flag Sy N No is not set:
.Bl -enum -compact
.It
Initialize the adjustment to 0.
.It
If the half-carry flag
.Sy H
is set or
.Sy A
&
.Ad $F
>
.Ad $9 ,
then add
.Ad $6
to the adjustment.
.It
If the carry flag is set or
.Sy A
>
.Ad $9F ,
then add
.Ad $60
to the adjustment.
.It
Add the adjustment to
.Sy A .
.It
Set the carry flag if overflow from bit 7.
.El
.El
.Pp
Cycles: 1
.Pp
Expand Down Expand Up @@ -1072,11 +1131,14 @@ Bytes: 3
Flags: None affected.
.Ss LD [n16],SP
Store
.Sy SP & $FF
.Sy SP
&
.Ad $FF
at address
.Ar n16
and
.Sy SP >> 8
.Sy SP
>> 8
at address
.Ar n16
+ 1.
Expand Down Expand Up @@ -1859,7 +1921,7 @@ Set if result is 0.
.It Sy H
Set if borrow from bit 4.
.It Sy C
Set if borrow (set if
Set if borrow (i.e. if
.Ar r8
>
.Sy A ) .
Expand Down

0 comments on commit a5d18d6

Please sign in to comment.