private static List<FunctionBlockRow> GetMC5Rows(byte[] code, int codestart, List<S5Parameter> parameters, Step5BlocksFolder blkFld, S5FunctionBlock block)
        {
            List<FunctionBlockRow> retVal=new List<FunctionBlockRow>();
            int codepos = codestart;

            retVal.Add(new S5FunctionBlockRow() {Command = "BLD", Parameter = "255", Parent = block}); //Command for first Network does not exist!
                                                                                        //This needs to be removen when written back to S5D!

            while (codepos < code.Length)
            {

                byte mc = code[codepos];

                // finde Befehle in der Symbol Tabelle 
                int index = find_mc5_code(code, codepos);

                

                S5FunctionBlockRow newRow = new S5FunctionBlockRow(){Parent = block};
                                
                if (index >= 0)
                {
                    newRow.MC5LIB_SYMTAB_Row = (object[])sym[index];

                    bool fb = false;
                   newRow.Command = (string) ((object[]) sym[index])[operation];

                    string oper = (string) ((object[]) sym[index])[operand];
                    
                    if (oper == "PAR")
                    {
                    	int paridx = find_mc5_param(code, codepos, index);

	                    if (parameters != null && parameters.Count > paridx - 1)
	                    {
		                    newRow.Parameter = "=" + parameters[paridx - 1].Name;
	                    }
	                    else
	                    {
		                    newRow.Parameter = "$$error$$ -> invalid Parameter, idx:"+ (paridx - 1);
	                    }
                    }
                    else
                    	
                    {
                    
                    if (oper != null)
                        newRow.Parameter = oper;

                    fb = IsCall(newRow);    //false;
                    /*if ((newRow.Command=="SPA" && newRow.Parameter=="FB") ||
                        (newRow.Command=="SPB" && newRow.Parameter=="FB") ||
                        (newRow.Command=="BA" && newRow.Parameter=="FX") ||
                        (newRow.Command=="BAB" && newRow.Parameter=="FX"))
                    {
                        fb = true;
                    }*/

                    int par = find_mc5_param(code, codepos, index);

                    switch ((string)((object[])sym[index])[printpar])
                    {
                        case null:
                            break;
                        case "PrintNummer":
                            if (!string.IsNullOrEmpty(newRow.Parameter))
                                newRow.Parameter += " ";
                            Int16 wrt =(Int16)par;
                            if (wrt >= 0 && newRow.Parameter == "KF ")
                                newRow.Parameter += " +";
                            newRow.Parameter += wrt.ToString();
                            break;
                        case "PrintKY":
                            newRow.Parameter += BitConverter.GetBytes(par)[1].ToString() + "," +
                                                BitConverter.GetBytes(par)[0].ToString();
                            break;
                        case "PrintChar":
                            if (!string.IsNullOrEmpty(newRow.Parameter))
                                newRow.Parameter += " ";                                                                       
                            newRow.Parameter += ((char)(BitConverter.GetBytes((Int16)par)[0])).ToString() + ((char)(BitConverter.GetBytes((Int16)par)[1]));
                            break;
                        case "PrintHEX4":
                            if (!string.IsNullOrEmpty(newRow.Parameter))
                                newRow.Parameter += " ";
                            newRow.Parameter += par.ToString("X").PadLeft(4, '0');
                            break;
                        case "PrintGleitpunkt":
                            if (!string.IsNullOrEmpty(newRow.Parameter))
                                newRow.Parameter += " ";
                            newRow.Parameter += "gleit";
                            break;
                        case "PrintHEX8":
                            if (!string.IsNullOrEmpty(newRow.Parameter))
                                newRow.Parameter += " ";
                            newRow.Parameter += par.ToString("X").PadLeft(4, '0');
                            break;
                        case "PrintMerkerBit":
                            if (!string.IsNullOrEmpty(newRow.Parameter))
                                newRow.Parameter += " ";
                            newRow.Parameter += (par & 0x00FF).ToString() + "." + (par >> 8).ToString();
                            break;
                        case "PrintSMerkerBit":
                            if (!string.IsNullOrEmpty(newRow.Parameter))
                                newRow.Parameter += " ";
                            newRow.Parameter += (par & 0x0FFF).ToString() + "." + (par >> 12).ToString();
                            break;
                    }
                    }

                    int _parlen = (int)((object[])sym[index])[parlen];
                    int _codelen = (int)((object[])sym[index])[codelen];

                    
                    int btSize = (_codelen + _parlen) / 8;
                                           
                    codepos += btSize; // Schleifenzähler     

                    if (fb)
                    {
                        //FB-Call
                        index = find_mc5_code(code, codepos);
                        if (index < 0 || (string)((object[])sym[index])[operation] != "SPA=")
                            newRow.Command = "Error reading Jump after FB/FX Call!";
                        int spa = find_mc5_param(code, codepos, index);

                        newRow.ExtParameter=new List<S5Parameter>();
                        
                        //newRow.ExtParameter = new List<string>();



                        byte[] calledfb = blkFld.GetBlockInByte("S5_" + newRow.Parameter.Replace(" ", ""));
                        if (calledfb!=null)
                        {
                            S5Parameter newPar = new S5Parameter();
                            newRow.ExtParameter.Add(newPar);

                            string fbNM = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(calledfb, 12, 8);
                            //newRow.ExtParameter.Add("Name :" + fbNM);

                            newPar.Name = "Name :";
                            newPar.Value = fbNM;

                            List<S5Parameter> par = GetParameters(calledfb);
                            int j = 0;

                      
                            foreach (var s5Parameter in par)
                            {
                                newPar = new S5Parameter();
                                newRow.ExtParameter.Add(newPar);

                                j++;
                                string akOper = "";

                                int pos = codepos + j*2;                            

                                switch (s5Parameter.S5ParameterType)
                                {
                                    case S5ParameterType.A:
                                    case S5ParameterType.E:
                                        int ber = code[pos];
                                        switch (s5Parameter.S5ParameterFormat)
                                        {
                                                
                                            case S5ParameterFormat.BI:
                                                switch (ber & 0xf0)
                                                {
                                                    case 0x80:
                                                        akOper += "M";
                                                        break;
                                                }
                                                akOper += code[pos + 1].ToString();
                                                akOper += "." + (code[pos] & 0x0f).ToString();
                                                break;
                                            case S5ParameterFormat.BY:
                                                switch (ber)
                                                {
                                                    case 0x0a:
                                                        akOper += "MB";
                                                        break;
                                                    case 0x2a:
                                                        akOper += "DR";
                                                        break;
                                                    case 0x22:
                                                        akOper += "DL";
                                                        break;
                                                    default:
                                                        akOper += "";
                                                        break;                                                
                                                }
                                                akOper += code[pos + 1].ToString();
                                                break;
                                            case S5ParameterFormat.W:                                                                                              
                                                switch (ber)
                                                { 
                                                    case 0x12:
                                                        akOper += "MW";
                                                        break;
                                                    case 0x32:
                                                        akOper += "DW";
                                                        break;
                                                }

                                                akOper += code[pos + 1].ToString();
                                                break;
                                            case S5ParameterFormat.D:
                                                break;
                                        }
                                        break;
                                    case S5ParameterType.D:
                                        int wrt = code[pos]*0x100 + code[pos + 1];
                                        akOper += s5Parameter.S5ParameterFormat.ToString() + " ";
                                        switch (s5Parameter.S5ParameterFormat)
                                        { 
                                            case S5ParameterFormat.KF:
                                                if (((Int16)wrt)>0)
                                                    akOper += "+";
                                                akOper += ((Int16)wrt).ToString();                                                
                                                break;
                                            case S5ParameterFormat.KT:
                                                //code[pos + 1]
                                                // Bit      4-7       0-3    4-7   0-3         
                                                //Format: Zeitbasis, 100er, 10er, 1er
                                                akOper += wrt.ToString().PadLeft(3, '0') + ".0";
                                                break;
                                            case S5ParameterFormat.KC:
                                                akOper += (char)code[pos] + (char)code[pos + 1];
                                                break;
                                            case S5ParameterFormat.KM:
                                                akOper += libnodave.dec2bin(code[pos]) + " ";
                                                akOper += libnodave.dec2bin(code[pos + 1]);                                               
                                                break;
                                            case S5ParameterFormat.KY:
                                                akOper += code[pos].ToString() + "," + code[pos + 1].ToString();
                                                break;
                                            default:
                                                akOper += ((Int16)wrt).ToString("X").PadLeft(4, '0');
                                                break;                                            
                                        }
                                        break;
                                    case S5ParameterType.B:
                                        int bst = code[pos];                                              
                                                switch (bst)
                                                { 
                                                    case 0x20:
                                                        akOper += "DB";
                                                        break;
                                                }
                                        akOper += code[pos + 1].ToString();

                                        break;
                                    case S5ParameterType.T:
                                         //int abst = code[pos];                                                                                              
                                        akOper += "T " + code[pos + 1].ToString();
                                        break;
                                    case S5ParameterType.Z:
                                        akOper += "Z " + code[pos + 1].ToString();
                                        break;
                                }
                                newPar.Name = s5Parameter.Name;
                                newPar.Value = akOper;
                                //newRow.ExtParameter.Add(s5Parameter.Name.PadRight(5, ' ') + ":  " + akOper);

                            }
                        }
                        else
                            for (int j = 1; j < spa; j++)
                            {
                                int myPar = (code[codepos + j*2] << 8) | code[codepos + j*2 + 1];
                                //newRow.ExtParameter.Add(myPar.ToString());
                                S5Parameter newPar = new S5Parameter();
                                newRow.ExtParameter.Add(newPar);
                                newPar.Name = myPar.ToString();
                            }

                        btSize += spa * 2;
                        	
                        codepos += spa * 2;
                        fb = false;
                    }
                   
                    byte[] MC5 = new byte[btSize];
                    
                    Array.Copy(code,codepos - btSize, MC5, 0, btSize);
                    
                    newRow.MC5 = MC5;
                        
                }
                else
                {
                    newRow.Command = "Error converting command!";
                    codepos += 2;
                    //throw new Exception("Code nicht in Symtab!");
                }
                retVal.Add(newRow);

            }

            #region Build the Jumpsmarks....
            int JumpCount = 1;
            int akBytePos = 0;
            Dictionary<int, S5FunctionBlockRow> ByteAdressNumerPLCFunctionBlocks = new Dictionary<int, S5FunctionBlockRow>();
            foreach (S5FunctionBlockRow tmp in retVal)
            {
                if (tmp.ByteSize > 0)
                {
                    ByteAdressNumerPLCFunctionBlocks.Add(akBytePos, tmp);
                    akBytePos += tmp.ByteSize;
                }
            }

            akBytePos = 0;
            foreach (S5FunctionBlockRow tmp in retVal)
            {
                if (tmp.Command == "SPA=" || tmp.Command == "SPB=" || tmp.Command == "SPZ=" || tmp.Command == "SPN=" || tmp.Command == "SPP=" || tmp.Command == "SPM=" || tmp.Command == "SPO=")
                {
                    int jmpBytePos = 0;

                    byte[] backup = tmp.MC5;

                    tmp.Command = tmp.Command.Substring(0, tmp.Command.Length - 1);

                    tmp.JumpWidth = (SByte)Convert.ToInt32(tmp.Parameter);
                    
                    jmpBytePos = akBytePos + (tmp.JumpWidth * 2);

                    if (ByteAdressNumerPLCFunctionBlocks.ContainsKey(jmpBytePos))
                    {
                        var target = ByteAdressNumerPLCFunctionBlocks[jmpBytePos];
                        if (target.Label.Trim() == "")
                        {
                            target.Label = "M" + JumpCount.ToString().PadLeft(3, '0');
                            JumpCount++;
                        }

                        //Backup the MC7 Code, because the MC7 Code is always deleted when the command or parameter changes!

                        tmp.Parameter = "=" + target.Label;
                        tmp.MC5 = backup;
                    }
                    else
                    {                        
                        tmp.Parameter = "Error! JumpWidth :" + tmp.JumpWidth;
                        tmp.MC5 = backup;
                    }


                }
                akBytePos += tmp.ByteSize;
            }
            #endregion            

            return retVal;
        }
        private static List<S5Parameter> GetParameters(byte[] code)
        {
        	List<S5Parameter> retVal = new List<S5Parameter>();
        	int index = find_mc5_code(code, 10);
            if (index < 0 || (string)((object[])sym[index])[operation] != "SPA=")
                throw new Exception("Error converting FB!");
            int spa = find_mc5_param(code, 10, index);

            int paranz = (spa - 5) / 3;
                        
            for (int n = 0; n<paranz; n++)
            {
            	S5Parameter tmp = new S5Parameter();
            	int t1 = code[n * 6 + 20];
            	switch (t1)
            	{
            		case 2:
            			tmp.S5ParameterType = S5ParameterType.E;
            			break;
            		case 4:
            			tmp.S5ParameterType = S5ParameterType.D;
            			break;
            		case 5:
            			tmp.S5ParameterType = S5ParameterType.T;
            			break;
            		case 7:
            			tmp.S5ParameterType = S5ParameterType.B;
            			break;
            		case 8:
            			tmp.S5ParameterType = S5ParameterType.A;
            			break;
            	}
            	
            	int t2 = code[n * 6 + 21];
            	if (tmp.S5ParameterType == S5ParameterType.D)
            	{
            		switch (t2)
            		{
            			case 1:
            				tmp.S5ParameterFormat = S5ParameterFormat.KZ;
           					break; 
           				case 2:
            				tmp.S5ParameterFormat = S5ParameterFormat.KT;
           					break; 
            			case 4:
            				tmp.S5ParameterFormat = S5ParameterFormat.KF;
           					break;
						case 8:
            				tmp.S5ParameterFormat = S5ParameterFormat.KG;
           					break;            					
						case 16:
            				tmp.S5ParameterFormat = S5ParameterFormat.KC;
           					break;
						case 32:
            				tmp.S5ParameterFormat = S5ParameterFormat.KY;
           					break;           					
           				case 64:
            				tmp.S5ParameterFormat = S5ParameterFormat.KH;
           					break; 
           				case 128:
            				tmp.S5ParameterFormat = S5ParameterFormat.KM;
           					break; 
            		}
            	}
            	else
					switch (t2)
            			{
            				case 16:
            					tmp.S5ParameterFormat = S5ParameterFormat.D;
           						break; 
            				case 32:
            					tmp.S5ParameterFormat = S5ParameterFormat.W;
           						break; 
           					case 64:
            					tmp.S5ParameterFormat = S5ParameterFormat.BY;
           						break; 
           					case 128:
            					tmp.S5ParameterFormat = S5ParameterFormat.BI;
           						break;        				
            			}            			
            	tmp.Name = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(code, n * 6 + 22, 4);
            	retVal.Add(tmp);
            }
            
			return retVal;            
        }