-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_power.v
126 lines (106 loc) · 2.54 KB
/
mod_power.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
`timescale 1ns / 1ps
module mod_power
#(
parameter WIDTH = 512
)(
input aclk, // Clock
input aresetn, // Asynchronous reset active low
input wire [WIDTH-1:0] Number,
input wire [WIDTH-1:0] Exponent,
input wire [WIDTH-1:0] Modules,
output reg [WIDTH-1:0] response,
output reg res_done
);
//----------------------------------------------------------------
// Parameters.
//----------------------------------------------------------------
localparam STATE_INIT = 3'd0;
localparam STATE_ROUND = 3'd1;
localparam STATE_NEXT = 3'd2;
localparam STATE_DONE = 3'd3;
//---------------------------------------------------------------------------------------------------------------------
// Internal wires and registers
//---------------------------------------------------------------------------------------------------------------------
reg start;
reg [WIDTH-1:0] E,M;
reg [2*(WIDTH)-1:0] N,R;
reg [2:0] state;
//----------------------------------------------------------------
// assignments for ports.
//----------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Implementation
//---------------------------------------------------------------------------------------------------------------------
always @(Number,Modules,Exponent) begin
start = 1'b1;
res_done <= 1'b0;
end
always @(posedge aclk) begin
if(!aresetn) begin
state <= STATE_INIT;
end else begin
case (state)
STATE_INIT :
begin
if (start) begin
state <= STATE_ROUND;
end
end
STATE_ROUND :
begin
if (E != 512'd1) begin
state <= STATE_NEXT;
end else begin
state <= STATE_DONE;
end
end
STATE_NEXT :
begin
state <= STATE_ROUND;
end
STATE_DONE :
begin
state <= STATE_INIT;
end
default : /* default */;
endcase
end
end
always @(posedge aclk) begin
if(!aresetn) begin
start <= 1'b0;
response <= 512'b0;
res_done <= 1'b0;
R <= 512'd1;
end else begin
case (state)
STATE_INIT :
begin
N <= Number%Modules;
E <= Exponent;
M <= Modules;
R <= 512'd1;
end
STATE_ROUND :
begin
start <= 1'b0;
if (E%2) begin //if m is odd
R <= (R*N)%M;
E <= E - 512'd1;
end
end
STATE_NEXT :
begin
N <= (N*N)%M;
E <= E/2;
end
STATE_DONE :
begin
response <= R;
res_done <= 1'b1;
end
default : /* default */;
endcase
end
end
endmodule //mod_power