Advertisement
AndrewHaxalot

GCTWizard.cs

Jan 29th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.61 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.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace GeckoApp
  10. {
  11.     public partial class GCTWizard : Form
  12.     {
  13.         private object[] RAMWriteCollection;
  14.         private object[] IfThenCollection;
  15.         private object[] BasePointerCollection;
  16.         private int indexToSelect;
  17.  
  18.         private CodeController GCTCodeContents;
  19.  
  20.         public int SelectedCodeNameIndex
  21.         {
  22.             get
  23.             {
  24.                 return Math.Min(Math.Max(comboBoxCodeName.SelectedIndex, 0), GCTCodeContents.Count);
  25.             }
  26.             set
  27.             {
  28.                 if (value <= GCTCodeContents.Count && value >= 0)
  29.                     comboBoxCodeName.SelectedIndex = value;
  30.             }
  31.         }
  32.  
  33.         public GCTWizard(CodeController codeController)
  34.         {
  35.             InitializeComponent();
  36.             RAMWriteCollection = new String[] {
  37.                 "Write",
  38.                 "Fill" };
  39.             IfThenCollection = new String[] {
  40.                 "equal",
  41.                 "not equal",
  42.                 "greater",
  43.                 "lesser" };
  44.             //BasePointerCollection = new String[] {
  45.             //    "Load into",
  46.             //    "Set to",
  47.             //    "Save to",
  48.             //    "Load Code Address", };
  49.  
  50.             GCTCodeContents = codeController;
  51.         }
  52.  
  53.         private void comboBoxCodeType_SelectedIndexChanged(object sender, EventArgs e)
  54.         {
  55.             switch (comboBoxCodeType.SelectedIndex)
  56.             {
  57.                 case 0:
  58.                     comboBoxCodeSubType.Items.Clear();
  59.                     comboBoxCodeSubType.Items.AddRange(RAMWriteCollection);
  60.                     break;
  61.                 case 1:
  62.                     comboBoxCodeSubType.Items.Clear();
  63.                     comboBoxCodeSubType.Items.AddRange(IfThenCollection);
  64.                     break;
  65.                 //case 2:
  66.                 //    comboBoxCodeSubType.Items.Clear();
  67.                 //    comboBoxCodeSubType.Items.AddRange(BasePointerCollection);
  68.                 //    break;
  69.                 default:
  70.                     comboBoxCodeSubType.Items.Clear();
  71.                     comboBoxCodeSubType.Items.AddRange(RAMWriteCollection);
  72.                     break;
  73.             }
  74.             comboBoxCodeSubType.SelectedIndex = 0;
  75.         }
  76.  
  77.         public void PrepareGCTWizard(int selectedCodeIndex)
  78.         {
  79.             // Select the correct code type
  80.             comboBoxCodeType.SelectedIndex = 0;
  81.  
  82.             // Populate the code name drop down
  83.             comboBoxCodeName.Items.Clear();
  84.             for (int i = 0; i < GCTCodeContents.Count; i++)
  85.             {
  86.                 comboBoxCodeName.Items.Add(GCTCodeContents.GetCodeName(i));
  87.             }
  88.             comboBoxCodeName.Items.Add("New Code");
  89.             indexToSelect = selectedCodeIndex;
  90.             //GCTCodeContents.SelectedIndex;
  91.             //comboBoxCodeName.SelectedIndex = 0;
  92.         }
  93.  
  94.         private void GCTWizard_Shown(object sender, EventArgs e)
  95.         {
  96.             SelectedCodeNameIndex = indexToSelect;
  97.         }
  98.  
  99.         private void comboBoxCodeName_SelectedIndexChanged(object sender, EventArgs e)
  100.         {
  101.             int index = SelectedCodeNameIndex;
  102.             if (comboBoxCodeName.SelectedIndex != index)
  103.             {
  104.                 comboBoxCodeName.SelectedIndex = index;
  105.             }
  106.  
  107.             if (comboBoxCodeName.SelectedIndex == comboBoxCodeName.Items.Count - 1)
  108.             {
  109.                 textBoxCodeEntries.Text = String.Empty;
  110.             }
  111.             else
  112.             {
  113.                 textBoxCodeEntries.Text = CodeController.CodeContentToCodeTextBox(GCTCodeContents[comboBoxCodeName.SelectedIndex]);
  114.             }
  115.         }
  116.  
  117.         private void buttonClose_Click(object sender, EventArgs e)
  118.         {
  119.             DialogResult = DialogResult.Cancel;
  120.             Hide();
  121.         }
  122.  
  123.         private bool ValidUserAddress(out UInt32 address)
  124.         {
  125.             // Is it a valid 32-bit hexadecimal address?
  126.             if (!GlobalFunctions.tryToHex(textBoxAddress.Text, out address) ||
  127.                 !ValidMemory.validAddress(address))
  128.             {
  129.                 MessageBox.Show("Invalid address");
  130.                 textBoxAddress.Focus();
  131.                 textBoxAddress.SelectAll();
  132.                 return false;
  133.             }
  134.  
  135.             // Check alignment
  136.             if (radioButton16Bit.Checked && ((address & 1) != 0))
  137.             {
  138.                 MessageBox.Show("address must be multiple of 2");
  139.                 textBoxAddress.Focus();
  140.                 textBoxAddress.SelectAll();
  141.                 return false;
  142.             }
  143.             else if (radioButton32Bit.Checked && ((address & 3) != 0))
  144.             {
  145.                 MessageBox.Show("address must be multiple of 4");
  146.                 textBoxAddress.Focus();
  147.                 textBoxAddress.SelectAll();
  148.                 return false;
  149.             }
  150.  
  151.             return true;
  152.         }
  153.  
  154.         private bool ValidUserValue(out UInt32 value)
  155.         {
  156.             if (!GlobalFunctions.tryToHex(textBoxValue.Text, out value))
  157.             {
  158.                 MessageBox.Show("Invalid value");
  159.                 textBoxValue.Focus();
  160.                 textBoxValue.SelectAll();
  161.                 return false;
  162.             }
  163.  
  164.             // Check size
  165.             if (radioButton16Bit.Checked && value > 0xFFFF)
  166.             {
  167.                 MessageBox.Show("value must be <= FFFF");
  168.                 textBoxValue.Focus();
  169.                 textBoxValue.SelectAll();
  170.                 return false;
  171.             }
  172.             else if (radioButton8Bit.Checked && value > 0xFF)
  173.             {
  174.                 MessageBox.Show("value must be <= FF");
  175.                 textBoxValue.Focus();
  176.                 textBoxValue.SelectAll();
  177.                 return false;
  178.             }
  179.  
  180.             return true;
  181.         }
  182.  
  183.         private bool ValidUserMask(out UInt32 mask)
  184.         {
  185.             if (radioButton32Bit.Checked)
  186.             {
  187.                 // Mask is unused by 32-bit if
  188.                 mask = 0;
  189.                 return true;
  190.             }
  191.  
  192.             if (!GlobalFunctions.tryToHex(textBoxMask.Text, out mask))
  193.             {
  194.                 MessageBox.Show("Invalid mask");
  195.                 textBoxMask.Focus();
  196.                 textBoxMask.SelectAll();
  197.                 return false;
  198.             }
  199.  
  200.             if (mask > 0xFFFF)
  201.             {
  202.                 MessageBox.Show("mask must be <= FFFF");
  203.                 textBoxMask.Focus();
  204.                 textBoxMask.SelectAll();
  205.                 return false;
  206.             }
  207.  
  208.             return true;
  209.         }
  210.  
  211.         private bool ValidUserFill(out UInt32 fill)
  212.         {
  213.             if (radioButton32Bit.Checked)
  214.             {
  215.                 // fill is unused by 32-bit if
  216.                 fill = 0;
  217.                 return true;
  218.             }
  219.  
  220.             if (comboBoxCodeSubType.SelectedIndex == 0)
  221.             {
  222.                 // "Write" sub-type is a fill of 0!
  223.                 fill = 0;
  224.                 return true;
  225.             }
  226.  
  227.             if (!GlobalFunctions.tryToHex(textBoxFill.Text, out fill))
  228.             {
  229.                 MessageBox.Show("Invalid fill");
  230.                 textBoxFill.Focus();
  231.                 textBoxFill.SelectAll();
  232.                 return false;
  233.             }
  234.  
  235.             if (fill > 0xFFFF)
  236.             {
  237.                 MessageBox.Show("fill must be <= FFFF");
  238.                 textBoxFill.Focus();
  239.                 textBoxFill.SelectAll();
  240.                 return false;
  241.             }
  242.  
  243.             return true;
  244.         }
  245.  
  246.         private void AddCodeRAMWrite()
  247.         {
  248.             // Validate user inputs
  249.             UInt32 address, value, fill;
  250.  
  251.             bool addFill = comboBoxCodeSubType.SelectedIndex == 1;
  252.  
  253.             if (!ValidUserAddress(out address)) return;
  254.  
  255.             if (!ValidUserValue(out value)) return;
  256.  
  257.             if (!ValidUserFill(out fill)) return;
  258.  
  259.             UInt32 add;
  260.  
  261.             // Get the size
  262.             if (radioButton8Bit.Checked)
  263.             {
  264.                 // fill will be 0 if there is no fill to use
  265.                 value |= (fill << 16);
  266.  
  267.                 add = 0x00000000;
  268.             }
  269.             else if (radioButton16Bit.Checked)
  270.             {
  271.                 value |= (fill << 16);
  272.  
  273.                 add = 0x02000000;
  274.             }
  275.             else
  276.             {
  277.                 add = 0x04000000;
  278.             }
  279.  
  280.             StandardCodeAddressStuff(address, value, add);
  281.         }
  282.  
  283.         private void AddCodeIfThen()
  284.         {
  285.             // Validate user inputs
  286.             UInt32 address, value, mask;
  287.  
  288.             if (!ValidUserAddress(out address)) return;
  289.  
  290.             if (!ValidUserValue(out value)) return;
  291.  
  292.             if (!ValidUserMask(out mask)) return;
  293.            
  294.             UInt32 add;
  295.  
  296.             // Get the size
  297.             if (radioButton8Bit.Checked)
  298.             {
  299.                 MessageBox.Show("Can't do 8-bit if");
  300.                 return;
  301.             }
  302.             else if (radioButton16Bit.Checked)
  303.             {
  304.                 add = 0x28000000;
  305.                 value = (mask << 16) | value;
  306.             }
  307.             else
  308.             {
  309.                 add = 0x20000000;
  310.             }
  311.  
  312.             // Get the type of comparison
  313.             if (comboBoxCodeSubType.SelectedIndex == 0)    // equals
  314.             {
  315.                 add += 0;
  316.             }
  317.             else if (comboBoxCodeSubType.SelectedIndex == 1)    // not equals
  318.             {
  319.                 add += 0x02000000;
  320.             }
  321.             else if (comboBoxCodeSubType.SelectedIndex == 2)    // greater
  322.             {
  323.                 add += 0x04000000;
  324.             }
  325.             else if (comboBoxCodeSubType.SelectedIndex == 3)    // lesser
  326.             {
  327.                 add += 0x06000000;
  328.             }
  329.  
  330.             // End if?
  331.             if (checkBoxEndIf.Checked)
  332.             {
  333.                 add += 0x00000001;
  334.             }
  335.  
  336.             StandardCodeAddressStuff(address, value, add);
  337.         }
  338.  
  339.         private void StandardCodeAddressStuff(UInt32 address, UInt32 value, UInt32 add)
  340.         {
  341.             CodeContent nCode = CodeController.CodeTextBoxToCodeContent(textBoxCodeEntries.Text);
  342.             //UInt32 cAddressR = 0x80000000;
  343.             UInt32 rAddressR;
  344.             UInt32 offset;
  345.  
  346.             // base address is masked differently than pointer offset
  347.             if (radioButtonBA.Checked)
  348.             {
  349.                 rAddressR = address & 0xFE000000;
  350.             }
  351.             else
  352.             {
  353.                 // TODO the po doesn't actually have this restriction
  354.                 // but for now we keep it like the ba
  355.                 //rAddressR = address & 0xFEFFFFFF;
  356.                 rAddressR = address & 0xFE000000;
  357.                 add += 0x10000000;
  358.             }
  359.  
  360.             // Do we need to change the ba or po?
  361.             bool changeBAorPO = false;
  362.             if ((address & 0xFE000000) != 0x80000000)
  363.             {
  364.                 changeBAorPO = true;
  365.             }
  366.  
  367.             if (changeBAorPO)
  368.             {
  369.                 if (radioButtonBA.Checked)
  370.                 {
  371.                     nCode.addLine(0x42000000, rAddressR);
  372.                 }
  373.                 else
  374.                 {
  375.                     nCode.addLine(0x4A000000, rAddressR);
  376.                 }
  377.             }
  378.  
  379.             // Add the actual code
  380.             offset = address - rAddressR + add;
  381.             nCode.addLine(offset, value);
  382.  
  383.             // Add terminator if necessary
  384.             if (changeBAorPO)
  385.             {
  386.                 nCode.addLine(0xE0000000, 0x80008000);
  387.             }
  388.  
  389.             textBoxCodeEntries.Text = CodeController.CodeContentToCodeTextBox(nCode);
  390.         }
  391.  
  392.         private void buttonAddCode_Click(object sender, EventArgs e)
  393.         {
  394.             switch (comboBoxCodeType.SelectedIndex)
  395.             {
  396.                 case 0: AddCodeRAMWrite(); break;
  397.                 case 1: AddCodeIfThen(); break;
  398.                 default: AddCodeRAMWrite(); break;
  399.             }
  400.         }
  401.  
  402.         private void buttonStoreCode_Click(object sender, EventArgs e)
  403.         {
  404.             CheckNewCodeName();
  405.             GCTCodeContents.UpdateCode(SelectedCodeNameIndex, textBoxCodeEntries.Text);
  406.         }
  407.  
  408.         private void comboBoxCodeName_KeyDown(object sender, KeyEventArgs e)
  409.         {
  410.             // If the user hits enter...
  411.             if (e.KeyCode == Keys.Enter)
  412.             {
  413.                 CheckNewCodeName();
  414.             }
  415.         }
  416.  
  417.  
  418.         private void CheckNewCodeName()
  419.         {
  420.             // And the current text does not exist as a code name...
  421.             if (comboBoxCodeName.FindStringExact(comboBoxCodeName.Text) == -1)
  422.             {
  423.                 // Add a new entry
  424.                 GCTCodeContents.AddCode(comboBoxCodeName.Text);
  425.                 comboBoxCodeName.Items.Remove("New Code");
  426.                 comboBoxCodeName.Items.Add(comboBoxCodeName.Text);
  427.                 String codeText = textBoxCodeEntries.Text;
  428.                 comboBoxCodeName.SelectedIndex = comboBoxCodeName.Items.Count - 1;
  429.                 comboBoxCodeName.Items.Add("New Code");
  430.                 textBoxCodeEntries.Text = codeText;
  431.             }
  432.         }
  433.  
  434.         private void buttonAddStoreClose_Click(object sender, EventArgs e)
  435.         {
  436.             buttonAddCode_Click(sender, e);
  437.             buttonStoreCode_Click(sender, e);
  438.             DialogResult = DialogResult.OK;
  439.             Hide();
  440.         }
  441.  
  442.     }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement