summaryrefslogtreecommitdiff
path: root/lib/simulator.c
blob: 4ac415c3404fb5b096b4b4ca5d1b8dde10d2dd91 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <stdio.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define _(x) x
#endif

#include "alu.h"
#include "simulator.h"
#include "interne.h"
#include "registre.h"
#include "memoire.h"
#include "fpu.h"
#include "exceptions.h"
#include "linker.h"

/* 
 initialisation de la MP avec un malloc
 fermeture du prog et free
 catch du ctrl-c 
 */


int HasToRun = 1, HasToReset;

Uint32 base_addr = 0;


Uint32 LireInstruction(void)
{
	return (LD(LireRegistrePC()));
}

void IncrementeCompteurOrdinal(void)
{
	EcrireRegistrePC(AdditionNonSigne(LireRegistrePC(), 1));
}

Uint32 Adresse(Uint32 u, Uint32 instruction)
{
	Uint32 tmp;

	switch (champ(u, 2)) {
	case 0:
		exception(1, _("Adresse: Call With Invalid r/m Field State ( r/m=00 )"));
		return (0);
	case 1:
		tmp = LireInstruction();
		IncrementeCompteurOrdinal();
		return (tmp);
	case 2:
		return (LireRegistre(Champ3(instruction)));	/* Adresse dans registre */
	case 3:
		tmp = LireRegistre(Champ3(instruction)) + LireInstruction();	/* Adresse dans registre + decalage de nouvelle instruction */
		IncrementeCompteurOrdinal();
		return (tmp);
	default:
		exception(1, _("Adresse: Unmatched Addr Field"));
		return (0);
	}
}


void Initialisation(void)
{
	int i;

	for (i = 0; i < TAILLE_MEMOIRE; i++)
		Reset(&memoire_principale[i]);
	InitRegistres();	/* initialisation des registres */
	EcrireRegistreSP(ADD_SP);	/* initialisation du stack pointer */
}

