BizHawk/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.IEmulator.cs

308 lines
7.0 KiB
C#
Raw Normal View History

using BizHawk.Common.NumberExtensions;
using BizHawk.Emulation.Common;
2017-06-11 22:06:50 +00:00
using System;
2017-05-24 23:36:34 +00:00
namespace BizHawk.Emulation.Cores.Atari.A7800Hawk
{
public partial class A7800Hawk : IEmulator
{
public IEmulatorServiceProvider ServiceProvider { get; }
2017-06-11 22:06:50 +00:00
public ControllerDefinition ControllerDefinition => _controllerDeck.Definition;
2017-05-24 23:36:34 +00:00
2017-05-28 00:40:13 +00:00
//Maria related variables
public int cycle;
public int cpu_cycle;
public bool cpu_is_haltable;
public bool cpu_is_halted;
public bool cpu_halt_pending;
public bool cpu_resume_pending;
2017-05-28 00:40:13 +00:00
// input state of controllers and console
public byte p1_state;
public byte p2_state;
public byte p1_fire;
public byte p2_fire;
public byte p1_fire_2x;
public byte p2_fire_2x;
public byte con_state;
public bool left_toggle;
public bool right_toggle;
public bool left_was_pressed;
public bool right_was_pressed;
public bool p1_is_2button;
public bool p2_is_2button;
public bool p1_is_lightgun;
public bool p2_is_lightgun;
public float p1_lightgun_x;
public float p1_lightgun_y;
public float p2_lightgun_x;
public float p2_lightgun_y;
2017-07-29 19:18:45 +00:00
public int lg_counting_down;
public int lg_counting_down_2;
public int lg_counting_down_3;
public bool lg_trigger_hit;
2017-07-29 19:18:45 +00:00
public bool lg_do_once = true;
2017-06-11 22:06:50 +00:00
// there are 4 maria cycles in a CPU cycle (fast access, both NTSC and PAL)
// if the 6532 or TIA are accessed (PC goes to one of those addresses) the next access will be slower by 1/2 a CPU cycle
// i.e. it will take 6 Maria cycles instead of 4
public bool slow_access = false;
2017-07-25 15:30:53 +00:00
public int slow_countdown;
2017-06-11 22:06:50 +00:00
2017-05-24 23:36:34 +00:00
public void FrameAdvance(IController controller, bool render, bool rendersound)
{
2017-07-19 18:55:50 +00:00
Console.WriteLine("-----------------------FRAME-----------------------");
2017-06-11 22:06:50 +00:00
if (_tracer.Enabled)
{
cpu.TraceCallback = s => _tracer.Put(s);
}
else
{
cpu.TraceCallback = null;
}
2017-05-24 23:36:34 +00:00
_frame++;
if (controller.IsPressed("Power"))
{
// it seems that theMachine.Reset() doesn't clear ram, etc
// this should leave hsram intact but clear most other things
HardReset();
}
_islag = true;
GetControllerState(controller);
GetConsoleState(controller);
//reset lightgun detection
2017-07-29 19:18:45 +00:00
lg_do_once = true;
lg_trigger_hit = false;
maria.RunFrame();
2017-05-24 23:36:34 +00:00
if (_islag)
{
_lagcount++;
}
}
public void RunCPUCycle()
{
2017-07-25 15:30:53 +00:00
if (slow_countdown==0)
{
cpu_cycle++;
}
else
{
slow_countdown--;
}
2017-07-29 19:18:45 +00:00
if (lg_counting_down > 0)
{
lg_counting_down--;
if (lg_counting_down==0)
{
lg_trigger_hit = true;
lg_counting_down_2 = 454;
lg_counting_down_3 = 8;
2017-07-29 19:18:45 +00:00
}
}
if (lg_counting_down_2 > 0)
2017-07-29 19:18:45 +00:00
{
lg_counting_down_2--;
if (lg_counting_down_2 == 0 && lg_counting_down_3 > 0)
{
lg_counting_down_3--;
lg_counting_down_2 = 454;
lg_trigger_hit = true;
}
if (lg_counting_down_2 == 424)
2017-07-29 19:18:45 +00:00
{
lg_trigger_hit = false;
}
}
tia._hsyncCnt++;
tia._hsyncCnt %= 454;
// do the audio sampling
if (tia._hsyncCnt == 113 || tia._hsyncCnt == 340)
{
tia.Execute(0);
}
2017-05-28 00:40:13 +00:00
// tick the m6532 timer, which is still active although not recommended to use
2017-07-25 15:30:53 +00:00
// also it runs off of the cpu cycle timer
if (cpu_cycle== 4)
{
m6532.Timer.Tick();
}
if (cpu_cycle <= (2 + (slow_access ? 1 : 0)))
2017-05-28 00:40:13 +00:00
{
cpu_is_haltable = true;
}
else
{
cpu_is_haltable = false;
}
// the time a cpu cycle takes depends on the status of the address bus
// any address in range of the TIA or m6532 takes an extra cycle to complete
if (cpu_cycle == (4 + (slow_access ? 2 : 0)))
{
if (!cpu_is_halted)
{
2017-06-11 22:06:50 +00:00
cpu.ExecuteOne();
2017-07-25 15:30:53 +00:00
// we need to stall the next cpu cycle from starting if the current one is a slow access
if (slow_access)
{
slow_access = false;
slow_countdown = 2;
}
}
else
{
// we still want to keep track of CPU time even if it is halted, so increment the counter here
// The basic 6502 has no halt state, this feature is specific to SALLY
cpu.TotalExecutedCycles++;
}
cpu_cycle = 0;
if (cpu_halt_pending)
{
cpu_halt_pending = false;
cpu_is_halted = true;
2017-06-11 22:06:50 +00:00
}
if (cpu_resume_pending)
2017-06-11 22:06:50 +00:00
{
cpu_resume_pending = false;
cpu_is_halted = false;
}
}
2017-05-24 23:36:34 +00:00
}
public void GetControllerState(IController controller)
2017-06-11 22:06:50 +00:00
{
InputCallbacks.Call();
p1_state = _controllerDeck.ReadPort1(controller);
p2_state = _controllerDeck.ReadPort2(controller);
p1_fire = _controllerDeck.ReadFire1(controller);
p2_fire = _controllerDeck.ReadFire2(controller);
p1_fire_2x = _controllerDeck.ReadFire1_2x(controller);
p2_fire_2x = _controllerDeck.ReadFire2_2x(controller);
p1_is_2button = _controllerDeck.Is_2_button1(controller);
p2_is_2button = _controllerDeck.Is_2_button2(controller);
p1_is_lightgun = _controllerDeck.Is_LightGun1(controller, out p1_lightgun_x, out p1_lightgun_y);
p2_is_lightgun = _controllerDeck.Is_LightGun2(controller, out p2_lightgun_x, out p2_lightgun_y);
}
public void GetConsoleState(IController controller)
{
byte result = 0;
if (controller.IsPressed("Right Difficulty"))
{
if (!right_was_pressed)
{
right_toggle = !right_toggle;
}
right_was_pressed = true;
result |= (byte)((right_toggle ? 1 : 0) << 7);
}
else
{
right_was_pressed = false;
result |= (byte)((right_toggle ? 1 : 0) << 7);
}
if (controller.IsPressed("Left Difficulty"))
{
if (!left_was_pressed)
{
left_toggle = !left_toggle;
}
left_was_pressed = true;
result |= (byte)((left_toggle ? 1 : 0) << 6);
}
else
{
left_was_pressed = false;
result |= (byte)((left_toggle ? 1 : 0) << 6);
}
if (!controller.IsPressed("Pause"))
{
result |= (1 << 3);
}
if (!controller.IsPressed("Select"))
{
result |= (1 << 1);
}
if (!controller.IsPressed("Reset"))
{
result |= 1;
}
2017-06-11 22:06:50 +00:00
con_state = result;
2017-06-11 22:06:50 +00:00
}
public byte getLightGunState(int p_x)
{
float x = p_x == 1 ? p1_lightgun_x : p2_lightgun_x;
float y = p_x == 1 ? p1_lightgun_y : p2_lightgun_y;
if ((maria.scanline - 20) == y-4)
{
2017-07-29 19:18:45 +00:00
if (maria.cycle >= (133 + x) && lg_do_once)
{
2017-07-29 19:18:45 +00:00
// return true 61 cycles into the future
lg_counting_down = 64 - (maria.cycle - (int)(133 + x));
2017-07-29 19:18:45 +00:00
lg_do_once = false;
}
}
if (lg_trigger_hit)
{
return 0x0;
}
else
{
return 0x80;
}
}
2017-05-24 23:36:34 +00:00
public int Frame => _frame;
public string SystemId => "A7800";
2017-05-24 23:36:34 +00:00
public bool DeterministicEmulation { get; set; }
public void ResetCounters()
{
_frame = 0;
_lagcount = 0;
_islag = false;
}
public CoreComm CoreComm { get; }
public void Dispose()
{
maria = null;
tia = null;
2017-05-25 00:40:02 +00:00
m6532 = null;
2017-05-24 23:36:34 +00:00
}
}
}