libmwemu 0.24.4

x86 32/64bits and system internals emulator, for securely emulating malware and other stuff.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
use crate::*;

// DAA — Decimal Adjust AL After Addition
// DAS — Decimal Adjust AL After Subtraction
//
// Opcodes:
// - 27       DAA       Decimal adjust AL after addition
// - 2F       DAS       Decimal adjust AL after subtraction
//
// DAA Operation:
//   IF ((AL AND 0FH) > 9) OR (AF = 1) THEN
//     AL := AL + 6;
//     CF := CF OR (AL > 0xFF);
//     AF := 1;
//   ELSE
//     AF := 0;
//   FI;
//   IF (AL > 0x9F) OR (CF = 1) THEN
//     AL := AL + 0x60;
//     CF := 1;
//   ELSE
//     CF := 0;
//   FI;
//
// DAS Operation:
//   IF ((AL AND 0FH) > 9) OR (AF = 1) THEN
//     AL := AL - 6;
//     CF := CF OR (borrow occurred);
//     AF := 1;
//   ELSE
//     AF := 0;
//   FI;
//   IF (AL > 0x9F) OR (CF = 1) THEN
//     AL := AL - 0x60;
//     CF := 1;
//   ELSE
//     CF := 0;
//   FI;
//
// Flags: SF, ZF, PF, CF, AF are modified. OF is undefined.

// ============================================================================
// DAA (Decimal Adjust After Addition) Tests
// ============================================================================

#[test]
fn test_daa_no_adjustment_needed() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x25 (valid BCD), no flags set
    let code = [
        0x27, // DAA
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x0025;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x25, "AL should remain 0x25");
    assert!(!emu.flags().f_cf, "CF should be clear");
    assert!(!emu.flags().f_af, "AF should be clear");
}

#[test]
fn test_daa_low_nibble_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x1F (low nibble > 9, needs adjustment)
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x001F;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x1F + 6 = 0x25
    assert_eq!(
        emu.regs().rax & 0xFF,
        0x25,
        "AL should be 0x25 after adjustment"
    );
    assert!(!emu.flags().f_cf, "CF should be clear");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_daa_high_nibble_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0xA5 (high nibble > 9, needs adjustment)
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x00A5;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0xA5 + 0x60 = 0x05 (with carry)
    assert_eq!(
        emu.regs().rax & 0xFF,
        0x05,
        "AL should be 0x05 after adjustment"
    );
    assert!(emu.flags().f_cf, "CF should be set");
}

#[test]
fn test_daa_both_nibbles_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0xAF (both nibbles > 9)
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x00AF;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0xAF + 6 + 0x60 = 0x15 (with carry)
    assert_eq!(
        emu.regs().rax & 0xFF,
        0x15,
        "AL should be 0x15 after adjustment"
    );
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_daa_af_set_triggers_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x05 (valid), but AF is set from previous operation
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x0005;
    emu.flags_mut().load(0x10); // Set AF flag
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x05 + 6 = 0x0B
    assert_eq!(emu.regs().rax & 0xFF, 0x0B, "AL should be adjusted to 0x0B");
    assert!(emu.flags().f_af, "AF should remain set");
}

#[test]
fn test_daa_cf_set_triggers_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x25 (valid), but CF is set from previous operation
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x0025;
    emu.flags_mut().load(0x01); // Set CF flag
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x25 + 0x60 = 0x85
    assert_eq!(emu.regs().rax & 0xFF, 0x85, "AL should be adjusted to 0x85");
    assert!(emu.flags().f_cf, "CF should remain set");
}

#[test]
fn test_daa_bcd_addition_no_carry() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // 0x25 + 0x13 = 0x38 (valid BCD, no adjustment needed)
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x0038;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x38, "Result should remain 0x38");
}

#[test]
fn test_daa_bcd_addition_with_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // 0x28 + 0x34 = 0x5C, needs adjustment -> 0x62
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x005C;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(
        emu.regs().rax & 0xFF,
        0x62,
        "Result should be 0x62 (62 in BCD)"
    );
}

#[test]
fn test_daa_bcd_addition_with_carry() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // 0x89 + 0x25 = 0xAE, needs adjustment -> 0x14 with carry
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x00AE;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x14, "Result should be 0x14");
    assert!(emu.flags().f_cf, "CF should be set (carry to next digit)");
}

#[test]
fn test_daa_zero_result() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x0000;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x00, "AL should remain 0");
    assert!(emu.flags().f_zf, "ZF should be set");
}

#[test]
fn test_daa_preserves_high_bits() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0xDEADBEEF_12345625;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(
        emu.regs().rax >> 8,
        0xDEADBEEF_123456,
        "High bits should be preserved"
    );
}

#[test]
fn test_daa_all_valid_bcd_pairs() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    for high in 0..=9 {
        for low in 0..=9 {
            let val = (high << 4) | low;
            let code = [0x27, 0xf4]; // DAA, HLT
            emu.regs_mut().rax = val;
            emu.load_code_bytes(&code);
            emu.run(None).unwrap();

            // Valid BCD values should not be adjusted
            assert_eq!(
                emu.regs().rax & 0xFF,
                val,
                "Valid BCD 0x{:02X} should not be adjusted",
                val
            );
        }
    }
}

#[test]
fn test_daa_boundary_values() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let test_cases = vec![
        (0x09, 0x09, false), // Boundary: low nibble = 9
        (0x0A, 0x10, true),  // Boundary: low nibble = 10 (A)
        (0x90, 0x90, false), // Boundary: high nibble = 9
        (0x9A, 0x00, true),  // Boundary: 0x9A -> 0xA0 -> 0x100 (0x00 with CF)
    ];

    for (input, expected, expect_cf_or_af) in test_cases {
        let code = [0x27, 0xf4]; // DAA, HLT
        emu.regs_mut().rax = input;
        emu.load_code_bytes(&code);
        emu.run(None).unwrap();

        assert_eq!(
            emu.regs().rax & 0xFF,
            expected,
            "Wrong result for input 0x{:02X}",
            input
        );
        if expect_cf_or_af {
            assert!(
                emu.flags().f_cf || emu.flags().f_af,
                "CF or AF should be set for input 0x{:02X}",
                input
            );
        }
    }
}

// ============================================================================
// DAS (Decimal Adjust After Subtraction) Tests
// ============================================================================

#[test]
fn test_das_no_adjustment_needed() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x25 (valid BCD), no flags set
    let code = [
        0x2F, // DAS
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x0025;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x25, "AL should remain 0x25");
    assert!(!emu.flags().f_cf, "CF should be clear");
    assert!(!emu.flags().f_af, "AF should be clear");
}

#[test]
fn test_das_low_nibble_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x1F (low nibble > 9, needs adjustment)
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x001F;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x1F - 6 = 0x19
    assert_eq!(
        emu.regs().rax & 0xFF,
        0x19,
        "AL should be 0x19 after adjustment"
    );
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_das_high_nibble_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0xA5 (high nibble > 9, needs adjustment)
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x00A5;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0xA5 - 0x60 = 0x45
    assert_eq!(
        emu.regs().rax & 0xFF,
        0x45,
        "AL should be 0x45 after adjustment"
    );
    assert!(emu.flags().f_cf, "CF should be set");
}

#[test]
fn test_das_both_nibbles_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0xAF (both nibbles > 9)
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x00AF;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0xAF - 6 - 0x60 = 0x49
    assert_eq!(
        emu.regs().rax & 0xFF,
        0x49,
        "AL should be 0x49 after adjustment"
    );
    assert!(emu.flags().f_cf, "CF should be set");
    assert!(emu.flags().f_af, "AF should be set");
}

#[test]
fn test_das_af_set_triggers_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x05 (valid), but AF is set from previous operation
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x0005;
    emu.flags_mut().load(0x10); // Set AF flag
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x05 - 6 = 0xFF (wraps around)
    assert_eq!(emu.regs().rax & 0xFF, 0xFF, "AL should wrap to 0xFF");
    assert!(emu.flags().f_af, "AF should remain set");
}

#[test]
fn test_das_cf_set_triggers_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // AL = 0x45 (valid), but CF is set from previous operation
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x0045;
    emu.flags_mut().load(0x01); // Set CF flag
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x45 - 0x60 = 0xE5 (wraps around)
    assert_eq!(emu.regs().rax & 0xFF, 0xE5, "AL should be 0xE5");
    assert!(emu.flags().f_cf, "CF should remain set");
}

#[test]
fn test_das_bcd_subtraction_no_borrow() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // 0x58 - 0x23 = 0x35 (valid BCD, no adjustment needed)
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x0035;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x35, "Result should remain 0x35");
}

#[test]
fn test_das_bcd_subtraction_with_adjustment() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x005C; // Invalid BCD result from subtraction
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AL = 0x5C - 6 = 0x56
    assert_eq!(emu.regs().rax & 0xFF, 0x56, "Result should be 0x56");
}

#[test]
fn test_das_zero_result() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x0000;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x00, "AL should remain 0");
    assert!(emu.flags().f_zf, "ZF should be set");
}

#[test]
fn test_das_preserves_high_bits() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0xDEADBEEF_12345625;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(
        emu.regs().rax >> 8,
        0xDEADBEEF_123456,
        "High bits should be preserved"
    );
}

#[test]
fn test_das_all_valid_bcd_pairs() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    for high in 0..=9 {
        for low in 0..=9 {
            let val = (high << 4) | low;
            let code = [0x2F, 0xf4]; // DAS, HLT
            emu.regs_mut().rax = val;
            emu.load_code_bytes(&code);
            emu.run(None).unwrap();

            // Valid BCD values should not be adjusted
            assert_eq!(
                emu.regs().rax & 0xFF,
                val,
                "Valid BCD 0x{:02X} should not be adjusted",
                val
            );
        }
    }
}

// ============================================================================
// DAA/DAS Combined Tests
// ============================================================================

