/// <summary>
        /// Parses an instruction and generates the binary code for it.
        /// </summary>
        /// <param name="address">The address of the instruction being parsed in the .text segment.</param>
        /// <param name="args">An array containing the arguments of the instruction.</param>
        /// <returns>One or more 32-bit integers representing this instruction. If this interface is implemented
        /// for a pseudo-instruction, this may return more than one instruction value.</returns>
        public override IEnumerable <int> GenerateCodeForInstruction(int nextTextAddress, string[] args)
        {
            if (args.Length != 2)
            {
                throw new ArgumentException("Invalid number of arguments provided. Expected 2, received " + args.Length + '.');
            }

            int rs2 = RegisterMap.GetNumericFloatingPointRegisterValue(args[0]);

            var retList = new List <int>();
            ParameterizedInstructionArg arg = ParameterizedInstructionArg.ParameterizeArgument(args[1]);
            int instruction = 0;
            int upperOffset = (arg.Offset & 0xFE0);
            int lowerOffset = (arg.Offset & 0x1F);

            instruction |= (upperOffset << 25);
            instruction |= (rs2 << 20);
            instruction |= (arg.Register << 15);

            const int FUNC_CODE = 2;

            instruction |= (FUNC_CODE << 12);

            instruction |= (lowerOffset << 7);
            instruction |= 0x27;
            retList.Add(instruction);

            return(retList);
        }
