|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Converts Renode log to execution trace for RISC-V DV |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +import os |
| 8 | +import re |
| 9 | +import sys |
| 10 | +import logging |
| 11 | + |
| 12 | +sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) |
| 13 | + |
| 14 | +from riscv_trace_csv import * |
| 15 | +from lib import * |
| 16 | + |
| 17 | +# ============================================================================= |
| 18 | + |
| 19 | +GPR_NAMES = [ |
| 20 | + ("x0", "zero"), |
| 21 | + ("x1", "ra"), |
| 22 | + ("x2", "sp"), |
| 23 | + ("x3", "gp"), |
| 24 | + ("x4", "tp"), |
| 25 | + ("x5", "t0"), |
| 26 | + ("x6", "t1"), |
| 27 | + ("x7", "t2"), |
| 28 | + ("x8", "s0"), |
| 29 | + ("x9", "s1"), |
| 30 | + ("x10", "a0"), |
| 31 | + ("x11", "a1"), |
| 32 | + ("x12", "a2"), |
| 33 | + ("x13", "a3"), |
| 34 | + ("x14", "a4"), |
| 35 | + ("x15", "a5"), |
| 36 | + ("x16", "a6"), |
| 37 | + ("x17", "a7"), |
| 38 | + ("x18", "s2"), |
| 39 | + ("x19", "s3"), |
| 40 | + ("x20", "s4"), |
| 41 | + ("x21", "s5"), |
| 42 | + ("x22", "s6"), |
| 43 | + ("x23", "s7"), |
| 44 | + ("x24", "s8"), |
| 45 | + ("x25", "s9"), |
| 46 | + ("x26", "s10"), |
| 47 | + ("x27", "s11"), |
| 48 | + ("x28", "t3"), |
| 49 | + ("x29", "t4"), |
| 50 | + ("x30", "t5"), |
| 51 | + ("x31", "t6"), |
| 52 | +] |
| 53 | + |
| 54 | +# ============================================================================= |
| 55 | + |
| 56 | + |
| 57 | +def process_renode_sim_log(log_name, csv_name): |
| 58 | + """ |
| 59 | + Converts a Renode trace log to CSV format |
| 60 | + """ |
| 61 | + |
| 62 | + # Build lookups |
| 63 | + gpr_to_name = {m[0]: m[1] for m in GPR_NAMES} |
| 64 | + known_gpr = {m[0].upper() for m in GPR_NAMES} |
| 65 | + |
| 66 | + # FIXME: We need a previous PC each time. Assume its value for the first |
| 67 | + # entry. |
| 68 | + prev_pc = "80000000" |
| 69 | + |
| 70 | + # FIXME: Assume initial state of all GPR set to 0 |
| 71 | + state = {m[0].upper(): "0" for m in GPR_NAMES} |
| 72 | + trace = [] |
| 73 | + |
| 74 | + with open(log_name, "r") as fp: |
| 75 | + for line in fp: |
| 76 | + |
| 77 | + line = line.strip() |
| 78 | + if not line: |
| 79 | + continue |
| 80 | + |
| 81 | + # Skip non-regdump |
| 82 | + if not line.startswith("REGDUMP:"): |
| 83 | + continue |
| 84 | + |
| 85 | + # Decode state |
| 86 | + fields = line.replace("REGDUMP:", "").split(",") |
| 87 | + regs = {fields[i]: fields[i+1] for i in range(0, len(fields), 2)} |
| 88 | + |
| 89 | + # Compute state difference |
| 90 | + diff = {r: regs[r] for r in known_gpr \ |
| 91 | + if r in state and r in regs and state[r] != regs[r]} |
| 92 | + state = regs |
| 93 | + |
| 94 | + # Format the entry |
| 95 | + entry = RiscvInstructionTraceEntry() |
| 96 | + entry.pc = prev_pc |
| 97 | + entry.binary = "0" |
| 98 | + entry.operand = "" |
| 99 | + entry.mode = "0" |
| 100 | + |
| 101 | + # GPRs |
| 102 | + for x in range(32): |
| 103 | + name = "X{}".format(x) |
| 104 | + if name in diff: |
| 105 | + lname = name.lower() |
| 106 | + value = int(diff[name], 16) |
| 107 | + entry.gpr.append("{}:{:08x}".format(gpr_to_name[lname], value)) |
| 108 | + |
| 109 | + # CSRs |
| 110 | + # TODO: |
| 111 | + |
| 112 | + # Add only if there is a GPR/CSR change |
| 113 | + if entry.gpr or entry.csr: |
| 114 | + trace.append(entry) |
| 115 | + |
| 116 | + prev_pc = state["PC"] |
| 117 | + |
| 118 | + return trace |
| 119 | + |
| 120 | + |
| 121 | +def write_csv(file_name, data): |
| 122 | + """ |
| 123 | + Writes the trace to CSV |
| 124 | + """ |
| 125 | + |
| 126 | + with open(file_name, "w") as fp: |
| 127 | + |
| 128 | + writer = RiscvInstructionTraceCsv(fp) |
| 129 | + writer.start_new_trace() |
| 130 | + |
| 131 | + for entry in data: |
| 132 | + writer.write_trace_entry(entry) |
| 133 | + |
| 134 | +# ============================================================================ |
| 135 | + |
| 136 | + |
| 137 | +def main(): |
| 138 | + # Parse input arguments |
| 139 | + parser = argparse.ArgumentParser() |
| 140 | + parser.add_argument("--log", type=str, help="Input Renode simulation log") |
| 141 | + parser.add_argument("--csv", type=str, help="Output trace CSV file") |
| 142 | + parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", |
| 143 | + help="Verbose logging") |
| 144 | + parser.set_defaults(verbose=False) |
| 145 | + |
| 146 | + args = parser.parse_args() |
| 147 | + setup_logging(args.verbose) |
| 148 | + |
| 149 | + # Process Renode log |
| 150 | + trace = process_renode_sim_log(args.log, args.csv) |
| 151 | + # Write CSV |
| 152 | + write_csv(args.csv, trace) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + main() |
0 commit comments