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

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [tags/] [ADS_RELEASE_2_5_0/] [Hardware/] [adv_dbg_if/] [rtl/] [verilog/] [adbg_wb_module.v] - Blame information for rev 3

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

Line No. Rev Author Line
1 3 nyawn
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  adbg_wb_module.v                                            ////
4
////                                                              ////
5
////                                                              ////
6
////  This file is part of the SoC Advanced Debug Interface.      ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////       Nathan Yawn (nathan.yawn@opencores.org)                ////
10
////                                                              ////
11
////                                                              ////
12
////                                                              ////
13
//////////////////////////////////////////////////////////////////////
14
////                                                              ////
15
//// Copyright (C) 2008        Authors                            ////
16
////                                                              ////
17
//// This source file may be used and distributed without         ////
18
//// restriction provided that this copyright statement is not    ////
19
//// removed from the file and that any derivative work contains  ////
20
//// the original copyright notice and the associated disclaimer. ////
21
////                                                              ////
22
//// This source file is free software; you can redistribute it   ////
23
//// and/or modify it under the terms of the GNU Lesser General   ////
24
//// Public License as published by the Free Software Foundation; ////
25
//// either version 2.1 of the License, or (at your option) any   ////
26
//// later version.                                               ////
27
////                                                              ////
28
//// This source is distributed in the hope that it will be       ////
29
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
30
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
31
//// PURPOSE.  See the GNU Lesser General Public License for more ////
32
//// details.                                                     ////
33
////                                                              ////
34
//// You should have received a copy of the GNU Lesser General    ////
35
//// Public License along with this source; if not, download it   ////
36
//// from http://www.opencores.org/lgpl.shtml                     ////
37
////                                                              ////
38
//////////////////////////////////////////////////////////////////////
39
//
40
// CVS Revision History
41
//
42
// $Log: adbg_wb_module.v,v $
43
// Revision 1.2  2009/05/17 20:54:57  Nathan
44
// Changed email address to opencores.org
45
//
46
// Revision 1.1  2008/07/22 20:28:33  Nathan
47
// Changed names of all files and modules (prefixed an a, for advanced).  Cleanup, indenting.  No functional changes.
48
//
49
// Revision 1.12  2008/07/11 08:13:30  Nathan
50
// Latch opcode on posedge, like other signals.  This fixes a problem when 
51
// the module is used with a Xilinx BSCAN TAP.  Added signals to allow modules 
52
// to inhibit latching of a new active module by the top module.  This allows 
53
// the sub-modules to force the top level module to ignore the command present
54
// in the input shift register after e.g. a burst read.
55
//
56
 
57
 
58
`include "adbg_wb_defines.v"
59
 
60
// Top module
61
module adbg_wb_module (
62
                       // JTAG signals
63
                       tck_i,
64
                       module_tdo_o,
65
                       tdi_i,
66
 
67
                       // TAP states
68
                       capture_dr_i,
69
                       shift_dr_i,
70
                       update_dr_i,
71
 
72
                       data_register_i,  // the data register is at top level, shared between all modules
73
                       module_select_i,
74
                       top_inhibit_o,
75
                       rst_i,
76
 
77
                       // WISHBONE common signals
78
                       wb_clk_i,
79
 
80
                       // WISHBONE master interface
81
                       wb_adr_o, wb_dat_o, wb_dat_i, wb_cyc_o, wb_stb_o, wb_sel_o,
82
                       wb_we_o, wb_ack_i, wb_cab_o, wb_err_i, wb_cti_o, wb_bte_o
83
 
84
                       );
85
 
86
   // JTAG signals
87
   input         tck_i;
88
   output        module_tdo_o;
89
   input         tdi_i;  // This is only used by the CRC module - data_register_i[MSB] is delayed a cycle
90
 
91
   // TAP states
92
   input         capture_dr_i;
93
   input         shift_dr_i;
94
   input         update_dr_i;
95
 
96
   input [52:0]  data_register_i;
97
   input         module_select_i;
98
   output        top_inhibit_o;
99
   input         rst_i;
100
 
101
   // WISHBONE master interface
102
   input         wb_clk_i;
103
   output [31:0] wb_adr_o;
104
   output [31:0] wb_dat_o;
105
   input [31:0]  wb_dat_i;
106
   output        wb_cyc_o;
107
   output        wb_stb_o;
108
   output [3:0]  wb_sel_o;
109
   output        wb_we_o;
110
   input         wb_ack_i;
111
   output        wb_cab_o;
112
   input         wb_err_i;
113
   output [2:0]  wb_cti_o;
114
   output [1:0]  wb_bte_o;
115
   //reg           wb_cyc_o;
116
 
117
   // Declare inputs / outputs as wires / registers
118
   reg           module_tdo_o;
119
   reg           top_inhibit_o;
120
 
121
 
122
   // Registers to hold state etc.
123
   reg [31:0]     address_counter;     // Holds address for next Wishbone access
124
   reg [5:0]      bit_count;            // How many bits have been shifted in/out
125
   reg [15:0]     word_count;          // bytes remaining in current burst command
126
   reg [3:0]      operation;            // holds the current command (rd/wr, word size)
127
   reg [32:0]     data_out_shift_reg;  // 32 bits to accomodate the internal_reg_error
128
   reg [`DBG_WB_REGSELECT_SIZE-1:0] internal_register_select;  // Holds index of currently selected register