#[test]
fn test_daa_das_sequence() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0x27, // DAA
        0x2F, // DAS
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x0025; // Valid BCD
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x25, "Result should remain 0x25");
}

#[test]
fn test_multiple_daa_operations() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0x27, // DAA
        0x27, // DAA
        0x27, // DAA
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x0045; // Valid BCD
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x45, "Result should remain 0x45");
}

#[test]
fn test_multiple_das_operations() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [
        0x2F, // DAS
        0x2F, // DAS
        0x2F, // DAS
        0xf4, // HLT
    ];
    emu.regs_mut().rax = 0x0067; // Valid BCD
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x67, "Result should remain 0x67");
}

// ============================================================================
// Edge Cases and Special Scenarios
// ============================================================================

#[test]
fn test_daa_max_value() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x00FF;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // 0xFF + 6 + 0x60 = 0x165, AL = 0x65 with CF
    assert_eq!(emu.regs().rax & 0xFF, 0x65, "AL should be 0x65");
    assert!(emu.flags().f_cf, "CF should be set");
}

#[test]
fn test_das_max_value() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x00FF;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // 0xFF - 6 - 0x60 = 0x99
    assert_eq!(emu.regs().rax & 0xFF, 0x99, "AL should be 0x99");
    assert!(emu.flags().f_cf, "CF should be set");
}

#[test]
fn test_daa_sign_flag() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x0080; // Bit 7 set
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert!(emu.flags().f_sf, "SF should be set when bit 7 is set");
}

#[test]
fn test_das_sign_flag() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x0090;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert!(
        emu.flags().f_sf,
        "SF should be set when result has bit 7 set"
    );
}

#[test]
fn test_daa_parity_flag() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x0003; // Will become 0x03 (even parity)
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x03, "AL should be 0x03");
    assert!(emu.flags().f_pf, "PF should be set for even parity");
}

#[test]
fn test_das_parity_flag() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x0003; // Will become 0x03 (even parity)
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax & 0xFF, 0x03, "AL should be 0x03");
    assert!(emu.flags().f_pf, "PF should be set for even parity");
}

#[test]
fn test_daa_with_both_flags_set() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x0023;
    emu.flags_mut().load(0x11); // Set both AF and CF
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AF set: +6 -> 0x29, CF set: +0x60 -> 0x89
    assert_eq!(emu.regs().rax & 0xFF, 0x89, "AL should be 0x89");
    assert!(emu.flags().f_cf, "CF should be set");
}

#[test]
fn test_das_with_both_flags_set() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let code = [0x2F, 0xf4]; // DAS, HLT
    emu.regs_mut().rax = 0x0089;
    emu.flags_mut().load(0x11); // Set both AF and CF
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    // AF set: -6 -> 0x83, CF set: -0x60 -> 0x23
    assert_eq!(emu.regs().rax & 0xFF, 0x23, "AL should be 0x23");
    assert!(emu.flags().f_cf, "CF should be set");
}

#[test]
fn test_daa_bcd_chain_99_plus_1() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    // 0x99 + 0x01 = 0x9A, DAA -> 0x00 with CF
    let code = [0x27, 0xf4]; // DAA, HLT
    emu.regs_mut().rax = 0x009A;
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();

    assert_eq!(
        emu.regs().rax & 0xFF,
        0x00,
        "AL should be 0x00 (overflow to 100)"
    );
    assert!(emu.flags().f_cf, "CF should be set (carry to next byte)");
}

#[test]
fn test_daa_sequential_additions() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let test_cases = vec![
        (0x15, 0x15), // 15 (valid BCD)
        (0x19, 0x19), // 19 (valid BCD)
        (0x1E, 0x24), // 1E -> 24 (adjustment needed)
        (0x2A, 0x30), // 2A -> 30 (adjustment needed)
    ];

    for (input, expected) in test_cases {
        let code = [0x27, 0xf4]; // DAA, HLT
        emu.regs_mut().rax = input;
        emu.load_code_bytes(&code);
        emu.run(None).unwrap();

        assert_eq!(
            emu.regs().rax & 0xFF,
            expected,
            "DAA(0x{:02X}) should be 0x{:02X}",
            input,
            expected
        );
    }
}

#[test]
fn test_das_sequential_subtractions() {
    let DATA_ADDR = 0x7000;
    let mut emu = emu64();
    let test_cases = vec![
        (0x35, 0x35), // 35 (valid BCD)
        (0x42, 0x42), // 42 (valid BCD)
        (0x1F, 0x19), // 1F -> 19 (adjustment needed)
        (0xA0, 0x40), // A0 -> 40 (adjustment needed)
    ];

    for (input, expected) in test_cases {
        let code = [0x2F, 0xf4]; // DAS, HLT
        emu.regs_mut().rax = input;
        emu.load_code_bytes(&code);
        emu.run(None).unwrap();

        assert_eq!(
            emu.regs().rax & 0xFF,
            expected,
            "DAS(0x{:02X}) should be 0x{:02X}",
            input,
            expected
        );
    }
}