11 MSDOS: BC(y) Watcom(y) DJGPP(y)
17 (n) - no (not possible or not useful)
18 (?) - may be possible, but not tested
27 /* DMA Controler #1 (8-bit controller) */
28 #define DMA1_STAT 0x08 /* read status register */
29 #define DMA1_WCMD 0x08 /* write command register */
30 #define DMA1_WREQ 0x09 /* write request register */
31 #define DMA1_SNGL 0x0A /* write single bit register */
32 #define DMA1_MODE 0x0B /* write mode register */
33 #define DMA1_CLRFF 0x0C /* clear byte ptr flip/flop */
34 #define DMA1_MCLR 0x0D /* master clear register */
35 #define DMA1_CLRM 0x0E /* clear mask register */
36 #define DMA1_WRTALL 0x0F /* write all mask register */
38 /* DMA Controler #2 (16-bit controller) */
39 #define DMA2_STAT 0xD0 /* read status register */
40 #define DMA2_WCMD 0xD0 /* write command register */
41 #define DMA2_WREQ 0xD2 /* write request register */
42 #define DMA2_SNGL 0xD4 /* write single bit register */
43 #define DMA2_MODE 0xD6 /* write mode register */
44 #define DMA2_CLRFF 0xD8 /* clear byte ptr flip/flop */
45 #define DMA2_MCLR 0xDA /* master clear register */
46 #define DMA2_CLRM 0xDC /* clear mask register */
47 #define DMA2_WRTALL 0xDE /* write all mask register */
49 #define DMA0_ADDR 0x00 /* chan 0 base adddress */
50 #define DMA0_CNT 0x01 /* chan 0 base count */
51 #define DMA1_ADDR 0x02 /* chan 1 base adddress */
52 #define DMA1_CNT 0x03 /* chan 1 base count */
53 #define DMA2_ADDR 0x04 /* chan 2 base adddress */
54 #define DMA2_CNT 0x05 /* chan 2 base count */
55 #define DMA3_ADDR 0x06 /* chan 3 base adddress */
56 #define DMA3_CNT 0x07 /* chan 3 base count */
57 #define DMA4_ADDR 0xC0 /* chan 4 base adddress */
58 #define DMA4_CNT 0xC2 /* chan 4 base count */
59 #define DMA5_ADDR 0xC4 /* chan 5 base adddress */
60 #define DMA5_CNT 0xC6 /* chan 5 base count */
61 #define DMA6_ADDR 0xC8 /* chan 6 base adddress */
62 #define DMA6_CNT 0xCA /* chan 6 base count */
63 #define DMA7_ADDR 0xCC /* chan 7 base adddress */
64 #define DMA7_CNT 0xCE /* chan 7 base count */
66 #define DMA0_PAGE 0x87 /* chan 0 page register (refresh)*/
67 #define DMA1_PAGE 0x83 /* chan 1 page register */
68 #define DMA2_PAGE 0x81 /* chan 2 page register */
69 #define DMA3_PAGE 0x82 /* chan 3 page register */
70 #define DMA4_PAGE 0x8F /* chan 4 page register (unuseable)*/
71 #define DMA5_PAGE 0x8B /* chan 5 page register */
72 #define DMA6_PAGE 0x89 /* chan 6 page register */
73 #define DMA7_PAGE 0x8A /* chan 7 page register */
77 #define DMA_DECREMENT 0x20 /* mask to make DMA hardware go backwards */
80 UBYTE dma_disable; /* bits to disable dma channel */
81 UBYTE dma_enable; /* bits to enable dma channel */
82 UWORD page; /* page port location */
83 UWORD addr; /* addr port location */
84 UWORD count; /* count port location */
85 UWORD single; /* single mode port location */
86 UWORD mode; /* mode port location */
87 UWORD clear_ff; /* clear flip-flop port location */
88 UBYTE write; /* bits for write transfer */
89 UBYTE read; /* bits for read transfer */
92 /* Variables needed ... */
94 static DMA_ENTRY mydma[MAX_DMA] = {
97 {0x04,0x00,DMA0_PAGE,DMA0_ADDR,DMA0_CNT,
98 DMA1_SNGL,DMA1_MODE,DMA1_CLRFF,0x48,0x44},
101 {0x05,0x01,DMA1_PAGE,DMA1_ADDR,DMA1_CNT,
102 DMA1_SNGL,DMA1_MODE,DMA1_CLRFF,0x49,0x45},
105 {0x06,0x02,DMA2_PAGE,DMA2_ADDR,DMA2_CNT,
106 DMA1_SNGL,DMA1_MODE,DMA1_CLRFF,0x4A,0x46},
109 {0x07,0x03,DMA3_PAGE,DMA3_ADDR,DMA3_CNT,
110 DMA1_SNGL,DMA1_MODE,DMA1_CLRFF,0x4B,0x47},
113 {0x04,0x00,DMA4_PAGE,DMA4_ADDR,DMA4_CNT,
114 DMA2_SNGL,DMA2_MODE,DMA2_CLRFF,0x48,0x44},
117 {0x05,0x01,DMA5_PAGE,DMA5_ADDR,DMA5_CNT,
118 DMA2_SNGL,DMA2_MODE,DMA2_CLRFF,0x49,0x45},
121 {0x06,0x02,DMA6_PAGE,DMA6_ADDR,DMA6_CNT,
122 DMA2_SNGL,DMA2_MODE,DMA2_CLRFF,0x4A,0x46},
125 {0x07,0x03,DMA7_PAGE,DMA7_ADDR,DMA7_CNT,
126 DMA2_SNGL,DMA2_MODE,DMA2_CLRFF,0x4B,0x47},
132 Each specialised DMA code part should provide the following things:
136 - a DMAMEM typedef, which should contain all the data that the
137 routines need for maintaining/allocating/freeing dma memory.
142 - 2 macros ENTER_CRITICAL and LEAVE_CRITICAL
144 - A function 'static BOOL MDma_AllocMem0(DMAMEM *dm,UWORD size)'
145 which should perform the actual dma-memory allocation. It should
146 use DMAMEM *dm to store all it's information.
148 - A function 'static void MDma_FreeMem0(DMAMEM *dm)' to free the memory
150 - A function 'static ULONG MDma_GetLinearPtr(DMAMEM *dm)' which should
151 return the linear 20 bits pointer to the actual dmabuffer.. this
152 function is used by MDma_Start
154 - A function 'void *MDma_GetPtr(DMAMEM *dm)' which should return a pointer
155 to the dmabuffer. If the dma memory can't be accessed directly it should
156 return a pointer to a FAKE dma buffer (DJGPP!!)
158 - A function 'void MDma_Commit(DMAMEM *dm,UWORD index,UWORD count)'. This
159 function will be called each time a routine wrote something to the
160 dmabuffer (returned by MDma_GetPtr()). In the case of a FAKE dmabuffer
161 this routine should take care of copying the data from the fake buffer to
162 the real dma memory ('count' bytes from byteoffset 'index').
170 /****************************************************************************
171 ********************* Watcom C specialised DMA code: ************************
172 ****************************************************************************/
174 #define ENTER_CRITICAL IRQ_PUSH_OFF()
175 extern void IRQ_PUSH_OFF (void);
176 #pragma aux IRQ_PUSH_OFF = \
181 #define LEAVE_CRITICAL IRQ_POP()
182 extern void IRQ_POP (void);
183 #pragma aux IRQ_POP = \
188 static BOOL MDma_AllocMem0(DMAMEM *dm,UWORD size)
190 Allocates a dma buffer of 'size' bytes.
191 returns FALSE if failed.
197 /* allocate TWICE the size of the requested dma buffer..
198 this fixes the 'page-crossing' bug of previous versions */
200 r.x.eax = 0x0100; /* DPMI allocate DOS memory */
201 r.x.ebx = ((size*2) + 15) >> 4; /* Number of paragraphs requested */
203 int386 (0x31, &r, &r);
205 if( r.x.cflag ) return 0; /* failed */
207 dm->raw_selector=r.x.edx;
209 /* convert the segment into a linear address */
211 p=(r.x.eax&0xffff)<<4;
213 /* if the first half of the allocated memory crosses a page
214 boundary, return the second half which is then guaranteed to
215 be page-continuous */
217 if( (p>>16) != ((p+size-1)>>16) ) p+=size;
219 dm->continuous=(void *)p;
225 static void MDma_FreeMem0(DMAMEM *dm)
228 r.x.eax = 0x0101; /* DPMI free DOS memory */
229 r.x.edx = dm->raw_selector; /* base selector */
230 int386 (0x31, &r, &r);
234 static ULONG MDma_GetLinearPtr(DMAMEM *dm)
236 return (ULONG)dm->continuous;
240 void *MDma_GetPtr(DMAMEM *dm)
242 return(dm->continuous);
246 void MDma_Commit(DMAMEM *dm,UWORD index,UWORD count)
248 /* This function doesnt do anything here (WATCOM C
249 can access dma memory directly) */
253 #elif defined(__DJGPP__)
254 /****************************************************************************
255 *********************** DJGPP specialised DMA code: *************************
256 ****************************************************************************/
257 #define ENTER_CRITICAL __asm__( "pushf \n\t popl %0 \n\t cli" : "=g" (__flags))
258 #define LEAVE_CRITICAL __asm__( "pushl %0 \n\t popf" : : "g" (__flags))
259 #include <sys/farptr.h>
260 #include <sys/movedata.h>
262 static BOOL MDma_AllocMem0(DMAMEM *dm,UWORD size)
264 Allocates a dma buffer of 'size' bytes - one in the code segment and
265 one in the lower 1 Mb physical mem.
266 It checks if the dma mem is page-continuous, and can only be
267 used to allocate exactly 1 block.
272 if (!(dm->continuous = (void *) malloc(size)))
276 if ((seg = __dpmi_allocate_dos_memory((size * 2 + 15) >> 4, &sel)) < 0)
278 free(dm->continuous);
282 if ((seg & 0xf000) != ((seg + (size >> 4)) & 0xf000)) // crosses boundary?
288 dm->raw.pm_offset = 0;
289 dm->raw.pm_selector = sel;
290 dm->raw.rm_offset = 0;
291 dm->raw.rm_segment = seg;
298 static void MDma_FreeMem0(DMAMEM *dm)
300 __dpmi_free_dos_memory(dm->raw.pm_selector);
301 free(dm->continuous);
304 static ULONG MDma_GetLinearPtr(DMAMEM *dm)
306 return (ULONG) dm->raw.rm_segment << 4;
310 void *MDma_GetPtr(DMAMEM *dm)
312 return(dm->continuous);
315 void MDma_Commit(DMAMEM *dm,UWORD index,UWORD count)
317 char *src = &(((UBYTE*)dm->continuous)[index]);
318 ULONG dest = 16 * dm->raw.rm_segment + (ULONG) index;
320 _farsetsel(_go32_conventional_mem_selector());
322 _farnspokeb(dest++, *(src++));
325 dosmemput(src, count, dest);
330 /****************************************************************************
331 ********************* Borland C specialised DMA code: ***********************
332 ****************************************************************************/
334 #define ENTER_CRITICAL asm{ pushf; cli }
335 #define LEAVE_CRITICAL asm{ popf }
337 #define LPTR(ptr) (((ULONG)FP_SEG(ptr)<<4)+FP_OFF(ptr))
338 #define NPTR(ptr) MK_FP(FP_SEG(p)+(FP_OFF(p)>>4),FP_OFF(p)&15)
341 static BOOL MDma_AllocMem0(DMAMEM *dm,UWORD size)
343 Allocates a dma buffer of 'size' bytes.
344 returns FALSE if failed.
350 /* allocate TWICE the size of the requested dma buffer..
351 so we can always get a page-contiguous dma buffer */
353 if((dm->raw=malloc((ULONG)size*2))==NULL) return 0;
355 p=(char huge *)dm->raw;
358 /* if the first half of the allocated memory crosses a page
359 boundary, return the second half which is then guaranteed to
360 be page-continuous */
362 if( (s>>16) != ((s+size-1)>>16) ) p+=size;
364 /* put the page-continuous pointer into DMAMEM */
366 dm->continuous=NPTR(p);
372 static void MDma_FreeMem0(DMAMEM *dm)
378 static ULONG MDma_GetLinearPtr(DMAMEM *dm)
380 return LPTR(dm->continuous);
384 void *MDma_GetPtr(DMAMEM *dm)
386 return(dm->continuous);
391 void MDma_Commit(DMAMEM *dm,UWORD index,UWORD count)
393 /* This function doesnt do anything here (BORLAND C
394 can access dma memory directly) */
400 /****************************************************************************
401 ************************* General DMA code: *********************************
402 ****************************************************************************/
405 DMAMEM *MDma_AllocMem(UWORD size)
409 /* allocate dma memory structure */
411 if(!(p=(DMAMEM *)malloc(sizeof(DMAMEM)))) return NULL;
413 /* allocate dma memory */
415 if(!MDma_AllocMem0(p,size)){
417 /* didn't succeed? -> free everything & return NULL */
427 void MDma_FreeMem(DMAMEM *p)
434 int MDma_Start(int channel,DMAMEM *dm,UWORD size,int type)
437 ULONG s_20bit,e_20bit;
438 UWORD spage,saddr,tcount;
439 UWORD epage/*,eaddr*/;
443 tdma=&mydma[channel]; /* point to this dma data */
445 /* Convert the pc address to a 20 bit physical
446 address that the DMA controller needs */
448 s_20bit = MDma_GetLinearPtr(dm);
450 e_20bit = s_20bit + size - 1;
454 if(spage != epage) return 0;
457 /* if 16-bit xfer, then addr,count & size are divided by 2 */
458 s_20bit = s_20bit >> 1;
459 e_20bit = e_20bit >> 1;
463 saddr=s_20bit&0xffff;
470 cur_mode = tdma->read;
474 cur_mode = tdma->write;
478 cur_mode = tdma->read | 0x10; /* turn on auto init */
482 cur_mode = tdma->write | 0x10; /* turn on auto init */
490 outportb(tdma->single,tdma->dma_disable); /* disable channel */
491 outportb(tdma->mode,cur_mode); /* set mode */
492 outportb(tdma->clear_ff,0); /* clear f/f */
493 outportb(tdma->addr,saddr&0xff); /* LSB */
494 outportb(tdma->addr,saddr>>8); /* MSB */
495 outportb(tdma->page,spage); /* page # */
496 outportb(tdma->clear_ff,0); /* clear f/f */
497 outportb(tdma->count,tcount&0x0ff); /* LSB count */
498 outportb(tdma->count,tcount>>8); /* MSB count */
499 outportb(tdma->single,tdma->dma_enable); /* enable */
506 void MDma_Stop(int channel)
509 tdma=&mydma[channel]; /* point to this dma data */
510 outportb(tdma->single,tdma->dma_disable); /* disable chan */
514 UWORD MDma_Todo(int channel)
520 DMA_ENTRY *tdma=&mydma[channel];
526 outportb(tdma->clear_ff,0xff);
530 val1|=inportb(creg)<<8;
532 val2|=inportb(creg)<<8;
535 if((SWORD)val1>64) goto redo;
536 if((SWORD)val1<-64) goto redo;
540 if(channel>3) val2<<=1;