rxtxbuffer.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 154 行

JAVA
154
字号
/*
 * $Id: RxTxBuffer.java,v 1.1 2003/11/25 11:52:25 epr Exp $
 */
package org.jnode.driver.net.lance;

import org.jnode.net.ethernet.EthernetConstants;
import org.jnode.system.MemoryResource;
import org.jnode.system.ResourceManager;
import org.jnode.vm.Address;

/**
 * @author epr
 */
public class RxTxBuffer implements EthernetConstants {
	
	private static final int INITDATA_SIZE = 16;
	private static final int ENTRY_SIZE = 8;
	private static final int DATABUFFER_SIZE = ETH_FRAME_LEN + 4;
	
	/** Data space, layout: R/TDE[0..nrEntries-1], databuffer[0..nrEntries-1] */
	private final byte[] data;
	private final int nrRxEntries;
	private final int nrTxEntries;
	private final int offset;
	private final int address;
	private final MemoryResource mem;
	private final int initDataOffset;
	private final int rxRingOffset;
	private final int txRingOffset;
	private final int rxDataBufferOffset;
	private final int txDataBufferOffset;
	
	/**
	 * Create a new instance
	 * @param nrRxEntries
	 * @param nrTxEntries
	 * @param mode
	 * @param paddr
	 * @param laddr
	 * @param rm
	 */
	public RxTxBuffer(int nrRxEntries, int nrTxEntries, int mode, long paddr, long laddr, ResourceManager rm) {
		
		this.nrRxEntries = nrRxEntries;
		this.nrTxEntries = nrTxEntries;
		// Calculate the data size
		final int size = INITDATA_SIZE + ((nrRxEntries + nrTxEntries) * (ENTRY_SIZE + DATABUFFER_SIZE)) + 8/* alignment*/;
		data = new byte[size];
				
		mem = rm.asMemoryResource(data);
		int addr32 = Address.as32bit(mem.getAddress());
		int dataOffset = 0;
		// Align of 8-bytes
		while ((addr32 & 7) != 0) {
			addr32++;
			dataOffset++;
		}
		this.offset = dataOffset;
		this.address = addr32;
		
		// Calculate the offsets from address
		initDataOffset = 0;
		rxRingOffset = initDataOffset + INITDATA_SIZE;
		txRingOffset = rxRingOffset + (nrRxEntries * ENTRY_SIZE); 
		rxDataBufferOffset = txRingOffset + (nrTxEntries * ENTRY_SIZE);
		txDataBufferOffset = rxDataBufferOffset + (nrRxEntries * DATABUFFER_SIZE);
		
		// Now initialize the ring entries
		for (int i = 0; i < nrRxEntries; i++) {
			initEntry(i, rxRingOffset, rxDataBufferOffset);
		}
		for (int i = 0; i < nrTxEntries; i++) {
			initEntry(i, txRingOffset, txDataBufferOffset);
		}
		
		// Initialize the initial data structure
		mem.setShort(offset + initDataOffset + 0x00, (short)mode);

		mem.setShort(offset + initDataOffset + 0x02, (short)(paddr & 0xFFFF));
		mem.setShort(offset + initDataOffset + 0x04, (short)((paddr >> 16) & 0xFFFF));
		mem.setShort(offset + initDataOffset + 0x06, (short)((paddr >> 32) & 0xFFFF));

		mem.setShort(offset + initDataOffset + 0x08, (short)(laddr & 0xFFFF));
		mem.setShort(offset + initDataOffset + 0x0A, (short)((laddr >> 16) & 0xFFFF));
		mem.setShort(offset + initDataOffset + 0x0C, (short)((laddr >> 32) & 0xFFFF));
		mem.setShort(offset + initDataOffset + 0x0E, (short)((laddr >> 48) & 0xFFFF));
		
		mem.setShort(offset + initDataOffset + 0x10, (short)(getRxRingAddress() & 0xFFFF));
		final int rs = (nrRxEntries << 12) | ((getRxRingAddress() >> 16) & 0xFF);
		mem.setShort(offset + initDataOffset + 0x12, (short)rs);

		mem.setShort(offset + initDataOffset + 0x14, (short)(getTxRingAddress() & 0xFFFF));
		final int ts = (nrTxEntries << 12) | ((getTxRingAddress() >> 16) & 0xFF);
		mem.setShort(offset + initDataOffset + 0x16, (short)ts);
	}
	
	/**
	 * Gets the address of the initdata structure
	 */
	public final int getInitDataAddress() {
		return this.address + initDataOffset;
	}
	
	/**
	 * Gets the baseaddress of the rx descriptor ring
	 */
	public final int getRxRingAddress() {
		return this.address + rxRingOffset;
	}
	
	/**
	 * Gets the baseaddress of the tx descriptor ring
	 */
	public final int getTxRingAddress() {
		return this.address + txRingOffset;
	}
	
	/**
	 * Gets the number of rx entries in this buffer
	 */
	public final int getNrRxEntries() {
		return nrRxEntries;
	}
	
	/**
	 * Gets the number of rx entries in this buffer
	 */
	public final int getNrTxEntries() {
		return nrTxEntries;
	}
	
	/**
	 * Initialize a single entry
	 * @param entry
	 */
	private void initEntry(int entry, int ringOffset, int dbOffset) {
		final int ofs = this.offset + ringOffset + (entry * ENTRY_SIZE);
		// Set the address
		mem.setInt(ofs+0, address + getDataBufferOffset(dbOffset, entry));
		mem.setShort(ofs+4, (short)(-DATABUFFER_SIZE));
		mem.setShort(ofs+6, (short)0);
	}
	
	/**
	 * Gets the offset (from this.offset) into the memory buffer of the databuffer
	 * with the given entry number.
	 * @param entry
	 */
	private final int getDataBufferOffset(int dbOffset, int entry) {
		return dbOffset + (entry * DATABUFFER_SIZE);
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?