Debian Codegraph @

src/hardware/dma.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: dma.cpp,v 1.41 2009-07-24 09:56:14 c2woody Exp $ */
20
21 #include <string.h>
22 #include "dosbox.h"
23 #include "mem.h"
24 #include "inout.h"
25 #include "dma.h"
26 #include "pic.h"
27 #include "paging.h"
28 #include "setup.h"
29
30 DmaController *DmaControllers[2];
31
32 #define EMM_PAGEFRAME4K ((0xE000*16)/4096)
33 Bit32u ems_board_mapping[LINK_START];
34
35 static void UpdateEMSMapping(void) {
36 /* if EMS is not present, this will result in a 1:1 mapping */
37 Bitu i;
38 for (i=0;i<0x10;i++) {
39 ems_board_mapping[EMM_PAGEFRAME4K+i]=paging.firstmb[EMM_PAGEFRAME4K+i];
40 }
41 }
42
43 /* read a block from physical memory */
44 static void DMA_BlockRead(PhysPt spage,PhysPt offset,void * data,Bitu size,Bit8u dma16) {
45 Bit8u * write=(Bit8u *) data;
46 Bitu highpart_addr_page = spage>>12;
47 size <<= dma16;
48 offset <<= dma16;
49 Bit32u dma_wrap = ((0xffff<<dma16)+dma16) | dma_wrapping;
50 for ( ; size ; size--, offset++) {
51 if (offset>(dma_wrapping<<dma16)) E_Exit("DMA segbound wrapping (read)");
52 offset &= dma_wrap;
53 Bitu page = highpart_addr_page+(offset >> 12);
54 /* care for EMS pageframe etc. */
55 if (page < EMM_PAGEFRAME4K) page = paging.firstmb[page];
56 else if (page < EMM_PAGEFRAME4K+0x10) page = ems_board_mapping[page];
57 else if (page < LINK_START) page = paging.firstmb[page];
58 *write++=phys_readb(page*4096 + (offset & 4095));
59 }
60 }
61
62 /* write a block into physical memory */
63 static void DMA_BlockWrite(PhysPt spage,PhysPt offset,void * data,Bitu size,Bit8u dma16) {
64 Bit8u * read=(Bit8u *) data;
65 Bitu highpart_addr_page = spage>>12;
66 size <<= dma16;
67 offset <<= dma16;
68 Bit32u dma_wrap = ((0xffff<<dma16)+dma16) | dma_wrapping;
69 for ( ; size ; size--, offset++) {
70 if (offset>(dma_wrapping<<dma16)) E_Exit("DMA segbound wrapping (write)");
71 offset &= dma_wrap;
72 Bitu page = highpart_addr_page+(offset >> 12);
73 /* care for EMS pageframe etc. */
74 if (page < EMM_PAGEFRAME4K) page = paging.firstmb[page];
75 else if (page < EMM_PAGEFRAME4K+0x10) page = ems_board_mapping[page];
76 else if (page < LINK_START) page = paging.firstmb[page];
77 phys_writeb(page*4096 + (offset & 4095), *read++);
78 }
79 }
80
81 DmaChannel * GetDMAChannel(Bit8u chan) {
82 if (chan<4) {
83 /* channel on first DMA controller */
84 if (DmaControllers[0]) return DmaControllers[0]->GetChannel(chan);
85 } else if (chan<8) {
86 /* channel on second DMA controller */
87 if (DmaControllers[1]) return DmaControllers[1]->GetChannel(chan-4);
88 }
89 return NULL;
90 }
91
92 /* remove the second DMA controller (ports are removed automatically) */
93 void CloseSecondDMAController(void) {
94 if (DmaControllers[1]) {
95 delete DmaControllers[1];
96 DmaControllers[1]=NULL;
97 }
98 }
99
100 /* check availability of second DMA controller, needed for SB16 */
101 bool SecondDMAControllerAvailable(void) {
102 if (DmaControllers[1]) return true;
103 else return false;
104 }
105
106 static void DMA_Write_Port(Bitu port,Bitu val,Bitu /*iolen*/) {
107 if (port<0x10) {
108 /* write to the first DMA controller (channels 0-3) */
109 DmaControllers[0]->WriteControllerReg(port,val,1);
110 } else if (port>=0xc0 && port <=0xdf) {
111 /* write to the second DMA controller (channels 4-7) */
112 DmaControllers[1]->WriteControllerReg((port-0xc0) >> 1,val,1);
113 } else {
114 UpdateEMSMapping();
115 switch (port) {
116 /* write DMA page register */
117 case 0x81:GetDMAChannel(2)->SetPage((Bit8u)val);break;
118 case 0x82:GetDMAChannel(3)->SetPage((Bit8u)val);break;
119 case 0x83:GetDMAChannel(1)->SetPage((Bit8u)val);break;
120 case 0x89:GetDMAChannel(6)->SetPage((Bit8u)val);break;
121 case 0x8a:GetDMAChannel(7)->SetPage((Bit8u)val);break;
122 case 0x8b:GetDMAChannel(5)->SetPage((Bit8u)val);break;
123 }
124 }
125 }
126
127 static Bitu DMA_Read_Port(Bitu port,Bitu iolen) {
128 if (port<0x10) {
129 /* read from the first DMA controller (channels 0-3) */
130 return DmaControllers[0]->ReadControllerReg(port,iolen);
131 } else if (port>=0xc0 && port <=0xdf) {
132 /* read from the second DMA controller (channels 4-7) */
133 return DmaControllers[1]->ReadControllerReg((port-0xc0) >> 1,iolen);
134 } else switch (port) {
135 /* read DMA page register */
136 case 0x81:return GetDMAChannel(2)->pagenum;
137 case 0x82:return GetDMAChannel(3)->pagenum;
138 case 0x83:return GetDMAChannel(1)->pagenum;
139 case 0x89:return GetDMAChannel(6)->pagenum;
140 case 0x8a:return GetDMAChannel(7)->pagenum;
141 case 0x8b:return GetDMAChannel(5)->pagenum;
142 }
143 return 0;
144 }
145
146 void DmaController::WriteControllerReg(Bitu reg,Bitu val,Bitu /*len*/) {
147 DmaChannel * chan;
148 switch (reg) {
149 /* set base address of DMA transfer (1st byte low part, 2nd byte high part) */
150 case 0x0:case 0x2:case 0x4:case 0x6:
151 UpdateEMSMapping();
152 chan=GetChannel((Bit8u)(reg >> 1));
153 flipflop=!flipflop;
154 if (flipflop) {
155 chan->baseaddr=(chan->baseaddr&0xff00)|val;
156 chan->curraddr=(chan->curraddr&0xff00)|val;
157 } else {
158 chan->baseaddr=(chan->baseaddr&0x00ff)|(val << 8);
159 chan->curraddr=(chan->curraddr&0x00ff)|(val << 8);
160 }
161 break;
162 /* set DMA transfer count (1st byte low part, 2nd byte high part) */
163 case 0x1:case 0x3:case 0x5:case 0x7:
164 UpdateEMSMapping();
165 chan=GetChannel((Bit8u)(reg >> 1));
166 flipflop=!flipflop;
167 if (flipflop) {
168 chan->basecnt=(chan->basecnt&0xff00)|val;
169 chan->currcnt=(chan->currcnt&0xff00)|val;
170 } else {
171 chan->basecnt=(chan->basecnt&0x00ff)|(val << 8);
172 chan->currcnt=(chan->currcnt&0x00ff)|(val << 8);
173 }
174 break;
175 case 0x8: /* Comand reg not used */
176 break;
177 case 0x9: /* Request registers, memory to memory */
178 //TODO Warning?
179 break;
180 case 0xa: /* Mask Register */
181 if ((val & 0x4)==0) UpdateEMSMapping();
182 chan=GetChannel(val & 3);
183 chan->SetMask((val & 0x4)>0);
184 break;
185 case 0xb: /* Mode Register */
186 UpdateEMSMapping();
187 chan=GetChannel(val & 3);
188 chan->autoinit=(val & 0x10) > 0;
189 chan->increment=(val & 0x20) > 0;
190 //TODO Maybe other bits?
191 break;
192 case 0xc: /* Clear Flip/Flip */
193 flipflop=false;
194 break;
195 case 0xd: /* Master Clear/Reset */
196 for (Bit8u ct=0;ct<4;ct++) {
197 chan=GetChannel(ct);
198 chan->SetMask(true);
199 chan->tcount=false;
200 }
201 flipflop=false;
202 break;
203 case 0xe: /* Clear Mask register */
204 UpdateEMSMapping();
205 for (Bit8u ct=0;ct<4;ct++) {
206 chan=GetChannel(ct);
207 chan->SetMask(false);
208 }
209 break;
210 case 0xf: /* Multiple Mask register */
211 UpdateEMSMapping();
212 for (Bit8u ct=0;ct<4;ct++) {
213 chan=GetChannel(ct);
214 chan->SetMask(val & 1);
215 val>>=1;
216 }
217 break;
218 }
219 }
220
221 Bitu DmaController::ReadControllerReg(Bitu reg,Bitu /*len*/) {
222 DmaChannel * chan;Bitu ret;
223 switch (reg) {
224 /* read base address of DMA transfer (1st byte low part, 2nd byte high part) */
225 case 0x0:case 0x2:case 0x4:case 0x6:
226 chan=GetChannel((Bit8u)(reg >> 1));
227 flipflop=!flipflop;
228 if (flipflop) {
229 return chan->curraddr & 0xff;
230 } else {
231 return (chan->curraddr >> 8) & 0xff;
232 }
233 /* read DMA transfer count (1st byte low part, 2nd byte high part) */
234 case 0x1:case 0x3:case 0x5:case 0x7:
235 chan=GetChannel((Bit8u)(reg >> 1));
236 flipflop=!flipflop;
237 if (flipflop) {
238 return chan->currcnt & 0xff;
239 } else {
240 return (chan->currcnt >> 8) & 0xff;
241 }
242 case 0x8: /* Status Register */
243 ret=0;
244 for (Bit8u ct=0;ct<4;ct++) {
245 chan=GetChannel(ct);
246 if (chan->tcount) ret|=1 << ct;
247 chan->tcount=false;
248 if (chan->request) ret|=1 << (4+ct);
249 }
250 return ret;
251 default:
252 LOG(LOG_DMACONTROL,LOG_NORMAL)("Trying to read undefined DMA port %x",reg);
253 break;
254 }
255 return 0xffffffff;
256 }
257
258 DmaChannel::DmaChannel(Bit8u num, bool dma16) {
259 masked = true;
260 callback = NULL;
261 if(num == 4) return;
262 channum = num;
263 DMA16 = dma16 ? 0x1 : 0x0;
264 pagenum = 0;
265 pagebase = 0;
266 baseaddr = 0;
267 curraddr = 0;
268 basecnt = 0;
269 currcnt = 0;
270 increment = true;
271 autoinit = false;
272 tcount = false;
273 request = false;
274 }
275
276 Bitu DmaChannel::Read(Bitu want, Bit8u * buffer) {
277 Bitu done=0;
278 curraddr &= dma_wrapping;
279 again:
280 Bitu left=(currcnt+1);
281 if (want<left) {
282 DMA_BlockRead(pagebase,curraddr,buffer,want,DMA16);
283 done+=want;
284 curraddr+=want;
285 currcnt-=want;
286 } else {
287 DMA_BlockRead(pagebase,curraddr,buffer,want,DMA16);
288 buffer+=left << DMA16;
289 want-=left;
290 done+=left;
291 ReachedTC();
292 if (autoinit) {
293 currcnt=basecnt;
294 curraddr=baseaddr;
295 if (want) goto again;
296 UpdateEMSMapping();
297 } else {
298 curraddr+=left;
299 currcnt=0xffff;
300 masked=true;
301 UpdateEMSMapping();
302 DoCallBack(DMA_TRANSFEREND);
303 }
304 }
305 return done;
306 }
307
308 Bitu DmaChannel::Write(Bitu want, Bit8u * buffer) {
309 Bitu done=0;
310 curraddr &= dma_wrapping;
311 again:
312 Bitu left=(currcnt+1);
313 if (want<left) {
314 DMA_BlockWrite(pagebase,curraddr,buffer,want,DMA16);
315 done+=want;
316 curraddr+=want;
317 currcnt-=want;
318 } else {
319 DMA_BlockWrite(pagebase,curraddr,buffer,left,DMA16);
320 buffer+=left << DMA16;
321 want-=left;
322 done+=left;
323 ReachedTC();
324 if (autoinit) {
325 currcnt=basecnt;
326 curraddr=baseaddr;
327 if (want) goto again;
328 UpdateEMSMapping();
329 } else {
330 curraddr+=left;
331 currcnt=0xffff;
332 masked=true;
333 UpdateEMSMapping();
334 DoCallBack(DMA_TRANSFEREND);
335 }
336 }
337 return done;
338 }
339
340 class DMA:public Module_base{
341 public:
342 DMA(Section* configuration):Module_base(configuration){
343 Bitu i;
344 DmaControllers[0] = new DmaController(0);
345 if (IS_EGAVGA_ARCH) DmaControllers[1] = new DmaController(1);
346 else DmaControllers[1] = NULL;
347
348 for (i=0;i<0x10;i++) {
349 Bitu mask=IO_MB;
350 if (i<8) mask|=IO_MW;
351 /* install handler for first DMA controller ports */
352 DmaControllers[0]->DMA_WriteHandler[i].Install(i,DMA_Write_Port,mask);
353 DmaControllers[0]->DMA_ReadHandler[i].Install(i,DMA_Read_Port,mask);
354 if (IS_EGAVGA_ARCH) {
355 /* install handler for second DMA controller ports */
356 DmaControllers[1]->DMA_WriteHandler[i].Install(0xc0+i*2,DMA_Write_Port,mask);
357 DmaControllers[1]->DMA_ReadHandler[i].Install(0xc0+i*2,DMA_Read_Port,mask);
358 }
359 }
360 /* install handlers for ports 0x81-0x83 (on the first DMA controller) */
361 DmaControllers[0]->DMA_WriteHandler[0x10].Install(0x81,DMA_Write_Port,IO_MB,3);
362 DmaControllers[0]->DMA_ReadHandler[0x10].Install(0x81,DMA_Read_Port,IO_MB,3);
363
364 if (IS_EGAVGA_ARCH) {
365 /* install handlers for ports 0x81-0x83 (on the second DMA controller) */
366 DmaControllers[1]->DMA_WriteHandler[0x10].Install(0x89,DMA_Write_Port,IO_MB,3);
367 DmaControllers[1]->DMA_ReadHandler[0x10].Install(0x89,DMA_Read_Port,IO_MB,3);
368 }
369 }
370 ~DMA(){
371 if (DmaControllers[0]) {
372 delete DmaControllers[0];
373 DmaControllers[0]=NULL;
374 }
375 if (DmaControllers[1]) {
376 delete DmaControllers[1];
377 DmaControllers[1]=NULL;
378 }
379 }
380 };
381
382 void DMA_SetWrapping(Bitu wrap) {
383 dma_wrapping = wrap;
384 }
385
386 static DMA* test;
387
388 void DMA_Destroy(Section* /*sec*/){
389 delete test;
390 }
391 void DMA_Init(Section* sec) {
392 DMA_SetWrapping(0xffff);
393 test = new DMA(sec);
394 sec->AddDestroyFunction(&DMA_Destroy);
395 Bitu i;
396 for (i=0;i<LINK_START;i++) {
397 ems_board_mapping[i]=i;
398 }
399 }