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

Subversion Repositories spacewire

[/] [spacewire/] [trunk/] [rtl/] [SPW_FSM.v] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 btltz
//File name=Module name=SPW_FSM  2005-2-18      btltz@mail.china.com    btltz from CASIC  
2
//Description:   The state exit conditions for SpaceWire encoder-decoder state machine:
3
//               Coder/Decoder FSM,also called the PPU(Protocol Processing Unit) Controls the overall operation of the link interface
4
//Abbreviations: FCT:     flow control token
5
//               N-Char:  normal characters(data or EOP or EEP).   L_Chars:Link characters.
6
//               EOP:     End_of_packet marcker;     EEP: error End_of_packet marcker.      
7
//Origin:        SpaceWire Std - Draft-1 of ECSS(European Cooperation for Space Standardization),ESTEC,ESA.
8
//--     TODO:    make rtl faster
9
////////////////////////////////////////////////////////////////////////////////////
10
//
11
//
12
/*synthesis translate off*/
13
`timescale 1ns/100ps
14
/*synthesis translate on */
15
`define gFreq  80
16
`define ErrorReset  6'b000001
17
`define ErrorWait   6'b000010
18
`define Ready       6'b000100
19
`define Started     6'b001000
20
`define Connecting  6'b010000
21
`define Run         6'b100000
22
`define reset  1          // WISHBONE standard reset
23
`define FOR_SIM
24
//`define FOR_REAL
25
 
26
module SPW_FSM  //#(parameter DW=8)
27
               (output [5:0] state_o ,    //state is a global vector signal of the CODEC
28
                output active_o,      //Active interface
29
                input  lnk_start,lnk_dis, //Link enable interface, set by software or hardware
30
                                         input  AUTOSTART,
31
 
32
                                         output reg LINK_ERR_o,
33
                output reg err_sqc,   //err_Nchar or err_FCT as sequence err "should be detected by PPU"
34
 
35
//Interface with Transmitter
36
                                    output RST_tx_o,
37
                                         output reg enTx_o,  //control output to Transmitter
38
                                         input err_crd_i,
39
//Interface with Receiver
40
                                    output RST_rx_o,
41
                                         output reg enRx_o,   //control output to Receiver 
42
                                         input Lnk_dsc_i,
43
                                         input gotBit_i,
44
                      gotFCT_i,gotNchar_i,gotTime_i,gotNULL_i,
45
                      err_par_i,err_esc_i,err_dsc_i,       //input from receiver
46
//global signal input
47
                input reset,
48
                input gclk   /* synthesis syn_isclock = 1 */
49
                 );
50
 
51
                parameter StateNum = 6;
52
                parameter ErrorReset = `ErrorReset;  //6'b000001;
53
                parameter ErrorWait  = `ErrorWait;   //6'b000010;
54
                parameter Ready      = `Ready;       //6'b000100;
55
                parameter Started    = `Started;     //6'b001000;        
56
                parameter Connecting = `Connecting;  //6'b010000;
57
                parameter Run        = `Run;         //6'b100000;       
58
 
59
                                         parameter DEFLT      = 6'bxxxxxx;//6'bxxxxxx;
60
 
61
                parameter True = 1,  False = 0;
62
                                         `ifdef FOR_SIM
63
                parameter NUM_T6_4uS = 10 , NUM_T12_8uS = 20;
64
                                         `else
65
                parameter NUM_T6_4uS = (`gFreq==80) ? (64*8-1) :
66
                                                                (`gFreq==100) ? (64*10-1) :
67
                                                                                                        (`gFreq==120)  ? (64*12-1)      :
68
                                                                                                        (`gFreq==50)    ? (64*5-1);
69
                                         parameter NUM_T12_8uS =(`gFreq==80) ? (128*8-1)  :
70
                                                                (`gFreq==100) ? (128*10-1) :
71
                                                                                                        (`gFreq==120)  ? (128*12-1) :
72
                                                                                                        (`gFreq==50)    ? (128*5-1);
