misc cleanups in Emulation.Cores

This commit is contained in:
adelikat 2017-04-17 15:23:04 -05:00
parent 28da1e215f
commit 94f152d1d1
7 changed files with 253 additions and 211 deletions

View File

@ -120,16 +120,19 @@ namespace BizHawk.Emulation.Cores.Calculators
}
}
public IMemoryCallbackSystem MemoryCallbacks { get; private set; }
public IMemoryCallbackSystem MemoryCallbacks { get; }
[FeatureNotImplemented]
public void Step(StepType type) { throw new NotImplementedException(); }
public bool CanStep(StepType type) { return false; }
public int TotalExecutedCycles
public void Step(StepType type)
{
get { return Cpu.TotalExecutedCycles; }
}
throw new NotImplementedException();
}
public bool CanStep(StepType type)
{
return false;
}
public int TotalExecutedCycles => Cpu.TotalExecutedCycles;
}
}

View File

@ -14,7 +14,7 @@ namespace BizHawk.Emulation.Cores.Calculators
set { _lagCount = value; }
}
public IInputCallbackSystem InputCallbacks { get; private set; }
public IInputCallbackSystem InputCallbacks { get; } = new InputCallbackSystem();
public bool IsLagFrame
{

View File

@ -4,11 +4,11 @@ namespace BizHawk.Emulation.Cores.Calculators
{
public partial class TI83 : IVideoProvider
{
public int VirtualWidth{ get { return 96; } }
public int VirtualHeight { get { return 64; } }
public int BufferWidth { get { return 96; } }
public int BufferHeight { get { return 64; } }
public int BackgroundColor { get { return 0; } }
public int VirtualWidth => 96;
public int VirtualHeight => 64;
public int BufferWidth => 96;
public int BufferHeight => 64;
public int BackgroundColor => 0;
public int[] GetVideoBuffer()
{
@ -19,19 +19,21 @@ namespace BizHawk.Emulation.Cores.Calculators
{
for (int x = 0; x < 96; x++)
{
int offset = y * 96 + x;
int offset = (y * 96) + x;
int bufbyte = offset >> 3;
int bufbit = offset & 7;
int bit = ((_vram[bufbyte] >> (7 - bufbit)) & 1);
int bit = (_vram[bufbyte] >> (7 - bufbit)) & 1;
if (bit == 0)
{
unchecked { pixels[i++] = (int)Settings.BGColor; }
unchecked
{
pixels[i++] = (int)Settings.BGColor;
}
}
else
{
pixels[i++] = (int)Settings.ForeColor;
}
}
}

View File

@ -5,7 +5,6 @@ using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Components.Z80;
// http://www.ticalc.org/pub/text/calcinfo/
namespace BizHawk.Emulation.Cores.Calculators
{
[CoreAttributes(
@ -21,8 +20,6 @@ namespace BizHawk.Emulation.Cores.Calculators
public TI83(CoreComm comm, GameInfo game, byte[] rom, object Settings)
{
ServiceProvider = new BasicServiceProvider(this);
InputCallbacks = new InputCallbackSystem();
MemoryCallbacks = new MemoryCallbackSystem();
PutSettings((TI83Settings)Settings ?? new TI83Settings());
CoreComm = comm;
@ -57,7 +54,7 @@ namespace BizHawk.Emulation.Cores.Calculators
serviceProvider.Register<IDisassemblable>(new Disassembler());
}
private readonly ITraceable Tracer;
private readonly TraceBuffer Tracer;
// hardware
private const ushort RamSizeMask = 0x7FFF;
@ -83,21 +80,20 @@ namespace BizHawk.Emulation.Cores.Calculators
private ushort _startPC;
// Link Cable
public TI83LinkPort LinkPort { get; private set; }
public TI83LinkPort LinkPort { get; }
internal bool LinkActive;
internal int LinkOutput, LinkInput;
internal int LinkState
{
get { return (LinkOutput | LinkInput) ^ 3; }
}
internal int LinkState => (LinkOutput | LinkInput) ^ 3;
private static readonly ControllerDefinition TI83Controller =
new ControllerDefinition
{
Name = "TI83 Controller",
BoolButtons = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","DOT",
BoolButtons =
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9","DOT",
"ON", "ENTER",
"DOWN", "LEFT", "UP", "RIGHT",
"PLUS", "MINUS", "MULTIPLY", "DIVIDE",
@ -112,14 +108,14 @@ namespace BizHawk.Emulation.Cores.Calculators
{
byte ret;
int romPage = _romPageLow3Bits | (_romPageHighBit << 3);
//Console.WriteLine("read memory: {0:X4}", addr);
if (addr < 0x4000)
{
ret = Rom[addr]; // ROM zero-page
}
else if (addr < 0x8000)
{
ret = Rom[romPage * 0x4000 + addr - 0x4000]; //other rom page
ret = Rom[(romPage * 0x4000) + addr - 0x4000]; // other rom page
}
else
{
@ -157,10 +153,13 @@ namespace BizHawk.Emulation.Cores.Calculators
{
// Prevent rom calls from disturbing link port activity
if (LinkActive && Cpu.RegisterPC < 0x4000)
{
return;
}
LinkPort.Update();
}
break;
case 1: // PORT_KEYBOARD:
_lagged = false;
@ -238,6 +237,7 @@ namespace BizHawk.Emulation.Cores.Calculators
if (Controller.IsPressed("RIGHT")) ret ^= 4;
if (Controller.IsPressed("UP")) ret ^= 8;
}
if ((_keyboardMask & 2) == 0)
{
if (Controller.IsPressed("ENTER")) ret ^= 1;
@ -248,6 +248,7 @@ namespace BizHawk.Emulation.Cores.Calculators
if (Controller.IsPressed("EXP")) ret ^= 32;
if (Controller.IsPressed("CLEAR")) ret ^= 64;
}
if ((_keyboardMask & 4) == 0)
{
if (Controller.IsPressed("DASH")) ret ^= 1;
@ -258,6 +259,7 @@ namespace BizHawk.Emulation.Cores.Calculators
if (Controller.IsPressed("TAN")) ret ^= 32;
if (Controller.IsPressed("VARS")) ret ^= 64;
}
if ((_keyboardMask & 8) == 0)
{
if (Controller.IsPressed("DOT")) ret ^= 1;
@ -269,6 +271,7 @@ namespace BizHawk.Emulation.Cores.Calculators
if (Controller.IsPressed("PRGM")) ret ^= 64;
if (Controller.IsPressed("STAT")) ret ^= 128;
}
if ((_keyboardMask & 16) == 0)
{
if (Controller.IsPressed("0")) ret ^= 1;
@ -319,7 +322,7 @@ namespace BizHawk.Emulation.Cores.Calculators
byte ret;
if (_displayMode == 1)
{
ret = _vram[_displayY * 12 + _displayX];
ret = _vram[(_displayY * 12) + _displayX];
}
else
{
@ -349,8 +352,8 @@ namespace BizHawk.Emulation.Cores.Calculators
{
int shift = column & 7;
int mask = ~(252 >> shift);
int Data = value << 2;
_vram[offset] = (byte)(_vram[offset] & mask | (Data >> shift));
int data = value << 2;
_vram[offset] = (byte)(_vram[offset] & mask | (data >> shift));
if (shift > 2 && offset < 0x2ff)
{
offset++;
@ -358,7 +361,7 @@ namespace BizHawk.Emulation.Cores.Calculators
shift = 8 - shift;
mask = ~(252 << shift);
_vram[offset] = (byte)(_vram[offset] & mask | (Data << shift));
_vram[offset] = (byte)(_vram[offset] & mask | (data << shift));
}
}
}
@ -436,7 +439,10 @@ namespace BizHawk.Emulation.Cores.Calculators
Cpu.Reset();
_ram = new byte[0x8000];
for (int i = 0; i < 0x8000; i++)
{
_ram[i] = 0xFF;
}
Cpu.RegisterPC = _startPC;
Cpu.IFF1 = false;

View File

@ -10,20 +10,20 @@ namespace BizHawk.Emulation.Cores.Calculators
// See http://www.ticalc.org/archives/files/fileinfo/294/29418.html for documentation
// Note: Each hardware read/write to the link port calls tthe update method.
readonly TI83 Parent;
private readonly TI83 Parent;
private readonly Queue<byte> CurrentData = new Queue<byte>();
private FileStream CurrentFile;
//private int FileBytesLeft;
private byte[] VariableData;
private FileStream _currentFile;
private byte[] _variableData;
private Action NextStep;
private Queue<byte> CurrentData = new Queue<byte>();
private ushort BytesToSend;
private byte BitsLeft;
private byte CurrentByte;
private byte StepsLeft;
private Action _nextStep;
private Status CurrentStatus = Status.Inactive;
private ushort _bytesToSend;
private byte _bitsLeft;
private byte _currentByte;
private byte _stepsLeft;
private Status _currentStatus = Status.Inactive;
private enum Status
{
@ -34,226 +34,252 @@ namespace BizHawk.Emulation.Cores.Calculators
Send
}
public TI83LinkPort(TI83 Parent)
public TI83LinkPort(TI83 parent)
{
this.Parent = Parent;
Parent = parent;
}
public void Update()
{
if (CurrentStatus == Status.PrepareReceive)
if (_currentStatus == Status.PrepareReceive)
{
// Get the first byte, and start sending it.
CurrentByte = CurrentData.Dequeue();
CurrentStatus = Status.Receive;
BitsLeft = 8;
StepsLeft = 5;
_currentByte = CurrentData.Dequeue();
_currentStatus = Status.Receive;
_bitsLeft = 8;
_stepsLeft = 5;
}
if (CurrentStatus == Status.PrepareSend && Parent.LinkState != 3)
if (_currentStatus == Status.PrepareSend && Parent.LinkState != 3)
{
CurrentStatus = Status.Send;
BitsLeft = 8;
StepsLeft = 5;
CurrentByte = 0;
_currentStatus = Status.Send;
_bitsLeft = 8;
_stepsLeft = 5;
_currentByte = 0;
}
if (CurrentStatus == Status.Receive)
if (_currentStatus == Status.Receive)
{
switch (StepsLeft)
switch (_stepsLeft)
{
case 5:
// Receive step 1: Lower the other device's line.
Parent.LinkInput = ((CurrentByte & 1) == 1) ? 2 : 1;
CurrentByte >>= 1;
StepsLeft--;
Parent.LinkInput = ((_currentByte & 1) == 1) ? 2 : 1;
_currentByte >>= 1;
_stepsLeft--;
break;
case 4:
// Receive step 2: Wait for the calc to lower the other line.
if ((Parent.LinkState & 3) == 0)
StepsLeft--;
{
_stepsLeft--;
}
break;
case 3:
// Receive step 3: Raise the other device's line back up.
Parent.LinkInput = 0;
StepsLeft--;
_stepsLeft--;
break;
case 2:
// Receive step 4: Wait for the calc to raise its line back up.
if ((Parent.LinkState & 3) == 3)
StepsLeft--;
{
_stepsLeft--;
}
break;
case 1:
// Receive step 5: Finish.
BitsLeft--;
_bitsLeft--;
if (BitsLeft == 0)
if (_bitsLeft == 0)
{
if (CurrentData.Count > 0)
CurrentStatus = Status.PrepareReceive;
{
_currentStatus = Status.PrepareReceive;
}
else
{
CurrentStatus = Status.Inactive;
if (NextStep != null)
NextStep();
_currentStatus = Status.Inactive;
_nextStep?.Invoke();
}
}
else
//next bit in the current byte.
StepsLeft = 5;
{
// Next bit in the current byte.
_stepsLeft = 5;
}
break;
}
}
else if (CurrentStatus == Status.Send)
else if (_currentStatus == Status.Send)
{
switch (StepsLeft)
switch (_stepsLeft)
{
case 5:
// Send step 1: Calc lowers a line.
if (Parent.LinkState != 3)
{
int Bit = Parent.LinkState & 1;
int Shift = 8 - BitsLeft;
CurrentByte |= (byte)(Bit << Shift);
StepsLeft--;
int bit = Parent.LinkState & 1;
int shift = 8 - _bitsLeft;
_currentByte |= (byte)(bit << shift);
_stepsLeft--;
}
break;
case 4:
//send step 2: Lower our line.
// Send step 2: Lower our line.
Parent.LinkInput = Parent.LinkOutput ^ 3;
StepsLeft--;
_stepsLeft--;
break;
case 3:
// Send step 3: wait for the calc to raise its line.
if ((Parent.LinkOutput & 3) == 0)
StepsLeft--;
{
_stepsLeft--;
}
break;
case 2:
// Send step 4: raise the other devices lines.
Parent.LinkInput = 0;
StepsLeft--;
_stepsLeft--;
break;
case 1:
// Send step 5: Finish
BitsLeft--;
_bitsLeft--;
if (BitsLeft == 0)
if (_bitsLeft == 0)
{
BytesToSend--;
CurrentData.Enqueue(CurrentByte);
_bytesToSend--;
CurrentData.Enqueue(_currentByte);
if (BytesToSend > 0)
CurrentStatus = Status.PrepareSend;
if (_bytesToSend > 0)
{
_currentStatus = Status.PrepareSend;
}
else
{
CurrentStatus = Status.Inactive;
if (NextStep != null)
NextStep();
_currentStatus = Status.Inactive;
_nextStep?.Invoke();
}
}
else
{
//next bit in the current byte.
StepsLeft = 5;
// Next bit in the current byte.
_stepsLeft = 5;
}
break;
}
}
}
public void SendFileToCalc(FileStream FS, bool Verify)
public void SendFileToCalc(FileStream fs, bool verify)
{
if (Verify)
VerifyFile(FS);
if (verify)
{
VerifyFile(fs);
}
FS.Seek(55, SeekOrigin.Begin);
CurrentFile = FS;
fs.Seek(55, SeekOrigin.Begin);
_currentFile = fs;
SendNextFile();
}
private void VerifyFile(FileStream FS)
private void VerifyFile(FileStream fs)
{
// Verify the file format.
byte[] Expected = new byte[] { 0x2a, 0x2a, 0x54, 0x49, 0x38, 0x33, 0x2a, 0x2a, 0x1a, 0x0a, 0x00 };
byte[] Actual = new byte[11];
byte[] expected = { 0x2a, 0x2a, 0x54, 0x49, 0x38, 0x33, 0x2a, 0x2a, 0x1a, 0x0a, 0x00 };
byte[] actual = new byte[11];
FS.Seek(0, SeekOrigin.Begin);
FS.Read(Actual, 0, 11);
fs.Seek(0, SeekOrigin.Begin);
fs.Read(actual, 0, 11);
// Check the header.
for (int n = 0; n < 11; n++)
if (Expected[n] != Actual[n])
{
FS.Close();
if (expected[n] != actual[n])
{
fs.Close();
throw new IOException("Invalid Header.");
}
}
// Seek to the end of the comment.
FS.Seek(53, SeekOrigin.Begin);
fs.Seek(53, SeekOrigin.Begin);
int Size = FS.ReadByte() + FS.ReadByte() * 256;
int size = fs.ReadByte() + (fs.ReadByte() * 256);
if (FS.Length != Size + 57)
if (fs.Length != size + 57)
{
FS.Close();
fs.Close();
throw new IOException("Invalid file length.");
}
// Verify the checksum.
ushort Checksum = 0;
for (int n = 0; n < Size; n++)
Checksum += (ushort)FS.ReadByte();
ushort ActualChecksum = (ushort)(FS.ReadByte() + FS.ReadByte() * 256);
if (Checksum != ActualChecksum)
ushort checksum = 0;
for (int n = 0; n < size; n++)
{
FS.Close();
checksum += (ushort)fs.ReadByte();
}
ushort actualChecksum = (ushort)(fs.ReadByte() + (fs.ReadByte() * 256));
if (checksum != actualChecksum)
{
fs.Close();
throw new IOException("Invalid Checksum.");
}
}
private void SendNextFile()
{
byte[] Header = new byte[13];
if (!CurrentFile.CanRead || CurrentFile.Read(Header, 0, 13) != 13)
byte[] header = new byte[13];
if (!_currentFile.CanRead || _currentFile.Read(header, 0, 13) != 13)
{
// End of file.
CurrentFile.Close();
_currentFile.Close();
return;
}
int Size = Header[2] + Header[3] * 256;
VariableData = new byte[Size + 2];
CurrentFile.Read(VariableData, 0, Size + 2);
int size = header[2] + (header[3] * 256);
_variableData = new byte[size + 2];
_currentFile.Read(_variableData, 0, size + 2);
// Request to send the file.
CurrentData.Clear();
CurrentData.Enqueue(0x03);
CurrentData.Enqueue(0xC9);
foreach (byte B in Header)
CurrentData.Enqueue(B);
foreach (byte b in header)
{
CurrentData.Enqueue(b);
}
// Calculate the checksum for the command.
ushort Checksum = 0;
for (int n = 2; n < Header.Length; n++)
Checksum += Header[n];
ushort checksum = 0;
for (int n = 2; n < header.Length; n++)
{
checksum += header[n];
}
CurrentData.Enqueue((byte)(Checksum % 256));
CurrentData.Enqueue((byte)(Checksum / 256));
CurrentData.Enqueue((byte)(checksum % 256));
CurrentData.Enqueue((byte)(checksum / 256));
// Finalize the command.
CurrentStatus = Status.PrepareReceive;
NextStep = ReceiveReqAck;
_currentStatus = Status.PrepareReceive;
_nextStep = ReceiveReqAck;
Parent.LinkActive = true;
}
@ -263,9 +289,9 @@ namespace BizHawk.Emulation.Cores.Calculators
CurrentData.Clear();
// Prepare to receive the Aknowledgement response from the calculator.
BytesToSend = 8;
CurrentStatus = Status.PrepareSend;
NextStep = SendVariableData;
_bytesToSend = 8;
_currentStatus = Status.PrepareSend;
_nextStep = SendVariableData;
}
private void SendVariableData()
@ -294,19 +320,23 @@ namespace BizHawk.Emulation.Cores.Calculators
CurrentData.Enqueue(0x15);
// Add variable data.
foreach (byte B in VariableData)
CurrentData.Enqueue(B);
foreach (byte b in _variableData)
{
CurrentData.Enqueue(b);
}
// Calculate the checksum.
ushort Checksum = 0;
for (int n = 2; n < VariableData.Length; n++)
Checksum += VariableData[n];
ushort checksum = 0;
for (int n = 2; n < _variableData.Length; n++)
{
checksum += _variableData[n];
}
CurrentData.Enqueue((byte)(Checksum % 256));
CurrentData.Enqueue((byte)(Checksum / 256));
CurrentData.Enqueue((byte)(checksum % 256));
CurrentData.Enqueue((byte)(checksum / 256));
CurrentStatus = Status.PrepareReceive;
NextStep = ReceiveDataAck;
_currentStatus = Status.PrepareReceive;
_nextStep = ReceiveDataAck;
Parent.LinkActive = true;
}
}
@ -317,9 +347,9 @@ namespace BizHawk.Emulation.Cores.Calculators
CurrentData.Clear();
// Prepare to receive the Aknowledgement response from the calculator.
BytesToSend = 4;
CurrentStatus = Status.PrepareSend;
NextStep = EndTransmission;
_bytesToSend = 4;
_currentStatus = Status.PrepareSend;
_nextStep = EndTransmission;
}
private void EndTransmission()
@ -332,21 +362,21 @@ namespace BizHawk.Emulation.Cores.Calculators
CurrentData.Enqueue(0x00);
CurrentData.Enqueue(0x00);
CurrentStatus = Status.PrepareReceive;
NextStep = FinalizeFile;
_currentStatus = Status.PrepareReceive;
_nextStep = FinalizeFile;
Parent.LinkActive = true;
}
private void OutOfMemory()
{
CurrentFile.Close();
_currentFile.Close();
Parent.LinkActive = false;
CurrentData.Clear();
// Prepare to receive the Aknowledgement response from the calculator.
BytesToSend = 3;
CurrentStatus = Status.PrepareSend;
NextStep = EndOutOfMemory;
_bytesToSend = 3;
_currentStatus = Status.PrepareSend;
_nextStep = EndOutOfMemory;
}
private void EndOutOfMemory()
@ -359,8 +389,8 @@ namespace BizHawk.Emulation.Cores.Calculators
CurrentData.Enqueue(0x01);
CurrentData.Enqueue(0x00);
CurrentStatus = Status.PrepareReceive;
NextStep = FinalizeFile;
_currentStatus = Status.PrepareReceive;
_nextStep = FinalizeFile;
Parent.LinkActive = true;
}
@ -369,7 +399,7 @@ namespace BizHawk.Emulation.Cores.Calculators
// Resets the link software, and checks to see if there is an additional file to send.
CurrentData.Clear();
Parent.LinkActive = false;
NextStep = null;
_nextStep = null;
SendNextFile();
}
}

View File

@ -64,14 +64,13 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII
PutSettings(settings ?? new Settings());
}
public List<GameInfo> GameInfoSet { get; private set; }
private List<GameInfo> GameInfoSet { get; set; }
private readonly List<byte[]> RomSet = new List<byte[]>();
public int CurrentDisk { get; private set; }
public int DiskCount { get { return RomSet.Count; } }
public int DiskCount => RomSet.Count;
private ITraceable Tracer { get; set; }
private ITraceable Tracer { get; }
public void SetDisk(int discNum)
{

View File

@ -29,7 +29,9 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=II/@EntryIndexedValue">II</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IO/@EntryIndexedValue">IO</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IPS/@EntryIndexedValue">IPS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IRQ/@EntryIndexedValue">IRQ</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=NES/@EntryIndexedValue">NES</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=NMI/@EntryIndexedValue">NMI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OR/@EntryIndexedValue">OR</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PC/@EntryIndexedValue">PC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PCECD/@EntryIndexedValue">PCECD</s:String>