floppywritesectorcommand.java

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

JAVA
125
字号
/*
 * $Id: FloppyWriteSectorCommand.java,v 1.2 2003/11/29 07:45:33 epr Exp $
 */
package org.jnode.driver.floppy;

import org.jnode.fs.util.CHS;
import org.jnode.fs.util.Geometry;
import org.jnode.system.DMAResource;
import org.jnode.util.TimeoutException;

/**
 * Command for writing a single sector to a floppy disk
 * @author epr
 */
public class FloppyWriteSectorCommand extends FloppyCommand {

	private final Geometry geometry;
	private final int sector;
	private final int sectorSize;
	private final int length;
	private final int cylinder;
	private final int head;
	private final byte[] data;
	private final int dataOffset;
	private final int gapSize;
	private final boolean multiTrack;
	private byte[] st;

	/**
	 * @param drive
	 * @param geometry
	 * @param chs
	 * @param sectorSize
	 * @param multiTrack
	 * @param gapSize
	 * @param src
	 * @param srcOffset
	 */
	public FloppyWriteSectorCommand(
		int drive,
		Geometry geometry,
		CHS chs,
		int sectorSize,
		boolean multiTrack,
		int gapSize,
		byte[] src,
		int srcOffset) {
		super(drive);
		this.geometry = geometry;
		this.sector = chs.getSector();
		this.sectorSize = sectorSize;
		this.multiTrack = multiTrack;
		this.gapSize = gapSize;
		if (multiTrack) {
			this.length = 2 * SECTOR_LENGTH[sectorSize];
		} else {
			this.length = SECTOR_LENGTH[sectorSize];
		}
		this.cylinder = chs.getCylinder();
		this.head = chs.getHead();
		this.data = src;
		this.dataOffset = srcOffset;
	}

	/**
	 * @param fdc
	 * @see org.jnode.driver.floppy.FloppyCommand#setup(org.jnode.driver.floppy.FDC)
	 * @throws FloppyException
	 */
	public void setup(FDC fdc) throws FloppyException {
		final byte[] cmd = new byte[9];
		if (multiTrack) {
			cmd[0] = (byte)0xc5;
		} else {
			cmd[0] = (byte)0x45;
		}
		cmd[1] = (byte) (getDrive() | ((head & 1) << 2));
		cmd[2] = (byte)cylinder;
		cmd[3] = (byte)head;
		cmd[4] = (byte)sector;
		cmd[5] = (byte)sectorSize;
		cmd[6] = (byte)geometry.getSectors();
		cmd[7] = (byte)gapSize;
		if (sectorSize == 0) {
			cmd[8] = (byte)length;
		} else {
			cmd[8] = (byte)0xff;
		}

		fdc.copyToDMA(data, dataOffset, length);
		fdc.setupDMA(length, DMAResource.MODE_WRITE);
		fdc.setDorReg(getDrive(), true, true);
		fdc.sendCommand(cmd, true);
	}

	/**
	 * @param fdc
	 * @see org.jnode.driver.floppy.FloppyCommand#handleIRQ(org.jnode.driver.floppy.FDC)
	 * @throws FloppyException
	 */
	public void handleIRQ(FDC fdc) throws FloppyException {
		try {
			st = fdc.getCommandState(7);
			final int st0 = st[0] & 0xFF;
			final int cmdState = (st0 & ST0_CMDST_MASK);
			if (cmdState != ST0_CMDST_NORMAL) {
				throw new FloppyException(
					"WriteSector failed [command state=" + cmdState + "]");
			}
			notifyFinished();
		} catch (TimeoutException ex) {
			notifyError(new FloppyException(ex));
		}
	}

	/**
	 * Gets the data that will be written
	 * @return data 
	 */
	public byte[] getData() {
		return data;
	}

}

⌨️ 快捷键说明

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