Copyright © 1995 &a.uhclem;, All Rights Reserved. Here is an example of the steps that occur to cause a
	DMA transfer.  In this example, the floppy disk
	controller (FDC) has just read a byte from a diskette and
	wants the DMA to place it in memory at location
	0x00123456.  The process begins by the FDC asserting the
	DRQ2 signal to alert the DMA controller.
	The DMA controller will note that the DRQ2 signal is asserted.
	The DMA controller will then make sure that DMA channel 2
	has been programmed and is enabled.  The DMA controller
	also makes sure that none of the other DMA channels are active
	or have a higher priority.  Once these checks are
	complete, the DMA asks the CPU to release the bus so that
	the DMA may use the bus.  The DMA requests the bus by
	asserting the HRQ signal which goes to the CPU.
	The CPU detects the HRQ signal, and will complete
	executing the current instruction.  Once the processor
	has reached a state where it can release the bus, it
	will.  Now all of the signals normally generated by the
	CPU (-MEMR, -MEMW, -IOR, -IOW and a few others) are
	placed in a tri-stated condition (neither high or low)
	and then the CPU asserts the HLDA signal which tells the
	DMA controller that it is now in charge of the bus.  
	Depending on the processor, the CPU may be able to
	execute a few additional instructions now that it no
	longer has the bus, but the CPU will eventually have to
	wait when it reaches an instruction that must read
	something from memory that is not in the internal
	processor cache or pipeline.
	Now that the DMA ``is in charge'', the DMA activates its
	-MEMR, -MEMW, -IOR, -IOW output signals, and the address
	outputs from the DMA are set to 0x3456, which will be
	used to direct the byte that is about to transferred to a
	specific memory location.
	The DMA will then let the device that requested the DMA
	transfer know that the transfer is commencing.  This is
	done by asserting the -DACK signal, or in the case of the
	floppy disk controller, -DACK2 is asserted.
	The floppy disk controller is now responsible for placing
	the byte to be transferred on the bus Data lines.  Unless
	the floppy controller needs more time to get the data
	byte on the bus (and if the peripheral needs more time it
	alerts the DMA via the READY signal), the DMA will wait
	one DMA clock, and then de-assert the -MEMW and -IOR
	signals so that the memory will latch and store the byte
	that was on the bus, and the FDC will know that the byte
	has been transferred.
	Since the DMA cycle only transfers a single byte at a
	time, the FDC now drops the DRQ2 signal, so that the DMA
	knows it is no longer needed.  The DMA will de-assert the
	-DACK2 signal, so that the FDC knows it must stop placing
	data on the bus.
	The DMA will now check to see if any of the other DMA
	channels have any work to do.  If none of the channels
	have their DRQ lines asserted, the DMA controller has
	completed its work and will now tri-state the -MEMR,
	-MEMW, -IOR, -IOW and address signals.
	Finally, the DMA will de-assert the HRQ signal.  The CPU
	sees this, and de-asserts the HOLDA signal.  Now the CPU
	activates its -MEMR, -MEMW, -IOR, -IOW and address lines,
	and it resumes executing instructions and accessing main
	memory and the peripherals.
	For a typical floppy disk sector, the above process is
	repeated 512 times, once for each byte.  Each time a byte
	is transferred, the address register in the DMA is
	incremented and the counter that shows how many bytes are
	to be transferred is decremented.
	When the counter reaches zero, the DMA asserts the EOP
	signal, which indicates that the counter has reached zero
	and no more data will be transferred until the DMA
	controller is reprogrammed by the CPU.  This event is
	also called the Terminal Count (TC).  There is only one
	EOP signal, because only one DMA channel can be active at
	any instant.
	If a peripheral wants to generate an interrupt when the
	transfer of a buffer is complete, it can test for its
	-DACK signal and the EOP signal both being asserted at
	the same time.  When that happens, it means the DMA will not
	transfer any more information for that peripheral without
	intervention by the CPU.  The peripheral can then assert
	one of the interrupt signals to get the processors'
	attention.  The DMA chip itself is not capable of
	generating an interrupt.  The peripheral and its
	associated hardware is responsible for generating any
	interrupt that occurs.
	It is important to understand that although the CPU
	always releases the bus to the DMA when the DMA makes the
	request, this action is invisible to both applications
	and the operating systems, except for slight changes in
	the amount of time the processor takes to execute
	instructions when the DMA is active.  Subsequently, the
	processor must poll the peripheral, poll the registers in
	the DMA chip, or receive an interrupt from the peripheral
	to know for certain when a DMA transfer has completed.
 You may have noticed earlier that instead of the DMA
	setting the address lines to 0x00123456 as we said
	earlier, the DMA only set 0x3456.  The reason for this
	takes a bit of explaining.
	When the original IBM PC was designed, IBM elected to use
	both DMA and interrupt controller chips that were
	designed for use with the 8085, an 8-bit processor with
	an address space of 16 bits (64K).  Since the IBM PC
	supported more than 64K of memory, something had to be
	done to allow the DMA to read or write memory locations
	above the 64K mark.  What IBM did to solve this problem
	was to add a latch for each DMA channel, that holds the
	upper bits of the address to be read to or written from.
	Whenever a DMA channel is active, the contents of that
	latch is written to the address bus and kept there until
	the DMA operation for the channel ends.  These latches
	are called ``Page Registers''.
	So for our example above, the DMA would put the 0x3456
	part of the address on the bus, and the Page Register for
	DMA channel 2 would put 0x0012xxxx on the bus.  Together,
	these two values form the complete address in memory that
	is to be accessed.
	Because the Page Register latch is independent of the DMA
	chip, the area of memory to be read or written must not
	span a 64K physical boundary.  If the DMA accesses memory
	location 0xffff, the DMA will then increment the address
	register and it will access the next byte at 0x0000, not
	0x10000.  The results of letting this happen are probably not intended.
	 The 8237 DMA can be operated in several modes.  The main
	ones are:
 The DMA channel that is to be programmed should always
	be ``masked'' before loading any settings.  This is because
	the hardware might assert DRQ, and the DMA might respond,
	even though not all of the parameters have been loaded or
	updated.
	Once masked, the host must specify the direction of the
	transfer (memory-to-I/O or I/O-to-memory), what mode of
	DMA operation is to be used for the transfer (Single,
	Block, Demand, Cascade, etc), and finally the address and
	length of the transfer are loaded.  The length that is
	loaded is one less than the amount you expect the DMA to
	transfer.  The LSB and MSB of the address and length are
	written to the same 8-bit I/O port, so another port must
	be written to first to guarantee that the DMA accepts the
	first byte as the LSB and the second byte as the MSB.
	Then, be sure to update the Page Register, which is
	external to the DMA and is accessed through a different
	set of I/O ports.
	Once all the settings are ready, the DMA channel can be
	un-masked.  That DMA channel is now considered to be
	``armed'', and will respond when DRQ is asserted.
	Refer to a hardware data book for precise programming
	details for the 8237.  You will also need to refer to the
	I/O port map for the PC system.  This map describes where
	the DMA and Page Register ports are located.  A complete
	table is located below.
     All systems based on the IBM-PC and PC/AT have the DMA
	hardware located at the same I/O ports.  The complete
	list is provided below.  Ports assigned to DMA Controller
	#2 are undefined on non-AT designs.
 DMA Address and Count Registers
 DMA Address and Count Registers
