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

Subversion Repositories crcahb

[/] [crcahb/] [trunk/] [rtl/] [crc_control_unit.v] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 redbear
//////////////////////////////////////////////////////////////////
2
////
3
////
4
////    CRCAHB CORE BLOCK
5
////
6
////
7
////
8
//// This file is part of the APB to I2C project
9
////
10
//// http://www.opencores.org/cores/apbi2c/
11
////
12
////
13
////
14
//// Description
15
////
16
//// Implementation of APB IP core according to
17
////
18
//// crcahb IP core specification document.
19
////
20
////
21
////
22
//// To Do: Things are right here but always all block can suffer changes
23
////
24
////
25
////
26
////
27
////
28
//// Author(s): -  Julio Cesar 
29
////
30
///////////////////////////////////////////////////////////////// 
31
////
32
////
33
//// Copyright (C) 2009 Authors and OPENCORES.ORG
34
////
35
////
36
////
37
//// This source file may be used and distributed without
38
////
39
//// restriction provided that this copyright statement is not
40
////
41
//// removed from the file and that any derivative work contains
42
//// the original copyright notice and the associated disclaimer.
43
////
44
////
45
//// This source file is free software; you can redistribute it
46
////
47
//// and/or modify it under the terms of the GNU Lesser General
48
////
49
//// Public License as published by the Free Software Foundation;
50
//// either version 2.1 of the License, or (at your option) any
51
////
52
//// later version.
53
////
54
////
55
////
56
//// This source is distributed in the hope that it will be
57
////
58
//// useful, but WITHOUT ANY WARRANTY; without even the implied
59
////
60
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
61
////
62
//// PURPOSE. See the GNU Lesser General Public License for more
63
//// details.
64
////
65
////
66
////
67
//// You should have received a copy of the GNU Lesser General
68
////
69
//// Public License along with this source; if not, download it
70
////
71
//// from http://www.opencores.org/lgpl.shtml
72
////
73
////
74
///////////////////////////////////////////////////////////////////
75
 
76 2 julioameri
module crc_control_unit
77
(
78
 //OUTPUTS
79
 output reg [1:0] byte_sel,
80
 output bypass_byte0,
81
 output buffer_full,
82
 output read_wait,
83
 output bypass_size,
84
 output set_crc_init_sel,
85
 output clear_crc_init_sel,
86
 output crc_out_en,
87
 output byte_en,
88
 output reset_pending,
89
 //INPUTS
90
 input [1:0] size_in,
91
 input write,
92
 input reset_chain,
93
 input clk,
94
 input rst_n
95
);
96
 
97
//States definition for state_full
98
localparam EMPTY   = 2'b00;
99
localparam WRITE_1 = 2'b01;
100
localparam WRITE_2 = 2'b10;
101
localparam BYPASS  = 2'b11;
102
 
103
//States definition for state_byte
104
localparam IDLE   = 3'b100;
105
localparam BYTE_0 = 3'b000;
106
localparam BYTE_1 = 3'b001;
107
localparam BYTE_2 = 3'b010;
108
localparam BYTE_3 = 3'b011;
109
 
110
//States definition for state_reset
111
localparam NO_RESET = 3'b000;
112
localparam RESET    = 3'b001;
113
localparam WAIT     = 3'b010;
114
localparam WRITE    = 3'b011;
115
localparam RESET_2  = 3'b100;
116
 
117
//Coding for size signal
118
localparam BYTE      = 2'b00;
119
localparam HALF_WORD = 2'b01;
120
localparam WORD      = 2'b10;
121
 
122
//Flops Definition
123
reg [1:0] state_full;
124
reg [2:0] state_byte;
125
reg [2:0] state_reset;
126
 
127
//Internal signals
128
reg [1:0] next_state_full;
129
reg [2:0] next_state_byte;
130
reg [2:0] next_state_reset;
131
 
132
wire last_byte;
133
wire has_data;
134
 
135
 
136
//FSM for management of writes in the input buffers 
137
//Definition of state register
138
always @(posedge clk)
139
 begin
140
  if(!rst_n)
141
   state_full <= EMPTY;
142
  else
143
   state_full <= next_state_full;
144
 end
145
 
146
//This signal indicates that the last byte is in processing
147
assign last_byte = (size_in == BYTE      && state_byte == BYTE_0) ||
148
                   (size_in == HALF_WORD && state_byte == BYTE_1) ||
149
                   (size_in == WORD      && state_byte == BYTE_3) ;
150
 
151
//Next state Logic
152
always @(*)
153
 begin
154
  next_state_full = state_full;
155
  case(state_full)
156
   EMPTY  : next_state_full = (write) ? WRITE_1 : EMPTY;
157
   WRITE_1:
158
    begin
159
     if(last_byte)
160
      begin
161
       if(!write)
162
        next_state_full = EMPTY;
163
      end
164
     else
165
      begin
166
       if(write)
167
        next_state_full = WRITE_2;
168
      end
169
    end
170
   WRITE_2:
171
    begin
172
     if(last_byte)
173
      next_state_full = (write) ? BYPASS : WRITE_1;
174
    end
175
   BYPASS :
176
    begin
177
     if(last_byte && !write)
178
      next_state_full = WRITE_1;
179
    end
180
  endcase
181
 end
182
 
183
//The flag full indicates that buffer is full and any attempt of writing must wait
184
assign buffer_full = (state_full == WRITE_2 && !last_byte) ||
185
                     (state_full == BYPASS  && !last_byte);
