Skip to content

Commit

Permalink
Verilog code.
Browse files Browse the repository at this point in the history
  • Loading branch information
VenkatKS committed May 20, 2017
1 parent d863201 commit 25cc5c5
Show file tree
Hide file tree
Showing 11 changed files with 1,408 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/BCDBinaryConverter.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
`timescale 1ns / 1ps

module BCDConverter(int, ones, tens, hundreds, thousands);

input [13:0] int;
output reg [3:0] ones;
output reg [3:0] tens;
output reg [3:0] hundreds;
output reg [3:0] thousands;

integer i;
always @(int)
begin

thousands = 4'd0;
hundreds = 4'd0;
tens = 4'd0;
ones = 4'd0;

for (i = 13; i >=0; i = i-1)
begin

if (thousands >= 5) thousands = thousands + 3;
if (hundreds >= 5) hundreds = hundreds + 3;
if (tens >= 5) tens = tens + 3;
if (ones >= 5) ones = ones + 3;



thousands = thousands << 1;
thousands[0] = hundreds[3];
hundreds = hundreds << 1;
hundreds[0] = tens[3];
tens = tens << 1;
tens[0] = ones[3];
ones = ones << 1;
ones[0] = int[i];

end
end



endmodule
Binary file added src/Driver.bit
Binary file not shown.
128 changes: 128 additions & 0 deletions src/Driver.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 11/02/2015 05:45:37 PM
// Design Name:
// Module Name: Driver
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////


module Driver(
CLKBoard,
KEYSIG_DATA,
KEYSIG_CLK,
KEYSIG_STROBE,
seg,
an,
R_VAL,
G_VAL,
B_VAL,
hSYNC,
vSYNC,
S0,
S1,
S2,
S3,
S4,
S5,
S6,
S7,
debugger
);

input CLKBoard;
input KEYSIG_DATA;
input KEYSIG_CLK;
input S0, S1, S2, S3, S4, S5, S6, S7;
output KEYSIG_STROBE;
wire[7:0] LED_DATA;
output[7:0] seg;
output[3:0] an;

// Port definitions
// CLKBoard: 100 MHz clock from the board

// Clock speeds are self descriptive

// Clock Architecture
wire HZ1_CLK;
wire HZ2_CLK;
wire HZ128_CLK;
wire HZ25M_CLK;
wire HZ5_CLK;
wire HZ40_CLK;

CLKSignals CLKController(CLKBoard, HZ1_CLK, HZ2_CLK, HZ128_CLK, HZ25M_CLK, HZ5_CLK, HZ40_CLK);

// PS2 Controller
wire NewData;

wire KEYPRESS_S,
KEYPRESS_P,
KEYPRESS_R,
KEYPRESS_ESC,
KEYPRESS_UP,
KEYPRESS_DOWN,
KEYPRESS_LEFT,
KEYPRESS_RIGHT;

output[3:0] debugger;

assign debugger[0] = GAME_RUN[0];
assign debugger[1] = GAME_RUN[1];
assign debugger[2] = GAME_RUN[2];
assign debugger[3] = KEYPRESS_ESC;

PS2Controller KeyboardDriver(KEYSIG_CLK, KEYSIG_DATA, LED_DATA, HZ5_CLK, NewData, KEYPRESS_S,
KEYPRESS_P, KEYPRESS_R, KEYPRESS_ESC, KEYPRESS_UP, KEYPRESS_DOWN, KEYPRESS_LEFT, KEYPRESS_RIGHT);
// 7 Segment Display
SevenSegmentDriver LcdMod(HZ128_CLK, LED_DATA[3:0], LED_DATA[7:4], seg, an, 1'b1);
// Strobe light controller
StrobeController Blinker(CLKBoard, NewData, KEYSIG_STROBE);

// VGA Driver
output wire[3:0] R_VAL;
output wire[3:0] B_VAL;
output wire[3:0] G_VAL;
output wire hSYNC;
output wire vSYNC;
wire RDY;

wire[3:0] SnakeRed;
wire[3:0] SnakeBlue;
wire[3:0] SnakeGreen;
wire TEMP;

// Replace with game logic
wire[9:0] XPOS;
wire[9:0] YPOS;
wire[9:0] SIZE;
wire[1:0] SNDIR;
wire ACTIVE = 1'd1;
wire[2:0] GAME_RUN;

//GameController SnakeGameSystem(TEMP, KEYPRESS_UP, KEYPRESS_DOWN, KEYPRESS_LEFT, KEYPRESS_RIGHT, KEYPRESS_ESC, KEYPRESS_P, KEYPRESS_S, KEYPRESS_R, XPOS, YPOS, SIZE, SNDIR,ACTIVE);

DisplayDriver DDriver(HZ25M_CLK, RDY, XPOS, YPOS, SIZE, SNDIR, SnakeRed, SnakeBlue, SnakeGreen);

VGADriver MonitorDriver(HZ25M_CLK, S0, S1, S2, S3, S4, S5, S6, S7, R_VAL, G_VAL, B_VAL, hSYNC, vSYNC, TEMP, RDY, SnakeRed, SnakeGreen, SnakeBlue, ACTIVE);

// Game controller
SnakeGameController Snake(HZ40_CLK, KEYPRESS_ESC, KEYPRESS_R, KEYPRESS_S, KEYPRESS_P, KEYPRESS_UP,
KEYPRESS_DOWN, KEYPRESS_LEFT, KEYPRESS_RIGHT, XPOS, YPOS, SNDIR, GAME_RUN);


endmodule
116 changes: 116 additions & 0 deletions src/GameController.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
module GameController(CLK, UP, DOWN, LEFT, RIGHT, Escape, Pause, Start, Resume, X, Y, Size, Dir, Playing);

input CLK, UP, DOWN, LEFT, RIGHT, Escape, Start, Pause, Resume;
output X, Y, Size, Dir, Playing;

wire [9:0] X;
wire [9:0] Y;
reg [9:0] Xin = 60;
reg [9:0] Yin = 60;
reg [3:0] Size = 2;
reg [1:0] Dir = 0; //convention: 00 is right, 01 (1) is down, 10 (2) is left, 11 (3) is up
reg Paused = 0;
reg Playing = 0;
reg oldDir = 1;
//reg [1:0] turn; //this code is used to see if there is a current turn that needs to be taken.
//assign statments for X and Y based on Xin and Yin go here
assign X = (Dir == 2'd0) ? Xin : ((Dir == 2'd2) ? (Xin - 83) : (Xin - 42));
assign Y = (Dir == 2'd1) ? (Yin - 38) : ((Dir == 2'd3) ? (Yin+38) : Yin);


always @(posedge CLK)
begin


//following code increments position based on current direction
if (Playing && !Paused) begin
if (Dir == 2'd0) begin
Xin = Xin + 1;
end
else if (Dir == 2'd1) begin
Yin = Yin - 1;
end
else if (Dir == 2'd2) begin
Xin = Xin - 1;
end
else begin
Yin = Yin + 1;
end

end
else if (!Playing) begin
Xin = 60;
Yin = 60; //initialize to start coordinates for once the game begins again
end


end

always @(Escape, Start, Pause, Resume)
begin

if (Playing == 0) //game is currently black screen - not playing
begin
if (Start ==1) // check to see if start button is pressed, if so, start the game
begin
Playing = 1; //start button is pressed - start the game

end

end

else begin //so game is currently playing right now

if (Escape == 1) begin //escape is pressed while game is playing, so end the game
Playing = 0;
end

else if (Paused == 0) //check to see if it is not paused
begin
if (Pause == 1) //pause is pressed while game is playing and unpaused
begin
Paused = 1; //pause the game
end
else if ((Dir == 2'd1)||(Dir == 2'd3)) //snake is moving vertically - need to check for horizontal keypresses
begin
if (LEFT)
begin
oldDir = Dir;
Dir = 2'd2;
end
else if (RIGHT)
begin
oldDir = Dir;
Dir = 2'd0;
end
end
else begin //snake must be moving horizontally - check for vertical key presses
if (UP) begin
oldDir = Dir;
Dir = 2'd3;
end
if (DOWN) begin
oldDir = Dir;
Dir = 2'd1;
end
end
end
else begin //so the game is paused - check to see if resume has been pressed
if (Resume == 1) begin
Paused = 0;
end
end
end


end

endmodule


//notes:
//need to 40 pixels per second.
//each screen takes .0168 seconds to display.
//this means that hte game is incrementing about every .0168 seconds
//40 pixels/second ----> .672 pixels/.0168 seconds. damn
//will estimate to 1 pixel/.0168 seconds
Loading

0 comments on commit 25cc5c5

Please sign in to comment.