Exemplo n.º 1
0
        public TheAssembler(TheCpu CPU)
        {
            this.INIT  = new byte[64, 32];
            this.INITP = new byte[8, 32];

            this.CPU          = CPU;
            this.LineMappings = new Dictionary <int, int>();
        }
Exemplo n.º 2
0
        public static CommandBase RecognizeCommand(string line, string[] lines, TheCpu cpu, out string error)
        {
            bool isValid;

            // Keep cache small enounght
            if (Cache.Count > 1024 * 2)
            {
                Cache.Clear();
            }

            // Find first in cache
            if (Cache.ContainsKey(line))
            {
                CommandBase command = Cache[line];
                isValid = command.Verify(lines, cpu, out error);
                if (isValid)
                {
                    return(command);
                }
                else
                {
                    Cache.Remove(line);
                }
            }

            // Recognize instruction
            foreach (CommandBase command in Commands)
            {
                isValid = command.Verify(lines, cpu, out error);
                if (isValid)
                {
                    Cache.Add(line, command);
                    return(command);
                }
            }

            error = "Непозната инструкция";
            return(null);
        }
Exemplo n.º 3
0
 public void Prepare(CPU.TheCpu CPU)
 {
     cpu = CPU;
     cpu.PropertyChanged += new PropertyChangedEventHandler(CPU_PropertyChanged);
 }
Exemplo n.º 4
0
 public void Enable(CPU.TheCpu cpu)
 {
     this.bsCPU.DataSource = typeof(CPU.TheCpu);
     this.bsCPU.DataSource = cpu;
     this.Enabled          = true;
 }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
 public void Prepare(CPU.TheCpu cpu)
 {
     _cpu = cpu;
     _cpu.PropertyChanged += new PropertyChangedEventHandler(CPU_PropertyChanged);
 }