From 3d244d1ec014c98a212b6861f57836ce1521cf77 Mon Sep 17 00:00:00 2001
From: MadMaurice <madmaurice@zom.bi>
Date: Wed, 30 Aug 2023 12:55:16 +0200
Subject: [PATCH] cpu/decoder - Implement DAA

This is a best effort implementation, possible quirks of the actual
hardware have not been considered.
---
 cpu/decoder.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/cpu/decoder.cpp b/cpu/decoder.cpp
index 36aa25b..4e8ac9e 100644
--- a/cpu/decoder.cpp
+++ b/cpu/decoder.cpp
@@ -427,7 +427,21 @@ void Cpu::executeInstruction()
           state.halfcarry = false;
           break;
 
-          // TODO: case 0x27: break; // DAA
+        case 0x27: // DAA
+          {
+            u16 corr = 0;
+            if (state.halfcarry || ((state.A & 0x0F) > 0x9))
+              corr |= 0x06;
+            if ((state.A & 0xF0) > 0x90)
+              corr |= 0x06;
+
+            u32 res16 = (u16)state.A + (state.subtract ? (-corr) : corr);
+            state.A = (u8)res16;
+            state.halfcarry = false;
+            state.zero = (state.A == 0);
+            state.carry = (res16 & 0x100);
+          }
+          break;
 
         case 0x2F: // CPL Complement accumulator
           state.A = ~state.A;