|
@@ -1,26 +1,24 @@
|
1
|
|
-
|
2
|
|
-/*
|
3
|
|
-----------------------------------------
|
4
|
|
-| Adresse | Registre | Modifié |
|
5
|
|
-----------------------------------------
|
6
|
|
-| | | |
|
7
|
|
-| | | |
|
8
|
|
-| | | |
|
9
|
|
-| i | 0x777756b8 | int |
|
10
|
|
-| size | 0x777756b8 | int |
|
11
|
|
-----------------------------------------
|
12
|
|
-*/
|
13
|
|
-
|
14
|
1
|
#include "tables.h"
|
15
|
2
|
#define NB_REG 4
|
16
|
3
|
#define MEM_SIZE 16
|
17
|
|
-#define NB_INSTRUCTIONS 128
|
18
|
4
|
#define MEM_INST_SIZE 128
|
19
|
5
|
#define NB_BITS_INSTRUCTION 5
|
20
|
6
|
#define NB_BITS 8
|
21
|
7
|
|
22
|
|
-int traduction_JMP[NB_INSTRUCTIONS];
|
23
|
8
|
|
|
9
|
+
|
|
10
|
+/**************************************************/
|
|
11
|
+/**************************************************/
|
|
12
|
+/***************** Initialisation *****************/
|
|
13
|
+/**************************************************/
|
|
14
|
+/**************************************************/
|
|
15
|
+
|
|
16
|
+// Buffer to patch Jumps difference due to adding LOAD and STORE
|
|
17
|
+int traduction_JMP[MEM_INST_SIZE];
|
|
18
|
+// Index of the buffer
|
|
19
|
+int last_instruction = 0;
|
|
20
|
+
|
|
21
|
+// Structure coding an instruction
|
24
|
22
|
struct str_instruction {
|
25
|
23
|
enum instruction_t instruction;
|
26
|
24
|
int param1;
|
|
@@ -28,136 +26,23 @@ struct str_instruction {
|
28
|
26
|
int param3;
|
29
|
27
|
};
|
30
|
28
|
|
31
|
|
-int last_instruction = 0;
|
32
|
|
-struct str_instruction buffer[3*NB_INSTRUCTIONS];
|
33
|
|
-
|
34
|
|
-void add_instruction(enum instruction_t inst, int param1, int param2, int param3) {
|
35
|
|
- struct str_instruction my_instruction = {inst, param1, param2, param3};
|
36
|
|
- buffer[last_instruction] = my_instruction;
|
37
|
|
- last_instruction++;
|
38
|
|
-}
|
39
|
|
-
|
40
|
|
-void write_asm(FILE * file) {
|
41
|
|
- int i = 0;
|
42
|
|
- while (i<MEM_INST_SIZE) {
|
43
|
|
- if (buffer[i].instruction == ADD) {
|
44
|
|
- fprintf(file, "ADD %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
45
|
|
- } else if (buffer[i].instruction == SUB) {
|
46
|
|
- fprintf(file, "SUB %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
47
|
|
- } else if (buffer[i].instruction == MUL) {
|
48
|
|
- fprintf(file, "MUL %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
49
|
|
- } else if (buffer[i].instruction == DIV) {
|
50
|
|
- fprintf(file, "DIV %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
51
|
|
- } else if (buffer[i].instruction == INF) {
|
52
|
|
- fprintf(file, "INF %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
53
|
|
- } else if (buffer[i].instruction == SUP) {
|
54
|
|
- fprintf(file, "SUP %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
55
|
|
- } else if (buffer[i].instruction == EQU) {
|
56
|
|
- fprintf(file, "EQU %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
57
|
|
-
|
58
|
|
- } else if (buffer[i].instruction == AFC) {
|
59
|
|
- fprintf(file, "AFC %d %d\n", buffer[i].param1, buffer[i].param2);
|
60
|
|
- } else if (buffer[i].instruction == CPY) {
|
61
|
|
- fprintf(file, "CPY %d %d\n", buffer[i].param1, buffer[i].param2);
|
62
|
|
-
|
63
|
|
- } else if (buffer[i].instruction == LOAD) {
|
64
|
|
- fprintf(file, "LOAD %d %d\n", buffer[i].param1, buffer[i].param2);
|
65
|
|
- } else if (buffer[i].instruction == STORE) {
|
66
|
|
- fprintf(file, "STORE %d %d\n", buffer[i].param1, buffer[i].param2);
|
67
|
|
- } else if (buffer[i].instruction == LOADI) {
|
68
|
|
- fprintf(file, "LOADI %d %d\n", buffer[i].param1, buffer[i].param2);
|
69
|
|
- } else if (buffer[i].instruction == STOREI) {
|
70
|
|
- fprintf(file, "STOREI %d %d\n", buffer[i].param1, buffer[i].param2);
|
71
|
|
- } else if (buffer[i].instruction == STOREA) {
|
72
|
|
- fprintf(file, "STOREA %d %d\n", buffer[i].param1, buffer[i].param2);
|
73
|
|
-
|
74
|
|
- } else if (buffer[i].instruction == JMP) {
|
75
|
|
- fprintf(file, "JMP %d\n", traduction_JMP[buffer[i].param1]);
|
76
|
|
- } else if (buffer[i].instruction == JMZ) {
|
77
|
|
- fprintf(file, "JMZ %d\n", traduction_JMP[buffer[i].param1]);
|
78
|
|
- } else if (buffer[i].instruction == GET) {
|
79
|
|
- fprintf(file, "GET %d\n", buffer[i].param1);
|
80
|
|
- } else if (buffer[i].instruction == PRI) {
|
81
|
|
- fprintf(file, "PRI %d\n", buffer[i].param1);
|
82
|
|
-
|
83
|
|
- } else if (buffer[i].instruction == CALL) {
|
84
|
|
- fprintf(file, "CALL %d %d\n", traduction_JMP[buffer[i].param1], buffer[i].param2);
|
85
|
|
- } else if (buffer[i].instruction == RET) {
|
86
|
|
- fprintf(file, "RET\n");
|
87
|
|
- } else if (buffer[i].instruction == STOP) {
|
88
|
|
- fprintf(file, "STOP %d\n", buffer[i].param1);
|
89
|
|
- }
|
90
|
|
- i++;
|
91
|
|
- }
|
92
|
|
-}
|
93
|
|
-
|
94
|
|
-void int_2_bin(char * buff, int n) {
|
95
|
|
- int _m = n;
|
96
|
|
- for (int i = 0; i < 32; i++) {
|
97
|
|
- buff[31 - i] = ((_m & (1 << 31)) ? '1' : '0');
|
98
|
|
- _m = _m << 1;
|
99
|
|
- }
|
100
|
|
-}
|
101
|
|
-
|
102
|
|
-void convert_to_binary_on_N(int value, int N, char * buff) {
|
103
|
|
- char tampon[33];
|
104
|
|
- int_2_bin(tampon, value);
|
105
|
|
- int i;
|
106
|
|
- for (i = N-1; i>=0; i--) {
|
107
|
|
- buff[N-1-i] = tampon[i];
|
108
|
|
- }
|
109
|
|
- buff[N] = '\0';
|
110
|
|
-}
|
111
|
|
-
|
112
|
|
-void write_instruction_binary(FILE * file, struct str_instruction instr, char compact) {
|
113
|
|
- char buff1[33];
|
114
|
|
- char buff2[33];
|
115
|
|
- char buff3[33];
|
116
|
|
- char buff4[33];
|
117
|
|
- convert_to_binary_on_N(instr.instruction, NB_BITS_INSTRUCTION, buff1);
|
118
|
|
- if (instr.instruction == JMP || instr.instruction == JMZ || instr.instruction == CALL) {
|
119
|
|
- convert_to_binary_on_N(traduction_JMP[instr.param1], NB_BITS, buff2);
|
120
|
|
- } else {
|
121
|
|
- convert_to_binary_on_N(instr.param1, NB_BITS, buff2);
|
122
|
|
- }
|
123
|
|
- convert_to_binary_on_N(instr.param2, NB_BITS, buff3);
|
124
|
|
- convert_to_binary_on_N(instr.param3, NB_BITS, buff4);
|
125
|
|
- if (compact) {
|
126
|
|
- fprintf(file, "%s%s%s%s", buff1, buff2, buff3, buff4);
|
127
|
|
- } else {
|
128
|
|
- fprintf(file, "\"%s%s%s%s\" & ", buff1, buff2, buff3, buff4);
|
129
|
|
- }
|
130
|
|
-}
|
131
|
|
-
|
132
|
|
-
|
133
|
|
-void write_code_machine(FILE * file) {
|
134
|
|
- int i = MEM_INST_SIZE - 1;
|
135
|
|
- while (i>=0) {
|
136
|
|
- write_instruction_binary(file, buffer[i], 0);
|
137
|
|
- i--;
|
138
|
|
- }
|
139
|
|
-}
|
140
|
|
-
|
141
|
|
-void write_code_machine_compact(FILE * file) {
|
142
|
|
- printf(file, "\"");
|
143
|
|
- int i = MEM_INST_SIZE - 1;
|
144
|
|
- while (i>=0) {
|
145
|
|
- write_instruction_binary(file, buffer[i], 1);
|
146
|
|
- i--;
|
147
|
|
- }
|
148
|
|
- printf(file, "\"\n");
|
149
|
|
-}
|
150
|
|
-
|
|
29
|
+// Buffer to store registers oriented instructions
|
|
30
|
+struct str_instruction buffer[3*MEM_INST_SIZE];
|
151
|
31
|
|
|
32
|
+// Structure coding an address and the correpondance with a register
|
152
|
33
|
struct case_adresse {
|
153
|
34
|
int adresse;
|
154
|
35
|
int registre;
|
155
|
36
|
char modifie;
|
156
|
37
|
};
|
157
|
38
|
|
|
39
|
+// Buffer of addresses (Memory)
|
158
|
40
|
struct case_adresse tableau[MEM_SIZE];
|
|
41
|
+
|
|
42
|
+// Buffer to manage priority policy
|
159
|
43
|
int registres[NB_REG];
|
160
|
44
|
|
|
45
|
+// Initialise all : the addresses-registers association table, the registers priority table, the instructions buffer, the instruction address association table
|
161
|
46
|
void init (void) {
|
162
|
47
|
int i;
|
163
|
48
|
struct case_adresse case_courante = {0, -1, 0};
|
|
@@ -174,10 +59,20 @@ void init (void) {
|
174
|
59
|
}
|
175
|
60
|
}
|
176
|
61
|
|
|
62
|
+
|
|
63
|
+/**************************************************/
|
|
64
|
+/**************************************************/
|
|
65
|
+/************** Registers Management **************/
|
|
66
|
+/**************************************************/
|
|
67
|
+/**************************************************/
|
|
68
|
+
|
|
69
|
+// INTERN FUNCTION
|
|
70
|
+// Print a case address
|
177
|
71
|
void print_case_adresse(struct case_adresse case_courante) {
|
178
|
72
|
printf("{addr : %d ; reg : %d ; modi : %d}\n", case_courante.adresse, case_courante.registre, (int)case_courante.modifie);
|
179
|
73
|
}
|
180
|
74
|
|
|
75
|
+// Print the adresses-registers correspondance table
|
181
|
76
|
void print() {
|
182
|
77
|
int i;
|
183
|
78
|
for (i=0; i<MEM_SIZE; i++) {
|
|
@@ -185,11 +80,14 @@ void print() {
|
185
|
80
|
}
|
186
|
81
|
}
|
187
|
82
|
|
188
|
|
-
|
|
83
|
+// INTERN FUNCTION
|
|
84
|
+// return the case corresponding to the given address
|
189
|
85
|
struct case_adresse get_info(int adresse) {
|
190
|
86
|
return tableau[adresse];
|
191
|
87
|
}
|
192
|
88
|
|
|
89
|
+// INTERN FUNCTION
|
|
90
|
+// return the address corresponding to the given register
|
193
|
91
|
int get_adresse (int registre) {
|
194
|
92
|
int i = 0;
|
195
|
93
|
while (i < MEM_SIZE && tableau[i].registre != registre) {
|
|
@@ -202,14 +100,19 @@ int get_adresse (int registre) {
|
202
|
100
|
}
|
203
|
101
|
}
|
204
|
102
|
|
|
103
|
+// INTERN FUNCTION
|
|
104
|
+// Set the given register to the given address
|
205
|
105
|
void set_registre(int adresse, int registre) {
|
206
|
106
|
tableau[adresse].registre = registre;
|
207
|
107
|
}
|
208
|
108
|
|
|
109
|
+// INTERN FUNCTION
|
|
110
|
+// Set modifie to the address (0 the value in the memory is up to date, 1 the value in the register is more recent)
|
209
|
111
|
void set_modifie(int adresse, char modifie) {
|
210
|
112
|
tableau[adresse].modifie = modifie;
|
211
|
113
|
}
|
212
|
114
|
|
|
115
|
+// Increment the register priority policy buffer
|
213
|
116
|
void increment_time() {
|
214
|
117
|
int i;
|
215
|
118
|
for (i=0; i<NB_REG; i++) {
|
|
@@ -217,10 +120,14 @@ void increment_time() {
|
217
|
120
|
}
|
218
|
121
|
}
|
219
|
122
|
|
|
123
|
+// INTERN FUNCTION
|
|
124
|
+// Specifie that the given register have been used
|
220
|
125
|
void refresh_registre(int registre) {
|
221
|
126
|
registres[registre] = 0;
|
222
|
127
|
}
|
223
|
128
|
|
|
129
|
+// INTERN FUNCTION
|
|
130
|
+// Return the LRU register
|
224
|
131
|
int get_register() {
|
225
|
132
|
int i;
|
226
|
133
|
int index_max = 0;
|
|
@@ -232,6 +139,48 @@ int get_register() {
|
232
|
139
|
return index_max;
|
233
|
140
|
}
|
234
|
141
|
|
|
142
|
+/* Ask for a register to read the value
|
|
143
|
+
|
|
144
|
+ @param :
|
|
145
|
+ - adresse : The address of value wanted
|
|
146
|
+ - added_instruction : Address of an int storing the number of added_instructions
|
|
147
|
+ @return : The number of the register corresponding to the given address
|
|
148
|
+*/
|
|
149
|
+int get_reg_read(int adresse, int * added_instruction) {
|
|
150
|
+ struct case_adresse ma_case = get_info(adresse);
|
|
151
|
+ if (ma_case.registre == -1) {
|
|
152
|
+ int dispo = get_register();
|
|
153
|
+ int previous_addr = get_adresse(dispo);
|
|
154
|
+ if (previous_addr != -1) {
|
|
155
|
+ struct case_adresse ancienne_case = get_info(previous_addr);
|
|
156
|
+ if (ancienne_case.modifie == 1) {
|
|
157
|
+ *added_instruction = (*added_instruction) + 1;
|
|
158
|
+ add_instruction(STORE, previous_addr, dispo, 0);
|
|
159
|
+ set_modifie(previous_addr, 0);
|
|
160
|
+ }
|
|
161
|
+ set_registre(previous_addr, -1);
|
|
162
|
+ }
|
|
163
|
+ *added_instruction = (*added_instruction) + 1;
|
|
164
|
+ add_instruction(LOAD, dispo, adresse, 0);
|
|
165
|
+ set_registre(adresse, dispo);
|
|
166
|
+ refresh_registre(dispo);
|
|
167
|
+ return dispo;
|
|
168
|
+ } else {
|
|
169
|
+ refresh_registre(ma_case.registre);
|
|
170
|
+ return ma_case.registre;
|
|
171
|
+ }
|
|
172
|
+}
|
|
173
|
+
|
|
174
|
+/* Ask for a register to write the value
|
|
175
|
+
|
|
176
|
+ @param :
|
|
177
|
+ - adresse : The address of value (if -1 return a free register without associating it to any address)
|
|
178
|
+ - added_instruction : Address of an int storing the number of added_instructions
|
|
179
|
+ @return : The number of the register corresponding to the given address
|
|
180
|
+
|
|
181
|
+ WARNING : The value of the address will not be LOADED in the register
|
|
182
|
+ Always ask READ registers before the WRITE register
|
|
183
|
+*/
|
235
|
184
|
int get_reg_write(int adresse, int * added_instruction) {
|
236
|
185
|
if (adresse == -1) {
|
237
|
186
|
int dispo = get_register();
|
|
@@ -271,35 +220,12 @@ int get_reg_write(int adresse, int * added_instruction) {
|
271
|
220
|
}
|
272
|
221
|
}
|
273
|
222
|
|
274
|
|
-int get_reg_read(int adresse, int * added_instruction) {
|
275
|
|
- struct case_adresse ma_case = get_info(adresse);
|
276
|
|
- if (ma_case.registre == -1) {
|
277
|
|
- int dispo = get_register();
|
278
|
|
- int previous_addr = get_adresse(dispo);
|
279
|
|
- if (previous_addr != -1) {
|
280
|
|
- struct case_adresse ancienne_case = get_info(previous_addr);
|
281
|
|
- if (ancienne_case.modifie == 1) {
|
282
|
|
- *added_instruction = (*added_instruction) + 1;
|
283
|
|
- add_instruction(STORE, previous_addr, dispo, 0);
|
284
|
|
- set_modifie(previous_addr, 0);
|
285
|
|
- }
|
286
|
|
- set_registre(previous_addr, -1);
|
287
|
|
- }
|
288
|
|
- *added_instruction = (*added_instruction) + 1;
|
289
|
|
- add_instruction(LOAD, dispo, adresse, 0);
|
290
|
|
- set_registre(adresse, dispo);
|
291
|
|
- refresh_registre(dispo);
|
292
|
|
- return dispo;
|
293
|
|
- } else {
|
294
|
|
- refresh_registre(ma_case.registre);
|
295
|
|
- return ma_case.registre;
|
296
|
|
- }
|
297
|
|
-}
|
298
|
|
-
|
|
223
|
+// Broke the association between adresse and its corresponding register
|
299
|
224
|
void unlink(int adresse) {
|
300
|
225
|
set_registre(adresse, -1);
|
301
|
226
|
}
|
302
|
227
|
|
|
228
|
+// Store used register, init the association table between addresses and registers
|
303
|
229
|
int flush_and_init() {
|
304
|
230
|
int i;
|
305
|
231
|
int added_instruction = 0;
|
|
@@ -321,28 +247,148 @@ int flush_and_init() {
|
321
|
247
|
return added_instruction;
|
322
|
248
|
}
|
323
|
249
|
|
324
|
|
-void new_instruction(int nb_inst) {
|
325
|
|
- static int last_intruction_adresse = 0;
|
326
|
|
- static int current_instruction = 0;
|
327
|
|
- traduction_JMP[current_instruction] = last_intruction_adresse;
|
328
|
|
- current_instruction++;
|
329
|
|
- last_intruction_adresse += nb_inst;
|
330
|
|
-}
|
331
|
250
|
|
332
|
251
|
|
333
|
252
|
|
334
|
253
|
|
335
|
254
|
|
|
255
|
+/**************************************************/
|
|
256
|
+/**************************************************/
|
|
257
|
+/************** Instructions Writing **************/
|
|
258
|
+/**************************************************/
|
|
259
|
+/**************************************************/
|
336
|
260
|
|
|
261
|
+// Add a new Registers oriented instruction
|
|
262
|
+void add_instruction(enum instruction_t inst, int param1, int param2, int param3) {
|
|
263
|
+ struct str_instruction my_instruction = {inst, param1, param2, param3};
|
|
264
|
+ buffer[last_instruction] = my_instruction;
|
|
265
|
+ last_instruction++;
|
|
266
|
+}
|
337
|
267
|
|
|
268
|
+// Specifie the number of Register oriented instructions corresponding to the memory oriented instruction
|
|
269
|
+void new_instruction(int nb_inst) {
|
|
270
|
+ static int last_intruction_adresse = 0;
|
|
271
|
+ static int current_instruction = 0;
|
|
272
|
+ traduction_JMP[current_instruction] = last_intruction_adresse;
|
|
273
|
+ current_instruction++;
|
|
274
|
+ last_intruction_adresse += nb_inst;
|
|
275
|
+}
|
338
|
276
|
|
|
277
|
+// Write the new assembly in the given file
|
|
278
|
+void write_asm(FILE * file) {
|
|
279
|
+ int i = 0;
|
|
280
|
+ while (i<MEM_INST_SIZE) {
|
|
281
|
+ if (buffer[i].instruction == ADD) {
|
|
282
|
+ fprintf(file, "ADD %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
|
283
|
+ } else if (buffer[i].instruction == SUB) {
|
|
284
|
+ fprintf(file, "SUB %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
|
285
|
+ } else if (buffer[i].instruction == MUL) {
|
|
286
|
+ fprintf(file, "MUL %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
|
287
|
+ } else if (buffer[i].instruction == DIV) {
|
|
288
|
+ fprintf(file, "DIV %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
|
289
|
+ } else if (buffer[i].instruction == INF) {
|
|
290
|
+ fprintf(file, "INF %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
|
291
|
+ } else if (buffer[i].instruction == SUP) {
|
|
292
|
+ fprintf(file, "SUP %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
|
293
|
+ } else if (buffer[i].instruction == EQU) {
|
|
294
|
+ fprintf(file, "EQU %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
339
|
295
|
|
|
296
|
+ } else if (buffer[i].instruction == AFC) {
|
|
297
|
+ fprintf(file, "AFC %d %d\n", buffer[i].param1, buffer[i].param2);
|
|
298
|
+ } else if (buffer[i].instruction == CPY) {
|
|
299
|
+ fprintf(file, "CPY %d %d\n", buffer[i].param1, buffer[i].param2);
|
340
|
300
|
|
|
301
|
+ } else if (buffer[i].instruction == LOAD) {
|
|
302
|
+ fprintf(file, "LOAD %d %d\n", buffer[i].param1, buffer[i].param2);
|
|
303
|
+ } else if (buffer[i].instruction == STORE) {
|
|
304
|
+ fprintf(file, "STORE %d %d\n", buffer[i].param1, buffer[i].param2);
|
|
305
|
+ } else if (buffer[i].instruction == LOADI) {
|
|
306
|
+ fprintf(file, "LOADI %d %d\n", buffer[i].param1, buffer[i].param2);
|
|
307
|
+ } else if (buffer[i].instruction == STOREI) {
|
|
308
|
+ fprintf(file, "STOREI %d %d\n", buffer[i].param1, buffer[i].param2);
|
|
309
|
+ } else if (buffer[i].instruction == STOREA) {
|
|
310
|
+ fprintf(file, "STOREA %d %d\n", buffer[i].param1, buffer[i].param2);
|
341
|
311
|
|
|
312
|
+ } else if (buffer[i].instruction == JMP) {
|
|
313
|
+ fprintf(file, "JMP %d\n", traduction_JMP[buffer[i].param1]);
|
|
314
|
+ } else if (buffer[i].instruction == JMZ) {
|
|
315
|
+ fprintf(file, "JMZ %d\n", traduction_JMP[buffer[i].param1]);
|
|
316
|
+ } else if (buffer[i].instruction == GET) {
|
|
317
|
+ fprintf(file, "GET %d\n", buffer[i].param1);
|
|
318
|
+ } else if (buffer[i].instruction == PRI) {
|
|
319
|
+ fprintf(file, "PRI %d\n", buffer[i].param1);
|
342
|
320
|
|
|
321
|
+ } else if (buffer[i].instruction == CALL) {
|
|
322
|
+ fprintf(file, "CALL %d %d\n", traduction_JMP[buffer[i].param1], buffer[i].param2);
|
|
323
|
+ } else if (buffer[i].instruction == RET) {
|
|
324
|
+ fprintf(file, "RET\n");
|
|
325
|
+ } else if (buffer[i].instruction == STOP) {
|
|
326
|
+ fprintf(file, "STOP %d\n", buffer[i].param1);
|
|
327
|
+ }
|
|
328
|
+ i++;
|
|
329
|
+ }
|
|
330
|
+}
|
343
|
331
|
|
|
332
|
+// INTERN FUNCTION
|
|
333
|
+// Write binary value of n in buff
|
|
334
|
+void int_2_bin(char * buff, int n) {
|
|
335
|
+ int _m = n;
|
|
336
|
+ for (int i = 0; i < 32; i++) {
|
|
337
|
+ buff[31 - i] = ((_m & (1 << 31)) ? '1' : '0');
|
|
338
|
+ _m = _m << 1;
|
|
339
|
+ }
|
|
340
|
+}
|
344
|
341
|
|
|
342
|
+// INTERN FUNCTION
|
|
343
|
+// Write binary value of value in buff on N bits
|
|
344
|
+void convert_to_binary_on_N(int value, int N, char * buff) {
|
|
345
|
+ char tampon[33];
|
|
346
|
+ int_2_bin(tampon, value);
|
|
347
|
+ int i;
|
|
348
|
+ for (i = N-1; i>=0; i--) {
|
|
349
|
+ buff[N-1-i] = tampon[i];
|
|
350
|
+ }
|
|
351
|
+ buff[N] = '\0';
|
|
352
|
+}
|
345
|
353
|
|
|
354
|
+// INTERN FUNCTION
|
|
355
|
+// Write a binary instruction in the given file
|
|
356
|
+// If not compact ("010..10" & ) else only (010..10)
|
|
357
|
+void write_instruction_binary(FILE * file, struct str_instruction instr, char compact) {
|
|
358
|
+ char buff1[33];
|
|
359
|
+ char buff2[33];
|
|
360
|
+ char buff3[33];
|
|
361
|
+ char buff4[33];
|
|
362
|
+ convert_to_binary_on_N(instr.instruction, NB_BITS_INSTRUCTION, buff1);
|
|
363
|
+ if (instr.instruction == JMP || instr.instruction == JMZ || instr.instruction == CALL) {
|
|
364
|
+ convert_to_binary_on_N(traduction_JMP[instr.param1], NB_BITS, buff2);
|
|
365
|
+ } else {
|
|
366
|
+ convert_to_binary_on_N(instr.param1, NB_BITS, buff2);
|
|
367
|
+ }
|
|
368
|
+ convert_to_binary_on_N(instr.param2, NB_BITS, buff3);
|
|
369
|
+ convert_to_binary_on_N(instr.param3, NB_BITS, buff4);
|
|
370
|
+ if (compact) {
|
|
371
|
+ fprintf(file, "%s%s%s%s", buff1, buff2, buff3, buff4);
|
|
372
|
+ } else {
|
|
373
|
+ fprintf(file, "\"%s%s%s%s\" & ", buff1, buff2, buff3, buff4);
|
|
374
|
+ }
|
|
375
|
+}
|
346
|
376
|
|
347
|
|
-
|
348
|
|
-
|
|
377
|
+// Write the binary code in the given file
|
|
378
|
+void write_code_machine(FILE * file, char compact) {
|
|
379
|
+ if (compact) {
|
|
380
|
+ int i = MEM_INST_SIZE - 1;
|
|
381
|
+ while (i>=0) {
|
|
382
|
+ write_instruction_binary(file, buffer[i], 0);
|
|
383
|
+ i--;
|
|
384
|
+ }
|
|
385
|
+ } else {
|
|
386
|
+ printf(file, "\"");
|
|
387
|
+ int i = MEM_INST_SIZE - 1;
|
|
388
|
+ while (i>=0) {
|
|
389
|
+ write_instruction_binary(file, buffer[i], 1);
|
|
390
|
+ i--;
|
|
391
|
+ }
|
|
392
|
+ printf(file, "\"\n");
|
|
393
|
+ }
|
|
394
|
+}
|