/
test_simul.c
128 lines (109 loc) · 3.2 KB
/
test_simul.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*!
* \file test_simul.c
* \brief Test du simulateur
*/
#include <stdio.h>
#include <stdlib.h>
#include "machine.h"
#include "debug.h"
//! Segment de texte
extern Instruction text[];
//! Taille utile du programme
extern const unsigned textsize;
//! Premier exemple de segment de données initial
extern Word data[];
//! Fin de la zone de données utile
extern const unsigned dataend;
//! Taille utile du segment de données
extern const unsigned datasize;
extern bool allocated;
//! Help message.
/*!
* Printed with option \c -h.
*/
static void usage()
{
printf("Usage: test_simul [options] [binfile]\n");
printf("where options are:\n"
"\t-d\tDebug mode (interactive execution)\n"
"\t-b\tA binary file is provided\n"
"\t-l\tDo not execute; just display the listing\n"
"\t-h\tprint this help message\n"
"If -b is given, the next argument must be a file name containing\n"
"a valid program in binary format. Otherwise an internally defined\n"
"example program is used; the program is also dumped in binary into\n"
"the file dump.bin\n");
}
//! Programme de test
/*!
* Options de la ligne de commande :
*
* <dl>
* <dt>-d</dt><dd>mode pas à pas (mise au point)</dd>
*
* <dt>-f</dt><dd>le programme est dans un fichier binaire ; le nom de ce
* fichier doit être fourni également en paramètre de la ligne de
* commande ; sans cette option, on exécute un programme de test prédéfini.</dd>
*
* </dl>
*/
int main(int argc, char *argv[])
{
bool debug = false;
bool binfile = false;
bool no_exec = false;
char *programfile = NULL;
if (argc > 1)
{
for (int iarg = 1; iarg < argc; ++iarg)
{
if (argv[iarg][0] == '-')
switch (argv[iarg][1])
{
case 'd':
debug = true;
break;
case 'b':
binfile = true;
break;
case 'l':
no_exec = true;
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
default:
fprintf(stderr, "Unknown option: %s\n", argv[iarg]);
usage();
exit(EXIT_FAILURE);
}
else if (binfile)
programfile = argv[iarg];
else
fprintf(stderr, "Trailing options ignored...\n");
}
}
Machine mach;
if (!binfile)
load_program(&mach, textsize, text, datasize, data, dataend);
else
read_program(&mach, programfile);
printf("\n*** Sauvegarde des programmes et données initiales en format binaire ***\n\n");
dump_memory(&mach);
printf("\n*** Machine state before execution ***\n");
print_program(&mach);
print_data(&mach);
print_cpu(&mach);
if (no_exec)
return 0;
printf("\n*** Execution trace ***\n\n");
simul(&mach, debug);
printf("\n*** Machine state after execution ***\n");
print_cpu(&mach);
print_data(&mach);
if(allocated){
free(mach._text);
free(mach._data);
}
return 0;
}