using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.IO;
using Renci.SshNet;

// Based on, see below
// https://www.codeproject.com/Articles/678025/Serial-Comms-in-Csharp-for-Beginners


namespace uartTestGUI
{
    public partial class Form1 : Form
    {

        SshClient ssh_client;

        public Form1()
        {
            InitializeComponent();
//            ssh_client = new SshClient(ipAddress.Text, "root", "");

        }

        public void backgroundTask()
        {
            // while (true == status_update_en.Checked)
            while (true)
            {

                ssh_connect.Invoke(new delegateRegDump(RegDump),
                     new object[] { });


                Thread.Sleep(1000);
            }

        }

        public delegate void delegateRegDump();


        string ssh_ReadReg(string addr)
        {
            SshCommand cmd;
            string str = String.Empty;
            string str1 = String.Empty;
            if (true == ssh_client.IsConnected)
            {
                str1 = "devmem " + addr;
                Console.WriteLine("ssh_ReadReg(): Command: "+str1);
 //               Console.WriteLine(str1); 


                cmd = ssh_client.RunCommand(str1);
                str = cmd.Result;
                Console.WriteLine("ssh_ReadReg(): Response:"+str);
//                Console.WriteLine(str);
                return str.Substring(2, 8);
            }
            return "FFFFFFFF";
        }

        void ssh_WriteReg(string addr, string val)
        {
            SshCommand cmd;
            string str = String.Empty;

            if (true == ssh_client.IsConnected)
            {
                str = "devmem " + addr + " 32 " + val;
                Console.WriteLine("ssh_WriteReg(): Command:"+str);
       //         Console.WriteLine(str); // Get the command output

                cmd = ssh_client.RunCommand(str);// Execute a command

                Console.WriteLine("ssh_WriteReg(): Response:"+ cmd.Result);
 //               Console.WriteLine(cmd.Result); // Get the command output
            }
        }


   


