Ejemplo n.º 1
0
        public static int JInstruction(string[][] programTextArray, ref int i, ref int j,
                                       Dictionary <uint, ExeCommand> commandsToExe)
        {
            var commandName       = programTextArray[i][j];
            int instructionLayout = JInstructionOpcode[commandName];
            var args = Healper.GetArgs(programTextArray, ref i, ref j, commandName == "jal" ? 2 : 1);

            commandsToExe[commandsToExe.Last().Key] = new ExeCommand {
                Args = args, Instraction = commandName, Line = i
            };
            if (int.TryParse(args[commandName == "jal" ? 1 : 0], out int number))
            {
                instructionLayout |= (commandName == "jal" ? registersOpcode[args[0]] << 7 : 0);
                commandsToExe[commandsToExe.Last().Key].Args[commandName == "jal" ? 1 : 0] = number.ToString();
            }
            else
            {
                throw new SimulatorException {
                          ErrorMessage = $"'bad offset for j command"
                };
            }

            var bits1to10  = number & Convert.ToInt32("1111111111", 2);
            var bits12to19 = (number & Convert.ToInt32("1111111100000000000", 2) >> 11);
            var bit11      = (number & Convert.ToInt32("10000000000", 2) >> 10);
            var bit20      = (number & Convert.ToInt32("00000000000000000001", 2) >> 18);

            instructionLayout |= (bits12to19 << 12) | (bit11 << 20) | (bits1to10 << 21) | (bit20 << 31);
            return(instructionLayout);
        }
Ejemplo n.º 2
0
        public static int UInstruction(string[][] programTextArray, ref int i, ref int j, out string label,
                                       Dictionary <uint, ExeCommand> commandsToExe)
        {
            var commandName       = programTextArray[i][j];
            int instructionLayout = UInstructionOpcode[commandName];
            var args = Healper.GetArgs(programTextArray, ref i, ref j, 2);

            commandsToExe[commandsToExe.Last().Key] = new ExeCommand {
                Args = args, Instraction = commandName, Line = i
            };

            label = null;
            if (!registersOpcode.ContainsKey(args[0]))
            {
                throw new SimulatorException {
                          ErrorMessage = $"'unknown '{args[0]}' register for U type command"
                }
            }
            ;
            if (int.TryParse(args[1], out int number))
            {
                instructionLayout |= number << 12 | registersOpcode[args[0]] << 7;
                commandsToExe[commandsToExe.Last().Key].Args[1] = number.ToString();
            }
            else
            {
                label = args[1];
            }

            return(instructionLayout);
        }
Ejemplo n.º 3
0
 public static void ExeLoadInstruction(RiscVProgramResult res, string exeCommand, List <string> args)
 {
     res.Register[registersOpcode[args[0]]].Value =
         Healper.CalculatLoadComand((uint)res.Register[registersOpcode[args[1]]].Value, uint.Parse(args[2]),
                                    exeCommand,
                                    res.Memory);
 }
Ejemplo n.º 4
0
        public static int StoreInstruction(string[][] programTextArray, ref int i, ref int j,
                                           Dictionary <uint, ExeCommand> commandsToExe)
        {
            var commandName       = programTextArray[i][j];
            int instructionLayout = SInstructionOpcode[commandName];
            var args = Healper.GetArgs(programTextArray, ref i, ref j, 2);

            if (!Healper.IsComandWithOffset(args[1], out string register, out int offset))
            {
                throw new SimulatorException {
                          ErrorMessage = $"'unknown store format command"
                }
            }
            ;
            if (!registersOpcode.ContainsKey(register))
            {
                throw new SimulatorException {
                          ErrorMessage = $"'unknown '{register}' register for load command"
                }
            }
            ;
            commandsToExe[commandsToExe.Last().Key] = new ExeCommand
            {
                Args = new List <string> {
                    args[0], register, offset.ToString()
                }, Instraction = commandName, Line = i
            };
            instructionLayout |= registersOpcode[args[0]] << 20 | registersOpcode[register] << 15;
            instructionLayout |= (offset & Convert.ToInt32("111111100000", 2) << 20) |
                                 (offset & Convert.ToInt32("11111", 2) << 7);


            return(instructionLayout);
        }
Ejemplo n.º 5
0
        public static int ShamtIInstruction(string[][] programTextArray, ref int i, ref int j,
                                            Dictionary <uint, ExeCommand> commandsToExe)
        {
            var commandName       = programTextArray[i][j];
            int instructionLayout = IInstructionOpcode[commandName];
            var args = Healper.GetArgs(programTextArray, ref i, ref j);

            if (!registersOpcode.ContainsKey(args[0]) || !registersOpcode.ContainsKey(args[1]))
            {
                throw new SimulatorException {
                          ErrorMessage = $"unknown register"
                }
            }
            ;
            instructionLayout |= registersOpcode[args[0]] << 7 | registersOpcode[args[1]] << 15;
            commandsToExe[commandsToExe.Last().Key] = new ExeCommand {
                Args = args, Instraction = commandName, Line = i
            };

            if (int.TryParse(args[2], out int number))
            {
                instructionLayout = instructionLayout | ((number << 25) >> 5);
            }
            else
            {
                throw new SimulatorException {
                          ErrorMessage = $"unknown shamit"
                }
            };


            return(instructionLayout);
        }
Ejemplo n.º 6
0
 public static void ExeRInstruction(RiscVProgramResult res, string exeCommand, List <string> args)
 {
     if (registersOpcode[args[0]] != 0)
     {
         res.Register[registersOpcode[args[0]]].Value = Healper.CalculatRComand(
             res.Register[registersOpcode[args[1]]].Value, res.Register[registersOpcode[args[2]]].Value,
             exeCommand);
     }
 }
