Example #1
0
        private void populateITypeExecutors()
        {
            ITypeExecutors = new Dictionary <Opcode, Action <ITypeInstruction, IStateAccessor> >
            {
                { Opcode.AddImmediate, (r, StateAccessor) => {
                      var a = (int)StateAccessor.ReadRegister(r.Rs);
                      StateAccessor.WriteRegister(r.Rt, (uint)(a + ((short)r.Immediate)));
                  } },
                { Opcode.UnsignedAddImmediate, (r, StateAccessor) => {
                      var a = StateAccessor.ReadRegister(r.Rs);
                      StateAccessor.WriteRegister(r.Rt, (uint)(a + (short)r.Immediate));
                  } },
                { Opcode.BitwiseAndImmediate, (r, StateAccessor) => {
                      var a = StateAccessor.ReadRegister(r.Rs);
                      StateAccessor.WriteRegister(r.Rt, a & r.Immediate);
                  } },
                { Opcode.BranchOnEqual, (r, StateAccessor) => {
                      var rs = StateAccessor.ReadRegister(r.Rs);
                      var rt = StateAccessor.ReadRegister(r.Rt);
                      if (rs == rt)
                      {
                          StateAccessor.SetPc((uint)(StateAccessor.GetPc() + (short)(r.Immediate << 2)));
                      }
                  } },
                { Opcode.Branch, (r, StateAccessor) => {
                      var rs = (int)StateAccessor.ReadRegister(r.Rs);
                      var rt = r.Rt;
                      switch (rt)
                      {
                      case 0:
                          if (rs < 0)
                          {
                              StateAccessor.SetPc((uint)(StateAccessor.GetPc() + (short)(r.Immediate << 2)));
                          }
                          break;

                      case 1:
                          if (rs >= 0)
                          {
                              StateAccessor.SetPc((uint)(StateAccessor.GetPc() + (short)(r.Immediate << 2)));
                          }
                          break;

                      case 16:
                          StateAccessor.WriteRegister(31, StateAccessor.GetPc() + 4);
                          if (rs < 0)
                          {
                              StateAccessor.SetPc((uint)(StateAccessor.GetPc() + (short)(r.Immediate << 2)));
                          }
                          break;

                      case 17:
                          StateAccessor.WriteRegister(31, StateAccessor.GetPc() + 4);
                          if (rs >= 0)
                          {
                              StateAccessor.SetPc((uint)(StateAccessor.GetPc() + (short)(r.Immediate << 2)));
                          }
                          break;

                      default:
                          throw new InvalidOperationException(String.Format("Invalid Branch {0}", rt));
                      }
                  } },
                { Opcode.BranchOnGreaterThanZero, (r, StateAccessor) => {
                      var rs = (int)StateAccessor.ReadRegister(r.Rs);
                      var rt = StateAccessor.ReadRegister(r.Rt);
                      if (rs > 0)
                      {
                          StateAccessor.SetPc((uint)(StateAccessor.GetPc() + (short)(r.Immediate << 2)));
                      }
                  } },
                { Opcode.BranchOnLessThanOrEqualToZero, (r, StateAccessor) => {
                      var rs = (int)StateAccessor.ReadRegister(r.Rs);
                      var rt = StateAccessor.ReadRegister(r.Rt);
                      if (rs <= 0)
                      {
                          StateAccessor.SetPc((uint)(StateAccessor.GetPc() + (short)(r.Immediate << 2)));
                      }
                  } },
                { Opcode.BranchOnNotEqual, (r, StateAccessor) => {
                      var rs = StateAccessor.ReadRegister(r.Rs);
                      var rt = StateAccessor.ReadRegister(r.Rt);
                      if (rs != rt)
                      {
                          StateAccessor.SetPc((uint)(StateAccessor.GetPc() + (short)(r.Immediate << 2)));
                      }
                  } },
                { Opcode.LoadByte, (r, StateAccessor) => {
                      var  ptr = (uint)(((short)r.Immediate) + (int)StateAccessor.ReadRegister(r.Rs));
                      byte dt;
                      StateAccessor.GetMemoryData(ptr, out dt);
                      var data = (uint)((int)((sbyte)(dt)));
                      StateAccessor.WriteRegister(r.Rt, data);
                  } },
                { Opcode.UnsignedLoadByte, (r, StateAccessor) => {
                      var  ptr = (uint)(((short)r.Immediate) + (int)StateAccessor.ReadRegister(r.Rs));
                      byte dt;
                      StateAccessor.GetMemoryData(ptr, out dt);
                      var data = (uint)dt;
                      StateAccessor.WriteRegister(r.Rt, data);
                  } },
                { Opcode.LoadHalfword, (r, StateAccessor) => {
                      var    ptr = (uint)(((short)r.Immediate) + (int)StateAccessor.ReadRegister(r.Rs));
                      UInt16 dt;
                      StateAccessor.GetMemoryData(ptr, out dt);
                      var data = (uint)((int)((short)DataConverterUInt16(dt)));
                      StateAccessor.WriteRegister(r.Rt, data);
                  } },
                { Opcode.UnsignedLoadHalfword, (r, StateAccessor) => {
                      var    ptr = (uint)(((short)r.Immediate) + (int)StateAccessor.ReadRegister(r.Rs));
                      UInt16 dt;
                      StateAccessor.GetMemoryData(ptr, out dt);
                      var data = DataConverterUInt16(dt);
                      StateAccessor.WriteRegister(r.Rt, data);
                  } },
                { Opcode.LoadUpperImmediate, (r, StateAccessor) => {
                      StateAccessor.WriteRegister(r.Rt, r.Immediate << 16);
                  } },
                { Opcode.LoadWord, (r, StateAccessor) => {
                      var    ptr = (uint)(((short)r.Immediate) + (int)StateAccessor.ReadRegister(r.Rs));
                      UInt32 data;
                      StateAccessor.GetMemoryData(ptr, out data);
                      data = DataConverterUInt32(data);
                      StateAccessor.WriteRegister(r.Rt, data);
                  } },
                { Opcode.BitwiseOrImmediate, (r, StateAccessor) => {
                      var a = StateAccessor.ReadRegister(r.Rs);
                      StateAccessor.WriteRegister(r.Rt, a | r.Immediate);
                  } },
                { Opcode.StoreByte, (r, StateAccessor) => {
                      var ptr  = (uint)(((short)r.Immediate) + (int)StateAccessor.ReadRegister(r.Rs));
                      var data = (byte)(StateAccessor.ReadRegister(r.Rt));
                      StateAccessor.SetMemoryData(ptr, data);
                  } },
                { Opcode.SetOnLessThanImmediate, (r, StateAccessor) => {
                      var a = (int)StateAccessor.ReadRegister(r.Rs);
                      StateAccessor.WriteRegister(r.Rt, a < (short)r.Immediate ? 1u : 0u);
                  } },
                { Opcode.UnsignedSetOnLessThanImmediate, (r, StateAccessor) => {
                      var a = StateAccessor.ReadRegister(r.Rs);
                      StateAccessor.WriteRegister(r.Rt, a < r.Immediate ? 1u : 0u);
                  } },
                { Opcode.StoreHalfword, (r, StateAccessor) => {
                      var ptr = (uint)(((short)r.Immediate) + (int)StateAccessor.ReadRegister(r.Rs));
                      var dt  = (ushort)StateAccessor.ReadRegister(r.Rt);
                      dt = DataConverterUInt16(dt);
                      StateAccessor.SetMemoryData(ptr, (ushort)dt);
                  } },
                { Opcode.StoreWord, (r, StateAccessor) => {
                      var ptr  = (uint)(((short)r.Immediate) + (int)StateAccessor.ReadRegister(r.Rs));
                      var data = DataConverterUInt32(StateAccessor.ReadRegister(r.Rt));
                      StateAccessor.SetMemoryData(ptr, data);
                  } },
                { Opcode.BitwiseExclusiveOrImmediate, (r, StateAccessor) => {
                      var a = StateAccessor.ReadRegister(r.Rs);
                      StateAccessor.WriteRegister(r.Rt, a ^ r.Immediate);
                  } },
            };
        }