public override void WriteByte(int Address, byte Value) { // Clear the error status data[Address] = Value; switch (Address) { // Reset everything case MemoryMap.GABE_SDC_CONTROL_REG - MemoryMap.GABE_SDC_CTRL_START: blockPtr = 0; ResetMbrBootSector(); data[5] = 0; break; case MemoryMap.GABE_SDC_TRANS_TYPE_REG - MemoryMap.GABE_SDC_CTRL_START: currentCommand = (GabeCtrlCommand)Value; data[5] = 0; break; case MemoryMap.GABE_SDC_TRANS_CONTROL_REG - MemoryMap.GABE_SDC_CTRL_START: data[5] = 0; switch (currentCommand) { case GabeCtrlCommand.INIT: waitCounter = 4; blockPtr = 0; break; case GabeCtrlCommand.READ_BLK: waitCounter = 10; // the read operation will be a little longer // determine which block to read blockPtr = 0; block = null; int address = ReadWord(7) + (ReadWord(9) << 16); if (address == 0) { block = mbr; } else if (address == BOOT_SECTOR_ADDR) { block = boot_sector; } else if (address >= FAT_OFFSET_START && address <= FAT_OFFSET_START + FAT_SIZE - 1) { // read the fat table blockPtr = 0; int fatPage = (address - FAT_OFFSET_START) / 512; BuildFatPage(fatPage); block = fat; } else if (address >= ROOT_OFFSET_START && address <= ROOT_OFFSET_START + ROOT_SIZE - 1) { // read the root table block = root; blockPtr = address - ROOT_OFFSET_START; } else if (address >= DATA_OFFSET_START && address <= DATA_OFFSET_START + DATA_SIZE - 1) { // read the data int dataPage = (address - DATA_OFFSET_START) / 512; block = GetData(dataPage); blockPtr = 0; } if (block != null) { // tell the reader that we've got 512 bytes data[0x12] = 2; data[0x13] = 0; // clear the error status data[5] = 0; } else { // the controller returns an error data[5] = 1; } break; } break; case MemoryMap.GABE_SDC_RX_FIFO_CTRL_REG - MemoryMap.GABE_SDC_CTRL_START: data[5] = 0; if (Value == 1) { data[0x12] = 0; data[0x13] = 0; } break; } }
public override void WriteByte(int Address, byte Value) { // Clear the error status data[Address] = Value; switch (Address) { // Reset everything case MemoryMap.GABE_SDC_CONTROL_REG - MemoryMap.GABE_SDC_CTRL_START: blockPtr = 0; ResetMbrBootSector(); data[5] = 0; break; case MemoryMap.GABE_SDC_TRANS_TYPE_REG - MemoryMap.GABE_SDC_CTRL_START: currentCommand = (GabeCtrlCommand)Value; data[5] = 0; break; case MemoryMap.GABE_SDC_TRANS_CONTROL_REG - MemoryMap.GABE_SDC_CTRL_START: data[5] = 0; if (isPresent) { switch (currentCommand) { case GabeCtrlCommand.INIT: waitCounter = 4; blockPtr = 0; break; case GabeCtrlCommand.READ_BLK: waitCounter = 10; // the read operation will be a little longer // determine which block to read blockPtr = 0; readBlock = null; int readAddress = ReadWord(7) + (ReadWord(9) << 16); if (GetISOMode()) { // In the case of address 0, return the MBR. if (readAddress == 0) { readBlock = mbr; } else { readBlock = GetData_ISO((readAddress - (mbrPresent ? 0 : BOOT_SECTOR_ADDR)) / 512); } } else { if (readAddress == 0) { readBlock = mbr; } else if (readAddress == BOOT_SECTOR_ADDR) { readBlock = boot_sector; } else if (readAddress >= FAT_OFFSET_START && readAddress <= FAT_OFFSET_START + FAT_SIZE - 1) { // read the fat table blockPtr = 0; int fatPage = (readAddress - FAT_OFFSET_START) / 512; BuildFatPage(fatPage); readBlock = fat; } else if (readAddress >= ROOT_OFFSET_START && readAddress <= ROOT_OFFSET_START + ROOT_SIZE - 1) { // read the root table readBlock = root; blockPtr = readAddress - ROOT_OFFSET_START; } else if (readAddress >= DATA_OFFSET_START && readAddress <= DATA_OFFSET_START + DATA_SIZE - 1) { // read the data int dataPage = (readAddress - DATA_OFFSET_START) / 512; if (GetFSType() == FSType.FAT32) { dataPage += ROOT_SIZE / 512; } readBlock = GetData(dataPage); blockPtr = 0; } } if (readBlock != null) { // tell the reader that we've got 512 bytes data[0x12] = 2; data[0x13] = 0; // clear the error status data[5] = 0; } else { // the controller returns an error data[5] = 1; } break; case GabeCtrlCommand.WRITE_BLK: waitCounter = 10; // the write operation will be a little longer // determine which block to write blockPtr = 0; int writeAddress = ReadWord(7) + (ReadWord(9) << 16); if (GetISOMode()) { SetData_ISO((writeAddress - BOOT_SECTOR_ADDR) / 512, writeBlock); // write the 512 Byte buffer } else { if (writeAddress == 0) { Console.WriteLine("Gabe is trying to write to MBR!"); } else if (writeAddress == BOOT_SECTOR_ADDR) { Console.WriteLine("Gabe is trying to write to Boot Sector!"); } else if (writeAddress >= FAT_OFFSET_START && writeAddress <= FAT_OFFSET_START + FAT_SIZE - 1) { // read the fat table blockPtr = 0; int fatPage = (writeAddress - FAT_OFFSET_START) / 512; Console.WriteLine("Gabe is trying to write to FAT Area! Page: " + fatPage); // Compare the last FAT with the writeBlock - based on how many clusters are created, we can determine the filesize PrepareEmptyFileEntry(fatPage); writeBlock = new byte[512]; } else if (writeAddress >= ROOT_OFFSET_START && writeAddress <= ROOT_OFFSET_START + ROOT_SIZE - 1) { Console.WriteLine("Gabe is trying to write to Root Area!"); blockPtr = writeAddress - ROOT_OFFSET_START; UpdateRootEntries(); firstPtr = -1; lastPtr = -1; } else if (writeAddress >= DATA_OFFSET_START && writeAddress <= DATA_OFFSET_START + DATA_SIZE - 1) { // read the data int dataPage = (writeAddress - DATA_OFFSET_START) / 512; if (GetFSType() == FSType.FAT32) { dataPage += 2 + ROOT_SIZE / 512; } Console.WriteLine("Gabe is trying to write to Data Area! Cluster: " + dataPage); SetData(dataPage, writeBlock); blockPtr = 0; } else { // Invalid address Console.WriteLine("Gabe is trying to write to an invalid address:" + writeAddress); data[5] = 1; } } // reset the write block writeBlock = new byte[512]; break; } } else { // set the error status data[5] = 1; } break; case MemoryMap.GABE_SDC_RX_FIFO_CTRL_REG - MemoryMap.GABE_SDC_CTRL_START: data[5] = 0; if (Value == 1) { data[0x12] = 0; data[0x13] = 0; } break; case MemoryMap.GABE_SDC_TX_FIFO_CTRL_REG - MemoryMap.GABE_SDC_CTRL_START: data[5] = 0; if (Value == 1) { blockPtr = 0; writeBlock = new byte[512]; data[0x12] = 0; data[0x13] = 0; } break; case MemoryMap.GABE_SDC_TX_FIFO_DATA_REG - MemoryMap.GABE_SDC_CTRL_START: data[5] = 0; if (blockPtr > 511) { blockPtr = 0; } writeBlock[blockPtr++] = Value; break; } }