void DecodeExec(Uint32 instruction, Uint32 entrypoint)
{
	Uint32 champ_registre_resultat, val1, val2, resultat;
	int test1, test2;
	Uint32 val;		/* valeur qui va etre stockée */

	if (Opcode(instruction) & 0x80) {
		fpu(Opcode(instruction));
	} else {
		switch (Opcode(instruction)) {
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
			/* ALU */
			champ_registre_resultat = Champ1(instruction);	/* Champ du registre dans lequel va etre stocké le résultat */
			val1 = LireRegistre(Champ2(instruction));	/* Premier entier qui va etre utilisé dans l'opération */
			if (Opcode(instruction) < 6) {
				if (ValeurBit(Extension(instruction), 0) == 0)
					val2 = LireRegistre(Champ3(instruction));	/* Deuxième entier, stocké dans un registre, qui va etre utilisé dans l'opération */
				else {
					val2 = LireInstruction();	/* Deuxième entier, stocké après l'instruction, qui va etre utilisé dans l'opération */
					IncrementeCompteurOrdinal();
				}
			}
			if (ValeurBit(Extension(instruction), 1) == 0) {	/* Teste si l'opération est signée ou pas */
				switch (Opcode(instruction)) {
				case 0:
					resultat = AdditionNonSigne(val1, val2);
					break;
				case 1:
					resultat = SoustractionNonSigne(val1, val2);
					break;
				case 2:
					resultat = MultiplicationNonSigne(val1, val2);
					break;
				case 3:
					resultat = DivisionNonSigne(val1, val2);
					break;
				case 4:
					resultat = AND(val1, val2);
					break;
				case 5:
					resultat = OR(val1, val2);
					break;
				case 6:
					resultat = SHL(val1);
					break;
				case 7:
					resultat = SHR(val1);
					break;
				}
			} else {
				switch (Opcode(instruction)) {
				case 0:
					resultat = AdditionSigne(val1, val2);
					break;
				case 1:
					resultat = SoustractionSigne(val1, val2);
					break;
				case 2:
					resultat = MultiplicationSigne(val1, val2);
					break;
				case 3:
					resultat = DivisionSigne(val1, val2);
					break;
				case 4:
					resultat = AND(val1, val2);
					break;
				case 5:
					resultat = OR(val1, val2);
					break;
				case 6:
					resultat = SHL(val1);
					break;
				case 7:
					resultat = SHR(val1);
					break;
				}
			}
			if ((Opcode(instruction) & 2) && !(Opcode(instruction & 3))) {
				EcrireRegistreRG(resultat);
				EcrireRegistreRD(SecondResult);
			} else {
				EcrireRegistre(champ_registre_resultat, resultat);	/* On écrit le résultat dans le registre de sortie */
			}
			break;
		case 8:	/*  MOV  */
			if (ValeurBit(Extension(instruction), 4) == 1) {	/* MOV conditionnel  */
				if (ValeurBit(Extension(instruction), 5) == 0) {	/* Test normal */
					switch (champ(Extension(instruction) >> 2, 4)) {	/* teste les bits 2 et 3 */
					case 0:
						if (Overflow() == 1)
							goto fin;
					case 1:
						if (Zero() == 1)
							goto fin;
					case 2:
						if (Sign() == 1)
							goto fin;
					case 3:
						if (Parity() == 1)
							goto fin;
					}
				} else {	/* Negation du test */
					switch (champ(Extension(instruction) >> 2, 4)) {	/* teste les bits 2 et 3 */
					case 0:
						if (Overflow() == 0)
							goto fin;
					case 1:
						if (Zero() == 0)
							goto fin;
					case 2:
						if (Sign() == 0)
							goto fin;
					case 3:
						if (Parity() == 0)
							goto fin;
					}
				}
			}
			/* Pas de MOV conditionnel */
			if (ValeurBit(Extension(instruction), 1) == 0) {	/* Mov arg1 arg2 */
				if (ValeurBit(Extension(instruction), 0) == 0) {	/* arg2 = reg */
					if (champ(Champ1(instruction), 2) == 0) {	/* r/m de arg1 = 0 */
						EcrireRegistre(Champ3(instruction), LireRegistre(Champ2(instruction)));
					} else {
						ST(Adresse(Champ1(instruction), instruction),
						   LireRegistre(Champ2(instruction)));
					}
				} else {	/* arg2 = imm32 */
					if (champ(Champ1(instruction), 2) == 0) {	/* r/m de arg1 = 0 */
						EcrireRegistre(Champ3(instruction), LireInstruction());
						IncrementeCompteurOrdinal();
					} else {
						ST(Adresse(Champ1(instruction), instruction), LireInstruction());
						IncrementeCompteurOrdinal();
					}
				}
			} else {
				if (ValeurBit(Extension(instruction), 0) == 0) {	/* arg2 = reg */
					if (champ(Champ1(instruction), 2) == 0) {	/* r/m de arg1 = 0 */
						EcrireRegistre(Champ2(instruction), LireRegistre(Champ3(instruction)));

					} else {
						EcrireRegistre(Champ2(instruction),
							       LD(Adresse(Champ1(instruction), instruction)));
					}
				} else {	/* arg2 = imm32 */
					exception(1, _("MOV: Memory to Memory Forbidden On This Type Of Processor"));
				}
			}
		      fin:
			break;

		case 9:	/*  NOP */
			/*  Instruction nulle   */
			break;

		case 10:	/*  J[cond]   */
		case 11:

			switch (champ(Extension(instruction), 4)) {
			case 0:
				if (Champ1(instruction) == Champ2(instruction)) {
					test1 = 1;
				} else {
					test1 = LireRegistre(Champ1(instruction)) == LireRegistre(Champ2(instruction));
				}
				break;
			case 1:
				test1 = LireRegistre(Champ1(instruction)) != LireRegistre(Champ2(instruction));
				break;
			case 2:
				test1 = LireRegistre(Champ1(instruction)) < LireRegistre(Champ2(instruction));
				break;
			case 3:
				test1 = LireRegistre(Champ1(instruction)) <= LireRegistre(Champ2(instruction));
				break;
			}
			switch (champ(Extension(instruction) >> 2, 4)) {
			case 0:
				test2 = Overflow();
				break;
			case 1:
				test2 = Zero();
				break;
			case 2:
				test2 = Sign();
				break;
			case 3:
				test2 = Parity();
				break;
			}
			switch (champ(Extension(instruction) >> 4, 4)) {
			case 0:
				test1 = test1;
				break;
			case 1:
				test1 = test1 || test2;
				break;
			case 2:
				test1 = test1 && !test2;
				break;
			case 3:
				test1 = test1 || !test2;
				break;
			}
			if (test1) {
				Uint32 tmp;


				tmp = LireInstruction();
				if (Opcode(instruction) & 1) {
					tmp += LireRegistrePC();
				}
				EcrireRegistrePC(tmp);
			}
			break;

		case 12:	/*  JMP */
		case 13:
			if (ValeurBit(Extension(instruction), 0) == 0) {
				/* RET */
				EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), Champ1(instruction)));
				EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), 1));
				EcrireRegistrePC(LD(LireRegistreSP()));

			} else if (ValeurBit(Extension(instruction), 1) == 0) {	/* JMP */
				if (ValeurBit(Extension(instruction), 2) == 0) {
					EcrireRegistrePC(LireRegistre(Champ1(instruction)));
				} else {
					EcrireRegistrePC(LireInstruction());
				}
			} else {	/* CALL */
				ST(LireRegistreSP(), LireRegistrePC());
				EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1));
				if (ValeurBit(Extension(instruction), 2) == 0) {
					EcrireRegistrePC(LireRegistre(Champ1(instruction)));
				} else {
					EcrireRegistrePC(LireInstruction());
				}
			}
			break;
		case 14:	/*  PUSH    */

			if (ValeurBit(Extension(instruction), 0) == 0)
				val = LireRegistre(Champ1(instruction));
			else {
				val = LireInstruction();
				IncrementeCompteurOrdinal();
			}
			ST(LireRegistreSP(), val);
			EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1));
			break;
		case 15:	/*  POP */
			EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), 1));
			EcrireRegistre(Champ1(instruction), LD(LireRegistreSP()));

			break;
		case 127:	/*  HALT-RESET  */
			if (ValeurBit(Extension(instruction), 0)) {
				HasToRun = 0;	/* Halt */
			} else {
				HasToReset = 1;	/* Reset */
				ResetRegistres();
			}
			break;
		default:
			exception(1, _("DecodeExec: Invalid Opcode"));
		}
	}
}
void Traitement(Uint32 entrypoint)
{				/* ******************** FIXMI ************************* */
	Uint32 instruction;

	while (HasToRun) {
		EcrireRegistrePC(entrypoint);
		HasToRun = 1;
		HasToReset = 0;
		while ((HasToRun) && (!HasToReset)) {
			instruction = LireInstruction();
			IncrementeCompteurOrdinal();
			DecodeExec(instruction, entrypoint);
		}
	}
}

void AfficheReg(void)		//  affiche reg
{
	int i, j;

	for (i = 0; i <= 3; i++) {
		for (j = 1; j <= 8; j++) {
			printf("   R%02d   ", (i * 8 + j));
		}
		printf("\n");
		for (j = 1; j <= 8; j++) {
			printf("%08lX ", (registre[i * 8 + j - 1]));
		}
		printf("\n");
	}
	printf("Rg: %08lX  | Rd: %08lX | Flag: %08lX | PC: %08lX\n", LireRegistreRG(), LireRegistreRD(),
	       LireRegistreFLAG(), registre[REG_PC]);
	printf("\n");
}

void Debogueur(void)
{
	AfficheReg();
}

void ChargeBinaire(char *filename)
{
	FILE *file;
	char message[BUFSIZ];
	Uint32 entrypoint, nb, ns, nbss, nr;
	Uint32 *relocation_table;
	int i;

	file = openfilereading(filename);
	if (readword(file) != 0x58454e4e) {	/* verification de la signature */
		exception(1, _("Signature invalid"));
	}
	sprintf(message, _("Total Size Of The Binary File: %d"), readword(file));
	pushcontext(message);
	nb = readword(file);	/* taille du segment text */
	ns = readword(file);	/* taille des donnes statiques */
	nbss = readword(file);	/* taille des donnees non init */
	nr = readword(file);	/* taille de la table de relogement */

	relocation_table = Emalloc(nr);
	for (i = 0; i < nr; i++) {
		relocation_table[i] = readword(file);
	}


	for (i = base_addr; i < (base_addr + nb + ns); i++) {	/*chargement en ram de .text et .data */
		ST(i, readword(file));
	}

	for (i = 0; i < nr; i++) {	/* relogement */
		ST(base_addr + relocation_table[i], LD(base_addr + relocation_table[i]) + base_addr);
	}

	free(relocation_table);

	InitRegistres();	/* initialisation des registres */
	EcrireRegistreSP(ADD_SP);	/* initialisation du stack pointer */

	EcrireRegistrePC(LireRegistrePC() + base_addr);	/* maj de PC */

	base_addr += nb + ns + nbss;
	fclose(file);
}