Beispiel #1
0
        public void Update(ASMEncoding.ASMEncodingUtility asmUtility)
        {
            UpdateReferenceVariableValues();

            List <PatchedByteArray> allPatches = GetAllPatches();

            foreach (PatchedByteArray patchedByteArray in allPatches)
            {
                if (patchedByteArray.IsAsm)
                {
                    string encodeContent = patchedByteArray.Text;
                    //string strPrefix = "";
                    //IList<VariableType> variables = Variables;

                    System.Text.StringBuilder sbPrefix = new System.Text.StringBuilder();
                    foreach (PatchedByteArray currentPatchedByteArray in allPatches)
                    {
                        if (!string.IsNullOrEmpty(currentPatchedByteArray.Label))
                        {
                            sbPrefix.AppendFormat(".label @{0}, {1}{2}", currentPatchedByteArray.Label, currentPatchedByteArray.RamOffset, Environment.NewLine);
                        }
                    }
                    foreach (VariableType variable in Variables)
                    {
                        sbPrefix.AppendFormat(".eqv %{0}, {1}{2}", ASMEncoding.Helpers.ASMStringHelper.RemoveSpaces(variable.Name).Replace(",", ""),
                                              Utilities.GetUnsignedByteArrayValue_LittleEndian(variable.ByteArray), Environment.NewLine);
                    }

                    encodeContent = sbPrefix.ToString() + patchedByteArray.Text;
                    //patchedByteArray.SetBytes(asmUtility.EncodeASM(encodeContent, (uint)patchedByteArray.RamOffset).EncodedBytes);

                    byte[] bytes = asmUtility.EncodeASM(encodeContent, (uint)patchedByteArray.RamOffset).EncodedBytes;

                    if ((!patchedByteArray.IsMoveSimple) && (blockMoveList.Count > 0))
                    {
                        bytes = asmUtility.UpdateBlockReferences(bytes, (uint)patchedByteArray.RamOffset, true, blockMoveList);
                    }

                    patchedByteArray.SetBytes(bytes);
                }
                else if (!string.IsNullOrEmpty(patchedByteArray.Text))
                {
                    GetBytesResult result = GetBytes(patchedByteArray.Text, (uint)patchedByteArray.RamOffset);
                    patchedByteArray.SetBytes(result.Bytes);
                }
            }
        }
Beispiel #2
0
        private static GetBytesResult TranslateBytes(string byteText, uint pc, Dictionary <string, VariableType> variableMap, bool reportErrors = true)
        {
            StringBuilder resultTextBuilder = new StringBuilder();
            StringBuilder errorTextBuilder  = new StringBuilder();

            GetBytesResult result = new GetBytesResult();

            List <bool> isSkippingLine = new List <bool>()
            {
                false
            };
            int ifNestLevel = 0;

            string[] lines = byteText.Split('\n');
            lines = Utilities.RemoveFromLines(lines, "\r");
            foreach (string line in lines)
            {
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                string   processLine = Utilities.RemoveLeadingSpaces(line);
                string[] parts       = Utilities.SplitLine(processLine);

                string firstPart = parts[0].ToLower().Trim();

                if (firstPart == ".endif")
                {
                    if (ifNestLevel == 0)
                    {
                        if (reportErrors)
                        {
                            errorTextBuilder.AppendLine("WARNING: No matching .if statement for .endif statement at address 0x" + Utilities.UnsignedToHex_WithLength(pc, 8));
                        }
                    }
                    else
                    {
                        isSkippingLine.RemoveAt(isSkippingLine.Count - 1);
                        ifNestLevel--;
                    }
                }
                else if (firstPart == ".else")
                {
                    if (ifNestLevel == 0)
                    {
                        if (reportErrors)
                        {
                            errorTextBuilder.AppendLine("WARNING: No matching .if statement for .else statement at address 0x" + Utilities.UnsignedToHex_WithLength(pc, 8));
                        }
                    }
                    else if (!isSkippingLine[ifNestLevel - 1])
                    {
                        isSkippingLine[ifNestLevel] = !isSkippingLine[ifNestLevel];
                    }
                }
                else if (firstPart == ".if")
                {
                    try
                    {
                        string[] innerParts = parts[1].Split(',');

                        if (!parts[1].Contains(","))
                        {
                            if (reportErrors)
                            {
                                errorTextBuilder.AppendLine("WARNING: Unreachable code at address 0x" + Utilities.UnsignedToHex_WithLength(pc, 8)
                                                            + " inside .if statement with bad argument list (no commas): \"" + parts[1] + "\"");
                            }

                            isSkippingLine.Add(true);
                            ifNestLevel++;
                        }
                        else if (innerParts.Length < 2)
                        {
                            if (reportErrors)
                            {
                                errorTextBuilder.AppendLine("WARNING: Unreachable code at address 0x" + Utilities.UnsignedToHex_WithLength(pc, 8) +
                                                            " inside .if statement with bad argument list (less than 2 arguments): \"" + parts[1] + "\"");
                            }

                            isSkippingLine.Add(true);
                            ifNestLevel++;
                        }
                        else if (isSkippingLine[ifNestLevel])
                        {
                            isSkippingLine.Add(true);
                            ifNestLevel++;
                        }
                        else
                        {
                            string operation = string.Empty;
                            string eqvKey, eqvValue;

                            if (innerParts.Length >= 3)
                            {
                                operation = Utilities.RemoveSpaces(innerParts[0]);
                                eqvKey    = Utilities.RemoveSpaces(innerParts[1]);
                                eqvValue  = Utilities.RemoveSpaces(innerParts[2]);
                            }
                            else
                            {
                                operation = "=";
                                eqvKey    = Utilities.RemoveSpaces(innerParts[0]);
                                eqvValue  = Utilities.RemoveSpaces(innerParts[1]);
                            }

                            int  intKey       = 0;
                            int  intValue     = 0;
                            bool isKeyInt     = int.TryParse(eqvKey, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out intKey);
                            bool isValueInt   = int.TryParse(eqvValue, out intValue);
                            bool isIntCompare = isKeyInt && isValueInt;

                            bool isPass = false;
                            switch (operation)
                            {
                            case "=":
                            case "==":
                                isPass = eqvKey.Equals(eqvValue);
                                break;

                            case "!=":
                            case "<>":
                                isPass = !eqvKey.Equals(eqvValue);
                                break;

                            case "<":
                                isPass = isIntCompare && (intKey < intValue);
                                break;

                            case ">":
                                isPass = isIntCompare && (intKey > intValue);
                                break;

                            case "<=":
                                isPass = isIntCompare && (intKey <= intValue);
                                break;

                            case ">=":
                                isPass = isIntCompare && (intKey >= intValue);
                                break;

                            default:
                                break;
                            }

                            isSkippingLine.Add(!isPass);
                            ifNestLevel++;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (reportErrors)
                        {
                            errorTextBuilder.AppendLine("Error on .if statement: " + ex.Message + "\r\n");
                        }
                    }
                }
                else if (!isSkippingLine[ifNestLevel])
                {
                    resultTextBuilder.AppendLine(line);
                    pc += (uint)(line.Length / 2);
                }
            }

            result.Bytes        = Utilities.GetBytesFromHexString(resultTextBuilder.ToString());
            result.ErrorMessage = errorTextBuilder.ToString();
            return(result);
        }