-
Notifications
You must be signed in to change notification settings - Fork 4
/
ppu.vhd
1434 lines (1243 loc) · 42.7 KB
/
ppu.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_arith.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
use work.common.all;
entity parallel_serial_shifter is
generic
(
width: integer := 8;
size: integer := 8
);
port
(
i_clk: in std_logic;
i_clk_enable : in std_logic := '1';
i_load: in std_logic;
i_enable: in std_logic;
i_data: in std_logic_vector(size - 1 downto 0);
o_q: out std_logic_vector(size - 1 downto 0)
);
end parallel_serial_shifter;
architecture behavioral of parallel_serial_shifter is
signal s_buffer: std_logic_vector(width - 1 downto 0);
begin
process (i_clk, i_clk_enable)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_enable = '1' and i_load = '1' then
s_buffer <= s_buffer(width - 2 downto size) & i_data & '0';
elsif i_enable = '1' then
s_buffer <= s_buffer(width - 2 downto 0) & '0';
elsif i_load = '1' then
s_buffer(size - 1 downto 0) <= i_data;
end if;
end if;
end if;
end process;
o_q <= reverse_vector(s_buffer(width - 1 downto width - size));
end architecture;
/*****************************************************************/
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity sprite_renderer is
port
(
i_sprite_x : in std_logic_vector(7 downto 0);
i_line_x : in std_logic_vector(7 downto 0);
i_tile_low : in std_logic_vector(7 downto 0);
i_tile_high : in std_logic_vector(7 downto 0);
i_enable : in std_logic;
i_first_col : in std_logic;
o_pixel : out std_logic_vector(1 downto 0)
);
end sprite_renderer;
architecture behavioral of sprite_renderer is
signal s_offset : integer range 0 to 7;
signal s_sprite_right : std_logic_vector(8 downto 0);
signal s_draw_n : boolean;
begin
s_offset <= to_integer(unsigned(i_sprite_x(2 downto 0) - i_line_x(2 downto 0) - "001"));
s_sprite_right <= ('0' & i_sprite_x) + "000001000";
s_draw_n <= (i_first_col = '0') and (i_line_x(7 downto 3) = "00000");
o_pixel <= "00" when (i_enable = '0') or s_draw_n or (i_line_x < i_sprite_x) or (i_line_x >= s_sprite_right) else i_tile_high(s_offset) & i_tile_low(s_offset);
end architecture;
/*****************************************************************/
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
use work.common.all;
entity 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 ppu;
architecture behavioral of ppu is
component parallel_serial_shifter is
generic
(
width: integer := 8;
size: integer := 8
);
port
(
i_clk : in std_logic;
i_clk_enable : in std_logic := '1';
i_load : in std_logic;
i_enable : in std_logic;
i_data : in std_logic_vector(7 downto 0);
o_q : out std_logic_vector(7 downto 0)
);
end component;
component spritemem is
port
(
address : in std_logic_vector(7 downto 0);
clken : in std_logic := '1';
clock : in std_logic := '1';
data : in std_logic_vector(7 downto 0);
wren : in std_logic := '0';
q : out std_logic_vector(7 downto 0)
);
end component;
component soamem is
port
(
address : in std_logic_vector(4 downto 0);
clken : in std_logic := '1';
clock : in std_logic := '1';
data : in std_logic_vector(7 downto 0);
wren : in std_logic;
q : out std_logic_vector(7 downto 0)
);
end component;
component sprite_renderer is
port
(
i_sprite_x : in std_logic_vector(7 downto 0);
i_line_x : in std_logic_vector(7 downto 0);
i_tile_low : in std_logic_vector(7 downto 0);
i_tile_high : in std_logic_vector(7 downto 0);
i_enable : in std_logic;
i_first_col : in std_logic;
o_pixel : out std_logic_vector(1 downto 0)
);
end component;
function to_pal_idx(addr: std_logic_vector(4 downto 0)) return integer is
begin
if (addr(4) = '1') and (addr(1 downto 0) = "00") then
return to_integer(unsigned('0' & addr(3 downto 0)));
else
return to_integer(unsigned(addr));
end if;
end;
type sprite_t is record
x : std_logic_vector(7 downto 0);
tile_low : std_logic_vector(7 downto 0);
tile_high : std_logic_vector(7 downto 0);
priority : std_logic;
palette : std_logic_vector(1 downto 0);
pixel : std_logic_vector(1 downto 0);
end record;
type io_state_t is (idle, ppuctrl, ppumask, ppustatus, oamaddr, oamdata_read, oamdata_write, ppuscroll_x, ppuscroll_y, ppuaddr_hi, ppuaddr_lo, ppudata_read, ppudata_write);
type sprite_memory_t is (oam, soa);
type sprite_state_t is (idle, clear1, clear2, ev_y1, ev_y2, ev_tile1, ev_tile2, ev_attr1, ev_attr2, ev_x1, ev_x2, of_y1, of_y2, of_title1, of_title2, of_attr1, of_attr2, of_x1, of_x2, wait1a, wait1b, wait_eol, gf1, gf2, gf3, gf4, ftl1, ftl2, fth1, fth2);
type background_state_t is (idle, nt1, nt2, at1, at2, tl1, tl2, th1, th2);
type sprite_list_t is array (0 to 7) of sprite_t;
type byte_array_t is array (0 to 31) of std_logic_vector(5 downto 0);
constant NULL_SPRITE : sprite_t := (x => x"00", tile_low => x"00", tile_high => x"00", priority => '0', palette => "00", pixel => "00");
constant SKIP_DOT_CYCLE : integer := 339;
constant ENABLE_NMI_WRITE_CYCLE : integer := 0;
constant PPUSTATUS_READ_CYCLE : integer := 1; -- 0
constant VBLANK_SET_CYCLE : integer := 0; -- 1
constant VBLANK_CLEAR_CYCLE : integer := 0; -- 1
constant SPR0HIT_CLEAR_CYCLE : integer := 0; -- 0
constant SPROVFW_CLEAR_CYCLE : integer := 0; -- 0
constant MAX_DECAY : integer := 3192000;
type decay_array_t is array (0 to 7) of integer range 0 to MAX_DECAY;
signal s_io_state : io_state_t := idle;
signal s_io_data : std_logic_vector(7 downto 0) := x"00";
signal s_io_mem : std_logic_vector(7 downto 0) := x"00";
signal s_io_cycle : integer range 0 to 7 := 0;
signal s_io_latch : boolean := true;
signal s_sprites : sprite_list_t := (others => NULL_SPRITE);
signal s_spr_mem : sprite_memory_t := oam;
signal s_spr_mem_d : sprite_memory_t := oam;
signal s_oam_clk_enable : std_logic;
signal s_oam_addr : std_logic_vector(7 downto 0) := x"00";
signal s_oam_write_enable : std_logic;
signal s_oam_eff_q : std_logic_vector(7 downto 0);
signal s_oam_q : std_logic_vector(7 downto 0);
signal s_soa_clk_enable : std_logic;
signal s_soa_addr : std_logic_vector(4 downto 0) := (others => '0');
signal s_soa_q : std_logic_vector(7 downto 0) := (others => '0');
signal s_soa_write_enable : std_logic := '0';
signal s_spr_addr : std_logic_vector(15 downto 0) := (others => '0');
signal s_bkg_addr : std_logic_vector(15 downto 0) := (others => '0');
signal s_spr_read_enable : std_logic := '0';
signal s_bkg_read_enable : std_logic := '0';
signal s_video_addr : std_logic_vector(15 downto 0) := (others => '0');
signal s_video_read_enable : std_logic;
signal s_video_write_enable : std_logic;
signal s_xpos : std_logic_vector(7 downto 0);
signal s_ypos : std_logic_vector(7 downto 0);
signal s_cycle : integer range 0 to 340 := 0;
signal s_line : integer range 0 to 311 := 311;
signal s_max_line : integer range 0 to 311 := 261;
signal s_new_max_line : integer range 0 to 311;
signal s_tile_index : std_logic_vector(7 downto 0);
signal s_background_half : std_logic := '0';
signal s_sprite_half : std_logic := '0';
signal s_shifter_enable : std_logic := '0';
signal s_shifter_load : std_logic := '0';
signal s_render : boolean;
signal s_tl_data : std_logic_vector(7 downto 0) := (others => '0');
signal s_tl_q : std_logic_vector(7 downto 0);
signal s_th_data : std_logic_vector(7 downto 0) := (others => '0');
signal s_th_q : std_logic_vector(7 downto 0);
signal s_bl_q : std_logic_vector(7 downto 0);
signal s_bh_q : std_logic_vector(7 downto 0);
signal s_background_palette_index: std_logic_vector(1 downto 0);
signal s_new_background_palette_index : std_logic_vector(1 downto 0);
signal s_color : std_logic_vector(5 downto 0);
signal s_palette_color : std_logic_vector(5 downto 0);
signal s_bkg_state : background_state_t := idle;
signal s_spr_state : sprite_state_t := idle;
signal s_out_addr : std_logic_vector(15 downto 0) := x"0000";
signal s_out_data : std_logic_vector(8 downto 0);
signal s_out_color : std_logic_vector(5 downto 0);
signal s_out_write_enable : std_logic := '0';
signal s_visible_line : boolean;
signal s_visible_or_prescan_line : boolean;
signal s_prescan_line : boolean;
signal s_background_pixel : std_logic_vector(1 downto 0);
signal s_winning_sprite : sprite_t;
signal s_spr_idx : integer range 0 to 7 := 0;
signal s_spr_fill : integer range -1 to 7 := -1;
signal s_sprite_y : std_logic_vector(8 downto 0) := (others => '0');
signal s_sprite_tile : std_logic_vector(7 downto 0) := (others => '0');
signal s_sprite_disable : std_logic;
signal s_sprite_0_hit : std_logic := '0';
signal s_sprite_0_visible : boolean := false;
signal s_new_sprite_0_visible : boolean := false;
signal s_sprite_overflow : std_logic := '0';
signal s_sprite_flip_horizontal : std_logic := '0';
signal s_tile_data : std_logic_vector(7 downto 0);
signal s_enable_background : std_logic := '0';
signal s_enable_sprites : std_logic := '0';
signal s_render_first_bkg_col : std_logic := '0';
signal s_render_first_spr_col : std_logic := '0';
signal s_big_sprites : std_logic := '0';
signal s_sprite_online : boolean;
signal s_sprite_line_lower_test : boolean;
signal s_sprite_line_upper_test : boolean;
signal s_sprite_line_test_height : std_logic_vector(7 downto 0);
signal s_inner_tile_pos : std_logic_vector(3 downto 0);
signal s_next_tile_addr : std_logic_vector(12 downto 0);
signal s_tile_addr : std_logic_vector(15 downto 0);
signal s_attr_addr : std_logic_vector(15 downto 0);
signal s_tile_lo_addr : std_logic_vector(15 downto 0);
signal s_tile_hi_addr : std_logic_vector(15 downto 0);
signal s_q : std_logic_vector(7 downto 0) := x"00";
signal s_perform_oam_write_access : boolean;
signal s_perform_oam_access : boolean;
signal s_vblank : std_logic := '0';
signal s_enable_nmi : std_logic := '0';
signal s_vram_inc : std_logic := '0';
signal s_vram_addr_t : std_logic_vector(14 downto 0) := 15x"0000";
signal s_vram_addr_v : std_logic_vector(14 downto 0) := 15x"0000";
signal s_fine_scroll_x : integer range 0 to 7 := 0;
signal s_palette_access : boolean;
signal s_palette_index : std_logic_vector(4 downto 0);
signal s_palette_quadrant : std_logic_vector(1 downto 0);
signal s_palette_mem : byte_array_t := ( 6x"09", 6x"01", 6x"00", 6x"01", 6x"00", 6x"02", 6x"02", 6x"0D", 6x"08", 6x"10", 6x"08", 6x"24", 6x"00", 6x"00", 6x"04", 6x"2C", 6x"09", 6x"01", 6x"34", 6x"03", 6x"00", 6x"04", 6x"00", 6x"14", 6x"08", 6x"3A", 6x"00", 6x"02", 6x"00", 6x"20", 6x"2C", 6x"08" );
signal s_hblank_cycle : boolean;
signal s_first_col_n : boolean;
--signal s_vram_bkg_inc : boolean;
signal s_frame_latch : boolean := true;
signal s_enable_rendering : boolean;
signal s_enable_shortcut : boolean := false;
signal s_new_enable_shortcut : boolean;
signal s_greyscale : std_logic := '0';
signal s_color_emphasize : std_logic_vector(2 downto 0) := "000";
signal s_skip_dot : boolean;
signal s_last_cycle : boolean;
signal s_q_decay : decay_array_t := (others => 0);
signal s_decay_mask : std_logic_vector(7 downto 0) := (others => '0');
signal s_oam_data : std_logic_vector(7 downto 0);
begin
oamem: spritemem port map
(
clock => i_clk,
clken => s_oam_clk_enable,
address => s_oam_addr,
data => s_oam_data,
wren => s_oam_write_enable,
q => s_oam_q
);
soam: soamem port map
(
clock => i_clk,
clken => s_soa_clk_enable,
address => s_soa_addr,
data => s_oam_eff_q,
wren => s_soa_write_enable,
q => s_soa_q
);
tl_shifter: parallel_serial_shifter generic map (16, 8) port map
(
i_clk => i_clk,
i_clk_enable => i_clk_enable,
i_load => s_shifter_load,
i_enable => s_shifter_enable,
i_data => s_tl_data,
o_q => s_tl_q
);
th_shifter: parallel_serial_shifter generic map (16, 8) port map
(
i_clk => i_clk,
i_clk_enable => i_clk_enable,
i_load => s_shifter_load,
i_enable => s_shifter_enable,
i_data => i_chr_data,
o_q => s_th_q
);
bl_shifter: parallel_serial_shifter generic map (16, 8) port map
(
i_clk => i_clk,
i_clk_enable => i_clk_enable,
i_load => s_shifter_load,
i_enable => s_shifter_enable,
i_data => s_new_background_palette_index(0) & s_new_background_palette_index(0) & s_new_background_palette_index(0) & s_new_background_palette_index(0) & s_new_background_palette_index(0) & s_new_background_palette_index(0) & s_new_background_palette_index(0) & s_new_background_palette_index(0),
o_q => s_bl_q
);
bh_shifter: parallel_serial_shifter generic map (16, 8) port map
(
i_clk => i_clk,
i_clk_enable => i_clk_enable,
i_load => s_shifter_load,
i_enable => s_shifter_enable,
i_data => s_new_background_palette_index(1) & s_new_background_palette_index(1) & s_new_background_palette_index(1) & s_new_background_palette_index(1) & s_new_background_palette_index(1) & s_new_background_palette_index(1) & s_new_background_palette_index(1) & s_new_background_palette_index(1),
o_q => s_bh_q
);
spr_gen: for i in 0 to 7 generate
spr: sprite_renderer port map
(
i_sprite_x => s_sprites(i).x,
i_line_x => s_xpos,
i_tile_low => s_sprites(i).tile_low,
i_tile_high => s_sprites(i).tile_high,
i_enable => s_enable_sprites,
i_first_col => s_render_first_spr_col,
o_pixel => s_sprites(i).pixel
);
end generate;
-- IO
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
s_decay_mask <= (others => '0');
if i_reset_n = '0' then
s_q <= x"00";
s_io_data <= x"00";
s_io_state <= idle;
s_io_latch <= true;
s_q_decay <= (others => 0);
else
for i in 0 to 7 loop
if s_decay_mask(i) = '1' then
s_q_decay(i) <= MAX_DECAY;
elsif s_q_decay(i) /= 0 then
s_q_decay(i) <= s_q_decay(i) - 1;
else
s_q(i) <= '0';
end if;
end loop;
case s_io_state is
when idle =>
if i_cs_n = '0' then
s_io_data <= i_data;
s_io_cycle <= 0;
if i_write_enable = '1' then
s_q <= i_data;
s_decay_mask <= (others => '1');
end if;
case i_addr is
when "000" => -- PPUCTRL
if i_write_enable = '1' then
s_io_state <= ppuctrl;
end if;
when "001" => -- PPUMASK
if i_write_enable = '1' then
s_io_state <= ppumask;
end if;
when "010" => -- PPUSTATUS
if i_write_enable = '0' then
s_io_state <= ppustatus;
s_io_latch <= true;
end if;
when "011" => -- OAMADDR
if i_write_enable = '1' then
s_io_state <= oamaddr;
s_io_data <= i_data;
end if;
when "100" => -- OAMDATA
if i_write_enable = '1' then
s_io_state <= oamdata_write;
else
s_io_state <= oamdata_read;
end if;
when "101" => -- PPUSCROLL
if i_write_enable = '1' then
if s_io_latch then
s_io_state <= ppuscroll_x;
else
s_io_state <= ppuscroll_y;
end if;
s_io_latch <= not s_io_latch;
end if;
when "110" => -- PPUADDR
if i_write_enable = '1' then
if s_io_latch then
s_io_state <= ppuaddr_hi;
else
s_io_state <= ppuaddr_lo;
end if;
s_io_latch <= not s_io_latch;
end if;
when "111" => -- PPUDATA
if i_write_enable = '1' then
s_io_state <= ppudata_write;
else
s_io_state <= ppudata_read;
end if;
when others =>
end case;
end if;
when oamdata_read =>
if s_io_cycle = 1 then
s_io_state <= idle;
s_decay_mask <= (others => '1');
if s_spr_mem_d = oam then
s_q <= s_oam_eff_q;
else
s_q <= s_soa_q;
end if;
else
s_io_cycle <= s_io_cycle + 1;
end if;
when ppustatus =>
if s_io_cycle = 2 then
s_io_state <= idle;
else
if s_io_cycle = PPUSTATUS_READ_CYCLE then
s_q(7 downto 5) <= s_vblank & s_sprite_0_hit & s_sprite_overflow;
s_decay_mask(7 downto 5) <= (others => '1');
end if;
s_io_cycle <= s_io_cycle + 1;
end if;
when oamaddr | oamdata_write | ppuctrl | ppuscroll_x | ppuscroll_y | ppumask | ppuaddr_hi =>
if s_io_cycle = 1 then
s_io_state <= idle;
else
s_io_cycle <= s_io_cycle + 1;
end if;
when ppudata_read =>
if s_io_cycle = 5 then
s_io_state <= idle;
s_io_mem <= i_chr_data;
else
if s_io_cycle = 1 then
if s_palette_access then
s_q(5 downto 0) <= s_palette_mem(to_pal_idx(s_vram_addr_v(4 downto 0)));
s_decay_mask(5 downto 0) <= (others => '1');
else
s_q <= s_io_mem;
s_decay_mask <= (others => '1');
end if;
end if;
s_io_cycle <= s_io_cycle + 1;
end if;
when ppuaddr_lo =>
if s_io_cycle = 2 then
s_io_state <= idle;
else
s_io_cycle <= s_io_cycle + 1;
end if;
when ppudata_write =>
if s_io_cycle = 7 then
s_io_state <= idle;
else
s_io_cycle <= s_io_cycle + 1;
end if;
end case;
end if;
end if;
end if;
end process;
-- Zeilen & Zyklen
process (i_video_mode)
begin
case i_video_mode is
when ntsc =>
s_new_max_line <= 261;
s_new_enable_shortcut <= true;
when pal =>
s_new_max_line <= 311;
s_new_enable_shortcut <= false;
end case;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_line <= s_new_max_line;
s_max_line <= s_new_max_line;
s_enable_shortcut <= s_new_enable_shortcut;
s_cycle <= 0;
s_frame_latch <= true;
else
if s_last_cycle then
s_cycle <= 0;
if s_prescan_line then
s_line <= 0;
s_max_line <= s_new_max_line;
s_frame_latch <= not s_frame_latch;
s_enable_shortcut <= s_new_enable_shortcut;
if s_skip_dot then
s_cycle <= 1;
end if;
else
s_line <= s_line + 1;
end if;
else
s_cycle <= s_cycle + 1;
end if;
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_skip_dot <= false;
elsif s_prescan_line and s_last_cycle then
s_skip_dot <= false;
elsif s_prescan_line and (s_cycle = SKIP_DOT_CYCLE) and s_frame_latch and s_enable_rendering and s_enable_shortcut then
s_skip_dot <= true;
end if;
end if;
end if;
end process;
-- Enable Rendering
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_greyscale <= '0';
elsif s_io_state = ppumask then
s_greyscale <= s_io_data(0);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_render_first_bkg_col <= '0';
elsif s_io_state = ppumask then
s_render_first_bkg_col <= s_io_data(1);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_render_first_spr_col <= '0';
elsif s_io_state = ppumask then
s_render_first_spr_col <= s_io_data(2);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_enable_background <= '0';
elsif s_io_state = ppumask then
s_enable_background <= s_io_data(3);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_enable_sprites <= '0';
elsif s_io_state = ppumask then
s_enable_sprites <= s_io_data(4);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_color_emphasize <= "000";
elsif s_io_state = ppumask then
case i_video_mode is
when ntsc =>
s_color_emphasize <= s_io_data(7 downto 5);
when pal =>
s_color_emphasize <= s_io_data(7) & s_io_data(5) & s_io_data(6);
end case;
end if;
end if;
end if;
end process;
-- Setup
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_vram_inc <= '0';
elsif s_io_state = ppuctrl then
s_vram_inc <= s_io_data(2);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_sprite_half <= '0';
elsif s_io_state = ppuctrl then
s_sprite_half <= s_io_data(3);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_background_half <= '0';
elsif s_io_state = ppuctrl then
s_background_half <= s_io_data(4);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_big_sprites <= '0';
elsif s_io_state = ppuctrl then
s_big_sprites <= s_io_data(5);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_enable_nmi <= '0';
elsif (s_io_state = ppuctrl) and (s_io_cycle = ENABLE_NMI_WRITE_CYCLE) then
s_enable_nmi <= s_io_data(7);
end if;
end if;
end if;
end process;
-- VRAM Adressierung
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_vram_addr_t <= 15x"0000";
elsif s_io_state = ppuaddr_hi then
s_vram_addr_t(14) <= '0';
s_vram_addr_t(13 downto 8) <= s_io_data(5 downto 0);
elsif (s_io_state = ppuaddr_lo) and (s_io_cycle = 0) then
s_vram_addr_t(7 downto 0) <= s_io_data;
elsif s_io_state = ppuscroll_x then
s_vram_addr_t(4 downto 0) <= s_io_data(7 downto 3);
elsif s_io_state = ppuscroll_y then
s_vram_addr_t(14 downto 12) <= s_io_data(2 downto 0);
s_vram_addr_t(9 downto 5) <= s_io_data(7 downto 3);
elsif s_io_state = ppuctrl then
s_vram_addr_t(11 downto 10) <= s_io_data(1 downto 0);
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_fine_scroll_x <= 0;
elsif s_io_state = ppuscroll_x then
s_fine_scroll_x <= to_integer(unsigned(s_io_data(2 downto 0)));
end if;
end if;
end if;
end process;
process (i_clk)
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if i_reset_n = '0' then
s_vram_addr_v <= 15x"0000";
elsif (s_io_state = ppuaddr_lo) and (s_io_cycle = 2) then
s_vram_addr_v <= s_vram_addr_t;
elsif ((s_io_state = ppudata_write) and (s_io_cycle = 7)) or ((s_io_state = ppudata_read) and (s_io_cycle = 5)) then
if s_vram_inc = '0' then
s_vram_addr_v <= s_vram_addr_v + 15x"0001";
else
s_vram_addr_v <= s_vram_addr_v + 15x"0020";
end if;
s_vram_addr_v(14) <= '0';
elsif s_enable_rendering and s_visible_or_prescan_line then
if std_logic_vector(to_unsigned(s_cycle, 3)) = "111" and not s_hblank_cycle then
-- Horizontal LoopyV increment
if s_vram_addr_v(4 downto 0) = "11111" then
s_vram_addr_v(4 downto 0) <= "00000";
s_vram_addr_v(10) <= not s_vram_addr_v(10);
else
s_vram_addr_v(4 downto 0) <= s_vram_addr_v(4 downto 0) + "00001";
end if;
end if;
if s_cycle = 255 then
-- Vertical LoopyV increment
if s_vram_addr_v(14 downto 12) = "111" then
s_vram_addr_v(14 downto 12) <= "000";
if s_vram_addr_v(9 downto 5) = "11101" then -- 29
s_vram_addr_v(11) <= not s_vram_addr_v(11);
s_vram_addr_v(9 downto 5) <= "00000";
elsif s_vram_addr_v(9 downto 5) = "11111" then -- 31
s_vram_addr_v(9 downto 5) <= "00000";
else
s_vram_addr_v(9 downto 5) <= s_vram_addr_v(9 downto 5) + "00001";
end if;
else
s_vram_addr_v(14 downto 12) <= s_vram_addr_v(14 downto 12) + "001";
end if;
elsif s_cycle = 256 then
-- Horizontal LoopyV <= Horizontal LoopyT
s_vram_addr_v(10) <= s_vram_addr_t(10);
s_vram_addr_v(4 downto 0) <= s_vram_addr_t(4 downto 0);
elsif (s_cycle >= 279) and (s_cycle <= 303) and s_prescan_line then
-- Vertical LoopyV <= Vertical LoopyT
s_vram_addr_v(9 downto 5) <= s_vram_addr_t(9 downto 5);
s_vram_addr_v(14 downto 11) <= s_vram_addr_t(14 downto 11);
end if;
end if;
end if;
end if;
end process;
-- Palette
process (i_clk)
variable index : integer range 0 to 31;
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
if (s_io_state = ppudata_write) and s_palette_access and (s_io_cycle = 1) then
index := to_pal_idx(s_vram_addr_v(4 downto 0));
s_palette_mem(index) <= s_io_data(5 downto 0);
end if;
end if;
end if;
end process;
-- Hintergrund rendern
process (i_clk)
variable ypos : std_logic_vector(7 downto 0);
begin
if rising_edge(i_clk) then
if i_clk_enable = '1' then
s_shifter_load <= '0';
s_bkg_read_enable <= '0';
if i_reset_n = '0' then
s_bkg_state <= idle;
else
case s_bkg_state is
when idle =>
s_bkg_state <= nt1;
s_bkg_addr <= s_tile_addr;
when nt1 => -- Name Table
s_bkg_state <= nt2;
s_bkg_read_enable <= '1';
when nt2 =>
s_bkg_addr <= s_attr_addr;
s_bkg_state <= at1;
when at1 => -- Attribute Table
s_tile_index <= i_chr_data;
s_bkg_state <= at2;
s_bkg_read_enable <= '1';
when at2 =>
if not s_last_cycle then
s_bkg_state <= tl1;
s_bkg_addr <= s_tile_lo_addr;
elsif s_skip_dot then
s_bkg_state <= nt1;
s_bkg_addr <= s_tile_addr;
else
s_bkg_state <= idle;
end if;
when tl1 => -- Tile low
s_bkg_state <= tl2;
s_bkg_read_enable <= '1';
case s_palette_quadrant is
when "00" =>
s_new_background_palette_index <= i_chr_data(1 downto 0);
when "01" =>
s_new_background_palette_index <= i_chr_data(3 downto 2);
when "10" =>
s_new_background_palette_index <= i_chr_data(5 downto 4);
when "11" =>
s_new_background_palette_index <= i_chr_data(7 downto 6);
when others =>
s_new_background_palette_index <= "00";
end case;
when tl2 =>
s_bkg_state <= th1;
s_bkg_addr <= s_tile_hi_addr;
when th1 => -- Tile high
s_bkg_state <= th2;
s_bkg_read_enable <= '1';
s_tl_data <= i_chr_data;
when th2 =>
s_shifter_load <= '1';
s_bkg_state <= nt1;
s_bkg_addr <= s_tile_addr;
when others =>
s_bkg_state <= idle;
end case;
end if;
end if;
end if;
end process;
s_shifter_enable <= '0' when (s_cycle > 336) or (s_cycle = 0) else '1';
-- Sprites rendern
process (i_clk)
begin
if rising_edge(i_clk) then