MIPS I Collection Tools

lxXTaCoXxl 0 Tallied Votes 277 Views Share

Figured I've been working on material for a while and would throw these up for everyone that needs them in the future to use. It's very simple implementation; if I get enough people asking me to, I will write the methods for each of the instructions so you don't have to do it yourselves. I know Jump and Link was hard enough for me. :)

Welcome,
Jamie

// Masks
		public const int Mask5Bit = 0x1f;
        public const int Mask16Bit = 0xffff;
        public const int Mask26Bit = 0x3fffffc;

        // R Types
        public const int SllOpcode = 0x00000000;
        public const int SrlOpcode = 0x00000002;
        public const int SraOpcode = 0x00000003;
        public const int SllvOpcode = 0x00000004;
        public const int SravOpcode = 0x00000007;
        public const int JrOpcode = 0x00000008;
        public const int JalrOpcode = 0x00000009;
        public const int SyscallOpcode = 0x0000000c;
        public const int BreakOpcode = 0x0000000d;
        public const int MfhiOpcode = 0x00000010;
        public const int MthiOpcode = 0x00000011;
        public const int MfloOpcode = 0x00000012;
        public const int MtloOpcode = 0x00000013;
        public const int MultOpcode = 0x00000018;
        public const int MultuOpcode = 0x00000019;
        public const int DivOpcode = 0x0000001a;
        public const int DivuOpcode = 0x0000001b;
        public const int AddOpcode = 0x00000020;
        public const int AdduOpcode = 0x00000021;
        public const int SubOpcode = 0x00000022;
        public const int SubuOpcode = 0x00000023;
        public const int AndOpcode = 0x00000024;
        public const int OrOpcode = 0x00000025;
        public const int XorOpcode = 0x00000026;
        public const int NorOpcode = 0x00000027;
        public const int SltOpcode = 0x0000002a;
        public const int SltuOpcode = 0x0000002b;

        // I Types
        public const int BltzOpcode = 0x04000000;
        public const int BgezOpcode = 0x04010000;
        public const int BeqOpcode = 0x10000000;
        public const int BneOpcode = 0x14000000;
        public const int BlezOpcode = 0x18000000;
        public const int BgtzOpcode = 0x1c000000;
        public const int AddiOpcode = 0x20000000;
        public const int AddiuOpcode = 0x24000000;
        public const int SltiOpcode = 0x28000000;
        public const int AndiOpcode = 0x30000000;
        public const int OriOpcode = 0x34000000;
        public const int XoriOpcode = 0x38000000;
        public const int LuiOpcode = 0x3c000000;
        public const uint LbOpcode = 0x80000000;
        public const uint LhOpcode = 0x84000000;
        public const uint LwOpcode = 0x8c000000;
        public const uint LbuOpcode = 0x90000000;
        public const uint LhuOpcode = 0x94000000;
        public const uint SbOpcode = 0xa0000000;
        public const uint ShOpcode = 0xa4000000;
        public const uint SwOpcode = 0xac000000;
        public const uint Lwc1Opcode = 0xc4000000;
        public const uint Swc1Opcode = 0xe4000000;

        // J Types
        public const int JOpcode = 0x08000000;
        public const int JalOpcode = 0x0c000000;
		
		// Jump and Link Example:
		public static string Link(string LinkTo) {
            if (!CheckForIllegals(LinkTo)) {
                int Link = Convert.ToInt32(LinkTo, 16);
                return ((((Link & Mask26Bit) >> 2) | JalOpcode).ToString("x").PadLeft(8, '0'));
            }

            return "0c000000";
        }

		// Collection of illegal characters in a hex address.
        public static char[] IllegalCharacters = new char[52] { 'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X',
            'Y','Z','`','~','!','@','#','$','%','^','&','*','(',')','_','-','+','=','{','}','[',']','\\','/','|',':',';','\'','\"',',',
            '.','?','<','>'
        };

		// Method to check for any illegals.
        public static bool CheckForIllegals(string Address) {
            for (int i = 0; i < 52; i++) {
                if (Address.Contains(IllegalCharacters[i])) {
                    MessageBox.Show("Address/Data contained an illegal character. Address/Data has been set to null (00000000)");
                    return true;
                }
            }

            return false;
        }

		// Removes write mode for handling raw data.
        public static string RemoveWriteMode(string Address) { return "0" + Address.Remove(0, 1); }
		
		// Inserts write mode for output to user.
        public static string Insert32BitWrite(string Address) { return "2" + Address.Remove(0, 1); }

		// Convert a number to hex.
        public static string ToHex(int NumberToConvert) { return NumberToConvert.ToString("x"); }
		
		// Convert a hex number to decimal.
        public static int ToDecimal(string Hex) { return Convert.ToInt32(Hex, 16); }

		// Get the offset value between address 1 and address 2.
        public static string GetOffset(string Address1, string Address2) {
            return ToHex(ToDecimal(Address1) - ToDecimal(Address2)).PadLeft(8, '0');
        }

		// Add an offset to an address.
        public static string AddOffset(string Address1, string Offset) {
            return ToHex(ToDecimal(Address1) + ToDecimal(Offset.PadLeft(8, '0')));
        }
lxXTaCoXxl 26 Posting Whiz in Training

Change data at line 3 to 0xffff; the 0xfffc is with the 2 lower bits dropped.

lxXTaCoXxl 26 Posting Whiz in Training

Also change lines 106 and 111 to:

// 106:
return ToHex(ToDecimal(Address1) - ToDecimal(Offset.PadRight(8, '0')));

// 111:
return ToHex(ToDecimal(Address1) + ToDecimal(Offset.PadRight(8, '0')));
skatamatic 371 Practically a Posting Shark

What is the purpose of this? Is it for use with a MIPS compatible IDE?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.