-
Notifications
You must be signed in to change notification settings - Fork 4
/
apu.vhd
2059 lines (1824 loc) · 58.8 KB
/
apu.vhd
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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of fpgaNES.
fpgaNES is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
fpgaNES is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with fpgaNES. If not, see <http://www.gnu.org/licenses/>.
*/
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity envelope is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n : in std_logic := '1';
i_reload : in boolean := false;
i_loop : in std_logic := '0';
i_disable : in std_logic := '1';
i_volume : in std_logic_vector(3 downto 0) := "0000";
o_q : out std_logic_vector(3 downto 0)
);
end envelope;
architecture behavioral of envelope is
signal s_counter : std_logic_vector(3 downto 0) := (others => '1');
signal s_divider : std_logic_vector(3 downto 0) := (others => '0');
begin
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_divider <= (others => '0');
s_counter <= (others => '1');
elsif i_clk_enable = '1' then
if i_reload then
s_divider <= i_volume;
s_counter <= (others => '1');
elsif s_divider /= "0000" then
s_divider <= s_divider - "0001";
else
s_divider <= i_volume;
if s_counter /= "0000" then
s_counter <= s_counter - "0001";
elsif i_loop = '1' then
s_counter <= (others => '1');
end if;
end if;
end if;
end if;
end process;
o_q <= i_volume when i_disable = '1' else s_counter;
end behavioral;
/********************************************************/
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity length_counter is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n : in std_logic := '1';
i_lcounter_clk : in std_logic := '1';
i_channel_enable : in std_logic := '1';
i_channel_reload : in std_logic := '0';
i_enable : in std_logic := '1';
i_addr : in std_logic_vector(1 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_write_enable : in std_logic := '0';
i_cs_n : in std_logic := '1';
o_active : out std_logic
);
end length_counter;
architecture behavioral of length_counter is
type length_counter_t is array (0 to 31) of std_logic_vector(7 downto 0);
constant LENGTH_COUNTER_TABLE : length_counter_t := ( x"0A", x"FE", x"14", x"02", x"28", x"04", x"50", x"06", x"A0", x"08", x"3C", x"0A", x"0E", x"0C", x"1A", x"0E",
x"0C", x"10", x"18", x"12", x"30", x"14", x"60", x"16", x"C0", x"18", x"48", x"1A", x"10", x"1C", x"20", x"1E" );
signal s_length_active : boolean;
signal s_length_counter : std_logic_vector(7 downto 0) := x"00";
begin
process (i_clk)
variable length_index : integer range 0 to 31;
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_length_counter <= x"00";
elsif i_clk_enable = '1' then
if (i_cs_n = '0') and (i_write_enable = '1') and (i_addr = "11") and (i_channel_enable = '1') and ((i_lcounter_clk = '0') or (s_length_counter = x"00")) then
length_index := to_integer(unsigned(i_data(7 downto 3)));
s_length_counter <= LENGTH_COUNTER_TABLE(length_index);
elsif (i_channel_reload = '1') and (i_channel_enable = '0') then
s_length_counter <= x"00";
elsif (i_lcounter_clk = '1') and s_length_active and (i_enable = '1') then
s_length_counter <= s_length_counter - x"01";
end if;
end if;
end if;
end process;
s_length_active <= s_length_counter /= x"00";
o_active <= '1' when s_length_active else '0';
end behavioral;
/********************************************************/
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity square_channel is
generic
(
INCREMENT : std_logic := '0'
);
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic;
i_reset_n : in std_logic := '1';
i_apu_clk : in std_logic;
i_envelope_clk : in std_logic := '1';
i_lcounter_clk : in std_logic := '1';
i_addr : in std_logic_vector(1 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_write_enable : in std_logic := '0';
i_cs_n : in std_logic := '1';
i_enable : in std_logic := '0';
i_reload : in std_logic := '0';
o_active : out std_logic;
o_q : out std_logic_vector(3 downto 0)
);
end square_channel;
architecture behavioral of square_channel is
component envelope is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n : in std_logic := '1';
i_reload : in boolean := false;
i_loop : in std_logic := '0';
i_disable : in std_logic := '1';
i_volume : in std_logic_vector(3 downto 0) := "0000";
o_q : out std_logic_vector(3 downto 0)
);
end component;
component length_counter is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n : in std_logic := '1';
i_lcounter_clk : in std_logic := '1';
i_channel_enable : in std_logic := '1';
i_channel_reload : in std_logic := '0';
i_enable : in std_logic := '1';
i_addr : in std_logic_vector(1 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_write_enable : in std_logic := '0';
i_cs_n : in std_logic := '1';
o_active : out std_logic
);
end component;
type duty_table_t is array (0 to 31) of std_logic;
constant DUTY_TABLE : duty_table_t := ( '0', '0', '0', '0', '0', '0', '0', '1',
'0', '0', '0', '0', '0', '0', '1', '1',
'0', '0', '0', '0', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '0', '0' );
signal s_duty : std_logic_vector(1 downto 0) := "00";
signal s_envelope_reload : boolean := false;
signal s_envelope_loop : std_logic := '0';
signal s_envelope_disable : std_logic := '1';
signal s_envelope_volume : std_logic_vector(3 downto 0) := "0000";
signal s_envelope_q : std_logic_vector(3 downto 0);
signal s_sweep_enable : std_logic := '0';
signal s_sweep_period : std_logic_vector(2 downto 0) := "000";
signal s_sweep_negate : std_logic := '0';
signal s_sweep_shift : std_logic_vector(2 downto 0) := "000";
signal s_sweep_update : std_logic := '0';
signal s_length_active : std_logic;
signal s_sweep_counter : std_logic_vector(2 downto 0) := "000";
signal s_duty_counter : std_logic_vector(2 downto 0) := "000";
signal s_current_period : std_logic_vector(10 downto 0) := 11x"0000";
signal s_freq_counter : std_logic_vector(10 downto 0) := 11x"0000";
signal s_shift_res : std_logic_vector(10 downto 0);
signal s_target_period : std_logic_vector(11 downto 0);
signal s_target_in_range : boolean;
signal s_write_ch : boolean;
signal s_write_r0 : boolean;
signal s_write_r1 : boolean;
signal s_write_r2 : boolean;
signal s_write_r3 : boolean;
signal s_duty_index : integer range 0 to 31;
signal s_sweep_reload : boolean := false;
signal s_timer_reload : boolean := false;
signal s_timer_done : boolean;
begin
ev : envelope port map
(
i_clk => i_clk,
i_clk_enable => i_envelope_clk,
i_reset_n => i_reset_n,
i_reload => s_envelope_reload,
i_loop => s_envelope_loop,
i_disable => s_envelope_disable,
i_volume => s_envelope_volume,
o_q => s_envelope_q
);
lc : length_counter port map
(
i_clk => i_clk,
i_clk_enable => i_clk_enable,
i_reset_n => i_reset_n,
i_lcounter_clk => i_lcounter_clk,
i_channel_enable => i_enable,
i_channel_reload => i_reload,
i_enable => not s_envelope_loop,
i_addr => i_addr,
i_data => i_data,
i_write_enable => i_write_enable,
i_cs_n => i_cs_n,
o_active => s_length_active
);
-- Register
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_duty <= "00";
s_envelope_loop <= '0';
s_envelope_disable <= '1';
s_envelope_volume <= "0000";
elsif i_clk_enable = '1' then
if s_write_r0 then
s_duty <= i_data(7 downto 6);
s_envelope_loop <= i_data(5);
s_envelope_disable <= i_data(4);
s_envelope_volume <= i_data(3 downto 0);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_sweep_enable <= '0';
s_sweep_period <= "000";
s_sweep_negate <= '0';
s_sweep_shift <= "000";
elsif i_clk_enable = '1' then
if s_write_r1 then
s_sweep_enable <= i_data(7) and (i_data(2) or i_data(1) or i_data(0));
s_sweep_period <= i_data(6 downto 4);
s_sweep_negate <= i_data(3);
s_sweep_shift <= i_data(2 downto 0);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_sweep_reload <= false;
elsif i_clk_enable = '1' then
if s_write_r1 then
s_sweep_reload <= true;
elsif i_lcounter_clk = '1' then
s_sweep_reload <= false;
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_timer_reload <= false;
elsif i_clk_enable = '1' then
if s_write_r3 then
s_timer_reload <= true;
elsif i_apu_clk = '1' then
s_timer_reload <= false;
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_envelope_reload <= false;
elsif i_clk_enable = '1' then
if s_write_r3 then
s_envelope_reload <= true;
elsif i_envelope_clk = '1' then
s_envelope_reload <= false;
end if;
end if;
end if;
end process;
-- Duty
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_duty_counter <= "000";
elsif i_apu_clk = '1' then
if s_timer_reload then
s_duty_counter <= "000";
elsif s_timer_done then
s_duty_counter <= s_duty_counter - "001";
end if;
end if;
end if;
end process;
-- Timer
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_freq_counter <= 11x"0000";
elsif i_apu_clk = '1' then
if s_timer_done or s_timer_reload then
s_freq_counter <= s_current_period;
else
s_freq_counter <= s_freq_counter - 11x"0001";
end if;
end if;
end if;
end process;
-- Sweep
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_current_period <= 11x"0000";
elsif i_clk_enable = '1' then
if s_write_r2 then
s_current_period(7 downto 0) <= i_data;
elsif s_write_r3 then
s_current_period(10 downto 8) <= i_data(2 downto 0);
elsif (i_lcounter_clk = '1') and (s_sweep_enable = '1') and (s_sweep_counter = "000") and s_target_in_range then
s_current_period <= s_target_period(10 downto 0);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_sweep_counter <= "000";
elsif i_lcounter_clk = '1' then
if s_sweep_reload or (s_sweep_counter = "000") then
s_sweep_counter <= s_sweep_period;
else
s_sweep_counter <= s_sweep_counter - "001";
end if;
end if;
end if;
end process;
process (s_current_period, s_sweep_shift)
begin
case s_sweep_shift is
when "001" =>
s_shift_res <= '0' & s_current_period(10 downto 1);
when "010" =>
s_shift_res <= "00" & s_current_period(10 downto 2);
when "011" =>
s_shift_res <= "000" & s_current_period(10 downto 3);
when "100" =>
s_shift_res <= "0000" & s_current_period(10 downto 4);
when "101" =>
s_shift_res <= "00000" & s_current_period(10 downto 5);
when "110" =>
s_shift_res <= "000000" & s_current_period(10 downto 6);
when "111" =>
s_shift_res <= "0000000" & s_current_period(10 downto 7);
when others =>
s_shift_res <= s_current_period;
end case;
end process;
s_write_ch <= (i_cs_n = '0') and (i_write_enable = '1');
s_write_r0 <= s_write_ch and (i_addr = "00");
s_write_r1 <= s_write_ch and (i_addr = "01");
s_write_r2 <= s_write_ch and (i_addr = "10");
s_write_r3 <= s_write_ch and (i_addr = "11");
s_timer_done <= s_freq_counter = 11x"0000";
s_target_period <= ('0' & s_current_period) + ('0' & s_shift_res) when s_sweep_negate = '0'
else ('0' & s_current_period) + ('1' & not(s_shift_res)) + (10x"0000" & INCREMENT);
s_target_in_range <= (s_current_period(10 downto 3) /= "00000000") and ((s_target_period(11) = '0') or (s_sweep_negate = '1'));
s_duty_index <= to_integer(unsigned(s_duty & s_duty_counter));
o_q <= s_envelope_q when (DUTY_TABLE(s_duty_index) = '1') and s_target_in_range and (s_length_active = '1') and (i_enable = '1') else "0000";
o_active <= s_length_active;
end behavioral;
/********************************************************/
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity triangle_channel is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic;
i_reset_n : in std_logic := '1';
i_envelope_clk : in std_logic := '1';
i_lcounter_clk : in std_logic := '1';
i_addr : in std_logic_vector(1 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_write_enable : in std_logic := '0';
i_cs_n : in std_logic := '1';
i_enable : in std_logic := '0';
i_reload : in std_logic := '0';
o_active : out std_logic;
o_q : out std_logic_vector(3 downto 0)
);
end triangle_channel;
architecture behavioral of triangle_channel is
component length_counter is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n : in std_logic := '1';
i_lcounter_clk : in std_logic := '1';
i_channel_enable : in std_logic := '1';
i_channel_reload : in std_logic := '0';
i_enable : in std_logic := '1';
i_addr : in std_logic_vector(1 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_write_enable : in std_logic := '0';
i_cs_n : in std_logic := '1';
o_active : out std_logic
);
end component;
signal s_linear_control : std_logic := '1';
signal s_linear_load : std_logic_vector(6 downto 0) := 7x"00";
signal s_linear_counter : std_logic_vector(6 downto 0) := 7x"00";
signal s_linear_reload : boolean := false;
signal s_linear_active : boolean;
signal s_length_active : std_logic;
signal s_timer_value : std_logic_vector(10 downto 0) := 11x"0000";
signal s_timer_counter : std_logic_vector(10 downto 0) := 11x"0000";
signal s_length_counter : std_logic_vector(7 downto 0) := x"00";
signal s_sequencer : std_logic_vector(4 downto 0) := "00000";
signal s_sequencer_res : std_logic_vector(4 downto 0);
signal s_timer_enable : boolean;
signal s_timer_done : boolean;
signal s_write_ch : boolean;
signal s_write_r0 : boolean;
signal s_write_r2 : boolean;
signal s_write_r3 : boolean;
begin
lc : length_counter port map
(
i_clk => i_clk,
i_clk_enable => i_clk_enable,
i_reset_n => i_reset_n,
i_lcounter_clk => i_lcounter_clk,
i_channel_enable => i_enable,
i_channel_reload => i_reload,
i_enable => not s_linear_control,
i_addr => i_addr,
i_data => i_data,
i_write_enable => i_write_enable,
i_cs_n => i_cs_n,
o_active => s_length_active
);
-- Register
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_linear_control <= '1';
s_linear_load <= 7x"00";
elsif i_clk_enable = '1' then
if s_write_r0 then
s_linear_control <= i_data(7);
s_linear_load <= i_data(6 downto 0);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_timer_value <= 11x"0000";
elsif i_clk_enable = '1' then
if s_write_r2 then
s_timer_value(7 downto 0) <= i_data;
elsif s_write_r3 then
s_timer_value(10 downto 8) <= i_data(2 downto 0);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_linear_reload <= false;
elsif i_clk_enable = '1' then
if s_write_r3 then
s_linear_reload <= true;
elsif (i_envelope_clk = '1') and (s_linear_control = '0') then
s_linear_reload <= false;
end if;
end if;
end if;
end process;
-- Linear Counter
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_linear_counter <= 7x"00";
elsif i_envelope_clk = '1' then
if s_linear_reload then
s_linear_counter <= s_linear_load;
elsif s_linear_active then
s_linear_counter <= s_linear_counter - 7x"01";
end if;
end if;
end if;
end process;
-- Timer
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_timer_counter <= 11x"0000";
elsif i_clk_enable = '1' then
if s_timer_enable then
if s_timer_done then
s_timer_counter <= s_timer_value;
else
s_timer_counter <= s_timer_counter - 11x"0001";
end if;
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_sequencer <= "00000";
elsif i_clk_enable = '1' then
if s_timer_done and s_timer_enable then
s_sequencer <= s_sequencer + "00001";
end if;
end if;
end if;
end process;
s_linear_active <= (s_linear_counter /= 7x"00");
s_timer_enable <= (s_length_active = '1') and s_linear_active;
s_timer_done <= s_timer_counter = 11x"0000";
s_sequencer_res <= s_sequencer xor 5x"1F";
s_write_ch <= (i_cs_n = '0') and (i_write_enable = '1');
s_write_r0 <= s_write_ch and (i_addr = "00");
s_write_r2 <= s_write_ch and (i_addr = "10");
s_write_r3 <= s_write_ch and (i_addr = "11");
o_active <= s_length_active;
o_q <= s_sequencer(3 downto 0) when s_sequencer(4) = '1' else s_sequencer_res(3 downto 0);
end behavioral;
/********************************************************/
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
use work.common.all;
entity noise_channel is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic;
i_reset_n : in std_logic := '1';
i_apu_clk : in std_logic := '1';
i_envelope_clk : in std_logic := '1';
i_lcounter_clk : in std_logic := '1';
i_addr : in std_logic_vector(1 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_write_enable : in std_logic := '0';
i_cs_n : in std_logic := '1';
i_enable : in std_logic := '0';
i_reload : in std_logic := '0';
i_video_mode : in video_mode_t := ntsc;
o_active : out std_logic;
o_q : out std_logic_vector(3 downto 0)
);
end noise_channel;
architecture behavioral of noise_channel is
component envelope is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n : in std_logic := '1';
i_reload : in boolean := false;
i_loop : in std_logic := '0';
i_disable : in std_logic := '1';
i_volume : in std_logic_vector(3 downto 0) := "0000";
o_q : out std_logic_vector(3 downto 0)
);
end component;
component length_counter is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n : in std_logic := '1';
i_lcounter_clk : in std_logic := '1';
i_channel_enable : in std_logic := '1';
i_channel_reload : in std_logic := '0';
i_enable : in std_logic := '1';
i_addr : in std_logic_vector(1 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_write_enable : in std_logic := '0';
i_cs_n : in std_logic := '1';
o_active : out std_logic
);
end component;
/*
NTSC 4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068
PAL 4, 8, 14, 30, 60, 88, 118, 148, 188, 236, 354, 472, 708, 944, 1890, 3778
*/
type period_table_t is array (0 to 15) of std_logic_vector(11 downto 0);
constant PERIOD_TABLE_NTSC : period_table_t := ( 12x"0004", 12x"0008", 12x"0010", 12x"0020", 12x"0040", 12x"0060", 12x"0080", 12x"00A0",
12x"00CA", 12x"00FE", 12x"017C", 12x"01FC", 12x"02FA", 12x"03F8", 12x"07F2", 12x"0FE4" );
constant PERIOD_TABLE_PAL : period_table_t := ( 12x"0004", 12x"0008", 12x"000E", 12x"001E", 12x"003C", 12x"0058", 12x"0076", 12x"0094",
12x"00BC", 12x"00EC", 12x"0162", 12x"01D8", 12x"02C4", 12x"03B0", 12x"0762", 12x"0EC2" );
signal s_envelope_reload : boolean := false;
signal s_envelope_loop : std_logic := '0';
signal s_envelope_disable : std_logic := '1';
signal s_envelope_volume : std_logic_vector(3 downto 0) := "0000";
signal s_envelope_q : std_logic_vector(3 downto 0);
signal s_length_active : std_logic;
signal s_shift_mode : std_logic := '0';
signal s_shift_bit : std_logic;
signal s_shift_new : std_logic;
signal s_noise_shift : std_logic_vector(14 downto 0) := 15x"01";
signal s_timer_value : std_logic_vector(11 downto 0) := 12x"0000";
signal s_timer_counter : std_logic_vector(11 downto 0) := 12x"0000";
signal s_timer_done : boolean;
signal s_write_ch : boolean;
signal s_write_r0 : boolean;
signal s_write_r2 : boolean;
signal s_write_r3 : boolean;
signal s_period_table : period_table_t;
begin
ev : envelope port map
(
i_clk => i_clk,
i_clk_enable => i_envelope_clk,
i_reset_n => i_reset_n,
i_reload => s_envelope_reload,
i_loop => s_envelope_loop,
i_disable => s_envelope_disable,
i_volume => s_envelope_volume,
o_q => s_envelope_q
);
lc : length_counter port map
(
i_clk => i_clk,
i_clk_enable => i_clk_enable,
i_reset_n => i_reset_n,
i_lcounter_clk => i_lcounter_clk,
i_channel_enable => i_enable,
i_channel_reload => i_reload,
i_enable => not s_envelope_loop,
i_addr => i_addr,
i_data => i_data,
i_write_enable => i_write_enable,
i_cs_n => i_cs_n,
o_active => s_length_active
);
-- Register
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_envelope_volume <= "0000";
s_envelope_disable <= '1';
s_envelope_loop <= '0';
elsif i_clk_enable = '1' then
if s_write_r0 then
s_envelope_volume <= i_data(3 downto 0);
s_envelope_disable <= i_data(4);
s_envelope_loop <= i_data(5);
end if;
end if;
end if;
end process;
process (i_clk)
variable period_index : integer range 0 to 15;
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_shift_mode <= '0';
s_timer_value <= 12x"00";
elsif i_clk_enable = '1' then
if s_write_r2 then
s_shift_mode <= i_data(7);
period_index := to_integer(unsigned(i_data(3 downto 0)));
s_timer_value <= s_period_table(period_index);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_envelope_reload <= false;
elsif i_clk_enable = '1' then
if s_write_r3 then
s_envelope_reload <= true;
elsif i_envelope_clk = '1' then
s_envelope_reload <= false;
end if;
end if;
end if;
end process;
-- Timer
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_timer_counter <= 12x"0000";
elsif i_apu_clk = '1' then
if s_timer_done then
s_timer_counter <= s_timer_value;
else
s_timer_counter <= s_timer_counter - 12x"0001";
end if;
end if;
end if;
end process;
-- Shift
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_noise_shift <= 15x"01";
elsif (i_apu_clk = '1') and s_timer_done then
s_noise_shift <= s_shift_new & s_noise_shift(14 downto 1);
end if;
end if;
end process;
s_shift_bit <= s_noise_shift(6) when s_shift_mode = '1' else s_noise_shift(1);
s_shift_new <= s_shift_bit xor s_noise_shift(0);
s_timer_done <= s_timer_counter = 12x"0000";
s_write_ch <= (i_cs_n = '0') and (i_write_enable = '1');
s_write_r0 <= s_write_ch and (i_addr = "00");
s_write_r2 <= s_write_ch and (i_addr = "10");
s_write_r3 <= s_write_ch and (i_addr = "11");
s_period_table <= PERIOD_TABLE_PAL when i_video_mode = pal else PERIOD_TABLE_NTSC;
o_active <= s_length_active;
o_q <= s_envelope_q when (s_noise_shift(0) = '0') and (s_length_active = '1') else "0000";
end behavioral;
/********************************************************/
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
use work.common.all;
entity dmc_channel is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic;
i_reset_n : in std_logic := '1';
i_addr : in std_logic_vector(1 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_dma_busy : in std_logic := '0';
i_dma_data : in std_logic_vector(7 downto 0) := x"00";
i_write_enable : in std_logic := '0';
i_cs_n : in std_logic := '1';
i_enable : in std_logic := '0';
i_reload : in std_logic := '0';
i_video_mode : in video_mode_t := ntsc;
o_dma_request : out std_logic;
o_dma_addr : out std_logic_vector(15 downto 0);
o_active : out std_logic;
o_int_pending : out std_logic;
o_q : out std_logic_vector(6 downto 0)
);
end dmc_channel;
architecture behavioral of dmc_channel is
type period_t is array (0 to 15) of std_logic_vector(8 downto 0);
constant PERIOD_TABLE_NTSC : period_t := ( 9x"1AB", 9x"17B", 9x"153", 9x"13F", 9x"11D", 9x"0FD", 9x"0E1", 9x"0D5",
9x"0BD", 9x"09F", 9x"08D", 9x"07F", 9x"069", 9x"053", 9x"047", 9x"035" );
constant PERIOD_TABLE_PAL : period_t := ( 9x"18E", 9x"162", 9x"13C", 9x"12A", 9x"114", 9x"0EC", 9x"0D2", 9x"0C6",
9x"0B0", 9x"094", 9x"084", 9x"076", 9x"062", 9x"04E", 9x"042", 9x"032" );
signal s_int_enable : std_logic := '0';
signal s_loop : std_logic := '0';
signal s_timer_counter : std_logic_vector(8 downto 0) := 9x"000";
signal s_timer_value : std_logic_vector(8 downto 0) := 9x"000";
signal s_length_load : unsigned(11 downto 0) := 12x"000";
signal s_addr_load : std_logic_vector(15 downto 0) := x"0000";
signal s_output : std_logic_vector(6 downto 0) := 7x"00";
signal s_next_output : std_logic_vector(7 downto 0);
signal s_write_ch : boolean;
signal s_write_r0 : boolean;
signal s_write_r1 : boolean;
signal s_write_r2 : boolean;
signal s_write_r3 : boolean;
signal s_timer_done : boolean;
signal s_silent : boolean := true;
signal s_bits_remaining : unsigned(2 downto 0) := "000";
signal s_shift_buffer : std_logic_vector(7 downto 0) := x"00";
signal s_sample_buffer_empty : boolean := true;
signal s_bits_empty : boolean;
signal s_dma_request : std_logic := '0';
signal s_dma_addr : std_logic_vector(15 downto 0) := x"0000";
signal s_next_addr : std_logic_vector(15 downto 0) := x"0000";
signal s_length : unsigned(11 downto 0) := 12x"000";
signal s_dma_busy_d : std_logic := '0';
signal s_int_pending : std_logic := '0';
signal s_int_trigger : boolean := false;
signal s_dma_free : boolean;
signal s_dma_done : boolean;
signal s_period_table : period_t;
begin
-- DMC
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_loop <= '0';
elsif i_clk_enable = '1' then
if s_write_r0 then
s_loop <= i_data(6);
end if;
end if;
end if;
end process;
process (i_clk)
variable period_index : integer range 0 to 15;
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_timer_value <= 9x"000";
elsif i_clk_enable = '1' then
if s_write_r0 then
period_index := to_integer(unsigned(i_data(3 downto 0)));
s_timer_value <= s_period_table(period_index);
end if;
end if;
end if;
end process;
-- Output
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_output <= 7x"00";
elsif i_clk_enable = '1' then
if s_write_r1 then
s_output <= i_data(6 downto 0);
elsif s_timer_done and not s_silent then
if s_next_output(7) = '0' then
s_output <= s_next_output(6 downto 0);
end if;
end if;
end if;
end if;
end process;
-- Addr
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_addr_load <= x"0000";
elsif i_clk_enable = '1' then
if s_write_r2 then
s_addr_load <= x"C000" or ("00" & i_data & "000000");
end if;
end if;
end if;
end process;
-- Length Counter
process (i_clk)