Advertisement
Guest User

Untitled

a guest
May 4th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 34.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Collections;
  12. using System.Drawing.Drawing2D;
  13.  
  14. namespace Virtual_SicXe
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public string[] Directives = {"START","END","BYTE","WORD","RESB","RESW" };
  19.         public Hashtable SYMTAB = new Hashtable();
  20.         public Hashtable optable = new Hashtable();
  21.         public string LhexValue2;
  22.         public string FileObj = "";
  23.         public string objectprogram = "";
  24.         public string[] opcodes = {"ADD"
  25.             ,"ADDF"
  26.             ,"ADDR"
  27.             , "AND"
  28.             , "CLEAR"
  29.             ,"COMP"
  30.             , "COMPF"
  31.             , "COMPR"
  32.             , "DIV"
  33.             , "DIVF"
  34.             , "DIVR"
  35.             , "FIX"
  36.             , "FLOAT"
  37.             , "HIO"
  38.             , "J"
  39.             , "JEQ"
  40.             , "JGT"
  41.             , "JLT"
  42.             , "JSUB"
  43.             , "LDA"
  44.             , "LDB"
  45.             , "LDCH"
  46.             , "LDF"
  47.             , "LDL"
  48.             , "LDS"
  49.             , "LDT"
  50.             , "LDX"
  51.             , "LPS"
  52.             , "MUL"
  53.             , "MULF"
  54.             , "MULR"
  55.             , "NORM"
  56.             , "OR"
  57.             , "RD"
  58.             , "RMO"
  59.             , "RSUB"
  60.             , "SHIFTL"
  61.             , "SHIFTR"
  62.             , "SIO"
  63.             , "SSK"
  64.             , "STA"
  65.             , "STB"
  66.             , "STCH"
  67.             , "STF"
  68.             , "STI"
  69.             , "STL"
  70.             , "STS"
  71.             , "STSW"
  72.             , "STT"
  73.             , "STX"
  74.             , "SUB"
  75.             , "SUBF"
  76.             , "SUBR"
  77.             , "SVC"
  78.             , "TD"
  79.             , "TIO"
  80.             , "TIX"
  81.             , "TIXR"
  82.             , "WD"
  83.                            };
  84.  
  85.         public string[] opcodetype ={"3",
  86.             "3",
  87.             "2",
  88.             "3",
  89.             "2",
  90.             "3",
  91.             "3",
  92.             "2",
  93.             "3",
  94.             "3",
  95.             "2",
  96.             "1",
  97.             "1",
  98.             "1",
  99.             "3",
  100.             "3",
  101.             "3",
  102.             "3",
  103.             "3",
  104.             "3",
  105.             "3",
  106.             "3",
  107.             "3",
  108.             "3",
  109.             "3",
  110.             "3",
  111.             "3",
  112.             "3",
  113.             "3",
  114.             "3",
  115.             "2",
  116.             "1",
  117.             "3",
  118.             "3",
  119.             "2",
  120.             "3",
  121.             "2",
  122.             "2",
  123.             "1",
  124.             "3",
  125.             "3",
  126.             "3",
  127.             "3",
  128.             "3",
  129.             "3",
  130.             "3",
  131.             "3",
  132.             "3",
  133.             "3",
  134.             "3",
  135.             "3",
  136.             "3",
  137.             "2",
  138.             "2",
  139.             "3",
  140.             "1",
  141.             "3",
  142.             "2",
  143.             "3", };
  144.  
  145.         public string intermediate = "";
  146.         public string FinalObjectCode = "";
  147.         public Form1()
  148.         {
  149.             InitializeComponent();
  150.             this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
  151.             optable.Add("ADD", "18");
  152.             optable.Add("ADDF", "58");
  153.             optable.Add("ADDR", "90");
  154.             optable.Add("AND", "40");
  155.             optable.Add("CLEAR", "B4");
  156.             optable.Add("COMP", "28");
  157.             optable.Add("COMPF", "88");
  158.             optable.Add("COMPR", "A0");
  159.             optable.Add("DIV", "24");
  160.             optable.Add("DIVF", "64");
  161.             optable.Add("DIVR", "9C");
  162.             optable.Add("FIX", "C4");
  163.             optable.Add("FLOAT", "C0");
  164.             optable.Add("HIO", "F4");
  165.             optable.Add("J", "3C");
  166.             optable.Add("JEQ", "30");
  167.             optable.Add("JGT", "34");
  168.             optable.Add("JLT", "38");
  169.             optable.Add("JSUB", "48");
  170.             optable.Add("LDA", "00");
  171.             optable.Add("LDB", "68");
  172.             optable.Add("LDCH", "50");
  173.             optable.Add("LDF", "70");
  174.             optable.Add("LDL", "08");
  175.             optable.Add("LDS", "6C");
  176.             optable.Add("LDT", "74");
  177.             optable.Add("LDX", "04");
  178.             optable.Add("LPS", "D0");
  179.             optable.Add("MUL", "20");
  180.             optable.Add("MULF", "60");
  181.             optable.Add("MULR", "98");
  182.             optable.Add("NORM", "C8");
  183.             optable.Add("OR", "44");
  184.             optable.Add("RD", "D8");
  185.             optable.Add("RMO", "AC");
  186.             optable.Add("RSUB", "4C");
  187.             optable.Add("SHIFTL", "A4");
  188.             optable.Add("SHIFTR", "A8");
  189.             optable.Add("SIO", "F0");
  190.             optable.Add("SSK", "EC");
  191.             optable.Add("STA", "0C");
  192.             optable.Add("STB", "78");
  193.             optable.Add("STCH", "54");
  194.             optable.Add("STF", "80");
  195.             optable.Add("STI", "D4");
  196.             optable.Add("STL", "14");
  197.             optable.Add("STS", "7C");
  198.             optable.Add("STSW", "E8");
  199.             optable.Add("STT", "84");
  200.             optable.Add("STX", "10");
  201.             optable.Add("SUB", "1C");
  202.             optable.Add("SUBF", "5C");
  203.             optable.Add("SUBR", "94");
  204.             optable.Add("SVC", "B0");
  205.             optable.Add("TD", "E0");
  206.             optable.Add("TIO", "F8");
  207.             optable.Add("TIX", "2C");
  208.             optable.Add("TIXR", "B8");
  209.             optable.Add("WD", "DC");
  210.         }
  211.  
  212.         private void Form1_Load(object sender, EventArgs e){}
  213.         private void loadFileToolStripMenuItem_Click(object sender, EventArgs e)
  214.         {
  215.             OpenFileDialog openFileDialog1 = new OpenFileDialog();
  216.             StreamReader sr;
  217.             int StartAddress = 0;
  218.             openFileDialog1.InitialDirectory = @"e:\";
  219.             int line = 0;
  220.             openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  221.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  222.             {
  223.                 label1.Show();
  224.                 label2.Show();
  225.                 tableLayoutPanel1.Controls.Add(new Label() { Text = "Line" }, 0, 0);
  226.                 tableLayoutPanel1.Controls.Add(new Label() { Text = "Address" }, 1, 0);
  227.                 tableLayoutPanel1.Controls.Add(new Label() { Text = "Symbol" }, 2, 0);
  228.                 tableLayoutPanel1.Controls.Add(new Label() { Text = "Opcode" }, 3, 0);
  229.                 tableLayoutPanel1.Controls.Add(new Label() { Text = "Operand" }, 4, 0);
  230.  
  231.                 tableLayoutPanel2.Controls.Add(new Label() { Text = "Symbol" }, 0, 0);
  232.                 tableLayoutPanel2.Controls.Add(new Label() { Text = "Address" }, 1, 0);
  233.  
  234.                 int i = 1;
  235.  
  236.                 sr = new StreamReader(openFileDialog1.FileName);
  237.                 String x = sr.ReadLine();
  238.                 string aa = x.Substring(0, 8);
  239.                 string space1 = x.Substring(8, 1);
  240.                 string opcode = x.Substring(9, 6);
  241.                 string space2 = x.Substring(15, 2);
  242.                 string operand = x.Substring(17, 18);
  243.                 StartAddress = Convert.ToInt32(operand);
  244.                 if (!aa.Contains("      "))
  245.                 {
  246.                     SYMTAB.Add(aa, StartAddress);
  247.                 }
  248.  
  249.                 tableLayoutPanel1.Controls.Add(new Label() { Text = line.ToString() }, 0, i);
  250.                 tableLayoutPanel1.Controls.Add(new Label() { Text = operand }, 1, i);
  251.                 tableLayoutPanel1.Controls.Add(new Label() { Text = aa }, 2, i);
  252.                 tableLayoutPanel1.Controls.Add(new Label() { Text = opcode }, 3, i);
  253.                 tableLayoutPanel1.Controls.Add(new Label() { Text = operand }, 4, i);
  254.                 string LOCCTR = StartAddress.ToString();
  255.                 string CurrentLoc;
  256.                 string Error = "";
  257.  
  258.                 intermediate += LOCCTR + "   " + x + Environment.NewLine;
  259.  
  260.                 while ((x = sr.ReadLine()) != null)
  261.                 {
  262.                     string LhexValue = "";
  263.                     CurrentLoc = LOCCTR;
  264.                     i++;
  265.                     if (x.Substring(0, 1) == ".")
  266.                     {
  267.                         i--;
  268.                         continue;
  269.                     }
  270.                     aa = x.Substring(0, 8);
  271.                     space1 = x.Substring(8, 1);
  272.                     opcode = x.Substring(9, 6);
  273.  
  274.                     if (opcode.Trim() == "END")
  275.                     {
  276.                         intermediate += "       " + aa + space1 + opcode;
  277.                     }
  278.                     else
  279.                     {
  280.                         intermediate += LOCCTR + "   " + x + Environment.NewLine;
  281.                     }
  282.                    
  283.                     if (opcode.Trim() == "END")
  284.                     {
  285.                         tableLayoutPanel1.Controls.Add(new Label() { Text = (++line).ToString() }, 0, i);
  286.                         tableLayoutPanel1.Controls.Add(new Label() { Text = "" }, 1, i);
  287.                         tableLayoutPanel1.Controls.Add(new Label() { Text = aa }, 2, i);
  288.                         tableLayoutPanel1.Controls.Add(new Label() { Text = opcode }, 3, i);
  289.                         tableLayoutPanel1.Controls.Add(new Label() { Text = "" }, 4, i);
  290.                         break;
  291.                     }
  292.  
  293.                     space2 = x.Substring(15, 2);
  294.                     operand = x.Substring(17, 18);
  295.  
  296.                     string opcodeValue = opcode;
  297.                     if (opcodeValue.Substring(0, 1) == "+")
  298.                     {
  299.                         opcodeValue = opcodeValue.Substring(1, 5);
  300.                     }
  301.                     if (!optable.ContainsKey(opcodeValue.Trim()) && !Directives.Contains(opcodeValue.Trim()))
  302.                     {
  303.                         Error += "unidentified opcode at line : " + line;
  304.                         MessageBox.Show(Error);
  305.                         break;
  306.                     }
  307.                     int index_of_opcode = Array.IndexOf(opcodes, opcode.Trim());
  308.                     string type0="";
  309.                     if (index_of_opcode>0)
  310.                         type0 = opcodetype[index_of_opcode];
  311.  
  312.                     if (!aa.Contains("        "))
  313.                     {
  314.                         if (SYMTAB.ContainsKey(aa))
  315.                         {
  316.                             Error += "Dublicate Symbol Table at line " + (line + 1);
  317.                             MessageBox.Show(Error);
  318.                             break;
  319.                         }
  320.                         else
  321.                             SYMTAB.Add(aa, CurrentLoc);
  322.                     }
  323.  
  324.  
  325.                     if (opcode.Substring(0, 1) == "+")
  326.                     {
  327.                         int decAgain = int.Parse(LOCCTR, System.Globalization.NumberStyles.HexNumber);
  328.                         decAgain += 4;
  329.                         LhexValue = decAgain.ToString("X");
  330.                         LOCCTR = LhexValue;
  331.                     }
  332.                     else if (opcode == "RESB  ")
  333.                     {
  334.                         int decAgain = int.Parse(LOCCTR, System.Globalization.NumberStyles.HexNumber);
  335.                         decAgain += int.Parse(operand);
  336.                         LhexValue = decAgain.ToString("X");
  337.                         LOCCTR = LhexValue;
  338.                     }
  339.                     else if (opcode == "RESW  ")
  340.                     {
  341.                         int decAgain = int.Parse(LOCCTR, System.Globalization.NumberStyles.HexNumber);
  342.                         decAgain += (int.Parse(operand) * 3);
  343.                         LhexValue = decAgain.ToString("X");
  344.                         LOCCTR = LhexValue;
  345.                     }
  346.                     else if (opcode == "BYTE  ")
  347.                     {
  348.                         string AsciiType = operand.Substring(0, 1);
  349.                         string op2 = operand.Substring(2);
  350.                         int j = 0;
  351.                         int cnt = 0;
  352.                         while (op2[j] != ' ')
  353.                         {
  354.                             cnt++;
  355.                             j++;
  356.                         }
  357.                         cnt -= 1;
  358.  
  359.                         int decAgain = int.Parse(LOCCTR, System.Globalization.NumberStyles.HexNumber);
  360.                         if (AsciiType == "C")
  361.                             decAgain += cnt;
  362.                         else
  363.                             decAgain += cnt / 2;
  364.                         LhexValue = decAgain.ToString("X");
  365.                         LOCCTR = LhexValue;
  366.                     }
  367.                     else if (type0=="2")
  368.                     {
  369.                         int decAgain = int.Parse(LOCCTR, System.Globalization.NumberStyles.HexNumber);
  370.                         decAgain += 2;
  371.                         LhexValue = decAgain.ToString("X");
  372.                         LOCCTR = LhexValue;
  373.                     }
  374.                     else if (operand.Contains("                  ") && opcode.Trim() !="RSUB")
  375.                     {
  376.                         int decAgain = int.Parse(LOCCTR, System.Globalization.NumberStyles.HexNumber);
  377.                         decAgain += 1;
  378.                         LhexValue = decAgain.ToString("X");
  379.                         LOCCTR = LhexValue;
  380.                     }
  381.                     else
  382.                     {
  383.                         int decAgain = int.Parse(LOCCTR, System.Globalization.NumberStyles.HexNumber);
  384.                         decAgain += 3;
  385.                         LhexValue = decAgain.ToString("X");
  386.                         LOCCTR = LhexValue;
  387.                     }
  388.                     line++;
  389.                     tableLayoutPanel1.Controls.Add(new Label() { Text = line.ToString() }, 0, i);
  390.                     tableLayoutPanel1.Controls.Add(new Label() { Text = CurrentLoc }, 1, i);
  391.                     tableLayoutPanel1.Controls.Add(new Label() { Text = aa }, 2, i);
  392.                     tableLayoutPanel1.Controls.Add(new Label() { Text = opcode }, 3, i);
  393.                     tableLayoutPanel1.Controls.Add(new Label() { Text = operand }, 4, i);
  394.                 }
  395.                 label4.Text = LOCCTR;
  396.                 string srtAddress = StartAddress.ToString();
  397.                 int Fv = int.Parse(LOCCTR, System.Globalization.NumberStyles.HexNumber);
  398.                 Fv -= int.Parse(srtAddress, System.Globalization.NumberStyles.HexNumber);
  399.                 LhexValue2 = Fv.ToString("X");
  400.                 label6.Text = LhexValue2;
  401.                 ICollection key = SYMTAB.Keys;
  402.                
  403.                
  404.                 int cntr = 1;
  405.                 foreach (string k in key)
  406.                 {
  407.                     tableLayoutPanel2.Controls.Add(new Label() { Text = SYMTAB[k].ToString() }, 0, cntr);
  408.                     tableLayoutPanel2.Controls.Add(new Label() { Text = k }, 1, cntr);
  409.                     cntr++;
  410.                 }
  411.  
  412.             }
  413.         }
  414.  
  415.         private void saveSymtabToolStripMenuItem1_Click(object sender, EventArgs e)
  416.         {
  417.             SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  418.             saveFileDialog1.InitialDirectory = @"e:\";
  419.             saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  420.             ICollection key = SYMTAB.Keys;
  421.  
  422.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  423.             {
  424.                 string symboltable="";
  425.                 foreach(string k in key)
  426.                 {
  427.                     symboltable += k + ": " + SYMTAB[k] + Environment.NewLine;
  428.                 }
  429.                 File.WriteAllText(saveFileDialog1.FileName, symboltable);
  430.             }
  431.         }
  432.  
  433.         private void saveIntermediateFileToolStripMenuItem_Click(object sender, EventArgs e)
  434.         {
  435.             SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  436.             saveFileDialog1.InitialDirectory = @"e:\";
  437.             saveFileDialog1.Filter = "Intermediate files (*.txt)|*.txt|All files (*.*)|*.*";
  438.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  439.             {
  440.  
  441.                 File.WriteAllText(saveFileDialog1.FileName, intermediate);
  442.             }
  443.         }
  444.         private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e){}
  445.         private void button1_Click(object sender, EventArgs e){}
  446.         private void button2_Click(object sender, EventArgs e){}
  447.         private void button3_Click(object sender, EventArgs e){}
  448.         private void label7_Click(object sender, EventArgs e){}
  449.         private void openIntermediateToolStripMenuItem_Click(object sender, EventArgs e)
  450.         {
  451.             OpenFileDialog openFileDialog1 = new OpenFileDialog();
  452.             openFileDialog1.InitialDirectory = @"e:\";
  453.             openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  454.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  455.             {
  456.                 //**************************************************
  457.                 //*********************biginPass2*******************
  458.                 //**************************************************
  459.                 string aaaadddddd = File.ReadAllText(openFileDialog1.FileName);
  460.                 Hashtable registers = new Hashtable();
  461.                 registers.Add("A", "0");
  462.                 registers.Add("X", "1");
  463.                 registers.Add("L", "2");
  464.                 registers.Add("B", "3");
  465.                 registers.Add("S", "4");
  466.                 registers.Add("T", "5");
  467.                 registers.Add("F", "6");
  468.                 registers.Add("PC", "8");
  469.                 registers.Add("SW", "9");
  470.  
  471.                 string[] words = aaaadddddd.Split('\n');
  472.                 string objectcode = "";
  473.                 string type;
  474.                 string ProgramName = "";
  475.                 string Pass2Err = "";
  476.                 int cntINTER = 1;
  477.                 string StartingAddr = "";
  478.                 string StartingAddrForI = "";
  479.                 foreach (string strrr in words)
  480.                 {
  481.                     if (strrr == "")
  482.                     {
  483.                         continue;
  484.                     }
  485.                     string aa2 = strrr.Substring(7, 8);
  486.                     string space12 = strrr.Substring(15, 1);
  487.                     string opcode2 = strrr.Substring(16, 6);
  488.                     string space22 = strrr.Substring(22, 2);
  489.                     StartingAddrForI = strrr.Substring(0, 4);
  490.                     string operand2 = strrr.Substring(24, 18);
  491.                     string OperandTemp = operand2;
  492.  
  493.                     if (operand2.Substring(0, 1) == "@")
  494.                         operand2 = operand2.Substring(1);
  495.  
  496.                     string nextInst = words[cntINTER + 1].Substring(0, 4);//strrr.Substring(0, 4);
  497.                     string currentLocFORBYTE = strrr.Substring(1, 4);
  498.                     if (opcode2.Trim() == "START")
  499.                     {
  500.                         ProgramName = aa2;
  501.                         StartingAddr = "00" + strrr.Substring(0, 4);
  502.                         continue;
  503.                     }
  504.                     string opcodeValue = opcode2;
  505.                     if (opcodeValue.Substring(0, 1) == "+")
  506.                     {
  507.                         opcodeValue = opcodeValue.Substring(1, 5);
  508.                     }
  509.                     if (opcodeValue.Trim() == "RESW" || opcodeValue.Trim() == "RESB")
  510.                     {
  511.                         continue;
  512.                     }
  513.                     if (opcodeValue.Trim() == "BYTE")
  514.                     {
  515.                         string AsciiType = operand2.Substring(0, 1);
  516.                         string op2 = operand2.Substring(2);
  517.                         int j = 0;
  518.                         int cnt = 0;
  519.                         while (op2[j] != ' ')
  520.                         {
  521.                             cnt++;
  522.                             j++;
  523.                         }
  524.                         cnt -= 1;
  525.                         char ch;
  526.  
  527.                         // the following lines for pass 2
  528.                         if (AsciiType == "C")
  529.                         {
  530.                             string hexV = "";
  531.                             for (j = 0; j < cnt; j++)
  532.                             {
  533.                                 ch = op2[j];
  534.                                 byte ByteOp = Convert.ToByte(ch);
  535.                                 hexV += ByteOp.ToString("x");
  536.                             }
  537.                             objectcode = hexV;
  538.                         }
  539.                         else
  540.                         {
  541.                             string hexV = "";
  542.                             for (j = 0; j < cnt; j++)
  543.                             {
  544.                                 hexV += op2[j];
  545.                             }
  546.                             objectcode = hexV;
  547.                         }
  548.  
  549.                     }
  550.                     else if (opcodeValue.Trim() == "WORD")
  551.                     {
  552.                         int decAgain = int.Parse(operand2.Trim());
  553.                         string LhexValue = decAgain.ToString("X");
  554.                         if (LhexValue.Length == 1)
  555.                             LhexValue = "00000" + LhexValue;
  556.                         else if (LhexValue.Length == 2)
  557.                             LhexValue = "0000" + LhexValue;
  558.                         else if (LhexValue.Length == 3)
  559.                             LhexValue = "000" + LhexValue;
  560.                         else if (LhexValue.Length == 4)
  561.                             LhexValue = "00" + LhexValue;
  562.                         else if (LhexValue.Length == 5)
  563.                             LhexValue = "0" + LhexValue;
  564.  
  565.                         objectcode = LhexValue;
  566.  
  567.                     }
  568.                     else if (opcodeValue.Trim() == "RSUB")
  569.                     {
  570.                         string hexopcode = optable[opcodeValue.Trim()].ToString();
  571.                         string binaryopcode = Convert.ToString(Convert.ToInt32(hexopcode.Substring(1, 1), 16), 2).PadLeft(4, '0');
  572.                         string binaryout = binaryopcode.Substring(0, 2) + "11";
  573.                         string hexout = Convert.ToInt32(binaryout, 2).ToString("X");
  574.                         objectcode = hexopcode.Substring(0, 1) + hexout + "0000";
  575.                     }
  576.                     else
  577.                     {
  578.                         int index_of_opcode1 = Array.IndexOf(opcodes, opcodeValue.Trim());
  579.                         type = opcodetype[index_of_opcode1];
  580.                         if (OperandTemp.Contains(",X") && type != "2")
  581.                         {
  582.                             operand2 = operand2.Substring(0, operand2.IndexOf(","));
  583.                             operand2 += "          ";
  584.                         }
  585.                         if (type == "1")
  586.                         {
  587.                             objectcode = optable[opcodeValue.Trim()].ToString();
  588.                         }
  589.  
  590.                         else if (type == "2")
  591.                         {
  592.                             objectcode = optable[opcodeValue.Trim()].ToString();
  593.                             string reg1 = operand2.Substring(0, 1);
  594.                             string reg2 = operand2.Substring(2, 1);
  595.  
  596.                             objectcode += registers[reg1];
  597.                             if (reg2 != " ")
  598.                             {
  599.                                 objectcode += registers[reg2];
  600.                             }
  601.                             else
  602.                             {
  603.                                 objectcode += 0;
  604.                             }
  605.                         }
  606.                         else if (type == "3")
  607.                         {
  608.                             if (operand2.Substring(0, 1) == "#")
  609.                             {
  610.  
  611.                                 if (int.Parse((operand2.Substring(1, 17)).Trim()) >= 0 && int.Parse((operand2.Substring(1, 17)).Trim()) <= 4095)
  612.                                 {
  613.                                     string immediatevalue = (operand2.Substring(1, 17)).Trim();
  614.                                     string immediatehex = int.Parse(immediatevalue).ToString("X");
  615.                                     string hexopcode = optable[opcodeValue.Trim()].ToString();
  616.                                     string binaryopcode = Convert.ToString(Convert.ToInt32(hexopcode.Substring(1, 1), 16), 2).PadLeft(4, '0');
  617.                                     string binaryout = binaryopcode.Substring(0, 2) + "01";
  618.                                     string hexout = Convert.ToInt32(binaryout, 2).ToString("X");
  619.  
  620.                                     if (immediatehex.Length == 1)
  621.                                     {
  622.                                         immediatehex = "00" + immediatehex;
  623.                                     }
  624.                                     if (immediatehex.Length == 2)
  625.                                     {
  626.                                         immediatehex = "0" + immediatehex;
  627.                                     }
  628.  
  629.                                     objectcode = hexopcode.Substring(0, 1) + hexout + "0" + immediatehex;
  630.                                 }
  631.  
  632.                                 else if (int.Parse((operand2.Substring(1, 17)).Trim()) >= 4096 && opcode2.Substring(0, 1) == "+")
  633.                                 {
  634.                                     string immediatevalue = (operand2.Substring(1, 17)).Trim();
  635.                                     string immediatehex = int.Parse(immediatevalue).ToString("X");
  636.                                     string hexopcode = optable[opcodeValue.Trim()].ToString();
  637.                                     string binaryopcode = Convert.ToString(Convert.ToInt32(hexopcode.Substring(1, 1), 16), 2).PadLeft(4, '0');
  638.                                     string binaryout = binaryopcode.Substring(0, 2) + "01";
  639.                                     string hexout = Convert.ToInt32(binaryout, 2).ToString("X");
  640.                                     if (immediatehex.Length == 4)
  641.                                     {
  642.                                         immediatehex = "0" + immediatehex;
  643.                                     }
  644.                                     objectcode = hexopcode.Substring(0, 1) + hexout + "1" + immediatehex;
  645.                                 }
  646.                             }
  647.                             else if (!operand2.Equals(""))
  648.                             {
  649.                                 //compute TA-PC
  650.                                 int diff = 0;
  651.                                 int progCTR = int.Parse(nextInst, System.Globalization.NumberStyles.HexNumber);
  652.                                 string locTarADD;
  653.                                 bool SymLoc2 = (SYMTAB.ContainsKey(operand2.Substring(0, 8)));
  654.                                 int loctaradd = 0;
  655.                                 if (SymLoc2)
  656.                                 {
  657.                                     string targetADD = operand2.Substring(0, 8).ToString();
  658.                                     locTarADD = SYMTAB[targetADD].ToString();
  659.                                     loctaradd = int.Parse(locTarADD, System.Globalization.NumberStyles.HexNumber);
  660.                                 }
  661.                                 if (loctaradd < progCTR)
  662.                                 {
  663.                                     diff = (progCTR - loctaradd);
  664.                                 }
  665.                                 else
  666.                                     diff = loctaradd - progCTR;
  667.                                    
  668.                                 if (opcode2.Substring(0, 1) == "+")
  669.                                 {
  670.                                     bool SymLoc = (SYMTAB.ContainsKey(operand2.Substring(0, 8)));
  671.                                     if (SymLoc)
  672.                                     {
  673.                                         string operandSYMTBL = operand2.Substring(0, 8).ToString();
  674.                                         string LocationSYMBOL = SYMTAB[operandSYMTBL].ToString();
  675.                                         string hexopcode = optable[opcodeValue.Trim()].ToString();
  676.                                         string binaryopcode = Convert.ToString(Convert.ToInt32(hexopcode.Substring(1, 1), 16), 2).PadLeft(4, '0');
  677.                                         string binaryout = binaryopcode.Substring(0, 2) + "11";
  678.  
  679.                                         string hexout = Convert.ToInt32(binaryout, 2).ToString("X");
  680.                                         if (LocationSYMBOL.Length == 4)
  681.                                         {
  682.                                             LocationSYMBOL = "0" + LocationSYMBOL;
  683.                                         }
  684.                                         objectcode = hexopcode.Substring(0, 1) + hexout + "1" + LocationSYMBOL;
  685.  
  686.                                     }//else generate error
  687.                                     else
  688.                                     {
  689.                                         Pass2Err += "unidentified Symbol Location for " + opcode2 + " instruction.";
  690.                                         MessageBox.Show(Pass2Err);
  691.                                     }
  692.                                 }
  693.                                 else if (diff >= -2048 && diff <= 2047)
  694.                                 {
  695.                                     string disp;
  696.  
  697.                                     if (loctaradd < progCTR)
  698.                                     {
  699.                                         //Convert To 2
  700.                                         string hx = diff.ToString();
  701.                                         ushort intVal = Convert.ToUInt16(hx, 16);  
  702.                                         ushort twosComp = (ushort)(~intVal + 1);        // twosComp == 4294902016
  703.                                         string h = string.Format("{0:X}", twosComp); // h == "FFFF0100"
  704.                                         disp = h.Substring(1, 3).ToString();
  705.                                     }
  706.                                     else
  707.                                         disp = diff.ToString("X");
  708.  
  709.                                     string hexopcode = optable[opcodeValue.Trim()].ToString();
  710.                                     string binaryopcode = Convert.ToString(Convert.ToInt32(hexopcode.Substring(1, 1), 16), 2).PadLeft(4, '0');
  711.  
  712.                                     string binaryout = binaryopcode.Substring(0, 2) + "11";
  713.  
  714.                                     string hexout = Convert.ToInt32(binaryout, 2).ToString("X");
  715.                                     if (disp.Length == 1)
  716.                                     {
  717.                                         disp = "00" + disp;
  718.                                     }
  719.                                     if (disp.Length == 2)
  720.                                     {
  721.                                         disp = "0" + disp;
  722.                                     }
  723.                                     objectcode = hexopcode.Substring(0, 1) + hexout + "2" + disp;
  724.                                 }
  725.                             }
  726.  
  727.                             if (OperandTemp.Substring(0, 1) == "@")
  728.                             {
  729.                                 string hexopcode = optable[opcodeValue.Trim()].ToString();
  730.                                 string binaryopcode = Convert.ToString(Convert.ToInt32(hexopcode.Substring(1, 1), 16), 2).PadLeft(4, '0');
  731.                                 string binaryout = binaryopcode.Substring(0, 2) + "10";
  732.                                 string hexout;
  733.                                 if (binaryout.Length == 4)
  734.                                 {
  735.                                     hexout = "0" + Convert.ToInt32(binaryout, 2).ToString("X");
  736.                                 }
  737.                                 else
  738.                                     hexout = Convert.ToInt32(binaryout, 2).ToString("X");
  739.  
  740.                                 objectcode = hexout + objectcode.Substring(2);
  741.                             }
  742.                             if (OperandTemp.Contains(",X") && type != "2")
  743.                             {
  744.                                 string hexopcode = objectcode.Substring(2, 1);
  745.                                 int binaryopcode = Convert.ToInt32(hexopcode.Substring(0, 1), 16);
  746.                                 int binaryopcode2 = Convert.ToInt32("8", 16);
  747.                                 int res = binaryopcode + binaryopcode2;
  748.                                 string binaryout = res.ToString("X");
  749.                                 objectcode = objectcode.Substring(0, 2) + binaryout + objectcode.Substring(3);
  750.  
  751.                             }
  752.  
  753.                         }
  754.                     }
  755.  
  756.                     objectcode += Environment.NewLine;
  757.                     FinalObjectCode += objectcode;
  758.                     cntINTER++;
  759.                     OperandTemp = "";
  760.                 }
  761.  
  762.                 string[] Fobj = FinalObjectCode.Split('\n');
  763.                 FileObj = "H^" + ProgramName.Trim() + " " + "^" + StartingAddr + "^" + "00" + LhexValue2;
  764.                 objectprogram += FileObj;
  765.                 int NumOfchars = 0;
  766.                 FileObj = Environment.NewLine + "T";
  767.                 for (int k = 0; k < Fobj.Length; k++)
  768.                 {
  769.                     FileObj += "^" + Fobj[k];
  770.                     NumOfchars += Fobj[k].Length;
  771.                     if (NumOfchars > 60)
  772.                     {
  773.                         NumOfchars -= Fobj[k].Length;
  774.                         NumOfchars /= 2;
  775.                         NumOfchars -= 1;
  776.  
  777.                         FileObj = FileObj.Substring(0, 10) + "^" + NumOfchars.ToString("X") + "^" + FileObj.Substring(4);
  778.                         objectprogram += FileObj;
  779.                         FileObj = "";
  780.  
  781.                         NumOfchars = 0;
  782.                         FileObj = Environment.NewLine + "T";
  783.                     }
  784.                 }
  785.                 NumOfchars -= Fobj[Fobj.Length - 1].Length;
  786.  
  787.                 NumOfchars /= 2;
  788.                 NumOfchars -= 2;
  789.  
  790.                 FileObj = FileObj.Substring(0, 10) + "^0" + NumOfchars.ToString("X") + "^" + FileObj.Substring(4);
  791.                 objectprogram += FileObj;
  792.                 FileObj = Environment.NewLine + "E^" + StartingAddr;
  793.                 objectprogram += FileObj;
  794.                 MessageBox.Show("succefull");
  795.             }
  796.         }
  797.  
  798.         private void saveObjectProgramToolStripMenuItem_Click(object sender, EventArgs e)
  799.         {
  800.             SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  801.             saveFileDialog1.InitialDirectory = @"e:\";
  802.             saveFileDialog1.Filter = "Intermediate files (*.txt)|*.txt|All files (*.*)|*.*";
  803.  
  804.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  805.             {
  806.                 File.WriteAllText(saveFileDialog1.FileName, objectprogram);
  807.                 MessageBox.Show("succefull");
  808.             }
  809.         }
  810.         private void tableLayoutPanel2_Paint(object sender, PaintEventArgs e)
  811.         {
  812.         }
  813.     }
  814. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement