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

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [tags/] [ADS_RELEASE_1_0_0/] [Hardware/] [adv_dbg_if/] [bench/] [full_system/] [adv_dbg_tb.v] - Blame information for rev 67

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@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: adv_dbg_tb.v,v $
43
// Revision 1.4  2009/05/17 20:54:55  Nathan
44
// Changed email address to opencores.org
45
//
46
// Revision 1.3  2008/07/11 08:18:47  Nathan
47
// Added a bit to the CPU test.  Added the hack that allows the driver to work with a Xilinx BSCAN device.
48
//
49
 
50
 
51
`include "tap_defines.v"
52
`include "dbg_defines.v"
53
`include "dbg_wb_defines.v"
54
 
55
 
56
// Polynomial for the CRC calculation
57
// Yes, it's backwards.  Yes, this is on purpose.
58
// To decrease logic + routing, we want to shift the CRC calculation
59
// in the same direction we use to shift the data out, LSB first.
60
`define DBG_CRC_POLY 32'hedb88320
61
 
62
// These are indicies into an array which hold values for the JTAG outputs
63
`define JTAG_TMS 0
64
`define JTAG_TCK 1
65
`define JTAG_TDO 2
66
 
67
`define JTAG_TMS_bit 3'h1
68
`define JTAG_TCK_bit 3'h2
69
`define JTAG_TDO_bit 3'h4
70
 
71
`define wait_jtag_period #50
72
 
73
 
74
module adv_debug_tb (
75
 
76
jtag_tck_o,
77
jtag_tms_o,
78
jtag_tdo_o,
79
jtag_tdi_i,
80
 
81
wb_clk_o,
82
sys_rstn_o
83
 
84
 
85
);
86
 
87
output jtag_tck_o;
88
output jtag_tms_o;
89
output jtag_tdo_o;
90
input jtag_tdi_i;
91
output wb_clk_o;
92
output sys_rstn_o;
93
 
94
// Connections to the JTAG TAP
95
reg jtag_tck_o;
96
reg jtag_tms_o;
97
reg jtag_tdo_o;
98
wire jtag_tdi_i;
99
 
100
reg wb_clk_o;
101
reg sys_rst_o;
102
reg sys_rstn_o;
103
 
104
reg test_enabled;
105
 
106
// Data which will be written to the WB interface
107
reg [31:0] static_data32 [0:15];
108
reg [15:0] static_data16 [0:15];
109
reg [7:0] static_data8 [0:15];
110
 
111
// Arrays to hold data read back from the WB interface, for comparison
112
reg [31:0] input_data32 [0:15];
113
reg [15:0] input_data16 [0:15];
114
reg [7:0]  input_data8 [0:15];
115
 
116
reg [32:0] err_data;  // holds the contents of the error register from the various modules
117
 
118
reg failed;
119
integer i;
120
 
121
initial
122
begin
123
   jtag_tck_o = 1'b0;
124
   jtag_tms_o = 1'b0;
125
   jtag_tdo_o = 1'b0;
126
end
127
 
128
// Provide the wishbone / CPU / system clock
129
initial
130
begin
131
  wb_clk_o = 1'b0;
132
  forever #5 wb_clk_o = ~wb_clk_o;
133
end
134
 
135
initial
136
begin
137
   sys_rstn_o = 1'b1;
138
   #200 sys_rstn_o = 1'b0;
139
   #5000 sys_rstn_o = 1'b1;
140
end
141
 
142
 
143
// Start the test (and reset the wishbone)
144
initial
145
begin
146
  test_enabled = 1'b0;
147
 
148
   // Init the memory
149
  initialize_memory(32'h0,32'h16);
150
 
151
  #5 test_enabled<= 1'b1;
152
end
153
 
154
// This is the main test procedure
155
always @ (posedge test_enabled)
156
begin
157
 
158
  $display("Starting advanced debug test");
159
 
160
  reset_jtag;
161
  #6000;
162
  check_idcode;
163
  #1000;
164
 
165
  // Select the debug module in the IR
166
  set_ir(`DEBUG);
167
  #1000;
168
 
169
 
170
  `ifdef DBG_CPU0_SUPPORTED
171
  // STALL the CPU, so it won't interfere with WB tests
172
  // Select the CPU0 unit in the debug module
173
  #1000;
174
  $display("Selecting CPU0 module at time %t", $time);
175
  select_debug_module(`DBG_TOP_CPU0_DEBUG_MODULE);
