0%

init_monitor()

parse_args(int argc, char *argv[])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 解析命令行选项参数
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
// {const char *name; int has_arg; int *flag; int val}
// 匹配时:flag = NULL,返回 val ;flag != NULL ,返回 0
static int parse_args(int argc, char *argv[]) {
const struct option table[] = {
{"batch" , no_argument , NULL, 'b'},
{"log" , required_argument, NULL, 'l'},
{"diff" , required_argument, NULL, 'd'},
{"port" , required_argument, NULL, 'p'},
{"help" , no_argument , NULL, 'h'},
{0 , 0 , NULL, 0 },
};
int o;
// 依次检测是否有参数:-b -h -l xxx -d xxx -p xxx --batch --log xxx ...
// 若有则返回参数字符,若检测到 opstring 未定义的参数返回 ? ,检测完毕返回-1
while ( (o = getopt_long(argc, argv, "-bhl:d:p:", table, NULL)) != -1) {
switch (o) {
// batch mode
case 'b': sdb_set_batch_mode(); break;
// difftest
case 'p': sscanf(optarg, "%d", &difftest_port); break;
// log
case 'l': log_file = optarg; break;
// difftest
case 'd': diff_so_file = optarg; break;
// 存疑
case 1: img_file = optarg; return 0;
// 返回 ? :未定义参数
default:
printf("Usage: %s [OPTION...] IMAGE [args]\n\n", argv[0]);
printf("\t-b,--batch run with batch mode\n");
printf("\t-l,--log=FILE output log to FILE\n");
printf("\t-d,--diff=REF_SO run DiffTest with reference REF_SO\n");
printf("\t-p,--port=PORT run DiffTest with port PORT\n");
printf("\n");
exit(0);
}
}
return 0;
}

init_rand()

1
2
3
4
// 依据时间 time(0) 设置随机数种子 seed 以供 rand() 使用
void init_rand() {
srand(MUXDEF(CONFIG_TARGET_AM, 0, time(0)));
}

init_log(const char *log_file)

1
2
3
4
5
6
7
8
9
void init_log(const char *log_file) {
log_fp = stdout;
if (log_file != NULL) {
FILE *fp = fopen(log_file, "w");
Assert(fp, "Can not open '%s'", log_file);
log_fp = fp;
}
Log("Log is written to %s", log_file ? log_file : "stdout");
}

init_mem()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 使用随机数初始化内存
void init_mem() {
#if defined(CONFIG_PMEM_MALLOC)
pmem = malloc(CONFIGMSIZE);
assert(pmem);
#endif
#ifdef CONFIG_MEM_RANDOM
uint32_t *p = (uint32_t *)pmem;
int i;
for (i = 0; i < (int) (CONFIG_MSIZE / sizeof(p[0])); i ++) {
p[i] = rand();
}
#endif
Log("physical memory area [" FMT_PADDR ", " FMT_PADDR "]", PMEM_LEFT, PMEM_RIGHT);
}

IFDEF(CONFIG_DEVICE, init_device())

1

init_isa()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void init_isa() {
/* Load built-in image. */
memcpy(guest_to_host(RESET_VECTOR), img, sizeof(img));

/* Initialize this virtual computer system. */
restart();
}
// physical memory 128MB
static uint8_t pmem[CONFIG_MSIZE] PG_ALIGN = {};
uint8_t* guest_to_host(paddr_t paddr) { return pmem + paddr - CONFIG_MBASE; }
static const uint32_t img [] = {
0x00000297, // auipc t0,0
0x0002b823, // sd zero,16(t0)
0x0102b503, // ld a0,16(t0)
0x00100073, // ebreak (used as nemu_trap)
0xdeadbeef, // some data
};

static void restart() {
/* Set the initial program counter. */
cpu.pc = RESET_VECTOR;

/* The zero register is always 0. */
cpu.gpr[0] = 0;
}

test