Structural Modeling 10-07-2023
Structural Modeling 10-07-2023
module mux4x1(
input [3:0] in,
input [1:0] sel,
output reg out);
endmodule
module mux2x1 (
input wire a,
input wire b,
input wire sel,
output reg out
);
always @* begin
case (sel)
1'b0: out = a;
1'b1: out = b;
endcase
end
endmodule
//testbench
module mux4x1_tb;
// Inputs
reg [3:0]
in;
reg [1:0] sel;
// Outputs
wire out;
// Test
stimulus initial
begin
$monitor("in=%b, sel=%b, out=%b", in, sel, out);
// Initialize inputs
in = 4'b1010;
sel = 2'b00;
#10; // Wait for some time
sel = 2'b01;
#10; // Wait for some time
sel = 2'b10;
#10; // Wait for some time
sel = 2'b11;
#10; // Wait for some time
$finish;
end
endmodule
Output Waveforms:
Rtl Schematic :
//Mux 16x1
// level 1
mux4x1 dut1(i[3:0],select[1:0],y1);
mux4x1 dut2(i[7:4],select[1:0],y2);
mux4x1 dut3(i[11:8],select[1:0],y3);
mux4x1 dut4(i[15:12],select[1:0],y4);
//level 2
mux4x1 dut5({y4,y3,y2,y1},select[3:2],y);
endmodule
//Mux 4x1
module mux4x1(input [3:0]i,
input [1:0]select,
output reg y);
always@*
case(select)
2'b00: y=i[0];
2'b01: y=i[1];
2'b10: y=i[2];
2'b11: y=i[3];
endcase
endmodule
//Testbench
module test_mux16x1;
// Inputs
reg [15:0] data_in;
reg [3:0] select;
// Outputs
wire out;
endmodule
Output Waveform:
module full_adder(a,b,cin,sum,co);
input a,b,cin;
output sum,co;
assign sum=a^b^cin;
assign co=(a&b)|(a&cin)|(cin&b);
endmodule
output [3:0]sum;
output cout;
input [3:0]a;
input [3:0]b;
input cin;
wire [2:0]c;
full_adder u0 (a[0],b[0],cin,sum[0],c[0]);
full_adder u1 (a[1],b[1],c[0],sum[1],c[1]);
full_adder u2 (a[2],b[2],c[1],sum[2],c[2]);
full_adder u3 (a[3],b[3],c[2],sum[3],cout);
endmodule
module TestModule;
// Inputs
reg [3:0] a;
reg [3:0] b;
reg cin;
// Outputs
wire [3:0] sum;
wire cout;
// Initialize Inputs
initial begin
$monitor("a=%0d b=%0d cin=%b sum=%0d cout=%b",a,b,cin,sum,cout);
a = 0;
b = 0;
cin = 0;
// Wait 100 ns for global reset to
finish #100;
a = 5;
b = 6;
cin = 1;
// Wait 100 ns for global reset to
finish #100;
end
endmodule
Output Waveform:
RTL Schematic:
4. 3-to-8 decoder using 2-to-4 decoder
endmodule
//3 to 8 decoder
module decoder3x8(input [2:0]sel, output [7:0]out);
endmodule
//Testbench
module testbench();
sel = 0;
#10;
sel = 1;
#10;
sel = 2;
#10;
sel = 3;
#10;
sel = 4;
#10;
sel = 5;
#10;
sel = 6;
#10;
sel = 7;
#10;
$finish;
end
endmodule
Output Waveform: