int ReadUsbData(byte[] data, int index = 0) { ch.WriteCommand(RD_USB_DATA0); var length = ch.ReadData(); var data2 = ch.ReadMultipleData(length); if (data != null) { Array.Copy(data2, 0, data, index, length); } return(length); }
private void Cpu_MemoryAccess(object sender, MemoryAccessEventArgs e) { if (e.Address == 0x20) { if (e.EventType == MemoryAccessEventType.BeforePortRead) { e.CancelMemoryAccess = true; if (waitingMultiDataTransferLength) { waitingMultiDataTransferLength = false; e.Value = chPorts.ReadData(); multiDataTransferRemaining = e.Value; multiDataTransferPointer = 0; if (e.Value > 0) { multiDataTransferBuffer = chPorts.ReadMultipleData(multiDataTransferRemaining); } } else if (multiDataTransferRemaining > 0) { e.Value = multiDataTransferBuffer[multiDataTransferPointer]; multiDataTransferPointer++; multiDataTransferRemaining--; } else { e.Value = chPorts.ReadData(); } } else if (e.EventType == MemoryAccessEventType.BeforePortWrite) { e.CancelMemoryAccess = true; if (waitingMultiDataTransferLength) { waitingMultiDataTransferLength = false; multiDataTransferBuffer = new byte[e.Value]; multiDataTransferPointer = 0; multiDataTransferRemaining = e.Value; chPorts.WriteData(e.Value); } else if (multiDataTransferRemaining > 0) { multiDataTransferBuffer[multiDataTransferPointer] = e.Value; multiDataTransferPointer++; multiDataTransferRemaining--; if (multiDataTransferRemaining == 0) { chPorts.WriteMultipleData(multiDataTransferBuffer); } } else { chPorts.WriteData(e.Value); } } } else if (e.Address == 0x21) { if (e.EventType == MemoryAccessEventType.BeforePortRead) { e.CancelMemoryAccess = true; e.Value = chPorts.ReadStatus(); } else if (e.EventType == MemoryAccessEventType.BeforePortWrite) { e.CancelMemoryAccess = true; chPorts.WriteCommand(e.Value); if (e.Value == CMD_RD_USB_DATA0 || e.Value == CMD_WR_HOST_DATA) { waitingMultiDataTransferLength = true; } } } else if (e.Address == 0xAA) { if (e.EventType == MemoryAccessEventType.AfterPortWrite) { var wasOn = (aaPortValue & 0x40) == 0; var on = (e.Value & 0x40) == 0; if (on != wasOn) { Debug.WriteLine("CAPS " + (on ? "ON" : "OFF")); } aaPortValue = e.Value; } else if (e.EventType == MemoryAccessEventType.AfterPortRead) { e.Value = aaPortValue; } } }