-
Notifications
You must be signed in to change notification settings - Fork 4
/
nes.vhd
1150 lines (1099 loc) · 32.2 KB
/
nes.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;
use work.common.all;
entity nes is
port
(
CLOCK_125_p : in std_logic;
CLOCK_50_B5B : in std_logic;
CLOCK_50_B6A : in std_logic;
CLOCK_50_B7A : in std_logic;
CLOCK_50_B8A : in std_logic;
CPU_RESET_n : in std_logic;
KEY : in std_logic_vector(3 downto 0);
SW: in std_logic_vector(9 downto 0);
I2C_SCL : inout std_logic;
I2C_SDA : inout std_logic;
GPIO : inout std_logic_vector(21 downto 0);
LEDG : out std_logic_vector(7 downto 0);
LEDR : out std_logic_vector(9 downto 0);
HEX0 : out std_logic_vector(6 downto 0);
HEX1 : out std_logic_vector(6 downto 0);
HEX2 : out std_logic_vector(6 downto 0);
HEX3 : out std_logic_vector(6 downto 0);
UART_RX: in std_logic;
UART_TX: out std_logic;
CPU_ADDR : out std_logic_vector(14 downto 0);
CPU_DATA : inout std_logic_vector(7 downto 0);
CPU_DIR : out std_logic;
CPU_RW : out std_logic;
PPU_ADDR : out std_logic_vector(13 downto 0);
PPU_A13_N : out std_logic;
PPU_DATA : inout std_logic_vector(7 downto 0);
PPU_DIR : out std_logic;
PPU_RD_N : out std_logic;
PPU_WR_N : out std_logic;
ROMSEL_N : out std_logic;
CIRAM_CE_N : in std_logic;
CIRAM_A10 : in std_logic;
M2 : out std_logic;
SYS_CLK : out std_logic;
IRQ_N : in std_logic;
CIC_CLK : out std_logic;
CIC_TOPAK : out std_logic;
CIC_RST_N : out std_logic;
CIC_TOMB : in std_logic;
HDMI_TX_CLK : out std_logic;
HDMI_TX_D : out std_logic_vector(23 downto 0);
HDMI_TX_DE : out std_logic;
HDMI_TX_HS : out std_logic;
HDMI_TX_INT : in std_logic;
HDMI_TX_VS : out std_logic;
AUD_ADCDAT : in std_logic;
AUD_ADCLRCK : inout std_logic;
AUD_BCLK : inout std_logic;
AUD_DACDAT : out std_logic;
AUD_DACLRCK : inout std_logic;
AUD_XCK : out std_logic;
SD_CLK : out std_logic;
SD_CMD : inout std_logic;
SD_DAT : inout std_logic_vector(3 downto 0)
);
end nes;
architecture behavioral of nes is
component master_pll is
port
(
refclk : in std_logic := '0';
rst : in std_logic := '0';
outclk_0 : out std_logic;
locked : out std_logic;
reconfig_to_pll : in std_logic_vector(63 downto 0) := (others => '0');
reconfig_from_pll : out std_logic_vector(63 downto 0)
);
end component;
component audio_pll is
port
(
refclk : in std_logic := '0';
rst : in std_logic := '0';
outclk_0 : out std_logic;
locked : out std_logic
);
end component;
component vga_pll is
port
(
refclk : in std_logic := '0';
rst : in std_logic := '0';
outclk_0 : out std_logic;
locked : out std_logic
);
end component;
component nescore is
port
(
i_clk : in std_logic;
i_reset_n : in std_logic := '1';
i_ready : in std_logic := '1';
i_ctrl_a_data : in std_logic := '1';
i_ctrl_b_data : in std_logic := '1';
i_video_mode : in video_mode_t := ntsc;
i_ciram_ce_n : in std_logic;
i_ciram_a10 : in std_logic;
i_prg_q : in std_logic_vector(7 downto 0);
i_chr_q : in std_logic_vector(7 downto 0);
i_irq_n : in std_logic := '1';
i_rx : in std_logic := '1';
o_tx : out std_logic;
o_prg_addr : out std_logic_vector(14 downto 0);
o_prg_data : out std_logic_vector(7 downto 0);
o_prg_write_enable : out std_logic;
o_prg_cs_n : out std_logic;
o_chr_addr : out std_logic_vector(13 downto 0);
o_chr_data : out std_logic_vector(7 downto 0);
o_chr_read_enable : out std_logic;
o_chr_write_enable : out std_logic;
o_ctrl_strobe : out std_logic;
o_ctrl_a_clk : out std_logic;
o_ctrl_b_clk : out std_logic;
o_cpu_clk_enable : out std_logic;
o_cpu_sync : out std_logic;
o_vga_addr : out std_logic_vector(15 downto 0);
o_vga_data : out std_logic_vector(8 downto 0);
o_vga_write_enable : out std_logic;
o_vga_clk_enable : out std_logic;
o_audio_q : out std_logic_vector(15 downto 0)
);
end component;
component vga is
generic
(
HFP : natural := 88;
HSYNC : natural := 44;
HBP : natural := 148;
HRES : natural := 1920;
VFP : natural := 4;
VSYNC : natural := 5;
VBP : natural := 36;
VRES : natural := 1080
);
port
(
i_data_clk : in std_logic;
i_data_clk_enable : in std_logic;
i_vga_clk : in std_logic;
i_vga_clk_enable : in std_logic;
i_reset_n : in std_logic;
i_addr : in std_logic_vector(15 downto 0);
i_data : in std_logic_vector(8 downto 0);
i_write_enable : in std_logic;
o_data_enable : out std_logic;
o_vsync : out std_logic;
o_hsync : out std_logic;
o_data : out std_logic_vector(23 downto 0)
);
end component;
component i2s is
generic
(
DIVIDER : natural := 4;
WORD_WIDTH : natural := 16;
CHANNEL_WIDTH : natural := 32
);
port
(
i_audio_clk : in std_logic;
i_master_clk : in std_logic;
i_clk_enable : in std_logic;
i_audio_reset_n : in std_logic := '1';
i_master_reset_n : in std_logic := '1';
i_data : in std_logic_vector(WORD_WIDTH - 1 downto 0);
o_lrclk : out std_logic;
o_sclk : out std_logic;
o_sdata : out std_logic
);
end component;
component hex_digit is
port
(
i_d : in std_logic_vector(3 downto 0);
o_q : out std_logic_vector(6 downto 0)
);
end component;
component periphery_ctrl is
generic
(
CLK_SPEED : integer := 50_000_000
);
port
(
i_clk : in std_logic;
i_reset_n : in std_logic := '1';
i_int_n : in std_logic := '1';
io_sda : inout std_logic;
io_scl : inout std_logic;
o_status : out std_logic_vector(7 downto 0);
o_ack_error : out std_logic
);
end component;
component master_reconfig is
generic (
ENABLE_BYTEENABLE : boolean := false;
BYTEENABLE_WIDTH : integer := 4;
RECONFIG_ADDR_WIDTH : integer := 6;
RECONFIG_DATA_WIDTH : integer := 32;
reconf_width : integer := 64
);
port (
mgmt_clk : in std_logic := '0';
mgmt_reset : in std_logic := '0';
mgmt_waitrequest : out std_logic;
mgmt_read : in std_logic := '0';
mgmt_write : in std_logic := '0';
mgmt_readdata : out std_logic_vector(31 downto 0);
mgmt_address : in std_logic_vector(5 downto 0) := (others => '0');
mgmt_writedata : in std_logic_vector(31 downto 0) := (others => '0');
reconfig_to_pll : out std_logic_vector(63 downto 0);
reconfig_from_pll : in std_logic_vector(63 downto 0) := (others => '0')
);
end component;
component master_ctrl is
port
(
i_clk : in std_logic;
i_reset_n : in std_logic := '1';
i_video_mode : in video_mode_t;
i_waitrequest : in std_logic := '0';
i_reconfig_data : in std_logic_vector(31 downto 0) := (others => '0');
o_video_mode : out video_mode_t;
o_status : out std_logic_vector(7 downto 0);
o_reconfig_read : out std_logic;
o_reconfig_write : out std_logic;
o_reconfig_addr : out std_logic_vector(5 downto 0);
o_reconfig_new_data : out std_logic_vector(31 downto 0)
);
end component;
component debounce is
port
(
i_clk : in std_logic;
i_in : in std_logic;
o_q : out std_logic;
o_riseedge : out std_logic;
o_falledge : out std_logic
);
end component;
alias HDMI_AUDIO_SPDIF : std_logic is GPIO(0);
alias HDMI_AUDIO_MCLK : std_logic is GPIO(1);
alias HDMI_AUDIO_I2S0 : std_logic is GPIO(2);
alias HDMI_AUDIO_I2S1 : std_logic is GPIO(3);
alias HDMI_AUDIO_I2S2 : std_logic is GPIO(4);
alias HDMI_AUDIO_I2S3 : std_logic is GPIO(5);
alias HDMI_AUDIO_SCLK : std_logic is GPIO(6);
alias HDMI_AUDIO_LRCLK : std_logic is GPIO(7);
alias CTRL_A_DATA : std_logic is GPIO(8);
alias CTRL_B_DATA : std_logic is GPIO(9);
alias CTRL_A_CLOCK : std_logic is GPIO(10);
alias CTRL_B_CLOCK : std_logic is GPIO(11);
alias CTRL_STROBE : std_logic is GPIO(12);
signal s_audio_clk : std_logic;
signal s_master_clk : std_logic;
signal s_vga_clk : std_logic;
signal s_reset_n : std_logic;
signal s_audio_reset_n : std_logic;
signal s_vga_reset_n : std_logic;
signal s_vga_addr : std_logic_vector(15 downto 0);
signal s_vga_data : std_logic_vector(8 downto 0);
signal s_vga_write_enable : std_logic;
signal s_vga_clk_enable : std_logic;
signal s_audio_q : std_logic_vector(15 downto 0);
signal s_debug1 : std_logic_vector(7 downto 0) := (others => '0');
signal s_debug2 : std_logic_vector(7 downto 0) := (others => '0');
signal s_audio_counter : integer range 0 to 62499 := 0;
signal s_ack_error : std_logic;
signal s_audio_lrclk : std_logic;
signal s_audio_sclk : std_logic;
signal s_audio_dat : std_logic;
signal s_sample_req : std_logic_vector(1 downto 0);
signal s_cpu_clk_enable : std_logic;
signal s_cpu_sync : std_logic;
signal s_prg_cs_n : std_logic;
signal s_prg_addr : std_logic_vector(14 downto 0);
signal s_chr_addr : std_logic_vector(13 downto 0);
signal s_prg_data : std_logic_vector(7 downto 0);
signal s_chr_data : std_logic_vector(7 downto 0);
signal s_prg_write_enable : std_logic;
signal s_chr_read_enable : std_logic;
signal s_chr_write_enable : std_logic;
signal s_reconfig_to_pll : std_logic_vector(63 downto 0);
signal s_reconfig_from_pll : std_logic_vector(63 downto 0);
signal s_reconfig_read : std_logic := '0';
signal s_reconfig_write : std_logic := '0';
signal s_reconfig_addr : std_logic_vector(5 downto 0) := (others => '0');
signal s_reconfig_data : std_logic_vector(31 downto 0);
signal s_reconfig_new_data : std_logic_vector(31 downto 0) := (others => '0');
signal s_reconfig_waitrequest : std_logic;
signal s_video_mode : video_mode_t;
signal s_video_mode_configured : video_mode_t;
signal s_key_d : std_logic_vector(3 downto 0) := (others => '1');
signal s_debounced_vm_switch : std_logic;
begin
master : master_pll port map
(
refclk => CLOCK_50_B5B,
rst => not CPU_RESET_n,
outclk_0 => s_master_clk,
locked => s_reset_n,
reconfig_to_pll => s_reconfig_to_pll,
reconfig_from_pll => s_reconfig_from_pll
);
audio : audio_pll port map
(
refclk => CLOCK_50_B6A,
rst => not CPU_RESET_n,
outclk_0 => s_audio_clk,
locked => s_audio_reset_n
);
vgapll : vga_pll port map
(
refclk => CLOCK_50_B7A,
rst => not CPU_RESET_n,
outclk_0 => s_vga_clk,
locked => s_vga_reset_n
);
master_recfg : master_reconfig port map
(
mgmt_clk => CLOCK_50_B5B,
-- mgmt_reset => not CPU_RESET_n,
mgmt_read => s_reconfig_read,
mgmt_write => s_reconfig_write,
mgmt_address => s_reconfig_addr,
mgmt_readdata => s_reconfig_data,
mgmt_writedata => s_reconfig_new_data,
mgmt_waitrequest => s_reconfig_waitrequest,
reconfig_to_pll => s_reconfig_to_pll,
reconfig_from_pll => s_reconfig_from_pll
);
master_c : master_ctrl port map (
i_clk => CLOCK_50_B5B,
i_reset_n => CPU_RESET_n,
i_video_mode => s_video_mode,
i_waitrequest => s_reconfig_waitrequest,
i_reconfig_data => s_reconfig_data,
o_video_mode => s_video_mode_configured,
o_status => s_debug2,
o_reconfig_read => s_reconfig_read,
o_reconfig_write => s_reconfig_write,
o_reconfig_addr => s_reconfig_addr,
o_reconfig_new_data => s_reconfig_new_data
);
nes_core : nescore port map
(
i_clk => s_master_clk,
i_reset_n => s_reset_n,
i_ready => SW(8),
i_ctrl_a_data => CTRL_A_DATA,
i_ctrl_b_data => CTRL_B_DATA,
i_video_mode => s_video_mode,
i_ciram_ce_n => CIRAM_CE_N,
i_ciram_a10 => CIRAM_A10,
i_prg_q => CPU_DATA,
i_chr_q => PPU_DATA,
i_irq_n => IRQ_N,
i_rx => UART_RX,
o_tx => UART_TX,
o_prg_addr => s_prg_addr,
o_prg_data => s_prg_data,
o_prg_write_enable => s_prg_write_enable,
o_prg_cs_n => s_prg_cs_n,
o_chr_addr => s_chr_addr,
o_chr_data => s_chr_data,
o_chr_read_enable => s_chr_read_enable,
o_chr_write_enable => s_chr_write_enable,
o_ctrl_strobe => CTRL_STROBE,
o_ctrl_a_clk => CTRL_A_CLOCK,
o_ctrl_b_clk => CTRL_B_CLOCK,
o_cpu_clk_enable => s_cpu_clk_enable,
o_cpu_sync => s_cpu_sync,
o_vga_addr => s_vga_addr,
o_vga_data => s_vga_data,
o_vga_write_enable => s_vga_write_enable,
o_vga_clk_enable => s_vga_clk_enable,
o_audio_q => s_audio_q
);
vga_cmp : vga generic map
(
HFP => 16,
HSYNC => 96,
HBP => 48,
HRES => 640,
VFP => 10,
VSYNC => 2,
VBP => 33,
VRES => 480
)
port map
(
i_data_clk => s_master_clk,
i_data_clk_enable => s_vga_clk_enable,
i_vga_clk => s_vga_clk,
i_vga_clk_enable => '1',
i_reset_n => s_vga_reset_n,
i_addr => s_vga_addr,
i_data => s_vga_data,
i_write_enable => s_vga_write_enable,
o_data_enable => HDMI_TX_DE,
o_vsync => HDMI_TX_VS,
o_hsync => HDMI_TX_HS,
o_data => HDMI_TX_D
);
pc : periphery_ctrl port map
(
i_clk => CLOCK_50_B5B,
i_reset_n => CPU_RESET_n,
i_int_n => HDMI_TX_INT,
io_sda => I2C_SDA,
io_scl => I2C_SCL,
o_status => s_debug1,
o_ack_error => s_ack_error
);
i2s_cmp : i2s port map
(
i_audio_clk => s_audio_clk,
i_master_clk => s_master_clk,
i_clk_enable => s_cpu_clk_enable,
i_audio_reset_n => s_audio_reset_n,
i_master_reset_n => CPU_RESET_n,
i_data => s_audio_q,
o_lrclk => s_audio_lrclk,
o_sclk => s_audio_sclk,
o_sdata => s_audio_dat
);
hd0 : hex_digit port map
(
i_d => s_debug1(3 downto 0),
o_q => HEX0
);
hd1 : hex_digit port map
(
i_d => s_debug1(7 downto 4),
o_q => HEX1
);
hd2 : hex_digit port map
(
i_d => s_debug2(3 downto 0),
o_q => HEX2
);
hd3 : hex_digit port map
(
i_d => s_debug2(7 downto 4),
o_q => HEX3
);
deb1 : debounce port map
(
i_clk => CLOCK_50_B5B,
i_in => SW(9),
o_q => s_debounced_vm_switch
);
HDMI_AUDIO_MCLK <= s_audio_clk;
HDMI_AUDIO_LRCLK <= s_audio_lrclk;
HDMI_AUDIO_SCLK <= s_audio_sclk;
HDMI_AUDIO_I2S0 <= s_audio_dat;
HDMI_AUDIO_I2S1 <= '0';
HDMI_AUDIO_I2S2 <= '0';
HDMI_AUDIO_I2S3 <= '0';
HDMI_AUDIO_SPDIF <= '0';
AUD_DACDAT <= s_audio_dat;
AUD_XCK <= s_audio_clk;
AUD_DACLRCK <= s_audio_lrclk;
AUD_BCLK <= s_audio_sclk;
HDMI_TX_CLK <= s_vga_clk;
LEDR(9) <= '1' when s_video_mode_configured = pal else '0';
LEDR(8) <= s_reconfig_waitrequest;
LEDR(7) <= not HDMI_TX_INT;
LEDR(6) <= s_ack_error;
LEDR(5 downto 0) <= (others => '0');
LEDG <= (others => '0');
CTRL_A_DATA <= 'Z';
CTRL_B_DATA <= 'Z';
CIC_CLK <= '0';
CIC_TOPAK <= '0';
CIC_RST_N <= '1';
SYS_CLK <= '0'; -- s_master_clk
M2 <= s_cpu_sync;
ROMSEL_N <= s_prg_cs_n;
CPU_ADDR <= s_prg_addr;
CPU_DATA <= s_prg_data when s_prg_write_enable = '1' else (others => 'Z');
CPU_DIR <= s_prg_write_enable;
CPU_RW <= not s_prg_write_enable;
PPU_ADDR <= s_chr_addr;
PPU_DATA <= s_chr_data when s_chr_write_enable = '1' else (others => 'Z');
PPU_DIR <= s_chr_write_enable;
PPU_RD_N <= not s_chr_read_enable;
PPU_WR_N <= not s_chr_write_enable;
PPU_A13_N <= not s_chr_addr(13);
s_video_mode <= ntsc when s_debounced_vm_switch = '0' else pal;
end;
/********************************************************/
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
use work.common.all;
entity nescore is
port
(
i_clk : in std_logic;
i_reset_n : in std_logic := '1';
i_ready : in std_logic := '1';
i_ctrl_a_data : in std_logic := '1';
i_ctrl_b_data : in std_logic := '1';
i_video_mode : in video_mode_t := ntsc;
i_ciram_ce_n : in std_logic := '1';
i_ciram_a10 : in std_logic := '0';
i_prg_q : in std_logic_vector(7 downto 0);
i_chr_q : in std_logic_vector(7 downto 0);
i_irq_n : in std_logic := '1';
i_rx : in std_logic := '1';
o_tx : out std_logic;
o_prg_addr : out std_logic_vector(14 downto 0);
o_prg_data : out std_logic_vector(7 downto 0);
o_prg_write_enable : out std_logic;
o_prg_cs_n : out std_logic;
o_chr_addr : out std_logic_vector(13 downto 0);
o_chr_data : out std_logic_vector(7 downto 0);
o_chr_read_enable : out std_logic;
o_chr_write_enable : out std_logic;
o_ctrl_strobe : out std_logic;
o_ctrl_a_clk : out std_logic;
o_ctrl_b_clk : out std_logic;
o_cpu_clk_enable : out std_logic;
o_cpu_sync : out std_logic;
o_vga_addr : out std_logic_vector(15 downto 0);
o_vga_data : out std_logic_vector(8 downto 0);
o_vga_write_enable : out std_logic;
o_vga_clk_enable : out std_logic;
o_audio_q : out std_logic_vector(15 downto 0)
);
end nescore;
architecture behavioral of nescore is
component cpu is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_ready : in std_logic := '1';
i_reset_n : in std_logic := '1';
i_int_n : in std_logic := '1';
i_nmi_n : in std_logic := '1';
i_mem_q : in std_logic_vector(7 downto 0) := x"00";
o_mem_addr : out std_logic_vector(15 downto 0);
o_mem_data : out std_logic_vector(7 downto 0);
o_mem_write_enable : out std_logic
);
end component;
component ppu is
port
(
i_clk: in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n: in std_logic := '1';
i_video_mode : in video_mode_t := ntsc;
i_addr : in std_logic_vector(2 downto 0) := "000";
i_data : in std_logic_vector(7 downto 0) := x"00";
i_write_enable : in std_logic := '0';
i_cs_n : in std_logic := '0';
i_chr_data : in std_logic_vector(7 downto 0) := x"00";
o_q : out std_logic_vector(7 downto 0);
o_int_n : out std_logic;
o_vga_addr: out std_logic_vector(15 downto 0);
o_vga_data: out std_logic_vector(8 downto 0);
o_vga_write_enable: out std_logic;
o_chr_addr : out std_logic_vector(13 downto 0);
o_chr_read_enable : out std_logic;
o_chr_write_enable : out std_logic;
o_chr_q : out std_logic_vector(7 downto 0)
);
end component;
component apu 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(4 downto 0) := 5x"00";
i_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_dma_write_enable : in std_logic := '0';
i_dma_q : in std_logic_vector(7 downto 0) := x"00";
i_ctrl_a_data : in std_logic := '1';
i_ctrl_b_data : in std_logic := '1';
i_video_mode : in video_mode_t := ntsc;
o_ctrl_strobe : out std_logic;
o_ctrl_a_clk : out std_logic;
o_ctrl_b_clk : out std_logic;
o_int_n : out std_logic;
o_audio : out std_logic_vector(15 downto 0);
o_q : out std_logic_vector(7 downto 0);
o_dma_addr : out std_logic_vector(15 downto 0);
o_dma_data : out std_logic_vector(7 downto 0);
o_dma_write_enable : out std_logic;
o_dma_ready : out std_logic;
o_dma_active : out std_logic
);
end component;
component peripherial_io is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n : in std_logic;
i_addr : in std_logic_vector(2 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_write_enable : in std_logic;
i_cs_n : in std_logic := '1';
i_rx : in std_logic := '1';
o_tx : out std_logic;
o_q : out std_logic_vector(7 downto 0)
);
end component;
component data_path is
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_reset_n : in std_logic;
i_sync : in std_logic;
i_addr : in std_logic_vector(15 downto 0);
i_data : in std_logic_vector(7 downto 0);
i_write_enable : in std_logic;
i_ppu_q : in std_logic_vector(7 downto 0);
i_apu_q : in std_logic_vector(7 downto 0);
i_prg_q : in std_logic_vector(7 downto 0);
i_pio_q : in std_logic_vector(7 downto 0);
o_prg_addr : out std_logic_vector(14 downto 0);
o_prg_cs_n : out std_logic;
o_ppu_addr : out std_logic_vector(2 downto 0);
o_ppu_cs_n : out std_logic;
o_apu_addr : out std_logic_vector(4 downto 0);
o_apu_cs_n : out std_logic;
o_pio_addr : out std_logic_vector(2 downto 0);
o_pio_cs_n : out std_logic;
o_q : out std_logic_vector(7 downto 0)
);
end component;
component videomem is
port
(
address : in std_logic_vector(10 downto 0);
clken : in std_logic := '1';
clock : in std_logic := '1';
data : in std_logic_vector(7 downto 0);
rden : in std_logic := '0';
wren : in std_logic := '0';
q : out std_logic_vector(7 downto 0)
);
end component;
signal s_cpu_clk_enable : std_logic;
signal s_cpu_sync : std_logic;
signal s_ppu_q : std_logic_vector(7 downto 0);
signal s_ppu_addr : std_logic_vector(2 downto 0);
signal s_ppu_cs_n : std_logic;
signal s_apu_addr : std_logic_vector(4 downto 0);
signal s_apu_cs_n : std_logic;
signal s_apu_int_n : std_logic;
signal s_apu_q : std_logic_vector(7 downto 0);
signal s_nmi_n : std_logic;
signal s_int_n : std_logic;
signal s_mem_q : std_logic_vector(7 downto 0);
signal s_mem_addr : std_logic_vector(15 downto 0);
signal s_mem_data : std_logic_vector(7 downto 0);
signal s_mem_write_enable : std_logic;
signal s_eff_addr : std_logic_vector(15 downto 0);
signal s_eff_data : std_logic_vector(7 downto 0);
signal s_eff_write_enable : std_logic;
signal s_dma_addr : std_logic_vector(15 downto 0);
signal s_dma_data : std_logic_vector(7 downto 0);
signal s_dma_write_enable : std_logic;
signal s_dma_ready : std_logic;
signal s_dma_active : std_logic;
signal s_chr_addr : std_logic_vector(13 downto 0);
signal s_chr_q : std_logic_vector(7 downto 0);
signal s_chr_data : std_logic_vector(7 downto 0);
signal s_chr_latch : std_logic_vector(7 downto 0) := (others => '0');
signal s_chr_read_enable : std_logic;
signal s_chr_write_enable : std_logic;
signal s_ppu_clk_enable : std_logic;
signal s_ciram_ce_d : std_logic_vector(1 downto 0) := "00";
signal s_ciram_a10_d : std_logic := '0';
signal s_ciram_q : std_logic_vector(7 downto 0);
signal s_chr_eff_q : std_logic_vector(7 downto 0);
signal s_prg_q : std_logic_vector(7 downto 0) := (others => '0');
signal s_prg_latch : std_logic_vector(7 downto 0) := (others => '0');
signal s_cpu_divider : natural range 0 to 31 := 31;
signal s_ppu_divider : natural range 0 to 31 := 31;
signal s_cpu_counter : natural range 0 to 31 := 0;
signal s_ppu_counter : natural range 0 to 31 := 0;
signal s_sync_start : natural range 0 to 31 := 0;
signal s_sync_stop : natural range 0 to 31 := 0;
signal s_next_cpu_divider : natural range 0 to 31;
signal s_next_ppu_divider : natural range 0 to 31;
signal s_next_sync_start : natural range 0 to 31;
signal s_next_sync_stop : natural range 0 to 31;
signal s_last_cpu_cycle : boolean;
signal s_last_ppu_cycle : boolean;
signal s_pio_q : std_logic_vector(7 downto 0);
signal s_pio_addr : std_logic_vector(2 downto 0);
signal s_pio_cs_n : std_logic;
begin
cpu_core : cpu port map
(
i_clk => i_clk,
i_clk_enable => s_cpu_clk_enable,
i_ready => s_dma_ready and i_ready,
i_reset_n => i_reset_n,
i_nmi_n => s_nmi_n,
i_int_n => s_int_n,
i_mem_q => s_mem_q,
o_mem_addr => s_mem_addr,
o_mem_data => s_mem_data,
o_mem_write_enable => s_mem_write_enable
);
ppu_cmp : ppu port map
(
i_clk => i_clk,
i_clk_enable => s_ppu_clk_enable,
i_reset_n => i_reset_n,
i_video_mode => i_video_mode,
i_addr => s_ppu_addr,
i_data => s_eff_data,
i_write_enable => s_eff_write_enable,
i_cs_n => s_ppu_cs_n,
i_chr_data => s_chr_q,
o_q => s_ppu_q,
o_int_n => s_nmi_n,
o_vga_addr => o_vga_addr,
o_vga_data => o_vga_data,
o_vga_write_enable => o_vga_write_enable,
o_chr_addr => s_chr_addr,
o_chr_q => s_chr_data,
o_chr_read_enable => s_chr_read_enable,
o_chr_write_enable => s_chr_write_enable
);
apu_core : apu port map
(
i_clk => i_clk,
i_clk_enable => s_cpu_clk_enable,
i_reset_n => i_reset_n,
i_addr => s_apu_addr,
i_data => s_eff_data,
i_write_enable => s_eff_write_enable,
i_cs_n => s_apu_cs_n,
i_ctrl_a_data => i_ctrl_a_data,
i_ctrl_b_data => i_ctrl_b_data,
i_dma_write_enable => s_mem_write_enable,
i_dma_q => s_mem_q,
i_video_mode => i_video_mode,
o_ctrl_strobe => o_ctrl_strobe,
o_ctrl_a_clk => o_ctrl_a_clk,
o_ctrl_b_clk => o_ctrl_b_clk,
o_int_n => s_apu_int_n,
o_audio => o_audio_q,
o_q => s_apu_q,
o_dma_addr => s_dma_addr,
o_dma_data => s_dma_data,
o_dma_write_enable => s_dma_write_enable,
o_dma_ready => s_dma_ready,
o_dma_active => s_dma_active
);
/*
pio_core : peripherial_io port map
(
i_clk => i_clk,
i_clk_enable => s_cpu_clk_enable,
i_reset_n => i_reset_n,
i_addr => s_pio_addr,
i_data => s_eff_data,
i_write_enable => s_eff_write_enable,
i_cs_n => s_pio_cs_n,
i_rx => i_rx,
o_tx => o_tx,
o_q => s_pio_q
);
*/
dpath : data_path port map
(
i_clk => i_clk,
i_clk_enable => s_cpu_clk_enable,
i_sync => s_cpu_sync,
i_reset_n => i_reset_n,
i_addr => s_eff_addr,
i_data => s_eff_data,
i_write_enable => s_eff_write_enable,
i_ppu_q => s_ppu_q,
i_apu_q => s_apu_q,
i_prg_q => s_prg_q,
i_pio_q => s_pio_q,
o_prg_addr => o_prg_addr,
o_prg_cs_n => o_prg_cs_n,
o_ppu_addr => s_ppu_addr,
o_ppu_cs_n => s_ppu_cs_n,
o_apu_addr => s_apu_addr,
o_apu_cs_n => s_apu_cs_n,
o_pio_addr => s_pio_addr,
o_pio_cs_n => s_pio_cs_n,
o_q => s_mem_q
);
ciram : videomem port map
(
clock => i_clk,
address => s_ciram_a10_d & s_chr_addr(9 downto 0),
clken => s_ppu_clk_enable and s_ciram_ce_d(0),
data => s_chr_data,
rden => s_chr_read_enable,
wren => s_chr_write_enable,
q => s_ciram_q
);
process (i_video_mode)
begin
case i_video_mode is
when ntsc =>
s_next_cpu_divider <= 12;
s_next_ppu_divider <= 4;
s_next_sync_start <= 1;
s_next_sync_stop <= 10;
when pal =>
s_next_cpu_divider <= 16;
s_next_ppu_divider <= 5;
s_next_sync_start <= 2;
s_next_sync_stop <= 14;
end case;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if (i_reset_n = '0') or (s_last_cpu_cycle and s_last_ppu_cycle) then
s_cpu_divider <= s_next_cpu_divider;
s_ppu_divider <= s_next_ppu_divider;
s_sync_start <= s_next_sync_start;
s_sync_stop <= s_next_sync_stop;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if s_last_cpu_cycle then
s_cpu_counter <= 0;
else
s_cpu_counter <= s_cpu_counter + 1;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if s_last_ppu_cycle then
s_ppu_counter <= 0;
else
s_ppu_counter <= s_ppu_counter + 1;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if s_cpu_counter = s_sync_stop then
s_cpu_sync <= '0';
elsif s_cpu_counter = s_sync_start then
s_cpu_sync <= '1';
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_prg_q <= (others => '0');
elsif s_last_cpu_cycle then
s_prg_q <= i_prg_q;
else
s_prg_q <= s_prg_latch;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_prg_latch <= (others => '0');
elsif s_cpu_counter = s_sync_stop then
s_prg_latch <= i_prg_q;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_ciram_a10_d <= '0';
elsif s_ppu_clk_enable = '1' then
s_ciram_a10_d <= i_ciram_a10;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_ciram_ce_d <= (others => '0');
elsif s_ppu_clk_enable = '1' then
s_ciram_ce_d <= s_ciram_ce_d(0) & not i_ciram_ce_n;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_reset_n = '0' then
s_chr_latch <= (others => '0');
elsif s_ppu_clk_enable = '1' then
s_chr_latch <= i_chr_q;
end if;
end if;
end process;
s_last_cpu_cycle <= s_cpu_counter = s_cpu_divider - 1;
s_last_ppu_cycle <= s_ppu_counter = s_ppu_divider - 1;
s_cpu_clk_enable <= '1' when s_last_cpu_cycle else '0';
s_ppu_clk_enable <= '1' when s_last_ppu_cycle else '0';
s_eff_write_enable <= s_dma_write_enable when s_dma_active = '1' else s_mem_write_enable;
s_eff_addr <= s_dma_addr when s_dma_active = '1' else s_mem_addr;
s_eff_data <= s_dma_data when s_dma_active = '1' else s_mem_data;
s_int_n <= s_apu_int_n and i_irq_n;
s_chr_q <= s_ciram_q when s_ciram_ce_d(1) = '1' else s_chr_latch;
o_cpu_clk_enable <= s_cpu_clk_enable;
o_cpu_sync <= s_cpu_sync;
o_vga_clk_enable <= s_ppu_clk_enable;
o_chr_addr <= s_chr_addr;
o_chr_data <= s_chr_data;
o_chr_read_enable <= s_chr_read_enable;
o_chr_write_enable <= s_chr_write_enable;
o_prg_data <= s_eff_data;
o_prg_write_enable <= s_eff_write_enable;