2021-12-16 16:24:48 +01:00
|
|
|
#include<stdio.h>
|
|
|
|
#include<stdlib.h>
|
2021-12-17 10:36:49 +01:00
|
|
|
#include<string.h>
|
2021-12-16 16:24:48 +01:00
|
|
|
#include<time.h> /* Utilisé pour obtenir une adresse de démarrage aléatoire */
|
|
|
|
#include"mars_types.h" /* importe les types utilisés dans le programme */
|
2021-12-17 10:36:49 +01:00
|
|
|
#include"options.h" /* Inclus les options du programme */
|
|
|
|
|
2021-12-17 16:38:22 +01:00
|
|
|
void initialiser_memoire(char* fichier, int player, int positions[2]){
|
2021-12-17 12:11:49 +01:00
|
|
|
/* Boucle qui vient placer le programme dans la mémoire */
|
|
|
|
} /* Va initialiser la mémoire en plaçant les deux programmes dans des adresses aléatoires */
|
2021-12-17 10:36:49 +01:00
|
|
|
|
2021-12-17 16:38:22 +01:00
|
|
|
int calculer_position(int posActuelle, short int modeAddr, short int valeur, struct adresse memoire[TAILLE_MEM]){
|
|
|
|
/* Permet de calculer l'adresse en fonction de la position actuelle et le mode d'adressage */
|
|
|
|
if(modeAddr == DIRECT){
|
|
|
|
return (posActuelle + (TAILLE_MEM + valeur)) % TAILLE_MEM;
|
|
|
|
} else if (modeAddr == INDIRECT){
|
|
|
|
/* Va voir à la position + valeur, */
|
2021-12-17 17:14:04 +01:00
|
|
|
int posToCheck = (posActuelle + (TAILLE_MEM + valeur)) % TAILLE_MEM;
|
|
|
|
union mars_instruction val;
|
|
|
|
val.instructLL = memoire[posToCheck].instruction;
|
2021-12-17 19:08:59 +01:00
|
|
|
|
|
|
|
if(val.instruct.codeOp == DAT){
|
|
|
|
return (posActuelle + (TAILLE_MEM + val.instruct.ArgB) % TAILLE_MEM);
|
|
|
|
}
|
|
|
|
|
2021-12-17 17:14:04 +01:00
|
|
|
|
2021-12-17 16:38:22 +01:00
|
|
|
} else if(modeAddr == IMMEDIAT) {
|
|
|
|
/* Retourne l'arguement de la position */
|
|
|
|
return valeur;
|
|
|
|
}
|
2021-12-17 10:36:49 +01:00
|
|
|
|
2021-12-17 19:08:59 +01:00
|
|
|
return valeur; /* retour par defaut */
|
2021-12-17 16:38:22 +01:00
|
|
|
}
|
2021-12-17 12:11:49 +01:00
|
|
|
|
2021-12-17 16:38:22 +01:00
|
|
|
void execute_instruction(union mars_instruction instruction, int player, struct adresse memoire[TAILLE_MEM], int positions[2]){
|
|
|
|
short int operation = instruction.instruct.codeOp; /* Pour rendre le code un peu plus lisible */
|
|
|
|
int position = positions[player];
|
|
|
|
printf("Position dans le exec: %d\n", position);
|
2021-12-17 12:11:49 +01:00
|
|
|
switch (operation){
|
2021-12-17 16:38:22 +01:00
|
|
|
case MOV:{
|
|
|
|
short int modeaddrA = instruction.instruct.modeAddrA;
|
|
|
|
|
|
|
|
if(modeaddrA == IMMEDIAT){
|
|
|
|
/* On écrit l'argument A dans la mémoire à l'aggr B */
|
|
|
|
union mars_instruction to_write;
|
|
|
|
to_write.instruct.codeOp = 0;
|
|
|
|
to_write.instruct.ArgB = to_write.instruct.ArgA;
|
|
|
|
int position = calculer_position(position, instruction.instruct.modeAddrB, instruction.instruct.ArgB, memoire);
|
|
|
|
memoire[position].instruction = to_write.instructLL;
|
|
|
|
memoire[position].player = player;
|
2021-12-17 17:14:04 +01:00
|
|
|
/* Ecrit un DAT avec la valeur demandée par le VIRUS */
|
2021-12-17 16:38:22 +01:00
|
|
|
} else {
|
2021-12-17 17:14:04 +01:00
|
|
|
/* Gérer les autres opérations de mov ici */
|
|
|
|
int position_1 = calculer_position(position, instruction.instruct.modeAddrA, instruction.instruct.ArgA, memoire);
|
|
|
|
int position_2 = calculer_position(position, instruction.instruct.modeAddrB, instruction.instruct.ArgB, memoire);
|
|
|
|
|
|
|
|
memoire[position_2].instruction = memoire[position_1].instruction;
|
|
|
|
memoire[position_2].player = player;
|
2021-12-17 19:08:59 +01:00
|
|
|
|
2021-12-17 17:14:04 +01:00
|
|
|
/* @APPEL GRAPHIQUE */
|
2021-12-17 12:11:49 +01:00
|
|
|
|
2021-12-17 16:38:22 +01:00
|
|
|
}
|
2021-12-17 17:14:04 +01:00
|
|
|
|
2021-12-17 16:38:22 +01:00
|
|
|
}
|
2021-12-17 12:11:49 +01:00
|
|
|
|
2021-12-17 16:38:22 +01:00
|
|
|
case ADD: {
|
2021-12-17 19:08:59 +01:00
|
|
|
int premiere_valeur = 0;
|
2021-12-17 16:38:22 +01:00
|
|
|
/* Addition de deux cases mémoires ? */
|
2021-12-17 19:08:59 +01:00
|
|
|
if (instruction.instruct.modeAddrA == IMMEDIAT){
|
|
|
|
/* Si le mode d'adressage pour le arg A est immediat alors valeur = a*/
|
|
|
|
premiere_valeur = instruction.instruct.ArgA;
|
|
|
|
} else {
|
|
|
|
int pos_to_check = calculer_position(position, instruction.instruct.modeAddrA, instruction.instruct.ArgA, memoire);
|
|
|
|
union mars_instruction to_read;
|
|
|
|
to_read.instructLL = memoire[pos_to_check].instruction;
|
|
|
|
|
|
|
|
if (to_read.instruct.codeOp == DAT){
|
|
|
|
premiere_valeur = to_read.instruct.ArgB;
|
|
|
|
} else {
|
|
|
|
/* Je ne sais pas si on peut continuer le programme mais ça doit être une errreur */
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int deuxieme_valeur = 0;
|
|
|
|
int next_pos_to_check = calculer_position(position, instruction.instruct.modeAddrB, instruction.instruct.ArgB, memoire);
|
|
|
|
union mars_instruction next_instruction;
|
|
|
|
next_instruction.instructLL = memoire[next_pos_to_check].instruction;
|
|
|
|
if (next_instruction.instruct.codeOp == DAT){
|
|
|
|
next_instruction.instruct.ArgB += premiere_valeur;
|
|
|
|
memoire[next_pos_to_check].instruction = next_instruction.instructLL;
|
|
|
|
memoire[next_pos_to_check].player = player;
|
|
|
|
} else {
|
|
|
|
/* Peut-on dire que ça fail ici ? */
|
|
|
|
}
|
|
|
|
|
2021-12-17 16:38:22 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 19:08:59 +01:00
|
|
|
case SUB:{
|
|
|
|
|
|
|
|
}
|
2021-12-17 13:36:49 +01:00
|
|
|
case JMP: {
|
2021-12-17 13:29:04 +01:00
|
|
|
short int modeadresse = instruction.instruct.modeAddrA;
|
2021-12-17 16:38:22 +01:00
|
|
|
int position_calc = calculer_position(position, modeadresse, instruction.instruct.ArgA, memoire);
|
|
|
|
printf("Positon calculée: %d\n", position_calc);
|
2021-12-17 19:08:59 +01:00
|
|
|
positions[player] = position_calc; /* On change la position du joueur */
|
|
|
|
}
|
|
|
|
case DJZ:{
|
|
|
|
|
2021-12-17 13:36:49 +01:00
|
|
|
}
|
2021-12-17 19:08:59 +01:00
|
|
|
|
|
|
|
case CMP: {
|
2021-12-17 14:15:33 +01:00
|
|
|
/* Compare deux valeurs dans la mémoire */
|
2021-12-17 19:08:59 +01:00
|
|
|
int pos_1, pos_2;
|
|
|
|
|
|
|
|
pos_1 = calculer_position(position, instruction.instruct.modeAddrA, instruction.instruct.ArgA, memoire);
|
|
|
|
pos_2 = calculer_position(position, instruction.instruct.modeAddrB, instruction.instruct.ArgB, memoire);
|
|
|
|
|
|
|
|
if (memoire[pos_1].instruction != memoire[pos_2].instruction){
|
|
|
|
positions[player] += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-12-17 12:11:49 +01:00
|
|
|
case DAT:
|
2021-12-17 14:15:33 +01:00
|
|
|
/* Instruction qui rapporte des données */
|
2021-12-17 12:11:49 +01:00
|
|
|
default:
|
|
|
|
break;
|
2021-12-17 10:36:49 +01:00
|
|
|
}
|
2021-12-17 16:38:22 +01:00
|
|
|
printf("Position après exécution: %d\n", positions[player]);
|
2021-12-17 10:36:49 +01:00
|
|
|
}
|
2021-12-16 16:24:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]){
|
2021-12-17 10:36:49 +01:00
|
|
|
srand(time(NULL)); /* Initialise le pseudo aléatoire pour tout le programme */
|
|
|
|
|
|
|
|
struct adresse memoire[TAILLE_MEM]; // Initialisation de la mémoire 0->7999
|
2021-12-17 16:38:22 +01:00
|
|
|
int positions[2] = {0,10}; /* Position des joueurs */
|
2021-12-16 16:24:48 +01:00
|
|
|
|
|
|
|
if (argc < 3){
|
|
|
|
printf("Vous n'avez pas rentrez assez d'arguments \n");
|
2021-12-17 19:08:59 +01:00
|
|
|
printf("Exemple d'utilisation: %s fichier1.mars fichier2.mars\n", argv[0]);
|
2021-12-16 16:24:48 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else if (argc > 3){
|
|
|
|
printf("Vous avez utilisé trop d'arguments \n");
|
2021-12-17 19:08:59 +01:00
|
|
|
printf("Exemple d'utilisation: %s fichier1.mars fichier2.mars\n", argv[0]);
|
2021-12-16 16:24:48 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* fichier1;
|
|
|
|
FILE* fichier2;
|
|
|
|
|
|
|
|
fichier1 = fopen(argv[1], "r");
|
|
|
|
fichier2 = fopen(argv[2], "r");
|
|
|
|
|
|
|
|
if(fichier1 == NULL || fichier2 == NULL){
|
2021-12-17 19:08:59 +01:00
|
|
|
printf("Erreur d'ouverture sur un des 2 fichiers !\n");
|
2021-12-16 16:24:48 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:42:33 +01:00
|
|
|
/* To do: faire le programme qui lit le fichier compilé */
|
|
|
|
|
2021-12-16 16:24:48 +01:00
|
|
|
fclose(fichier1);
|
|
|
|
fclose(fichier2);
|
2021-12-17 10:36:49 +01:00
|
|
|
|
2021-12-17 19:08:59 +01:00
|
|
|
/* USED FOR DEBUG */
|
2021-12-17 10:36:49 +01:00
|
|
|
union mars_instruction to_do;
|
2021-12-17 19:08:59 +01:00
|
|
|
to_do.instruct.codeOp = MOV;
|
2021-12-17 14:15:33 +01:00
|
|
|
to_do.instruct.ArgA = 753;
|
2021-12-17 19:08:59 +01:00
|
|
|
to_do.instruct.ArgB = 10;
|
2021-12-17 16:38:22 +01:00
|
|
|
to_do.instruct.modeAddrA = DIRECT;
|
|
|
|
to_do.instruct.modeAddrB = DIRECT;
|
2021-12-17 19:08:59 +01:00
|
|
|
|
|
|
|
union mars_instruction test_value;
|
|
|
|
test_value.instruct.codeOp = DAT;
|
|
|
|
test_value.instruct.ArgB = 80;
|
|
|
|
|
|
|
|
memoire[763].instruction = test_value.instructLL;
|
|
|
|
memoire[763].player = 1;
|
|
|
|
|
2021-12-17 16:38:22 +01:00
|
|
|
execute_instruction(to_do, 1, memoire, positions);
|
2021-12-17 19:08:59 +01:00
|
|
|
|
|
|
|
/* DEBUG END */
|
|
|
|
|
2021-12-16 16:24:48 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|