        private void RegDump()
        {
            if (true == ssh_client.IsConnected)
            {
                string str;

                reg_0x00.Text = "";
                reg_0x04.Text = "";
                reg_0x14.Text = "";
                reg_0x20.Text = "";
                reg_0x28.Text = "";
                reg_0x2C.Text = "";
                reg_0x30.Text = "";
                reg_0x34.Text = "";
                reg_0x38.Text = "";
                reg_0x3C.Text = "";
                reg_0x40.Text = "";
                reg_0xD4.Text = "";
                reg_0xD8.Text = "";
                reg_0xDC.Text = "";
                reg_0xE0.Text = "";
                reg_0xE4.Text = "";
                reg_0xE8.Text = "";
                reg_0xEC.Text = "";
                reg_0xF0.Text = "";

                reg_0x00.Text = ssh_ReadReg("0x80000000");
                //
                str = ssh_ReadReg("0x80000004");
                uint val = (Convert.ToUInt32(str, 16));
                reg_0x04.Text = str;
                if (0x2 == (val & 0x02))
                {
                    qsfp1_present.BackColor = Color.Red;
                }
                else
                {
                    qsfp1_present.BackColor = Color.LightGreen;
                }

                if (0x20 == (val & 0x020))
                {
                    qsfp2_present.BackColor = Color.Red;
                }
                else
                {
                    qsfp2_present.BackColor = Color.LightGreen;
                }

                if (0x200 == (val & 0x0200))
                {
                    qsfp3_present.BackColor = Color.Red;
                }
                else
                {
                    qsfp3_present.BackColor = Color.LightGreen;
                }

                if (0x2000 == (val & 0x02000))
                {
                    qsfp4_present.BackColor = Color.Red;
                }
                else
                {
                    qsfp4_present.BackColor = Color.LightGreen;
                }

                reg_0x14.Text = ssh_ReadReg("0x80000014");
                reg_0x20.Text = ssh_ReadReg("0x80000020");
                //
                reg_0x28.Text = ssh_ReadReg("0x80000028");
                val = (Convert.ToUInt32(reg_0x28.Text, 16));
                if (0 == (val & 0x300))
                {
                    dac_data_src_quadsend_rcv.Checked = true;
                }
                else if (0x100 == (val & 0x300))
                {
                    dac_data_src_cmac.Checked = true;
                }

                if (1 == (val & 3))
                {
                    cmac_tx_en.Checked = true;
                }
                else if (2 == (val & 3))
                {
                    cmac_rx_en.Checked = true;
                }
                else if(3 == (val & 3))
                {
                    cmac_rxtx_en.Checked = true;
                }
                else
                {
                    cmac_disable.Checked = true;
                }


                str = ssh_ReadReg("0x80000020");  // dummy register to flag that cmac has been initialized
                val = (Convert.ToUInt32(str, 16));
                if (0xCAFE == val)
                {
                    // CMAC already init'ed
                    cmac_init.Text = "CMACs Initialized";
                    cmac_init.BackColor = Color.LightGreen;
                    cmac_read_all_regs.PerformClick();
                }
                else
                {
                    cmac_init.Text = "CMAC_Init";
                    cmac_init.BackColor = Color.Gainsboro;
                }


                str = ssh_ReadReg("0x8000002C");
                val = (Convert.ToUInt32(str, 16));
                reg_0x2C.Text = val.ToString("D");

                reg_0x30.Text = ssh_ReadReg("0x80000030");
//               
                str = ssh_ReadReg("0x80000034");
                val = (Convert.ToUInt32(str, 16));
                reg_0x34.Text = val.ToString("D");



                reg_0x38.Text = ssh_ReadReg("0x80000038");
//
                str = ssh_ReadReg("0x8000003C");
                val = (Convert.ToUInt32(str, 16));
                reg_0x3C.Text = val.ToString("D");
//
                str = ssh_ReadReg("0x80000040");
                val = (Convert.ToUInt32(str, 16));
                reg_0x40.Text = val.ToString("D");


                reg_0xD4.Text = ssh_ReadReg("0x800000D4");
                reg_0xD8.Text = ssh_ReadReg("0x800000D8");
                reg_0xDC.Text = ssh_ReadReg("0x800000DC");
                reg_0xE0.Text = ssh_ReadReg("0x800000E0");
                reg_0xE4.Text = ssh_ReadReg("0x800000E4");
                reg_0xE8.Text = ssh_ReadReg("0x800000E8");
                reg_0xEC.Text = ssh_ReadReg("0x800000EC");
                reg_0xF0.Text = ssh_ReadReg("0x800000F0");


            }
        }




        private void cmac_0_RegDump()
        {
            if (true == ssh_client.IsConnected)
            {

                cmac_0_reg_0x10000.Text = "";
                cmac_0_reg_0x10004.Text = "";
                cmac_0_reg_0x1000C.Text = "";
                cmac_0_reg_0x10014.Text = "";
                cmac_0_reg_0x10024.Text = "";
                cmac_0_reg_0x10090.Text = "";
                cmac_4_reg_0x10030.Text = "";
                cmac_4_reg_0x10058.Text = "";

                cmac_0_reg_0x00.Text = "";
                cmac_0_reg_0x04.Text = "";
                cmac_0_reg_0x08.Text = "";
                cmac_0_reg_0x0C.Text = "";
                cmac_0_reg_0x10.Text = "";
                cmac_0_reg_0x14.Text = "";
                cmac_0_reg_0x18.Text = "";
                cmac_0_reg_0x1C.Text = "";

                cmac_0_reg_0x10000.Text = ssh_ReadReg("0x81010000");
                cmac_0_reg_0x10004.Text = ssh_ReadReg("0x81010004");
                cmac_0_reg_0x1000C.Text = ssh_ReadReg("0x8101000C");
                cmac_0_reg_0x10014.Text = ssh_ReadReg("0x81010014");
                cmac_0_reg_0x10024.Text = ssh_ReadReg("0x81010024");
                cmac_0_reg_0x10090.Text = ssh_ReadReg("0x81010090");
                cmac_0_reg_0x10030.Text = ssh_ReadReg("0x81010030");
                cmac_0_reg_0x10058.Text = ssh_ReadReg("0x81010058");

                cmac_0_reg_0x00.Text = ssh_ReadReg("0x81000000");
                cmac_0_reg_0x04.Text = ssh_ReadReg("0x81000004");
                cmac_0_reg_0x08.Text = ssh_ReadReg("0x81000008");
                cmac_0_reg_0x0C.Text = ssh_ReadReg("0x8100000C");
                cmac_0_reg_0x10.Text = ssh_ReadReg("0x81000010");
                cmac_0_reg_0x14.Text = ssh_ReadReg("0x81000014");
                cmac_0_reg_0x18.Text = ssh_ReadReg("0x81000018");
                cmac_0_reg_0x1C.Text = ssh_ReadReg("0x8100001C");

            }
        }