Ejemplo n.º 7
0
 public static void ExeIInstruction(RiscVProgramResult res, string exeCommand, List <string> args)
 {
     if (exeCommand == "jalr")
     {
         res.Register[registersOpcode[args[0]]].Value = res.Register[registersOpcode["pc"]].Value;
         res.Register[registersOpcode["pc"]].Value    =
             res.Register[registersOpcode[args[1]]].Value + Convert.ToInt32(args[1]);
     }
     else
     {
         if (registersOpcode[args[0]] != 0)
         {
             res.Register[registersOpcode[args[0]]].Value =
                 Healper.CalculatIComand(res.Register[registersOpcode[args[1]]].Value, int.Parse(args[2]) & 4095,
                                         exeCommand);
         }
     }
 }
Ejemplo n.º 8
0
        public static int BInstruction(string[][] programTextArray, ref int i, ref int j,
                                       Dictionary <uint, ExeCommand> commandsToExe)
        {
            var commandName       = programTextArray[i][j];
            int instructionLayout = BInstructionOpcode[commandName];
            var args = Healper.GetArgs(programTextArray, ref i, ref j);

            commandsToExe[commandsToExe.Last().Key] = new ExeCommand {
                Args = args, Instraction = commandName, Line = i
            };

            if (!registersOpcode.ContainsKey(args[0]) || !registersOpcode.ContainsKey(args[1]))
            {
                throw new SimulatorException {
                          ErrorMessage = $"unknown register"
                }
            }
            ;
            instructionLayout |= registersOpcode[args[0]] << 15 | registersOpcode[args[1]] << 20;
            if (int.TryParse(args[2], out int number))
            {
                instructionLayout = instructionLayout | ((number << 25) >> 5);
            }
            else
            {
                throw new SimulatorException {
                          ErrorMessage = $"unknown shamit"
                }
            };

            var bits1to4  = number & Convert.ToInt32("1111", 2);
            var bits5to11 = (number & Convert.ToInt32("1111110000", 2) >> 4);
            var bit11     = (number & Convert.ToInt32("10000000000", 2) >> 10);
            var bit12     = (number & Convert.ToInt32("100000000000", 2) >> 11);

            instructionLayout |= (bits1to4 << 8) | (bit11 << 7) | (bits5to11 << 25) | (bit12 << 30);
            return(instructionLayout);
        }
Ejemplo n.º 9
0
        public static int RInstruction(string[][] programTextArray, ref int i, ref int j,
                                       Dictionary <uint, ExeCommand> commandsToExe)
        {
            var commandName       = programTextArray[i][j];
            int instructionLayout = RInstructionOpcode[commandName];
            var args = Healper.GetArgs(programTextArray, ref i, ref j);

            commandsToExe[commandsToExe.Last().Key] = new ExeCommand {
                Args = args, Instraction = commandName, Line = i
            };
            if (!registersOpcode.ContainsKey(args[0]) || !registersOpcode.ContainsKey(args[1]) ||
                !registersOpcode.ContainsKey(args[2]))
            {
                throw new SimulatorException {
                          ErrorMessage = $"unknown register"
                }
            }
            ;

            instructionLayout = instructionLayout | registersOpcode[args[0]] << 7 |
                                registersOpcode[args[1]] << 15 | registersOpcode[args[2]] << 20;

            return(instructionLayout);
        }
Ejemplo n.º 10
0
        public static int IInstruction(string[][] programTextArray, ref int i, ref int j, out string label,
                                       Dictionary <uint, ExeCommand> commandsToExe)
        {
            var commandName = programTextArray[i][j];

            int instructionLayout = IInstructionOpcode[commandName];

            if (commandName == "jalr")
            {
                label = null;
                var args = Healper.ParseJalr(programTextArray, ref i, ref j);
                commandsToExe[commandsToExe.Last().Key] = new ExeCommand
                {
                    Args = args, Instraction = commandName, Line = i
                };

                if (!Healper.IsComandWithOffset(args[1], out string register, out int offset))
                {
                    throw new SimulatorException {
                              ErrorMessage = $"unknown load format command"
                    }
                }
                ;
                if (!registersOpcode.ContainsKey(register))
                {
                    throw new SimulatorException {
                              ErrorMessage = $"unknown '{register}' register for load command"
                    }
                }
                ;
                instructionLayout |= registersOpcode[args[0]] << 7 | registersOpcode[register] << 15 | offset << 20;

                return(instructionLayout);
            }
            else
            {
                var args = Healper.GetArgs(programTextArray, ref i, ref j);
                if (!registersOpcode.ContainsKey(args[0]) || !registersOpcode.ContainsKey(args[1]))
                {
                    throw new SimulatorException {
                              ErrorMessage = $"unknown register"
                    }
                }
                ;
                instructionLayout |= registersOpcode[args[0]] << 7 | registersOpcode[args[1]] << 15;
                commandsToExe[commandsToExe.Last().Key] = new ExeCommand
                {
                    Args = args, Instraction = commandName, Line = i
                };

                if (int.TryParse(args[2], out int number))
                {
                    instructionLayout = instructionLayout | number << 20;
                    label             = null;
                }
                else
                {
                    label = args[2];
                }

                return(instructionLayout);
            }
        }
Ejemplo n.º 11
0
 public static void ExeShamtIInstruction(RiscVProgramResult res, string exeCommand, List <string> args)
 {
     res.Register[registersOpcode[args[0]]].Value =
         Healper.CalculatIComand(res.Register[registersOpcode[args[1]]].Value, int.Parse(args[2]) & 31,
                                 exeCommand);
 }