#include "emul_inst_exec.h" #include <stdio.h> int emul_inst_exec_loadfile(char *filename) { return emul_inst_dec_load(filename); } void emul_inst_exec_closefile() { emul_inst_dec_close(); } /* オペコード解読処理群 - START */ void emul_inst_exec_add(int r0, int r1, int r2) { emul_reg_set(r0, emul_reg_get(r1) + emul_reg_get(r2)); printf("Execute: add r0[%d], r1[%d], r2[%d];\n", r0, r1, r2); } void emul_inst_exec_addi(int r0, int r1, int im) { emul_reg_set(r0, emul_reg_get(r1) + im); printf("Execute: addi r0[%d], r1[%d], im[%d];\n", r0, r1, im); } void emul_inst_exec_sub(int r0, int r1, int r2) { emul_reg_set(r0, emul_reg_get(r1) - emul_reg_get(r2)); printf("Execute: sub r0[%d], r1[%d], r2[%d];\n", r0, r1, r2); } void emul_inst_exec_slt(int r0, int r1, int r2) { if (emul_reg_get(r1) < emul_reg_get(r2)) emul_reg_set(r0, 1); else emul_reg_set(r0, 0); printf("Execute: slt r0[%d], r1[%d], r2[%d];\n", r0, r1, r2); } void emul_inst_exec_slti(int r0, int r1, int im) { if (emul_reg_get(r1) < im) emul_reg_set(r0, 1); else emul_reg_set(r0, 0); printf("Execute: slti r0[%d], r1[%d], im[%d];\n", r0, r1, im); } void emul_inst_exec_seq(int r0, int r1, int r2) { if (emul_reg_get(r1) == emul_reg_get(r2)) emul_reg_set(r0, 1); else emul_reg_set(r0, 0); printf("Execute: seq r0[%d], r1[%d], r2[%d];\n", r0, r1, r2); } void emul_inst_exec_sge(int r0, int r1, int r2) { if (emul_reg_get(r1) >= emul_reg_get(r2)) emul_reg_set(r0, 1); else emul_reg_set(r0, 0); printf("Execute: sge r0[%d], r1[%d], r2[%d];\n", r0, r1, r2); } void emul_inst_exec_sgt(int r0, int r1, int r2) { if (emul_reg_get(r1) >= emul_reg_get(r2)) emul_reg_set(r0, 1); else emul_reg_set(r0, 0); printf("Execute: sgt r0[%d], r1[%d], r2[%d];\n", r0, r1, r2); } void emul_inst_exec_sle(int r0, int r1, int r2) { if (emul_reg_get(r1) <= emul_reg_get(r2)) emul_reg_set(r0, 1); else emul_reg_set(r0, 0); printf("Execute: sle r0[%d], r1[%d], r2[%d];\n", r0, r1, r2); } void emul_inst_exec_sne(int r0, int r1, int r2) { if (emul_reg_get(r1) != emul_reg_get(r2)) emul_reg_set(r0, 1); else emul_reg_set(r0, 0); printf("Execute: sne r0[%d], r1[%d], r2[%d];\n", r0, r1, r2); } void emul_inst_exec_j(int target) { emul_reg_set(Spc, target); printf("Execute: j target[%d];\n", target); } void emul_inst_exec_beq(int r0, int r1, int label) { if (emul_reg_get(r0) == emul_reg_get(r1)) emul_reg_set(Spc, label); emul_reg_set(Spc, label); printf("Execute: beq r0[%d] r1[%d] label[%d];\n", r0, r1, label); } void emul_inst_exec_bne(int r0, int r1, int label) { if (emul_reg_get(r0) != emul_reg_get(r1)) emul_reg_set(Spc, label); printf("Execute: bne r0[%d] r1[%d] label[%d];\n", r0, r1, label); } void emul_inst_exec_lw(int r0, int address) { emul_reg_set(r0, emul_mem_get_word(address)); printf("Execute: lw r0[%d] address[%d];\n", r0, address); } void emul_inst_exec_sw(int r0, int address) { emul_mem_set_word(address, emul_reg_get(r0)); printf("Execute: sw r0[%d] address[%d];\n", r0, address); } /* オペコード解読処理群 - END */ /* Execute Step * RETURN: * seccsess: 1 * failed : 0 */ int emul_inst_exec_step() #define COUNT_OPERAND_MAX 3 { char token[LOAD_TOKEN_MAX]; int op_id, oprs[COUNT_OPERAND_MAX]; // トークンを取得 emul_inst_dec_get_token(token); // オペコードIDを取得 op_id = emul_inst_dec_get_opecode(token); // 例外処理: オペコードが見つからない if ( op_id == OPECODE_DECODE_ERROR ) { printf("Non exist next operand\n"); return 0; } // オペコード実行 switch (op_id) { case ADD : emul_inst_exec_add( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); break; case ADDI: emul_inst_exec_addi( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_immediate() ); break; case SUB : emul_inst_exec_sub( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); break; case SLT : emul_inst_exec_slt( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); break; case SLTI: emul_inst_exec_slti( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_immediate() ); break; case SEQ : emul_inst_exec_seq( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); break; case SGE : emul_inst_exec_sge( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); break; case SGT : break; emul_inst_exec_sgt( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); case SLE : emul_inst_exec_sle( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); break; case SNE : emul_inst_exec_sne( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); break; //case B : // break; case BEQ : emul_inst_exec_beq( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_immediate() ); break; case BNE : emul_inst_exec_bne( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_immediate() ); break; case J : emul_inst_exec_j( emul_inst_dec_get_next_immediate() ); break; case LW : emul_inst_exec_lw( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); break; case SW : emul_inst_exec_sw( emul_inst_dec_get_next_operand(), emul_inst_dec_get_next_operand() ); break; case MOVE: break; default: printf("まだ実装してないので待って!\n"); } //emul_inst_dec_get_operand(token); return 1; } /* Execute Run All * RETURN: * seccsess: 1 * failed : 0 */ int emul_inst_exec_run() { // step関数を終わるまで呼び出せばよい while (emul_inst_exec_step()); return 0; } int main(void) { emul_inst_exec_loadfile("test.asm"); while ( emul_inst_exec_step() ) { puts("...Wait for step, please input key enter."); getchar(); } emul_inst_exec_closefile(); return 0; }