        private void cmac_4_RegDump()
        {
            if (true == ssh_client.IsConnected)
            {

                cmac_4_reg_0x10000.Text = "";
                cmac_4_reg_0x10004.Text = "";
                cmac_4_reg_0x1000C.Text = "";
                cmac_4_reg_0x10014.Text = "";
                cmac_4_reg_0x10024.Text = "";
                cmac_4_reg_0x10090.Text = "";
                cmac_4_reg_0x10030.Text = "";
                cmac_4_reg_0x10058.Text = "";

                cmac_4_reg_0x00.Text = "";
                cmac_4_reg_0x04.Text = "";
                cmac_4_reg_0x08.Text = "";
                cmac_4_reg_0x0C.Text = "";
                cmac_4_reg_0x10.Text = "";
                cmac_4_reg_0x14.Text = "";
                cmac_4_reg_0x18.Text = "";
                cmac_4_reg_0x1C.Text = "";

                cmac_4_reg_0x10000.Text = ssh_ReadReg("0x82010000");
                cmac_4_reg_0x10004.Text = ssh_ReadReg("0x82010004");
                cmac_4_reg_0x1000C.Text = ssh_ReadReg("0x8201000C");
                cmac_4_reg_0x10014.Text = ssh_ReadReg("0x82010014");
                cmac_4_reg_0x10024.Text = ssh_ReadReg("0x82010024");
                cmac_4_reg_0x10090.Text = ssh_ReadReg("0x82010090");
                cmac_4_reg_0x10030.Text = ssh_ReadReg("0x81010030");
                cmac_4_reg_0x10058.Text = ssh_ReadReg("0x81010058");

                cmac_4_reg_0x00.Text = ssh_ReadReg("0x82000000");
                cmac_4_reg_0x04.Text = ssh_ReadReg("0x82000004");
                cmac_4_reg_0x08.Text = ssh_ReadReg("0x82000008");
                cmac_4_reg_0x0C.Text = ssh_ReadReg("0x8200000C");
                cmac_4_reg_0x10.Text = ssh_ReadReg("0x82000010");
                cmac_4_reg_0x14.Text = ssh_ReadReg("0x82000014");
                cmac_4_reg_0x18.Text = ssh_ReadReg("0x82000018");
                cmac_4_reg_0x1C.Text = ssh_ReadReg("0x8200001C");

            }
        }