176
 
177
 
178
   //  Set the stall bit...holding the CPU in reset prevents WB access (?)
179
   $display("Setting reset and stall bits at time %t", $time);
180
    write_module_internal_register(32'h0, 8'h1, 32'h1, 8'h2);  // idx, idxlen, data, datalen
181
   #1000;
182
   `endif
183
 
184
 
185
  ///////////////////////////////////////////////////////////////////
186
  // Test the Wishbone unit
187
  ////////////////////////////////////////////////////////////////////
188
 
189
`ifdef DBG_WISHBONE_SUPPORTED
190
  // Select the WB unit in the debug module
191
  #1000;
192
  $display("Selecting Wishbone module at time %t", $time);
193
  select_debug_module(`DBG_TOP_WISHBONE_DEBUG_MODULE);
194
 
195
   // Reset the error bit    
196
    write_module_internal_register(32'h0, 8'h1, 32'h1, 8'h1);  // idx, idxlen, data, datalen
197
   #1000;
198
 
199
  /////////////////////////////////
200
  // Test 8-bit WB access
201
  failed = 0;
202
  $display("Testing WB 8-bit burst write at time %t: resetting ", $time);
203
  do_module_burst_write(3'h1, 16'd16, 32'h87);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
204
  #1000;
205
  $display("Testing WB 8-bit burst read at time %t", $time);
206
  do_module_burst_read(3'h1, 16'd16, 32'h87);
207
  #1000;
208
   for(i = 0; i < 16; i = i+1) begin
209
     if(static_data8[i] != input_data8[i]) begin
210
        failed = 1;
211
        $display("32-bit data mismatch at index %d, wrote 0x%x, read 0x%x", i, static_data8[i], input_data8[i]);
212
    end
213
  end
214
  if(!failed) $display("8-bit read/write OK!");
215
 
216
 
217
  /////////////////////////////////
218
  // Test 16-bit WB access
219
  failed = 0;
220
  $display("Testing WB 16-bit burst write at time %t", $time);
221
  do_module_burst_write(3'h2, 16'd16, 32'h22);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
222
  #1000;
223
  $display("Testing WB 16-bit burst read at time %t", $time);
224
  do_module_burst_read(3'h2, 16'd16, 32'h22);
225
  #1000;
226
   for(i = 0; i < 16; i = i+1) begin
227
     if(static_data16[i] != input_data16[i]) begin
228
        failed = 1;
229
        $display("16-bit data mismatch at index %d, wrote 0x%x, read 0x%x", i, static_data16[i], input_data16[i]);
230
    end
231
  end
232
  if(!failed) $display("16-bit read/write OK!");
233
 
234
 
235
  ////////////////////////////////////
236
  // Test 32-bit WB access
237
  failed = 0;
238
  $display("Testing WB 32-bit burst write at time %t", $time);
239
  do_module_burst_write(3'h4, 16'd16, 32'h100);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
240
  #1000;
241
  $display("Testing WB 32-bit burst read at time %t", $time);
242
  do_module_burst_read(3'h4, 16'd16, 32'h100);
243
  #1000;
244
   for(i = 0; i < 16; i = i+1) begin
245
     if(static_data32[i] != input_data32[i]) begin
246
        failed = 1;
247
        $display("32-bit data mismatch at index %d, wrote 0x%x, read 0x%x", i, static_data32[i], input_data32[i]);
248
    end
249
  end
250
  if(!failed) $display("32-bit read/write OK!");
251
 
252
 
253
  ////////////////////////////////
254
  // Test error register
255
  err_data = 33'h0;
256
  // Select and reset the error register
257
  write_module_internal_register(`DBG_WB_INTREG_ERROR, `DBG_WB_REGSELECT_SIZE, 64'h1, 8'h1); // regidx,idxlen,writedata, datalen;
258
  //i_wb.cycle_response(`ERR_RESPONSE, 2, 0);  // response type, wait cycles, retry_cycles
