rvsim-core 2.0.0

A cycle-level RISC-V 64-bit system simulator.
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
//! # Hart Trap Handling Tests
//!
//! This module contains unit tests for trap and exception handling,
//! including trap dispatch and context saving.

use crate::common::PhysAddr;
use crate::config::Config;
use crate::isa::privileged::{PrivilegeMode, Trap};
use crate::isa::reg::RegIdx;
use crate::system::SystemState;

fn create_test_cpu() -> SystemState {
    let config = Config::default();
    let mut state = SystemState::build(&config, "");
    state.direct_mode = false;
    state
}

#[test]
fn test_trap_clears_load_reservation() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.set_reservation(PhysAddr::new(0x8000_0000));

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    assert!(!state.check_reservation(PhysAddr::new(0x8000_0000)));
}

#[test]
fn test_trap_direct_mode_illegal_instruction_zero_exits() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.direct_mode = true;
    state.exit_signal.store(u64::MAX, std::sync::atomic::Ordering::Relaxed);

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    assert_eq!(state.check_exit(), Some(0));
}

#[test]
fn test_trap_direct_mode_other_exceptions_set_exit_code_1() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.direct_mode = true;
    state.exit_signal.store(u64::MAX, std::sync::atomic::Ordering::Relaxed);

    state.trap(&Trap::LoadAddressMisaligned(0x8000_0001), state.hart.pc);

    assert_eq!(state.check_exit(), Some(1));
}

#[test]
fn test_trap_direct_mode_ecall_from_umode_processed() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.direct_mode = true;
    state.hart.privilege = PrivilegeMode::User;
    state.exit_signal.store(u64::MAX, std::sync::atomic::Ordering::Relaxed);
    state.hart.csrs.mtvec = 0x8000_0000;

    // ECALL in direct mode should be processed normally (not treated as fatal)
    state.trap(&Trap::EnvironmentCallFromUMode, state.hart.pc);

    // Exit code should remain None (trap is processed, not fatal)
}

#[test]
fn test_trap_sets_mcause_without_interrupt_bit_for_exceptions() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    let mcause = state.hart.csrs.mcause;
    // Exceptions should not have interrupt bit set (bit 63)
    assert_eq!(mcause & (1u64 << 63), 0);
}

#[test]
fn test_trap_exceptions_dont_set_interrupt_bit() {
    let exceptions = vec![
        Trap::InstructionAddressMisaligned(0),
        Trap::InstructionAccessFault(0),
        Trap::IllegalInstruction(0),
        Trap::Breakpoint(0),
        Trap::LoadAddressMisaligned(0),
        Trap::LoadAccessFault(0),
        Trap::StoreAddressMisaligned(0),
        Trap::StoreAccessFault(0),
        Trap::EnvironmentCallFromUMode,
        Trap::EnvironmentCallFromSMode,
        Trap::EnvironmentCallFromMMode,
    ];

    for exception in exceptions {
        let mut sys = create_test_cpu();
        let mut state = sys.core_ctx(0);
        state.hart.privilege = PrivilegeMode::Machine;
        state.hart.csrs.mtvec = 0x8000_0000;

        state.trap(&exception, state.hart.pc);

        // Exceptions should not have interrupt bit set
        assert_eq!(state.hart.csrs.mcause & (1u64 << 63), 0);
    }
}

#[test]
fn test_trap_ecall_from_all_modes() {
    let ecalls = vec![
        Trap::EnvironmentCallFromUMode,
        Trap::EnvironmentCallFromSMode,
        Trap::EnvironmentCallFromMMode,
    ];

    for ecall in ecalls {
        let mut sys = create_test_cpu();
        let mut state = sys.core_ctx(0);
        state.hart.privilege = PrivilegeMode::Machine;
        state.hart.csrs.mtvec = 0x8000_0000;

        state.trap(&ecall, state.hart.pc);

        // Should not have interrupt bit set (ECALL is an exception, not interrupt)
        assert_eq!(state.hart.csrs.mcause & (1u64 << 63), 0);
    }
}