Note: The 8237 does allow two channels to
	be connected together to allow memory-to-memory DMA
	operations in a non-``fly-by'' mode, but nobody in the PC
	industry uses this scarce resource this way since it is
	faster to move data between memory locations using the
	CPU.
      In the PC architecture, each DMA channel is normally
      activated only when the hardware that uses that DMA
      requests a transfer by asserting the DRQ line for that
      channel.
    Note: ``Physical'' 64K boundaries should
	  not be confused with 8086-mode 64K ``Segments'', which
	  are created by adding a segment register with an offset
	  register.  Page Registers have no address overlap.
	To further complicate matters, the external DMA address
	latches on the PC/AT hold only eight bits, so that gives
	us 8+16=24 bits, which means that the DMA can only point
	at memory locations between 0 and 16Meg.  For newer
	computers that allow more than 16Meg of memory, the
	PC-compatible DMA cannot access locations above 16Meg.
	To get around this restriction, operating systems will
	reserve a buffer in an area below 16Meg that also does not
	span a physical 64K boundary.  Then the DMA will be
	programmed to read data to that buffer.  Once the DMA has
	moved the data into this buffer, the operating system
	will then copy the data from the buffer to the address
	where the data is really supposed to be stored.
	When writing data from an address above 16Meg to a
	DMA-based peripheral, the data must be first copied from
	where it resides into a buffer located below 16Meg, and
	then the DMA can copy the data from the buffer to the
	hardware.  In FreeBSD, these reserved buffers are called
	``Bounce Buffers''.  In the MS-DOS world, they are
	sometimes called ``Smart Buffers''.
    Note: DMA channel 0 was reserved for
	      refresh operations in early IBM PC computers, but
	      is generally available for use by peripherals in
	      modern systems.
	    When a peripheral is performing Bus Mastering, it is
	    important that the peripheral transmit data to or
	    from memory constantly while it holds the system bus.
	    If the peripheral cannot do this, it must release the
	    bus frequently so that the system can perform refresh
	    operations on memory.
	    Since memory read and write cycles ``count'' as refresh
	    cycles (a refresh cycle is actually an incomplete
	    memory read cycle), as long as the peripheral
	    controller continues reading or writing data to
	    sequential memory locations, that action will refresh
	    all of memory.
	
	    Bus-mastering is found in some SCSI adapters and
	    other high-performance peripheral cards.