73
                                         `endif
74
                parameter TIMERW = 14;   //Timer width MAX=16,384. *1ns for 16us
75
 
76
reg [StateNum-1:0] state, next_state/* synthesis syn_encoding="safe,onhot" */;
77
reg t6_4us,t12_8us;          //The output of the timer indicate if adequate time has elapsed
78
 
79
///////////////////
80
// Output Generate
81
//
82
reg HASgotNULL;
83
reg HASgotBit;
84
always @(posedge gclk)
85
if(reset ||state==ErrorReset)
86
  begin
87
  HASgotNULL <= 0;
88
  HASgotBit <= 0;
89
  end
90
else begin
91
     if(gotNULL_i)
92
     HASgotNULL <= 1'b1;
93
     if(gotBit_i)
94
          HASgotBit <= 1'b1;
95
     end
96
 
97
assign RST_tx_o = reset || (state == ErrorReset || state==ErrorWait || state==Ready);
98
assign RST_rx_o = reset || (state == ErrorReset);
99
assign state_o = state;
100
assign lnk_en = ~lnk_dis && ( lnk_start || (AUTOSTART && HASgotNULL) );//internal "lnk_en" to enable link
101
 
102
assign active_o = lnk_en &&
103
                 ( !Lnk_dsc_i  //indicate Rx is receiving
104
                  || state==Started || state==Connecting  || state==Run );//indicate Tx is transmitting
105
 
106
////////////////////////////
107
// err_sqc   made
108
// Charactor sequence error
109
always @(posedge gclk)
110
if(reset)
111
    err_sqc <= 0;
112
else begin
113
     err_sqc <= 0;
114
     if( gotFCT_i && (state==ErrorWait || state==Ready || state==Started)
115
            || gotNchar_i && state!=Run )
116
     err_sqc <= 1;  //character sequence error can only occur during initialization.
117
          end
118
 
119
//////////////////////
120
// PPU  (fsm)
121
//
122
always @(posedge gclk)
123
begin:STATES_TRANSFER
124
  if (reset || lnk_dis || Lnk_dsc_i)
125
     state <= ErrorReset;  //Initialized state
126
  else                    // state transfer
127
     state <= next_state;
128
end  //end block "STATES_TRANSFER"
129
 
130
//------ next_state assignment
131
always @(*)
132
begin:NEXT_ASSIGN
133
  //Default Values for FSM outputs:
134
    /*RST_tx_o = 1'b0;                 //not reset
135
    RST_rx_o = 1'b0;    */
136
    enRx_o = 1'b1;
137
    enTx_o = 1'b0;
138
         LINK_ERR_o = 1'b0;
139
 
140
  //Use "Default next_state" style ->
141
    next_state = state;
142
 
143
  case(state)  /* synthesis parallel_case */
144
         ErrorReset    : begin/*Time-lapse*///Entered when the link interface is reset, when the link is disabled [Link Disabled] in the 
145
                                            //Run state, or when error conditions occur in any other state.
146
                           enRx_o = 1'b0;
147
//The error detecting end shall be reset and re-initialized to recover character synchronization and flow control status.
148
                           if(lnk_en && t6_4us==True)
149
                           next_state = ErrorWait;
150
                         end
151
         ErrorWait:      begin/*Time-lapse*///Entered from ErrorReset state after being in ErrorReset state for 6,4us
152
                           if(HASgotBit && lnk_dis
153
                             || HASgotNULL && (err_par_i || err_esc_i || gotFCT_i || gotNchar_i || gotTime_i) )
154
                           next_state = ErrorReset;
155
                           else if(lnk_en && t12_8us==True)
156
                             next_state = Ready;
157
                         end
158
         Ready/*Temp*/ : begin  //Entered from ErrorWait state after being in ErrorWait state for 12,8us
159
                           if( HASgotBit && err_dsc_i
160
                              || HASgotNULL && (err_par_i || err_esc_i || gotFCT_i || gotNchar_i || gotTime_i) )
161
                           next_state = ErrorReset;
162
                           else if(lnk_en)
163
                           next_state = Started;
164
                         end
165
         Started       : begin   //Entered from Ready state when [LinkEnabled] guard is TRUE
166
                           enTx_o = 1'b1;
167
                           if( (t12_8us==True) ||  //after 12,8us
168
                                HASgotBit && err_dsc_i ||
169
                                HASgotNULL && (err_par_i || err_esc_i || gotFCT_i || gotNchar_i || gotTime_i) )
170
                             next_state = ErrorReset;   //GotNULL Timeout
171
                           else if (gotNULL_i ==True)
172
                             next_state = Connecting;
173
                         end
174
         Connecting    : begin    //Entered from Started state on receipt of gotNULL (which also satisfies First Bit Received)
175
                          enTx_o = 1'b1;
176
                          if( (t12_8us==True)     ||  //gotFCT Timeout
177
                              (err_dsc_i==True) ||   //First Bit Received as part of the gotNULL
178
                                err_par_i       ||    //First NULL Received is already True in order to enter this state
179
                                err_esc_i          ||     //First NULL Received is already True in order to enter this state
180
                                gotNchar_i      ||      //First NULL Received is already True in order to enter this state
181
                                gotTime_i )         //First NULL Received is already True in order to enter this state                                
182
                          next_state = ErrorReset;
183
                          else if(gotFCT_i==True)
184
                          next_state = Run;
185
                         end
186
         Run           : begin //Entered from Connecting state when FCT received. First Bit Received and
187
                                     //gotNULL conditions are TRUE since they were True in Connecting state.
188
                           enTx_o = 1'b1;
189
                           if(  lnk_dis
190
                                                                           || err_dsc_i //First Bit Received is already True since passed through Connecting State
191
                              || err_par_i //First NULL Received is already True since passed through Connecting State
192
                              || err_esc_i //First NULL Received is already True since passed through Connecting State
193
                              || err_crd_i //First NULL Received is already True since passed through Connecting State
194
                              || err_sqc  )//If the escape error occurs in the Run state then the escape error shall be flagged up to the Network Level as a "link error"   
195
                           begin
196
                                                                        LINK_ERR_o = 1'b1;
197
                           next_state = ErrorReset;
198
                           end
199
                                                                 end
200
           /* Default assignment for simulation */
201
         default : next_state = DEFLT;
202
   endcase
203
 
204
end //end block NEXT_ASSIGN(and output assignment)
205
 
206
 
207
//////////////////////////////////
208
// The Timer for 6.4us & 12.8us
209
reg [TIMERW-1:0] timer;
210
always @(posedge gclk)
211
begin:TIMER
212
  if(reset || (state==Run) || (state==Ready) || ( (state==Started) && gotNULL_i ) )
213
      begin
214
                t6_4us <= 1'b0;
215
                t12_8us <= 1'b0;
216
                timer <= 0;
217
                end
218
  else if (timer == NUM_T12_8uS)
219
       begin
220
                 t12_8us <= 1'b1;   //Timer overflow pulse to trigger states transform
221
                 t6_4us <= 1'b0;
222
       timer <= 0;
223
       end
224
  else if ( (state==ErrorReset) && (timer ==NUM_T6_4uS ) )
225
       begin
226
                 t6_4us <= 1'b1;
227
                 timer <= 0;
228
                 t12_8us <= 1'b0;
229
                 end
230
  else
231
      begin
232
                 t6_4us <= 1'b0;
233
                 t12_8us <= 1'b0;
234
       timer <= timer + 1'b1;
235
      end
236
end   //End block "TIMER" ...
237
 
238
//...................................................................................
239
endmodule
240
 
241
`undef ErrorReset
242
`undef ErrorWait
243
`undef Ready
244
`undef Started
245
`undef Connecting
246
`undef Run
247
`undef reset

powered by: WebSVN 2.1.0

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