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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [verilog/] [vpi/] [verilog/] [vpi_debug_module.v] - Blame information for rev 49

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

Line No. Rev Author Line
1 40 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  ORPSoC Testbench                                            ////
4
////                                                              ////
5
////  Description                                                 ////
6
////  ORPSoC VPI Debugging Testbench file                         ////
7
////                                                              ////
8
////  To Do:                                                      ////
9
////                                                              ////
10
////                                                              ////
11
////  Author(s):                                                  ////
12
////      - jb, jb@orsoc.se                                       ////
13
////                                                              ////
14
////                                                              ////
15
//////////////////////////////////////////////////////////////////////
16
////                                                              ////
17
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
18
////                                                              ////
19
//// This source file may be used and distributed without         ////
20
//// restriction provided that this copyright statement is not    ////
21
//// removed from the file and that any derivative work contains  ////
22
//// the original copyright notice and the associated disclaimer. ////
23
////                                                              ////
24
//// This source file is free software; you can redistribute it   ////
25
//// and/or modify it under the terms of the GNU Lesser General   ////
26
//// Public License as published by the Free Software Foundation; ////
27
//// either version 2.1 of the License, or (at your option) any   ////
28
//// later version.                                               ////
29
////                                                              ////
30
//// This source is distributed in the hope that it will be       ////
31
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
32
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
33
//// PURPOSE.  See the GNU Lesser General Public License for more ////
34
//// details.                                                     ////
35
////                                                              ////
36
//// You should have received a copy of the GNU Lesser General    ////
37
//// Public License along with this source; if not, download it   ////
38
//// from http://www.opencores.org/lgpl.shtml                     ////
39
////                                                              ////
40
//////////////////////////////////////////////////////////////////////
41
`timescale 1ns/10ps
42
 
43
`include "vpi_debug_defines.v"
44
 
45
// uncomment the following line to get more debug output for this module 
46
//`define DEBUG_INFO
47
//`define VPI_DEBUG_INFO
48
 
49
module vpi_debug_module(tms, tck, tdi, tdo);
50
 
51
   output               tms;
52
   output               tck;
53
   output               tdi;
54
   input                tdo;
55
 
56
   reg                  tms;
57
   reg                  tck;
58
   reg                  tdi;
59
 
60
   reg [31:0]            in_data_le, in_data_be;
61
   reg                  err;
62
   integer              i;
63
 
64
   reg [31:0]            id;
65
   reg [31:0]            npc;
66
   reg [31:0]            ppc;
67
   reg [31:0]            r1;
68
   reg [31:0]            insn;
69
   reg [31:0]            result;
70
   reg [31:0]            tmp;
71
 
72
   reg [31:0]            crc_out;
73
   reg [31:0]            crc_in;
74
   wire                 crc_match_in;
75
 
76
   reg [`DBG_TOP_STATUS_LEN -1:0] status;
77
   reg [`DBG_WB_STATUS_LEN -1:0]  status_wb;
78
   reg [`DBG_CPU_STATUS_LEN -1:0] status_cpu;
79
 
80
   // Text used for easier debugging
81
   reg [199:0]                     test_text;
82
   reg [`DBG_WB_CMD_LEN -1:0]      last_wb_cmd;
