1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-28 23:49:20 +00:00
8bitworkshop/presets/verilog/sprite_bitmap.v

104 lines
2.3 KiB
Coq
Raw Normal View History

`ifndef SPRITE_BITMAP_H
`define SPRITE_BITMAP_H
2017-11-21 21:36:38 +00:00
`include "hvsync_generator.v"
2018-10-01 16:30:47 +00:00
/*
Simple sprite renderer example.
car_bitmap - ROM for a car sprite.
sprite_bitmap_top - Example sprite rendering module.
*/
2018-02-07 00:07:40 +00:00
module car_bitmap(yofs, bits);
input [3:0] yofs;
output [7:0] bits;
reg [7:0] bitarray[0:15];
2017-11-21 21:36:38 +00:00
assign bits = bitarray[yofs];
2017-11-21 21:36:38 +00:00
initial begin/*{w:8,h:16}*/
bitarray[0] = 8'b0;
bitarray[1] = 8'b1100;
bitarray[2] = 8'b11001100;
bitarray[3] = 8'b11111100;
bitarray[4] = 8'b11101100;
bitarray[5] = 8'b11100000;
bitarray[6] = 8'b1100000;
bitarray[7] = 8'b1110000;
2017-11-21 21:36:38 +00:00
bitarray[8] = 8'b110000;
bitarray[9] = 8'b110000;
bitarray[10] = 8'b110000;
bitarray[11] = 8'b1101110;
bitarray[12] = 8'b11101110;
bitarray[13] = 8'b11111110;
bitarray[14] = 8'b11101110;
bitarray[15] = 8'b101110;
2017-11-21 21:36:38 +00:00
end
2018-02-07 00:07:40 +00:00
2017-11-21 21:36:38 +00:00
endmodule
2018-02-03 20:20:56 +00:00
module sprite_bitmap_top(clk, reset, hsync, vsync, rgb);
2017-11-21 21:36:38 +00:00
2018-02-03 20:20:56 +00:00
input clk, reset;
2017-11-21 21:36:38 +00:00
output hsync, vsync;
output [2:0] rgb;
wire display_on;
wire [8:0] hpos;
wire [8:0] vpos;
reg sprite_active;
reg [3:0] car_sprite_xofs;
reg [3:0] car_sprite_yofs;
wire [7:0] car_sprite_bits;
2018-02-03 20:20:56 +00:00
reg [8:0] player_x = 128;
reg [8:0] player_y = 128;
2017-11-21 21:36:38 +00:00
hvsync_generator hvsync_gen(
.clk(clk),
2018-02-03 20:20:56 +00:00
.reset(reset),
2017-11-21 21:36:38 +00:00
.hsync(hsync),
.vsync(vsync),
.display_on(display_on),
.hpos(hpos),
.vpos(vpos)
);
car_bitmap car(
.yofs(car_sprite_yofs),
.bits(car_sprite_bits));
// start Y counter when we hit the top border (player_y)
2017-11-21 21:36:38 +00:00
always @(posedge hsync)
2018-02-03 20:20:56 +00:00
if (vpos == player_y)
2017-11-21 21:36:38 +00:00
car_sprite_yofs <= 15;
else if (car_sprite_yofs != 0)
car_sprite_yofs <= car_sprite_yofs - 1;
// restart X counter when we hit the left border (player_x)
2017-11-21 21:36:38 +00:00
always @(posedge clk)
2018-02-03 20:20:56 +00:00
if (hpos == player_x)
car_sprite_xofs <= 15;
else if (car_sprite_xofs != 0)
car_sprite_xofs <= car_sprite_xofs - 1;
2017-11-21 21:36:38 +00:00
// mirror sprite in X direction
2018-02-03 20:20:56 +00:00
wire [3:0] car_bit = car_sprite_xofs>=8 ?
2017-11-21 21:36:38 +00:00
15-car_sprite_xofs:
2018-02-03 20:20:56 +00:00
car_sprite_xofs;
wire car_gfx = car_sprite_bits[car_bit[2:0]];
2017-11-21 21:36:38 +00:00
wire r = display_on && car_gfx;
wire g = display_on && car_gfx;
wire b = display_on && car_gfx;
assign rgb = {b,g,r};
endmodule
`endif