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
    {
        bool system_on = false;
        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);

                cmd = ssh_client.RunCommand(str1);
                str = cmd.Result;
//                Console.WriteLine("ssh_ReadReg(): Response:"+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);

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

//                Console.WriteLine("ssh_WriteReg(): Response:"+ cmd.Result);
            }
        }


   


        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_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;
                }
                else if (0x200 == (val & 0x300))
                {
                    dac_data_src_dds.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;
                }

                if (0 == (val & 0x3_0000))
                {
                    cmac2_dac_chan_both.Checked = true;
                }
                else if (0x10000 == (val & 0x3_0000))
                {
                    cmac2_dac_chan_1.Checked = true;
                }
                else if (0x20000 == (val & 0x3_0000))
                {
                    cmac2_dac_chan_2.Checked = true;
                }

                if (0 == (val & 0x0100_0000))
                {
                    tx_fiber_data_src_adc.Checked = true;
                }
                else if (0x0100_0000 == (val & 0x0100_0000))
                {
                    tx_fiber_data_src_dds.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");

                read_counters.PerformClick();
            }
        }




        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_0_reg_0x10030.Text = "";
                cmac_0_reg_0x10058.Text = "";
                cmac_0_reg_0x10044.Text = "";
                cmac_0_reg_0x10084.Text = "";
                cmac_0_reg_0x10088.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_0x10044.Text = ssh_ReadReg("0x81010044");
                cmac_0_reg_0x10084.Text = ssh_ReadReg("0x81010084");
                cmac_0_reg_0x10088.Text = ssh_ReadReg("0x81010088");

                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_0x10044.Text = "";
                cmac_4_reg_0x10084.Text = "";
                cmac_4_reg_0x10088.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_0x10044.Text = ssh_ReadReg("0x81010044");
                cmac_4_reg_0x10084.Text = ssh_ReadReg("0x81010084");
                cmac_4_reg_0x10088.Text = ssh_ReadReg("0x81010088");

                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)
        {
   

        }

        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 &= 0xFFFF_FCFF;  // bit9:8 - clear bits
            cntrl_reg28 |= 0x100;   // bit9:8 = 01 <= 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_FCFF;   // bit9:8 = 00 <= 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...";
            // GT_RESET - CMAC_1 - self clearing
            ssh_WriteReg("0x81010000", "0x1");
            cmac_0_reg_0x10000.Text = ssh_ReadReg("0x81010000");
            // RESET - reset all logic
            ssh_WriteReg("0x81010004", "0xFFFFFFFF");
            cmac_0_reg_0x10004.Text = ssh_ReadReg("0x81010004");

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

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

            // CONFIGURATION_TX_FLOW_CONTROL_REFRESH_REG5 
            // bits 15:0 - ctl_tx_pause_refresh_timer8
            ssh_WriteReg("0x81010044", "0x00000000");
            cmac_0_reg_0x10044.Text = ssh_ReadReg("0x81010044");

            // 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_CONTROL_REG1 
            // bits 8:0 - ctl_tx_pause_enable
            ssh_WriteReg("0x81010030", "0x00000100");
            cmac_0_reg_0x10030.Text = ssh_ReadReg("0x81010030");

            // CONFIGURATION_RX_FLOW_CONTROL_CONTROL_REG1 
            // bit12 - ctl_rx_enable_gpp, bit10 - ctl_rx_enable_gcp bit8 - ctl_rx_pause_enable
            ssh_WriteReg("0x81010084", "0x00001500");
            cmac_0_reg_0x10084.Text = ssh_ReadReg("0x81010084");

            // CONFIGURATION_RX_FLOW_CONTROL_CONTROL_REG2 
            // bits 31:0 - ctl_rx_check....
            ssh_WriteReg("0x81010088", "0x00007C1F");
            cmac_0_reg_0x10088.Text = ssh_ReadReg("0x81010088");

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

            //#:: 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");

            //#:: 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");

            //#:: 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");
            // RESET - reset all logic
            ssh_WriteReg("0x82010004", "0xFFFFFFFF");
            cmac_4_reg_0x10004.Text = ssh_ReadReg("0x82010004");

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

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

            // CONFIGURATION_TX_FLOW_CONTROL_REFRESH_REG5 
            // bits 15:0 - ctl_tx_pause_refresh_timer8
            ssh_WriteReg("0x82010044", "0x00000000");
            cmac_4_reg_0x10044.Text = ssh_ReadReg("0x82010044");

            // 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");

            // 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_RX_FLOW_CONTROL_CONTROL_REG1 
            // bit12 - ctl_rx_enable_gpp, bit10 - ctl_rx_enable_gcp bit8 - ctl_rx_pause_enable
            ssh_WriteReg("0x82010084", "0x00001500");
            cmac_4_reg_0x10084.Text = ssh_ReadReg("0x82010084");

            // CONFIGURATION_RX_FLOW_CONTROL_CONTROL_REG2 
            // bits 31:0 - ctl_rx_check....
            ssh_WriteReg("0x82010088", "0x00007C1F");
            cmac_4_reg_0x10088.Text = ssh_ReadReg("0x82010088");

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

            //#:: 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");

            //#:: 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");

            //#:: 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)
        {
            uint overflow_0 = (Convert.ToUInt32(cmac_0_reg_0x58.Text, 16));
            uint overflow_4 = (Convert.ToUInt32(cmac_4_reg_0x58.Text, 16));

            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");

            }

            uint overflow_0_cur = (Convert.ToUInt32(cmac_0_reg_0x58.Text, 16));
            uint overflow_4_cur = (Convert.ToUInt32(cmac_4_reg_0x58.Text, 16));
            uint overflow_0_err = overflow_0_cur - overflow_0;
            uint overflow_4_err = overflow_4_cur - overflow_4;
            textBox1.Text = overflow_0_err.ToString("X8");
            textBox2.Text = overflow_4_err.ToString("X8");


        }

        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");

        }
        private void ComputeSend_PDW(uint chan, bool wave_type, double start_freq, double end_freq, double duration, double idle_samples, uint phase_inc_dwell, out double actual_start_freq, out double actual_end_freq, out double actual_duration, out double actual_idle_samples)
        {
            double sample_rate = 375e6;
            string str = String.Empty;
            double MULT = 4294967296.0; // 2 ^ 32;
//            double MULT = 65536.0;  // 2 ^ 16;

            int step_size_t;

            str += "************************";
            str += "\n";
            richTextBox0.AppendText(str); //.Text = str;            //  

            str = String.Empty;
            str += "chan: ";
            str += chan.ToString("X1");
            str += "\n";
            richTextBox0.AppendText(str);

            Console.WriteLine("start_freq: {0} ", start_freq);
            str = String.Empty;
            str += "start_freq: ";
            str += start_freq.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            Console.WriteLine("end_freq: {0} ", end_freq);
            str = String.Empty;
            str += "end_freq: ";
            str += end_freq.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            Console.WriteLine("duration: {0} ", duration);
            str = String.Empty;
            str += "duration: ";
            str += duration.ToString("G6");
            str += "\n\n";
            richTextBox0.AppendText(str);

            Console.WriteLine("idle_samples: {0} ", idle_samples);
            str = String.Empty;
            str += "idle_samples: ";
            str += idle_samples.ToString("G6");
            str += "\n";
            richTextBox0.AppendText(str);

            double num_samples = duration / (1 / sample_rate); // num_samples = duration / (1/sample_rate)
            Console.WriteLine("num_samples: {0} ", num_samples);
            str = String.Empty;
            str += "num_samples: ";
            str += num_samples.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);
            uint num_samples_t = (uint)Math.Round(num_samples);/////////////////////////////////////
            str = String.Empty;
            str += "num_samples_t: ";
            str += num_samples_t.ToString("D");
            str += "\n\n";
            richTextBox0.AppendText(str);

            actual_duration = num_samples / sample_rate;
            str = String.Empty;
            str += "actual_duration: ";
            str += actual_duration.ToString("G6");
            str += "\n\n";
            richTextBox0.AppendText(str);

            double start_freq_phase_inc = MULT * (start_freq / sample_rate); // PhaseInc = 2^32 * (desired freq / sample rate)
            Console.WriteLine("start_freq_phase_inc: {0} ", start_freq_phase_inc);
            str = String.Empty;
            str += "start_freq_phase_inc: ";
            str += start_freq_phase_inc.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            double t_double = Math.Round(start_freq_phase_inc, MidpointRounding.AwayFromZero);//////////////////////////////
            str = String.Empty;
            str += "t_double: ";
            str += t_double.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            int start_freq_phase_inc_t = (int)t_double;
            str = String.Empty;
            str += "start_freq_phase_inc_t: ";
            str += start_freq_phase_inc_t.ToString("D");
            str += "\n";
            richTextBox0.AppendText(str);
            actual_start_freq = start_freq_phase_inc_t * sample_rate / MULT;   // desired freq = PhaseInc * sample rate / 2^32
            str = String.Empty;
            str += "actual_start_freq: ";
            str += actual_start_freq.ToString("G6");
            str += "\n\n";
            richTextBox0.AppendText(str);

            if (wave_type == true)
            {
                double end_freq_phase_inc = MULT * (end_freq / sample_rate);  // PhaseInc = 2^32 * (desired freq / sample rate)
                Console.WriteLine("end_freq_phase_inc: {0} ", end_freq_phase_inc);
                str = String.Empty;
                str += "end_freq_phase_inc: ";
                str += end_freq_phase_inc.ToString("F");
                str += "\n";
                richTextBox0.AppendText(str);
                int end_freq_phase_inc_t = (int)Math.Round(end_freq_phase_inc);//////////////////////////////////////
                str = String.Empty;
                str += "end_freq_phase_inc_t: ";
                str += end_freq_phase_inc_t.ToString("D");
                str += "\n";
                richTextBox0.AppendText(str);
                actual_end_freq = end_freq_phase_inc_t * sample_rate / MULT;   // desired freq = PhaseInc * sample rate / 2^32

                double step_size = (double)(end_freq_phase_inc - start_freq_phase_inc) / num_samples; // step_size = start_phase_inc - end_phase_inc / num_samples
                Console.WriteLine("step_size: {0} ", step_size);
                str = String.Empty;
                str += "step_size: ";
                str += step_size.ToString("F");
                str += "\n";
                richTextBox0.AppendText(str);
                step_size_t = (int)Math.Round(step_size);///////////////////////////
            }
            else
            {
                str = String.Empty;
                str += "*****CW******: ";
                str += "\n";
                richTextBox0.AppendText(str);
                step_size_t = 0;
                actual_end_freq = actual_start_freq;
            }

            double idle_samples_d = idle_samples / (1 / sample_rate);
            uint idle_samples_t = (uint)Math.Round(idle_samples_d);
            actual_idle_samples = idle_samples_t * (1 / sample_rate);
            str = String.Empty;
            str += "actual_idle_samples: ";
            str += actual_idle_samples.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            str = String.Empty;
            str += "step_size_t: ";
            str += step_size_t.ToString("D");
            str += "\n";
            richTextBox0.AppendText(str);

            str = String.Empty;
            str += "actual_end_freq: ";
            str += actual_end_freq.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            str = String.Empty;
            str += "--------------------------";
            str += "\n";
            richTextBox0.AppendText(str);

            str = String.Empty;
            str += "phase_inc_dwell: ";
            str += phase_inc_dwell.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x83000014", "0x" + phase_inc_dwell.ToString("X8"));
                dds_reg14.Text = phase_inc_dwell.ToString("X8");
//                ssh_WriteReg("0x83000034", "0x" + phase_inc_dwell.ToString("X8"));
//                dds_reg34.Text = phase_inc_dwell.ToString("X8");
//                ssh_WriteReg("0x83000054", "0x" + phase_inc_dwell.ToString("X8"));
//                dds_reg54.Text = phase_inc_dwell.ToString("X8");
//                ssh_WriteReg("0x83000074", "0x" + phase_inc_dwell.ToString("X8"));
//                dds_reg74.Text = phase_inc_dwell.ToString("X8");

            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x84000014", "0x" + phase_inc_dwell.ToString("X8"));
                dds_reg4_14.Text = phase_inc_dwell.ToString("X8");
//                ssh_WriteReg("0x84000034", "0x" + phase_inc_dwell.ToString("X8"));
//                dds_reg4_34.Text = phase_inc_dwell.ToString("X8");
//                ssh_WriteReg("0x84000054", "0x" + phase_inc_dwell.ToString("X8"));
//                dds_reg4_54.Text = phase_inc_dwell.ToString("X8");
//                ssh_WriteReg("0x84000074", "0x" + phase_inc_dwell.ToString("X8"));
//                dds_reg4_74.Text = phase_inc_dwell.ToString("X8");
            }

            str = String.Empty;
            str += "step_size_t: ";
            str += step_size_t.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x83000018", "0x" + step_size_t.ToString("X8"));
                dds_reg18.Text = step_size_t.ToString("X8");
//                ssh_WriteReg("0x83000038", "0x" + step_size_t.ToString("X8"));
//                dds_reg38.Text = step_size_t.ToString("X8");
//                ssh_WriteReg("0x83000058", "0x" + step_size_t.ToString("X8"));
//                dds_reg58.Text = step_size_t.ToString("X8");
//                ssh_WriteReg("0x83000078", "0x" + step_size_t.ToString("X8"));
//                dds_reg78.Text = step_size_t.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x84000018", "0x" + step_size_t.ToString("X8"));
                dds_reg4_18.Text = step_size_t.ToString("X8");
 //               ssh_WriteReg("0x84000038", "0x" + step_size_t.ToString("X8"));
//                dds_reg4_38.Text = step_size_t.ToString("X8");
//                ssh_WriteReg("0x84000058", "0x" + step_size_t.ToString("X8"));
//                dds_reg4_58.Text = step_size_t.ToString("X8");
//                ssh_WriteReg("0x84000078", "0x" + step_size_t.ToString("X8"));
//                dds_reg4_78.Text = step_size_t.ToString("X8");
            }

            str = String.Empty;
            str += "idle_samples_t: ";
            str += idle_samples_t.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x8300001C", "0x" + idle_samples_t.ToString("X8"));
                dds_reg1C.Text = idle_samples_t.ToString("X8");
