#include "tables.h" #define NB_REG 4 #define MEM_SIZE 16 #define MEM_INST_SIZE 256 #define NB_BITS_INSTRUCTION 5 #define NB_BITS 8 /**************************************************/ /**************************************************/ /***************** Initialisation *****************/ /**************************************************/ /**************************************************/ // Buffer to patch Jumps difference due to adding LOAD and STORE int traduction_JMP[MEM_INST_SIZE]; // Index of the buffer int last_instruction = 0; // Structure coding an instruction struct str_instruction { enum instruction_t instruction; int param1; int param2; int param3; }; // Buffer to store registers oriented instructions struct str_instruction buffer[3*MEM_INST_SIZE]; // Structure coding an address and the correpondance with a register struct case_adresse { int adresse; int registre; char modifie; }; // Buffer of addresses (Memory) struct case_adresse tableau[MEM_SIZE]; // Buffer to manage priority policy int registres[NB_REG]; // Initialise all : the addresses-registers association table, the registers priority table, the instructions buffer, the instruction address association table void init (void) { int i; struct case_adresse case_courante = {0, -1, 0}; for (i=0; i=0; i--) { buff[N-1-i] = tampon[i]; } buff[N] = '\0'; } // INTERN FUNCTION // Write a binary instruction in the given file // If not compact ("010..10" & ) else only (010..10) void write_instruction_binary(FILE * file, struct str_instruction instr, char compact) { char buff1[33]; char buff2[33]; char buff3[33]; char buff4[33]; convert_to_binary_on_N(instr.instruction, NB_BITS_INSTRUCTION, buff1); if (instr.instruction == JMP || instr.instruction == JMZ || instr.instruction == CALL) { convert_to_binary_on_N(traduction_JMP[instr.param1], NB_BITS, buff2); } else { convert_to_binary_on_N(instr.param1, NB_BITS, buff2); } convert_to_binary_on_N(instr.param2, NB_BITS, buff3); convert_to_binary_on_N(instr.param3, NB_BITS, buff4); if (compact) { fprintf(file, "%s%s%s%s", buff1, buff2, buff3, buff4); } else { fprintf(file, "\"%s%s%s%s\" & ", buff1, buff2, buff3, buff4); } } // Write the binary code in the given file void write_code_machine(FILE * file, char compact) { if (compact) { int i = MEM_INST_SIZE - 1; while (i>=0) { write_instruction_binary(file, buffer[i], 0); i--; } } else { fprintf(file, "\""); int i = MEM_INST_SIZE - 1; while (i>=0) { write_instruction_binary(file, buffer[i], 1); i--; } fprintf(file, "\"\n"); } }