186
 
187
assign read_wait = (state_byte != IDLE);
188
 
189
//This signal controls the selection of the byte0 
190
//When bypass_byte0 = 1 the input of byte_ff is taken
191
//Otherwise, its output is taken
192
assign bypass_byte0 = (state_full != BYPASS);
193
 
194
//This signal indicates that there are data in the second position of the buffer
195
assign has_data = (state_full == WRITE_2) ||
196
                  (state_full == BYPASS ) ;
197
 
198
 
199
//FSM for management of readings in the buffer
200
//Definition of state register
201
always @(posedge clk)
202
 begin
203
  if(!rst_n)
204
   state_byte <= IDLE;
205
  else
206
   state_byte <= next_state_byte;
207
 end
208
 
209
always @(*)
210
 begin
211
  next_state_byte = state_byte;
212
  case(state_byte)
213
   IDLE: next_state_byte = (write) ? BYTE_0 : IDLE;
214
   BYTE_0:
215
    begin
216
     if(size_in == BYTE)
217
      begin
218
       if(!write && !has_data)
219
        next_state_byte = IDLE;
220
      end
221
     else
222
      begin
223
       next_state_byte = BYTE_1;
224
      end
225
    end
226
   BYTE_1:
227
    begin
228
     if(size_in == HALF_WORD)
229
      begin
230
       if(has_data || (write && !buffer_full))
231
        next_state_byte = BYTE_0;
232
       else
233
        next_state_byte = IDLE;
234
      end
235
     else
236
      begin
237
       next_state_byte = BYTE_2;
238
      end
239
    end
240
   BYTE_2:
241
    begin
242
     next_state_byte = BYTE_3;
243
    end
244
   BYTE_3:
245
    begin
246
     if(has_data || (write && !buffer_full))
247
      next_state_byte = BYTE_0;
248
     else
249
      next_state_byte = IDLE;
250
    end
251
  endcase
252
 end
253
 
254
//The signal byte_sel controls the number of byte that will be processed by CRC Unit
255
always @(*)
256
 begin
257
  byte_sel = 2'b00;
258
  case(state_byte)
259
   BYTE_0: byte_sel = BYTE_0;
260
   BYTE_1: byte_sel = BYTE_1;
261
   BYTE_2: byte_sel = BYTE_2;
262
   BYTE_3: byte_sel = BYTE_3;
263
  endcase
264
 end
265
//This signal controls the selection of the metadata size 
266
//When bypass_size = 1 the input of size_ff is taken
267
//Otherwise, its output is taken
268
assign bypass_size = !( (state_full != BYPASS && state_byte != BYTE_0) ||
269
                        (state_full == BYPASS)
270
                      );
271
 
272
//This signal enables the write in the crc_out register
273
assign crc_out_en = (state_byte != IDLE);
274
 
275
//
276
assign byte_en = (state_byte == BYTE_0 && (size_in == HALF_WORD || size_in == WORD) && state_full != BYPASS) ||
277
                 (last_byte && has_data);
278
 
279
//FSM for control of reset of chained operation
280
//Definition of state register
281
always @(posedge clk)
282
 begin
283
  if(!rst_n)
284
   state_reset <= NO_RESET;
285
  else
286
   state_reset <= next_state_reset;
287
 end
288
 
289
always @(*)
290
 begin
291
  next_state_reset = state_reset;
292
  case(state_reset)
293
   NO_RESET:
294
    begin
295
     if((reset_chain && !has_data && state_byte != IDLE && !last_byte) || (reset_chain && has_data && last_byte))
296
      next_state_reset = RESET;
297
     if(reset_chain  && has_data && !last_byte)
298
      next_state_reset = WAIT;
299
    end
300
   RESET:
301
    begin
302
     if(last_byte)
303
      next_state_reset = NO_RESET;
304
     else
305
      next_state_reset = (write) ? WRITE : RESET;
306
    end
307
   WAIT:
308
    begin
309
     if(last_byte)
310
      next_state_reset = (write) ? WRITE : RESET;
311
     else
312
      next_state_reset = WAIT;
313
    end
314
   WRITE:
315
    begin
316
     if(reset_chain)
317
      next_state_reset = (last_byte) ? RESET : RESET_2;
318
     else
319
      next_state_reset = (last_byte) ? NO_RESET : WRITE;
320
    end
321
   RESET_2:
322
    begin
323
     if(last_byte)
324
      next_state_reset = (write) ? WRITE : RESET;
325
     else
326
      next_state_reset = RESET_2;
327
    end
328
  endcase
329
 end
330
 
331
//This signal set the crc_init_sel flop
332
//When seted this flop turn on the chained operation of crc 
333
assign set_crc_init_sel = (state_byte == BYTE_0);
334
 
335
//This signal clear the crc_init_sel
336
//The clear get priority over set
337
assign clear_crc_init_sel = (state_reset == NO_RESET && last_byte && reset_chain) ||
338
                            (state_byte  == IDLE     && reset_chain             ) ||
339
                            (state_reset == RESET    && last_byte               ) ||
340
                            (state_reset == WRITE    && last_byte               ) ||
341
                            (state_reset == RESET_2  && last_byte               ) ;
342
 
343
assign reset_pending = (state_reset != NO_RESET);
344
 
345
endmodule

powered by: WebSVN 2.1.0

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