libmwemu 0.24.0

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
//! Tests for the FBLD and FBSTP instructions.
//!
//! FBLD - Load Binary Coded Decimal
//! FBSTP - Store BCD and Pop
//!
//! FBLD converts a BCD (Binary Coded Decimal) source operand to double extended-precision
//! floating-point format and pushes the value onto the FPU register stack.
//!
//! FBSTP converts the value in ST(0) to an 18-digit packed BCD integer, stores the
//! result in the destination, and pops the register stack.
//!
//! BCD Format: 10 bytes (80 bits)
//! - Bytes 0-8: 18 BCD digits (2 digits per byte)
//! - Byte 9: Sign bit (bit 7) + most significant digit
//!
//! Opcodes:
//! - FBLD m80dec: DF /4
//! - FBSTP m80bcd: DF /6
//!
//! Reference: /Users/int/dev/rax/docs/fbld.txt, /Users/int/dev/rax/docs/fbstp.txt

use crate::*;
use std::convert::TryInto;

const DATA_ADDR: u64 = 0x2000;

fn write_f64(mem: u64, addr: u64, value: f64) {
    let mut emu = emu64();    emu.maps.write_bytes_slice(addr, &value.to_le_bytes());
}

fn read_f64(mem: u64, addr: u64) -> f64 {
    let emu = emu64();    let mut buf = [0u8; 8];
    emu.maps.read_bytes_buff(&mut buf, addr);
    f64::from_le_bytes(buf)
}

// Write BCD value to memory
fn write_bcd(mem: u64, addr: u64, value: &[u8; 10]) {
    let mut emu = emu64();    emu.maps.write_bytes_slice(addr, value);
}

// Read BCD value from memory
fn read_bcd(mem: u64, addr: u64) -> [u8; 10] {
    let emu = emu64();    let mut buf = [0u8; 10];
    emu.maps.read_bytes_buff(&mut buf, addr);
    buf
}

// Helper to create BCD representation from a decimal string
// BCD format: bytes 0-8 contain digits (LSB first), byte 9 has sign bit
fn make_bcd(value: i64) -> [u8; 10] {
    let emu = emu64();    let mut bcd = [0u8; 10];
    let is_negative = value < 0;
    let mut abs_value = value.abs() as u64;

    for i in 0..9 {
        let low = (abs_value % 10) as u8;
        abs_value /= 10;
        let high = (abs_value % 10) as u8;
        abs_value /= 10;
        bcd[i] = (high << 4) | low;
    }

    bcd[9] = if is_negative { 0x80 } else { 0x00 };

    bcd
}

// Helper to extract value from BCD
fn parse_bcd(bcd: &[u8; 10]) -> Option<i64> {
    let emu = emu64();    let is_negative = (bcd[9] & 0x80) != 0;
    let mut value: i64 = 0;
    let mut multiplier: i64 = 1;

    for i in 0..9 {
        let low = (bcd[i] & 0x0F) as i64;
        let high = ((bcd[i] >> 4) & 0x0F) as i64;

        if low > 9 || high > 9 {
            return None; // Invalid BCD
        }

        value += low * multiplier;
        multiplier *= 10;
        value += high * multiplier;
        multiplier *= 10;
    }

    if is_negative {
        Some(-value)
    } else {
        Some(value)
    }
}

// ============================================================================
// FBLD - Load BCD
// ============================================================================

#[test]
fn test_fbld_zero() {
    let mut emu = emu64();    // FBLD tbyte ptr [0x2000]
    // FSTP qword ptr [0x3000]
    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(0));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), 0.0);
}

#[test]
fn test_fbld_positive_one() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(1));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), 1.0);
}

#[test]
fn test_fbld_negative_one() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(-1));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), -1.0);
}

#[test]
fn test_fbld_positive_123() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(123));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), 123.0);
}

#[test]
fn test_fbld_negative_456() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(-456));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), -456.0);
}

#[test]
fn test_fbld_large_positive() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(123456789));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), 123456789.0);
}

#[test]
fn test_fbld_large_negative() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(-987654321));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), -987654321.0);
}

#[test]
fn test_fbld_max_digits() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(999999999999999999));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), 999999999999999999.0);
}

#[test]
fn test_fbld_pushes_to_stack() {
    let mut emu = emu64();    // FBLD should push value onto stack
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x30, 0x00, 0x00, // FLD existing value
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00, // FBLD
        0xDD, 0x1C, 0x25, 0x08, 0x30, 0x00, 0x00, // FSTP new value
        0xDD, 0x1C, 0x25, 0x10, 0x30, 0x00, 0x00, // FSTP old value
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(0x3000, 99.0);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(42));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3008).unwrap(), 42.0); // New value on top
    assert_eq!(emu.maps.read_f64(0x3010).unwrap(), 99.0); // Old value below
}

#[test]
fn test_fbld_multiple() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00, // FBLD 10
        0xDF, 0x24, 0x25, 0x0A, 0x20, 0x00, 0x00, // FBLD 20
        0xDF, 0x24, 0x25, 0x14, 0x20, 0x00, 0x00, // FBLD 30
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP 30
        0xDD, 0x1C, 0x25, 0x08, 0x30, 0x00, 0x00, // FSTP 20
        0xDD, 0x1C, 0x25, 0x10, 0x30, 0x00, 0x00, // FSTP 10
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(10));
    emu.maps.write_bytes_slice(DATA_ADDR + 10, &make_bcd(20));
    emu.maps.write_bytes_slice(DATA_ADDR + 20, &make_bcd(30));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), 30.0);
    assert_eq!(emu.maps.read_f64(0x3008).unwrap(), 20.0);
    assert_eq!(emu.maps.read_f64(0x3010).unwrap(), 10.0);
}

// ============================================================================
// FBSTP - Store BCD and Pop
// ============================================================================

#[test]
fn test_fbstp_zero() {
    let mut emu = emu64();    // FBSTP tbyte ptr [0x3000]
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 0.0);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(0));
}

#[test]
fn test_fbstp_positive_one() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 1.0);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(1));
}

#[test]
fn test_fbstp_negative_one() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, -1.0);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(-1));
}

#[test]
fn test_fbstp_positive_123() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 123.0);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(123));
}

#[test]
fn test_fbstp_negative_456() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, -456.0);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(-456));
}

#[test]
fn test_fbstp_large_positive() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 123456789.0);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(123456789));
}

#[test]
fn test_fbstp_large_negative() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, -987654321.0);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(-987654321));
}

#[test]
fn test_fbstp_rounds_down() {
    let mut emu = emu64();    // 123.4 should round to 123
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 123.4);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(123));
}

#[test]
fn test_fbstp_rounds_up() {
    let mut emu = emu64();    // 123.6 should round to 124
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 123.6);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(124));
}

#[test]
fn test_fbstp_pops_stack() {
    let mut emu = emu64();    // FBSTP should pop the stack
    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD 100
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD 200
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00, // FBSTP 200
        0xDF, 0x34, 0x25, 0x0A, 0x30, 0x00, 0x00, // FBSTP 100
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 100.0);
    emu.maps.write_f64(DATA_ADDR + 8, 200.0);

    emu.run(None).unwrap();
    let bcd1 = emu.maps.read_bytes(0x3000, 10);
    let bcd2 = emu.maps.read_bytes(0x300A, 10);
    assert_eq!(parse_bcd((bcd1).try_into().unwrap()), Some(200));
    assert_eq!(parse_bcd((bcd2).try_into().unwrap()), Some(100));
}

// ============================================================================
// Round-trip tests
// ============================================================================

#[test]
fn test_fbld_fbstp_roundtrip_positive() {
    let mut emu = emu64();    // FBLD followed by FBSTP should preserve value
    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00, // FBLD
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00, // FBSTP
        0xf4,
    ];
    emu.load_code_bytes(&code);
    let original_bcd = make_bcd(12345);
    emu.maps.write_bytes_slice(DATA_ADDR, &original_bcd);

    emu.run(None).unwrap();
    let result_bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((result_bcd).try_into().unwrap()), Some(12345));
}

