| 1086 | ri(RealInstruction::Addi(Addi::new(17, 0, 64))), // a7 = 64 (write) |
| 1087 | ri(RealInstruction::Ecall(Ecall::new())), // syscall write |
| 1088 | // exit(0) |
| 1089 | ri(RealInstruction::Addi(Addi::new(17, 0, 93))), // a7 = 93 |
| 1090 | ri(RealInstruction::Addi(Addi::new(10, 0, 0))), // a0 = 0 |
| 1091 | ri(RealInstruction::Ecall(Ecall::new())), |
| 1092 | ]) |
| 1093 | .expect("assembly failed"); |
| 1094 | |
| 1095 | let mut vm = VirtualMachine::new(&assembled); |
| 1096 | let result = vm.run(100_000); |
| 1097 | assert_eq!(result.uart_output, "hi\n", "UART output mismatch"); |
| 1098 | assert!( |
| 1099 | matches!(result.termination, RunOutcome::Halted(0)), |
| 1100 | "expected Halted(0), got {:?}", |
| 1101 | result.termination |
| 1102 | ); |
| 1103 | } |
| 1104 | |
| 1105 | #[test] |
| 1106 | fn test_div_by_zero() { |
| 1107 | // Unsigned divide by zero must yield u64::MAX. The result is parked in s0 |
| 1108 | // because the ROM sys_exit handler uses t0-t6 as scratch. |
| 1109 | let (vm, _outcome) = assemble_and_run(vec![ |
| 1110 | ri(RealInstruction::Addi(Addi::new(11, 0, 42))), // a1 = 42 |
| 1111 | ri(RealInstruction::Divu(Divu::new(10, 11, 0))), // a0 = divu(42, 0) = u64::MAX |