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

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [trunk/] [Hardware/] [adv_dbg_if/] [bench/] [simulated_system/] [adv_dbg_tb.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
////  adv_dbg_tb.v                                                ////
4
////                                                              ////
5
////                                                              ////
6
////  Testbench for the SoC Advanced Debug Interface.             ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////       Nathan Yawn (nathan.yawn@opencored.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: adv_dbg_tb.v,v $
43
// Revision 1.2  2009/05/17 20:54:55  Nathan
44
// Changed email address to opencores.org
45
//
46
// Revision 1.1  2008/07/08 19:11:55  Nathan
47
// Added second testbench to simulate a complete system, including OR1200, wb_conbus, and onchipram.  Renamed sim-only testbench directory from verilog to simulated_system.
48
//
49
// Revision 1.11  2008/07/08 18:53:47  Nathan
50
// Fixed wrong include name.
51
//
52
// Revision 1.10  2008/06/30 20:09:19  Nathan
53
// Removed code to select top-level module as active (it served no purpose).  Re-numbered modules, requiring changes to testbench and software driver.
54
//
55
 
56
 
57
`include "tap_defines.v"
58
`include "dbg_defines.v"
59
`include "dbg_wb_defines.v"
60
`include "wb_model_defines.v"
61
 
62
// Polynomial for the CRC calculation
63
// Yes, it's backwards.  Yes, this is on purpose.
64
// To decrease logic + routing, we want to shift the CRC calculation
65
// in the same direction we use to shift the data out, LSB first.
66
`define DBG_CRC_POLY 32'hedb88320
67
 
68
// These are indicies into an array which hold values for the JTAG outputs
69
`define JTAG_TMS 0
70
`define JTAG_TCK 1
71
`define JTAG_TDO 2
72
 
73
`define JTAG_TMS_bit 3'h1
74
`define JTAG_TCK_bit 3'h2
75
`define JTAG_TDO_bit 3'h4
76
 
77
`define wait_jtag_period #50
78
 
79
 
80
module adv_debug_tb;
81
 
82
// Connections to the JTAG TAP
83
reg jtag_tck_o;
84
reg jtag_tms_o;
85
reg jtag_tdo_o;
86
wire jtag_tdi_i;
87
 
88
// Connections between TAP and debug module
89
wire capture_dr;
90
wire shift_dr;
91
wire pause_dr;
92
wire update_dr;
93
wire dbg_rst;
94
wire dbg_tdi;
95
wire dbg_tdo;
96
wire dbg_sel;
97
 
98
// Connections between the debug module and the wishbone
99
`ifdef DBG_WISHBONE_SUPPORTED
100
wire [31:0] wb_adr;
101
wire [31:0] wb_dat_m;
102
wire [31:0] wb_dat_s;
103
wire wb_cyc;
104
wire wb_stb;
105
wire [3:0] wb_sel;
106
wire wb_we;
107
wire wb_ack;
108
wire wb_err;
109
reg wb_clk_i;  // the wishbone clock
110
reg wb_rst_i;
111
`endif
112
 
113
`ifdef DBG_CPU0_SUPPORTED
114
wire cpu0_clk;
115
wire [31:0]cpu0_addr;
116
wire [31:0] cpu0_data_c;
117
wire [31:0] cpu0_data_d;
118
wire cpu0_bp;
119
wire cpu0_stall;
120
wire cpu0_stb;
121
wire cpu0_we;
122
wire cpu0_ack;
123
wire cpu0_rst;
124
`endif
125
 
126
`ifdef DBG_CPU1_SUPPORTED
127
reg cpu1_clk;
128
wire [31:0]cpu1_addr;
129
wire [31:0] cpu1_data_c;
130
wire [31:0] cpu1_data_d;
131
wire cpu1_bp;
132
wire cpu1_stall;
133
wire cpu1_stb;
134
wire cpu1_we;
135
wire cpu1_ack;
136
wire cpu1_rst;
137
`endif //  `ifdef DBG_CPU1_SUPPORTED
138
 
139
reg test_enabled;
140
 
141
// Data which will be written to the WB interface
142
reg [31:0] static_data32 [0:15];
143
reg [15:0] static_data16 [0:15];
144
reg [7:0] static_data8 [0:15];
145
 
146
// Arrays to hold data read back from the WB interface, for comparison
147
reg [31:0] input_data32 [0:15];
148
reg [15:0] input_data16 [0:15];
149
reg [7:0]  input_data8 [0:15];
150
 
151
reg [32:0] err_data;  // holds the contents of the error register from the various modules
152
 
153
reg failed;
154
integer i;
155
 
156
initial
157
begin
158
   jtag_tck_o = 1'b0;
159
   jtag_tms_o = 1'b0;
160
   jtag_tdo_o = 1'b0;
161
end
162
 
163
// Provide the wishbone clock
164
`ifdef DBG_WISHBONE_SUPPORTED
165
initial
166
begin
167
  wb_clk_i = 1'b0;
168
  forever #7 wb_clk_i = ~wb_clk_i;  // Odd frequency ratio to test the synchronization
169
end
170
`endif
171
 
172
// Provide the CPU0 clock
173
//`ifdef DBG_CPU0_SUPPORTED
174
//initial
175
//begin
176
  //cpu0_clk = 1'b0;
177
  //forever #6 cpu0_clk = ~cpu0_clk;  // Odd frequency ratio to test the synchronization
178
//end
179
//`endif
180
 
181
// Start the test (and reset the wishbone)
182
initial
183
begin
184
  test_enabled = 1'b0;
185
  wb_rst_i = 1'b0;
186
  #100;
187
  wb_rst_i = 1'b1;
188
  #100;
189
  wb_rst_i = 1'b0;
190
 
191
   // Init the memory
192
  initialize_memory(32'h0,32'h16);
193
 
194
  // Init the WB model
195
  i_wb.cycle_response(`ACK_RESPONSE, 2, 0);  // response type, wait cycles, retry_cycles
196
 
197
  #1 test_enabled<=#1 1'b1;
198
end
199
 
200
// This is the main test procedure
201
always @ (posedge test_enabled)
202
begin
203
 
204
  $display("Starting advanced debug test");
205
 
206
  reset_jtag;
207
  #1000;
208
  check_idcode;
209
  #1000;
210
 
211
  // Select the debug module in the IR
212
  set_ir(`DEBUG);
213
  #1000;
214
 
215
 
216
  ///////////////////////////////////////////////////////////////////
217
  // Test CPU0 unit
218
  ////////////////////////////////////////////////////////////////////
219
`ifdef DBG_CPU0_SUPPORTED
220
  // Select the CPU0 unit in the debug module
221
  #1000;
222
  $display("Selecting CPU0 module at time %t", $time);
223
  select_debug_module(`DBG_TOP_CPU0_DEBUG_MODULE);
