2 // DAC (really, Synchronous Serial Interface) Handler
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Rewritten by James L. Hammons
13 #define BUFFER_SIZE 0x8000 // Make the DAC buffers 32K x 16 bits
15 // Jaguar memory locations
20 #define SMODE 0xF1A154
24 uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr;
25 SDL_AudioSpec desired;
27 // We can get away with using native endian here because we can tell SDL to use the native
28 // when looking at the sample buffer, i.e., no need to worry about it.
31 uint8 SCLKFrequencyDivider = 9; // Start out roughly 44.1K (46164 Hz in NTSC mode)
32 uint16 serialMode = 0;
34 // Private function prototypes
36 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
37 int GetCalculatedFrequency(void);
40 // Initialize the SDL sound system (?) (!)
44 memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
46 desired.freq = GetCalculatedFrequency(); // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
47 desired.format = AUDIO_S16SYS; // This uses the native endian (for portability)...
49 desired.samples = 4096; // Let's try a 4K buffer (can always go lower)
50 desired.callback = SDLSoundCallback;
52 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
54 WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n");
60 SDL_PauseAudio(false); // Start playback!
61 WriteLog("DAC: Successfully initialized.\n");
65 // Reset the sound buffer FIFOs
69 LeftFIFOHeadPtr = LeftFIFOTailPtr = 0, RightFIFOHeadPtr = RightFIFOTailPtr = 1;
73 // Close down the SDL sound subsystem (?) (!)
79 WriteLog("DAC: Done.\n");
83 // SDL callback routine to fill audio buffer
85 // Note: The samples are packed in the buffer in 16 bit left/16 bit right pairs.
87 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
89 //WriteLog("DAC: Inside callback...\n");
90 if (LeftFIFOHeadPtr != LeftFIFOTailPtr)
92 //WriteLog("DAC: About to write some data!\n");
93 int numLeftSamplesReady
94 = (LeftFIFOTailPtr + (LeftFIFOTailPtr < LeftFIFOHeadPtr ? BUFFER_SIZE : 0))
96 int numRightSamplesReady
97 = (RightFIFOTailPtr + (RightFIFOTailPtr < RightFIFOHeadPtr ? BUFFER_SIZE : 0))
100 = (numLeftSamplesReady < numRightSamplesReady
101 ? numLeftSamplesReady : numRightSamplesReady) * 2;
103 if (numSamplesReady > length)
104 numSamplesReady = length;
106 // Actually, it's a bit more involved than this, but this is the general idea:
107 // memcpy(buffer, DACBuffer, length);
108 for(int i=0; i<numSamplesReady; i++)
109 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
110 ((uint16 *)buffer)[i] = DACBuffer[(LeftFIFOHeadPtr + i) % BUFFER_SIZE];
111 // buffer[i] = DACBuffer[(LeftFIFOHeadPtr + i) & (BUFFER_SIZE - 1)];
113 LeftFIFOHeadPtr = (LeftFIFOHeadPtr + (numSamplesReady / 2)) % BUFFER_SIZE;
114 RightFIFOHeadPtr = (RightFIFOHeadPtr + (numSamplesReady / 2)) % BUFFER_SIZE;
115 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
116 // LeftFIFOHeadPtr = (LeftFIFOHeadPtr + (numSamplesReady / 2)) & (BUFFER_SIZE - 1);
117 // RightFIFOHeadPtr = (RightFIFOHeadPtr + (numSamplesReady / 2)) & (BUFFER_SIZE - 1);
122 // Calculate the frequency of SCLK * 32 using the divider
124 int GetCalculatedFrequency(void)
126 extern bool hardwareTypeNTSC;
127 int systemClockFrequency = (hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
129 // We divide by 32 here in order to find the frequency of 32 SCLKs in a row (transferring
130 // 16 bits of left data + 16 bits of right data = 32 bits, 1 SCLK = 1 bit transferred).
131 return systemClockFrequency / (32 * (2 * (SCLKFrequencyDivider + 1)));
135 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
137 void DACWriteByte(uint32 offset, uint8 data)
139 // WriteLog("DAC: Writing %02X at %08X\n", data, offset);
142 void DACWriteWord(uint32 offset, uint16 data)
144 if (offset == LTXD + 2)
146 if (LeftFIFOTailPtr + 2 != LeftFIFOHeadPtr)
148 SDL_LockAudio(); // Is it necessary to do this? Mebbe.
149 // We use a circular buffer 'cause it's easy. Note that the callback function
150 // takes care of dumping audio to the soundcard...!
151 LeftFIFOTailPtr = (LeftFIFOTailPtr + 2) % BUFFER_SIZE;
152 DACBuffer[LeftFIFOTailPtr] = data;
153 // Aaron's code does this, but I don't know why...
154 //Flipping this bit makes the audio MUCH louder. Need to look at the amplitude of the
155 //waveform to see if any massaging is needed here...
156 // DACBuffer[LeftFIFOTailPtr] = data ^ 0x8000;
160 WriteLog("DAC: Ran into FIFO's left tail pointer!\n");
162 else if (offset == RTXD + 2)
164 if (RightFIFOTailPtr + 2 != RightFIFOHeadPtr)
167 RightFIFOTailPtr = (RightFIFOTailPtr + 2) % BUFFER_SIZE;
168 DACBuffer[RightFIFOTailPtr] = data;
169 // Aaron's code does this, but I don't know why...
170 // DACBuffer[RightFIFOTailPtr] = data ^ 0x8000;
174 WriteLog("DAC: Ran into FIFO's right tail pointer!\n");
176 else if (offset == SCLK + 2) // Sample rate
178 if ((uint8)data != SCLKFrequencyDivider)
180 WriteLog("DAC: Changing sample rate!\n");
182 SCLKFrequencyDivider = (uint8)data;
183 desired.freq = GetCalculatedFrequency();// SDL will do conversion on the fly, if it can't get the exact rate. Nice!
185 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
187 WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n");
193 SDL_PauseAudio(false); // Start playback!
196 else if (offset == SMODE + 2)
199 WriteLog("DAC: Writing to SMODE. Bits: %s%s%s%s%s%s\n",
200 (data & 0x01 ? "INTERNAL " : ""), (data & 0x02 ? "MODE " : ""),
201 (data & 0x04 ? "WSEN " : ""), (data & 0x08 ? "RISING " : ""),
202 (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : ""));
207 // LRXD/RRXD/SSTAT ($F1A148/4C/50)
209 uint8 DACReadByte(uint32 offset)
211 // WriteLog("DAC: Reading byte from %08X\n", offset);
215 uint16 DACReadWord(uint32 offset)
217 // WriteLog("DAC: Reading word from %08X\n", offset);