#[test]
fn test_trap_page_faults() {
    let page_faults = vec![
        Trap::InstructionPageFault(0x1000_0000),
        Trap::LoadPageFault(0x2000_0000),
        Trap::StorePageFault(0x3000_0000),
    ];

    for fault_trap in page_faults {
        let mut sys = create_test_cpu();
        let mut state = sys.core_ctx(0);
        state.hart.privilege = PrivilegeMode::Machine;
        state.hart.csrs.mtvec = 0x8000_0000;

        state.trap(&fault_trap, state.hart.pc);

        // Should not have interrupt bit set (page faults are exceptions)
        assert_eq!(state.hart.csrs.mcause & (1u64 << 63), 0);
    }
}

#[test]
fn test_trap_double_fault_detection() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    let handler_pc = 0x8000_0000;
    state.hart.csrs.mtvec = handler_pc;

    // A trap whose EPC equals the handler address is legitimate (not a double fault).
    state.trap(&Trap::IllegalInstruction(0), handler_pc);
    assert_eq!(state.check_exit(), None);
    assert_eq!(state.hart.pc, handler_pc); // dispatched to M-mode handler
}

#[test]
fn test_trap_interrupts_set_interrupt_bit() {
    let interrupts = vec![
        Trap::UserSoftwareInterrupt,
        Trap::SupervisorSoftwareInterrupt,
        Trap::MachineSoftwareInterrupt,
        Trap::SupervisorTimerInterrupt,
        Trap::MachineTimerInterrupt,
        Trap::UserExternalInterrupt,
        Trap::SupervisorExternalInterrupt,
        Trap::MachineExternalInterrupt,
    ];

    for interrupt in interrupts {
        let mut sys = create_test_cpu();
        let mut state = sys.core_ctx(0);
        state.hart.privilege = PrivilegeMode::Machine;
        state.hart.csrs.mtvec = 0x8000_0000;
        state.hart.pc = 0x8000_1000; // Different from trap handler

        state.trap(&interrupt, state.hart.pc);

        // Interrupts should have interrupt bit set (bit 63)
        assert_ne!(state.hart.csrs.mcause & (1u64 << 63), 0);
    }
}

#[test]
fn test_trap_machine_timer_interrupt() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    state.trap(&Trap::MachineTimerInterrupt, state.hart.pc);

    // Should have interrupt bit set
    assert_ne!(state.hart.csrs.mcause & (1u64 << 63), 0);
}

#[test]
fn test_trap_supervisor_timer_interrupt() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.csrs.stvec = 0x8000_1000;
    state.hart.csrs.mideleg = 1 << 5; // Delegate supervisor timer interrupts
    state.hart.pc = 0x8000_2000; // Different from trap handler

    state.trap(&Trap::SupervisorTimerInterrupt, state.hart.pc);

    // Should have delegated to S-mode
    assert_eq!(state.hart.privilege, PrivilegeMode::Supervisor);
}

#[test]
fn test_trap_machine_software_interrupt() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    state.trap(&Trap::MachineSoftwareInterrupt, state.hart.pc);

    // Should have interrupt bit set
    assert_ne!(state.hart.csrs.mcause & (1u64 << 63), 0);
}

#[test]
fn test_trap_supervisor_software_interrupt() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.csrs.stvec = 0x8000_1000;
    state.hart.csrs.mideleg = 1 << 1; // Delegate supervisor software interrupts
    state.hart.pc = 0x8000_2000; // Different from trap handler

    state.trap(&Trap::SupervisorSoftwareInterrupt, state.hart.pc);

    assert_eq!(state.hart.privilege, PrivilegeMode::Supervisor);
}

#[test]
fn test_trap_machine_external_interrupt() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    state.trap(&Trap::MachineExternalInterrupt, state.hart.pc);

    // Should have interrupt bit set
    assert_ne!(state.hart.csrs.mcause & (1u64 << 63), 0);
}

