-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha_dma.c
231 lines (174 loc) · 5.5 KB
/
a_dma.c
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
/*
Copyright (C) 1994-1995 Apogee Software, Ltd.
Copyright (C) 2023 Frenkel Smeijers
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 "id_heads.h"
#include "a_dma.h"
#define DMA_MaxChannel 7
#define VALID ( 1 == 1 )
#define INVALID ( !VALID )
#define BYTE 0
#define WORD 1
typedef struct
{
int32_t Valid;
int32_t Width;
int32_t Mask;
int32_t Mode;
int32_t Clear;
int32_t Page;
int32_t Address;
int32_t 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}
};
/*---------------------------------------------------------------------
Function: DMA_VerifyChannel
Verifies whether a DMA channel is available to transfer data.
---------------------------------------------------------------------*/
int32_t DMA_VerifyChannel(int32_t channel)
{
if (!(0 <= channel && channel <= DMA_MaxChannel))
return DMA_Error;
else if (DMA_PortInfo[channel].Valid == INVALID)
return DMA_Error;
else
return DMA_Ok;
}
/*---------------------------------------------------------------------
Function: DMA_SetupTransfer
Programs the specified DMA channel to transfer data.
---------------------------------------------------------------------*/
int32_t DMA_SetupTransfer(int32_t channel, uint8_t *address, int32_t length)
{
const DMA_PORT *Port;
int32_t addr;
int32_t ChannelSelect;
int32_t Page;
int32_t HiByte;
int32_t LoByte;
int32_t TransferLength;
int32_t status;
status = DMA_VerifyChannel(channel);
if (status == DMA_Ok)
{
Port = &DMA_PortInfo[channel];
ChannelSelect = channel & 0x3;
addr = ((int32_t)address) - __djgpp_conventional_base;
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 to AutoInitRead
outp(Port->Mode, 0x58 | ChannelSelect);
// Send address
outp(Port->Address, LoByte);
outp(Port->Address, HiByte);
// Send page
outp(Port->Page, Page);
// Send length
outp(Port->Length, LOBYTE(TransferLength));
outp(Port->Length, HIBYTE(TransferLength));
// enable DMA channel
outp(Port->Mask, ChannelSelect);
}
return status;
}
/*---------------------------------------------------------------------
Function: DMA_EndTransfer
Ends use of the specified DMA channel.
---------------------------------------------------------------------*/
int32_t DMA_EndTransfer(int32_t channel)
{
const DMA_PORT *Port;
int32_t ChannelSelect;
int32_t 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.
---------------------------------------------------------------------*/
uint8_t *DMA_GetCurrentPos(int32_t channel)
{
const DMA_PORT *Port;
uint32_t addr = 0;
int32_t 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 (uint8_t *)(addr + __djgpp_conventional_base);
}