1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
/*
Name:
MDMA.C
Description:
DMA routines
Portability:
MSDOS: BC(y) Watcom(y) DJGPP(y)
Win95: n
Os2: n
Linux: n
(y) - yes
(n) - no (not possible or not useful)
(?) - may be possible, but not tested
*/
#include <dos.h>
#include <malloc.h>
#include <conio.h>
#include "mdma.h"
/* DMA Controler #1 (8-bit controller) */
#define DMA1_STAT 0x08 /* read status register */
#define DMA1_WCMD 0x08 /* write command register */
#define DMA1_WREQ 0x09 /* write request register */
#define DMA1_SNGL 0x0A /* write single bit register */
#define DMA1_MODE 0x0B /* write mode register */
#define DMA1_CLRFF 0x0C /* clear byte ptr flip/flop */
#define DMA1_MCLR 0x0D /* master clear register */
#define DMA1_CLRM 0x0E /* clear mask register */
#define DMA1_WRTALL 0x0F /* write all mask register */
/* DMA Controler #2 (16-bit controller) */
#define DMA2_STAT 0xD0 /* read status register */
#define DMA2_WCMD 0xD0 /* write command register */
#define DMA2_WREQ 0xD2 /* write request register */
#define DMA2_SNGL 0xD4 /* write single bit register */
#define DMA2_MODE 0xD6 /* write mode register */
#define DMA2_CLRFF 0xD8 /* clear byte ptr flip/flop */
#define DMA2_MCLR 0xDA /* master clear register */
#define DMA2_CLRM 0xDC /* clear mask register */
#define DMA2_WRTALL 0xDE /* write all mask register */
#define DMA0_ADDR 0x00 /* chan 0 base adddress */
#define DMA0_CNT 0x01 /* chan 0 base count */
#define DMA1_ADDR 0x02 /* chan 1 base adddress */
#define DMA1_CNT 0x03 /* chan 1 base count */
#define DMA2_ADDR 0x04 /* chan 2 base adddress */
#define DMA2_CNT 0x05 /* chan 2 base count */
#define DMA3_ADDR 0x06 /* chan 3 base adddress */
#define DMA3_CNT 0x07 /* chan 3 base count */
#define DMA4_ADDR 0xC0 /* chan 4 base adddress */
#define DMA4_CNT 0xC2 /* chan 4 base count */
#define DMA5_ADDR 0xC4 /* chan 5 base adddress */
#define DMA5_CNT 0xC6 /* chan 5 base count */
#define DMA6_ADDR 0xC8 /* chan 6 base adddress */
#define DMA6_CNT 0xCA /* chan 6 base count */
#define DMA7_ADDR 0xCC /* chan 7 base adddress */
#define DMA7_CNT 0xCE /* chan 7 base count */
#define DMA0_PAGE 0x87 /* chan 0 page register (refresh) */
#define DMA1_PAGE 0x83 /* chan 1 page register */
#define DMA2_PAGE 0x81 /* chan 2 page register */
#define DMA3_PAGE 0x82 /* chan 3 page register */
#define DMA4_PAGE 0x8F /* chan 4 page register (unuseable) */
#define DMA5_PAGE 0x8B /* chan 5 page register */
#define DMA6_PAGE 0x89 /* chan 6 page register */
#define DMA7_PAGE 0x8A /* chan 7 page register */
#define MAX_DMA 8
#define DMA_DECREMENT 0x20 /* mask to make DMA hardware go backwards */
typedef struct {
UBYTE dma_disable; /* bits to disable dma channel */
UBYTE dma_enable; /* bits to enable dma channel */
UWORD page; /* page port location */
UWORD addr; /* addr port location */
UWORD count; /* count port location */
UWORD single; /* single mode port location */
UWORD mode; /* mode port location */
UWORD clear_ff; /* clear flip-flop port location */
UBYTE write; /* bits for write transfer */
UBYTE read; /* bits for read transfer */
} DMA_ENTRY;
/* Variables needed ... */
static DMA_ENTRY mydma[MAX_DMA] =
{
/* DMA channel 0 */
{0x04, 0x00, DMA0_PAGE, DMA0_ADDR, DMA0_CNT,
DMA1_SNGL, DMA1_MODE, DMA1_CLRFF, 0x48, 0x44},
/* DMA channel 1 */
{0x05, 0x01, DMA1_PAGE, DMA1_ADDR, DMA1_CNT,
DMA1_SNGL, DMA1_MODE, DMA1_CLRFF, 0x49, 0x45},
/* DMA channel 2 */
{0x06, 0x02, DMA2_PAGE, DMA2_ADDR, DMA2_CNT,
DMA1_SNGL, DMA1_MODE, DMA1_CLRFF, 0x4A, 0x46},
/* DMA channel 3 */
{0x07, 0x03, DMA3_PAGE, DMA3_ADDR, DMA3_CNT,
DMA1_SNGL, DMA1_MODE, DMA1_CLRFF, 0x4B, 0x47},
/* DMA channel 4 */
{0x04, 0x00, DMA4_PAGE, DMA4_ADDR, DMA4_CNT,
DMA2_SNGL, DMA2_MODE, DMA2_CLRFF, 0x48, 0x44},
/* DMA channel 5 */
{0x05, 0x01, DMA5_PAGE, DMA5_ADDR, DMA5_CNT,
DMA2_SNGL, DMA2_MODE, DMA2_CLRFF, 0x49, 0x45},
/* DMA channel 6 */
{0x06, 0x02, DMA6_PAGE, DMA6_ADDR, DMA6_CNT,
DMA2_SNGL, DMA2_MODE, DMA2_CLRFF, 0x4A, 0x46},
/* DMA channel 7 */
{0x07, 0x03, DMA7_PAGE, DMA7_ADDR, DMA7_CNT,
DMA2_SNGL, DMA2_MODE, DMA2_CLRFF, 0x4B, 0x47},
};
/*
Each specialised DMA code part should provide the following things:
In MDMA.H:
- a DMAMEM typedef, which should contain all the data that the
routines need for maintaining/allocating/freeing dma memory.
In MDMA.C:
- 2 macros ENTER_CRITICAL and LEAVE_CRITICAL
- A function 'static BOOL MDma_AllocMem0(DMAMEM *dm,UWORD size)'
which should perform the actual dma-memory allocation. It should
use DMAMEM *dm to store all it's information.
- A function 'static void MDma_FreeMem0(DMAMEM *dm)' to free the memory
- A function 'static ULONG MDma_GetLinearPtr(DMAMEM *dm)' which should
return the linear 20 bits pointer to the actual dmabuffer.. this
function is used by MDma_Start
- A function 'void *MDma_GetPtr(DMAMEM *dm)' which should return a pointer
to the dmabuffer. If the dma memory can't be accessed directly it should
return a pointer to a FAKE dma buffer (DJGPP!!)
- A function 'void MDma_Commit(DMAMEM *dm,UWORD index,UWORD count)'. This
function will be called each time a routine wrote something to the
dmabuffer (returned by MDma_GetPtr()). In the case of a FAKE dmabuffer
this routine should take care of copying the data from the fake buffer to
the real dma memory ('count' bytes from byteoffset 'index').
*/
#ifdef __WATCOMC__
/****************************************************************************
********************* Watcom C specialised DMA code: ************************
****************************************************************************/
#define ENTER_CRITICAL IRQ_PUSH_OFF()
extern void IRQ_PUSH_OFF(void);
#pragma aux IRQ_PUSH_OFF = \
"pushfd", \
"cli" \
modify [esp];
#define LEAVE_CRITICAL IRQ_POP()
extern void IRQ_POP(void);
#pragma aux IRQ_POP = \
"popfd" \
modify [esp];
static BOOL MDma_AllocMem0(DMAMEM * dm, UWORD size)
/*
Allocates a dma buffer of 'size' bytes.
returns FALSE if failed.
*/
{
static union REGS r;
ULONG p;
/* allocate TWICE the size of the requested dma buffer..
this fixes the 'page-crossing' bug of previous versions */
r.x.eax = 0x0100; /* DPMI allocate DOS memory */
r.x.ebx = ((size * 2) + 15) >> 4; /* Number of paragraphs requested */
int386(0x31, &r, &r);
if (r.x.cflag)
return 0; /* failed */
dm->raw_selector = r.x.edx;
/* convert the segment into a linear address */
p = (r.x.eax & 0xffff) << 4;
/* if the first half of the allocated memory crosses a page
boundary, return the second half which is then guaranteed to
be page-continuous */
if ((p >> 16) != ((p + size - 1) >> 16))
p += size;
dm->continuous = (void *) p;
return 1;
}
static void MDma_FreeMem0(DMAMEM * dm)
{
static union REGS r;
r.x.eax = 0x0101; /* DPMI free DOS memory */
r.x.edx = dm->raw_selector; /* base selector */
int386(0x31, &r, &r);
}
static ULONG MDma_GetLinearPtr(DMAMEM * dm)
{
return (ULONG) dm->continuous;
}
void *MDma_GetPtr(DMAMEM * dm)
{
return (dm->continuous);
}
void MDma_Commit(DMAMEM * dm, UWORD index, UWORD count)
{
/* This function doesnt do anything here (WATCOM C
can access dma memory directly) */
}
#elif defined(__DJGPP__)
/****************************************************************************
*********************** DJGPP specialised DMA code: *************************
****************************************************************************/
#define ENTER_CRITICAL __asm__( "pushf \n\t cli" )
#define LEAVE_CRITICAL __asm__( "popf \n\t" )
#include <sys/farptr.h>
static BOOL MDma_AllocMem0(DMAMEM * dm, UWORD size)
/*
Allocates a dma buffer of 'size' bytes - one in the code segment and
one in the lower 1 Mb physical mem.
It doesn't check if the dma mem is page-continuous, and can only be
used to allocate exactly 1 block.
*/
{
dm->raw.size = (size + 15) >> 4;
if (_go32_dpmi_allocate_dos_memory(&(dm->raw)))
return 0;
dm->continuous = (void *) malloc(size);
return 1;
}
static void MDma_FreeMem0(DMAMEM * dm)
{
_go32_dpmi_free_dos_memory(&(dm->raw));
free(dm->continuous);
}
static ULONG MDma_GetLinearPtr(DMAMEM * dm)
{
return (ULONG) dm->raw.rm_segment << 4;
}
void *MDma_GetPtr(DMAMEM * dm)
{
return (dm->continuous);
}
void MDma_Commit(DMAMEM * dm, UWORD index, UWORD count)
{
char *src = &(((UBYTE *) dm->continuous)[index]);
ULONG dest = 16 * dm->raw.rm_segment + (ULONG) index;
_farsetsel(_go32_conventional_mem_selector());
while (count--) {
_farnspokeb(dest++, *(src++));
}
}
#else
/****************************************************************************
********************* Borland C specialised DMA code: ***********************
****************************************************************************/
#define ENTER_CRITICAL asm{ pushf; cli }
#define LEAVE_CRITICAL asm{ popf }
#define LPTR(ptr) (((ULONG)FP_SEG(ptr)<<4)+FP_OFF(ptr))
#define NPTR(ptr) MK_FP(FP_SEG(p)+(FP_OFF(p)>>4),FP_OFF(p)&15)
static BOOL MDma_AllocMem0(DMAMEM * dm, UWORD size)
/*
Allocates a dma buffer of 'size' bytes.
returns FALSE if failed.
*/
{
char huge *p;
ULONG s;
/* allocate TWICE the size of the requested dma buffer..
so we can always get a page-contiguous dma buffer */
if ((dm->raw = malloc((ULONG) size * 2)) == NULL)
return 0;
p = (char huge *) dm->raw;
s = LPTR(p);
/* if the first half of the allocated memory crosses a page
boundary, return the second half which is then guaranteed to
be page-continuous */
if ((s >> 16) != ((s + size - 1) >> 16))
p += size;
/* put the page-continuous pointer into DMAMEM */
dm->continuous = NPTR(p);
return 1;
}
static void MDma_FreeMem0(DMAMEM * dm)
{
free(dm->raw);
}
static ULONG MDma_GetLinearPtr(DMAMEM * dm)
{
return LPTR(dm->continuous);
}
void *MDma_GetPtr(DMAMEM * dm)
{
return (dm->continuous);
}
#pragma argsused
void MDma_Commit(DMAMEM * dm, UWORD index, UWORD count)
{
/* This function doesnt do anything here (BORLAND C
can access dma memory directly) */
}
#endif
/****************************************************************************
************************* General DMA code: *********************************
****************************************************************************/
DMAMEM *MDma_AllocMem(UWORD size)
{
DMAMEM *p;
/* allocate dma memory structure */
if (!(p = (DMAMEM *) malloc(sizeof(DMAMEM))))
return NULL;
/* allocate dma memory */
if (!MDma_AllocMem0(p, size)) {
/* didn't succeed? -> free everything & return NULL */
free(p);
return NULL;
}
return p;
}
void MDma_FreeMem(DMAMEM * p)
{
MDma_FreeMem0(p);
free(p);
}
int MDma_Start(int channel, DMAMEM * dm, UWORD size, int type)
{
DMA_ENTRY *tdma;
ULONG s_20bit, e_20bit;
UWORD spage, saddr, tcount;
UWORD epage, eaddr;
UBYTE cur_mode;
tdma = &mydma[channel]; /* point to this dma data */
/* Convert the pc address to a 20 bit physical
address that the DMA controller needs */
s_20bit = MDma_GetLinearPtr(dm);
e_20bit = s_20bit + size - 1;
spage = s_20bit >> 16;
epage = e_20bit >> 16;
if (spage != epage)
return 0;
if (channel >= 4) {
/* if 16-bit xfer, then addr,count & size are divided by 2 */
s_20bit = s_20bit >> 1;
e_20bit = e_20bit >> 1;
size = size >> 1;
}
saddr = s_20bit & 0xffff;
tcount = size - 1;
switch (type) {
case READ_DMA:
cur_mode = tdma->read;
break;
case WRITE_DMA:
cur_mode = tdma->write;
break;
case INDEF_READ:
cur_mode = tdma->read | 0x10; /* turn on auto init */
break;
case INDEF_WRITE:
cur_mode = tdma->write | 0x10; /* turn on auto init */
break;
}
ENTER_CRITICAL;
outportb(tdma->single, tdma->dma_disable); /* disable channel */
outportb(tdma->mode, cur_mode); /* set mode */
outportb(tdma->clear_ff, 0); /* clear f/f */
outportb(tdma->addr, saddr & 0xff); /* LSB */
outportb(tdma->addr, saddr >> 8); /* MSB */
outportb(tdma->page, spage); /* page # */
outportb(tdma->clear_ff, 0); /* clear f/f */
outportb(tdma->count, tcount & 0x0ff); /* LSB count */
outportb(tdma->count, tcount >> 8); /* MSB count */
outportb(tdma->single, tdma->dma_enable); /* enable */
LEAVE_CRITICAL;
return 1;
}
void MDma_Stop(int channel)
{
DMA_ENTRY *tdma;
tdma = &mydma[channel]; /* point to this dma data */
outportb(tdma->single, tdma->dma_disable); /* disable chan */
}
UWORD MDma_Todo(int channel)
{
UWORD creg;
UWORD val1, val2;
DMA_ENTRY *tdma = &mydma[channel];
creg = tdma->count;
ENTER_CRITICAL;
outportb(tdma->clear_ff, 0xff);
redo:
val1 = inportb(creg);
val1 |= inportb(creg) << 8;
val2 = inportb(creg);
val2 |= inportb(creg) << 8;
val1 -= val2;
if ((SWORD) val1 > 64)
goto redo;
if ((SWORD) val1 < -64)
goto redo;
LEAVE_CRITICAL;
if (channel > 3)
val2 <<= 1;
return val2;
}
|