Assignment
Assignment
// Block: gates
module gates(y_not,y_and,y_or,y_xor,y_xnor,y_nand,y_nor,a,b);
// Input Ports:
input a;// a: 1-bit
input b;// b: 1-bit
// Output Ports:
output y_not; // y_not: 1-bit
output y_and; // y_and: 1-bit
output y_or; // y_or: 1-bit
output y_xor; // y_xor: 1-bit
output y_xnor; // y_xnor: 1-bit
output y_nand; // y_nand: 1-bit
output/ y_nor; // y_nor: 1-bit
// Description:
// Design all basic gates.
assign y_not = ~a,~b;
assign y_and = a & b;
assign y_or = a | b;
assign y_xor = a^b;
assign y_xnor = ~(a^b);
assign y_nand = ~(a & b);
assign y_nor = ~(a|b);
endmodule
2a <<<<<<-----binary2gray------->>>>>>>>
2b <<<<<<-----gray2binary-------->>>>>>>
//------------------------------------------------------------------------//
module countones(i_data,o_cnt);// Block: countones
// Input Ports:
input [31:0]i_data;// i_data: 32-bit Vector
// Output Ports:
output reg[5:0]o_cnt;// o_cnt: 6-bit Vector
integer i;
// Description:
// Counts number of 1's in 32-bit input vector.
//------------------------------------------------------------------------//
// Code your design here
always@(i_data)
begin
o_cnt=0;
for(i=0;i<32;i=i+1)
if(i_data[i] == 1'b1)
o_cnt=o_cnt+1;
end
endmodule
4(A)<<<<<<<------mux4*1if------->>>>>>>>>>
//------------------------------------------------------------------------//
module mux4b_if(in0,in1,in2,in3,sel,out);// Block: mux4b_if
// Input Ports:
input [3:0]in0;// in0, in1, in2, in3: 4-bit Vector
input [3:0]in1;
input [3:0]in2;
input [3:0]in3;
input[1:0]sel;// sel: 2-bit Vector
// Output Ports:
output reg [3:0]out;// out: 4-bit Vector
// Description:
// Implement 4:1 Mux using if-statements.
always @(*) begin
if (sel==0)
out <= in0;
else if (sel==1)
out <= in1;
else if (sel==2)
out = in2;
else
out = in3;
end
endmodule
4(b)<<<<<<--------MUX4*1_CASE--------->>>>>>>>>>
//------------------------------------------------------------------------//
module mux4b_case(in0,in1,in2,in3,sel,out);// Block: mux4b_case
// Input Ports:
input [3:0]in0;// in0, in1, in2, in3: 4-bit Vector
input [3:0]in1;
input [3:0]in2;
input [3:0]in3;
input[1:0]sel;// sel: 2-bit Vector
// Output Ports:
output reg [3:0]out;// out: 4-bit Vector
// Description:
// Implement 4:1 Mux using if-statements.
always @ (*)
begin
case(sel)
2'b00: out<=in0;
2'b01: out<=in1;
2'b10: out<=in2;
2'b11: out<=in3;
endcase
end
endmodule
4(C)<<<<<<<--------mux4*1_terinary------->>>>>>>>>
//------------------------------------------------------------------------//
module mux4b_ternary(in0,in1,in2,in3,sel,out);// Block: mux4b_case
// Input Ports:
input [3:0]in0;// in0, in1, in2, in3: 4-bit Vector
input [3:0]in1;
input [3:0]in2;
input [3:0]in3;
input[1:0]sel;// sel: 2-bit Vector
// Output Ports:
output [3:0]out;// out: 4-bit Vector
assign out=(sel[1]?(sel[0]?in3:in2):(sel[0]?in1:in0));
endmodule// Code your design here
5<<<<<<<<<<<-------demux-------->>>>>>>>>>>>>>>
//------------------------------------------------------------------------//
always@(*)
begin
case(desel)
3'd0:begin
out0=in;out1=0;out2=0;out3=0;out4=0;out5=0;out6=0;out7=0;
end
3'd1:begin
out0=0;out1=in;out2=0;out3=0;out4=0;out5=0;out6=0;out7=0;
end
3'd2:begin
out0=0;out1=0;out2=in;out3=0;out4=0;out5=0;out6=0;out7=0;
end
3'd3:begin
out0=0;out1=0;out2=0;out3=in;out4=0;out5=0;out6=0;out7=0;
end
3'd4:begin
out0=0;out1=0;out2=0;out3=0;out4=in;out5=0;out6=0;out7=0;
end
3'd5:begin
out0=0;out1=0;out2=0;out3=0;out4=0;out5=in;out6=0;out7=0;
end
3'd6:begin
out0=0;out1=0;out2=0;out3=0;out4=0;out5=0;out6=in;out7=0;
end
3'd7:begin
out0=0;out1=0;out2=0;out3=0;out4=0;out5=0;out6=0;out7=in;
end
endcase
end
endmodule
6<<<<<<<<<<-------dec3*8---------->>>>>>>>>>>>>
end
endmodule
//------------------------------------------------------------------------//
7<<<<<<<<<<-----encoder-------->>>>>>>>>>>>
end
else
begin
e0=0;
e1=0;
e2=0;
end
$display("enable zero");
end
endmodule