annotate cdd_mcu.c @ 2229:f699f9d500b4

Fix build
author Michael Pavone <pavone@retrodev.com>
date Mon, 05 Sep 2022 23:48:17 -0700
parents a8af8d898a7c
children 5083bc84fd84
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2215
a8af8d898a7c Fix windows build for real
Michael Pavone <pavone@retrodev.com>
parents: 2152
diff changeset
1 #include <stdlib.h>
a8af8d898a7c Fix windows build for real
Michael Pavone <pavone@retrodev.com>
parents: 2152
diff changeset
2 #include <string.h>
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include "cdd_mcu.h"
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include "backend.h"
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #define SCD_MCLKS 50000000
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #define CD_BLOCK_CLKS 16934400
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 #define CDD_MCU_DIVIDER 8
2141
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
9 #define SECTORS_PER_SECOND 75
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
10 #define SECTOR_CLOCKS (CD_BLOCK_CLKS/SECTORS_PER_SECOND)
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 #define NIBBLE_CLOCKS (CDD_MCU_DIVIDER * 77)
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
12 #define BYTE_CLOCKS (SECTOR_CLOCKS/2352) // 96
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
13 #define SUBCODE_CLOCKS (SECTOR_CLOCKS/98)
2115
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
14 #define PROCESSING_DELAY 54000 //approximate, based on Wondermega M1 measurements
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 //lead in start max diameter 46 mm
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 //program area start max diameter 50 mm
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 //difference 4 mm = 4000 um
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 //radius difference 2 mm = 2000 um
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 //track pitch 1.6 um
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 //1250 physical tracks in between
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 //linear speed 1.2 m/s - 1.4 m/s
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 // 1.3 m = 1300 mm
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 // circumference at 46 mm ~ 144.51 mm
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 // circumference at 50 mm ~ 157.08 mm
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 // avg is 150.795
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 // 75 sectors per second
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 // 17.3333 mm "typical" length of a sector
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 // ~8.7 sectors per track in lead-in area
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 #define LEADIN_SECTORS 10875
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 static uint32_t cd_block_to_mclks(uint32_t cycles)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 return ((uint64_t)cycles) * ((uint64_t)SCD_MCLKS) / ((uint64_t)CD_BLOCK_CLKS);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 static uint32_t mclks_to_cd_block(uint32_t cycles)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 return ((uint64_t)cycles) * ((uint64_t)CD_BLOCK_CLKS) / ((uint64_t)SCD_MCLKS);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 void cdd_mcu_init(cdd_mcu *context, system_media *media)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 context->next_int_cycle = CYCLE_NEVER;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
45 context->next_subcode_int_cycle = CYCLE_NEVER;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
46 context->last_sector_cycle = CYCLE_NEVER;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 context->last_nibble_cycle = CYCLE_NEVER;
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2084
diff changeset
48 context->next_byte_cycle = 0;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
49 context->next_subcode_cycle = CYCLE_NEVER;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 context->requested_format = SF_NOTREADY;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 context->media = media;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 context->current_status_nibble = -1;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 context->current_cmd_nibble = -1;
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2075
diff changeset
54 context->current_sector_byte = -1;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
55 context->current_subcode_byte = -1;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
56 context->current_subcode_dest = 0;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 enum {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 GAO_CDD_CTRL,
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 GAO_CDD_STATUS,
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
62 GAO_CDD_CMD = GAO_CDD_STATUS+5,
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
63 GAO_SUBCODE_ADDR = (0x68-0x36)/2,
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
64 GAO_SUBCODE_START = (0x100-0x36)/2
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 };
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
66 //GAO_CDD_CTRL
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
67 #define BIT_MUTE 0x100
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
68 #define BIT_HOCK 0x0004
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
69 #define BIT_DRS 0x0002
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
70 #define BIT_DTS 0x0001
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 static uint8_t checksum(uint8_t *vbuffer)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 uint8_t *buffer = vbuffer;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 uint8_t sum = 0;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 for (int i = 0; i < 9; i++)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 sum += buffer[i];
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 return (~sum) & 0xF;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 }
2141
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
82
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
83 #define MIN_CIRCUMFERENCE 144.51f
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
84 #define MAX_CIRCUMFERENCE 364.42f
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
85 #define SECTOR_LENGTH 17.3333f
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
86 // max diameter for program area 116 mm
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
87 // circumference ~ 364.42 mm
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
88 // ~ 21 sectors per physical track at edge
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
89 // ~8 sectors per physical track at start of lead-in
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
90 #define COARSE_SEEK_TRACKS 57 //estimate based on seek test
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 static void handle_seek(cdd_mcu *context)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 {
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
93 uint32_t old_coarse = context->coarse_seek;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 if (context->seeking) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 if (context->seek_pba == context->head_pba) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 context->seeking = 0;
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
97 context->coarse_seek = 0;
2095
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
98 if (context->status == DS_PAUSE) {
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
99 context->pause_pba = context->head_pba;
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
100 }
2141
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
101 } else {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
102 //TODO: better estimate of sectors per track at current head location
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
103 //TODO: drive will periodically lose tracking when seeking which slows
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
104 //things down from this ideal speed I'm curently estimating
2141
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
105 float circumference = (MAX_CIRCUMFERENCE-MIN_CIRCUMFERENCE) * ((float)context->head_pba) / (74 * 60 * SECTORS_PER_SECOND) + MIN_CIRCUMFERENCE;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
106 float sectors_per_track = circumference / SECTOR_LENGTH;
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
107 uint32_t max_seek = sectors_per_track * COARSE_SEEK_TRACKS;
2141
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
108 uint32_t min_seek = sectors_per_track;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
109
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
110 uint32_t old_pba = context->head_pba;
2141
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
111 if (context->seek_pba > context->head_pba) {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
112 uint32_t seek_amount;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
113 for (seek_amount = max_seek; seek_amount >= min_seek; seek_amount >>= 1)
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
114 {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
115 if (context->seek_pba - context->head_pba >= seek_amount) {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
116 break;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
117 }
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
118 }
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
119 if (seek_amount >= min_seek) {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
120 context->head_pba += seek_amount;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
121 } else {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
122 context->head_pba++;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
123 }
2067
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
124 } else {
2141
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
125 uint32_t seek_amount;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
126 for (seek_amount = max_seek; seek_amount >= min_seek; seek_amount >>= 1)
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
127 {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
128 if (context->head_pba - context->seek_pba >= seek_amount) {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
129 break;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
130 }
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
131 }
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
132 if (seek_amount >= min_seek) {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
133 context->head_pba -= seek_amount;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
134 } else if (context->head_pba >= min_seek){
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
135 context->head_pba -= min_seek;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
136 } else {
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
137 context->head_pba = 0;
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
138 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
139 }
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
140 if (context->head_pba != old_pba + 1) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
141 context->coarse_seek++;
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
142 } else {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
143 context->coarse_seek = 0;
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
144 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
145 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
146 }
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
147 if (old_coarse && !context->coarse_seek) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
148 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
149 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
150
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
151 static void lba_to_status(cdd_mcu *context, uint32_t lba)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
152 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
153 uint32_t seconds = lba / 75;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
154 uint32_t frames = lba % 75;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
155 uint32_t minutes = seconds / 60;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
156 seconds = seconds % 60;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
157 context->status_buffer.b.time.min_high = minutes / 10;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
158 context->status_buffer.b.time.min_low = minutes % 10;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
159 context->status_buffer.b.time.sec_high = seconds / 10;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
160 context->status_buffer.b.time.sec_low = seconds % 10;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
161 context->status_buffer.b.time.frame_high = frames / 10;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
162 context->status_buffer.b.time.frame_low = frames % 10;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
163 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
164
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
165 static void update_status(cdd_mcu *context, uint16_t *gate_array)
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
166 {
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
167 gate_array[GAO_CDD_CTRL] |= BIT_MUTE;
2115
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
168 uint32_t prev_pba = context->head_pba;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
169 switch (context->status)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
170 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
171 case DS_PLAY:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
172 handle_seek(context);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
173 if (!context->seeking) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
174 context->head_pba++;
2081
cfd53c94fffb Initial stab at RF5C164 emulation
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
175 }
cfd53c94fffb Initial stab at RF5C164 emulation
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
176 if (context->head_pba >= LEADIN_SECTORS) {
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
177 uint8_t track = context->media->seek(context->media, context->head_pba - LEADIN_SECTORS);
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
178 if (!context->seeking && context->media->tracks[track].type == TRACK_AUDIO) {
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
179 gate_array[GAO_CDD_CTRL] &= ~BIT_MUTE;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
180 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
181 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
182 break;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
183 case DS_PAUSE:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
184 handle_seek(context);
2095
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
185 if (!context->seeking) {
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
186 context->head_pba++;
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
187 //TODO: better estimate of sectors per track at current head location
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
188 float circumference = (MAX_CIRCUMFERENCE-MIN_CIRCUMFERENCE) * ((float)context->head_pba) / (74 * 60 * SECTORS_PER_SECOND) + MIN_CIRCUMFERENCE;
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
189 float sectors_per_track = circumference / SECTOR_LENGTH;
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
190 //TODO: check the exact behavior during pause on hardware
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
191 uint32_t diff = sectors_per_track * 0.5f + 0.5f;
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
192 if (context->head_pba > context->pause_pba + diff) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
193 context->head_pba = context->pause_pba - diff;
2095
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
194 if (context->head_pba < LEADIN_SECTORS) {
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
195 context->head_pba = LEADIN_SECTORS;
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
196 }
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
197 }
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
198 }
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
199 if (context->head_pba >= LEADIN_SECTORS) {
2117
03304d350339 Set force-mute when CD drive is in the pause state
Michael Pavone <pavone@retrodev.com>
parents: 2116
diff changeset
200 context->media->seek(context->media, context->head_pba - LEADIN_SECTORS);
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
201 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
202 break;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
203 case DS_TOC_READ:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
204 handle_seek(context);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
205 if (!context->seeking) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
206 context->head_pba++;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
207 if (context->media && context->media->type == MEDIA_CDROM && context->media->num_tracks) {
2079
5a2b759f6b2d BIOS doesn't like it if TOC read happens too quickly
Michael Pavone <pavone@retrodev.com>
parents: 2078
diff changeset
208 if (context->head_pba > 3*(context->media->num_tracks + 2)) {
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
209 context->toc_valid = 1;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
210 context->seeking = 1;
2095
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
211 context->seek_pba = LEADIN_SECTORS + context->media->tracks[0].start_lba + context->media->tracks[0].fake_pregap;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
212 context->status = DS_PAUSE;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
213 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
214
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
215 } else {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
216 context->status = DS_NO_DISC;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
217 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
218 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
219 break;
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
220 case DS_TRACKING:
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
221 handle_seek(context);
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
222 if (!context->seeking) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
223 context->status = DS_PAUSE;
2095
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
224 context->pause_pba = context->head_pba;
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
225 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
226 if (context->head_pba >= LEADIN_SECTORS) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
227 uint8_t track = context->media->seek(context->media, context->head_pba - LEADIN_SECTORS);
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
228 if (!context->seeking && context->media->tracks[track].type == TRACK_AUDIO) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
229 gate_array[GAO_CDD_CTRL] &= ~BIT_MUTE;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
230 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
231 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
232 break;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
233 }
2124
ad346054ca78 Return "not ready" status sometimes when seeking. Fixes issues in multiple games
Michael Pavone <pavone@retrodev.com>
parents: 2122
diff changeset
234 uint8_t force_not_ready = 0;
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
235 if (context->coarse_seek && !(context->coarse_seek % 15)) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
236 //TODO: adjust seeking for focus error when these bad statuses happen
2124
ad346054ca78 Return "not ready" status sometimes when seeking. Fixes issues in multiple games
Michael Pavone <pavone@retrodev.com>
parents: 2122
diff changeset
237 //BIOS depends on getting a not ready status during seeking to clear certain state
ad346054ca78 Return "not ready" status sometimes when seeking. Fixes issues in multiple games
Michael Pavone <pavone@retrodev.com>
parents: 2122
diff changeset
238 force_not_ready = context->status_buffer.format != SF_NOTREADY;
ad346054ca78 Return "not ready" status sometimes when seeking. Fixes issues in multiple games
Michael Pavone <pavone@retrodev.com>
parents: 2122
diff changeset
239 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
240 if (context->first_cmd_received) {
2124
ad346054ca78 Return "not ready" status sometimes when seeking. Fixes issues in multiple games
Michael Pavone <pavone@retrodev.com>
parents: 2122
diff changeset
241 switch (force_not_ready ? SF_NOTREADY : context->requested_format)
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
242 {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
243 case SF_ABSOLUTE:
2115
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
244 if (context->toc_valid && prev_pba >= LEADIN_SECTORS) {
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
245 lba_to_status(context, prev_pba - LEADIN_SECTORS);
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
246 context->status_buffer.format = SF_ABSOLUTE;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
247 } else {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
248 context->status_buffer.format = SF_NOTREADY;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
249 }
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
250 break;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
251 case SF_RELATIVE:
2115
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
252 if (context->toc_valid && prev_pba >= LEADIN_SECTORS) {
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
253 uint32_t lba =prev_pba - LEADIN_SECTORS;
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
254 for (uint32_t i = 0; i < context->media->num_tracks; i++)
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
255 {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
256 if (lba < context->media->tracks[i].end_lba) {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
257 if (context->media->tracks[i].fake_pregap) {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
258 if (lba > context->media->tracks[i].fake_pregap) {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
259 lba -= context->media->tracks[i].fake_pregap;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
260 } else {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
261 //relative time counts down to 0 in pregap
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
262 lba = context->media->tracks[i].fake_pregap - lba;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
263 break;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
264 }
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
265 }
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
266 if (lba < context->media->tracks[i].start_lba) {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
267 //relative time counts down to 0 in pregap
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
268 lba = context->media->tracks[i].start_lba - lba;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
269 } else {
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
270 lba -= context->media->tracks[i].start_lba;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
271 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
272 break;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
273 } else if (context->media->tracks[i].fake_pregap) {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
274 lba -= context->media->tracks[i].fake_pregap;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
275 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
276 }
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
277 lba_to_status(context, lba);
2065
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
278 context->status_buffer.format = SF_RELATIVE;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
279 } else {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
280 context->status_buffer.format = SF_NOTREADY;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
281 }
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
282 break;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
283 case SF_TRACK:
2115
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
284 if (context->toc_valid && prev_pba >= LEADIN_SECTORS) {
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
285 uint32_t lba =prev_pba - LEADIN_SECTORS;
2065
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
286 uint32_t i;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
287 for (i = 0; i < context->media->num_tracks; i++)
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
288 {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
289 if (lba < context->media->tracks[i].end_lba) {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
290 if (context->media->tracks[i].fake_pregap) {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
291 if (lba > context->media->tracks[i].fake_pregap) {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
292 lba -= context->media->tracks[i].fake_pregap;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
293 } else {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
294 //relative time counts down to 0 in pregap
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
295 lba = context->media->tracks[i].fake_pregap - lba;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
296 break;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
297 }
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
298 }
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
299 if (lba < context->media->tracks[i].start_lba) {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
300 //relative time counts down to 0 in pregap
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
301 lba = context->media->tracks[i].start_lba - lba;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
302 } else {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
303 lba -= context->media->tracks[i].start_lba;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
304 }
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
305 break;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
306 } else if (context->media->tracks[i].fake_pregap) {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
307 lba -= context->media->tracks[i].fake_pregap;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
308 }
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
309 }
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
310 context->status_buffer.b.track.track_high = (i + 1) / 10;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
311 context->status_buffer.b.track.track_low = (i + 1) % 10;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
312 if (context->media->tracks[i].type == TRACK_DATA) {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
313 context->status_buffer.b.track.control = 4;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
314 } else {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
315 //TODO: pre-emphasis flag
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
316 //TODO: copy permitted flag
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
317 context->status_buffer.b.track.control = 0;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
318 }
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
319 context->status_buffer.b.track.adr = 1;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
320 context->status_buffer.format = SF_TRACK;
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
321 } else {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
322 context->status_buffer.format = SF_NOTREADY;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
323 }
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
324 break;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
325 case SF_TOCO:
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
326 if (context->toc_valid) {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
327 uint32_t total_fake_pregap = 0;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
328 for (uint32_t i = 0; i < context->media->num_tracks; i++)
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
329 {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
330 total_fake_pregap += context->media->tracks[i].fake_pregap;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
331 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
332 lba_to_status(context, context->media->tracks[context->media->num_tracks - 1].end_lba + total_fake_pregap);
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
333 context->status_buffer.format = SF_TOCO;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
334 } else {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
335 context->status_buffer.format = SF_NOTREADY;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
336 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
337 break;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
338 case SF_TOCT:
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
339 if (context->toc_valid) {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
340 context->status_buffer.b.toct.first_track_high = 0;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
341 context->status_buffer.b.toct.first_track_low = 1;
2073
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
342 context->status_buffer.b.toct.last_track_high = (context->media->num_tracks) / 10;
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
343 context->status_buffer.b.toct.last_track_low = (context->media->num_tracks) % 10;
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
344 context->status_buffer.b.toct.version = 0;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
345 context->status_buffer.format = SF_TOCT;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
346 } else {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
347 context->status_buffer.format = SF_NOTREADY;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
348 }
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
349 break;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
350 case SF_TOCN:
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
351 if (context->toc_valid) {
2073
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
352 if (context->requested_track > context->media->num_tracks) {
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
353 printf("track number %d is bad\n", context->requested_track);
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
354 exit(0);
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
355 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
356 uint32_t lba = context->media->tracks[context->requested_track - 1].start_lba;
2065
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
357 for (uint32_t i = 0; i < context->requested_track; i++) {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
358 lba += context->media->tracks[i].fake_pregap;
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
359 }
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
360 lba_to_status(context, lba);
2065
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
361 if (context->media->tracks[context->requested_track - 1].type == TRACK_DATA) {
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
362 context->status_buffer.b.tocn.frame_low |= 0x80;
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
363 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
364 context->status_buffer.b.tocn.track_low = context->requested_track % 10;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
365 context->status_buffer.format = SF_TOCN;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
366 } else {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
367 context->status_buffer.format = SF_NOTREADY;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
368 }
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
369 break;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
370 case SF_NOTREADY:
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
371 memset(&context->status_buffer, 0, sizeof(context->status_buffer) - 1);
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
372 context->status_buffer.format = SF_NOTREADY;
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
373 break;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
374 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
375 if (context->error_status == DS_STOP) {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
376 if (context->requested_format >= SF_TOCO && context->requested_format <= SF_TOCN) {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
377 context->status_buffer.status = DS_TOC_READ;
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
378 } else if (context->seeking && context->status != DS_TRACKING) {
2065
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
379 context->status_buffer.status = DS_SEEK;
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
380 } else {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
381 context->status_buffer.status = context->status;
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
382 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
383 } else {
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
384 context->status_buffer.status = context->error_status;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
385 context->status_buffer.format = SF_NOTREADY;
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
386 context->error_status = DS_STOP;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
387 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
388 if (context->requested_format != SF_TOCN) {
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
389 context->status_buffer.b.time.flags = !!(gate_array[GAO_CDD_CTRL] & BIT_MUTE); //TODO: populate these
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
390 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
391 } else {
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
392 // Did not receive our first command so just send zeroes
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
393 memset(&context->status_buffer, 0, sizeof(context->status_buffer) - 1);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
394 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
395 context->status_buffer.checksum = checksum((uint8_t *)&context->status_buffer);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
396 if (context->status_buffer.format != SF_NOTREADY) {
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
397 printf("CDD Status %X%X.%X%X%X%X%X%X.%X%X\n",
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
398 context->status_buffer.status, context->status_buffer.format,
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
399 context->status_buffer.b.time.min_high, context->status_buffer.b.time.min_low,
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
400 context->status_buffer.b.time.sec_high, context->status_buffer.b.time.sec_low,
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
401 context->status_buffer.b.time.frame_high, context->status_buffer.b.time.frame_low,
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
402 context->status_buffer.b.time.flags, context->status_buffer.checksum
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
403 );
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
404 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
405 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
406
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
407 static void run_command(cdd_mcu *context)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
408 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
409 uint8_t check = checksum((uint8_t*)&context->cmd_buffer);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
410 if (check != context->cmd_buffer.checksum) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
411 context->error_status = DS_SUM_ERROR;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
412 return;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
413 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
414 if (context->cmd_buffer.must_be_zero) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
415 context->error_status = DS_CMD_ERROR;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
416 return;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
417 }
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
418 context->first_cmd_received = 1;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
419 switch (context->cmd_buffer.cmd_type)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
420 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
421 case CMD_NOP:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
422 break;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
423 case CMD_STOP:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
424 puts("CDD CMD: STOP");
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
425 context->status = DS_STOP;
2064
91e4d2fe5cd9 Get CDD working well enough to get into BIOS CD player
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
426 context->requested_format = SF_ABSOLUTE;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
427 break;
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
428 case CMD_READ:
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
429 case CMD_SEEK: {
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
430 if (context->status == DS_DOOR_OPEN || context->status == DS_TRAY_MOVING || context->status == DS_DISC_LEADOUT || context->status == DS_DISC_LEADIN) {
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
431 context->error_status = DS_CMD_ERROR;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
432 break;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
433 }
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
434 if (context->requested_format == SF_TOCT || context->requested_format == SF_TOCN || context->requested_format == SF_TOCO) {
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
435 context->requested_format = SF_ABSOLUTE;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
436 }
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
437 if (!context->toc_valid) {
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
438 context->error_status = DS_CMD_ERROR;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
439 break;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
440 }
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
441 uint32_t lba = context->cmd_buffer.b.time.min_high * 10 + context->cmd_buffer.b.time.min_low;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
442 lba *= 60;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
443 lba += context->cmd_buffer.b.time.sec_high * 10 + context->cmd_buffer.b.time.sec_low;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
444 lba *= 75;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
445 lba += context->cmd_buffer.b.time.frame_high * 10 + context->cmd_buffer.b.time.frame_low;
2067
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
446 printf("CDD CMD: %s cmd for lba %d, MM:SS:FF %u%u:%u%u:%u%u\n",
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
447 context->cmd_buffer.cmd_type == CMD_READ ? "READ" : "SEEK", lba,
2065
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
448 context->cmd_buffer.b.time.min_high, context->cmd_buffer.b.time.min_low,
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
449 context->cmd_buffer.b.time.sec_high, context->cmd_buffer.b.time.sec_low,
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
450 context->cmd_buffer.b.time.frame_high, context->cmd_buffer.b.time.frame_low
02a9846668d1 Implement transfer of data from CDC to elsewhere. Other miscellaneous CDD/CDC improvements
Michael Pavone <pavone@retrodev.com>
parents: 2064
diff changeset
451 );
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
452 if (lba >= context->media->tracks[0].fake_pregap + context->media->tracks[context->media->num_tracks - 1].end_lba) {
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
453 context->error_status = DS_CMD_ERROR;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
454 break;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
455 }
2141
793715149f16 More accurate seek emulation
Michael Pavone <pavone@retrodev.com>
parents: 2139
diff changeset
456 context->seek_pba = lba + LEADIN_SECTORS - 3;
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
457 context->seeking = 1;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
458 context->status = context->cmd_buffer.cmd_type == CMD_READ ? DS_PLAY : DS_PAUSE;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
459 break;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
460 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
461 case CMD_REPORT_REQUEST:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
462 switch (context->cmd_buffer.b.format.status_type)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
463 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
464 case SF_ABSOLUTE:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
465 case SF_RELATIVE:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
466 case SF_TRACK:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
467 context->requested_format = context->cmd_buffer.b.format.status_type;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
468 break;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
469 case SF_TOCO:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
470 if (context->toc_valid) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
471 context->requested_format = SF_TOCO;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
472 } else {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
473 context->error_status = DS_CMD_ERROR;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
474 context->requested_format = SF_ABSOLUTE;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
475 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
476 break;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
477 case SF_TOCT:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
478 if (context->toc_valid) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
479 if (context->status == DS_STOP) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
480 context->status = DS_TOC_READ;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
481 context->seeking = 1;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
482 context->seek_pba = 0;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
483 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
484 } else {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
485 context->status = DS_TOC_READ;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
486 context->seeking = 1;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
487 context->seek_pba = 0;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
488 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
489 context->requested_format = SF_TOCT;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
490 break;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
491 case SF_TOCN:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
492 context->requested_track = context->cmd_buffer.b.format.track_high * 10;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
493 context->requested_track += context->cmd_buffer.b.format.track_low;
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
494 if (!context->media || context->requested_track > context->media->num_tracks) {
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
495 context->requested_format = SF_ABSOLUTE;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
496 context->error_status = DS_CMD_ERROR;
2073
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
497 break;
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
498 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
499 context->status = DS_TOC_READ;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
500 context->seeking = 1;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
501 context->seek_pba = 0;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
502 context->requested_format = SF_TOCN;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
503 break;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
504 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
505 printf("CDD CMD: REPORT REQUEST(%d), format set to %d\n", context->cmd_buffer.b.format.status_type, context->requested_format);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
506 break;
2067
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
507 case CMD_PAUSE:
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
508 if (context->status == DS_DOOR_OPEN || context->status == DS_TRAY_MOVING || context->status == DS_DISC_LEADOUT || context->status == DS_DISC_LEADIN) {
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
509 context->error_status = DS_CMD_ERROR;
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
510 break;
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
511 }
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
512 if (context->requested_format == SF_TOCT || context->requested_format == SF_TOCN) {
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
513 context->requested_format = SF_ABSOLUTE;
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
514 }
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
515 if (!context->toc_valid) {
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
516 context->error_status = DS_CMD_ERROR;
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
517 break;
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
518 }
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
519 if (context->status == DS_STOP) {
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
520 context->seeking = 1;
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
521 context->seek_pba = LEADIN_SECTORS + context->media->tracks[0].fake_pregap + context->media->tracks[0].start_lba;
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
522 printf("CDD CMD: PAUSE, seeking to %u\n", context->seek_pba);
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
523 } else {
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
524 puts("CDD CMD: PAUSE");
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
525 }
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
526 context->status = DS_PAUSE;
2095
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
527 if (!context->seeking) {
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
528 context->pause_pba = context->head_pba;
c732dcc9c61b Better simulation of CDD pause behavior
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
529 }
2067
f22e04b69272 More CDC/CDD improvements
Michael Pavone <pavone@retrodev.com>
parents: 2065
diff changeset
530 break;
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
531 case CMD_PLAY:
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
532 if (context->status == DS_DOOR_OPEN || context->status == DS_TRAY_MOVING || context->status == DS_DISC_LEADOUT || context->status == DS_DISC_LEADIN) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
533 context->error_status = DS_CMD_ERROR;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
534 break;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
535 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
536 if (context->requested_format == SF_TOCT || context->requested_format == SF_TOCN) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
537 context->requested_format = SF_ABSOLUTE;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
538 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
539 if (!context->toc_valid) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
540 context->error_status = DS_CMD_ERROR;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
541 break;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
542 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
543 if (context->status == DS_STOP || context->status == DS_TOC_READ) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
544 context->seeking = 1;
2100
539450d905b3 Fix CDD PLAY command and CDD fader sample alignment
Michael Pavone <pavone@retrodev.com>
parents: 2096
diff changeset
545 context->seek_pba = LEADIN_SECTORS + context->media->tracks[0].fake_pregap + context->media->tracks[0].start_lba - 4;
539450d905b3 Fix CDD PLAY command and CDD fader sample alignment
Michael Pavone <pavone@retrodev.com>
parents: 2096
diff changeset
546 printf("CDD CMD: PLAY, seeking to %u\n", context->seek_pba);
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
547 } else {
2100
539450d905b3 Fix CDD PLAY command and CDD fader sample alignment
Michael Pavone <pavone@retrodev.com>
parents: 2096
diff changeset
548 puts("CDD CMD: PLAY");
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
549 }
2100
539450d905b3 Fix CDD PLAY command and CDD fader sample alignment
Michael Pavone <pavone@retrodev.com>
parents: 2096
diff changeset
550 context->status = DS_PLAY;
2084
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
551 break;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
552 //TODO: CMD_FFWD, CMD_RWD
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
553 case CMD_TRACK_SKIP:
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
554 if (context->status != DS_PLAY && context->status != DS_PAUSE && context->status != DS_DISC_LEADOUT) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
555 context->error_status = DS_CMD_ERROR;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
556 break;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
557 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
558 if (context->requested_format == SF_TOCT || context->requested_format == SF_TOCN) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
559 context->requested_format = SF_ABSOLUTE;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
560 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
561 if (!context->toc_valid) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
562 context->error_status = DS_CMD_ERROR;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
563 break;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
564 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
565 {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
566 int32_t to_skip = context->cmd_buffer.b.skip.tracks_highest << 12 | context->cmd_buffer.b.skip.tracks_midhigh << 8
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
567 | context->cmd_buffer.b.skip.tracks_midlow << 4 | context->cmd_buffer.b.skip.tracks_lowest;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
568 if (context->cmd_buffer.b.skip.direction) {
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
569 to_skip = -to_skip;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
570 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
571 printf("CDD CMD: TRACK_SKIP direction %u, num_tracks %i, delta %i\n", context->cmd_buffer.b.skip.direction, abs(to_skip), to_skip);
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
572 //circumference at 83mm point (roughly half way between inner and outer edge of program area)
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
573 //~ 260.75cm ~ 15 sectors
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
574 context->seek_pba = context->head_pba + to_skip * 15;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
575 context->seeking = 1;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
576 }
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
577 context->status = DS_TRACKING;
c9d3b8e1ea56 Implement a couple more CDD commands force CDDA mute when seeking
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
578 break;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
579 default:
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
580 printf("CDD CMD: Unimplemented(%d)\n", context->cmd_buffer.cmd_type);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
581 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
582 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
583
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
584 void cdd_mcu_run(cdd_mcu *context, uint32_t cycle, uint16_t *gate_array, lc8951* cdc, cdd_fader* fader)
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
585 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
586 uint32_t cd_cycle = mclks_to_cd_block(cycle);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
587 if (!(gate_array[GAO_CDD_CTRL] & BIT_HOCK)) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
588 //it's a little unclear if this gates the actual cd block clock or just handshaking
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
589 //assum it's actually the clock for now
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
590 for (; context->cycle < cd_cycle; context->cycle += CDD_MCU_DIVIDER) {
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2084
diff changeset
591 if (context->cycle >= context->next_byte_cycle) {
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
592 cdd_fader_data(fader, 0);
2130
28b6453cf7e3 Emulate CDC sync detection and sync insertion rather than relying on external knowledge about sector offset
Michael Pavone <pavone@retrodev.com>
parents: 2124
diff changeset
593 lc8951_write_byte(cdc, cd_block_to_mclks(context->cycle), 0, 0);
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2084
diff changeset
594 context->next_byte_cycle += BYTE_CLOCKS;
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
595 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
596 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
597 gate_array[GAO_CDD_CTRL] |= BIT_MUTE;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
598 return;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
599 }
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
600 uint32_t next_subcode = context->last_sector_cycle + SECTOR_CLOCKS;
2115
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
601 uint32_t next_nibble;
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
602 if (context->current_status_nibble > 0) {
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
603 next_nibble = context->last_nibble_cycle + NIBBLE_CLOCKS;
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
604 } else if (!context->current_status_nibble) {
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
605 next_nibble = context->last_sector_cycle + PROCESSING_DELAY;
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
606 if (context->coarse_seek % 3) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
607 next_nibble += SECTOR_CLOCKS * (3 - (context->coarse_seek % 3));
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
608 }
2115
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
609 } else {
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
610 next_nibble = CYCLE_NEVER;
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
611 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
612 uint32_t next_cmd_nibble = context->current_cmd_nibble >= 0 ? context->last_nibble_cycle + NIBBLE_CLOCKS : CYCLE_NEVER;
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
613
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
614 for (; context->cycle < cd_cycle; context->cycle += CDD_MCU_DIVIDER)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
615 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
616 if (context->cycle >= next_subcode) {
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
617 uint32_t old_coarse = context->coarse_seek;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
618 context->last_sector_cycle = context->cycle;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
619 next_subcode = context->cycle + SECTOR_CLOCKS;
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
620 update_status(context, gate_array);
2115
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
621 next_nibble = context->cycle + PROCESSING_DELAY;
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
622 if (context->coarse_seek % 3) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
623 next_nibble += SECTOR_CLOCKS * (3 - (context->coarse_seek % 3));
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
624 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
625 context->current_status_nibble = 0;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
626 if (context->next_subcode_int_cycle != CYCLE_NEVER) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
627 context->subcode_int_pending = 1;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
628 }
2142
d9151d0894c7 Fix some CD-ROM emulation issues
Michael Pavone <pavone@retrodev.com>
parents: 2141
diff changeset
629 if ((context->status == DS_PLAY || context->status == DS_PAUSE) && context->head_pba >= LEADIN_SECTORS && !context->seeking) {
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
630 context->current_sector_byte = 0;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
631 context->current_subcode_byte = 0;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
632 context->next_subcode_cycle = context->cycle;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
633 context->next_subcode_int_cycle = cd_block_to_mclks(next_subcode);
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
634 } else {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
635 context->next_subcode_int_cycle = CYCLE_NEVER;
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
636 }
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
637 if (old_coarse != context->coarse_seek) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
638 context->next_int_cycle = cd_block_to_mclks(context->cycle + PROCESSING_DELAY + 7 * NIBBLE_CLOCKS);
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
639 if (context->coarse_seek % 3) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
640 context->next_int_cycle += cd_block_to_mclks(SECTOR_CLOCKS * (3 - (context->coarse_seek % 3)));
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
641 }
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
642 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
643 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
644 if (context->cycle >= next_nibble) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
645 if (context->current_status_nibble == sizeof(cdd_status)) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
646 context->current_status_nibble = -1;
2139
11a3d5b00a66 Fix DRS and DTS bits of CDD status
Michael Pavone <pavone@retrodev.com>
parents: 2130
diff changeset
647 gate_array[GAO_CDD_CTRL] &= ~BIT_DRS;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
648 if (context->cmd_recv_pending) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
649 context->cmd_recv_pending = 0;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
650 context->current_cmd_nibble = 0;
2139
11a3d5b00a66 Fix DRS and DTS bits of CDD status
Michael Pavone <pavone@retrodev.com>
parents: 2130
diff changeset
651 gate_array[GAO_CDD_CTRL] |= BIT_DTS;
2075
983f57d08eff Fix a couple of CDD bugs
Michael Pavone <pavone@retrodev.com>
parents: 2073
diff changeset
652 next_cmd_nibble = context->cycle + NIBBLE_CLOCKS;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
653 } else {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
654 context->cmd_recv_wait = 1;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
655 }
2075
983f57d08eff Fix a couple of CDD bugs
Michael Pavone <pavone@retrodev.com>
parents: 2073
diff changeset
656 next_nibble = CYCLE_NEVER;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
657 } else {
2139
11a3d5b00a66 Fix DRS and DTS bits of CDD status
Michael Pavone <pavone@retrodev.com>
parents: 2130
diff changeset
658 gate_array[GAO_CDD_CTRL] |= BIT_DRS;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
659 uint8_t value = ((uint8_t *)&context->status_buffer)[context->current_status_nibble];
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
660 int ga_index = GAO_CDD_STATUS + (context->current_status_nibble >> 1);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
661 if (context->current_status_nibble & 1) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
662 gate_array[ga_index] = value | (gate_array[ga_index] & 0xFF00);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
663 } else {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
664 gate_array[ga_index] = (value << 8) | (gate_array[ga_index] & 0x00FF);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
665 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
666 if (context->current_status_nibble == 7) {
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
667 if (!(context->coarse_seek % 3)) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
668 context->int_pending = 1;
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
669 if (context->coarse_seek) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
670 context->next_int_cycle = cd_block_to_mclks(context->cycle + 3 * SECTOR_CLOCKS);
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
671 } else {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
672 context->next_int_cycle = cd_block_to_mclks(context->cycle + SECTOR_CLOCKS);
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
673 }
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
674 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
675 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
676 context->current_status_nibble++;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
677 context->last_nibble_cycle = context->cycle;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
678 next_nibble = context->cycle + NIBBLE_CLOCKS;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
679 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
680 } else if (context->cycle >= next_cmd_nibble) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
681 if (context->current_cmd_nibble == sizeof(cdd_cmd)) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
682 next_cmd_nibble = CYCLE_NEVER;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
683 context->current_cmd_nibble = -1;
2139
11a3d5b00a66 Fix DRS and DTS bits of CDD status
Michael Pavone <pavone@retrodev.com>
parents: 2130
diff changeset
684 gate_array[GAO_CDD_CTRL] &= ~BIT_DTS;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
685 run_command(context);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
686 } else {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
687 int ga_index = GAO_CDD_CMD + (context->current_cmd_nibble >> 1);
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
688 uint8_t value = (context->current_cmd_nibble & 1) ? gate_array[ga_index] : gate_array[ga_index] >> 8;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
689 ((uint8_t *)&context->cmd_buffer)[context->current_cmd_nibble] = value;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
690 context->current_cmd_nibble++;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
691 context->last_nibble_cycle = context->cycle;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
692 next_cmd_nibble = context->cycle + NIBBLE_CLOCKS;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
693 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
694 }
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2084
diff changeset
695 if (context->cycle >= context->next_byte_cycle) {
2100
539450d905b3 Fix CDD PLAY command and CDD fader sample alignment
Michael Pavone <pavone@retrodev.com>
parents: 2096
diff changeset
696 if (context->current_sector_byte >= 0 && (!fader->byte_counter || context->current_sector_byte)) {
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
697 uint8_t byte = context->media->read(context->media, context->current_sector_byte);
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
698 lc8951_write_byte(cdc, cd_block_to_mclks(context->cycle), context->current_sector_byte++, byte);
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
699 cdd_fader_data(fader, gate_array[GAO_CDD_CTRL] & BIT_MUTE ? 0 : byte);
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
700 } else {
2130
28b6453cf7e3 Emulate CDC sync detection and sync insertion rather than relying on external knowledge about sector offset
Michael Pavone <pavone@retrodev.com>
parents: 2124
diff changeset
701 lc8951_write_byte(cdc, cd_block_to_mclks(context->cycle), 0, 0);
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
702 cdd_fader_data(fader, 0);
2100
539450d905b3 Fix CDD PLAY command and CDD fader sample alignment
Michael Pavone <pavone@retrodev.com>
parents: 2096
diff changeset
703 if (context->current_sector_byte >= 0) {
539450d905b3 Fix CDD PLAY command and CDD fader sample alignment
Michael Pavone <pavone@retrodev.com>
parents: 2096
diff changeset
704 next_subcode += BYTE_CLOCKS;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
705 context->last_sector_cycle += BYTE_CLOCKS;
2100
539450d905b3 Fix CDD PLAY command and CDD fader sample alignment
Michael Pavone <pavone@retrodev.com>
parents: 2096
diff changeset
706 }
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
707 }
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
708 if (context->current_sector_byte == 2352) {
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
709 context->current_sector_byte = -1;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
710 }
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2084
diff changeset
711 context->next_byte_cycle += BYTE_CLOCKS;
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
712 }
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
713 if (context->cycle >= context->next_subcode_cycle) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
714 uint8_t byte;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
715 if (!context->current_subcode_byte) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
716 byte = 0x9F;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
717 //This probably happens after the second sync symbol, but doing it here simplifies things a little
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
718 context->current_subcode_dest &= 0x7E;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
719 gate_array[GAO_SUBCODE_ADDR] = (context->current_subcode_dest - 96) & 0x7E;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
720 } else if (context->current_subcode_byte == 1) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
721 byte = 0xFD;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
722 } else {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
723 byte = context->media->read_subcodes(context->media, context->current_subcode_byte - 2);
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
724 }
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
725 int offset = GAO_SUBCODE_START + (context->current_subcode_dest >> 1);
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
726 if (context->current_subcode_dest & 1) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
727 gate_array[offset] &= 0xFF00;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
728 gate_array[offset] |= byte;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
729 } else {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
730 gate_array[offset] &= 0x00FF;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
731 gate_array[offset] |= byte << 8;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
732 }
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
733 context->current_subcode_byte++;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
734 if (context->current_subcode_byte == 98) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
735 context->current_subcode_byte = 0;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
736 } else if (context->current_subcode_byte == 32) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
737 gate_array[GAO_SUBCODE_ADDR] |= 0x80;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
738 }
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
739 context->current_subcode_dest++;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
740 context->current_subcode_dest &= 0x7F;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
741 context->next_subcode_cycle += SUBCODE_CLOCKS;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
742 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
743 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
744 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
745
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
746 void cdd_mcu_start_cmd_recv(cdd_mcu *context, uint16_t *gate_array)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
747 {
2139
11a3d5b00a66 Fix DRS and DTS bits of CDD status
Michael Pavone <pavone@retrodev.com>
parents: 2130
diff changeset
748 if (gate_array[GAO_CDD_CTRL] & BIT_DTS) {
11a3d5b00a66 Fix DRS and DTS bits of CDD status
Michael Pavone <pavone@retrodev.com>
parents: 2130
diff changeset
749 return;
11a3d5b00a66 Fix DRS and DTS bits of CDD status
Michael Pavone <pavone@retrodev.com>
parents: 2130
diff changeset
750 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
751 if (context->cmd_recv_wait) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
752 context->current_cmd_nibble = 0;
2139
11a3d5b00a66 Fix DRS and DTS bits of CDD status
Michael Pavone <pavone@retrodev.com>
parents: 2130
diff changeset
753 gate_array[GAO_CDD_CTRL] |= BIT_DTS;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
754 context->last_nibble_cycle = context->cycle;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
755 context->cmd_recv_wait = 0;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
756 } else {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
757 context->cmd_recv_pending = 1;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
758 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
759 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
760
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
761 void cdd_hock_enabled(cdd_mcu *context)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
762 {
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
763 context->last_sector_cycle = context->cycle;
2115
e93ced356a21 Hopefully more accurate relative timing of CDD status and data reaching the CDC
Michael Pavone <pavone@retrodev.com>
parents: 2100
diff changeset
764 context->next_int_cycle = cd_block_to_mclks(context->cycle + SECTOR_CLOCKS + PROCESSING_DELAY + 7 * NIBBLE_CLOCKS);
2152
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
765 if (context->coarse_seek % 3) {
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
766 context->next_int_cycle += cd_block_to_mclks(SECTOR_CLOCKS * (3 - (context->coarse_seek % 3)));
c3ee42c89b27 More accurate seek times and basic emulation of extended time between CDD interrupts during coarse seeking
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
767 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
768 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
769
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
770 void cdd_hock_disabled(cdd_mcu *context)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
771 {
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
772 context->last_sector_cycle = CYCLE_NEVER;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
773 context->next_int_cycle = CYCLE_NEVER;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
774 context->last_nibble_cycle = CYCLE_NEVER;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
775 context->current_status_nibble = -1;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
776 context->current_cmd_nibble = -1;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
777 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
778
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
779 void cdd_mcu_adjust_cycle(cdd_mcu *context, uint32_t deduction)
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
780 {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
781 uint32_t cd_deduction = mclks_to_cd_block(deduction);
2073
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
782 if (context->cycle > cd_deduction) {
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
783 context->cycle -= cd_deduction;
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
784 } else {
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
785 context->cycle = 0;
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
786 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
787 if (context->next_int_cycle != CYCLE_NEVER) {
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
788 context->next_int_cycle -= deduction;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
789 }
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
790 if (context->last_sector_cycle != CYCLE_NEVER) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
791 if (context->last_sector_cycle > cd_deduction) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
792 context->last_sector_cycle -= cd_deduction;
2073
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
793 } else {
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
794 context->last_sector_cycle = 0;
2073
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
795 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
796 }
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
797 if (context->last_nibble_cycle != CYCLE_NEVER) {
2073
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
798 if (context->last_nibble_cycle > cd_deduction) {
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
799 context->last_nibble_cycle -= cd_deduction;
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
800 } else {
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
801 context->last_nibble_cycle = 0;
c69e42444f96 Fix some cycle adjustment stuff and an off-by one on hte TOCT response
Michael Pavone <pavone@retrodev.com>
parents: 2067
diff changeset
802 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
803 }
2096
460c93c19146 Fix CDD byte cycle counter deduction that got broken when I fixed the off-by-one on CD-DA
Michael Pavone <pavone@retrodev.com>
parents: 2095
diff changeset
804 context->next_byte_cycle -= cd_deduction;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
805 if (context->next_subcode_cycle != CYCLE_NEVER) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
806 context->next_subcode_cycle -= cd_deduction;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2115
diff changeset
807 }
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
808 }