-
Notifications
You must be signed in to change notification settings - Fork 15
/
apu.sv
982 lines (902 loc) · 36.6 KB
/
apu.sv
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
// Copyright (c) 2012-2013 Ludvig Strigeus
// This program is GPL Licensed. See COPYING for the full license.
module LenCtr_Lookup(input [4:0] X, output [7:0] Yout);
reg [6:0] Y;
always @*
begin
case(X)
0: Y = 7'h05;
1: Y = 7'h7F;
2: Y = 7'h0A;
3: Y = 7'h01;
4: Y = 7'h14;
5: Y = 7'h02;
6: Y = 7'h28;
7: Y = 7'h03;
8: Y = 7'h50;
9: Y = 7'h04;
10: Y = 7'h1E;
11: Y = 7'h05;
12: Y = 7'h07;
13: Y = 7'h06;
14: Y = 7'h0D;
15: Y = 7'h07;
16: Y = 7'h06;
17: Y = 7'h08;
18: Y = 7'h0C;
19: Y = 7'h09;
20: Y = 7'h18;
21: Y = 7'h0A;
22: Y = 7'h30;
23: Y = 7'h0B;
24: Y = 7'h60;
25: Y = 7'h0C;
26: Y = 7'h24;
27: Y = 7'h0D;
28: Y = 7'h08;
29: Y = 7'h0E;
30: Y = 7'h10;
31: Y = 7'h0F;
endcase
end
assign Yout = {Y, 1'b0};
endmodule
module SquareChan(input MMC5,
input clk, input ce, input reset, input sq2,
input [1:0] Addr,
input [7:0] DIN,
input MW,
input LenCtr_Clock,
input Env_Clock,
input odd_or_even,
input Enabled,
input [7:0] LenCtr_In,
output reg [3:0] Sample,
output IsNonZero);
reg [7:0] LenCtr;
// Register 1
reg [1:0] Duty;
reg EnvLoop, EnvDisable, EnvDoReset;
reg [3:0] Volume, Envelope, EnvDivider;
wire LenCtrHalt = EnvLoop; // Aliased bit
assign IsNonZero = (LenCtr != 0);
// Register 2
reg SweepEnable, SweepNegate, SweepReset;
reg [2:0] SweepPeriod, SweepDivider, SweepShift;
reg [10:0] Period;
reg [11:0] TimerCtr;
reg [2:0] SeqPos;
wire [10:0] ShiftedPeriod = (Period >> SweepShift);
wire [10:0] PeriodRhs = (SweepNegate ? (~ShiftedPeriod + {10'b0, sq2}) : ShiftedPeriod);
wire [11:0] NewSweepPeriod = Period + PeriodRhs;
// XXX: This should be enabled for MMC5, but do we really want ultrasonic frequencies?
// Re-enable if we ever get a proper LPF.
wire ValidFreq = /*(MMC5==1) ||*/ ((|Period[10:3]) && (SweepNegate || !NewSweepPeriod[11]));
// |Period[10:3] is equivalent to Period >= 8
//double speed for MMC5=Env_Clock
wire LenCtrClockEnable = (MMC5==0 && LenCtr_Clock) || (MMC5==1 && Env_Clock);
always @(posedge clk) if (reset) begin
LenCtr <= 0;
Duty <= 0;
EnvDoReset <= 0;
EnvLoop <= 0;
EnvDisable <= 0;
Volume <= 0;
Envelope <= 0;
EnvDivider <= 0;
SweepEnable <= 0;
SweepNegate <= 0;
SweepReset <= 0;
SweepPeriod <= 0;
SweepDivider <= 0;
SweepShift <= 0;
Period <= 0;
TimerCtr <= 0;
SeqPos <= 0;
end else if (ce) begin
// Check if writing to the regs of this channel
// NOTE: This needs to be done before the clocking below.
if (MW) begin
case(Addr)
0: begin
// if (sq2) $write("SQ0: Duty=%d, EnvLoop=%d, EnvDisable=%d, Volume=%d\n", DIN[7:6], DIN[5], DIN[4], DIN[3:0]);
Duty <= DIN[7:6];
EnvLoop <= DIN[5];
EnvDisable <= DIN[4];
Volume <= DIN[3:0];
end
1: begin
// if (sq2) $write("SQ1: SweepEnable=%d, SweepPeriod=%d, SweepNegate=%d, SweepShift=%d, DIN=%X\n", DIN[7], DIN[6:4], DIN[3], DIN[2:0], DIN);
if (MMC5==0) begin
SweepEnable <= DIN[7];
SweepPeriod <= DIN[6:4];
SweepNegate <= DIN[3];
SweepShift <= DIN[2:0];
SweepReset <= 1;
end
end
2: begin
// if (sq2) $write("SQ2: Period=%d. DIN=%X\n", DIN, DIN);
Period[7:0] <= DIN;
end
3: begin
// Upper bits of the period
// if (sq2) $write("SQ3: PeriodUpper=%d LenCtr=%x DIN=%X\n", DIN[2:0], LenCtr_In, DIN);
Period[10:8] <= DIN[2:0];
LenCtr <= LenCtr_In;
EnvDoReset <= 1;
SeqPos <= 0;
end
endcase
end
// Count down the square timer...
// Should be clocked on every even cpu cycle
if (~odd_or_even) begin
if (TimerCtr == 0) begin
// Timer was clocked
TimerCtr <= Period;
SeqPos <= SeqPos - 1'd1;
end else begin
TimerCtr <= TimerCtr - 1'd1;
end
end
// Clock the length counter?
if (LenCtrClockEnable && LenCtr != 0 && !LenCtrHalt) begin
LenCtr <= LenCtr - 1'd1;
end
// Clock the sweep unit?
if (LenCtr_Clock) begin
if (SweepDivider == 0) begin
SweepDivider <= SweepPeriod;
if (SweepEnable && SweepShift != 0 && ValidFreq)
Period <= NewSweepPeriod[10:0];
end else begin
SweepDivider <= SweepDivider - 1'd1;
end
if (SweepReset)
SweepDivider <= SweepPeriod;
SweepReset <= 0;
end
// Clock the envelope generator?
if (Env_Clock) begin
if (EnvDoReset) begin
EnvDivider <= Volume;
Envelope <= 15;
EnvDoReset <= 0;
end else if (EnvDivider == 0) begin
EnvDivider <= Volume;
if (Envelope != 0 || EnvLoop)
Envelope <= Envelope - 1'd1;
end else begin
EnvDivider <= EnvDivider - 1'd1;
end
end
// Length counter forced to zero if disabled.
if (!Enabled)
LenCtr <= 0;
end
wire DutyEnabledUsed = (MMC5==1) ^ DutyEnabled;
reg DutyEnabled;
always @* begin
// Determine if the duty is enabled or not
case (Duty)
0: DutyEnabled = (SeqPos == 7);
1: DutyEnabled = (SeqPos >= 6);
2: DutyEnabled = (SeqPos >= 4);
3: DutyEnabled = (SeqPos < 6);
endcase
// Compute the output
if (LenCtr == 0 || !ValidFreq || !DutyEnabledUsed)
Sample = 0;
else
Sample = EnvDisable ? Volume : Envelope;
end
endmodule
module TriangleChan(input clk, input ce, input reset,
input [1:0] Addr,
input [7:0] DIN,
input MW,
input LenCtr_Clock,
input LinCtr_Clock,
input Enabled,
input [7:0] LenCtr_In,
output reg [3:0] Sample,
output IsNonZero);
//
reg [10:0] Period, TimerCtr;
reg [4:0] SeqPos;
//
// Linear counter state
reg [6:0] LinCtrPeriod, LinCtr;
reg LinCtrl, LinHalt;
wire LinCtrZero = (LinCtr == 0);
//
// Length counter state
reg [7:0] LenCtr;
wire LenCtrHalt = LinCtrl; // Aliased bit
wire LenCtrZero = (LenCtr == 0);
assign IsNonZero = !LenCtrZero;
//
always @(posedge clk) if (reset) begin
Period <= 0;
TimerCtr <= 0;
SeqPos <= 0;
LinCtrPeriod <= 0;
LinCtr <= 0;
//LinCtrl <= 0; do not reset
LinHalt <= 0;
LenCtr <= 0;
end else if (ce) begin
// Check if writing to the regs of this channel
if (MW) begin
case (Addr)
0: begin
LinCtrl <= DIN[7];
LinCtrPeriod <= DIN[6:0];
end
2: begin
Period[7:0] <= DIN;
end
3: begin
Period[10:8] <= DIN[2:0];
LenCtr <= LenCtr_In;
LinHalt <= 1;
end
endcase
end
// Count down the period timer...
if (TimerCtr == 0) begin
TimerCtr <= Period;
end else begin
TimerCtr <= TimerCtr - 1'd1;
end
//
// Clock the length counter?
if (LenCtr_Clock && !LenCtrZero && !LenCtrHalt) begin
LenCtr <= LenCtr - 1'd1;
end
//
// Clock the linear counter?
if (LinCtr_Clock) begin
if (LinHalt)
LinCtr <= LinCtrPeriod;
else if (!LinCtrZero)
LinCtr <= LinCtr - 1'd1;
if (!LinCtrl)
LinHalt <= 0;
end
//
// Length counter forced to zero if disabled.
if (!Enabled)
LenCtr <= 0;
//
// Clock the sequencer position
if (TimerCtr == 0 && !LenCtrZero && !LinCtrZero)
SeqPos <= SeqPos + 1'd1;
end
// Generate the output
// XXX: Ultrisonic frequencies cause issues, so are disabled.
// This can be removed for accuracy if a proper LPF is ever implemented.
always @(posedge clk)
Sample <= (Period > 1) ? SeqPos[3:0] ^ {4{~SeqPos[4]}} : Sample;
//
endmodule
module NoiseChan(input clk, input ce, input reset,
input [1:0] Addr,
input [7:0] DIN,
input MW,
input LenCtr_Clock,
input Env_Clock,
input Enabled,
input [7:0] LenCtr_In,
output [3:0] Sample,
output IsNonZero);
//
// Envelope volume
reg EnvLoop, EnvDisable, EnvDoReset;
reg [3:0] Volume, Envelope, EnvDivider;
// Length counter
wire LenCtrHalt = EnvLoop; // Aliased bit
reg [7:0] LenCtr;
//
reg ShortMode;
reg [14:0] Shift = 1;
assign IsNonZero = (LenCtr != 0);
//
// Period stuff
reg [3:0] Period;
reg [11:0] NoisePeriod, TimerCtr;
always @* begin
case (Period)
0: NoisePeriod = 12'd4;
1: NoisePeriod = 12'd8;
2: NoisePeriod = 12'd16;
3: NoisePeriod = 12'd32;
4: NoisePeriod = 12'd64;
5: NoisePeriod = 12'd96;
6: NoisePeriod = 12'd128;
7: NoisePeriod = 12'd160;
8: NoisePeriod = 12'd202;
9: NoisePeriod = 12'd254;
10: NoisePeriod = 12'd380;
11: NoisePeriod = 12'd508;
12: NoisePeriod = 12'd762;
13: NoisePeriod = 12'd1016;
14: NoisePeriod = 12'd2034;
15: NoisePeriod = 12'd4068;
endcase
end
//
always @(posedge clk) if (reset) begin
EnvLoop <= 0;
EnvDisable <= 0;
EnvDoReset <= 0;
Volume <= 0;
Envelope <= 0;
EnvDivider <= 0;
LenCtr <= 1;
ShortMode <= 0;
Shift <= 1;
Period <= 0;
TimerCtr <= NoisePeriod - 1'b1;
end else if (ce) begin
// Check if writing to the regs of this channel
if (MW) begin
case (Addr)
0: begin
EnvLoop <= DIN[5];
EnvDisable <= DIN[4];
Volume <= DIN[3:0];
end
2: begin
ShortMode <= DIN[7];
Period <= DIN[3:0];
end
3: begin
LenCtr <= LenCtr_In;
EnvDoReset <= 1;
end
endcase
end
// Count down the period timer...
if (TimerCtr == 0) begin
TimerCtr <= NoisePeriod - 1'b1;
// Clock the shift register. Use either
// bit 1 or 6 as the tap.
Shift <= {
Shift[0] ^ (ShortMode ? Shift[6] : Shift[1]),
Shift[14:1]};
end else begin
TimerCtr <= TimerCtr - 1'd1;
end
// Clock the length counter?
if (LenCtr_Clock && LenCtr != 0 && !LenCtrHalt) begin
LenCtr <= LenCtr - 1'd1;
end
// Clock the envelope generator?
if (Env_Clock) begin
if (EnvDoReset) begin
EnvDivider <= Volume;
Envelope <= 15;
EnvDoReset <= 0;
end else if (EnvDivider == 0) begin
EnvDivider <= Volume;
if (Envelope != 0)
Envelope <= Envelope - 1'd1;
else if (EnvLoop)
Envelope <= 15;
end else
EnvDivider <= EnvDivider - 1'd1;
end
if (!Enabled)
LenCtr <= 0;
end
// Produce the output signal
assign Sample =
(LenCtr == 0 || Shift[0]) ?
4'd0 :
(EnvDisable ? Volume : Envelope);
endmodule
module DmcChan(input MMC5,
input clk, input ce, input reset,
input odd_or_even,
input [2:0] Addr,
input [7:0] DIN,
input MW,
output [6:0] Sample,
output DmaReq, // 1 when DMC wants DMA
input DmaAck, // 1 when DMC byte is on DmcData. DmcDmaRequested should go low.
output [15:0] DmaAddr, // Address DMC wants to read
input [7:0] DmaData, // Input data to DMC from memory.
output Irq,
input PAL,
output IsDmcActive);
reg IrqEnable;
reg IrqActive;
reg Loop; // Looping enabled
reg [3:0] Freq; // Current value of frequency register
reg [7:0] Dac = 0; // Current value of DAC
reg [7:0] SampleAddress; // Base address of sample
reg [7:0] SampleLen; // Length of sample
reg [7:0] ShiftReg; // Shift register
reg [8:0] Cycles; // Down counter, is the period
reg [14:0] Address; // 15 bits current address, 0x8000-0xffff
reg [11:0] BytesLeft; // 12 bits bytes left counter 0 - 4081.
reg [2:0] BitsUsed; // Number of bits left in the SampleBuffer.
reg [7:0] SampleBuffer; // Next value to be loaded into shift reg
reg HasSampleBuffer; // Sample buffer is nonempty
reg HasShiftReg; // Shift reg is non empty
reg DmcEnabled;
reg [1:0] ActivationDelay;
assign DmaAddr = {1'b1, Address};
assign Sample = Dac[6:0];
assign Irq = IrqActive;
assign IsDmcActive = DmcEnabled;
assign DmaReq = !HasSampleBuffer && DmcEnabled && !ActivationDelay[0];
reg [8:0] NewPeriod[16];
initial begin
NewPeriod[0] = 428;
NewPeriod[1] = 380;
NewPeriod[2] = 340;
NewPeriod[3] = 320;
NewPeriod[4] = 286;
NewPeriod[5] = 254;
NewPeriod[6] = 226;
NewPeriod[7] = 214;
NewPeriod[8] = 190;
NewPeriod[9] = 160;
NewPeriod[10] = 142;
NewPeriod[11] = 128;
NewPeriod[12] = 106;
NewPeriod[13] = 84;
NewPeriod[14] = 72;
NewPeriod[15] = 54;
end
reg [8:0] NewPeriodPAL[16];
initial begin
NewPeriodPAL[0] = 398;
NewPeriodPAL[1] = 354;
NewPeriodPAL[2] = 316;
NewPeriodPAL[3] = 298;
NewPeriodPAL[4] = 276;
NewPeriodPAL[5] = 236;
NewPeriodPAL[6] = 210;
NewPeriodPAL[7] = 198;
NewPeriodPAL[8] = 176;
NewPeriodPAL[9] = 148;
NewPeriodPAL[10] = 132;
NewPeriodPAL[11] = 118;
NewPeriodPAL[12] = 98;
NewPeriodPAL[13] = 78;
NewPeriodPAL[14] = 66;
NewPeriodPAL[15] = 50;
end
// Shift register initially loaded with 07
always @(posedge clk) begin
if (reset) begin
IrqEnable <= 0;
IrqActive <= 0;
Loop <= 0;
Freq <= 0;
Dac <= 0;
SampleAddress <= 0;
SampleLen <= 1;
ShiftReg <= 8'h0; // XXX: should be 0 or 07? Visual 2C02 says 0, as does Mesen.
Cycles <= 439;
Address <= 15'h4000;
BytesLeft <= 0;
BitsUsed <= 0;
SampleBuffer <= 0;
HasSampleBuffer <= 0;
HasShiftReg <= 0;
DmcEnabled <= 1;
ActivationDelay <= 0;
end else if (ce) begin
if (ActivationDelay == 3 && !odd_or_even) ActivationDelay <= 1;
if (ActivationDelay == 1) ActivationDelay <= 0;
if (MW) begin
case (Addr)
0: begin // $4010 il-- ffff IRQ enable, loop, frequency index
IrqEnable <= DIN[7];
Loop <= DIN[6];
Freq <= DIN[3:0];
if (!DIN[7]) IrqActive <= 0;
end
1: begin // $4011 -ddd dddd DAC
// This will get missed if the Dac <= far below runs, that is by design.
Dac <= {(MMC5==1) && DIN[7],DIN[6:0]};
end
2: begin // $4012 aaaa aaaa sample address
SampleAddress <= (MMC5==1) ? 8'h0 : DIN[7:0];
end
3: begin // $4013 llll llll sample length
SampleLen <= (MMC5==1) ? 8'h0 : DIN[7:0];
end
5: begin // $4015 write ---D NT21 Enable DMC (D)
IrqActive <= 0;
DmcEnabled <= DIN[4];
// If the DMC bit is set, the DMC sample will be restarted only if not already active.
if (DIN[4] && !DmcEnabled) begin
Address <= {1'b1, SampleAddress, 6'b000000};
BytesLeft <= {SampleLen, 4'b0000};
ActivationDelay <= 3;
end
end
endcase
end
Cycles <= Cycles - 1'd1;
if (Cycles == 1) begin
Cycles <= PAL ? NewPeriodPAL[Freq] : NewPeriod[Freq];
if (HasShiftReg) begin
if (ShiftReg[0]) begin
Dac[6:1] <= (Dac[6:1] != 6'b111111) ? Dac[6:1] + 6'b000001 : Dac[6:1];
end else begin
Dac[6:1] <= (Dac[6:1] != 6'b000000) ? Dac[6:1] + 6'b111111 : Dac[6:1];
end
end
ShiftReg <= {1'b0, ShiftReg[7:1]};
BitsUsed <= BitsUsed + 1'd1;
if (BitsUsed == 7) begin
HasShiftReg <= HasSampleBuffer;
ShiftReg <= SampleBuffer;
HasSampleBuffer <= 0;
end
end
// Acknowledge DMA?
if (DmaAck) begin
Address <= Address + 1'd1;
BytesLeft <= BytesLeft - 1'd1;
HasSampleBuffer <= 1;
SampleBuffer <= DmaData;
if (BytesLeft == 0) begin
Address <= {1'b1, SampleAddress, 6'b000000};
BytesLeft <= {SampleLen, 4'b0000};
DmcEnabled <= Loop;
if (!Loop && IrqEnable)
IrqActive <= 1;
end
end
end
end
endmodule
module APU(input MMC5,
input clk, input ce, input reset,
input PAL,
input [4:0] ADDR, // APU Address Line
input [7:0] DIN, // Data to APU
output [7:0] DOUT, // Data from APU
input MW, // Writes to APU
input MR, // Reads from APU
input [4:0] audio_channels, // Enabled audio channels
output [15:0] Sample,
output DmaReq, // 1 when DMC wants DMA
input DmaAck, // 1 when DMC byte is on DmcData. DmcDmaRequested should go low.
output [15:0] DmaAddr, // Address DMC wants to read
input [7:0] DmaData, // Input data to DMC from memory.
input odd_or_even,
output IRQ); // IRQ asserted
// Which channels are enabled?
reg [3:0] Enabled;
// Output samples from the 4 channels
wire [3:0] Sq1Sample,Sq2Sample,TriSample,NoiSample;
// Output samples from the DMC channel
wire [6:0] DmcSample;
wire DmcIrq;
wire IsDmcActive;
// Generate internal memory write signals
wire ApuMW0 = MW && ADDR[4:2]==0; // SQ1
wire ApuMW1 = MW && ADDR[4:2]==1; // SQ2
wire ApuMW2 = MW && ADDR[4:2]==2; // TRI
wire ApuMW3 = MW && ADDR[4:2]==3; // NOI
wire ApuMW4 = MW && ADDR[4:2]>=4; // DMC
wire ApuMW5 = MW && ADDR[4:2]==5; // Control registers
wire Sq1NonZero, Sq2NonZero, TriNonZero, NoiNonZero;
// Common input to all channels
wire [7:0] LenCtr_In;
LenCtr_Lookup len(DIN[7:3], LenCtr_In);
// Frame sequencer registers
reg FrameSeqMode;
reg [15:0] Cycles;
reg ClkE, ClkL;
reg Wrote4017;
reg [1:0] IrqCtr;
// Generate each channel
SquareChan Sq1(MMC5, clk, ce, reset, 1'b0, ADDR[1:0], DIN, ApuMW0, ClkL, ClkE, odd_or_even, Enabled[0], LenCtr_In, Sq1Sample, Sq1NonZero);
SquareChan Sq2(MMC5, clk, ce, reset, 1'b1, ADDR[1:0], DIN, ApuMW1, ClkL, ClkE, odd_or_even, Enabled[1], LenCtr_In, Sq2Sample, Sq2NonZero);
TriangleChan Tri(clk, ce, reset, ADDR[1:0], DIN, ApuMW2, ClkL, ClkE, Enabled[2], LenCtr_In, TriSample, TriNonZero);
NoiseChan Noi(clk, ce, reset, ADDR[1:0], DIN, ApuMW3, ClkL, ClkE, Enabled[3], LenCtr_In, NoiSample, NoiNonZero);
DmcChan Dmc(MMC5, clk, ce, reset, odd_or_even, ADDR[2:0], DIN, ApuMW4, DmcSample, DmaReq, DmaAck, DmaAddr, DmaData, DmcIrq, PAL, IsDmcActive);
// Reading this register clears the frame interrupt flag (but not the DMC interrupt flag).
// If an interrupt flag was set at the same moment of the read, it will read back as 1 but it will not be cleared.
reg FrameInterrupt, DisableFrameInterrupt;
//mode 0: 4-step effective rate (approx)
//---------------------------------------
// - - - f 60 Hz
// - l - l 120 Hz
// e e e e 240 Hz
//mode 1: 5-step effective rate (approx)
//---------------------------------------
// - - - - - (interrupt flag never set)
// l - l - - 96 Hz
// e e e e - 192 Hz
reg [7:0] last_4017 = 0;
reg [2:0] delayed_clear;
reg delayed_interrupt;
wire set_irq_ntsc = (Cycles == cyc_ntsc[3]) || (Cycles == cyc_ntsc[4]);
wire set_irq_pal = (Cycles == cyc_pal[3]) || (Cycles == cyc_pal[4]);
wire set_irq = ( (PAL ? set_irq_pal : set_irq_ntsc) || delayed_interrupt) && ~DisableFrameInterrupt && ~FrameSeqMode;
reg [15:0] cyc_pal[7];
reg [15:0] cyc_ntsc[7];
initial begin
cyc_pal[0] = 312;
cyc_pal[1] = 16626;
cyc_pal[2] = 24938;
cyc_pal[3] = 33251;
cyc_pal[4] = 33252;
cyc_pal[5] = 41564;
cyc_pal[6] = 41565;
end
initial begin
cyc_ntsc[0] = 7456;
cyc_ntsc[1] = 14912;
cyc_ntsc[2] = 22370;
cyc_ntsc[3] = 29828;
cyc_ntsc[4] = 29829;
cyc_ntsc[5] = 37280;
cyc_ntsc[6] = 37281;
end
always @(posedge clk) if (reset) begin
delayed_interrupt <= 0;
FrameInterrupt <= 0;
Enabled <= 0;
ClkE <= 0;
ClkL <= 0;
delayed_clear <= 0;
Cycles <= 0;
IrqCtr <= 0;
{FrameSeqMode, DisableFrameInterrupt} <= last_4017[7:6];
end else if (ce) begin
FrameInterrupt <= set_irq ? 1'd1 : (ADDR == 5'h15 && MR || ApuMW5 && ADDR[1:0] == 3 && DIN[6]) ? 1'd0 : FrameInterrupt;
IrqCtr <= {IrqCtr[0], 1'b0};
// NesDev's wiki on this is ambiguous and written from a strange perspective. To the best
// of my understanding, the Frame Counter works like this:
// The APU alternates between Read cycles (even) and Write cycles, (odd). The internal counter
// is incremented on every Write cycle, and if it hits certain special points, the clocks are
// generated *on* the next Read cycle. The counter can only be controlled by one thing at a time,
// so resetting the counter must always be on a Write cycle.
//
// In the case of writes to 4017, the internal registers are written normally as the write occurs, so
// that their values are visible on the next CE, however a reset cannot start writing to the counter
// until the *next* Write cycle. It's probably implemented as a flag to tell the counter to reset, that
// is written as part of the register data, and since the counter only runs on Writes, that's when it
// happens.
if (delayed_clear)
delayed_clear <= delayed_clear - 1'b1;
Cycles <= Cycles + 1'd1;
ClkE <= 0;
ClkL <= 0;
delayed_interrupt <= 1'b0;
if (Cycles == (PAL ? cyc_pal[0] : cyc_ntsc[0])) begin
ClkE <= 1;
end else if (Cycles == (PAL ? cyc_pal[1] : cyc_ntsc[1])) begin
ClkE <= 1;
ClkL <= 1;
end else if (Cycles == (PAL ? cyc_pal[2] : cyc_ntsc[2])) begin
ClkE <= 1;
end else if (Cycles == (PAL ? cyc_pal[3] : cyc_ntsc[3])) begin
if (!FrameSeqMode) begin
ClkE <= 1;
ClkL <= 1;
end
end else if (Cycles == (PAL ? cyc_pal[4] : cyc_ntsc[4])) begin
if (!FrameSeqMode) begin
delayed_interrupt <= 1'b1;
Cycles <= 0;
end
end else if (Cycles == (PAL ? cyc_pal[5] : cyc_ntsc[5])) begin
ClkE <= 1;
ClkL <= 1;
end else if (Cycles == (PAL ? cyc_pal[6] : cyc_ntsc[6])) begin
Cycles <= 0;
end
// Handle one cycle delayed write to 4017.
if (delayed_clear == 1) begin
if (FrameSeqMode) begin
ClkE <= 1;
ClkL <= 1;
end
Cycles <= 0;
end
// Handle writes to control registers
if (ApuMW5) begin
case (ADDR[1:0])
1: begin // Register $4015
Enabled <= DIN[3:0];
end
3: begin // Register $4017
if (~MMC5) begin
last_4017 <= DIN;
FrameSeqMode <= DIN[7]; // 1 = 5 frames cycle, 0 = 4 frames cycle
DisableFrameInterrupt <= DIN[6];
if (odd_or_even)
delayed_clear <= 3'd2;
else
delayed_clear <= 3'd1;
end
end
endcase
end
end
ApuLookupTable lookup(clk,
(audio_channels[0] ? {4'b0, Sq1Sample} : 8'b0) +
(audio_channels[1] ? {4'b0, Sq2Sample} : 8'b0),
(audio_channels[2] ? {4'b0, TriSample} + {3'b0, TriSample, 1'b0} : 8'b0) +
(audio_channels[3] ? {3'b0, NoiSample, 1'b0} : 8'b0) +
(audio_channels[4] ? {1'b0, DmcSample} : 8'b0),
Sample);
// TODO: Rework the LUT initialization so yosys can synthesize the module
/*APUMixer mixer (
.square1(Sq1Sample),
.square2(Sq2Sample),
.noise(NoiSample),
.triangle(TriSample),
.dmc(DmcSample),
.sample(Sample)
);*/
wire frame_irq = FrameInterrupt && !DisableFrameInterrupt;
// Generate bus output
assign DOUT = {DmcIrq, FrameInterrupt, 1'b0,
IsDmcActive,
NoiNonZero,
TriNonZero,
Sq2NonZero,
Sq1NonZero};
assign IRQ = frame_irq || DmcIrq;
endmodule
module ApuLookupTable(input clk, input [7:0] in_a, input [7:0] in_b, output [15:0] out);
reg [15:0] lookup[0:511];
reg [15:0] tmp_a, tmp_b;
initial begin
lookup[ 0] = 0; lookup[ 1] = 760; lookup[ 2] = 1503; lookup[ 3] = 2228;
lookup[ 4] = 2936; lookup[ 5] = 3627; lookup[ 6] = 4303; lookup[ 7] = 4963;
lookup[ 8] = 5609; lookup[ 9] = 6240; lookup[ 10] = 6858; lookup[ 11] = 7462;
lookup[ 12] = 8053; lookup[ 13] = 8631; lookup[ 14] = 9198; lookup[ 15] = 9752;
lookup[ 16] = 10296; lookup[ 17] = 10828; lookup[ 18] = 11349; lookup[ 19] = 11860;
lookup[ 20] = 12361; lookup[ 21] = 12852; lookup[ 22] = 13334; lookup[ 23] = 13807;
lookup[ 24] = 14270; lookup[ 25] = 14725; lookup[ 26] = 15171; lookup[ 27] = 15609;
lookup[ 28] = 16039; lookup[ 29] = 16461; lookup[ 30] = 16876; lookup[256] = 0;
lookup[257] = 439; lookup[258] = 874; lookup[259] = 1306; lookup[260] = 1735;
lookup[261] = 2160; lookup[262] = 2581; lookup[263] = 2999; lookup[264] = 3414;
lookup[265] = 3826; lookup[266] = 4234; lookup[267] = 4639; lookup[268] = 5041;
lookup[269] = 5440; lookup[270] = 5836; lookup[271] = 6229; lookup[272] = 6618;
lookup[273] = 7005; lookup[274] = 7389; lookup[275] = 7769; lookup[276] = 8147;
lookup[277] = 8522; lookup[278] = 8895; lookup[279] = 9264; lookup[280] = 9631;
lookup[281] = 9995; lookup[282] = 10356; lookup[283] = 10714; lookup[284] = 11070;
lookup[285] = 11423; lookup[286] = 11774; lookup[287] = 12122; lookup[288] = 12468;
lookup[289] = 12811; lookup[290] = 13152; lookup[291] = 13490; lookup[292] = 13825;
lookup[293] = 14159; lookup[294] = 14490; lookup[295] = 14818; lookup[296] = 15145;
lookup[297] = 15469; lookup[298] = 15791; lookup[299] = 16110; lookup[300] = 16427;
lookup[301] = 16742; lookup[302] = 17055; lookup[303] = 17366; lookup[304] = 17675;
lookup[305] = 17981; lookup[306] = 18286; lookup[307] = 18588; lookup[308] = 18888;
lookup[309] = 19187; lookup[310] = 19483; lookup[311] = 19777; lookup[312] = 20069;
lookup[313] = 20360; lookup[314] = 20648; lookup[315] = 20935; lookup[316] = 21219;
lookup[317] = 21502; lookup[318] = 21783; lookup[319] = 22062; lookup[320] = 22339;
lookup[321] = 22615; lookup[322] = 22889; lookup[323] = 23160; lookup[324] = 23431;
lookup[325] = 23699; lookup[326] = 23966; lookup[327] = 24231; lookup[328] = 24494;
lookup[329] = 24756; lookup[330] = 25016; lookup[331] = 25274; lookup[332] = 25531;
lookup[333] = 25786; lookup[334] = 26040; lookup[335] = 26292; lookup[336] = 26542;
lookup[337] = 26791; lookup[338] = 27039; lookup[339] = 27284; lookup[340] = 27529;
lookup[341] = 27772; lookup[342] = 28013; lookup[343] = 28253; lookup[344] = 28492;
lookup[345] = 28729; lookup[346] = 28964; lookup[347] = 29198; lookup[348] = 29431;
lookup[349] = 29663; lookup[350] = 29893; lookup[351] = 30121; lookup[352] = 30349;
lookup[353] = 30575; lookup[354] = 30800; lookup[355] = 31023; lookup[356] = 31245;
lookup[357] = 31466; lookup[358] = 31685; lookup[359] = 31904; lookup[360] = 32121;
lookup[361] = 32336; lookup[362] = 32551; lookup[363] = 32764; lookup[364] = 32976;
lookup[365] = 33187; lookup[366] = 33397; lookup[367] = 33605; lookup[368] = 33813;
lookup[369] = 34019; lookup[370] = 34224; lookup[371] = 34428; lookup[372] = 34630;
lookup[373] = 34832; lookup[374] = 35032; lookup[375] = 35232; lookup[376] = 35430;
lookup[377] = 35627; lookup[378] = 35823; lookup[379] = 36018; lookup[380] = 36212;
lookup[381] = 36405; lookup[382] = 36597; lookup[383] = 36788; lookup[384] = 36978;
lookup[385] = 37166; lookup[386] = 37354; lookup[387] = 37541; lookup[388] = 37727;
lookup[389] = 37912; lookup[390] = 38095; lookup[391] = 38278; lookup[392] = 38460;
lookup[393] = 38641; lookup[394] = 38821; lookup[395] = 39000; lookup[396] = 39178;
lookup[397] = 39355; lookup[398] = 39532; lookup[399] = 39707; lookup[400] = 39881;
lookup[401] = 40055; lookup[402] = 40228; lookup[403] = 40399; lookup[404] = 40570;
lookup[405] = 40740; lookup[406] = 40909; lookup[407] = 41078; lookup[408] = 41245;
lookup[409] = 41412; lookup[410] = 41577; lookup[411] = 41742; lookup[412] = 41906;
lookup[413] = 42070; lookup[414] = 42232; lookup[415] = 42394; lookup[416] = 42555;
lookup[417] = 42715; lookup[418] = 42874; lookup[419] = 43032; lookup[420] = 43190;
lookup[421] = 43347; lookup[422] = 43503; lookup[423] = 43659; lookup[424] = 43813;
lookup[425] = 43967; lookup[426] = 44120; lookup[427] = 44273; lookup[428] = 44424;
lookup[429] = 44575; lookup[430] = 44726; lookup[431] = 44875; lookup[432] = 45024;
lookup[433] = 45172; lookup[434] = 45319; lookup[435] = 45466; lookup[436] = 45612;
lookup[437] = 45757; lookup[438] = 45902; lookup[439] = 46046; lookup[440] = 46189;
lookup[441] = 46332; lookup[442] = 46474; lookup[443] = 46615; lookup[444] = 46756;
lookup[445] = 46895; lookup[446] = 47035; lookup[447] = 47173; lookup[448] = 47312;
lookup[449] = 47449; lookup[450] = 47586; lookup[451] = 47722; lookup[452] = 47857;
lookup[453] = 47992; lookup[454] = 48127; lookup[455] = 48260; lookup[456] = 48393;
lookup[457] = 48526; lookup[458] = 48658;
end
always @(posedge clk) begin
tmp_a <= lookup[{1'b0, in_a}];
tmp_b <= lookup[{1'b1, in_b}];
end
assign out = tmp_a + tmp_b;
endmodule
// http://wiki.nesdev.com/w/index.php/APU_Mixer
// I generated three LUT's for each mix channel entry and one lut for the squares, then a
// 282 entry lut for the mix channel. It's more accurate than the original LUT system listed on
// the NesDev page.
/*module APUMixer (
input [3:0] square1,
input [3:0] square2,
input [3:0] triangle,
input [3:0] noise,
input [6:0] dmc,
output [15:0] sample
);
wire [15:0] pulse_lut[32] = '{
16'd0, 16'd763, 16'd1509, 16'd2236, 16'd2947, 16'd3641, 16'd4319, 16'd4982,
16'd5630, 16'd6264, 16'd6883, 16'd7490, 16'd8083, 16'd8664, 16'd9232, 16'd9789,
16'd10334, 16'd10868, 16'd11392, 16'd11905, 16'd12408, 16'd12901, 16'd13384, 16'd13858,
16'd14324, 16'd14780, 16'd15228, 16'd15668, 16'd16099, 16'd16523, 16'd16939, 16'd17348
};
wire [5:0] tri_lut[16] = '{
6'd0, 6'd3, 6'd7, 6'd11, 6'd15, 6'd19, 6'd23, 6'd27,
6'd31, 6'd35, 6'd39, 6'd43, 6'd47, 6'd51, 6'd55, 6'd59
};
wire [5:0] noise_lut[16] = '{
6'd0, 6'd2, 6'd5, 6'd8, 6'd10, 6'd13, 6'd16, 6'd18,
6'd21, 6'd24, 6'd26, 6'd29, 6'd32, 6'd34, 6'd37, 6'd40
};
wire [7:0] dmc_lut[128] = '{
8'd0, 8'd1, 8'd2, 8'd4, 8'd5, 8'd7, 8'd8, 8'd10, 8'd11, 8'd13, 8'd14, 8'd15, 8'd17, 8'd18, 8'd20, 8'd21,
8'd23, 8'd24, 8'd26, 8'd27, 8'd28, 8'd30, 8'd31, 8'd33, 8'd34, 8'd36, 8'd37, 8'd39, 8'd40, 8'd41, 8'd43, 8'd44,
8'd46, 8'd47, 8'd49, 8'd50, 8'd52, 8'd53, 8'd55, 8'd56, 8'd57, 8'd59, 8'd60, 8'd62, 8'd63, 8'd65, 8'd66, 8'd68,
8'd69, 8'd70, 8'd72, 8'd73, 8'd75, 8'd76, 8'd78, 8'd79, 8'd81, 8'd82, 8'd83, 8'd85, 8'd86, 8'd88, 8'd89, 8'd91,
8'd92, 8'd94, 8'd95, 8'd96, 8'd98, 8'd99, 8'd101, 8'd102, 8'd104, 8'd105, 8'd107, 8'd108, 8'd110, 8'd111, 8'd112, 8'd114,
8'd115, 8'd117, 8'd118, 8'd120, 8'd121, 8'd123, 8'd124, 8'd125, 8'd127, 8'd128, 8'd130, 8'd131, 8'd133, 8'd134, 8'd136, 8'd137,
8'd138, 8'd140, 8'd141, 8'd143, 8'd144, 8'd146, 8'd147, 8'd149, 8'd150, 8'd151, 8'd153, 8'd154, 8'd156, 8'd157, 8'd159, 8'd160,
8'd162, 8'd163, 8'd165, 8'd166, 8'd167, 8'd169, 8'd170, 8'd172, 8'd173, 8'd175, 8'd176, 8'd178, 8'd179, 8'd180, 8'd182, 8'd183
};
wire [15:0] mix_lut[512] = '{
16'd0, 16'd318, 16'd635, 16'd950, 16'd1262, 16'd1573, 16'd1882, 16'd2190, 16'd2495, 16'd2799, 16'd3101, 16'd3401, 16'd3699, 16'd3995, 16'd4290, 16'd4583,
16'd4875, 16'd5164, 16'd5452, 16'd5739, 16'd6023, 16'd6306, 16'd6588, 16'd6868, 16'd7146, 16'd7423, 16'd7698, 16'd7971, 16'd8243, 16'd8514, 16'd8783, 16'd9050,
16'd9316, 16'd9581, 16'd9844, 16'd10105, 16'd10365, 16'd10624, 16'd10881, 16'd11137, 16'd11392, 16'd11645, 16'd11897, 16'd12147, 16'd12396, 16'd12644, 16'd12890, 16'd13135,
16'd13379, 16'd13622, 16'd13863, 16'd14103, 16'd14341, 16'd14579, 16'd14815, 16'd15050, 16'd15284, 16'd15516, 16'd15747, 16'd15978, 16'd16206, 16'd16434, 16'd16661, 16'd16886,
16'd17110, 16'd17333, 16'd17555, 16'd17776, 16'd17996, 16'd18215, 16'd18432, 16'd18649, 16'd18864, 16'd19078, 16'd19291, 16'd19504, 16'd19715, 16'd19925, 16'd20134, 16'd20342,
16'd20549, 16'd20755, 16'd20960, 16'd21163, 16'd21366, 16'd21568, 16'd21769, 16'd21969, 16'd22169, 16'd22367, 16'd22564, 16'd22760, 16'd22955, 16'd23150, 16'd23343, 16'd23536,
16'd23727, 16'd23918, 16'd24108, 16'd24297, 16'd24485, 16'd24672, 16'd24858, 16'd25044, 16'd25228, 16'd25412, 16'd25595, 16'd25777, 16'd25958, 16'd26138, 16'd26318, 16'd26497,
16'd26674, 16'd26852, 16'd27028, 16'd27203, 16'd27378, 16'd27552, 16'd27725, 16'd27898, 16'd28069, 16'd28240, 16'd28410, 16'd28579, 16'd28748, 16'd28916, 16'd29083, 16'd29249,
16'd29415, 16'd29580, 16'd29744, 16'd29907, 16'd30070, 16'd30232, 16'd30393, 16'd30554, 16'd30714, 16'd30873, 16'd31032, 16'd31190, 16'd31347, 16'd31503, 16'd31659, 16'd31815,
16'd31969, 16'd32123, 16'd32276, 16'd32429, 16'd32581, 16'd32732, 16'd32883, 16'd33033, 16'd33182, 16'd33331, 16'd33479, 16'd33627, 16'd33774, 16'd33920, 16'd34066, 16'd34211,
16'd34356, 16'd34500, 16'd34643, 16'd34786, 16'd34928, 16'd35070, 16'd35211, 16'd35352, 16'd35492, 16'd35631, 16'd35770, 16'd35908, 16'd36046, 16'd36183, 16'd36319, 16'd36456,
16'd36591, 16'd36726, 16'd36860, 16'd36994, 16'd37128, 16'd37261, 16'd37393, 16'd37525, 16'd37656, 16'd37787, 16'd37917, 16'd38047, 16'd38176, 16'd38305, 16'd38433, 16'd38561,
16'd38689, 16'd38815, 16'd38942, 16'd39068, 16'd39193, 16'd39318, 16'd39442, 16'd39566, 16'd39690, 16'd39813, 16'd39935, 16'd40057, 16'd40179, 16'd40300, 16'd40421, 16'd40541,
16'd40661, 16'd40780, 16'd40899, 16'd41017, 16'd41136, 16'd41253, 16'd41370, 16'd41487, 16'd41603, 16'd41719, 16'd41835, 16'd41950, 16'd42064, 16'd42178, 16'd42292, 16'd42406,
16'd42519, 16'd42631, 16'd42743, 16'd42855, 16'd42966, 16'd43077, 16'd43188, 16'd43298, 16'd43408, 16'd43517, 16'd43626, 16'd43735, 16'd43843, 16'd43951, 16'd44058, 16'd44165,
16'd44272, 16'd44378, 16'd44484, 16'd44589, 16'd44695, 16'd44799, 16'd44904, 16'd45008, 16'd45112, 16'd45215, 16'd45318, 16'd45421, 16'd45523, 16'd45625, 16'd45726, 16'd45828,
16'd45929, 16'd46029, 16'd46129, 16'd46229, 16'd46329, 16'd46428, 16'd46527, 16'd46625, 16'd46723, 16'd46821, 16'd46919, 16'd47016, 16'd47113, 16'd47209, 16'd47306, 16'd47402,
16'd47497, 16'd47592, 16'd47687, 16'd47782, 16'd47876, 16'd47970, 16'd48064, 16'd48157, 16'd48250, 16'd48343, 16'd48436, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0,
16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0, 16'd0
};
wire [4:0] squares = square1 + square2;
wire [8:0] mix = tri_lut[triangle] + noise_lut[noise] + dmc_lut[dmc];
wire [15:0] ch1 = pulse_lut[squares];
wire [15:0] ch2 = mix_lut[mix];
wire [63:0] chan_mix = ch1 + ch2;
assign sample = chan_mix > 16'hFFFF ? 16'hFFFF : chan_mix[15:0];
endmodule*/