Vlsi Lab Programs
Vlsi Lab Programs
ALU
module alu_32bit_case(y,a,b,f);
input [31:0]a;
input [31:0]b;
input [2:0]f;
always@(*)
begin
case(f)
3'b000:y=a&b;//AND Operation
3'b001:y=a|b;//OR Operation
3'b010:y=~(a&b);//NAND Operation
3'b011:y=~(a|b);//NOR Operation
3'b101:y=a+b;//Addition
3'b110:y=a-b;//Subtraction
3'b111:y=a*b;//Multiply
default:y=32'bx;
endcase
end
endmodule
module alu_32bit_tb_case;
reg [31:0]a;
reg [31:0]b;
reg [2:0]f;
wire [31:0]y;
alu_32bit_case dut(y,a,b,f);
initial
begin
a=32'h00000000;
b=32'hFFFFFFFF;
#10 f=3'b000;
#10 f=3'b001;
#10 f=3'b010;
#10 f=3'b011;
#10 f=3'b100;
#10 f=3'b101;
#10 f=3'b110;
#10 f=3'b111;
end
initial
#500 $finish;
Endmodule
JK-FF
input clock;
output reg q, qb;
begin
case (JK)
2'b00 : q = q;
2'b01 : q = 1'b0;
2'b10 : q = 1'b1;
2'b11 : q =~ q;
endcase
qb =~ q;
end
endmodule
TESTBENCH-JK FF
module JK_ff_tb;
reg clock;
wire q, qb;
initial
clock=1'b1;
always #5 clock=~clock;
initial
begin
JK=2'b00; #20;
JK=2'b01; #20;
JK=2'b10; #20;
JK=2'b11; #50;
end
initial
Endmodule
D-FF
output reg Q;
output Qb;
input D,clock,reset;
begin
Q <= 1'b0;
else
Q <= D;
end
assign Qb = ~Q;
endmodule
module dff_tb;
initial
begin
clock=1'b1;
reset=1'b0;
end
always #5 clock=~clock;
initial
begin
#10 D =1'b1;
#10 D =1'b0;
#10 D =1'b1;
#10 D =1'b0;
end
initial
#100 $finish;
endmodule
UART
`timescale 1 ns / 1 ps
module uart (
reset ,
txclk ,
ld_tx_data ,
tx_data ,
tx_enable ,
tx_out ,
tx_empty ,
rxclk ,
uld_rx_data ,
rx_data ,
rx_enable ,
rx_in ,
rx_empty
);
// Port declarations
input reset ;
input txclk ;
input ld_tx_data ;
input tx_enable ;
output tx_out ;
output tx_empty ;
input rxclk ;
input uld_rx_data ;
input rx_enable ;
input rx_in ;
output rx_empty ;
// Internal Variables
reg tx_empty ;
reg tx_over_run ;
reg tx_out ;
reg rx_frame_err ;
reg rx_over_run ;
reg rx_empty ;
reg rx_d1 ;
reg rx_d2 ;
reg rx_busy ;
// UART RX Logic
if (reset) begin
rx_reg <= 0;
rx_data <= 0;
rx_sample_cnt <= 0;
rx_cnt <= 0;
rx_frame_err <= 0;
rx_over_run <= 0;
rx_empty <= 1;
rx_d1 <= 1;
rx_d2 <= 1;
rx_busy <= 0;
if (uld_rx_data) begin
rx_empty <= 1;
end
if (rx_enable) begin
rx_busy <= 1;
rx_sample_cnt <= 1;
rx_cnt <= 0;
end
if (rx_sample_cnt == 7) begin
rx_busy <= 0;
end
if (rx_cnt == 9) begin
rx_busy <= 0;
if (rx_d2 == 0) begin
rx_frame_err <= 1;
rx_empty <= 0;
rx_frame_err <= 0;
end
end
end
end
end
end
if ( ! rx_enable) begin
rx_busy <= 0;
end
end
// UART TX Logic
if (reset) begin
tx_reg <= 0;
tx_empty <= 1;
tx_over_run <= 0;
tx_out <= 1;
tx_cnt <= 0;
if (ld_tx_data) begin
if ( ! tx_empty) begin
tx_over_run <= 0;
tx_empty <= 0;
end
end
if (tx_cnt == 0) begin
tx_out <= 0;
end
end
if (tx_cnt == 9) begin
tx_out <= 1;
tx_cnt <= 0;
tx_empty <= 1;
end
end
if ( ! tx_enable) begin
tx_cnt <= 0;
end
end
endmodule
UART-TEST BENCH
`timescale 1 ns / 1 ps
module uart_tb;
// Inputs
reg reset;
reg txclk;
reg ld_tx_data;
reg tx_enable;
reg rxclk;
reg uld_rx_data;
reg rx_enable;
reg rx_in;
// Outputs
wire tx_out;
wire tx_empty;
wire rx_empty;
uart uut (
.reset(reset),
.txclk(txclk),
.ld_tx_data(ld_tx_data),
.tx_data(tx_data),
.tx_enable(tx_enable),
.tx_out(tx_out),
.tx_empty(tx_empty),
.rxclk(rxclk),
.uld_rx_data(uld_rx_data),
.rx_data(rx_data),
.rx_enable(rx_enable),
.rx_in(rx_in),
.rx_empty(rx_empty)
);
reg clk;
//setup clocks
initial clk=0;
always #10 clk = ~clk; //this speed is somewhat arbitrary for the purposes of this sim...clk
should be 16X faster than desired baud rate. I like my simulation time to match the physical
system.
//generate rxclk and txclk so that txclk is 16 times slower than rxclk
initial begin
rxclk=0;
txclk=0;
counter=0;
end
counter<=counter+1;
rxclk<= ~rxclk;
end
//setup loopback
initial begin
// Initialize Inputs
reset = 1;
ld_tx_data = 0;
tx_data = 0;
tx_enable = 1;
uld_rx_data = 0;
rx_enable = 1;
` rx_in = 1;
#500;
reset = 0;
// Add stimulus here
// Send data using tx portion of UART and wait until data is recieved
tx_data=8'b0111_1111;
#1200 tx_data=8'b0000_1111;
#1200 tx_data=8'b1111_0000;
ld_tx_data = 0;
$display("Data sent");
uld_rx_data = 1;
wait (rx_empty==1);
#100;
$stop;
end
endmodule
4BIT-COUNTER
`timescale 1ns/1ps
//Defining Module
module counter(clk,rst,m,count);
input clk,rst,m;
begin
if(!rst)
count=0;
if(m)
count=count+1;
else
count=count-1;
end
endmodule
Initial
begin
end
initial
begin
rst=0;#25;
rst=1;
#500 m=0;
end
initial
endmodule