libmwemu 0.24.1

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
use crate::*;

// VPSHUFHW - Shuffle Packed High Words (AVX2)
//
// Shuffles the high 4 words (words 4-7) of each 128-bit lane according to an 8-bit immediate.
// The low 4 words (words 0-3) are copied unchanged.
//
// For each 128-bit lane:
// - Words 0-3 are copied unchanged
// - Bits [1:0] of imm8 select which high word (4-7) goes to word 4
// - Bits [3:2] of imm8 select which high word (4-7) goes to word 5
// - Bits [5:4] of imm8 select which high word (4-7) goes to word 6
// - Bits [7:6] of imm8 select which high word (4-7) goes to word 7
//
// Opcode: VEX.256.F3.0F.WIG 70 /r ib    VPSHUFHW ymm1, ymm2/m256, imm8

const ALIGNED_ADDR: u64 = 0x3000;

// ============================================================================
// Tests with identity shuffle (0xE4 = 11 10 01 00)
// ============================================================================

#[test]
fn test_vpshufhw_ymm0_ymm1_identity() {
    let mut emu = emu64();
    // VPSHUFHW YMM0, YMM1, 0xE4 (identity: high words 7,6,5,4)
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0xe4, // VPSHUFHW YMM0, YMM1, 0xE4
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm3_ymm4_identity() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xdc, 0xe4, // VPSHUFHW YMM3, YMM4, 0xE4
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm6_ymm7_identity() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xf7, 0xe4, // VPSHUFHW YMM6, YMM7, 0xE4
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

// ============================================================================
// Tests with reverse shuffle (0x1B = 00 01 10 11)
// ============================================================================

#[test]
fn test_vpshufhw_ymm0_ymm1_reverse() {
    let mut emu = emu64();
    // VPSHUFHW YMM0, YMM1, 0x1B (reverse high words: 4,5,6,7)
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x1b, // VPSHUFHW YMM0, YMM1, 0x1B
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm2_ymm3_reverse() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xd3, 0x1b, // VPSHUFHW YMM2, YMM3, 0x1B
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm5_ymm6_reverse() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xee, 0x1b, // VPSHUFHW YMM5, YMM6, 0x1B
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

// ============================================================================
// Tests with broadcast patterns
// ============================================================================

#[test]
fn test_vpshufhw_ymm0_ymm1_broadcast_word4() {
    let mut emu = emu64();
    // VPSHUFHW YMM0, YMM1, 0x00 (broadcast word 4 to high words)
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x00, // VPSHUFHW YMM0, YMM1, 0x00
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm3_ymm4_broadcast_word5() {
    let mut emu = emu64();
    // VPSHUFHW YMM3, YMM4, 0x55 (broadcast word 5: 01 01 01 01)
    let code = [
        0xc5, 0xfe, 0x70, 0xdc, 0x55, // VPSHUFHW YMM3, YMM4, 0x55
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm5_ymm6_broadcast_word6() {
    let mut emu = emu64();
    // VPSHUFHW YMM5, YMM6, 0xAA (broadcast word 6: 10 10 10 10)
    let code = [
        0xc5, 0xfe, 0x70, 0xee, 0xaa, // VPSHUFHW YMM5, YMM6, 0xAA
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm7_ymm0_broadcast_word7() {
    let mut emu = emu64();
    // VPSHUFHW YMM7, YMM0, 0xFF (broadcast word 7: 11 11 11 11)
    let code = [
        0xc5, 0xfe, 0x70, 0xf8, 0xff, // VPSHUFHW YMM7, YMM0, 0xFF
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

// ============================================================================
// Tests with swap pairs (0x4E = 01 00 11 10)
// ============================================================================

#[test]
fn test_vpshufhw_ymm0_ymm1_swap_pairs() {
    let mut emu = emu64();
    // VPSHUFHW YMM0, YMM1, 0x4E (swap low/high pairs of high words)
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x4e, // VPSHUFHW YMM0, YMM1, 0x4E
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm2_ymm3_swap_pairs() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xd3, 0x4e, // VPSHUFHW YMM2, YMM3, 0x4E
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm4_ymm5_swap_pairs() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xe5, 0x4e, // VPSHUFHW YMM4, YMM5, 0x4E
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

// ============================================================================
// Tests with extended registers (YMM8-YMM15)
// ============================================================================

#[test]
fn test_vpshufhw_ymm8_ymm9_identity() {
    let mut emu = emu64();
    let code = [
        0xc4, 0x41, 0x7e, 0x70, 0xc1, 0xe4, // VPSHUFHW YMM8, YMM9, 0xE4
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm10_ymm11_reverse() {
    let mut emu = emu64();
    let code = [
        0xc4, 0x41, 0x7e, 0x70, 0xd3, 0x1b, // VPSHUFHW YMM10, YMM11, 0x1B
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm12_ymm13_broadcast() {
    let mut emu = emu64();
    let code = [
        0xc4, 0x41, 0x7e, 0x70, 0xe5, 0x00, // VPSHUFHW YMM12, YMM13, 0x00
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm14_ymm15_swap() {
    let mut emu = emu64();
    let code = [
        0xc4, 0x41, 0x7e, 0x70, 0xf7, 0x4e, // VPSHUFHW YMM14, YMM15, 0x4E
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm15_ymm8_custom() {
    let mut emu = emu64();
    let code = [
        0xc4, 0x41, 0x7e, 0x70, 0xf8, 0x39, // VPSHUFHW YMM15, YMM8, 0x39
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

// ============================================================================
// Tests with memory operands
// ============================================================================

#[test]
fn test_vpshufhw_ymm0_mem_identity() {
    let mut emu = emu64();
    let code = [0x48, 0xb8];
    let mut full_code = code.to_vec();
    full_code.extend_from_slice(&ALIGNED_ADDR.to_le_bytes());
    full_code.extend_from_slice(&[
        0xc5, 0xfe, 0x70, 0x00, 0xe4, // VPSHUFHW YMM0, [RAX], 0xE4
        0xf4, // HLT
    ]);

    emu.load_code_bytes(&full_code);
    let data: Vec<u8> = (0..32).collect();
    emu.maps.write_bytes_slice(ALIGNED_ADDR, &data);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm1_mem_reverse() {
    let mut emu = emu64();
    let code = [0x48, 0xb8];
    let mut full_code = code.to_vec();
    full_code.extend_from_slice(&ALIGNED_ADDR.to_le_bytes());
    full_code.extend_from_slice(&[
        0xc5, 0xfe, 0x70, 0x08, 0x1b, // VPSHUFHW YMM1, [RAX], 0x1B
        0xf4, // HLT
    ]);

    emu.load_code_bytes(&full_code);
    let data: Vec<u8> = (0..32).map(|i| i * 2).collect();
    emu.maps.write_bytes_slice(ALIGNED_ADDR, &data);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm2_mem_broadcast() {
    let mut emu = emu64();
    let code = [0x48, 0xb8];
    let mut full_code = code.to_vec();
    full_code.extend_from_slice(&ALIGNED_ADDR.to_le_bytes());
    full_code.extend_from_slice(&[
        0xc5, 0xfe, 0x70, 0x10, 0x00, // VPSHUFHW YMM2, [RAX], 0x00
        0xf4, // HLT
    ]);

    emu.load_code_bytes(&full_code);
    let data: Vec<u8> = vec![0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA];
    emu.maps.write_bytes_slice(ALIGNED_ADDR, &data);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm3_mem_swap() {
    let mut emu = emu64();
    let code = [0x48, 0xb8];
    let mut full_code = code.to_vec();
    full_code.extend_from_slice(&ALIGNED_ADDR.to_le_bytes());
    full_code.extend_from_slice(&[
        0xc5, 0xfe, 0x70, 0x18, 0x4e, // VPSHUFHW YMM3, [RAX], 0x4E
        0xf4, // HLT
    ]);

    emu.load_code_bytes(&full_code);
    let data: Vec<u8> = vec![0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55];
    emu.maps.write_bytes_slice(ALIGNED_ADDR, &data);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm4_mem_custom_pattern() {
    let mut emu = emu64();
    let code = [0x48, 0xb8];
    let mut full_code = code.to_vec();
    full_code.extend_from_slice(&ALIGNED_ADDR.to_le_bytes());
    full_code.extend_from_slice(&[
        0xc5, 0xfe, 0x70, 0x20, 0x93, // VPSHUFHW YMM4, [RAX], 0x93
        0xf4, // HLT
    ]);

    emu.load_code_bytes(&full_code);
    let data: Vec<u8> = (0..32).map(|i| 0xFF - i).collect();
    emu.maps.write_bytes_slice(ALIGNED_ADDR, &data);
    emu.run(None).unwrap();
}

// ============================================================================
// Tests with various immediate patterns
// ============================================================================

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0x27() {
    let mut emu = emu64();
    // 0x27 = 00 10 01 11
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x27, // VPSHUFHW YMM0, YMM1, 0x27
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0x39() {
    let mut emu = emu64();
    // 0x39 = 00 11 10 01
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x39, // VPSHUFHW YMM0, YMM1, 0x39
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0x4B() {
    let mut emu = emu64();
    // 0x4B = 01 00 10 11
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x4b, // VPSHUFHW YMM0, YMM1, 0x4B
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0x72() {
    let mut emu = emu64();
    // 0x72 = 01 11 00 10
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x72, // VPSHUFHW YMM0, YMM1, 0x72
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0x8D() {
    let mut emu = emu64();
    // 0x8D = 10 00 11 01
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x8d, // VPSHUFHW YMM0, YMM1, 0x8D
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0x93() {
    let mut emu = emu64();
    // 0x93 = 10 01 00 11
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x93, // VPSHUFHW YMM0, YMM1, 0x93
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0xB1() {
    let mut emu = emu64();
    // 0xB1 = 10 11 00 01
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0xb1, // VPSHUFHW YMM0, YMM1, 0xB1
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0xC6() {
    let mut emu = emu64();
    // 0xC6 = 11 00 01 10
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0xc6, // VPSHUFHW YMM0, YMM1, 0xC6
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0xD8() {
    let mut emu = emu64();
    // 0xD8 = 11 01 10 00
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0xd8, // VPSHUFHW YMM0, YMM1, 0xD8
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

// ============================================================================
// Tests with duplicate patterns
// ============================================================================

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0x44() {
    let mut emu = emu64();
    // 0x44 = 01 00 01 00 (duplicate low pair)
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x44, // VPSHUFHW YMM0, YMM1, 0x44
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0xEE() {
    let mut emu = emu64();
    // 0xEE = 11 10 11 10 (duplicate high pair)
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0xee, // VPSHUFHW YMM0, YMM1, 0xEE
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0x50() {
    let mut emu = emu64();
    // 0x50 = 01 01 00 00
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x50, // VPSHUFHW YMM0, YMM1, 0x50
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_ymm0_ymm1_imm_0xFA() {
    let mut emu = emu64();
    // 0xFA = 11 11 10 10
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0xfa, // VPSHUFHW YMM0, YMM1, 0xFA
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

// ============================================================================
// Chained operations and edge cases
// ============================================================================

#[test]
fn test_vpshufhw_chain_operations() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x1b, // VPSHUFHW YMM0, YMM1, 0x1B
        0xc5, 0xfe, 0x70, 0xc0, 0x1b, // VPSHUFHW YMM0, YMM0, 0x1B (should restore)
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_same_register() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xc0, 0x4e, // VPSHUFHW YMM0, YMM0, 0x4E
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_all_extended_regs() {
    let mut emu = emu64();
    let code = [
        0xc4, 0x41, 0x7e, 0x70, 0xff, 0x27, // VPSHUFHW YMM15, YMM15, 0x27
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_mem_unaligned() {
    let mut emu = emu64();
    let code = [0x48, 0xb8];
    let mut full_code = code.to_vec();
    full_code.extend_from_slice(&(ALIGNED_ADDR + 1).to_le_bytes());
    full_code.extend_from_slice(&[
        0xc5, 0xfe, 0x70, 0x00, 0xe4, // VPSHUFHW YMM0, [RAX], 0xE4
        0xf4, // HLT
    ]);

    emu.load_code_bytes(&full_code);
    emu.maps.write_bytes_slice(ALIGNED_ADDR, &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_complex_pattern_1() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x6c, // VPSHUFHW YMM0, YMM1, 0x6C
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_complex_pattern_2() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x9e, // VPSHUFHW YMM0, YMM1, 0x9E
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_complex_pattern_3() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0x2d, // VPSHUFHW YMM0, YMM1, 0x2D
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_with_ymm9_ymm12() {
    let mut emu = emu64();
    let code = [
        0xc4, 0x41, 0x7e, 0x70, 0xcc, 0xb1, // VPSHUFHW YMM9, YMM12, 0xB1
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_mem_extended_reg() {
    let mut emu = emu64();
    let code = [0x48, 0xb8];
    let mut full_code = code.to_vec();
    full_code.extend_from_slice(&ALIGNED_ADDR.to_le_bytes());
    full_code.extend_from_slice(&[
        0xc4, 0x61, 0x7e, 0x70, 0x38, 0x93, // VPSHUFHW YMM15, [RAX], 0x93
        0xf4, // HLT
    ]);

    emu.load_code_bytes(&full_code);
    let data: Vec<u8> = (0..32).map(|i| i as u8).collect();
    emu.maps.write_bytes_slice(ALIGNED_ADDR, &data);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_alternating_pattern() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0xa5, // VPSHUFHW YMM0, YMM1, 0xA5
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}

#[test]
fn test_vpshufhw_low_words_unchanged() {
    let mut emu = emu64();
    let code = [
        0xc5, 0xfe, 0x70, 0xc1, 0xff, // VPSHUFHW YMM0, YMM1, 0xFF
        0xf4, // HLT
    ];
    emu.load_code_bytes(&code);
    emu.run(None).unwrap();
}