-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzpusim.c
126 lines (120 loc) · 3.79 KB
/
zpusim.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "vm.h"
#include "cmdline.h"
#define MIN_MEM_SIZE 0x10000
#define DEFAULT_MEM_SIZE 0x10000
int main(int argc, char *argv[]) {
struct gengetopt_args_info ai;
if (cmdline_parser (argc, argv, &ai) != 0) {
return -1;
}
char* filename = ai.filename_arg;
uintzpu_t main_mem_size = DEFAULT_MEM_SIZE;
uintzpu_t print_stack_size = 0;
size_t max_cycles = 0;
bool peek = false;
uintzpu_t peek_addr = 0;
bool silent = false;
if (ai.mem_given && ai.mem_arg >= MIN_MEM_SIZE) {
main_mem_size = (uintzpu_t)ai.mem_arg;
}
if (ai.stack_given && ai.stack_arg > 0) {
print_stack_size = (uintzpu_t)ai.stack_arg;
}
if (ai.cycles_given && ai.cycles_arg > 0) {
max_cycles = (size_t)ai.cycles_arg;
}
if (ai.peek_given && ai.peek_arg >= 0 && ai.peek_arg < main_mem_size) {
peek = true;
peek_addr = (uintzpu_t)ai.peek_arg;
}
if (max_cycles && ai.silent_flag) {
silent = true;
}
// FILE LOADING
FILE* f = fopen (filename, "rb");
if (!f) {
fprintf(stderr, "File? \n");
return -1;
}
fseek (f, 0, SEEK_END);
size_t length = ftell(f);
if (length > main_mem_size) {
fclose(f);
fprintf(stderr, "File too large for given memory size! \n");
return -1;
}
fseek (f, 0, SEEK_SET);
zpuvm vm;
zpuvm_memblock memblock[3];
void* mem[1];
mem[0] = malloc(main_mem_size);
if (!mem[0]) {
fclose(f);
fprintf(stderr, "Mem allocation error\n");
return -1;
}
size_t read_bytes = fread(mem[0], 1, length, f);
fclose(f);
if (read_bytes != length) {
fprintf(stderr, "File read error \n");
return -1;
}
if (!silent) {
#ifdef ZPU_LITTLE_ENDIAN
const char endianess[] = "little endian";
#endif
#ifndef ZPU_LITTLE_ENDIAN
const char endianess[] = "big endian";
#endif
printf("ZPUSIM: %zu bit %s ZPU, %u bytes of mem", ZPU_WORD_SIZE * 8, endianess, main_mem_size);
if (print_stack_size) printf(", printing upto %d deep in stack", print_stack_size);
if (peek) printf(", peeking 0x%08x", peek_addr);
printf(".\n");
}
zpuvm_memblock_init(&(memblock[0]), mem[0], 0x0, main_mem_size);
zpuvm_init(&vm, memblock, 1, 0, main_mem_size);
size_t cycles = 1;
while(1) {
unsigned char op = *((unsigned char*)((char*)mem[0] + vm.pc));
if (!silent) {
printf("cycle %10zu ", cycles);
printf("opcode 0x%02x\n", op);
printf("pc_before 0x%08x ", vm.pc);
printf("sp_before %08x \n", vm.sp);
}
zpuvm_step(&vm);
if (!silent) {
printf("pc_after 0x%08x ", vm.pc);
printf("sp_after %08x \n", vm.sp);
if (peek) {
printf("peek_after 0x%08x <= 0x%08x\n", peek_addr, zpu2h(*((zpuv*)((char*)mem[0] + peek_addr))));
}
if (print_stack_size) {
uintzpu_t sp = vm.sp;
uintzpu_t sc = print_stack_size;
printf("STACK LAYOUT (After execution):\n");
while(sc && sp <= main_mem_size - ZPU_WORD_SIZE) {
uintzpu_t st = zpu2h(*((zpuv*)((char*)mem[0] + sp)));
printf("0x%08x <= 0x%08x\n", sp, st);
sp += ZPU_WORD_SIZE;
sc--;
}
}
}
if (max_cycles) {
if (cycles >= max_cycles) break;
if (!silent) {
printf("\n");
}
} else {
printf("Press enter for next cycle.\n");
getchar();
}
cycles++;
}
free(mem[0]);
return 0;
}