Пример #1
0
        public void UpdateStack(Stack <AsmLabel> stack)
        {
            this.Controls.Clear();

            //copy stack to an iterable array
            AsmLabel[] stackarray = new AsmLabel[stack.Count];
            stack.CopyTo(stackarray, 0);

            //print data to GUI
            foreach (AsmLabel lbl in stackarray)
            {
                StackControl ctrl = new StackControl();
                ctrl.Server   = this.Server;
                ctrl.Filepath = "C:\\Teensy\\Projects\\HelloWorld\\Main_Asm.txt";
                ctrl.Line     = lbl.PC_LineNumber;
                ctrl.Label    = lbl.Name;
                ctrl.Width    = this.ClientRectangle.Width / 2;
                ctrl.Margin   = new Padding(0, 0, 0, 0);
                this.Controls.Add(ctrl);
            }
        }
Пример #2
0
        void debug()
        {
            if (serialPort == null || !serialPort.IsOpen)
            {
                return;
            }

            int serialTimeout = 10;//ms


            inputTask.DisableMonitor();


            //flush pre-existing data
            serialPort.DiscardInBuffer();
            //trigger ISR through data transmission
            serialPort.Write(new char[] { '$' }, 0, 1);//0=start index, 1=length

            SpinWait.SpinUntil(() => serialPort.BytesToRead > 1, serialTimeout);
            if (serialPort.BytesToRead < 1)
            {
                return;
            }

            //max receivable bytes
            int memLimit = 4096;
            //current index in buffer array
            int memIndex = 0;
            //number of bytes to read in an iteration
            int memIncrement = 0;

            //storage for received bytes
            byte[] buffer = new byte[memLimit];



            //listen to serial port until 10ms have elapsed without any
            //data transmission or until 4KB has been received
            while (serialPort.BytesToRead > 0 && memIndex < memLimit)
            {
                //because BytesToRead can be increasing line-by-line, capture it once
                //and use as reference until next iteration
                memIncrement = serialPort.BytesToRead;
                memIndex    += serialPort.Read(buffer, memIndex, memIncrement);
                //wait until more data or timeout
                if (!SpinWait.SpinUntil(() => serialPort.BytesToRead > 0, serialTimeout))
                {
                    break;//timeout occured; jump out of loop
                }
            }
            inputTask.EnableMonitor();
            //explicitly stop serial connection
            //serialPort.Close();

            //convert ASCII encoded byte[] to a string
            string str = System.Text.Encoding.ASCII.GetString(buffer, 0, memIndex);

            //txtbxOutput.AppendText(str);
            //create a new stack dump object for converting the stack data to a data object
            StackDump d = new StackDump(str, manager);
            //the magic happens here
            Stack <AsmLabel> stack = d.Unwind();

            this.stackFlowLayout1.UpdateStack(stack);

            //copy stack to an iterable array
            AsmLabel[] stackarray = new AsmLabel[stack.Count];
            stack.CopyTo(stackarray, 0);

            //print data to GUI
            foreach (AsmLabel lbl in stackarray)
            {
                txtbxOutput.AppendText(lbl.ToString() + "\r\n");
            }

            txtbxOutput.AppendText("\r\n");
            this.txtbxOutput.Navigate(txtbxOutput.Lines.Count - 1);
        }
Пример #3
0
        public AsmLabel GetJumpLabel(UInt32 Addr)
        {
            UInt32 JAddr;

            if (Addr % 2 == 0)
            {
                JAddr = Addr;
            }
            else
            {
                JAddr = Addr - 5;//all branch commands are off by 5. Not sure why
            }
            AsmLabel containingCall = LookupLabel(Addr);

            FileReader.DiscardBufferedData();
            //long result = FileReader.BaseStream.Seek((long)((double)containingCall.LineNumber *.8) , SeekOrigin.Begin);
            long result = FileReader.BaseStream.Seek(containingCall.CharPosition, SeekOrigin.Begin);
            //FileReader.DiscardBufferedData();

            int    colonIndex     = 0;
            int    openBraceIndex = 0;
            string line;

            while (true)
            {
                line = FileReader.ReadLine();
                if (FileReader.EndOfStream)
                {
                    return(null);
                }
                if (line == null || line.Length < 1)
                {
                    continue;
                }
                colonIndex     = line.IndexOf(':');
                openBraceIndex = line.IndexOf('<');

                if (colonIndex < 0 || (openBraceIndex >= 0 && openBraceIndex < colonIndex))
                {
                    continue;
                }

                if (line.Contains("Disassembly of section .data"))
                {
                    return(null);
                }

                string addrStr = line.Substring(0, colonIndex).Trim();

                if (string.IsNullOrEmpty(addrStr))
                {
                    return(null);
                }

                UInt32 addr = UInt32.Parse(addrStr, System.Globalization.NumberStyles.HexNumber, null);

                if (addr == JAddr)
                {
                    int addrEnd = line.LastIndexOf('<');
                    if (addrEnd < 0)
                    {
                        return(null);
                    }
                    int addrStart = line.LastIndexOf('\t', addrEnd - 2);

                    if (addrStart > addrEnd)
                    {
                        return(null);
                    }

                    string jAddrStr = line.Substring(addrStart + 1, addrEnd - addrStart - 2);
                    UInt32 val      = 0;
                    if (!UInt32.TryParse(jAddrStr, System.Globalization.NumberStyles.HexNumber, null, out val))
                    {
                        return(null);
                    }


                    return(LookupLabel(val));
                }
                else if (addr > JAddr)
                {
                    return(null);
                }
            }
            return(null);
        }