224
 
225
   // Test reset, stall bits
226
   #1000;
227
   $display("Testing CPU0 intreg select at time %t", $time);
228
   select_module_internal_register(32'h1, 1);  // Really just a read, with discarded data
229
   #1000;
230
   select_module_internal_register(32'h0, 1);  // Really just a read, with discarded data
231
   #1000;
232
 
233
   // Read the stall and reset bits
234
   $display("Testing reset and stall bits at time %t", $time);
235
   read_module_internal_register(8'd2, err_data);  // We assume the register is already selected
236
   $display("Reset and stall bits are %x", err_data);
237
   #1000;
238
 
239
   //  Set rst/stall bits
240
   $display("Setting reset and stall bits at time %t", $time);
241
    write_module_internal_register(32'h0, 8'h1, 32'h3, 8'h2);  // idx, idxlen, data, datalen
242
   #1000;
243
 
244
   // Read the bits again
245
   $display("Testing reset and stall bits again at time %t", $time);
246
   read_module_internal_register(8'd2, err_data);  // We assume the register is already selected
247
   $display("Reset and stall bits are %x", err_data);
248
   #1000;
249
 
250
   // Clear the bits
251
      $display("Clearing reset and stall bits at time %t", $time);
252
    write_module_internal_register(32'h0, 8'h1, 32'h0, 8'h2);  // idx, idxlen, data, datalen
253
   #1000;
254
 
255
      // Read the bits again
256
   $display("Testing reset and stall bits again at time %t", $time);
257
   read_module_internal_register(8'd2, err_data);  // We assume the register is already selected
258
   $display("Reset and stall bits are %x", err_data);
259
   #1000;
260
 
261
   // Behavioral CPU model must be stalled in order to do SPR access
262
   //$display("Setting reset and stall bits at time %t", $time);    
263
   write_module_internal_register(32'h0, 8'h1, 32'h1, 8'h2);  // idx, idxlen, data, datalen
264
   #1000;
265
 
266
   // Test SPR bus access
267
  $display("Testing CPU0 32-bit burst write at time %t", $time);
268
  do_module_burst_write(3'h4, 16'd16, 32'h10);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
269
  #1000;
270
  $display("Testing CPU0 32-bit burst read at time %t", $time);
271
  do_module_burst_read(3'h4, 16'd16, 32'h0);
272
  #1000;
273
 
274
`endif
275
 
276
 
277
  ///////////////////////////////////////////////////////////////////
278
  // Test the Wishbone unit
279
  ////////////////////////////////////////////////////////////////////
280
 
281
`ifdef DBG_WISHBONE_SUPPORTED
282
  // Select the WB unit in the debug module
283
  #1000;
284
  $display("Selecting Wishbone module at time %t", $time);
285
  select_debug_module(`DBG_TOP_WISHBONE_DEBUG_MODULE);
286
 
287
  /*
288
  // Test error conditions
289
  #1000;
290
  $display("Testing error (size 0 WB burst write) at time %t", $time);
291
  do_module_burst_write(3'h1, 16'h0, 32'h0);  // 0-word write = error, ignored
292
  #1000;
293
  $display("Testing error (size 0 WB burst read) at time %t", $time);
294
  do_module_burst_read(3'h1, 16'h0, 32'h0);  // 0-word read = error, ignored
295
 
296
  // Test NOP (a zero in the MSB, then a NOP opcode)
297
  #1000;
298
  $display("Testing NOP at time %t", $time);
299
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
300
    write_bit(3'h0);           // capture_ir
301
    write_bit(3'h0);           // shift_ir
302
    jtag_write_stream(5'h0, 8'h5, 1);  // write data, exit_1
303
    write_bit(`JTAG_TMS_bit);  // update_dr
304
    write_bit(3'h0);           // idle
305
    #1000;
306
    */
307
 
308
    #1000;
309
     $display("Testing WB intreg select at time %t", $time);
310
    select_module_internal_register(32'h1, 1);  // Really just a read, with discarded data
311
    #1000;
312
     select_module_internal_register(32'h0, 1);  // Really just a read, with discarded data
313
   #1000;
314
 
315
   // Reset the error bit    
316
    write_module_internal_register(32'h0, 8'h1, 32'h1, 8'h1);  // idx, idxlen, data, datalen
317
   #1000;
318
 
319
   // Read the error bit
320
   read_module_internal_register(8'd33, err_data);  // We assume the register is already selected
321
   #1000;
322
 
323
 
324
  /////////////////////////////////
325
  // Test 8-bit WB access
326
  failed = 0;
327
  $display("Testing WB 8-bit burst write at time %t: resetting ", $time);
328
  do_module_burst_write(3'h1, 16'd16, 32'h0);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
329
  #1000;
330
  $display("Testing WB 8-bit burst read at time %t", $time);
331
  do_module_burst_read(3'h1, 16'd16, 32'h0);
332
  #1000;
333
   for(i = 0; i < 16; i = i+1) begin
334
     if(static_data8[i] != input_data8[i]) begin
335
        failed = 1;
336
        $display("32-bit data mismatch at index %d, wrote 0x%x, read 0x%x", i, static_data8[i], input_data8[i]);
337
    end
338
  end
339
  if(!failed) $display("8-bit read/write OK!");
340
 
341
   /* try it unaligned
342
  do_module_burst_write(3'h1, 16'd5, 32'h3);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
343
    #1000;
344
  do_module_burst_read(3'h1, 16'd4, 32'h4);
345
    #1000;
346
  */
347
 
348
  /////////////////////////////////
349
  // Test 16-bit WB access
350
  failed = 0;
351
  $display("Testing WB 16-bit burst write at time %t", $time);
352
  do_module_burst_write(3'h2, 16'd16, 32'h0);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
353
  #1000;
354
  $display("Testing WB 16-bit burst read at time %t", $time);
355
  do_module_burst_read(3'h2, 16'd16, 32'h0);
356
  #1000;
357
   for(i = 0; i < 16; i = i+1) begin
358
     if(static_data16[i] != input_data16[i]) begin
359
        failed = 1;
360
        $display("16-bit data mismatch at index %d, wrote 0x%x, read 0x%x", i, static_data16[i], input_data16[i]);
361
    end
362
  end
363
  if(!failed) $display("16-bit read/write OK!");
364
 
365
   /* try it unaligned
366
  do_module_burst_write(3'h2, 16'd5, 32'h2);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
367
    #1000;
368
  do_module_burst_read(3'h2, 16'd4, 32'h4);
369
    #1000;
370
  */
371
 
372
  ////////////////////////////////////
373
  // Test 32-bit WB access
374
  failed = 0;
375
  $display("Testing WB 32-bit burst write at time %t", $time);
376
  do_module_burst_write(3'h4, 16'd16, 32'h0);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
377
  #1000;
378
  $display("Testing WB 32-bit burst read at time %t", $time);
379
  do_module_burst_read(3'h4, 16'd16, 32'h0);
380
  #1000;
381
   for(i = 0; i < 16; i = i+1) begin
382
     if(static_data32[i] != input_data32[i]) begin
383
        failed = 1;
384
        $display("32-bit data mismatch at index %d, wrote 0x%x, read 0x%x", i, static_data32[i], input_data32[i]);
385
    end
386
  end
387
  if(!failed) $display("32-bit read/write OK!");
388
 
389
  /* Try another address
390
  do_module_burst_write(3'h4, 16'd16, 32'h200);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
391
    #1000;
392
  do_module_burst_read(3'h4, 16'd15, 32'h204);
393
    #1000;
394
  */
395
 
396
  ////////////////////////////////
397
  // Test error register
398
  err_data = 33'h0;
399
  // Select and reset the error register
400
  write_module_internal_register(`DBG_WB_INTREG_ERROR, `DBG_WB_REGSELECT_SIZE, 64'h1, 8'h1); // regidx,idxlen,writedata, datalen;
401
  i_wb.cycle_response(`ERR_RESPONSE, 2, 0);  // response type, wait cycles, retry_cycles
402
  do_module_burst_write(3'h4, 16'd4, 32'hdeaddead);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
403
  read_module_internal_register(8'd33, err_data);  // get the error register
404
  $display("Error bit is %d, error address is %x", err_data[0], err_data>>1);
405
 
406
`endif  // WB module supported
407
 
408
end
409
 
410
task initialize_memory;
411
  input [31:0] start_addr;
412
  input [31:0] length;
413
  integer i;
414
  reg [31:0] addr;
415
  begin
416
 
417
    for (i=0; i<length; i=i+1)
418
      begin
419
        static_data32[i] <= {i[7:0], i[7:0]+2'd1, i[7:0]+2'd2, i[7:0]+2'd3};
420
        static_data16[i] <= {i[7:0], i[7:0]+ 2'd1};
421
        static_data8[i] <= i[7:0];
422
      end
423
  end
424
endtask
425
 
426
///////////////////////////////////////////////////////////////////////////////
427
// Declaration and interconnection of components
428
 
429
// Top module
430
tap_top  i_tap (
431
                // JTAG pads
432
                .tms_pad_i(jtag_tms_o),
433
                .tck_pad_i(jtag_tck_o),
434
                .trstn_pad_i(1'b1),
435
                .tdi_pad_i(jtag_tdo_o),
436
                .tdo_pad_o(jtag_tdi_i),
437
                .tdo_padoe_o(),
438
 
439
                // TAP states
440
                                    .test_logic_reset_o(dbg_rst),
441
                                    .run_test_idle_o(),
442
                .shift_dr_o(shift_dr),
443
                .pause_dr_o(pause_dr),
444
                .update_dr_o(update_dr),
445
                .capture_dr_o(capture_dr),
446
 
447
                // Select signals for boundary scan or mbist
448
                .extest_select_o(),
449
                .sample_preload_select_o(),
450
                .mbist_select_o(),
451
                .debug_select_o(dbg_sel),
452
 
453
                // TDO signal that is connected to TDI of sub-modules.
454
                .tdo_o(dbg_tdo),
455
 
456
                // TDI signals from sub-modules
457
                .debug_tdi_i(dbg_tdi),    // from debug module
458
                .bs_chain_tdi_i(1'b0), // from Boundary Scan Chain
459
                .mbist_tdi_i(1'b0)     // from Mbist Chain
460
              );
461
 
462
 
463
// Top module
464
dbg_top i_dbg_module(
465
                // JTAG signals
466
                .tck_i(jtag_tck_o),
467
                .tdi_i(dbg_tdo),
468
                .tdo_o(dbg_tdi),
469
                .rst_i(dbg_rst),
470
 
471
                // TAP states
472
                .shift_dr_i(shift_dr),
473
                .pause_dr_i(pause_dr),
474
                .update_dr_i(update_dr),
475
                .capture_dr_i(capture_dr),
476
 
477
                // Instructions
478
                .debug_select_i(dbg_sel)
479
 
480
 
481
                `ifdef DBG_WISHBONE_SUPPORTED
482
                // WISHBONE common signals
483
                ,
484
                .wb_clk_i(wb_clk_i),
485
 
486
                // WISHBONE master interface
487
                .wb_adr_o(wb_adr),
488
                .wb_dat_o(wb_dat_m),
489
                .wb_dat_i(wb_dat_s),
490
                .wb_cyc_o(wb_cyc),
491
                .wb_stb_o(wb_stb),
492
                .wb_sel_o(wb_sel),
493
                .wb_we_o(wb_we),
494
                .wb_ack_i(wb_ack),
495
                .wb_cab_o(),
496
                .wb_err_i(wb_err),
497
                .wb_cti_o(),
498
                .wb_bte_o()
499
                `endif
500
 
501
                `ifdef DBG_CPU0_SUPPORTED
502
                // CPU signals
503
                ,
504
                .cpu0_clk_i(cpu0_clk),
505
                .cpu0_addr_o(cpu0_addr),
506
                .cpu0_data_i(cpu0_data_c),
507
                .cpu0_data_o(cpu0_data_d),
508
                .cpu0_bp_i(cpu0_bp),
509
                .cpu0_stall_o(cpu0_stall),
510
                .cpu0_stb_o(cpu0_stb),
511
                .cpu0_we_o(cpu0_we),
512
                .cpu0_ack_i(cpu0_ack),
513
                .cpu0_rst_o(cpu0_rst)
514
                `endif
515
 
516
                `ifdef DBG_CPU1_SUPPORTED
517
                // CPU signals
518
                ,
519
                .cpu1_clk_i(cpu1_clk),
520
                .cpu1_addr_o(cpu1_addr),
521
                .cpu1_data_i(cpu1_data_c),
522
                .cpu1_data_o(cpu1_data_d),
523
                .cpu1_bp_i(cpu1_bp),
524
                .cpu1_stall_o(cpu1_stall),
525
                .cpu1_stb_o(cpu1_stb),
526
                .cpu1_we_o(cpu1_we),
527
                .cpu1_ack_i(cpu1_ack),
528
                .cpu1_rst_o(cpu1_rst)
529
                `endif
530
 
531
              );
532
 
533
 
534
`ifdef DBG_WISHBONE_SUPPORTED
535
// The 'wishbone' may be just a p2p connection to a simple RAM
536
/*
537
onchip_ram_top i_ocram (
538
   .wb_clk_i(wb_clk_i),
539
   .wb_rst_i(wb_rst_i),
540
   .wb_dat_i(wb_dat_m),
541
   .wb_dat_o(wb_dat_s),
542
   .wb_adr_i(wb_adr[11:0]),
543
   .wb_sel_i(wb_sel),
544
   .wb_we_i(wb_we),
545
   .wb_cyc_i(wb_cyc),
546
   .wb_stb_i(wb_stb),
547
   .wb_ack_o(wb_ack),
548
   .wb_err_o(wb_err)
549
);
550
*/
551
 
552
wb_slave_behavioral i_wb
553
(
554
        .CLK_I(wb_clk_i),
555
        .RST_I(wb_rst_i),
556
        .ACK_O(wb_ack),
557
        .ADR_I(wb_adr),
558
        .CYC_I(wb_cyc),
559
        .DAT_O(wb_dat_s),
560
        .DAT_I(wb_dat_m),
561
        .ERR_O(wb_err),
562
        .RTY_O(),
563
        .SEL_I(wb_sel),
564
        .STB_I(wb_stb),
565
        .WE_I(wb_we),
566
        .CAB_I(1'b0)
567
);
568
`endif
569
 
570
 
571
`ifdef DBG_CPU0_SUPPORTED
572
// Instantiate a behavioral model of the CPU SPR bus
573
cpu_behavioral cpu0_i  (
574
   .cpu_rst_i(cpu0_rst),
575
   .cpu_clk_o(cpu0_clk),
576
   .cpu_addr_i(cpu0_addr),
577
   .cpu_data_o(cpu0_data_c),
578
   .cpu_data_i(cpu0_data_d),
579
   .cpu_bp_o(cpu0_bp),
580
   .cpu_stall_i(cpu0_stall),
581
   .cpu_stb_i(cpu0_stb),
582
   .cpu_we_i(cpu0_we),
583
   .cpu_ack_o(cpu0_ack),
584
   .cpu_rst_o(cpu0_rst)
585
);
586
 
587
`endif
588
 
589
 
590
`ifdef DBG_CPU1_SUPPORTED
591
// Instantiate a behavioral model of the CPU SPR bus
592
cpu_behavioral cpu1_i  (
593
                    .cpu_rst_i(cpu1_rst),
594
                    .cpu_clk_o(cpu1_clk),
595
                    .cpu_addr_i(cpu1_addr),
596
                    .cpu_data_o(cpu1_data_c),
597
                    .cpu_data_i(cpu1_data_d),
598
                    .cpu_bp_o(cpu1_bp),
599
                    .cpu_stall_i(cpu1_stall),
600
                    .cpu_stb_i(cpu1_stb),
601
                    .cpu_we_i(cpu1_we),
602
                    .cpu_ack_o(cpu1_ack),
603
                    .cpu_rst_o(cpu1_rst)
604
);
605
`endif
606
 
607
///////////////////////////////////////////////////////////////////////////
608
// Higher-level chain manipulation functions
609
 
610
// calculate the CRC, up to 32 bits at a time
611
task compute_crc;
612
    input [31:0] crc_in;
613
    input [31:0] data_in;
614
    input [5:0] length_bits;
615
    output [31:0] crc_out;
616
    integer i;
617
    reg [31:0] d;
618
    reg [31:0] c;
619
    begin
620
        crc_out = crc_in;
621
        for(i = 0; i < length_bits; i = i+1) begin
622
           d = (data_in[i]) ? 32'hffffffff : 32'h0;
623
           c = (crc_out[0]) ? 32'hffffffff : 32'h0;
624
           //crc_out = {crc_out[30:0], 1'b0};  // original
625
           crc_out = crc_out >> 1;
626
           crc_out = crc_out ^ ((d ^ c) & `DBG_CRC_POLY);
627
           //$display("CRC Itr %d, inbit = %d, crc = 0x%x", i, data_in[i], crc_out);
628
        end
629
    end
630
endtask
631
 
632
task check_idcode;
633
reg [63:0] readdata;
634
reg[31:0] idcode;
635
begin
636
    set_ir(`IDCODE);
637
 
638
    // Read the IDCODE in the DR
639
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
640
    write_bit(3'h0);           // capture_ir
641
    write_bit(3'h0);           // shift_ir
642
    jtag_read_write_stream(64'h0, 8'd32, 1, readdata);  // write data, exit_1
643
    write_bit(`JTAG_TMS_bit);  // update_ir
644
    write_bit(3'h0);           // idle
645
    idcode = readdata[31:0];
646
    $display("Got TAP IDCODE 0x%x, expected 0x%x", idcode, `IDCODE_VALUE);
647
end
648
endtask;
649
 
650
task select_debug_module;
651
input [1:0] moduleid;
652
reg validid;
653
begin
654
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
655
    write_bit(3'h0);           // capture_ir
656
    write_bit(3'h0);           // shift_ir
657
    jtag_write_stream({1'b1,moduleid}, 8'h3, 1);  // write data, exit_1
658
    write_bit(`JTAG_TMS_bit);  // update_dr
659
    write_bit(3'h0);           // idle
660
 
661
    $display("Selecting module (%0x)", moduleid);
662
 
663
    // Read back the status to make sure a valid chain is selected
664
    /* Pointless, the newly selected module would respond instead...
665
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
666
    write_bit(3'h0);           // capture_ir
667
    write_bit(3'h0);           // shift_ir
668
    read_write_bit(`JTAG_TMS_bit, validid);  // get data, exit_1
669
    write_bit(`JTAG_TMS_bit);  // update_dr
670
    write_bit(3'h0);           // idle
671
 
672
    if(validid)   $display("Selected valid module (%0x)", moduleid);
673
    else          $display("Failed to select module (%0x)", moduleid);
674
    */
675
end
676
endtask
677
 
678
 
679
task send_module_burst_command;
680
input [3:0] opcode;
681
input [31:0] address;
682
input [15:0] burstlength;
683
reg [63:0] streamdata;
684
begin
685
    streamdata = {11'h0,1'b0,opcode,address,burstlength};
686
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
687
    write_bit(3'h0);           // capture_ir
688
    write_bit(3'h0);           // shift_ir
689
    jtag_write_stream(streamdata, 8'd53, 1);  // write data, exit_1
690
    write_bit(`JTAG_TMS_bit);  // update_dr
691
    write_bit(3'h0);           // idle
692
end
693
endtask
694
 
695
task select_module_internal_register;  // Really just a read, with discarded data
696
    input [31:0] regidx;
697
    input [7:0] len;  // the length of the register index data, we assume not more than 32
698
    reg[63:0] streamdata;
699
begin
700
    streamdata = 64'h0;
701
    streamdata = streamdata | regidx;
702
    streamdata = streamdata | (`DBG_WB_CMD_IREG_SEL << len);
703
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
704
    write_bit(3'h0);           // capture_ir
705
    write_bit(3'h0);           // shift_ir
706
    jtag_write_stream(streamdata, (len+5), 1);  // write data, exit_1
707
    write_bit(`JTAG_TMS_bit);  // update_dr
708
    write_bit(3'h0);           // idle
709
end
710
endtask
711
 
712
 
713
task read_module_internal_register;  // We assume the register is already selected
714
    //input [31:0] regidx;
715
    input [7:0] len;  // the length of the data desired, we assume a max of 64 bits
716
    output [63:0] instream;
717
    reg [63:0] bitmask;
718
begin
719
    instream = 64'h0;
720
    // We shift out all 0's, which is a NOP to the debug unit
721
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
722
    write_bit(3'h0);           // capture_ir
723
    write_bit(3'h0);           // shift_ir
724
    // Shift at least 5 bits, as this is the min, for a valid NOP
725
    jtag_read_write_stream(64'h0, len+4,1,instream);  // exit_1
726
    write_bit(`JTAG_TMS_bit);  // update_dr
727
    write_bit(3'h0);           // idle
728
    bitmask = 64'hffffffffffffffff;
729
    bitmask = bitmask << len;
730
    bitmask = ~bitmask;
731
    instream = instream & bitmask;  // Cut off any unwanted excess bits
732
end
733
endtask
734
 
735
task write_module_internal_register;
736
    input [31:0] regidx; // the length of the register index data
737
    input [7:0] idxlen;
738
    input [63:0] writedata;
739
    input [7:0] datalen;  // the length of the data to write.  We assume the two length combined are 59 or less.
740
    reg[63:0] streamdata;
741
begin
742
    streamdata = 64'h0;  // This will 0 the toplevel/module select bit
743
    streamdata = streamdata | writedata;
744
    streamdata = streamdata | (regidx << datalen);
745
    streamdata = streamdata | (`DBG_WB_CMD_IREG_WR << (idxlen+datalen));
746
 
747
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
748
    write_bit(3'h0);           // capture_ir
749
    write_bit(3'h0);           // shift_ir
750
    jtag_write_stream(streamdata, (idxlen+datalen+5), 1);  // write data, exit_1
751
    write_bit(`JTAG_TMS_bit);  // update_dr
752
    write_bit(3'h0);           // idle
753
end
754
endtask
755
 
756
// This includes the sending of the burst command
757
task do_module_burst_read;
758
input [5:0] word_size_bytes;
759
input [15:0] word_count;
760
input [31:0] start_address;
761
reg [3:0] opcode;
762
reg status;
763
reg [63:0] instream;
764
integer i;
765
integer j;
766
reg [31:0] crc_calc_i;
767
reg [31:0] crc_calc_o;  // temp signal...
768
reg [31:0] crc_read;
769
reg [5:0] word_size_bits;
770
begin
771
    $display("Doing burst read, word size %d, word count %d, start address 0x%x", word_size_bytes, word_count, start_address);
772
    instream = 64'h0;
773
    word_size_bits = word_size_bytes << 3;
774
    crc_calc_i = 32'hffffffff;
775
 
776
    // Send the command
777
    case (word_size_bytes)
778
       3'h1: opcode = `DBG_WB_CMD_BREAD8;
779
       3'h2: opcode = `DBG_WB_CMD_BREAD16;
780
       3'h4: opcode = `DBG_WB_CMD_BREAD32;
781
       default:
782
          begin
783
           $display("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
784
           opcode = `DBG_WB_CMD_BREAD32;
785
          end
786
   endcase
787
 
788
   send_module_burst_command(opcode,start_address, word_count);  // returns to state idle
789
 
790
   // Get us back to shift_dr mode to read a burst
791
   write_bit(`JTAG_TMS_bit);  // select_dr_scan
792
   write_bit(3'h0);           // capture_ir
793
   write_bit(3'h0);           // shift_ir
794
 
795
   // Now, repeat...
796
   for(i = 0; i < word_count; i=i+1) begin
797
      // Get 1 status bit, then word_size_bytes*8 bits
798
      status = 1'b0;
799
      j = 0;
800
      while(!status) begin
801
         read_write_bit(3'h0, status);
802
         j = j + 1;
803
      end
804
 
805
      if(j > 1) begin
806
         $display("Took %0d tries before good status bit during burst read", j);
807
      end
808
 
809
     jtag_read_write_stream(64'h0, {2'h0,(word_size_bytes<<3)},0,instream);
810
     //$display("Read 0x%0x", instream[31:0]);
811
     compute_crc(crc_calc_i, instream[31:0], word_size_bits, crc_calc_o);
812
     crc_calc_i = crc_calc_o;
813
     if(word_size_bytes == 1) input_data8[i] = instream[7:0];
814
     else if(word_size_bytes == 2) input_data16[i] = instream[15:0];
815
     else input_data32[i] = instream[31:0];
816
   end
817
 
818
   // Read the data CRC from the debug module.
819
   jtag_read_write_stream(64'h0, 6'd32, 1, crc_read);
820
   if(crc_calc_o != crc_read) $display("CRC ERROR! Computed 0x%x, read CRC 0x%x", crc_calc_o, crc_read);
821
   else $display("CRC OK!");
822
 
823
   // Finally, shift out 5 0's, to make the next command a NOP
824
   // Not necessary, debug unit won't latch a new opcode at the end of a burst
825
   //jtag_write_stream(64'h0, 8'h5, 1);
826
   write_bit(`JTAG_TMS_bit);  // update_ir
827
   write_bit(3'h0);           // idle
828
end
829
endtask
830
 
831
 
832
task do_module_burst_write;
833
input [5:0] word_size_bytes;
834
input [15:0] word_count;
835
input [31:0] start_address;
836
reg [3:0] opcode;
837
reg status;
838
reg [63:0] dataword;
839
integer i;
840
integer j;
841
reg [31:0] crc_calc_i;
842
reg [31:0] crc_calc_o;
843
reg crc_match;
844
reg [5:0] word_size_bits;
845
begin
846
    $display("Doing burst write, word size %d, word count %d, start address 0x%x", word_size_bytes, word_count, start_address);
847
    word_size_bits = word_size_bytes << 3;
848
    crc_calc_i = 32'hffffffff;
849
 
850
    // Send the command
851
    case (word_size_bytes)
852
       3'h1: opcode = `DBG_WB_CMD_BWRITE8;
853
       3'h2: opcode = `DBG_WB_CMD_BWRITE16;
854
       3'h4: opcode = `DBG_WB_CMD_BWRITE32;
855
       default:
856
          begin
857
           $display("Tried burst write with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
858
           opcode = `DBG_WB_CMD_BWRITE32;
859
          end
860
   endcase
861
 
862
   send_module_burst_command(opcode, start_address, word_count);  // returns to state idle
863
 
864
   // Get us back to shift_dr mode to write a burst
865
   write_bit(`JTAG_TMS_bit);  // select_dr_scan
866
   write_bit(3'h0);           // capture_ir
867
   write_bit(3'h0);           // shift_ir
868
 
869
 
870
   // Write a start bit (a 1) so it knows when to start counting
871
   write_bit(`JTAG_TDO_bit);
872
 
873
   // Now, repeat...
874
   for(i = 0; i < word_count; i=i+1) begin
875
      // Write word_size_bytes*8 bits, then get 1 status bit
876
      if(word_size_bytes == 4)      dataword = {32'h0, static_data32[i]};
877
      else if(word_size_bytes == 2) dataword = {48'h0, static_data16[i]};
878
      else                          dataword = {56'h0, static_data8[i]};
879
 
880
 
881
      jtag_write_stream(dataword, {2'h0,(word_size_bytes<<3)},0);
882
      compute_crc(crc_calc_i, dataword[31:0], word_size_bits, crc_calc_o);
883
      crc_calc_i = crc_calc_o;
884
 
885
      // Check if WB bus is ready
886
      // *** THIS WILL NOT WORK IF THERE IS MORE THAN 1 DEVICE IN THE JTAG CHAIN!!!
887
      status = 1'b0;
888
      read_write_bit(3'h0, status);
889
 
890
      if(!status) begin
891
         $display("Bad status bit during burst write, index %d", i);
892
      end
893
 
894
 
895
     //$display("Wrote 0x%0x", dataword);
896
   end
897
 
898
   // Send the CRC we computed
899
   jtag_write_stream(crc_calc_o, 6'd32,0);
900
 
901
   // Read the 'CRC match' bit, and go to exit1_dr
902
   read_write_bit(`JTAG_TMS_bit, crc_match);
903
   if(!crc_match) $display("CRC ERROR! match bit after write is %d (computed CRC 0x%x)", crc_match, crc_calc_o);
904
   else $display("CRC OK!");
905
 
906
   // Finally, shift out 5 0's, to make the next command a NOP
907
   // Not necessary, module will not latch new opcode during burst
908
   //jtag_write_stream(64'h0, 8'h5, 1);
909
   write_bit(`JTAG_TMS_bit);  // update_ir
910
   write_bit(3'h0);           // idle
911
end
912
 
913
endtask
914
 
915
 
916
// Puts a value in the TAP IR, assuming we start in IDLE state.
917
// Returns to IDLE state when finished
918
task set_ir;
919
input [3:0] irval;
920
begin
921
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
922
    write_bit(`JTAG_TMS_bit);  // select_ir_scan
923
    write_bit(3'h0);           // capture_ir
924
    write_bit(3'h0);           // shift_ir
925
    jtag_write_stream({60'h0,irval}, 8'h4, 1);  // write data, exit_1
926
    write_bit(`JTAG_TMS_bit);  // update_ir
927
    write_bit(3'h0);           // idle
928
end
929
endtask
930
 
931
// Resets the TAP and puts it into idle mode
932
task reset_jtag;
933
integer i;
934
begin
935
   for(i = 0; i < 8; i=i+1) begin
936
      write_bit(`JTAG_TMS_bit);  // 5 TMS should put us in test_logic_reset mode
937
   end
938
   write_bit(3'h0);              // idle
939
end
940
endtask
941
 
942
 
943
////////////////////////////////////////////////////////////////////////////
944
// Tasks to write or read-write a string of data
945
 
946
task jtag_write_stream;
947
input [63:0] stream;
948
input [7:0] len;
949
input set_last_bit;
950
integer i;
951
integer databit;
952
reg [2:0] bits;
953
begin
954
    for(i = 0; i < (len-1); i=i+1) begin
955
       databit = (stream >> i) & 1'h1;
956
       bits = databit << `JTAG_TDO;
957
       write_bit(bits);
958
   end
959
 
960
   databit = (stream >> i) & 1'h1;
961
   bits = databit << `JTAG_TDO;
962
   if(set_last_bit) bits = (bits | `JTAG_TMS_bit);
963
   write_bit(bits);
964
 
965
end
966
endtask
967
 
968
 
969
task jtag_read_write_stream;
970
input [63:0] stream;
971
input [7:0] len;
972
input set_last_bit;
973
output [63:0] instream;
974
integer i;
975
integer databit;
976
reg [2:0] bits;
977
reg inbit;
978
begin
979
    instream = 64'h0;
980
    for(i = 0; i < (len-1); i=i+1) begin
981
       databit = (stream >> i) & 1'h1;
982
       bits = databit << `JTAG_TDO;
983
       read_write_bit(bits, inbit);
984
       instream = (instream | (inbit << i));
985
   end
986
 
987
   databit = (stream >> i) & 1'h1;
988
   bits = databit << `JTAG_TDO;
989
   if(set_last_bit) bits = (bits | `JTAG_TMS_bit);
990
   read_write_bit(bits, inbit);
991
   instream = (instream | (inbit << (len-1)));
992
end
993
endtask
994
 
995
/////////////////////////////////////////////////////////////////////////
996
// Tasks which write or readwrite a single bit (including clocking)
997
 
998
task write_bit;
999
   input [2:0] bitvals;
1000
   begin
1001
 
1002
   // Set data
1003
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
1004
   `wait_jtag_period;
1005
 
1006
   // Raise clock
1007
   jtag_out(bitvals | `JTAG_TCK_bit);
1008
   `wait_jtag_period;
1009
 
1010
   // drop clock (making output available in the SHIFT_xR states)
1011
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
1012
   `wait_jtag_period;
1013
   end
1014
endtask
1015
 
1016
task read_write_bit;
1017
   input [2:0] bitvals;
1018
   output l_tdi_val;
1019
   begin
1020
 
1021
   // read bit state
1022
   l_tdi_val <= jtag_tdi_i;
1023
 
1024
   // Set data
1025
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
1026
   `wait_jtag_period;
1027
 
1028
   // Raise clock
1029
   jtag_out(bitvals | `JTAG_TCK_bit);
1030
   `wait_jtag_period;
1031
 
1032
   // drop clock (making output available in the SHIFT_xR states)
1033
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
1034
   `wait_jtag_period;
1035
   end
1036
endtask
1037
 
1038
/////////////////////////////////////////////////////////////////
1039
// Basic functions to set the state of the JTAG TAP I/F bits
1040
 
1041
task jtag_out;
1042
  input   [2:0]   bitvals;
1043
  begin
1044
 
1045
   jtag_tck_o <= bitvals[`JTAG_TCK];
1046
   jtag_tms_o <= bitvals[`JTAG_TMS];
1047
   jtag_tdo_o <= bitvals[`JTAG_TDO];
1048
   end
1049
endtask
1050
 
1051
 
1052
task jtag_inout;
1053
  input   [2:0]   bitvals;
1054
  output l_tdi_val;
1055
  begin
1056
 
1057
   jtag_tck_o <= bitvals[`JTAG_TCK];
1058
   jtag_tms_o <= bitvals[`JTAG_TMS];
1059
   jtag_tdo_o <= bitvals[`JTAG_TDO];
1060
 
1061
   l_tdi_val <= jtag_tdi_i;
1062
   end
1063
endtask
1064
 
1065
endmodule

powered by: WebSVN 2.1.0

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