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

Subversion Repositories uart2bus_testbench

[/] [uart2bus_testbench/] [trunk/] [tb/] [interfaces/] [uart_interface.sv] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 HanySalah
//-----------------------------------------------------------------------------
2
//
3
//                             UART2BUS VERIFICATION
4
//
5
//-----------------------------------------------------------------------------
6
// CREATOR    : HANY SALAH
7
// PROJECT    : UART2BUS UVM TEST BENCH
8
// UNIT       : BUS FUNCTIONAL MODEL
9
//-----------------------------------------------------------------------------
10
// TITLE      : UART Interface
11
// DESCRIPTION: This
12
//-----------------------------------------------------------------------------
13
// LOG DETAILS
14
//-------------
15
// VERSION      NAME        DATE        DESCRIPTION
16
//    1       HANY SALAH    25122015    FILE CREATION
17
//    2       HANY SALAH    07012016    ADD USER ROUTINE, TEXT MODE ROUTINES.
18
//    3       HANY SALAH    21012016    REPLACE PUSH BYTE WITH PUSH FIELD
19
//-----------------------------------------------------------------------------
20
// ALL COPYRIGHTS ARE RESERVED FOR THE PRODUCER ONLY .THIS FILE IS PRODUCED FOR
21
// OPENCORES MEMBERS ONLY AND IT IS PROHIBTED TO USE THIS MATERIAL WITHOUT THE
22
// CREATOR'S PERMISSION
23
//-----------------------------------------------------------------------------
24
`include "defin_lib.svh"
25
interface uart_interface (input bit clock,        // Global Clock Signal
26
                          input bit reset);       // Global Asynchronous Reset Signal
27
 
28
//-----------------------------------------------
29
//
30
//                BFM Parameter
31
//
32
//-----------------------------------------------
33
 
34
  int                 act_edge;     // 2: negative edge   1: positive edge
35
  int                 start_bit;    // 2: LSB first       1: MSB first
36
  int                 num_stop_bits;// 2: two stop bits   1: one stop bits
37
  int                 num_bits;     // 7: seven bits data 8: eight bits data
38
  int                 data_rep;     // 2: binarry         1: ASCII
39
  int                 parity;  // 3: parity odd      2: parity even        1: parity off
40
  time                response_time;
41
 
42
 
43
//-----------------------------------------------
44
//
45
//              UART Signals
46
//
47
//-----------------------------------------------
48
 
49
  logic               ser_in;         // Serial Data Input
50
  logic               ser_out;        // Serial Data Ouptut
51
  wire                serial_out;
52
 
53
//-----------------------------------------------
54
//
55
//              USER Variables Declarations
56
//
57
//-----------------------------------------------
58
 
59
  event start_trans;
60
 
61
  assign serial_out = 1'bz;
62
  assign serial_out = ser_out;
63
 
64
//-----------------------------------------------
65
//
66
//   USER Routines
67
//
68
//-----------------------------------------------
69
 
70
  // Through This routine, the uart bfm is initialize its configuration parameters
71
  // BFM should know through which edge it will either capture or force the data.
72
  // Also should know whether the sent or received data will start with most sign-
73
  // ificant bit or least significant bit.
74
  function void set_configuration ( int _edge,
75
                                    int first_bit,
76
                                    int _numstopbits,
77
                                    int _numbits,
78
                                    int _datarep,
79
                                    int _paritymode,
80
                                    time _resp);
81
    act_edge        = _edge;
82
    start_bit       = first_bit;
83
    num_stop_bits   = _numstopbits;
84
    num_bits        = _numbits;
85
    data_rep        = _datarep;
86
    parity          = _paritymode;
87
    response_time   = _resp;
88
  endfunction: set_configuration
89
 
90
 
91
  // Through the following routine, the uart bfm make an event that some transact-
92
  // -ion would be forced on UART signals
93
  function void set_event ();
94
    -> start_trans ;
95
  endfunction:set_event
96
 
97
 
98
  function void force_sout(bit x);
99
    case (x)
100
      1'b1:
101
        begin
102
        //ser_out = `one;
103
        ser_out = 1'b1;
104
        end
105
      1'b0:
106
        begin
107
        //ser_out = `zero;
108
        ser_out = 1'b0;
109
        end
110
    endcase
111
  endfunction:force_sout
112
 
113
  // Through this routine, the uart bfm push bit on the serial ouptut port ser_out
114
  // based on the configured active edge field, UART will push bit on data. BFM
115
  // will assign testbench error in case of un-configured active edge.
116
  task push_bit_serout (input bit data);
117
    case (act_edge)
118
      `_negedge:
119
        begin
120
        @(negedge clock)
121
          begin
122
          force_sout(data);
123
          end
124
        end
125
      `_posedge:
126
        begin
127
        @(posedge clock)
128
          begin
129
          force_sout(data);
130
          end
