1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2025-01-18 09:30:11 +00:00

verilog preset comments

This commit is contained in:
Steven Hugg 2018-10-01 13:36:26 -04:00
parent b6197a009a
commit 7e00cc898b
3 changed files with 38 additions and 44 deletions

View File

@ -69,6 +69,8 @@ TODO:
- C/asm formatter - C/asm formatter
- fix WebAudio (https://news.ycombinator.com/item?id=18066474) - fix WebAudio (https://news.ycombinator.com/item?id=18066474)
- Safari: verilog scope doesn't work - Safari: verilog scope doesn't work
- share playable link w/ verilog?
- pixedit Sprite Rotation bitmap wrong (bpw?)
WEB WORKER FORMAT WEB WORKER FORMAT

View File

@ -1,9 +1,10 @@

`ifndef SPRITE_ROTATION_H `ifndef SPRITE_ROTATION_H
`define SPRITE_ROTATION_H `define SPRITE_ROTATION_H
`include "hvsync_generator.v" `include "hvsync_generator.v"
// tank bitmap ROM module
module tank_bitmap(addr, bits); module tank_bitmap(addr, bits);
input [7:0] addr; input [7:0] addr;
@ -101,6 +102,7 @@ module tank_bitmap(addr, bits);
end end
endmodule endmodule
// 16x16 sprite renderer that supports rotation
module sprite_renderer2(clk, vstart, load, hstart, rom_addr, rom_bits, module sprite_renderer2(clk, vstart, load, hstart, rom_addr, rom_bits,
hmirror, vmirror, hmirror, vmirror,
gfx, busy); gfx, busy);
@ -180,30 +182,31 @@ module sprite_renderer2(clk, vstart, load, hstart, rom_addr, rom_bits,
endmodule endmodule
// converts 0..15 rotation value to bitmap index / mirror bits
module rotation_selector(rotation, bitmap_num, hmirror, vmirror); module rotation_selector(rotation, bitmap_num, hmirror, vmirror);
input [3:0] rotation; input [3:0] rotation; // angle (0..15)
output [2:0] bitmap_num; output [2:0] bitmap_num; // bitmap index (0..4)
output hmirror, vmirror; output hmirror, vmirror; // horiz & vert mirror bits
always @(*) always @(*)
case (rotation[3:2]) case (rotation[3:2]) // 4 quadrants
0: begin 0: begin // 0..3 -> 0..3
bitmap_num = {1'b0, rotation[1:0]}; bitmap_num = {1'b0, rotation[1:0]};
hmirror = 0; hmirror = 0;
vmirror = 0; vmirror = 0;
end end
1: begin 1: begin // 4..7 -> 4..1
bitmap_num = -rotation[2:0]; bitmap_num = -rotation[2:0];
hmirror = 0; hmirror = 0;
vmirror = 1; vmirror = 1;
end end
2: begin 2: begin // 8-11 -> 0..3
bitmap_num = {1'b0, rotation[1:0]}; bitmap_num = {1'b0, rotation[1:0]};
hmirror = 1; hmirror = 1;
vmirror = 1; vmirror = 1;
end end
3: begin 3: begin // 12-15 -> 4..1
bitmap_num = -rotation[2:0]; bitmap_num = -rotation[2:0];
hmirror = 1; hmirror = 1;
vmirror = 0; vmirror = 0;
@ -212,6 +215,7 @@ module rotation_selector(rotation, bitmap_num, hmirror, vmirror);
endmodule endmodule
// tank controller module -- handles rendering and movement
module tank_controller(clk, reset, hpos, vpos, hsync, vsync, module tank_controller(clk, reset, hpos, vpos, hsync, vsync,
sprite_addr, sprite_bits, gfx, sprite_addr, sprite_bits, gfx,
playfield, playfield,
@ -276,18 +280,17 @@ module tank_controller(clk, reset, hpos, vpos, hsync, vsync,
player_rot <= initial_rot; player_rot <= initial_rot;
player_speed <= 0; player_speed <= 0;
end else begin end else begin
frame <= frame + 1; frame <= frame + 1; // increment frame counter
// rotation if (frame[0]) begin // only update every other frame
if (frame[0]) begin
if (switch_left) if (switch_left)
player_rot <= player_rot - 1; player_rot <= player_rot - 1; // turn left
else if (switch_right) else if (switch_right)
player_rot <= player_rot + 1; player_rot <= player_rot + 1; // turn right
if (switch_up) begin if (switch_up) begin
if (player_speed != 15) if (player_speed != 15) // max accel
player_speed <= player_speed + 1; player_speed <= player_speed + 1;
end else end else
player_speed <= 0; player_speed <= 0; // stop
end end
end end
end end
@ -302,17 +305,16 @@ module tank_controller(clk, reset, hpos, vpos, hsync, vsync,
collision_detected <= 1; collision_detected <= 1;
// sine lookup (4 bits input, 4 signed bits output) // sine lookup (4 bits input, 4 signed bits output)
function signed [3:0] sin_16x4; function signed [3:0] sin_16x4;
input [3:0] in; input [3:0] in; // input angle 0..15
integer y; integer y;
case (in[1:0]) case (in[1:0]) // 4 values per quadrant
0: y = 0; 0: y = 0;
1: y = 3; 1: y = 3;
2: y = 5; 2: y = 5;
3: y = 6; 3: y = 6;
endcase endcase
case (in[3:2]) case (in[3:2]) // 4 quadrants
0: sin_16x4 = 4'(y); 0: sin_16x4 = 4'(y);
1: sin_16x4 = 4'(7-y); 1: sin_16x4 = 4'(7-y);
2: sin_16x4 = 4'(-y); 2: sin_16x4 = 4'(-y);
@ -322,16 +324,18 @@ module tank_controller(clk, reset, hpos, vpos, hsync, vsync,
always @(posedge hsync or posedge reset) always @(posedge hsync or posedge reset)
if (reset) begin if (reset) begin
// set initial position
player_x_fixed <= initial_x << 4; player_x_fixed <= initial_x << 4;
player_y_fixed <= initial_y << 4; player_y_fixed <= initial_y << 4;
end else begin end else begin
// movement // collision detected? move backwards
if (collision_detected && vpos[3:1] == 0) begin if (collision_detected && vpos[3:1] == 0) begin
if (vpos[0]) if (vpos[0])
player_x_fixed <= player_x_fixed + 12'(sin_16x4(player_rot+8)); player_x_fixed <= player_x_fixed + 12'(sin_16x4(player_rot+8));
else else
player_y_fixed <= player_y_fixed - 12'(sin_16x4(player_rot+12)); player_y_fixed <= player_y_fixed - 12'(sin_16x4(player_rot+12));
end else end else
// forward movement
if (vpos < 9'(player_speed)) begin if (vpos < 9'(player_speed)) begin
if (vpos[0]) if (vpos[0])
player_x_fixed <= player_x_fixed + 12'(sin_16x4(player_rot)); player_x_fixed <= player_x_fixed + 12'(sin_16x4(player_rot));

View File

@ -1,4 +1,4 @@

`include "hvsync_generator.v" `include "hvsync_generator.v"
`include "ram.v" `include "ram.v"
@ -42,10 +42,10 @@ module sprite_scanline_renderer(clk, reset, hpos, vpos, rgb,
localparam N = 1<<NB; // number of sprites localparam N = 1<<NB; // number of sprites
localparam M = 1<<MB; // slots per scanline localparam M = 1<<MB; // slots per scanline
input clk, reset; input clk, reset; // clock and reset inputs
input [8:0] hpos; input [8:0] hpos; // horiz. sync pos
input [8:0] vpos; input [8:0] vpos; // vert. sync pos
output [3:0] rgb; output [3:0] rgb; // rgb output
output [NB:0] ram_addr; // RAM for sprite data output [NB:0] ram_addr; // RAM for sprite data
input [15:0] ram_data; // (2 words per sprite) input [15:0] ram_data; // (2 words per sprite)
@ -55,9 +55,9 @@ module sprite_scanline_renderer(clk, reset, hpos, vpos, rgb,
input [15:0] rom_data; // sprite ROM data input [15:0] rom_data; // sprite ROM data
// copy of sprite data from RAM (N entries) // copy of sprite data from RAM (N entries)
reg [7:0] sprite_xpos[0:N-1]; reg [7:0] sprite_xpos[0:N-1]; // X positions
reg [7:0] sprite_ypos[0:N-1]; reg [7:0] sprite_ypos[0:N-1]; // Y positions
reg [7:0] sprite_attr[0:N-1]; reg [7:0] sprite_attr[0:N-1]; // attributes
// M sprite slots // M sprite slots
reg [7:0] line_xpos[0:M-1]; // X pos for M slots reg [7:0] line_xpos[0:M-1]; // X pos for M slots
@ -84,19 +84,7 @@ module sprite_scanline_renderer(clk, reset, hpos, vpos, rgb,
// which offset in scanline buffer to read? // which offset in scanline buffer to read?
wire [8:0] read_bufidx = {vpos[0], hpos[7:0]}; wire [8:0] read_bufidx = {vpos[0], hpos[7:0]};
/* // always block (every clock cycle)
0: read sprite_ypos[i]
1: check ypos, write line_ypos[j]
...
0: read line_xpos[0]
1: store xpos
2: read line_ypos[0]
3: store ypos
4: read line_attr[0]
5: store attr
8: write 16 pixels
*/
always @(posedge clk) begin always @(posedge clk) begin
ram_busy <= 0; ram_busy <= 0;