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); }
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); }
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); }
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); }
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); }
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); }
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); } }