RegistersForm.cs
上传用户:huajielb
上传日期:2022-07-29
资源大小:626k
文件大小:29k
源码类别:

驱动编程

开发平台:

Visual C++

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using Jungo.wdapi_dotnet;
  7. using Jungo.plx_lib;
  8. using wdc_err = Jungo.wdapi_dotnet.WD_ERROR_CODES;
  9. using UINT64 = System.UInt64;
  10. using DWORD = System.UInt32;
  11. using UINT32 = System.UInt32;
  12. using WORD = System.UInt16;
  13. using BYTE = System.Byte;
  14. using BOOL = System.Boolean;
  15. namespace Jungo.PLX_Sample
  16. {
  17.     public class RegistersForm : System.Windows.Forms.Form
  18.     {
  19.         private Exception m_excp;
  20.         private PLX_Device m_device;
  21.         private WDC_REG[] m_regs;
  22.         private RW m_direction;
  23.         private DWORD m_dwOffset;
  24.         private ACTION_TYPE m_regType;
  25.         private BYTE m_bData;
  26.         private WORD m_wData;
  27.         private UINT32 m_u32Data;
  28.         private UINT64 m_u64Data;
  29.         private System.Windows.Forms.TextBox txtOffset;
  30.         private System.Windows.Forms.Label lblOffset;
  31.         private System.Windows.Forms.Label label3;
  32.         private System.Windows.Forms.Label lblRegs;
  33.         private System.Windows.Forms.Button btReadAll;
  34.         private System.Windows.Forms.ComboBox cmboRegs;
  35.         private System.Windows.Forms.TextBox txtData;
  36.         private System.Windows.Forms.Button btExit;
  37.         private System.Windows.Forms.Button btRead;
  38.         private System.Windows.Forms.Button btWrite;
  39.         private System.Windows.Forms.Button btLog;
  40.         private System.Windows.Forms.TextBox txtInput;
  41.         private System.Windows.Forms.Label label2;
  42.         private System.Windows.Forms.Label lblInput;
  43.         private System.ComponentModel.Container components = null;
  44.         public RegistersForm(PLX_Device dev, ACTION_TYPE regType)
  45.         {
  46.             InitializeComponent();
  47.             m_device = dev;
  48.             m_regType = regType;
  49.             switch(regType)
  50.             {
  51.             case ACTION_TYPE.CFG:
  52.                 {
  53.                     this.Text = "Read/Write Configuration Space by Registers";
  54.                     lblRegs.Visible = true;
  55.                     lblRegs.Text = "Choose a Cfg Register";
  56.                     cmboRegs.Visible = true;
  57.                     WDC_REG[] regs = PLX_Regs.gPLX_CfgRegs;
  58.                     for (int i=0; i<regs.GetLength(0); ++i)
  59.                     {
  60.                         cmboRegs.Items.AddRange(new object[] {regs[i].sName +
  61.                             " size: " + regs[i].dwSize.ToString("X") +
  62.                                                         " - " + regs[i].sDesc});
  63.                     }
  64.                     break;
  65.                 }
  66.             case ACTION_TYPE.RT:
  67.                 {
  68.                     this.Text = "Read/Write RunTime Registers";
  69.                     lblRegs.Visible = true;
  70.                     lblRegs.Text = "Choose a RunTime Register";
  71.                     cmboRegs.Visible = true;
  72.                     WDC_REG[] regs = m_device.IsMaster? PLX_Regs.gPLX_M_Regs :
  73.                         PLX_Regs.gPLX_T_Regs;
  74.                     for (int i=0; i<regs.GetLength(0); ++i)
  75.                     {
  76.                         cmboRegs.Items.AddRange(new object[]{regs[i].sName +
  77.                             " size: " + regs[i].dwSize.ToString("X") +
  78.                                                         " - " + regs[i].sDesc});
  79.                     }
  80.                     break;
  81.                 }
  82.             case ACTION_TYPE.EEPROM:
  83.                 {
  84.                     this.Text = "Read/Write EEPROM";
  85.                     lblRegs.Visible = false;
  86.                     cmboRegs.Visible = false;
  87.                     txtOffset = new System.Windows.Forms.TextBox();
  88.                     txtOffset.Visible = true;
  89.                     txtOffset.Location = new
  90.                         System.Drawing.Point(txtInput.Left, cmboRegs.Top);
  91.                     txtOffset.Name = "txtOffset";
  92.                     txtOffset.Size = new System.Drawing.Size(txtInput.Width,
  93.                         txtInput.Height);
  94.                     txtOffset.TabIndex = 0;
  95.                     txtOffset.Text = "";
  96.                     txtOffset.Parent = this;
  97.                     lblOffset = new Label();
  98.                     string sOffMult = m_device.EEprom.IsVPD()? 
  99.                         wdc_lib_consts.WDC_SIZE_32.ToString():
  100.                         wdc_lib_consts.WDC_SIZE_16.ToString();
  101.                     lblOffset.Text = "Enter offset (must be a multiple of " + 
  102.                         sOffMult + "):";
  103.                     lblOffset.Visible = true;
  104.                     lblOffset.Location = new
  105.                         System.Drawing.Point(lblInput.Left, cmboRegs.Top);
  106.                     lblOffset.Name = "lblOffset";
  107.                     lblOffset.Size = new 
  108.                         System.Drawing.Size(lblInput.Width-5, lblInput.Height);
  109.                     lblOffset.TabIndex = 19;
  110.                     lblOffset.Parent = this;
  111.                     label3 = new Label();
  112.                     label3.Location = new
  113.                         System.Drawing.Point(label2.Left, cmboRegs.Top);
  114.                     label3.Name = "label3";
  115.                     label3.Size = new System.Drawing.Size(16, 23);
  116.                     label3.TabIndex = 20;
  117.                     label3.Text = "0x";
  118.                     label3.Parent = this;
  119.                     break;
  120.                 }                                        
  121.             }
  122.         }
  123.         protected override void Dispose( bool disposing )
  124.         {
  125.             if( disposing )
  126.             {
  127.                 if(components != null)
  128.                 {
  129.                     components.Dispose();
  130.                 }
  131.             }
  132.             base.Dispose( disposing );
  133.         }
  134. #region Windows Form Designer generated code
  135.         private void InitializeComponent()
  136.         {
  137.             this.cmboRegs = new System.Windows.Forms.ComboBox();
  138.             this.txtData = new System.Windows.Forms.TextBox();
  139.             this.lblRegs = new System.Windows.Forms.Label();
  140.             this.btReadAll = new System.Windows.Forms.Button();
  141.             this.btExit = new System.Windows.Forms.Button();
  142.             this.btRead = new System.Windows.Forms.Button();
  143.             this.txtInput = new System.Windows.Forms.TextBox();
  144.             this.btWrite = new System.Windows.Forms.Button();
  145.             this.btLog = new System.Windows.Forms.Button();
  146.             this.lblInput = new System.Windows.Forms.Label();
  147.             this.label2 = new System.Windows.Forms.Label();
  148.             this.SuspendLayout();
  149.             // cmboRegs
  150.             this.cmboRegs.Location = new System.Drawing.Point(40, 32);
  151.             this.cmboRegs.Name = "cmboRegs";
  152.             this.cmboRegs.Size = new System.Drawing.Size(360, 21);
  153.             this.cmboRegs.TabIndex = 0;
  154.             this.cmboRegs.Text = "---- ----";
  155.             // txtData
  156.             this.txtData.AutoSize = false;
  157.             this.txtData.Location = new System.Drawing.Point(40, 112);
  158.             this.txtData.Multiline = true;
  159.             this.txtData.Name = "txtData";
  160.             this.txtData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
  161.             this.txtData.Size = new System.Drawing.Size(360, 136);
  162.             this.txtData.TabIndex = 8;
  163.             this.txtData.Text = "";
  164.             // lblRegs
  165.             this.lblRegs.Location = new System.Drawing.Point(48, 8);
  166.             this.lblRegs.Name = "lblRegs";
  167.             this.lblRegs.Size = new System.Drawing.Size(256, 23);
  168.             this.lblRegs.TabIndex = 9;
  169.             this.lblRegs.Text = "Choose a RunTime Register";
  170.             // btReadAll
  171.             this.btReadAll.Location = new System.Drawing.Point(176, 264);
  172.             this.btReadAll.Name = "btReadAll";
  173.             this.btReadAll.TabIndex = 3;
  174.             this.btReadAll.Text = "Read All ";
  175.             this.btReadAll.Click += 
  176.                 new System.EventHandler(this.btReadAll_Click);
  177.             // btExit
  178.             this.btExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  179.             this.btExit.Location = new System.Drawing.Point(432, 192);
  180.             this.btExit.Name = "btExit";
  181.             this.btExit.TabIndex = 6;
  182.             this.btExit.Text = "Exit";
  183.             // btRead
  184.             this.btRead.Location = new System.Drawing.Point(48, 264);
  185.             this.btRead.Name = "btRead";
  186.             this.btRead.TabIndex = 2;
  187.             this.btRead.Text = "Read";
  188.             this.btRead.Click += new System.EventHandler(this.btRead_Click);
  189.             // txtInput
  190.             this.txtInput.Location = new System.Drawing.Point(256, 72);
  191.             this.txtInput.Name = "txtInput";
  192.             this.txtInput.Size = new System.Drawing.Size(144, 20);
  193.             this.txtInput.TabIndex = 1;
  194.             this.txtInput.Text = "";
  195.             // btWrite
  196.             this.btWrite.Location = new System.Drawing.Point(304, 264);
  197.             this.btWrite.Name = "btWrite";
  198.             this.btWrite.TabIndex = 4;
  199.             this.btWrite.Text = "Write";
  200.             this.btWrite.Click += new System.EventHandler(this.btWrite_Click);
  201.             // btLog
  202.             this.btLog.Location = new System.Drawing.Point(432, 136);
  203.             this.btLog.Name = "btLog";
  204.             this.btLog.TabIndex = 5;
  205.             this.btLog.Text = "Clear Log";
  206.             this.btLog.Click += new System.EventHandler(this.btLog_Click);
  207.             // lblInput
  208.             this.lblInput.Location = new System.Drawing.Point(40, 72);
  209.             this.lblInput.Name = "lblInput";
  210.             this.lblInput.Size = new System.Drawing.Size(208, 23);
  211.             this.lblInput.TabIndex = 17;
  212.             this.lblInput.Text = "Enter Input in hex (Write Transactions):";
  213.             // label2
  214.             this.label2.Location = new System.Drawing.Point(240, 72);
  215.             this.label2.Name = "label2";
  216.             this.label2.Size = new System.Drawing.Size(16, 23);
  217.             this.label2.TabIndex = 18;
  218.             this.label2.Text = "0x";
  219.             // RegistersForm
  220.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  221.             this.CancelButton = this.btExit;
  222.             this.ClientSize = new System.Drawing.Size(520, 301);
  223.             this.Controls.Add(this.label2);
  224.             this.Controls.Add(this.lblInput);
  225.             this.Controls.Add(this.btLog);
  226.             this.Controls.Add(this.btWrite);
  227.             this.Controls.Add(this.txtInput);
  228.             this.Controls.Add(this.btRead);
  229.             this.Controls.Add(this.btExit);
  230.             this.Controls.Add(this.btReadAll);
  231.             this.Controls.Add(this.lblRegs);
  232.             this.Controls.Add(this.txtData);
  233.             this.Controls.Add(this.cmboRegs);
  234.             this.Name = "RegistersForm";
  235.             this.StartPosition = 
  236.                 System.Windows.Forms.FormStartPosition.CenterScreen;
  237.             this.Text = "Registers";
  238.             this.ResumeLayout(false);
  239.         }
  240. #endregion
  241.         public bool GetInput()
  242.         {
  243.             DialogResult result = DialogResult.Retry;
  244.             while((result = ShowDialog()) == DialogResult.Retry);
  245.             return true;
  246.         }
  247.         private void btClick(object sender, System.EventArgs e)
  248.         {
  249.             DialogResult = DialogResult.None;
  250.             try
  251.             {
  252.                 if(m_regType == ACTION_TYPE.EEPROM)
  253.                     TranslateInput_EEPROM();
  254.                 else
  255.                     TranslateInput_Regs();                                
  256.             }
  257.             catch
  258.             {
  259.                 MessageBox.Show(m_excp.Message,"Input Entry Error",
  260.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  261.                 DialogResult = DialogResult.Retry;
  262.                 return;
  263.             }         
  264.             if(m_regType == ACTION_TYPE.EEPROM)
  265.                 ReadWriteEEPROM();
  266.             else
  267.             {
  268.                 if(m_direction == RW.READ_ALL)
  269.                     ReadAllRegs();
  270.                 else
  271.                     ReadWriteReg();
  272.             }
  273.         }
  274.         private void btRead_Click(object sender, System.EventArgs e)
  275.         {
  276.             m_direction = RW.READ;
  277.             btClick(sender, e);
  278.         }
  279.         private void btWrite_Click(object sender, System.EventArgs e)
  280.         {
  281.             m_direction = RW.WRITE;
  282.             btClick(sender, e);                        
  283.         }
  284.         private void btReadAll_Click(object sender, System.EventArgs e)
  285.         {
  286.             m_direction = RW.READ_ALL;
  287.             btClick(sender, e);
  288.         }
  289.         private void TranslateInput_Regs()
  290.         {
  291.             if(m_direction != RW.READ_ALL)
  292.             {
  293.                 m_regs = new WDC_REG[1];
  294.                 m_excp = new Exception("Select a Register");
  295.                 int iSelectedReg = cmboRegs.SelectedIndex;
  296.                 if ((uint)iSelectedReg == 0xffffffff)
  297.                     throw m_excp;
  298.                 m_regs[0] = (m_regType == ACTION_TYPE.CFG)? 
  299.                     PLX_Regs.gPLX_CfgRegs[iSelectedReg]:
  300.                     (m_device.IsMaster)?
  301.                     PLX_Regs.gPLX_M_Regs[iSelectedReg]:
  302.                     PLX_Regs.gPLX_T_Regs[iSelectedReg];
  303.                 if(m_direction == RW.WRITE)
  304.                 {
  305.                     m_excp = new Exception("Enter the data to write. "
  306.                         + "Entered value should be a hex number");
  307.                     if(txtInput.Text == "")
  308.                         throw m_excp;
  309.                     DWORD dwSize = m_regs[0].dwSize;
  310.                     switch(dwSize)
  311.                     {
  312.                     case wdc_lib_consts.WDC_SIZE_8:
  313.                         {
  314.                             m_bData = Convert.ToByte(txtInput.Text, 16);
  315.                             break;
  316.                         }
  317.                     case wdc_lib_consts.WDC_SIZE_16:
  318.                         {
  319.                             m_wData = Convert.ToUInt16(txtInput.Text, 16);
  320.                             break;
  321.                         }
  322.                     case wdc_lib_consts.WDC_SIZE_32:
  323.                         {
  324.                             m_u32Data = Convert.ToUInt32(txtInput.Text, 16);
  325.                             break;
  326.                         }
  327.                     case wdc_lib_consts.WDC_SIZE_64:
  328.                         {
  329.                             m_u64Data = Convert.ToUInt64(txtInput.Text, 16);
  330.                             break;
  331.                         }
  332.                     }
  333.                 }
  334.             }
  335.             else
  336.             {                                
  337.                 m_regs = (m_regType == ACTION_TYPE.CFG)? 
  338.                     PLX_Regs.gPLX_CfgRegs: (m_device.IsMaster)?
  339.                     PLX_Regs.gPLX_M_Regs: PLX_Regs.gPLX_T_Regs;
  340.             }                        
  341.         }
  342.         private BOOL IsLegalDirection(int iRegIndex)
  343.         {
  344.             WDC_REG reg = m_regs[iRegIndex];
  345.             if (((RW.READ == m_direction) && 
  346.                 (WDC_DIRECTION.WDC_WRITE == reg.direction)) ||
  347.                 ((RW.WRITE == m_direction) && 
  348.                 (WDC_DIRECTION.WDC_READ == reg.direction)))
  349.                 return false;
  350.             return true;
  351.         }
  352.         private void ReadWriteReg()
  353.         {
  354.             WDC_REG reg = m_regs[0];
  355.             DWORD dwStatus = (DWORD)wdc_err.WD_STATUS_SUCCESS;
  356.             BOOL bIsRead = (m_direction == RW.READ);
  357.             if(!IsLegalDirection(0))
  358.             {
  359.                 txtData.Text += "you have chosen to " + (bIsRead? 
  360.                     "read from" : "write to") + " a register which is " +
  361.                     (bIsRead? "write-only" : "read-only") + Environment.NewLine;
  362.                 return;
  363.             }
  364.             switch (reg.dwSize)
  365.             {
  366.             case wdc_lib_consts.WDC_SIZE_8:
  367.                 {
  368.                     if (RW.READ == m_direction)
  369.                         dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  370.                             wdc_lib_decl.WDC_PciReadCfg8(m_device.Handle,
  371.                                 reg.dwOffset, ref m_bData) :
  372.                             wdc_lib_decl.WDC_ReadAddr8(m_device.Handle,
  373.                                 reg.dwAddrSpace, reg.dwOffset, ref m_bData);
  374.                     else
  375.                     {
  376.                         dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  377.                             wdc_lib_decl.WDC_PciWriteCfg8(m_device.Handle,
  378.                                 reg.dwOffset, m_bData) :
  379.                             wdc_lib_decl.WDC_WriteAddr8(m_device.Handle,
  380.                                 reg.dwAddrSpace, reg.dwOffset, m_bData);
  381.                     }
  382.                     break;
  383.                 }
  384.             case wdc_lib_consts.WDC_SIZE_16:
  385.                 {
  386.                     if (RW.READ == m_direction)
  387.                         dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  388.                             wdc_lib_decl.WDC_PciReadCfg16(m_device.Handle,
  389.                                 reg.dwOffset, ref m_wData) :
  390.                             wdc_lib_decl.WDC_ReadAddr16(m_device.Handle,
  391.                                 reg.dwAddrSpace, reg.dwOffset, ref m_wData);
  392.                     else
  393.                     {
  394.                         dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  395.                             wdc_lib_decl.WDC_PciWriteCfg16(m_device.Handle,
  396.                                 reg.dwOffset, m_wData) :
  397.                             wdc_lib_decl.WDC_WriteAddr16(m_device.Handle,
  398.                                 reg.dwAddrSpace, reg.dwOffset, m_wData);
  399.                     }
  400.                     break;
  401.                 }
  402.             case wdc_lib_consts.WDC_SIZE_32:
  403.                 {
  404.                     if (RW.READ == m_direction)
  405.                         dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  406.                             wdc_lib_decl.WDC_PciReadCfg32(m_device.Handle,
  407.                                 reg.dwOffset, ref m_u32Data) :
  408.                             wdc_lib_decl.WDC_ReadAddr32(m_device.Handle,
  409.                                 reg.dwAddrSpace, reg.dwOffset, ref m_u32Data);
  410.                     else
  411.                     {
  412.                         dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  413.                             wdc_lib_decl.WDC_PciWriteCfg32(m_device.Handle,
  414.                                 reg.dwOffset, m_u32Data) :
  415.                             wdc_lib_decl.WDC_WriteAddr32(m_device.Handle,
  416.                                 reg.dwAddrSpace, reg.dwOffset, m_u32Data);
  417.                     }
  418.                     break;
  419.                 }
  420.             case wdc_lib_consts.WDC_SIZE_64:
  421.                 {
  422.                     if (RW.READ == m_direction)
  423.                         dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  424.                             wdc_lib_decl.WDC_PciReadCfg64(m_device.Handle,
  425.                                 reg.dwOffset, ref m_u64Data) :
  426.                             wdc_lib_decl.WDC_ReadAddr64(m_device.Handle,
  427.                                 reg.dwAddrSpace, reg.dwOffset, ref m_u64Data);
  428.                     else
  429.                     {
  430.                         dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  431.                             wdc_lib_decl.WDC_PciWriteCfg64(m_device.Handle,
  432.                                 reg.dwOffset, m_u64Data) :
  433.                             wdc_lib_decl.WDC_WriteAddr64(m_device.Handle,
  434.                                 reg.dwAddrSpace, reg.dwOffset, m_u64Data);
  435.                     }
  436.                     break;
  437.                 }
  438.             }        
  439.             TraceLog((((DWORD)wdc_err.WD_STATUS_SUCCESS == dwStatus)?
  440.                 (bIsRead? "read " : "wrote ") + "0x" +
  441.                 ((reg.dwSize == wdc_lib_consts.WDC_SIZE_8)? 
  442.                 m_bData.ToString("X2"): 
  443.                 ((reg.dwSize == wdc_lib_consts.WDC_SIZE_16)?
  444.                 m_wData.ToString("X4") :
  445.                 ((reg.dwSize == wdc_lib_consts.WDC_SIZE_32)?
  446.                 m_u32Data.ToString("X8") : m_u64Data.ToString("X16")))) +
  447.                 (bIsRead? " from " : " to ") + "register " + reg.sName :
  448.                 "failed to complete the transaction on register" + reg.sName),
  449.                     (wdc_err)dwStatus); 
  450.         }
  451.         private void ReadAllRegs()
  452.         {
  453.             WDC_REG reg = m_regs[0];
  454.             DWORD dwStatus = (DWORD)wdc_err.WD_STATUS_SUCCESS;
  455.             TraceLog("displaying all registers (" + m_device.ToString(false) +
  456.                 ")", (wdc_err)dwStatus);
  457.             for(int i = 0; i < m_regs.GetLength(0); ++i)
  458.             {
  459.                 reg = m_regs[i];
  460.                 if(!IsLegalDirection(i))
  461.                     txtData.Text = "register " + reg.sName + "is write-only" + 
  462.                         Environment.NewLine;
  463.                 else
  464.                 {
  465.                     switch (reg.dwSize)
  466.                     {
  467.                     case wdc_lib_consts.WDC_SIZE_8:
  468.                         {
  469.                             dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  470.                                 wdc_lib_decl.WDC_PciReadCfg8(m_device.Handle,
  471.                                     reg.dwOffset, ref m_bData) :
  472.                                 wdc_lib_decl.WDC_ReadAddr8(m_device.Handle,
  473.                                     reg.dwAddrSpace, reg.dwOffset, ref m_bData);
  474.                             break;
  475.                         }
  476.                     case wdc_lib_consts.WDC_SIZE_16:
  477.                         {
  478.                             dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  479.                                 wdc_lib_decl.WDC_PciReadCfg16(m_device.Handle,
  480.                                     reg.dwOffset, ref m_wData) :
  481.                                 wdc_lib_decl.WDC_ReadAddr16(m_device.Handle,
  482.                                     reg.dwAddrSpace, reg.dwOffset, ref m_wData);
  483.                             break;
  484.                         }
  485.                     case wdc_lib_consts.WDC_SIZE_32:
  486.                         {
  487.                             dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  488.                                 wdc_lib_decl.WDC_PciReadCfg32(m_device.Handle,
  489.                                     reg.dwOffset, ref m_u32Data) :
  490.                                 wdc_lib_decl.WDC_ReadAddr32(m_device.Handle,
  491.                                     reg.dwAddrSpace, reg.dwOffset, 
  492.                                     ref m_u32Data);
  493.                             break;
  494.                         }
  495.                     case wdc_lib_consts.WDC_SIZE_64:
  496.                         {
  497.                             dwStatus = (m_regType == ACTION_TYPE.CFG) ? 
  498.                                 wdc_lib_decl.WDC_PciReadCfg64(m_device.Handle,
  499.                                     reg.dwOffset, ref m_u64Data) :
  500.                                 wdc_lib_decl.WDC_ReadAddr64(m_device.Handle,
  501.                                     reg.dwAddrSpace, reg.dwOffset, ref m_u64Data);
  502.                             break;
  503.                         }
  504.                     }        
  505.                     TraceLog((((DWORD)wdc_err.WD_STATUS_SUCCESS == dwStatus)?
  506.                         "read from register " + reg.sName + " 0x" +
  507.                         ((reg.dwSize == wdc_lib_consts.WDC_SIZE_8)?
  508.                         m_bData.ToString("X2"):  
  509.                         ((reg.dwSize == wdc_lib_consts.WDC_SIZE_16)?
  510.                         m_wData.ToString("X4") :
  511.                         ((reg.dwSize == wdc_lib_consts.WDC_SIZE_32)?
  512.                         m_u32Data.ToString("X8") : m_u64Data.ToString("X16")))) :
  513.                         "failed to complete the transaction on register " + 
  514.                         reg.sName), (wdc_err)dwStatus);
  515.                 }                        
  516.             }
  517.         }
  518.         private void TranslateInput_EEPROM()
  519.         {
  520.             bool bIsVPD = m_device.EEprom.IsVPD();
  521.             if(m_direction != RW.READ_ALL)
  522.             {
  523.                 DWORD dwMult = (bIsVPD ? wdc_lib_consts.WDC_SIZE_32 : 
  524.                     wdc_lib_consts.WDC_SIZE_16);
  525.                 m_excp = new Exception("the offset entered is not a multiple " +
  526.                     "of " + dwMult.ToString());
  527.                 m_dwOffset = (DWORD)Convert.ToByte(txtOffset.Text, 16);
  528.                 if((m_dwOffset % dwMult) != 0)
  529.                     throw m_excp;
  530.                 if(m_direction == RW.WRITE)
  531.                 {
  532.                     m_excp = new Exception("Enter the data to write. "
  533.                         + "Entered value should be a hex number");
  534.                 
  535.                     if(txtInput.Text == "")
  536.                         throw m_excp;
  537.                     if(bIsVPD)
  538.                         m_u32Data = Convert.ToUInt32(txtInput.Text, 16);
  539.                     else
  540.                         m_wData = Convert.ToUInt16(txtInput.Text, 16);
  541.                 }                                
  542.             }
  543.         }
  544.         private void ReadWriteEEPROM()
  545.         {
  546.             bool bIsVPD = m_device.EEprom.IsVPD();
  547.             DWORD dwStatus = (DWORD)wdc_err.WD_STATUS_SUCCESS;
  548.             string str;
  549.             switch(m_direction)
  550.             {
  551.             case RW.READ:
  552.                 {
  553.                     dwStatus = bIsVPD? m_device.EEprom.EEPROM_Read(m_dwOffset,
  554.                         ref m_u32Data):
  555.                         ((EEPROM_RT)m_device.EEprom).EEPROM_Read16(m_dwOffset,
  556.                             ref m_wData);
  557.                     if(dwStatus == (DWORD)wdc_err.WD_STATUS_SUCCESS)
  558.                     {
  559.                         str = "read 0x" + (bIsVPD? m_u32Data.ToString("X") :
  560.                             m_wData.ToString("X")) + " from offset " 
  561.                             + m_dwOffset.ToString("X");
  562.                     }
  563.                     else
  564.                         str = "Failed reading from offset " +
  565.                             m_dwOffset.ToString("X");
  566.                     TraceLog(str, (wdc_err)dwStatus);                        
  567.                     break;
  568.                 }
  569.             case RW.WRITE:
  570.                 {
  571.                     dwStatus = bIsVPD? m_device.EEprom.EEPROM_Write(m_dwOffset,
  572.                         m_u32Data):
  573.                         ((EEPROM_RT)m_device.EEprom).EEPROM_Write16(m_dwOffset,
  574.                             m_wData);
  575.                     if(dwStatus == (DWORD)wdc_err.WD_STATUS_SUCCESS)
  576.                     {
  577.                         str = "Wrote 0x" + (bIsVPD? m_u32Data.ToString("X") :
  578.                             m_wData.ToString("X")) + " to offset " + 
  579.                             m_dwOffset.ToString("X");
  580.                     }
  581.                     else
  582.                         str = "Failed writing to offset " + 
  583.                             m_dwOffset.ToString("X");
  584.                     
  585.                     TraceLog(str, (wdc_err)dwStatus);
  586.                     break;
  587.                 }
  588.             case RW.READ_ALL:
  589.                 {
  590.                     string sDisplay = "displaying EEPROM (" +
  591.                         m_device.ToString(false) + ")" ;
  592.                     for (DWORD dwOffset = 0; dwOffset < 0xff; dwOffset += 4)
  593.                     {
  594.                         if((dwOffset % 0x10) == 0)
  595.                         {
  596.                             TraceLog(sDisplay, (wdc_err)dwStatus);
  597.                             sDisplay = dwOffset.ToString("X2") + ": " ;
  598.                         }
  599.                         dwStatus = m_device.EEprom.EEPROM_Read(dwOffset,
  600.                             ref m_u32Data);
  601.                         if((DWORD)wdc_err.WD_STATUS_SUCCESS != dwStatus)
  602.                         {
  603.                             sDisplay += Environment.NewLine + "Error " +
  604.                                 "occurred while reading offset 0x" + 
  605.                                 dwOffset.ToString("X") + "of the serial " +
  606.                                 "EEPROM. Error 0x" + dwStatus.ToString("X") +
  607.                                 ": " + utils.Stat2Str((DWORD)dwStatus);
  608.                             break; 
  609.                         }
  610.                         sDisplay += m_u32Data.ToString("X8") + "  ";
  611.                     }
  612.                     TraceLog(sDisplay, (wdc_err)dwStatus);
  613.                     break;
  614.                 }
  615.             }
  616.         }
  617.         private void btLog_Click(object sender, System.EventArgs e)
  618.         {
  619.             txtData.Clear();
  620.         }
  621.         private void TraceLog(string str, wdc_err status)
  622.         {
  623.             txtData.Text += str + Environment.NewLine;
  624.             string sForm = (m_regType == ACTION_TYPE.EEPROM)? 
  625.                 "EEPROM R/W Form: " : (m_regType == ACTION_TYPE.CFG)? 
  626.                 "CFG Registers R/W Form: " : "RT Registers R/W Form: ";
  627.             if(m_direction == RW.READ_ALL)
  628.             {
  629.                 Log.TraceLog(sForm + str);
  630.                 return;
  631.             }
  632.             
  633.             string sMsg = sForm + str + " (" + m_device.ToString(false) + ")";
  634.             if(status == wdc_err.WD_STATUS_SUCCESS)
  635.                 Log.TraceLog(sMsg);
  636.             else
  637.                 Log.ErrLog(sMsg);            
  638.         }
  639.     }
  640. }