/*
    CaPriCe Forever - Amstrad CPC 464/664/6128 emulator
    Copyright (C) 2014-2015 by Frédéric Coste

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*****************************************************************************
 *
 *   z80dasm.c
 *   Portable Z80 disassembler
 *
 *   Copyright Juergen Buchmueller, all rights reserved.
 *
 *   - This source code is released as freeware for non-commercial purposes.
 *   - You are free to use and redistribute this code in modified or
 *     unmodified form, provided you list me in the credits.
 *   - If you modify this source code, you must add a notice to each modified
 *     source file that it has been changed.  If you're a nice person, you
 *     will clearly mark each change too.  :)
 *   - If you wish to use this for commercial purposes, please contact me at
 *     pullmoll@t-online.de
 *   - The author of this copywritten work reserves the right to change the
 *     terms of its usage and license at any time, including retroactively
 *   - This entire notice must remain in the source code.
 *
 *****************************************************************************/


#include <vcl.h>
#pragma hdrstop
#include "TFormZ80Editor.h"
#include "TFormMain.h"
#include "TFormDebugger.h"
#include "TFormMemoryEditor.h"
#include "..\..\Common\Z80DASM\z80dasm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


//=================================
// Options

//=================================

static const AnsiString cTitle = "Z80 CPU editor";
static const AnsiString cTitlePaused  = " - [PAUSED]";


TFormZ80Editor *FormZ80Editor;

