-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryExponentiation.v
More file actions
100 lines (88 loc) · 1.86 KB
/
BinaryExponentiation.v
File metadata and controls
100 lines (88 loc) · 1.86 KB
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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 13:06:08 04/16/207
// Design Name:
// Module Name: BinaryExponentiation
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module BinaryExponentiation( input clk, input [8:0] base, input [7:0] exponent, input reset,
output reg [99:0] result, output reg isDone
);
reg [99:0] nextResult;
reg nextIsDone;
reg [3:0] nextIndex;
reg [3:0] index;
reg nextStart;
reg start;
initial begin
nextResult = 0;
result = 0;
nextIsDone = 0;
isDone = 0;
index = 7;
nextIndex = 7;
nextStart = 0;
start = 0;
end
always @ (posedge clk) begin
result <= nextResult;
isDone <= nextIsDone;
index <= nextIndex;
start <= nextStart;
end
always @ (*) begin
if (result == 0) begin
nextResult = base;
nextIsDone = 0;
end
else begin
if (isDone == 1) begin
if (reset == 1) begin
nextIsDone = 0;
nextResult = 0;
nextIndex = 7;
nextStart = 0;
end
else begin
nextIsDone = 1;
nextResult = result;
nextIndex = index;
nextStart = start;
end
end
else begin
nextIndex = index > 0 ? index - 1 : index;
nextIsDone = 0;
nextStart = start;
if (exponent[index] == 1 & start == 0) begin
nextStart = 1;
nextResult = result;
end
if (start == 1) begin
if (exponent[index] == 0) begin
nextResult = result * result;
end
else begin
nextResult = result * result * base;
end
if (index == 0) begin
nextIsDone = 1;
end
end
end
end
end
endmodule