Ejemplo n.º 2
0
            /// <summary>
            /// Takes an argument (e.g. 4(f4)) and parameterizes it into the offset component
            /// and its numeric register ID.
            /// </summary>
            /// <param name="trimmedArgToken">The token to parameterize, with whitespace trimmed on both left/right sides.</param>
            /// <returns>A parameterized register/offset structure.</returns>
            public static ParameterizedInstructionArg ParameterizeFloatingPtArgument(string trimmedArgToken)
            {
                string[] parameterizedArgs = trimmedArgToken.Split(new[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries).Apply((str) => str.Trim()).ToArray();

                // we should expect one or two arguments.
                if (parameterizedArgs.Length != 1 && parameterizedArgs.Length != 2)
                {
                    throw new ArgumentException(trimmedArgToken + " was not in a valid format.");
                }

                ParameterizedInstructionArg retVal = default(ParameterizedInstructionArg);

                // if we have one argument, assume its the register name, and that the offset is 0.
                if (parameterizedArgs.Length == 1)
                {
                    int registerId = RegisterMap.GetNumericFloatingPointRegisterValue(parameterizedArgs[0]);
                    retVal = new ParameterizedInstructionArg(0, registerId);
                }
                else
                {
                    bool isValidOffset = IntExtensions.TryParseEx(parameterizedArgs[0], out short offsetVal) && ((offsetVal & 0xF000) == 0);
                    if (!isValidOffset)
                    {
                        throw new ArgumentException(parameterizedArgs[0] + " is not a valid 12-bit offset.");
                    }

                    int registerId = RegisterMap.GetNumericFloatingPointRegisterValue(parameterizedArgs[1]);
                    retVal = new ParameterizedInstructionArg(offsetVal, registerId);
                }

                return(retVal);
            }
Ejemplo n.º 3
0
 public void Execute(string[] args)
 {
     try
     {
         int    regIdx  = -1;
         string regName = args[0];
         if (RegisterMap.IsNamedIntegerRegister(regName))
         {
             regIdx = RegisterMap.GetNumericRegisterValue(regName);
             m_Terminal.PrintString("\t" + regName + " = " + m_Registers.UserIntRegisters[regIdx].Value + '\n');
         }
         else if (RegisterMap.IsNamedFloatingPointRegister(regName))
         {
             regIdx = RegisterMap.GetNumericFloatingPointRegisterValue(regName);
             m_Terminal.PrintString("\t" + regName + " = " + m_Registers.UserFloatingPointRegisters[regIdx].Value + '\n');
         }
         else
         {
             throw new ParseException(regName + " is not a valid integer register name.");
         }
     }
     catch (Exception ex)
     {
         m_Terminal.PrintString(ex.Message + '\n');
     }
 }
        /// <summary>
        /// Parses an instruction and generates the binary code for it.
        /// </summary>
        /// <param name="address">The address of the instruction being parsed in the .text segment.</param>
        /// <param name="args">An array containing the arguments of the instruction.</param>
        /// <returns>One or more 32-bit integers representing this instruction. If this interface is implemented
        /// for a pseudo-instruction, this may return more than one instruction value.</returns>
        public override IEnumerable <int> GenerateCodeForInstruction(int address, string[] args)
        {
            if (args.Length != 2)
            {
                throw new ArgumentException("Invalid number of arguments provided. Expected 2, received " + args.Length + '.');
            }

            int rdReg = RegisterMap.GetNumericFloatingPointRegisterValue(args[0]);

            var retList = new List <int>();

            const int FUNC_CODE = 0x2;

            if (IsParameterizedToken(args[1]))
            {
                ParameterizedInstructionArg arg = ParameterizedInstructionArg.ParameterizeArgument(args[1]);
                int instruction = 0;
                instruction |= ((arg.Offset & 0xFFF) << 20);
                instruction |= (arg.Register << 15);
                instruction |= (FUNC_CODE << 12);
                instruction |= (rdReg << 7);
                instruction |= 0x7;
                retList.Add(instruction);
            }
            else
            {
                Symbol sym            = SymbolTable.GetSymbol(args[1]);
                int    shiftedAddress = sym.Address >> 12;
                retList.AddRange(new LuiProcessor().GenerateCodeForInstruction(address, new[] { args[0], shiftedAddress.ToString() }));
                int numericOffset = sym.Address & 0xFFF;
                int instruction   = 0;
                instruction |= (numericOffset << 20);
                instruction |= (rdReg << 15);
                instruction |= (FUNC_CODE << 12);
                instruction |= (rdReg << 7);
                instruction |= 0x7;
                retList.Add(instruction);
            }

            return(retList);
        }
Ejemplo n.º 5
0
 public void Execute(string[] args)
 {
     try
     {
         string regName = args[0];
         if (IntExtensions.TryParseEx(args[1], out int iValue))
         {
             if (RegisterMap.IsNamedIntegerRegister(regName))
             {
                 int regIdx = RegisterMap.GetNumericRegisterValue(regName);
                 m_Registers.UserIntRegisters[regIdx].Value = iValue;
                 m_Terminal.PrintString("\t" + regName + " = " + iValue + '\n');
             }
             else
             {
                 throw new ParseException(regName + " was not a valid register name.");
             }
         }
         else if (FloatExtensions.TryParseEx(args[1], out float fValue))
         {
             if (RegisterMap.IsNamedFloatingPointRegister(regName))
             {
                 int regIdx = RegisterMap.GetNumericFloatingPointRegisterValue(regName);
                 m_Registers.UserFloatingPointRegisters[regIdx].Value = fValue;
                 m_Terminal.PrintString("\t" + regName + " = " + fValue + '\n');
             }
             else
             {
                 throw new ParseException(regName + " was not a valid register name.");
             }
         }
         else
         {
             throw new ParseException(args[1] + " was not a valid 32-bit value");
         }
     }
     catch (Exception ex)
     {
         m_Terminal.PrintString(ex.Message + '\n');
     }
 }
        public override IEnumerable <int> GenerateCodeForInstruction(int address, string[] instructionArgs)
        {
            // we expect three arguments. if not, throw an ArgumentException
            if (instructionArgs.Length != 2)
            {
                throw new ArgumentException("Invalid number of arguments provided. Expected 2, received " + instructionArgs.Length + '.');
            }

            const int BASE_OPCODE = 0x53;

            int rdReg  = RegisterMap.GetNumericFloatingPointRegisterValue(instructionArgs[0]);
            int rs1Reg = RegisterMap.GetNumericRegisterValue(instructionArgs[1]);

            int outputInst = (0x1A << 27) | (rs1Reg << 15) | (rdReg << 7) | BASE_OPCODE;

            var instList = new List <int> {
                outputInst
            };

            return(instList);
        }
        public void Execute(string[] args)
        {
            try
            {
                string fmtString = args[1];

                if (fmtString.Contains("\""))
                {
                    fmtString = fmtString.Replace("\"", string.Empty);
                }

                fmtString = fmtString.Trim();

                int    regIdx  = -1;
                string regName = string.Empty;
                if (RegisterMap.IsNamedIntegerRegister(args[0]))
                {
                    regIdx  = RegisterMap.GetNumericRegisterValue(args[0]);
                    regName = args[0];
                    m_Terminal.PrintString("\t" + regName + " = " +
                                           m_Registers.UserIntRegisters[regIdx].Value.ToString(fmtString) + '\n');
                }
                else if (RegisterMap.IsNamedFloatingPointRegister(args[0]))
                {
                    regIdx  = RegisterMap.GetNumericFloatingPointRegisterValue(args[0]);
                    regName = args[0];
                    m_Terminal.PrintString("\t" + regName + " = " +
                                           m_Registers.UserFloatingPointRegisters[regIdx].Value.ToString(fmtString) + '\n');
                }
                else
                {
                    throw new ParseException(regIdx + " was not a valid register name.");
                }
            }
            catch (Exception ex)
            {
                m_Terminal.PrintString(ex.Message + '\n');
            }
        }