2014-04-19 19:01:13 +00:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Data ;
using System.Drawing ;
using System.Linq ;
using System.Text ;
using System.Windows.Forms ;
using BizHawk.Client.Common ;
using BizHawk.Emulation.Cores.Atari.Atari2600 ;
namespace BizHawk.Client.EmuHawk
{
public partial class Atari2600Debugger : Form , IToolForm
{
private Atari2600 _core = Global . Emulator as Atari2600 ;
2014-04-20 01:19:33 +00:00
private readonly List < string > _instructions = new List < string > ( ) ;
2014-04-19 19:01:13 +00:00
public Atari2600Debugger ( )
{
InitializeComponent ( ) ;
2014-04-20 01:19:33 +00:00
TraceView . QueryItemText + = TraceView_QueryItemText ;
TraceView . VirtualMode = true ;
2014-04-19 19:01:13 +00:00
//TODO: add to Closing a Mainform.ResumeControl() call
}
private void Atari2600Debugger_Load ( object sender , EventArgs e )
{
// TODO: some kind of method like PauseAndRelinquishControl() which will set a flag preventing unpausing by the user, and then a ResumeControl() method that is done on close
GlobalWin . MainForm . PauseEmulator ( ) ;
2014-04-20 01:19:33 +00:00
Global . CoreComm . Tracer . Enabled = true ;
2014-04-19 19:01:13 +00:00
}
public void Restart ( )
{
// TODO
}
public bool AskSave ( )
{
return false ;
}
public bool UpdateBefore
{
get { return false ; } // TODO: think about this
}
public void UpdateValues ( )
{
2014-04-20 01:19:33 +00:00
var flags = _core . GetCpuFlagsAndRegisters ( ) ;
PCRegisterBox . Text = flags [ "PC" ] . ToString ( ) ;
2014-04-19 22:23:13 +00:00
2014-04-20 01:19:33 +00:00
SPRegisterBox . Text = flags [ "S" ] . ToString ( ) ;
SPRegisterHexBox . Text = string . Format ( "{0:X2}" , flags [ "S" ] ) ;
SPRegisterBinaryBox . Text = ToBinStr ( flags [ "S" ] ) ;
2014-04-19 22:23:13 +00:00
2014-04-20 01:19:33 +00:00
ARegisterBox . Text = flags [ "A" ] . ToString ( ) ;
ARegisterHexBox . Text = string . Format ( "{0:X2}" , flags [ "A" ] ) ;
ARegisterBinaryBox . Text = ToBinStr ( flags [ "A" ] ) ;
2014-04-19 22:23:13 +00:00
2014-04-20 01:19:33 +00:00
XRegisterBox . Text = flags [ "X" ] . ToString ( ) ;
XRegisterHexBox . Text = string . Format ( "{0:X2}" , flags [ "X" ] ) ;
XRegisterBinaryBox . Text = ToBinStr ( flags [ "X" ] ) ;
2014-04-19 22:23:13 +00:00
2014-04-20 01:19:33 +00:00
YRegisterBox . Text = flags [ "Y" ] . ToString ( ) ;
YRegisterHexBox . Text = string . Format ( "{0:X2}" , flags [ "Y" ] ) ;
YRegisterBinaryBox . Text = ToBinStr ( flags [ "Y" ] ) ;
2014-04-19 22:23:13 +00:00
2014-04-20 01:19:33 +00:00
NFlagCheckbox . Checked = flags [ "Flag N" ] = = 1 ;
VFlagCheckbox . Checked = flags [ "Flag V" ] = = 1 ;
TFlagCheckbox . Checked = flags [ "Flag T" ] = = 1 ;
BFlagCheckbox . Checked = flags [ "Flag B" ] = = 1 ;
2014-04-19 22:23:13 +00:00
2014-04-20 01:19:33 +00:00
DFlagCheckbox . Checked = flags [ "Flag D" ] = = 1 ;
IFlagCheckbox . Checked = flags [ "Flag I" ] = = 1 ;
ZFlagCheckbox . Checked = flags [ "Flag Z" ] = = 1 ;
CFlagCheckbox . Checked = flags [ "Flag C" ] = = 1 ;
2014-04-19 19:01:13 +00:00
FrameCountBox . Text = _core . Frame . ToString ( ) ;
2014-04-20 01:19:33 +00:00
ScanlineBox . Text = _core . CurrentScanLine . ToString ( ) ;
VSyncChexkbox . Checked = _core . IsVsync ;
VBlankCheckbox . Checked = _core . IsVBlank ;
UpdateTraceLog ( ) ;
}
private void UpdateTraceLog ( )
{
var instructions = Global . CoreComm . Tracer . TakeContents ( ) . Split ( '\n' ) ;
if ( ! string . IsNullOrWhiteSpace ( instructions [ 0 ] ) )
{
_instructions . AddRange ( instructions . Where ( str = > ! string . IsNullOrEmpty ( str ) ) ) ;
}
if ( _instructions . Count > = Global . Config . TraceLoggerMaxLines )
{
_instructions . RemoveRange ( 0 , _instructions . Count - Global . Config . TraceLoggerMaxLines ) ;
}
TraceView . ItemCount = _instructions . Count ;
2014-04-19 19:01:13 +00:00
}
private string ToBinStr ( int val )
{
return Convert . ToString ( ( uint ) val , 2 ) . PadLeft ( 8 , '0' ) ;
}
2014-04-20 01:19:33 +00:00
private void TraceView_QueryItemText ( int index , int column , out string text )
{
text = index < _instructions . Count ? _instructions [ index ] : string . Empty ;
}
2014-04-19 19:01:13 +00:00
#region Events
private void ExitMenuItem_Click ( object sender , EventArgs e )
{
Close ( ) ;
}
private void StepBtn_Click ( object sender , EventArgs e )
{
_core . CycleAdvance ( ) ;
UpdateValues ( ) ;
}
private void ScanlineAdvanceBtn_Click ( object sender , EventArgs e )
{
_core . ScanlineAdvance ( ) ;
UpdateValues ( ) ;
}
private void FrameAdvButton_Click ( object sender , EventArgs e )
{
_core . FrameAdvance ( true , true ) ;
UpdateValues ( ) ;
}
#endregion
}
}