数字集成电路设计-3-除法器的verilog简单实现(续)

引言

1,改成clk方式。
2,添加clk,50MHz。
3, 添加rst,同步复位。
4,添加calc_done,指示计算完成,高有效。

3.1 模块代码

  1. /*  
  2. * module:div_rill  
  3. * file name:div_rill.v  
  4. * syn:yes  
  5. * author:network  
  6. * modify:rill  
  7. * date:2012-09-10  
  8. */  
  9.   
  10. module div_rill  
  11. (  
  12. input clk,  
  13. input rst,  
  14. input[31:0] a,   
  15. input[31:0] b,  
  16.   
  17. output reg [31:0] yshang,  
  18. output reg [31:0] yyushu,  
  19. output reg calc_done  
  20. );  
  21.   
  22. reg[31:0] tempa;  
  23. reg[31:0] tempb;  
  24. reg[63:0] temp_a;  
  25. reg[63:0] temp_b;  
  26.   
  27. reg [5:0] counter;  
  28.   
  29. always @(a or b)  
  30. begin  
  31.     tempa <= a;  
  32.     tempb <= b;  
  33. end  
  34.   
  35. always @(posedge clk)  
  36. begin  
  37.     if(!rst)  
  38.         begin  
  39.             temp_a <= 64'h0000_0000_0000_0000;  
  40.             temp_b <= 64'h0000_0000_0000_0000;     
  41.             calc_done <= 1'b0;  
  42.         end  
  43.     else  
  44.         begin  
  45.             if(counter <= 31)  
  46.                 begin  
  47.                     temp_a <= {temp_a[62:0],1'b0};  
  48.                     if(temp_a[63:32] >= tempb)  
  49.                         begin  
  50.                             temp_a <= temp_a - temp_b + 1'b1;  
  51.                         end  
  52.                     else  
  53.                         begin  
  54.                             temp_a <= temp_a;  
  55.                         end  
  56.                           
  57.                     counter <= counter + 1;  
  58.                     calc_done <= 1'b0;  
  59.                 end  
  60.             else  
  61.                 begin  
  62.                     counter <= 0;  
  63.                     calc_done <= 1'b1;  
  64.                     temp_a <= {32'h00000000,tempa};  
  65.                     temp_b <= {tempb,32'h00000000};   
  66.                     yshang <= temp_a[31:0];  
  67.                     yyushu <= temp_a[63:32];  
  68.                 end  
  69.   
  70.   
  71.         end  
  72.   
  73. end  
  74.   
  75. endmodule  
  76.   
  77. /*************** EOF ******************/  
/*
* module:div_rill
* file name:div_rill.v
* syn:yes
* author:network
* modify:rill
* date:2012-09-10
*/

module div_rill
(
input clk,
input rst,
input[31:0] a, 
input[31:0] b,

output reg [31:0] yshang,
output reg [31:0] yyushu,
output reg calc_done
);

reg[31:0] tempa;
reg[31:0] tempb;
reg[63:0] temp_a;
reg[63:0] temp_b;

reg [5:0] counter;

always @(a or b)
begin
    tempa <= a;
    tempb <= b;
end

always @(posedge clk)
begin
	if(!rst)
		begin
			temp_a <= 64'h0000_0000_0000_0000;
			temp_b <= 64'h0000_0000_0000_0000;	
			calc_done <= 1'b0;
		end
	else
		begin
			if(counter <= 31)
				begin
					temp_a <= {temp_a[62:0],1'b0};
					if(temp_a[63:32] >= tempb)
						begin
							temp_a <= temp_a - temp_b + 1'b1;
						end
					else
						begin
							temp_a <= temp_a;
						end
						
					counter <= counter + 1;
					calc_done <= 1'b0;
				end
			else
				begin
					counter <= 0;
					calc_done <= 1'b1;
					temp_a <= {32'h00000000,tempa};
					temp_b <= {tempb,32'h00000000}; 
					yshang <= temp_a[31:0];
					yyushu <= temp_a[63:32];
				end


		end

end

endmodule

/*************** EOF ******************/

3.2 testbench

  1. /*  
  2. * module:div_rill_tb  
  3. * file name:div_rill_tb.v  
  4. * syn:no  
  5. * author:rill  
  6. * date:2012-09-10  
  7. */  
  8.   
  9.   
  10. `timescale 1ns/1ns  
  11.   
  12. module div_rill_tb;  
  13.   
  14. reg clk;  
  15. reg rst;  
  16. reg [31:0] a;  
  17. reg [31:0] b;  
  18. wire [31:0] yshang;  
  19. wire [31:0] yyushu;  
  20. wire calc_done;  
  21.   
  22. initial  
  23. begin  
  24.     clk = 0;  
  25.     rst = 0;  
  26.     #20 rst = 1;  
  27.       
  28.     #40 a = $random()%10000;  
  29.         b = $random()%1000;  
  30.           
  31.     #1000 a = $random()%1000;  
  32.         b = $random()%100;  
  33.           
  34.     #1000 a = $random()%100;  
  35.         b = $random()%10;     
  36.           
  37.     #1000 $stop;  
  38. end  
  39.   
  40. always #10 clk = ~clk;  
  41.   
  42.   
  43. div_rill DIV_RILL  
  44. (  
  45. .clk (clk),  
  46. .rst (rst),  
  47. .a (a),  
  48. .b (b),  
  49.   
  50. .yshang (yshang),  
  51. .yyushu (yyushu),  
  52. .calc_done (calc_done)  
  53. );  
  54.   
  55. endmodule  
  56. /******** EOF ******************/  
/*
* module:div_rill_tb
* file name:div_rill_tb.v
* syn:no
* author:rill
* date:2012-09-10
*/


`timescale 1ns/1ns

module div_rill_tb;

reg clk;
reg rst;
reg [31:0] a;
reg [31:0] b;
wire [31:0] yshang;
wire [31:0] yyushu;
wire calc_done;

initial
begin
	clk = 0;
	rst = 0;
	#20 rst = 1;
	
	#40 a = $random()%10000;
		b = $random()%1000;
		
	#1000 a = $random()%1000;
		b = $random()%100;
		
	#1000 a = $random()%100;
		b = $random()%10;	
		
	#1000 $stop;
end

always #10 clk = ~clk;


div_rill DIV_RILL
(
.clk (clk),
.rst (rst),
.a (a),
.b (b),

.yshang (yshang),
.yyushu (yyushu),
.calc_done (calc_done)
);

endmodule
/******** EOF ******************/

3.3 仿真


http://blog.csdn.net/rill_zhen/article/details/7964535

更多
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值