7 static void start_vm(FILE *, const HwHeader
*);
8 static void execute_opcode(uint8_t);
9 static void vm_error(const char *, ...);
12 main(int argc
, char **argv
)
18 vm_error("No executable file!\n");
20 file
= fopen(argv
[1], "rt");
22 vm_error("Cannot read file %s\n", argv
[1]);
24 fread(&head
, sizeof(HwHeader
), 1, file
);
26 if (head
.hw_magic
!= HW_HEADER_MAGIC
)
27 vm_error("File is corrupted or not a valid application!\n");
29 start_vm(file
, &head
);
34 start_vm(FILE *file
, const HwHeader
*head
)
36 uint8_t *text_segment
;
38 text_segment
= (uint8_t *)malloc(sizeof(uint8_t) * head
->hw_instr_count
);
40 if (text_segment
== NULL
)
41 vm_error("Cannot allocate text segment\n");
43 fread(text_segment
, sizeof(uint8_t), head
->hw_instr_count
, file
);
45 for (i
= 0; i
< head
->hw_instr_count
; i
++) {
46 execute_opcode(text_segment
[i
]);
51 execute_opcode(uint8_t opcode
)
56 for (i
= 0; i
< LAST_INSTRUCTION
; i
++) {
57 inst
= &instructions
[i
];
58 if (opcode
== inst
->i_opcode
) {
59 printf("%s", inst
->i_data
);
65 vm_error(const char *fmt
, ...)
69 fprintf(stderr
, "HW Virtual Machine - (C) Akos Kovacs\n\nError: ");
70 vfprintf(stderr
, fmt
, ap
);