//---------------------------------------------------------------------------
__fastcall TFormZ80Editor::TFormZ80Editor(TComponent* Owner)
  : TForm(Owner)
{
  mSettingsP = NULL;
  mNextInstructions = NULL;
  mPaused = false;
	mMinAddress = 0;
	mMaxAddress = 65535;
  mTopAddress = mMinAddress;

	mFormEditValue = new TFormEditValue(this);

	PanelTopAddress->Caption = AnsiString::IntToHex(mTopAddress, 4);

  // Initialise Theme
  Font->Name = "Amstrad CPC464 Regular";
  Font->Size = 6;

  for (int Loop=0; Loop < ComponentCount; Loop++)
  {
    if ( (Components[Loop]->Name.Pos("PanelFormat") == 1)
				 || (Components[Loop]->Name.Pos("PanelReg") == 1) )
    {
      ((TPanel*)Components[Loop])->Font->Color = clBlack;
    }
    else if (Components[Loop]->Name.Pos("Panel") == 1)
		{
			((TPanel*)Components[Loop])->Font->Color = clBlack;
		}
	}

	StringGridInstructions->Canvas->Font = Font;
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::FormDestroy(TObject *Sender)
{
  delete mFormEditValue;

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::FormShow(TObject *Sender)
{
  if (FormMain == NULL)
  {
    Close();
    return;
  }

	FormMain->OnZ80EditorFormShow();

	memset(&mZ80dasmParam, 0, sizeof(TZ80dasmParam));
	mZ80dasmParam.NativeCPC = FormMain->NativeCPC;
	mZ80dasmParam.ReadFunction = Engine_ReadBankedRead;

	StringGridInstructions->RowCount = StringGridInstructions->Height / StringGridInstructions->DefaultRowHeight;
  StringGridInstructions->ColWidths[0] = 50;
  StringGridInstructions->ColWidths[1] = 100;
  StringGridInstructions->ColWidths[3] = 80;
  StringGridInstructions->ColWidths[2] = StringGridInstructions->Width -
        (StringGridInstructions->ColWidths[0]+StringGridInstructions->ColWidths[1]+StringGridInstructions->ColWidths[3]);

  StringGridStack->RowCount = StringGridStack->Height / StringGridStack->DefaultRowHeight;
  StringGridStack->ColWidths[0] = 50;
  StringGridStack->ColWidths[1] = StringGridStack->Width - StringGridStack->ColWidths[0];

  // Subscribe to step by step event
  FormMain->SubscribeToCPCEvents(OnStepByStepEvent);

  delete [] mNextInstructions;
  mNextInstructions = new tULong[StringGridInstructions->RowCount];

  UpdateRegisters(FormMain->NativeCPC);
  UpdateFlags(FormMain->NativeCPC);
  UpdateInstructions(FormMain->NativeCPC, false);
  UpdateStack(FormMain->NativeCPC);
  UpdateInterrupt(FormMain->NativeCPC);

  mPaused = FormMain->NativeCPC->paused;
  Caption = Title;

	UpdateRegistersEdition(mPaused);

  if (mSettingsP)
  {
    mSettingsP->Z80FormVisible = true;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::FormHide(TObject *Sender)
{
  // Unsubscribe to step by step event
  if (FormMain != NULL)
  {
    FormMain->UnsubscribeToCPCEvents(OnStepByStepEvent);
    FormMain->OnZ80EditorFormHide();
	}

  delete [] mNextInstructions;
  mNextInstructions = NULL;

  if (mSettingsP)
  {
    mSettingsP->Z80FormVisible = false;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


AnsiString __fastcall TFormZ80Editor::GetTitle(void)
{
  AnsiString Title = cTitle;
	if (mPaused == true) Title += cTitlePaused;

 return Title;
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::SetSettings(TSettings* Value)
{
  mSettingsP = Value;
  mFormEditValue->Settings = mSettingsP;

  if (    (mSettingsP->Z80FormLeft != 0)
       || (mSettingsP->Z80FormTop != 0)
       || (mSettingsP->Z80FormWidth != 0)
       || (mSettingsP->Z80FormHeight != 0) )
  {
    Position = poDesigned;
    Left = mSettingsP->Z80FormLeft;
    Top = mSettingsP->Z80FormTop;
    Width = mSettingsP->Z80FormWidth;
    Height = mSettingsP->Z80FormHeight;
  }

  ShowHint = mSettingsP->ShowHints;

  // Apply theme
  Color = mSettingsP->ThemeBackgroundColor;
  Font->Color = mSettingsP->ThemeText1Color;
  for (int Loop=0; Loop < ComponentCount; Loop++)
  {
    if (Components[Loop]->Name.Pos("Label") == 1)
    {
      ((TLabel*)Components[Loop])->Font->Color = mSettingsP->ThemeText1Color;
    }
    else if (Components[Loop]->Name.Pos("GroupBox") == 1)
    {
      ((TGroupBox*)Components[Loop])->Font->Color = mSettingsP->ThemeText2Color;
    }
    else if (Components[Loop]->Name.Pos("PanelButton") == 1)
    {
      ((TPanel*)Components[Loop])->Color = mSettingsP->ThemeButtonColor;
      ((TPanel*)Components[Loop])->Font->Color = mSettingsP->ThemeButtonTextColor;
    }
    else if ( (Components[Loop]->Name.Pos("PanelIFF") == 1)
              || (Components[Loop]->Name.Pos("PanelPrev") == 1) )
    {
      ((TPanel*)Components[Loop])->Color = mSettingsP->ThemeButtonColor;
      ((TPanel*)Components[Loop])->Font->Color = mSettingsP->ThemeText1Color;
    }

		// Refresh display
		if ( (Visible == true) && (((TControl*)Components[Loop])->Visible == true) )
		{
			((TControl*)Components[Loop])->Invalidate();
		}
	}

	if (mSettingsP->HideAll == false)
  {
    Visible = mSettingsP->Z80FormVisible;
  }
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::FormResize(TObject *Sender)
{
  if ( (Visible == true) && (mSettingsP) )
  {
    mSettingsP->Z80FormWidth = Width;
    mSettingsP->Z80FormHeight = Height;
  }

	StringGridInstructions->RowCount = StringGridInstructions->Height / StringGridInstructions->DefaultRowHeight;
  StringGridInstructions->ColWidths[2] = StringGridInstructions->Width -
				(StringGridInstructions->ColWidths[0]+StringGridInstructions->ColWidths[1]+StringGridInstructions->ColWidths[3]);
	ScrollBarAddress->LargeChange = StringGridInstructions->RowCount;

  delete [] mNextInstructions;
  mNextInstructions = new tULong[StringGridInstructions->RowCount];

  StringGridStack->RowCount = StringGridStack->Height / StringGridStack->DefaultRowHeight;

  if (FormMain->NativeCPC != NULL)
  {
    UpdateRegisters(FormMain->NativeCPC);
    UpdateFlags(FormMain->NativeCPC);
    UpdateInstructions(FormMain->NativeCPC, false);
    UpdateStack(FormMain->NativeCPC);
    UpdateInterrupt(FormMain->NativeCPC);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::WMMove(TMessage &Message)
{
  if ( (Visible == true) && (mSettingsP) )
  {
    mSettingsP->Z80FormLeft = Left;
    mSettingsP->Z80FormTop = Top;
  }

	NOT_USED(Message);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::PanelFormatRegAFClick(TObject *Sender)
{
  if (PanelFormatRegAF->Caption.IsEmpty() == true)
  {
    PanelFormatRegAF->Caption = "&&";
    tULong CurrentValue = PanelRegA->Caption.ToInt();
    PanelRegA->Caption = AnsiString::IntToHex(CurrentValue, 2);
    CurrentValue = PanelRegF->Caption.ToInt();
    PanelRegF->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegAF->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegA->Caption);
    PanelRegA->Caption = CurrentValue;
    CurrentValue = StrToInt("0x" + PanelRegF->Caption);
    PanelRegF->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegBCClick(TObject *Sender)
{
  if (PanelFormatRegBC->Caption.IsEmpty() == true)
  {
    PanelFormatRegBC->Caption = "&&";
    tULong CurrentValue = PanelRegB->Caption.ToInt();
    PanelRegB->Caption = AnsiString::IntToHex(CurrentValue, 2);
    CurrentValue = PanelRegC->Caption.ToInt();
    PanelRegC->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegBC->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegB->Caption);
    PanelRegB->Caption = CurrentValue;
    CurrentValue = StrToInt("0x" + PanelRegC->Caption);
    PanelRegC->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegDEClick(TObject *Sender)
{
  if (PanelFormatRegDE->Caption.IsEmpty() == true)
  {
    PanelFormatRegDE->Caption = "&&";
    tULong CurrentValue = PanelRegD->Caption.ToInt();
    PanelRegD->Caption = AnsiString::IntToHex(CurrentValue, 2);
    CurrentValue = PanelRegE->Caption.ToInt();
    PanelRegE->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegDE->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegD->Caption);
    PanelRegD->Caption = CurrentValue;
    CurrentValue = StrToInt("0x" + PanelRegE->Caption);
    PanelRegE->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegHLClick(TObject *Sender)
{
  if (PanelFormatRegHL->Caption.IsEmpty() == true)
  {
    PanelFormatRegHL->Caption = "&&";
    tULong CurrentValue = PanelRegH->Caption.ToInt();
    PanelRegH->Caption = AnsiString::IntToHex(CurrentValue, 2);
    CurrentValue = PanelRegL->Caption.ToInt();
    PanelRegL->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegHL->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegH->Caption);
    PanelRegH->Caption = CurrentValue;
    CurrentValue = StrToInt("0x" + PanelRegL->Caption);
    PanelRegL->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegAFAltClick(
      TObject *Sender)
{
  if (PanelFormatRegAFAlt->Caption.IsEmpty() == true)
  {
    PanelFormatRegAFAlt->Caption = "&&";
    tULong CurrentValue = PanelRegAAlt->Caption.ToInt();
    PanelRegAAlt->Caption = AnsiString::IntToHex(CurrentValue, 2);
    CurrentValue = PanelRegFAlt->Caption.ToInt();
    PanelRegFAlt->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegAFAlt->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegAAlt->Caption);
    PanelRegAAlt->Caption = CurrentValue;
    CurrentValue = StrToInt("0x" + PanelRegFAlt->Caption);
    PanelRegFAlt->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegBCAltClick(
      TObject *Sender)
{
  if (PanelFormatRegBCAlt->Caption.IsEmpty() == true)
  {
    PanelFormatRegBCAlt->Caption = "&&";
    tULong CurrentValue = PanelRegBAlt->Caption.ToInt();
    PanelRegBAlt->Caption = AnsiString::IntToHex(CurrentValue, 2);
    CurrentValue = PanelRegCAlt->Caption.ToInt();
    PanelRegCAlt->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegBCAlt->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegBAlt->Caption);
    PanelRegBAlt->Caption = CurrentValue;
    CurrentValue = StrToInt("0x" + PanelRegCAlt->Caption);
    PanelRegCAlt->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegDEAltClick(
      TObject *Sender)
{
  if (PanelFormatRegDEAlt->Caption.IsEmpty() == true)
  {
    PanelFormatRegDEAlt->Caption = "&&";
    tULong CurrentValue = PanelRegDAlt->Caption.ToInt();
    PanelRegDAlt->Caption = AnsiString::IntToHex(CurrentValue, 2);
    CurrentValue = PanelRegEAlt->Caption.ToInt();
    PanelRegEAlt->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegDEAlt->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegDAlt->Caption);
    PanelRegDAlt->Caption = CurrentValue;
    CurrentValue = StrToInt("0x" + PanelRegEAlt->Caption);
    PanelRegEAlt->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegHLAltClick(
      TObject *Sender)
{
  if (PanelFormatRegHLAlt->Caption.IsEmpty() == true)
  {
    PanelFormatRegHLAlt->Caption = "&&";
    tULong CurrentValue = PanelRegHAlt->Caption.ToInt();
    PanelRegHAlt->Caption = AnsiString::IntToHex(CurrentValue, 2);
    CurrentValue = PanelRegLAlt->Caption.ToInt();
    PanelRegLAlt->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegHLAlt->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegHAlt->Caption);
    PanelRegHAlt->Caption = CurrentValue;
    CurrentValue = StrToInt("0x" + PanelRegLAlt->Caption);
    PanelRegLAlt->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegIXClick(TObject *Sender)
{
  if (PanelFormatRegIX->Caption.IsEmpty() == true)
  {
    PanelFormatRegIX->Caption = "&&";
    tULong CurrentValue = PanelRegIX->Caption.ToInt();
    PanelRegIX->Caption = AnsiString::IntToHex(CurrentValue, 4);
  }
  else
  {
    PanelFormatRegIX->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegIX->Caption);
    PanelRegIX->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegIYClick(TObject *Sender)
{
  if (PanelFormatRegIY->Caption.IsEmpty() == true)
  {
    PanelFormatRegIY->Caption = "&&";
    tULong CurrentValue = PanelRegIY->Caption.ToInt();
    PanelRegIY->Caption = AnsiString::IntToHex(CurrentValue, 4);
  }
  else
  {
    PanelFormatRegIY->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegIY->Caption);
    PanelRegIY->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegSPClick(TObject *Sender)
{
  if (PanelFormatRegSP->Caption.IsEmpty() == true)
  {
    PanelFormatRegSP->Caption = "&&";
    tULong CurrentValue = PanelRegSP->Caption.ToInt();
    PanelRegSP->Caption = AnsiString::IntToHex(CurrentValue, 4);
  }
  else
  {
    PanelFormatRegSP->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegSP->Caption);
    PanelRegSP->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegPCClick(TObject *Sender)
{
  if (PanelFormatRegPC->Caption.IsEmpty() == true)
  {
    PanelFormatRegPC->Caption = "&&";
    tULong CurrentValue = PanelRegPC->Caption.ToInt();
    PanelRegPC->Caption = AnsiString::IntToHex(CurrentValue, 4);
  }
  else
  {
    PanelFormatRegPC->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegPC->Caption);
    PanelRegPC->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegIClick(TObject *Sender)
{
  if (PanelFormatRegI->Caption.IsEmpty() == true)
  {
    PanelFormatRegI->Caption = "&&";
    tULong CurrentValue = PanelRegI->Caption.ToInt();
    PanelRegI->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegI->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegI->Caption);
    PanelRegI->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelFormatRegRClick(TObject *Sender)
{
  if (PanelFormatRegR->Caption.IsEmpty() == true)
  {
    PanelFormatRegR->Caption = "&&";
    tULong CurrentValue = PanelRegR->Caption.ToInt();
    PanelRegR->Caption = AnsiString::IntToHex(CurrentValue, 2);
  }
  else
  {
    PanelFormatRegR->Caption = "";
    tULong CurrentValue = StrToInt("0x" + PanelRegR->Caption);
    PanelRegR->Caption = CurrentValue;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::ComboBoxFlagChange(TObject *Sender)
{
TComboBox* ComboBox = (TComboBox*)Sender;
tUChar mask = (tUChar)ComboBox->Tag;

  // Update flags
  FormMain->NativeCPC->Z80->Regs.AF.UChar.l &= ~mask;
  if (ComboBox->ItemIndex == 1)
  {
    FormMain->NativeCPC->Z80->Regs.AF.UChar.l |= mask;
  }

  UpdateRegisters(FormMain->NativeCPC);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::ComboBoxFlag0DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ComboBoxFlag0->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Brush->Color = clHighlight;
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = clWhite;
    Canvas->Font->Color = clBlack;
  }

  // Draw cell
  Canvas->FillRect(Rect);
  AnsiString Text = ComboBoxFlag0->Items->Strings[Index];
  int TextHeight = Canvas->TextHeight(Text);
  int TextPos = (ComboBoxFlag0->ItemHeight - TextHeight) / 2;
  Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
  if (State.Contains(odFocused))
  {
    Canvas->DrawFocusRect(Rect);
  }

  NOT_USED(Control);
  NOT_USED(Index);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::ComboBoxFlag1DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ComboBoxFlag1->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Brush->Color = clHighlight;
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = clWhite;
    Canvas->Font->Color = clBlack;
  }

  // Draw cell
  Canvas->FillRect(Rect);
  AnsiString Text = ComboBoxFlag1->Items->Strings[Index];
  int TextHeight = Canvas->TextHeight(Text);
  int TextPos = (ComboBoxFlag1->ItemHeight - TextHeight) / 2;
  Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
  if (State.Contains(odFocused))
  {
    Canvas->DrawFocusRect(Rect);
  }

  NOT_USED(Control);
  NOT_USED(Index);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::ComboBoxFlag2DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ComboBoxFlag2->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Brush->Color = clHighlight;
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = clWhite;
    Canvas->Font->Color = clBlack;
  }

  // Draw cell
  Canvas->FillRect(Rect);
  AnsiString Text = ComboBoxFlag2->Items->Strings[Index];
  int TextHeight = Canvas->TextHeight(Text);
  int TextPos = (ComboBoxFlag2->ItemHeight - TextHeight) / 2;
  Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
  if (State.Contains(odFocused))
  {
    Canvas->DrawFocusRect(Rect);
  }

  NOT_USED(Control);
  NOT_USED(Index);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::ComboBoxFlag3DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ComboBoxFlag3->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Brush->Color = clHighlight;
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = clWhite;
    Canvas->Font->Color = clBlack;
  }

  // Draw cell
  Canvas->FillRect(Rect);
  AnsiString Text = ComboBoxFlag3->Items->Strings[Index];
  int TextHeight = Canvas->TextHeight(Text);
  int TextPos = (ComboBoxFlag3->ItemHeight - TextHeight) / 2;
  Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
  if (State.Contains(odFocused))
  {
    Canvas->DrawFocusRect(Rect);
  }

  NOT_USED(Control);
  NOT_USED(Index);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::ComboBoxFlag4DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ComboBoxFlag4->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Brush->Color = clHighlight;
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = clWhite;
    Canvas->Font->Color = clBlack;
  }

  // Draw cell
  Canvas->FillRect(Rect);
  AnsiString Text = ComboBoxFlag4->Items->Strings[Index];
  int TextHeight = Canvas->TextHeight(Text);
  int TextPos = (ComboBoxFlag4->ItemHeight - TextHeight) / 2;
  Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
  if (State.Contains(odFocused))
  {
    Canvas->DrawFocusRect(Rect);
  }

  NOT_USED(Control);
  NOT_USED(Index);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::ComboBoxFlag5DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ComboBoxFlag5->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Brush->Color = clHighlight;
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = clWhite;
    Canvas->Font->Color = clBlack;
  }

  // Draw cell
  Canvas->FillRect(Rect);
  AnsiString Text = ComboBoxFlag5->Items->Strings[Index];
  int TextHeight = Canvas->TextHeight(Text);
  int TextPos = (ComboBoxFlag5->ItemHeight - TextHeight) / 2;
  Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
  if (State.Contains(odFocused))
  {
    Canvas->DrawFocusRect(Rect);
  }

  NOT_USED(Control);
  NOT_USED(Index);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::ComboBoxFlag6DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ComboBoxFlag6->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Brush->Color = clHighlight;
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = clWhite;
    Canvas->Font->Color = clBlack;
  }

  // Draw cell
  Canvas->FillRect(Rect);
  AnsiString Text = ComboBoxFlag6->Items->Strings[Index];
  int TextHeight = Canvas->TextHeight(Text);
  int TextPos = (ComboBoxFlag6->ItemHeight - TextHeight) / 2;
  Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
  if (State.Contains(odFocused))
  {
    Canvas->DrawFocusRect(Rect);
  }

  NOT_USED(Control);
  NOT_USED(Index);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::ComboBoxFlag7DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ComboBoxFlag7->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Brush->Color = clHighlight;
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = clWhite;
    Canvas->Font->Color = clBlack;
  }

  // Draw cell
  Canvas->FillRect(Rect);
  AnsiString Text = ComboBoxFlag7->Items->Strings[Index];
  int TextHeight = Canvas->TextHeight(Text);
  int TextPos = (ComboBoxFlag7->ItemHeight - TextHeight) / 2;
  Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
  if (State.Contains(odFocused))
  {
    Canvas->DrawFocusRect(Rect);
  }

  NOT_USED(Control);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::PanelRegAClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.AF.UChar.h;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegAF->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegA->Caption = (PanelFormatRegAF->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegFClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.AF.UChar.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegAF->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegF->Caption = (PanelFormatRegAF->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
    UpdateFlags(FormMain->NativeCPC);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegBClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.BC.UChar.h;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegBC->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegB->Caption = (PanelFormatRegBC->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegCClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.BC.UChar.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegBC->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegC->Caption = (PanelFormatRegBC->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegDClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.DE.UChar.h;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegDE->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegD->Caption = (PanelFormatRegDE->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegEClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.DE.UChar.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegDE->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegE->Caption = (PanelFormatRegDE->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegHClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.HL.UChar.h;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegHL->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegH->Caption = (PanelFormatRegHL->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegLClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.HL.UChar.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegHL->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegL->Caption = (PanelFormatRegHL->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegAAltClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.AFx.UChar.h;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegAFAlt->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegAAlt->Caption = (PanelFormatRegAFAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegFAltClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.AFx.UChar.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegAFAlt->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegFAlt->Caption = (PanelFormatRegAFAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegBAltClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.BCx.UChar.h;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegBCAlt->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegBAlt->Caption = (PanelFormatRegBCAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegCAltClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.BCx.UChar.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegBCAlt->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegCAlt->Caption = (PanelFormatRegBCAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegDAltClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.DEx.UChar.h;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegDEAlt->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegDAlt->Caption = (PanelFormatRegDEAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegEAltClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.DEx.UChar.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegDEAlt->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegEAlt->Caption = (PanelFormatRegDEAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegHAltClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.HLx.UChar.h;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegHLAlt->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegHAlt->Caption = (PanelFormatRegHLAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegLAltClick(TObject *Sender)
{
tUChar* Regs = &FormMain->NativeCPC->Z80->Regs.HLx.UChar.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegHLAlt->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegLAlt->Caption = (PanelFormatRegHLAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegIXClick(TObject *Sender)
{
tUShort* Regs = &FormMain->NativeCPC->Z80->Regs.IX.UShort.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUShort)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegIX->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = mMinAddress;
	mFormEditValue->MaxValue = mMaxAddress;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegIX->Caption = (PanelFormatRegIX->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 4) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegIYClick(TObject *Sender)
{
tUShort* Regs = &FormMain->NativeCPC->Z80->Regs.IY.UShort.l;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUShort)*Regs;
  mFormEditValue->Hexadecimal = PanelFormatRegIY->Caption.IsEmpty() == true ? false : true;
  mFormEditValue->MinValue = mMinAddress;
  mFormEditValue->MaxValue = mMaxAddress;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegIY->Caption = (PanelFormatRegIY->Caption.IsEmpty() == false) ? AnsiString::IntToHex(*Regs, 4) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegSPClick(TObject *Sender)
{
tUShort* Regs = &FormMain->NativeCPC->Z80->Regs.SP.UShort.l;
bool ShowHexa = PanelFormatRegSP->Caption.IsEmpty() == true ? false : true;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUShort)*Regs;
  mFormEditValue->Hexadecimal = ShowHexa;
  mFormEditValue->MinValue = mMinAddress;
	mFormEditValue->MaxValue = mMaxAddress;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegSP->Caption = (ShowHexa == true) ? AnsiString::IntToHex(*Regs, 4) : AnsiString(*Regs);

    UpdateStack(FormMain->NativeCPC);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegPCClick(TObject *Sender)
{
tUShort* Regs = &FormMain->NativeCPC->Z80->Regs.PC.UShort.l;
bool ShowHexa = PanelFormatRegPC->Caption.IsEmpty() == true ? false : true;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUShort)*Regs;
  mFormEditValue->Hexadecimal = ShowHexa;
  mFormEditValue->MinValue = mMinAddress;
	mFormEditValue->MaxValue = mMaxAddress;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegPC->Caption = (ShowHexa == true) ? AnsiString::IntToHex(*Regs, 4) : AnsiString(*Regs);

    UpdateInstructions(FormMain->NativeCPC, true);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegIClick(TObject *Sender)
{
tULong* Regs = &FormMain->NativeCPC->Z80->Regs.I;
bool ShowHexa = PanelFormatRegI->Caption.IsEmpty() == true ? false : true;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = ShowHexa;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegI->Caption = (ShowHexa == true) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::PanelRegRClick(TObject *Sender)
{
tULong* Regs = &FormMain->NativeCPC->Z80->Regs.R;
bool ShowHexa = PanelFormatRegR->Caption.IsEmpty() == true ? false : true;

  mFormEditValue->Title = ((TPanel*)Sender)->Hint;
  mFormEditValue->CurrentValue = (tUChar)*Regs;
  mFormEditValue->Hexadecimal = ShowHexa;
  mFormEditValue->MinValue = 0;
  mFormEditValue->MaxValue = 255;
  if (mFormEditValue->ShowModal() == mrOk)
  {
    *Regs = mFormEditValue->NewValue;
    PanelRegR->Caption = (ShowHexa == true) ? AnsiString::IntToHex(*Regs, 2) : AnsiString(*Regs);
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::ComboBoxITModeChange(TObject *Sender)
{
  // Update interrupt mode
  FormMain->NativeCPC->Z80->Regs.IM = ComboBoxITMode->ItemIndex;

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::ComboBoxITModeDrawItem(TWinControl *Control,
          int Index, TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ComboBoxITMode->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Brush->Color = clHighlight;
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = clWhite;
    Canvas->Font->Color = clBlack;
  }

	// Draw cell
	Canvas->FillRect(Rect);
	AnsiString Text = ComboBoxITMode->Items->Strings[Index];
	int TextHeight = Canvas->TextHeight(Text);
	int TextPos = (ComboBoxITMode->ItemHeight - TextHeight) / 2;
	Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
	if (State.Contains(odFocused))
	{
		Canvas->DrawFocusRect(Rect);
	}

	NOT_USED(Control);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::UpdateRegisters(tNativeCPC* NativeCPC)
{
tZ80* Z80 = NativeCPC->Z80;

  PanelRegA->Caption = (PanelFormatRegAF->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_A, 2) : AnsiString(_A);
  PanelRegF->Caption = (PanelFormatRegAF->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_F, 2) : AnsiString(_F);
  PanelRegB->Caption = (PanelFormatRegBC->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_B, 2) : AnsiString(_B);
  PanelRegC->Caption = (PanelFormatRegBC->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_C, 2) : AnsiString(_C);
  PanelRegD->Caption = (PanelFormatRegDE->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_D, 2) : AnsiString(_D);
  PanelRegE->Caption = (PanelFormatRegDE->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_E, 2) : AnsiString(_E);
  PanelRegH->Caption = (PanelFormatRegHL->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_H, 2) : AnsiString(_H);
  PanelRegL->Caption = (PanelFormatRegHL->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_L, 2) : AnsiString(_L);

  PanelRegAAlt->Caption = (PanelFormatRegAFAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(Z80->Regs.AFx.UChar.h, 2) : AnsiString(Z80->Regs.AFx.UChar.h);
  PanelRegFAlt->Caption = (PanelFormatRegAFAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(Z80->Regs.AFx.UChar.l, 2) : AnsiString(Z80->Regs.AFx.UChar.l);
  PanelRegBAlt->Caption = (PanelFormatRegBCAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(Z80->Regs.BCx.UChar.h, 2) : AnsiString(Z80->Regs.BCx.UChar.h);
	PanelRegCAlt->Caption = (PanelFormatRegBCAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(Z80->Regs.BCx.UChar.l, 2) : AnsiString(Z80->Regs.BCx.UChar.l);
  PanelRegDAlt->Caption = (PanelFormatRegDEAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(Z80->Regs.DEx.UChar.h, 2) : AnsiString(Z80->Regs.DEx.UChar.h);
  PanelRegEAlt->Caption = (PanelFormatRegDEAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(Z80->Regs.DEx.UChar.l, 2) : AnsiString(Z80->Regs.DEx.UChar.l);
  PanelRegHAlt->Caption = (PanelFormatRegHLAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(Z80->Regs.HLx.UChar.h, 2) : AnsiString(Z80->Regs.HLx.UChar.h);
  PanelRegLAlt->Caption = (PanelFormatRegHLAlt->Caption.IsEmpty() == false) ? AnsiString::IntToHex(Z80->Regs.HLx.UChar.l, 2) : AnsiString(Z80->Regs.HLx.UChar.l);

  PanelRegIX->Caption = (PanelFormatRegIX->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_IX, 4) : AnsiString(_IX);
  PanelRegIY->Caption = (PanelFormatRegIY->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_IY, 4) : AnsiString(_IY);
  PanelRegSP->Caption = (PanelFormatRegSP->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_SP, 4) : AnsiString(_SP);
	PanelRegPC->Caption = (PanelFormatRegPC->Caption.IsEmpty() == false) ? AnsiString::IntToHex(_PC, 4) : AnsiString(_PC);

  PanelRegI->Caption = (PanelFormatRegI->Caption.IsEmpty() == false) ? AnsiString::IntToHex((tUChar)_I, 2) : AnsiString((tUChar)_I);
  PanelRegR->Caption = (PanelFormatRegR->Caption.IsEmpty() == false) ? AnsiString::IntToHex((tUChar)_R, 2) : AnsiString((tUChar)_R);

  PanelPrevPC->Caption = AnsiString::IntToHex(_PrevPC, 4);
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::UpdateFlags(tNativeCPC* NativeCPC)
{
tUChar flags = NativeCPC->Z80->Regs.AF.UChar.l;

  ComboBoxFlag0->ItemIndex = (flags & 0x01) ? 1 : 0;
  ComboBoxFlag1->ItemIndex = (flags & 0x02) ? 1 : 0;
  ComboBoxFlag2->ItemIndex = (flags & 0x04) ? 1 : 0;
	ComboBoxFlag3->ItemIndex = (flags & 0x08) ? 1 : 0;
	ComboBoxFlag4->ItemIndex = (flags & 0x10) ? 1 : 0;
	ComboBoxFlag5->ItemIndex = (flags & 0x20) ? 1 : 0;
	ComboBoxFlag6->ItemIndex = (flags & 0x40) ? 1 : 0;
	ComboBoxFlag7->ItemIndex = (flags & 0x80) ? 1 : 0;
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::UpdateInstructions(tNativeCPC* NativeCPC, bool SelectPC)
{
TZ80dasmParam Z80dasmParam;
tULong pc = NativeCPC->Z80->Regs.PC.ULong;
tULong addr = mTopAddress;

	memset(&Z80dasmParam, 0, sizeof(TZ80dasmParam));
	Z80dasmParam.NativeCPC = NativeCPC;
	Z80dasmParam.ReadFunction = Engine_ReadBankedRead;

	for (int Loop=0; Loop < StringGridInstructions->RowCount; Loop++)
	{
		bool ShowMemoryContent = false;
		StringGridInstructions->Cells[0][Loop] = AnsiString::IntToHex(addr,4) + ":";

		mNextInstructions[Loop] = addr;

		if (addr == pc)
		{
			ShowMemoryContent = true;
			if (SelectPC == true)
			{
				// Select instruction at PC
				StringGridInstructions->Row = Loop;
			}
		}

		if (addr <= mMaxAddress)
		{
			addr = Z80dasmDecodeInstruction(addr,
																			&Z80dasmParam);
			StringGridInstructions->Cells[1][Loop] = AnsiString(Z80dasmParam.MachineCode).UpperCase();
			StringGridInstructions->Cells[2][Loop] = AnsiString(Z80dasmParam.Instruction).UpperCase();
			if (ShowMemoryContent)
			{
				StringGridInstructions->Cells[3][Loop] = AnsiString(Z80dasmParam.MemoryContent).UpperCase();
			}
			else
			{
				StringGridInstructions->Cells[3][Loop] = "";
			}
		}
		else
		{
			StringGridInstructions->Rows[Loop]->Clear();
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::UpdateStack(tNativeCPC* NativeCPC)
{
tULong addr = NativeCPC->Z80->Regs.SP.UShort.l;

  for (int Loop=0; Loop < StringGridStack->RowCount; Loop++)
  {
    StringGridStack->Cells[0][Loop] = AnsiString::IntToHex(addr,4) + ":";

    tUShort word = Engine_ReadBankedRead(NativeCPC, addr++);
    word += Engine_ReadBankedRead(NativeCPC, addr++) << 8;

    StringGridStack->Cells[1][Loop] = AnsiString::IntToHex(word, 4);
  }

  TGridRect Rect;
  Rect.Top = Rect.Bottom = 0;
  Rect.Left = 0;
  Rect.Right = 1;
	StringGridStack->Selection = Rect;
}
//---------------------------------------------------------------------------
void __fastcall TFormZ80Editor::UpdateInterrupt(tNativeCPC* NativeCPC)
{
tZ80* Z80 = NativeCPC->Z80;

  PanelIFF1->Caption = Z80->Regs.IFF1 ? "1" : "0";
  PanelIFF2->Caption = Z80->Regs.IFF2 ? "1" : "0";
  ComboBoxITMode->ItemIndex = Z80->Regs.IM & 0x03;
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::UpdateRegistersEdition(bool Allow)
{
	if (Allow == false)
	{
		ActiveControl = NULL;
	}

	ComboBoxITMode->Enabled = Allow;

	PanelRegA->Enabled = Allow;
	PanelRegF->Enabled = Allow;
	PanelRegB->Enabled = Allow;
	PanelRegC->Enabled = Allow;
	PanelRegD->Enabled = Allow;
	PanelRegE->Enabled = Allow;
	PanelRegH->Enabled = Allow;
	PanelRegL->Enabled = Allow;

	PanelRegAAlt->Enabled = Allow;
	PanelRegFAlt->Enabled = Allow;
	PanelRegBAlt->Enabled = Allow;
	PanelRegCAlt->Enabled = Allow;
	PanelRegDAlt->Enabled = Allow;
	PanelRegEAlt->Enabled = Allow;
	PanelRegHAlt->Enabled = Allow;
	PanelRegLAlt->Enabled = Allow;

	PanelRegIX->Enabled = Allow;
	PanelRegIY->Enabled = Allow;
	PanelRegSP->Enabled = Allow;
	PanelRegPC->Enabled = Allow;

	PanelRegI->Enabled = Allow;
	PanelRegR->Enabled = Allow;

	ComboBoxFlag7->Enabled = Allow;
	ComboBoxFlag6->Enabled = Allow;
	ComboBoxFlag5->Enabled = Allow;
	ComboBoxFlag4->Enabled = Allow;
	ComboBoxFlag3->Enabled = Allow;
	ComboBoxFlag2->Enabled = Allow;
	ComboBoxFlag1->Enabled = Allow;
	ComboBoxFlag0->Enabled = Allow;

	StringGridInstructions->Enabled = Allow;

	PanelButtonGotoPC->Visible = Allow;
	PanelButtonGotoPPC->Visible = Allow;
	PanelButtonAddBreakpoint->Visible = Allow;
	PanelButtonShowMemory->Visible = Allow;

	PanelTopAddress->Enabled = Allow;
	ScrollBarAddress->Enabled = Allow;
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::UpdateTopAddress(tNativeCPC* NativeCPC)
{
TZ80dasmParam Z80dasmParam;
tULong pc = NativeCPC->Z80->Regs.PC.UShort.l;
tULong prevpc = NativeCPC->Z80->Regs.PrevPC.UShort.l;
tULong addr = mTopAddress;

	Z80dasmParam.NativeCPC = NativeCPC;
	Z80dasmParam.ReadFunction = Engine_ReadBankedRead;
	Z80dasmParam.StartAddress = 0;

	// Detect previous instructions
	int Loop;
	for (Loop=0; Loop < (StringGridInstructions->RowCount-1); Loop++)
	{
		if (prevpc == mNextInstructions[Loop])
		{
			if (pc != mNextInstructions[Loop+1])
			{
				addr = pc;
				break;
			}
			else if (Loop < (StringGridInstructions->RowCount/2))
			{
				addr = mNextInstructions[0];
				break;
			}
		}
	}
	if (Loop == (StringGridInstructions->RowCount-1))
	{
		if (Z80dasmDecodeInstruction(prevpc, &Z80dasmParam) == pc)
		{
			addr = prevpc;
		}
		else
		{
			addr = pc;
		}
	}

	if (addr == pc)
	{
		TopAddress = addr;
	}
	else if (addr == prevpc)
	{
		TopAddress = addr;
	}
}
//---------------------------------------------------------------------------


bool __fastcall TFormZ80Editor::OnStepByStepEvent(tULong Condition, tNativeCPC* NativeCPC)
{
bool Pause = NativeCPC->paused;

  if ( (mPaused == true) && (Pause == false) )
  {
    mPaused = false;
		Caption = Title;

		// Prevent registers edition
		UpdateRegistersEdition(false);
	}

  if ( ((Condition & EC_END_INSTRUCTION) == 0) && (Pause == false) )
    return false;

	if (mPaused == false)
	{
		mPaused = true;
		Caption = Title;

		TopAddress = NativeCPC->Z80->Regs.PC.ULong;

		// Allow registers edition
		UpdateRegistersEdition(true);
  }

  if (Condition & (EC_END_INSTRUCTION | EC_BREAKPOINT | EC_FDC_EVENT) )
  {
    UpdateTopAddress(NativeCPC);

		UpdateRegisters(NativeCPC);
    UpdateFlags(NativeCPC);
		UpdateInstructions(NativeCPC, true);
    UpdateStack(NativeCPC);
    UpdateInterrupt(NativeCPC);
  }

  return Pause;
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::StringGridInstructionsDrawCell(TObject *Sender,
          int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
  TStringGrid* StringGrid = (TStringGrid*)Sender;

  if (mNextInstructions[ARow] == FormMain->NativeCPC->Z80->Regs.PC.ULong)
  {
    StringGrid->Canvas->Brush->Color = clHighlight;
    StringGrid->Canvas->Font->Color = clWhite;
  }
	else if (State.Contains(gdSelected))
	{
		StringGrid->Canvas->Brush->Color = clTeal;
		StringGrid->Canvas->Font->Color = clWhite;
  }
  else if (ACol == 0)
  {
    StringGrid->Canvas->Brush->Color = Color;
    StringGrid->Canvas->Font->Color = mSettingsP->ThemeText2Color;
  }
  else
  {
    StringGrid->Canvas->Brush->Color = Color;
    StringGrid->Canvas->Font->Color = mSettingsP->ThemeText1Color;
  }

  // Draw cell
	StringGrid->Canvas->FillRect(Rect);
	AnsiString CellText = StringGrid->Cells[ACol][ARow];
	if (CellText.Length() > 0)
	{
		int TextHeight = StringGrid->Canvas->TextHeight(CellText);
		int TextPos = (StringGrid->RowHeights[ARow] - TextHeight) / 2;
		StringGrid->Canvas->TextOut(Rect.Left+5,
																Rect.Top+TextPos,
																CellText);
	}

  NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::StringGridStackDrawCell(TObject *Sender,
      int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
  TStringGrid* StringGrid = (TStringGrid*)Sender;

  if (State.Contains(gdSelected))
  {
    StringGrid->Canvas->Brush->Color = clHighlight;
    StringGrid->Canvas->Font->Color = clWhite;
  }
  else if (ACol == 0)
  {
    StringGrid->Canvas->Brush->Color = Color;
    StringGrid->Canvas->Font->Color = mSettingsP->ThemeText2Color;
  }
  else
  {
    StringGrid->Canvas->Brush->Color = Color;
    StringGrid->Canvas->Font->Color = mSettingsP->ThemeText1Color;
  }

  // Draw cell
  StringGrid->Canvas->FillRect(Rect);
  int TextHeight = StringGrid->Canvas->TextHeight(StringGrid->Cells[ACol][ARow]);
  int TextPos = (StringGrid->RowHeights[ARow] - TextHeight) / 2;
  StringGrid->Canvas->TextOut(Rect.Left+5,
                              Rect.Top+TextPos,
                              StringGrid->Cells[ACol][ARow]);

  NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::SetTopAddress(tULong Value)
{
	Visible = true;

	if (mTopAddress != Value)
	{
		mTopAddress = Value;
		if (mTopAddress < mMinAddress)
		{
			mTopAddress = mMinAddress;
		}
		else if (mTopAddress > mMaxAddress)
		{
			mTopAddress = mMaxAddress;
		}

		ScrollBarAddress->Position = mTopAddress;
		StringGridInstructions->Row = 0;
		PanelTopAddress->Caption = AnsiString::IntToHex(mTopAddress, 4);

		UpdateInstructions(FormMain->NativeCPC, true);
	}
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::ScrollBarAddressChange(TObject *Sender)
{
	TopAddress = ScrollBarAddress->Position;

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::PanelButtonGotoPCClick(TObject *Sender)
{
	TopAddress = FormMain->NativeCPC->Z80->Regs.PC.ULong;

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::PanelButtonGotoPPCClick(TObject *Sender)
{
	TopAddress = FormMain->NativeCPC->Z80->Regs.PrevPC.ULong;

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::StringGridInstructionsSelectCell(TObject *Sender,
					int ACol, int ARow, bool &CanSelect)
{
	if (mNextInstructions)
	{
		if (mNextInstructions[ARow] > mMaxAddress)
		{
			CanSelect = false;
		}
	}

	NOT_USED(Sender);
	NOT_USED(ACol);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::StringGridInstructionsDblClick(TObject *Sender)
{
tULong addr = mTopAddress;

	for (int Loop=0;
			 ((Loop < StringGridInstructions->Row) && (addr <= mMaxAddress));
			 Loop++)
	{
		addr = Z80dasmDecodeInstruction(addr,
																		&mZ80dasmParam);
	}

	if (addr <= mMaxAddress)
	{
		TopAddress = addr;
		StringGridInstructions->SetFocus();
		StringGridInstructions->Row = 0;
	}

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::StringGridInstructionsMouseWheelUp(TObject *Sender,
					TShiftState Shift, TPoint &MousePos, bool &Handled)
{
	if (StringGridInstructions->Enabled == false)
	{
		Handled = true;
		return;
	}

	if (!StringGridInstructions->Row)
	{
		if (TopAddress > mMinAddress)
		{
			TopAddress--;
		}
	}

	NOT_USED(Sender);
	NOT_USED(Shift);
	NOT_USED(MousePos);
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::StringGridInstructionsMouseWheelDown(TObject *Sender,
					TShiftState Shift, TPoint &MousePos, bool &Handled)
{
	if (StringGridInstructions->Enabled == false)
	{
		Handled = true;
		return;
	}

	if (StringGridInstructions->Row == StringGridInstructions->RowCount-1)
	{
		if (TopAddress < mMaxAddress)
		{
			TopAddress++;
		}

		StringGridInstructions->Row = StringGridInstructions->RowCount-1;
	}

	NOT_USED(Sender);
	NOT_USED(Shift);
	NOT_USED(MousePos);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::StringGridInstructionsKeyDown(TObject *Sender, WORD &Key,
					TShiftState Shift)
{
	if (StringGridInstructions->Enabled == false)
	{
		Key = 0;
		return;
	}

  switch (Key)
  {
    case VK_UP:
    {
      if (!StringGridInstructions->Row)
      {
        if (TopAddress > mMinAddress)
        {
          TopAddress--;
        }
      }
    }
    break;

    case VK_DOWN:
    {
			if (StringGridInstructions->Row == StringGridInstructions->RowCount-1)
			{
				if (mNextInstructions[StringGridInstructions->Row] < mMaxAddress)
				{
					if (TopAddress < mMaxAddress)
					{
						TopAddress++;
					}
					StringGridInstructions->Row = StringGridInstructions->RowCount-1;
				}
			}
    }
    break;

    case VK_PRIOR:
    {
      if (!StringGridInstructions->Row)
      {
        if ((TopAddress-mMinAddress) > (tULong)StringGridInstructions->RowCount)
        {
          TopAddress = TopAddress - StringGridInstructions->RowCount;
        }
        else
        {
					TopAddress = mMinAddress;
        }
      }
    }
    break;

    case VK_NEXT:
    {
      if (StringGridInstructions->Row == StringGridInstructions->RowCount-1)
      {
        if ((mMaxAddress-TopAddress) > (tULong)StringGridInstructions->RowCount)
        {
          TopAddress = TopAddress + StringGridInstructions->RowCount;
        }
        else
        {
          TopAddress = mMaxAddress;
        }
      }
    }
    break;

    case VK_HOME:
    {
      if (Shift.Contains(ssCtrl) == true)
      {
        TopAddress = mMinAddress;
      }
    }
    break;

    case VK_END:
    {
      if (Shift.Contains(ssCtrl) == true)
      {
        TopAddress = mMaxAddress;
      }
    }
    break;
  }

	NOT_USED(Shift);
  NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::StringGridInstructionsMouseDown(TObject *Sender, TMouseButton Button,
					TShiftState Shift, int X, int Y)
{
	// Right click button
	if (Button == mbRight)
	{
		int ACol;
		int ARow;

		// Get cell from coordinates
		StringGridInstructions->MouseToCell(X, Y, ACol, ARow);

		// Select cell
		StringGridInstructions->Col = ACol;
		StringGridInstructions->Row = ARow;
	}

	NOT_USED(Sender);
	NOT_USED(Shift);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::PanelTopAddressClick(TObject *Sender)
{
  mFormEditValue->Title = "Top address";
  mFormEditValue->CurrentValue = mTopAddress;
	mFormEditValue->Hexadecimal = true;
  mFormEditValue->MinValue = mMinAddress;
	mFormEditValue->MaxValue = mMaxAddress;

  if (mFormEditValue->ShowModal() == mrOk)
  {
    TopAddress = mFormEditValue->NewValue;
    StringGridInstructions->SetFocus();
    StringGridInstructions->Row = 0;
  }

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormZ80Editor::PanelButtonMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
{
  ((TPanel*)Sender)->Color = mSettingsP->ThemeButtonTextColor;
  ((TPanel*)Sender)->Font->Color = mSettingsP->ThemeButtonColor;

  NOT_USED(Button);
  NOT_USED(Shift);
  NOT_USED(X);
  NOT_USED(Y);
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::PanelButtonMouseUp(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
{
  ((TPanel*)Sender)->Color = mSettingsP->ThemeButtonColor;
  ((TPanel*)Sender)->Font->Color = mSettingsP->ThemeButtonTextColor;

  NOT_USED(Button);
  NOT_USED(Shift);
  NOT_USED(X);
  NOT_USED(Y);
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::AddPCBreakpointMenuClick(TObject *Sender)
{
	FormDebugger->ToggleBreakpointPC(mNextInstructions[StringGridInstructions->Row]);

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormZ80Editor::ShowMemoryMenuClick(TObject *Sender)
{
	FormMemoryEditor->ShowCurrentAddress(mNextInstructions[StringGridInstructions->Row]);

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


tULong __fastcall TFormZ80Editor::GetSelectedAddress(void)
{
tZ80* Z80 = FormMain->NativeCPC->Z80;
tULong Address;

	if (StringGridInstructions->Enabled == false)
	{
		Address = _PC;
	}
	else
	{
		Address = mNextInstructions[StringGridInstructions->Row];
	}

	return (Address);
}
//---------------------------------------------------------------------------