129
   reg [32:0]                        internal_reg_error;  // WB error module internal register.  32 bit address + error bit (LSB)
130
 
131
 
132
   // Control signals for the various counters / registers / state machines
133
   reg                              addr_sel;          // Selects data for address_counter. 0 = data_register_i, 1 = incremented address count
134
   reg                              addr_ct_en;        // Enable signal for address counter register
135
   reg                              op_reg_en;         // Enable signal for 'operation' register
136
   reg                              bit_ct_en;         // enable bit counter
137
   reg                              bit_ct_rst;        // reset (zero) bit count register
138
   reg                              word_ct_sel;       // Selects data for byte counter.  0 = data_register_i, 1 = decremented byte count
139
   reg                              word_ct_en;        // Enable byte counter register
140
   reg                              out_reg_ld_en;     // Enable parallel load of data_out_shift_reg
141
   reg                              out_reg_shift_en;  // Enable shift of data_out_shift_reg
142
   reg                              out_reg_data_sel;  // 0 = BIU data, 1 = internal register data
143
   reg [1:0]                         tdo_output_sel;  // Selects signal to send to TDO.  0 = ready bit, 1 = output register, 2 = CRC match, 3 = CRC shift reg.
144
   reg                              biu_strobe;      // Indicates that the bus unit should latch data and start a transaction
145
   reg                              crc_clr;         // resets CRC module
146
   reg                              crc_en;          // does 1-bit iteration in CRC module
147
   reg                              crc_in_sel;      // selects incoming write data (=0) or outgoing read data (=1)as input to CRC module
148
   reg                              crc_shift_en;    // CRC reg is also it's own output shift register; this enables a shift
149
   reg                              regsel_ld_en;    // Reg. select register load enable
150
   reg                              intreg_ld_en;    // load enable for internal registers
151
   reg                              error_reg_en;    // Tells the error register to check for and latch a bus error
152
   reg                              biu_clr_err;     // Allows FSM to reset BIU, to clear the biu_err bit which may have been set on the last transaction of the last burst.
153
 
154
   // Status signals
155
   wire                             word_count_zero;   // true when byte counter is zero
156
   wire                             bit_count_max;     // true when bit counter is equal to current word size
157
   wire                             module_cmd;        // inverse of MSB of data_register_i. 1 means current cmd not for top level (but is for us)
158
   wire                             biu_ready;         // indicates that the BIU has finished the last command
159
   wire                             biu_err;           // indicates wishbone error during BIU transaction
160
   wire                             burst_instruction; // True when the input_data_i reg has a valid burst instruction for this module
161
   wire                             intreg_instruction; // True when the input_data_i reg has a valid internal register instruction
162
   wire                             intreg_write;       // True when the input_data_i reg has an internal register write op
163
   reg                              rd_op;              // True when operation in the opcode reg is a read, false when a write
164
   wire                             crc_match;         // indicates whether data_register_i matches computed CRC
165
   wire                             bit_count_32;      // true when bit count register == 32, for CRC after burst writes
166
 
167
   // Intermediate signals
168
   reg [5:0]                         word_size_bits;          // 8,16, or 32.  Decoded from 'operation'
169
   reg [2:0]                         word_size_bytes;         // 1,2, or 4
170
   wire [32:0]                       incremented_address;   // value of address counter plus 'word_size'
171
   wire [31:0]                       data_to_addr_counter;  // output of the mux in front of the address counter inputs
172
   wire [15:0]                       data_to_word_counter;  // output of the mux in front of the byte counter input
173
   wire [15:0]                       decremented_word_count;
174
   wire [31:0]                       address_data_in;       // from data_register_i
175
   wire [15:0]                       count_data_in;         // from data_register_i
176
   wire [3:0]                        operation_in;          // from data_register_i
177
   wire [31:0]                       data_to_biu;           // from data_register_i
178
   wire [31:0]                       data_from_biu;         // to data_out_shift_register
179
   wire [31:0]                       crc_data_out;          // output of CRC module, to output shift register
180
   wire                             crc_data_in;                  // input to CRC module, either data_register_i[52] or data_out_shift_reg[0]
181
   wire                             crc_serial_out;
182
   wire [`DBG_WB_REGSELECT_SIZE-1:0] reg_select_data; // from data_register_i, input to internal register select register
183
   wire [32:0]                        out_reg_data;           // parallel input to the output shift register
184
   reg [32:0]                         data_from_internal_reg;  // data from internal reg. MUX to output shift register
185
   wire                              biu_rst;                       // logical OR of rst_i and biu_clr_err
186
 
187
   /////////////////////////////////////////////////
188
   // Combinatorial assignments
189
 
190
       assign module_cmd = ~(data_register_i[52]);
191
   assign     operation_in = data_register_i[51:48];
192
   assign     address_data_in = data_register_i[47:16];
193
   assign     count_data_in = data_register_i[15:0];
194
   assign     data_to_biu = data_register_i[52:21];
195
   assign     reg_select_data = data_register_i[47:(47-(`DBG_WB_REGSELECT_SIZE-1))];
196
 
197
   ////////////////////////////////////////////////
198
              // Operation decoder
199
 
200
   // These are only used before the operation is latched, so decode them from operation_in
201
   assign     burst_instruction = (~operation_in[3]) & (operation_in[0] | operation_in[1]);
202
   assign     intreg_instruction = ((operation_in == `DBG_WB_CMD_IREG_WR) | (operation_in == `DBG_WB_CMD_IREG_SEL));
