From 5247e1d357b5ad47d92693dc31312b86c981d7f6 Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 24 Apr 2017 08:21:05 -0500 Subject: [PATCH] Cleanup and simplify AppleII core code --- .../Computers/AppleII/AppleII.IDebuggable.cs | 78 ++++++------ .../AppleII/AppleII.IDisassembler.cs | 12 +- .../Computers/AppleII/AppleII.IEmulator.cs | 23 ++-- .../AppleII/AppleII.IInputPollable.cs | 2 +- .../AppleII/AppleII.IMemoryDomains.cs | 12 ++ .../Computers/AppleII/AppleII.ISettable.cs | 14 +-- .../AppleII/AppleII.ISoundProvider.cs | 10 +- .../Computers/AppleII/AppleII.IStatable.cs | 24 ++-- .../AppleII/AppleII.IVideoProvider.cs | 21 ++-- .../Computers/AppleII/AppleII.cs | 79 ++++++------ .../Computers/AppleII/LBSON.cs | 115 ++++++++++-------- 11 files changed, 191 insertions(+), 199 deletions(-) diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IDebuggable.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IDebuggable.cs index 9b304f5cae..601bc3f0cc 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IDebuggable.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IDebuggable.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using BizHawk.Emulation.Common; @@ -20,28 +19,12 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII reg.Key, reg.Key.Contains("Flag") ? reg.Value > 0 - : getRegisterValue(reg)); + : GetRegisterValue(reg)); } return dic; } - private RegisterValue getRegisterValue(KeyValuePair reg) - { - switch (reg.Key) - { - case "A": - case "X": - case "Y": - case "S": - return (byte)reg.Value; - case "PC": - return (ushort)reg.Value; - default: - return reg.Value; - } - } - public void SetCpuRegister(string register, int value) { switch (register) @@ -90,6 +73,8 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII } } + public IMemoryCallbackSystem MemoryCallbacks { get; } = new MemoryCallbackSystem(); + public bool CanStep(StepType type) { switch (type) @@ -103,7 +88,6 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII } } - public void Step(StepType type) { switch (type) @@ -120,17 +104,34 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII } } - public int TotalExecutedCycles + public int TotalExecutedCycles => (int)_machine.Cpu.Cycles; + + private RegisterValue GetRegisterValue(KeyValuePair reg) { - get { return (int)_machine.Cpu.Cycles; } + switch (reg.Key) + { + case "A": + case "X": + case "Y": + case "S": + return (byte)reg.Value; + case "PC": + return (ushort)reg.Value; + default: + return reg.Value; + } } private void StepInto() { - if (Tracer.Enabled) - _machine.Cpu.TraceCallback = (s) => TracerWrapper(s); + if (_tracer.Enabled) + { + _machine.Cpu.TraceCallback = TracerWrapper; + } else + { _machine.Cpu.TraceCallback = null; + } var machineInVblank = _machine.Video.IsVBlank; @@ -153,9 +154,9 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII { var instruction = _machine.Memory.Read(_machine.Cpu.RPC); - if (instruction == JSR) + if (instruction == Jsr) { - var destination = _machine.Cpu.RPC + JSRSize; + var destination = _machine.Cpu.RPC + JsrSize; while (_machine.Cpu.RPC != destination) { StepInto(); @@ -171,7 +172,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII { var instr = _machine.Memory.Read(_machine.Cpu.RPC); - JSRCount = instr == JSR ? 1 : 0; + _jsrCount = instr == Jsr ? 1 : 0; var bailOutFrame = Frame + 1; @@ -179,21 +180,21 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII { StepInto(); instr = _machine.Memory.Read(_machine.Cpu.RPC); - if (instr == JSR) + if (instr == Jsr) { - JSRCount++; + _jsrCount++; } - else if (instr == RTS && JSRCount <= 0) + else if (instr == Rts && _jsrCount <= 0) { StepInto(); - JSRCount = 0; + _jsrCount = 0; break; } - else if (instr == RTS) + else if (instr == Rts) { - JSRCount--; + _jsrCount--; } - else //Emergency Bailout Logic + else // Emergency Bailout Logic { if (Frame == bailOutFrame) { @@ -203,13 +204,10 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII } } - private int JSRCount = 0; + private int _jsrCount; - private const byte JSR = 0x20; - private const byte RTS = 0x60; - - private const byte JSRSize = 3; - - public IMemoryCallbackSystem MemoryCallbacks { get; private set; } + private const byte Jsr = 0x20; + private const byte Rts = 0x60; + private const byte JsrSize = 3; } } diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IDisassembler.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IDisassembler.cs index 2766a460e1..b7184d053f 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IDisassembler.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IDisassembler.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using BizHawk.Emulation.Common; using BizHawk.Emulation.Cores.Components.M6502; @@ -15,15 +13,13 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII { return "6502"; } + set { } } - public string PCRegisterName - { - get { return "PC"; } - } + public string PCRegisterName => "PC"; public IEnumerable AvailableCpus { @@ -32,7 +28,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII public string Disassemble(MemoryDomain m, uint addr, out int length) { - return MOS6502X.Disassemble((ushort)addr, out length, (a) => m.PeekByte(a)); + return MOS6502X.Disassemble((ushort)addr, out length, a => m.PeekByte(a)); } } } diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IEmulator.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IEmulator.cs index 62eac4baf5..49065b12b2 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IEmulator.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IEmulator.cs @@ -4,33 +4,24 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII { public partial class AppleII : IEmulator { - public IEmulatorServiceProvider ServiceProvider { get; private set; } + public IEmulatorServiceProvider ServiceProvider { get; } - public ControllerDefinition ControllerDefinition - { - get { return AppleIIController; } - } + public ControllerDefinition ControllerDefinition => AppleIIController; public IController Controller { get; set; } - public int Frame { get; set; } + public int Frame { get; private set; } - public string SystemId - { - get { return "AppleII"; } - } + public string SystemId => "AppleII"; - public bool DeterministicEmulation - { - get { return true; } - } + public bool DeterministicEmulation => true; public void FrameAdvance(bool render, bool rendersound) { FrameAdv(render, rendersound); } - public string BoardName { get { return null; } } + public string BoardName => null; public void ResetCounters() { @@ -39,7 +30,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII IsLagFrame = false; } - public CoreComm CoreComm { get; private set; } + public CoreComm CoreComm { get; } public void Dispose() { diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IInputPollable.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IInputPollable.cs index a595a2b73b..51f471dab3 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IInputPollable.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IInputPollable.cs @@ -12,6 +12,6 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII set { _machine.Lagged = value; } } - public IInputCallbackSystem InputCallbacks { get; private set; } + public IInputCallbackSystem InputCallbacks { get; } = new InputCallbackSystem(); } } diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IMemoryDomains.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IMemoryDomains.cs index f877ec5e3a..8ee9b1f331 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IMemoryDomains.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IMemoryDomains.cs @@ -15,13 +15,19 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII (addr) => { if (addr < 0 || addr >= 0xC000) + { throw new ArgumentOutOfRangeException(); + } + return (byte)_machine.Memory.Peek((int)addr); }, (addr, value) => { if (addr < 0 || addr >= 0xC000) + { throw new ArgumentOutOfRangeException(); + } + _machine.Memory.Write((int)addr, value); }, 1); @@ -31,13 +37,19 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII (addr) => { if (addr < 0 || addr >= 65536) + { throw new ArgumentOutOfRangeException(); + } + return (byte)_machine.Memory.Peek((int)addr); }, (addr, value) => { if (addr < 0 || addr >= 65536) + { throw new ArgumentOutOfRangeException(); + } + _machine.Memory.Write((int)addr, value); }, 1); diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.ISettable.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.ISettable.cs index a1bd7be8e4..6b61dea2b6 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.ISettable.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.ISettable.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.ComponentModel; using BizHawk.Emulation.Common; -using System.ComponentModel; namespace BizHawk.Emulation.Cores.Computers.AppleII { - partial class AppleII : ISettable + public partial class AppleII : ISettable { private Settings _settings; @@ -23,7 +19,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII } } - public AppleII.Settings GetSettings() + public Settings GetSettings() { return _settings.Clone(); } @@ -33,12 +29,12 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII return null; } - public bool PutSettings(AppleII.Settings o) + public bool PutSettings(Settings o) { _settings = o; _machine.Video.IsMonochrome = _settings.Monochrome; - setCallbacks(); + SetCallbacks(); return false; } diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.ISoundProvider.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.ISoundProvider.cs index c1bf7a9971..378601f045 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.ISoundProvider.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.ISoundProvider.cs @@ -5,10 +5,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII { public partial class AppleII : ISoundProvider { - public bool CanProvideAsync - { - get { return false; } - } + public bool CanProvideAsync => false; public void GetSamplesSync(out short[] samples, out int nsamp) { @@ -20,10 +17,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII _machine.Speaker.AudioService.Clear(); } - public SyncSoundMode SyncMode - { - get { return SyncSoundMode.Sync; } - } + public SyncSoundMode SyncMode => SyncSoundMode.Sync; public void SetSyncMode(SyncSoundMode mode) { diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IStatable.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IStatable.cs index 66c439936d..c3fa2f9aab 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IStatable.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IStatable.cs @@ -1,10 +1,12 @@ -using BizHawk.Emulation.Common; +using System; using System.IO; -using System; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; + +using BizHawk.Emulation.Common; + using Jellyfish.Virtu; +using Newtonsoft.Json; using Newtonsoft.Json.Bson; +using Newtonsoft.Json.Linq; namespace BizHawk.Emulation.Cores.Computers.AppleII { @@ -17,8 +19,9 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII return objectType == typeof(Machine); } - public override bool CanRead { get { return true; } } - public override bool CanWrite { get { return false; } } + public override bool CanRead => true; + + public override bool CanWrite => false; public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { @@ -32,7 +35,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII } } - public bool BinarySaveStatesPreferred { get { return true; } } + public bool BinarySaveStatesPreferred => true; private void SerializeEverything(JsonWriter w) { @@ -57,7 +60,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII private void DeserializeEverything(JsonReader r) { - var o = (OtherData)ser.Deserialize(r, typeof(OtherData)); + var o = (OtherData)_ser.Deserialize(r, typeof(OtherData)); Frame = o.Frame; LagCount = o.LagCount; IsLagFrame = o.IsLagFrame; @@ -65,6 +68,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII _machine = o.Core; _prevPressed = o.PreviousDiskPressed; _nextPressed = o.NextDiskPressed; + // since _machine was replaced, we need to reload settings from frontend PutSettings(_settings); } @@ -82,10 +86,10 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII private void InitSaveStates() { - ser.Converters.Add(new CoreConverter()); + _ser.Converters.Add(new CoreConverter()); } - private JsonSerializer ser = new JsonSerializer(); + private readonly JsonSerializer _ser = new JsonSerializer(); public void SaveStateText(TextWriter writer) { diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IVideoProvider.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IVideoProvider.cs index 3de60f4e10..c75fcfe855 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IVideoProvider.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.IVideoProvider.cs @@ -1,25 +1,22 @@ using BizHawk.Emulation.Common; -using Jellyfish.Virtu; namespace BizHawk.Emulation.Cores.Computers.AppleII { public partial class AppleII : IVideoProvider { - private Jellyfish.Virtu.Services.VideoService _V - { get { return _machine.Video.VideoService; } } - - int[] IVideoProvider.GetVideoBuffer() { return _V.fb; } + public int[] GetVideoBuffer() + { + return _machine.Video.VideoService.fb; + } // put together, these describe a metric on the screen // they should define the smallest size that the buffer can be placed inside such that: // 1. no actual pixel data is lost // 2. aspect ratio is accurate - int IVideoProvider.VirtualWidth { get { return 560; } } - int IVideoProvider.VirtualHeight { get { return 384; } } - - int IVideoProvider.BufferWidth { get { return 560; } } - int IVideoProvider.BufferHeight { get { return 384; } } - int IVideoProvider.BackgroundColor { get { return 0; } } - + public int VirtualWidth => 560; + public int VirtualHeight => 384; + public int BufferWidth => 560; + public int BufferHeight => 384; + public int BackgroundColor => 0; } } diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.cs index 03266e8b60..2606287c37 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.cs @@ -10,37 +10,37 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII "Virtu", "fool", isPorted: true, - isReleased: true - )] + isReleased: true)] [ServiceNotApplicable(typeof(ISaveRam), typeof(IRegionable))] public partial class AppleII : IEmulator, ISoundProvider, IVideoProvider, IStatable, IDriveLight { + static AppleII() + { + AppleIIController = new ControllerDefinition { Name = "Apple IIe Keyboard" }; + AppleIIController.BoolButtons.AddRange(RealButtons); + AppleIIController.BoolButtons.AddRange(ExtraButtons); + } + public AppleII(CoreComm comm, IEnumerable gameInfoSet, IEnumerable romSet, Settings settings) : this(comm, gameInfoSet.First(), romSet.First(), settings) { - GameInfoSet = gameInfoSet.ToList(); - RomSet = romSet.ToList(); + _romSet = romSet.ToList(); } [CoreConstructor("AppleII")] public AppleII(CoreComm comm, GameInfo game, byte[] rom, Settings settings) { - GameInfoSet = new List(); - var ser = new BasicServiceProvider(this); ServiceProvider = ser; CoreComm = comm; - Tracer = new TraceBuffer + _tracer = new TraceBuffer { Header = "6502: PC, opcode, register (A, X, Y, P, SP, Cy), flags (NVTBDIZC)" }; - MemoryCallbacks = new MemoryCallbackSystem(); - InputCallbacks = new InputCallbackSystem(); - _disk1 = rom; - RomSet.Add(rom); + _romSet.Add(rom); _appleIIRom = comm.CoreFileProvider.GetFirmware( SystemId, "AppleIIe", true, "The Apple IIe BIOS firmware is required"); @@ -55,22 +55,27 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII // for junk.dsk the .dsk is important because it determines the format from that InitDisk(); - ser.Register(Tracer); + ser.Register(_tracer); - setCallbacks(); + SetCallbacks(); InitSaveStates(); SetupMemoryDomains(); PutSettings(settings ?? new Settings()); } - private List GameInfoSet { get; set; } - private readonly List RomSet = new List(); + private static readonly ControllerDefinition AppleIIController; + + private readonly List _romSet = new List(); + private readonly ITraceable _tracer; + + private Machine _machine; + private byte[] _disk1; + private readonly byte[] _appleIIRom; + private readonly byte[] _diskIIRom; public int CurrentDisk { get; private set; } - public int DiskCount => RomSet.Count; - - private ITraceable Tracer { get; } + public int DiskCount => _romSet.Count; public void SetDisk(int discNum) { @@ -81,7 +86,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII private void IncrementDisk() { CurrentDisk++; - if (CurrentDisk >= RomSet.Count) + if (CurrentDisk >= _romSet.Count) { CurrentDisk = 0; } @@ -94,29 +99,21 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII CurrentDisk--; if (CurrentDisk < 0) { - CurrentDisk = RomSet.Count - 1; + CurrentDisk = _romSet.Count - 1; } InitDisk(); } - private void InitDisk() { - _disk1 = RomSet[CurrentDisk]; + _disk1 = _romSet[CurrentDisk]; // make a writeable memory stream cloned from the rom. // for junk.dsk the .dsk is important because it determines the format from that _machine.BootDiskII.Drives[0].InsertDisk("junk.dsk", (byte[])_disk1.Clone(), false); } - private Machine _machine; - private byte[] _disk1; - private readonly byte[] _appleIIRom; - private readonly byte[] _diskIIRom; - - private static readonly ControllerDefinition AppleIIController; - private static readonly List RealButtons = new List(Keyboard.GetKeyNames() .Where(k => k != "White Apple") // Hack because these buttons aren't wired up yet .Where(k => k != "Black Apple") @@ -128,22 +125,15 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII "Next Disk", }; - static AppleII() - { - AppleIIController = new ControllerDefinition { Name = "Apple IIe Keyboard" }; - AppleIIController.BoolButtons.AddRange(RealButtons); - AppleIIController.BoolButtons.AddRange(ExtraButtons); - } + public bool DriveLightEnabled => true; + public bool DriveLightOn => _machine.DriveLight; - public bool DriveLightEnabled { get { return true; } } - public bool DriveLightOn { get { return _machine.DriveLight; } } - - private bool _nextPressed = false; - private bool _prevPressed = false; + private bool _nextPressed; + private bool _prevPressed; private void TracerWrapper(string[] content) { - Tracer.Put(new TraceInfo + _tracer.Put(new TraceInfo { Disassembly = content[0], RegisterInfo = content[1] @@ -152,9 +142,9 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII private void FrameAdv(bool render, bool rendersound) { - if (Tracer.Enabled) + if (_tracer.Enabled) { - _machine.Cpu.TraceCallback = (s) => TracerWrapper(s); + _machine.Cpu.TraceCallback = TracerWrapper; } else { @@ -191,13 +181,12 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII Frame++; } - private void setCallbacks() + private void SetCallbacks() { _machine.Memory.ReadCallback = MemoryCallbacks.CallReads; _machine.Memory.WriteCallback = MemoryCallbacks.CallWrites; _machine.Memory.ExecuteCallback = MemoryCallbacks.CallExecutes; _machine.Memory.InputCallback = InputCallbacks.Call; } - } } diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/LBSON.cs b/BizHawk.Emulation.Cores/Computers/AppleII/LBSON.cs index 2265df2788..56efe107b4 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/LBSON.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/LBSON.cs @@ -1,16 +1,11 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; using System.IO; -using Newtonsoft.Json.Linq; + +using Newtonsoft.Json; namespace BizHawk.Emulation.Cores.Computers.AppleII { // barebones classes for writing and reading a simple bson-like format, used to gain a bit of speed in Apple II savestates - internal enum LBTOK : byte { Null, @@ -38,61 +33,75 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII public class LBR : JsonReader { - public LBR(BinaryReader r) + private readonly BinaryReader _r; + private object _v; + private JsonToken _t; + + public LBR(BinaryReader reader) { - this.r = r; + _r = reader; } - private BinaryReader r; + public override void Close() { } + // as best as I can tell, the serializers refer to depth, but don't actually need to work except when doing certain error recovery - public override int Depth { get { return 0; } } - public override string Path { get { throw new NotImplementedException(); } } - public override Type ValueType { get { return v != null ? v.GetType() : null; } } - public override JsonToken TokenType { get { return t; } } - public override object Value { get { return v; } } - private object v; - private JsonToken t; + public override int Depth => 0; + + public override string Path + { + get { throw new NotImplementedException(); } + } + + public override Type ValueType => _v?.GetType(); + + public override JsonToken TokenType => _t; + + public override object Value => _v; public override bool Read() { - LBTOK l = (LBTOK)r.ReadByte(); + LBTOK l = (LBTOK)_r.ReadByte(); switch (l) { - case LBTOK.StartArray: t = JsonToken.StartArray; v = null; break; - case LBTOK.EndArray: t = JsonToken.EndArray; v = null; break; - case LBTOK.StartObject: t = JsonToken.StartObject; v = null; break; - case LBTOK.EndObject: t = JsonToken.EndObject; v = null; break; - case LBTOK.Null: t = JsonToken.Null; v = null; break; - case LBTOK.False: t = JsonToken.Boolean; v = false; break; - case LBTOK.True: t = JsonToken.Boolean; v = true; break; - case LBTOK.Property: t = JsonToken.PropertyName; v = r.ReadString(); break; - case LBTOK.Undefined: t = JsonToken.Undefined; v = null; break; - case LBTOK.S8: t = JsonToken.Integer; v = r.ReadSByte(); break; - case LBTOK.U8: t = JsonToken.Integer; v = r.ReadByte(); break; - case LBTOK.S16: t = JsonToken.Integer; v = r.ReadInt16(); break; - case LBTOK.U16: t = JsonToken.Integer; v = r.ReadUInt16(); break; - case LBTOK.S32: t = JsonToken.Integer; v = r.ReadInt32(); break; - case LBTOK.U32: t = JsonToken.Integer; v = r.ReadUInt32(); break; - case LBTOK.S64: t = JsonToken.Integer; v = r.ReadInt64(); break; - case LBTOK.U64: t = JsonToken.Integer; v = r.ReadUInt64(); break; - case LBTOK.String: t = JsonToken.String; v = r.ReadString(); break; - case LBTOK.F32: t = JsonToken.Float; v = r.ReadSingle(); break; - case LBTOK.F64: t = JsonToken.Float; v = r.ReadDouble(); break; - case LBTOK.ByteArray: t = JsonToken.Bytes; v = r.ReadBytes(r.ReadInt32()); break; + case LBTOK.StartArray: _t = JsonToken.StartArray; _v = null; break; + case LBTOK.EndArray: _t = JsonToken.EndArray; _v = null; break; + case LBTOK.StartObject: _t = JsonToken.StartObject; _v = null; break; + case LBTOK.EndObject: _t = JsonToken.EndObject; _v = null; break; + case LBTOK.Null: _t = JsonToken.Null; _v = null; break; + case LBTOK.False: _t = JsonToken.Boolean; _v = false; break; + case LBTOK.True: _t = JsonToken.Boolean; _v = true; break; + case LBTOK.Property: _t = JsonToken.PropertyName; _v = _r.ReadString(); break; + case LBTOK.Undefined: _t = JsonToken.Undefined; _v = null; break; + case LBTOK.S8: _t = JsonToken.Integer; _v = _r.ReadSByte(); break; + case LBTOK.U8: _t = JsonToken.Integer; _v = _r.ReadByte(); break; + case LBTOK.S16: _t = JsonToken.Integer; _v = _r.ReadInt16(); break; + case LBTOK.U16: _t = JsonToken.Integer; _v = _r.ReadUInt16(); break; + case LBTOK.S32: _t = JsonToken.Integer; _v = _r.ReadInt32(); break; + case LBTOK.U32: _t = JsonToken.Integer; _v = _r.ReadUInt32(); break; + case LBTOK.S64: _t = JsonToken.Integer; _v = _r.ReadInt64(); break; + case LBTOK.U64: _t = JsonToken.Integer; _v = _r.ReadUInt64(); break; + case LBTOK.String: _t = JsonToken.String; _v = _r.ReadString(); break; + case LBTOK.F32: _t = JsonToken.Float; _v = _r.ReadSingle(); break; + case LBTOK.F64: _t = JsonToken.Float; _v = _r.ReadDouble(); break; + case LBTOK.ByteArray: _t = JsonToken.Bytes; _v = _r.ReadBytes(_r.ReadInt32()); break; default: throw new InvalidOperationException(); } + return true; } public override byte[] ReadAsBytes() { - if (!Read() || t != JsonToken.Bytes) + if (!Read() || _t != JsonToken.Bytes) + { return null; - return (byte[])v; + } + + return (byte[])_v; } public override DateTime? ReadAsDateTime() @@ -114,21 +123,25 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII { // TODO: speed this up if needed if (!Read()) + { return null; + } - switch (t) + switch (_t) { case JsonToken.Null: return null; case JsonToken.Integer: case JsonToken.Float: - return Convert.ToInt32(v); + return Convert.ToInt32(_v); case JsonToken.String: int i; - if (int.TryParse(v.ToString(), out i)) + if (int.TryParse(_v.ToString(), out i)) + { return i; - else - return null; + } + + return null; default: return null; } @@ -137,9 +150,11 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII public override string ReadAsString() { if (!Read()) + { return null; + } - switch (t) + switch (_t) { case JsonToken.Null: return null; @@ -147,7 +162,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII case JsonToken.Integer: case JsonToken.Boolean: case JsonToken.String: - return v.ToString(); + return _v.ToString(); default: return null; } @@ -156,6 +171,8 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII public class LBW : JsonWriter { + private readonly BinaryWriter w; + private void WT(LBTOK t) { w.Write((byte)t); @@ -165,7 +182,6 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII { this.w = w; } - private BinaryWriter w; public override void Flush() { @@ -223,5 +239,4 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII public override void WriteValue(TimeSpan value) { throw new NotImplementedException(); } public override void WriteValue(Uri value) { throw new NotImplementedException(); } } - }