#[test]
fn test_trap_supervisor_external_interrupt() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.csrs.stvec = 0x8000_1000;
    state.hart.csrs.mideleg = 1 << 9; // Delegate supervisor external interrupts
    state.hart.pc = 0x8000_2000; // Different from trap handler

    state.trap(&Trap::SupervisorExternalInterrupt, state.hart.pc);

    assert_eq!(state.hart.privilege, PrivilegeMode::Supervisor);
}

#[test]
fn test_trap_user_software_interrupt() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.stvec = 0x8000_0000;

    state.trap(&Trap::UserSoftwareInterrupt, state.hart.pc);

    // User mode traps typically get handled at higher privilege
}

#[test]
fn test_trap_user_external_interrupt() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.stvec = 0x8000_0000;

    state.trap(&Trap::UserExternalInterrupt, state.hart.pc);

    // User mode external interrupt handling
}

#[test]
fn test_trap_delegation_to_supervisor_with_medeleg() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.csrs.stvec = 0x8000_1000;
    // Delegate instruction page faults (exception code 12) to S-mode
    state.hart.csrs.medeleg = 1 << 12;

    let old_pc = state.hart.pc;
    state.trap(&Trap::InstructionPageFault(0x1000), old_pc);

    // Should have delegated to S-mode
    assert_eq!(state.hart.csrs.scause & !CAUSE_INTERRUPT_BIT, 12);
    assert_eq!(state.hart.csrs.sepc, old_pc);
}

#[test]
fn test_trap_delegation_to_supervisor_with_mideleg() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.csrs.stvec = 0x8000_1000;
    // Delegate supervisor software interrupts (interrupt code 1) to S-mode
    state.hart.csrs.mideleg = 1 << 1;

    state.trap(&Trap::SupervisorSoftwareInterrupt, state.hart.pc);

    // Should have delegated to S-mode
    assert_ne!(state.hart.csrs.scause & CAUSE_INTERRUPT_BIT, 0);
}

#[test]
fn test_trap_no_delegation_when_medeleg_not_set() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.csrs.stvec = 0; // STVEC not set
    state.hart.csrs.medeleg = 0; // No delegation
    state.hart.pc = 0x8000_2000; // Different from trap handler

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    // Should NOT have delegated to S-mode, stays in M-mode
    assert_eq!(state.hart.privilege, PrivilegeMode::Machine);
}

#[test]
fn test_trap_delegation_only_from_lower_privilege() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.csrs.stvec = 0x8000_1000;
    // Enable delegation
    state.hart.csrs.medeleg = 1 << 2; // Delegate illegal instruction

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    // Machine mode traps should NOT delegate even with medeleg set
    assert_eq!(state.hart.privilege, PrivilegeMode::Machine);
}

#[test]
fn test_trap_user_mode_no_delegation_without_medeleg() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.csrs.stvec = 0x8000_1000; // STVEC is set but irrelevant
    state.hart.csrs.medeleg = 0; // No delegation in medeleg

    // Without medeleg bit set, trap must go to M-mode per spec
    state.trap(&Trap::LoadAddressMisaligned(0x1001), state.hart.pc);

    assert_eq!(state.hart.privilege, PrivilegeMode::Machine);
    assert_eq!(state.hart.pc, 0x8000_0000);
}

#[test]
fn test_trap_vectored_mode_direct_for_exceptions() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    let base = 0x8000_0000;
    state.hart.csrs.mtvec = base | 1; // Vectored mode (bit 0 = 1)

    let old_pc = state.hart.pc;
    state.trap(&Trap::IllegalInstruction(0), old_pc);

    // Exceptions should use base address (no offset)
    assert_eq!(state.hart.pc, base);
}

#[test]
fn test_trap_vectored_mode_offset_for_interrupts() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    let base = 0x8000_0000;
    state.hart.csrs.mtvec = base | 1; // Vectored mode

    state.trap(&Trap::MachineTimerInterrupt, state.hart.pc);

    // Machine timer interrupt (code 7) should offset by 4*7 = 28
    assert_eq!(state.hart.pc, base + 28);
}

