实验 12
多位数值比较器module comp(ABB,AEB,ASB,A,B,I1,I2,I3);
output ABB,AEB,ASB;
// ABB 表示A>B AEB 表示A=B, ASB 表示 A<B;
input [1:0] A,B;
input I1,I2,I3; // I1表示上一级的 A>B I2 表示上一级的 A=B, I3 表示上一级的 A<B;
reg ABB,AEB,ASB;
//行为描述
always@(A or B or I1 or I2 or I3)
if(A>B)
{ABB,AEB,ASB}=3'b100;
else if(A<B)
{ABB,AEB,ASB}=3'b001;
else
// A=B,但是考虑到前一级的情况
begin if(I1)
//I1 表示上一级的 A>B
{ABB,AEB,ASB}=3'b100;
else if(I3)
{ABB,AEB,ASB}=3'b001;//I3 表示上一级的 A<B;
else
{ABB,AEB,ASB}=3'b010;
end
endmodule
实验 13
奇偶校验//奇偶校验位产生器
module parity(even_bit,odd_bit,input_bus);
output even_bit,odd_bit;
input[7:0] input_bus;
assign odd_bit = ^ input_bus; //产生奇校验位
assign even_bit = ~odd_bit; //产生偶校验位
endmodule
实验 14
补码生成
module compo(d_out,d_in);
output [7:0] d_out;
input [7:0]d_in;
reg [7:0] d_out;
always @(d_in)
if (d_in[7]==1'b0)
// 正数,最高位为符号位,0说明是正数,正数补码是其本身
d_out=d_in;
else
// 负数
d_out={d_in[7],~d_in[6:0]+1'b1};
//最高位符号位不变,数据位加一构成其补码
endmodule
实验 15
8位硬件加法器的设计//8 位硬件加法器
module add8b(cout,sum,a,b,cin);
output[7:0] sum;
output cout;
input[7:0] a,b;
input cin;
assign {cout,sum}=a+b+cin;
endmodule
实验 16
4 位并行乘法器//4 位并行乘法器
module mult(outcome,a,b);
parameter size=4;
input[size:1] a,b; //两个操作数
output[2*size:1] outcome; //结果
assign outcome=a*b; //乘法运算符
endmodule
实验 17
七人表决器// for 语句描述的七人投票表决器
module voter7(pass,vote);
output pass; // 通过为高电平,否则为低电平
input[6:0] vote; // 7个投票输入#通过为高,否定为低
reg[2:0] sum;
integer i;
reg pass;
always @(vote)
begin
sum=0;
for(i=0;i<=6;i=i+1) //for 语句
if(vote) sum=sum+1;
if(sum[2]) pass=1; //若超过 4 人赞成,则 pass=1
else pass=0;
end
endmodule |