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/] [full_system/] [adv_dbg_tb.v] - Blame information for rev 56

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

powered by: WebSVN 2.1.0

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