#[test]
fn test_fbld_fbstp_roundtrip_negative() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    let original_bcd = make_bcd(-67890);
    emu.maps.write_bytes_slice(DATA_ADDR, &original_bcd);

    emu.run(None).unwrap();
    let result_bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((result_bcd).try_into().unwrap()), Some(-67890));
}

#[test]
fn test_fbld_fbstp_roundtrip_zero() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    let original_bcd = make_bcd(0);
    emu.maps.write_bytes_slice(DATA_ADDR, &original_bcd);

    emu.run(None).unwrap();
    let result_bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((result_bcd).try_into().unwrap()), Some(0));
}

#[test]
fn test_fbld_fbstp_roundtrip_large() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    let original_bcd = make_bcd(999999999999);
    emu.maps.write_bytes_slice(DATA_ADDR, &original_bcd);

    emu.run(None).unwrap();
    let result_bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((result_bcd).try_into().unwrap()), Some(999999999999));
}

// ============================================================================
// Arithmetic integration tests
// ============================================================================

#[test]
fn test_fbld_arithmetic() {
    let mut emu = emu64();    // FBLD two values and add them
    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00, // FBLD 100
        0xDF, 0x24, 0x25, 0x0A, 0x20, 0x00, 0x00, // FBLD 200
        0xDE, 0xC1, // FADDP
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00, // FSTP result
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(100));
    emu.maps.write_bytes_slice(DATA_ADDR + 10, &make_bcd(200));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), 300.0);
}

#[test]
fn test_fbstp_after_arithmetic() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00, // FLD 50.0
        0xDD, 0x04, 0x25, 0x08, 0x20, 0x00, 0x00, // FLD 30.0
        0xDE, 0xC1, // FADDP (80.0)
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00, // FBSTP
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 50.0);
    emu.maps.write_f64(DATA_ADDR + 8, 30.0);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(80));
}

#[test]
fn test_fbld_fbstp_sequence() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00, // FBLD 111
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00, // FBSTP 111
        0xDF, 0x24, 0x25, 0x0A, 0x20, 0x00, 0x00, // FBLD 222
        0xDF, 0x34, 0x25, 0x0A, 0x30, 0x00, 0x00, // FBSTP 222
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(111));
    emu.maps.write_bytes_slice(DATA_ADDR + 10, &make_bcd(222));

    emu.run(None).unwrap();
    let bcd1 = emu.maps.read_bytes(0x3000, 10);
    let bcd2 = emu.maps.read_bytes(0x300A, 10);
    assert_eq!(parse_bcd((bcd1).try_into().unwrap()), Some(111));
    assert_eq!(parse_bcd((bcd2).try_into().unwrap()), Some(222));
}

// ============================================================================
// Edge cases
// ============================================================================

#[test]
fn test_fbstp_very_large() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 999999999999999.0);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(999999999999999));
}

#[test]
fn test_fbld_all_nines() {
    let mut emu = emu64();    let code = [
        0xDF, 0x24, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDD, 0x1C, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_bytes_slice(DATA_ADDR, &make_bcd(999999));

    emu.run(None).unwrap();
    assert_eq!(emu.maps.read_f64(0x3000).unwrap(), 999999.0);
}

#[test]
fn test_fbstp_rounding_half() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, 99.5);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(100));
}

#[test]
fn test_fbstp_negative_rounding() {
    let mut emu = emu64();    let code = [
        0xDD, 0x04, 0x25, 0x00, 0x20, 0x00, 0x00,
        0xDF, 0x34, 0x25, 0x00, 0x30, 0x00, 0x00,
        0xf4,
    ];
    emu.load_code_bytes(&code);
    emu.maps.write_f64(DATA_ADDR, -99.7);

    emu.run(None).unwrap();
    let bcd = emu.maps.read_bytes(0x3000, 10);
    assert_eq!(parse_bcd((bcd).try_into().unwrap()), Some(-100));
}