diff --git a/MakefileInst b/MakefileInst index 0e82e6f..7ca1754 100644 --- a/MakefileInst +++ b/MakefileInst @@ -1,4 +1,4 @@ -OBJS=emul_inst_exec.o emul_inst_decipher.o +OBJS=emul_inst_exec.o emul_inst_decipher.o emul_reg.o emul_mem.o CC=gcc PROGRAM=emulexec diff --git a/emul_inst_decipher.c b/emul_inst_decipher.c index c9745a4..166c873 100644 --- a/emul_inst_decipher.c +++ b/emul_inst_decipher.c @@ -1,5 +1,7 @@ #include "emul_inst_decipher.h" +#include #include +#include #include /* @@ -118,9 +120,9 @@ int emul_inst_dec_get_opecode(char *op) { int i; for (i = 0; i < ISA_COUNT; i++) { - if ( strcmp(isa_dict[i].string, op) == 0 ) { - return isa_dict[i].opecode; - } + if ( strcmp(isa_dict[i].string, op) == 0 ) { + return isa_dict[i].opecode; + } } return OPECODE_DECODE_ERROR; } @@ -152,3 +154,10 @@ emul_inst_dec_get_token(token); return emul_inst_dec_get_operand(token); } + +// immediate +int emul_inst_dec_get_next_immediate() { + char token[LOAD_TOKEN_MAX]; + emul_inst_dec_get_token(token); + return atoi(token); +} diff --git a/emul_inst_decipher.h b/emul_inst_decipher.h index b09568b..fd57f8c 100644 --- a/emul_inst_decipher.h +++ b/emul_inst_decipher.h @@ -41,4 +41,7 @@ int emul_inst_dec_get_next_operand(); +// immediate +int emul_inst_dec_get_next_immediate(); + #endif diff --git a/emul_inst_decipher.o b/emul_inst_decipher.o new file mode 100644 index 0000000..e80d65e --- /dev/null +++ b/emul_inst_decipher.o Binary files differ diff --git a/emul_inst_exec.c b/emul_inst_exec.c index c055ea0..28a71f9 100644 --- a/emul_inst_exec.c +++ b/emul_inst_exec.c @@ -15,10 +15,85 @@ 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 @@ -51,8 +126,13 @@ emul_inst_dec_get_next_operand() ); break; - //case ADDI: - // 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(), @@ -60,34 +140,90 @@ emul_inst_dec_get_next_operand() ); break; - //case SLT : - // break; - //case SLTI: - // break; - //case SEQ : - // break; - //case SGE : - // break; - //case SGT : - // break; - //case SLE : - // break; - //case SNE : - // 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 : - // break; - //case BNE : - // break; - //case J : - // break; - //case LW : - // break; - //case SW : - // break; - //case MOVE: - // 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"); } @@ -102,7 +238,8 @@ * failed : 0 */ int emul_inst_exec_run() { // step関数を終わるまで呼び出せばよい - return ; + while (emul_inst_exec_step()); + return 0; } int main(void) { diff --git a/emul_inst_exec.h b/emul_inst_exec.h index c5ed8e9..1be33b2 100644 --- a/emul_inst_exec.h +++ b/emul_inst_exec.h @@ -3,6 +3,7 @@ #include "emul_inst_decipher.h" #include "emul_reg.h" +#include "emul_mem.h" int emul_inst_exec_loadfile(char*); void emul_inst_exec_closefile(); diff --git a/emul_inst_exec.o b/emul_inst_exec.o new file mode 100644 index 0000000..6cced64 --- /dev/null +++ b/emul_inst_exec.o Binary files differ diff --git a/emul_mem.c b/emul_mem.c index ed3f33f..aa81831 100644 --- a/emul_mem.c +++ b/emul_mem.c @@ -2,3 +2,18 @@ char mem[EMUL_MEMSIZE]; +int emul_mem_get_word(int address) { + int word = 0; + word |= mem[address+0] << 0; + word |= mem[address+1] << 8; + word |= mem[address+2] << 16; + word |= mem[address+3] << 24; + return word; +} + +void emul_mem_set_word(int address, int word) { + mem[address+0] = (char)((word >> 0 ) & 0x000F); + mem[address+1] = (char)((word >> 8 ) & 0x000F); + mem[address+2] = (char)((word >> 16) & 0x000F); + mem[address+3] = (char)((word >> 24) & 0x000F); +} diff --git a/emul_mem.h b/emul_mem.h index 5e34d52..4682844 100644 --- a/emul_mem.h +++ b/emul_mem.h @@ -5,4 +5,8 @@ void add(); +int emul_mem_get_word(int address); + +void emul_mem_set_word(int address, int word); + #endif diff --git a/emul_mem.o b/emul_mem.o new file mode 100644 index 0000000..d029216 --- /dev/null +++ b/emul_mem.o Binary files differ diff --git a/emul_reg.c b/emul_reg.c index 8565c22..53e74fb 100644 --- a/emul_reg.c +++ b/emul_reg.c @@ -6,10 +6,10 @@ reg[Szero] = 0; } -int emul_reg_get(int r) { +int emul_reg_get(EMUL_REG r) { return reg[r]; } -void emul_reg_set(int r, int value) { +void emul_reg_set(EMUL_REG r, int value) { reg[r] = value; } diff --git a/emul_reg.h b/emul_reg.h index 28d9094..a671f7b 100644 --- a/emul_reg.h +++ b/emul_reg.h @@ -44,7 +44,9 @@ Sfp, // frame - Sra // return address + Sra, // return address + + Spc // program counter } EMUL_REG; @@ -52,7 +54,7 @@ * this must be called */ void emul_reg_init(); -int emul_reg_get(int r); -void emul_reg_set(int r, int value); +int emul_reg_get(EMUL_REG r); +void emul_reg_set(EMUL_REG r, int value); #endif diff --git a/emul_reg.o b/emul_reg.o new file mode 100644 index 0000000..9c2157d --- /dev/null +++ b/emul_reg.o Binary files differ diff --git a/emulexec.exe b/emulexec.exe new file mode 100644 index 0000000..e3c8cec --- /dev/null +++ b/emulexec.exe Binary files differ