83
   reg [`DBG_CPU_CMD_LEN -1:0]     last_cpu_cmd;
84
   reg [199:0]                     last_wb_cmd_text;
85
   reg [199:0]                     last_cpu_cmd_text;
86
 
87
   reg [31:0]                      data_storage [0:4095];   // Data storage (for write and read operations). 
88
   reg [18:0]                      length_global;
89
 
90
   parameter                      Tp    = 1;
91
//parameter Tck   = 25;   // Clock half period (Clok period = 50 ns => 20 MHz)
92
   parameter                      Tck   = 50;   // Clock half period (Clok period = 100 ns => 10 MHz)
93
 
94
   integer cmd;
95
   integer block_cmd_length;
96
   integer jtag_instn_val;
97
   integer set_chain_val;
98
 
99
   reg [1:0] cpu_ctrl_val; // two important bits for the ctrl reg
100
   reg [31:0] cmd_adr;
101 46 julius
   reg [31:0]  cmd_size;
102 40 julius
   reg [31:0] cmd_data;
103
 
104
 
105
   initial
106
     begin
107
        $display("JTAG debug module with VPI interface enabled\n");
108
        tck    <=#Tp 1'b0;
109
        tdi    <=#Tp 1'bz;
110
        tms    <=#Tp 1'b0;
111
 
112
        // Insert a #delay here because we need to
113
        // wait until the PC isn't pointing to flash anymore
114
        // (this is around 20k ns if the flash_crash boot code
115
        // is being booted from, else much bigger, around 10mil ns)
116
 
117
        #200_000 main;
118
 
119
     end
120
 
121
   task main;
122
      begin
123
 
124
         id     <=#Tp 32'h00;
125
         npc    <=#Tp 32'h00;
126
         ppc    <=#Tp 32'h00;
127
         insn   <=#Tp 32'h00;
128
         result <=#Tp 32'h00;
129
         err    <=#Tp 1'b0;
130
         tmp    <=#Tp 32'h00;
131
 
132
        // execute some cycles
133
         #50000;
134
 
135
         reset_tap;
136
         goto_run_test_idle;
137
 
138
         //$init_rsp_server();
139
 
140
         while (1) begin
141
 
142
           // Check for incoming command
143
 
144
           // wait until a command is sent
145
           // poll with a delay here
146
           cmd = -1;
147
 
148
           while (cmd == -1)
149
             begin
150
                #1000 $check_for_command(cmd);
151
             end
152
 
153
            // now switch on the command
154
            case (cmd)
155
 
156
              `CMD_RESET : // reset
157
                begin
158
 
159
                   // call reset task
160
                   reset_tap;
161
                   // and put TAP into run_test_idle state
162
                   goto_run_test_idle;
163
 
164
               end
165
 
166
              `CMD_JTAG_SET_IR : // set jtag instruction register
167
                begin
168
 
169
                   $get_command_data(jtag_instn_val);
170
 
171
                   set_instruction(jtag_instn_val);
172
 
173
               end
174
              `CMD_SET_DEBUG_CHAIN : // set debug chain
175
               begin
176
 
177
                  $get_command_data(set_chain_val);
178
 
179
                  module_select(set_chain_val, 1'b0);   // {chain, gen_crc_err}
180
 
181
               end
182
             `CMD_CPU_CTRL_WR : // cpu CTRL write
183
               begin
184
 
185
                  $get_command_data(cpu_ctrl_val);
186
 
187
                  debug_cpu_wr_ctrl(cpu_ctrl_val, "");
188
 
189
               end
190
 
191
             `CMD_CPU_CTRL_RD : // cpu CTRL read
192
               begin
193
 
194
                  debug_cpu_rd_ctrl(cpu_ctrl_val);
195
 
196
                  $return_command_data(cpu_ctrl_val);
197
 
198
               end
199
 
200
             `CMD_CPU_WR_REG :
201
               begin
202
 
203
                  $get_command_address(cmd_adr);
204 49 julius
 
205
                  $get_command_data(block_cmd_length);
206
 
207
                  $get_command_block_data(block_cmd_length, data_storage);
208 40 julius
 
209 49 julius
                  if (block_cmd_length > 4)
210
                    cpu_write_block(cmd_adr, block_cmd_length);
211
                  else
212
                    begin
213
                       cmd_data = data_storage[0]; // Get the single word we'll write
214
                       cpu_write_32(cmd_data, cmd_adr,16'h3);
215 40 julius
`ifdef VPI_DEBUG_INFO
216
                  $display("CPU reg write. adr: 0x%x (reg group: %d reg#: %d), val: 0x%x",
217
                           cmd_adr,cmd_adr[15:11], cmd_adr[10:0], cmd_data);
218
`endif
219 49 julius
                    end
220 40 julius
 
221 49 julius
 
222 40 julius
 
223
               end
224
 
225
             `CMD_CPU_RD_REG :
226
               begin
227
 
228
                  $get_command_address(cmd_adr);
229
 
230 49 julius
                  $get_command_data(block_cmd_length); // Added 090901 --jb
231 40 julius
 
232 49 julius
                  /* Depending on size, issue a block or single read */
233
                  if (block_cmd_length > 4 )
234
                    cpu_read_block(cmd_adr, block_cmd_length);
235
                  else
236
                    cpu_read_32(cmd_data, cmd_adr, 16'h3);
237
 
238
 
239 40 julius
`ifdef VPI_DEBUG_INFO
240 49 julius
                  if (cmd_size > 4 )
241
                    $display("CPU reg read. block adr: 0x%x (reg group: %d reg#: %d), num: %d",
242
                                cmd_adr,cmd_adr[15:11], cmd_adr[10:0],  block_cmd_length);
243
                  else
244
                    $display("CPU reg read. adr: 0x%x (reg group: %d reg#: %d), val: 0x%x",
245
                                cmd_adr,cmd_adr[15:11], cmd_adr[10:0], cmd_data);
246 40 julius
`endif
247
 
248
 
249 49 julius
                  $return_command_block_data(block_cmd_length, data_storage);
250
 
251 40 julius
               end
252
 
253 46 julius
             `CMD_WB_WR :
254 40 julius
               begin
255
 
256
                  $get_command_address(cmd_adr);
257
 
258 46 julius
                  $get_command_data(cmd_size);
259
 
260 40 julius
                  $get_command_data(cmd_data);
261
 
262 46 julius
                  case (cmd_size)
263
                    4 :
264
                      begin
265
                         wb_write_32(cmd_data, cmd_adr, 16'h3);
266
                      end
267
                    2 :
268
                      begin
269
                         wb_write_16(cmd_data[15:0], cmd_adr, 16'h1);
270
                      end
271
                    1 :
272
                      begin
273
                         wb_write_8(cmd_data[7:0], cmd_adr, 16'h0);
274
                      end
275
                    default:
276
                      begin
277
                         $display("* vpi_debug_module: CMD_WB_WR size incorrect: %d\n", cmd_size);
278
                      end
279
                  endcase // case (cmd_size)
280
 
281 40 julius
               end
282
 
283
             `CMD_WB_RD32 :
284
               begin
285
 
286
                  $get_command_address(cmd_adr);
287
 
288
                  wb_read_32(cmd_data, cmd_adr, 16'h3);
289
 
290
                  $return_command_data(cmd_data);
291
 
292
               end
293
 
294
             `CMD_WB_BLOCK_WR32 :
295
               begin
296
 
297
                  $get_command_address(cmd_adr);
298
 
299
                  $get_command_data(block_cmd_length);
300
 
301
                  $get_command_block_data(block_cmd_length, data_storage);
302
 
303
                  wb_block_write_32(cmd_adr ,block_cmd_length);
304
 
305
               end
306
 
307
              `CMD_WB_BLOCK_RD32 :
308
                begin
309
 
310
                   $get_command_address(cmd_adr);
311
 
312
                   $get_command_data(block_cmd_length);
313
 
314
                   wb_block_read_32(cmd_adr, block_cmd_length);
315
 
316
                   $return_command_block_data(block_cmd_length, data_storage);
317
 
318
                end
319
 
320
              `CMD_READ_JTAG_ID :
321
                begin
322
 
323
                   read_id_code(id);
324
 
325
                   $return_command_data(id);
326
 
327
                end
328
 
329
              `CMD_GDB_DETACH :
330
                begin
331
 
332
                   $display("Debugging client disconnected. Finishing simulation");
333
 
334
 
335
                   $finish();
336
 
337
                end
338
 
339
              default:
340
                begin
341
                   $display("Somehow got to the default case in the command case statement.");
342
                   $display("Command was: %x", cmd);
343
                   $display("Exiting...");
344
 
345
                   $finish();//shouldn't be here
346
                end
347
 
348
            endcase // case (cmd)
349
 
350
            // send back response, which is currently nothing
351
            // but could be used to signal something
352
            $return_response();
353
 
354
         end // while (1)
355
      end
356
 
357
   endtask // main
358
 
359
 
360
 
361
   // Receiving data and calculating input crc
362
   always @(posedge tck)
363
     begin
364
        in_data_be[31:1] <= #1 in_data_be[30:0];
365
        in_data_be[0]    <= #1 tdo;
366
 
367
        in_data_le[31]   <= #1 tdo;
368
        in_data_le[30:0] <= #1 in_data_le[31:1];
369
     end
370
 
371
   // Generation of the TCK signal
372
   task gen_clk;
373
      input [31:0] number;
374
      integer      i;
375
      begin
376
         for(i=0; i<number; i=i+1)
377
           begin
378
              #Tck tck<=1;
379
              #Tck tck<=0;
380
           end
381
      end
382
   endtask
383
 
384
   // TAP reset
385
   task reset_tap;
386
 
387
      begin
388
`ifdef DEBUG_INFO
389
         $display("(%0t) Task reset_tap", $time);
390
`endif
391
         tms<=#1 1'b1;
392
         gen_clk(5);
393
      end
394
   endtask
395
 
396
 
397
   // Goes to RunTestIdle state
398
   task goto_run_test_idle;
399
      begin
400
`ifdef DEBUG_INFO
401
         $display("(%0t) Task goto_run_test_idle", $time);
402
`endif
403
         tms<=#1 1'b0;
404
         gen_clk(1);
405
      end
406
   endtask
407
 
408
   // sets the instruction to the IR register and goes to the RunTestIdle state
409
   task set_instruction;
410
      input [3:0] instr;
411
      integer     i;
412
 
413
      begin
414
`ifdef DEBUG_INFO
415
         case (instr)
416
           `EXTEST          : $display("(%0t) Task set_instruction (EXTEST)", $time);
417
           `SAMPLE_PRELOAD  : $display("(%0t) Task set_instruction (SAMPLE_PRELOAD)", $time);
418
           `IDCODE          : $display("(%0t) Task set_instruction (IDCODE)", $time);
419
           `DEBUG           : $display("(%0t) Task set_instruction (DEBUG)", $time);
420
           `MBIST           : $display("(%0t) Task set_instruction (MBIST)", $time);
421
           `BYPASS          : $display("(%0t) Task set_instruction (BYPASS)", $time);
422
           default
423
             begin
424
                $display("(%0t) Task set_instruction (Unsupported instruction !!!)", $time);
425
                $display("\tERROR: Unsupported instruction !!!", $time);
426
                $stop;
427
             end
428
         endcase
429
`endif
430
 
431
         tms<=#1 1;
432
         gen_clk(2);
433
         tms<=#1 0;
434
         gen_clk(2);  // we are in shiftIR
435
 
436
         for(i=0; i<`IR_LENGTH-1; i=i+1)
437
           begin
438
              tdi<=#1 instr[i];
439
              gen_clk(1);
440
           end
441
 
442
         tdi<=#1 instr[i]; // last shift
443
         tms<=#1 1;        // going out of shiftIR
444
         gen_clk(1);
445
         tdi<=#1 'hz;    // tri-state
446
         gen_clk(1);
447
         tms<=#1 0;
448
         gen_clk(1);       // we are in RunTestIdle
449
      end
450
   endtask
451
 
452
 
453
   // send 32 bits through the device
454
   task test_bypass;
455
      input  [31:0] in;
456
      output [31:0] out;
457
      integer       i;
458
 
459
      reg [31:0]    out;
460
 
461
      begin
462
         tms<=#Tp 1;
463
         gen_clk(1);
464
         tms<=#Tp 0;
465
         gen_clk(2);             // we are in shiftDR
466
 
467
         for(i=31; i>=0; i=i-1)
468
           begin
469
              tdi<=#Tp in[i];
470
              gen_clk(1);
471
           end
472
 
473
         tms<=#Tp 1;             // going out of shiftDR
474
         gen_clk(1);
475
 
476
         out <=#Tp in_data_be;
477
         tdi<=#Tp 'hz;
478
 
479
         gen_clk(1);
480
         tms<=#Tp 0;
481
         gen_clk(1);             // we are in RunTestIdle
482
      end
483
   endtask
484
 
485
   // Reads the ID code
486
   task read_id_code;
487
      output [31:0] code;
488
      reg [31:0]    code;
489
      begin
490
`ifdef DEBUG_INFO
491
         $display("(%0t) Task read_id_code", $time);
492
`endif
493
 
494
         tms<=#1 1;
495
         gen_clk(1);
496
         tms<=#1 0;
497
         gen_clk(2);  // we are in shiftDR
498
 
499
         tdi<=#1 0;
500
         gen_clk(31);
501
 
502
         tms<=#1 1;        // going out of shiftIR
503
         gen_clk(1);
504
 
505
         code = in_data_le;
506
 
507
         tdi<=#1 'hz; // tri-state
508
         gen_clk(1);
509
         tms<=#1 0;
510
         gen_clk(1);       // we are in RunTestIdle
511
      end
512
   endtask
513
 
514
   // test bundary scan chain
515
   task test_bs;
516
      input  [31:0] in;
517
      output [31:0] out;
518
      integer       i;
519
 
520
      reg [31:0]    out;
521
 
522
      begin
523
         tms<=#Tp 1;
524
         gen_clk(1);
525
         tms<=#Tp 0;
526
         gen_clk(2);             // we are in shiftDR
527
 
528
         for(i=31; i>=0; i=i-1)
529
           begin
530
              tdi<=#Tp in[i];
531
              gen_clk(1);
532
           end
533
 
534
         gen_clk(`BS_CELL_NB-1);
535
         tms<=#Tp 1;             // going out of shiftDR
536
         gen_clk(1);
537
 
538
         out <=#Tp in_data_be;
539
 
540
         gen_clk(1);
541
         tms<=#Tp 0;
542
         gen_clk(1);             // we are in RunTestIdle
543
      end
544
   endtask
545
 
546
 
547
 
548
   // sets the selected scan chain and goes to the RunTestIdle state
549
   task module_select;
550
      input [`DBG_TOP_MODULE_ID_LENGTH -1:0]  data;
551
      input                                   gen_crc_err;
552
      integer                                 i;
553
 
554
      begin
555
`ifdef DEBUG_INFO
556
         case (data)
557
           `DBG_TOP_CPU1_DEBUG_MODULE : $display("(%0t) Task module_select (DBG_TOP_CPU1_DEBUG_MODULE, gen_crc_err=%0d)", $time, gen_crc_err);
558
           `DBG_TOP_CPU0_DEBUG_MODULE : $display("(%0t) Task module_select (DBG_TOP_CPU0_DEBUG_MODULE, gen_crc_err=%0d)", $time, gen_crc_err);
559
           `DBG_TOP_WISHBONE_DEBUG_MODULE : $display("(%0t) Task module_select (DBG_TOP_WISHBONE_DEBUG_MODULE gen_crc_err=%0d)", $time, gen_crc_err);
560
           default               : $display("(%0t) Task module_select (ERROR!!! Unknown module selected)", $time);
561
         endcase
562
`endif
563
 
564
         tms<=#1 1'b1;
565
         gen_clk(1);
566
         tms<=#1 1'b0;
567
         gen_clk(2);  // we are in shiftDR
568
 
569
         status = {`DBG_TOP_STATUS_LEN{1'b0}};    // Initialize status to all 0's
570
         crc_out = {`DBG_TOP_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff    
571
         tdi<=#1 1'b1; // module_select bit
572
         calculate_crc(1'b1);
573
         gen_clk(1);
574
 
575
         for(i=`DBG_TOP_MODULE_ID_LENGTH -1; i>=0; i=i-1) // Shifting module ID
576
           begin
577
              tdi<=#1 data[i];
578
              calculate_crc(data[i]);
579
              gen_clk(1);
580
           end
581
 
582
         for(i=`DBG_TOP_CRC_LEN -1; i>=0; i=i-1)
583
           begin
584
              if (gen_crc_err & (i==0))  // Generate crc error at last crc bit
585
                tdi<=#1 ~crc_out[i];   // error crc
586
              else
587
                tdi<=#1 crc_out[i];    // ok crc
588
 
589
              gen_clk(1);
590
           end
591
 
592
         tdi<=#1 1'hz;  // tri-state
593
 
594
         crc_in = {`DBG_TOP_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
595
 
596
         for(i=`DBG_TOP_STATUS_LEN -1; i>=0; i=i-1)
597
           begin
598
              gen_clk(1);     // Generating 1 clock to read out a status bit.
599
              status[i] = tdo;
600
           end
601
 
602
         for(i=0; i<`DBG_TOP_CRC_LEN -1; i=i+1)
603
           gen_clk(1);
604
 
605
         tms<=#1 1'b1;
606
         gen_clk(1);         // to exit1_dr
607
 
608
         if (~crc_match_in)
609
           begin
610
              $display("(%0t) Incoming CRC failed !!!", $time);
611
`ifdef DEBUG_INFO
612
              $stop;
613
`endif
614
           end
615
 
616
         tms<=#1 1'b1;
617
         gen_clk(1);         // to update_dr
618
         tms<=#1 1'b0;
619
         gen_clk(1);         // to run_test_idle
620
 
621
         if (|status)
622
           begin
623
              $write("(*E) (%0t) Module select error: ", $time);
624
              casex (status)
625
                4'b1xxx : $display("CRC error !!!\n\n", $time);
626
                4'bx1xx : $display("Non-existing module selected !!!\n\n", $time);
627
                4'bxx1x : $display("Status[1] should be 1'b0 !!!\n\n", $time);
628
                4'bxxx1 : $display("Status[0] should be 1'b0 !!!\n\n", $time);
629
              endcase
630
`ifdef DEBUG_INFO
631
              $stop;
632
`endif
633
           end
634
      end
635
   endtask   // module_select
636
 
637
 
638
 
639
   // 32-bit write to the wishbone
640
   task wb_write_32;
641
      input [31:0] data;
642
      input [`DBG_WB_ADR_LEN -1:0] addr;
643
      input [`DBG_WB_LEN_LEN -1:0] length;
644
 
645
      begin
646
         data_storage[0] = data;
647
         debug_wishbone_wr_comm(`DBG_WB_WRITE32, addr, length, 1'b0);
648
         last_wb_cmd = `DBG_WB_WRITE32;  last_wb_cmd_text = "DBG_WB_WRITE32";
649
         length_global = length + 1;
650
         debug_wishbone_go(1'b0, 1'b0);
651
         //debug_wishbone_go(1'b1, 1'b0); // maybe causes underrun/overrun error when wait for WB ready?
652
         if (length>3)
653
           $display("WARNING: Only first data word is stored for writting ( See module %m)");
654
      end
655
   endtask
656
 
657
   // block 32-bit write to the wishbone
658
   // presumes data is already in data_storage[]
659
   task wb_block_write_32;
660
 
661
      input [`DBG_WB_ADR_LEN -1:0] addr;
662
      input [`DBG_WB_LEN_LEN -1:0] length;
663
 
664
      begin
665
 
666
         debug_wishbone_wr_comm(`DBG_WB_WRITE32, addr, length-1, 1'b0);
667
 
668
         last_wb_cmd = `DBG_WB_WRITE32;  last_wb_cmd_text = "DBG_WB_WRITE32";
669
 
670
         length_global = length; // number of bytes!
671
 
672
         debug_wishbone_go(1'b0, 1'b0);
673
 
674
         //debug_wishbone_go(1'b1, 1'b0); // maybe causes underrun/overrun error when wait for WB ready?
675
 
676
      end
677
   endtask
678
 
679
   // 32-bit read from the wishbone
680
   task wb_read_32;
681
 
682
    output [31:0] data;
683
 
684
    input [`DBG_WB_ADR_LEN -1:0] addr;
685
    input [`DBG_WB_LEN_LEN -1:0] length;
686
 
687
      begin
688
         debug_wishbone_wr_comm(`DBG_WB_READ32, addr, length, 1'b0);
689
         last_wb_cmd = `DBG_WB_READ32;  last_wb_cmd_text = "DBG_WB_READ32";
690
         length_global = length + 1;
691
         //debug_wishbone_go(1'b0, 1'b0);
692
         debug_wishbone_go(1'b1, 1'b0);
693
         data = data_storage[0];
694
         if (length>3)
695
           $display("WARNING: Only first data word is stored for writting ( See module %m)");
696
      end
697
   endtask
698
 
699
 
700
   // block 32-bit read from the wishbone
701
   // assumes data will be stored into data_storage[]
702
   task wb_block_read_32;
703
 
704
    input [`DBG_WB_ADR_LEN -1:0] addr;
705
    input [`DBG_WB_LEN_LEN -1:0] length;
706
 
707
      begin
708
         debug_wishbone_wr_comm(`DBG_WB_READ32, addr, length-1, 1'b0);
709
 
710
         last_wb_cmd = `DBG_WB_READ32;  last_wb_cmd_text = "DBG_WB_READ32";
711
 
712
         length_global = length;
713
 
714
         //debug_wishbone_go(1'b0, 1'b0);
715
         debug_wishbone_go(1'b1, 1'b0);
716
 
717
      end
718
   endtask
719
 
720
 
721
   // 16-bit write to the wishbone
722
   task wb_write_16;
723
      input [15:0] data;
724
      input [`DBG_WB_ADR_LEN -1:0] addr;
725
      input [`DBG_WB_LEN_LEN -1:0] length;
726
 
727
      begin
728
         data_storage[0] = {data, 16'h0};
729
         debug_wishbone_wr_comm(`DBG_WB_WRITE16, addr, length, 1'b0);
730
         last_wb_cmd = `DBG_WB_WRITE16;  last_wb_cmd_text = "DBG_WB_WRITE16";
731
         length_global = length + 1;
732
         debug_wishbone_go(1'b0, 1'b0);
733
         if (length>1)
734
           $display("WARNING: Only first data half is stored for writting ( See module %m)");
735
      end
736
   endtask
737
 
738
 
739
 
740
   // 8-bit write to the wishbone
741
   task wb_write_8;
742
      input [7:0] data;
743
      input [`DBG_WB_ADR_LEN -1:0] addr;
744
      input [`DBG_WB_LEN_LEN -1:0] length;
745
 
746
      begin
747
         data_storage[0] = {data, 24'h0};
748
         debug_wishbone_wr_comm(`DBG_WB_WRITE8, addr, length, 1'b0);
749
         last_wb_cmd = `DBG_WB_WRITE8;  last_wb_cmd_text = "DBG_WB_WRITE8";
750
         length_global = length + 1;
751
         debug_wishbone_go(1'b0, 1'b0);
752
         if (length>0)
753
           $display("WARNING: Only first data byte is stored for writting ( See module %m)");
754
      end
755
   endtask
756
 
757
 
758
 
759
   task debug_wishbone_wr_comm;
760
      input [`DBG_WB_ACC_TYPE_LEN -1:0]   acc_type;
761
      input [`DBG_WB_ADR_LEN -1:0]         addr;
762
      input [`DBG_WB_LEN_LEN -1:0]         length;
763
      input                               gen_crc_err;
764
      integer                             i;
765
      reg [`DBG_WB_CMD_LEN -1:0]           command;
766
 
767
      begin
768
`ifdef DEBUG_INFO
769
         $display("(%0t) Task debug_wishbone_wr_comm: ", $time);
770
`endif
771
 
772
         command = `DBG_WB_WR_COMM;
773
         tms<=#1 1'b1;
774
         gen_clk(1);
775
         tms<=#1 1'b0;
776
         gen_clk(2);  // we are in shiftDR
777
 
778
         crc_out = {`DBG_WB_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
779
 
780
         tdi<=#1 1'b0; // module_select bit = 0
781
         calculate_crc(1'b0);
782
         gen_clk(1);
783
 
784
         for(i=`DBG_WB_CMD_LEN -1; i>=0; i=i-1)
785
           begin
786
              tdi<=#1 command[i]; // command
787
              calculate_crc(command[i]);
788
              gen_clk(1);
789
           end
790
 
791
         for(i=`DBG_WB_ACC_TYPE_LEN -1; i>=0; i=i-1)
792
           begin
793
              tdi<=#1 acc_type[i]; // command
794
              calculate_crc(acc_type[i]);
795
              gen_clk(1);
796
           end
797
 
798
         for(i=`DBG_WB_ADR_LEN -1; i>=0; i=i-1)       // address
799
           begin
800
              tdi<=#1 addr[i];
801
              calculate_crc(addr[i]);
802
              gen_clk(1);
803
           end
804
 
805
         for(i=`DBG_WB_LEN_LEN -1; i>=0; i=i-1)       // length
806
           begin
807
              tdi<=#1 length[i];
808
              calculate_crc(length[i]);
809
              gen_clk(1);
810
           end
811
 
812
         for(i=`DBG_WB_CRC_LEN -1; i>=0; i=i-1)
813
           begin
814
              if (gen_crc_err & (i==0))  // Generate crc error at last crc bit
815
                tdi<=#1 ~crc_out[i];   // error crc
816
              else
817
                tdi<=#1 crc_out[i];    // ok crc
818
 
819
              gen_clk(1);
820
           end
821
 
822
         tdi<=#1 1'hz;
823
 
824
         crc_in = {`DBG_WB_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
825
 
826
         for(i=`DBG_WB_STATUS_LEN -1; i>=0; i=i-1)
827
           begin
828
              gen_clk(1);     // Generating clock to read out a status bit.
829
              status_wb[i] = tdo;
830
           end
831
 
832
         if (|status_wb)
833
           begin
834
              $write("(*E) (%0t) debug_wishbone_wr_comm error: ", $time);
835
              casex (status_wb)
836
                4'b1xxx : $display("CRC error !!!\n\n", $time);
837
                4'bx1xx : $display("Unknown command !!!\n\n", $time);
838
                4'bxx1x : $display("WISHBONE error !!!\n\n", $time);
839
                4'bxxx1 : $display("Overrun/Underrun !!!\n\n", $time);
840
              endcase
841
`ifdef DEBUG_INFO
842
              $stop;
843
`endif
844
           end
845
 
846
 
847
         for(i=0; i<`DBG_WB_CRC_LEN -1; i=i+1)  // Getting in the CRC
848
           begin
849
              gen_clk(1);
850
           end
851
 
852
         tms<=#1 1'b1;
853
         gen_clk(1);         // to exit1_dr
854
 
855
         if (~crc_match_in)
856
           begin
857
              $display("(%0t) Incoming CRC failed !!!", $time);
858
`ifdef DEBUG_INFO
859
              $stop;
860
`endif
861
           end
862
 
863
         tms<=#1 1'b1;
864
         gen_clk(1);         // to update_dr
865
         tms<=#1 1'b0;
866
         gen_clk(1);         // to run_test_idle
867
      end
868
   endtask       // debug_wishbone_wr_comm
869
 
870
 
871
 
872
   task debug_wishbone_go;
873
 
874
      input         wait_for_wb_ready;
875
      input         gen_crc_err;
876
      integer       i;
877
      reg [4:0]     bit_pointer;
878
      integer       word_pointer;
879
      reg [31:0]    tmp_data;
880
      reg [`DBG_WB_CMD_LEN -1:0] command;
881
 
882
 
883
      begin
884
 
885
`ifdef DEBUG_INFO
886
         $display("(%0t) Task debug_wishbone_go (previous command was %0s): ", $time, last_wb_cmd_text);
887
`endif
888
 
889
         command = `DBG_WB_GO;
890
         word_pointer = 0;
891
 
892
         tms<=#1 1'b1;
893
         gen_clk(1);
894
         tms<=#1 1'b0;
895
         gen_clk(2);  // we are in shiftDR
896
 
897
         crc_out = {`DBG_WB_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
898
 
899
         tdi<=#1 1'b0; // module_select bit = 0
900
         calculate_crc(1'b0);
901
         gen_clk(1);
902
 
903
         for(i=`DBG_WB_CMD_LEN -1; i>=0; i=i-1)
904
           begin
905
              tdi<=#1 command[i]; // command
906
              calculate_crc(command[i]);
907
              gen_clk(1);
908
           end
909
 
910
         // W R I T E
911
         if (
912
             (last_wb_cmd == `DBG_WB_WRITE8) | (last_wb_cmd == `DBG_WB_WRITE16) |
913
             (last_wb_cmd == `DBG_WB_WRITE32)
914
             )  // When WB_WRITEx was previously activated, data needs to be shifted.
915
           begin
916
              for (i=0; i<((length_global) << 3); i=i+1)
917
                begin
918
 
919
                   if ((!(i%32)) && (i>0))
920
                     begin
921
                        word_pointer = word_pointer + 1;
922
                     end
923
 
924
                   tmp_data = data_storage[word_pointer];
925
 
926
                   bit_pointer = 31-i[4:0];
927
 
928
                   tdi<=#1 tmp_data[bit_pointer];
929
 
930
                   calculate_crc(tmp_data[bit_pointer]);
931
 
932
                   gen_clk(1);
933
 
934
                end
935
           end
936
 
937
         for(i=`DBG_WB_CRC_LEN -1; i>=1; i=i-1)
938
           begin
939
              tdi<=#1 crc_out[i];
940
              gen_clk(1);
941
           end
942
 
943
         if (gen_crc_err)  // Generate crc error at last crc bit
944
           tdi<=#1 ~crc_out[0];   // error crc
945
         else
946
           tdi<=#1 crc_out[0];    // ok crc
947
 
948
         if (wait_for_wb_ready)
949
           begin
950
              tms<=#1 1'b1;
951
              gen_clk(1);       // to exit1_dr. Last CRC is shifted on this clk
952
              tms<=#1 1'b0;
953
              gen_clk(1);       // to pause_dr
954
 
955
              #2;             // wait a bit for tdo to activate
956
              while (tdo)     // waiting for wb to send "ready"
957
                begin
958
                   gen_clk(1);       // staying in pause_dr
959
                end
960
 
961
              tms<=#1 1'b1;
962
              gen_clk(1);       // to exit2_dr
963
              tms<=#1 1'b0;
964
              gen_clk(1);       // to shift_dr
965
           end
966
         else
967
           begin
968
              gen_clk(1);       // Last CRC is shifted on this clk
969
           end
970
 
971
 
972
         tdi<=#1 1'hz;
973
 
974
         // R E A D
975
 
976
         crc_in = {`DBG_WB_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
977
 
978
         if (
979
             (last_wb_cmd == `DBG_WB_READ8) | (last_wb_cmd == `DBG_WB_READ16) |
980
             (last_wb_cmd == `DBG_WB_READ32)
981
             )  // When WB_READx was previously activated, data needs to be shifted.
982
           begin
983
 
984
`ifdef DEBUG_INFO
985
              $display("\t\tGenerating %0d clocks to read %0d data bytes.", length_global<<3, length_global);
986
`endif
987
              word_pointer = 0; // Reset pointer
988
 
989
              for (i=0; i<(length_global<<3); i=i+1)
990
                begin
991
 
992
                   gen_clk(1);
993
 
994
                   if (i[4:0] == 31)   // Latching data
995
                     begin
996
 
997
                        data_storage[word_pointer] = in_data_be;
998
`ifdef DEBUG_INFO
999
                        $display("\t\tin_data_be = 0x%x", in_data_be);
1000
`endif
1001
                        word_pointer = word_pointer + 1;
1002
 
1003
                     end
1004
                end
1005
           end
1006
 
1007
         for(i=`DBG_WB_STATUS_LEN -1; i>=0; i=i-1)
1008
           begin
1009
 
1010
              gen_clk(1);     // Generating clock to read out a status bit.
1011
              status_wb[i] = tdo;
1012
 
1013
           end
1014
 
1015
         if (|status_wb)
1016
           begin
1017
              $write("(*E) (%0t) debug_wishbone_go error: ", $time);
1018
              casex (status_wb)
1019
                4'b1xxx : $display("CRC error !!!\n\n", $time);
1020
                4'bx1xx : $display("Unknown command !!!\n\n", $time);
1021
                4'bxx1x : $display("WISHBONE error !!!\n\n", $time);
1022
                4'bxxx1 : $display("Overrun/Underrun !!!\n\n", $time);
1023
              endcase
1024
`ifdef DEBUG_INFO
1025
              $stop;
1026
`endif
1027
           end
1028
 
1029
 
1030
         for(i=0; i<`DBG_WB_CRC_LEN -1; i=i+1)  // Getting in the CRC
1031
           begin
1032
              gen_clk(1);
1033
           end
1034
 
1035
         tms<=#1 1'b1;
1036
         gen_clk(1);         // to exit1_dr
1037
 
1038
         if (~crc_match_in)
1039
           begin
1040
              $display("(%0t) Incoming CRC failed !!!", $time);
1041
`ifdef DEBUG_INFO
1042
              $stop;
1043
`endif
1044
           end
1045
 
1046
         tms<=#1 1'b1;
1047
         gen_clk(1);         // to update_dr
1048
         tms<=#1 1'b0;
1049
         gen_clk(1);         // to run_test_idle
1050
      end
1051
   endtask       // debug_wishbone_go
1052
 
1053
 
1054
 
1055
   task debug_cpu_wr_ctrl;
1056
      input [`DBG_CPU_DR_LEN -1:0]  data;
1057
      input [99:0]                   text;
1058
      integer                       i;
1059
      reg [`DBG_CPU_CMD_LEN -1:0]   command;
1060
 
1061
      begin
1062
         test_text = text;
1063
 
1064
`ifdef DEBUG_INFO
1065
         $display("(%0t) Task debug_cpu_wr_ctrl (data=0x%0x (%0s))", $time, data, text);
1066
`endif
1067
 
1068
         command = `DBG_CPU_WR_CTRL;
1069
         tms<=#1 1'b1;
1070
         gen_clk(1);
1071
         tms<=#1 1'b0;
1072
         gen_clk(2);  // we are in shiftDR
1073
 
1074
         crc_out = {`DBG_CPU_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
1075
 
1076
         tdi<=#1 1'b0; // module_select bit = 0
1077
         calculate_crc(1'b0);
1078
         gen_clk(1);
1079
 
1080
         for(i=`DBG_CPU_CMD_LEN -1; i>=0; i=i-1)
1081
           begin
1082
              tdi<=#1 command[i]; // command
1083
              calculate_crc(command[i]);
1084
              gen_clk(1);
1085
           end
1086
 
1087
 
1088
         for(i=`DBG_CPU_CTRL_LEN -1; i>=0; i=i-1)
1089
           begin
1090
              tdi<=#1 data[i];                                    // data (used cotrol bits
1091
              calculate_crc(data[i]);
1092
              gen_clk(1);
1093
           end
1094
 
1095
         for(i=`DBG_CPU_DR_LEN - `DBG_CPU_CTRL_LEN -1; i>=0; i=i-1)  // unused control bits
1096
           begin
1097
              tdi<=#1 1'b0;
1098
              calculate_crc(1'b0);
1099
              gen_clk(1);
1100
           end
1101
 
1102
 
1103
         for(i=`DBG_CPU_CRC_LEN -1; i>=0; i=i-1)
1104
           begin
1105
              tdi<=#1 crc_out[i];    // ok crc
1106
              gen_clk(1);
1107
           end
1108
 
1109
         tdi<=#1 1'hz;
1110
 
1111
         crc_in = {`DBG_CPU_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
1112
 
1113
         for(i=`DBG_CPU_STATUS_LEN -1; i>=0; i=i-1)
1114
           begin
1115
              gen_clk(1);     // Generating clock to read out a status bit.
1116
              status_cpu[i] = tdo;
1117
           end
1118
 
1119
         if (|status_cpu)
1120
           begin
1121
              $write("(*E) (%0t) debug_cpu_wr_ctrl error: ", $time);
1122
              casex (status_cpu)
1123
                4'b1xxx : $display("CRC error !!!\n\n", $time);
1124
                4'bx1xx : $display("??? error !!!\n\n", $time);
1125
                4'bxx1x : $display("??? error !!!\n\n", $time);
1126
                4'bxxx1 : $display("??? error !!!\n\n", $time);
1127
              endcase
1128
`ifdef DEBUG_INFO
1129
              $stop;
1130
`endif
1131
           end
1132
 
1133
 
1134
         for(i=0; i<`DBG_CPU_CRC_LEN -1; i=i+1)  // Getting in the CRC
1135
           begin
1136
              gen_clk(1);
1137
           end
1138
 
1139
         tms<=#1 1'b1;
1140
         gen_clk(1);         // to exit1_dr
1141
 
1142
         if (~crc_match_in)
1143
           begin
1144
              $display("(%0t) Incoming CRC failed !!!", $time);
1145
`ifdef DEBUG_INFO
1146
              $stop;
1147
`endif
1148
           end
1149
 
1150
         tms<=#1 1'b1;
1151
         gen_clk(1);         // to update_dr
1152
         tms<=#1 1'b0;
1153
         gen_clk(1);         // to run_test_idle
1154
      end
1155
   endtask       // debug_cpu_wr_ctrl
1156
 
1157
   task debug_cpu_rd_ctrl;
1158
      output [`DBG_CPU_DR_LEN -1:0]  data;
1159
      //input [99:0]                 text;
1160
      integer                        i;
1161
      reg [`DBG_CPU_CMD_LEN -1:0]    command;
1162
 
1163
      begin
1164
 
1165
 
1166
`ifdef DEBUG_INFO
1167
         $display("(%0t) Task debug_cpu_rd_ctrl", $time);
1168
`endif
1169
 
1170
         command = `DBG_CPU_RD_CTRL;
1171
         tms<=#1 1'b1;
1172
         gen_clk(1);
1173
         tms<=#1 1'b0;
1174
         gen_clk(2);  // we are in shiftDR
1175
 
1176
         crc_out = {`DBG_CPU_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
1177
 
1178
         tdi<=#1 1'b0; // module_select bit = 0
1179
         calculate_crc(1'b0);
1180
         gen_clk(1);
1181
 
1182
         for(i=`DBG_CPU_CMD_LEN -1; i>=0; i=i-1)
1183
           begin
1184
              tdi<=#1 command[i]; // command
1185
              calculate_crc(command[i]);
1186
              gen_clk(1);
1187
           end
1188
 
1189
         for(i=`DBG_CPU_CRC_LEN -1; i>=0; i=i-1)
1190
           begin
1191
              tdi<=#1 crc_out[i];    // ok crc
1192
              gen_clk(1);
1193
           end
1194
 
1195
         tdi<=#1 1'hz;
1196
 
1197
         crc_in = {`DBG_CPU_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
1198
 
1199
         // Read incoming debug ctrl data (52 bits)
1200
         //cpu_ctrl_val[1:0];
1201
         gen_clk(1);
1202
         //cpu_ctrl_val[0]  <= #1 tdo; // cpu reset bit
1203
         data[0]  <= #1 tdo; // cpu reset bit
1204
         gen_clk(1);
1205
         //cpu_ctrl_val[1]  <= #1 tdo; // cpu stall bit
1206
         data[1]  <= #1 tdo; // cpu stall bit
1207
 
1208
         for(i=`DBG_CPU_DR_LEN - `DBG_CPU_CTRL_LEN -1; i>=0; i=i-1)  // unused control bits
1209
           begin
1210
              gen_clk(1);
1211
           end
1212
 
1213
         for(i=`DBG_CPU_STATUS_LEN -1; i>=0; i=i-1)
1214
           begin
1215
              gen_clk(1);     // Generating clock to read out a status bit.
1216
              status_cpu[i] = tdo;
1217
           end
1218
 
1219
         if (|status_cpu)
1220
           begin
1221
              $write("(*E) (%0t) debug_cpu_wr_ctrl error: ", $time);
1222
              casex (status_cpu)
1223
                4'b1xxx : $display("CRC error !!!\n\n", $time);
1224
                4'bx1xx : $display("??? error !!!\n\n", $time);
1225
                4'bxx1x : $display("??? error !!!\n\n", $time);
1226
                4'bxxx1 : $display("??? error !!!\n\n", $time);
1227
              endcase // casex (status_cpu)
1228
 
1229
           end
1230
 
1231
 
1232
         for(i=0; i<`DBG_CPU_CRC_LEN -1; i=i+1)  // Getting in the CRC
1233
           begin
1234
              gen_clk(1);
1235
           end
1236
 
1237
         tms<=#1 1'b1;
1238
         gen_clk(1);         // to exit1_dr
1239
 
1240
         if (~crc_match_in)
1241
           begin
1242
              $display("(%0t) Incoming CRC failed !!!", $time);
1243
 
1244
           end
1245
 
1246
         tms<=#1 1'b1;
1247
         gen_clk(1);         // to update_dr
1248
         tms<=#1 1'b0;
1249
         gen_clk(1);         // to run_test_idle
1250
      end
1251
   endtask       // debug_cpu_rd_ctrl
1252
 
1253
 
1254
   // 32-bit read from cpu
1255
   task cpu_read_32;
1256
      output [31:0] data;
1257
      input [`DBG_WB_ADR_LEN -1:0] addr;
1258
      input [`DBG_WB_LEN_LEN -1:0] length;
1259
 
1260
      reg [31:0]                    tmp;
1261
 
1262
      begin
1263
         debug_cpu_wr_comm(`DBG_CPU_READ, addr, length, 1'b0);
1264
 
1265
         last_cpu_cmd = `DBG_CPU_READ;  last_cpu_cmd_text = "DBG_CPU_READ";
1266
 
1267
         length_global = length + 1;
1268
 
1269
         debug_cpu_go(1'b0, 1'b0);
1270
 
1271
         data = data_storage[0];
1272
 
1273
         if (length>3)
1274
           $display("WARNING: Only first data word is returned( See module %m.)");
1275
 
1276
      end
1277
   endtask
1278
 
1279 49 julius
   // block of 32-bit reads from cpu
1280
   task cpu_read_block;
1281
      //output [31:0] data;
1282
      input [`DBG_WB_ADR_LEN -1:0] addr;
1283
      input [`DBG_WB_LEN_LEN -1:0] length;
1284 40 julius
 
1285 49 julius
      reg [31:0]                    tmp;
1286 40 julius
 
1287 49 julius
      begin
1288
         debug_cpu_wr_comm(`DBG_CPU_READ, addr, length-1, 1'b0);
1289
 
1290
         last_cpu_cmd = `DBG_CPU_READ;  last_cpu_cmd_text = "DBG_CPU_READ";
1291
 
1292
         length_global = length;
1293
 
1294
         debug_cpu_go(1'b0, 1'b0);
1295
 
1296
         //data = data_storage[0];
1297
 
1298
         //if (length>3)
1299
         //  $display("WARNING: Only first data word is returned( See module %m.)");
1300
 
1301
      end
1302
   endtask
1303
 
1304
 
1305 40 julius
   // 32-bit write to cpu
1306
   task cpu_write_32;
1307
      input [31:0] data;
1308
      input [`DBG_WB_ADR_LEN -1:0] addr;
1309
      input [`DBG_WB_LEN_LEN -1:0] length;
1310
 
1311
      reg [31:0]                    tmp;
1312
 
1313
      begin
1314
         debug_cpu_wr_comm(`DBG_CPU_WRITE, addr, length, 1'b0);
1315
         last_cpu_cmd = `DBG_CPU_WRITE;  last_cpu_cmd_text = "DBG_CPU_WRITE";
1316
         length_global = length + 1;
1317
         data_storage[0] = data;
1318
         debug_cpu_go(1'b0, 1'b0);
1319
         if (length>3)
1320
           $display("WARNING: Only first data word is stored for writting ( See module %m)");
1321
      end
1322
   endtask
1323
 
1324 49 julius
   // block of 32-bit writes to cpu
1325
   // Data will already be in data_storage
1326
   task cpu_write_block;
1327
      //input [31:0] data;
1328
      input [`DBG_WB_ADR_LEN -1:0] addr;
1329
      input [`DBG_WB_LEN_LEN -1:0] length;
1330 40 julius
 
1331 49 julius
      reg [31:0]                    tmp;
1332 40 julius
 
1333 49 julius
      begin
1334
         debug_cpu_wr_comm(`DBG_CPU_WRITE, addr, length-1, 1'b0);
1335
         last_cpu_cmd = `DBG_CPU_WRITE;  last_cpu_cmd_text = "DBG_CPU_WRITE";
1336
         length_global = length;
1337
         debug_cpu_go(1'b0, 1'b0);
1338
      end
1339
   endtask
1340
 
1341
 
1342 40 julius
   task debug_cpu_wr_comm;
1343
      input [`DBG_CPU_ACC_TYPE_LEN -1:0]  acc_type;
1344
      input [`DBG_CPU_ADR_LEN -1:0]        addr;
1345
      input [`DBG_CPU_LEN_LEN -1:0]        length;
1346
      input                               gen_crc_err;
1347
      integer                             i;
1348
      reg [`DBG_CPU_CMD_LEN -1:0]          command;
1349
 
1350
      begin
1351
`ifdef DEBUG_INFO
1352
         $display("(%0t) Task debug_cpu_wr_comm: ", $time);
1353
`endif
1354
 
1355
         command = `DBG_CPU_WR_COMM;
1356
         tms<=#1 1'b1;
1357
         gen_clk(1);
1358
         tms<=#1 1'b0;
1359
         gen_clk(2);  // we are in shiftDR
1360
 
1361
         crc_out = {`DBG_CPU_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
1362
 
1363
         tdi<=#1 1'b0; // module_select bit = 0
1364
         calculate_crc(1'b0);
1365
         gen_clk(1);
1366
 
1367
         for(i=`DBG_CPU_CMD_LEN -1; i>=0; i=i-1)
1368
           begin
1369
              tdi<=#1 command[i]; // command
1370
              calculate_crc(command[i]);
1371
              gen_clk(1);
1372
           end
1373
 
1374
         for(i=`DBG_CPU_ACC_TYPE_LEN -1; i>=0; i=i-1)
1375
           begin
1376
              tdi<=#1 acc_type[i]; // command
1377
              calculate_crc(acc_type[i]);
1378
              gen_clk(1);
1379
           end
1380
 
1381
         for(i=`DBG_CPU_ADR_LEN -1; i>=0; i=i-1)       // address
1382
           begin
1383
              tdi<=#1 addr[i];
1384
              calculate_crc(addr[i]);
1385
              gen_clk(1);
1386
           end
1387
 
1388
         for(i=`DBG_CPU_LEN_LEN -1; i>=0; i=i-1)       // length
1389
           begin
1390
              tdi<=#1 length[i];
1391
              calculate_crc(length[i]);
1392
              gen_clk(1);
1393
           end
1394
 
1395
         for(i=`DBG_CPU_CRC_LEN -1; i>=0; i=i-1)
1396
           begin
1397
              if (gen_crc_err & (i==0))      // Generate crc error at last crc bit
1398
                tdi<=#1 ~crc_out[i];   // error crc
1399
              else
1400
                tdi<=#1 crc_out[i];    // ok crc
1401
 
1402
              gen_clk(1);
1403
           end
1404
 
1405
         tdi<=#1 1'hz;
1406
 
1407
         crc_in = {`DBG_CPU_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
1408
 
1409
         for(i=`DBG_CPU_STATUS_LEN -1; i>=0; i=i-1)
1410
           begin
1411
              gen_clk(1);     // Generating clock to read out a status bit.
1412
              status_cpu[i] = tdo;
1413
           end
1414
 
1415
         if (|status_cpu)
1416
           begin
1417
              $write("(*E) (%0t) debug_cpu_wr_comm error: ", $time);
1418
              casex (status_cpu)
1419
                4'b1xxx : $display("CRC error !!!\n\n", $time);
1420
                4'bx1xx : $display("Unknown command !!!\n\n", $time);
1421
                4'bxx1x : $display("??? error !!!\n\n", $time);
1422
                4'bxxx1 : $display("Overrun/Underrun !!!\n\n", $time);
1423
              endcase
1424
`ifdef DEBUG_INFO
1425
              $stop;
1426
`endif
1427
           end
1428
 
1429
 
1430
         for(i=0; i<`DBG_CPU_CRC_LEN -1; i=i+1)  // Getting in the CRC
1431
           begin
1432
              gen_clk(1);
1433
           end
1434
 
1435
         tms<=#1 1'b1;
1436
         gen_clk(1);         // to exit1_dr
1437
 
1438
         if (~crc_match_in)
1439
           begin
1440
              $display("(%0t) Incoming CRC failed !!!", $time);
1441
`ifdef DEBUG_INFO
1442
              $stop;
1443
`endif
1444
           end
1445
 
1446
         tms<=#1 1'b1;
1447
         gen_clk(1);         // to update_dr
1448
         tms<=#1 1'b0;
1449
         gen_clk(1);         // to run_test_idle
1450
      end
1451
   endtask       // debug_cpu_wr_comm
1452
 
1453
 
1454
 
1455
   task debug_cpu_go;
1456
      input         wait_for_cpu_ready;
1457
      input         gen_crc_err;
1458
      integer       i;
1459
      reg [4:0]     bit_pointer;
1460
      integer       word_pointer;
1461
      reg [31:0]    tmp_data;
1462
      reg [`DBG_CPU_CMD_LEN -1:0] command;
1463
 
1464
 
1465
      begin
1466
`ifdef DEBUG_INFO
1467
         $display("(%0t) Task debug_cpu_go (previous command was %0s): ", $time, last_cpu_cmd_text);
1468
`endif
1469
         command = `DBG_CPU_GO;
1470
         word_pointer = 0;
1471
 
1472
         tms<=#1 1'b1;
1473
         gen_clk(1);
1474
         tms<=#1 1'b0;
1475
         gen_clk(2);  // we are in shiftDR
1476
 
1477
         crc_out = {`DBG_CPU_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
1478
 
1479
         tdi<=#1 1'b0; // module_select bit = 0
1480
         calculate_crc(1'b0);
1481
         gen_clk(1);
1482
 
1483
         for(i=`DBG_CPU_CMD_LEN -1; i>=0; i=i-1)
1484
           begin
1485
              tdi<=#1 command[i]; // command
1486
              calculate_crc(command[i]);
1487
              gen_clk(1);
1488
           end
1489
 
1490
 
1491
         if (last_cpu_cmd == `DBG_CPU_WRITE)  // When DBG_CPU_WRITE was previously activated, data needs to be shifted.
1492
           begin
1493
              for (i=0; i<((length_global) << 3); i=i+1)
1494
                begin
1495
                   tmp_data = data_storage[word_pointer];
1496
                   if ((!(i%32)) && (i>0))
1497
                     begin
1498
                        word_pointer = word_pointer + 1;
1499
                     end
1500
                   bit_pointer = 31-i[4:0];
1501
                   tdi<=#1 tmp_data[bit_pointer];
1502
                   calculate_crc(tmp_data[bit_pointer]);
1503
                   gen_clk(1);
1504
 
1505
                end
1506
           end
1507
 
1508
         for(i=`DBG_CPU_CRC_LEN -1; i>=1; i=i-1)
1509
           begin
1510
              tdi<=#1 crc_out[i];
1511
              gen_clk(1);
1512
           end
1513
 
1514
         if (gen_crc_err)  // Generate crc error at last crc bit
1515
           tdi<=#1 ~crc_out[0];   // error crc
1516
         else
1517
           tdi<=#1 crc_out[0];    // ok crc
1518
 
1519
         if (wait_for_cpu_ready)
1520
           begin
1521
              tms<=#1 1'b1;
1522
              gen_clk(1);       // to exit1_dr. Last CRC is shifted on this clk
1523
              tms<=#1 1'b0;
1524
              gen_clk(1);       // to pause_dr
1525
 
1526
              #2;             // wait a bit for tdo to activate
1527
              while (tdo)     // waiting for wb to send "ready"
1528
                begin
1529
                   gen_clk(1);       // staying in pause_dr
1530
                end
1531
 
1532
              tms<=#1 1'b1;
1533
              gen_clk(1);       // to exit2_dr
1534
              tms<=#1 1'b0;
1535
              gen_clk(1);       // to shift_dr
1536
           end
1537
         else
1538
           begin
1539
              gen_clk(1);       // Last CRC is shifted on this clk
1540
           end
1541
 
1542
 
1543
         tdi<=#1 1'hz;
1544
         crc_in = {`DBG_CPU_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
1545
 
1546
         if (last_cpu_cmd == `DBG_CPU_READ)  // When DBG_CPU_READ was previously activated, data needs to be shifted.
1547
           begin
1548
`ifdef DEBUG_INFO
1549
              $display("\t\tGenerating %0d clocks to read %0d data bytes.", length_global<<3, length_global);
1550
`endif
1551
              word_pointer = 0; // Reset pointer
1552
              for (i=0; i<(length_global<<3); i=i+1)
1553
                begin
1554
 
1555
                   gen_clk(1);
1556
 
1557
                   if (i[4:0] == 31)   // Latching data
1558
                     begin
1559
                        data_storage[word_pointer] = in_data_be;
1560
`ifdef DEBUG_INFO
1561
                        $display("\t\tin_data_be = 0x%x", in_data_be);
1562
`endif
1563
                        word_pointer = word_pointer + 1;
1564
                     end
1565
 
1566
                end
1567
           end
1568
 
1569
 
1570
         for(i=`DBG_CPU_STATUS_LEN -1; i>=0; i=i-1)
1571
           begin
1572
              gen_clk(1);     // Generating clock to read out a status bit.
1573
              status_cpu[i] = tdo;
1574
           end
1575
 
1576
         if (|status_cpu)
1577
           begin
1578
              $write("(*E) (%0t) debug_cpu_go error: ", $time);
1579
              casex (status_cpu)
1580
                4'b1xxx : $display("CRC error !!!\n\n", $time);
1581
                4'bx1xx : $display("Unknown command !!!\n\n", $time);
1582
                4'bxx1x : $display("??? error !!!\n\n", $time);
1583
                4'bxxx1 : $display("Overrun/Underrun !!!\n\n", $time);
1584
              endcase
1585
              $stop;
1586
           end
1587
 
1588
 
1589
         for(i=0; i<`DBG_CPU_CRC_LEN -1; i=i+1)  // Getting in the CRC
1590
           begin
1591
              gen_clk(1);
1592
           end
1593
 
1594
         tms<=#1 1'b1;
1595
         gen_clk(1);         // to exit1_dr
1596
 
1597
         if (~crc_match_in)
1598
           begin
1599
              $display("(%0t) Incoming CRC failed !!!", $time);
1600
              $stop;
1601
           end
1602
 
1603
         tms<=#1 1'b1;
1604
         gen_clk(1);         // to update_dr
1605
         tms<=#1 1'b0;
1606
         gen_clk(1);         // to run_test_idle
1607
      end
1608
   endtask       // debug_cpu_go
1609
 
1610
 
1611
 
1612
 
1613
   // Calculating outgoing CRC
1614
   task calculate_crc;
1615
      input data;
1616
 
1617
      begin
1618
         crc_out[0]  <= #1 data          ^ crc_out[31];
1619
         crc_out[1]  <= #1 data          ^ crc_out[0]  ^ crc_out[31];
1620
         crc_out[2]  <= #1 data          ^ crc_out[1]  ^ crc_out[31];
1621
         crc_out[3]  <= #1 crc_out[2];
1622
         crc_out[4]  <= #1 data          ^ crc_out[3]  ^ crc_out[31];
1623
         crc_out[5]  <= #1 data          ^ crc_out[4]  ^ crc_out[31];
1624
         crc_out[6]  <= #1 crc_out[5];
1625
         crc_out[7]  <= #1 data          ^ crc_out[6]  ^ crc_out[31];
1626
         crc_out[8]  <= #1 data          ^ crc_out[7]  ^ crc_out[31];
1627
         crc_out[9]  <= #1 crc_out[8];
1628
         crc_out[10] <= #1 data         ^ crc_out[9]  ^ crc_out[31];
1629
         crc_out[11] <= #1 data         ^ crc_out[10] ^ crc_out[31];
1630
         crc_out[12] <= #1 data         ^ crc_out[11] ^ crc_out[31];
1631
         crc_out[13] <= #1 crc_out[12];
1632
         crc_out[14] <= #1 crc_out[13];
1633
         crc_out[15] <= #1 crc_out[14];
1634
         crc_out[16] <= #1 data         ^ crc_out[15] ^ crc_out[31];
1635
         crc_out[17] <= #1 crc_out[16];
1636
         crc_out[18] <= #1 crc_out[17];
1637
         crc_out[19] <= #1 crc_out[18];
1638
         crc_out[20] <= #1 crc_out[19];
1639
         crc_out[21] <= #1 crc_out[20];
1640
         crc_out[22] <= #1 data         ^ crc_out[21] ^ crc_out[31];
1641
         crc_out[23] <= #1 data         ^ crc_out[22] ^ crc_out[31];
1642
         crc_out[24] <= #1 crc_out[23];
1643
         crc_out[25] <= #1 crc_out[24];
1644
         crc_out[26] <= #1 data         ^ crc_out[25] ^ crc_out[31];
1645
         crc_out[27] <= #1 crc_out[26];
1646
         crc_out[28] <= #1 crc_out[27];
1647
         crc_out[29] <= #1 crc_out[28];
1648
         crc_out[30] <= #1 crc_out[29];
1649
         crc_out[31] <= #1 crc_out[30];
1650
      end
1651
   endtask // calculate_crc
1652
 
1653
 
1654
   // Calculating and checking input CRC
1655
   always @(posedge tck)
1656
     begin
1657
        crc_in[0]  <= #1 tdo           ^ crc_in[31];
1658
        crc_in[1]  <= #1 tdo           ^ crc_in[0]  ^ crc_in[31];
1659
        crc_in[2]  <= #1 tdo           ^ crc_in[1]  ^ crc_in[31];
1660
        crc_in[3]  <= #1 crc_in[2];
1661
        crc_in[4]  <= #1 tdo           ^ crc_in[3]  ^ crc_in[31];
1662
        crc_in[5]  <= #1 tdo           ^ crc_in[4]  ^ crc_in[31];
1663
        crc_in[6]  <= #1 crc_in[5];
1664
        crc_in[7]  <= #1 tdo           ^ crc_in[6]  ^ crc_in[31];
1665
        crc_in[8]  <= #1 tdo           ^ crc_in[7]  ^ crc_in[31];
1666
        crc_in[9]  <= #1 crc_in[8];
1667
        crc_in[10] <= #1 tdo          ^ crc_in[9]  ^ crc_in[31];
1668
        crc_in[11] <= #1 tdo          ^ crc_in[10] ^ crc_in[31];
1669
        crc_in[12] <= #1 tdo          ^ crc_in[11] ^ crc_in[31];
1670
        crc_in[13] <= #1 crc_in[12];
1671
        crc_in[14] <= #1 crc_in[13];
1672
        crc_in[15] <= #1 crc_in[14];
1673
        crc_in[16] <= #1 tdo          ^ crc_in[15] ^ crc_in[31];
1674
        crc_in[17] <= #1 crc_in[16];
1675
        crc_in[18] <= #1 crc_in[17];
1676
        crc_in[19] <= #1 crc_in[18];
1677
        crc_in[20] <= #1 crc_in[19];
1678
        crc_in[21] <= #1 crc_in[20];
1679
        crc_in[22] <= #1 tdo          ^ crc_in[21] ^ crc_in[31];
1680
        crc_in[23] <= #1 tdo          ^ crc_in[22] ^ crc_in[31];
1681
        crc_in[24] <= #1 crc_in[23];
1682
        crc_in[25] <= #1 crc_in[24];
1683
        crc_in[26] <= #1 tdo          ^ crc_in[25] ^ crc_in[31];
1684
        crc_in[27] <= #1 crc_in[26];
1685
        crc_in[28] <= #1 crc_in[27];
1686
        crc_in[29] <= #1 crc_in[28];
1687
        crc_in[30] <= #1 crc_in[29];
1688
        crc_in[31] <= #1 crc_in[30];
1689
     end
1690
 
1691
   assign crc_match_in = crc_in == 32'h0;
1692
 
1693
 
1694
endmodule // vpi_debug_module

powered by: WebSVN 2.1.0

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