131
        end
132
      default:
133
        begin
134
        $error("undefined active edge");
135
        end
136
    endcase
137
  endtask:push_bit_serout
138
 
139
  // Through this routine, the uart bfm push bit on the serial input port ser_in
140
  // based on the configured active edge field, UART will push bit on data. BFM
141
  // will assign testbench error in case of un-configured active edge.
142
  /*task push_bit_serin (input bit data);
143
    case (act_edge)
144
      `_negedge:
145
        begin
146
        @(negedge clock)
147
          begin
148
          ser_in = data;
149
          end
150
        end
151
      `_posedge:
152
        begin
153
        @(posedge clock)
154
          begin
155
          ser_in = data;
156
          end
157
        end
158
      default:
159
        begin
160
        $error("undefined active edge");
161
        end
162
    endcase
163
  endtask:push_bit_serin*/
164
 
165
  // The following task will catch single bit from serial output port ser_out based
166
  // on the configured active edge field, UART will capture data bit. BFM will
167
  // assign testbench error in case of un-configured active edge.
168
  task catch_bit_serout (output bit data);
169
    case (act_edge)
170
      `_negedge:
171
        begin
172
        @(negedge clock)
173
          begin
174
          #(`buad_clk_period/4) data = ser_out;
175
          end
176
        end
177
      `_posedge:
178
        begin
179
        @(posedge clock)
180
          begin
181
          #(`buad_clk_period/4) data = ser_out;
182
          end
183
        end
184
      default:
185
        begin
186
        $error("undefined active edge");
187
        end
188
    endcase
189
  endtask:catch_bit_serout
190
 
191
  // The following task will catch single bit from serial input port ser_in based
192
  // on the configured active edge field, UART will capture data bit. BFM will
193
  // assign testbench error in case of un-configured active edge.
194
  task catch_bit_serin (output bit data);
195
    case (act_edge)
196
      `_negedge:
197
        begin
198
        @(negedge clock)
199
          begin
200
          #(`buad_clk_period/4) data = ser_in;
201
          end
202
        end
203
      `_posedge:
204
        begin
205
        @(posedge clock)
206
          begin
207
          #(`buad_clk_period/4)data = ser_in;
208
          end
209
        end
210
      default:
211
        begin
212
        $error("undefined active edge");
213
        end
214
    endcase
215
  endtask:catch_bit_serin
216
 
217
  // Through the following task, UART BFM will force data byte on serial output
218
  // port based on the configured start_bit field. BFM will assign testbench err-
219
  // or in case of un-configured start_bit field.
220
  task push_field_serout (input byte data);
221
    // start bit
