1、新建工程
2、本实验实现的功能是:4位数的乘法运算,对于运算结果,成绩的低四位数显示在左数第四个数码管上,高四位显示在左数第三个数码管上。由于本实验中两个乘数分别为1011和0111,所以乘积就为:01001101,所以左数第四个数码管上显示d,左数第三个数码管上显示4,由于数码管段选的复用,所以数码管以1s的间隔交替显示。乘法器如下:该图中,对乘法器中的各个加法器进行了编号处理。
程序如下:
module multiplication(clk,Key4,Key3,Key2,Key1,DS_EN4,DS_EN3,DS_EN2,DS_EN1,DS_A,DS_B,DS_C,DS_D,DS_E,DS_F,DS_G,DS_DP);
input clk;
input Key4,Key3,Key2,Key1;
output wire DS_EN4,DS_EN3,DS_EN2,DS_EN1;
output wire DS_A,DS_B,DS_C,DS_D,DS_E,DS_F,DS_G,DS_DP;
wire [3:0]Key;
wire [3:0]wei;
wire [7:0]duan;
reg [3:0]wei_r;
reg [7:0]duan_r;
wire Cout1,Cout2,Cout3,Cout4,Cout5,Cout6,Cout7,Cout8,Cout9,Cout10,Cout11,Cout12;//图中各加法器的进位
wire Sout1,Sout2,Sout3,Sout4,Sout5,Sout6,Sout7,Sout8,Sout9,Sout10,Sout11,Sout12;//图中各加法器的和
wire [7:0]P;
wire clk_1k,clk_1s;
parameter B=4'b1011,
A=4'b0111;
assign Key={Key4,Key1,Key2,Key3};
assign {DS_EN1,DS_EN2,DS_EN3,DS_EN4}=wei;//低电平选中对应数码管
assign {DS_DP,DS_G,DS_F,DS_E,DS_D,DS_C,DS_B,DS_A}=duan; //数码管共阴极
fp_k fp_1k(clk,clk_1k);
fp_s fp_1s(clk_1k,clk_1s);
//乘法器
assign P[0]=A[0]&B[0]; //P0
adder_1bit add1(0,(A[1]&B[0]),(A[0]&B[1]),P[1],Cout1); //P1
adder_1bit add2(Cout1,(A[2]&B[0]),(A[1]&B[1]),Sout2,Cout2); //P2
adder_1bit add3(0,Sout2,(A[0]&B[2]),P[2],Cout3); //P2
adder_1bit add4(Cout2,(A[3]&B[0]),(A[2]&B[1]),Sout4,Cout4); //P3
adder_1bit add5(Cout3,Sout4,(A[1]&B[2]),Sout5,Cout5); //P3
adder_1bit add6(0,Sout5,(A[0]&B[3]),P[3],Cout6); //P3
adder_1bit add7(Cout4,Cin,(A[3]&B[1]),Sout7,Cout7); //P4
adder_1bit add8(Cout5,Sout7,(A[2]&B[2]),Sout8,Cout8); //P4
adder_1bit add9(Cout6,Sout8,(A[1]&B[3]),P[4],Cout9); //P4
adder_1bit add10(Cout8,Cout7,(A[3]&B[2]),Sout10,Cout10); //P5
adder_1bit add11(Cout9,Sout10,(A[2]&B[3]),P[5],Cout11); //P5
adder_1bit add12(Cout11,Cout10,(A[3]&B[3]),P[6],Cout12); //P6
assign P[7]=Cout12; //P7
initial
begin
//Key<=4'b1111;
wei_r<=4'b1111;
end
always @(posedge clk_1s)
begin
//if(Key==4'b1110)//按下最右边的那个个按键时,进行复位操作
//begin
//wei_r<=4'b1111;
//end
if(wei_r==4'b1111)
begin
wei_r<=4'b1110; //左数第四个数码管亮
end
else if(wei_r==4'b1110)
begin
wei_r<=4'b1101; //左数第三个数码管亮
end
else if(wei_r==4'b1101)
begin
wei_r<=4'b1110; //左数第四个数码管亮
end
end
always @(clk_1k)
begin
if(wei_r==4'b1110) //左数第四个数码管亮,并且显示乘积数的低四位
begin
case(P[3:0])
4'b0000: duan_r=7'b00111111;
4'b0001: duan_r=7'b00000110;
4'b0010: duan_r=7'b01011011;
4'b0011: duan_r=7'b01001111;
4'b0100: duan_r=7'b01100110;
4'b0101: duan_r=7'b01101101;
4'b0110: duan_r=7'b01111101;
4'b0111: duan_r=7'b00000111;
4'b1000: duan_r=7'b01111111;
4'b1001: duan_r=7'b01101111;
4'b1010: duan_r=7'b01110111;
4'b1011: duan_r=7'b01111100;
4'b1100: duan_r=7'b00111001;
4'b1101: duan_r=7'b01011110;
4'b1110: duan_r=7'b01111001;
4'b1111: duan_r=7'b01110001;
default: duan_r=7'b00000000;
endcase
end
else if(wei_r==4'b1101) //左数第三个数码管亮,并且显示乘积数的高四位
begin
case(P[7:4])
4'b0000: duan_r=7'b00111111;
4'b0001: duan_r=7'b00000110;
4'b0010: duan_r=7'b01011011;
4'b0011: duan_r=7'b01001111;
4'b0100: duan_r=7'b01100110;
4'b0101: duan_r=7'b01101101;
4'b0110: duan_r=7'b01111101;
4'b0111: duan_r=7'b00000111;
4'b1000: duan_r=7'b01111111;
4'b1001: duan_r=7'b01101111;
4'b1010: duan_r=7'b01110111;
4'b1011: duan_r=7'b01111100;
4'b1100: duan_r=7'b00111001;
4'b1101: duan_r=7'b01011110;
4'b1110: duan_r=7'b01111001;
4'b1111: duan_r=7'b01110001;
default: duan_r=7'b00000000;
endcase
end
end
assign wei=wei_r;
assign duan=duan_r;
endmodule
//分频48M-----1k
module fp_k(clk,clk_1k);
input clk;
output reg clk_1k;
reg [31:0]cout;
//reg clk_en;
initial
begin
cout<=32'd0;
clk_1k<=1'd0;
end
always @(posedge clk)
begin
if(cout==32'd24000)
begin
cout<=32'd0;
clk_1k<=~clk_1k;
end
else
begin
cout<=cout+1;
end
//clk_1s<=clk_en;
end
endmodule
//分频1k-----1s
module fp_s(clk_1k,clk_1s);
input clk_1k;
output reg clk_1s;
reg [10:0]cout;
//reg clk_en;
initial
begin
cout<=10'd0;
clk_1s<=1'd0;
end
always @(posedge clk_1k)
begin
if(cout==10'd500)
begin
cout<=10'd0;
clk_1s<=~clk_1s;
end
else
begin
cout<=cout+1;
end
//clk_1s<=clk_en;
end
endmodule
//1bit_adder
module adder_1bit(Cin,A,B,S,Cout);
input Cin;
input A,B;
output S;
output Cout;
assign S=A^B^Cin;
assign Cout=(A&B)|(A&Cin)|(Cin&B);
endmodule
3、实验结果
乘积的低四位
乘积的高四位