Esempio n. 1
0
 private void populateRTypeExecutors()
 {
     RTypeExecutors = new Dictionary <RTypeFunction, Action <RTypeInstruction, IStateAccessor> >
     {
         { RTypeFunction.Add, (r, StateAccessor) => {
               StateAccessor.WriteRegister(r.Rd, (uint)(((int)StateAccessor.ReadRegister(r.Rs)) + (int)(StateAccessor.ReadRegister(r.Rt))));
           } },
         { RTypeFunction.UnsignedAdd, (r, StateAccessor) => {
               StateAccessor.WriteRegister(r.Rd, StateAccessor.ReadRegister(r.Rs) + StateAccessor.ReadRegister(r.Rt));
           } },
         { RTypeFunction.BitwiseAnd, (r, StateAccessor) => {
               StateAccessor.WriteRegister(r.Rd, StateAccessor.ReadRegister(r.Rs) & StateAccessor.ReadRegister(r.Rt));
           } },
         { RTypeFunction.Break, (r, StateAccessor) => {
               StateAccessor.SetEpc(StateAccessor.GetPc());
               StateAccessor.SetPc(0x3c);
               StateAccessor.Skip();
           } },
         { RTypeFunction.Divide, (r, StateAccessor) => {
               var dividend = (int)StateAccessor.ReadRegister(r.Rs);
               var divisor  = (int)StateAccessor.ReadRegister(r.Rt);
               StateAccessor.SetHi((uint)(dividend % divisor));
               StateAccessor.SetLo((uint)(dividend / divisor));
           } },
         { RTypeFunction.UnsignedDivide, (r, StateAccessor) => {
               var dividend = StateAccessor.ReadRegister(r.Rs);
               var divisor  = StateAccessor.ReadRegister(r.Rt);
               StateAccessor.SetHi(dividend % divisor);
               StateAccessor.SetLo(dividend / divisor);
           } },
         { RTypeFunction.JumpAndLinkRegister, (r, StateAccessor) => {
               StateAccessor.WriteRegister(r.Rd, StateAccessor.GetPc() + 4);
               StateAccessor.SetPc(StateAccessor.ReadRegister(r.Rs));
           } },
         { RTypeFunction.JumpRegister, (r, StateAccessor) => {
               StateAccessor.SetPc(StateAccessor.ReadRegister(r.Rs));
           } },
         { RTypeFunction.MoveFromHi, (r, StateAccessor) => {
               StateAccessor.WriteRegister(r.Rd, StateAccessor.GetHi());
           } },
         { RTypeFunction.MoveFromLo, (r, StateAccessor) => {
               StateAccessor.WriteRegister(r.Rd, StateAccessor.GetLo());
           } },
         { RTypeFunction.MoveToHi, (r, StateAccessor) => {
               StateAccessor.SetHi(StateAccessor.ReadRegister(r.Rs));
           } },
         { RTypeFunction.MoveToLo, (r, StateAccessor) => {
               StateAccessor.SetLo(StateAccessor.ReadRegister(r.Rs));
           } },
         { RTypeFunction.Multiply, (r, StateAccessor) => {
               var  a      = (long)(int)StateAccessor.ReadRegister(r.Rs);
               var  b      = (long)(int)StateAccessor.ReadRegister(r.Rt);
               long result = a * b;
               StateAccessor.SetLo((uint)((int)result));
               StateAccessor.SetHi((uint)(result >> 32));
           } },
         { RTypeFunction.UnsignedMultiply, (r, StateAccessor) => {
               var a      = (ulong)StateAccessor.ReadRegister(r.Rs);
               var b      = (ulong)StateAccessor.ReadRegister(r.Rt);
               var result = a * b;
               StateAccessor.SetLo((uint)result);
               StateAccessor.SetHi((uint)(result >> 32));
           } },
         { RTypeFunction.BitwiseNor, (r, StateAccessor) => {
               StateAccessor.WriteRegister(r.Rd, ~(StateAccessor.ReadRegister(r.Rs) | StateAccessor.ReadRegister(r.Rt)));
           } },
         { RTypeFunction.BitwiseOr, (r, StateAccessor) => {
               StateAccessor.WriteRegister(r.Rd, (StateAccessor.ReadRegister(r.Rs) | StateAccessor.ReadRegister(r.Rt)));
           } },
         { RTypeFunction.ShiftLeftLogical, (r, StateAccessor) => {
               var a = StateAccessor.ReadRegister(r.Rt);
               var b = (int)r.Sa;
               StateAccessor.WriteRegister(r.Rd, a << b);
           } },
         { RTypeFunction.ShiftLeftLogicalVariable, (r, StateAccessor) => {
               var a = StateAccessor.ReadRegister(r.Rt);
               var b = (int)StateAccessor.ReadRegister(r.Rs);
               StateAccessor.WriteRegister(r.Rd, a << b);
           } },
         { RTypeFunction.SetOnLessThan, (r, StateAccessor) => {
               var a = (int)StateAccessor.ReadRegister(r.Rs);
               var b = (int)StateAccessor.ReadRegister(r.Rt);
               StateAccessor.WriteRegister(r.Rd, a < b ? 1u : 0u);
           } },
         { RTypeFunction.UnsignedSetOnLessThan, (r, StateAccessor) => {
               var a = StateAccessor.ReadRegister(r.Rs);
               var b = StateAccessor.ReadRegister(r.Rt);
               StateAccessor.WriteRegister(r.Rd, a < b ? 1u : 0u);
           } },
         { RTypeFunction.ShiftRightArithmetic, (r, StateAccessor) => {
               var a = (int)StateAccessor.ReadRegister(r.Rt);
               var b = (int)r.Sa;
               StateAccessor.WriteRegister(r.Rd, (uint)(a >> b));
           } },
         { RTypeFunction.ShiftRightArithmeticVariable, (r, StateAccessor) => {
               var a = (int)StateAccessor.ReadRegister(r.Rt);
               var b = (int)StateAccessor.ReadRegister(r.Rs);
               StateAccessor.WriteRegister(r.Rd, (uint)(a >> b));
           } },
         { RTypeFunction.ShiftRightLogical, (r, StateAccessor) => {
               var a = StateAccessor.ReadRegister(r.Rt);
               var b = (int)r.Sa;
               StateAccessor.WriteRegister(r.Rd, a >> b);
           } },
         { RTypeFunction.ShiftRightLogicalVariable, (r, StateAccessor) => {
               var a = StateAccessor.ReadRegister(r.Rt);
               var b = (int)StateAccessor.ReadRegister(r.Rs);
               StateAccessor.WriteRegister(r.Rd, a >> b);
           } },
         { RTypeFunction.Subtract, (r, StateAccessor) => {
               var a = (int)StateAccessor.ReadRegister(r.Rs);
               var b = (int)StateAccessor.ReadRegister(r.Rt);
               StateAccessor.WriteRegister(r.Rd, (uint)(a - b));
           } },
         { RTypeFunction.UnsignedSubtract, (r, StateAccessor) => {
               var a = (int)StateAccessor.ReadRegister(r.Rs);
               var b = (int)StateAccessor.ReadRegister(r.Rt);
               StateAccessor.WriteRegister(r.Rd, (uint)(a - b));
           } },
         { RTypeFunction.Syscall, (r, StateAccessor) => {
               StateAccessor.SetEpc(StateAccessor.GetPc());
               StateAccessor.SetPc(0x3c);
               StateAccessor.Skip();
           } },
         { RTypeFunction.BitwiseExclusiveOr, (r, StateAccessor) => {
               StateAccessor.WriteRegister(r.Rd, (StateAccessor.ReadRegister(r.Rs) ^ StateAccessor.ReadRegister(r.Rt)));
           } },
     };
 }