-
Notifications
You must be signed in to change notification settings - Fork 2
/
modulo.v
74 lines (68 loc) · 1.88 KB
/
modulo.v
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
68
69
70
71
72
73
74
/* ----------------------------------------- *
* Title : Modulo *
* Project : Verilog Utility Modules *
* ----------------------------------------- *
* File : modulo.v *
* Author : Yigit Suoglu *
* Last Edit : 11/04/2021 *
* Licence : CERN-OHL-W *
* ----------------------------------------- *
* Description : Modules for modulo opration *
* ----------------------------------------- */
//<divident> mod <divisor>
module modulo_sec#(
parameter SIZE = 16,
parameter CALCULATION_OUT = 1'b0/*1'b0 or 1'b1*/)(
input clk,
input rst,
input [(SIZE-1):0] divident,
input [(SIZE-1):0] divisor,
input start,
output done,
output [(SIZE-1):0] remainder);
reg calculating; //State reg
wire negativeRes;
reg [(SIZE-1):0] stepVal, divisor_reg;
wire [(SIZE-1):0] stepRes;
assign done = ~calculating;
assign remainder = (done) ? stepVal : {SIZE{CALCULATION_OUT}};
assign {negativeRes, stepRes} = {1'b0, stepVal} - {1'b0, divisor_reg};
//Handle calculation result
always@(posedge clk)
begin
if(start & done)
begin
stepVal <= divident;
end
else
begin
stepVal <= (~negativeRes & calculating) ? stepRes : stepVal;
end
end
//Store divisor value during calculation
always@(posedge clk)
begin
divisor_reg <= (done) ? divisor : divisor_reg;
end
//State transactions
always@(posedge clk or posedge rst)
begin
if(rst)
begin
calculating <= 1'b0;
end
else
begin
case(calculating)
1'b0:
begin
calculating <= start;
end
1'b1:
begin
calculating <= ~negativeRes;
end
endcase
end
end
endmodule