//                ssh_WriteReg("0x8300003C", "0x" + idle_samples_t.ToString("X8"));
//                dds_reg3C.Text = idle_samples_t.ToString("X8");
//                ssh_WriteReg("0x8300005C", "0x" + idle_samples_t.ToString("X8"));
//                dds_reg5C.Text = idle_samples_t.ToString("X8");
//                ssh_WriteReg("0x8300007C", "0x" + idle_samples_t.ToString("X8"));
//                dds_reg7C.Text = idle_samples_t.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x8400001C", "0x" + idle_samples_t.ToString("X8"));
                dds_reg4_1C.Text = idle_samples_t.ToString("X8");
//                ssh_WriteReg("0x8400003C", "0x" + idle_samples_t.ToString("X8"));
//                dds_reg4_3C.Text = idle_samples_t.ToString("X8");
//                ssh_WriteReg("0x8400005C", "0x" + idle_samples_t.ToString("X8"));
//                dds_reg4_5C.Text = idle_samples_t.ToString("X8");
//                ssh_WriteReg("0x8400007C", "0x" + idle_samples_t.ToString("X8"));
 //               dds_reg4_7C.Text = idle_samples_t.ToString("X8");
            }

            str = String.Empty;
            str += "num_samples_t: ";
            str += num_samples_t.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x83000020", "0x" + num_samples_t.ToString("X8"));
                dds_reg20.Text = num_samples_t.ToString("X8");
//                ssh_WriteReg("0x83000040", "0x" + num_samples_t.ToString("X8"));
//                dds_reg40.Text = num_samples_t.ToString("X8");
//                ssh_WriteReg("0x83000060", "0x" + num_samples_t.ToString("X8"));
//                dds_reg60.Text = num_samples_t.ToString("X8");
//                ssh_WriteReg("0x83000080", "0x" + num_samples_t.ToString("X8"));
//                dds_reg80.Text = num_samples_t.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x84000020", "0x" + num_samples_t.ToString("X8"));
                dds_reg4_20.Text = num_samples_t.ToString("X8");
//                ssh_WriteReg("0x84000040", "0x" + num_samples_t.ToString("X8"));
//                dds_reg4_40.Text = num_samples_t.ToString("X8");
//                ssh_WriteReg("0x84000060", "0x" + num_samples_t.ToString("X8"));
//                dds_reg4_60.Text = num_samples_t.ToString("X8");
//                ssh_WriteReg("0x84000080", "0x" + num_samples_t.ToString("X8"));
//                dds_reg4_80.Text = num_samples_t.ToString("X8");
            }

            str = String.Empty;
            str += "start_freq_phase_inc_t: ";
            str += start_freq_phase_inc_t.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x83000024", "0x" + start_freq_phase_inc_t.ToString("X8"));
                dds_reg24.Text = start_freq_phase_inc_t.ToString("X8");
 //               ssh_WriteReg("0x83000044", "0x" + start_freq_phase_inc_t.ToString("X8"));
 //               dds_reg44.Text = start_freq_phase_inc_t.ToString("X8");
 //               ssh_WriteReg("0x83000064", "0x" + start_freq_phase_inc_t.ToString("X8"));
 //               dds_reg64.Text = start_freq_phase_inc_t.ToString("X8");
 //               ssh_WriteReg("0x83000084", "0x" + start_freq_phase_inc_t.ToString("X8"));
 //               dds_reg84.Text = start_freq_phase_inc_t.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x84000024", "0x" + start_freq_phase_inc_t.ToString("X8"));
                dds_reg4_24.Text = start_freq_phase_inc_t.ToString("X8");
