VIVADO Codes Edit
VIVADO Codes Edit
Form :-
module T_FF_Form(
output reg Q,
output reg Qb ,
input T,
input clk
);
initial begin
Q=1;Qb=0;
end
always @(negedge clk )begin
if(T==0) begin
Q <=Q;Qb<=Qb;
end
module MOD_16_form(
output Qc0,
output Qc1,
output Qc2,
output Qc3,
input T1,
input clk,
input M
);
wire Qb0;
wire Qb1;
wire Qb2;
wire Qb3;
);
wire Qc3;
wire Qc2;
wire Qc1;
wire Qc0;
reg T1;
reg clk;
reg M;
MOD_16_form uut(.Qc0(Qc0),.Qc1(Qc1),.Qc2(Qc2),.Qc3(Qc3),.T1(T1),.clk(clk),.M(M));
always #25 clk=~clk;
initial begin
clk=1;
T1=1;M=0;
#1000
$finish ;
end
endmodule
input clk
);
initial begin
Q = 0; // Initialize Q
Qb = 1; // Initialize Qb (the complement of Q)
end
else if (T == 1 ) begin
Q <= ~Q; // Toggle
Qb <= ~Qb;
end
end
endmodule
module counter_form(
output Qc0,
output Qc1,
output Qc2,
output Qc3,
input M,
input T,
input clk
);
// initial begin
// Qc = 3'b000; // Initialize the counter output
// end
test :
module test();
reg M;
// Clock and JK input registers
reg T;
reg clk;
.Qc1(Qc1),
.Qc2(Qc2),
.Qc3(Qc3),
.T(T),
.M(M),
.clk(clk)
);
// Initialize clock and run the simulation for 1000 time units
initial begin
3) MOD 16 UP asyn
Form :-
module T_FF_Form(
output reg Q,
output reg Qb,
input T,
input clk
);
initial begin
Q = 0; // Initialize Q
Qb = 1; // Initialize Qb (the complement of Q)
end
else if (T == 1 ) begin
Q <= ~Q; // Toggle
Qb <= ~Qb;
end
end
endmodule
module Counters_UP_3_form(
output Qc0,
output Qc1,
output Qc2,
output Qc3,
input T,
input clk
);
// initial begin
// Qc = 3'b000; // Initialize the counter output
// end
Endmodule
Test :-
module Counters_UP_sim();
reg clk;
.Qc1(Qc1),
.Qc2(Qc2),
.Qc3(Qc3),
.T(T),
.clk(clk)
);
// Initialize clock and run the simulation for 1000 time units
initial begin
endmodule
4) MOD 16 UP syn
Form :
module T_FF_Form(
output reg Q,
output reg Qb ,
input T,
input clk
);
initial begin
Q=1;Qb=0;
end
always @(negedge clk )begin
if(T==0) begin
Q <=Q;Qb<=Qb;
end
else if(T==1 ) begin
Q <= ~Q; Qb<= ~Qb;
end
end
endmodule
module counter_form(
output Qc0,
output Qc1,
output Qc2,
output Qc3,
input T1,
input clk
);
wire Qb0;
wire Qb1;
wire Qb2;
wire Qb3;
Test :-
module test(
);
wire Qc3;
wire Qc2;
wire Qc1;
wire Qc0;
reg T1;
reg clk;
counter_form uut(.Qc0(Qc0),.Qc1(Qc1),.Qc2(Qc2),.Qc3(Qc3),.T1(T1),.clk(clk));
initial begin
clk=0;
T1=1;
#1000
$finish ;
end
always #25 clk=~clk;
endmodule
5) MOD 9 syn
Form :
module T_FF_Form(
output reg Q,
output reg Qb ,
input T,
input clk
);
initial begin
Q=1;Qb=0;
end
always @(negedge clk )begin
if(T==0) begin
Q <=Q;Qb<=Qb;
end
module counter(
output Qc0,
output Qc1,
output Qc2,
output Qc3,
//input T1,
input clk
);
wire Qb0;
wire Qb1;
wire Qb2;
wire Qb3;
Test :-
module test(
);
wire Qc3;
wire Qc2;
wire Qc1;
wire Qc0;
// reg T1;
reg clk;
counter uut(.Qc0(Qc0),.Qc1(Qc1),.Qc2(Qc2),.Qc3(Qc3),.clk(clk));
initial begin
clk=1;
//T1=1;
#1000
$finish ;
end
always #25 clk=~clk;
endmodule
6) MOD 9 asyn :-
Form :
module counter(
input clk,
output [3:0]q);
wire reset;
assign reset=q[3]&q[0];
t_ff t0(1,clk,reset,q[0]);
t_ff t1(1,q[0],reset,q[1]);
t_ff t2(1,q[1],reset,q[2]);
t_ff t3(1,q[2],reset,q[3]);
endmodule
module t_ff(
input t,clk,reset,
output reg q);
initial begin
q=0;
end
always@(negedge clk or posedge reset)
begin
if(reset==1)
begin
q=0;
end
else
begin
if(t==0)
begin
q<=q;
end
if(t==1)
begin
q<=~q;
end
end
end
endmodule
tb:-
module test();
reg clk;
wire [3:0]q;
counter dut(.clk(clk),.q(q));
always #25 clk=~clk;
initial begin
clk=0;
#1000;
$finish;
end
endmodule
7) MOD 9 behave
Form ;
module counter(
input clk,rst,enable,
output reg [3:0]counter_output
);
test :-
module test;
// Inputs
reg clk;
reg rst;
reg enable;
// Outputs
wire [3:0] counter_output;
initial begin
clk=1;
// Initialize Inputs
rst = 0;
enable = 0;
#100;
rst=0;
enable=1;
#100;
rst=0;
enable=1;
module counter(
input clk,rst,enable,
output reg [3:0]counter_output
);
test :-
module mod_10_counter_test;
// Inputs
reg clk;
reg rst;
reg enable;
// Outputs
wire [3:0] counter_output;
initial begin
clk=1;
// Initialize Inputs
rst = 0;
enable = 0;
#100;
rst=0;
enable=1;
#100;
rst=0;
enable=1;
9) MOD 16 UD behave :
Form :
module counter(
Clk,
reset,
UpOrDown, //high for UP counter and low for Down counter
Count
);
endmodule
test :-
module test;
// Inputs
reg Clk;
reg reset;
reg UpOrDown;
// Outputs
wire [3:0] Count;
initial begin
// Apply Inputs
reset = 0;
UpOrDown = 0;
#300;
// UpOrDown = 1;
// #300;
// reset = 1;
// UpOrDown = 0;
// #100;
// reset = 0;
end
endmodule
10) seq_det_mealy_1010_over :-
form :-
module seq_det (
input clk,
input rst,
input in,
output reg out
);
// Define states
parameter A = 2'b00, B = 2'b01, C = 2'b10, D = 2'b11;
reg [1:0] CS, NS;
case (CS)
A: begin
NS = (in == 1) ? B : A;
out = 1'b0;
end
B: begin
NS = (in == 0) ? C : B;
out = 1'b0;
end
C: begin
NS = (in == 1) ? D : A;
out = 1'b0;
end
D: begin
NS = (in == 0) ? C : B;
out = (in == 0) ? 1'b1 : 1'b0; // Output 1 for sequence 1010
end
default: begin
NS = A;
out = 1'b0;
end
endcase
end
endmodule
test :-
module test;
// Testbench signals
reg clk;
reg rst;
reg in;
wire out;
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk; // Clock period: 10ns
end
// Test sequence
initial begin
// Initialize inputs
rst = 0;
in = 0;
// Apply reset
#7 rst = 1; // Assert reset after 7ns
// End simulation
#50 $stop;
end
// Monitor signals
endmodule
11) seq_det_mealy_1010_non_over :-
Form :-
module seq_det (
input clk,
input rst,
input in,
output reg out
);
// Define states
parameter A = 2'b00, B = 2'b01, C = 2'b10, D = 2'b11;
reg [1:0] CS, NS;
case (CS)
A: begin
NS = (in == 1) ? B : A;
out = 1'b0;
end
B: begin
NS = (in == 0) ? C : B;
out = 1'b0;
end
C: begin
NS = (in == 1) ? D : A;
out = 1'b0;
end
D: begin
NS = (in == 0) ? A : B;
out = (in == 0) ? 1'b1 : 1'b0; // Output 1 for sequence 1010
end
default: begin
NS = A;
out = 1'b0;
end
endcase
end
endmodule
test :-
module test;
// Testbench signals
reg clk;
reg rst;
reg in;
wire out;
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk; // Clock period: 10ns
end
// Test sequence
initial begin
// Initialize inputs
rst = 0;
in = 0;
// Apply reset
#7 rst = 1; // Assert reset after 7ns
// End simulation
#50 $stop;
end
// Monitor signals
endmodule
12) seq_det_moore_1010_over :-
form :-
module seq_det (
input clk,
input rst,
input in,
output reg out
);
// Define states
parameter A = 3'b000, B = 3'b001, C = 3'b010, D = 3'b011,E=3'b100;
reg [2:0] CS, NS;
case (CS)
A: begin
NS = (in == 1) ? B : A;
out = 1'b0;
end
B: begin
NS = (in == 0) ? C : B;
out = 1'b0;
end
C: begin
NS = (in == 1) ? D : A;
out = 1'b0;
end
D: begin
NS = (in == 0) ? E : B;
out = (in == 0) ? 1'b1 : 1'b0; // Output 1 for sequence 1010
end
E: begin
NS = (in == 0) ? A : D;
out = 1'b0 ;// Output 1 for sequence 1010
end
default: begin
NS = A;
out = 1'b0;
end
endcase
end
endmodule
test :-
module test;
// Testbench signals
reg clk;
reg rst;
reg in;
wire out;
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk; // Clock period: 10ns
end
// Test sequence
initial begin
// Initialize inputs
rst = 0;
in = 0;
// Apply reset
#7 rst = 1; // Assert reset after 7ns
// Monitor signals
endmodule
13) seq_det_moore_1010_non_over :-
form :-
module seq_det (
input clk,
input rst,
input in,
output reg out
);
// Define states
parameter A = 3'b000, B = 3'b001, C = 3'b010, D = 3'b011,E=3'b100;
reg [2:0] CS, NS;
case (CS)
A: begin
NS = (in == 1) ? B : A;
out = 1'b0;
end
B: begin
NS = (in == 0) ? C : B;
out = 1'b0;
end
C: begin
NS = (in == 1) ? D : A;
out = 1'b0;
end
D: begin
NS = (in == 0) ? E : B;
out = (in == 0) ? 1'b1 : 1'b0; // Output 1 for sequence 1010
end
E: begin
NS = (in == 0) ? A : B;
out = 1'b0 ;// Output 1 for sequence 1010
end
default: begin
NS = A;
out = 1'b0;
end
endcase
end
endmodule
test :-
module test;
// Testbench signals
reg clk;
reg rst;
reg in;
wire out;
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk; // Clock period: 10ns
end
// Test sequence
initial begin
// Initialize inputs
rst = 0;
in = 0;
// Apply reset
#7 rst = 1; // Assert reset after 7ns
// Monitor signals
endmodule
14) seq_gen_10110_behave :-
form :-
module form (
input clk,
input rst,
case (CS)
A: begin
NS = B;
out = 1'b1;
end
B: begin
NS = C ;
out = 1'b0;
end
C: begin
NS = D ;
out = 1'b1;
end
D: begin
NS = E;
out = 1'b1; // Output 1 for sequence 1010
end
E: begin
NS = A;
out = 1'b0; // Output 1 for sequence 1010
end
default: begin
NS = A;
out = 1'b0;
end
endcase
end
endmodule
tb:-
module tb;
// Testbench signals
reg clk;
reg rst;
wire out;
.out(out)
);
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk; // Clock period: 10ns
end
// Test sequence
initial begin
// Initialize inputs
rst = 0;
// Apply reset
#7 rst = 1; // Assert reset after 7ns
// End simulation
#200 $stop;
end
// Monitor signals
endmodule
15) Seq_gen_10110_str:-
Form :-
module D_FF(
output reg Q,
output reg Qb,
input D,
input clk,
input rst
);
initial begin
Q = 0;
Qb = 1;
end
module tb;
reg clk;
reg rst;
// reg reset;
wire Q;
form uut (
.Q(Q),
.clk(clk),
.rst(rst)
);
initial begin
clk = 1;
forever #5 clk = ~clk; // Clock period: 10ns
end
// Test sequence
initial begin
// Initialize inputs
rst = 0;
// in = 0;
// Apply reset
#7 rst = 1; // Assert reset after 7ns
#300;
$finish;
end
endmodule
16) USR_behave:-
Form :-
module form (
input clk, rst,
input [1:0] select, // select operation
input [3:0] pin, // parallel data in
input left_in, // serial left data in
input right_in, // serial right data in
output reg [3:0] pout, // parallel data out
output reg left_out, // serial left data out
output reg right_out // serial right data out
);
always @(posedge clk or negedge rst) begin
if (!rst)
pout <= 4'b0; // Clear pout on reset
else begin
case(select)
2'b01: pout <= {right_in, pout[3:1]}; // Right Shift
2'b10: pout <= {pout[2:0], left_in}; // Left Shift
2'b11: pout <= pin; // Parallel in
default: pout <= pout; // Retain current value
endcase
end
end
tb:-
module tb;
// Testbench signals
reg clk, rst;
reg [1:0] select;
reg [3:0] pin;
reg left_in, right_in;
wire [3:0] pout;
wire left_out, right_out;
// Instantiate the module under test
form uut (
.clk(clk),
.rst(rst),
.select(select),
.pin(pin),
.left_in(left_in),
.right_in(right_in),
.pout(pout),
.left_out(left_out),
.right_out(right_out)
);
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk; // Generate a clock with a period of 10 time units
end
// Testbench logic
initial begin
// Initialize signals
rst = 0;
select = 2'b00;
pin = 4'b0000;
left_in = 0;
right_in = 0;
// Apply reset
#10 rst = 1;
#10
pin = 4'b1011;
select = 2'b11;
#10 select = 2'b00; // Wait for operation
// End simulation
#50 $stop;
end
17) USR_str:-
Form :-
module MUX_4_1(
input D0,
input D1,
input D2,
input D3,
input [1:0] Select,
output Y
);
assign Y = ((~Select[1]) & (~Select[0]) & D0) |
((~Select[1]) & Select[0] & D1) |
(Select[1] & (~Select[0]) & D2) |
(Select[1] & Select[0] & D3);
endmodule
module D_FF(
output reg Q,
input D,
input clk,
input rst
);
always @(posedge clk or posedge rst) begin
if (rst) begin
Q <= 0; // Reset state
end else begin
Q <= D; // Update state
end
end
endmodule
module form(
input P0, P1, P2, P3, // Parallel inputs
input Sr, Sl, // Serial inputs (right and left)
input [1:0] Select, // Mode select
input clk,
input rst,
output Q0, Q1, Q2, Q3 // Parallel outputs
);
wire C0, T0, T1, T2, Y1, Y2, Y3, Y4;
tb:-
module tb;
reg P0, P1, P2, P3; // Parallel inputs
reg Sr, Sl; // Serial inputs (right and left)
reg [1:0] Select; // Mode select
reg clk, rst; // Clock and reset
wire Q0, Q1, Q2, Q3; // Parallel outputs
// Clock generation
always #5 clk = ~clk;
initial begin
// Initialize inputs
clk = 0;
rst = 1;
P0 = 0; P1 = 0; P2 = 0; P3 = 0;
Sr = 0; Sl = 0;
Select = 2'b00;
// Apply reset
#10 rst = 0;
18) SBA :-
Form :-
endmodule
sr s1(clk,rst,control,si,a,w1);
sr s2(clk,rst,control,si,b,w2);
fa f1(w1[0],w2[0],cin,z,carry);
d_ff d1(clk,rst,carry,cin);
sr s3(clk,rst,control,z,c,o);
endmodule
tb:-
module tb( );
reg clk,rst,control,si;
reg [3:0]a,b,c;
wire carry,z;
wire [3:0]o;
form uut(clk,rst,control,si,a,b,c,carry,z,o);
initial begin a=4'b1010;b=4'b1101; end
initial begin
clk=0;control=0;rst=0;c=4'b0000;
si=0;#5;rst=1;
si=0;#5;
control=1;
si=0;#5;
si=0;#5;
#100
rst=0;control=0;a=4'b1101 ; b=4'b1011;
si=0;
#5;rst=1;
si=0;
#5;control=1;
#100
//rst=0;control=0;a=4'b1101 ; b=4'b0111;
//si=0;
//#5;rst=1;
//si=0;
//#5;control=1;
//#100
$finish;
end
always #5 clk=~clk;
endmodule
19) Shift_add_mult :-
Form :-
module sa (
input [3:0] M, Q,
);
reg [3:0] a, q;
reg c;
if (!rst) begin
a <= 4'b0000;
q <= 4'b0000;
c <= 1'b0;
Y <= 8'b00000000;
end
else begin
q = Q; // Load Q
c = 1'b0;
if (q[0]) begin
end
end
end
end
endmodule
tb:-
module tb();
reg [3:0] M, Q;
wire [7:0] Y;
// reg [3:0] a, q;
// reg c;
sa uut (
initial begin
$dumpfile("waveform.vcd");
$dumpvars(0, tb);
clk = 0;
rst = 0;
M = 4'b0000;
Q = 4'b0000;
#20 rst = 1;
M = 4'b0011; Q = 4'b0100;
rst = 0; #10;
rst = 1;
M = 4'b1011; Q = 4'b0110;
#10;
#20
// End simulation
// #100;
$finish;
end
endmodule
module restoring(
input [3:0] Q, // Dividend
input [3:0] M,
input clk,rst, // Divisor
output reg [4:0] An, // Remainder
output reg [3:0] Qn // Quotient
);
reg [4:0] A, Mn;
//reg[4:0]c;
integer i;
always @(posedge clk or negedge rst) begin
if(!rst) begin
A<=5'b00000;
An<=5'b00000;
Qn <= 4'b0000;
Mn <= {1'b0, M};
end
else begin
A=5'b00000;
Qn = Q;
Mn = {1'b0, M};
for(i=0;i<4;i=i+1) begin
{A,Qn}={A,Qn}<<1;
A =A-Mn;
if(A[4]==1'b1) begin
A=A+Mn;
Qn[0]=1'b0;
end
else begin
Qn[0]=1'b1;
end
end
An<=A;
end
end
// Qn=Q;
Endmodule
Tb:-
module tb;
reg clk, rst;
reg [3:0] Q, M;
wire [3:0] Qn;
// Clock generation
always #5 clk = ~clk;
initial begin
// Initialize signals
clk = 0;
rst = 0;
Q = 4'b0000;
M = 4'b0000;
// Hold reset for at least 1 full clock cycle
#10 rst = 1;
// End simulation
$finish;
end
// Monitor outputs
initial begin
$monitor("Time = %0t | Q = %b | M = %b | Qn = %b | An = %b",
$time, Q, M, Qn, An);
end
endmodule
module non_restoring(
input [3:0] M,
);
reg[4:0]c;
integer i;
A<=5'b00000;
An<=5'b00000;
Qn <= 4'b0000;
end
else begin
A=5'b00000;
Qn = Q;
Mn = {1'b0, M};
for(i=0;i<4;i=i+1) begin
{A,Qn}={A,Qn}<<1;
if(A[4]==1'b0) begin
A=A-Mn;
end
else begin
A=A+Mn;
end
Qn[0]=~A[4];
end
if(A[4]==1'b1 ) begin
A=A+Mn;
end
An=A;
end
end
// Qn=Q;
Endmodule
Tb:-
module tb;
reg [3:0] Q, M;
non_restoring uut (
.Q(Q),
.M(M),
.clk(clk),
.rst(rst),
.An(An),
.Qn(Qn)
);
// Clock generation
initial begin
// Initialize signals
clk = 0;
rst = 0;
Q = 4'b0000;
M = 4'b0000;
#10 rst = 1;
Q = 4'b1110; // 15
M = 4'b0100; // 4
Q=4'b1001;
M=4'b0101;#50
// End simulation
$finish;
end
// Monitor outputs
initial begin
end
endmodule
module booth (
input [3:0] M, Q,
reg [3:0] a, q;
reg qb;
integer i;
if (!rst) begin
a <= 4'b0000;
q <= 4'b0000;
qb <= 1'b0;
Y <= 8'b00000000;
end
else begin
q = Q; // Load Q
qb = 1'b0;
qb = q[0];
end
qb = q[0];
a = a + M;
{a, q} = {a, q} >> 1;
end
// Add M to accumulator
qb = q[0];
a = a - M;
end
end
Y = {a, q};
end
end
endmodule
tb:-
module tb();
reg [3:0] M, Q;
wire [7:0] Y;
// reg [3:0] a, q;
// reg c;
booth uut (
initial begin
clk = 0;
rst = 0;
M = 4'b0000;
Q = 4'b0000;
#20 rst = 1;
M = 4'b0111; Q = 4'b0101;
rst = 0; #10;
rst = 1;
M = 4'b0101; Q = 4'b0100;
rst = 0; #10;
rst = 1;
rst = 0; #10;
rst = 1;
#10;
#30;
// End simulation
// #100;
$finish;
end
endmodule
Form :-
module modified_booth(
input [4:0]M,Q,
input clk,rst,
output reg[11:0]Y
);
reg [5:0]A,Qc,Mc;
reg Qb;
integer i,j;
always @(posedge clk or negedge rst) begin
if(!rst) begin
A <=6'b000000;
Qc <=6'b000000;
Mc <=6'b000000;
Qb <=1'b0;
Y <=12'b000000000000;
end
else begin
A =6'b000000;
Qc =Q;
Mc=M;
Qb =1'b0;
Y =12'b000000000000;
Qc[5]=Q[4];
Mc[5]=M[4];
for (i=0;i<3;i=i+1) begin
if((Qc[1]==0 && Qc[0]==0 && Qb==0 ) || (Qc[1]==1 && Qc[0]==1
&& Qb==1 ) ) begin
{A,Qc,Qb}={A,Qc,Qb}>>1;
{A,Qc,Qb}={A,Qc,Qb}>>1;
A[5]=A[3];
A[4]=A[3];
end
else if((Qc[1]==0 && Qc[0]==0 && Qb==1) || (Qc[1]==0 &&
Qc[0]==1 && Qb==0 ) ) begin
A=A+Mc;
{A,Qc,Qb}={A,Qc,Qb}>>1;
{A,Qc,Qb}={A,Qc,Qb}>>1;
A[5]=A[3];
A[4]=A[3];
end
else if((Qc[1]==1 && Qc[0]==0 && Qb==1) || (Qc[1]==1 &&
Qc[0]==1 && Qb==0 ) ) begin
A=A-Mc;
{A,Qc,Qb}={A,Qc,Qb}>>1;
{A,Qc,Qb}={A,Qc,Qb}>>1;
A[5]=A[3];
A[4]=A[3];
end
else if((Qc[1]==0 && Qc[0]==1 && Qb==1) ) begin
A=A+2*Mc;
{A,Qc,Qb}={A,Qc,Qb}>>1;
{A,Qc,Qb}={A,Qc,Qb}>>1;
A[5]=A[3];
A[4]=A[3];
end
else if((Qc[1]==1 && Qc[0]==0 && Qb==0) ) begin
A=A-2*Mc;
{A,Qc,Qb}={A,Qc,Qb}>>1;
{A,Qc,Qb}={A,Qc,Qb}>>1;
A[5]=A[3];
A[4]=A[3];
end
end
Y={A,Qc};
end
end
endmodule
tb:-
module tb(
);
reg clk,rst;
// reg [5:0]A,Qc,Mc;
reg [4:0]M,Q;
wire [11:0]Y;
modified_booth uut(
.M(M),
.Q(Q),
.clk(clk),
.rst(rst),
.Y(Y)
);
always #5 clk=~clk;
initial begin
clk=0;rst=0;M=5'b00000;Q=5'b00000;
#20 rst=1;
M=5'b01001 ;Q=5'b01110;
#30;
rst=0;#10;
rst=1;M=5'b11101;Q=5'b01100;
#30;
rst=0;#10;
rst=1;M=5'b10101;Q=5'b11100;
#30;
rst=0;#10;
rst=1;M=5'b01101;Q=5'b01100;
#30;
rst=0;#10;
rst=1;M=5'b01101;Q=5'b11100;
#30;$finish;
end
endmodule
24) Array_mult :-
Form :-
module fa( input a,b,c,output sum,carry);
assign sum=a^b^c;
assign carry=(a&b)|(b&c)|(c&a) ;
endmodule
module array(
input[3:0]A,
input[3:0]B,
output [7:0]P
);
wire C0,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,D0,D1,D2,D3,D4,D5;
assign P[0]=A[0]&B[0];
ha h1(A[1]&B[0],A[0]&B[1],P[1],C0);
fa f1(A[2]&B[0],A[1]&B[1],C0,D0,C1);
fa f2(A[3]&B[0],A[2]&B[1],C1,D1,C2);
ha h2(A[3]&B[1],C2,D2,C3);
ha h3(A[0]&B[2],D0,P[2],C4);
fa f3(A[1]&B[2],D1,C4,D3,C5);
fa f4(A[2]&B[2],D2,C5,D4,C6);
fa f5(A[3]&B[2],C3,C6,D5,C7);
ha h4(A[0]&B[3],D3,P[3],C8);
fa f6(A[1]&B[3],D4,C8,P[4],C9);
fa f7(A[2]&B[3],D5,C9,P[5],C10);
fa f8(A[3]&B[3],C7,C10,P[6],P[7]);
endmodule
tb:-
module tb(
);
reg [3:0]A;
reg [3:0]B;
wire [7:0]P;
array uut(A,B,P);
initial begin
A=4'b1011;B=4'b0110;
#10;
A=4'b1111;B=4'b1010;
#50;
$finish;end
Endmodule
// Align exponents
if (exp_A > exp_B) begin
mant_B = mant_B >> (exp_A - exp_B);
exp_res = exp_A;
end else if (exp_B > exp_A) begin
mant_A = mant_A >> (exp_B - exp_A);
exp_res = exp_B;
end else begin
exp_res = exp_A;
end
// Perform addition/subtraction
if (sign_A == sign_B)
sum_mant = mant_A + mant_B; // Addition
else
sum_mant = mant_A - mant_B; // Subtraction
// Normalization
if (sum_mant[24]) begin
sum_mant = sum_mant >> 1;
exp_res = exp_res + 1;
end else begin
while (sum_mant[23] == 0 && exp_res > 0) begin
sum_mant = sum_mant << 1;
exp_res = exp_res - 1;
end
end
// Assign result
result = {sign_res, exp_res, sum_mant[22:0]};
end
endmodule
module tb;
reg [31:0] A, B;
reg op;
wire [31:0] result;
add uut (
.A(A),
.B(B),
.op(op),
.result(result)
);
initial begin
// Example: 3.5 + 2.5
A = 32'b01000000011000000000000000000000; // IEEE
754 of 3.5
B = 32'b01000000001000000000000000000000; // IEEE
754 of 2.5
op = 0; // Addition
#10;
// Example: 5.75 - 2.25
A = 32'b01000000101110000000000000000000; // IEEE
754 of 5.75
B = 32'b01000000000100000000000000000000; // IEEE
754 of 2.25
op = 1; // Subtraction
#10;
$stop;
end
endmodule
endmodule
// Test 2: Subtraction (A - B)
A = 4'b0110; // 6
B = 4'b0010; // 2
ALU_Sel = 4'b0001; // Subtraction
#10;
$display("Subtraction: %b - %b = %b", A, B, result);
// Test 3: Multiplication (A * B)
A = 4'b0011; // 3
B = 4'b0100; // 4
ALU_Sel = 4'b0010; // Multiplication
#10;
$display("Multiplication: %b * %b = %b", A, B, result);
// Test 4: Division (A / B)
A = 4'b0110; // 6
B = 4'b0011; // 3
ALU_Sel = 4'b0011; // Division
#10;
$display("Division: %b / %b = %b", A, B, result);
// Test 5: Modulo (A % B)
A = 4'b0110; // 6
B = 4'b0011; // 3
ALU_Sel = 4'b0100; // Modulo
#10;
$display("Modulo: %b %% %b = %b", A, B, result);
// Test 7: OR (A | B)
A = 4'b1101; // 13
B = 4'b1011; // 11
ALU_Sel = 4'b0110; // OR
#10;
$display("OR: %b | %b = %b", A, B, result);
// Test 8: XOR (A ^ B)
A = 4'b1101; // 13
B = 4'b1011; // 11
ALU_Sel = 4'b0111; // XOR
#10;
$display("XOR: %b ^ %b = %b", A, B, result);
// End of simulation
$finish;
end
endmodule
27 ) vending :-
end
B: begin
if(inp==0) begin
NS=A;outp=0;change=5;
end
else if(inp==5) begin
NS=C;outp=0;change=0;
end
else if(inp == 10) begin
NS=A;outp=1;change=0;
end
end
C: begin
if(inp==0) begin
NS=A;outp=0;change=10;
end
else if(inp==5) begin
NS=A;outp=1;change=0;
end
else if(inp == 10) begin
NS=A;outp=1;change=5;
end
end
default: begin
NS = A;
outp = 0;change=0;
end
endcase
end
endmodule
module tb;
// Testbench signals
reg clk;
reg rst;
reg[3:0] inp;
wire out;
wire[3:0]change;
// Instantiate the sequence detector module
vending uut (
.clk(clk),
.rst(rst),
.inp(inp),
.out(out),.change(change)
);
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk; // Clock period: 10ns
end
// Test sequence
initial begin
// Initialize inputs
rst = 0;
inp = 0;
// Apply reset
#7 rst = 1; // Assert reset after 7ns
// Test sequence: 1010
@(posedge clk) inp = 5;
@(posedge clk) inp = 5;
@(posedge clk) inp = 5;
@(posedge clk) inp = 10;
@(posedge clk) inp = 10; // Output should be 1 here
#50
// End simulation
$stop;
end
// Monitor signals
Endmodule
27) TLC :-
case (CS)
A: begin
NS = (x == 1) ? B : A;
Ho = 2'b01; // Green for state A
Co = 2'b00; // No change in C for state A
end
B: begin
NS = C;
Ho = 2'b10; // Yellow for state B
Co = 2'b00; // No change in C for state B
end
C: begin
NS = D;
Ho = 2'b00; // Red for state C
Co = 2'b00; // No change in C for state C
end
D: begin
NS = (x == 1) ? D : E;
Ho = 2'b00; // Red for state D
Co = 2'b01; // Output 1 for sequence 1010
end
E: begin
NS = A;
Ho = 2'b00; // Red for state E
Co = 2'b10; // Output 1 for sequence 1010
end
default: begin
NS = A;
Ho = 2'b00; // Red by default
Co = 2'b00; // No change by default
end
endcase
end
endmodule
tb :- module tb;
// Testbench signals
reg clk;
reg rst;
reg x;
wire [1:0] Ho;
wire [1:0] Co;
// Clock generation
always begin
clk = 0;
forever #5 clk = ~clk;
end
// Initial block for applying test cases
initial begin
// Initialize signals
rst = 0;
x = 0;
#10 rst=1;
x=0;
#20;
x=1;
#40;
x=0;
#30;
$finish;
end
endmodule