203
   assign     intreg_write = (operation_in == `DBG_WB_CMD_IREG_WR);
204
 
205
 
206
   // This is decoded from the registered operation
207
   always @ (operation)
208
     begin
209
        case(operation)
210
          `DBG_WB_CMD_BWRITE8:
211
            begin
212
               word_size_bits <= 5'd7;  // Bits is actually bits-1, to make the FSM easier
213
               word_size_bytes <= 3'd1;
214
               rd_op <= 1'b0;
215
            end
216
          `DBG_WB_CMD_BWRITE16:
217
            begin
218
               word_size_bits <= 5'd15;  // Bits is actually bits-1, to make the FSM easier
219
               word_size_bytes <= 3'd2;
220
               rd_op <= 1'b0;
221
            end
222
          `DBG_WB_CMD_BWRITE32:
223
            begin
224
               word_size_bits <= 5'd31;  // Bits is actually bits-1, to make the FSM easier
225
               word_size_bytes <= 3'd4;
226
               rd_op <= 1'b0;
227
            end
228
          `DBG_WB_CMD_BREAD8:
229
            begin
230
               word_size_bits <= 5'd7;  // Bits is actually bits-1, to make the FSM easier
231
               word_size_bytes <= 3'd1;
232
               rd_op <= 1'b1;
233
            end
234
          `DBG_WB_CMD_BREAD16:
235
            begin
236
               word_size_bits <= 5'd15;  // Bits is actually bits-1, to make the FSM easier
237
               word_size_bytes <= 3'd2;
238
               rd_op <= 1'b1;
239
            end
240
          `DBG_WB_CMD_BREAD32:
241
            begin
242
               word_size_bits <= 5'd31;  // Bits is actually bits-1, to make the FSM easier
243
               word_size_bytes <= 3'd4;
244
               rd_op <= 1'b1;
245
            end
246
          default:
247
            begin
248
               word_size_bits <= 5'hXX;
249
               word_size_bytes <= 3'hX;
250
               rd_op <= 1'bX;
251
            end
252
        endcase
253
     end
254
 
255
 
256
   ////////////////////////////////////////////////
257
   // Module-internal register select register (no, that's not redundant.)
258
   // Also internal register output MUX
259
 
260
   always @ (posedge tck_i or posedge rst_i)
261
     begin
262
        if(rst_i) internal_register_select = 1'h0;
263
        else if(regsel_ld_en) internal_register_select = reg_select_data;
264
     end
265
 
266
   // This is completely unnecessary here, since the WB module has only 1 internal
267
   // register.  However, to make the module expandable, it is included anyway.
268
   always @ (internal_register_select or internal_reg_error)
269
     begin
270
        case(internal_register_select)
271
          `DBG_WB_INTREG_ERROR: data_from_internal_reg = internal_reg_error;
272
          default: data_from_internal_reg = internal_reg_error;
273
        endcase
274
     end
275
 
276
 
277
 
278
   ////////////////////////////////////////////////////////////////////
279
   // Module-internal registers
280
   // These have generic read/write/select code, but
281
   // individual registers may have special behavior, defined here.
282
 
283
   // This is the bus error register, which traps WB errors
284
   // We latch every new BIU address in the upper 32 bits, so we always have the address for the transaction which
285
   // generated the error (the address counter might increment, esp. for writes)
286
   // We stop latching addresses when the error bit (bit 0) is set. Keep the error bit set until it is 
287
   // manually cleared by a module internal register write.
288
   // Note we use reg_select_data straight from data_register_i, rather than the latched version - 
289
   // otherwise, we would write the previously selected register.
290
 
291
 
292
   always @ (posedge tck_i or posedge rst_i)
293
     begin
294
        if(rst_i) internal_reg_error = 33'h0;
295
        else if(intreg_ld_en && (reg_select_data == `DBG_WB_INTREG_ERROR))  // do load from data input register
296
          begin
297
             if(data_register_i[46]) internal_reg_error[0] = 1'b0;  // if write data is 1, reset the error bit
298
          end
299
        else if(error_reg_en && !internal_reg_error[0])
300
          begin
301
             if(biu_err)  internal_reg_error[0] = 1'b1;
302
             else if(biu_strobe) internal_reg_error[32:1] = address_counter;
303
          end
304
        else if(biu_strobe && !internal_reg_error[0]) internal_reg_error[32:1] = address_counter;  // When no error, latch this whether error_reg_en or not
305
     end
306
 
307
   ///////////////////////////////////////////////
308
   // Address counter
309
 
310
   assign data_to_addr_counter = (addr_sel) ? incremented_address[31:0] : address_data_in;
311
   assign incremented_address = address_counter + word_size_bytes;
312
 
313
   // Technically, since this data (sometimes) comes from the input shift reg, we should latch on
314
   // negedge, per the JTAG spec. But that makes things difficult when incrementing.
315
   always @ (posedge tck_i or posedge rst_i)  // JTAG spec specifies latch on negative edge in UPDATE_DR state
316
     begin
317
        if(rst_i)
318
          address_counter <= 32'h0;
319
        else if(addr_ct_en)
320
          address_counter <= data_to_addr_counter;
321
     end
322
 
323
   ////////////////////////////////////////
324
     // Opcode latch
325
 
326
   always @ (posedge tck_i or posedge rst_i)  // JTAG spec specifies latch on negative edge in UPDATE_DR state
327
     begin
328
        if(rst_i)
329
          operation <= 4'h0;
330
        else if(op_reg_en)
331
          operation <= operation_in;
332
     end
333
 
334
   //////////////////////////////////////
335
     // Bit counter
336
 
337
   always @ (posedge tck_i or posedge rst_i)
338
     begin
339
 
340
        if(rst_i)             bit_count <= 6'h0;
341
        else if(bit_ct_rst)  bit_count <= 6'h0;
342
        else if(bit_ct_en)    bit_count <= bit_count + 6'h1;
343
 
344
     end
345
 
346
   assign bit_count_max = (bit_count == word_size_bits) ? 1'b1 : 1'b0 ;
347
   assign bit_count_32 = (bit_count == 6'h20) ? 1'b1 : 1'b0;
348
 
349
   ////////////////////////////////////////
350
   // Word counter
351
 
352
   assign data_to_word_counter = (word_ct_sel) ?  decremented_word_count : count_data_in;
353
   assign decremented_word_count = word_count - 16'h1;
354
 
355
   // Technically, since this data (sometimes) comes from the input shift reg, we should latch on
356
   // negedge, per the JTAG spec. But that makes things difficult when incrementing.
357
   always @ (posedge tck_i or posedge rst_i)  // JTAG spec specifies latch on negative edge in UPDATE_DR state
358
     begin
359
        if(rst_i)
360
          word_count <= 16'h0;
361
        else if(word_ct_en)
362
          word_count <= data_to_word_counter;
363
     end
364
 
365
   assign word_count_zero = (word_count == 16'h0);
366
 
367
   /////////////////////////////////////////////////////
368
                            // Output register and TDO output MUX
369
 
370
                            assign out_reg_data = (out_reg_data_sel) ? data_from_internal_reg : {1'b0,data_from_biu};
371
 
372
   always @ (posedge tck_i or posedge rst_i)
373
     begin
374
        if(rst_i) data_out_shift_reg <= 33'h0;
375
        else if(out_reg_ld_en) data_out_shift_reg <= out_reg_data;
376
        else if(out_reg_shift_en) data_out_shift_reg <= {1'b0, data_out_shift_reg[32:1]};
377
     end
378
 
379
 
380
   always @ (tdo_output_sel or data_out_shift_reg[0] or biu_ready or crc_match or crc_serial_out)
381
     begin
382
        if(tdo_output_sel == 2'h0) module_tdo_o <= biu_ready;
383
        else if(tdo_output_sel == 2'h1) module_tdo_o <= data_out_shift_reg[0];
384
        else if(tdo_output_sel == 2'h2) module_tdo_o <= crc_match;
385
        else module_tdo_o <= crc_serial_out;
386
     end
387
 
388
   ////////////////////////////////////////
389
     // Bus Interface Unit
390
   // It is assumed that the BIU has internal registers, and will
391
   // latch address, operation, and write data on rising clock edge 
392
   // when strobe is asserted
393
 
394
   assign biu_rst = rst_i | biu_clr_err;
395
 
396
   adbg_wb_biu wb_biu_i
397
     (
398
      // Debug interface signals
399
      .tck_i           (tck_i),
400
      .rst_i           (biu_rst),
401
      .data_i          (data_to_biu),
402
      .data_o          (data_from_biu),
403
      .addr_i          (address_counter),
404
      .strobe_i        (biu_strobe),
405
      .rd_wrn_i        (rd_op),           // If 0, then write op
406
      .rdy_o           (biu_ready),
407
      .err_o           (biu_err),
408
      .word_size_i     (word_size_bytes),
409
 
410
      // Wishbone signals
411
      .wb_clk_i        (wb_clk_i),
412
      .wb_adr_o        (wb_adr_o),
413
      .wb_dat_o        (wb_dat_o),
414
      .wb_dat_i        (wb_dat_i),
415
      .wb_cyc_o        (wb_cyc_o),
416
      .wb_stb_o        (wb_stb_o),
417
      .wb_sel_o        (wb_sel_o),
418
      .wb_we_o         (wb_we_o),
419
      .wb_ack_i        (wb_ack_i),
420
      .wb_cab_o        (wb_cab_o),
421
      .wb_err_i        (wb_err_i),
422
      .wb_cti_o        (wb_cti_o),
423
      .wb_bte_o        (wb_bte_o)
424
      );
425
 
426
   /////////////////////////////////////
427
       // CRC module
428
 
429
       assign crc_data_in = (crc_in_sel) ? tdi_i : data_out_shift_reg[0];  // MUX, write or read data
430
 
431
   adbg_crc32 wb_crc_i
432
     (
433
      .clk(tck_i),
434
      .data(crc_data_in),
435
      .enable(crc_en),
436
      .shift(crc_shift_en),
437
      .clr(crc_clr),
438
      .rst(rst_i),
439
      .crc_out(crc_data_out),
440
      .serial_out(crc_serial_out)
441
      );
442
 
443
   assign     crc_match = (data_register_i[52:21] == crc_data_out) ? 1'b1 : 1'b0;
444
 
445
   ////////////////////////////////////////
446
   // Control FSM
447
 
448
   // Definition of machine state values.
449
   // Don't worry too much about the state encoding, the synthesis tool
450
   // will probably re-encode it anyway.
451
 
452
`define STATE_idle     4'h0
453
`define STATE_Rbegin   4'h1
454
`define STATE_Rready   4'h2
455
`define STATE_Rstatus  4'h3
456
`define STATE_Rburst   4'h4
457
`define STATE_Wready   4'h5
458
`define STATE_Wwait    4'h6
459
`define STATE_Wburst   4'h7
460
`define STATE_Wstatus  4'h8
461
`define STATE_Rcrc     4'h9
462
`define STATE_Wcrc     4'ha
463
`define STATE_Wmatch   4'hb
464
 
465
   reg [3:0]  module_state;       // FSM state
466
   reg [3:0]  module_next_state;  // combinatorial signal, not actually a register
467
 
468
 
469
 
470
   // sequential part of the FSM
471
   always @ (posedge tck_i or posedge rst_i)
472
     begin
473
        if(rst_i)
474
          module_state <= `STATE_idle;
475
        else
476
          module_state <= module_next_state;
477
     end
478
 
479
 
480
   // Determination of next state; purely combinatorial
481
   always @ (module_state or module_select_i or module_cmd or update_dr_i or capture_dr_i or operation_in[2]
482
             or word_count_zero or bit_count_max or data_register_i[52] or bit_count_32 or biu_ready or burst_instruction)
483
     begin
484
        case(module_state)
485
          `STATE_idle:
486
            begin
487
               if(module_cmd && module_select_i && update_dr_i && burst_instruction && operation_in[2]) module_next_state <= `STATE_Rbegin;
488
               else if(module_cmd && module_select_i && update_dr_i && burst_instruction) module_next_state <= `STATE_Wready;
489
               else module_next_state <= `STATE_idle;
490
            end
491
 
492
          `STATE_Rbegin:
493
            begin
494
               if(word_count_zero) module_next_state <= `STATE_idle;  // set up a burst of size 0, illegal.
495
               else module_next_state <= `STATE_Rready;
496
            end
497
          `STATE_Rready:
498
            begin
499
               if(module_select_i && capture_dr_i) module_next_state <= `STATE_Rstatus;
500
               else module_next_state <= `STATE_Rready;
501
            end
502
          `STATE_Rstatus:
503
            begin
504
               if(update_dr_i) module_next_state <= `STATE_idle;
505
               else if (biu_ready) module_next_state <= `STATE_Rburst;
506
               else module_next_state <= `STATE_Rstatus;
507
            end
508
          `STATE_Rburst:
509
            begin
510
               if(update_dr_i) module_next_state <= `STATE_idle;
511
               else if(bit_count_max && word_count_zero) module_next_state <= `STATE_Rcrc;
512
               else if(bit_count_max) module_next_state <= `STATE_Rstatus;
513
               else module_next_state <= `STATE_Rburst;
514
            end
515
          `STATE_Rcrc:
516
            begin
517
               if(update_dr_i) module_next_state <= `STATE_idle;
518
               // This doubles as the 'recovery' state, so stay here until update_dr_i.
519
               else module_next_state <= `STATE_Rcrc;
520
            end
521
 
522
          `STATE_Wready:
523
            begin
524
               if(word_count_zero) module_next_state <= `STATE_idle;
525
               else if(module_select_i && capture_dr_i) module_next_state <= `STATE_Wwait;
526
               else module_next_state <= `STATE_Wready;
527
            end
528
          `STATE_Wwait:
529
            begin
530
               if(update_dr_i)  module_next_state <= `STATE_idle;  // client terminated early
531
               else if(module_select_i && data_register_i[52]) module_next_state <= `STATE_Wburst; // Got a start bit
532
               else module_next_state <= `STATE_Wwait;
533
            end
534
          `STATE_Wburst:
535
            begin
536
               if(update_dr_i)  module_next_state <= `STATE_idle;  // client terminated early    
537
               else if(bit_count_max) module_next_state <= `STATE_Wstatus;
538
               else module_next_state <= `STATE_Wburst;
539
            end
540
          `STATE_Wstatus:
541
            begin
542
               if(update_dr_i)  module_next_state <= `STATE_idle;  // client terminated early    
543
               else if(word_count_zero) module_next_state <= `STATE_Wcrc;
544
               // can't wait until bus ready if multiple devices in chain...
545
               // Would have to read postfix_bits, then send another start bit and push it through
546
               // prefix_bits...potentially very inefficient.
547
               else module_next_state <= `STATE_Wburst;
548
            end
549
 
550
          `STATE_Wcrc:
551
            begin
552
               if(update_dr_i)  module_next_state <= `STATE_idle;  // client terminated early
553
               else if(bit_count_32) module_next_state <= `STATE_Wmatch;
554
               else module_next_state <= `STATE_Wcrc;
555
            end
556
 
557
          `STATE_Wmatch:
558
            begin
559
               if(update_dr_i)  module_next_state <= `STATE_idle;
560
               // This doubles as our recovery state, stay here until update_dr_i
561
               else module_next_state <= `STATE_Wmatch;
562
            end
563
 
564
          default: module_next_state <= `STATE_idle;  // shouldn't actually happen...
565
        endcase
566
     end
567
 
568
 
569
   // Outputs of state machine, pure combinatorial
570
   always @ (module_state or module_next_state or module_select_i or update_dr_i or capture_dr_i or shift_dr_i or operation_in[2]
571
             or word_count_zero or bit_count_max or data_register_i[52] or biu_ready or intreg_instruction or module_cmd
572
             or intreg_write or decremented_word_count)
573
     begin
574
        // Default everything to 0, keeps the case statement simple
575
        addr_sel <= 1'b1;  // Selects data for address_counter. 0 = data_register_i, 1 = incremented address count
576
        addr_ct_en <= 1'b0;  // Enable signal for address counter register
577
        op_reg_en <= 1'b0;  // Enable signal for 'operation' register
578
        bit_ct_en <= 1'b0;  // enable bit counter
579
        bit_ct_rst <= 1'b0;  // reset (zero) bit count register
580
        word_ct_sel <= 1'b1;  // Selects data for byte counter.  0 = data_register_i, 1 = decremented byte count
581
        word_ct_en <= 1'b0;   // Enable byte counter register
582
        out_reg_ld_en <= 1'b0;  // Enable parallel load of data_out_shift_reg
583
        out_reg_shift_en <= 1'b0;  // Enable shift of data_out_shift_reg
584
        tdo_output_sel <= 2'b1;   // 1 = data reg, 0 = biu_ready, 2 = crc_match, 3 = CRC data
585
        biu_strobe <= 1'b0;
586
        crc_clr <= 1'b0;
587
        crc_en <= 1'b0;      // add the input bit to the CRC calculation
588
        crc_in_sel <= 1'b0;  // 0 = tdo, 1 = tdi
589
        crc_shift_en <= 1'b0;
590
        out_reg_data_sel <= 1'b1;  // 0 = BIU data, 1 = internal register data
591
        regsel_ld_en <= 1'b0;
592
        intreg_ld_en <= 1'b0;
593
        error_reg_en <= 1'b0;
594
        biu_clr_err <= 1'b0;  // Set this to reset the BIU, clearing the biu_err bit
595
        top_inhibit_o <= 1'b0;  // Don't disable the top-level module in the default case
596
 
597
        case(module_state)
598
          `STATE_idle:
599
            begin
600
               addr_sel <= 1'b0;
601
               word_ct_sel <= 1'b0;
602
 
603
               // Operations for internal registers - stay in idle state
604
               if(module_select_i & shift_dr_i) out_reg_shift_en <= 1'b1; // For module regs
605
               if(module_select_i & capture_dr_i)
606
                 begin
607
                    out_reg_data_sel <= 1'b1;  // select internal register data
608
                    out_reg_ld_en <= 1'b1;   // For module regs
609
                 end
610
               if(module_select_i & module_cmd & update_dr_i) begin
611
                  if(intreg_instruction) regsel_ld_en <= 1'b1;  // For module regs
612
                  if(intreg_write)       intreg_ld_en <= 1'b1;  // For module regs
613
               end
614
 
615
               // Burst operations
616
               if(module_next_state != `STATE_idle) begin  // Do the same to receive read or write opcode
617
                  addr_ct_en <= 1'b1;
618
                  op_reg_en <= 1'b1;
619
                  bit_ct_rst <= 1'b1;
620
                  word_ct_en <= 1'b1;
621
                  crc_clr <= 1'b1;
622
               end
623
            end
624
 
625
          `STATE_Rbegin:
626
            begin
627
               if(!word_count_zero) begin  // Start a biu read transaction
628
                  biu_strobe <= 1'b1;
629
                  addr_sel <= 1'b1;
630
                  addr_ct_en <= 1'b1;
631
               end
632
            end
633
 
634
          `STATE_Rready:
635
            ; // Just a wait state
636
 
637
          `STATE_Rstatus:
638
            begin
639
               tdo_output_sel <= 2'h0;
640
               top_inhibit_o <= 1'b1;    // in case of early termination
641
 
642
               if (module_next_state == `STATE_Rburst) begin
643
                  error_reg_en <= 1'b1;       // Check the wb_error bit
644
                  out_reg_data_sel <= 1'b0;  // select BIU data
645
                  out_reg_ld_en <= 1'b1;
646
                  bit_ct_rst <= 1'b1;
647
                  word_ct_sel <= 1'b1;
648
                  word_ct_en <= 1'b1;
649
                  if(!(decremented_word_count == 0) && !word_count_zero) begin  // Start a biu read transaction
650
                     biu_strobe <= 1'b1;
651
                     addr_sel <= 1'b1;
652
                     addr_ct_en <= 1'b1;
653
                  end
654
               end
655
            end
656
 
657
          `STATE_Rburst:
658
            begin
659
               tdo_output_sel <= 2'h1;
660
               out_reg_shift_en <= 1'b1;
661
               bit_ct_en <= 1'b1;
662
               crc_en <= 1'b1;
663
               crc_in_sel <= 1'b0;  // read data in output shift register LSB (tdo)
664
               top_inhibit_o <= 1'b1;  // in case of early termination
665
            end
666
 
667
          `STATE_Rcrc:
668
            begin
669
               // Just shift out the data, don't bother counting, we don't move on until update_dr_i
670
               tdo_output_sel <= 2'h3;
671
               crc_shift_en <= 1'b1;
672
               top_inhibit_o <= 1'b1;
673
            end
674
 
675
          `STATE_Wready:
676
            ; // Just a wait state
677
 
678
          `STATE_Wwait:
679
            begin
680
               tdo_output_sel <= 2'h1;
681
               top_inhibit_o <= 1'b1;    // in case of early termination
682
               if(module_next_state == `STATE_Wburst) begin
683
                  biu_clr_err <= 1'b1;  // If error occurred on last transaction of last burst, biu_err is still set.  Clear it.
684
                  bit_ct_en <= 1'b1;
685
                  word_ct_sel <= 1'b1;  // Pre-decrement the byte count
686
                  word_ct_en <= 1'b1;
687
                  crc_en <= 1'b1;  // CRC gets tdi_i, which is 1 cycle ahead of data_register_i, so we need the bit there now in the CRC
688
                  crc_in_sel <= 1'b1;  // read data from tdi_i
689
               end
690
            end
691
 
692
          `STATE_Wburst:
693
            begin
694
               bit_ct_en <= 1'b1;
695
               tdo_output_sel <= 2'h1;
696
               crc_en <= 1'b1;
697
               crc_in_sel <= 1'b1;  // read data from tdi_i
698
               top_inhibit_o <= 1'b1;    // in case of early termination
699
            end
700
 
701
          `STATE_Wstatus:
702
            begin
703
               tdo_output_sel <= 2'h0;  // Send the status bit to TDO
704
               error_reg_en <= 1'b1;       // Check the wb_error bit
705
               // start transaction
706
               biu_strobe <= 1'b1;  // Start a BIU transaction
707
               word_ct_sel <= 1'b1;  // Decrement the byte count
708
               word_ct_en <= 1'b1;
709
               bit_ct_rst <= 1'b1;  // Zero the bit count
710
               addr_ct_en <= 1'b1;  // Increment thte address counter
711
               top_inhibit_o <= 1'b1;    // in case of early termination
712
            end
713
 
714
          `STATE_Wcrc:
715
            begin
716
               bit_ct_en <= 1'b1;
717
               top_inhibit_o <= 1'b1;    // in case of early termination
718
               if(module_next_state == `STATE_Wmatch) tdo_output_sel <= 2'h2;  // This is when the 'match' bit is actually read
719
            end
720
 
721
          `STATE_Wmatch:
722
            begin
723
               tdo_output_sel <= 2'h2;
724
               top_inhibit_o <= 1'b1;
725
               // Bit of a hack here...an error on the final write won't be detected in STATE_Wstatus like the rest, 
726
               // so we assume the bus transaction is done and check it / latch it into the error register here.
727
               if(module_next_state == `STATE_idle) error_reg_en <= 1'b1;
728
            end
729
 
730
          default: ;
731
        endcase
732
     end
733
 
734
 
735
endmodule
736
 

powered by: WebSVN 2.1.0

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