#[test]
fn test_trap_direct_mode_no_offset() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    let base = 0x8000_0000;
    state.hart.csrs.mtvec = base; // Direct mode (bit 0 = 0)

    state.trap(&Trap::MachineTimerInterrupt, state.hart.pc);

    // Direct mode should use base address (no offset)
    assert_eq!(state.hart.pc, base);
}

#[test]
fn test_trap_supervisor_vectored_mode() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.mtvec = 0x8000_0000;
    let base = 0x8000_1000;
    state.hart.csrs.stvec = base | 1; // Vectored mode
    state.hart.csrs.mideleg = 1 << 5; // Delegate supervisor timer interrupts
    state.hart.pc = 0x8000_2000; // Different from trap handler

    state.trap(&Trap::SupervisorTimerInterrupt, state.hart.pc);

    // Supervisor timer interrupt (code 5) should offset by 4*5 = 20
    assert_eq!(state.hart.pc, base + 20);
}

#[test]
fn test_trap_tval_for_address_exceptions() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    let fault_addr = 0x1234_5678;
    state.trap(&Trap::LoadAddressMisaligned(fault_addr), state.hart.pc);

    assert_eq!(state.hart.csrs.mtval, fault_addr);
}

#[test]
fn test_trap_tval_for_page_faults() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    let fault_addr = 0xdead_beef;
    state.trap(&Trap::StorePageFault(fault_addr), state.hart.pc);

    assert_eq!(state.hart.csrs.mtval, fault_addr);
}

#[test]
fn test_trap_tval_for_illegal_instruction() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    let bad_instr = 0xdeadbeef;
    state.trap(&Trap::IllegalInstruction(bad_instr), state.hart.pc);

    assert_eq!(state.hart.csrs.mtval, bad_instr as u64);
}

#[test]
fn test_trap_tval_zero_for_ecall() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    state.trap(&Trap::EnvironmentCallFromMMode, state.hart.pc);

    // ECALL should set tval to 0
    assert_eq!(state.hart.csrs.mtval, 0);
}

#[test]
fn test_trap_stval_on_delegation() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.stvec = 0x8000_1000;
    state.hart.csrs.medeleg = 1 << 13; // Delegate load page faults
    state.hart.pc = 0x8000_2000; // Different from trap handler

    let fault_addr = 0xcafe_babe;
    state.trap(&Trap::LoadPageFault(fault_addr), state.hart.pc);

    assert_eq!(state.hart.csrs.stval, fault_addr);
}

#[test]
fn test_trap_saves_previous_privilege_in_mpp() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Supervisor;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    // mstatus.MPP should be set to Supervisor (0b01)
    assert_eq!(state.hart.csrs.mstatus >> 11 & 0b11, 1);
}

#[test]
fn test_trap_disables_mie_and_saves_to_mpie() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler
    // Enable MIE (bit 3)
    state.hart.csrs.mstatus = 1 << 3;

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    // MIE should be disabled
    assert_eq!(state.hart.csrs.mstatus & (1 << 3), 0);
    // MPIE (bit 7) should be set from MIE
    assert_ne!(state.hart.csrs.mstatus & (1 << 7), 0);
}

#[test]
fn test_trap_saves_previous_privilege_in_spp_on_delegation() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.stvec = 0x8000_1000;
    state.hart.csrs.medeleg = 1 << 2; // Delegate illegal instruction
    state.hart.pc = 0x8000_2000; // Different from trap handler

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    // mstatus.SPP should be cleared for User (bit 8 = 0)
    assert_eq!(state.hart.csrs.mstatus >> 8 & 1, 0);
}

