Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GBHL.GBFile output = null;
- AssemblyProcedures = new List<AssemblyProcedure>();
- if (write)
- {
- ApplyRewriteZones();
- byte[] buffer = new byte[rom.Buffer.Length];
- Array.Copy(rom.Buffer, buffer, buffer.Length);
- output = new GBHL.GBFile(buffer);
- }
- string[] lines = rtAsm.Text.Replace("\r", "").Split('\n');
- //Format the lines
- if (ignoreWhiteSpaceToolStripMenuItem.Checked)
- {
- pbStatus.Maximum = lines.Length + 1;
- for (int i = 0; i < lines.Length; i++)
- {
- if (lines[i].Length == 0)
- {
- pbStatus.Value++;
- continue;
- }
- string s = lines[i];
- while (s.IndexOf('\t') != -1)
- s = s.Replace("\t", "");
- while (s.IndexOf(" ") != -1)
- s = s.Replace(" ", " ");
- while (s.IndexOf(" ,") != -1)
- s = s.Replace(" ,", ",");
- while (s.IndexOf(", ") != -1)
- s = s.Replace(", ", ",");
- while (s.IndexOf(" )") != -1)
- s = s.Replace(" )", ")");
- //while (s.IndexOf(" (") != -1)
- // s = s.Replace(" (", "(");
- while (s.IndexOf(") ") != -1)
- s = s.Replace(") ", ")");
- while (s.IndexOf("( ") != -1)
- s = s.Replace("( ", "(");
- if (s.Length == 0)
- {
- pbStatus.Value++;
- lines[i] = s;
- continue;
- }
- while (s[0] == ' ')
- s = s.Substring(1);
- lines[i] = s;
- pbStatus.Value++;
- }
- }
- pbStatus.Value = 0;
- pbStatus.Maximum = lines.Length * 2;
- int errorCount = 0;
- labels = new List<CodeLabel>();
- List<string> labelErrors = new List<string>();
- //We have to compile twice to add jumping to future labels
- for (int i = 0; i < 2; i++)
- {
- int activeOffset = 0;
- int line = 1;
- rtOutput.Text = "";
- int startOffset = 0;
- if (i == 1)
- AssemblyProcedures.Add(new AssemblyProcedure(0));
- foreach (string s in lines)
- {
- if (s == "" || s.StartsWith(";") || s.StartsWith(" ;"))
- {
- line++;
- pbStatus.Value++;
- continue;
- }
- if (s.StartsWith("#org "))
- {
- int value;
- if (!int.TryParse(s.Substring(5), System.Globalization.NumberStyles.HexNumber, null, out value))
- {
- rtOutput.Text += "Bad number on line " + line + ".\r\n";
- errorCount++;
- pbStatus.Value++;
- continue;
- }
- if (i == 1)
- AssemblyProcedures.Add(new AssemblyProcedure(value));
- activeOffset = value;
- startOffset = value;
- line++;
- pbStatus.Value++;
- continue;
- }
- if (s.Contains(":"))
- {
- if (i == 0)
- {
- int colonIndex = s.IndexOf(':');
- string lbl;
- lbl = s.Substring(0, colonIndex);
- if (lbl.Length == 0)
- {
- labelErrors.Add("Error on line " + line + ".\r\n");
- errorCount++;
- pbStatus.Value++;
- continue;
- }
- lbl = lbl.ToLower();
- foreach (CodeLabel c in labels)
- {
- if (c.Label == lbl)
- {
- labelErrors.Add("Duplicate label on line " + line + ".\r\n");
- errorCount++;
- break;
- }
- }
- CodeLabel cl = new CodeLabel();
- cl.Label = lbl;
- cl.Offset = activeOffset;
- cl.StartOffset = startOffset;
- labels.Add(cl);
- }
- line++;
- pbStatus.Value++;
- continue;
- }
- byte[] b = a.CompileLine(s, labels, activeOffset);
- if (b == null)
- {
- line++;
- pbStatus.Value++;
- continue;
- }
- if (i == 1)
- {
- if (b.Length == 4)
- {
- //MessageBox.Show("Error on line " + line + ".", "Error");
- //pbStatus.Value = 0;
- //return;
- rtOutput.Text += "Unknown opcode or syntax error on line " + line + ".\r\n";
- errorCount++;
- }
- if (b.Length == 5)
- {
- //MessageBox.Show("Unknown opcode on line " + line + ".", "Error");
- //pbStatus.Value = 0;
- //return;
- rtOutput.Text += "Unknown opcode or syntax error on line " + line + ".\r\n";
- errorCount++;
- }
- if (b.Length == 7)
- {
- //MessageBox.Show("Unknown label reference on line " + line + ".", "Error");
- //pbStatus.Value = 0;
- //return;
- rtOutput.Text += "Unknown label reference on line " + line + ".\r\n";
- errorCount++;
- }
- if (b.Length == 8)
- {
- //MessageBox.Show("Label out of scope on line + " + line + ".", "Error");
- //pbStatus.Value = 0;
- //return;
- rtOutput.Text += "Label out of scope on line " + line + ".\r\n";
- errorCount++;
- }
- }
- if (i == 1)
- {
- int readOnly = InReadOnlyZone(activeOffset, b);
- if (readOnly == -1)
- {
- if (write)
- output.WriteBytes(activeOffset, b);
- foreach (byte byt in b)
- AssemblyProcedures[AssemblyProcedures.Count - 1].Bytes.Add(byt);
- }
- else
- {
- rtOutput.Text += "Attempt to write to read-only address " + readOnly.ToString("X") + " at " + activeOffset.ToString("X") + " on line " + line + "\r\n";
- errorCount++;
- }
- }
- if (b.Length < 4) //For future JR jumps and an error is returned
- activeOffset += b.Length;
- else if (b.Length == 7)
- {
- if (s.StartsWith("call") || s.StartsWith("jp"))
- activeOffset += 3;
- else
- activeOffset += 2;
- }
- line++;
- pbStatus.Value++;
- //rtOutput.Text += ConvertToString(b);
- }
- }
- if (write && errorCount == 0)
- {
- rom = output;
- try
- {
- BinaryWriter bw = new BinaryWriter(File.Open(lblROM.Text, FileMode.OpenOrCreate));
- bw.Write(rom.Buffer);
- bw.Close();
- }
- catch (Exception)
- {
- MessageBox.Show("Error saving file.", "Error");
- }
- }
- pbStatus.Value = 0;
- if (errorCount > 0)
- {
- foreach (string s in labelErrors)
- rtOutput.Text += s;
- rtOutput.Text = "Assemble failed with " + errorCount + " error" + (errorCount > 1 ? "s" : "") + ".\r\n" + "=============================\r\n" + rtOutput.Text;
- }
- else
- rtOutput.Text = "Assemble successful." + (write ? " Wrote to ROM." : "");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement