DD LAB
DD LAB
IT ON FPGA
AIM:
TOOLS REQUIRED:
1. Hardware:
FPGA Development Board (e.g., Xilinx, Intel
Altera)
Oscilloscope (for output verification, if needed)
JTAG Programmer
2. Software:
THEORY:
DEFINITION:
PROCEDURE:
N = 7; % Filter order
Clock (clk), Reset (rst), Input Signal (x), Output Signal (y).
PROGRAM:
Module fir_filter (
Input clk,
Input reset,
Input [15:0] x,
);
Integer I;
Initial begin
h[0] = 16’d1;
h1] = 16’d2;
h[2] = 16’d3;
End
If (reset) begin
Y <= 32’d0;
Shift_reg[0] <= x;
End
End
Endmodule
TESTBENCH CODE:
Module fir_filter_tb;
Reg clk;
Reg reset;
Reg [15:0] x;
Wire [31:0] y;
Fir_filter uut (
.clk(clk),
.reset(reset),
.x(x),
.y(y)
);
Initial begin
Clk = 0;
Initial begin
Reset = 1;
X = 16’d0;
#10;
Reset = 0;
#10;
X = 16’d10;
#10;
X = 16’d20;
#10;
X = 16’d30;
#10;
X = 16’d40;
#10;
X = 16’d50;
#10;
$finish;
End
Initial begin
$dumpfile(“fir_filter_tb.vcd”);
$dumpvars(1, fir_filter_tb);
End
Endmodule
RESULT:
AIM:
TOOLS REQUIRED:
1. Hardware:
FPGA Development Board (e.g., Xilinx, Intel Altera)
Seven-Segment Display (if using hardware display)
Clock Source (On-board or external)
JTAG Programmer
2. Software:
HDL Development Environment (e.g., Xilinx)
THEORY:
Seconds Counter: Counts from 0 to 59, then resets and increments the
minutes counter.
Minutes Counter: Counts from 0 to 59, then resets and increments the
hours counter.
Hours Counter: Counts from 0 to 23 (for 24-hour format) and then
resets.
F
igure 1: Digital Clock Block Diagram
PROCEDURE:
Since the FPGA operates at 50 MHz, the clock must be divided down
to 1 Hz:
Formula:
The minutes and hours counters follow a similar logic, with cascading
carry signals.
Module digital_clock(
);
Reg tick;
If (reset) begin
Count <= 0;
Tick <= 0;
Tick <= 1;
Count <= 0;
Tick <= 0;
End
End
End
Always @(posedge tick or posedge reset) begin
If (reset) begin
Sec <= 0;
Min <= 0;
Hour <= 0;
Sec <= 0;
Min <= 0;
Hour <= 0;
End
End
End
End
End
Endmodule
TESTBENCH CODE:
Module digital_clock_tb;
Reg clk_tb;
Reg reset_tb;
Wire [5:0] sec_tb;
Wire [5:0] min_tb;
Wire [4:0] hour_tb;
Digital_clock uut (
.clk(clk_tb),
.reset(reset_tb),
.sec(sec_tb),
.min(min_tb),
.hour(hour_tb)
);
Always begin
#10 clk_tb = ~clk_tb;
End
Initial begin
Clk_tb = 0;
Reset_tb = 0;
Reset_tb = 1;
#20 reset_tb = 0;
$dumpfile(“digital_clock_tb.vcd”);
$dumpvars(1, digital_clock_tb);
#1000;
$finish;
End
Endmodule
RESULT:
AIM:
APPARATUS REQUIRED:
1. Hardware:
FPGA Development Board (e.g., Xilinx, Intel Altera)
Push Buttons (for coin inputs)
Seven-Segment Display or LED Indicators (for output
display)
JTAG Programmer
2. Software:
THEORY:
FSM Design:
Inputs:
Outputs:
PROCEDURE:
The FSM should handle state transitions based on the coin input.
1. Write a Testbench:
1. Press Button 1 (5¢) and Button 2 (10¢) and observe LED output.
PROGRAM:
Module vending_machine (
Input clk,
Input reset,
);
IDLE = 3’b000,
SELECT_PRODUCT = 3’b001,
INSERT_MONEY = 3’b010,
DISPENSE = 3’b011,
ERROR = 3’b100
} state_t;
If (reset) begin
End
End
Case (current_state)
IDLE: begin
Product_dispensed = 0;
Dispensed_product = 2’b00;
Error = 0;
If (product_select != 2’b00)
Next_state = SELECT_PRODUCT;
Else
Next_state = IDLE;
End
SELECT_PRODUCT: begin
Error = 0;
If (product_select == 2’b00)
Next_state = ERROR;
Else
Next_state = INSERT_MONEY;
End
INSERT_MONEY: begin
Next_state = DISPENSE;
Next_state = INSERT_MONEY;
End
End
DISPENSE: begin
Product_dispensed = 1;
Dispensed_product = product_select;
Next_state = IDLE;
End
ERROR: begin
Error = 1;
Next_state = IDLE;
End
Endcase
End
Endmodule
TESTBENCH CODE:
Module tb_vending_machine;
Reg clk;
Reg reset;
Wire product_dispensed;
Wire error;
Vending_machine uut (
.clk(clk),
.reset(reset),
.product_select(product_select),
.money_in(money_in),
.product_dispensed(product_dispensed),
.dispensed_product(dispensed_product),
.error(error)
);
Always begin
#5 clk = ~clk;
End
Initial begin
Clk = 0;
Reset = 0;
Product_select = 2’b00;
Money_in = 4’b0000;
Reset = 1;
#10 reset = 0;
Product_select = 2’b01;
Money_in = 4’d5;
#10;
Product_select = 2’b10;
Money_in = 4’d3;
#10;
Product_select = 2’b11;
Money_in = 4’d9;
#10;
Product_select = 2’b00;
Money_in = 4’d5;
#10;
$finish;
End
Initial begin
$dumpfile(“dump.vcd”);
$dumpvars(1);
End
Endmodule
RESULT:
AIM:
TOOLS REQUIRED:
1. Hardware:
FPGA Development Board (Xilinx, Intel Altera)
Push buttons (for player input)
LEDs/Seven-Segment Display (to show the game state)
VGA Display (optional) for graphical interface
JTAG Programmer
2. Software:
HDL Development Environment (Xilinx Vivado, Intel
Quartus)
Simulation Tool (ModelSim, ISim)
THEORY:
Tic-Tac-Toe Overview:
The FSM handles player moves, checks for a win, and resets
the board.
FSM States:
PROCEDURE:
The game consists of a 3×3 grid where two players take turns
placing X (Player 1) or O (Player 2).
The system should track moves, check for a win, and handle
draws.
The design will use a Finite State Machine (FSM) to control the
game flow.
FSM States:
PROGRAM:
Module tic_tac_toe(
Input clk,
Input rst_n,
Input make_move,
);
If (!rst_n) begin
If (check_win(board_next)) begin
Game_over <= 1;
End
End
End
End
Function check_win;
Input [8:0] b;
Begin
Check_win = (b[0] && b[1] && b[2]) || (b[3] && b[4] && b[5]) || (b[6]
&& b[7] && b[8]);
Check_win = check_win || (b[0] && b[3] && b[6]) || (b[1] && b[4] &&
b[7]) || (b[2] && b[5] && b[8]);
Check_win = check_win || (b[0] && b[4] && b[8]) || (b[2] && b[4] &&
b[6]);
End
Endfunction
Endmodule
TESTBENCH CODE:
Module tb_tic_tac_toe;
Reg clk;
Reg rst_n;
Reg make_move;
Wire game_over;
Tic_tac_toe uut (
.clk(clk),
.rst_n(rst_n),
.player(player),
.row(row),
.col(col),
.make_move(make_move),
.board(board),
.game_over(game_over),
.winner(winner)
);
Always begin
#5 clk = ~clk;
End
Initial begin
Clk = 0;
Rst_n = 0;
Player = 2’b01;
Row = 0;
Col = 0;
Make_move = 0;
#10 rst_n = 1;
#10 make_move = 0;
#10 make_move = 0;
#10 make_move = 0;
#10 make_move = 0;
#20 $finish;
End
Initial begin
$dumpfile(“tic_tac_toe.vcd”);
$dumpvars(1, tb_tic_tac_toe);
End
Endmodule
RESULT:
AIM:
TOOLS REQUIRED:
1. Computer with HDL development environment (such as Xilinx
Vivado, Quartus Prime, or ModelSim)
THEORY:
1. Image Representation:
Images are stored as pixel arrays, where each pixel
contains color/intensity values.
Grayscale images use 8-bit intensity values, while RGB
images use 24-bit values (8 bits per channel).
2. Image Storage in HDL:
Images are stored in memory modules like BRAM in
FPGAs or external RAM.The HDL code reads pixel values
from memory, processes them, and writes them back.
3. Common Image Processing Algorithms:
Edge Detection: Uses Sobel or Prewitt filters to detect
edges.
Image Thresholding: Converts an image to black and
white based on intensity values.
Image Smoothing: Uses a moving average or Gaussian
filter to reduce noise.
Contrast Enhancement: Modifies pixel intensity to
improve visibility.
4. HDL Implementation:
HDL cannot directly read image files like Python or
MATLAB. Instead, the image is converted into pixel
values and stored in memory (using an initialization file
or a memory block).
HDL processes the pixels sequentially and produces the
output in a similar format.
PROCEDURE:
4. Testbench Simulation:
PROGRAM:
Module image_processing #(
)(
Input clk,
Input rst_n,
Input valid_in,
);
If (~rst_n) begin
Valid_out <= 1;
Valid_out <= 0;
End
End
Endmodule
TESTBENCH CODE:
Module tb_image_processing;
Reg clk;
Reg rst_n;
Reg valid_in;
Wire valid_out;
Image_processing uut (
.clk(clk),
.rst_n(rst_n),
.pixel_in(pixel_in),
.valid_in(valid_in),
.pixel_out(pixel_out),
.valid_out(valid_out)
);
Always begin
#5 clk = ~clk;
End
Task reset;
Begin
Rst_n = 0;
#10 rst_n = 1;
End
Endtask
Task test_image;
Integer I;
Begin
Pixel_in = (I == 0) ? 24’hFF0000 :
(I == 1) ? 24’h00FF00 :
(I == 2) ? 24’h0000FF :
24’hFFFFFF;
Valid_in = 1;
#10 valid_in = 0;
#10;
End
End
Endtask
Initial begin
Clk = 0;
Rst_n = 0;
Pixel_in = 0;
Valid_in = 0;
Reset;
Test_image;
$dumpfile(“image_processing.vcd”);
$dumpvars(1, tb_image_processing);
#100 $finish;
End
Endmodule
RESULT:
AIM:
TOOLS REQUIRED:
1. HDL Development Environment (Xilinx Vivado, Quartus Prime,
ModelSim, etc.)
2. Verilog or VHDL Compiler and Simulator
3. FPGA Board (Optional for hardware implementation)
4. Motion Sensor (PIR Sensor)
5. Window Sensor (Magnetic Reed Switch)
6. Siren or LED Indicator for Alarm
THEORY:
PROCEDURE:
PROGRAM:
Module burglar_alarm (
);
IDLE = 2’b00,
MOTION = 2’b01,
ALARM = 2’b10
} state_t;
If (reset) begin
End
End
Always @(*) begin
Case(state)
IDLE: begin
If (motion_detected)
Next_state = MOTION;
Else
Next_state = IDLE;
End
MOTION: begin
Next_state = ALARM;
End
ALARM: begin
Next_state = ALARM;
End
Endcase
End
If (reset) begin
Alarm_triggered <= 0;
If (state == ALARM)
Alarm_triggered <= 1;
Else
Alarm_triggered <= 0;
End
End
Endmodule
TESTBENCH CODE:
Module tb_burglar_alarm;
Reg clk;
Reg reset;
Reg motion_detected;
Wire alarm_triggered;
Burglar_alarm uut (
.clk(clk),
.reset(reset),
.motion_detected(motion_detected),
.alarm_triggered(alarm_triggered)
);
Initial begin
Clk = 0;
Reset = 0;
Motion_detected = 0;
$monitor(“Time: %0t | Reset: %b | Motion: %b | Alarm: %b”, $time,
reset, motion_detected, alarm_triggered);
Reset = 1;
#10;
Reset = 0;
Motion_detected = 0;
#20;
Motion_detected = 1;
#10;
Motion_detected = 0;
#30;
Reset = 1;
#10;
Reset = 0;
$finish;
End
Initial begin
$dumpfile(“burglar_alarm.vcd”);
$dumpvars(1, tb_burglar_alarm);
End
Endmodule
RESULT: