-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
319 lines (281 loc) · 9.6 KB
/
main.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
* main.cpp
*
* (c) Copyright 2010, P. Jakubco
*
* How to build
* ^^^^^^^^^^^^
* - do not use code align
* - all microcode functions must have only 1 return instruction
*/
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <getopt.h>
#include "bin.hpp"
#include "ram.h"
#include "cache.h"
#include "dynarec.h"
#include "compiler.h"
#include "clprogram.hpp"
#include "emucl.hpp"
#include "main.hpp"
static struct option options[] = {{"help", no_argument, NULL, 'h' },
{"save-code", optional_argument, NULL, 'S'},
{"verbose", no_argument, NULL, 'v'},
{"summary", no_argument, NULL, 's'},
{"interpret", no_argument, NULL, 'i'},
{"compile", required_argument, NULL, 'c'},
{"compile-only", no_argument, NULL, 'C'},
{"log-time",optional_argument,NULL,'l'},
{"loop", required_argument, NULL, 'L'},
{"open-cl", no_argument, NULL, 'o'},
{"dynamic", no_argument, NULL, 'd'},
{"gpu", no_argument, NULL, 'g'},
{0,0,0,0}};
int cmd_options;
char *code_filename;
char *input_filename;
static RAMBin bin;
int main(int argc, char *argv[])
{
int status;
clock_t start, end;
double overall_time = 0;
int log_iter = -1; // log time iteration
int log_counter = 0;
int loops = 0;
FILE *flog;
BASIC_BLOCK *tmp;
char input_str[INPUT_CHARS];
int opt_index, opt;
code_filename = NULL;
while(1) {
opt = getopt_long(argc, argv, "hS::vsl::L:idgoc:C", options, &opt_index);
if (opt == -1)
break;
switch(opt) {
case 'h':
printf("dynamic 0.22b\nDynamic emulator of RAM programs\n\n" \
"Usage: dynamic [hS::vsl::L:idoc:C] path/to/ram/program\n\n" \
"Options:\n" \
" -h --help - This help screen.\n" \
" -S --save-code [[filename]]- Lets the dynamic to save generated\n" \
" code to file(s).\n" \
" -v --verbose - Hides all output (prints only the\n" \
" results from the output tape).\n" \
" -s --summary - Prints summary information while\n" \
" performing the translation.\n" \
" -l --log-time [[n]] - Log time for every n-th iteration.\n" \
" If n isn't given, log overall time only.\n" \
" The file 'log-time.txt' will be created.\n" \
" -L --loop [n] - Loop n times.\n" \
" -i --interpret - Perform interpretation.\n" \
" -d --dynamic - Perform dynamic translation.\n" \
" If no of the 'i','d','o' options are passed,\n" \
" this is the default option.\n" \
" -o --open-cl - Perform OpenCL RAM emulation.\n" \
" -g --gpu - Run the OpenCL emulation on GPU\n" \
" -c --compile [source_file] - Compile a source file into the output file.\n" \
" -C --compile-only - Do not perform emulation after compile.\n\n");
return 0;
case 'S':
cmd_options |= CMD_SAVE_CODE;
if (optarg)
code_filename = optarg;
break;
case 'v':
cmd_options |= CMD_VERBOSE;
break;
case 's':
cmd_options |= CMD_SUMMARY;
break;
case 'l':
cmd_options |= CMD_LOGTIME;
if (optarg)
log_iter = atoi(optarg);
else
log_iter = -1;
break;
case 'L':
cmd_options |= CMD_LOOPS;
loops = atoi(optarg);
break;
case 'i':
cmd_options |= CMD_INTERPRET;
break;
case 'd':
cmd_options |= CMD_DYNAMIC;
break;
case 'o':
cmd_options |= CMD_OPENCL;
break;
case 'c':
cmd_options |= CMD_COMPILE;
input_filename = optarg;
break;
case 'g':
cmd_options |= CMD_GPU;
break;
case 'C':
cmd_options |= CMD_COMPILE_ONLY;
break;
default: opt = -1; break;
}
if (opt == -1)
break;
}
if (optind >= argc) {
if (!(cmd_options & CMD_VERBOSE))
printf("Error: Missing RAM program file name\n\n(Type 'dynamic --help' for help)\n");
return ERROR_MISSING_ARGS;
}
/* first - compile */
if (cmd_options & CMD_COMPILE) {
if (!(cmd_options & CMD_VERBOSE))
printf("Compiling file '%s'...\n", input_filename);
if (compile(input_filename, argv[optind])) {
if (!(cmd_options & CMD_VERBOSE))
printf("\nFix the errors and try again!\n");
return ERROR_COMPILE;
} else {
if (!(cmd_options & CMD_VERBOSE))
printf("\nCompile runned OK\n");
}
if (cmd_options & CMD_COMPILE_ONLY)
return OK;
}
/* load RAM program */
if (cmd_options & CMD_SUMMARY)
printf("Loading file: %s\n", argv[optind]);
if (!bin.bin_load(argv[optind])) {
if (!(cmd_options & CMD_VERBOSE))
printf("Error loading file: %s\n", argv[optind]);
return ERROR_LOAD;
}
int ram_size = bin.get_size();
const char *prog = (const char*)bin.get_program();
if (cmd_options & CMD_SUMMARY) {
printf("\nEmulating program:\n");
bin.bin_print();
}
if (cmd_options & CMD_SUMMARY)
printf("Initializing...\n");
ram_init(INPUT_CHARS);
cache_init(ram_size);
if (cmd_options & CMD_LOGTIME) {
if ((flog = fopen("log-time.txt", "w")) == NULL) {
printf("Error: cannot open file 'log-time.txt' !\n");
return ERROR_LOGFILE;
}
}
do {
if (loops > 0) {
if (!(cmd_options & CMD_VERBOSE))
printf("Loop: #%d\n", loops);
loops--;
}
if (!(cmd_options & CMD_VERBOSE))
printf("Enter input tape (max. %d chars):", INPUT_CHARS);
scanf("%s", input_str);
// convert the input tape into binary form
int i,l=strlen(input_str);
for (i = 0; i < l; i++)
ram_env.input[i] = input_str[i]-'0';
if (cmd_options & CMD_INTERPRET) {
if (!(cmd_options & CMD_VERBOSE))
printf("Interpreting...\n");
start = clock();
log_counter = 0;
overall_time = 0;
while ((status = ram_interpret(prog, ram_size)) == RAM_OK) {
if (cmd_options & CMD_LOGTIME) {
if (log_counter < log_iter)
log_counter++;
else if (log_counter == log_iter) {
log_counter = 0;
end = clock();
fprintf(flog, "%lf\n", (double)((double)end-(double)start));
overall_time += (double)((double)end-(double)start);
start = end;
}
}
}
end = clock();
overall_time += (double)((double)end-(double)start);
if (!(cmd_options & CMD_VERBOSE)) {
printf("\nDone.\n\tError code: %d (%s)\n", status, ram_error(status));
if ((cmd_options & CMD_LOGTIME) && (log_iter == -1))
fprintf(flog, "%lf\n", overall_time);
}
if (!(cmd_options & CMD_VERBOSE))
printf("\nOutput tape:\n\t");
ram_output();
printf("\n");
char *input = ram_env.input;
ram_init(INPUT_CHARS);
strcpy(ram_env.input, input);
free(input);
}
if ((cmd_options & CMD_DYNAMIC)
|| !(cmd_options & (CMD_INTERPRET | CMD_OPENCL))) {
if (!(cmd_options & CMD_VERBOSE))
printf("Emulating using dynamic translation...\n");
start = clock();
log_counter = 0;
overall_time = 0;
do {
tmp = cache_get_block(ram_env.pc);
if (tmp == NULL) {
if ((tmp = cache_create_block(ram_env.pc)) == NULL) {
cache_flush();
tmp = cache_create_block(ram_env.pc);
}
dyn_translate(tmp, prog, ram_size);
} else if (tmp->size > 0)
(*(void (*)())tmp->code)();
else
ram_env.state = ram_interpret(prog, ram_size);
if (cmd_options & CMD_LOGTIME) {
if (log_counter < log_iter)
log_counter++;
else if (log_counter == log_iter) {
log_counter = 0;
end = clock();
fprintf(flog, "%lf\n", (double)((double)end-(double)start));
overall_time += (double)((double)end-(double)start);
start = end;
}
}
} while ((ram_env.state == RAM_OK) && (ram_env.pc < ram_size));
end = clock();
overall_time += (double)((double)end-(double)start);
if (!(cmd_options & CMD_VERBOSE)) {
printf("\nDone.\n\tError code: %d (%s)\n", ram_env.state,
ram_error(ram_env.state));
if ((cmd_options & CMD_LOGTIME) && (log_iter == -1))
fprintf(flog,"%lf\n", overall_time);
}
if (!(cmd_options & CMD_VERBOSE))
printf("\nOutput tape:\n\t");
ram_output();
printf("\n");
char *input = ram_env.input;
ram_init(INPUT_CHARS);
strcpy(ram_env.input, input);
free(input);
cache_flush();
}
if (cmd_options & CMD_OPENCL) {
if (!(cmd_options & CMD_VERBOSE))
printf("Emulating using OpenCL...\n\n");
cl_execute(prog, ram_size, cmd_options, flog);
}
} while (loops > 0);
if (cmd_options & CMD_LOGTIME)
fclose(flog);
cache_destroy();
ram_destroy();
return OK;
}