#[test]
fn test_trap_disables_sie_on_delegation() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.stvec = 0x8000_1000;
    state.hart.csrs.medeleg = 1 << 2; // Delegate illegal instruction
    state.hart.pc = 0x8000_2000; // Different from trap handler

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    // Should have delegated to S-mode
    assert_eq!(state.hart.privilege, PrivilegeMode::Supervisor);
    // SEPC should be set
    assert_eq!(state.hart.csrs.sepc, 0x8000_2000);
}

#[test]
fn test_trap_requested_trap_custom_code() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    let custom_code = 42;
    state.trap(&Trap::RequestedTrap(custom_code), state.hart.pc);

    assert_eq!(state.hart.csrs.mcause, custom_code);
}

#[test]
fn test_trap_double_fault_trap_variant() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    state.trap(&Trap::DoubleFault(0x1234), state.hart.pc);

    // DoubleFault should map to hardware error exception
}

#[test]
fn test_trap_breakpoint() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    state.trap(&Trap::Breakpoint(0), state.hart.pc);

    // Breakpoint should be handled as exception
    assert_eq!(state.hart.csrs.mcause & !CAUSE_INTERRUPT_BIT, 3);
}

#[test]
fn test_trap_all_access_faults() {
    let faults = vec![
        (Trap::InstructionAccessFault(0x1000), 1),
        (Trap::LoadAccessFault(0x2000), 5),
        (Trap::StoreAccessFault(0x3000), 7),
    ];

    for (fault, expected_code) in faults {
        let mut sys = create_test_cpu();
        let mut state = sys.core_ctx(0);
        state.hart.privilege = PrivilegeMode::Machine;
        state.hart.csrs.mtvec = 0x8000_0000;
        state.hart.pc = 0x8000_1000; // Different from trap handler

        state.trap(&fault, state.hart.pc);

        assert_eq!(state.hart.csrs.mcause, expected_code);
    }
}

#[test]
fn test_trap_all_misaligned() {
    let misaligned = vec![
        (Trap::InstructionAddressMisaligned(0x1001), 0),
        (Trap::LoadAddressMisaligned(0x2001), 4),
        (Trap::StoreAddressMisaligned(0x3001), 6),
    ];

    for (trap, expected_code) in misaligned {
        let mut sys = create_test_cpu();
        let mut state = sys.core_ctx(0);
        state.hart.privilege = PrivilegeMode::Machine;
        state.hart.csrs.mtvec = 0x8000_0000;
        state.hart.pc = 0x8000_1000; // Different from trap handler

        state.trap(&trap, state.hart.pc);

        assert_eq!(state.hart.csrs.mcause, expected_code);
    }
}

#[test]
fn test_trap_preserves_registers() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    state.hart.pc = 0x8000_1000; // Different from trap handler

    // Set up some register state
    state.hart.regs.write(RegIdx::new(1), 0x1234);
    state.hart.regs.write(RegIdx::new(2), 0x5678);

    state.trap(&Trap::IllegalInstruction(0), state.hart.pc);

    // Registers should be preserved across trap
    assert_eq!(state.hart.regs.read(RegIdx::new(1)), 0x1234);
    assert_eq!(state.hart.regs.read(RegIdx::new(2)), 0x5678);
}

#[test]
fn test_trap_updates_mepc_correctly() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::Machine;
    state.hart.csrs.mtvec = 0x8000_0000;
    let trap_pc = 0x8000_1234;

    state.trap(&Trap::IllegalInstruction(0), trap_pc);

    assert_eq!(state.hart.csrs.mepc, trap_pc);
}

#[test]
fn test_trap_updates_sepc_on_delegation() {
    let mut sys = create_test_cpu();
    let mut state = sys.core_ctx(0);
    state.hart.privilege = PrivilegeMode::User;
    state.hart.csrs.stvec = 0x8000_1000;
    state.hart.csrs.medeleg = 1 << 2; // Delegate illegal instruction
    let trap_pc = 0x8000_5678;

    state.trap(&Trap::IllegalInstruction(0), trap_pc);

    assert_eq!(state.hart.csrs.sepc, trap_pc);
}

use crate::isa::privileged::cause::CAUSE_INTERRUPT_BIT;