public static void rx_process(ref netHeader.NetPacket pk) { if (!rx_fifo_can_rx()) { Console.Error.WriteLine("ERROR : !rx_fifo_can_rx at rx_process"); return; } //smap_bd_t* pbd = ((smap_bd_t*)&dev9.dev9R[SMAP_BD_RX_BASE & 0xffff]) + dev9.rxbdi; int soff = (int)((DEV9Header.SMAP_BD_RX_BASE & 0xffff) + DEV9Header.dev9.rxbdi * DEV9Header.smap_bd.GetSize()); DEV9Header.smap_bd pbd = new DEV9Header.smap_bd(DEV9Header.dev9.dev9R, soff); int bytes = (pk.size + 3) & (~3); if (!((pbd.ctrl_stat & DEV9Header.SMAP_BD_RX_EMPTY) != 0)) { Console.Error.WriteLine("ERROR (!(pbd->ctrl_stat & SMAP_BD_RX_EMPTY))"); Console.Error.WriteLine("ERROR : Discarding " + bytes + " bytes (RX" + DEV9Header.dev9.rxbdi + " not ready)"); return; } int pstart = (DEV9Header.dev9.rxfifo_wr_ptr) & 16383; int i = 0; while (i < bytes) { DEV9Header.dev9_rxfifo_write(pk.buffer[i++]); DEV9Header.dev9.rxfifo_wr_ptr &= 16383; } lock (reset_sentry) { //increase RXBD DEV9Header.dev9.rxbdi++; DEV9Header.dev9.rxbdi &= (uint)((DEV9Header.SMAP_BD_SIZE / 8) - 1); //Fill the BD with info ! pbd.length = (ushort)pk.size; pbd.pointer = (ushort)(0x4000 + pstart); unchecked //Allow -int to uint { pbd.ctrl_stat &= (ushort)~DEV9Header.SMAP_BD_RX_EMPTY; } //increase frame count lock (counter_sentry) { byte framecount = DEV9Header.dev9Ru8((int)DEV9Header.SMAP_R_RXFIFO_FRAME_CNT); framecount++; DEV9Header.dev9Wu8((int)DEV9Header.SMAP_R_RXFIFO_FRAME_CNT, framecount); } } //spams// emu_printf("Got packet, %d bytes (%d fifo)\n", pk->size,bytes); fireIntR = true; //DEV9._DEV9irq(DEV9Header.SMAP_INTR_RXEND, 0);//now ? or when the fifo is full ? i guess now atm //note that this _is_ wrong since the IOP interrupt system is not thread safe.. but nothing i can do about that }
public override Int32 Init(byte wrapperRev, byte wrapperBuid) { LogInit();//dev9Log = fopen("logs/dev9Log.txt", "w"); //setvbuf(dev9Log, NULL, _IONBF, 0); DEV9_LOG("DEV9init"); //memset(&dev9, 0, sizeof(dev9)); DEV9Header.dev9 = new DEV9Header.dev9DataClass(); DEV9_LOG("DEV9init2"); DEV9_LOG("DEV9init3"); flash.FLASHinit(); //hEeprom = CreateFile( // "eeprom.dat", // GENERIC_READ|GENERIC_WRITE, // 0, // NULL, // OPEN_EXISTING, // FILE_FLAG_WRITE_THROUGH, // NULL //); //if(hEeprom==INVALID_HANDLE_VALUE) //{ //DEV9Header.dev9.eeprom = eeprom; DEV9Header.dev9.eeprom = new ushort[eeprom.Length / 2]; for (int i = 0; i < eeprom.Length; i += 2) { byte[] byte1 = BitConverter.GetBytes(eeprom[i]); byte[] byte2 = BitConverter.GetBytes(eeprom[i + 1]); byte[] shortBytes = new byte[2]; Utils.memcpy(ref shortBytes, 0, byte1, 0, 1); Utils.memcpy(ref shortBytes, 1, byte2, 0, 1); DEV9Header.dev9.eeprom[i / 2] = BitConverter.ToUInt16(shortBytes, 0); } //} //else //{ // mapping=CreateFileMapping(hEeprom,NULL,PAGE_READWRITE,0,0,NULL); // if(mapping==INVALID_HANDLE_VALUE) // { // CloseHandle(hEeprom); // dev9.eeprom=(u16*)eeprom; // } // else // { // dev9.eeprom = (u16*)MapViewOfFile(mapping,FILE_MAP_WRITE,0,0,0); // if(dev9.eeprom==NULL) // { // CloseHandle(mapping); // CloseHandle(hEeprom); // dev9.eeprom=(u16*)eeprom; // } // } //} { int rxbi; for (rxbi = 0; rxbi < (DEV9Header.SMAP_BD_SIZE / 8); rxbi++) { DEV9Header.smap_bd pbd; pbd = new DEV9Header.smap_bd(DEV9Header.dev9.dev9R, (int)((DEV9Header.SMAP_BD_RX_BASE & 0xffff) + (DEV9Header.smap_bd.GetSize() * rxbi))); // = (smap_bd_t*)&DEV9Header.dev9.dev9R[DEV9Header.SMAP_BD_RX_BASE & 0xffff]; //pbd = &pbd[rxbi]; pbd.ctrl_stat = (UInt16)DEV9Header.SMAP_BD_RX_EMPTY; pbd.length = 0; //The class should already store its values into the byte array } } DEV9_LOG("DEV9init ok"); return(0); }
//tx_process private static void tx_process() { //Console.Error.WriteLine("TX"); //we loop based on count ? or just *use* it ? UInt32 cnt = DEV9Header.dev9Ru8((int)DEV9Header.SMAP_R_TXFIFO_FRAME_CNT); //spams// printf("tx_process : %d cnt frames !\n",cnt); netHeader.NetPacket pk = new netHeader.NetPacket(); int fc = 0; for (fc = 0; fc < cnt; fc++) { //smap_bd_t *pbd= ((smap_bd_t *)&dev9.dev9R[SMAP_BD_TX_BASE & 0xffff])+dev9.txbdi; DEV9Header.smap_bd pbd; pbd = new DEV9Header.smap_bd(DEV9Header.dev9.dev9R, (int)((DEV9Header.SMAP_BD_TX_BASE & 0xffff) + (DEV9Header.smap_bd.GetSize() * DEV9Header.dev9.txbdi))); if (!((pbd.ctrl_stat & DEV9Header.SMAP_BD_TX_READY) != 0)) { Console.Error.WriteLine("ERROR : !pbd->ctrl_stat&SMAP_BD_TX_READY\n"); break; } if ((pbd.length & 3) != 0) { //spams// emu_printf("WARN : pbd->length not alligned %d\n",pbd->length); } if (pbd.length > 1514) { Console.Error.WriteLine("ERROR : Trying to send packet too big.\n"); } else { UInt32 _base = (UInt32)((pbd.pointer - 0x1000) & 16383); DEV9.DEV9_LOG("Sending Packet from base " + _base.ToString("X") + ", size " + pbd.length); //The 1st packet we send should be base 0, size 1514 //spams// emu_printf("Sending Packet from base %x, size %d\n", base, pbd->length); pk.size = pbd.length; if (!(pbd.pointer >= 0x1000)) { DEV9.DEV9_LOG("ERROR: odd , !pbd->pointer>0x1000 | 0x" + pbd.pointer.ToString("X") + " " + pbd.length.ToString()); } if (_base + pbd.length > 16384) { UInt32 was = 16384 - _base; Utils.memcpy(ref pk.buffer, 0, DEV9Header.dev9.txfifo, (int)_base, (int)was); Utils.memcpy(ref pk.buffer, (int)was, DEV9Header.dev9.txfifo, 0, (int)(pbd.length - was)); //I thingk this was a bug in the original plugin Console.Error.WriteLine("Warped read, was=" + was + ", sz=" + pbd.length + ", sz-was=" + (pbd.length - was)); } else { Utils.memcpy(ref pk.buffer, 0, DEV9Header.dev9.txfifo, (int)_base, (int)pbd.length); } net.tx_put(ref pk); } unchecked { pbd.ctrl_stat &= (UInt16)(~DEV9Header.SMAP_BD_TX_READY); } //increase TXBD DEV9Header.dev9.txbdi++; DEV9Header.dev9.txbdi &= (DEV9Header.SMAP_BD_SIZE / 8) - 1; //decrease frame count -- this is not thread safe //dev9Ru8(SMAP_R_TXFIFO_FRAME_CNT)--; DEV9Header.dev9Wu8((int)DEV9Header.SMAP_R_TXFIFO_FRAME_CNT, (byte)(DEV9Header.dev9Ru8((int)DEV9Header.SMAP_R_TXFIFO_FRAME_CNT) - 1)); } //spams// emu_printf("processed %d frames, %d count, cnt = %d\n",fc,dev9Ru8(SMAP_R_TXFIFO_FRAME_CNT),cnt); //if some error/early exit signal TXDNV if (fc != cnt || cnt == 0) { Console.Error.WriteLine("WARN : (fc!=cnt || cnt==0) but packet send request was made oO.."); DEV9._DEV9irq(DEV9Header.SMAP_INTR_TXDNV, 0); } //if we actualy send something send TXEND if (fc != 0) { DEV9._DEV9irq(DEV9Header.SMAP_INTR_TXEND, 100);//now ? or when the fifo is empty ? i guess now atm } }