|
| 1 | +#include "riscv_callbacks.h" |
| 2 | +#include "riscv_config.h" |
| 3 | +#include "riscv_platform_impl.h" |
| 4 | +#include <stdlib.h> |
| 5 | +#include <vector> |
| 6 | +#include <inttypes.h> |
| 7 | + |
| 8 | +void print_lbits_hex(lbits val, int length = 0) |
| 9 | +{ |
| 10 | + if (length == 0) { |
| 11 | + length = val.len; |
| 12 | + } |
| 13 | + std::vector<uint8_t> data(length); |
| 14 | + mpz_export(data.data(), nullptr, -1, 1, 0, 0, *val.bits); |
| 15 | + for (int i = length - 1; i >= 0; --i) { |
| 16 | + fprintf(trace_log, "%02" PRIX8, data[i]); |
| 17 | + } |
| 18 | + fprintf(trace_log, "\n"); |
| 19 | +} |
| 20 | + |
| 21 | +// Implementations of default callbacks for trace printing and RVFI. |
| 22 | +// The model assumes that these functions do not change the state of the model. |
| 23 | +unit mem_write_callback(uint64_t addr, uint64_t width, lbits value) |
| 24 | +{ |
| 25 | + if (config_print_mem_access) { |
| 26 | + fprintf(trace_log, "mem[0x%016" PRIX64 "] <- 0x", addr); |
| 27 | + print_lbits_hex(value, width); |
| 28 | + } |
| 29 | +#ifdef RVFI_DII |
| 30 | + if (config_enable_rvfi) { |
| 31 | + zrvfi_write(addr, width, value); |
| 32 | + } |
| 33 | +#endif |
| 34 | + return UNIT; |
| 35 | +} |
| 36 | + |
| 37 | +unit mem_read_callback(const char *type, uint64_t addr, uint64_t width, |
| 38 | + lbits value) |
| 39 | +{ |
| 40 | + if (config_print_mem_access) { |
| 41 | + fprintf(trace_log, "mem[%s,0x%016" PRIX64 "] -> 0x", type, addr); |
| 42 | + print_lbits_hex(value, width); |
| 43 | + } |
| 44 | +#ifdef RVFI_DII |
| 45 | + if (config_enable_rvfi) { |
| 46 | + sail_int len; |
| 47 | + CREATE(sail_int)(&len); |
| 48 | + CONVERT_OF(sail_int, mach_int)(&len, width); |
| 49 | + zrvfi_read(addr, len, value); |
| 50 | + KILL(sail_int)(&len); |
| 51 | + } |
| 52 | +#endif |
| 53 | + return UNIT; |
| 54 | +} |
| 55 | + |
| 56 | +unit mem_exception_callback(uint64_t addr, uint64_t num_of_exception) |
| 57 | +{ |
| 58 | + (void)num_of_exception; |
| 59 | +#ifdef RVFI_DII |
| 60 | + if (config_enable_rvfi) { |
| 61 | + zrvfi_mem_exception(addr); |
| 62 | + } |
| 63 | +#else |
| 64 | + (void)addr; |
| 65 | +#endif |
| 66 | + return UNIT; |
| 67 | +} |
| 68 | + |
| 69 | +unit xreg_write_callback(unsigned reg, uint64_t value) |
| 70 | +{ |
| 71 | + if (config_print_reg) { |
| 72 | + fprintf(trace_log, "x%d <- 0x%016" PRIX64 "\n", reg, value); |
| 73 | + } |
| 74 | +#ifdef RVFI_DII |
| 75 | + if (config_enable_rvfi) { |
| 76 | + zrvfi_wX(reg, value); |
| 77 | + } |
| 78 | +#endif |
| 79 | + return UNIT; |
| 80 | +} |
| 81 | + |
| 82 | +unit freg_write_callback(unsigned reg, uint64_t value) |
| 83 | +{ |
| 84 | + // TODO: will only print bits; should we print in floating point format? |
| 85 | + if (config_print_reg) { |
| 86 | + fprintf(trace_log, "f%d <- 0x%016" PRIX64 "\n", reg, value); |
| 87 | + } |
| 88 | + return UNIT; |
| 89 | +} |
| 90 | + |
| 91 | +unit csr_write_callback(sail_string csr_name, unsigned reg, uint64_t value) |
| 92 | +{ |
| 93 | + if (config_print_reg) { |
| 94 | + fprintf(trace_log, "CSR %s (0x%03X) <- 0x%016" PRIX64 "\n", csr_name, reg, |
| 95 | + value); |
| 96 | + } |
| 97 | + return UNIT; |
| 98 | +} |
| 99 | + |
| 100 | +unit csr_read_callback(sail_string csr_name, unsigned reg, uint64_t value) |
| 101 | +{ |
| 102 | + if (config_print_reg) { |
| 103 | + fprintf(trace_log, "CSR %s (0x%03X) -> 0x%016" PRIX64 "\n", csr_name, reg, |
| 104 | + value); |
| 105 | + } |
| 106 | + return UNIT; |
| 107 | +} |
| 108 | + |
| 109 | +unit vreg_write_callback(unsigned reg, lbits value) |
| 110 | +{ |
| 111 | + if (config_print_reg) { |
| 112 | + fprintf(trace_log, "v%d <- ", reg); |
| 113 | + print_lbits_hex(value); |
| 114 | + } |
| 115 | + return UNIT; |
| 116 | +} |
| 117 | + |
| 118 | +unit pc_write_callback(uint64_t value) |
| 119 | +{ |
| 120 | + (void)value; |
| 121 | + return UNIT; |
| 122 | +} |
| 123 | + |
| 124 | +unit trap_callback(unit) |
| 125 | +{ |
| 126 | +#ifdef RVFI_DII |
| 127 | + if (config_enable_rvfi) { |
| 128 | + zrvfi_trap(); |
| 129 | + } |
| 130 | +#endif |
| 131 | + return UNIT; |
| 132 | +} |
0 commit comments