-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.v
1736 lines (1493 loc) · 48.2 KB
/
video.v
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
/**
* Team Arcade (1942)
*
* Video Infrastructure
*/
// state defines
`define FG_PIPELINE_IDLE 0
`define FG_PIPELINE_FETCH_TILE_FROM_RAM_1 1
`define FG_PIPELINE_FETCH_TILE_FROM_RAM_2 2
`define FG_PIPELINE_FETCH_TILE_FROM_ROM_1 3
`define FG_PIPELINE_FETCH_TILE_FROM_ROM_2 4
`define BG_PIPELINE_IDLE 0
`define BG_PIPELINE_FETCH_TILE_FROM_RAM_1 1
`define BG_PIPELINE_FETCH_TILE_FROM_RAM_2 2
`define BG_PIPELINE_FETCH_TILE_FROM_ROM 3
`define SPRITE_PIPELINE_IDLE 0
`define SPRITE_PIPELINE_FETCH_RAM_1 1
`define SPRITE_PIPELINE_FETCH_RAM_2 2
`define SPRITE_PIPELINE_FETCH_RAM_3 3
`define SPRITE_PIPELINE_FETCH_RAM_4 4
`define SPRITE_PIPELINE_EVALUATE 5
`define SPRITE_PIPELINE_FETCH_ROM_1 6
`define SPRITE_PIPELINE_FETCH_ROM_2 7
`define SPRITE_PIPELINE_FETCH_ROM_3 8
`define SPRITE_PIPELINE_FETCH_ROM_4 9
`define SPRITE_PIPELINE_COMMIT 10
`default_nettype none
/**
video controller
"top" module for video controller
interfaces with ROMS, RAM, and VGA
timing control for rendering pipelines
*/
module video_controller(
// RAM
input wire [7:0] fgvideoram_read_data,
input wire [7:0] bgvideoram_read_data,
input wire [7:0] spriteram_read_data,
input wire [1:0] palette_bank_read_data,
input wire [15:0] scroll_read_data,
output wire [15:0] fgvideoram_read_addr,
output wire [15:0] bgvideoram_read_addr,
output wire [15:0] spriteram_read_addr,
// ROM
input wire [7:0] gfx1_read_data,
input wire [7:0] gfx2_bitplane_1_read_data,
input wire [7:0] gfx2_bitplane_2_read_data,
input wire [7:0] gfx2_bitplane_3_read_data,
input wire [7:0] gfx3_1_read_data,
input wire [7:0] gfx3_2_read_data,
input wire [15:0] palette_read_data,
output wire [15:0] gfx1_read_addr,
output wire [15:0] gfx2_bitplane_1_read_addr,
output wire [15:0] gfx2_bitplane_2_read_addr,
output wire [15:0] gfx2_bitplane_3_read_addr,
output wire [15:0] gfx3_1_read_addr,
output wire [15:0] gfx3_2_read_addr,
output wire [15:0] palette_read_addr,
// VGA controller
input wire [8:0] scanline,
input wire [9:0] column,
output wire [3:0] rgb_r,
output wire [3:0] rgb_b,
output wire [3:0] rgb_g,
input wire video_clk, rst_b,
input wire [7:0] dip
);
reg pipeline_start;
reg [7:0] effective_scanline;
reg [8:0] effective_column;
reg bg_pipeline_start;
reg [7:0] bg_effective_scanline;
reg [8:0] bg_effective_column;
wire [7:0] fg_linebuffer_color;
wire fg_linebuffer_transparent;
wire [9:0] bg_linebuffer_color;
wire bg_linebuffer_transparent;
wire [2:0] linebuffer_bit;
reg render_pipeline;
wire [7:0] sprite_linebuffer_color;
reg [7:0] sprite_effective_column;
reg [7:0] sprite_effective_scanline;
wire sprite_linebuffer_color_transparent;
reg sprite_flop_linebuffers;
reg sprite_linebuffers_clr;
reg sprite_pipeline_start;
wire [9:0] orig_bg_linebuffer_color;
reg load_bg_scanline_buffer;
wire [2:0] bg_linebuffer_bit;
reg [9:0] out_effective_column;
// fg linebuffer pipeline
fg_linebuffer_pipeline fg_pipe(
// inputs
.scanline(effective_scanline),
.column(effective_column[7:0]),
.pipeline_start(pipeline_start),
.fgvideoram_read_data(fgvideoram_read_data),
.gfx1_read_data(gfx1_read_data),
.linebuffer_bit(linebuffer_bit),
.video_clk(video_clk),
.rst_b(rst_b),
// outputs
.gfx1_read_addr(gfx1_read_addr),
.fg_linebuffer_color(fg_linebuffer_color),
.fgvideoram_read_addr(fgvideoram_read_addr),
.fg_linebuffer_color_transparent(fg_linebuffer_transparent)
);
// Scrolling logic
bg_scroller bg_scroll(
// inputs
.scanline(effective_scanline),
.column(out_effective_column),
.bg_column(bg_effective_column),
.scroll_read_data(scroll_read_data),
.video_clk(video_clk),
.rst_b(rst_b),
.orig_bg_linebuffer_color(orig_bg_linebuffer_color),
.load_bg_scanline_buffer(load_bg_scanline_buffer),
//outputs
.bg_linebuffer_bit(bg_linebuffer_bit),
.new_bg_linebuffer_color(bg_linebuffer_color)
);
// bg linebuffer pipeline
bg_linebuffer_pipeline bg_pipe(
// inputs
.scanline(bg_effective_scanline),
.column(bg_effective_column),
.pipeline_start(bg_pipeline_start),
.scroll_read_data(scroll_read_data),
.bgvideoram_read_data(bgvideoram_read_data),
.gfx2_bitplane_1_read_data(gfx2_bitplane_1_read_data),
.gfx2_bitplane_2_read_data(gfx2_bitplane_2_read_data),
.gfx2_bitplane_3_read_data(gfx2_bitplane_3_read_data),
.linebuffer_bit(bg_linebuffer_bit),
.palette_bank_read_data(palette_bank_read_data),
.video_clk(video_clk),
.rst_b(rst_b),
// outputs
.gfx2_bitplane_1_read_addr(gfx2_bitplane_1_read_addr),
.gfx2_bitplane_2_read_addr(gfx2_bitplane_2_read_addr),
.gfx2_bitplane_3_read_addr(gfx2_bitplane_3_read_addr),
.bg_linebuffer_color(orig_bg_linebuffer_color),
.bgvideoram_read_addr(bgvideoram_read_addr)
);
// sprite linebuffer pipeline
sprite_linebuffer_pipeline sprite_pipe(
// inputs
.scanline(sprite_effective_scanline),
.column(sprite_effective_column),
.sprite_pipeline_start(sprite_pipeline_start),
.linebuffers_clr(sprite_linebuffers_clr),
.flop_linebuffers(sprite_flop_linebuffers),
.gfx3_1_read_data(gfx3_1_read_data),
.gfx3_2_read_data(gfx3_2_read_data),
.spriteram_read_data(spriteram_read_data),
.video_clk(video_clk), .rst_b(rst_b),
// outputs
.sprite_linebuffer_color(sprite_linebuffer_color),
.sprite_linebuffer_color_transparent(sprite_linebuffer_color_transparent),
.spriteram_addr(spriteram_read_addr),
.gfx3_1_read_addr(gfx3_1_read_addr),
.gfx3_2_read_addr(gfx3_2_read_addr)
);
// renderer
renderer_pipeline renderer(
// inputs
.render_pipeline(render_pipeline),
.fg_linebuffer_color(fg_linebuffer_color),
.fg_linebuffer_color_transparent(fg_linebuffer_transparent),
.bg_linebuffer_color(bg_linebuffer_color),
.sprite_linebuffer_color(sprite_linebuffer_color),
.sprite_linebuffer_color_transparent(sprite_linebuffer_color_transparent),
.palette_data(palette_read_data[11:0]),
.video_clk(video_clk),
.rst_b(rst_b),
.dip(dip),
// outputs
.palette_addr(palette_read_addr),
.linebuffer_bit(linebuffer_bit),
.rgb_r(rgb_r),
.rgb_b(rgb_b),
.rgb_g(rgb_g)
);
// timing control
always @(*) begin
// defaults
sprite_effective_scanline = 0;
effective_scanline = 0;
effective_column = 0;
pipeline_start = 0;
sprite_pipeline_start = 0;
render_pipeline = 0;
sprite_flop_linebuffers = 0;
sprite_linebuffers_clr = 0;
sprite_effective_column = 0;
bg_effective_scanline = 0;
bg_effective_column = 0;
bg_pipeline_start = 0;
load_bg_scanline_buffer = 0;
out_effective_column = 0;
// sprite pipeline control
// operates one scanline ahead for next scanline
if (scanline >= 127 && scanline <= 350) begin
if (column == 10) begin // soon after clearing, start
sprite_pipeline_start = 1;
end else if (column == 1) begin // clear at start of scanline
sprite_linebuffers_clr = 1;
end else if (column == 600) begin // flop at end of scanline
sprite_flop_linebuffers = 1;
end
sprite_effective_scanline = scanline - 127 + 16 + 1;
end
// bg pipeline control (for scrolling)
// operates one scanline ahead at all times
if (scanline >= 126 && scanline <= 349) begin
bg_effective_scanline = scanline - 126 + 16;
if (column >= 184 && column <= 695) begin
load_bg_scanline_buffer = 1;
bg_effective_column = column - 184;
if (((column) % 8) == 0) begin
bg_pipeline_start = 1;
end
end
end
// bg output control (for scrolling)
// operates one scanline ahead at all times
if (scanline >= 128 && scanline <= 351) begin
bg_effective_scanline = scanline - 128 + 16;
if (column >= 184 && column <= 456) begin
out_effective_column = column - 184;
end
end
// fg/bg pipeline control
// operates 8 pixels ahead for next 8 pixels
if (scanline >= 128 && scanline <= 351) begin
effective_scanline = scanline - 128 + 16;
if (column >= 184 && column <= 440) begin
effective_column = column - 184;
if (((column) % 8) == 0) begin
pipeline_start = 1;
end
end
// renderer control
// has delay of 2
if (column >= 193 && column <= 448) begin
sprite_effective_column = column - 193;
render_pipeline = 1;
end
end
end
endmodule
/**
structure called line buffer will hold next scanline so that this happens
in parallel with rendering current scanline
create sprite buffer for scanline:
-scan all sprites (looking at y-coordinate)
-load sprite from rom
-initialize shift register to x-coordinate
-flip if applicable
create bg tile buffer
-load bg tile from bgvideoram
-load tile from rom
-flip if applicable
create fg tile buffer
-load fg tile from fgvideoram
-load tile from rom
PIPELINE STAGES
bg tile pipeline:
(1) fetch bg tile (2 reads)
(2) load tile from rom (3 reads)
(3) transform
(4) set bg line buffer cell
sprite pipeline:
(1) fetch sprite
(2) evaluate y-coordinate
(3) load sprite from rom (2 reads)
(4) transform
(5) set sprite line buffer cell
fg tile pipeline:
(1) fetch fg tile (2 reads)
(2) load tile from rom (1 read)
(3) set fg line buffer cell
render pipeline:
(1) fetch 3 line buffer cells
(2) fetch color based on priority (fg > sprite > bg)
*/
/**
fg linebuffer pipeline
reads fg RAM, loads tiles from ROM, generates pixels for rendering
*/
module fg_linebuffer_pipeline(
input wire [7:0] scanline,
input wire [7:0] column,
input wire [7:0] fgvideoram_read_data,
input wire [7:0] gfx1_read_data,
input wire [2:0] linebuffer_bit,
input wire pipeline_start,
input wire video_clk, rst_b,
output reg [15:0] gfx1_read_addr,
output reg [15:0] fgvideoram_read_addr,
output wire [7:0] fg_linebuffer_color,
output wire fg_linebuffer_color_transparent
);
wire [7:0] fgvideoram_code_reg_out;
wire [5:0] fgvideoram_color_reg_out;
wire [7:0] gfx1_1_reg_out;
wire [7:0] gfx1_2_reg_out;
reg fgvideoram_code_reg_load;
reg fgvideoram_color_reg_load;
reg gfx1_1_reg_load;
reg gfx1_2_reg_load;
// -- fg, gfx1 registers
// Code = all read data
// Color = [5:0]read data
generic_register #(8) fgvideoram_code_reg (
.in(fgvideoram_read_data),
.out(fgvideoram_code_reg_out),
.load(fgvideoram_code_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(6) fgvideoram_color_reg(
.in(fgvideoram_read_data[5:0]),
.out(fgvideoram_color_reg_out),
.load(fgvideoram_color_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx1_1_reg(
.in(gfx1_read_data),
.out(gfx1_1_reg_out),
.load(gfx1_1_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx1_2_reg(
.in(gfx1_read_data),
.out(gfx1_2_reg_out),
.load(gfx1_2_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
wire [5:0] fgvideoram_color_linebuffer_out;
wire [2:0] gfx1_1_linebuffer_sel;
wire [2:0] gfx1_2_linebuffer_sel;
wire gfx1_1_linebuffer_out;
wire gfx1_2_linebuffer_out;
// commits to linebuffers will occur on assertion of pipeline_start
wire linebuffer_load;
assign linebuffer_load = pipeline_start;
wire [7:0] gfx1_1_linebuffer_in, gfx1_2_linebuffer_in;
// Reordering linebuffers
assign gfx1_1_linebuffer_in = {gfx1_2_reg_out[3:0], gfx1_1_reg_out[3:0]};
assign gfx1_2_linebuffer_in = {gfx1_2_reg_out[7:4], gfx1_1_reg_out[7:4]};
// fg, gfx1 linebuffers
generic_register #(6) fgvideoram_color_linebuffer(
.in(fgvideoram_color_reg_out),
.out(fgvideoram_color_linebuffer_out),
.load(linebuffer_load),
.clk(video_clk),
.rst_b(rst_b)
);
linebuffer8 gfx1_1_linebuffer(
.in(gfx1_1_linebuffer_in),
.sel(gfx1_1_linebuffer_sel),
.load(linebuffer_load),
.out(gfx1_1_linebuffer_out),
.clk(video_clk),
.rst_b(rst_b)
);
linebuffer8 gfx1_2_linebuffer(
.in(gfx1_2_linebuffer_in),
.sel(gfx1_2_linebuffer_sel),
.load(linebuffer_load),
.out(gfx1_2_linebuffer_out),
.clk(video_clk),
.rst_b(rst_b)
);
// inputs/outputs to renderer
assign gfx1_1_linebuffer_sel = linebuffer_bit - 1;
assign gfx1_2_linebuffer_sel = linebuffer_bit - 1;
assign fg_linebuffer_color_transparent = ({gfx1_2_linebuffer_out, gfx1_1_linebuffer_out} == 2'd0);
assign fg_linebuffer_color = {fgvideoram_color_linebuffer_out,
gfx1_1_linebuffer_out,
gfx1_2_linebuffer_out};
reg [2:0] fg_pipeline_state, fg_pipeline_next_state;
// fg tile element indexes in gfx1
reg [15:0] gfx1_read_addr_computed;
reg [7:0] gfx1_read_addr_offset;
reg [15:0] gfx1_1_read_addr, gfx1_2_read_addr;
reg [7:0] fg_tile_row;
always @(*) begin
gfx1_read_addr_computed = 0;
gfx1_read_addr_offset = 0;
gfx1_read_addr_computed = (fgvideoram_code_reg_out + {fgvideoram_read_data[7], 8'b00000000}) << 4; // fgvideoram_read_data is color reg this cycle
gfx1_read_addr_offset = (fg_tile_row << 1);
gfx1_read_addr_computed = (gfx1_read_addr_computed + gfx1_read_addr_offset); // align to byte
// addresses to roms
gfx1_1_read_addr = gfx1_read_addr_computed;
gfx1_2_read_addr = gfx1_read_addr_computed + 1;
end
// fg pipeline fsm
always @(posedge video_clk or negedge rst_b) begin
if (~rst_b) fg_pipeline_state <= `FG_PIPELINE_IDLE;
else fg_pipeline_state <= fg_pipeline_next_state;
end
// fg pipeline output function (5 cycles)
reg [15:0] fgvideoram_tile_index;
always @(*) begin
// defaults
fg_pipeline_next_state = `FG_PIPELINE_IDLE;
fgvideoram_color_reg_load = 0;
fgvideoram_code_reg_load = 0;
gfx1_1_reg_load = 0;
gfx1_2_reg_load = 0;
gfx1_read_addr = 0;
fgvideoram_read_addr = 0;
// compute tile index
fgvideoram_tile_index = ((scanline >> 3) << 5) + (column >> 3); // (scanline/8) * 32 + (column/8)
fg_tile_row = scanline % 8;
case (fg_pipeline_state)
`FG_PIPELINE_IDLE: begin
if (pipeline_start) begin
fg_pipeline_next_state = `FG_PIPELINE_FETCH_TILE_FROM_RAM_1;
fgvideoram_read_addr = fgvideoram_tile_index;
end
end
`FG_PIPELINE_FETCH_TILE_FROM_RAM_1: begin
fg_pipeline_next_state = `FG_PIPELINE_FETCH_TILE_FROM_RAM_2;
fgvideoram_code_reg_load = 1;
fgvideoram_read_addr = fgvideoram_tile_index + 'h0400; // tile_index + 1024
end
`FG_PIPELINE_FETCH_TILE_FROM_RAM_2: begin
fg_pipeline_next_state = `FG_PIPELINE_FETCH_TILE_FROM_ROM_1;
fgvideoram_color_reg_load = 1;
gfx1_read_addr = gfx1_1_read_addr;
fgvideoram_read_addr = fgvideoram_tile_index + 'h0400; // tile_index + 1024
end
`FG_PIPELINE_FETCH_TILE_FROM_ROM_1: begin
fg_pipeline_next_state = `FG_PIPELINE_FETCH_TILE_FROM_ROM_2;
gfx1_1_reg_load = 1;
gfx1_read_addr = gfx1_2_read_addr;
fgvideoram_read_addr = fgvideoram_tile_index + 'h0400; // tile_index + 1024
end
`FG_PIPELINE_FETCH_TILE_FROM_ROM_2: begin
fg_pipeline_next_state = `FG_PIPELINE_IDLE;
gfx1_2_reg_load = 1;
end
endcase
end
endmodule
/**
bg linebuffer pipeline
reads bg RAM, loads tiles from ROM, generates pixels for rendering
*/
module bg_linebuffer_pipeline(
input wire [7:0] scanline,
input wire [8:0] column,
input wire [15:0] scroll_read_data,
input wire [7:0] bgvideoram_read_data,
input wire [7:0] gfx2_bitplane_1_read_data,
input wire [7:0] gfx2_bitplane_2_read_data,
input wire [7:0] gfx2_bitplane_3_read_data,
input wire [2:0] linebuffer_bit,
input wire [1:0] palette_bank_read_data,
input wire pipeline_start,
input wire video_clk, rst_b,
output reg [15:0] bgvideoram_read_addr,
output reg [15:0] gfx2_bitplane_1_read_addr,
output reg [15:0] gfx2_bitplane_2_read_addr,
output reg [15:0] gfx2_bitplane_3_read_addr,
output wire [9:0] bg_linebuffer_color
);
// commits to linebuffers will occur on assertion of pipeline_start
wire linebuffer_load;
assign linebuffer_load = pipeline_start;
wire [7:0] bgvideoram_code_reg_out;
wire [7:0] bgvideoram_color_reg_out;
wire [7:0] gfx2_bitplane_1_reg_out;
wire [7:0] gfx2_bitplane_2_reg_out;
wire [7:0] gfx2_bitplane_3_reg_out;
reg bgvideoram_code_reg_load;
reg bgvideoram_color_reg_load;
reg gfx2_bitplane_1_reg_load;
reg gfx2_bitplane_2_reg_load;
reg gfx2_bitplane_3_reg_load;
// bg, gfx2 registers
// bg code is all read data
// bg color is [4:0] read data
generic_register #(8) bgvideoram_code_reg(
.in(bgvideoram_read_data),
.out(bgvideoram_code_reg_out),
.load(bgvideoram_code_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) bgvideoram_color_reg(
.in(bgvideoram_read_data),
.out(bgvideoram_color_reg_out),
.load(bgvideoram_color_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx2_bitplane_1_reg(
.in(gfx2_bitplane_1_read_data),
.out(gfx2_bitplane_1_reg_out),
.load(gfx2_bitplane_1_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx2_bitplane_2_reg(
.in(gfx2_bitplane_2_read_data),
.out(gfx2_bitplane_2_reg_out),
.load(gfx2_bitplane_2_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx2_bitplane_3_reg(
.in(gfx2_bitplane_3_read_data),
.out(gfx2_bitplane_3_reg_out),
.load(gfx2_bitplane_3_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
wire [7:0] bgvideoram_color_linebuffer_out;
wire [2:0] gfx2_bitplane_1_linebuffer_sel;
wire [2:0] gfx2_bitplane_2_linebuffer_sel;
wire [2:0] gfx2_bitplane_3_linebuffer_sel;
wire gfx2_bitplane_1_linebuffer_out;
wire gfx2_bitplane_2_linebuffer_out;
wire gfx2_bitplane_3_linebuffer_out;
// bg, gfx2 linebuffers
generic_register #(8) bgvideoram_color_linebuffer(
.in(bgvideoram_color_reg_out),
.out(bgvideoram_color_linebuffer_out),
.load(linebuffer_load),
.clk(video_clk),
.rst_b(rst_b)
);
linebuffer8 bgfx2_bitplane_1_reg(
.in(gfx2_bitplane_1_reg_out),
.sel(gfx2_bitplane_1_linebuffer_sel),
.load(linebuffer_load),
.out(gfx2_bitplane_1_linebuffer_out),
.clk(video_clk),
.rst_b(rst_b)
);
linebuffer8 bgfx2_bitplane_2_reg(
.in(gfx2_bitplane_2_reg_out),
.sel(gfx2_bitplane_2_linebuffer_sel),
.load(linebuffer_load),
.out(gfx2_bitplane_2_linebuffer_out),
.clk(video_clk),
.rst_b(rst_b)
);
linebuffer8 bgfx2_bitplane_3_reg(
.in(gfx2_bitplane_3_reg_out),
.sel(gfx2_bitplane_3_linebuffer_sel),
.load(linebuffer_load),
.out(gfx2_bitplane_3_linebuffer_out),
.clk(video_clk),
.rst_b(rst_b)
);
wire flipx;
assign flipx = bgvideoram_color_linebuffer_out[5];
// inputs/outputs to renderer
reg [2:0] flipped_linebuffer_bit;
always @(*) begin
// flipx if color[5]
if (flipx) begin
flipped_linebuffer_bit = 3'd7 - linebuffer_bit + 5;
end else begin
flipped_linebuffer_bit = linebuffer_bit + 3;
end
end
// inputs/outputs to renderer
assign gfx2_bitplane_1_linebuffer_sel = flipped_linebuffer_bit;
assign gfx2_bitplane_2_linebuffer_sel = flipped_linebuffer_bit;
assign gfx2_bitplane_3_linebuffer_sel = flipped_linebuffer_bit;
assign bg_linebuffer_color = {palette_bank_read_data[1:0],
bgvideoram_color_linebuffer_out[4:0],
gfx2_bitplane_1_linebuffer_out,
gfx2_bitplane_2_linebuffer_out,
gfx2_bitplane_3_linebuffer_out};
// bg tile element indexes in gfx2
reg [15:0] gfx2_read_addr_computed;
reg [7:0] gfx2_read_addr_offset;
reg [7:0] bg_tile_row, bg_tile_col;
always @(*) begin
gfx2_read_addr_computed = 0;
gfx2_read_addr_offset = 0;
gfx2_read_addr_computed = (bgvideoram_code_reg_out + {bgvideoram_read_data[7], 8'b00000000}) << 5;
// flipx
if (bgvideoram_read_data[5]) begin
if (bg_tile_col >= 8) gfx2_read_addr_offset = 0;
else gfx2_read_addr_offset = 16;
end else begin
if (bg_tile_col >= 8) gfx2_read_addr_offset = 16;
else gfx2_read_addr_offset = 0;
end
// flipy
if (bgvideoram_read_data[6]) begin
gfx2_read_addr_offset = gfx2_read_addr_offset + (8'd15 - bg_tile_row);
end else begin
gfx2_read_addr_offset = gfx2_read_addr_offset + bg_tile_row;
end
gfx2_read_addr_computed = (gfx2_read_addr_computed + gfx2_read_addr_offset); // align to byte
// addresses to roms
gfx2_bitplane_1_read_addr = gfx2_read_addr_computed;
gfx2_bitplane_2_read_addr = gfx2_read_addr_computed;
gfx2_bitplane_3_read_addr = gfx2_read_addr_computed;
end
reg [2:0] bg_pipeline_state, bg_pipeline_next_state;
// bg pipeline fsm
always @(posedge video_clk or negedge rst_b) begin
if (~rst_b) bg_pipeline_state <= `BG_PIPELINE_IDLE;
else bg_pipeline_state <= bg_pipeline_next_state;
end
wire [15:0] effective_column;
assign effective_column = column + {scroll_read_data[15:4], 4'd0};//((scroll_read_data >> 4) << 4);
// bg pipeline output function (4 cycles)
reg [15:0] bgvideoram_tile_index, bgvideoram_tile_index_computed;
always @(*) begin
// defaults
bg_pipeline_next_state = `FG_PIPELINE_IDLE;
bgvideoram_color_reg_load = 0;
bgvideoram_code_reg_load = 0;
gfx2_bitplane_1_reg_load = 0;
gfx2_bitplane_2_reg_load = 0;
gfx2_bitplane_3_reg_load = 0;
bgvideoram_read_addr = 0;
// compute tile index
bgvideoram_tile_index = ((effective_column >> 4) << 4) + (scanline >> 4); // (column/16) * 32 + (row/16)
bgvideoram_tile_index_computed = {bgvideoram_tile_index[8:4], 1'd0, bgvideoram_tile_index[3:0]};
bg_tile_row = scanline % 16;
bg_tile_col = effective_column % 16;
case (bg_pipeline_state)
`BG_PIPELINE_IDLE: begin
bgvideoram_read_addr = bgvideoram_tile_index_computed + 'h10; // computed tile index + 16
if (pipeline_start) begin
bg_pipeline_next_state = `BG_PIPELINE_FETCH_TILE_FROM_RAM_1;
bgvideoram_read_addr = bgvideoram_tile_index_computed;
end
end
`BG_PIPELINE_FETCH_TILE_FROM_RAM_1: begin
bg_pipeline_next_state = `BG_PIPELINE_FETCH_TILE_FROM_RAM_2;
bgvideoram_code_reg_load = 1;
bgvideoram_read_addr = bgvideoram_tile_index_computed + 'h10; // computed tile index + 16
end
`BG_PIPELINE_FETCH_TILE_FROM_RAM_2: begin
bg_pipeline_next_state = `BG_PIPELINE_FETCH_TILE_FROM_ROM;
bgvideoram_color_reg_load = 1;
bgvideoram_read_addr = bgvideoram_tile_index_computed + 'h10; // computed tile index + 16
end
`BG_PIPELINE_FETCH_TILE_FROM_ROM: begin
bg_pipeline_next_state = `BG_PIPELINE_IDLE;
gfx2_bitplane_1_reg_load = 1;
gfx2_bitplane_2_reg_load = 1;
gfx2_bitplane_3_reg_load = 1;
bgvideoram_read_addr = bgvideoram_tile_index_computed + 'h10; // computed tile index + 16
end
endcase
end
endmodule
/**
sprite pipeline:
(1) fetch sprite
(2) evaluate y-coordinate
(3) load sprite from rom (2 reads)
(4) transform
(5) set sprite line buffer cell
*/
/**
sprite linebuffer pipeline
reads sprite RAM, loads tiles from ROM, generates pixels for rendering
*/
module sprite_linebuffer_pipeline(
input wire [7:0] scanline,
input wire [7:0] column,
input wire sprite_pipeline_start,
input wire linebuffers_clr,
input wire flop_linebuffers,
input wire [7:0] gfx3_1_read_data,
input wire [7:0] gfx3_2_read_data,
input wire [7:0] spriteram_read_data,
input wire video_clk, rst_b,
output wire [7:0] sprite_linebuffer_color,
output wire sprite_linebuffer_color_transparent,
output reg [15:0] spriteram_addr,
output reg [15:0] gfx3_1_read_addr,
output reg [15:0] gfx3_2_read_addr
);
wire [7:0] gfx3_1_1_reg_out;
wire [7:0] gfx3_1_2_reg_out;
wire [7:0] gfx3_1_3_reg_out;
wire [7:0] gfx3_1_4_reg_out;
wire [7:0] gfx3_2_1_reg_out;
wire [7:0] gfx3_2_2_reg_out;
wire [7:0] gfx3_2_3_reg_out;
wire [7:0] gfx3_2_4_reg_out;
reg gfx3_1_1_reg_load;
reg gfx3_1_2_reg_load;
reg gfx3_1_3_reg_load;
reg gfx3_1_4_reg_load;
reg gfx3_2_1_reg_load;
reg gfx3_2_2_reg_load;
reg gfx3_2_3_reg_load;
reg gfx3_2_4_reg_load;
reg sprite_counter_inc;
wire [4:0] sprite_counter_out;
// counter (32 sprites)
generic_counter #(5) sprite_counter(
.inc(sprite_counter_inc),
.out(sprite_counter_out),
.rst_b(rst_b),
.clk(video_clk),
.clr(1'b0)
);
generic_register #(8) gfx3_1_1_reg(
.in(gfx3_1_read_data),
.out(gfx3_1_1_reg_out),
.load(gfx3_1_1_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx3_1_2_reg(
.in(gfx3_1_read_data),
.out(gfx3_1_2_reg_out),
.load(gfx3_1_2_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx3_1_3_reg(
.in(gfx3_1_read_data),
.out(gfx3_1_3_reg_out),
.load(gfx3_1_3_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx3_1_4_reg(
.in(gfx3_1_read_data),
.out(gfx3_1_4_reg_out),
.load(gfx3_1_4_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx3_2_1_reg(
.in(gfx3_2_read_data),
.out(gfx3_2_1_reg_out),
.load(gfx3_2_1_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx3_2_2_reg(
.in(gfx3_2_read_data),
.out(gfx3_2_2_reg_out),
.load(gfx3_2_2_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx3_2_3_reg(
.in(gfx3_2_read_data),
.out(gfx3_2_3_reg_out),
.load(gfx3_2_3_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) gfx3_2_4_reg(
.in(gfx3_2_read_data),
.out(gfx3_2_4_reg_out),
.load(gfx3_2_4_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
wire [7:0] spriteram_code_reg_data, spriteram_color_reg_data, spriteram_sy_reg_data, spriteram_sx_reg_data;
reg spriteram_code_reg_load, spriteram_color_reg_load, spriteram_sy_reg_load, spriteram_sx_reg_load;
generic_register #(8) spriteram_code_reg(
.in(spriteram_read_data),
.out(spriteram_code_reg_data),
.load(spriteram_code_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) spriteram_color_reg(
.in(spriteram_read_data),
.out(spriteram_color_reg_data),
.load(spriteram_color_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) spriteram_sy_reg(
.in(spriteram_read_data),
.out(spriteram_sy_reg_data),
.load(spriteram_sy_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
generic_register #(8) spriteram_sx_reg(
.in(spriteram_read_data),
.out(spriteram_sx_reg_data),
.load(spriteram_sx_reg_load),
.clk(video_clk),
.rst_b(rst_b)
);
wire linebuffers_full;
reg linebuffers_load;
reg [8:0] sprite_sx;
sprite_linebuffers linebuffers(
.bitplane1({gfx3_1_4_reg_out[3:0], gfx3_1_3_reg_out[3:0], gfx3_1_2_reg_out[3:0], gfx3_1_1_reg_out[3:0]}),
.bitplane2({gfx3_1_4_reg_out[7:4], gfx3_1_3_reg_out[7:4], gfx3_1_2_reg_out[7:4], gfx3_1_1_reg_out[7:4]}),
.bitplane3({gfx3_2_4_reg_out[3:0], gfx3_2_3_reg_out[3:0], gfx3_2_2_reg_out[3:0], gfx3_2_1_reg_out[3:0]}),
.bitplane4({gfx3_2_4_reg_out[7:4], gfx3_2_3_reg_out[7:4], gfx3_2_2_reg_out[7:4], gfx3_2_1_reg_out[7:4]}),
.color(spriteram_color_reg_data[3:0]),
.sx(sprite_sx),
.load(linebuffers_load),
.clr(linebuffers_clr),
.clk(video_clk),
.rst_b(rst_b),
.sel(column),
.flop_linebuffers(flop_linebuffers),
.transparent(sprite_linebuffer_color_transparent),
.full(linebuffers_full),
.out(sprite_linebuffer_color)
);
reg [3:0] sprite_pipeline_state, sprite_pipeline_next_state;
// sprite pipeline fsm
always @(posedge video_clk or negedge rst_b) begin
if (~rst_b) begin
sprite_pipeline_state <= `SPRITE_PIPELINE_IDLE;
end else begin
sprite_pipeline_state <= sprite_pipeline_next_state;
end
end
// spriteram address logic
reg [15:0] spriteram_addr_computed;
always @(*) begin
spriteram_addr_computed = (31 - sprite_counter_out) << 2; // (32 - offs)*4
end
// sprite relevance, address, color, and position logic
reg [1:0] sprite_i;
reg sprite_in_scanline;
reg [15:0] sprite_code;
reg [15:0] sprite_code_rom_addr_1, sprite_code_rom_addr_2, sprite_code_rom_addr_3, sprite_code_rom_addr_4;
reg [3:0] sprite_color;
reg [1:0] sprite_i_effective;
always @(*) begin
// defaults
sprite_i_effective = 0;
sprite_in_scanline = 0;
// double/quadruple height
sprite_i = spriteram_color_reg_data[7:6];