From 78216ace0ab196ba5d632893bde4ebafd698da5d Mon Sep 17 00:00:00 2001 From: pfaure Date: Mon, 10 May 2021 14:15:26 +0200 Subject: [PATCH] OK a tester --- al.lex | 9 ++-- as.y | 127 +++++++++++++++++++++++++------------------------- tables.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++----- tables.h | 13 ++++-- toto.asm | 43 +++++++++++++++-- 5 files changed, 242 insertions(+), 88 deletions(-) diff --git a/al.lex b/al.lex index c6fe54f..b935478 100644 --- a/al.lex +++ b/al.lex @@ -13,7 +13,7 @@ yyerror (char const *s) %% "ADD" { return tADD ;} -"SUB" { return tSUB;} +"SOU" { return tSUB;} "MUL" { return tMUL; } "DIV" { return tDIV; } "INF" { return tINF; } @@ -23,7 +23,6 @@ yyerror (char const *s) "AFC" { return tAFC; } "COP" { return tCPY; } "AFCA" { return tAFCA; } -"COPA" { return tCPYA; } "READ" { return tREAD; } @@ -42,9 +41,9 @@ yyerror (char const *s) [0-9]+ { yylval.nombre = atoi(yytext); return tNB; } -"\n" -" " -"\t" +"\n" {} +" " {} +"\t" {} %% diff --git a/as.y b/as.y index b472542..079e057 100644 --- a/as.y +++ b/as.y @@ -6,11 +6,12 @@ #include FILE * file; +FILE * file2; %} %token tMUL tDIV tADD tSUB tINF tSUP tEQU -%token tAFC tCPY tAFCA tCPYA +%token tAFC tCPY tAFCA %token tREAD tWR %token tJMP tJMF %token tGET tPRI @@ -25,134 +26,132 @@ Programme : Instruction; Instruction : tMUL tNB tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_src1 = get_reg_read($3, file, &added_instruction); - int reg_src2 = get_reg_read($4, file, &added_instruction); - fprintf(file, "MUL %d %d %d\n", reg_dest, reg_src1, reg_src2); + int reg_dest = get_reg_write($2, &added_instruction); + int reg_src1 = get_reg_read($3, &added_instruction); + int reg_src2 = get_reg_read($4, &added_instruction); + add_instruction(MUL, reg_dest, reg_src1, reg_src2); new_instruction(added_instruction + 1);}; Instruction : tADD tNB tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_src1 = get_reg_read($3, file, &added_instruction); - int reg_src2 = get_reg_read($4, file, &added_instruction); - fprintf(file, "ADD %d %d %d\n", reg_dest, reg_src1, reg_src2); + int reg_dest = get_reg_write($2, &added_instruction); + int reg_src1 = get_reg_read($3, &added_instruction); + int reg_src2 = get_reg_read($4, &added_instruction); + add_instruction(ADD, reg_dest, reg_src1, reg_src2); new_instruction(added_instruction + 1);}; Instruction : tDIV tNB tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_src1 = get_reg_read($3, file, &added_instruction); - int reg_src2 = get_reg_read($4, file, &added_instruction); - fprintf(file, "DIV %d %d %d\n", reg_dest, reg_src1, reg_src2); + int reg_dest = get_reg_write($2, &added_instruction); + int reg_src1 = get_reg_read($3, &added_instruction); + int reg_src2 = get_reg_read($4, &added_instruction); + add_instruction(DIV, reg_dest, reg_src1, reg_src2); new_instruction(added_instruction + 1);}; Instruction : tSUB tNB tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_src1 = get_reg_read($3, file, &added_instruction); - int reg_src2 = get_reg_read($4, file, &added_instruction); - fprintf(file, "SUB %d %d %d\n", reg_dest, reg_src1, reg_src2); + int reg_dest = get_reg_write($2, &added_instruction); + int reg_src1 = get_reg_read($3, &added_instruction); + int reg_src2 = get_reg_read($4, &added_instruction); + add_instruction(SUB, reg_dest, reg_src1, reg_src2); new_instruction(added_instruction + 1);}; Instruction : tINF tNB tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_src1 = get_reg_read($3, file, &added_instruction); - int reg_src2 = get_reg_read($4, file, &added_instruction); - fprintf(file, "INF %d %d %d\n", reg_dest, reg_src1, reg_src2); + int reg_dest = get_reg_write($2, &added_instruction); + int reg_src1 = get_reg_read($3, &added_instruction); + int reg_src2 = get_reg_read($4, &added_instruction); + add_instruction(INF, reg_dest, reg_src1, reg_src2); new_instruction(added_instruction + 1);}; Instruction : tSUP tNB tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_src1 = get_reg_read($3, file, &added_instruction); - int reg_src2 = get_reg_read($4, file, &added_instruction); - fprintf(file, "SUP %d %d %d\n", reg_dest, reg_src1, reg_src2); + int reg_dest = get_reg_write($2, &added_instruction); + int reg_src1 = get_reg_read($3, &added_instruction); + int reg_src2 = get_reg_read($4, &added_instruction); + add_instruction(SUP, reg_dest, reg_src1, reg_src2); new_instruction(added_instruction + 1);}; Instruction : tEQU tNB tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_src1 = get_reg_read($3, file, &added_instruction); - int reg_src2 = get_reg_read($4, file, &added_instruction); - fprintf(file, "EQU %d %d %d\n", reg_dest, reg_src1, reg_src2); + int reg_dest = get_reg_write($2, &added_instruction); + int reg_src1 = get_reg_read($3, &added_instruction); + int reg_src2 = get_reg_read($4, &added_instruction); + add_instruction(EQU, reg_dest, reg_src1, reg_src2); new_instruction(added_instruction + 1);}; Instruction : tAFC tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - fprintf(file, "AFC %d %d\n", reg_dest, $3); + int reg_dest = get_reg_write($2, &added_instruction); + add_instruction(AFC, reg_dest, $3, 0); new_instruction(added_instruction + 1);}; Instruction : tCPY tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_src = get_reg_read($3, file, &added_instruction); - fprintf(file, "CPY %d %d\n", reg_dest, reg_src); + int reg_dest = get_reg_write($2, &added_instruction); + int reg_src = get_reg_read($3, &added_instruction); + add_instruction(CPY, reg_dest, reg_src, 0); new_instruction(added_instruction + 1);}; Instruction : tAFCA tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - fprintf(file, "AFCA %d %d\n", reg_dest, $3); - new_instruction(added_instruction + 1);}; -Instruction : tCPYA tNB tNB {increment_time(); - int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_src = get_reg_read($3, file, &added_instruction); - fprintf(file, "CPYA %d %d\n", reg_dest, reg_src); - new_instruction(added_instruction + 1);}; + int reg_aux = get_reg_write(-1, &added_instruction); + add_instruction(AFC, reg_aux, $3, 0); + add_instruction(STOREA, $2, reg_aux, 0); + new_instruction(added_instruction + 2);}; Instruction : tJMP tNB {increment_time(); - fprintf(file, "JMP %d\n", $2); + add_instruction(JMP, $2, 0, 0); new_instruction(1);}; Instruction : tJMF tNB tNB {increment_time(); int added_instruction = 0; - int reg_src = get_reg_read($2, file, &added_instruction); - int reg_aux = get_reg_write(-1, file, &added_instruction); - fprintf(file, "SUB %d %d %d\n", reg_aux, reg_aux, reg_src); - fprintf(file, "JMZ %d\n", $3); + int reg_src = get_reg_read($2, &added_instruction); + int reg_aux = get_reg_write(-1, &added_instruction); + add_instruction(SUB, reg_aux, reg_aux, reg_src); + add_instruction(JMZ, $3, 0, 0); new_instruction(added_instruction + 2);}; Instruction : tWR tNB tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - int reg_addr = get_reg_read($3, file, &added_instruction); - fprintf(file, "LOADA %d %d\n", reg_dest, reg_addr); + int reg_dest = get_reg_write($2, &added_instruction); + int reg_addr = get_reg_read($3, &added_instruction); + add_instruction(LOADI, reg_dest, reg_addr, 0); new_instruction(added_instruction + 1);}; Instruction : tREAD tNB tNB {increment_time(); int added_instruction = 0; - int reg_addr = get_reg_read($2, file, &added_instruction); - int reg_value = get_reg_read($3, file, &added_instruction); - fprintf(file, "STOREA %d %d\n", reg_addr, reg_value); + int reg_addr = get_reg_read($2, &added_instruction); + int reg_value = get_reg_read($3, &added_instruction); + add_instruction(STOREI, reg_addr, reg_value, 0); new_instruction(added_instruction + 1);}; Instruction : tGET tNB {increment_time(); int added_instruction = 0; - int reg_dest = get_reg_write($2, file, &added_instruction); - fprintf(file, "GET %d\n", reg_dest); + int reg_dest = get_reg_write($2, &added_instruction); + add_instruction(GET, reg_dest, 0, 0); new_instruction(added_instruction + 1);}; Instruction : tPRI tNB {increment_time(); int added_instruction = 0; - int reg_src = get_reg_read($2, file, &added_instruction); - fprintf(file, "PRI %d\n", reg_src); + int reg_src = get_reg_read($2, &added_instruction); + add_instruction(PRI, reg_src, 0, 0); new_instruction(added_instruction + 1);}; Instruction : tCALL tNB tNB {increment_time(); int added_instruction = flush_and_init(file); - fprintf(file, "CALL %d %d\n", $2, $3); + add_instruction(STOP, $2, $3, 0); new_instruction(added_instruction + 1);}; Instruction : tRET {increment_time(); int added_instruction = flush_and_init(file); - fprintf(file, "RET\n"); + add_instruction(RET, 0, 0, 0); new_instruction(added_instruction + 1);}; -Instruction : tSTOP {increment_time(); - fprintf(file, "STOP\n"); +Instruction : tSTOP tNB {increment_time(); + add_instruction(STOP, $2, 0, 0); new_instruction(1);}; %% int main(void) { - file = stdout; - init(); + file = fopen("output.asm", "w"); + file2 = fopen("output.bin", "w"); + init(); yyparse(); + write_asm(file); + write_code_machine(file2); return 0; } diff --git a/tables.c b/tables.c index f879642..11c109a 100644 --- a/tables.c +++ b/tables.c @@ -13,9 +13,122 @@ #include "tables.h" #define NB_REG 4 -#define MEM_SIZE 1024 -#define NB_INSTRUCTIONS 1024 +#define MEM_SIZE 16 +#define NB_INSTRUCTIONS 64 +#define MEM_INST_SIZE 64 +#define NB_BITS_INSTRUCTION 5 +#define NB_BITS 8 +int traduction_JMP[NB_INSTRUCTIONS]; + +struct str_instruction { + enum instruction_t instruction; + int param1; + int param2; + int param3; +}; + +int last_instruction = 0; +struct str_instruction buffer[3*NB_INSTRUCTIONS]; + +void add_instruction(enum instruction_t inst, int param1, int param2, int param3) { + struct str_instruction my_instruction = {inst, param1, param2, param3}; + buffer[last_instruction] = my_instruction; + last_instruction++; +} + +void write_asm(FILE * file) { + int i = 0; + while (i=0; i--) { + buff[N-1-i] = tampon[i]; + } + buff[N] = '\0'; +} + +void write_instruction_binary(FILE * file, struct str_instruction instr) { + char buff1[33]; + char buff2[33]; + char buff3[33]; + char buff4[33]; + convert_to_binary_on_N(instr.instruction, NB_BITS_INSTRUCTION, buff1); + 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); + fprintf(file, "\"%s%s%s%s\" & ", buff1, buff2, buff3, buff4); +} + + +void write_code_machine(FILE * file) { + int i = MEM_INST_SIZE - 1; + while (i>=0) { + write_instruction_binary(file, buffer[i]); + i--; + } +} struct case_adresse { @@ -26,7 +139,6 @@ struct case_adresse { struct case_adresse tableau[MEM_SIZE]; int registres[NB_REG]; -int traduction_JMP[NB_INSTRUCTIONS]; void init (void) { int i; @@ -38,6 +150,10 @@ void init (void) { for (i=0; i #include +enum instruction_t {NOP, ADD, MUL, SUB, DIV, INF, SUP, EQU, CPY, AFC, LOAD, STORE, LOADI, STOREI, STOREA, JMP, JMZ, PRI, GET, CALL, RET, STOP}; + void init(void); void increment_time(); -int get_reg_read(int adresse, FILE * file, int * added_instruction); -int get_reg_write(int adresse, FILE * file, int * added_instruction); -int flush_and_init(FILE * file); +int get_reg_read(int adresse, int * added_instruction); +int get_reg_write(int adresse, int * added_instruction); +int flush_and_init(); void new_instruction(int nb_inst); +void write_asm(FILE * file); +void write_code_machine(FILE * file); +void add_instruction(enum instruction_t inst, int param1, int param2, int param3); #endif diff --git a/toto.asm b/toto.asm index bc58383..4350482 100644 --- a/toto.asm +++ b/toto.asm @@ -1,8 +1,41 @@ -AFC 0 1 +JMP 31 AFC 1 1 -AFC 2 1 +COP 2 0 AFC 3 1 -AFC 4 1 +MUL 1 3 1 +ADD 1 2 1 +AFC 2 10 +WR 1 2 +AFC 1 0 +COP 2 0 +AFC 3 1 +MUL 1 3 1 +ADD 1 2 1 +READ 1 1 +PRI 1 +STOP 15 +AFC 1 2 +COP 0 1 +RET +AFC 1 0 +COP 2 0 +AFC 3 1 +MUL 1 3 1 +ADD 1 2 1 +AFC 2 1 +WR 1 2 +COP 1 0 +CALL 1 1 +AFC 1 1 +COP 0 1 +RET +AFCA 5 0 +CALL 19 5 AFC 5 1 -AFC 6 1 -MUL 0 1 2 +AFCA 6 0 +AFC 7 1 +MUL 5 7 5 +ADD 5 6 5 +READ 5 5 +PRI 5 +STOP 0