        private void Open_ssh_connection_Click(object sender, EventArgs e)
        {
//            if (false == ssh_client.IsConnected)
//            {
                try
                {
                    ssh_client = new SshClient(ipAddress.Text, "root", "");
                    ssh_connect.Text = "Connecting...";
                    ssh_client.Connect();
                    Console.WriteLine($"Connected to {ssh_client.ConnectionInfo.Host} successfully.");

                    ssh_connect.BackColor = Color.LightGreen;
                    //                  SshCommand cmd = ssh_client.RunCommand("devmem 0x80000000"); // Execute a command
                    //                  Console.WriteLine("Command Output:");
                    //                   Console.WriteLine(cmd.Result); // Get the command output
                    //                  reg_0x00.Text = cmd.Result;
//                    read_all_regs.PerformClick();
//                    cmac_read_all_regs.PerformClick();

              //      reg_0x00.Text = ssh_ReadReg("0x80000000");
                    cmac_0_reg_0x10024.Text = ssh_ReadReg("0x81010024");
                    cmac_4_reg_0x10024.Text = ssh_ReadReg("0x82010024");
                read_all_regs.PerformClick();
                    ssh_connect.Text = "Connected";

                    //                ssh_client.Disconnect();
                    //                Thread oThread = new Thread(new ThreadStart(backgroundTask));
                    //                oThread.IsBackground = true;
                    //                oThread.Start();

                }
                catch (Exception ex)
                {
                    Console.WriteLine($"An error occurred: {ex.Message}");
                    ssh_connect.Text = "Connect";
                    ssh_connect.BackColor = Color.Red;
                }
 //           }
 //           else
 //           {
 //               
 //           }
        }

        private void reboot_fpga_Click(object sender, EventArgs e)
        {
            string str = String.Empty;

            if (MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                str = String.Empty;
                str += "XY";
                str += "\n";


            }

        }

        private void read_all_regs_Click(object sender, EventArgs e)
        {
            RegDump();
        }

        private void cmac_reg_read_Click(object sender, EventArgs e)
        {
            cmac_0_RegDump();
            cmac_4_RegDump();
        }

        private void dac_data_src_cmac_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028"); 
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 |= 0x100;   // bit8 = 1 <= CMAC

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg_0x28.Text = ssh_ReadReg("0x80000028");
        }

