Commit 730f179 1 parent aadab0a commit 730f179 Copy full SHA for 730f179
File tree 2 files changed +55
-1
lines changed
SnesEmulator/SnesEmulator.Hardware
Instructions/InstructionsSets
2 files changed +55
-1
lines changed Original file line number Diff line number Diff line change @@ -203,5 +203,33 @@ public void UpdateCurrentContext(ref InstructionDecodeContext context)
203
203
context . MFlag = MFlag ;
204
204
context . XFlag = XFlag ;
205
205
}
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
+ }
206
234
}
207
235
}
Original file line number Diff line number Diff line change @@ -174,7 +174,33 @@ protected void Execute(int value)
174
174
{
175
175
if ( CPU . CarryFlag )
176
176
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 ;
178
204
}
179
205
180
206
protected void SetRegisters ( )
You can’t perform that action at this time.
0 commit comments