DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
include/dma.h
00001 /*
00002  *  Copyright (C) 2002-2015  The DOSBox Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #ifndef DOSBOX_DMA_H
00021 #define DOSBOX_DMA_H
00022 #include <fstream>
00023 
00024 enum DMAEvent {
00025         DMA_REACHED_TC,
00026         DMA_MASKED,
00027         DMA_UNMASKED,
00028         DMA_TRANSFEREND
00029 };
00030 
00031 class DmaChannel;
00032 typedef void (* DMA_CallBack)(DmaChannel * chan,DMAEvent event);
00033 
00034 class DmaChannel {
00035 public:
00036         Bit32u pagebase;
00037         Bit16u baseaddr;
00038         Bit32u curraddr;
00039         Bit16u basecnt;
00040         Bit16u currcnt;
00041         Bit8u channum;
00042         Bit8u pagenum;
00043     Bit8u DMA16_PAGESHIFT;
00044     Bit32u DMA16_ADDRMASK;
00045         Bit8u DMA16;
00046         bool increment;
00047         bool autoinit;
00048         bool masked;
00049         bool tcount;
00050         bool request;
00051         DMA_CallBack callback;
00052 
00053         DmaChannel(Bit8u num, bool dma16);
00054         void DoCallBack(DMAEvent event) {
00055                 if (callback)   (*callback)(this,event);
00056         }
00057         void SetMask(bool _mask) {
00058                 masked=_mask;
00059                 DoCallBack(masked ? DMA_MASKED : DMA_UNMASKED);
00060         }
00061     void Set128KMode(bool en) {
00062         // 128KB mode (legacy ISA) (en=true):
00063         //    page shift = 1        (discard bit 0 of page register)
00064         //    addr mask = 0x1FFFF   (all bits 0-15 become bits 1-16, bit 15 of addr takes the place of page register bit 0)
00065         // 64KB mode (modern PCI including Intel chipsets) (en=false):
00066         //    page shift = 0        (all 8 bits of page register are used)
00067         //    addr mask = 0xFFFF    (discard bit 15, bits 0-14 become bits 1-15 on ISA bus)
00068         DMA16_PAGESHIFT = (en && DMA16) ? 0x1 : 0x0; // nonzero if we're to discard bit 0 of page register
00069         DMA16_ADDRMASK = (1UL << ((en && DMA16) ? 17UL : 16UL)) - 1UL; // nonzero if (addrreg << 1) to cover 128KB, zero if (addrreg << 1) to discard MSB, limit to 64KB
00070     }
00071         void Register_Callback(DMA_CallBack _cb) { 
00072                 callback = _cb; 
00073                 SetMask(masked);
00074                 if (callback) Raise_Request();
00075                 else Clear_Request();
00076         }
00077         void ReachedTC(void) {
00078                 tcount=true;
00079                 DoCallBack(DMA_REACHED_TC);
00080         }
00081         void SetPage(Bit8u val) {
00082                 pagenum=val;
00083                 pagebase=(Bitu)((Bitu)(pagenum >> DMA16_PAGESHIFT) << (Bitu)(16u + DMA16_PAGESHIFT));
00084         }
00085         void Raise_Request(void) {
00086                 request=true;
00087         }
00088         void Clear_Request(void) {
00089                 request=false;
00090         }
00091         Bitu Read(Bitu size, Bit8u * buffer);
00092         Bitu Write(Bitu size, Bit8u * buffer);
00093 
00094         void SaveState( std::ostream& stream );
00095         void LoadState( std::istream& stream );
00096 };
00097 
00098 class DmaController {
00099 private:
00100         Bit8u ctrlnum;
00101         bool flipflop;
00102         DmaChannel *DmaChannels[4];
00103 public:
00104         IO_ReadHandleObject DMA_ReadHandler[0x11];
00105         IO_WriteHandleObject DMA_WriteHandler[0x11];
00106         DmaController(Bit8u num) {
00107                 flipflop = false;
00108                 ctrlnum = num;          /* first or second DMA controller */
00109                 for(Bit8u i=0;i<4;i++) {
00110                         DmaChannels[i] = new DmaChannel(i+ctrlnum*4,ctrlnum==1);
00111                 }
00112         }
00113         ~DmaController(void) {
00114                 for(Bit8u i=0;i<4;i++) {
00115                         delete DmaChannels[i];
00116                 }
00117         }
00118         DmaChannel * GetChannel(Bit8u chan) {
00119                 if (chan<4) return DmaChannels[chan];
00120                 else return NULL;
00121         }
00122         void WriteControllerReg(Bitu reg,Bitu val,Bitu len);
00123         Bitu ReadControllerReg(Bitu reg,Bitu len);
00124 
00125         void SaveState( std::ostream& stream );
00126         void LoadState( std::istream& stream );
00127 };
00128 
00129 DmaChannel * GetDMAChannel(Bit8u chan);
00130 
00131 void CloseSecondDMAController(void);
00132 bool SecondDMAControllerAvailable(void);
00133 
00134 void DMA_SetWrapping(Bitu wrap);
00135 
00136 #endif