verilog_Programs
verilog_Programs
always @( in or en)
begin
if (en)
begin
y=8'd0;
case (in)
3'b000: y= 8'b0000_0001;
3'b001: y= 8'b0000_0010;
3'b010: y= 8'b0000_0100;
3'b011: y= 8'b0000_1000;
3'b100: y= 8'b0001_0000;
3'b101: y= 8'b0010_0000;
3'b110: y= 8'b0100_0000;
3'b111: y= 8'b1000_0000;
default: y=8'd0;
endcase
end
else
y=8'd0;
end
endmodule
assign out = (D0 & S2bar & S1bar & S0bar) | (D1 & S2bar & S1bar & D) |
(D2 & S2bar & C & S0bar) + (D3 & S2bar & C & D) + (D4 & B & S1bar &
S0bar) + (D5 & B & S1bar & D) + (D6 & B & C & S0bar) + (D7 & B & C &
D);
endmodule
/*module full_adder_tb;
reg a,b,cin;
wire sum,carry;
full_adder_d uut(a,b,cin,sum,carry);
initial begin
a = 0; b = 0; cin = 0;
#10 a = 0; b = 0; cin = 1;
#10 a = 0; b = 1; cin = 0;
#10 a = 0; b = 1; cin = 1;
#10 a = 1; b = 0; cin = 0;
#10 a = 1; b = 0; cin = 1;
#10 a = 1; b = 1; cin = 0;
#10 a = 1; b = 1; cin = 1;
#10 $stop();
$monitor(" time=%0d a=%b b=%b cin=%b sum=%b carry=%b",
$time,a,b,cin,sum,cout);
end
endmodule*/
module tb_top;
reg a, b, cin;
wire sum, carry;
initial begin
$monitor("At time %0t: a=%b b=%b, cin=%b, sum=%b, carry=%b",$time,
a,b,cin,sum,carry);
a = 0; b = 0; cin = 0; #1;
a = 0; b = 0; cin = 1; #1;
a = 0; b = 1; cin = 0; #1;
a = 0; b = 1; cin = 1; #1;
a = 1; b = 0; cin = 0; #1;
a = 1; b = 0; cin = 1; #1;
a = 1; b = 1; cin = 0; #1;
a = 1; b = 1; cin = 1;
end
endmodule
5. module ring_count(q,clk,clr);
input clk,clr;
output [3:0]q;
reg [3:0]q;
always @(posedge clk)
if(clr==1)
q<=4'b1000;
else
begin
q[3]<=q[0];
q[2]<=q[3];
q[1]<=q[2];
q[0]<=q[1];
end
endmodule
`timescale 1ns/1ps
module ring_count_test();
reg clk_tb,clr_tb;
wire [3:0]q_tb;
ring_count dut1(q_tb,clk_tb,clr_tb);
initial
begin
clr_tb=1'b0;
#50 clr_tb=1'b1;
#100 clr_tb=1'b0;
#10000 $finish;
end
always
begin
#50 clk_tb=1'b1;
#50 clk_tb=1'b0;
end
initial
begin
$display("time,\t clk_tb,\t clr_tb,\t q_tb");
$monitor("%g,\t %h,\t %h,\t %h",$time,clk_tb,clr_tb,q_tb);
end
endmodule
reg [3:0] q;
if(reset)
q<=4'd0;
else
begin
q[3]<=q[2];
q[2]<=q[1];
q[1]<=q[0];
q[0]<=(~q[3]);
end
end
assign out=q;
endmodule
`timescale 1ns/1ps
module Johnson_count_test();
reg clk_tb,reset_tb;
wire [3:0]q_tb;
Johnson_Counter dut1(q_tb,reset_tb,clk_tb);
initial
begin
$display("time,\t clk_tb,\t reset_tb,\t q_tb");
$monitor("%g,\t %h,\t %h,\t %h",$time,clk_tb,reset_tb,q_tb);
reset_tb=1'b0;
#50 reset_tb=1'b1;
#100 reset_tb=1'b0;
#10000 $finish;
end
always
begin
#50 clk_tb=1'b1;
#50 clk_tb=1'b0;
end
endmodule
7. module universal_shift_reg( clk, rst_n,select,p_din,
s_left_din,s_right_din,p_dout,s_left_dout,s_right_dout );
input clk, rst_n;
input [1:0] select; // select operation
input [3:0] p_din; // parallel data in
input s_left_din; // serial left data in
input s_right_din; // serial right data in
output [3:0] p_dout; //parallel data out
output s_left_dout; // serial left data out
output s_right_dout; // serial right data out
reg [3:0] p_dout;
always@(posedge clk)
begin
if(!rst_n)
p_dout <= 0;
else
begin
case(select)
2'h1: p_dout <= {s_right_din,p_dout[3:1]}; // Right Shift
2'h2: p_dout <= {p_dout[2:0],s_left_din}; // Left Shift
2'h3: p_dout <= p_din; // Parallel in - Parallel out
default: p_dout <= p_dout; // Do nothing
endcase
end
end
assign s_left_dout = p_dout[0];
assign s_right_dout = p_dout[3];
endmodule
module TB;
reg clk, rst_n;
reg [1:0] select;
reg [3:0] p_din;
reg s_left_din, s_right_din;
wire [3:0] p_dout; //parallel data out
wire s_left_dout, s_right_dout;
p_din = 4'b1101;
s_left_din = 1'b1;
s_right_din = 1'b0;
select = 2'h3; #10;
select = 2'h1; #20;
p_din = 4'b1101;
select = 2'h3; #10;
select = 2'h2; #20;
select = 2'h0; #20;
$finish;
end
// To enable waveform
/*initial begin
$dumpfile("dump.vcd"); $dumpvars;
end*/
endmodule
8. module up_counter(clk,reset,counter);
input clk, reset;
output[3:0] counter;
//reg [3:0] counter_up;
reg [3:0] counter;
// up counter
always @(posedge clk or posedge reset)
begin
if(reset) counter
<= 4'd0;
else
counter <= counter + 4'd1;
end
//assign counter = counter_up;
endmodule
module upcounter_testbench();
reg clk, reset;
wire [3:0] counter;
module tb;
reg clk, rst_n;
reg up;
wire [3:0] cnt;
synchronous_counter dut(clk, rst_n, up, cnt);
initial
begin
$display("time,\t clk,\t rst_n,\t up,\t cnt");
$monitor("%g,\t %h,\t %h,\t %h,\t %h",$time,clk,rst_n,up,cnt);
clk = 0; rst_n = 0;
clk = 0; rst_n = 0;
up = 1;
#4;
rst_n = 1;
#80;
rst_n = 0;
#4;
rst_n = 1;
#4
up = 0;
#1000;
$finish;
end
always #2 clk = ~clk;
endmodule
10a. module b_comp1 (a, b, L, E,G);
input a, b;
output L, E, G;
wire s1, s2;
not (s1, a);
not (s2, b);
and (L,s1, b);
and (G,s2, a);
xnor (E, a, b);
endmodule
endmodule
module tb_segment7;
//Apply inputs
initial
begin
for(i = 0;i < 16;i = i+1) //run loop for 0 to 15.
begin
bcd = i;
#10; //wait for 10 ns
end
end
initial
begin
endmodule
//xor gates.
assign G[3] = bin[3];
assign G[2] = bin[3] ^ bin[2];
assign G[1] = bin[2] ^ bin[1];
assign G[0] = bin[1] ^ bin[0];
endmodule
module tb();
// stimulus
always
begin
bin <= 0; #10;
bin <= 1; #10;
bin <= 2; #10;
bin <= 3; #10;
bin <= 4; #10;
bin <= 5; #10;
bin <= 6; #10;
bin <= 7; #10;
bin <= 8; #10;
bin <= 9; #10;
bin <= 10; #10;
bin <= 11; #10;
bin <= 12; #10;
bin <= 13; #10;
bin <= 14; #10;
bin <= 15; #10;
#100;
$stop;
end
initial
begin
endmodule