-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.cpp
61 lines (47 loc) · 1.52 KB
/
cpu.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
#include "main_proc.h"
elem_t* get_reg_num(int cmd, cpu_struct* processor)
{
#define STR_COMMANDS(reg, letter) if (cmd == reg) return &(processor->reg_##letter);
#include "string_define.h"
#undef STR_COMMANDS
return nullptr;
}
int CPU(cpu_struct *processor, const char* result_file,
const char* binary_file)
{
int size_bin = 0;
int line = 0;
int counter_byte = 0;
const int size_elem_t = (int) sizeof(elem_t);
char *asm_text = (char*) readFile (binary_file, &size_bin, "r+b");
#define DEF(name, elements, code) \
else if (name == asm_text[counter_byte] && \
elements == (int) asm_text[counter_byte + 1]) \
{ \
counter_byte += 2; \
code \
}
while (counter_byte < size_bin)
{
line++;
if (0) ;
#include "proc_commands.h"
else
{
printf("ERROR in command number %d %d\n", asm_text[counter_byte], line);
counter_byte += 2;
}
}
#undef DEF
FILE *output_file = fopen(result_file, "w");
if (output_file == nullptr)
return 1;
while( processor->cpu_stack.counter > 1)
{
elem_t result = 0;
stack_pop(&processor->cpu_stack, &result);
fprintf(output_file, CONST_FOR_ELEM_T, result);
}
fclose(output_file);
return 0;
}