|
| 1 | +using FTD2XX_NET; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO.Ports; |
| 6 | +using System.Linq; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Security.Cryptography; |
| 9 | +using System.Text; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar; |
| 13 | + |
| 14 | +namespace zanac.VGMPlayer |
| 15 | +{ |
| 16 | + public class PortWriterMsxPi : PortWriter |
| 17 | + { |
| 18 | + private int lastSccType = -1; |
| 19 | + private int lastSccSlot = -1; |
| 20 | + |
| 21 | + private int lastOpllType = -1; |
| 22 | + private int lastOpllSlot = -1; |
| 23 | + |
| 24 | + private int lastOpmType = -1; |
| 25 | + private int lastOpmSlot = -1; |
| 26 | + |
| 27 | + private byte lastDataType = 0xff; |
| 28 | + private byte lastWriteAddress; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// |
| 32 | + /// </summary> |
| 33 | + /// <param name="serialPort"></param> |
| 34 | + public PortWriterMsxPi(SerialPort serialPort) : base(serialPort) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// |
| 40 | + /// </summary> |
| 41 | + /// <param name="ftdiPort"></param> |
| 42 | + public PortWriterMsxPi(FTDI ftdiPort, PortId portNo) : base(ftdiPort, portNo) |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// |
| 48 | + /// </summary> |
| 49 | + public override void Abort() |
| 50 | + { |
| 51 | + lastOpllType = -1; |
| 52 | + lastOpllSlot = -1; |
| 53 | + |
| 54 | + lastSccType = -1; |
| 55 | + lastSccSlot = -1; |
| 56 | + |
| 57 | + lastOpmType = -1; |
| 58 | + lastOpmSlot = -1; |
| 59 | + |
| 60 | + lastDataType = 0xff; |
| 61 | + lastWriteAddress = 0; |
| 62 | + |
| 63 | + base.Abort(); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// |
| 68 | + /// </summary> |
| 69 | + /// <param name="type"></param> |
| 70 | + /// <param name="address"></param> |
| 71 | + /// <param name="data"></param> |
| 72 | + /// <param name="wait"></param> |
| 73 | + public override void Write(PortWriteData[] data) |
| 74 | + { |
| 75 | + List<byte> ds = new List<byte>(); |
| 76 | + foreach (var dt in data) |
| 77 | + { |
| 78 | + if (dt.Type == 0x17 && dt.Address == 0x07) |
| 79 | + //https://hra1129.github.io/system/psg_reg7.html |
| 80 | + dt.Data = (byte)((dt.Data & 0x3f) | 0x80); |
| 81 | + if (lastDataType == dt.Type && dt.Wait < 0) |
| 82 | + { |
| 83 | + //dummy wait |
| 84 | + for (int i = 0; i < -dt.Wait; i++) |
| 85 | + ds.Add(0x1F); |
| 86 | + } |
| 87 | + switch (dt.Type) |
| 88 | + { |
| 89 | + case 0x1F: |
| 90 | + //dummy wait |
| 91 | + ds.Add(dt.Type); |
| 92 | + break; |
| 93 | + case 2: |
| 94 | + if (lastOpllType != dt.Address || lastOpllSlot != dt.Data) |
| 95 | + { |
| 96 | + byte[] sd = new byte[] { |
| 97 | + dt.Type, |
| 98 | + dt.Address, |
| 99 | + dt.Data, |
| 100 | + }; |
| 101 | + ds.AddRange(sd); |
| 102 | + |
| 103 | + lastOpllType = dt.Address; |
| 104 | + lastOpllSlot = dt.Data; |
| 105 | + |
| 106 | + lastSccType = -1; |
| 107 | + lastSccSlot = -1; |
| 108 | + } |
| 109 | + break; |
| 110 | + case 3: |
| 111 | + if (lastSccType != dt.Address || lastSccSlot != dt.Data) |
| 112 | + { |
| 113 | + lastSccType = dt.Address; |
| 114 | + lastSccSlot = dt.Data; |
| 115 | + |
| 116 | + byte[] sd = new byte[] { |
| 117 | + dt.Type, |
| 118 | + dt.Address, |
| 119 | + dt.Data, |
| 120 | + }; |
| 121 | + ds.AddRange(sd); |
| 122 | + |
| 123 | + lastSccType = dt.Address; |
| 124 | + lastSccSlot = dt.Data; |
| 125 | + |
| 126 | + lastOpllType = -1; |
| 127 | + lastOpllSlot = -1; |
| 128 | + } |
| 129 | + break; |
| 130 | + case 0x18: // NEOTRON SLOT |
| 131 | + if (lastSccType != dt.Address || lastSccSlot != dt.Data) |
| 132 | + { |
| 133 | + lastSccType = dt.Address; |
| 134 | + lastSccSlot = dt.Data; |
| 135 | + |
| 136 | + byte[] sd = new byte[] { |
| 137 | + dt.Type, |
| 138 | + dt.Address, |
| 139 | + dt.Data, |
| 140 | + }; |
| 141 | + ds.AddRange(sd); |
| 142 | + |
| 143 | + lastSccType = dt.Address; |
| 144 | + lastSccSlot = dt.Data; |
| 145 | + |
| 146 | + lastOpllType = -1; |
| 147 | + lastOpllSlot = -1; |
| 148 | + } |
| 149 | + break; |
| 150 | + case 0xd: |
| 151 | + if (lastOpmType != dt.Address || lastOpmSlot != dt.Data) |
| 152 | + { |
| 153 | + lastOpmType = dt.Address; |
| 154 | + lastOpmSlot = dt.Data; |
| 155 | + byte[] sd = new byte[] { |
| 156 | + dt.Type, |
| 157 | + dt.Address, |
| 158 | + dt.Data, |
| 159 | + }; |
| 160 | + ds.AddRange(sd); |
| 161 | + } |
| 162 | + break; |
| 163 | + default: |
| 164 | + { |
| 165 | + if ((dt.Type == 0xa || dt.Type == 0xb) && lastWriteAddress == 0xf && //YM8950 ADPCM write |
| 166 | + lastDataType == dt.Type && (ushort)dt.Address == ((ushort)lastWriteAddress)) |
| 167 | + { |
| 168 | + byte[] sd = new byte[] { |
| 169 | + 0x00, |
| 170 | + dt.Data, |
| 171 | + }; |
| 172 | + ds.AddRange(sd); |
| 173 | + } |
| 174 | + else if (dt.Type == 0x11 && lastWriteAddress == 0x8 && //OPNA ADPCM write |
| 175 | + lastDataType == dt.Type && (ushort)dt.Address == ((ushort)lastWriteAddress)) |
| 176 | + { |
| 177 | + byte[] sd = new byte[] { |
| 178 | + 0x00, |
| 179 | + dt.Data, |
| 180 | + }; |
| 181 | + ds.AddRange(sd); |
| 182 | + } |
| 183 | + else if (dt.Type == 0x13) //OPNA Pseudo DAC write |
| 184 | + { |
| 185 | + byte[] sd = new byte[] { |
| 186 | + dt.Type, |
| 187 | + dt.Data, |
| 188 | + }; |
| 189 | + ds.AddRange(sd); |
| 190 | + } |
| 191 | + else if (dt.Type == 0x14) //OPN2 DAC write |
| 192 | + { |
| 193 | + byte[] sd = new byte[] { |
| 194 | + dt.Type, |
| 195 | + dt.Data, |
| 196 | + }; |
| 197 | + ds.AddRange(sd); |
| 198 | + } |
| 199 | + else if (dt.Type == 0x15) //tR DAC write |
| 200 | + { |
| 201 | + byte[] sd = new byte[] { |
| 202 | + dt.Type, |
| 203 | + dt.Data, |
| 204 | + }; |
| 205 | + ds.AddRange(sd); |
| 206 | + } |
| 207 | + else if (dt.Type == 0x16) //OPNA DAC write |
| 208 | + { |
| 209 | + byte[] sd = new byte[] { |
| 210 | + dt.Type, |
| 211 | + dt.Data, |
| 212 | + }; |
| 213 | + ds.AddRange(sd); |
| 214 | + } |
| 215 | + else if ((dt.Type == 1 || dt.Type == 0xc || //OPLL |
| 216 | + //dt.Type == 4 || dt.Type == 5 || //SCC |
| 217 | + dt.Type == 0xa || dt.Type == 0xb || //OPL3 |
| 218 | + dt.Type == 0xe || //OPM BUSY WAIT |
| 219 | + dt.Type == 0x10 || dt.Type == 0x11 || //OPN2 or OPNA |
| 220 | + dt.Type == 0x12 || //OPN |
| 221 | + dt.Type == 0x1A || dt.Type == 0x1B //OPNB |
| 222 | + ) |
| 223 | + && lastDataType == dt.Type && (ushort)dt.Address == ((ushort)lastWriteAddress + 1)) |
| 224 | + { |
| 225 | + //連続書き込み |
| 226 | + byte[] sd = new byte[] { |
| 227 | + 0, |
| 228 | + dt.Data, |
| 229 | + }; |
| 230 | + ds.AddRange(sd); |
| 231 | + } |
| 232 | + else if ((dt.Type == 0x1e //SAA1099, ... |
| 233 | + ) |
| 234 | + && lastDataType == dt.Type && (ushort)dt.Address == ((ushort)lastWriteAddress)) |
| 235 | + { |
| 236 | + //連続書き込み |
| 237 | + byte[] sd = new byte[] { |
| 238 | + 0, |
| 239 | + dt.Data, |
| 240 | + }; |
| 241 | + ds.AddRange(sd); |
| 242 | + } |
| 243 | + else if (dt.Type >= 0x20) |
| 244 | + { |
| 245 | + byte[] sd = new byte[] { |
| 246 | + (byte)(dt.Type - 0x20), |
| 247 | + dt.Address, |
| 248 | + dt.Data, |
| 249 | + }; |
| 250 | + ds.AddRange(sd); |
| 251 | + } |
| 252 | + else |
| 253 | + { |
| 254 | + byte[] sd = new byte[] { |
| 255 | + dt.Type, |
| 256 | + dt.Address, |
| 257 | + dt.Data, |
| 258 | + }; |
| 259 | + ds.AddRange(sd); |
| 260 | + } |
| 261 | + lastDataType = dt.Type; |
| 262 | + lastWriteAddress = dt.Address; |
| 263 | + break; |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + sendData(ds.ToArray()); |
| 268 | + } |
| 269 | + |
| 270 | + /// <summary> |
| 271 | + /// |
| 272 | + /// </summary> |
| 273 | + /// <param name="data"></param> |
| 274 | + /// <param name="wait"></param> |
| 275 | + public override void RawWrite(byte[] data, int[] wait) |
| 276 | + { |
| 277 | + sendData(data); |
| 278 | + } |
| 279 | + |
| 280 | + private void sendData(byte[] sendData) |
| 281 | + { |
| 282 | + lock (LockObject) |
| 283 | + { |
| 284 | + try |
| 285 | + { |
| 286 | + SerialPort?.Write(sendData, 0, sendData.Length); |
| 287 | + } |
| 288 | + catch (Exception ex) |
| 289 | + { |
| 290 | + SerialPort.Close(); |
| 291 | + if (ex.GetType() == typeof(Exception)) |
| 292 | + throw; |
| 293 | + else if (ex.GetType() == typeof(SystemException)) |
| 294 | + throw; |
| 295 | + } |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + } |
| 300 | + |
| 301 | +} |
0 commit comments