|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Globalization; |
| 3 | +using BizHawk.Common.NumberExtensions; |
| 4 | +using BizHawk.Emulation.Common; |
| 5 | + |
| 6 | +namespace BizHawk.Client.Common |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// This class holds a quad word (64 bits) <see cref="Watch"/> |
| 10 | + /// </summary> |
| 11 | + public sealed class QWordWatch : Watch |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Gets a list of <see cref="WatchDisplayType"/> for a <see cref="QWordWatch"/> |
| 15 | + /// </summary> |
| 16 | + public static readonly IReadOnlyList<WatchDisplayType> ValidTypes = [ |
| 17 | + WatchDisplayType.Unsigned, |
| 18 | + WatchDisplayType.Signed, |
| 19 | + WatchDisplayType.Hex, |
| 20 | + WatchDisplayType.Binary, |
| 21 | + WatchDisplayType.Float, |
| 22 | + ]; |
| 23 | + |
| 24 | + private ulong _value; |
| 25 | + |
| 26 | + private ulong _previous; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of the <see cref="QWordWatch"/> class |
| 30 | + /// </summary> |
| 31 | + /// <param name="domain"><see cref="MemoryDomain"/> where you want to track</param> |
| 32 | + /// <param name="address">The address you want to track</param> |
| 33 | + /// <param name="type">How you you want to display the value See <see cref="WatchDisplayType"/></param> |
| 34 | + /// <param name="bigEndian">Specify the endianess. true for big endian</param> |
| 35 | + /// <param name="note">A custom note about the <see cref="Watch"/></param> |
| 36 | + /// <param name="value">Current value</param> |
| 37 | + /// <param name="previous">Previous value</param> |
| 38 | + /// <param name="changeCount">How many times value has changed</param> |
| 39 | + /// <exception cref="ArgumentException">Occurs when a <see cref="WatchDisplayType"/> is incompatible with <see cref="WatchSize.QWord"/></exception> |
| 40 | + internal QWordWatch( |
| 41 | + MemoryDomain domain, |
| 42 | + long address, |
| 43 | + WatchDisplayType type, |
| 44 | + bool bigEndian, |
| 45 | + string note, |
| 46 | + ulong value, |
| 47 | + ulong previous, |
| 48 | + int changeCount) |
| 49 | + : base(domain, address, WatchSize.QWord, type, bigEndian: bigEndian, note) |
| 50 | + { |
| 51 | + _value = value is 0 ? GetQWord() : value; |
| 52 | + _previous = previous; |
| 53 | + ChangeCount = changeCount; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Get a list of <see cref="WatchDisplayType"/> that can be used for a <see cref="QWordWatch"/> |
| 58 | + /// </summary> |
| 59 | + /// <returns>An enumeration that contains all valid <see cref="WatchDisplayType"/></returns> |
| 60 | + public override IReadOnlyList<WatchDisplayType> AvailableTypes() |
| 61 | + => ValidTypes; |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Reset the previous value; set it to the current one |
| 65 | + /// </summary> |
| 66 | + public override void ResetPrevious() |
| 67 | + => _previous = GetQWord(); |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Try to sets the value into the <see cref="MemoryDomain"/> |
| 71 | + /// at the current <see cref="Watch"/> address |
| 72 | + /// </summary> |
| 73 | + /// <param name="value">Value to set</param> |
| 74 | + /// <returns>True if value successfully sets; otherwise, false</returns> |
| 75 | + public override bool Poke(string value) |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + PokeQWord(Type switch |
| 80 | + { |
| 81 | + WatchDisplayType.Unsigned => ulong.Parse(value), |
| 82 | + WatchDisplayType.Signed => (ulong) long.Parse(value), |
| 83 | + WatchDisplayType.Hex => ulong.Parse(value, NumberStyles.HexNumber), |
| 84 | + WatchDisplayType.Float => NumberExtensions.ReinterpretAsUInt64(float.Parse(value, NumberFormatInfo.InvariantInfo)), |
| 85 | + WatchDisplayType.Binary => Convert.ToUInt64(value, fromBase: 2), |
| 86 | + _ => 0, |
| 87 | + }); |
| 88 | + return true; |
| 89 | + } |
| 90 | + catch |
| 91 | + { |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Update the Watch (read it from <see cref="MemoryDomain"/> |
| 98 | + /// </summary> |
| 99 | + public override void Update(PreviousType previousType) |
| 100 | + { |
| 101 | + switch (previousType) |
| 102 | + { |
| 103 | + case PreviousType.Original: |
| 104 | + return; |
| 105 | + case PreviousType.LastChange: |
| 106 | + var nextValue = GetQWord(); |
| 107 | + if (nextValue != _value) |
| 108 | + { |
| 109 | + _previous = nextValue; |
| 110 | + ChangeCount++; |
| 111 | + } |
| 112 | + _value = nextValue; |
| 113 | + break; |
| 114 | + case PreviousType.LastFrame: |
| 115 | + _previous = _value; |
| 116 | + _value = GetQWord(); |
| 117 | + if (_value != Previous) ChangeCount++; |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // TODO: Implements IFormattable |
| 123 | + public string FormatValue(ulong val) |
| 124 | + { |
| 125 | + string FormatFloat() |
| 126 | + => NumberExtensions.ReinterpretAsF64(val).ToString(NumberFormatInfo.InvariantInfo); |
| 127 | + string FormatBinary() |
| 128 | + { |
| 129 | + var str = Convert.ToString(unchecked((long) val), toBase: 2).PadLeft(64, '0'); |
| 130 | + for (var i = 60; i > 0; i -= 4) str = str.Insert(i, " "); |
| 131 | + return str; |
| 132 | + } |
| 133 | + return Type switch |
| 134 | + { |
| 135 | + _ when !IsValid => "-", |
| 136 | + WatchDisplayType.Unsigned => val.ToString(), |
| 137 | + WatchDisplayType.Signed => ((long) val).ToString(), |
| 138 | + WatchDisplayType.Hex => $"{val:X16}", |
| 139 | + WatchDisplayType.Float => FormatFloat(), |
| 140 | + WatchDisplayType.Binary => FormatBinary(), |
| 141 | + _ => val.ToString(), |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Get a string representation of difference |
| 147 | + /// between current value and the previous one |
| 148 | + /// </summary> |
| 149 | + public override string Diff |
| 150 | + => $"{unchecked((long) _value - (long) _previous):+#;-#;0}"; |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Returns true if the Watch is valid, false otherwise |
| 154 | + /// </summary> |
| 155 | + public override bool IsValid |
| 156 | + => Domain.Size is 0 || Address < (Domain.Size - (sizeof(ulong) - 1)); |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Get the maximum possible value |
| 160 | + /// </summary> |
| 161 | + public override ulong MaxValue |
| 162 | + => ulong.MaxValue; |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Get the current value |
| 166 | + /// </summary> |
| 167 | + public override long Value |
| 168 | + => unchecked((long) GetQWord()); |
| 169 | + |
| 170 | + /// <summary> |
| 171 | + /// Get a string representation of the current value |
| 172 | + /// </summary> |
| 173 | + public override string ValueString |
| 174 | + => FormatValue(GetQWord()); |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Get the previous value |
| 178 | + /// </summary> |
| 179 | + public override ulong Previous |
| 180 | + => _previous; |
| 181 | + |
| 182 | + /// <summary> |
| 183 | + /// Get a string representation of the previous value |
| 184 | + /// </summary> |
| 185 | + public override string PreviousStr |
| 186 | + => FormatValue(_previous); |
| 187 | + } |
| 188 | +} |
0 commit comments