-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapu.go
247 lines (212 loc) · 4.74 KB
/
apu.go
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
236
237
238
239
240
241
242
243
244
245
246
247
package vcsgo
// technically part of TIA, but w/e
type apu struct {
// not marshalled in snapshot
buffer apuCircleBuf
FreqClk int
SampleSum int
SampleSumCount int
ClocksPerSample int
// everything else marshalled
Channel0 sound
Channel1 sound
}
func (apu *apu) init(emu *emuState) {
if emu.TIA.TVFormat == FormatPAL {
f := palClocksPerSecond / samplesPerSecond
apu.ClocksPerSample = int(f)
} else {
f := ntscClocksPerSecond / samplesPerSecond
apu.ClocksPerSample = int(f)
}
apu.Channel0.init()
apu.Channel1.init()
}
type sound struct {
Volume byte
FreqDiv byte
Control byte
FreqDivCounter divCounter
SubFreqDivCounter divCounter
Div31Counter divCounter
PolyCounter4 int
PolyCounter5 int
PolyCounter9 int
Out int
}
func (s *sound) init() {
s.PolyCounter4 = 0xffff
s.PolyCounter5 = 0xffff
s.PolyCounter9 = 0xffff
}
type divCounter struct {
Counter byte
}
func (d *divCounter) tick(val byte) bool {
d.Counter++
if d.Counter < val {
return false
}
d.Counter = 0
return true
}
func (s *sound) runFreqCycle() {
if !s.FreqDivCounter.tick(s.FreqDiv) {
return
}
switch s.Control {
case 0x0, 0xb:
s.Out = 1
case 0x1:
s.Out = s.run4BitPoly()
case 0x2:
if s.SubFreqDivCounter.tick(15) {
s.Out = s.run4BitPoly()
}
case 0x3:
if s.run5BitPoly() == 1 {
s.Out = s.run4BitPoly()
}
case 0x4, 0x5:
s.Out ^= 1
case 0x6, 0xa: // div 31
s.Out = s.runDiv31()
case 0x7, 0x9:
s.Out = s.run5BitPoly()
case 0x8:
s.Out = s.run9BitPoly()
case 0xc, 0xd:
if s.SubFreqDivCounter.tick(3) {
s.Out ^= 1
}
case 0xe:
if s.SubFreqDivCounter.tick(3) {
s.Out = s.runDiv31()
}
case 0xf:
if s.SubFreqDivCounter.tick(3) {
s.Out = s.run5BitPoly()
}
}
}
func (s *sound) runDiv31() int {
if s.Out == 1 {
if s.Div31Counter.tick(18) {
return 0
}
} else {
if s.Div31Counter.tick(13) {
return 1
}
}
return s.Out
}
func (s *sound) run4BitPoly() int {
out := s.PolyCounter4 & 1
s.PolyCounter4 >>= 1
s.PolyCounter4 &^= 0x08
in := s.PolyCounter4 & 1
s.PolyCounter4 |= (out ^ in) << 3
return out
}
func (s *sound) run5BitPoly() int {
out := s.PolyCounter5 & 1
s.PolyCounter5 >>= 1
s.PolyCounter5 &^= 0x10
in := (s.PolyCounter5 >> 1) & 1
s.PolyCounter5 |= (out ^ in) << 4
return out
}
func (s *sound) run9BitPoly() int {
out := s.PolyCounter9 & 1
s.PolyCounter9 >>= 1
s.PolyCounter9 &^= 0x100
in := (s.PolyCounter9 >> 3) & 1
s.PolyCounter9 |= (out ^ in) << 8
return out
}
const (
amountGenerateAhead = 16 * 512 * 2 // must be power of 2
samplesPerSecond = 44100
timePerSample = 1.0 / samplesPerSecond
)
const apuCircleBufSize = amountGenerateAhead
const ntscClocksPerSecond = 3 * 1.193182 * 1000 * 1000
const palClocksPerSecond = 3 * 1.182298 * 1000 * 1000
// NOTE: size must be power of 2
type apuCircleBuf struct {
writeIndex uint
readIndex uint
buf [apuCircleBufSize]byte
}
func (c *apuCircleBuf) write(bytes []byte) (writeCount int) {
for _, b := range bytes {
if c.full() {
return writeCount
}
c.buf[c.mask(c.writeIndex)] = b
c.writeIndex++
writeCount++
}
return writeCount
}
func (c *apuCircleBuf) read(preSizedBuf []byte) []byte {
readCount := 0
for i := range preSizedBuf {
if c.size() == 0 {
break
}
preSizedBuf[i] = c.buf[c.mask(c.readIndex)]
c.readIndex++
readCount++
}
return preSizedBuf[:readCount]
}
func (c *apuCircleBuf) mask(i uint) uint { return i & (uint(len(c.buf)) - 1) }
func (c *apuCircleBuf) size() uint { return c.writeIndex - c.readIndex }
func (c *apuCircleBuf) full() bool { return c.size() == uint(len(c.buf)) }
func (apu *apu) genSample() {
apu.FreqClk++
if apu.FreqClk == 114 {
apu.runFreqCycle()
apu.FreqClk = 0
}
c0 := apu.Channel0.Out * int(apu.Channel0.Volume)
c1 := apu.Channel1.Out * int(apu.Channel1.Volume)
apu.SampleSum += c0 + c1
apu.SampleSumCount++
if apu.SampleSumCount >= apu.ClocksPerSample {
if !apu.buffer.full() {
sum := float32(apu.SampleSum) / 30.0 // 2 channels, 15 vol levels
output := sum / float32(apu.SampleSumCount)
sample := int16(output * 32767.0)
sampleLo := byte(sample & 0xff)
sampleHi := byte(sample >> 8)
apu.buffer.write([]byte{
sampleLo, sampleHi,
sampleLo, sampleHi,
})
}
apu.SampleSum = 0
apu.SampleSumCount = 0
}
}
func (apu *apu) runThreeCycles() {
for i := 0; i < 3; i++ {
apu.genSample()
}
}
func (apu *apu) runFreqCycle() {
apu.Channel0.runFreqCycle()
apu.Channel1.runFreqCycle()
}
func (apu *apu) readSoundBuffer(toFill []byte) []byte {
if int(apu.buffer.size()) < len(toFill) {
//fmt.Println("audSize:", apu.buffer.size(), "len(toFill)", len(toFill))
}
for int(apu.buffer.size()) < len(toFill) {
// stretch sound to fill buffer to avoid click
apu.genSample()
}
return apu.buffer.read(toFill)
}