1 /***************************************************************************************
3 * CD data controller (LC89510 compatible)
5 * Copyright (C) 2012 Eke-Eke (Genesis Plus GX)
7 * Redistribution and use of this code or any derivative works are permitted
8 * provided that the following conditions are met:
10 * - Redistributions may not be sold, nor may they be used in a commercial
11 * product or activity.
13 * - Redistributions that are modified from the original source must include the
14 * complete source code, including the source code for all components used by a
15 * binary built from the modified sources. However, as a special exception, the
16 * source code distributed need not include anything that is normally distributed
17 * (in either source or binary form) with the major components (compiler, kernel,
18 * and so on) of the operating system on which the executable runs, unless that
19 * component itself accompanies the executable.
21 * - Redistributions must reproduce the above copyright notice, this list of
22 * conditions and the following disclaimer in the documentation and/or other
23 * materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
37 ****************************************************************************************/
39 #include "../pico_int.h"
40 #include "genplus_macros.h"
42 /* IFSTAT register bitmasks */
45 #define BIT_DTBSY 0x08
48 /* IFCTRL register bitmasks */
49 #define BIT_DTEIEN 0x40
50 #define BIT_DECIEN 0x20
51 #define BIT_DOUTEN 0x02
53 /* CTRL0 register bitmasks */
54 #define BIT_DECEN 0x80
55 #define BIT_E01RQ 0x20
56 #define BIT_AUTORQ 0x10
59 /* CTRL1 register bitmasks */
60 #define BIT_MODRQ 0x08
61 #define BIT_FORMRQ 0x04
62 #define BIT_SHDREN 0x01
64 /* CTRL2 register bitmask */
65 #define BIT_VALST 0x80
67 /* PicoDrive: doing DMA at once, not using callbacks */
68 //#define DMA_BYTES_PER_LINE 512
73 word_ram_2M_dma_w = 3,
91 //void (*dma_w)(unsigned int words);
93 uint8 ram[0x4000 + 2352]; /* 16K external RAM (with one block overhead to handle buffer overrun) */
100 memset(&cdc, 0, sizeof(cdc_t));
105 /* reset CDC register index */
106 Pico_mcd->s68k_regs[0x04+1] = 0x00;
108 /* reset CDC registers */
117 cdc.head[0][0] = 0x00;
118 cdc.head[0][1] = 0x00;
119 cdc.head[0][2] = 0x00;
120 cdc.head[0][3] = 0x01;
121 cdc.head[1][0] = 0x00;
122 cdc.head[1][1] = 0x00;
123 cdc.head[1][2] = 0x00;
124 cdc.head[1][3] = 0x00;
126 /* reset CDC cycle counter */
129 /* DMA transfer disabled */
133 int cdc_context_save(uint8 *state)
138 if (cdc.dma_w == pcm_ram_dma_w)
142 else if (cdc.dma_w == prg_ram_dma_w)
146 else if (cdc.dma_w == word_ram_0_dma_w)
150 else if (cdc.dma_w == word_ram_1_dma_w)
154 else if (cdc.dma_w == word_ram_2M_dma_w)
163 save_param(&cdc, sizeof(cdc));
164 save_param(&tmp8, 1);
169 int cdc_context_load(uint8 *state)
174 load_param(&cdc, sizeof(cdc));
175 load_param(&tmp8, 1);
180 cdc.dma_w = pcm_ram_dma_w;
183 cdc.dma_w = prg_ram_dma_w;
186 cdc.dma_w = word_ram_0_dma_w;
189 cdc.dma_w = word_ram_1_dma_w;
192 cdc.dma_w = word_ram_2M_dma_w;
202 int cdc_context_load_old(uint8 *state)
204 #define old_load(v, ofs) \
205 memcpy(&cdc.v, state + ofs, sizeof(cdc.v))
207 memcpy(cdc.ram, state, 0x4000);
208 old_load(ifstat, 67892);
209 old_load(ifctrl, 67924);
210 old_load(dbc, 67896);
211 old_load(dac, 67900);
214 old_load(ctrl, 67928);
215 old_load(head[0], 67904);
216 old_load(stat, 67916);
219 switch (Pico_mcd->s68k_regs[0x04+0] & 0x07)
221 case 4: /* PCM RAM DMA */
222 cdc.dma_w = pcm_ram_dma_w;
224 case 5: /* PRG-RAM DMA */
225 cdc.dma_w = prg_ram_dma_w;
227 case 7: /* WORD-RAM DMA */
228 if (Pico_mcd->s68k_regs[0x02+1] & 0x04)
230 if (Pico_mcd->s68k_regs[0x02+1] & 0x01)
231 cdc.dma_w = word_ram_0_dma_w;
233 cdc.dma_w = word_ram_1_dma_w;
237 if (Pico_mcd->s68k_regs[0x02+1] & 0x02)
238 cdc.dma_w = word_ram_2M_dma_w;
243 return 0x10960; // sizeof(old_cdc)
247 static void do_dma(enum dma_type type, int words_in)
249 int dma_addr = (Pico_mcd->s68k_regs[0x0a] << 8) | Pico_mcd->s68k_regs[0x0b];
250 int src_addr = cdc.dac & 0x3ffe;
251 int dst_addr = dma_addr;
252 int words = words_in;
257 elprintf(EL_CD, "dma %d %04x->%04x %x",
258 type, cdc.dac, dst_addr, words_in);
263 dst_addr = (dst_addr << 2) & 0xffc;
264 if (dst_addr + words * 2 > 0x1000) {
265 elprintf(EL_ANOMALY, "pcm dma oflow: %x %x", dst_addr, words);
266 words = (0x1000 - dst_addr) / 2;
268 dst = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank];
269 dst = dst + dst_addr;
272 if (src_addr + words * 2 > 0x4000) {
273 len = 0x4000 - src_addr;
274 memcpy(dst, cdc.ram + src_addr, len);
280 memcpy(dst, cdc.ram + src_addr, words * 2);
287 dst = Pico_mcd->prg_ram + dst_addr;
291 case word_ram_0_dma_w:
292 dst_addr = (dst_addr << 3) & 0x1fffe;
293 dst = Pico_mcd->word_ram1M[0] + dst_addr;
297 case word_ram_1_dma_w:
298 dst_addr = (dst_addr << 3) & 0x1fffe;
299 dst = Pico_mcd->word_ram1M[1] + dst_addr;
303 case word_ram_2M_dma_w:
304 dst_addr = (dst_addr << 3) & 0x3fffe;
305 dst = Pico_mcd->word_ram2M + dst_addr;
310 elprintf(EL_ANOMALY, "invalid dma: %d", type);
314 if (dst_addr + words * 2 > dst_limit) {
315 elprintf(EL_ANOMALY, "cd dma %d oflow: %x %x", type, dst_addr, words);
316 words = (dst_limit - dst_addr) / 2;
320 if (src_addr + words * 2 > 0x4000) {
321 len = 0x4000 - src_addr;
322 memcpy16bswap((void *)dst, cdc.ram + src_addr, len / 2);
328 memcpy16bswap((void *)dst, cdc.ram + src_addr, words);
333 /* update DMA addresses */
334 cdc.dac += words_in * 2;
335 if (type == pcm_ram_dma_w)
336 dma_addr += words_in >> 1;
338 dma_addr += words_in >> 2;
340 Pico_mcd->s68k_regs[0x0a] = dma_addr >> 8;
341 Pico_mcd->s68k_regs[0x0b] = dma_addr;
344 void cdc_dma_update(void)
346 /* end of DMA transfer ? */
347 //if (cdc.dbc < DMA_BYTES_PER_LINE)
349 /* transfer remaining words using 16-bit DMA */
350 //cdc.dma_w((cdc.dbc + 1) >> 1);
351 do_dma(cdc.dma_w, (cdc.dbc + 1) >> 1);
353 /* reset data byte counter (DBCH bits 4-7 should be set to 1) */
356 /* clear !DTEN and !DTBSY */
357 cdc.ifstat |= (BIT_DTBSY | BIT_DTEN);
359 /* pending Data Transfer End interrupt */
360 cdc.ifstat &= ~BIT_DTEI;
362 /* Data Transfer End interrupt enabled ? */
363 if (cdc.ifctrl & BIT_DTEIEN)
365 /* level 5 interrupt enabled ? */
366 if (Pico_mcd->s68k_regs[0x32+1] & PCDS_IEN5)
368 /* update IRQ level */
369 elprintf(EL_INTS, "cdc DTE irq 5");
374 /* clear DSR bit & set EDT bit (SCD register $04) */
375 Pico_mcd->s68k_regs[0x04+0] = (Pico_mcd->s68k_regs[0x04+0] & 0x07) | 0x80;
377 /* disable DMA transfer */
383 /* transfer all words using 16-bit DMA */
384 cdc.dma_w(DMA_BYTES_PER_LINE >> 1);
386 /* decrement data byte counter */
392 int cdc_decoder_update(uint8 header[4])
394 /* data decoding enabled ? */
395 if (cdc.ctrl[0] & BIT_DECEN)
397 /* update HEAD registers */
398 memcpy(cdc.head[0], header, sizeof(cdc.head[0]));
403 /* pending decoder interrupt */
404 cdc.ifstat &= ~BIT_DECI;
406 /* decoder interrupt enabled ? */
407 if (cdc.ifctrl & BIT_DECIEN)
409 /* level 5 interrupt enabled ? */
410 if (Pico_mcd->s68k_regs[0x32+1] & PCDS_IEN5)
412 /* update IRQ level */
413 elprintf(EL_INTS, "cdc DEC irq 5");
418 /* buffer RAM write enabled ? */
419 if (cdc.ctrl[0] & BIT_WRRQ)
423 /* increment block pointer */
426 /* increment write address */
429 /* CDC buffer address */
430 offset = cdc.pt & 0x3fff;
432 /* write CDD block header (4 bytes) */
433 memcpy(cdc.ram + offset, header, 4);
435 /* write CDD block data (2048 bytes) */
436 cdd_read_data(cdc.ram + 4 + offset);
438 /* take care of buffer overrun */
439 if (offset > (0x4000 - 2048 - 4))
441 /* data should be written at the start of buffer */
442 memcpy(cdc.ram, cdc.ram + 0x4000, offset + 2048 + 4 - 0x4000);
445 /* read next data block */
450 /* keep decoding same data block if Buffer Write is disabled */
454 void cdc_reg_w(unsigned char data)
457 elprintf(EL_STATUS, "CDC register %X write 0x%04x", Pico_mcd->s68k_regs[0x04+1] & 0x0F, data);
459 switch (Pico_mcd->s68k_regs[0x04+1] & 0x0F)
461 case 0x01: /* IFCTRL */
463 /* pending interrupts ? */
464 if (((data & BIT_DTEIEN) && !(cdc.ifstat & BIT_DTEI)) ||
465 ((data & BIT_DECIEN) && !(cdc.ifstat & BIT_DECI)))
467 /* level 5 interrupt enabled ? */
468 if (Pico_mcd->s68k_regs[0x32+1] & PCDS_IEN5)
470 /* update IRQ level */
471 elprintf(EL_INTS, "cdc pending irq 5");
475 else // if (scd.pending & (1 << 5))
477 /* clear pending level 5 interrupts */
478 SekInterruptClearS68k(5);
481 /* abort any data transfer if data output is disabled */
482 if (!(data & BIT_DOUTEN))
484 /* clear !DTBSY and !DTEN */
485 cdc.ifstat |= (BIT_DTBSY | BIT_DTEN);
489 Pico_mcd->s68k_regs[0x04+1] = 0x02;
493 case 0x02: /* DBCL */
496 Pico_mcd->s68k_regs[0x04+1] = 0x03;
499 case 0x03: /* DBCH */
501 cdc.dbc |= data << 8;
502 Pico_mcd->s68k_regs[0x04+1] = 0x04;
505 case 0x04: /* DACL */
508 Pico_mcd->s68k_regs[0x04+1] = 0x05;
511 case 0x05: /* DACH */
513 cdc.dac |= data << 8;
514 Pico_mcd->s68k_regs[0x04+1] = 0x06;
517 case 0x06: /* DTRG */
519 /* start data transfer if data output is enabled */
520 if (cdc.ifctrl & BIT_DOUTEN)
523 cdc.ifstat &= ~BIT_DTBSY;
525 /* clear DBCH bits 4-7 */
528 /* clear EDT & DSR bits (SCD register $04) */
529 Pico_mcd->s68k_regs[0x04+0] &= 0x07;
533 /* setup data transfer destination */
534 switch (Pico_mcd->s68k_regs[0x04+0] & 0x07)
536 case 2: /* MAIN-CPU host read */
537 case 3: /* SUB-CPU host read */
540 cdc.ifstat &= ~BIT_DTEN;
542 /* set DSR bit (register $04) */
543 Pico_mcd->s68k_regs[0x04+0] |= 0x40;
547 case 4: /* PCM RAM DMA */
549 cdc.dma_w = pcm_ram_dma_w;
553 case 5: /* PRG-RAM DMA */
555 cdc.dma_w = prg_ram_dma_w;
559 case 7: /* WORD-RAM DMA */
561 /* check memory mode */
562 if (Pico_mcd->s68k_regs[0x02+1] & 0x04)
565 if (Pico_mcd->s68k_regs[0x02+1] & 0x01)
567 /* Word-RAM bank 0 is assigned to SUB-CPU */
568 cdc.dma_w = word_ram_0_dma_w;
572 /* Word-RAM bank 1 is assigned to SUB-CPU */
573 cdc.dma_w = word_ram_1_dma_w;
579 if (Pico_mcd->s68k_regs[0x02+1] & 0x02)
581 /* only process DMA if Word-RAM is assigned to SUB-CPU */
582 cdc.dma_w = word_ram_2M_dma_w;
588 default: /* invalid */
590 elprintf(EL_ANOMALY, "invalid CDC tranfer destination (%d)",
591 Pico_mcd->s68k_regs[0x04+0] & 0x07);
597 pcd_event_schedule_s68k(PCD_EVENT_DMA, cdc.dbc / 2);
600 Pico_mcd->s68k_regs[0x04+1] = 0x07;
604 case 0x07: /* DTACK */
606 /* clear pending data transfer end interrupt */
607 cdc.ifstat |= BIT_DTEI;
609 /* clear DBCH bits 4-7 */
613 /* no pending decoder interrupt ? */
614 if ((cdc.ifstat | BIT_DECI) || !(cdc.ifctrl & BIT_DECIEN))
616 /* clear pending level 5 interrupt */
617 SekInterruptClearS68k(5);
620 Pico_mcd->s68k_regs[0x04+1] = 0x08;
627 Pico_mcd->s68k_regs[0x04+1] = 0x09;
633 Pico_mcd->s68k_regs[0x04+1] = 0x0a;
636 case 0x0a: /* CTRL0 */
638 /* set CRCOK bit only if decoding is enabled */
639 cdc.stat[0] = data & BIT_DECEN;
641 /* update decoding mode */
642 if (data & BIT_AUTORQ)
644 /* set MODE bit according to CTRL1 register & clear FORM bit */
645 cdc.stat[2] = cdc.ctrl[1] & BIT_MODRQ;
649 /* set MODE & FORM bits according to CTRL1 register */
650 cdc.stat[2] = cdc.ctrl[1] & (BIT_MODRQ | BIT_FORMRQ);
654 Pico_mcd->s68k_regs[0x04+1] = 0x0b;
658 case 0x0b: /* CTRL1 */
660 /* update decoding mode */
661 if (cdc.ctrl[0] & BIT_AUTORQ)
663 /* set MODE bit according to CTRL1 register & clear FORM bit */
664 cdc.stat[2] = data & BIT_MODRQ;
668 /* set MODE & FORM bits according to CTRL1 register */
669 cdc.stat[2] = data & (BIT_MODRQ | BIT_FORMRQ);
673 Pico_mcd->s68k_regs[0x04+1] = 0x0c;
680 Pico_mcd->s68k_regs[0x04+1] = 0x0d;
686 Pico_mcd->s68k_regs[0x04+1] = 0x0e;
689 case 0x0e: /* CTRL2 (unused) */
690 Pico_mcd->s68k_regs[0x04+1] = 0x0f;
693 case 0x0f: /* RESET */
697 default: /* by default, SBOUT is not used */
702 unsigned char cdc_reg_r(void)
704 switch (Pico_mcd->s68k_regs[0x04+1] & 0x0F)
706 case 0x01: /* IFSTAT */
707 Pico_mcd->s68k_regs[0x04+1] = 0x02;
710 case 0x02: /* DBCL */
711 Pico_mcd->s68k_regs[0x04+1] = 0x03;
712 return cdc.dbc & 0xff;
714 case 0x03: /* DBCH */
715 Pico_mcd->s68k_regs[0x04+1] = 0x04;
716 return (cdc.dbc >> 8) & 0xff;
718 case 0x04: /* HEAD0 */
719 Pico_mcd->s68k_regs[0x04+1] = 0x05;
720 return cdc.head[cdc.ctrl[1] & BIT_SHDREN][0];
722 case 0x05: /* HEAD1 */
723 Pico_mcd->s68k_regs[0x04+1] = 0x06;
724 return cdc.head[cdc.ctrl[1] & BIT_SHDREN][1];
726 case 0x06: /* HEAD2 */
727 Pico_mcd->s68k_regs[0x04+1] = 0x07;
728 return cdc.head[cdc.ctrl[1] & BIT_SHDREN][2];
730 case 0x07: /* HEAD3 */
731 Pico_mcd->s68k_regs[0x04+1] = 0x08;
732 return cdc.head[cdc.ctrl[1] & BIT_SHDREN][3];
735 Pico_mcd->s68k_regs[0x04+1] = 0x09;
736 return cdc.pt & 0xff;
739 Pico_mcd->s68k_regs[0x04+1] = 0x0a;
740 return (cdc.pt >> 8) & 0xff;
743 Pico_mcd->s68k_regs[0x04+1] = 0x0b;
744 return cdc.wa & 0xff;
747 Pico_mcd->s68k_regs[0x04+1] = 0x0c;
748 return (cdc.wa >> 8) & 0xff;
750 case 0x0c: /* STAT0 */
751 Pico_mcd->s68k_regs[0x04+1] = 0x0d;
754 case 0x0d: /* STAT1 (always return 0) */
755 Pico_mcd->s68k_regs[0x04+1] = 0x0e;
758 case 0x0e: /* STAT2 */
759 Pico_mcd->s68k_regs[0x04+1] = 0x0f;
762 case 0x0f: /* STAT3 */
764 uint8 data = cdc.stat[3];
766 /* clear !VALST (note: this is not 100% correct but BIOS do not seem to care) */
767 cdc.stat[3] = BIT_VALST;
769 /* clear pending decoder interrupt */
770 cdc.ifstat |= BIT_DECI;
773 /* no pending data transfer end interrupt */
774 if ((cdc.ifstat | BIT_DTEI) || !(cdc.ifctrl & BIT_DTEIEN))
776 /* clear pending level 5 interrupt */
777 SekInterruptClearS68k(5);
781 Pico_mcd->s68k_regs[0x04+1] = 0x00;
785 default: /* by default, COMIN is always empty */
790 unsigned short cdc_host_r(void)
792 /* check if data is available */
793 if (!(cdc.ifstat & BIT_DTEN))
795 /* read data word from CDC RAM buffer */
796 uint8 *datap = cdc.ram + (cdc.dac & 0x3ffe);
797 uint16 data = (datap[0] << 8) | datap[1];
800 error("CDC host read 0x%04x -> 0x%04x (dbc=0x%x) (%X)\n", cdc.dac, data, cdc.dbc, s68k.pc);
803 /* increment data address counter */
806 /* decrement data byte counter */
809 /* end of transfer ? */
810 if ((int16)cdc.dbc <= 0)
812 /* reset data byte counter (DBCH bits 4-7 should be set to 1) */
815 /* clear !DTEN and !DTBSY */
816 cdc.ifstat |= (BIT_DTBSY | BIT_DTEN);
818 /* pending Data Transfer End interrupt */
819 cdc.ifstat &= ~BIT_DTEI;
821 /* Data Transfer End interrupt enabled ? */
822 if (cdc.ifctrl & BIT_DTEIEN)
824 /* level 5 interrupt enabled ? */
825 if (Pico_mcd->s68k_regs[0x32+1] & PCDS_IEN5)
827 /* update IRQ level */
828 elprintf(EL_INTS, "cdc DTE irq 5");
833 /* clear DSR bit & set EDT bit (SCD register $04) */
834 Pico_mcd->s68k_regs[0x04+0] = (Pico_mcd->s68k_regs[0x04+0] & 0x07) | 0x80;
841 error("error reading CDC host (data transfer disabled)\n");
846 // vim:shiftwidth=2:ts=2:expandtab