-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_vicII_656x.vhd
1497 lines (1394 loc) · 43.6 KB
/
video_vicII_656x.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
-- -----------------------------------------------------------------------
--
-- FPGA 64
--
-- A fully functional commodore 64 implementation in a single FPGA
--
-- -----------------------------------------------------------------------
-- Peter Wendrich ([email protected])
-- http://www.syntiac.com/fpga64.html
-- -----------------------------------------------------------------------
--
-- VIC-II - Video Interface Chip no 2
--
-- -----------------------------------------------------------------------
-- Dar 08/03/2014 : shift hsync to sprite #3
-- -----------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.ALL;
entity video_vicii_656x is
generic (
registeredAddress : boolean;
emulateRefresh : boolean := false;
emulateLightpen : boolean := false;
emulateGraphics : boolean := true
);
port (
clk: in std_logic;
-- phi = 0 is VIC cycle
-- phi = 1 is CPU cycle (only used by VIC when BA is low)
phi : in std_logic;
enaData : in std_logic;
enaPixel : in std_logic;
baSync : in std_logic;
ba: out std_logic;
mode6569 : in std_logic; -- PAL 63 cycles and 312 lines
mode6567old : in std_logic; -- old NTSC 64 cycles and 262 line
mode6567R8 : in std_logic; -- new NTSC 65 cycles and 263 line
mode6572 : in std_logic; -- PAL-N 65 cycles and 312 lines
reset : in std_logic;
cs : in std_logic;
we : in std_logic;
rd : in std_logic;
lp_n : in std_logic;
aRegisters: in unsigned(5 downto 0);
diRegisters: in unsigned(7 downto 0);
di: in unsigned(7 downto 0);
diColor: in unsigned(3 downto 0);
do: out unsigned(7 downto 0);
vicAddr: out unsigned(13 downto 0);
irq_n: out std_logic;
-- Video output
hSync : out std_logic;
vSync : out std_logic;
colorIndex : out unsigned(3 downto 0);
-- Debug outputs
debugX : out unsigned(9 downto 0);
debugY : out unsigned(8 downto 0);
vicRefresh : out std_logic;
addrValid : out std_logic
);
end entity;
architecture rtl of video_vicii_656x is
type vicCycles is (
cycleRefresh1, cycleRefresh2, cycleRefresh3, cycleRefresh4, cycleRefresh5,
cycleIdle1,
cycleChar,
cycleCalcSprites, cycleSpriteBa1, cycleSpriteBa2, cycleSpriteBa3,
cycleSpriteA, cycleSpriteB
);
subtype ColorDef is unsigned(3 downto 0);
type MFlags is array(0 to 7) of boolean;
type MXdef is array(0 to 7) of unsigned(8 downto 0);
type MYdef is array(0 to 7) of unsigned(7 downto 0);
type MCntDef is array(0 to 7) of unsigned(5 downto 0);
type MPixelsDef is array(0 to 7) of unsigned(23 downto 0);
type MCurrentPixelDef is array(0 to 7) of unsigned(1 downto 0);
type charStoreDef is array(38 downto 0) of unsigned(11 downto 0);
type spriteColorsDef is array(7 downto 0) of unsigned(3 downto 0);
type pixelColorStoreDef is array(7 downto 0) of unsigned(3 downto 0);
-- State machine
signal lastLineFlag : boolean; -- True for on last line of the frame.
signal beyondFrameFlag : boolean; -- Y>frame lines
signal vicCycle : vicCycles := cycleRefresh1;
signal sprite : unsigned(2 downto 0) := "000";
signal shiftChars : boolean;
signal idle: std_logic := '1';
signal rasterIrqDone : std_logic; -- Only one interrupt each rasterLine
signal rasterEnable: std_logic;
-- BA signal
signal badLine : boolean; -- true if we have a badline condition
signal baLoc : std_logic;
signal baCnt : unsigned(2 downto 0);
signal baChars : std_logic;
signal baSprite04 : std_logic;
signal baSprite15 : std_logic;
signal baSprite26 : std_logic;
signal baSprite37 : std_logic;
-- Memory refresh cycles
signal refreshCounter : unsigned(7 downto 0);
-- User registers
signal MX : MXdef; -- Sprite X
signal MY : MYdef; -- Sprite Y
signal ME : unsigned(7 downto 0); -- Sprite enable
signal MXE : unsigned(7 downto 0); -- Sprite X expansion
signal MYE : unsigned(7 downto 0); -- Sprite Y expansion
signal MPRIO : unsigned(7 downto 0); -- Sprite priority
signal MC : unsigned(7 downto 0); -- sprite multi color
-- !!! Krestage 3 hacks
signal MCDelay : unsigned(7 downto 0); -- sprite multi color
-- mode
signal BMM: std_logic; -- Bitmap mode
signal ECM: std_logic; -- Extended color mode
signal MCM: std_logic; -- Multi color mode
signal DEN: std_logic; -- DMA enable
signal RSEL: std_logic; -- Visible rows selection (24/25)
signal CSEL: std_logic; -- Visible columns selection (38/40)
signal RES: std_logic;
signal VM: unsigned(13 downto 10);
signal CB: unsigned(13 downto 11);
signal EC : ColorDef; -- border color
signal B0C : ColorDef; -- background color 0
signal B1C : ColorDef; -- background color 1
signal B2C : ColorDef; -- background color 2
signal B3C : ColorDef; -- background color 3
signal MM0 : ColorDef; -- sprite multicolor 0
signal MM1 : ColorDef; -- sprite multicolor 1
signal spriteColors: spriteColorsDef;
-- borders and blanking
signal LRBorder: std_logic;
signal TBBorder: std_logic;
signal hBlack: std_logic;
signal vBlanking : std_logic;
signal hBlanking : std_logic;
signal xscroll: unsigned(2 downto 0);
signal yscroll: unsigned(2 downto 0);
signal rasterCmp : unsigned(8 downto 0);
-- Address generator
signal vicAddrReg : unsigned(13 downto 0);
signal vicAddrLoc : unsigned(13 downto 0);
-- Address counters
signal ColCounter: unsigned(9 downto 0) := (others => '0');
signal ColRestart: unsigned(9 downto 0) := (others => '0');
signal RowCounter: unsigned(2 downto 0) := (others => '0');
-- IRQ Registers
signal IRST: std_logic := '0';
signal ERST: std_logic := '0';
signal IMBC: std_logic := '0';
signal EMBC: std_logic := '0';
signal IMMC: std_logic := '0';
signal EMMC: std_logic := '0';
signal ILP: std_logic := '0';
signal ELP: std_logic := '0';
signal IRQ: std_logic;
-- Collision detection registers
signal M2M: unsigned(7 downto 0); -- Sprite to sprite collision
signal M2D: unsigned(7 downto 0); -- Sprite to character collision
signal M2Mhit : std_logic;
signal M2Dhit : std_logic;
-- Raster counters
signal rasterX : unsigned(9 downto 0) := (others => '0');
signal rasterY : unsigned(8 downto 0) := (others => '0');
-- Light pen
signal lightPenHit: std_logic;
signal lpX : unsigned(7 downto 0);
signal lpY : unsigned(7 downto 0);
-- IRQ Resets
signal resetLightPenIrq: std_logic;
signal resetIMMC : std_logic;
signal resetIMBC : std_logic;
signal resetRasterIrq : std_logic;
-- Character generation
signal charStore: charStoreDef;
signal nextChar : unsigned(11 downto 0);
-- Char/Pixels just coming from memory
signal readChar : unsigned(11 downto 0);
signal readPixels : unsigned(7 downto 0);
-- Char/Pixels pair waiting to be shifted
signal waitingChar : unsigned(11 downto 0);
signal waitingPixels : unsigned(7 downto 0);
-- Stores colorinfo and the Pixels that are currently in shift register
signal shiftingChar : unsigned(11 downto 0);
signal shiftingPixels : unsigned(7 downto 0);
signal shifting_ff : std_logic; -- Multicolor shift-regiter status bit.
-- Sprite work registers
signal MPtr : unsigned(7 downto 0); -- sprite base pointer
signal MPixels : MPixelsDef; -- Sprite 24 bit shift register
signal MActive : MFlags; -- Sprite is active (derived from MCnt)
signal MCnt : MCntDef;
signal MXE_ff : unsigned(7 downto 0); -- Sprite X expansion flipflop
signal MYE_ff : unsigned(7 downto 0); -- Sprite Y expansion flipflop
signal MC_ff : unsigned(7 downto 0); -- controls sprite shift-register in multicolor
signal MShift : MFlags; -- Sprite is shifting
signal MCurrentPixel : MCurrentPixelDef;
-- Current colors and pixels
signal pixelColor: ColorDef;
signal pixelBgFlag: std_logic; -- For collision detection
signal pixelDelay: pixelColorStoreDef;
-- Read/Write lines
signal myWr : std_logic;
signal myRd : std_logic;
begin
-- -----------------------------------------------------------------------
-- Ouput signals
-- -----------------------------------------------------------------------
ba <= baLoc;
vicAddr <= vicAddrReg when registeredAddress else vicAddrLoc;
hSync <= hBlanking;
vSync <= vBlanking;
irq_n <= not IRQ;
-- -----------------------------------------------------------------------
-- chip-select signals
-- -----------------------------------------------------------------------
myWr <= cs and we;
myRd <= cs and rd;
-- -----------------------------------------------------------------------
-- debug signals
-- -----------------------------------------------------------------------
debugX <= rasterX;
debugY <= rasterY;
-- -----------------------------------------------------------------------
-- Badline condition
-- -----------------------------------------------------------------------
process(rasterY, yscroll, rasterEnable)
begin
badLine <= false;
if (rasterY(2 downto 0) = yscroll)
and (rasterEnable = '1') then
badLine <= true;
end if;
end process;
-- -----------------------------------------------------------------------
-- BA=low counter
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if baLoc = '0' then
if phi = '0'
and enaData = '1'
and baCnt(2) = '0' then
baCnt <= baCnt + 1;
end if;
else
baCnt <= (others => '0');
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Calculate lastLineFlag
-- -----------------------------------------------------------------------
process(clk)
variable rasterLines : integer range 0 to 312;
begin
if rising_edge(clk) then
lastLineFlag <= false;
rasterLines := 311; -- PAL
if mode6567old = '1' then
rasterLines := 261; -- NTSC (R7 and earlier have 262 lines)
end if;
if mode6567R8 = '1' then
rasterLines := 262; -- NTSC (R8 and newer have 263 lines)
end if;
if rasterY = rasterLines then
lastLineFlag <= true;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- State machine
-- -----------------------------------------------------------------------
vicStateMachine: process(clk)
begin
if rising_edge(clk) then
if enaData = '1'
and baSync = '0' then
if phi = '0' then
case vicCycle is
when cycleRefresh1 =>
vicCycle <= cycleRefresh2;
if ((mode6567old or mode6567R8) = '1') then
vicCycle <= cycleIdle1;
end if;
when cycleIdle1 => vicCycle <= cycleRefresh2;
when cycleRefresh2 => vicCycle <= cycleRefresh3;
when cycleRefresh3 => vicCycle <= cycleRefresh4;
when cycleRefresh4 => vicCycle <= cycleRefresh5; -- X=0..7 on this cycle
when cycleRefresh5 => vicCycle <= cycleChar;
when cycleChar =>
if ((mode6569 = '1') and rasterX(9 downto 3) = "0100111") -- PAL
or ((mode6567old = '1') and rasterX(9 downto 3) = "0100111") -- Old NTSC
or ((mode6567R8 = '1') and rasterX(9 downto 3) = "0101000") -- New NTSC
or ((mode6572 = '1') and rasterX(9 downto 3) = "0101000") then -- PAL-N
vicCycle <= cycleCalcSprites;
end if;
when cycleCalcSprites => vicCycle <= cycleSpriteBa1;
when cycleSpriteBa1 => vicCycle <= cycleSpriteBa2;
when cycleSpriteBa2 => vicCycle <= cycleSpriteBa3;
when others =>
null;
end case;
else
case vicCycle is
when cycleSpriteBa3 => vicCycle <= cycleSpriteA;
when cycleSpriteA =>
vicCycle <= cycleSpriteB;
when cycleSpriteB =>
vicCycle <= cycleSpriteA;
if sprite = 7 then
vicCycle <= cycleRefresh1;
end if;
when others =>
null;
end case;
end if;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Iterate through all sprites.
-- Only used when state-machine above is in any sprite cycles.
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if phi = '1'
and enaData = '1'
and vicCycle = cycleSpriteB
and baSync = '0' then
sprite <= sprite + 1;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Address generator
-- -----------------------------------------------------------------------
process(phi, vicCycle, sprite, shiftChars, idle,
VM, CB, ECM, BMM, nextChar, colCounter, rowCounter, MPtr, MCnt)
begin
--
-- Default case ($3FFF fetches)
vicAddrLoc <= (others => '1');
if (idle = '0')
and shiftChars then
if BMM = '1' then
vicAddrLoc <= CB(13) & colCounter & rowCounter;
else
vicAddrLoc <= CB & nextChar(7 downto 0) & rowCounter;
end if;
end if;
if ECM = '1' then
vicAddrLoc(10 downto 9) <= "00";
end if;
case vicCycle is
when cycleRefresh1 | cycleRefresh2 | cycleRefresh3 | cycleRefresh4 | cycleRefresh5 =>
if emulateRefresh then
vicAddrLoc <= "111111" & refreshCounter;
else
vicAddrLoc <= (others => '-');
end if;
when cycleSpriteBa1 | cycleSpriteBa2 | cycleSpriteBa3 =>
vicAddrLoc <= (others => '1');
when cycleSpriteA =>
vicAddrLoc <= VM & "1111111" & sprite;
if phi = '1' then
vicAddrLoc <= MPtr & MCnt(to_integer(sprite));
end if;
when cycleSpriteB =>
vicAddrLoc <= MPtr & MCnt(to_integer(sprite));
when others =>
if phi = '1' then
vicAddrLoc <= VM & colCounter;
end if;
end case;
end process;
-- Registered address
process(clk)
begin
if rising_edge(clk) then
vicAddrReg <= vicAddrLoc;
end if;
end process;
-- -----------------------------------------------------------------------
-- Character storage
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if enaData = '1'
and shiftChars
and phi = '1' then
if badLine then
nextChar(7 downto 0) <= di;
nextChar(11 downto 8) <= diColor;
else
nextChar <= charStore(38);
end if;
charStore <= charStore(37 downto 0) & nextChar;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Sprite base pointer (MPtr)
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if phi = '0'
and enaData = '1'
and vicCycle = cycleSpriteA then
MPtr <= (others => '1');
if MActive(to_integer(sprite)) then
MPtr <= di;
end if;
-- If refresh counter is not emulated we don't care about
-- MPtr having the correct value in idle state.
if not emulateRefresh then
MPtr <= di;
end if;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Refresh counter
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
vicRefresh <= '0';
case vicCycle is
when cycleRefresh1 | cycleRefresh2 | cycleRefresh3 | cycleRefresh4 | cycleRefresh5 =>
vicRefresh <= '1';
if phi = '0'
and enaData = '1'
and baSync = '0' then
refreshCounter <= refreshCounter - 1;
end if;
when others =>
null;
end case;
if lastLineFlag then
refreshCounter <= (others => '1');
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Generate Raster Enable
-- -----------------------------------------------------------------------
process(clk)
begin
-- Enable screen and character display.
-- This is only possible in line 48 on the VIC-II.
-- On other lines any DEN changes are ignored.
if rising_edge(clk) then
if (rasterY = 48) and (DEN = '1') then
rasterEnable <= '1';
end if;
if (rasterY = 248) then
rasterEnable <= '0';
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- BA generator (Text/Bitmap)
-- -----------------------------------------------------------------------
--
-- For Text/Bitmap BA goes low 3 cycles before real access. So BA starts
-- going low during refresh2 state. See diagram below for timing:
--
-- X 0 0 0 0 0
-- 0 0 0 0 1
-- 0 4 8 C 0
--
-- phi ___ ___ ___ ___ ___ ___ ___ ___...
-- ___ ___ ___ ___ ___ ___ ___ ...
--
-- | | | | | | |...
-- rfr2 rfr3 rfr4 rfr5 char1 char2 char3
--
-- BA _______
-- \\\_______________________________________
-- | 1 | 2 | 3 |
--
-- BACnt 000 001 | 010 | 011 | 100 100 100 ...
--
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if phi = '0' then
baChars <= '1';
case vicCycle is
when cycleRefresh2 | cycleRefresh3 | cycleRefresh4 | cycleRefresh5 =>
if badLine then
baChars <= '0';
end if;
when others =>
if rasterX(9 downto 3) < "0101000"
and badLine then
baChars <= '0';
end if;
end case;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- BA generator (Sprites)
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if phi = '0' then
if sprite = 1 then
baSprite04 <= '1';
end if;
if sprite = 2 then
baSprite15 <= '1';
end if;
if sprite = 3 then
baSprite26 <= '1';
end if;
if sprite = 4 then
baSprite37 <= '1';
end if;
if sprite = 5 then
baSprite04 <= '1';
end if;
if sprite = 6 then
baSprite15 <= '1';
end if;
if sprite = 7 then
baSprite26 <= '1';
end if;
if vicCycle = cycleRefresh1 then
baSprite37 <= '1';
end if;
if MActive(0) and (vicCycle = cycleCalcSprites) then
baSprite04 <= '0';
end if;
if MActive(1) and (vicCycle = cycleSpriteBa2) then
baSprite15 <= '0';
end if;
if MActive(2) and (vicCycle = cycleSpriteB) and (sprite = 0) then
baSprite26 <= '0';
end if;
if MActive(3) and (vicCycle = cycleSpriteB) and (sprite = 1) then
baSprite37 <= '0';
end if;
if MActive(4) and (vicCycle = cycleSpriteB) and (sprite = 2) then
baSprite04 <= '0';
end if;
if MActive(5) and (vicCycle = cycleSpriteB) and (sprite = 3) then
baSprite15 <= '0';
end if;
if MActive(6) and (vicCycle = cycleSpriteB) and (sprite = 4) then
baSprite26 <= '0';
end if;
if MActive(7) and (vicCycle = cycleSpriteB) and (sprite = 5) then
baSprite37 <= '0';
end if;
end if;
end if;
end process;
baLoc <= baChars and baSprite04 and baSprite15 and baSprite26 and baSprite37;
-- -----------------------------------------------------------------------
-- Address valid?
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
addrValid <= '0';
if phi = '0'
or baCnt(2) = '1' then
addrValid <= '1';
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Generate ShiftChars flag
-- -----------------------------------------------------------------------
process(rasterX)
begin
shiftChars <= false;
if rasterX(9 downto 3) > "0000000"
and rasterX(9 downto 3) < "0101001" then
shiftChars <= true;
end if;
end process;
-- -----------------------------------------------------------------------
-- RowCounter and ColCounter
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if phi = '0'
and enaData = '1'
and baSync = '0' then
if shiftChars
and idle = '0' then
colCounter <= colCounter + 1;
end if;
case vicCycle is
when cycleRefresh4 =>
colCounter <= colRestart;
if badline then
rowCounter <= (others => '0');
end if;
when cycleSpriteA =>
if sprite = "000" then
if rowCounter = 7 then
colRestart <= colCounter;
idle <= '1';
else
rowCounter <= rowCounter + 1;
end if;
if badline then
rowCounter <= rowCounter + 1;
end if;
end if;
when others =>
null;
end case;
if lastLineFlag then
-- Reset column counter outside visible range.
colRestart <= (others => '0');
end if;
-- Set display mode (leave idle-mode) as soon as
-- there is a badline condition.
if badline then
idle <= '0';
end if;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- X/Y Raster counter
-- -----------------------------------------------------------------------
rasterCounters: process(clk)
begin
if rising_edge(clk) then
if enaPixel = '1' then
rasterX(2 downto 0) <= rasterX(2 downto 0) + 1;
end if;
if phi = '0'
and enaData = '1'
and baSync = '0' then
rasterX(9 downto 3) <= rasterX(9 downto 3) + 1;
rasterX(2 downto 0) <= (others => '0');
if vicCycle = cycleRefresh4 then
rasterX <= (others => '0');
end if;
end if;
if phi = '1'
and enaData = '1'
and baSync = '0' then
beyondFrameFlag <= false;
if (vicCycle = cycleSpriteB)
and (sprite = 2) then
rasterY <= rasterY + 1;
beyondFrameFlag <= lastLineFlag;
end if;
if beyondFrameFlag then
rasterY <= (others => '0');
end if;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Raster IRQ
-- -----------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if phi = '1'
and enaData = '1'
and baSync = '0'
and (vicCycle = cycleSpriteB)
and (sprite = 2) then
rasterIrqDone <= '0';
end if;
if resetRasterIrq = '1' then
IRST <= '0';
end if;
if (rasterIrqDone = '0')
and (rasterY = rasterCmp) then
rasterIrqDone <= '1';
IRST <= '1';
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Light pen
-- -----------------------------------------------------------------------
-- On a negative edge on the LP input, the current position of the raster beam
-- is latched in the registers LPX ($d013) and LPY ($d014). LPX contains the
-- upper 8 bits (of 9) of the X position and LPY the lower 8 bits (likewise of
-- 9) of the Y position. So the horizontal resolution of the light pen is
-- limited to 2 pixels.
-- Only one negative edge on LP is recognized per frame. If multiple edges
-- occur on LP, all following ones are ignored. The trigger is not released
-- until the next vertical blanking interval.
-- -----------------------------------------------------------------------
lightPen: process(clk)
begin
if rising_edge(clk) then
if emulateLightpen then
if resetLightPenIrq = '1' then
-- Reset light pen interrupt
ILP <= '0';
end if;
if lastLineFlag then
-- Reset lightpen state at beginning of frame
lightPenHit <= '0';
elsif (lightPenHit = '0') and (lp_n = '0') then
-- One hit/frame
lightPenHit <= '1';
-- Toggle Interrupt
ILP <= '1';
-- Store position of beam
lpx <= rasterX(8 downto 1);
lpy <= rasterY(7 downto 0);
end if;
else
ILP <= '0';
lpx <= (others => '1');
lpy <= (others => '1');
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- VSync
-- -----------------------------------------------------------------------
doVBlanking: process(clk, mode6569, mode6567old, mode6567R8)
variable rasterBlank : integer range 0 to 300;
begin
rasterBlank := 300;
if (mode6567old or mode6567R8) = '1' then
rasterBlank := 12;
end if;
if rising_edge(clk) then
vBlanking <= '0';
if rasterY = rasterBlank then
vBlanking <= '1';
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- HSync
-- -----------------------------------------------------------------------
doHBlanking: process(clk)
begin
if rising_edge(clk) then
if sprite = 3 then
hBlack <= '1';
end if;
if vicCycle = cycleRefresh1 then
hBlack <= '0';
end if;
if sprite = 3 then -- dar 5 then
hBlanking <= '1';
else
hBlanking <= '0';
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Borders
-- -----------------------------------------------------------------------
calcBorders: process(clk)
variable newTBBorder: std_logic;
begin
if rising_edge(clk) then
if enaPixel = '1' then
--
-- Calc top/bottom border
newTBBorder := TBBorder;
-- if (rasterY = 55) and (RSEL = '0') and (rasterEnable = '1') then
if (rasterY = 55) and (rasterEnable = '1') then
newTBBorder := '0';
end if;
if (rasterY = 51) and (RSEL = '1') and (rasterEnable = '1') then
newTBBorder := '0';
end if;
if (rasterY = 247) and (RSEL = '0') then
newTBBorder := '1';
end if;
if (rasterY = 251) and (RSEL = '1') then
newTBBorder := '1';
end if;
--
-- Calc left/right border
if (rasterX = (31+1)) and (CSEL = '0') then
LRBorder <= newTBBorder;
TBBorder <= newTBBorder;
end if;
if (rasterX = (24+1)) and (CSEL = '1') then
LRBorder <= newTBBorder;
TBBorder <= newTBBorder;
end if;
if (rasterX = (335+1)) and (CSEL = '0') then
LRBorder <= '1';
end if;
if (rasterX = (344+1)) and (CSEL = '1') then
LRBorder <= '1';
end if;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Pixel generator for Text/Bitmap screen
-- -----------------------------------------------------------------------
calcBitmap: process(clk)
variable multiColor : std_logic;
begin
if rising_edge(clk) then
if enaPixel = '1' then
--
-- Toggle flipflop for multicolor 2-bits shift.
shifting_ff <= not shifting_ff;
--
-- Multicolor mode is active with MCM, but for character
-- mode it depends on bit3 of color ram too.
multiColor := MCM and (BMM or ECM or shiftingChar(11));
--
-- Reload shift register when xscroll=rasterX
-- otherwise shift pixels
if xscroll = rasterX(2 downto 0) then
shifting_ff <= '0';
shiftingChar <= waitingChar;
shiftingPixels <= waitingPixels;
elsif multiColor = '0' then
shiftingPixels <= shiftingPixels(6 downto 0) & '0';
elsif shifting_ff = '1' then
shiftingPixels <= shiftingPixels(5 downto 0) & "00";
end if;
--
-- Calculate if pixel is in foreground or background
pixelBgFlag <= shiftingPixels(7);
--
-- Calculate color of next pixel
pixelColor <= B0C;
if (BMM = '0') and (ECM='0') then
if (multiColor = '0') then
-- normal character mode
if shiftingPixels(7) = '1' then
pixelColor <= shiftingChar(11 downto 8);
end if;
else
-- multi-color character mode
case shiftingPixels(7 downto 6) is
when "01" => pixelColor <= B1C;
when "10" => pixelColor <= B2C;
when "11" => pixelColor <= '0' & shiftingChar(10 downto 8);
when others => null;
end case;
end if;
elsif (MCM = '0') and (BMM = '0') and (ECM='1') then
-- extended-color character mode
-- multiple background colors but only 64 characters
if shiftingPixels(7) = '1' then
pixelColor <= shiftingChar(11 downto 8);
else
case shiftingChar(7 downto 6) is
when "01" => pixelColor <= B1C;
when "10" => pixelColor <= B2C;
when "11" => pixelColor <= B3C;
when others => null;
end case;
end if;
elsif emulateGraphics and (MCM = '0') and (BMM = '1') and (ECM='0') then
-- highres bitmap mode
if shiftingPixels(7) = '1' then
pixelColor <= shiftingChar(7 downto 4);
else
pixelColor <= shiftingChar(3 downto 0);
end if;
elsif emulateGraphics and (MCM = '1') and (BMM = '1') and (ECM='0') then
-- Multi-color bitmap mode
case shiftingPixels(7 downto 6) is
when "01" => pixelColor <= shiftingChar(7 downto 4);
when "10" => pixelColor <= shiftingChar(3 downto 0);
when "11" => pixelColor <= shiftingChar(11 downto 8);
when others => null;
end case;
else
-- illegal display mode, the output is black
pixelColor <= "0000";
end if;
end if;
--
-- Store fetched pixels, until current pixels are displayed
-- and shift-register is empty.
if enaData = '1'
and phi = '0' then
readPixels <= (others => '0');
if shiftChars then
readPixels <= di;
readChar <= (others => '0');
if idle = '0' then
readChar <= nextChar;
end if;
end if;
-- Store the characters until shiftregister is empty
waitingPixels <= readPixels;
waitingChar <= readChar;
end if;
end if;
end process;
-- -----------------------------------------------------------------------
-- Which sprites are active?
-- -----------------------------------------------------------------------
process(MCnt)
begin
for i in 0 to 7 loop
MActive(i) <= false;
if MCnt(i) /= 63 then
MActive(i) <= true;
end if;
end loop;
end process;
-- -----------------------------------------------------------------------
-- Sprite byte counter
-- Y expansion flipflop
-- -----------------------------------------------------------------------