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 40

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

powered by: WebSVN 2.1.0

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