-
Notifications
You must be signed in to change notification settings - Fork 1
/
Number_Division.sv
67 lines (57 loc) · 1.68 KB
/
Number_Division.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Code created by vipin on https://verilogcodes.blogspot.com/2015/11/synthesisable-verilog-code-for-division.html
module Number_Division(A,B,Res);
//the size of input and output ports of the division module is generic.
parameter WIDTH = 32;
//input and output ports.
input [WIDTH-1:0] A;
input [WIDTH-1:0] B;
output [WIDTH-1:0] Res;
//internal variables
reg [WIDTH-1:0] Res = 0;
reg [WIDTH-1:0] a1,b1;
reg [WIDTH:0] p1;
integer i;
always@ (A or B)
begin
//initialize the variables.
a1 = A;
b1 = B;
p1= 0;
for(i=0;i < WIDTH;i=i+1) begin //start the for loop
p1 = {p1[WIDTH-2:0],a1[WIDTH-1]};
a1[WIDTH-1:1] = a1[WIDTH-2:0];
p1 = p1-b1;
if(p1[WIDTH-1] == 1) begin
a1[0] = 0;
p1 = p1 + b1; end
else
a1[0] = 1;
end
Res = a1;
end
endmodule
module tb_division;
parameter WIDTH = 8;
// Inputs
reg [WIDTH-1:0] A;
reg [WIDTH-1:0] B;
// Outputs
wire [WIDTH-1:0] Res;
// Instantiate the division module (UUT)
Number_Division #(WIDTH) uut (
.A(A),
.B(B),
.Res(Res)
);
initial begin
// Initialize Inputs and wait for 100 ns
A = 0; B = 0; #100; //Undefined inputs
//Apply each set of inputs and wait for 100 ns.
A = 100; B = 10; #100;
A = 200; B = 40; #100;
A = 90; B = 9; #100;
A = 70; B = 10; #100;
A = 16; B = 3; #100;
A = 255; B = 5; #100;
end
endmodule