This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
101 lines (80 loc) · 1.92 KB
/
main.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mipscpu.h>
#define MEMORY 0xffff
uint8_t* load_file(uint64_t* fsize, char* filename);
int main(int argc, char* argv[])
{
if (argc < 2)
goto end;
if (strcmp(argv[1], "-e") == 0)
{
struct mipscpu cpu;
initmips(&cpu);
uint64_t file_size;
uint8_t* file_data = load_file(&file_size, argv[2]);
cpu.memsize = file_size + MEMORY;
cpu.memory = malloc(cpu.memsize);
memset(cpu.memory, 0, cpu.memsize);
memcpy(cpu.memory + MEMORY, file_data, file_size);
cpu.pc = MEMORY;
free(file_data);
while (cpu.running && cpu.pc < cpu.memsize)
emulate_cycle_mips(&cpu);
free(cpu.memory);
}
else if (strcmp(argv[1], "-d") == 0)
{
struct mipscpu cpu;
initmips(&cpu);
cpu.memory = load_file(&cpu.memsize, argv[2]);
while (cpu.pc < cpu.memsize)
{
disassemble_mips(&cpu);
cpu.pc += INS_SIZE;
}
free(cpu.memory);
}
else if (strcmp(argv[1], "-a") == 0)
{
uint64_t size;
uint8_t* data = load_file(&size, argv[2]);
char* str = malloc(strlen(argv[2]) * sizeof(char));
strcpy(str, argv[2]);
int dotpos = 0;
while (str[++dotpos] != '.' && dotpos != strlen(argv[2]));
str[dotpos] = 0;
assemble_mips(data, str, &size);
free(str);
free(data);
}
else
{
end:
printf("Usage:\n-e FILE\tEmulate\n-d FILE\tDisassemble\n-a FILE\tAssemble\n");
}
return 0;
}
uint8_t* load_file(uint64_t* fsize, char* filename)
{
FILE* file = fopen(filename, "rb");
if (file == NULL)
{
printf("File error: %s\n", filename);
exit(-1);
}
fseek(file, 0, SEEK_END);
*fsize = ftell(file);
rewind(file);
uint8_t* buffer = (uint8_t*)malloc(*fsize);
size_t result = fread(buffer, 1, *fsize, file);
if (result != *fsize)
{
printf("Reading error\n");
exit(-1);
}
fclose(file);
return buffer;
}