-
Notifications
You must be signed in to change notification settings - Fork 1
/
apu.prg
693 lines (618 loc) · 15.6 KB
/
apu.prg
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//#define XHB_BITOP // Habilita das operações | & ^^
#include "xhb.ch"
#include "common.ch"
#include "hbclass.ch"
#translate ( <exp1> | <exp2> ) => ( hb_qbitOr( ( <exp1> ), ( <exp2> ) ) )
#translate ( <exp1> & <exp2> ) => ( hb_qbitAnd( ( <exp1> ), ( <exp2> ) ) )
#translate ( <exp1> ^^ <exp2> ) => ( hb_qbitXor( ( <exp1> ), ( <exp2> ) ) )
// APU
CREATE CLASS APU
VAR console
VAR channel INIT 0
VAR sampleRate INIT 0
VAR pulse1 //Pulse
VAR pulse2 //Pulse
VAR triangle //Triangle
VAR noise //Noise
VAR dmc //DMC
VAR cycle INIT 0 //uint64
VAR framePeriod INIT 0 //byte
VAR frameValue INIT 0 //byte
VAR frameIRQ INIT .f. //INIT .f.
VAR filterChain //FilterChain
METHOD New(console)
METHOD Step()
METHOD sendSample()
METHOD output()
METHOD stepFrameCounter()
METHOD stepTimer()
METHOD stepEnvelope()
METHOD stepSweep()
METHOD stepLength()
METHOD fireIRQ()
METHOD readRegister(address)
METHOD writeRegister(address, value)
METHOD readStatus()
METHOD writeControl(value)
METHOD writeFrameCounter(value)
END CLASS
METHOD New(console) CLASS APU
::console = console
::noise = apuNoise():new()
::noise:shiftRegister = 1
::pulse1=apuPulse():new()
::pulse2=apuPulse():new()
::triangle=apuTriangle():new()
::pulse1:channel = 1
::pulse2:channel = 2
::dmc = dmc():new(@console)
// ::dmc:cpu = console:CPU
return Self
METHOD Step() CLASS Apu
local cycle1,cycle2,f1,f2,s1,s2
cycle1 := ::cycle
::cycle++
cycle2 := ::cycle
::stepTimer()
f1 := int((cycle1) / frameCounterRate)
f2 := int((cycle2) / frameCounterRate)
if f1 != f2
::stepFrameCounter()
end if
s1 := int((cycle1) / ::sampleRate)
s2 := int((cycle2) / ::sampleRate)
if s1 != s2
::sendSample()
end if
METHOD sendSample() CLASS Apu
local output
output := ::filterChain:Step(::output())
//select {
//case ::channel <- output:
//default:
//} // Suspeito
METHOD output() CLASS Apu
local p1,p2,t,n,d
p1 := ::pulse1:output()
p2 := ::pulse2:output()
t := ::triangle:output()
n := ::noise:output()
d := ::dmc:output()
pulseOut := pulseTable[p1+p2+1]
tndOut := tndTable[3*t+2*n+d+1]
return pulseOut + tndOut
// mode 0: mode 1: function
// --------- ----------- -----------------------------
// - - - f - - - - - IRQ (if bit 6 is clear)
// - l - l l - l - - Length counter and sweep
// e e e e e e e e - Envelope and linear counter
METHOD stepFrameCounter() CLASS Apu
do case
case ::framePeriod=4
::frameValue = (::frameValue + 1) % 4
do case
case ::frameValue=0 .or. ::frameValue=2
::stepEnvelope()
case ::frameValue=1
::stepEnvelope()
::stepSweep()
::stepLength()
case ::frameValue=3
::stepEnvelope()
::stepSweep()
::stepLength()
::fireIRQ()
end case
case ::framePeriod=5
::frameValue = (::frameValue + 1) % 5
do case
case ::frameValue=0 .or. ::frameValue=2
::stepEnvelope()
case ::frameValue=1 .or. ::frameValue=3
::stepEnvelope()
::stepSweep()
::stepLength()
end case
end case
METHOD stepTimer() CLASS Apu
if ::cycle % 2 == 0
::pulse1:stepTimer()
::pulse2:stepTimer()
::noise:stepTimer()
::dmc:stepTimer()
end if
::triangle:stepTimer()
METHOD stepEnvelope() CLASS Apu
::pulse1:stepEnvelope()
::pulse2:stepEnvelope()
::triangle:stepCounter()
::noise:stepEnvelope()
METHOD stepSweep() CLASS Apu
::pulse1:stepSweep()
::pulse2:stepSweep()
METHOD stepLength() CLASS Apu
::pulse1:stepLength()
::pulse2:stepLength()
::triangle:stepLength()
::noise:stepLength()
METHOD fireIRQ() CLASS Apu
if ::frameIRQ
::console:CPU:triggerIRQ()
end if
METHOD readRegister(address) CLASS Apu
do case
case address=0x4015
return ::readStatus()
// default:
// log.Fatalf("unhandled apu register read at address: 0x%04X", address)
end case
return 0
METHOD writeRegister(address, value) CLASS Apu
do case
case address=0x4000
::pulse1:writeControl(value)
case address=0x4001
::pulse1:writeSweep(value)
case address=0x4002
::pulse1:writeTimerLow(value)
case address=0x4003
::pulse1:writeTimerHigh(value)
case address=0x4004
::pulse2:writeControl(value)
case address=0x4005
::pulse2:writeSweep(value)
case address=0x4006
::pulse2:writeTimerLow(value)
case address=0x4007
::pulse2:writeTimerHigh(value)
case address=0x4008
::triangle:writeControl(value)
case address=0x4009
case address=0x4010
::dmc:writeControl(value)
case address=0x4011
::dmc:writeValue(value)
case address=0x4012
::dmc:writeAddress(value)
case address=0x4013
::dmc:writeLength(value)
case address=0x400A
::triangle:writeTimerLow(value)
case address=0x400B
::triangle:writeTimerHigh(value)
case address=0x400C
::noise:writeControl(value)
case address=0x400D
case address=0x400E
::noise:writePeriod(value)
case address=0x400F
::noise:writeLength(value)
case address=0x4015
::writeControl(value)
case address=0x4017
::writeFrameCounter(value)
// default:
// log.Fatalf("unhandled apu register write at address: 0x%04X", address)
end case
METHOD readStatus() CLASS Apu
local result
if ::pulse1:lengthValue > 0
result = (result | 1)
end if
if ::pulse2:lengthValue > 0
result = (result | 2)
end if
if ::triangle:lengthValue > 0
result = (result | 4)
end if
if ::noise:lengthValue > 0
result = (result | 8)
end if
if ::dmc:currentLength > 0
result = (result | 16)
end if
return result
METHOD writeControl(value) CLASS Apu
::pulse1:enabled = (value & 1) == 1
::pulse2:enabled = (value & 2) == 2
::triangle:enabled = (value & 4) == 4
::noise:enabled = (value & 8) == 8
::dmc:enabled = (value & 16) == 16
if !::pulse1:enabled
::pulse1:lengthValue = 0
end if
if !::pulse2:enabled
::pulse2:lengthValue = 0
end if
if !::triangle:enabled
::triangle:lengthValue = 0
end if
if !::noise:enabled
::noise:lengthValue = 0
end if
if !::dmc:enabled
::dmc:currentLength = 0
else
if ::dmc:currentLength == 0
::dmc:restart()
end if
end if
METHOD writeFrameCounter(value) CLASS Apu
::framePeriod = ((4 + (value>>7)) & 1)
::frameIRQ = ((value>>6) & 1) == 0
// ::frameValue = 0
if ::framePeriod == 5
::stepEnvelope()
::stepSweep()
::stepLength()
end if
// Pulse
CREATE CLASS apuPulse
VAR enabled INIT .f.
VAR channel INIT 0
VAR lengthEnabled INIT .f.
VAR lengthValue INIT 0
VAR timerPeriod INIT 0
VAR timerValue INIT 0
VAR dutyMode INIT 0
VAR dutyValue INIT 0
VAR sweepReload INIT .f.
VAR sweepEnabled INIT .f.
VAR sweepNegate INIT .f.
VAR sweepShift INIT 0
VAR sweepPeriod INIT 0
VAR sweepValue INIT 0
VAR envelopeEnabled INIT .f.
VAR envelopeLoop INIT .f.
VAR envelopeStart INIT .f.
VAR envelopePeriod INIT 0
VAR envelopeValue INIT 0
VAR envelopeVolume INIT 0
VAR constantVolume INIT 0
METHOD New()
METHOD writeControl(value)
METHOD writeSweep(value)
METHOD writeTimerLow(value)
METHOD writeTimerHigh(value)
METHOD stepTimer()
METHOD stepEnvelope()
METHOD stepSweep()
METHOD stepLength()
METHOD sweep()
METHOD output()
END CLASS
METHOD New() CLASS apuPulse
RETURN Self
METHOD writeControl(value) CLASS apuPulse
::dutyMode = ((value >> 6) & 3)
::lengthEnabled = ((value >> 5) & 1) == 0
::envelopeLoop = ((value >> 5) & 1) == 1
::envelopeEnabled = ((value >> 4) & 1) == 0
::envelopePeriod = (value & 15)
::constantVolume = (value & 15)
::envelopeStart = .t.
METHOD writeSweep(value) CLASS apuPulse
::sweepEnabled = ((value>>7) & 1) == 1
::sweepPeriod = ((value>>4) & 7) + 1
::sweepNegate = ((value>>3) & 1) == 1
::sweepShift = (value & 7)
::sweepReload = .t.
METHOD writeTimerLow(value) CLASS apuPulse
::timerPeriod = ((::timerPeriod & 0xFF00) | (value & 0xFFFF))
METHOD writeTimerHigh(value) CLASS apuPulse
::lengthValue = lengthTable[(value >> 3)+1]
::timerPeriod = ((::timerPeriod & 0x00FF) | (((value & 7) & 0xFFFF) << 8))
::envelopeStart = .t.
::dutyValue = 0
METHOD stepTimer() CLASS apuPulse
if ::timerValue == 0
::timerValue = ::timerPeriod
::dutyValue = (::dutyValue + 1) % 8
else
::timerValue--
end if
METHOD stepEnvelope() CLASS apuPulse
if ::envelopeStart
::envelopeVolume = 15
::envelopeValue = ::envelopePeriod
::envelopeStart = .f.
elseif ::envelopeValue > 0
::envelopeValue--
else
if ::envelopeVolume > 0
::envelopeVolume--
elseif ::envelopeLoop
::envelopeVolume = 15
end if
::envelopeValue = ::envelopePeriod
end if
METHOD stepSweep() CLASS apuPulse
if ::sweepReload
if ::sweepEnabled .and. ::sweepValue == 0
::sweep()
end if
::sweepValue = ::sweepPeriod
::sweepReload = .f.
elseif ::sweepValue > 0
::sweepValue--
else
if ::sweepEnabled
::sweep()
end if
::sweepValue = ::sweepPeriod
end if
METHOD stepLength() CLASS apuPulse
if ::lengthEnabled .and. ::lengthValue > 0
::lengthValue--
end if
METHOD sweep() CLASS apuPulse
delta := (::timerPeriod >> ::sweepShift)
if ::sweepNegate
::timerPeriod -= delta
if ::channel == 1
::timerPeriod--
end if
else
::timerPeriod += delta
end if
METHOD output() CLASS apuPulse
if !::enabled
return 0
end if
if ::lengthValue == 0
return 0
end if
if dutyTable[::dutyMode+1][::dutyValue+1] == 0
return 0
end if
if ::timerPeriod < 8 .or. ::timerPeriod > 0x7FF
return 0
end if
// if !::sweepNegate .and. ::timerPeriod+(::timerPeriod>>::sweepShift) > 0x7FF {
// return 0
// }
if ::envelopeEnabled
return ::envelopeVolume
else
return ::constantVolume
end if
// Triangle
CREATE CLASS apuTriangle
VAR enabled INIT .f.
VAR lengthEnabled INIT .f.
VAR lengthValue INIT 0
VAR timerPeriod INIT 0
VAR timerValue INIT 0
VAR dutyValue INIT 0
VAR counterPeriod INIT 0
VAR counterValue INIT 0
VAR counterReload INIT .f.
METHOD New()
METHOD writeControl(value)
METHOD writeTimerLow(value)
METHOD writeTimerHigh(value)
METHOD stepTimer()
METHOD stepLength()
METHOD stepCounter()
METHOD output()
END CLASS
METHOD New() CLASS apuTriangle
RETURN Self
METHOD writeControl(value) CLASS apuTriangle
::lengthEnabled = ((value>>7) & 1) == 0
::counterPeriod = (value & 0x7F)
METHOD writeTimerLow(value) CLASS apuTriangle
::timerPeriod = ((::timerPeriod & 0xFF00) | (value & 0xFFFF))
METHOD writeTimerHigh(value) CLASS apuTriangle
::lengthValue = lengthTable[((value >> 3) & 0xFF)+1]
::timerPeriod = ((::timerPeriod & 0x00FF) | (((value & 0xFFFF) & 7) << 8))
::timerValue = ::timerPeriod
::counterReload = .t.
METHOD stepTimer() CLASS apuTriangle
if ::timerValue == 0
::timerValue = ::timerPeriod
if ::lengthValue > 0 .and. ::counterValue > 0
::dutyValue = (::dutyValue + 1) % 32
end if
else
::timerValue--
end if
METHOD stepLength() CLASS apuTriangle
if ::lengthEnabled .and. ::lengthValue > 0
::lengthValue--
end if
METHOD stepCounter() CLASS apuTriangle
if ::counterReload
::counterValue = ::counterPeriod
elseif ::counterValue > 0
::counterValue--
end if
if ::lengthEnabled
::counterReload = .f.
end if
METHOD output() CLASS apuTriangle
if !::enabled
return 0
end if
if ::lengthValue == 0
return 0
end if
if ::counterValue == 0
return 0
end if
return triangleTable[::dutyValue+1]
// Noise
CREATE CLASS apuNoise
VAR enabled INIT .f.
VAR mode INIT .f.
VAR shiftRegister INIT 0
VAR lengthEnabled INIT .f.
VAR lengthValue INIT 0
VAR timerPeriod INIT 0
VAR timerValue INIT 0
VAR envelopeEnabled INIT .f.
VAR envelopeLoop INIT .f.
VAR envelopeStart INIT .f.
VAR envelopePeriod INIT 0
VAR envelopeValue INIT 0
VAR envelopeVolume INIT 0
VAR constantVolume INIT 0
METHOD New()
METHOD writeControl(value)
METHOD writePeriod(value)
METHOD writeLength(value)
METHOD stepTimer()
METHOD stepEnvelope()
METHOD stepLength()
METHOD output()
END CLASS
METHOD New() CLASS apuNoise
RETURN Self
METHOD writeControl(value) CLASS apuNoise
::lengthEnabled = ((value >> 5) & 1) == 0
::envelopeLoop = ((value >> 5) & 1) == 1
::envelopeEnabled = ((value >> 4) & 1) == 0
::envelopePeriod = (value & 15)
::constantVolume = (value & 15)
::envelopeStart = .t.
METHOD writePeriod(value) CLASS apuNoise
::mode = (value & 0x80) == 0x80
::timerPeriod = noiseTable[(value & 0x0F)+1]
METHOD writeLength(value) CLASS apuNoise
::lengthValue = lengthTable[(value >> 3)+1]
::envelopeStart = .t.
METHOD stepTimer() CLASS apuNoise
if ::timerValue == 0
::timerValue = ::timerPeriod
if ::mode
shift = 6
else
shift = 1
end if
b1 := (::shiftRegister & 1)
b2 := ((::shiftRegister >> shift) & 1)
::shiftRegister = (::shiftRegister >> 1)
::shiftRegister = (::shiftRegister | ((b1 ^^ b2) << 14))
else
::timerValue--
end if
METHOD stepEnvelope() CLASS apuNoise
if ::envelopeStart
::envelopeVolume = 15
::envelopeValue = ::envelopePeriod
::envelopeStart = .f.
elseif ::envelopeValue > 0
::envelopeValue--
else
if ::envelopeVolume > 0
::envelopeVolume--
elseif ::envelopeLoop
::envelopeVolume = 15
end if
::envelopeValue = ::envelopePeriod
end if
METHOD stepLength() CLASS apuNoise
if ::lengthEnabled .and. ::lengthValue > 0
::lengthValue--
end if
METHOD output() CLASS apuNoise
if !::enabled
return 0
end if
if ::lengthValue == 0
return 0
end if
if (::shiftRegister & 1) == 1
return 0
end if
if ::envelopeEnabled
return ::envelopeVolume
else
return ::constantVolume
end if
// DMC
CREATE CLASS DMC
VAR cpu
VAR enabled INIT .f.
VAR value INIT 0
VAR sampleAddress INIT 0
VAR sampleLength INIT 0
VAR currentAddress INIT 0
VAR currentLength INIT 0
VAR shiftRegister INIT 0
VAR bitCount INIT 0
VAR tickPeriod INIT 0
VAR tickValue INIT 0
VAR loop INIT .f.
VAR irq INIT .f.
METHOD New(console)
METHOD writeControl(value)
METHOD writeValue(value)
METHOD writeAddress(value)
METHOD writeLength(value)
METHOD restart()
METHOD stepTimer()
METHOD stepReader()
METHOD stepShifter()
METHOD output()
END CLASS
METHOD New(console) CLASS Dmc
::CPU=console:cpu
Return Self
METHOD writeControl(value) CLASS Dmc
::irq = (value & 0x80) == 0x80
::loop = (value & 0x40) == 0x40
::tickPeriod = dmcTable[(value & 0x0F)+1]
METHOD writeValue(value) CLASS Dmc
::value = (value & 0x7F)
METHOD writeAddress(value) CLASS Dmc
// Sample address = %11AAAAAA.AA000000
::sampleAddress = (0xC000 | ((value & 0xFFFF) << 6))
METHOD writeLength(value) CLASS Dmc
// Sample length = %0000LLLL.LLLL0001
::sampleLength = (((value & 0xFFFF) << 4) | 1)
METHOD restart() CLASS Dmc
::currentAddress = ::sampleAddress
::currentLength = ::sampleLength
METHOD stepTimer() CLASS Dmc
if !::enabled
return
end if
::stepReader()
if ::tickValue == 0
::tickValue = ::tickPeriod
::stepShifter()
else
::tickValue--
end if
METHOD stepReader() CLASS Dmc
if ::currentLength > 0 .and. ::bitCount == 0
::cpu:stall += 4
::shiftRegister = ::cpu:Read(::currentAddress)
::bitCount = 8
::currentAddress++
if ::currentAddress == 0
::currentAddress = 0x8000
end if
::currentLength--
if ::currentLength == 0 .and. ::loop
::restart()
end if
end if
METHOD stepShifter() CLASS Dmc
if ::bitCount == 0
return
end if
if (::shiftRegister & 1) == 1
if ::value <= 125
::value += 2
end if
else
if ::value >= 2
::value -= 2
end if
end if
::shiftRegister = (::shiftRegister >> 1)
::bitCount--
METHOD output() CLASS Dmc
return ::value