//                ssh_WriteReg("0x84000044", "0x" + start_freq_phase_inc_t.ToString("X8"));
//                dds_reg4_44.Text = start_freq_phase_inc_t.ToString("X8");
//                ssh_WriteReg("0x84000064", "0x" + start_freq_phase_inc_t.ToString("X8"));
//                dds_reg4_64.Text = start_freq_phase_inc_t.ToString("X8");
//                ssh_WriteReg("0x84000084", "0x" + start_freq_phase_inc_t.ToString("X8"));
//                dds_reg4_84.Text = start_freq_phase_inc_t.ToString("X8");
            }

            int phase_offset_t = start_freq_phase_inc_t / 4;
            int phase_offset_t1;
            if (chan == 1)
            {
                phase_offset_t1 = phase_offset_t * 0;
                ssh_WriteReg("0x83000028", "0x" + phase_offset_t1.ToString("X8"));// phase_off_1
                dds_reg28.Text = phase_offset_t1.ToString("X8");
                phase_offset_t1 = phase_offset_t * 1;
                ssh_WriteReg("0x83000048", "0x" + phase_offset_t1.ToString("X8")); 
                dds_reg48.Text = phase_offset_t1.ToString("X8");
                phase_offset_t1 = phase_offset_t * 2;
                ssh_WriteReg("0x83000068", "0x" + phase_offset_t1.ToString("X8")); 
                dds_reg68.Text = phase_offset_t1.ToString("X8");
                phase_offset_t1 = phase_offset_t * 3;
                ssh_WriteReg("0x83000088", "0x" + phase_offset_t1.ToString("X8")); 
                dds_reg88.Text = phase_offset_t1.ToString("X8");
            }
            else if (chan == 2)
            {
                phase_offset_t1 = phase_offset_t * 0;
                ssh_WriteReg("0x84000028", "0x" + phase_offset_t1.ToString("X8"));// phase_off_1
                dds_reg4_28.Text = phase_offset_t1.ToString("X8");
                phase_offset_t1 = phase_offset_t * 1;
                ssh_WriteReg("0x84000048", "0x" + phase_offset_t1.ToString("X8"));
                dds_reg4_48.Text = phase_offset_t1.ToString("X8");
                phase_offset_t1 = phase_offset_t * 2;
                ssh_WriteReg("0x84000068", "0x" + phase_offset_t1.ToString("X8"));
                dds_reg4_68.Text = phase_offset_t1.ToString("X8");
                phase_offset_t1 = phase_offset_t * 3;
                ssh_WriteReg("0x84000088", "0x" + phase_offset_t1.ToString("X8"));
                dds_reg4_88.Text = phase_offset_t1.ToString("X8");
            }


        }

        private void Set_ScaleFactor(uint chan, double scale_db, bool flag)
        {
            string str = String.Empty;

            uint scale_factor = (uint)(Math.Pow(2.0, scale_db / 6.0) * 0x8000);
            uint swap_sf;

            Console.WriteLine("scale_db: {0} ", scale_db);
            str += "scale_db: ";
            str += scale_db.ToString("N1");
            str += "\n";
            richTextBox0.AppendText(str);

            swap_sf = scale_factor;

            if (flag == true)
            {
                swap_sf = swap_sf | 0x10000;
            }
            else
            {
                swap_sf = swap_sf & 0xFFFEFFFF;
            }

            str = String.Empty;
            str += "scale_factor: ";
            str += scale_factor.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);

            if (chan == 1)
            {
                ssh_WriteReg("0x83000010", "0x" + scale_factor.ToString("X8"));
                dds_reg10.Text = scale_factor.ToString("X8");
//                ssh_WriteReg("0x83000030", "0x" + scale_factor.ToString("X8"));
//                dds_reg30.Text = scale_factor.ToString("X8");
//                ssh_WriteReg("0x83000050", "0x" + scale_factor.ToString("X8"));
//                dds_reg50.Text = scale_factor.ToString("X8");
//                ssh_WriteReg("0x83000070", "0x" + scale_factor.ToString("X8"));
//                dds_reg70.Text = scale_factor.ToString("X8");

                ssh_WriteReg("0x8300002C", "0x" + swap_sf.ToString("X8"));
                dds_reg2C.Text = swap_sf.ToString("X8");
//                ssh_WriteReg("0x8300004C", "0x" + swap_sf.ToString("X8"));
//                dds_reg4C.Text = swap_sf.ToString("X8");
//                ssh_WriteReg("0x8300006C", "0x" + swap_sf.ToString("X8"));
//                dds_reg6C.Text = swap_sf.ToString("X8");
//                ssh_WriteReg("0x8300008C", "0x" + swap_sf.ToString("X8"));
//                dds_reg8C.Text = swap_sf.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x84000010", "0x" + scale_factor.ToString("X8"));
                dds_reg4_10.Text = scale_factor.ToString("X8");
//                ssh_WriteReg("0x84000030", "0x" + scale_factor.ToString("X8"));
//                dds_reg4_30.Text = scale_factor.ToString("X8");
//                ssh_WriteReg("0x84000050", "0x" + scale_factor.ToString("X8"));
//                dds_reg4_50.Text = scale_factor.ToString("X8");
//                ssh_WriteReg("0x84000070", "0x" + scale_factor.ToString("X8"));
//                dds_reg4_70.Text = scale_factor.ToString("X8");

                ssh_WriteReg("0x8400002C", "0x" + swap_sf.ToString("X8"));
                dds_reg4_2C.Text = swap_sf.ToString("X8");
//                ssh_WriteReg("0x8400004C", "0x" + swap_sf.ToString("X8"));
//                dds_reg4_4C.Text = swap_sf.ToString("X8");
//                ssh_WriteReg("0x8400006C", "0x" + swap_sf.ToString("X8"));
//                dds_reg4_6C.Text = swap_sf.ToString("X8");
//                ssh_WriteReg("0x8400008C", "0x" + swap_sf.ToString("X8"));
//                dds_reg4_8C.Text = swap_sf.ToString("X8");
            }

        }

        private void load_button_w1_Click(object sender, EventArgs e)
        {
            load_button_w1.Text = "Loading...";
            load_button_w1.BackColor = Color.LightGreen;

            double actual_start_freq;
            double actual_end_freq;
            double actual_duration;
            double actual_idle_samples;

            string str = String.Empty;
            str += "************************";
            str += "\n";
            richTextBox0.Text = str;

            double start_freq = (Convert.ToDouble(start_freq_w1.Text)) * 1e6;
            double end_freq = (Convert.ToDouble(end_freq_w1.Text)) * 1e6;
            double duration = (Convert.ToDouble(duration_w1.Text)) * 1e-6;
            double idle_samples = (Convert.ToDouble(idle_samples_w1.Text)) * 1e-6;
            uint phase_inc_dwell = (Convert.ToUInt32(phase_inc_dwell_w1.Text, 10));

            ComputeSend_PDW(1, chirp_w1.Checked, start_freq, end_freq, duration, idle_samples, phase_inc_dwell, out actual_start_freq, out actual_end_freq, out actual_duration, out actual_idle_samples);
            actual_start_freq_w1.Text = actual_start_freq.ToString("G6");
            actual_end_freq_w1.Text = actual_end_freq.ToString("G6");
            actual_duration_w1.Text = actual_duration.ToString("G6");
            actual_idle_samples_w1.Text = actual_idle_samples.ToString("G6");

            double scale_factor = (double)scale_factor_w1.Value;
            Set_ScaleFactor(1, scale_factor, iq_swap_w1.Checked);



            load_button_w1.Text = "Load";
            load_button_w1.BackColor = Color.Gainsboro;
            dds_send_w1.BackColor = Color.Yellow;
        }

        private void load_button_w2_Click(object sender, EventArgs e)
        {
            load_button_w2.Text = "Loading...";
            load_button_w2.BackColor = Color.LightGreen;

            string str = String.Empty;
            double actual_start_freq;
            double actual_end_freq;
            double actual_duration;
            double actual_idle_samples;

            str += "************************";
            str += "\n";
            richTextBox0.AppendText(str);

            double start_freq = (double)(Convert.ToDouble(start_freq_w2.Text)) * 1e6;
            double end_freq = (double)(Convert.ToDouble(end_freq_w2.Text)) * 1e6;
            double duration = (double)(Convert.ToDouble(duration_w2.Text)) * 1e-6;
            double idle_samples = (Convert.ToDouble(idle_samples_w2.Text)) * 1e-6;
            uint phase_inc_dwell = (Convert.ToUInt32(phase_inc_dwell_w2.Text, 10));

            ComputeSend_PDW(2, chirp_w2.Checked, start_freq, end_freq, duration, idle_samples, phase_inc_dwell, out actual_start_freq, out actual_end_freq, out actual_duration, out actual_idle_samples);
            actual_start_freq_w2.Text = actual_start_freq.ToString("F");
            actual_end_freq_w2.Text = actual_end_freq.ToString("F");
            actual_duration_w2.Text = actual_duration.ToString("G6");
            actual_idle_samples_w2.Text = actual_idle_samples.ToString("G6");

            double scale_factor = (double)scale_factor_w2.Value;
            Set_ScaleFactor(2, scale_factor, iq_swap_w2.Checked);

            load_button_w2.Text = "Load";
            load_button_w2.BackColor = Color.Gainsboro;
            dds_send_w2.BackColor = Color.Yellow;
        }

        private void dds_send_w1_Click(object sender, EventArgs e)
        {
            dds_send_w1.Text = "Sending...";
            dds_send_w1.BackColor = Color.LightGreen;


            ssh_WriteReg("0x83000000", "0x00000001"); //bit 0  self clearing bit
            dds_send_w1.Text = "Send";
            dds_send_w1.BackColor = Color.Gainsboro;
        }

        private void dds_send_w2_Click(object sender, EventArgs e)
        {
            dds_send_w2.Text = "Sending...";
            dds_send_w2.BackColor = Color.LightGreen;


            ssh_WriteReg("0x84000000", "0x00000001"); //bit 4 self clearing bit
            dds_send_w2.Text = "Send";
            dds_send_w2.BackColor = Color.Gainsboro;
        }

        private void dac_off_Click(object sender, EventArgs e)
        {
            ssh_WriteReg("0x83000004", "0x00000001");  // set bit0 - 1 = DAC Hold Off ON
            dds_reg04.Text = ssh_ReadReg("0x8300004");

            system_status.Text = "OFF";
            dac_on.BackColor = Color.LightGray;
            dac_off.BackColor = Color.Red;
            system_status.BackColor = Color.LightGray;
            system_on = false;
        }

        private void dac_on_Click(object sender, EventArgs e)
        {
            ssh_WriteReg("0x83000004", "0x00000000");  // set bit0 - 0 = DAC Hold Off OFF
            dds_reg04.Text = ssh_ReadReg("0x8300004");

            dac_off.BackColor = Color.LightGray;
            dac_on.BackColor = Color.LightGreen;
            system_status.BackColor = Color.LightGreen;
            if (tx_fiber_data_src_adc.Checked == true)
            {
                system_status.Text = "ARMED";

            }
            else if (tx_fiber_data_src_dds.Checked == true)
            {
                system_status.Text = "ARMED";
            }

            system_on = true;
        }

        private void dds_all_refresh_Click(object sender, EventArgs e)
        {
            dds_reg10.Text = ssh_ReadReg("0x83000010");
            dds_reg14.Text = ssh_ReadReg("0x83000014");
            dds_reg18.Text = ssh_ReadReg("0x83000018");
            dds_reg1C.Text = ssh_ReadReg("0x8300001C");
            dds_reg20.Text = ssh_ReadReg("0x83000020");
            dds_reg24.Text = ssh_ReadReg("0x83000024");
            dds_reg28.Text = ssh_ReadReg("0x83000028");
            dds_reg2C.Text = ssh_ReadReg("0x8300002C");

            dds_reg48.Text = ssh_ReadReg("0x83000048");

            dds_reg68.Text = ssh_ReadReg("0x83000068");
            dds_reg88.Text = ssh_ReadReg("0x83000088");



            dds_reg4_10.Text = ssh_ReadReg("0x84000010");
            dds_reg4_14.Text = ssh_ReadReg("0x84000014");
            dds_reg4_18.Text = ssh_ReadReg("0x84000018");
            dds_reg4_1C.Text = ssh_ReadReg("0x8400001C");
            dds_reg4_20.Text = ssh_ReadReg("0x84000020");
            dds_reg4_24.Text = ssh_ReadReg("0x84000024");
            dds_reg4_28.Text = ssh_ReadReg("0x84000028");
            dds_reg4_2C.Text = ssh_ReadReg("0x8400002C");

            dds_reg4_48.Text = ssh_ReadReg("0x84000048");

            dds_reg4_68.Text = ssh_ReadReg("0x84000068");
            dds_reg4_88.Text = ssh_ReadReg("0x84000088");

        }

        private void dds_refresh_Click(object sender, EventArgs e)
        {
            dds_reg00.Text = ssh_ReadReg("0x83000000");
            dds_reg04.Text = ssh_ReadReg("0x83000004");
            dds_reg08.Text = ssh_ReadReg("0x83000008");
            dds_reg0C.Text = ssh_ReadReg("0x8300000C");

            dds_reg4_00.Text = ssh_ReadReg("0x84000000");
            dds_reg4_04.Text = ssh_ReadReg("0x84000004");
            dds_reg4_08.Text = ssh_ReadReg("0x84000008");
            dds_reg4_0C.Text = ssh_ReadReg("0x8400000C");

            string str = ssh_ReadReg("0x83000004");
            uint val = (Convert.ToUInt32(str, 16));
            dds_reg04.Text = str;

            if (1 == (val & 0x1))
            {
                system_status.Text = "OFF";
                dac_on.BackColor = Color.LightGray;
                dac_off.BackColor = Color.Red;
                system_status.BackColor = Color.LightGray;
                system_on = false;
            }
            else
            {
                dac_off.BackColor = Color.LightGray;
                dac_on.BackColor = Color.LightGreen;
                system_status.BackColor = Color.LightGreen;
                if (tx_fiber_data_src_adc.Checked == true)
                {
                    system_status.Text = "ARMED";

                }
                else if (tx_fiber_data_src_dds.Checked == true)
                {
                    system_status.Text = "ARMED";
                }

                system_on = true;
            }

            dds_reg90.Text = ssh_ReadReg("0x83000090");
            dds_reg94.Text = ssh_ReadReg("0x83000094");
            dds_reg98.Text = ssh_ReadReg("0x83000098");
            dds_reg9C.Text = ssh_ReadReg("0x8300009C");

            dds_reg4_90.Text = ssh_ReadReg("0x84000090");
            dds_reg4_94.Text = ssh_ReadReg("0x84000094");
            dds_reg4_98.Text = ssh_ReadReg("0x84000098");
            dds_reg4_9C.Text = ssh_ReadReg("0x8400009C");
        }

        private void dds_count_reset_Click(object sender, EventArgs e)
        {
            dds_reg0C.Text = ssh_ReadReg("0x8300000C");
            uint cntrl_reg0C = (Convert.ToUInt32(dds_reg0C.Text, 16));
            cntrl_reg0C |= 0x1;   // bit0 - cnt_reset
            ssh_WriteReg("0x8300000C", "0x" + cntrl_reg0C.ToString("X8"));
            dds_reg0C.Text = ssh_ReadReg("0x8300000C");

            cntrl_reg0C = (Convert.ToUInt32(dds_reg0C.Text, 16));
            cntrl_reg0C &= 0xFFFF_FFFE;  // bit0 - clear bit0
            ssh_WriteReg("0x8300000C", "0x" + cntrl_reg0C.ToString("X8"));
            dds_reg0C.Text = ssh_ReadReg("0x8300000C");

            dds_reg4_0C.Text = ssh_ReadReg("0x8400000C");
            cntrl_reg0C = (Convert.ToUInt32(dds_reg4_0C.Text, 16));
            cntrl_reg0C |= 0x1;   // bit0 - cnt_reset
            ssh_WriteReg("0x8400000C", "0x" + cntrl_reg0C.ToString("X8"));
            dds_reg4_0C.Text = ssh_ReadReg("0x8400000C");

            cntrl_reg0C = (Convert.ToUInt32(dds_reg4_0C.Text, 16));
            cntrl_reg0C &= 0xFFFF_FFFE;  // bit0 - clear bit0
            ssh_WriteReg("0x8400000C", "0x" + cntrl_reg0C.ToString("X8"));
            dds_reg4_0C.Text = ssh_ReadReg("0x8400000C");
        }

        private void dac_data_src_dds_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FCFF;  // bit9:8 - clear bits
            cntrl_reg28 |= 0x200;   // bit9:8 = 10 <= DDS

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

        private void tx_fiber_adcs_src_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFEFF_FFFF;  // bit24 = 0 ADCs

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

        private void tx_fiber_dds_src_Click(object sender, EventArgs e)
        {
            reg_0x28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg_0x28.Text, 16));
            cntrl_reg28 &= 0xFEFF_FFFF;  // bit24 - clear bit
            cntrl_reg28 |= 0x0100_0000;   // bit24 = 1 <= DDS

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

        private void read_counters_Click(object sender, EventArgs e)
        {
            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_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");

        }
    }
}
