4 * Copyright (c) 2003 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #define log(...) fprintf (stderr, "dma: " __VA_ARGS__)
28 #define lwarn(...) fprintf (stderr, "dma: " __VA_ARGS__)
29 #define linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
30 #define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
37 #define LENOFA(a) ((int) (sizeof(a)/sizeof(a[0])))
46 DMA_transfer_handler transfer_handler;
53 static struct dma_cont {
58 struct dma_regs regs[4];
62 CMD_MEMORY_TO_MEMORY = 0x01,
63 CMD_FIXED_ADDRESS = 0x02,
64 CMD_BLOCK_CONTROLLER = 0x04,
65 CMD_COMPRESSED_TIME = 0x08,
66 CMD_CYCLIC_PRIORITY = 0x10,
67 CMD_EXTENDED_WRITE = 0x20,
70 CMD_NOT_SUPPORTED = CMD_MEMORY_TO_MEMORY | CMD_FIXED_ADDRESS
71 | CMD_COMPRESSED_TIME | CMD_CYCLIC_PRIORITY | CMD_EXTENDED_WRITE
72 | CMD_LOW_DREQ | CMD_LOW_DACK
76 static void write_page (void *opaque, uint32_t nport, uint32_t data)
80 static int channels[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
83 ichan = channels[nport - 0x80 - (ncont << 3)];
86 log ("invalid channel %#x %#x\n", nport, data);
90 dma_controllers[ncont].regs[ichan].page = data;
93 static void init_chan (int ncont, int ichan)
97 r = dma_controllers[ncont].regs + ichan;
98 r->now[ADDR] = r->base[0] << ncont;
102 static inline int getff (int ncont)
106 ff = dma_controllers[ncont].flip_flop;
107 dma_controllers[ncont].flip_flop = !ff;
111 static uint32_t read_chan (void *opaque, uint32_t nport)
114 int ncont, ichan, nreg;
119 ichan = (nport >> (1 + ncont)) & 3;
120 nreg = (nport >> ncont) & 1;
121 r = dma_controllers[ncont].regs + ichan;
126 val = (r->base[COUNT] << ncont) - r->now[COUNT];
128 val = r->now[ADDR] + r->now[COUNT];
130 return (val >> (ncont + (ff << 3))) & 0xff;
133 static void write_chan (void *opaque, uint32_t nport, uint32_t data)
135 int ncont, ichan, nreg;
139 ichan = (nport >> (1 + ncont)) & 3;
140 nreg = (nport >> ncont) & 1;
141 r = dma_controllers[ncont].regs + ichan;
144 r->base[nreg] = (r->base[nreg] & 0xff) | ((data << 8) & 0xff00);
145 init_chan (ncont, ichan);
147 r->base[nreg] = (r->base[nreg] & 0xff00) | (data & 0xff);
151 static void write_cont (void *opaque, uint32_t nport, uint32_t data)
153 int iport, ichan, ncont;
159 d = dma_controllers + ncont;
161 iport = ((nport - 0xd0) >> 1) + 8;
168 case 8: /* command */
169 if (data && (data | CMD_NOT_SUPPORTED)) {
170 log ("command %#x not supported\n", data);
179 d->status |= 1 << (ichan + 4);
182 d->status &= ~(1 << (ichan + 4));
184 d->status &= ~(1 << ichan);
187 case 0xa: /* single mask */
189 d->mask |= 1 << (data & 3);
191 d->mask &= ~(1 << (data & 3));
203 op = (data >> 2) & 3;
204 ai = (data >> 4) & 1;
205 dir = (data >> 5) & 1;
206 opmode = (data >> 6) & 3;
208 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
209 ichan, op, ai, dir, opmode);
212 d->regs[ichan].mode = data;
216 case 0xc: /* clear flip flop */
220 case 0xd: /* reset */
227 case 0xe: /* clear mask for all channels */
231 case 0xf: /* write mask for all channels */
236 log ("dma: unknown iport %#x\n", iport);
242 linfo ("nport %#06x, ncont %d, ichan % 2d, val %#06x\n",
243 nport, d != dma_controllers, ichan, data);
252 int DMA_get_channel_mode (int nchan)
254 return dma_controllers[nchan > 3].regs[nchan & 3].mode;
257 void DMA_hold_DREQ (int nchan)
263 linfo ("held cont=%d chan=%d\n", ncont, ichan);
264 dma_controllers[ncont].status |= 1 << (ichan + 4);
267 void DMA_release_DREQ (int nchan)
273 linfo ("released cont=%d chan=%d\n", ncont, ichan);
274 dma_controllers[ncont].status &= ~(1 << (ichan + 4));
277 static void channel_run (int ncont, int ichan)
284 r = dma_controllers[ncont].regs + ichan;
285 /* ai = r->mode & 16; */
286 /* dir = r->mode & 32 ? -1 : 1; */
288 addr = (r->page << 16) | r->now[ADDR];
289 n = r->transfer_handler (r->opaque, addr,
290 (r->base[COUNT] << ncont) + (1 << ncont));
293 ldebug ("dma_pos %d size %d\n",
294 n, (r->base[1] << ncont) + (1 << ncont));
304 for (icont = 0; icont < 2; icont++, d++) {
305 for (ichan = 0; ichan < 4; ichan++) {
310 if ((0 == (d->mask & mask)) && (0 != (d->status & (mask << 4))))
311 channel_run (icont, ichan);
316 void DMA_register_channel (int nchan,
317 DMA_transfer_handler transfer_handler,
326 r = dma_controllers[ncont].regs + ichan;
327 r->transfer_handler = transfer_handler;
331 /* request the emulator to transfer a new DMA memory block ASAP */
332 void DMA_schedule(int nchan)
334 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
340 int page_port_list[] = { 0x1, 0x2, 0x3, 0x7 };
342 for (i = 0; i < 8; i++) {
343 register_ioport_write (i, 1, 1, write_chan, NULL);
345 register_ioport_write (0xc0 + (i << 1), 1, 1, write_chan, NULL);
347 register_ioport_read (i, 1, 1, read_chan, NULL);
348 register_ioport_read (0xc0 + (i << 1), 1, 1, read_chan, NULL);
351 for (i = 0; i < LENOFA (page_port_list); i++) {
352 register_ioport_write (page_port_list[i] + 0x80, 1, 1, write_page, NULL);
353 register_ioport_write (page_port_list[i] + 0x88, 1, 1, write_page, NULL);
356 for (i = 0; i < 8; i++) {
357 register_ioport_write (i + 8, 1, 1, write_cont, NULL);
358 register_ioport_write (0xd0 + (i << 1), 1, 1, write_cont, NULL);
361 write_cont (NULL, 0x0d, 0);
362 write_cont (NULL, 0xda, 0);