Skip to content

Commit f9b9392

Browse files
committed
feat(trace): implement itrace
1 parent 665f6b6 commit f9b9392

File tree

6 files changed

+62
-18
lines changed

6 files changed

+62
-18
lines changed

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@ all:
77
@echo "-------Build Test-------"
88
@$(MAKE) -C test T=$(T)
99
@echo "-------Start Simulation-------"
10-
@$(SIM) $(TARGET) -b
10+
@$(SIM) $(TARGET) --batch
1111

1212
debug:
1313
@echo "-------Build Simulator-------"
1414
@$(MAKE) -C sim
1515
@echo "-------Build Test-------"
1616
@$(MAKE) -C test T=$(T)
1717
@echo "-------Start Debugging-------"
18-
@$(SIM) $(TARGET) -d
18+
@$(SIM) $(TARGET) --debug
19+
20+
itrace:
21+
@echo "-------Build Simulator-------"
22+
@$(MAKE) -C sim
23+
@echo "-------Build Test-------"
24+
@$(MAKE) -C test T=$(T)
25+
@echo "-------Start Debugging-------"
26+
@$(SIM) $(TARGET) --itrace
1927

2028
unit_test:
2129
@echo "------- Building and Running Simulator Unit Tests -------"

sim/include/disasm.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef DISASM_H
2+
#define DISASM_H
3+
4+
#include <llvm-c/Target.h>
5+
#include <llvm-c/Disassembler.h>
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include <stdlib.h>
9+
10+
LLVMDisasmContextRef init_disasm(const char *triple);
11+
void disassemble_inst(LLVMDisasmContextRef disasm_ctx, uint8_t *bytes, int len, uint64_t pc, char *out, size_t out_len);
12+
void cleanup_disasm(LLVMDisasmContextRef disasm_ctx);
13+
14+
void init_llvm_disassembler();
15+
void cleanup_llvm_disassembler();
16+
17+
#endif

sim/src/cpu.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include <common.h>
22
#include <decode.h>
33
#include <memory.h>
4+
#include <disasm.h>
45

6+
extern int itrace_enabled;
7+
extern LLVMDisasmContextRef disasm_ctx;
58
CPU_state cpu = {};
69
static int running = 1;
710

@@ -17,6 +20,16 @@ void exec_once(){
1720
s.pc = cpu.pc;
1821
s.inst = inst_fetch(s.pc);
1922
s.snpc = s.pc + 4;
23+
if (itrace_enabled && disasm_ctx) {
24+
char asm_buf[128];
25+
uint8_t bytes[4];
26+
bytes[0] = s.inst & 0xFF;
27+
bytes[1] = (s.inst >> 8) & 0xFF;
28+
bytes[2] = (s.inst >> 16) & 0xFF;
29+
bytes[3] = (s.inst >> 24) & 0xFF;
30+
disassemble_inst(disasm_ctx, bytes, 4, s.pc, asm_buf, sizeof(asm_buf));
31+
printf("\33[1;34m0x%016lx\33[1;0m: %08x %s\n", s.pc, s.inst, asm_buf);
32+
}
2033
decode_exec(&s);
2134
cpu.pc = s.dnpc;
2235
}

sim/src/dbg.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,9 @@
33
#include <common.h>
44
#include <dbg.h>
55
#include <decode.h>
6-
#include <llvm-c/Disassembler.h>
6+
#include <disasm.h>
77

8-
LLVMDisasmContextRef init_disasm(const char *triple);
9-
void disassemble_inst(LLVMDisasmContextRef disasm_ctx, uint8_t *bytes, int len, uint64_t pc, char *out, size_t out_len);
10-
void cleanup_disasm(LLVMDisasmContextRef disasm_ctx);
11-
12-
static LLVMDisasmContextRef disasm_ctx;
13-
14-
void init_llvm_disassembler() {
15-
disasm_ctx = init_disasm("riscv64-unknown-elf");
16-
}
17-
18-
void cleanup_llvm_disassembler() {
19-
cleanup_disasm(disasm_ctx);
20-
}
8+
extern LLVMDisasmContextRef disasm_ctx;
219

2210
static void cmd_help() {
2311
printf("Available commands:\n");

sim/src/disasm.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <string.h>
55
#include <stdlib.h>
66

7+
extern LLVMDisasmContextRef disasm_ctx;
8+
79
LLVMDisasmContextRef init_disasm(const char *triple) {
810
LLVMInitializeAllTargetInfos();
911
LLVMInitializeAllTargetMCs();
@@ -40,4 +42,12 @@ void cleanup_disasm(LLVMDisasmContextRef disasm_ctx) {
4042
if (disasm_ctx) {
4143
LLVMDisasmDispose(disasm_ctx);
4244
}
45+
}
46+
47+
void init_llvm_disassembler() {
48+
disasm_ctx = init_disasm("riscv64-unknown-elf");
49+
}
50+
51+
void cleanup_llvm_disassembler() {
52+
cleanup_disasm(disasm_ctx);
4353
}

sim/src/main.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <common.h>
22
#include <memory.h>
33
#include <cpu.h>
4+
#include <disasm.h>
45

56
uint8_t *mem = NULL;
7+
int itrace_enabled = 0;
8+
LLVMDisasmContextRef disasm_ctx;
69

710
const char *help_string = "Usage: Simulator <img_file> [-d|-b]\n"
811
"Options:\n"
@@ -15,10 +18,15 @@ int main(int argc, char *argv[]){
1518
memset(mem, 0, MEM_SIZE);
1619
load_image(argv[1]);
1720
init_cpu();
18-
if (argc > 2 && strcmp(argv[2], "-d") == 0) {
21+
if (argc > 2 && strcmp(argv[2], "--debug") == 0) {
1922
debug_loop();
2023
}
21-
else if (argc > 2 && strcmp(argv[2], "-b") == 0) {
24+
else if (argc > 2 && strcmp(argv[2], "--batch") == 0) {
25+
cpu_exec();
26+
}
27+
else if (argc > 2 && strcmp(argv[2], "--itrace") == 0) {
28+
itrace_enabled = 1;
29+
init_llvm_disassembler();
2230
cpu_exec();
2331
}
2432
else {

0 commit comments

Comments
 (0)