Skip to content

Commit 730f179

Browse files
committed
Addition décimale à l'arrache...
1 parent aadab0a commit 730f179

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

SnesEmulator/SnesEmulator.Hardware/CPU.cs

+28
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,33 @@ public void UpdateCurrentContext(ref InstructionDecodeContext context)
203203
context.MFlag = MFlag;
204204
context.XFlag = XFlag;
205205
}
206+
207+
public int BCDConversion(int value)
208+
{
209+
double result = 0;
210+
int i = 1;
211+
string hexValue = value.ToString();
212+
foreach (char c in hexValue)
213+
{
214+
result += char.GetNumericValue(c) * Math.Pow(16, hexValue.Length - i);
215+
i++;
216+
}
217+
return (int)result;
218+
}
219+
220+
public int Decimal16bit(int value)
221+
{
222+
int result = 0;
223+
int mult = 1;
224+
string hexValue = value.ToString("X");
225+
IEnumerable<char> rString = hexValue.Reverse();
226+
foreach (char c in rString)
227+
{
228+
int digit = (int)Convert.ToInt32(c.ToString(), 16) * mult;
229+
result += digit;
230+
mult = mult * 10;
231+
}
232+
return result;
233+
}
206234
}
207235
}

SnesEmulator/SnesEmulator.Hardware/Instructions/InstructionsSets/InstructionADC.cs

+27-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,33 @@ protected void Execute(int value)
174174
{
175175
if (CPU.CarryFlag)
176176
value++;
177-
CPU.ACC += value;
177+
if (CPU.DecimalFlag)
178+
{
179+
if (CPU.MFlag)
180+
{
181+
int tmp = CPU.ACC;
182+
if (tmp > 0x10)
183+
tmp = Convert.ToInt32(tmp.ToString("X"), 10);
184+
CPU.ACC = CPU.BCDConversion(tmp + value);
185+
if (CPU.ACC >= 0x100)
186+
CPU.ACC = CPU.ACC - 0x100;
187+
}
188+
else
189+
{
190+
int tmp = CPU.ACC;
191+
if (tmp > 0x10)
192+
tmp = Convert.ToInt32(tmp.ToString("X"), 10);
193+
int sum = CPU.Decimal16bit(value) + tmp;
194+
CPU.ACC = CPU.BCDConversion(sum);
195+
if (CPU.ACC >= 0x9999)
196+
{
197+
CPU.ACC &= 0xFFFF;
198+
CPU.CarryFlag = true;
199+
}
200+
}
201+
}
202+
else
203+
CPU.ACC += value;
178204
}
179205

180206
protected void SetRegisters()

0 commit comments

Comments
 (0)