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
69 #define DMA_CYCLES_PER_BYTE 4 // or 6?
74 word_ram_2M_dma_w = 3,
92 //void (*dma_w)(unsigned int words);
94 uint8 ram[0x4000 + 2352]; /* 16K external RAM (with one block overhead to handle buffer overrun) */
101 memset(&cdc, 0, sizeof(cdc_t));
106 /* reset CDC register index */
107 Pico_mcd->s68k_regs[0x04+1] = 0x00;
109 /* reset CDC registers */
118 cdc.head[0][0] = 0x00;
119 cdc.head[0][1] = 0x00;
120 cdc.head[0][2] = 0x00;
121 cdc.head[0][3] = 0x01;
122 cdc.head[1][0] = 0x00;
123 cdc.head[1][1] = 0x00;
124 cdc.head[1][2] = 0x00;
125 cdc.head[1][3] = 0x00;
127 /* reset CDC cycle counter */
130 /* DMA transfer disabled */
134 int cdc_context_save(uint8 *state)
139 if (cdc.dma_w == pcm_ram_dma_w)
143 else if (cdc.dma_w == prg_ram_dma_w)
147 else if (cdc.dma_w == word_ram_0_dma_w)
151 else if (cdc.dma_w == word_ram_1_dma_w)
155 else if (cdc.dma_w == word_ram_2M_dma_w)
164 save_param(&cdc, sizeof(cdc));
165 save_param(&tmp8, 1);
170 int cdc_context_load(uint8 *state)
175 load_param(&cdc, sizeof(cdc));
176 load_param(&tmp8, 1);
181 cdc.dma_w = pcm_ram_dma_w;
184 cdc.dma_w = prg_ram_dma_w;
187 cdc.dma_w = word_ram_0_dma_w;
190 cdc.dma_w = word_ram_1_dma_w;
193 cdc.dma_w = word_ram_2M_dma_w;
203 int cdc_context_load_old(uint8 *state)
205 #define old_load(v, ofs) \
206 memcpy(&cdc.v, state + ofs, sizeof(cdc.v))
208 memcpy(cdc.ram, state, 0x4000);
209 old_load(ifstat, 67892);
210 old_load(ifctrl, 67924);
211 old_load(dbc, 67896);
212 old_load(dac, 67900);
215 old_load(ctrl, 67928);
216 old_load(head[0], 67904);
217 old_load(stat, 67916);
220 switch (Pico_mcd->s68k_regs[0x04+0] & 0x07)
222 case 4: /* PCM RAM DMA */
223 cdc.dma_w = pcm_ram_dma_w;
225 case 5: /* PRG-RAM DMA */
226 cdc.dma_w = prg_ram_dma_w;
228 case 7: /* WORD-RAM DMA */
229 if (Pico_mcd->s68k_regs[0x02+1] & 0x04)
231 if (Pico_mcd->s68k_regs[0x02+1] & 0x01)
232 cdc.dma_w = word_ram_0_dma_w;
234 cdc.dma_w = word_ram_1_dma_w;
238 if (Pico_mcd->s68k_regs[0x02+1] & 0x02)
239 cdc.dma_w = word_ram_2M_dma_w;
244 return 0x10960; // sizeof(old_cdc)
248 static void do_dma(enum dma_type type, int bytes_in)
250 int dma_addr = (Pico_mcd->s68k_regs[0x0a] << 8) | Pico_mcd->s68k_regs[0x0b];
251 int src_addr = cdc.dac & 0x3ffe;
252 int dst_addr = dma_addr;
253 int bytes = bytes_in;
254 int words = bytes_in >> 1;
259 elprintf(EL_CD, "dma %d %04x->%04x %x",
260 type, cdc.dac, dst_addr, bytes_in);
265 dst_addr = (dst_addr << 2) & 0xffc;
266 if (dst_addr + bytes > 0x1000) {
267 elprintf(EL_ANOMALY, "pcm dma oflow: %x %x", dst_addr, words);
268 bytes = 0x1000 - dst_addr;
270 dst = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank];
271 dst = dst + dst_addr;
274 if (src_addr + bytes > 0x4000) {
275 len = 0x4000 - src_addr;
276 memcpy(dst, cdc.ram + src_addr, len);
282 memcpy(dst, cdc.ram + src_addr, bytes);
289 dst = Pico_mcd->prg_ram + dst_addr;
293 case word_ram_0_dma_w:
294 dst_addr = (dst_addr << 3) & 0x1fffe;
295 dst = Pico_mcd->word_ram1M[0] + dst_addr;
299 case word_ram_1_dma_w:
300 dst_addr = (dst_addr << 3) & 0x1fffe;
301 dst = Pico_mcd->word_ram1M[1] + dst_addr;
305 case word_ram_2M_dma_w:
306 dst_addr = (dst_addr << 3) & 0x3fffe;
307 dst = Pico_mcd->word_ram2M + dst_addr;
312 elprintf(EL_ANOMALY, "invalid dma: %d", type);
316 if (dst_addr + words * 2 > dst_limit) {
317 elprintf(EL_ANOMALY, "cd dma %d oflow: %x %x", type, dst_addr, words);
318 words = (dst_limit - dst_addr) / 2;
322 if (src_addr + words * 2 > 0x4000) {
323 len = 0x4000 - src_addr;
324 memcpy16bswap((void *)dst, cdc.ram + src_addr, len / 2);
330 memcpy16bswap((void *)dst, cdc.ram + src_addr, words);
334 bytes_in &= ~1; // Todo leftover byte?
337 /* update DMA addresses */
339 if (type == pcm_ram_dma_w)
340 dma_addr += bytes_in >> 2;
342 dma_addr += bytes_in >> 3;
344 Pico_mcd->s68k_regs[0x0a] = dma_addr >> 8;
345 Pico_mcd->s68k_regs[0x0b] = dma_addr;
348 void cdc_dma_update(void)
350 /* end of DMA transfer ? */
351 //if (cdc.dbc < DMA_BYTES_PER_LINE)
353 /* transfer remaining words using 16-bit DMA */
354 //cdc.dma_w((cdc.dbc + 1) >> 1);
355 do_dma(cdc.dma_w, cdc.dbc + 1);
357 /* reset data byte counter (DBCH bits 4-7 should be set to 1) */
360 /* clear !DTEN and !DTBSY */
361 cdc.ifstat |= (BIT_DTBSY | BIT_DTEN);
363 /* clear DSR bit & set EDT bit (SCD register $04) */
364 Pico_mcd->s68k_regs[0x04+0] = (Pico_mcd->s68k_regs[0x04+0] & 0x07) | 0x80;
366 if (cdc.ifstat & BIT_DTEI) {
367 /* pending Data Transfer End interrupt */
368 cdc.ifstat &= ~BIT_DTEI;
370 /* Data Transfer End interrupt enabled ? */
371 if (cdc.ifctrl & BIT_DTEIEN)
373 /* level 5 interrupt enabled ? */
374 if (Pico_mcd->s68k_regs[0x32+1] & PCDS_IEN5)
376 /* update IRQ level */
377 elprintf(EL_INTS, "cdc DTE irq 5");
383 /* disable DMA transfer */
389 /* transfer all words using 16-bit DMA */
390 cdc.dma_w(DMA_BYTES_PER_LINE >> 1);
392 /* decrement data byte counter */
398 int cdc_decoder_update(uint8 header[4])
400 /* data decoding enabled ? */
401 if (cdc.ctrl[0] & BIT_DECEN)
403 /* update HEAD registers */
404 memcpy(cdc.head[0], header, sizeof(cdc.head[0]));
410 cdc.stat[0] = BIT_DECEN;
412 /* pending decoder interrupt */
413 cdc.ifstat &= ~BIT_DECI;
415 /* decoder interrupt enabled ? */
416 if (cdc.ifctrl & BIT_DECIEN)
418 /* level 5 interrupt enabled ? */
419 if (Pico_mcd->s68k_regs[0x32+1] & PCDS_IEN5)
421 /* update IRQ level */
422 elprintf(EL_INTS, "cdc DEC irq 5");
427 /* buffer RAM write enabled ? */
428 if (cdc.ctrl[0] & BIT_WRRQ)
432 /* increment block pointer */
435 /* increment write address */
438 /* CDC buffer address */
439 offset = cdc.pt & 0x3fff;
441 /* write CDD block header (4 bytes) */
442 memcpy(cdc.ram + offset, header, 4);
444 /* write CDD block data (2048 bytes) */
445 cdd_read_data(cdc.ram + 4 + offset);
447 /* take care of buffer overrun */
448 if (offset > (0x4000 - 2048 - 4))
450 /* data should be written at the start of buffer */
451 memcpy(cdc.ram, cdc.ram + 0x4000, offset + 2048 + 4 - 0x4000);
454 /* read next data block */
459 /* keep decoding same data block if Buffer Write is disabled */
463 void cdc_reg_w(unsigned char data)
466 elprintf(EL_STATUS, "CDC register %X write 0x%04x", Pico_mcd->s68k_regs[0x04+1] & 0x0F, data);
468 switch (Pico_mcd->s68k_regs[0x04+1] & 0x1F)
473 case 0x01: /* IFCTRL */
475 /* pending interrupts ? */
476 if (((data & BIT_DTEIEN) && !(cdc.ifstat & BIT_DTEI)) ||
477 ((data & BIT_DECIEN) && !(cdc.ifstat & BIT_DECI)))
479 /* level 5 interrupt enabled ? */
480 if (Pico_mcd->s68k_regs[0x32+1] & PCDS_IEN5)
482 /* update IRQ level */
483 elprintf(EL_INTS, "cdc pending irq 5");
487 else // if (scd.pending & (1 << 5))
489 /* clear pending level 5 interrupts */
493 /* abort any data transfer if data output is disabled */
494 if (!(data & BIT_DOUTEN))
496 /* clear !DTBSY and !DTEN */
497 cdc.ifstat |= (BIT_DTBSY | BIT_DTEN);
501 Pico_mcd->s68k_regs[0x04+1] = 0x02;
505 case 0x02: /* DBCL */
508 Pico_mcd->s68k_regs[0x04+1] = 0x03;
511 case 0x03: /* DBCH */
513 cdc.dbc |= (data & 0x0f) << 8;
514 Pico_mcd->s68k_regs[0x04+1] = 0x04;
517 case 0x04: /* DACL */
520 Pico_mcd->s68k_regs[0x04+1] = 0x05;
523 case 0x05: /* DACH */
525 cdc.dac |= data << 8;
526 Pico_mcd->s68k_regs[0x04+1] = 0x06;
529 case 0x06: /* DTRG */
531 /* start data transfer if data output is enabled */
532 if (cdc.ifctrl & BIT_DOUTEN)
535 cdc.ifstat &= ~BIT_DTBSY;
537 /* clear DBCH bits 4-7 */
540 /* clear EDT & DSR bits (SCD register $04) */
541 Pico_mcd->s68k_regs[0x04+0] &= 0x07;
545 /* setup data transfer destination */
546 switch (Pico_mcd->s68k_regs[0x04+0] & 0x07)
548 case 2: /* MAIN-CPU host read */
549 case 3: /* SUB-CPU host read */
552 cdc.ifstat &= ~BIT_DTEN;
554 /* set DSR bit (register $04) */
555 Pico_mcd->s68k_regs[0x04+0] |= 0x40;
559 case 4: /* PCM RAM DMA */
561 cdc.dma_w = pcm_ram_dma_w;
565 case 5: /* PRG-RAM DMA */
567 cdc.dma_w = prg_ram_dma_w;
571 case 7: /* WORD-RAM DMA */
573 /* check memory mode */
574 if (Pico_mcd->s68k_regs[0x02+1] & 0x04)
577 if (Pico_mcd->s68k_regs[0x02+1] & 0x01)
579 /* Word-RAM bank 0 is assigned to SUB-CPU */
580 cdc.dma_w = word_ram_0_dma_w;
584 /* Word-RAM bank 1 is assigned to SUB-CPU */
585 cdc.dma_w = word_ram_1_dma_w;
591 if (Pico_mcd->s68k_regs[0x02+1] & 0x02)
593 /* only process DMA if Word-RAM is assigned to SUB-CPU */
594 cdc.dma_w = word_ram_2M_dma_w;
600 default: /* invalid */
602 elprintf(EL_ANOMALY, "invalid CDC tranfer destination (%d)",
603 Pico_mcd->s68k_regs[0x04+0] & 0x07);
609 pcd_event_schedule_s68k(PCD_EVENT_DMA, cdc.dbc * DMA_CYCLES_PER_BYTE);
612 Pico_mcd->s68k_regs[0x04+1] = 0x07;
616 case 0x07: /* DTACK */
618 /* clear pending data transfer end interrupt */
619 cdc.ifstat |= BIT_DTEI;
621 /* clear DBCH bits 4-7 */
625 /* no pending decoder interrupt ? */
626 if ((cdc.ifstat | BIT_DECI) || !(cdc.ifctrl & BIT_DECIEN))
628 /* clear pending level 5 interrupt */
632 Pico_mcd->s68k_regs[0x04+1] = 0x08;
639 Pico_mcd->s68k_regs[0x04+1] = 0x09;
645 Pico_mcd->s68k_regs[0x04+1] = 0x0a;
648 case 0x0a: /* CTRL0 */
650 /* reset DECI if decoder turned off */
651 if (!(data & BIT_DECEN))
652 cdc.ifstat |= BIT_DECI;
654 /* update decoding mode */
655 if (data & BIT_AUTORQ)
657 /* set MODE bit according to CTRL1 register & clear FORM bit */
658 cdc.stat[2] = cdc.ctrl[1] & BIT_MODRQ;
662 /* set MODE & FORM bits according to CTRL1 register */
663 cdc.stat[2] = cdc.ctrl[1] & (BIT_MODRQ | BIT_FORMRQ);
667 Pico_mcd->s68k_regs[0x04+1] = 0x0b;
671 case 0x0b: /* CTRL1 */
673 /* update decoding mode */
674 if (cdc.ctrl[0] & BIT_AUTORQ)
676 /* set MODE bit according to CTRL1 register & clear FORM bit */
677 cdc.stat[2] = data & BIT_MODRQ;
681 /* set MODE & FORM bits according to CTRL1 register */
682 cdc.stat[2] = data & (BIT_MODRQ | BIT_FORMRQ);
686 Pico_mcd->s68k_regs[0x04+1] = 0x0c;
693 Pico_mcd->s68k_regs[0x04+1] = 0x0d;
699 Pico_mcd->s68k_regs[0x04+1] = 0x0e;
702 case 0x0e: /* CTRL2 (unused) */
703 Pico_mcd->s68k_regs[0x04+1] = 0x0f;
706 case 0x0f: /* RESET */
710 default: /* by default, SBOUT is not used */
711 Pico_mcd->s68k_regs[0x04+1] = (Pico_mcd->s68k_regs[0x04+1] + 1) & 0x1f;
716 unsigned char cdc_reg_r(void)
718 switch (Pico_mcd->s68k_regs[0x04+1] & 0x1F)
723 case 0x01: /* IFSTAT */
724 Pico_mcd->s68k_regs[0x04+1] = 0x02;
727 case 0x02: /* DBCL */
728 Pico_mcd->s68k_regs[0x04+1] = 0x03;
729 return cdc.dbc & 0xff;
731 case 0x03: /* DBCH */
732 Pico_mcd->s68k_regs[0x04+1] = 0x04;
733 return (cdc.dbc >> 8) & 0xff;
735 case 0x04: /* HEAD0 */
736 Pico_mcd->s68k_regs[0x04+1] = 0x05;
737 return cdc.head[cdc.ctrl[1] & BIT_SHDREN][0];
739 case 0x05: /* HEAD1 */
740 Pico_mcd->s68k_regs[0x04+1] = 0x06;
741 return cdc.head[cdc.ctrl[1] & BIT_SHDREN][1];
743 case 0x06: /* HEAD2 */
744 Pico_mcd->s68k_regs[0x04+1] = 0x07;
745 return cdc.head[cdc.ctrl[1] & BIT_SHDREN][2];
747 case 0x07: /* HEAD3 */
748 Pico_mcd->s68k_regs[0x04+1] = 0x08;
749 return cdc.head[cdc.ctrl[1] & BIT_SHDREN][3];
752 Pico_mcd->s68k_regs[0x04+1] = 0x09;
753 return cdc.pt & 0xff;
756 Pico_mcd->s68k_regs[0x04+1] = 0x0a;
757 return (cdc.pt >> 8) & 0xff;
760 Pico_mcd->s68k_regs[0x04+1] = 0x0b;
761 return cdc.wa & 0xff;
764 Pico_mcd->s68k_regs[0x04+1] = 0x0c;
765 return (cdc.wa >> 8) & 0xff;
767 case 0x0c: /* STAT0 */
768 Pico_mcd->s68k_regs[0x04+1] = 0x0d;
771 case 0x0d: /* STAT1 (always return 0) */
772 Pico_mcd->s68k_regs[0x04+1] = 0x0e;
775 case 0x0e: /* STAT2 */
776 Pico_mcd->s68k_regs[0x04+1] = 0x0f;
779 case 0x0f: /* STAT3 */
781 uint8 data = cdc.stat[3];
783 /* clear !VALST (note: this is not 100% correct but BIOS do not seem to care) */
784 cdc.stat[3] = BIT_VALST;
786 /* clear pending decoder interrupt */
787 cdc.ifstat |= BIT_DECI;
790 /* no pending data transfer end interrupt */
791 if ((cdc.ifstat | BIT_DTEI) || !(cdc.ifctrl & BIT_DTEIEN))
793 /* clear pending level 5 interrupt */
798 Pico_mcd->s68k_regs[0x04+1] = 0x10;
802 default: /* by default, COMIN is always empty */
803 Pico_mcd->s68k_regs[0x04+1] = (Pico_mcd->s68k_regs[0x04+1] + 1) & 0x1f;
808 unsigned short cdc_host_r(void)
810 /* check if data is available */
811 if (!(cdc.ifstat & BIT_DTEN))
813 /* read data word from CDC RAM buffer */
814 uint8 *datap = cdc.ram + (cdc.dac & 0x3ffe);
815 uint16 data = (datap[0] << 8) | datap[1];
818 error("CDC host read 0x%04x -> 0x%04x (dbc=0x%x) (%X)\n", cdc.dac, data, cdc.dbc, s68k.pc);
821 /* increment data address counter */
824 /* decrement data byte counter */
827 /* end of transfer ? */
828 if ((int16)cdc.dbc <= 0)
830 /* reset data byte counter (DBCH bits 4-7 should be set to 1) */
833 /* clear !DTEN and !DTBSY */
834 cdc.ifstat |= (BIT_DTBSY | BIT_DTEN);
836 /* clear DSR bit & set EDT bit (SCD register $04) */
837 Pico_mcd->s68k_regs[0x04+0] = (Pico_mcd->s68k_regs[0x04+0] & 0x07) | 0x80;
839 } else if ((int16)cdc.dbc <= 2)
841 if (cdc.ifstat & BIT_DTEI) {
842 /* pending Data Transfer End interrupt */
843 cdc.ifstat &= ~BIT_DTEI;
845 /* Data Transfer End interrupt enabled ? */
846 if (cdc.ifctrl & BIT_DTEIEN)
848 /* level 5 interrupt enabled ? */
849 if (Pico_mcd->s68k_regs[0x32+1] & PCDS_IEN5)
851 /* update IRQ level */
852 elprintf(EL_INTS, "cdc DTE irq 5");
857 /* set DSR and EDT bit (SCD register $04) */
858 Pico_mcd->s68k_regs[0x04+0] = (Pico_mcd->s68k_regs[0x04+0] & 0x07) | 0xc0;
865 error("error reading CDC host (data transfer disabled)\n");
870 // vim:shiftwidth=2:ts=2:expandtab