Example #1
0
        public string GetHexString(string mipsCode, bool isWithNewLines, bool isBytePartition)
        {
            ProgramInfo     prog     = ParseMips(mipsCode);
            MachineCodePack codePack = prog.ToMachineCode();
            string          hex      = codePack.ToHexString(false);

            if (isBytePartition || isWithNewLines)
            {
                StringBuilder partitioned = new StringBuilder();
                int           cursor      = 0;
                while (cursor < hex.Length)
                {
                    partitioned.Append(hex.Substring(cursor, 2));
                    cursor += 2;
                    if (cursor % 8 == 0 && isWithNewLines)
                    {
                        partitioned.Append("\n");
                    }
                    else if (isBytePartition)
                    {
                        partitioned.Append(" ");
                    }
                }
                return(partitioned.ToString());
            }
            return(hex);
        }
Example #2
0
        public string GetBinaryString(string mipsCode, bool isWithNewLines)
        {
            ProgramInfo     prog     = ParseMips(mipsCode);
            MachineCodePack codePack = prog.ToMachineCode();

            return(codePack.ToBinaryString(isWithNewLines));
        }
Example #3
0
 // set the new binary
 public void Reset(MachineCodePack machineCode)
 {
     this.machineCode = machineCode;
     this.Pc          = 0;
     this.IsHalt      = false;
     this.Register    = new RegisterStorage(this.RegisterLogger);
     // this.Ram = new RamStorage(this.RamLogger);
     this.Ram   = new RamArrayStorage(this.RamLogger);
     this.Codes = new CodeReader(this.Ram, machineCode);
 }
Example #4
0
        // disassemble
        public string GetMipsString(uint address, uint length4Bytes)
        {
            MachineCodePack machineCodes = new MachineCodePack();
            uint            i;

            for (i = 0; i < length4Bytes; i++)
            {
                machineCodes.Add(this.GetOneMachineCode(address + i * 4));
            }
            return(machineCodes.ToMipsString());
        }
Example #5
0
        // write machine codes to storage
        public CodeReader(IStorage <uint> storage, MachineCodePack machineCodes)
        {
            _storage = storage;
            uint addr = 0;

            foreach (Word32b word in machineCodes)
            {
                this._storage.Write(addr, word);
                addr += 4;
            }
        }
Example #6
0
        private static void WriteRawTextCodeToRam(string filePath, MipsMachine vm, CodingSystem coding)
        {
            string rawTextCode;

            try
            {
                rawTextCode = File.ReadAllText(filePath);
            }
            catch (Exception)
            {
                Console.WriteLine("[Error] Invalid file");
                return;
            }
            MachineCodePack machineCode = new MachineCodePack(rawTextCode, coding);

            vm.Reset(machineCode);

            // print the next instructions
            PrintNextInstructions(vm, 16);
            // // print changes
            // PrintChanges(vm);
            vm.CleanLog();
        }
Example #7
0
        static void Main(string[] args)
        {
            MipsMachine  vm        = new MipsMachine();
            MipsToBinary converter = new MipsToBinary();

            Prologue();

            // interactive
            while (true)
            {
                bool isLittleEndian = true;
                uint address;
                uint length4Bytes = 1;

                Console.Write("> ");
                string[] input = Console.ReadLine().Split();
                switch (input[0])
                {
                case "c":      // continue
                    Continue(vm);
                    break;

                case "s":      // go to next step
                    Step(vm);
                    break;

                case "r":      // read register
                               // `r t0`
                    RegisterType reg = (RegisterType)Enum.Parse(typeof(RegisterType), input[1]);
                    if (input.Length >= 3)
                    {
                        bool.TryParse(input[2], out isLittleEndian);
                    }
                    Console.WriteLine($"0x{vm.QueryRegister(reg).ToHexString(isLittleEndian ? Endian.LittleEndian : Endian.BigEndian)}");
                    break;

                case "d":      // read RAM
                               // `d <address> <length/4Bytes> <0:little-endian/1:big-endian>`
                    try
                    {
                        address = (uint)new System.ComponentModel.UInt32Converter().ConvertFromString(input[1]);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("[Error] Invalid Address");
                        continue;
                    }
                    if (input.Length >= 3)
                    {
                        uint.TryParse(input[2], out length4Bytes);
                    }
                    if (input.Length >= 4)
                    {
                        bool.TryParse(input[3], out isLittleEndian);
                    }
                    Console.WriteLine($"0x {vm.QueryRamAsHex(address, length4Bytes, isLittleEndian ? Endian.LittleEndian : Endian.BigEndian)}");
                    break;

                case "u":      // read RAM as instructions
                               // binarty -> MIPS asm
                               // `u <address> <length/4Bytes>`
                    try
                    {
                        address = (uint)new System.ComponentModel.UInt32Converter().ConvertFromString(input[1]);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("[Error] Invalid Address");
                        continue;
                    }
                    if (input.Length >= 3)
                    {
                        uint.TryParse(input[2], out length4Bytes);
                    }
                    Console.WriteLine(vm.GetMipsString(address, length4Bytes));

                    break;

                case "a":      // write MIPS codes to RAM
                               // `a`
                               // `a <path to asm>`
                    string mips;
                    if (input.Length > 1)
                    {
                        mips = File.ReadAllText(input[1]);
                    }
                    else
                    {
                        mips = AskForMipsCode();
                    }
                    ProgramInfo     prog        = converter.ParseMips(mips);
                    MachineCodePack machineCode = prog.ToMachineCode();
                    vm.Reset(machineCode);
                    // print the next instructions
                    PrintNextInstructions(vm, 16);
                    // do not log the changes caused by program loading
                    vm.CleanLog();
                    break;

                case "h":      // write hex to RAM
                               // `h <path to hex>`
                    WriteRawTextCodeToRam(input[1], vm, CodingSystem.Hex);
                    break;

                case "b":      // write binary to RAM
                               // `b <path to binary>`
                    WriteRawTextCodeToRam(input[1], vm, CodingSystem.Binary);
                    break;
                }
            }
        }
Example #8
0
 public MipsMachine(MachineCodePack machineCode)
 {
     Reset(machineCode);
 }