Hazard3/test/sim/sw_testcases/load_misalign_halfword.c

69 lines
2.3 KiB
C

#include "tb_cxxrtl_io.h"
#include "hazard3_csr.h"
/*EXPECTED-OUTPUT***************************************************************
Load halfword signed, 1 byte offset
-> exception, mcause = 4
Result: 00000000
Load halfword signed, 3 byte offset
-> exception, mcause = 4
Result: 00000000
Load halfword signed aligned (sanity check)
Result: ffffcdef
Result: 00001234
Load halfword unsigned, 1 byte offset
-> exception, mcause = 4
Result: 00000000
Load halfword unsigned, 3 byte offset
-> exception, mcause = 4
Result: 00000000
Load halfword unsigned aligned (sanity check)
Result: 0000cdef
Result: 00001234
*******************************************************************************/
int main() {
volatile uint32_t target_word = 0x1234cdefu;
volatile uint32_t result_word = 0;
tb_puts("Load halfword signed, 1 byte offset\n");
asm volatile("lh %0, 1(%1)" : "+r" (result_word) : "r" (&target_word));
tb_printf("Result: %08x\n", result_word);
tb_puts("Load halfword signed, 3 byte offset\n");
asm volatile("lh %0, 3(%1)" : "+r" (result_word) : "r" (&target_word));
tb_printf("Result: %08x\n", result_word);
tb_puts("Load halfword signed aligned (sanity check)\n");
asm volatile("lh %0, 0(%1)" : "+r" (result_word) : "r" (&target_word));
tb_printf("Result: %08x\n", result_word);
asm volatile("lh %0, 2(%1)" : "+r" (result_word) : "r" (&target_word));
tb_printf("Result: %08x\n", result_word);
result_word = 0;
tb_puts("Load halfword unsigned, 1 byte offset\n");
asm volatile("lhu %0, 1(%1)" : "+r" (result_word) : "r" (&target_word));
tb_printf("Result: %08x\n", result_word);
tb_puts("Load halfword unsigned, 3 byte offset\n");
asm volatile("lhu %0, 3(%1)" : "+r" (result_word) : "r" (&target_word));
tb_printf("Result: %08x\n", result_word);
tb_puts("Load halfword unsigned aligned (sanity check)\n");
asm volatile("lhu %0, 0(%1)" : "+r" (result_word) : "r" (&target_word));
tb_printf("Result: %08x\n", result_word);
asm volatile("lhu %0, 2(%1)" : "+r" (result_word) : "r" (&target_word));
tb_printf("Result: %08x\n", result_word);
return 0;
}
void __attribute__((interrupt)) handle_exception() {
tb_printf("-> exception, mcause = %u\n", read_csr(mcause));
write_csr(mcause, 0);
if (*(uint16_t*)read_csr(mepc) & 0x3 == 0x3) {
write_csr(mepc, read_csr(mepc) + 4);
}
else {
write_csr(mepc, read_csr(mepc) + 2);
}
}