123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- /*
- Copyright (C) 1994-1995 Apogee Software, Ltd.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /**********************************************************************
- module: DMA.C
- author: James R. Dose
- date: February 4, 1994
- Low level routines to for programming the DMA controller for 8 bit
- and 16 bit transfers.
- (c) Copyright 1994 James R. Dose. All Rights Reserved.
- **********************************************************************/
- #include <dos.h>
- #include <conio.h>
- #include <stdlib.h>
- #include "dma.h"
- #define DMA_MaxChannel 7
- #define VALID ( 1 == 1 )
- #define INVALID ( !VALID )
- #define BYTE 0
- #define WORD 1
- typedef struct
- {
- int Valid;
- int Width;
- int Mask;
- int Mode;
- int Clear;
- int Page;
- int Address;
- int Length;
- } DMA_PORT;
- static const DMA_PORT DMA_PortInfo[ DMA_MaxChannel + 1 ] =
- {
- { VALID, BYTE, 0xA, 0xB, 0xC, 0x87, 0x0, 0x1 },
- { VALID, BYTE, 0xA, 0xB, 0xC, 0x83, 0x2, 0x3 },
- { INVALID, BYTE, 0xA, 0xB, 0xC, 0x81, 0x4, 0x5 },
- { VALID, BYTE, 0xA, 0xB, 0xC, 0x82, 0x6, 0x7 },
- { INVALID, WORD, 0xD4, 0xD6, 0xD8, 0x8F, 0xC0, 0xC2 },
- { VALID, WORD, 0xD4, 0xD6, 0xD8, 0x8B, 0xC4, 0xC6 },
- { VALID, WORD, 0xD4, 0xD6, 0xD8, 0x89, 0xC8, 0xCA },
- { VALID, WORD, 0xD4, 0xD6, 0xD8, 0x8A, 0xCC, 0xCE },
- };
- int DMA_ErrorCode = DMA_Ok;
- #define DMA_SetErrorCode( status ) \
- DMA_ErrorCode = ( status );
- /*---------------------------------------------------------------------
- Function: DMA_ErrorString
- Returns a pointer to the error message associated with an error
- number. A -1 returns a pointer the current error.
- ---------------------------------------------------------------------*/
- char *DMA_ErrorString
- (
- int ErrorNumber
- )
- {
- char *ErrorString;
- switch( ErrorNumber )
- {
- case DMA_Error :
- ErrorString = DMA_ErrorString( DMA_ErrorCode );
- break;
- case DMA_Ok :
- ErrorString = "DMA channel ok.";
- break;
- case DMA_ChannelOutOfRange :
- ErrorString = "DMA channel out of valid range.";
- break;
- case DMA_InvalidChannel :
- ErrorString = "Unsupported DMA channel.";
- break;
- default :
- ErrorString = "Unknown DMA error code.";
- break;
- }
- return( ErrorString );
- }
- /*---------------------------------------------------------------------
- Function: DMA_VerifyChannel
- Verifies whether a DMA channel is available to transfer data.
- ---------------------------------------------------------------------*/
- int DMA_VerifyChannel
- (
- int channel
- )
- {
- int status;
- int Error;
- status = DMA_Ok;
- Error = DMA_Ok;
- if ( ( channel < 0 ) || ( DMA_MaxChannel < channel ) )
- {
- Error = DMA_ChannelOutOfRange;
- status = DMA_Error;
- }
- else if ( DMA_PortInfo[ channel ].Valid == INVALID )
- {
- Error = DMA_InvalidChannel;
- status = DMA_Error;
- }
- DMA_SetErrorCode( Error );
- return( status );
- }
- /*---------------------------------------------------------------------
- Function: DMA_SetupTransfer
- Programs the specified DMA channel to transfer data.
- ---------------------------------------------------------------------*/
- int DMA_SetupTransfer
- (
- int channel,
- char *address,
- int length,
- int mode
- )
- {
- DMA_PORT *Port;
- int addr;
- int ChannelSelect;
- int Page;
- int HiByte;
- int LoByte;
- int TransferLength;
- int status;
- status = DMA_VerifyChannel( channel );
- if ( status == DMA_Ok )
- {
- Port = &DMA_PortInfo[ channel ];
- ChannelSelect = channel & 0x3;
- addr = ( int )address;
- if ( Port->Width == WORD )
- {
- Page = ( addr >> 16 ) & 255;
- HiByte = ( addr >> 9 ) & 255;
- LoByte = ( addr >> 1 ) & 255;
- // Convert the length in bytes to the length in words
- TransferLength = ( length + 1 ) >> 1;
- // The length is always one less the number of bytes or words
- // that we're going to send
- TransferLength--;
- }
- else
- {
- Page = ( addr >> 16 ) & 255;
- HiByte = ( addr >> 8 ) & 255;
- LoByte = addr & 255;
- // The length is always one less the number of bytes or words
- // that we're going to send
- TransferLength = length - 1;
- }
- // Mask off DMA channel
- outp( Port->Mask, 4 | ChannelSelect );
- // Clear flip-flop to lower byte with any data
- outp( Port->Clear, 0 );
- // Set DMA mode
- switch( mode )
- {
- case DMA_SingleShotRead :
- outp( Port->Mode, 0x48 | ChannelSelect );
- break;
- case DMA_SingleShotWrite :
- outp( Port->Mode, 0x44 | ChannelSelect );
- break;
- case DMA_AutoInitRead :
- outp( Port->Mode, 0x58 | ChannelSelect );
- break;
- case DMA_AutoInitWrite :
- outp( Port->Mode, 0x54 | ChannelSelect );
- break;
- }
- // Send address
- outp( Port->Address, LoByte );
- outp( Port->Address, HiByte );
- // Send page
- outp( Port->Page, Page );
- // Send length
- outp( Port->Length, TransferLength );
- outp( Port->Length, TransferLength >> 8 );
- // enable DMA channel
- outp( Port->Mask, ChannelSelect );
- }
- return( status );
- }
- /*---------------------------------------------------------------------
- Function: DMA_EndTransfer
- Ends use of the specified DMA channel.
- ---------------------------------------------------------------------*/
- int DMA_EndTransfer
- (
- int channel
- )
- {
- DMA_PORT *Port;
- int ChannelSelect;
- int status;
- status = DMA_VerifyChannel( channel );
- if ( status == DMA_Ok )
- {
- Port = &DMA_PortInfo[ channel ];
- ChannelSelect = channel & 0x3;
- // Mask off DMA channel
- outp( Port->Mask, 4 | ChannelSelect );
- // Clear flip-flop to lower byte with any data
- outp( Port->Clear, 0 );
- }
- return( status );
- }
- /*---------------------------------------------------------------------
- Function: DMA_GetCurrentPos
- Returns the position of the specified DMA transfer.
- ---------------------------------------------------------------------*/
- char *DMA_GetCurrentPos
- (
- int channel
- )
- {
- DMA_PORT *Port;
- unsigned long addr;
- int status;
- addr = NULL;
- status = DMA_VerifyChannel( channel );
- if ( status == DMA_Ok )
- {
- Port = &DMA_PortInfo[ channel ];
- if ( Port->Width == WORD )
- {
- // Get address
- addr = inp( Port->Address ) << 1;
- addr |= inp( Port->Address ) << 9;
- // Get page
- addr |= inp( Port->Page ) << 16;
- }
- else
- {
- // Get address
- addr = inp( Port->Address );
- addr |= inp( Port->Address ) << 8;
- // Get page
- addr |= inp( Port->Page ) << 16;
- }
- }
- return( ( char * )addr );
- }
- /*---------------------------------------------------------------------
- Function: DMA_GetTransferCount
- Returns how many bytes are left in the DMA's transfer.
- ---------------------------------------------------------------------*/
- int DMA_GetTransferCount
- (
- int channel
- )
- {
- DMA_PORT *Port;
- int count;
- int status;
- status = DMA_Ok;
- count = 0;
- if ( ( channel < 0 ) || ( DMA_MaxChannel < channel ) )
- {
- status = DMA_ChannelOutOfRange;
- }
- else if ( DMA_PortInfo[ channel ].Valid == INVALID )
- {
- status = DMA_InvalidChannel;
- }
- if ( status == DMA_Ok )
- {
- Port = &DMA_PortInfo[ channel ];
- outp( Port->Clear, 0 );
- count = inp( Port->Length );
- count += inp( Port->Length ) << 8;
- if ( Port->Width == WORD )
- {
- count <<= 1;
- }
- }
- DMA_SetErrorCode( status );
- return( count );
- }
|