259
  do_module_burst_write(3'h4, 16'd4, 32'hdeaddead);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
260
  read_module_internal_register(8'd33, err_data);  // get the error register
261
  $display("Error bit is %d, error address is %x", err_data[0], err_data>>1);
262
 
263
`endif  // WB module supported
264
 
265
 
266
  ///////////////////////////////////////////////////////////////////
267
  // Test CPU0 unit
268
  ////////////////////////////////////////////////////////////////////
269
`ifdef DBG_CPU0_SUPPORTED
270
  // Select the CPU0 unit in the debug module
271
  #1000;
272
  $display("Selecting CPU0 module at time %t", $time);
273
  select_debug_module(`DBG_TOP_CPU0_DEBUG_MODULE);
274
 
275
 
276
   //  Set the stall bit (clear the reset bit)
277
   $display("Setting reset and stall bits at time %t", $time);
278
    write_module_internal_register(32'h0, 8'h1, 32'h1, 8'h2);  // idx, idxlen, data, datalen
279
   #1000;
280
 
281
   // Make sure CPU stalled
282
   $display("Testing reset and stall bits at time %t", $time);
283
   read_module_internal_register(8'd2, err_data);  // We assume the register is already selected
284
   $display("Reset and stall bits are %x", err_data);
285
   #1000;
286
 
287
 
288
// Write some opcodes into the memory
289
select_debug_module(`DBG_TOP_WISHBONE_DEBUG_MODULE);
290
 
291
static_data32[0] = 32'hE0000005;/* l.xor   r0,r0,r0   */
292
static_data32[1] = 32'h9C200000; /* l.addi  r1,r0,0x0  */
293
static_data32[2] = 32'h18400000;/* l.movhi r2,0x4000  */
294
static_data32[3] = 32'hA8420030;/* l.ori   r2,r2,0x30 */
295
static_data32[4] = 32'h9C210001;/* l.addi  r1,r1,1    */
296
static_data32[5] = 32'h9C210001; /* l.addi  r1,r1,1    */
297
static_data32[6] = 32'hD4020800;/* l.sw    0(r2),r1   */
298
static_data32[7] = 32'h9C210001;/* l.addi  r1,r1,1    */
299
static_data32[8] = 32'h84620000;/* l.lwz   r3,0(r2)   */
300
static_data32[9] = 32'h03FFFFFB;/* l.j     loop2      */
301
static_data32[10] = 32'hE0211800;/* l.add   r1,r1,r3   */
302
 
303
do_module_burst_write(3'h4, 16'd11, 32'h0);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
304
 
305
   #1000;
306
select_debug_module(`DBG_TOP_CPU0_DEBUG_MODULE);
307
 
308
#1000;
309
$display("Enabling CPU exceptions at time %t", $time);
310
static_data32[0] = 32'h1;  // enable exceptions
311
do_module_burst_write(3'h4, 16'd1, 32'd17);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
312
 
313
#1000;
314
$display("Set \'trap causes stall\' at time %t", $time);
315
static_data32[0] = 32'h00002000;  // Trap causes stall
316
do_module_burst_write(3'h4, 16'd1, (6 << 11)+20);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
317
 
318
#1000;
319
$display("Set PC at time %t", $time);
320
static_data32[0] = 32'h0;  // Set PC
321
do_module_burst_write(3'h4, 16'd1, 32'd16);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
322
 
323
#1000;
324
$display("Set step bit at time %t", $time);
325
static_data32[0] = (1 << 22);  // set step bit
326
do_module_burst_write(3'h4, 16'd1, (6<<11) + 16);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
327
 
328
// Unstall x11
329
for(i = 0; i < 11; i = i + 1)
330
begin
331
   #1000;
332
   $display("Unstall (%d/11) at time %t", i, $time);
333
   write_module_internal_register(32'h0, 8'h1, 32'h0, 8'h2);  // idx, idxlen, data, datalen
334
end
335
 
336
#1000;
337
#1000;
338
#1000;
339
 
340
  $display("Getting NPC at time %t", $time);
341
  do_module_burst_read(3'h4, 16'd1, 32'd16);
342
 
343
  $display("NPC = %x, expected 0x00000010", input_data32[0]);
344
 
345
    $display("Getting PPC at time %t", $time);
346
  do_module_burst_read(3'h4, 16'd1, 32'd18);
347
  $display("PPC = %x, expected 0x00000028", input_data32[0]);
348
 
349
  #1000;
350
  $display("Getting R1 at time %t", $time);
351
  do_module_burst_read(3'h4, 16'd1, 32'h401);  // Word size, count, addr; save old instr
352
  $display("R1 = %d, expected 5", input_data32[0]);
353
 
354
  #1000;
355
$display("Un-set step bit at %t", $time);
356
static_data32[0] = 32'h0;  // Trap causes stall
357
do_module_burst_write(3'h4, 16'd1, (6 << 11)+16);
358
 
359
// Put a trap instr at 0x20
360
#1000;
361
$display("Select WB at %t", $time);
362
select_debug_module(`DBG_TOP_WISHBONE_DEBUG_MODULE);
363
#1000;
364
$display("Save old instr at %t", $time);
365
do_module_burst_read(3'h4, 16'd1, 32'h20);  // Save old instr
366
#1000;
367
$display("Put trap instr at %t", $time);
368
static_data32[0] = 32'h21000001;  /* l.trap   */
369
do_module_burst_write(3'h4, 16'd1, 32'h20); // put new instr
370
#1000;
371
$display("Select CPU0 at %t", $time);
372
select_debug_module(`DBG_TOP_CPU0_DEBUG_MODULE);
373
#1000;
374
$display("Set PC to 0x24 at %t", $time);
375
static_data32[0] = 32'h24;
376
do_module_burst_write(3'h4, 16'd1, 32'd16); // Set PC to 0x24
377
#1000;
378
$display("Unstall  at time %t", $time);
379
write_module_internal_register(32'h0, 8'h1, 32'h0, 8'h2);  // idx, idxlen, data, datalen
380
 
381
// We assume it stalls again here...
382
#1000;
383
 
384
err_data = 1;
385
while(err_data != 0)
386
begin
387
    $display("Testing for stall at %t", $time);
388
    read_module_internal_register(8'd2, err_data);  // We assume the register is already selected
389
    #1000;
390
end
391
 
392
 
393
// *** The software self-test does 2 separate reads here...
394
  $display("Getting NPC at time %t", $time);
395
  do_module_burst_read(3'h4, 16'd3, 32'd16);
396
 
397
  $display("NPC = %x, expected 0x00000024", input_data32[0]);
398
  $display("PPC = %x, expected 0x00000020", input_data32[2]);
399
 
400
 
401
 
402
`endif
403
 
404
 
405
end
406
 
407
task initialize_memory;
408
  input [31:0] start_addr;
409
  input [31:0] length;
410
  integer i;
411
  reg [31:0] addr;
412
  begin
413
 
414
    for (i=0; i<length; i=i+1)
415
      begin
416
        static_data32[i] <= {i[7:0], i[7:0]+2'd1, i[7:0]+2'd2, i[7:0]+2'd3};
417
        static_data16[i] <= {i[7:0], i[7:0]+ 2'd1};
418
        static_data8[i] <= i[7:0];
419
      end
420
  end
421
endtask
422
 
423
 
424
 
425
///////////////////////////////////////////////////////////////////////////
426
// Higher-level chain manipulation functions
427
 
428
// calculate the CRC, up to 32 bits at a time
429
task compute_crc;
430
    input [31:0] crc_in;
431
    input [31:0] data_in;
432
    input [5:0] length_bits;
433
    output [31:0] crc_out;
434
    integer i;
435
    reg [31:0] d;
436
    reg [31:0] c;
437
    begin
438
        crc_out = crc_in;
439
        for(i = 0; i < length_bits; i = i+1) begin
440
           d = (data_in[i]) ? 32'hffffffff : 32'h0;
441
           c = (crc_out[0]) ? 32'hffffffff : 32'h0;
442
           //crc_out = {crc_out[30:0], 1'b0};  // original
443
           crc_out = crc_out >> 1;
444
           crc_out = crc_out ^ ((d ^ c) & `DBG_CRC_POLY);
445
           //$display("CRC Itr %d, inbit = %d, crc = 0x%x", i, data_in[i], crc_out);
446
        end
447
    end
448
endtask
449
 
450
task check_idcode;
451
reg [63:0] readdata;
452
reg[31:0] idcode;
453
begin
454
    set_ir(`IDCODE);
455
 
456
    // Read the IDCODE in the DR
457
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
458
    write_bit(3'h0);           // capture_ir
459
    write_bit(3'h0);           // shift_ir
460
    jtag_read_write_stream(64'h0, 8'd32, 1, readdata);  // write data, exit_1
461
    write_bit(`JTAG_TMS_bit);  // update_ir
462
    write_bit(3'h0);           // idle
463
    idcode = readdata[31:0];
464
    $display("Got TAP IDCODE 0x%x, expected 0x%x", idcode, `IDCODE_VALUE);
465
end
466
endtask;
467
 
468
task select_debug_module;
469
input [1:0] moduleid;
470
reg validid;
471
begin
472
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
473
    write_bit(3'h0);           // capture_ir
474
    write_bit(3'h0);           // shift_ir
475
    jtag_write_stream({1'b1,moduleid}, 8'h3, 1);  // write data, exit_1
476
    write_bit(`JTAG_TMS_bit);  // update_dr
477
    write_bit(3'h0);           // idle
478
 
479
    $display("Selecting module (%0x)", moduleid);
480
 
481
    // Read back the status to make sure a valid chain is selected
482
    /* Pointless, the newly selected module would respond instead...
483
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
484
    write_bit(3'h0);           // capture_ir
485
    write_bit(3'h0);           // shift_ir
486
    read_write_bit(`JTAG_TMS_bit, validid);  // get data, exit_1
487
    write_bit(`JTAG_TMS_bit);  // update_dr
488
    write_bit(3'h0);           // idle
489
 
490
    if(validid)   $display("Selected valid module (%0x)", moduleid);
491
    else          $display("Failed to select module (%0x)", moduleid);
492
    */
493
end
494
endtask
495
 
496
 
497
task send_module_burst_command;
498
input [3:0] opcode;
499
input [31:0] address;
500
input [15:0] burstlength;
501
reg [63:0] streamdata;
502
begin
503
    streamdata = {11'h0,1'b0,opcode,address,burstlength};
504
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
505
    write_bit(3'h0);           // capture_ir
506
    write_bit(3'h0);           // shift_ir
507
    jtag_write_stream(streamdata, 8'd53, 1);  // write data, exit_1
508
    write_bit(`JTAG_TMS_bit);  // update_dr
509
    write_bit(3'h0);           // idle
510
end
511
endtask
512
 
513
task select_module_internal_register;  // Really just a read, with discarded data
514
    input [31:0] regidx;
515
    input [7:0] len;  // the length of the register index data, we assume not more than 32
516
    reg[63:0] streamdata;
517
begin
518
    streamdata = 64'h0;
519
    streamdata = streamdata | regidx;
520
    streamdata = streamdata | (`DBG_WB_CMD_IREG_SEL << len);
521
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
522
    write_bit(3'h0);           // capture_ir
523
    write_bit(3'h0);           // shift_ir
524
    jtag_write_stream(streamdata, (len+5), 1);  // write data, exit_1
525
    write_bit(`JTAG_TMS_bit);  // update_dr
526
    write_bit(3'h0);           // idle
527
end
528
endtask
529
 
530
 
531
task read_module_internal_register;  // We assume the register is already selected
532
    //input [31:0] regidx;
533
    input [7:0] len;  // the length of the data desired, we assume a max of 64 bits
534
    output [63:0] instream;
535
    reg [63:0] bitmask;
536
begin
537
    instream = 64'h0;
538
    // We shift out all 0's, which is a NOP to the debug unit
539
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
540
    write_bit(3'h0);           // capture_ir
541
    write_bit(3'h0);           // shift_ir
542
    // Shift at least 5 bits, as this is the min, for a valid NOP
543
    jtag_read_write_stream(64'h0, len+4,1,instream);  // exit_1
544
    write_bit(`JTAG_TMS_bit);  // update_dr
545
    write_bit(3'h0);           // idle
546
    bitmask = 64'hffffffffffffffff;
547
    bitmask = bitmask << len;
548
    bitmask = ~bitmask;
549
    instream = instream & bitmask;  // Cut off any unwanted excess bits
550
end
551
endtask
552
 
553
task write_module_internal_register;
554
    input [31:0] regidx; // the length of the register index data
555
    input [7:0] idxlen;
556
    input [63:0] writedata;
557
    input [7:0] datalen;  // the length of the data to write.  We assume the two length combined are 59 or less.
558
    reg[63:0] streamdata;
559
begin
560
    streamdata = 64'h0;  // This will 0 the toplevel/module select bit
561
    streamdata = streamdata | writedata;
562
    streamdata = streamdata | (regidx << datalen);
563
    streamdata = streamdata | (`DBG_WB_CMD_IREG_WR << (idxlen+datalen));
564
 
565
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
566
    write_bit(3'h0);           // capture_ir
567
    write_bit(3'h0);           // shift_ir
568
    jtag_write_stream(streamdata, (idxlen+datalen+5), 1);  // write data, exit_1
569
    write_bit(`JTAG_TMS_bit);  // update_dr
570
    write_bit(3'h0);           // idle
571
end
572
endtask
573
 
574
// This includes the sending of the burst command
575
task do_module_burst_read;
576
input [5:0] word_size_bytes;
577
input [15:0] word_count;
578
input [31:0] start_address;
579
reg [3:0] opcode;
580
reg status;
581
reg [63:0] instream;
582
integer i;
583
integer j;
584
reg [31:0] crc_calc_i;
585
reg [31:0] crc_calc_o;  // temp signal...
586
reg [31:0] crc_read;
587
reg [5:0] word_size_bits;
588
begin
589
    $display("Doing burst read, word size %d, word count %d, start address 0x%x", word_size_bytes, word_count, start_address);
590
    instream = 64'h0;
591
    word_size_bits = word_size_bytes << 3;
592
    crc_calc_i = 32'hffffffff;
593
 
594
    // Send the command
595
    case (word_size_bytes)
596
       3'h1: opcode = `DBG_WB_CMD_BREAD8;
597
       3'h2: opcode = `DBG_WB_CMD_BREAD16;
598
       3'h4: opcode = `DBG_WB_CMD_BREAD32;
599
       default:
600
          begin
601
           $display("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
602
           opcode = `DBG_WB_CMD_BREAD32;
603
          end
604
   endcase
605
 
606
   send_module_burst_command(opcode,start_address, word_count);  // returns to state idle
607
 
608
   // This charming kludge provides ONE TCK, in case a xilinx BSCAN TAP is used,
609
   // because the FSM needs it between the read burst command and the actual
610
   // read burst.  Blech.
611
   #500;
612
   set_ir(`IDCODE);
613
   #500;
614
   set_ir(`DEBUG);
615
  #500;
616
 
617
   // Get us back to shift_dr mode to read a burst
618
   write_bit(`JTAG_TMS_bit);  // select_dr_scan
619
   write_bit(3'h0);           // capture_ir
620
   write_bit(3'h0);           // shift_ir
621
 
622
   // Now, repeat...
623
   for(i = 0; i < word_count; i=i+1) begin
624
      // Get 1 status bit, then word_size_bytes*8 bits
625
      status = 1'b0;
626
      j = 0;
627
      while(!status) begin
628
         read_write_bit(3'h0, status);
629
         j = j + 1;
630
      end
631
 
632
      if(j > 1) begin
633
         $display("Took %0d tries before good status bit during burst read", j);
634
      end
635
 
636
     jtag_read_write_stream(64'h0, {2'h0,(word_size_bytes<<3)},0,instream);
637
     //$display("Read 0x%0x", instream[31:0]);
638
     compute_crc(crc_calc_i, instream[31:0], word_size_bits, crc_calc_o);
639
     crc_calc_i = crc_calc_o;
640
     if(word_size_bytes == 1) input_data8[i] = instream[7:0];
641
     else if(word_size_bytes == 2) input_data16[i] = instream[15:0];
642
     else input_data32[i] = instream[31:0];
643
   end
644
 
645
   // Read the data CRC from the debug module.
646
   jtag_read_write_stream(64'h0, 6'd32, 1, crc_read);
647
   if(crc_calc_o != crc_read) $display("CRC ERROR! Computed 0x%x, read CRC 0x%x", crc_calc_o, crc_read);
648
   else $display("CRC OK!");
649
 
650
   // Finally, shift out 5 0's, to make the next command a NOP
651
   // Not necessary, debug unit won't latch a new opcode at the end of a burst
652
   //jtag_write_stream(64'h0, 8'h5, 1);
653
   write_bit(`JTAG_TMS_bit);  // update_ir
654
   write_bit(3'h0);           // idle
655
end
656
endtask
657
 
658
 
659
task do_module_burst_write;
660
input [5:0] word_size_bytes;
661
input [15:0] word_count;
662
input [31:0] start_address;
663
reg [3:0] opcode;
664
reg status;
665
reg [63:0] dataword;
666
integer i;
667
integer j;
668
reg [31:0] crc_calc_i;
669
reg [31:0] crc_calc_o;
670
reg crc_match;
671
reg [5:0] word_size_bits;
672
begin
673
    $display("Doing burst write, word size %d, word count %d, start address 0x%x", word_size_bytes, word_count, start_address);
674
    word_size_bits = word_size_bytes << 3;
675
    crc_calc_i = 32'hffffffff;
676
 
677
    // Send the command
678
    case (word_size_bytes)
679
       3'h1: opcode = `DBG_WB_CMD_BWRITE8;
680
       3'h2: opcode = `DBG_WB_CMD_BWRITE16;
681
       3'h4: opcode = `DBG_WB_CMD_BWRITE32;
682
       default:
683
          begin
684
           $display("Tried burst write with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
685
           opcode = `DBG_WB_CMD_BWRITE32;
686
          end
687
   endcase
688
 
689
   send_module_burst_command(opcode, start_address, word_count);  // returns to state idle
690
 
691
   // Get us back to shift_dr mode to write a burst
692
   write_bit(`JTAG_TMS_bit);  // select_dr_scan
693
   write_bit(3'h0);           // capture_ir
694
   write_bit(3'h0);           // shift_ir
695
 
696
 
697
   // Write a start bit (a 1) so it knows when to start counting
698
   write_bit(`JTAG_TDO_bit);
699
 
700
   // Now, repeat...
701
   for(i = 0; i < word_count; i=i+1) begin
702
      // Write word_size_bytes*8 bits, then get 1 status bit
703
      if(word_size_bytes == 4)      dataword = {32'h0, static_data32[i]};
704
      else if(word_size_bytes == 2) dataword = {48'h0, static_data16[i]};
705
      else                          dataword = {56'h0, static_data8[i]};
706
 
707
 
708
      jtag_write_stream(dataword, {2'h0,(word_size_bytes<<3)},0);
709
      compute_crc(crc_calc_i, dataword[31:0], word_size_bits, crc_calc_o);
710
      crc_calc_i = crc_calc_o;
711
 
712
      // Check if WB bus is ready
713
      // *** THIS WILL NOT WORK IF THERE IS MORE THAN 1 DEVICE IN THE JTAG CHAIN!!!
714
      status = 1'b0;
715
      read_write_bit(3'h0, status);
716
 
717
      if(!status) begin
718
         $display("Bad status bit during burst write, index %d", i);
719
      end
720
 
721
 
722
     //$display("Wrote 0x%0x", dataword);
723
   end
724
 
725
   // Send the CRC we computed
726
   jtag_write_stream(crc_calc_o, 6'd32,0);
727
 
728
   // Read the 'CRC match' bit, and go to exit1_dr
729
   read_write_bit(`JTAG_TMS_bit, crc_match);
730
   if(!crc_match) $display("CRC ERROR! match bit after write is %d (computed CRC 0x%x)", crc_match, crc_calc_o);
731
   else $display("CRC OK!");
732
 
733
   // Finally, shift out 5 0's, to make the next command a NOP
734
   // Not necessary, module will not latch new opcode during burst
735
   //jtag_write_stream(64'h0, 8'h5, 1);
736
   write_bit(`JTAG_TMS_bit);  // update_ir
737
   write_bit(3'h0);           // idle
738
end
739
 
740
endtask
741
 
742
 
743
// Puts a value in the TAP IR, assuming we start in IDLE state.
744
// Returns to IDLE state when finished
745
task set_ir;
746
input [3:0] irval;
747
begin
748
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
749
    write_bit(`JTAG_TMS_bit);  // select_ir_scan
750
    write_bit(3'h0);           // capture_ir
751
    write_bit(3'h0);           // shift_ir
752
    jtag_write_stream({60'h0,irval}, 8'h4, 1);  // write data, exit_1
753
    write_bit(`JTAG_TMS_bit);  // update_ir
754
    write_bit(3'h0);           // idle
755
end
756
endtask
757
 
758
// Resets the TAP and puts it into idle mode
759
task reset_jtag;
760
integer i;
761
begin
762
   for(i = 0; i < 8; i=i+1) begin
763
      write_bit(`JTAG_TMS_bit);  // 5 TMS should put us in test_logic_reset mode
764
   end
765
   write_bit(3'h0);              // idle
766
end
767
endtask
768
 
769
 
770
////////////////////////////////////////////////////////////////////////////
771
// Tasks to write or read-write a string of data
772
 
773
task jtag_write_stream;
774
input [63:0] stream;
775
input [7:0] len;
776
input set_last_bit;
777
integer i;
778
integer databit;
779
reg [2:0] bits;
780
begin
781
    for(i = 0; i < (len-1); i=i+1) begin
782
       databit = (stream >> i) & 1'h1;
783
       bits = databit << `JTAG_TDO;
784
       write_bit(bits);
785
   end
786
 
787
   databit = (stream >> i) & 1'h1;
788
   bits = databit << `JTAG_TDO;
789
   if(set_last_bit) bits = (bits | `JTAG_TMS_bit);
790
   write_bit(bits);
791
 
792
end
793
endtask
794
 
795
 
796
task jtag_read_write_stream;
797
input [63:0] stream;
798
input [7:0] len;
799
input set_last_bit;
800
output [63:0] instream;
801
integer i;
802
integer databit;
803
reg [2:0] bits;
804
reg inbit;
805
begin
806
    instream = 64'h0;
807
    for(i = 0; i < (len-1); i=i+1) begin
808
       databit = (stream >> i) & 1'h1;
809
       bits = databit << `JTAG_TDO;
810
       read_write_bit(bits, inbit);
811
       instream = (instream | (inbit << i));
812
   end
813
 
814
   databit = (stream >> i) & 1'h1;
815
   bits = databit << `JTAG_TDO;
816
   if(set_last_bit) bits = (bits | `JTAG_TMS_bit);
817
   read_write_bit(bits, inbit);
818
   instream = (instream | (inbit << (len-1)));
819
end
820
endtask
821
 
822
/////////////////////////////////////////////////////////////////////////
823
// Tasks which write or readwrite a single bit (including clocking)
824
 
825
task write_bit;
826
   input [2:0] bitvals;
827
   begin
828
 
829
   // Set data
830
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
831
   `wait_jtag_period;
832
 
833
   // Raise clock
834
   jtag_out(bitvals | `JTAG_TCK_bit);
835
   `wait_jtag_period;
836
 
837
   // drop clock (making output available in the SHIFT_xR states)
838
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
839
   `wait_jtag_period;
840
   end
841
endtask
842
 
843
task read_write_bit;
844
   input [2:0] bitvals;
845
   output l_tdi_val;
846
   begin
847
 
848
   // read bit state
849
   l_tdi_val <= jtag_tdi_i;
850
 
851
   // Set data
852
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
853
   `wait_jtag_period;
854
 
855
   // Raise clock
856
   jtag_out(bitvals | `JTAG_TCK_bit);
857
   `wait_jtag_period;
858
 
859
   // drop clock (making output available in the SHIFT_xR states)
860
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
861
   `wait_jtag_period;
862
   end
863
endtask
864
 
865
/////////////////////////////////////////////////////////////////
866
// Basic functions to set the state of the JTAG TAP I/F bits
867
 
868
task jtag_out;
869
  input   [2:0]   bitvals;
870
  begin
871
 
872
   jtag_tck_o <= bitvals[`JTAG_TCK];
873
   jtag_tms_o <= bitvals[`JTAG_TMS];
874
   jtag_tdo_o <= bitvals[`JTAG_TDO];
875
   end
876
endtask
877
 
878
 
879
task jtag_inout;
880
  input   [2:0]   bitvals;
881
  output l_tdi_val;
882
  begin
883
 
884
   jtag_tck_o <= bitvals[`JTAG_TCK];
885
   jtag_tms_o <= bitvals[`JTAG_TMS];
886
   jtag_tdo_o <= bitvals[`JTAG_TDO];
887
 
888
   l_tdi_val <= jtag_tdi_i;
889
   end
890
endtask
891
 
892
endmodule

powered by: WebSVN 2.1.0

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