Esempio n. 1
0
        public string Assemble(string[] lines, TheCpu CPU, string outputFilename)
        {
            this.address = 0;
            string error;

            // Prepare line mappings
            for (int lineNumber = 0; lineNumber < lines.Length; lineNumber++)
            {
                string lineText = lines[lineNumber];

                if (lineText.Trim() == string.Empty)
                {
                    continue;
                }

                string[] lineParsed;
                CommandBase.Parse(lineText, lineNumber, CPU, out lineParsed);
                CommandBase command = CommandManager.RecognizeCommand(lineText, lineParsed, CPU, out error);

                if (command == null || error != null)
                {
                    return(error);
                }
                else
                {
                    LineMappings.Add(lineNumber, address);

                    if (command.GetType() == typeof(dirADDRESS) ||
                        command.GetType() == typeof(dirCONSTANT) ||
                        command.GetType() == typeof(dirNAMEREG) ||
                        command.GetType() == typeof(EmptyLine))
                    {
                        command.Assemble(lineParsed, CPU, this);
                    }
                    else
                    {
                        address++;
                    }
                }
            }

            this.address = 0;

            // Assemble
            for (int lineNumber = 0; lineNumber < lines.Length; lineNumber++)
            {
                string lineText = lines[lineNumber];

                if (lineText.Trim() == string.Empty)
                {
                    continue;
                }

                string[] lineParsed;
                CommandBase.Parse(lineText, lineNumber, CPU, out lineParsed);
                CommandBase command = CommandManager.RecognizeCommand(lineText, lineParsed, CPU, out error);

                if (command == null || error != null)
                {
                    return(error);
                }
                else
                {
                    command.Assemble(lineParsed, CPU, this);
                }
            }

            // prepare strings
            Dictionary <string, string> output = new Dictionary <string, string>();

            output.Add("{name}", "RAM");
            output.Add("{timestamp}", DateTime.Now.ToString());

            for (int i = 0; i < 64; i++)
            {
                var data        = new StringBuilder();
                var placeholder = "{" + string.Format("INIT_{0:X2}", i) + "}";

                for (int j = 31; j >= 0; j--)
                {
                    data.AppendFormat("{0:X2}", INIT[i, j]);
                }

                output.Add(placeholder, data.ToString());
            }

            for (int i = 0; i < 8; i++)
            {
                var data        = new StringBuilder();
                var placeholder = "{" + string.Format("INITP_{0:X2}", i) + "}";

                for (int j = 31; j >= 0; j--)
                {
                    data.AppendFormat("{0:X2}", INITP[i, j]);
                }

                output.Add(placeholder, data.ToString());
            }

            string templateFilename;

            if (outputFilename.ToUpper().EndsWith(".VHD"))
            {
                templateFilename = AppDomain.CurrentDomain.BaseDirectory + @"Templates\ROM_form.vhd";
            }
            else
            {
                templateFilename = AppDomain.CurrentDomain.BaseDirectory + @"Templates\ROM_form.v";
            }

            using (StreamReader sr = new StreamReader(templateFilename))
            {
                using (StreamWriter sw = new StreamWriter(outputFilename, false))
                {
                    bool flag = false;

                    while (sr.EndOfStream == false)
                    {
                        var line = sr.ReadLine();

                        if (flag)
                        {
                            foreach (KeyValuePair <string, string> kvp in output)
                            {
                                line = line.Replace(kvp.Key, kvp.Value);
                            }
                            sw.WriteLine(line);
                        }

                        if (line.Contains("{begin template}"))
                        {
                            flag = true;
                        }
                    }
                }
            }

            return(null);
        }