private void button23_Click(object sender, EventArgs e)
        {
            byte[]     bytes = FDDProcessing.HexToBytes(tbBIN.Text);
            ushort     datacrcchk;
            Crc16Ccitt crc = new Crc16Ccitt(InitialCrcValue.NonZero1);

            datacrcchk = crc.ComputeChecksum(bytes);
            tbTest.AppendText("CRC: " + datacrcchk.ToString("X4") + "\r\n");
        }
        private void AsciiCrcCheckBtn_Click(object sender, EventArgs e)
        {
            byte[] bytebuf = Encoding.ASCII.GetBytes(tbBIN.Text);

            ushort     datacrcchk;
            Crc16Ccitt crc = new Crc16Ccitt(InitialCrcValue.NonZero1);

            datacrcchk = crc.ComputeChecksum(bytebuf);
            tbTest.AppendText("CRC: " + datacrcchk.ToString("X4") + "\r\n");
        }
        public void CopySectorToBlue()
        {
            int i;
            int indexS1; //indexS2;
            int offset = 4;
            int diskoffset;
            //int x, y;
            //int bsbyte;
            int track, sectornr;
            int datacrc;
            //int processing.sectorspertrack;
            int threadid;

            switch ((int)processing.diskformat)
            {
            case 0:
                return;

            case 1:
                offset = 0;
                break;

            case 2:
                offset = 0;
                break;

            case 3:
                offset = 4;
                break;

            case 4:
                offset = 4;
                break;

            case 5:
                offset = 4;
                break;
            }

            if (BadSectorListBox.SelectedIndices.Count == 1)
            {
                indexS1  = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).id;
                threadid = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).threadid;

                // Copy sector from BadSectors to disk array
                if (BSBlueSectormapRadio.Checked && BSRedFromlistRadio.Checked)
                {
                    textBoxReceived.AppendText("Copy single sector to disk array.");
                    track    = processing.sectordata2[indexS1].track;
                    sectornr = processing.sectordata2[indexS1].sector;
                    datacrc  = processing.sectordata2[indexS1].crc;
                    //processing.sectorspertrack = 9;

                    //(tracknr * processing.sectorspertrack * 512 * 2) + (headnr * processing.sectorspertrack * 512) + (sectornr * 512);
                    diskoffset = track * processing.sectorspertrack * 512 + sectornr * 512;

                    for (i = 0; i < 512; i++)
                    {
                        processing.disk[diskoffset + i] = processing.sectordata2[indexS1].sectorbytes[i + offset];
                    }
                }

                // Copy sector from BadSectors to temporary sector buffer
                if (BlueTempRadio.Checked && BSRedFromlistRadio.Checked)
                {
                    textBoxReceived.AppendText("Copy full sector to Temp.");
                    track    = processing.sectordata2[indexS1].track;
                    sectornr = processing.sectordata2[indexS1].sector;
                    datacrc  = processing.sectordata2[indexS1].crc;

                    //processing.sectorspertrack = 9;

                    // I combined tracks and head to simplify stuff
                    // My track = tracks * 2 + headnr
                    // track 10 head 1 is 21
                    diskoffset = track * processing.sectorspertrack * 512 + sectornr * 512;

                    for (i = 0; i < 518; i++)
                    {
                        TempSector[i] = processing.sectordata2[indexS1].sectorbytes[i];
                    }

                    //Check crc
                    ushort     datacrcchk;
                    Crc16Ccitt crc = new Crc16Ccitt(InitialCrcValue.NonZero1);
                    datacrcchk             = crc.ComputeChecksum(TempSector);
                    BlueCrcCheckLabel.Text = "Crc: " + datacrcchk.ToString("X2");

                    processing.sectordata2[indexS1].crc = datacrcchk;
                }

                // Copy sector from temporary sector buffer to disk array
                if (BlueTempRadio.Checked && BSRedTempRadio.Checked)
                {
                    textBoxReceived.AppendText("Copy full sector to Temp.");
                    track    = processing.sectordata2[indexS1].track;
                    sectornr = processing.sectordata2[indexS1].sector;
                    datacrc  = processing.sectordata2[indexS1].crc;

                    //processing.sectorspertrack = 9;

                    //(tracknr * processing.sectorspertrack * 512 * 2) + (headnr * processing.sectorspertrack * 512) + (sectornr * 512);
                    diskoffset = track * processing.sectorspertrack * 512 + sectornr * 512;

                    for (i = 0; i < 512; i++)
                    {
                        processing.disk[diskoffset + i] = TempSector[i + offset];
                    }

                    //Do crc check
                    ushort     datacrcchk;
                    Crc16Ccitt crc = new Crc16Ccitt(InitialCrcValue.NonZero1);
                    datacrcchk             = crc.ComputeChecksum(TempSector);
                    BlueCrcCheckLabel.Text = "Crc: " + datacrcchk.ToString("X2");

                    processing.sectordata2[indexS1].crc = datacrcchk;
                }
            }
        }
        public void BadSectorPanelClick()
        {
            int indexS1;//, indexS2;
            int offset = 4;
            int diskoffset;
            int x, y;
            int bsbyte;
            int track, sectornr;
            int datacrc;

            int i;
            int threadid;

            BadSectorTooltipPos = BadSectorPanel.PointToClient(Cursor.Position);

            if (BadSectorListBox.SelectedIndices.Count >= 1)
            {
                indexS1  = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).id;
                threadid = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).threadid;

                int sectorlength = processing.sectordata2[indexS1].sectorlength;
                if (sectorlength < 512)
                {
                    tbreceived.Append("sector length is less than 512 bytes!!");
                    return;
                }

                if (ECMFMcheckBox.Checked)
                {
                    int w = 13;
                    int h = 8;
                    int lengthmfm;
                    int mfmoffset2 = 0;

                    switch ((int)processing.diskformat)
                    {
                    case 0:
                        return;

                    case 1:     //AmigaDos
                        offset    = -48;
                        lengthmfm = 8704;
                        break;

                    case 2:    //diskspare
                        offset    = -16;
                        lengthmfm = 8320;
                        break;

                    case 3:    //pcdd
                        offset     = -712;
                        lengthmfm  = 10464;
                        mfmoffset2 = -712;
                        break;

                    case 4:    //pchd
                        offset     = -712;
                        lengthmfm  = 10464;
                        mfmoffset2 = -712;
                        break;

                    case 5:    //pc2m
                        offset     = -712;
                        lengthmfm  = 10464;
                        mfmoffset2 = -712;
                        break;
                    }
                    //if (f == 0.0f) f = 1;
                    x = ((BadSectorTooltipPos.X) / w);
                    y = (int)(BadSectorTooltipPos.Y / h);
                    //int offset;


                    int mfmoffset = processing.sectordata2[indexS1].MarkerPositions;
                    bsbyte = ((y * 40 + x) * 8) + offset;
                    MFMByteStartUpDown.Value = ((y * 40 + x) * 8) + mfmoffset2;
                    int indexcnt = 0;
                    if (bsbyte > 0)
                    {
                        for (i = 0; i < bsbyte; i++)
                        {
                            if (processing.mfms[processing.sectordata2[indexS1].threadid][i + mfmoffset] == 1)
                            {
                                indexcnt++;
                            }
                        }
                    }
                    else
                    {
                        for (i = bsbyte; i < 0; i++)
                        {
                            if (processing.mfms[processing.sectordata2[indexS1].threadid][i + mfmoffset] == 1)
                            {
                                indexcnt--;
                            }
                        }
                    }
                    tbreceived.Append("index:" + indexcnt + "\r\n");
                    ScatterMinTrackBar.Value = indexcnt;
                    ScatterMaxTrackBar.Value = indexcnt + 14;
                    updateECInterface();
                }
                else
                {
                    int f = sectorlength / 512;

                    x = ((BadSectorTooltipPos.X) / 16);
                    y = (int)((BadSectorTooltipPos.Y) / (16 / f));

                    bsbyte = y * 32 + x;
                    // Temporary decouple event handler
                    byteinsector         = bsbyte;
                    BSEditByteLabel.Text = "Byte: " + bsbyte;

                    // Zoom in scatterplot
                    int indexcnt  = 0;
                    int mfmoffset = processing.sectordata2[indexS1].MarkerPositions;
                    // First find the period index
                    for (i = 0; i < (bsbyte + 4) * 16; i++)
                    {
                        if (processing.mfms[processing.sectordata2[indexS1].threadid][i + mfmoffset] == 1)
                        {
                            indexcnt++;
                        }
                    }
                    tbreceived.Append("index:" + indexcnt + "\r\n");
                    ScatterMinTrackBar.Value = indexcnt;
                    ScatterMaxTrackBar.Value = indexcnt + 14;
                    updateECInterface();
                    if ((int)BluetoRedByteCopyToolBtn.Tag == 1)
                    {
                        // Copy single byte from BadSectors to disk array
                        if (BSBlueSectormapRadio.Checked)
                        {
                            textBoxReceived.AppendText("Copy byte to disk array.");
                            track    = processing.sectordata2[indexS1].track;
                            sectornr = processing.sectordata2[indexS1].sector;
                            datacrc  = processing.sectordata2[indexS1].crc;

                            processing.sectorspertrack = 9;

                            //(tracknr * processing.sectorspertrack * 512 * 2) + (headnr * processing.sectorspertrack * 512) + (sectornr * 512);
                            diskoffset = track * processing.sectorspertrack * 512 + sectornr * 512;
                            processing.disk[diskoffset] = processing.sectordata2[indexS1].sectorbytes[bsbyte + offset];
                        }

                        //Copy byte from BadSectors to TempSector
                        if (BlueTempRadio.Checked)
                        {
                            textBoxReceived.AppendText("Copy byte to Temp.");
                            track    = processing.sectordata2[indexS1].track;
                            sectornr = processing.sectordata2[indexS1].sector;
                            datacrc  = processing.sectordata2[indexS1].crc;

                            processing.sectorspertrack = 9;

                            //(tracknr * processing.sectorspertrack * 512 * 2) + (headnr * processing.sectorspertrack * 512) + (sectornr * 512);
                            diskoffset = track * processing.sectorspertrack * 512 + sectornr * 512;
                            TempSector[bsbyte + offset] = processing.sectordata2[indexS1].sectorbytes[bsbyte + offset];
                        }

                        //Check crc
                        ushort     datacrcchk;
                        Crc16Ccitt crc = new Crc16Ccitt(InitialCrcValue.NonZero1);
                        datacrcchk             = crc.ComputeChecksum(TempSector);
                        BlueCrcCheckLabel.Text = "Crc: " + datacrcchk.ToString("X2");

                        processing.sectordata2[indexS1].crc = datacrcchk;
                    }
                }
            }
        }