Пример #4
0
        public bool ParseLabels()
        {
            Labels = new Dictionary <string, AsmLabel>();

            FileReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string line;
            long   lineNumber      = 0;
            long   labelLineNumber = 0;

            int labelStartIndex;
            int labelEndIndex;

            long currentPos = 0;


            while (!FileReader.EndOfStream)
            {
                currentPos      = GetActualPosition(FileReader);
                labelLineNumber = lineNumber;
                line            = FileReader.ReadLine();
                lineNumber++;


                //currentPos = FileReader.BaseStream.Position;


                labelEndIndex = line.IndexOf(">:");
                //found a label!
                if (labelEndIndex != -1)
                {
                    labelStartIndex = line.LastIndexOf('<', labelEndIndex);
                    if (labelStartIndex == -1)
                    {
                        //unmatched brace
                    }
                    else
                    {
                        string LabelName = line.Substring(labelStartIndex + 1, labelEndIndex - labelStartIndex - 1);

                        if (Labels.ContainsKey(LabelName))
                        {
                            //duplicate label. Yes, these do exist. and are not supported by me.
                            continue;
                        }

                        string LabelAddrStr = line.Substring(0, labelStartIndex - 1);
                        UInt32 addr;
                        if (!UInt32.TryParse(LabelAddrStr, System.Globalization.NumberStyles.HexNumber, null, out addr))
                        {
                            continue;
                        }

                        string tempLine;

                        string[] regs = new string[] {};

                        for (int i = 0; i < 5; i++)
                        {
                            //char[] tempLineCharArray = new char[100];
                            //FileReader.BaseStream.Seek(currentPos, SeekOrigin.Begin);
                            //FileReader.Read(tempLineCharArray, 0, tempLineCharArray.Length);
                            //tempLine = new string(tempLineCharArray);

                            tempLine = FileReader.ReadLine();
                            lineNumber++;
                            if (tempLine == null || tempLine.Contains(">:"))
                            {
                                break;
                            }
                            int startIndex = tempLine.IndexOf('{');
                            if (startIndex < 0)
                            {
                                continue;
                            }

                            int endIndex = tempLine.IndexOf('}', startIndex);
                            if (endIndex == -1)
                            {
                                string nextLine2 = FileReader.ReadLine();
                                lineNumber++;
                            }
                            else
                            {
                                string regsStr = tempLine.Substring(startIndex + 1, endIndex - startIndex - 1);
                                regs = regsStr.Split(new string[] { " ", ",", "\t" }, StringSplitOptions.RemoveEmptyEntries);
                            }
                            break;
                        }
                        //FileReader.BaseStream.Seek(currentPos, SeekOrigin.Begin);


                        if (LabelName == "main")
                        {
                            regs = new string[] { "", "", "" }
                        }
                        ;
                        AsmLabel lbl = new AsmLabel(LabelName, addr, regs, currentPos, labelLineNumber);

                        Labels.Add(LabelName, lbl);
                    }
                }
            }

            StringBuilder lineBuilder = new StringBuilder();

            return(true);
        }
Пример #5
0
        public Stack <AsmLabel> Unwind()
        {
            int index = Data.Length - 1;
            Stack <AsmLabel> result      = new Stack <AsmLabel>();
            bool             frontMatter = true;
            bool             stack       = false;
            bool             ISRStack    = false;

            UInt32 cWord;

            while (index >= 0)
            {
                cWord = Data[index];
                if (frontMatter)
                {
                    if (cWord != 0xffffffff && cWord != 0x00000000)
                    {
                        frontMatter = false;
                        stack       = true;
                        continue;
                    }
                    else
                    {
                        index--;
                    }
                }
                else if (stack)
                {
                    if (cWord > 0x10000000)
                    {
                        index--;
                        continue;
                    }
                    if (cWord == 0xffffffff && cWord == 0x00000000)
                    {
                        stack    = false;
                        ISRStack = true;
                        index--;
                        continue;
                    }
                    AsmLabel lbl     = Manager.LookupLabel(cWord);
                    AsmLabel jumpLbl = Manager.GetJumpLabel(cWord);
                    Manager.GetPC_LineNumber(lbl, cWord);

                    if (jumpLbl == null)
                    {
                        stack    = false;
                        ISRStack = true;
                        index--;
                        continue;
                    }

                    if (lbl != null)
                    {
                        if (jumpLbl != null)
                        {
                            index -= (jumpLbl.StackWords.Length > 0 ? jumpLbl.StackWords.Length :1);
                        }
                        else
                        {
                            index--;
                        }
                        result.Push(lbl);
                    }
                    else
                    {
                        stack    = false;
                        ISRStack = true;
                        index--;
                    }
                }
                else if (ISRStack)
                {
                    index--;
                }
                if (index < 0)
                {
                    break;
                }
            }
            return(result);
        }