        private void dac_data_src_quadsend_rcv_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FEFF;   // bit8 = 0 <= quadsendrecv

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg_0x28.Text = ssh_ReadReg("0x80000028");
        }

        private void cmac_rx_en_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FFFC;   // bit1:0 = 0 <= cmac disabled

            cntrl_reg28 |= 0x02;   // bit0 = 1 <= Rx enable

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg_0x28.Text = ssh_ReadReg("0x80000028");
        }

        private void cmac_tx_en_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FFFC;   // bit1:0 = 0 <= cmac disabled

            cntrl_reg28 |= 0x01;   // bit0 = 1 <= Tx enable

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg_0x28.Text = ssh_ReadReg("0x80000028");
        }

        private void cmac_rxtx_en_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));

            cntrl_reg28 |= 0x03;   // bit1:0 = 1 <= Rx and Tx enable

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg_0x28.Text = ssh_ReadReg("0x80000028");
        }

        private void cmac_disable_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FFFC;   // bit1:0 = 0 <= cmac disabled

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg_0x28.Text = ssh_ReadReg("0x80000028");
        }

        private void cmac_init_Click(object sender, EventArgs e)
        {
            cmac_1_Init();
            cmac_4_Init();

            cmac_init.Text = "CMACs Initialized";
            cmac_init.BackColor = Color.LightGreen;
            cmac_read_all_regs.PerformClick();
            ssh_WriteReg("0x80000020", "0xCAFE"); // init flag
        }

        private void cmac_1_Init()
        {
            cmac_init.Text = "CMAC_1 Initializing...";
            // RESET - CMAC_1
            ssh_WriteReg("0x81010000", "0x1");
            cmac_0_reg_0x10000.Text = ssh_ReadReg("0x81010000");
            ssh_WriteReg("0x81010000", "0xFFFFFFFF");
            cmac_0_reg_0x10000.Text = ssh_ReadReg("0x81010000");

 //           await Task.Run(() =>
 //           {
                // Sleep 5
                Thread.Sleep(5000);
 //           });

            // release RESET - CMAC_1
            ssh_WriteReg("0x81010000", "0x0");
            cmac_0_reg_0x10000.Text = ssh_ReadReg("0x81010000");

            //#:: RX ENABLE
            //#:: offset 0x14
            //#:: Set bit(0) to 1 to enable, do not modify other bits
            ssh_WriteReg("0x81010014", "0x1");
            cmac_0_reg_0x10014.Text = ssh_ReadReg("0x81010014");

            //#:: TX ENABLE
            //#:: offset 0xC
            //#:: Set bit(0) to 1 to enable, do not modify other bits
            ssh_WriteReg("0x8101000C", "0x1");
            cmac_0_reg_0x1000C.Text = ssh_ReadReg("0x8101000C");

            //#:: LOOPBACK MODE
            //#:: 1=LB, 0=No LB
            ssh_WriteReg("0x81010090", "0x0");
            cmac_0_reg_0x10090.Text = ssh_ReadReg("0x81010090");

            // CONFIGURATION_TX_FLOW_CONTROL_CONTROL_REG1 
            // bits 8:0 - ctl_tx_pause_enable
            ssh_WriteReg("0x81010030", "0x00000100");
            cmac_0_reg_0x10030.Text = ssh_ReadReg("0x81010030");

            // CONFIGURATION_TX_FLOW_CONTROL_QUANTA_REG5
            // bit 15:0 - ctl_tx_pause_quanta8
            ssh_WriteReg("0x81010058", "0x00000001");
            cmac_0_reg_0x10058.Text = ssh_ReadReg("0x81010058");


            //#:: Set Destination MAC address
            //#:: Broadcast (not multicast, but accepted everywhere): FF:FF:FF:FF:FF:FF
            //#:: IPv4 multicast (All-hosts group): 01:00:5E:00:00:01
            //#:: IPv6 multicast (All-nodes group): 33:33:00:00:00:01
            ssh_WriteReg("0x81000000", "0x5E000001");
            cmac_0_reg_0x00.Text = ssh_ReadReg("0x81000000");

            ssh_WriteReg("0x81000004", "0x00000100");
            cmac_0_reg_0x04.Text = ssh_ReadReg("0x81000004");


            //#:: Set Source MAC address 02:00:00:00:00:01
            ssh_WriteReg("0x81000008", "0x00000001");
            cmac_0_reg_0x08.Text = ssh_ReadReg("0x81000008");
            ssh_WriteReg("0x8100000C", "0x00000200");
            cmac_0_reg_0x0C.Text = ssh_ReadReg("0x8100000C");

            //#:: Set EtherType
            //#:: 0x88B5 – IEEE “local/experimental” Ethertype (commonly used for custom payloads)
            //#:: 0x0800 – IPv4 (often whitelisted by other stacks, even if the payload isn’t fully IP)
            ssh_WriteReg("0x81000010", "0x000088B5");
            cmac_0_reg_0x10.Text = ssh_ReadReg("0x81000010");

            // PROG_FULL_ON_THRESH - 0x18
            ssh_WriteReg("0x81000018", "0x00000300");  //256 
            cmac_0_reg_0x18.Text = ssh_ReadReg("0x81000018");
            //PROG_FULL_OFF_THRESH - 0x1C
            ssh_WriteReg("0x8100001C", "0x00000100");  //768
            cmac_0_reg_0x1C.Text = ssh_ReadReg("0x8100001C");
        }
        private void cmac_4_Init()
        {
            cmac_init.Text = "CMAC_4 Initializing...";

            // RESET - CMAC_4
            ssh_WriteReg("0x82010000", "0x1");
            cmac_4_reg_0x10090.Text = ssh_ReadReg("0x82010000");
            ssh_WriteReg("0x82010000", "0xFFFFFFFF");
            cmac_4_reg_0x10000.Text = ssh_ReadReg("0x82010000");

 //           await Task.Run(() =>
 //           {
                // Sleep 5
                Thread.Sleep(5000);
//            });

            // release RESET - CMAC_4
            ssh_WriteReg("0x82010000", "0x0");
            cmac_4_reg_0x10000.Text = ssh_ReadReg("0x82010000");

            //#:: RX ENABLE
            //#:: offset 0x14
            //#:: Set bit(0) to 1 to enable, do not modify other bits
            ssh_WriteReg("0x82010014", "0x1");
            cmac_4_reg_0x10014.Text = ssh_ReadReg("0x82010014");


            //#:: TX ENABLE
            //#:: offset 0xC
            //#:: Set bit(0) to 1 to enable, do not modify other bits
            ssh_WriteReg("0x8201000C", "0x1");
            cmac_4_reg_0x1000C.Text = ssh_ReadReg("0x8201000C");

            //#:: LOOPBACK MODE
            //#:: 1=LB, 0=No LB
            ssh_WriteReg("0x82010090", "0x0");
            cmac_4_reg_0x10090.Text = ssh_ReadReg("0x82010090");

            // CONFIGURATION_TX_FLOW_CONTROL_CONTROL_REG1 
            // bits 8:0 - ctl_tx_pause_enable
            ssh_WriteReg("0x82010030", "0x00000100");
            cmac_4_reg_0x10030.Text = ssh_ReadReg("0x82010030");

            // CONFIGURATION_TX_FLOW_CONTROL_QUANTA_REG5
            // bit 15:0 - ctl_tx_pause_quanta8
            ssh_WriteReg("0x82010058", "0x00000007");
            cmac_4_reg_0x10058.Text = ssh_ReadReg("0x82010058");



            //#:: Set Destination MAC address
            //#:: Broadcast (not multicast, but accepted everywhere): FF:FF:FF:FF:FF:FF
            //#:: IPv4 multicast (All-hosts group): 01:00:5E:00:00:01
            //#:: IPv6 multicast (All-nodes group): 33:33:00:00:00:01
            ssh_WriteReg("0x82000000", "0x5E000001");
            cmac_4_reg_0x00.Text = ssh_ReadReg("0x82000000");
            ssh_WriteReg("0x82000004", "0x00000100");
            cmac_4_reg_0x04.Text = ssh_ReadReg("0x82000004");


            //#:: Set Source MAC address 02:00:00:00:00:01
            ssh_WriteReg("0x82000008", "0x00000001");
            cmac_4_reg_0x08.Text = ssh_ReadReg("0x82000008");
            ssh_WriteReg("0x8200000C", "0x00000200");
            cmac_4_reg_0x0C.Text = ssh_ReadReg("0x8200000c");



            //#:: Set EtherType
            //#:: 0x88B5 – IEEE “local/experimental” Ethertype (commonly used for custom payloads)
            //#:: 0x0800 – IPv4 (often whitelisted by other stacks, even if the payload isn’t fully IP)
            ssh_WriteReg("0x82000010", "0x000088B5");
            cmac_4_reg_0x10.Text = ssh_ReadReg("0x82000010");

            // PROG_FULL_ON_THRESH - 0x18
            ssh_WriteReg("0x82000018", "0x00000300");  //256
            cmac_4_reg_0x18.Text = ssh_ReadReg("0x82000018");
            //PROG_FULL_OFF_THRESH - 0x1C
            ssh_WriteReg("0x8200001C", "0x00000100");  //768 
            cmac_4_reg_0x1C.Text = ssh_ReadReg("0x8200001C");
        }

        private void reg_read_Click(object sender, EventArgs e)
        {
            reg_data.Text = ssh_ReadReg("0x"+reg_addr.Text);
        }

        private void reg_write_Click(object sender, EventArgs e)
        {
            ssh_WriteReg("0x"+reg_addr.Text, "0x"+reg_data.Text);
        }

        private void counters_rst_Click(object sender, EventArgs e)
        {
            reg_0x20.Text = ssh_ReadReg("0x80000020");
            uint cntrl_reg20 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg20 |= 0x8000_0000;   // bit31:  1 <= reset

            ssh_WriteReg("0x80000020", "0x" + cntrl_reg20.ToString("X8"));
            reg_0x20.Text = ssh_ReadReg("0x80000020");

            reg_0x20.Text = ssh_ReadReg("0x80000020");
            cntrl_reg20 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg20 &= 0x7FFF_FFFF;   // bit31:  0 <= normal

            ssh_WriteReg("0x80000020", "0x" + cntrl_reg20.ToString("X8"));
            reg_0x20.Text = ssh_ReadReg("0x80000020");

        }

        private void cmac_rx_tx_debug_regs_Click(object sender, EventArgs e)
        {
            if (true == ssh_client.IsConnected)
            {
                cmac_0_reg_0x40.Text = "";
                cmac_0_reg_0x44.Text = "";
                cmac_0_reg_0x48.Text = "";
                cmac_0_reg_0x4C.Text = "";
                cmac_0_reg_0x50.Text = "";
                cmac_0_reg_0x54.Text = "";
                cmac_0_reg_0x58.Text = "";

                cmac_4_reg_0x40.Text = "";
                cmac_4_reg_0x44.Text = "";
                cmac_4_reg_0x48.Text = "";
                cmac_4_reg_0x4C.Text = "";
                cmac_4_reg_0x50.Text = "";
                cmac_4_reg_0x54.Text = "";
                cmac_4_reg_0x58.Text = "";

                cmac_0_reg_0x40.Text = ssh_ReadReg("0x81000040");
                cmac_0_reg_0x44.Text = ssh_ReadReg("0x81000044");
                cmac_0_reg_0x48.Text = ssh_ReadReg("0x81000048");
                cmac_0_reg_0x4C.Text = ssh_ReadReg("0x8100004C");
                cmac_0_reg_0x50.Text = ssh_ReadReg("0x81000050");
                cmac_0_reg_0x54.Text = ssh_ReadReg("0x81000054");
                cmac_0_reg_0x58.Text = ssh_ReadReg("0x81000058");

                cmac_4_reg_0x40.Text = ssh_ReadReg("0x82000040");
                cmac_4_reg_0x44.Text = ssh_ReadReg("0x82000044");
                cmac_4_reg_0x48.Text = ssh_ReadReg("0x82000048");
                cmac_4_reg_0x4C.Text = ssh_ReadReg("0x8200004C");
                cmac_4_reg_0x50.Text = ssh_ReadReg("0x82000050");
                cmac_4_reg_0x54.Text = ssh_ReadReg("0x82000054");
                cmac_4_reg_0x58.Text = ssh_ReadReg("0x82000058");

            }
        }

        private void cmac2_dac_chan_both_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFFFC_FFFF;   // bit17:16  0 <= both

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg_0x28.Text = ssh_ReadReg("0x80000028");

            // CONFIGURATION_TX_FLOW_CONTROL_QUANTA_REG5
            // bit 15:0 - ctl_tx_pause_quanta8
            ssh_WriteReg("0x81010058", "0x00000001");
            cmac_0_reg_0x10058.Text = ssh_ReadReg("0x81010058");


            // CONFIGURATION_TX_FLOW_CONTROL_QUANTA_REG5
            // bit 15:0 - ctl_tx_pause_quanta8
            ssh_WriteReg("0x82010058", "0x00000007");
            cmac_4_reg_0x10058.Text = ssh_ReadReg("0x82010058");

        }

        private void cmac2_dac_chan_1_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFFFC_FFFF;   // bit17:16  
            cntrl_reg28 |= 0x1_0000;   // bit17:16  1 <= chan 1

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg_0x28.Text = ssh_ReadReg("0x80000028");


            // CONFIGURATION_TX_FLOW_CONTROL_QUANTA_REG5
            // bit 15:0 - ctl_tx_pause_quanta8
            ssh_WriteReg("0x81010058", "0x00000007");
            cmac_0_reg_0x10058.Text = ssh_ReadReg("0x81010058");
        }

        private void cmac2_dac_chan_2_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFFFC_FFFF;   // bit17:16  
            cntrl_reg28 |= 0x2_0000;   // bit17:16  2 <= chan 2

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg_0x28.Text = ssh_ReadReg("0x80000028");

            // CONFIGURATION_TX_FLOW_CONTROL_QUANTA_REG5
            // bit 15:0 - ctl_tx_pause_quanta8
            ssh_WriteReg("0x82010058", "0x00000007");
            cmac_4_reg_0x10058.Text = ssh_ReadReg("0x82010058");

        }
    }
}
