forked from MiSTer-devel/TurboGrafx16_MiSTer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhuc6270.vhd
2192 lines (1884 loc) · 55.4 KB
/
huc6270.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
-- #############################################################################
-- ################################## TODO #####################################
-- #############################################################################
-- #############################################################################
-- #############################################################################
-- #############################################################################
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.NUMERIC_STD.ALL;
entity huc6270 is
generic (
MAX_SPPL : integer := 15
);
port (
CLK : in std_logic;
RESET_N : in std_logic;
HSIZE : in std_logic_vector(9 downto 0);
HSTART : in std_logic_vector(9 downto 0);
SP64 : in std_logic;
-- CPU Interface
A : in std_logic_vector(1 downto 0);
CE_N : in std_logic;
WR_N : in std_logic;
RD_N : in std_logic;
DI : in std_logic_vector(7 downto 0);
DO : out std_logic_vector(7 downto 0);
BUSY_N : out std_logic;
IRQ_N : out std_logic;
-- VCE Interface
COLNO : out std_logic_vector(8 downto 0);
CLKEN : in std_logic;
HS_N : in std_logic;
VS_N : in std_logic
);
end huc6270;
architecture rtl of huc6270 is
--------------------------------------------------------------------------------
-- Registers
--------------------------------------------------------------------------------
signal MAWR : std_logic_vector(15 downto 0);
signal MARR : std_logic_vector(15 downto 0);
signal VRR : std_logic_vector(15 downto 0); -- VRAM Read Buffer
signal VWR : std_logic_vector(15 downto 0); -- VRAM Write Latch
signal CR : std_logic_vector(15 downto 0);
signal RCR : std_logic_vector(15 downto 0);
signal BXR : std_logic_vector(15 downto 0);
signal BYR : std_logic_vector(15 downto 0);
signal MWR : std_logic_vector(15 downto 0);
--signal HPR : std_logic_vector(15 downto 0);
signal HDR : std_logic_vector(15 downto 0);
signal VSR : std_logic_vector(15 downto 0);
signal VDR : std_logic_vector(15 downto 0);
signal VDE : std_logic_vector(15 downto 0);
signal DCR : std_logic_vector(15 downto 0);
signal SOUR : std_logic_vector(15 downto 0);
signal DESR : std_logic_vector(15 downto 0);
signal LENR : std_logic_vector(15 downto 0);
signal SATB : std_logic_vector(15 downto 0);
--------------------------------------------------------------------------------
-- Video counting and internal synchronization
--------------------------------------------------------------------------------
-- Registers
signal HDW : std_logic_vector(6 downto 0);
-- Interrupts
signal IRQ_RCR_SET : std_logic;
signal IRQ_VBL_SET : std_logic;
-- Intermediate signals
signal X : std_logic_vector(9 downto 0);
signal Y : std_logic_vector(8 downto 0);
signal HS_N_PREV : std_logic;
signal VS_N_PREV : std_logic;
signal X_BG_START : std_logic_vector(9 downto 0);
signal X_REN_START : std_logic_vector(9 downto 0);
signal X_BG_END : std_logic_vector(9 downto 0);
signal X_REN_END : std_logic_vector(9 downto 0);
signal Y_BGREN_START : std_logic_vector(8 downto 0);
signal Y_BGREN_END : std_logic_vector(8 downto 0);
-- signal X_SP_START : std_logic_vector(9 downto 0);
-- signal X_SP_END : std_logic_vector(9 downto 0);
signal Y_SP_START : std_logic_vector(8 downto 0);
signal Y_SP_END : std_logic_vector(8 downto 0);
signal BG_ACTIVE : std_logic;
signal SP1_ACTIVE : std_logic;
signal SP2_ACTIVE : std_logic;
signal REN_ACTIVE : std_logic;
signal DBG_VBL : std_logic;
signal DCR_DMAS_REQ : std_logic;
signal SP_ON : std_logic;
signal BG_ON : std_logic;
signal BURST : std_logic;
--------------------------------------------------------------------------------
-- Background engine
--------------------------------------------------------------------------------
-- Pseudo-registers
signal BG_VS : std_logic;
signal BG_HS : std_logic_vector(1 downto 0);
signal BG_DW : std_logic_vector(1 downto 0);
signal BG_CM : std_logic;
-- Intermediate signals - Part 1
signal BG_Y : std_logic_vector(8 downto 0);
signal BG_TX : std_logic_vector(6 downto 0);
signal TX : std_logic_vector(6 downto 0);
signal TX_MAX : std_logic_vector(6 downto 0);
signal BG_PX : std_logic_vector(9 downto 0);
signal BG_PAL : std_logic_vector(3 downto 0);
signal BG_P01 : std_logic_vector(15 downto 0);
signal BG_P23 : std_logic_vector(15 downto 0);
signal YOFS : std_logic_vector(8 downto 0);
signal YOFS_RELOAD : std_logic;
signal YOFS_REL_REQ : std_logic;
signal YOFS_REL_ACK : std_logic;
signal XOFS : std_logic_vector(9 downto 0);
-- State machine - Part 1
type bg1_t is ( BG1_INI, BG1_INI_W,
BG1_CPU, BG1_CPU_W,
BG1_BAT, BG1_BAT_W,
BG1_NOP, BG1_NOP_W,
BG1_CG0, BG1_CG0_W,
BG1_CG1, BG1_CG1_W,
BG1_END );
signal BG1 : bg1_t;
signal BG_CYC : std_logic_vector(2 downto 0);
signal BG_BUSY : std_logic;
signal BG_BUSY2 : std_logic;
-- Intermediate signals - Part 2
signal BG2_REQ : std_logic;
signal BG2_INI_REQ : std_logic;
signal BG2_MEM_A : std_logic_vector(9 downto 0);
signal BG2_MEM_WE : std_logic;
signal BG2_MEM_DI : std_logic_vector(7 downto 0);
signal BG2_PAL : std_logic_vector(3 downto 0);
signal BG2_P0 : std_logic_vector(7 downto 0);
signal BG2_P1 : std_logic_vector(7 downto 0);
signal BG2_P2 : std_logic_vector(7 downto 0);
signal BG2_P3 : std_logic_vector(7 downto 0);
signal TPX : std_logic_vector(2 downto 0);
-- State machine - Part 2
type bg2_t is ( BG2_INI, BG2_WRITE, BG2_LOOP );
signal BG2 : bg2_t;
-- Background engine - RAM access signals
signal BG_RAM_A_FF : std_logic_vector(15 downto 0);
signal BG_RAM_REQ_FF : std_logic := '0';
signal BG_RAM_DO : std_logic_vector(15 downto 0);
signal BG_RAM_ACK : std_logic;
--------------------------------------------------------------------------------
-- Sprite engine
--------------------------------------------------------------------------------
-- Pseudo-registers
signal SP_DW : std_logic_vector(1 downto 0);
-- Interrupts
signal IRQ_COL_SET : std_logic;
signal IRQ_COL_TRIG : std_logic;
signal IRQ_OVF_SET : std_logic;
-- Intermediate signals - Part 1
signal SP_NB : std_logic_vector(6 downto 0);
signal SP_CUR_Y : std_logic_vector(9 downto 0);
signal SP_Y : std_logic_vector(9 downto 0);
signal SP_X : std_logic_vector(9 downto 0);
signal SP_CG : std_logic;
signal SP_NAME : std_logic_vector(9 downto 0);
signal SP_VF : std_logic;
signal SP_CGY : std_logic_vector(1 downto 0);
signal SP_HF : std_logic;
signal SP_CGX : std_logic;
signal SP_PRI : std_logic;
signal SP_PAL : std_logic_vector(3 downto 0);
-- Sprite pre-buffers
type sp_prebuf_entry_t is
record
-- Information that will be copied as is in the Sprite buffers
ZERO : std_logic;
PRI : std_logic;
PAL : std_logic_vector(3 downto 0);
HF : std_logic;
X : std_logic_vector(9 downto 0);
-- Information required to fill the P0-P3 buffers
CG : std_logic;
ADDR : std_logic_vector(15 downto 0);
end record;
type sp_prebuf_t is array(0 to MAX_SPPL) of sp_prebuf_entry_t;
signal SP_PREBUF : sp_prebuf_t;
-- Sprite engine - SAT access signals
signal SP_SAT_A : std_logic_vector(7 downto 0);
signal SP_SAT_DO : std_logic_vector(15 downto 0);
-- State machine - Part 1
type sp1_t is ( SP1_INI,
SP1_RD0, SP1_RD1, SP1_RD2, SP1_RD3, SP1_TST,
SP1_LEFT, SP1_RIGHT,
SP1_LOOP, SP1_END );
signal SP1 : sp1_t;
signal SP1_CNT : std_logic_vector(1 downto 0);
-- Intermediate signals - Part 2
signal SP_CUR : std_logic_vector(5 downto 0);
-- Sprite buffers
type sp_buf_entry_t is
record
ZERO : std_logic;
PRI : std_logic;
PAL : std_logic_vector(3 downto 0);
HF : std_logic;
X : std_logic_vector(9 downto 0);
P0 : std_logic_vector(15 downto 0);
P1 : std_logic_vector(15 downto 0);
P2 : std_logic_vector(15 downto 0);
P3 : std_logic_vector(15 downto 0);
end record;
type sp_buf_t is array(0 to MAX_SPPL) of sp_buf_entry_t;
signal SP_BUF : sp_buf_t;
-- Sprite engine - RAM access signals
signal SP_RAM_A_FF : std_logic_vector(15 downto 0);
signal SP_RAM_REQ_FF : std_logic := '0';
signal SP_RAM_DO : std_logic_vector(15 downto 0);
signal SP_RAM_ACK : std_logic;
-- State machine - Part 2
type sp2_t is ( SP2_INI, SP2_INI_W,
SP2_RD0, SP2_RD0_W,
SP2_RD1, SP2_RD1_W,
SP2_RD2, SP2_RD2_W,
SP2_RD3, SP2_RD3_W,
SP2_END );
signal SP2 : sp2_t;
signal SP_BUSY : std_logic;
signal SP_CYC : std_logic_vector(1 downto 0);
--------------------------------------------------------------------------------
-- Line rendering
--------------------------------------------------------------------------------
-- Intermediate signals
signal REN_MEM_A : std_logic_vector(9 downto 0);
signal REN_MEM_WE : std_logic;
signal REN_MEM_DO : std_logic_vector(7 downto 0);
signal REN_BG_COL : std_logic_vector(7 downto 0);
signal REN_SP_OPQ : std_logic_vector(MAX_SPPL downto 0); -- Sprite pixel on/off
constant SP_OPQ_Z : std_logic_vector(MAX_SPPL downto 0) := (others => '0');
type ren_sp_col_t is array(MAX_SPPL downto 0) of std_logic_vector(8 downto 0); -- PRI & PAL & COL
signal REN_SP_COLTAB : ren_sp_col_t;
signal REN_SP_COL : std_logic_vector(7 downto 0);
signal REN_SP_PRI : std_logic;
-- State machine
type ren_t is ( REN_INI, REN_BGR, REN_BGW, REN_CLK );
signal REN : ren_t;
-- Output buffers
signal COLNO_FF : std_logic_vector(8 downto 0);
--------------------------------------------------------------------------------
-- CPU Interface
--------------------------------------------------------------------------------
signal PREV_A : std_logic_vector(1 downto 0);
signal CPU_SOUR_SET_REQ : std_logic;
signal CPU_SOUR_SET_VAL : std_logic_vector(15 downto 0);
signal CPU_DESR_SET_REQ : std_logic;
signal CPU_DESR_SET_VAL : std_logic_vector(15 downto 0);
signal CPU_LENR_SET_REQ : std_logic;
signal CPU_LENR_SET_VAL : std_logic_vector(15 downto 0);
signal CPU_IRQ_CLR : std_logic;
signal CPU_DMAS_REQ : std_logic;
signal CPU_DMA_REQ : std_logic;
signal RD_BUF : std_logic_vector(15 downto 0);
signal WR_BUF : std_logic_vector(15 downto 0);
signal REG_SEL : std_logic_vector(4 downto 0);
-- Interrupts
signal IRQ_COL : std_logic;
signal IRQ_OVF : std_logic;
signal IRQ_RCR : std_logic;
signal IRQ_DMAS : std_logic;
signal IRQ_DMA : std_logic;
signal IRQ_VBL : std_logic;
-- RAM access signals
signal CPU_RAM_REQ_FF : std_logic := '0';
signal CPU_RAM_A_FF : std_logic_vector(15 downto 0);
signal CPU_RAM_DI_FF : std_logic_vector(15 downto 0);
signal CPU_RAM_WE_FF : std_logic := '0';
signal CPU_RAM_DO : std_logic_vector(15 downto 0);
signal CPU_RAM_ACK : std_logic;
-- Output buffers
signal DO_FF : std_logic_vector(7 downto 0);
signal BUSY_N_FF : std_logic;
signal IRQ_N_FF : std_logic;
-- State machine
type cpu_t is ( CPU_IDLE,
CPU_RAM_RD, CPU_RAM_PRE_RD,
CPU_RAM_WR_INC, CPU_RAM_PRE_WR_INC,
CPU_WAIT );
signal CPU : cpu_t;
--------------------------------------------------------------------------------
-- VRAM-VRAM DMA
--------------------------------------------------------------------------------
signal DMA_REQ : std_logic;
signal DMA_DMA_CLR : std_logic;
signal IRQ_DMA_SET : std_logic;
signal DMA_BUSY : std_logic;
signal DMA_RAM_REQ_FF : std_logic := '0';
signal DMA_RAM_A_FF : std_logic_vector(15 downto 0);
signal DMA_RAM_DI_FF : std_logic_vector(15 downto 0);
signal DMA_RAM_WE_FF : std_logic := '0';
signal DMA_RAM_DO : std_logic_vector(15 downto 0);
signal DMA_RAM_ACK : std_logic;
signal DMA_SOUR_SET_REQ : std_logic;
signal DMA_DESR_SET_REQ : std_logic;
signal DMA_LENR_SET_REQ : std_logic;
-- State machine
type dma_t is ( DMA_IDLE,
DMA_READ, DMA_READ1, DMA_READ2,
DMA_WRITE, DMA_WRITE1, DMA_WRITE2,
DMA_LOOP, DMA_LOOP2 );
signal DMA : dma_t;
--------------------------------------------------------------------------------
-- VRAM-SAT DMA
--------------------------------------------------------------------------------
signal DMAS_REQ : std_logic;
signal DMAS_DMAS_CLR : std_logic;
signal IRQ_DMAS_SET : std_logic;
signal DMAS_BUSY : std_logic;
signal DMAS_RAM_REQ_FF : std_logic := '0';
signal DMAS_RAM_A_FF : std_logic_vector(15 downto 0);
signal DMAS_RAM_DO : std_logic_vector(15 downto 0);
signal DMAS_RAM_ACK : std_logic;
signal DMAS_SAT_A : std_logic_vector(7 downto 0);
signal DMAS_SAT_DI : std_logic_vector(15 downto 0);
signal DMAS_SAT_WE : std_logic := '0';
-- State machine
type dmas_t is ( DMAS_IDLE,
DMAS_READ, DMAS_READ1, DMAS_READ2,
DMAS_WRITE,
DMAS_WAIT1, DMAS_WAIT2,
DMAS_END );
signal DMAS : dmas_t;
begin
--------------------------------------------------------------------------------
-- Background line buffer
--------------------------------------------------------------------------------
bg_buf : entity work.dpram generic map (10,8)
port map(
clock => CLK,
address_a=> BG2_MEM_A,
data_a => BG2_MEM_DI,
wren_a => BG2_MEM_WE,
q_a => open,
address_b=> REN_MEM_A,
data_b => "00000000",
wren_b => REN_MEM_WE,
q_b => REN_MEM_DO
);
--------------------------------------------------------------------------------
-- SAT
--------------------------------------------------------------------------------
sat : entity work.dpram generic map (8,16)
port map(
clock => CLK,
data_a => DMAS_SAT_DI,
address_a=> DMAS_SAT_A,
wren_a => DMAS_SAT_WE,
address_b=> SP_SAT_A,
q_b => SP_SAT_DO
);
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Video counting and internal synchronization
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
process( CLK )
variable V_HSW : std_logic_vector(9 downto 0);
variable V_HDS : std_logic_vector(9 downto 0);
variable V_HDE : std_logic_vector(6 downto 0);
variable V_HDW : std_logic_vector(9 downto 0);
variable V_VDS : std_logic_vector(8 downto 0);
variable V_VDE : std_logic_vector(8 downto 0);
variable V_VSW : std_logic_vector(5 downto 0);
variable V_VDW : std_logic_vector(8 downto 0);
variable V_VCR : std_logic_vector(7 downto 0);
variable RCNT : std_logic_vector(8 downto 0);
begin
if rising_edge(CLK) then
IRQ_RCR_SET <= '0';
IRQ_VBL_SET <= '0';
YOFS_REL_ACK <= '0';
if RESET_N = '0' then
X <= (others => '0');
Y <= (others => '1'); -- /!\
HS_N_PREV <= '1';
VS_N_PREV <= '0';
BG_ACTIVE <= '0';
SP1_ACTIVE <= '0';
SP2_ACTIVE <= '0';
REN_ACTIVE <= '0';
DCR_DMAS_REQ <= '0';
X_BG_START <= (others => '1');
X_REN_START <= (others => '1');
-- X_BG_END <= (others => '1');
X_REN_END <= (others => '1');
Y_BGREN_START <= (others => '1');
Y_BGREN_END <= (others => '1');
-- X_SP_START <= (others => '1');
-- X_SP_END <= (others => '1');
Y_SP_START <= (others => '1');
Y_SP_END <= (others => '1');
RCNT := (others => '1');
SP_ON <= '0';
BG_ON <= '0';
BURST <= '1';
YOFS <= (others => '0');
else
if CLKEN = '1' then
HS_N_PREV <= HS_N;
X <= X + 1;
DCR_DMAS_REQ <= '0';
if HS_N_PREV = '1' and HS_N = '0' then
X <= (others => '0');
--V_HDS := HPR(14 downto 8)&"000";
V_HDW := (HDR(6 downto 0)+"1")&"000";
if V_HDW >= HSIZE then
V_HDS := (others => '0');
V_HDW := HSIZE;
else
V_HDS := HSIZE - V_HDW;
end if;
V_HDS := '0'&V_HDS(9 downto 1) + HSTART - 1;
--V_HDS := HPR(4 downto 0);
--V_HDE := HDR(14 downto 8);
HDW <= HDR(6 downto 0);
X_REN_START <= V_HDS;
X_REN_END <= V_HDS + V_HDW;
-- BG must start before REN (max 2*8 tile reads, plus render overhead)
X_BG_START <= V_HDS - "10101";
-- Raster counter
RCNT := RCNT + 1;
if Y = Y_BGREN_START then
RCNT := "0" & x"40";
end if;
-- Raster compare interrupt
if RCNT = RCR(9 downto 0) and CR(2) = '1' then
IRQ_RCR_SET <= '1';
end if;
end if;
if X = X_BG_START-1 then
SP2_ACTIVE <= '0';
BG_ON <= CR(7);
if Y >= Y_BGREN_START and Y < Y_BGREN_END and CR(7) = '1' then
BG_ACTIVE <= '1';
YOFS_REL_ACK <= '1';
if Y = Y_BGREN_START then
YOFS <= BYR(8 downto 0);
elsif YOFS_RELOAD = '1' then
YOFS <= BYR(8 downto 0) + 1;
else
YOFS <= YOFS + 1;
end if;
XOFS <= BXR(9 downto 0);
end if;
end if;
if X = X_REN_START-1 then
SP_ON <= CR(6);
if Y >= Y_BGREN_START and Y < Y_BGREN_END then
REN_ACTIVE <= '1';
end if;
if Y >= Y_SP_START and Y < Y_SP_END and CR(6) = '1' then
SP1_ACTIVE <= '1';
end if;
-- VBlank Interrupt
if Y = Y_BGREN_END and CR(3) = '1' then
IRQ_VBL_SET <= '1';
end if;
-- Burst Mode
-- Shouldn't CR(7 downto 6)be checked every visible line according to doc?
if Y = Y_BGREN_START then
if CR(7 downto 6) = "00" then
BURST <= '1';
else
BURST <= '0';
end if;
end if;
end if;
if X = X_REN_END-1 then
BG_ACTIVE <= '0';
REN_ACTIVE <= '0';
SP1_ACTIVE <= '0';
Y <= Y + 1;
VS_N_PREV <= VS_N;
if VS_N_PREV = '1' and VS_N = '0' then
Y <= (others => '0');
V_VDS := ('0'&VSR(15 downto 8))+2;
V_VSW := ('0'&VSR( 4 downto 0))+1;
V_VDW := VDR(8 downto 0);
if V_VDW > 262 then
-- some games use 1FF value for VDW which overflows calculations below
-- thus limit it to max possible value.
V_VDW := std_logic_vector(to_unsigned(262,9));
end if;
--V_VCR := VDE(7 downto 0);
V_VDS := V_VDS + V_VSW;
V_VDE := V_VDS + V_VDW + 1;
-- Make sure display ends before vsync and there is at least 1 blank line at the end
-- possible Y values 0..262 (limited by ext VS_N)
if V_VDE > 262 then
V_VDE := std_logic_vector(to_unsigned(262,9));
end if;
Y_BGREN_START <= V_VDS;
Y_BGREN_END <= V_VDE;
Y_SP_START <= V_VDS - 1; -- SP1 state machine starts on line before BG REN
Y_SP_END <= V_VDE;
end if;
if Y = Y_BGREN_END-1 then
BURST <= '1';
if DCR(4) = '1' then -- Auto SATB DMA
DCR_DMAS_REQ <= '1';
end if;
end if;
if Y >= Y_SP_START-1 and Y < Y_SP_END-1 and SP_ON = '1' then
SP2_ACTIVE <= '1';
end if;
end if;
end if; -- CLKEN
end if;
end if;
end process;
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Background engine
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
BG_VS <= MWR(6);
BG_HS <= MWR(5 downto 4);
BG_DW <= MWR(1 downto 0);
-- BG_DW <= "11";
BG_CM <= MWR(7);
-- BG_CM <= '1';
--------------------------------------------------------------------------------
-- Background engine - Part 1
--------------------------------------------------------------------------------
process( CLK )
variable V_BG_Y : std_logic_vector(8 downto 0);
variable V_BG_X_TX : std_logic_vector(6 downto 0);
begin
if rising_edge(CLK) then
BG2_INI_REQ <= '0';
BG2_REQ <= '0';
if RESET_N = '0' then
BG1 <= BG1_INI;
BG_RAM_REQ_FF <= '0';
BG_BUSY <= '0';
--BG_BUSY2 <= '0';
else
case BG1 is
when BG1_INI =>
BG_BUSY <= '0';
--BG_BUSY2 <= '0';
if BG_ACTIVE = '1' then
BG_BUSY <= '1';
--BG_BUSY2 <= '1';
-- V_BG_Y := Y - Y_BGREN_START + BYR(8 downto 0);
V_BG_Y := YOFS;
if BG_VS = '0' then
BG_Y <= "0" & V_BG_Y(7 downto 0);
else
BG_Y <= V_BG_Y;
end if;
BG_TX <= XOFS(9 downto 3);
BG_PX <= X_REN_START - ("0000000" & XOFS(2 downto 0));
TX <= (others => '0');
if XOFS(2 downto 0) = "000" then
TX_MAX <= HDW;
else
TX_MAX <= HDW + 1;
end if;
BG2_INI_REQ <= '1';
BG_CYC <= "000";
BG1 <= BG1_INI_W;
end if;
when BG1_INI_W =>
if CLKEN = '1' then
case BG_DW is
when "00" =>
BG1 <= BG1_CPU;
when others =>
BG1 <= BG1_BAT;
end case;
end if;
when BG1_CPU =>
-- Allow one pending CPU VRAM request to be handled
BG_BUSY <= '0';
--BG_BUSY2 <= '0';
BG1 <= BG1_CPU_W;
when BG1_CPU_W =>
BG_BUSY <= '1';
if CLKEN = '1' then
BG_CYC <= BG_CYC + 1;
case BG_DW is
when "00" =>
--BG_BUSY2 <= '1';
case BG_CYC(2 downto 1) is
when "00" => BG1 <= BG1_BAT;
when "01" => BG1 <= BG1_NOP;
when "10" => BG1 <= BG1_CG0;
when "11" => BG1 <= BG1_CG1;
when others => null;
end case;
when others =>
if BG_CYC = "011" then
--BG_BUSY2 <= '1';
BG1 <= BG1_CG0;
end if;
end case;
end if;
when BG1_BAT =>
BG_P01 <= x"0000";
BG_P23 <= x"0000";
V_BG_X_TX := BG_TX + TX;
case BG_HS is
when "00" =>
BG_RAM_A_FF <= "00000" & BG_Y(8 downto 3) & V_BG_X_TX(4 downto 0);
when "01" =>
BG_RAM_A_FF <= "0000" & BG_Y(8 downto 3) & V_BG_X_TX(5 downto 0);
when others =>
BG_RAM_A_FF <= "000" & BG_Y(8 downto 3) & V_BG_X_TX(6 downto 0);
end case;
BG_RAM_REQ_FF <= not BG_RAM_REQ_FF;
BG1 <= BG1_BAT_W;
when BG1_BAT_W =>
if CLKEN = '1' and BG_RAM_REQ_FF = BG_RAM_ACK then
BG_PAL <= BG_RAM_DO(15 downto 12);
BG_RAM_A_FF <= BG_RAM_DO(11 downto 0) & "0" & BG_Y(2 downto 0);
BG_CYC <= BG_CYC + 1;
case BG_DW is
when "00" => BG1 <= BG1_CPU;
when "11" =>
if BG_CYC = "011" then
BG1 <= BG1_CG1; -- CG0/CG1
end if;
when others =>
if BG_CYC = "001" then
BG1 <= BG1_CPU;
end if;
end case;
end if;
when BG1_NOP =>
BG1 <= BG1_NOP_W;
when BG1_NOP_W =>
if CLKEN = '1' then
BG_CYC <= BG_CYC + 1;
BG1 <= BG1_CPU;
end if;
when BG1_CG0 =>
BG_RAM_REQ_FF <= not BG_RAM_REQ_FF;
BG1 <= BG1_CG0_W;
when BG1_CG0_W =>
if CLKEN = '1' and BG_RAM_REQ_FF = BG_RAM_ACK then
BG_P01 <= BG_RAM_DO;
BG_CYC <= BG_CYC + 1;
case BG_DW is
when "00" => BG1 <= BG1_CPU;
when "11" => BG1 <= BG1_CG1; -- Just in case
when others =>
if BG_CYC = "101" then
BG1 <= BG1_CG1;
end if;
end case;
end if;
when BG1_CG1 =>
BG_RAM_REQ_FF <= not BG_RAM_REQ_FF;
if BG_DW = "11" and BG_CM = '0' then
BG_RAM_A_FF(3) <= '0';
else
BG_RAM_A_FF(3) <= '1';
end if;
BG1 <= BG1_CG1_W;
when BG1_CG1_W =>
if CLKEN = '1' and BG_RAM_REQ_FF = BG_RAM_ACK then
if BG_DW = "11" and BG_CM = '0' then
BG_P01 <= BG_RAM_DO;
else
BG_P23 <= BG_RAM_DO;
end if;
BG_CYC <= BG_CYC + 1;
case BG_DW is
when "00" =>
TX <= TX + 1;
BG2_REQ <= '1';
if TX = TX_MAX then
BG1 <= BG1_END;
else
BG1 <= BG1_CPU;
end if;
when others =>
if BG_CYC = "111" then
TX <= TX + 1;
BG2_REQ <= '1';
if TX = TX_MAX then
BG1 <= BG1_END;
else
BG1 <= BG1_BAT;
end if;
end if;
end case;
end if;
when BG1_END =>
BG_BUSY <= '0';
--BG_BUSY2 <= '0';
if BG_ACTIVE = '0' then
BG1 <= BG1_INI;
end if;
when others => null;
end case;
end if;
end if;
end process;
--------------------------------------------------------------------------------
-- Background engine - Part 2
--------------------------------------------------------------------------------
process( CLK )
begin
if rising_edge(CLK) then
if RESET_N = '0' then
BG2_MEM_WE <= '0';
BG2 <= BG2_INI;
else
case BG2 is
when BG2_INI =>
BG2_MEM_WE <= '0';
if BG2_INI_REQ = '1' then
BG2_MEM_A <= BG_PX;
end if;
if BG2_REQ = '1' then
TPX <= "111";
BG2_PAL <= BG_PAL;
BG2_P1 <= BG_P01(15 downto 8);
BG2_P0 <= BG_P01(7 downto 0);
BG2_P3 <= BG_P23(15 downto 8);
BG2_P2 <= BG_P23(7 downto 0);
BG2 <= BG2_WRITE;
end if;
when BG2_WRITE =>
BG2_MEM_DI <= BG2_PAL
& BG2_P3(conv_integer(TPX))
& BG2_P2(conv_integer(TPX))
& BG2_P1(conv_integer(TPX))
& BG2_P0(conv_integer(TPX));
if (BG2_MEM_A >= X_REN_START) and (BG2_MEM_A < X_REN_END) then
BG2_MEM_WE <= '1';
end if;
BG2 <= BG2_LOOP;
when BG2_LOOP =>
BG2_MEM_WE <= '0';
TPX <= TPX - 1;
BG2_MEM_A <= BG2_MEM_A + 1;
if TPX = "000" then
BG2 <= BG2_INI;
else
BG2 <= BG2_WRITE;
end if;
when others => null;
end case;
end if;
end if;
end process;
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Sprite engine
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
SP_DW <= MWR(3 downto 2);
--------------------------------------------------------------------------------
-- Sprite engine - Part 1
--------------------------------------------------------------------------------
process( CLK )
variable V_SP_H : std_logic_vector(8 downto 0);
variable V_SP_NAME : std_logic_vector(9 downto 0);
variable V_Y_OFS : std_logic_vector(9 downto 0);
begin
if rising_edge(CLK) then
IRQ_OVF_SET <= '0';
if RESET_N = '0' then
SP1 <= SP1_INI;
SP_NB <= "0000000";
else
case SP1 is
when SP1_INI =>
if SP1_ACTIVE = '1' then
SP_NB <= "0000000";
SP_CUR_Y <= ("0" & Y) - ("0" & Y_SP_START) + 64;
SP_SAT_A <= "000000" & "11";
SP1_CNT <= "00";
SP1 <= SP1_RD0;
end if;
when SP1_RD0 =>
SP_SAT_A(1 downto 0) <= "00";
SP1_CNT <= SP1_CNT + 1;
if SP1_CNT = "11" then
SP1_CNT <= "00";
SP_Y <= SP_SAT_DO(9 downto 0);
SP1 <= SP1_RD3;
end if;
when SP1_RD3 =>
SP_SAT_A(1 downto 0) <= "11";
SP1_CNT <= SP1_CNT + 1;
if SP1_CNT = "11" then
SP1_CNT <= "00";
SP_VF <= SP_SAT_DO(15);
SP_CGY <= SP_SAT_DO(13 downto 12);
SP_HF <= SP_SAT_DO(11);
SP_CGX <= SP_SAT_DO(8);
SP_PRI <= SP_SAT_DO(7);
SP_PAL <= SP_SAT_DO(3 downto 0);
SP1 <= SP1_TST;
end if;
when SP1_TST =>
case SP_CGY is
when "00" =>
V_SP_H := "000010000";
when "01" =>
V_SP_H := "000100000";
when others =>
V_SP_H := "001000000";
end case;
if ( SP_CUR_Y >= SP_Y) and ( SP_CUR_Y < SP_Y + V_SP_H) then
if (SP_NB = "0010000" and SP64 = '0') or SP_NB = "1000000" then
SP_NB <= "1111111"; -- Overflow
if CR(1) = '1' then
IRQ_OVF_SET <= '1';
end if;
SP1 <= SP1_END;
else
SP1 <= SP1_RD1;
end if;
else
SP1 <= SP1_LOOP;
end if;
when SP1_RD1 =>
SP_SAT_A(1 downto 0) <= "01";
SP1_CNT <= SP1_CNT + 1;
if SP1_CNT = "11" then