Advertisement
Guest User

Untitled

a guest
Jul 8th, 2012
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.08 KB | None | 0 0
  1.             GBHL.GBFile output = null;
  2.             AssemblyProcedures = new List<AssemblyProcedure>();
  3.             if (write)
  4.             {
  5.                 ApplyRewriteZones();
  6.                 byte[] buffer = new byte[rom.Buffer.Length];
  7.                 Array.Copy(rom.Buffer, buffer, buffer.Length);
  8.                 output = new GBHL.GBFile(buffer);
  9.             }
  10.  
  11.             string[] lines = rtAsm.Text.Replace("\r", "").Split('\n');
  12.             //Format the lines
  13.             if (ignoreWhiteSpaceToolStripMenuItem.Checked)
  14.             {
  15.                 pbStatus.Maximum = lines.Length + 1;
  16.                 for (int i = 0; i < lines.Length; i++)
  17.                 {
  18.                     if (lines[i].Length == 0)
  19.                     {
  20.                         pbStatus.Value++;
  21.                         continue;
  22.                     }
  23.                     string s = lines[i];
  24.                     while (s.IndexOf('\t') != -1)
  25.                         s = s.Replace("\t", "");
  26.                     while (s.IndexOf("  ") != -1)
  27.                         s = s.Replace("  ", " ");
  28.                     while (s.IndexOf(" ,") != -1)
  29.                         s = s.Replace(" ,", ",");
  30.                     while (s.IndexOf(", ") != -1)
  31.                         s = s.Replace(", ", ",");
  32.                     while (s.IndexOf(" )") != -1)
  33.                         s = s.Replace(" )", ")");
  34.                     //while (s.IndexOf(" (") != -1)
  35.                     //  s = s.Replace(" (", "(");
  36.                     while (s.IndexOf(") ") != -1)
  37.                         s = s.Replace(") ", ")");
  38.                     while (s.IndexOf("( ") != -1)
  39.                         s = s.Replace("( ", "(");
  40.                     if (s.Length == 0)
  41.                     {
  42.                         pbStatus.Value++;
  43.                         lines[i] = s;
  44.                         continue;
  45.                     }
  46.                     while (s[0] == ' ')
  47.                         s = s.Substring(1);
  48.                     lines[i] = s;
  49.                     pbStatus.Value++;
  50.                 }
  51.             }
  52.  
  53.             pbStatus.Value = 0;
  54.             pbStatus.Maximum = lines.Length * 2;
  55.             int errorCount = 0;
  56.  
  57.             labels = new List<CodeLabel>();
  58.             List<string> labelErrors = new List<string>();
  59.             //We have to compile twice to add jumping to future labels
  60.             for (int i = 0; i < 2; i++)
  61.             {
  62.                 int activeOffset = 0;
  63.                 int line = 1;
  64.                 rtOutput.Text = "";
  65.                 int startOffset = 0;
  66.                 if (i == 1)
  67.                     AssemblyProcedures.Add(new AssemblyProcedure(0));
  68.                 foreach (string s in lines)
  69.                 {
  70.                     if (s == "" || s.StartsWith(";") || s.StartsWith(" ;"))
  71.                     {
  72.                         line++;
  73.                         pbStatus.Value++;
  74.                         continue;
  75.                     }
  76.                     if (s.StartsWith("#org "))
  77.                     {
  78.                         int value;
  79.                         if (!int.TryParse(s.Substring(5), System.Globalization.NumberStyles.HexNumber, null, out value))
  80.                         {
  81.                             rtOutput.Text += "Bad number on line " + line + ".\r\n";
  82.                             errorCount++;
  83.                             pbStatus.Value++;
  84.                             continue;
  85.                         }
  86.  
  87.                         if (i == 1)
  88.                             AssemblyProcedures.Add(new AssemblyProcedure(value));
  89.                         activeOffset = value;
  90.                         startOffset = value;
  91.                         line++;
  92.                         pbStatus.Value++;
  93.                         continue;
  94.                     }
  95.                     if (s.Contains(":"))
  96.                     {
  97.                         if (i == 0)
  98.                         {
  99.                             int colonIndex = s.IndexOf(':');
  100.  
  101.                             string lbl;
  102.                             lbl = s.Substring(0, colonIndex);
  103.                             if (lbl.Length == 0)
  104.                             {
  105.                                 labelErrors.Add("Error on line " + line + ".\r\n");
  106.                                 errorCount++;
  107.                                 pbStatus.Value++;
  108.                                 continue;
  109.                             }
  110.                             lbl = lbl.ToLower();
  111.                             foreach (CodeLabel c in labels)
  112.                             {
  113.                                 if (c.Label == lbl)
  114.                                 {
  115.                                     labelErrors.Add("Duplicate label on line " + line + ".\r\n");
  116.                                     errorCount++;
  117.                                     break;
  118.                                 }
  119.                             }
  120.                             CodeLabel cl = new CodeLabel();
  121.                             cl.Label = lbl;
  122.                             cl.Offset = activeOffset;
  123.                             cl.StartOffset = startOffset;
  124.                             labels.Add(cl);
  125.                         }
  126.                         line++;
  127.                         pbStatus.Value++;
  128.                         continue;
  129.                     }
  130.  
  131.                     byte[] b = a.CompileLine(s, labels, activeOffset);
  132.                     if (b == null)
  133.                     {
  134.                         line++;
  135.                         pbStatus.Value++;
  136.                         continue;
  137.                     }
  138.                     if (i == 1)
  139.                     {
  140.                         if (b.Length == 4)
  141.                         {
  142.                             //MessageBox.Show("Error on line " + line + ".", "Error");
  143.                             //pbStatus.Value = 0;
  144.                             //return;
  145.                             rtOutput.Text += "Unknown opcode or syntax error on line " + line + ".\r\n";
  146.                             errorCount++;
  147.                         }
  148.                         if (b.Length == 5)
  149.                         {
  150.                             //MessageBox.Show("Unknown opcode on line " + line + ".", "Error");
  151.                             //pbStatus.Value = 0;
  152.                             //return;
  153.                             rtOutput.Text += "Unknown opcode or syntax error on line " + line + ".\r\n";
  154.                             errorCount++;
  155.                         }
  156.                         if (b.Length == 7)
  157.                         {
  158.                             //MessageBox.Show("Unknown label reference on line " + line + ".", "Error");
  159.                             //pbStatus.Value = 0;
  160.                             //return;
  161.                             rtOutput.Text += "Unknown label reference on line " + line + ".\r\n";
  162.                             errorCount++;
  163.                         }
  164.                         if (b.Length == 8)
  165.                         {
  166.                             //MessageBox.Show("Label out of scope on line + " + line + ".", "Error");
  167.                             //pbStatus.Value = 0;
  168.                             //return;
  169.                             rtOutput.Text += "Label out of scope on line " + line + ".\r\n";
  170.                             errorCount++;
  171.                         }
  172.                     }
  173.                     if (i == 1)
  174.                     {
  175.                         int readOnly = InReadOnlyZone(activeOffset, b);
  176.                         if (readOnly == -1)
  177.                         {
  178.                             if (write)
  179.                                 output.WriteBytes(activeOffset, b);
  180.                             foreach (byte byt in b)
  181.                                 AssemblyProcedures[AssemblyProcedures.Count - 1].Bytes.Add(byt);
  182.                         }
  183.                         else
  184.                         {
  185.                             rtOutput.Text += "Attempt to write to read-only address " + readOnly.ToString("X") + " at " + activeOffset.ToString("X") + " on line " + line + "\r\n";
  186.                             errorCount++;
  187.                         }
  188.                     }
  189.                     if (b.Length < 4) //For future JR jumps and an error is returned
  190.                         activeOffset += b.Length;
  191.                     else if (b.Length == 7)
  192.                     {
  193.                         if (s.StartsWith("call") || s.StartsWith("jp"))
  194.                             activeOffset += 3;
  195.                         else
  196.                             activeOffset += 2;
  197.                     }
  198.                     line++;
  199.                     pbStatus.Value++;
  200.                     //rtOutput.Text += ConvertToString(b);
  201.                 }
  202.             }
  203.  
  204.             if (write && errorCount == 0)
  205.             {
  206.                 rom = output;
  207.                 try
  208.                 {
  209.                     BinaryWriter bw = new BinaryWriter(File.Open(lblROM.Text, FileMode.OpenOrCreate));
  210.                     bw.Write(rom.Buffer);
  211.                     bw.Close();
  212.                 }
  213.                 catch (Exception)
  214.                 {
  215.                     MessageBox.Show("Error saving file.", "Error");
  216.                 }
  217.             }
  218.             pbStatus.Value = 0;
  219.             if (errorCount > 0)
  220.             {
  221.                 foreach (string s in labelErrors)
  222.                     rtOutput.Text += s;
  223.                 rtOutput.Text = "Assemble failed with " + errorCount + " error" + (errorCount > 1 ? "s" : "") + ".\r\n" + "=============================\r\n" + rtOutput.Text;
  224.             }
  225.             else
  226.                 rtOutput.Text = "Assemble successful." + (write ? " Wrote to ROM." : "");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement