-
Notifications
You must be signed in to change notification settings - Fork 0
/
sb16.cpp
235 lines (172 loc) · 4.57 KB
/
sb16.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "sb16.hpp"
#include <algorithm>
#include <cstdint>
#include "pc_bus.hpp"
#include "pc_cpu.hpp"
#include "pc_dma.hpp"
#include "pc_pic.hpp"
using std::uint8_t;
using std::uint16_t;
using std::int8_t;
using std::int16_t;
using rqdq::pc::InB;
using rqdq::pc::OutB;
namespace rqdq {
namespace {
const int kSampleSizeInWords = 1;
} // namespace
namespace snd {
int spuriousIRQCnt = 0;
Ports make_ports(int baseAddr) {
Ports out;
out.reset = baseAddr + 0x06;
out.read = baseAddr + 0x0a;
out.write = baseAddr + 0x0c;
out.poll = baseAddr + 0x0e;
out.ack16 = baseAddr + 0x0f;
return out; }
class Blaster* theBlaster = NULL;
uint8_t lo(uint16_t value) { return value & 0x00ff; }
uint8_t hi(uint16_t value) { return value >> 8; }
Blaster::Blaster(int baseAddr, int irqNum, int dmaChannelNum, int sampleRateInHz, int numChannels, int bufferSizeInSamples)
:port_(make_ports(baseAddr)),
irqLine_(irqNum),
dma_(dmaChannelNum),
bits_(dmaChannelNum < 4 ? 8 : 16),
sampleRateInHz_(sampleRateInHz),
numChannels_(numChannels),
bufferSizeInSamples_(bufferSizeInSamples),
userBuffer_(1),
playBuffer_(0),
dmaBuffer_(bufferSizeInSamples_*numChannels_ * (bits_ == 16 ? 2 : 1)),
good_(false),
userProc_(NULL),
userPtr_(NULL)
{
theBlaster = this;
RESET();
good_ = SpinUntilReset();
if (!good_) {
return; }
/*
TX(0xe1); // get hw version info
uint16_t hwInfo;
hwInfo = RX() << 8;
hwInfo |= RX();
*/
{
pc::CriticalSection cs;
irqLine_.Disconnect();
irqLine_.SaveISR();
irqLine_.SetISR(Blaster::isrJmp);
irqLine_.Connect(); }
dmaBuffer_.Zero();
dma_.Setup(dmaBuffer_);
// set output sample rate
SetSampleRate();
SpeakerOn();
StartDMA(); }
void Blaster::SpeakerOn() {
if (bits_ == 8) {
TX(0xd1); }}
void Blaster::SpeakerOff() {
if (bits_ == 8) {
TX(0xd3); }}
void Blaster::SetSampleRate() {
if (bits_ == 8) {
TX(0x40);
int tmp = 256 - (1000000 / (numChannels_ * sampleRateInHz_));
TX(tmp); }
else {
TX(0x41);
TX(hi(sampleRateInHz_));
TX(lo(sampleRateInHz_)); }}
void Blaster::StartDMA() {
if (bits_ == 8) {
/*
TX(0xc6); // 8-bit, DAC, auto-init, fifo-enable
TX(0x10); // mode: mono, signed
TX(lo(bufferSizeInSamples_*numChannels_-1));
TX(hi(bufferSizeInSamples_*numChannels_-1));
*/
TX(0x48);
TX(lo(bufferSizeInSamples_*numChannels_-1));
TX(hi(bufferSizeInSamples_*numChannels_-1));
TX(0x1c); } // start auto-init playback
else {
TX(0xb6); // 16-bit DAC, A/I, FIFO
if (numChannels_ == 2) {
TX(0x30); } // DMA mode: 16-bit signed stereo
else {
TX(0x10); } // DMA mode: 16-bit signed mono
TX(lo(bufferSizeInSamples_*kSampleSizeInWords*numChannels_-1));
TX(hi(bufferSizeInSamples_*kSampleSizeInWords*numChannels_-1)); }}
void Blaster::StopDMA() {
if (bits_ == 8) {
TX(0xd0); // pause 8-bit DMA
TX(0xd3); } // turn off speaker
else {
TX(0xd5); }} // pause output
Blaster::~Blaster() {
StopDMA();
{
pc::CriticalSection cs;
dma_.Stop();
irqLine_.RestoreISR(); }
RESET();
SpinUntilReset(); }
inline void Blaster::SpinUntilReadyForWrite() {
while (InB(port_.write) & 0x80); }
inline void Blaster::SpinUntilReadyForRead() {
while (!(InB(port_.poll) & 0x80)); }
void Blaster::TX(uint8_t value) {
SpinUntilReadyForWrite();
OutB(port_.write, value); }
uint8_t Blaster::RX() {
SpinUntilReadyForRead();
return InB(port_.read); }
void Blaster::RESET() {
OutB(port_.reset, 1);
OutB(port_.reset, 0); }
bool Blaster::SpinUntilReset() {
int attempts = 100;
while ((RX() != 0xaa) && attempts--);
return attempts != 0; }
inline void* Blaster::GetUserBuffer() const {
if (bits_ == 8) {
int8_t* dst = (int8_t*)dmaBuffer_.Ptr16();
dst += userBuffer_*bufferSizeInSamples_*numChannels_;
return dst; }
else {
int16_t* dst = (int16_t*)dmaBuffer_.Ptr16();
dst += userBuffer_*bufferSizeInSamples_*numChannels_;
return dst; }}
void __interrupt Blaster::isrJmp() {
theBlaster->isr(); }
inline void Blaster::isr() {
/*if (!irqLine_.IsReal()) {
spuriousIRQCnt++;
return; }*/
ACK();
irqLine_.SignalEOI();
pc::EnableInterrupts();
std::swap(userBuffer_, playBuffer_);
void* dst = GetUserBuffer();
int fmt = (bits_ == 8 ? 1 : 2);
if (userProc_ != NULL) {
userProc_(dst, fmt, numChannels_, bufferSizeInSamples_, userPtr_); }}
inline void Blaster::ACK() {
if (bits_ == 8) {
InB(port_.poll); }
else {
InB(port_.ack16); }}
void Blaster::AttachProc(audioproc userProc, void* userPtr) {
pc::CriticalSection cs;
userPtr_ = userPtr;
userProc_ = userProc; }
void Blaster::DetachProc() {
userProc_ = NULL; }
bool Blaster::IsGood() const {
return good_; }
} // namespace snd
} // namespace rqdq