1.创建一个1位宽的2比1多路复用器。当sel = 0时,选择一个。当sel = 1时,选择b。
module top_module(
input a, b, sel,
output out );
endmodule
2.创建一个100位宽的2比1多路复用器。当sel = 0时,选择一个。当sel = 1时,选择b。
module top_module(
input [99:0] a, b,
input sel,
output [99:0] out );
assign out = sel ? b : a;
endmodule
3.创建一个16位宽的9对1多路复用器。sel = 0选择a,sel = 1选择b,依此类推。对于未使用的情况(sel = 9至15),将所有输出位设置为“ 1”。
module top_module(
input [15:0] a, b, c, d, e, f, g, h, i,
input [3:0] sel,
output [15:0] out );
always@(*)begin
case(sel)
4'd0: out = a;
4'd1: out = b;
4'd2: out = c;
4'd3: out = d;
4'd4: out = e;
4'd5: out = f;
4'd6: out = g;
4'd7: out = h;
4'd8: out = i;
default: out = 16'b1111_1111_1111_1111;
endcase
end
endmodule
4.创建一个1位宽的256:1多路复用器。256个输入全部打包为单个256位输入向量。sel = 0应该选择in [0],sel = 1选择in [1]的比特,sel = 2选择in [2]的比特,依此类推。
module top_module(
input [255:0] in,
input [7:0] sel,
output out );
assign out = in[sel];
endmodule
5.创建一个4位宽的256:1多路复用器。256个4位输入全部打包为单个1024位输入向量。sel = 0应该选择[3:0]中的位,sel = 1选择[7:4]中的位,sel = 2选择[11:8]中的位,依此类推。
module top_module(
input [1023:0] in,
input [7:0] sel,
output [3:0] out );
assign out[3:0] = {in[sel*4+3],in[sel*4+2],in[sel*4+1],in[sel*4]};
endmodule
6.一位半加器
module top_module(
input a, b,
output cout, sum );
assign cout = (a & b == 1) ? 1'b1 : 1'b0;
assign sum = a ^ b;
endmodule
7.一位全加器
module top_module(
input a, b, cin,
output cout, sum );
assign cout = (a & b | (a ^ b) & cin == 1'b1) ? 1'b1 : 1'b0;
assign sum = a ^ b ^ cin;
endmodule
8.3位全加器
module top_module(
input [2:0] a, b,
input cin,
output [2:0] cout,
output [2:0] sum );
assign cout[0] = a[0] & b[0] | (a[0] ^ b[0]) & cin;
assign cout[1]= a[1] & b[1] | (a[1] ^ b[1]) & cout[0];
assign {cout[2],sum} = a + b + cin;
endmodule
9.需求如下
module top_module (
input [3:0] x,
input [3:0] y,
output [4:0] sum);
assign sum = x + y;
endmodule
10.假设有两个8位2的补码,即a [7:0]和b [7:0]。这些数字相加产生s [7:0]。同时计算是否发生(有符号的)溢出。
module top_module (
input [7:0] a,
input [7:0] b,
output [7:0] s,
output overflow
);
reg sig;
assign {sig,s} = a + b; // 可能产生一个更高位的符号位,此题可不加sig
assign overflow = ~a[7] & ~b[7] & s[7] | a[7] & b[7] & ~s[7];//前者为001,即两个负数相加溢出;后者为110,两个正数相加溢出;
endmodule
11.创建一个100位二进制加法器。加法器将两个100位数字和一个进位相加,以产生100位总和并执行。
module top_module(
input [99:0] a, b,
input cin,
output cout,
output [99:0] sum );
assign {cout,sum} = a+b+cin;
endmodule
12.为您提供了一个名为bcd_fadd的BCD(二进制编码的十进制)一位数字加法器,该加法器将两个BCD数字和进位相加,并产生总和和进位。
module top_module(
input [15:0] a, b,
input cin,
output cout,
output [15:0] sum );
reg [3:0] cout1;
bcd_fadd u1_bcd_fadd(.a(a[3:0]),.b(b[3:0]),.cin(cin),.cout(cout1[0]),.sum(sum[3:0]));
bcd_fadd u2_bcd_fadd(.a(a[7:4]),.b(b[7:4]),.cin(cout1[0]),.cout(cout1[1]),.sum(sum[7:4]));
bcd_fadd u3_bcd_fadd(.a(a[11:8]),.b(b[11:8]),.cin(cout1[1]),.cout(cout1[2]),.sum(sum[11:8]));
bcd_fadd u4_bcd_fadd(.a(a[15:12]),.b(b[15:12]),.cin(cout1[2]),.cout(cout1[3]),.sum(sum[15:12]));
assign cout = cout1[3];
endmodule
13.如下
module top_module(
input a,
input b,
input c,
output out );
assign out = ~(~a & ~b & ~c);
endmodule
14.如下:
module top_module(
input a,
input b,
input c,
input d,
output out );
assign out = ~a & ~d | ~c & ~b | (a | b) & c & d;
endmodule