-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
110 lines (93 loc) · 2.61 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
102
103
104
105
106
107
108
109
110
#include "fonctions.h"
#include "memory.h"
#include "register.h"
void interactif_mode()
{
initMemory();
char command[50] = "\0";
int instruction;
while( strcmp(command, "EXIT")){
printf("\nEnter Instruction. Enter 'EXIT' if it's over : ");
fgets(command, 50, stdin); //Récupération de ce qui est écrit par l'utilisateur
//On procède directement à l'exécution, ceci est possible car on traite seulement les instructions en séquences
instruction = encode_instruction(command);
if( instruction == -1 )
continue;
decode(instruction);
print_reg();
printMemory();
}
}
void non_interactif_mode(char* input_file, char* output_file, int pas)
{
FILE *input = fopen(input_file, "r");
if( input == NULL )
{
printf("Erreur lors de l'ouverture du fichier lecture\n");
exit(EXIT_FAILURE);
}
FILE *output = fopen(output_file, "w");
if( output == NULL )
{
printf("Erreur lors de l'ouverture du fichier écriture\n");
exit(EXIT_FAILURE);
}
readfile(input);
printf("\nEncodage...\n");
initMemory();
encode(input, output);
printf("Le code hexadécimal a été généré\n");
int hex;
char letter[2];
while( (hex = readMemory(DATA_MEM+get_pc())) != 0xffffffff ) //On traite chaque instruction en mémoire jusqu"à l'instruction EXIT
{
printf("\nPC = %d \n", get_pc());
printf("Processing instruction:\n%08x ", hex);
set_pc(); //On incrémente le PC
decode(hex);
if( pas )
{
do{
printf("Press [c] to continue, [r] to print registers or [m] to print memory\n");
scanf("%s", letter);
if( letter[0] == 'r' )
print_reg();
else if( letter[0] == 'm' )
printMemory();
else if ( letter[0] == 'c' )
printf("\nNext instruction : \n");
else
printf("Wrong command\n");
}while( letter[0] != 'c' );
}
}
print_reg();
printMemory();
fclose(input);
fclose(output);
}
int main(int argc, char** argv)
{
printf("--------------------------- COMPILATEUR MIPS MILHEIRO PEUBLE Mélissa, FOUCHER Sébastien ---------------------------\n");
if( argc > 4 || argc == 2)
{
printf("Erreur lors de l'exécution: format : ./emul-mips ['input file' 'output file' [-pas] ]\n");
return -1;
}
if ( argc == 1 )
interactif_mode(0);
else if ( argc == 3 )
non_interactif_mode(argv[1], argv[2], 0);
else if ( argc == 4 )
{
if( !strcmp( argv[3], "-pas") )
non_interactif_mode(argv[1], argv[2], 1);
else
{
printf("Erreur lors de l'exécution: format : ./emul-mips ['input file' 'output file' [-pas] ]\n");
return -1;
}
}
free(mem);
return 0;
}