Vivado使用之小练习

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////


/*二进制无符号整数除法*/
module divider_2bit(a,b,c,d);
input [7:0]a;//a为被除数
input [3:0]b;//b为除数
output reg[7:0]c;//c为商
output reg[3:0]d;//d为余数

reg[15:0] temp_a=0;
reg[7:0] temp_c=0;

integer I;

always@(a or b)
begin
    temp_a = a;
    for(I=0;I<9;I=I+1)//效果等同于repeat(9),只有循环次数确定的for、while、repeat语句才可综合,否则不可综合
    begin
        temp_c = temp_c<<1;//移位运算符的结果是一串数,并不能改变移位对象的值
        if(temp_a[15:8]>=b)
        begin
            temp_c = temp_c + 1'b1;
            temp_a[15:8] = temp_a[15:8] - b;
            temp_a = temp_a<<1;//效果等同于temp_a = {temp_a[14:0],1'b0};切记使用位拼接运算符要注明位宽
        end
        else
            temp_a = temp_a<<1;//不可写作temp_a = {temp_a[14:0],0};因为0没有注明位宽
    end
    c <= temp_c;
    d <= temp_a[12:9];
end

endmodule
View Code

 仿真代码:

`timescale 1ns / 1ns
module divider_2bit_tb;
reg[7:0] a;
reg[3:0] b;
wire[7:0]c;
wire[3:0]d;

divider_2bit u1(.a(a),.b(b),.c(c),.d(d));

initial
begin
a = 8'b0001_1011;
b = 4'b0101;
#100
a = 8'b1001_1011;
b = 4'b0001;
#100
a = 8'b0000_0011;
b = 4'b1101;
#100
a = 8'd250;
b = 4'd15;
#100
a = 8'd6;
b = 4'd3;
#100
a = 8'd97;
b = 4'd13;
end
endmodule
View Code

 仿真如下:

 

posted @ 2023-05-13 11:38  有翅膀的大象  阅读(18)  评论(0编辑  收藏  举报