diff --git a/mars_main.c b/mars_main.c index fdcafd1..382e81f 100644 --- a/mars_main.c +++ b/mars_main.c @@ -12,22 +12,24 @@ void initialiser_memoire(char* fichier, int player, int positions[2]){ 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){ - printf("valeur: %d\n", valeur); - printf("Pos actuelle: %d\n", posActuelle); return (posActuelle + (TAILLE_MEM + valeur)) % TAILLE_MEM; } else if (modeAddr == INDIRECT){ /* Va voir à la position + valeur, */ int posToCheck = (posActuelle + (TAILLE_MEM + valeur)) % TAILLE_MEM; union mars_instruction val; val.instructLL = memoire[posToCheck].instruction; - printf("Valeur stockée dans la ram: %d", val.instruct.ArgB); + + if(val.instruct.codeOp == DAT){ + return (posActuelle + (TAILLE_MEM + val.instruct.ArgB) % TAILLE_MEM); + } + } else if(modeAddr == IMMEDIAT) { /* Retourne l'arguement de la position */ return valeur; } - return valeur; + return valeur; /* retour par defaut */ } void execute_instruction(union mars_instruction instruction, int player, struct adresse memoire[TAILLE_MEM], int positions[2]){ @@ -54,7 +56,7 @@ void execute_instruction(union mars_instruction instruction, int player, struct memoire[position_2].instruction = memoire[position_1].instruction; memoire[position_2].player = player; - memoire[position_1].instruction = 0; + /* @APPEL GRAPHIQUE */ } @@ -62,21 +64,63 @@ void execute_instruction(union mars_instruction instruction, int player, struct } case ADD: { + int premiere_valeur = 0; /* Addition de deux cases mémoires ? */ + 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 ? */ + } + } - case SUB: - /* Soustraction */ + case SUB:{ + + } case JMP: { short int modeadresse = instruction.instruct.modeAddrA; int position_calc = calculer_position(position, modeadresse, instruction.instruct.ArgA, memoire); printf("Positon calculée: %d\n", position_calc); - positions[player] = position; /* On change la position du joueur */ + positions[player] = position_calc; /* On change la position du joueur */ } - case DJZ: - /* */ - case CMP: + case DJZ:{ + + } + + case CMP: { /* Compare deux valeurs dans la mémoire */ + 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; + } + + } case DAT: /* Instruction qui rapporte des données */ default: @@ -94,11 +138,11 @@ int main(int argc, char* argv[]){ if (argc < 3){ printf("Vous n'avez pas rentrez assez d'arguments \n"); - printf("Exemple d'utilisation: %s fichier1.cass fichier2.cass\n", argv[0]); + printf("Exemple d'utilisation: %s fichier1.mars fichier2.mars\n", argv[0]); return EXIT_FAILURE; } else if (argc > 3){ printf("Vous avez utilisé trop d'arguments \n"); - printf("Exemple d'utilisation: %s fichier1.cass fichier2.cass\n", argv[0]); + printf("Exemple d'utilisation: %s fichier1.mars fichier2.mars\n", argv[0]); return EXIT_FAILURE; } @@ -109,25 +153,33 @@ int main(int argc, char* argv[]){ fichier2 = fopen(argv[2], "r"); if(fichier1 == NULL || fichier2 == NULL){ - printf("Erreur d'ouverture du fichier !\n"); + printf("Erreur d'ouverture sur un des 2 fichiers !\n"); return EXIT_FAILURE; } - printf("Fichier 1: %s\n", argv[1]); - printf("Fichier 2: %s\n", argv[2]); - /* To do: faire le programme qui lit le fichier compilé */ fclose(fichier1); fclose(fichier2); + /* USED FOR DEBUG */ union mars_instruction to_do; - to_do.instruct.codeOp = JMP; + to_do.instruct.codeOp = MOV; to_do.instruct.ArgA = 753; - to_do.instruct.ArgB = 0; + to_do.instruct.ArgB = 10; to_do.instruct.modeAddrA = DIRECT; to_do.instruct.modeAddrB = DIRECT; - printf("Valeur instruction: %lld\n", to_do.instructLL); + + 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; + execute_instruction(to_do, 1, memoire, positions); + + /* DEBUG END */ + return EXIT_SUCCESS; }