222
    push_bit_serout(1'b0);
223
 
224
    // data fields
225
    case (start_bit)
226
      `lsb_first:
227
        begin
228
        for (int index=0;index<8;index++)
229
          begin
230
          push_bit_serout(data[index]);
231
          end
232
        end
233
      `msb_first:
234
        begin
235
        for (int index=7;index>=0;index--)
236
          begin
237
          push_bit_serout(data[index]);
238
          end
239
        end
240
      default:
241
        begin
242
        $error("Undefined serial mode");
243
        end
244
    endcase
245
 
246
    // Stop bit(s)
247
    repeat (num_stop_bits)
248
      begin
249
      push_bit_serout(1'b1);
250
      end
251
 
252
  endtask:push_field_serout
253
 
254
  // Through the following task, UART BFM will force data byte on serial output
255
  // port based on the configured start_bit field. BFM will assign testbench err-
256
  // or in case of un-configured start_bit field.
257
  /*task push_byte_serin (input byte data);
258
    case (start_bit)
259
      `lsb_first:
260
        begin
261
        for (int index=0;index<8;index++)
262
          begin
263
          push_bit_serin(data[index]);
264
          end
265
        end
266
      `msb_first:
267
        begin
268
        for (int index=7;index>=0;index--)
269
          begin
270
          push_bit_serin(data[index]);
271
          end
272
        end
273
      default:
274
        begin
275
        $error("Undefined serial mode");
276
        end
277
    endcase
278
  endtask:push_byte_serin*/
279
 
280
 
281
  // Through the following task, UART BFM will catpure data byte from serial out-
282
  // put port based on the configured start_bit field. BFM will assign testbench
283
  // error in case of un-configured start_bit field.
284
  task catch_field_serout (output byte data);
285
    bit end_bit;
286
 
287
    // wait start bit
288
    wait(ser_out == 1'b0);
289
    case(start_bit)
290
      `lsb_first:
291
        begin
292
        for (int index=0;index<8;index++)
293
          begin
294
          catch_bit_serout(data[index]);
295
          end
296
        end
297
      `msb_first:
298
        begin
299
        for (int index=7;index>=0;index--)
300
          begin
301
          catch_bit_serout(data[index]);
302
          end
303
        end
304
      default:
305
        begin
306
        $error("Undefined serial mode");
307
        end
308
    endcase
309
    catch_bit_serout(end_bit);
310
    if(end_bit != 1'b1)
311
      begin
312
      $error("at time =%0t ,, the first end bit = %0b",$time,end_bit);
313
      $stop;
314
      end
315
    if (num_stop_bits == 2)
316
      begin
317
      catch_bit_serout(end_bit);
318
      if(end_bit != 1'b1)
319
        begin
320
        $error("at time =%0t ,, the first end bit = %0b",$time,end_bit);
321
        $stop;
322
        end
323
      end
324
  endtask:catch_field_serout
325
 
326
  // Through the following task, UART BFM will catpure data byte from serial out-
327
  // put port based on the configured start_bit field. BFM will assign testbench
328
  // error in case of un-configured start_bit field.
329
  task catch_field_serin (output byte data);
330
    bit end_bit;
331
 
332
    // wait start bit
333
 
334
    wait (ser_in == 1'b0);
335
    #(`buad_clk_period/2);
336
    case(start_bit)
337
      `lsb_first:
338
        begin
339
        for (int index=0;index<8;index++)
340
          begin
341
          catch_bit_serin(data[index]);
342
          end
343
        end
344
      `msb_first:
345
        begin
346
        for (int index=7;index>=0;index--)
347
          begin
348
          catch_bit_serin(data[index]);
349
          end
350
        end
351
      default:
352
        begin
353
        $error("Undefined serial mode");
354
        end
355
    endcase
356
    catch_bit_serin(end_bit);
357
    if(end_bit != 1'b1)
358
      begin
359
      $error("at time =%0t ,, the first end bit = %0b",$time,end_bit);
360
      $stop;
361
      end
362
    if (num_stop_bits == 2)
363
      begin
364
      catch_bit_serin(end_bit);
365
      if(end_bit != 1'b1)
366
        begin
367
        $error("at time =%0t ,, the first end bit = %0b",$time,end_bit);
368
        $stop;
369
        end
370
      end
371
  endtask:catch_field_serin
372
 
373
 
374
  // Through the following function, the byte is reversed in the manner that the
375
  // byte is merrored
376
  function byte reverse_byte (byte data);
377
    byte tmp;
378
    for (int index=0;index<8;index++)
379
      begin
380
      tmp[index] = data[7-index];
381
      end
382
    return tmp;
383
  endfunction:reverse_byte
384
 
385
//-----------------------------------------------
386
//
387
//   UART Routines
388
//
389
//-----------------------------------------------
390
 
391
  // Through the following method, UART BFM will initialize write request in UART
392
  // text mode. This task is accomplished with known of either the request chara-
393
  // cter is capital or small, and also the used white space type is either single
394
  // space or tab, and finally the end of line mechanism.
395
  task write_text_mode(input int alph,
396
                       input int scp_type1,
397
                       input byte space_wrong1,
398
                       input int scp_type2,
399
                       input byte space_wrong2,
400
                       input int eol,
401
                       input byte eol_wrong,
402
                       input bit [`size-1:0] address,
403
                       input byte data);
404
    // Write procedures
405
      // First Field
406
      if (alph == `small_let)
407
        begin
408
        push_field_serout(`w);
409
        end
410
      else if (alph == `capital_let)
411
        begin
412
        push_field_serout(`W);
413
        end
414
      else
415
        $error("Testbench error .. No capitar or small letter is choosed");
416
 
417
      // Second Field
418
      if (scp_type1 == `single_space)
419
        begin
420
        push_field_serout(`space);
421
        end
422
      else if (scp_type1 == `tab_space)
423
        begin
424
        push_field_serout(`tab);
425
        end
426
      else if (scp_type1 == `space_wrong)
427
        begin
428
        push_field_serout(space_wrong1);
429
        end
430
      else
431
        $error("Testbench error .. No single space or multiple space is choosed");
432
 
433
      // third field
434
      case (data_rep)
435
        `binary_rep:
436
          begin
437
          push_field_serout(data);
438
          end
439
        `ascii_rep:
440
          begin
441
          push_field_serout(bin_asci_conv(data[7:4]));
442
          push_field_serout(bin_asci_conv(data[3:0]));
443
          end
444
        default:$error("undefined data representation");
445
      endcase
446
 
447
      // forth field
448
      if (scp_type2 == `single_space)
449
        begin
450
        push_field_serout(`space);
451
        end
452
      else if (scp_type2 == `tab_space)
453
        begin
454
        push_field_serout(`tab);
455
        end
456
      else if (scp_type2 == `space_wrong)
457
        begin
458
        push_field_serout(space_wrong2);
459
        end
460
      else
461
        $error("Testbench error .. No single or multiple space is choosed");
462
      // fivth field
463
      case (data_rep)
464
        `binary_rep:
465
          begin
466
          push_field_serout(address[15:08]);
467
          push_field_serout(address[07:00]);
468
          end
469
        `ascii_rep:
470
          begin
471
          push_field_serout(bin_asci_conv(address[15:12]));
472
          push_field_serout(bin_asci_conv(address[11:08]));
473
          push_field_serout(bin_asci_conv(address[07:04]));
474
          push_field_serout(bin_asci_conv(address[03:00]));
475
          end
476
        default:$error("undefined data representation");
477
      endcase
478
 
479
      // sixth Field
480
      if (eol == `cr_eol)
481
        begin
482
        push_field_serout(`CR);
483
        end
484
      else if (eol == `lf_eol)
485
        begin
486
        push_field_serout(`LF);
487
        end
488
      else if (eol == `eol_wrong)
489
        begin
490
        push_field_serout(eol_wrong);
491
        end
492
      else
493
        $error("Testbench error .. either CR or LF isn't choosed as eol");
494
  endtask:write_text_mode
495
 
496
  // Through the following method, UART BFM will initialize read request in UART
497
  // text mode and receive the response as defined in UART specification standard
498
  // This task is accomplished with known of either the request character is cap-
499
  // ital or small, and also the used white space type is either single space or
500
  // tab, and finally the end of line mechanism. This methed includes two main se-
501
  // ctions; the first one includes the four successive fields of defined read re-
502
  // quest. And the another one includes the response.
503
  task read_text_mode (input int alph,
504
                       input int scp_type,
505
                       input byte space_wrong,
506
                       input int eol,
507
                       input byte eol_wrong,
508
                       input bit [`size-1:0] address);
509
    byte data;
510
    byte temp;
511
    byte char1,char2;
512
    bit miss;
513
    time prope1;
514
    // Read Request
515
      // First Field
516
      if (alph == `small_let)
517
        begin
518
        push_field_serout(`r);
519
        end
520
      else if (alph == `capital_let)
521
        begin
522
        push_field_serout(`R);
523
        end
524
      else
525
        $error("Testbench error .. No capitar or small letter is choosed");
526
      // Second Field
527
      if (scp_type == `single_space)
528
        begin
529
        push_field_serout(`space);
530
        end
531
      else if (scp_type == `tab_space)
532
        begin
533
        push_field_serout(`tab);
534
        end
535
      else if (scp_type == `space_wrong)
536
        begin
537
        push_field_serout(space_wrong);
538
        end
539
      else
540
        $error("Testbench error .. No single or multiple white space is choosed");
541
 
542
      // Third Field
543
      case (data_rep)
544
        `binary_rep:
545
          begin
546
          push_field_serout(address[15:08]);
547
          push_field_serout(address[07:00]);
548
          end
549
        `ascii_rep:
550
          begin
551
          push_field_serout(bin_asci_conv(address[15:12]));
552
          push_field_serout(bin_asci_conv(address[11:08]));
553
          push_field_serout(bin_asci_conv(address[07:04]));
554
          push_field_serout(bin_asci_conv(address[03:00]));
555
          end
556
        default:$error("undefined data representation");
557
      endcase
558
 
559
      // Forth Field
560
      if (eol == `cr_eol)
561
        begin
562
        push_field_serout(`CR);
563
        end
564
      else if (eol == `lf_eol)
565
        begin
566
        push_field_serout(`LF);
567
        end
568
      else if (eol == `eol_wrong)
569
        begin
570
        push_field_serout(eol_wrong);
571
        end
572
      else
573
        $error("Testbench error .. No CR or LF is choosed");
574
 
575
      miss = 1'b0;
576
      prope1 = $time;
577
      fork
578
        begin: miss_response_thread
579
        while (($time-prope1)
580
          begin
581
          #1;
582
          end
583
        disable capture_response_thread;
584
        miss = 1'b1;
585
        end
586
 
587
        begin: capture_response_thread
588
        wait(ser_in == 1'b0);
589
        disable miss_response_thread;
590
        end
591
      join
592
      // Capture Response
593
      if (miss == 1'b0)
594
        begin
595
        case (data_rep)
596
          `binary_rep:
597
            begin
598
            catch_field_serin(data);
599
            end
600
          `ascii_rep:
601
            begin
602
            catch_field_serin(temp);
603
            data[7:4] = asci_bin_conv(temp);
604
            catch_field_serin(temp);
605
            data[3:0] = asci_bin_conv(temp);
606
            end
607
          default:
608
            begin
609
            $error("Undefined data representation");
610
            $stop;
611
            end
612
        endcase
613
        catch_field_serin(char1);
614
        catch_field_serin(char2);
615
        if (char1 != `CR || char2 != `LF)
616
          begin
617
          $error("EOL is refuesed");
618
          end
619
        end
620
  endtask:read_text_mode
621
 
622
  // Write bytes of data in command mode
623
  task automatic write_binary_mode (input int reqack,
624
                                    input int reqinc,
625
                                    input int unsigned data_length,
626
                                    input bit [`size-1:0] address,
627
                                    ref byte data []);
628
    byte tmp;
629
    int unsigned index;
630
 
631
    // first byte : binary prefix
632
    push_field_serout(`bin_prfx);
633
 
634
    // second byte : command
635
    tmp[5:4] = 2'b10;
636
    if(reqinc==`_yes)
637
      begin
638
      tmp[1] = 1'b0;
639
      end
640
    else if (reqinc == `_no)
641
      begin
642
      tmp[1] = 1'b1;
643
      end
644
    else
645
      begin
646
      $error("undefined increment request");
647
      end
648
 
649
    if(reqack==`_yes)
650
      begin
651
      tmp[0] = 1'b1;
652
      end
653
    else if (reqack == `_no)
654
      begin
655
      tmp[0] = 1'b0;
656
      end
657
    else
658
      begin
659
      $error("undefined acknowledge request");
660
      end
661
    push_field_serout(tmp);
662
 
663
    // Third byte : higher byte of address
664
    push_field_serout(address[15:08]);
665
 
666
    // Forth byte : Lower byte of address
667
    push_field_serout(address[07:00]);
668
 
669
    // Fifth byte : data length
670
    push_field_serout(data_length);
671
 
672
    index = 0;
673
    while(index
674
      begin
675
      push_field_serout(data[index]);
676
      index++;
677
      end
678
 
679
    if (reqack == `_yes)
680
      begin
681
      catch_field_serin(tmp);
682
      if (tmp != `ACK)
683
        begin
684
        $error("The captured acknowledge isn't as unified character");
685
        end
686
      end
687
 
688
  endtask:write_binary_mode
689
 
690
  // Read bytes of data in command mode
691
  task automatic read_binary_mode  (input int reqack,
692
                                    input int reqinc,
693
                                    input int unsigned data_length,
694
                                    input bit [`size-1:0] address,
695
                                    ref byte data []);
696
    byte tmp;
697
    int unsigned index;
698
 
699
    // first byte : binary prefix
700
    push_field_serout(`bin_prfx);
701
 
702
    // second byte : command
703
    tmp[5:4] = 2'b01;
704
    if(reqinc==`_yes)
705
      begin
706
      tmp[1] = 1'b0;
707
      end
708
    else if (reqinc == `_no)
709
      begin
710
      tmp[1] = 1'b1;
711
      end
712
    else
713
      begin
714
      $error("undefined increment request");
715
      end
716
 
717
    if(reqack==`_yes)
718
      begin
719
      tmp[0] = 1'b1;
720
      end
721
    else if (reqack == `_no)
722
      begin
723
      tmp[0] = 1'b0;
724
      end
725
    else
726
      begin
727
      $error("undefined acknowledge request");
728
      end
729
    push_field_serout(tmp);
730
 
731
    // Third byte : higher byte of address
732
    push_field_serout(address[15:08]);
733
 
734
    // Forth byte : Lower byte of address
735
    push_field_serout(address[07:00]);
736
 
737
    // Fifth byte : data length
738
    push_field_serout(data_length);
739
 
740
    index = 0;
741
    while(index
742
      begin
743
      catch_field_serin(data[index]);
744
      index++;
745
      end
746
 
747
    if (reqack == `_yes)
748
      begin
749
      catch_field_serin(tmp);
750
      if (tmp != `ACK)
751
        begin
752
        $error("The captured acknowledge isn't as unified character");
753
        end
754
      end
755
  endtask:read_binary_mode
756
 
757
  // No Operation Command
758
  task nop_command (input int reqack,
759
                    input int reqinc);
760
    byte tmp;
761
    int unsigned index;
762
    // first byte : prefix
763
    push_field_serout(`bin_prfx);
764
 
765
    // second byte :  command
766
    tmp[5:4] = 2'b00;
767
    if(reqinc==`_yes)
768
      begin
769
      tmp[1] = 1'b0;
770
      end
771
    else if (reqinc == `_no)
772
      begin
773
      tmp[1] = 1'b1;
774
      end
775
    else
776
      begin
777
      $error("undefined increment request");
778
      end
779
 
780
    if(reqack==`_yes)
781
      begin
782
      tmp[0] = 1'b1;
783
      end
784
    else if (reqack == `_no)
785
      begin
786
      tmp[0] = 1'b0;
787
      end
788
    else
789
      begin
790
      $error("undefined acknowledge request");
791
      end
792
    push_field_serout(tmp);
793
 
794
    if(reqack==`_yes)
795
      begin
796
      catch_field_serin(tmp);
797
      if(tmp!=`ACK)
798
        begin
799
        $error("Undefined acknowledge character");
800
        end
801
      end
802
  endtask:nop_command
803
 
804
  task automatic wrong_command (input int reqack,
805
                                input int reqinc,
806
                                input byte wrong_prefix,
807
                                input int unsigned data_length,
808
                                input bit [`size-1:0] address,
809
                                ref byte data []);
810
    byte tmp;
811
    int index;
812
    time prope1;
813
 
814
    push_field_serout(wrong_prefix);
815
    tmp[5:4] = 2'b01;
816
    if(reqinc==`_yes)
817
      begin
818
      tmp[1] = 1'b0;
819
      end
820
    else if (reqinc == `_no)
821
      begin
822
      tmp[1] = 1'b1;
823
      end
824
    else
825
      begin
826
      $error("undefined increment request");
827
      end
828
 
829
    if(reqack==`_yes)
830
      begin
831
      tmp[0] = 1'b1;
832
      end
833
    else if (reqack == `_no)
834
      begin
835
      tmp[0] = 1'b0;
836
      end
837
    else
838
      begin
839
      $error("undefined acknowledge request");
840
      end
841
    push_field_serout(tmp);
842
 
843
    // Third byte : higher byte of address
844
    push_field_serout(address[15:08]);
845
 
846
    // Forth byte : Lower byte of address
847
    push_field_serout(address[07:00]);
848
 
849
    // Fifth byte : data length
850
    push_field_serout(data_length);
851
 
852
    fork : response_thread
853
      begin
854
      index = 0;
855
 
856
      while(index
857
        begin
858
        catch_field_serin(data[index]);
859
        index++;
860
        end
861
 
862
      if (reqack == `_yes)
863
        begin
864
        catch_field_serin(tmp);
865
        if (tmp != `ACK)
866
          begin
867
          $error("The captured acknowledge isn't as unified character");
868
          end
869
        end
870
      end
871
 
872
      begin
873
      prope1=$time;
874
      while (($time-prope1)< response_time)
875
        begin
876
        #1;
877
        end
878
      disable response_thread;
879
      end
880
    join
881
 
882
  endtask:wrong_command
883
 
884
 
885
  // Wait idle time
886
  task wait_idle_time (time idle);
887
    force_sout(1'b1);
888
    #idle;
889
  endtask: wait_idle_time
890
 
891
//-----------------------------------------------
892
//
893
//    MONITOR ROUTINES
894
//
895
//-----------------------------------------------
896
 
897
  // This routine is used to wait for transaction start
898
  task wait_event();
899
    wait (start_trans);
900
  endtask:wait_event
901
 
902
  task capture_text_read_command (output int _spacetype,
903
                                  output int space_wrong,
904
                                  output int _eoltype,
905
                                  output int eol_wrong,
906
                                  output bit [`size-1:0] address,
907
                                  output byte data);
908
    byte read_byte;
909
    catch_field_serout(read_byte);
910
    case (read_byte)
911
      `space:
912
        begin
913
        _spacetype  = `single_space;
914
        end
915
      `tab:
916
        begin
917
        _spacetype  = `tab_space;
918
        end
919
      default:
920
        begin
921
        _spacetype  = `space_wrong;
922
        space_wrong = read_byte;
923
        end
924
    endcase
925
 
926
    case(data_rep)
927
      `binary_rep:
928
        begin
929
        catch_field_serout(read_byte);
930
        address[15:08] = read_byte;
931
        catch_field_serout(read_byte);
932
        address[07:00] = read_byte;
933
        end
934
      `ascii_rep:
935
        begin
936
        catch_field_serout(read_byte);
937
        address[15:12] = asci_bin_conv(read_byte);
938
        catch_field_serout(read_byte);
939
        address[11:08] = asci_bin_conv(read_byte);
940
        catch_field_serout(read_byte);
941
        address[07:04] = asci_bin_conv(read_byte);
942
        catch_field_serout(read_byte);
943
        address[03:00] = asci_bin_conv(read_byte);
944
        end
945
      default:
946
        begin
947
        $error("undefined data representation");
948
        $stop;
949
        end
950
    endcase
951
 
952
    catch_field_serout(read_byte);
953
    case(read_byte)
954
      `CR:
955
        begin
956
        _eoltype    = `cr_eol;
957
        end
958
      `LF:
959
        begin
960
        _eoltype    = `lf_eol;
961
        end
962
      default:
963
        begin
964
        _eoltype    = `eol_wrong;
965
        eol_wrong   = read_byte;
966
        end
967
    endcase
968
 
969
    case(data_rep)
970
      `binary_rep:
971
        begin
972
        catch_field_serin(read_byte);
973
        data = read_byte;
974
        end
975
      `ascii_rep:
976
        begin
977
        catch_field_serin(read_byte);
978
        data[7:4] = bin_asci_conv(read_byte);
979
        catch_field_serin(read_byte);
980
        data[3:0] = bin_asci_conv(read_byte);
981
        end
982
      default:
983
        begin
984
        $error("undefined data representation");
985
        $stop;
986
        end
987
    endcase
988
 
989
    catch_field_serin(read_byte);
990
    if (read_byte == `CR)
991
      begin
992
      catch_field_serin(read_byte);
993
      if (read_byte != `LF)
994
        begin
995
        $error("the catpured byte isn't LF");
996
        end
997
      end
998
    else if (read_byte == `LF)
999
      begin
1000
      $error("The captured byte is LF instead of CR");
1001
      end
1002
    else
1003
      begin
1004
      $error("Fatal Error : final character in read request isn't CR and LF");
1005
      end
1006
  endtask:capture_text_read_command
1007
 
1008
 
1009
  task capture_text_write_command ( output int _spacetype1,
1010
                                    output int space_wrong1,
1011
                                    output int _spacetype2,
1012
                                    output int space_wrong2,
1013
                                    output int _eoltype,
1014
                                    output int eol_wrong,
1015
                                    output bit [`size-1:0] address,
1016
                                    output byte data);
1017
 
1018
    byte read_byte;
1019
    catch_field_serout(read_byte);
1020
    case (read_byte)
1021
      `space:
1022
        begin
1023
        _spacetype1  = `single_space;
1024
        end
1025
      `tab:
1026
        begin
1027
        _spacetype1  = `tab_space;
1028
        end
1029
      default:
1030
        begin
1031
        _spacetype1  = `space_wrong;
1032
        space_wrong1 = read_byte;
1033
        end
1034
    endcase
1035
 
1036
    case(data_rep)
1037
      `binary_rep:
1038
        begin
1039
        catch_field_serout(read_byte);
1040
        data = read_byte;
1041
        end
1042
      `ascii_rep:
1043
        begin
1044
        catch_field_serout(read_byte);
1045
        data[7:4] = asci_bin_conv(read_byte);
1046
        catch_field_serout(read_byte);
1047
        data[3:0] = asci_bin_conv(read_byte);
1048
        end
1049
      default:
1050
        begin
1051
        $error("undefined data representation");
1052
        $stop;
1053
        end
1054
    endcase
1055
 
1056
    catch_field_serout(read_byte);
1057
    case (read_byte)
1058
      `space:
1059
        begin
1060
        _spacetype2  = `single_space;
1061
        end
1062
      `tab:
1063
        begin
1064
        _spacetype2  = `tab_space;
1065
        end
1066
      default:
1067
        begin
1068
        _spacetype2  = `space_wrong;
1069
        space_wrong2 = read_byte;
1070
        end
1071
    endcase
1072
 
1073
    case(data_rep)
1074
      `binary_rep:
1075
        begin
1076
        catch_field_serout(read_byte);
1077
        address[15:08] = read_byte;
1078
        catch_field_serout(read_byte);
1079
        address[07:00] = read_byte;
1080
        end
1081
      `ascii_rep:
1082
        begin
1083
        catch_field_serout(read_byte);
1084
        address[15:12] = asci_bin_conv(read_byte);
1085
        catch_field_serout(read_byte);
1086
        address[11:08] = asci_bin_conv(read_byte);
1087
        catch_field_serout(read_byte);
1088
        address[07:04] = asci_bin_conv(read_byte);
1089
        catch_field_serout(read_byte);
1090
        address[03:00] = asci_bin_conv(read_byte);
1091
        end
1092
      default:
1093
        begin
1094
        $error("Undefined data representation");
1095
        $stop;
1096
        end
1097
    endcase
1098
 
1099
    catch_field_serout(read_byte);
1100
    case(read_byte)
1101
      `CR:
1102
        begin
1103
        _eoltype    = `cr_eol;
1104
        end
1105
      `LF:
1106
        begin
1107
        _eoltype    = `lf_eol;
1108
        end
1109
      default:
1110
        begin
1111
        _eoltype    = `eol_wrong;
1112
        eol_wrong   = read_byte;
1113
        end
1114
    endcase
1115
  endtask: capture_text_write_command
1116
 
1117
  task automatic capture_binary_command (output int _command,
1118
                                         output int _reqack,
1119
                                         output int _reqinc,
1120
                                         output bit [15:0] address,
1121
                                         output int data_length,
1122
                                         ref byte data [ ],
1123
                                         output byte acknowledge);
1124
    byte read_byte;
1125
    int index;
1126
    byte que [$];
1127
    // first byte
1128
    catch_field_serout(read_byte);
1129
    case(read_byte[5:4])
1130
      `nop_ctrl:
1131
        begin
1132
        _command = `nop_comm;
1133
        end
1134
      `read_ctrl:
1135
        begin
1136
        _command = `read_comm;
1137
        end
1138
      `write_ctrl:
1139
        begin
1140
        _command = `write_comm;
1141
        end
1142
      default:
1143
        begin
1144
        $error("undefined 2-bits command");
1145
        end
1146
    endcase
1147
    if (read_byte[1])
1148
      begin
1149
      _reqinc = `_no;
1150
      end
1151
    else
1152
      begin
1153
      _reqinc = `_yes;
1154
      end
1155
    if (read_byte[0])
1156
      begin
1157
      _reqack = `_yes;
1158
      end
1159
    else
1160
      begin
1161
      _reqack = `_no;
1162
      end
1163
 
1164
    catch_field_serout(read_byte);
1165
    address[15:08] = read_byte;
1166
 
1167
    catch_field_serout(read_byte);
1168
    address[07:00] = read_byte;
1169
 
1170
    catch_field_serout(read_byte);
1171
    if (read_byte == 8'h00)
1172
      begin
1173
      data_length = 256;
1174
      end
1175
    else
1176
      begin
1177
      data_length = read_byte;
1178
      end
1179
 
1180
    que.delete();
1181
    case (_command)
1182
      `read_comm:
1183
        begin
1184
        index=0;
1185
        while(index < data_length)
1186
          begin
1187
          catch_field_serin(read_byte);
1188
          que.push_back(read_byte);
1189
          index++;
1190
          end
1191
        end
1192
      `write_comm:
1193
        begin
1194
        index=0;
1195
        while(index < data_length)
1196
          begin
1197
          catch_field_serout(read_byte);
1198
          que.push_back(read_byte);
1199
          index++;
1200
          end
1201
        end
1202
      default:
1203
        begin
1204
        $error("Undefined Command");
1205
        end
1206
    endcase
1207
 
1208
    data = new [que.size];
1209
    data = que;
1210
    catch_field_serin(acknowledge);
1211
  endtask:capture_binary_command
1212
 
1213
  // General Capture Command Routine
1214
  task automatic capture_command (output int command_type,
1215
                                  output int _command,
1216
                                  output int _chartype,
1217
                                  output int _spacetype1,
1218
                                  output int space_wrong1,
1219
                                  output int _spacetype2,
1220
                                  output int space_wrong2,
1221
                                  output int _eoltype,
1222
                                  output int eol_wrong,
1223
                                  output bit [`size-1:0] address,
1224
                                  ref byte data [],
1225
                                  output byte acknowledge,
1226
                                  output int unsigned data_length,
1227
                                  output int _reqack,
1228
                                  output int _reqinc);
1229
 
1230
    byte read_byte;
1231
    catch_field_serout(read_byte);
1232
    case (read_byte)
1233
      `w:
1234
        begin
1235
        command_type  = `text_mode;
1236
        _command      = `write_comm;
1237
        _chartype     = `small_let;
1238
        data = new [1];
1239
        capture_text_write_command(_spacetype1,
1240
                                   space_wrong1,
1241
                                   _spacetype2,
1242
                                   space_wrong2,
1243
                                   _eoltype,
1244
                                   eol_wrong,
1245
                                   address,
1246
                                   data[0]);
1247
        end
1248
      `W:
1249
        begin
1250
        command_type  = `text_mode;
1251
        _command      = `write_comm;
1252
        _chartype     = `capital_let;
1253
        data = new[1];
1254
        capture_text_write_command(_spacetype1,
1255
                                   space_wrong1,
1256
                                   _spacetype2,
1257
                                   space_wrong2,
1258
                                   _eoltype,
1259
                                   eol_wrong,
1260
                                   address,
1261
                                   data[0]);
1262
        end
1263
      `r:
1264
        begin
1265
        command_type  = `text_mode;
1266
        _command      = `read_comm;
1267
        _chartype     = `small_let;
1268
        data = new[1];
1269
        capture_text_read_command(_spacetype1,
1270
                                  space_wrong1,
1271
                                  _eoltype,
1272
                                  eol_wrong,
1273
                                  address,
1274
                                  data[0]);
1275
        end
1276
      `R:
1277
        begin
1278
        command_type  = `text_mode;
1279
        _command      = `read_comm;
1280
        _chartype     = `capital_let;
1281
        data = new [1];
1282
        capture_text_read_command(_spacetype1,
1283
                                  space_wrong1,
1284
                                  _eoltype,
1285
                                  eol_wrong,
1286
                                  address,
1287
                                  data[0]);
1288
        end
1289
      `bin_prfx:
1290
        begin
1291
        command_type  = `binary_mode;
1292
        capture_binary_command(_command,
1293
                               _reqack,
1294
                               _reqinc,
1295
                               address,
1296
                               data_length,
1297
                               data,
1298
                               acknowledge);
1299
        end
1300
      default:
1301
        begin
1302
        command_type  = `wrong_mode;
1303
        _command      = `wrong_comm;
1304
        end
1305
    endcase
1306
  endtask:capture_command
1307
endinterface:uart_interface

powered by: WebSVN 2.1.0

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