@@ -287,6 +287,39 @@ bitflags! {
287
287
}
288
288
}
289
289
290
+ trait Notified {
291
+ fn notified_add ( self , other : Self , sreg : & mut SReg ) -> Self ;
292
+ fn notified_sub ( self , other : Self , sreg : & mut SReg ) -> Self ;
293
+ }
294
+
295
+ impl Notified for u8 {
296
+ fn notified_add ( self , other : Self , sreg : & mut SReg ) -> Self {
297
+ match self . checked_add ( other) {
298
+ Some ( val) => {
299
+ sreg. remove ( SReg :: C ) ;
300
+ val
301
+ }
302
+ None => {
303
+ sreg. insert ( SReg :: C ) ;
304
+ self . wrapping_add ( other)
305
+ }
306
+ }
307
+ }
308
+
309
+ fn notified_sub ( self , other : Self , sreg : & mut SReg ) -> Self {
310
+ match self . checked_sub ( other) {
311
+ Some ( val) => {
312
+ sreg. remove ( SReg :: C ) ;
313
+ val
314
+ } ,
315
+ None => {
316
+ sreg. insert ( SReg :: C ) ;
317
+ self . wrapping_sub ( other)
318
+ }
319
+ }
320
+ }
321
+ }
322
+
290
323
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
291
324
struct Alu {
292
325
primary : u8 ,
@@ -295,19 +328,17 @@ struct Alu {
295
328
296
329
impl Alu {
297
330
fn compute ( & self , aol : bool , aom : bool , aoh : bool , sreg : & mut SReg ) -> u8 {
298
- let previous = self . primary ;
299
-
300
331
match ( aoh, aom, aol) {
301
- ( false , false , false ) => self . primary . wrapping_add ( self . secondary ) ,
302
- ( false , false , true ) => self . primary . wrapping_sub ( self . secondary ) ,
332
+ ( false , false , false ) => self . primary . notified_add ( self . secondary , sreg ) ,
333
+ ( false , false , true ) => self . primary . notified_sub ( self . secondary , sreg ) ,
303
334
( false , true , false ) => self
304
335
. primary
305
- . wrapping_add ( self . secondary )
306
- . wrapping_add ( sreg. contains ( SReg :: C ) as u8 ) ,
336
+ . notified_add ( self . secondary , sreg )
337
+ . notified_add ( sreg. contains ( SReg :: C ) as u8 , sreg ) ,
307
338
( false , true , true ) => self
308
339
. primary
309
- . wrapping_sub ( self . secondary )
310
- . wrapping_sub ( sreg. contains ( SReg :: C ) as u8 ) ,
340
+ . notified_sub ( self . secondary , sreg )
341
+ . notified_sub ( sreg. contains ( SReg :: C ) as u8 , sreg ) ,
311
342
( true , false , false ) => !( self . primary & self . secondary ) ,
312
343
( true , false , true ) => self . primary | self . secondary ,
313
344
_ => 0x00 ,
0 commit comments