OpenCores
URL https://opencores.org/ocsvn/tiny_tate_bilinear_pairing/tiny_tate_bilinear_pairing/trunk

Subversion Repositories tiny_tate_bilinear_pairing

[/] [tiny_tate_bilinear_pairing/] [trunk/] [group_size_is_911_bits/] [rtl/] [fsm.v] - Blame information for rev 11

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 homer.hsin
/*
2
    Copyright 2012 Homer Hsing
3
 
4
    This file is part of Tiny Tate Bilinear Pairing Core.
5
 
6
    Tiny Tate Bilinear Pairing Core is free software: you can redistribute it and/or modify
7
    it under the terms of the GNU Lesser General Public License as published by
8
    the Free Software Foundation, either version 3 of the License, or
9
    (at your option) any later version.
10
 
11
    Tiny Tate Bilinear Pairing Core is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU Lesser General Public License for more details.
15
 
16
    You should have received a copy of the GNU Lesser General Public License
17
    along with Tiny Tate Bilinear Pairing Core.  If not, see http://www.gnu.org/licenses/lgpl.txt
18
*/
19
 
20
/* FSM: finite state machine
21
 * halt if $ctrl == 0$
22
 */
23
module FSM(clk, reset, rom_addr, rom_q, ram_a_addr, ram_b_addr, ram_b_w, pe, done);
24
    input clk;
25
    input reset;
26
    output reg [8:0] rom_addr; /* command id. extra bits? */
27
    input [28:0] rom_q; /* command value */
28
    output reg [5:0] ram_a_addr;
29
    output reg [5:0] ram_b_addr;
30
    output ram_b_w;
31
    output reg [10:0] pe;
32
    output reg done;
33
 
34
    reg [4:0] state;
35
        parameter START=0, READ_SRC1=1, READ_SRC2=2, CALC=4, WAIT=8, WRITE=16, DON=3;
36
 
37
    wire [5:0] dest, src1, src2; wire [8:0] times; wire [1:0] op;
38
    assign {dest, src1, op, times, src2} = rom_q;
39
 
40
    reg [8:0] count;
41
 
42
    always @ (posedge clk)
43
       if (reset)
44
          state<=START;
45
       else
46
          case (state)
47
             START:
48
                state<=READ_SRC1;
49
             READ_SRC1:
50
                state<=READ_SRC2;
51
             READ_SRC2:
52
                if (times==0) state<=DON; else state<=CALC;
53
             CALC:
54
                if (count==1) state<=WAIT;
55
             WAIT:
56
                state<=WRITE;
57
             WRITE:
58
                state<=READ_SRC1;
59
          endcase
60
 
61
    /* we support two loops */
62
    parameter  LOOP1_START = 9'd21,
63
               LOOP1_END   = 9'd116,
64
               LOOP2_START = 9'd288,
65
               LOOP2_END   = 9'd301;
66
    reg [294:0] loop1, loop2;
67
 
68
        always @ (posedge clk)
69
           if (reset) rom_addr<=0;
70
           else if (state==WAIT)
71
          begin
72
             if(rom_addr == LOOP1_END && loop1[0])
73
                rom_addr <= LOOP1_START;
74
             else if(rom_addr == LOOP2_END && loop2[0])
75
                rom_addr <= LOOP2_START;
76
             else
77
                rom_addr <= rom_addr + 1'd1;
78
              end
79
 
80
        always @ (posedge clk)
81
           if (reset) loop1 <= ~0;
82
           else if(state==WAIT && rom_addr==LOOP1_END)
83
          loop1 <= loop1 >> 1;
84
 
85
        always @ (posedge clk)
86
           if (reset) loop2 <= ~0;
87
           else if(state==WAIT && rom_addr==LOOP2_END)
88
          loop2 <= loop2 >> 1;
89
 
90
        always @ (posedge clk)
91
           if (reset)
92
          count <= 0;
93
           else if (state==READ_SRC1)
94
          count <= times;
95
           else if (state==CALC)
96
          count <= count - 1'd1;
97
 
98
        always @ (posedge clk)
99
           if (reset) done<=0;
100
           else if (state==DON) done<=1;
101
           else done<=0;
102
 
103
    always @ (state, src1, src2)
104
       case (state)
105
       READ_SRC1: ram_a_addr=src1;
106
       READ_SRC2: ram_a_addr=src2;
107
       default: ram_a_addr=0;
108
       endcase
109
 
110
    parameter CMD_ADD=6'd4, CMD_SUB=6'd8, CMD_CUBIC=6'd16,
111
              ADD=2'd0, SUB=2'd1, CUBIC=2'd2, MULT=2'd3;
112
 
113
    always @ (posedge clk)
114
       case (state)
115
       READ_SRC1:
116
          case (op)
117
          ADD:   pe<=11'b11001000000;
118
          SUB:   pe<=11'b11001000000;
119
          CUBIC: pe<=11'b11111000000;
120
          MULT:  pe<=11'b11110000000;
121
          default: pe<=0;
122
          endcase
123
       READ_SRC2:
124
          case (op)
125
          ADD:   pe<=11'b00110000000;
126
          SUB:   pe<=11'b00110000000;
127
          CUBIC: pe<=0;
128
          MULT:  pe<=11'b00001000000;
129
          default: pe<=0;
130
          endcase
131
       CALC:
132
          case (op)
133
          ADD:   pe<=11'b00000010001;
134
          SUB:   pe<=11'b00000010001;
135
          CUBIC: pe<=11'b01010000001;
136
          MULT:  pe<=11'b00000111111;
137
          default: pe<=0;
138
          endcase
139
       default:
140
          pe<=0;
141
       endcase
142
 
143
    always @ (state, op, src2, dest)
144
       case (state)
145
       READ_SRC1:
146
          case (op)
147
          ADD: ram_b_addr=CMD_ADD;
148
          SUB: ram_b_addr=CMD_SUB;
149
          CUBIC: ram_b_addr=CMD_CUBIC;
150
          default: ram_b_addr=0;
151
          endcase
152
       READ_SRC2: ram_b_addr=src2;
153
       WRITE: ram_b_addr=dest;
154
       default: ram_b_addr=0;
155
       endcase
156
 
157
    assign ram_b_w = (state==WRITE) ? 1'b1 : 1'b0;
158
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.