Beruflich Dokumente
Kultur Dokumente
1) Source code :-
module vga_top(
input clk,
input reset,
input [11:0] sw,
output reg [3:0] vgaRed,
output reg [3:0] vgaGreen,
output reg [3:0] vgaBlue,
output Hsync,
output Vsync
);
reg hvedio_on;
reg [9:0]x;
reg [9:0]y;
VGA VGA(clk,reset,Hsync,Vsync,hvedio_on,x,y);
always@(posedge clk,posedge reset)
begin
if(reset == 1 || hvedio_on == 0)
begin
vgaRed = 0;
vgaGreen = 0;
vgaBlue = 0;
end
else
begin
assign vgaRed = sw[3:0];
assign vgaRed = sw[7:4];
assign vgaRed = sw[11:7];
end
end
endmodule
module VGA(
input clk_100,
input reset,
output reg hsync ,
);
reg clk_25;
initial begin
count = 0;
end
if(reset==1 || count == 3)
count = 0;
else
count = count + 1;
always@(posedge clk_100)
begin
case(count)
2'b00:
clk_25 <= 1;
2'b01:
clk_25 = 1;
2'b10:
clk_25 = 0;
2'b11:
clk_25 = 0;
endcase
end
localparam HD = 640, // horizontal display area
HR = 96 , // h. retrace
VR = 2;
begin
if (reset)
begin
x <= 0;
y <= 0;
end
else
begin
x <= x + 1;
else
begin
x <= 0;
y <= y + 1;
else
y <= 0;
end
end
end
always@(posedge clk_25)
begin
assign hvedio_on = (( x < HF + HD && x > HF) && (y > VF && y < VF + VD)) ? 1:0;
end
endmodule
TESTBENCH:-
module test();
reg clk;
reg reset;
wire hsync;
wire vsync;
wire hvedio_on;
wire [9:0]x;
wire [9:0]y;
integer k = 0;
initial begin
reset = 0;
clk = 1;
k = 0;
while(k<10000000)
begin
k = k+1;
end
end
Endmodule
TIMING DIAGRAMS:-
CONSTRAINT FILE:-
##clocksignal
## Switches
##VGA Connector
2)
In this question we have to make some changes in the vga_top module code
of (1) and the rest code part will be same.
Code:-
module vga_top(
input clk,
input reset,
input [11:0] sw,
output reg [3:0] vgaRed,
output reg [3:0] vgaGreen,
output reg [3:0] vgaBlue,
output Hsync,
output Vsync
);
reg hvedio_on;
reg [9:0]x;
reg [9:0]y;
VGA VGA(clk,reset,Hsync,Vsync,hvedio_on,x,y);
localparam r = 30,
cx = 399,
cy = 263;
always@(posedge clk,posedge reset)
begin
if(reset == 1 || hvedio_on == 0 || ((x-cx)*(x-cx) + (y-cy)*(y-cy)) > r*r )
begin
vgaRed = 0;
vgaGreen = 0;
vgaBlue = 0;
end
else
begin
assign vgaRed = sw[3:0];
assign vgaRed = sw[7:4];
assign vgaRed = sw[11:7];
end
end
endmodule
end
end
always@(posedge clk,posedge reset)
begin
case(sel)
4'b0000: r = 30;
4'b0001: r = 50;
4'b0010: r = 100;
4'b1111: r = 150;
default: r = 200;
endcase
end
endmodule
3)
Sourcecode:-
module vga_test(
input clk,
input reset,
output[11:0] rgb,
output h_sync,
output v_sync
);
reg[11:0] rgb_reg;
wire[9:0] x;
wire[9:0] y;
wire video_on;
wire[11:0] rgb_image;
vga_sync vs( clk,reset,x,y,h_sync,v_sync,video_on);
image_memory im_mem(x, y,rgb_image);
assign rgb = video_on?rgb_image:12'd0;
endmodule
module image_memory(
input[9:0] x,
input[9:0] y,
output [11:0] rgb
);
localparam rows = 10'd480;
localparam columns = 10'd640 ;
reg[11:0] rgb_values [0:rows * columns-1];
initial begin
$readmemb("C:\Users\lenovo\Downloads\image.txt", rgb_values);
end
assign rgb = rgb_values[y*rows + x];
endmodule
module vga_sync(
input clk_100,
input reset,
reg clk_25;
initial begin
count = 0;
end
if(reset==1 || count == 3)
count = 0;
else
count = count + 1;
always@(posedge clk_100)
begin
case(count)
2'b00:
clk_25 <= 1;
2'b01:
clk_25 = 1;
2'b10:
clk_25 = 0;
2'b11:
clk_25 = 0;
endcase
end
HR = 96 , // h. retrace
VR = 2;
begin
if (reset)
begin
x <= 0;
y <= 0;
end
else
begin
x <= x + 1;
else
begin
x <= 0;
y <= y + 1;
else
y <= 0;
end
end
end
always@(posedge clk_25)
begin
assign hvedio_on = (( x < HF + HD && x > HF) && (y > VF && y < VF + VD)) ? 1:0;
end
endmodule
TESTBENCH:-
module testbench( );
reg clk;
reg reset;
initial begin
clk = 0;
reset = 0;
#5 reset = ~reset;
#5 reset = ~reset;
end
always #5 clk = ~clk;
wire h_sync,v_sync;
wire[11:0] rgb;
vga_test vt(.clk, reset, h_sync, v_sync,rgb);
endmodule
RTL Analysis:-
CONSTRAINT FILE:-
set_property PACKAGE_PIN W5 [get_ports clk]
##VGA Connector
case (state_reg)
idle:
if (neg_edge & rx_en)
begin
n_next = 4'b1010;
state_next = rx;
end
rx:
begin
if (neg_edge)
begin
d_next = {ps2d, d_reg[10:1]};
n_next = n_reg - 1;
end
if (n_reg==0)
begin
rx_done = 1'b1;
state_next = idle;
end
end
endcase
end
reg[1:0] state,state_next;
always@(*) begin
state_next <= normal_state;
case(state)
normal_state:begin
if(scan_code == 8'hF0)begin
state_next <= ignore_state;
end
else state_next <= normal_state;
end
ignore_state:begin
state_next <= ignore_state2;
end
ignore_state:begin
state_next <= normal_state;
end
endcase
end
end
S2: begin
en = 0;
if(srt == 1)begin
en = 1;
state = S1;
end
if(rst == 1)begin
en = 0;
state = S0;
count = 0;
end
end
endcase
end
always @ (posedge clk or posedge rst)
begin
if(rst)
count <= 0;
else if(count == timer)
count <= 0;
else if(en)
count <= count + 1;
end
);
reg ps2c;
reg ps2d;
reg reset;
wire[7:0] scan_code;
wire done_tick;
initial begin
reset = 0;
#5 reset = 1;
#5 reset = 0;
end
initial begin
ps2c = 1;ps2d = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 0;
#5 ps2c = 1;
end
RTL Analysis:-
CONSTRAINT FILE:-
## Clock signal
## buttons
##leds
5) module VGA(
input clk_100,
input reset,
);
reg clk_25;
initial begin
count = 0;
end
if(reset==1 || count == 3)
count = 0;
else
count = count + 1;
always@(posedge clk_100)
begin
case(count)
2'b00:
clk_25 <= 1;
2'b01:
clk_25 = 1;
2'b10:
clk_25 = 0;
2'b11:
clk_25 = 0;
endcase
end
HR = 96 , // h. retrace
VR = 2;
begin
if (reset)
begin
x <= 0;
y <= 0;
end
else
begin
x <= x + 1;
else
begin
x <= 0;
y <= y + 1;
else
y <= 0;
end
end
end
always@(posedge clk_25)
begin
assign hvedio_on = (( x < HF + HD && x > HF) && (y > VF && y < VF + VD)) ? 1:0;
end
endmodule
module ps2_rx(
input wire clk,reset,
input wire ps2d,ps2c,rx_en,
output wire [7:0] scan_code,
output reg rx_done
);
localparam
idle = 1'b0,
rx = 1'b1;
reg state_reg,state_next;
reg [7:0] f_reg;
reg [7:0] f_nxt;
reg f_reg_value;
reg f_nxt_value;
reg neg_edge;
always@(posedge clk,posedge reset)
begin
if(reset)
begin
f_reg <= 0;
f_reg_value <= 0;
end
else
begin
f_reg <= f_nxt;
f_reg_value <= f_nxt_value;
end
assign f_nxt = {ps2c,f_reg[7:1]};
case (state_reg)
idle:
if (neg_edge & rx_en)
begin
n_next = 4'b1010;
state_next = rx;
end
rx:
begin
if (neg_edge)
begin
d_next = {ps2d, d_reg[10:1]};
n_next = n_reg - 1;
end
if (n_reg==0)
begin
rx_done = 1'b1;
state_next = idle;
end
end
endcase
end
reg[1:0] state,state_next;
always@(*) begin
state_next <= normal_state;
case(state)
normal_state:begin
if(scan_code == 8'hF0)begin
state_next <= ignore_state;
end
else state_next <= normal_state;
end
ignore_state:begin
state_next <= ignore_state2;
end
ignore_state:begin
state_next <= normal_state;
end
endcase
end
##VGA Connector
##Buttons
6)
Sourcecode:-
module multiplier(
input [31:0]in1,
input [31:0]in2,
output reg [31:0]out
);
reg [47:0]mantisa;
reg [23:0]man1;
reg [23:0]man2;
reg [7:0] expo;
reg [1:0]temp;
always@(*) begin
out[31] = in1[31]^in2[31];
expo = in1[30:23] + in2[30:23] - 127;
man1 = {1'b1,in1[22:0]};
man2 = {1'b1,in2[22:0]};
mantisa = man1*man2;
temp = mantisa[47:46];
case(temp)
2'b11:begin
expo = expo + 1;
out[22:0] = mantisa[46:24];
end
2'b10:begin
expo = expo + 1;
out[22:0] = mantisa[46:24];
end
2'b01:out[22:0] = mantisa[45:23];
endcase
out[30:23] = expo;
end
endmodule
TESTBENCH:-
module testbench();
reg [31:0]in1;
reg [31:0]in2;
wire [31:0]out;
initial begin
in1 = 32'b11000001100101000000000000000000;
in2 = 32'b01000001000110000000000000000000;
end
multiplier multi(in1,in2,out);
endmodule
Calculations:-
In1 = -18.5 , in2 = 9.5 .So, output should be -175.75
IEEE Format:-
In1 = 11000001100101000000000000000000
In2 = 01000001000110000000000000000000
Out= 11000011001011111100000000000000
RTL Analysis:-
reg[2:0] state;
always@(posedge a or posedge b)
state_next=state+1;
else p=0;
if(b) q=1;
else q=0;
if(reset)begin
state<=0;
state_next<=0;
end
else begin
case(state_next)
3'b001:
if(q) check[5]=1;
else check[5]=0;
3'b010:
if(q) check[4]=1;
else check[4]=0;
3'b011:
if(q) check[3]=1;
else check[3]=0;
3'b100:
if(q) check[2]=1;
else check[2]=0;
3'b101:
if(q) check[1]=1;
else check[1]=0;
3'b110:
if(q) check[0]=1;
else check[0]=0;
default :xxxxx;
endcase
end
always@(posedge clk)
if(state_next==3'b110&&check==6'b011001)
led=1;
else led=0;
endmodule
LCD interface:-
module PmodCLP(
btnr,CLK, JA, JB );
input btnr; // use BTNR as reset input
input CLK; // 100 MHz clock input
output [7:0] JA;
output [6:4] JB;
wire [7:0] JA;
wire [6:4] JB;
parameter [3:0] stFunctionSet = 0,
stDisplayCtrlSet = 1,
stDisplayClear = 2,
stPowerOn_Delay = 3,
stFunctionSet_Delay = 4,
stDisplayCtrlSet_Delay = 5,
stDisplayClear_Delay = 6,
stInitDne = 7,
stActWr = 8,
stCharDelay = 9;
reg [6:0] clkCount = 7'b0000000;
reg [20:0] count = 21'b000000000000000000000; // 21 bit count variable for timing delays
wire delayOK; // High when count has reached the right delay time
reg oneUSClk; // Signal is treated as a 1 MHz clock
reg [3:0] stCur = stPowerOn_Delay; // LCD control state machine
reg [3:0] stNext;
wire writeDone; // Command set finish
parameter [9:0] LCD_CMDS[0:23] = {
{2'b00, 8'h3C}, // 0, Function Set
{2'b00, 8'h0C}, // 1, Display ON, Cursor OFF, Blink OFF
{2'b00, 8'h01}, // 2, Clear Display
{2'b00, 8'h02}, // 3, Return Home
{2'b10, 8'h48}, // 4, H
{2'b10, 8'h65}, // 5, e
{2'b10, 8'h6C}, // 6, l
{2'b10, 8'h6C}, // 7, l
{2'b10, 8'h6F}, // 8, o
{2'b10, 8'h20}, // 9, blank
{2'b10, 8'h46}, // 10, F
{2'b10, 8'h72}, // 11, r
{2'b10, 8'h6F}, // 12, o
{2'b10, 8'h6D}, // 13, m