source: ResearchApps/PHY/MIMO_OFDM/fec_decoder_rest.v

Last change on this file was 1729, checked in by murphpo, 12 years ago

Adding Sysgen 13.4 version of coded OFDM PHY; includes temporary fix for crash during simulation (bbReplace.m)

File size: 115.5 KB
Line 
1
2//********************************************************************************
3// File:    decoder_system.v
4// Author:  Yang Sun (ysun@rice.edu)
5// Birth:   $ 11/25/07
6// Des:     decoder system
7// History: $ 11/25/07, Uncoded decoder
8//          $ 12/1/07, Added Viterbi decoder (VBD)
9//          $ 09/27/08: Added dec-puncture
10//********************************************************************************
11module decoder_system (
12        clk             ,   // I, clock
13        nrst            ,   // I, n reset
14        start           ,   // I, start pulse
15        in_dec          ,
16        coding_en       ,
17        zero_tail       ,
18        sym_start       ,   // I, sym start
19        hdr_mod_level   ,   // I, header modulation level
20        vin             ,   // I, data valid input
21        llr_a           ,   // I, LLR bit a
22        llr_b           ,   // I, LLR bit b
23        llr_buf_empty   ,   // I, LLR buffer empty
24        unc_buf_rd      ,   // O, LLR buffer read by unc
25        vout            ,   // O, data valid output
26        dout            ,   // O, byte data output
27        nbyte           ,   // O, num of bytes
28        byte_cnt        ,   // O, byte cnt
29        cc_rate         ,   // O, code rate
30        data_coded      ,   // O, coded system
31        in_fullrate         // O, in full rate
32        ) ;
33       
34input                   clk ;
35input                   start ;
36input                   in_dec ;
37input                   coding_en ;
38input                   zero_tail ;
39input                   sym_start ;
40input       [3:0]       hdr_mod_level ;
41input                   nrst ;
42input                   vin ;
43input       [3:0]       llr_a ;
44input       [3:0]       llr_b ;
45input                   llr_buf_empty ;
46                   
47output                  unc_buf_rd ;                   
48output                  vout ;
49output      [7:0]       dout ;
50
51output      [13:0]      nbyte ;
52output reg  [13:0]      byte_cnt ;
53output      [1:0]       cc_rate ;
54output                  data_coded ;
55output                  in_fullrate ;
56
57//==============================
58//Internal signal
59//==============================
60wire            hd_a ;
61wire            hd_b ;
62wire            unc_vout ;
63wire    [7:0]   unc_byte_out ;
64wire            unc_vin ;
65
66wire            ld_mod_type ;
67wire            ld_payload_lsb ;
68wire            ld_payload_msb ;
69reg             ld_payload ;
70wire            ld_rate ;
71reg             update_mod_type ;
72
73wire    [13:0]  num_payload_i ;
74reg     [13:0]  num_payload ;
75reg     [7:0]   num_payload_low ;
76reg     [7:0]   num_payload_high ;
77
78reg     [16:0]  bit_cnt ; 
79wire            input_done ;
80reg             input_done_s0 ;
81wire            pkt_end ;
82
83wire    [3:0]   vb_b1 ;
84wire    [3:0]   vb_b0 ;
85wire            vb_vin ;
86wire            vb_vout ;
87wire    [7:0]   vb_dout ;
88wire            vb_done ;
89
90wire    [7:0]   dec_byte ;
91wire            dec_val ;
92
93wire    [4:0]   descramb_addr ;
94wire    [7:0]   descramb_data ;
95
96reg     [3:0]   mod_type_i ;
97wire    [13:0]  input_byte_cnt ;
98
99reg     [3:0]   sym_cnt ;
100
101wire            vb_start ;
102wire            vb_end ;
103
104reg     [1:0]   cc_rate_i ;
105reg             in_baserate ;
106reg             data_coded_i ;
107wire            dout_sel ;
108
109//==============================
110// Main body of code
111//==============================
112assign nbyte = num_payload ;
113assign vout = dec_val ;
114assign dout = dec_byte ;
115
116assign data_coded = data_coded_i ;
117
118assign hd_a = llr_a [3] ;
119assign hd_b = llr_b [3] ;
120
121always @ (posedge clk or negedge nrst)
122  if (~nrst)
123    sym_cnt <= 0 ;
124  else if (start)
125    sym_cnt <= 0 ;
126  else if (sym_start)
127    sym_cnt <= (sym_cnt == 9 ? sym_cnt : sym_cnt +1) ;
128
129//?? assign unc_vin = in_fullrate & ~llr_buf_empty & ~data_coded ;
130
131assign unc_vin = coding_en ? (in_fullrate & ~llr_buf_empty & ~data_coded) : (in_dec & ~llr_buf_empty) ;
132assign unc_buf_rd =  coding_en ? (unc_vin & ~data_coded) : unc_vin ;
133//=====================================
134// uncodede decoder
135//=====================================
136unc_decoder unc_decoder (
137        .clk    (clk            ),   // clock
138        .nrst   (nrst           ),   // n reset       
139        .hd_a   (hd_a           ),   // hard decision of I
140        .hd_b   (hd_b           ),   // hard decision of Q
141        .start  (start          ),   // start decoding pulse
142        .vin    (unc_vin        ),   // valid input
143        .vout   (unc_vout       ),   // valid output
144        .dout   (unc_byte_out   )    // byte out
145        ) ;
146
147always @ (posedge clk or negedge nrst)
148  if (~nrst)
149    bit_cnt <= 0 ;
150  else if (start)
151    bit_cnt <= 0 ;
152  else if (vin)
153    bit_cnt <= bit_cnt +2 ;
154
155assign input_byte_cnt = bit_cnt [16:3] ;
156assign input_done = (bit_cnt [16:4] == (data_coded_i ? num_payload : 24)) ; 
157always @ (posedge clk)
158  input_done_s0 <= input_done ;
159assign pkt_end = input_done & ~input_done_s0 ;
160
161assign vb_vin = vin ;
162assign vb_b1 = llr_a ;
163assign vb_b0 = llr_b ;
164
165//======================================================
166// Viter decoder system
167//======================================================
168vb_decoder_top vb_decoder_top (
169        .clk            (clk            ),  //I
170        .nrst           (nrst           ),  //I
171        .packet_start   (vb_start       ),  //I
172        .zero_tail      (zero_tail      ),
173        .packet_end     (vb_end         ),  //I
174        .vin            (vb_vin         ),  //I
175        .llr_b1         (vb_b1          ),  //I
176        .llr_b0         (vb_b0          ),  //I
177        .vout           (vb_vout        ),  //O
178        .done           (vb_done        ),  //O
179        .dout_in_byte   (vb_dout        )   //O
180        ) ;
181
182
183// Select
184assign dout_sel = (in_baserate | data_coded) & coding_en ;
185
186assign dec_byte = dout_sel ? vb_dout : unc_byte_out ;
187assign dec_val =  dout_sel ? vb_vout : unc_vout ;
188
189assign descramb_addr = byte_cnt [4:0] ;
190//===================================
191// de-scrambler
192//===================================
193scrambler descrambler (
194        .addr       (descramb_addr  ),   // address 32
195        .din        (dec_byte       ),   // byte in
196        .dout       (descramb_data  )    // byte out
197        ) ;
198
199always @ (posedge clk or negedge nrst)
200  if (~nrst)
201    byte_cnt <= 0 ;
202  else if (start)
203    byte_cnt <= 0 ;
204  else if (dec_val)
205    byte_cnt <= byte_cnt +1 ;
206
207assign ld_mod_type = dec_val & (byte_cnt == 0) ;
208assign ld_payload_msb = byte_cnt == 2 ;
209assign ld_payload_lsb = byte_cnt == 3 ;
210assign ld_rate = byte_cnt == 1 ;
211
212always @*
213begin
214  update_mod_type = 1'b0 ; 
215  if(sym_start)
216  begin
217    if(coding_en)
218      case(hdr_mod_level)
219        4'd1: update_mod_type = (sym_cnt == 8) ;
220        4'd2: update_mod_type = (sym_cnt == 4) ;
221        4'd4: update_mod_type = (sym_cnt == 2) ;
222      endcase
223    else
224      case(hdr_mod_level)
225        4'd1: update_mod_type = (sym_cnt == 4) ;
226        4'd2: update_mod_type = (sym_cnt == 2) ;
227        4'd4: update_mod_type = (sym_cnt == 1) ;
228      endcase         
229  end
230end
231
232// Header is always not punctured
233assign cc_rate = coding_en ? (in_baserate ? 2'd0 : cc_rate_i) : 2'd0 ;
234
235always @ (posedge clk or negedge nrst)
236  if (~nrst)
237  begin
238    num_payload_low <= 64 ;
239    num_payload_high <= 0 ;
240    cc_rate_i <= 0 ;
241    data_coded_i <= 1'b1 ;
242  end
243  else if (start)
244  begin
245    num_payload_low <= 64 ;
246    num_payload_high <= 0 ;
247    cc_rate_i <= 0 ;
248    data_coded_i <= 1'b1 ;
249  end
250  else if (dec_val)
251  begin
252    if (ld_payload_lsb)
253      num_payload_low <= descramb_data ;
254    if (ld_payload_msb)
255      num_payload_high <= descramb_data ;
256    if (ld_rate)
257    begin
258      cc_rate_i <= descramb_data[1:0] ;
259      data_coded_i <= (descramb_data[1:0] != 2'b11) ;
260    end
261  end 
262
263always @ (posedge clk)
264  ld_payload <= ld_payload_lsb ;
265 
266assign num_payload_i = {num_payload_high [5:0], num_payload_low} ;
267always @ (posedge clk or negedge nrst)
268  if (~nrst)
269    num_payload <= 64 ;
270  else if (start)
271    num_payload <= 64 ;
272  else if (ld_payload)
273    num_payload <= num_payload_i ; 
274
275always @ (posedge clk or negedge nrst)
276  if (~nrst)
277    mod_type_i <= 2 ;
278  else if (start)
279    mod_type_i <= 2 ;
280  else if (ld_mod_type)
281    mod_type_i <= descramb_data [3:0] ;
282
283assign in_fullrate = ~in_baserate ;
284always @ (posedge clk or negedge nrst)
285  if (~nrst)
286    in_baserate <= 1'b1 ;
287  else if (start)
288    in_baserate <= 1'b1 ;
289  else if (update_mod_type)
290    in_baserate <= 1'b0 ;
291
292assign vb_start = start ;
293assign vb_end = pkt_end ;
294
295endmodule
296
297//**************************************************************
298// File:    depunc.v
299// Author:  Yang Sun (ysun@rice.edu)
300// Created: $ 2/4/07
301// Des:     viterbi decoder system
302// History: $ 02/04/07, Support puncture 2/3, 3/4
303//          $ 09/27/08: updated
304//**************************************************************
305module depunc (
306        clk         , 
307        nrst        , 
308        start       , 
309        dav         ,   // I, data available
310        din         , 
311        vout        , 
312        dout_a      , 
313        dout_b      , 
314        buf_rd      , 
315        rate            // 0 = 1/2, 1 = 2/3, 2 = 3/4
316        ) ;
317       
318parameter           SW = 4 ;        // soft input precision
319
320input               clk ;
321input               nrst ;
322input               start ;
323input               dav ;
324input   [SW*2 -1:0] din ;
325input   [1:0]       rate ;
326output              vout ;
327output  [SW -1:0]   dout_a ;
328output  [SW -1:0]   dout_b ;
329output              buf_rd ;
330
331//=================================================
332//Internal signal
333//=================================================
334reg     [1:0]               cnt ;
335
336wire                        rd ;
337wire                        rd_r12 ;
338wire                        rd_r34 ;
339wire                        rd_r23 ;
340wire                        r23_ext ;
341reg                         r23_ext_s0 ;
342wire                        r34_ext ;
343
344reg     [SW -1:0]           app_i_r34 ; 
345reg     [SW -1:0]           app_q_r34 ;
346reg     [SW -1:0]           app_i_r23 ; 
347reg     [SW -1:0]           app_q_r23 ;
348wire    [SW -1:0]           app_i_r12 ; 
349wire    [SW -1:0]           app_q_r12 ;
350wire    [SW -1:0]           app_i_mux ; 
351wire    [SW -1:0]           app_q_mux ;
352wire                        app_valid ;
353wire    [SW*2 -1:0]         iq_data_buf ;
354reg     [SW*2 -1:0]         iq_data_buf_lat ;
355
356//=================================================
357// Main RTL
358//=================================================
359assign buf_rd = rd ;
360assign dout_a = app_i_mux ;
361assign dout_b = app_q_mux ;
362assign vout = app_valid ;
363
364always @ (posedge clk or negedge nrst)
365  if (~nrst)
366    cnt <= 0 ;
367  else if (start)
368    cnt <= 0 ;
369  else if (dav)
370  begin
371    if (rate == 1)
372      cnt <= cnt + 1 ; 
373    else if (rate ==2)
374      cnt <= (cnt == 2 ? 0 : cnt +1) ;   
375  end
376
377// for rate = 1/2
378assign rd_r12 = dav ;
379
380// for rate = 2/3
381assign r23_ext = cnt == 3 ;
382always @ (posedge clk)
383  r23_ext_s0 <= r23_ext ;
384
385assign r23_ext_p = ~r23_ext_s0 & r23_ext ;
386assign rd_r23 = dav & ~r23_ext ; 
387
388// for rate = 3/4
389assign rd_r34 = dav & (cnt != 2) ; 
390assign r34_ext = dav & (cnt == 2) ;
391
392// mux
393assign rd = rate == 0 ? rd_r12 : rate == 1 ? rd_r23 : rd_r34 ;
394
395assign iq_data_buf = din ;
396always @ (posedge clk)
397  if (rd)
398    iq_data_buf_lat <= iq_data_buf ;
399
400// rate = 2/3
401always @*
402begin
403  app_i_r23 = 0 ;
404  app_q_r23 = 0 ;
405  case (cnt)
406  0: begin
407    app_i_r23 = iq_data_buf [SW*2 -1 : SW] ;
408    app_q_r23 = iq_data_buf [SW -1: 0] ;
409  end
410  1: begin
411    app_i_r23 = iq_data_buf [SW*2 -1 : SW] ;
412    app_q_r23 = 0 ;
413  end
414  2: begin
415    app_i_r23 = iq_data_buf_lat [SW -1: 0] ;
416    app_q_r23 = iq_data_buf [SW*2 -1 : SW] ;
417  end
418  3: begin
419    app_i_r23 = iq_data_buf_lat [SW -1: 0] ;
420    app_q_r23 = 0 ;
421  end 
422  endcase
423end
424
425// rate = 3/4
426always @*
427begin
428  app_i_r34 = 0 ;
429  app_q_r34 = 0 ;
430  case (cnt)
431  0: begin
432    app_i_r34 = iq_data_buf [SW*2 -1 : SW] ;
433    app_q_r34 = iq_data_buf [SW -1: 0] ;
434  end
435  1: begin
436    app_i_r34 = iq_data_buf [SW*2 -1 : SW] ;
437    app_q_r34 = 0 ;
438  end
439  2: begin
440    app_i_r34 = 0 ;
441    app_q_r34 = iq_data_buf_lat [SW -1: 0] ;
442  end
443  default: begin
444    app_i_r34 = 0 ;
445    app_q_r34 = 0 ;
446  end 
447  endcase
448end
449
450// rate = 1/2
451assign app_i_r12 = iq_data_buf [SW*2 -1 : SW] ;
452assign app_q_r12 = iq_data_buf [SW -1: 0] ;
453
454assign app_i_mux = rate == 0 ? app_i_r12 : rate == 1 ? app_i_r23 : app_i_r34 ;
455assign app_q_mux = rate == 0 ? app_q_r12 : rate == 1 ? app_q_r23 : app_q_r34 ;
456
457assign app_valid = rate == 0 ? rd_r12 : rate == 1 ? (rd_r23 | r23_ext_p) : (rd_r34 | r34_ext) ;
458
459endmodule
460
461
462
463//*********************************************************
464// File:    fifo_16x8.v
465// Author:  Y. Sun
466// Des:     16x8 FIFO
467//*********************************************************
468module fifo_16x8 (
469    clk         ,   // I, clock
470    nrst        ,   // I, async reset
471    reset       ,   // I, sync reset
472    wdata       ,   // I, write data
473    wr          ,   // I, write enable
474    rd          ,   // I, read enable
475    rdata       ,   // O, read data
476    empty       ,   // O, empty
477    full            // O, full
478    ) ;
479
480parameter           WIDTH = 8 ;
481parameter           DEPTH = 16 ;
482parameter           ADDR_BITS = 4 ;
483
484input               clk ;
485input               nrst ;
486input               reset ;
487input   [WIDTH-1:0] wdata ;
488input               wr ;
489input               rd ;
490output  [WIDTH-1:0] rdata ;
491output              empty ;
492output              full ;
493
494
495//============================
496// Internal signals
497//============================
498reg     [WIDTH-1:0]     mem [DEPTH-1:0] ;
499reg     [ADDR_BITS:0]   cnt ;
500reg     [ADDR_BITS-1:0] waddr ;
501reg     [ADDR_BITS-1:0] raddr ;
502
503//============================
504// Main RTL Code
505//============================
506assign empty = cnt == 0 ;
507assign full = cnt == DEPTH ;
508
509
510always @ (posedge clk or negedge nrst)
511  if(~nrst)
512    cnt <= 0 ; 
513  else
514  begin
515    if (reset)
516      cnt <= 0 ;
517    else if (wr & ~rd)
518      cnt <= cnt +1 ;
519    else if (rd & ~wr)
520      cnt <= cnt -1 ;
521  end
522
523always @ (posedge clk or negedge nrst)
524  if(~nrst)
525    waddr <= 0 ;
526  else
527  begin
528    if (reset)
529      waddr <= 0 ;
530    else if (wr)
531      waddr <= waddr == DEPTH -1 ? 0 : waddr +1 ;
532  end
533   
534always @ (posedge clk or negedge nrst)
535  if(~nrst)
536    raddr <= 0 ;
537  else
538  begin
539    if (reset)
540      raddr <= 0 ;
541    else if (rd)
542      raddr <= raddr == DEPTH -1 ? 0 : raddr +1 ;   
543  end 
544
545always @ (posedge clk)
546    if (wr)
547      mem [waddr] <= wdata ;
548
549assign rdata = mem [raddr] ;
550
551endmodule
552
553//**************************************************************
554// File:    llr_buffer.v
555// Author:  Yang Sun (ysun@rice.edu)
556// Created: $ 2/4/07
557// Des:     Buffer IQ data for viterbi decoder
558// History: $ 09/27/07 :updated
559//**************************************************************
560module llr_buffer (
561        clk     , 
562        nrst    , 
563        reset   , 
564        din     , 
565        dout    , 
566        wr      , 
567        rd      , 
568        empty   , 
569        full
570        ) ;
571
572parameter               DEPTH = 256 ;   // 128 slots
573parameter               ADDRW = 8 ; 
574parameter               SW = 4 ;       
575       
576input                   clk ;       // system clock
577input                   nrst ;
578input                   reset ;
579input   [SW*2 -1:0]     din ;       
580input                   wr ;
581input                   rd ;
582output  [SW*2 -1:0]     dout ;
583output                  empty ;
584output                  full ;
585
586//==============================================
587// Internal signal
588//==============================================
589reg     [SW*2 -1:0]     mem [DEPTH -1:0] ;
590wire    [SW*2 -1:0]     data ;
591
592reg     [ADDRW :0]      waddr ;
593reg     [ADDRW :0]      raddr ;
594wire    [ADDRW -1:0]    waddr_i ;
595wire    [ADDRW -1:0]    raddr_i ;
596
597//==============================================
598// Main RTL
599//==============================================
600assign dout = data ;
601assign waddr_i = waddr [ADDRW -1 : 0] ;
602assign raddr_i = raddr [ADDRW -1 : 0] ;
603assign empty = (waddr [ADDRW] ~^ raddr [ADDRW]) & (waddr_i == raddr_i) ;
604assign full = (waddr [ADDRW] ^ raddr [ADDRW]) & (waddr_i == raddr_i) ;
605
606always @ (posedge clk or negedge nrst)
607  if (~nrst)
608    waddr <= 0 ;
609  else if (reset)
610    waddr <= 0 ;
611  else if (wr)
612    waddr <= waddr + 1 ;
613
614always @ (posedge clk or negedge nrst)
615  if (~nrst)
616    raddr <= 0 ;
617  else if (reset)
618    raddr <= 0 ;
619  else if (rd)
620    raddr <= raddr + 1 ;
621
622always @ (posedge clk)
623  if (wr)
624    mem [waddr_i] <= din ;
625   
626assign data = mem [raddr_i] ;
627
628
629endmodule
630
631//**************************************************************
632// File:    max_metric_logic.v
633// Author:  Yang Sun (ysun@rice.edu)
634// Birth:   $ 1/14/07
635// Des:     The top level of viterbi decoder
636//          Take 5 bit soft value is [-16 : +15]
637//          K = 7. g0 = 133, g1 = 171
638// History: $ 1/14/07, Init coding
639//          $ 1/22/07, K = 5, Zero latency
640//          $ 12/1/07: Updated
641//**************************************************************
642module max_metric_logic (
643    a       ,   // I, metric a
644    b       ,   // I, metric b
645    c       ,   // I, metric c
646    d       ,   // I, metric d
647    sa      ,   // I, state a
648    sb      ,   // I, state b
649    sc      ,   // I, state c
650    sd      ,   // I, state d
651    state   ,   // O, state
652    max         // O, max metric
653    ) ;
654   
655parameter   M = 7 ;
656parameter   K = 7 ;
657
658input   [M -1:0]    a ;
659input   [M -1:0]    b ;
660input   [M -1:0]    c ;
661input   [M -1:0]    d ;
662
663input   [K -2:0]    sa ;
664input   [K -2:0]    sb ;
665input   [K -2:0]    sc ;
666input   [K -2:0]    sd ;
667
668output  [K -2:0]    state ;
669output  [M -1:0]    max ;
670
671//==================================
672//Internal signal
673//==================================
674
675reg     [K -2:0]    pos0 ;
676reg     [K -2:0]    pos1 ;
677reg     [M -1:0]    max0 ;
678reg     [M -1:0]    max1 ;
679reg     [M -1:0]    tmp ;
680reg     [K -2:0]    state_i ;
681reg     [M -1:0]    max_i ;
682
683//==================================
684// Main body of code
685//==================================
686assign state = state_i ;
687assign max = max_i ;
688
689always @*
690begin 
691  tmp = a-b ;
692  if(~tmp[M -1])
693  begin
694    max0 = a ;
695    pos0 = sa ;
696  end
697  else
698  begin
699    max0 = b ;
700    pos0 = sb ;
701  end
702
703  tmp = c-d ;
704  if(~tmp[M-1])
705  begin
706    max1 = c ;
707    pos1 = sc ;
708  end
709  else
710  begin
711    max1 = d ;
712    pos1 = sd ;
713  end
714
715  tmp = max0 - max1 ;
716  if (~tmp[M-1])
717  begin
718    max_i = max0 ;
719    state_i = pos0 ;
720  end
721  else
722  begin
723    state_i = pos1 ;
724    max_i = max1 ;
725  end
726end
727
728endmodule
729
730//**************************************************************
731// File:    max_metric
732// Author:  Yang Sun (ysun@rice.edu)
733// Birth:   $ 1/14/07
734// Des:     Find max metric
735//          K = 7. g0 = 133, g1 = 171
736// History: $ 1/14/07, Init coding
737//          $ 1/22/07, K = 5
738//          $ 2/23/07, 1 pipeline
739//          $ 12/1/07: updated
740//**************************************************************
741module max_metric (
742    clk     ,   // I, clock
743    a       ,   // I, metric a
744    b       ,   // I, metric b
745    c       ,   // I, metric c
746    d       ,   // I, metric d
747    sa      ,   // I, state a
748    sb      ,   // I, state b
749    sc      ,   // I, state c
750    sd      ,   // I, state d
751    state   ,   // O, state
752    max         // O, max metric
753    ) ;
754
755parameter   M = 7 ;
756parameter   K = 7 ;
757
758input               clk ;
759input   [M -1:0]    a ;
760input   [M -1:0]    b ;
761input   [M -1:0]    c ;
762input   [M -1:0]    d ;
763
764input   [K -2:0]    sa ;
765input   [K -2:0]    sb ;
766input   [K -2:0]    sc ;
767input   [K -2:0]    sd ;
768
769output  [K -2:0]    state ;
770output  [M -1:0]    max ;
771
772//=====================================
773//Internal signal
774//=====================================
775reg     [K -2:0]    pos0 ;
776reg     [K -2:0]    pos1 ;
777reg     [M -1:0]    max0 ;
778reg     [M -1:0]    max1 ;
779reg     [M -1:0]    tmp ;
780reg     [K -2:0]    state_i ;
781reg     [M -1:0]    max_i ;
782reg     [K -2:0]    state_reg ;
783reg     [M -1:0]    max_reg ;
784
785//=====================================
786// Main RTL code
787//=====================================
788assign state = state_reg ;
789assign max = max_reg ;
790
791always @ (posedge clk)
792begin
793  state_reg <= state_i ;
794  max_reg <= max_i ;
795end
796
797always @*
798begin 
799  tmp = a-b ;
800  if(~tmp[M -1])
801  begin
802    max0 = a ;
803    pos0 = sa ;
804  end
805  else
806  begin
807    max0 = b ;
808    pos0 = sb ;
809  end
810
811  tmp = c-d ;
812  if(~tmp[M-1])
813  begin
814    max1 = c ;
815    pos1 = sc ;
816  end
817  else
818  begin
819    max1 = d ;
820    pos1 = sd ;
821  end
822
823  tmp = max0 - max1 ;
824  if (~tmp[M-1])
825  begin
826    max_i = max0 ;
827    state_i = pos0 ;
828  end
829  else
830  begin
831    state_i = pos1 ;
832    max_i = max1 ;
833  end
834end
835
836endmodule
837
838//*********************************************************
839// File:    out_ctrl
840// Author:  Y. Sun
841// Des:     Convert the outputs into clk/2 domain
842//          I have to convert the output into clk/2 domain
843//          coz the downstream block takes clk/2 signals
844//*********************************************************
845module out_ctrl (
846    clk     ,   // I, clock
847    nrst    ,   // I, async reset
848    start   ,   // I, start
849    vin     ,   // I, write enable
850    din     ,   // I, write data
851    vout    ,   // O, read enable
852    dout    ,   // O, read data
853    idx_out     // O, out index
854    ) ;
855
856input           clk ;
857input           nrst ;
858input           start ;
859input           vin ;
860input   [7:0]   din ;
861output          vout ;
862output  [7:0]   dout ;
863output  [13:0]  idx_out ;
864
865reg             en ;
866wire            ff_empty ;
867wire            ff_rd ;
868wire    [7:0]   ff_rdata ;
869reg     [7:0]   data_lat ;
870reg             ff_rd_d ;
871reg             ff_rd_dd ;
872reg     [13:0]  cnt ;
873
874//============================
875// Main RTL Code
876//============================
877assign vout = ff_rd_d | ff_rd_dd ;
878assign dout = data_lat ;
879assign idx_out = cnt ;
880
881always @(posedge clk or negedge nrst)
882  if(~nrst)
883    en <= 1'b1 ;
884  else if (start)
885    en <= 1'b1 ;
886  else
887    en <= ~en ;
888
889assign ff_rd = ~ff_empty & en ;
890
891fifo_16x8 fifo_16x8 (
892    .clk    (clk        ),
893    .nrst   (nrst       ),
894    .reset  (start      ),
895    .wr     (vin        ),
896    .wdata  (din        ),
897    .rd     (ff_rd      ),
898    .rdata  (ff_rdata   ),
899    .empty  (ff_empty   ),
900    .full   (           )
901    ) ;
902
903always @(posedge clk or negedge nrst)
904  if(~nrst)
905    data_lat <= 0 ;
906  else if (ff_rd)
907    data_lat <= ff_rdata ;
908
909always @(posedge clk or negedge nrst)
910  if(~nrst)
911  begin
912    ff_rd_d <= 1'b0 ;
913    ff_rd_dd <= 1'b0 ;
914  end
915  else if (start)
916  begin
917    ff_rd_dd <= 1'b0 ;
918    ff_rd_d <= 1'b0 ;
919  end
920  else
921  begin
922    ff_rd_dd <= ff_rd_d ;
923    ff_rd_d <= ff_rd ;
924  end
925 
926always @(posedge clk or negedge nrst)
927  if(~nrst)
928    cnt <= 0 ;
929  else if (start)     
930    cnt <= 0 ;
931  else if (ff_rd_dd)
932    cnt <= cnt +1 ;
933 
934
935endmodule
936
937
938//********************************************************************************
939// Module:  round_data.v
940// Author:  Yang Sun (ysun@rice.edu)
941// Birth:   $ 4/29/07
942// Des:     Round with 1 latency
943// History: $ 4/29/07, Init coding
944//          $ 05/04/08: Fixed a bug
945//********************************************************************************
946module round_data (
947        clk, din, dout             
948        ) ;
949
950input                   clk ;
951input       [12:0]      din ;
952output reg  [3:0]       dout ;
953
954//=====================================
955//Internal signal
956//=====================================
957reg     [3:0]       tmp ;
958
959//=====================================
960// Main body of code
961//=====================================
962
963always @ (posedge clk)
964  dout <= tmp ;
965
966always @*
967begin
968  if ((~din[12]) && (din[11:10] != 2'b00))
969    tmp = 4'b0111 ;
970  else if (din[12] && (din[11:10] != 2'b11))
971    tmp = 4'b1000 ;
972  else if (din[12:10] == 3'b000)  // positive
973  begin
974    if ((din[9:7] != 3'b111) & din[6])
975      tmp = din[10:7] +1 ;
976    else
977      tmp = din[10:7] ;
978  end
979  else                  // negtive
980  begin
981    if ((din[9:7] != 3'b000) & din[6])
982      tmp = din[10:7] +1 ;
983    else
984      tmp = din[10:7] ;
985  end 
986end
987
988endmodule
989//********************************************************************************
990// File:    scrambler.v
991// Author:  Yang Sun (ysun@rice.edu)
992// Des:     byte scrambler
993// History: $ 12/1/07, Created
994//********************************************************************************
995`ifndef INCLUDE_SCRAMBLER
996`define INCLUDE_SCRAMBLER
997module scrambler (
998        addr        ,   // address 32
999        din         ,   // byte in
1000        dout            // byte out
1001        ) ;
1002
1003input   [4:0]   addr ;
1004input   [7:0]   din ;
1005output  [7:0]   dout ;
1006
1007//==============================
1008//Internal signal
1009//==============================
1010reg     [7:0]   scram_mask ;
1011
1012//==============================
1013// Main RTL code
1014//============================== 
1015assign dout = din ^ scram_mask ;
1016
1017always @*
1018  begin
1019    case (addr[4:0])
1020      0: scram_mask = 40 ;
1021      1: scram_mask = 198 ;
1022      2: scram_mask = 78 ; 
1023      3: scram_mask = 63 ; 
1024      4: scram_mask = 82 ; 
1025      5: scram_mask = 173 ;
1026      6: scram_mask = 102 ; 
1027      7: scram_mask = 245 ; 
1028      8: scram_mask = 48 ; 
1029      9: scram_mask = 111 ; 
1030      10: scram_mask = 172 ; 
1031      11: scram_mask = 115 ; 
1032      12: scram_mask = 147 ; 
1033      13: scram_mask = 230 ; 
1034      14: scram_mask = 216 ; 
1035      15: scram_mask = 93 ; 
1036      16: scram_mask = 72 ; 
1037      17: scram_mask = 65 ; 
1038      18: scram_mask = 62 ;
1039      19: scram_mask = 2 ;
1040      20: scram_mask = 205 ; 
1041      21: scram_mask = 242 ;
1042      22: scram_mask = 122 ;
1043      23: scram_mask = 90 ;
1044      24: scram_mask = 128 ;
1045      25: scram_mask = 83 ;
1046      26: scram_mask = 105 ;
1047      27: scram_mask = 97 ;
1048      28: scram_mask = 73 ; 
1049      29: scram_mask = 10 ;
1050      30: scram_mask = 5 ;
1051      31: scram_mask = 252 ;
1052      default: scram_mask = 40 ;
1053    endcase
1054  end
1055
1056endmodule
1057`endif
1058
1059//********************************************************************************
1060// Module:      Multiplier
1061// Author:      Yang Sun (ysun@rice.edu)
1062// Birth:       $ 4/29/07
1063// Description: Mult with 1 latency
1064// History:     $ 4/29/07, Init coding
1065//********************************************************************************
1066module smult (
1067        clk, a_in, b_in, p_out             
1068        ) ;
1069
1070parameter               AWID = 8 ;
1071parameter               BWID = 5 ;
1072parameter               PWID = AWID + BWID ;
1073       
1074input                   clk ;
1075input   [AWID -1:0]     a_in ;
1076input   [BWID -1:0]     b_in ;
1077output  [PWID -1:0]     p_out ;
1078
1079//***********************************************
1080//Internal signal
1081//***********************************************
1082wire    [AWID -1:0]     a_tmp ;
1083wire                    a_sgn ;
1084reg                     a_sgn_pipe ;
1085reg     [PWID -1:0]     p_tmp ;
1086wire    [PWID -1:0]     p_tmp2 ;
1087//***********************************************
1088// Main body of code
1089//***********************************************
1090assign p_out = p_tmp2 ;
1091assign a_tmp = a_sgn ? ~a_in +1 : a_in ;
1092assign a_sgn = a_in [AWID -1] ;
1093
1094always @ (posedge clk)
1095  p_tmp <= a_tmp * b_in ;
1096
1097always @ (posedge clk)
1098  a_sgn_pipe <= a_sgn ;
1099
1100
1101assign p_tmp2 = a_sgn_pipe ? ~p_tmp +1 : p_tmp ;
1102
1103endmodule
1104
1105//********************************************************************************
1106// File:    soft_demapper.v
1107// Author:  Yang Sun (ysun@rice.edu)
1108// Birth:   $ 4/29/07
1109// Des:     Produce soft LLR for I/Q
1110// History: $ 4/29/07: Init coding
1111//          $ 4/17/08: Added 16-QAM support
1112//********************************************************************************
1113module soft_demapper (
1114        clk             , 
1115        nrst            ,
1116        start           , 
1117        coding_en       ,
1118        mod_level       , 
1119        vin             , 
1120        xk_index        , 
1121        rx_i            , 
1122        rx_q            , 
1123        scale_qpsk      ,
1124        scale_16qam     ,
1125        soft_decoding   ,
1126        in_fullrate     ,
1127        sym_start       , 
1128        hdr_mod_level   ,
1129        vout            , 
1130        llr_a           , 
1131        llr_b           
1132        ) ;
1133       
1134input           clk ;
1135input           nrst ;
1136input           start ;
1137input           coding_en ;
1138input   [3:0]   mod_level ;     // 0=Invalid, 1=BPSK, 2=QPSK, 4=16-QAM
1139input           vin ;           // valid input
1140input   [5:0]   xk_index ;      // FFT index
1141input   [15:0]  rx_i ;          // FIX_16_15
1142input   [15:0]  rx_q ;          // FIX_16_15
1143input   [3:0]   scale_qpsk ;    // 0~15 sclae factor
1144input   [4:0]   scale_16qam ;   // 0~31 sclae factor
1145input           soft_decoding ;
1146input           in_fullrate ;   
1147
1148output          sym_start ;
1149output  [3:0]   hdr_mod_level ;
1150output          vout ;
1151output  [3:0]   llr_a ;
1152output  [3:0]   llr_b ; 
1153 
1154
1155//================================================
1156//Internal signal
1157//================================================
1158wire            mod_vld ;
1159reg             mod_vld_d ;
1160reg             mod_vld_dd ;
1161reg             mod_vld_ddd ;
1162wire            llr_vld ;
1163wire    [7:0]   recv_fix_8_7 ;
1164wire    [12:0]  soft_a_mult ;
1165wire    [12:0]  soft_b_mult ;
1166wire    [3:0]   soft_a_round ;
1167wire    [3:0]   soft_b_round ;
1168
1169reg     [7:0]   rx_q_d ;
1170reg     [7:0]   rx_i_d ;
1171
1172wire    [7:0]   soft_a ;
1173reg     [7:0]   soft_b ;
1174
1175reg     [3:0]   hard_a ;
1176reg     [3:0]   hard_b ;
1177
1178reg     [3:0]   hard_a_s0 ;
1179reg     [3:0]   hard_b_s0 ;
1180wire    [8:0]   abs_tmp ;
1181wire    [7:0]   abs_tmp_sat ;
1182
1183wire    [4:0]   llr_scale ;
1184
1185reg     [7:0]   input_cnt ;
1186reg             bpsk_sel ;
1187wire            mod_ld ;
1188reg     [3:0]   mod_level_lat ; 
1189wire    [3:0]   mod_type ; 
1190wire            bpsk ;
1191wire            qpsk ;
1192wire            qam16 ;
1193reg             first_sym ;
1194reg             first_sym_d ;
1195reg             first_sym_dd ;
1196reg     [3:0]   hdr_mod_level_lat ;
1197
1198//================================================
1199// Main body of code
1200//================================================
1201assign llr_a = (soft_decoding & coding_en) ? soft_a_round : hard_a_s0 ;
1202assign llr_b = (soft_decoding & coding_en) ? soft_b_round : hard_b_s0 ;
1203assign vout = llr_vld ;
1204assign sym_start = vin & (xk_index == 0) ;
1205
1206
1207assign hdr_mod_level = hdr_mod_level_lat ;
1208
1209// 16-QAM
1210always @ (posedge clk or negedge nrst)
1211  if(~nrst)
1212    rx_q_d <= 0 ;
1213  else if (vin)
1214    rx_q_d <= rx_q [15:8] ;
1215
1216// BPSK
1217always @ (posedge clk or negedge nrst)
1218  if(~nrst)
1219    rx_i_d <= 0 ;
1220  else if (mod_vld_d)
1221    rx_i_d <= rx_i [15:8] ;
1222
1223assign mod_ld = vin & (xk_index == 1) ;
1224always @ (posedge clk or negedge nrst)
1225  if(~nrst)
1226    mod_level_lat <= 2 ;
1227  else if (mod_ld)
1228    mod_level_lat <= mod_level ;
1229
1230assign mod_type = mod_ld ? mod_level : mod_level_lat ;
1231
1232always @ (posedge clk or negedge nrst)
1233  if(~nrst)
1234    first_sym <= 1'b0 ;
1235  else if (start)
1236    first_sym <= 1'b1 ;
1237  else if (sym_start)
1238    first_sym <= 1'b0 ;
1239
1240always @ (posedge clk or negedge nrst)
1241  if(~nrst)
1242    {first_sym_dd, first_sym_d} <= 2'd0 ;
1243  else
1244    {first_sym_dd, first_sym_d} <= {first_sym_d, first_sym} ;
1245
1246
1247always @ (posedge clk or negedge nrst)
1248  if(~nrst)
1249    hdr_mod_level_lat <= 2 ;
1250  else if (first_sym_dd & (mod_ld))
1251    hdr_mod_level_lat <= mod_level ;
1252
1253// Trancate 16_15 to 8_7
1254//assign recv_fix_8_7 = in_fullrate ? (mod_type == 1 ? rx_i_s0 : (vin ? rx_i [15:8] : rx_q_s0)) : rx_i[15:8] ;   
1255
1256assign bpsk = (mod_type == 1) ;
1257assign qpsk = (mod_type == 2) ;
1258assign qam16 =(mod_type == 4) ;
1259
1260assign recv_fix_8_7 = bpsk ? rx_i_d : (vin ? rx_i[15:8] : rx_q_d) ;
1261
1262//assign abs_tmp = recv_fix_8_7 [7] ? -recv_fix_8_7 : recv_fix_8_7 ;
1263assign abs_tmp = (recv_fix_8_7 ^{8{recv_fix_8_7[7]}}) + recv_fix_8_7[7] ;
1264assign abs_tmp_sat = abs_tmp[8] ? 8'hff : abs_tmp[7:0] ;
1265
1266assign soft_a = recv_fix_8_7 ;
1267assign llr_scale = (bpsk | qpsk) ? scale_qpsk : scale_16qam ;
1268
1269always @*
1270begin
1271  if (bpsk)                         // BPSK
1272    soft_b = rx_i[15:8] ;
1273  else if (qpsk)                    // QPSK
1274    soft_b = rx_q[15:8] ;
1275  else                              // 16-QAM
1276    soft_b = 8'b0100_0000 - abs_tmp_sat ;  // 0.5 - |Y|
1277end
1278 
1279always @ (posedge clk or negedge nrst)
1280  if(~nrst)
1281  begin
1282    hard_a <= 1'b0 ;
1283    hard_b <= 1'b0 ;   
1284  end
1285  else
1286  begin
1287    hard_a <= soft_a [7] ? 4'hf : 4'h1 ;
1288    hard_b <= soft_b [7] ? 4'hf : 4'h1 ;
1289  end 
1290
1291always @ (posedge clk or negedge nrst)
1292  if(~nrst)
1293  begin
1294    hard_a_s0 <= 1'b0 ;
1295    hard_b_s0 <= 1'b0 ; 
1296  end
1297  else
1298  begin 
1299    hard_a_s0 <= hard_a ;
1300    hard_b_s0 <= hard_b ; 
1301  end 
1302 
1303//================================
1304// signed MULT
1305//================================ 
1306smult smult_a (
1307        .clk        (clk        ),  //I
1308        .a_in       (soft_a     ),  //I
1309        .b_in       (llr_scale  ),  //I
1310        .p_out      (soft_a_mult)   //O             
1311        ) ;
1312
1313//================================
1314// signed MULT
1315//================================         
1316smult smult_b (
1317        .clk        (clk        ),  //I
1318        .a_in       (soft_b     ),  //I
1319        .b_in       (llr_scale  ),  //I
1320        .p_out      (soft_b_mult)   //O             
1321        ) ;
1322
1323//================================
1324// Round
1325//================================
1326round_data data_i (
1327        .clk    (clk            ),  //I
1328        .din    (soft_a_mult    ),  //I
1329        .dout   (soft_a_round   )   //O           
1330        ) ;
1331
1332//================================
1333// Round
1334//================================         
1335round_data data_q (
1336        .clk    (clk            ),  //I
1337        .din    (soft_b_mult    ),  //I
1338        .dout   (soft_b_round   )   //O           
1339        ) ;       
1340
1341//assign mod_pipe = in_fullrate ? (mod_type == 1 ? (mod_s0 & bpsk_sel) : (mod_type == 2 ? mod_s0 : (mod_s0 | mod_s1))) : mod_s0 ;
1342assign llr_vld = bpsk ? (mod_vld_dd & bpsk_sel) : (qpsk ? mod_vld_dd : (mod_vld_dd | mod_vld_ddd)) ;
1343
1344assign mod_vld = vin & (mod_level != 0) ;
1345always @ (posedge clk )
1346  {mod_vld_ddd, mod_vld_dd,mod_vld_d} <= {mod_vld_dd, mod_vld_d,mod_vld}  ;
1347
1348always @ (posedge clk or negedge nrst)
1349  if (~nrst)
1350    input_cnt <= 0 ;
1351  else if (start)
1352    input_cnt <= 0 ;
1353  else if (llr_vld)
1354  begin
1355    if (input_cnt != 192)
1356      input_cnt <= input_cnt +1 ;
1357  end
1358
1359always @ (posedge clk or negedge nrst)
1360  if (~nrst)
1361    bpsk_sel <= 1'b0 ;
1362  else if (start)
1363    bpsk_sel <= 1'b0 ;
1364  else if (mod_vld_dd)
1365      bpsk_sel <= ~bpsk_sel ;
1366
1367endmodule
1368
1369
1370//********************************************************************************
1371// File:    unc_decoder.v
1372// Author:  Yang Sun (ysun@rice.edu)
1373// Birth:   $ 12/1/07
1374// Des:     decoder system
1375// History: $ 12/1/07, Uncoded decoder
1376//          $ 10/11/08, added BPSK
1377//********************************************************************************
1378module unc_decoder (
1379        clk     ,   // clock
1380        nrst    ,   // n reset
1381        hd_a    ,   // hard decision of a
1382        hd_b    ,   // hard decision of b
1383        start   ,   // start decoding pulse
1384        vin     ,   // valid input
1385        vout    ,   // valid output
1386        dout        // byte out
1387        ) ;
1388
1389input           clk ;
1390input           nrst ;
1391input           hd_a ;
1392input           hd_b ;
1393input           start ;
1394input           vin ;
1395output          vout ;
1396output  [7:0]   dout ;       
1397       
1398//==============================
1399//Internal signal
1400//==============================
1401reg     [7:2]   tmpd ;
1402reg     [1:0]   cnt ;   
1403wire    [1:0]   data_2b ;
1404       
1405//==============================
1406// Main RTL code
1407//==============================       
1408assign data_2b = {hd_a, hd_b} ;
1409always @ (posedge clk or negedge nrst)
1410  if (~nrst)
1411    tmpd <= 0 ;
1412  else if (start)
1413    tmpd <= 0 ;
1414  else if (vin)
1415  begin
1416    case (cnt)
1417      3'd0: tmpd [7:6] <= data_2b ;
1418      3'd1: tmpd [5:4] <= data_2b ;
1419      3'd2: tmpd [3:2] <= data_2b ;
1420      default: tmpd <= tmpd ;
1421    endcase             
1422  end
1423 
1424always @ (posedge clk or negedge nrst)
1425  if (~nrst)
1426    cnt <= 0 ;
1427  else if (start)
1428    cnt <= 0 ;
1429  else if (vin)
1430    cnt <= cnt +1 ;
1431
1432assign vout = vin & (cnt == 3) ;
1433assign dout = {tmpd, data_2b} ; 
1434
1435endmodule 
1436
1437//**************************************************************
1438// File:    unpack_m2n.v
1439// Author:  Yang Sun (ysun@rice.edu)
1440// Birth:   $ 1/15/07
1441// Des:     unpack from M bit to N bit, M > N
1442// History: $ 1/15/07, Init coding
1443//          $ 1/21/07, K = 7
1444//          $ 3/23, Fixed a naming problem for sysgen
1445//                  Can not use VHDL reserved key world
1446//          $ 12/1/07: Updated
1447//**************************************************************
1448
1449module unpack_m2n (
1450        clk     ,   // I, clock
1451        nrst    ,   // I, n reset
1452        start   ,   // I, start pulse
1453        din     ,   // I, data input
1454        vin     ,   // I, valid input
1455        dout    ,   // O, data output
1456        vout    ,   // O, valid output
1457        remain  ,   // I, remain
1458        last    ,   // I, last data
1459        done        // O, done
1460        ) ;
1461
1462parameter               BITM = 40 ;
1463parameter               BITN = 8 ;
1464parameter               LW = 6 ;
1465
1466input                   clk ;
1467input                   nrst ;
1468input                   start ;
1469input   [BITM-1 :0]     din ;
1470input                   vin ;
1471input   [LW -1:0]       remain ;
1472input                   last ;
1473
1474output  [BITN-1 :0]     dout ;
1475output                  vout ;
1476output                  done ;
1477
1478//================================================
1479//Internal signal
1480//================================================
1481reg     [BITM + BITN -1 :0]     sreg ;
1482wire    [BITM + BITN -1 :0]     sreg_next ;
1483reg     [LW -1 : 0]             cnt ;
1484wire                            rd ;
1485wire    [BITN-1 :0]             data ;
1486wire    [BITM + BITN -1 :0]     tmp ;
1487wire    [BITM + BITN -1 :0]     tmp2 ;
1488wire    [LW -1 : 0]             shift ;
1489reg                             last_i ;
1490reg                             last_byte_s0 ;
1491wire                            last_byte ;
1492
1493//================================================
1494// Main RTL code
1495//================================================
1496assign dout = data ;
1497assign vout = rd ;
1498
1499always @ (posedge clk or negedge nrst)
1500  if (~nrst)
1501    cnt <= 0 ;
1502  else if (start)
1503    cnt <= 0 ;
1504  else if (vin)
1505  begin
1506    if (~last)
1507      cnt <= cnt + BITM ;
1508    else
1509      cnt <= cnt + remain ;
1510  end
1511  else if (rd)
1512    cnt <= cnt - BITN ;
1513   
1514assign rd = cnt >= BITN ;
1515assign data = sreg [BITM + BITN -1 : BITM] ;
1516assign tmp = {{BITM{1'b0}}, din} ;
1517assign shift = BITN - cnt ;
1518assign tmp2 = (tmp << shift) | sreg ;
1519assign sreg_next = vin ? tmp2 : rd ? {sreg[BITM -1 : 0], {BITN{1'b0}}} : sreg ;
1520
1521always @ (posedge clk or negedge nrst)
1522  if (~nrst)
1523    sreg <= 0 ;
1524  else if (start)
1525    sreg <= 0 ;
1526  else 
1527    sreg <= sreg_next ;
1528
1529always @ (posedge clk or negedge nrst)
1530  if (~nrst)
1531    last_i <= 1'b0 ;
1532  else if (start)
1533    last_i <= 1'b0 ;
1534  else if (vin & last)
1535    last_i <= 1'b1 ;
1536
1537assign last_byte = last_i && cnt < BITN ;
1538always @ (posedge clk)
1539  last_byte_s0 <= last_byte ;
1540
1541assign done = ~last_byte_s0 & last_byte ;
1542
1543endmodule
1544//*****************************************************************
1545// File:    vb_decoder_top.v
1546// Author:  Yang Sun (ysun@rice.edu)
1547// Birth:   $ 1/15/07
1548// Des:     viterbi decoder top level
1549//          Take 5 bit soft value is [-16 : +15]
1550//          K = 3. g0 =7, g1 = 5
1551//          K = 7. g0 = 133, g1 = 171
1552// History: $ 1/15/07, Init coding
1553//          $ 1/21/07, K = 7
1554//          $ 1/27/07, Change to LLR domain
1555//          $ 2/4/07, Support puncture 2/3, 3/4
1556//          $ 3/22/07, Remove puncture and iq buffer for WARP
1557//          $ 3/23/07, Fixed a naming problem for sysgen
1558//                     Can not use VHDL reserved key world
1559//          $ 4/20/07, K = 7
1560//          $ 4/22/07, Change quantilization to -4 ~ 4 9-level
1561//          $ 12/1/07: Modified for OFDM V7
1562//*****************************************************************
1563module vb_decoder_top (
1564        clk         ,   // I, clock
1565        nrst        ,   // I, n reset
1566        packet_start,   // I, packet start pulse
1567        packet_end  ,   // I, packet end pulse
1568        zero_tail   ,   // I, the code is zero terminated
1569        vin         ,   // I, valid input
1570        llr_b1      ,   // I, 1st LLR
1571        llr_b0      ,   // I, 2nd LLR
1572        vout        ,   // O, valid output
1573        dout_in_byte,   // O, decoded output in byte
1574        done            // O, decoding done
1575        ) ;
1576
1577parameter           SW = 4 ;        // soft input precision
1578parameter           M = 7 ;         // Metric precision
1579parameter           R = 48 ;        // reliable trace
1580parameter           C = 40 ;        // unreliable trace
1581parameter           L = 88 ;        // total trace depth
1582parameter           LW = 7 ;        // L width
1583parameter           K = 7 ;         // constraint length
1584parameter           N = 64 ;        // number of states
1585parameter           TR = 128 ;      // trace buffer depth
1586parameter           TRW = 7 ;       // trace buffer address width
1587       
1588input               clk ;           // system clock
1589input               nrst ;          // active low reset
1590input               packet_start ;  // start of packet pulse
1591input               zero_tail ;     // 1 = the code is terminated with 0, 0 = no termination
1592input               packet_end ;    // end of packet pulse
1593input               vin ;           // data valid input
1594input   [SW -1:0]   llr_b1 ;        // soft value for bit1
1595input   [SW -1:0]   llr_b0 ;        // soft value for bit0
1596
1597output              done ;
1598output              vout ;
1599output  [7:0]       dout_in_byte ;
1600
1601//=============================================
1602//Internal signal
1603//=============================================
1604wire    [LW -1:0]           remain ;
1605wire                        dec_vout ;
1606wire    [R-1:0]             dec_dout ;
1607wire                        dec_done ;
1608
1609//=============================================
1610// Main RTL code
1611//=============================================
1612
1613//================================================================
1614// Viterbi decoder core logic
1615//================================================================
1616viterbi_core #(SW, M, R, C, L, LW, K, N, TR, TRW) viterbi_core (
1617        .clk            (clk            ),  //IN
1618        .nrst           (nrst           ),  //IN
1619        .packet_start   (packet_start   ),  //IN
1620        .packet_end     (packet_end     ),  //IN
1621        .zero_tail      (zero_tail      ),
1622        `ifdef INT_PUNC                     //Internal puncture
1623        .dv_in          (llr_valid      ),  //IN
1624        .llr1           (llr1_depunc    ),  //IN[SW-1:0]
1625        .llr0           (llr0_depunc    ),  //IN[SW-1:0]
1626        `else
1627        .dv_in          (vin            ),  //IN
1628        .llr1           (llr_b1         ),  //IN[SW-1:0]
1629        .llr0           (llr_b0         ),  //IN[SW-1:0]       
1630        `endif
1631        .remain         (remain         ),  //OUT[LW -1:0]
1632        .done           (dec_done       ),  //OUT
1633        .dv_out         (dec_vout       ),  //OUT
1634        .dout           (dec_dout       )   //OUT[R -1:0]
1635        ) ;
1636
1637//=============================================
1638// x to 8bit unpacking
1639//=============================================       
1640unpack_m2n #(R, 8, LW) unpack_Rto8 (
1641        .clk    (clk            ),  //IN
1642        .nrst   (nrst           ),  //IN
1643        .start  (packet_start   ),  //IN
1644        .din    (dec_dout       ),  //IN[R -1:0]   
1645        .vin    (dec_vout       ),  //IN
1646        .last   (dec_done       ),  //IN
1647        .remain (remain         ),  //IN[LW -1:0]
1648        .dout   (dout_in_byte   ),  //OUT
1649        .vout   (vout           ),  //OUT
1650        .done   (done           )   //OUT
1651        ) ;
1652       
1653endmodule
1654
1655//**************************************************************
1656// File:    viterbi_core
1657// Author:  Yang Sun (ysun@rice.edu)
1658// Birth:   $ 1/14/07
1659// Des:     The top level of viterbi decoder
1660//          Take 4 bit soft value is [-8 : +7]
1661//          K = 3. g0 = 7, g1 = 5
1662//          K = 5, g0 (A) = 1 + x + x^2 + x^4, g1 (B) = 1 + x^3 + x^4
1663//          K = 7, g0 (A) = 1 + x^2 + x^3 + x^4 + x^5
1664//                 g1 (B) = 1 + x + x^2 + x^3 + x^6
1665// History: $ 1/14/07, Init coding, K = 3
1666//          $ 1/22/07, K = 5
1667//          $ 1/26/07, Updated
1668//          $ 1/27/07, Change to LLR domain
1669//          $ 2/18/07, Supported K = 7
1670//          $ 2/23/07, Added pipeline to max_metric
1671//          $ 12/1/07, Updated
1672//**************************************************************
1673//`define ZERO_TERM
1674
1675module viterbi_core (
1676        clk         ,   // I, clock
1677        nrst        ,   // I, n reset
1678        packet_start,   // I, packet start pulse
1679        zero_tail   ,   // I, the code is zero terminated
1680        dv_in       ,   // I, data valid input
1681        llr0        ,   // I, LLR 2nd
1682        llr1        ,   // I, LLR 1st
1683        packet_end  ,   // I, packet end pulse
1684        dv_out      ,   // O, data valid out
1685        dout        ,   // O, data out
1686        done        ,   // O, done pulse
1687        remain          // O, remain data cnt
1688        ) ;
1689       
1690parameter           SW = 4 ;        // soft input precision
1691parameter           M = 7 ;         // metric width
1692
1693parameter           R = 48 ;        // reliable trace
1694parameter           C = 40 ;        // unreliable trace
1695parameter           L = 88 ;        // total trace depth
1696parameter           LW = 7 ;        // L width
1697
1698parameter           K = 7 ;         // constraint length
1699parameter           N = 64 ;        // number of states = 2^(K-1)
1700parameter           TR = 128 ;      // trace buffer
1701parameter           TRW = 7 ;       // trace
1702
1703input               clk ;           // system clock
1704input               nrst ;          // active low reset
1705input               packet_start ;  // start of packet pulse
1706input               zero_tail ;     // 1 = the code is terminated with 0, 0 = no termination
1707input               packet_end ;    // end of packet pulse
1708input               dv_in ;         // data valid input
1709input   [SW -1:0]   llr1 ;          // LLR soft value for bit1, LLR = log(Pr(ci=0/ri)/Pr(ci=1/ri))
1710input   [SW -1:0]   llr0 ;          // LLR soft value for bit0, LLR = log(Pr(ci=0/ri)/Pr(ci=1/ri))
1711
1712output              done ;
1713output  [LW -1:0]   remain ;
1714output              dv_out ;
1715output  [R-1:0]     dout ;
1716
1717//======================================
1718//Internal signal
1719//======================================
1720wire    [SW:0]      branch_00 ;
1721wire    [SW:0]      branch_01 ;
1722wire    [SW:0]      branch_10 ;
1723wire    [SW:0]      branch_11 ;
1724
1725wire    [SW:0]      branch_A_0 ;
1726wire    [SW:0]      branch_B_0 ;
1727wire    [SW:0]      branch_A_1 ;
1728wire    [SW:0]      branch_B_1 ;
1729wire    [SW:0]      branch_A_2 ;
1730wire    [SW:0]      branch_B_2 ;
1731wire    [SW:0]      branch_A_3 ;
1732wire    [SW:0]      branch_B_3 ;
1733wire    [SW:0]      branch_A_4 ;
1734wire    [SW:0]      branch_B_4 ;
1735wire    [SW:0]      branch_A_5 ;
1736wire    [SW:0]      branch_B_5 ;
1737wire    [SW:0]      branch_A_6 ;
1738wire    [SW:0]      branch_B_6 ;
1739wire    [SW:0]      branch_A_7 ;
1740wire    [SW:0]      branch_B_7 ;
1741wire    [SW:0]      branch_A_8 ;
1742wire    [SW:0]      branch_B_8 ;
1743wire    [SW:0]      branch_A_9 ;
1744wire    [SW:0]      branch_B_9 ;
1745wire    [SW:0]      branch_A_10 ;
1746wire    [SW:0]      branch_B_10 ;
1747wire    [SW:0]      branch_A_11 ;
1748wire    [SW:0]      branch_B_11 ;
1749wire    [SW:0]      branch_A_12 ;
1750wire    [SW:0]      branch_B_12 ;
1751wire    [SW:0]      branch_A_13 ;
1752wire    [SW:0]      branch_B_13 ;
1753wire    [SW:0]      branch_A_14 ;
1754wire    [SW:0]      branch_B_14 ;
1755wire    [SW:0]      branch_A_15 ;
1756wire    [SW:0]      branch_B_15 ;
1757wire    [SW:0]      branch_A_16 ;
1758wire    [SW:0]      branch_B_16 ;
1759wire    [SW:0]      branch_A_17;
1760wire    [SW:0]      branch_B_17;
1761wire    [SW:0]      branch_A_18;
1762wire    [SW:0]      branch_B_18;
1763wire    [SW:0]      branch_A_19;
1764wire    [SW:0]      branch_B_19;
1765wire    [SW:0]      branch_A_20;
1766wire    [SW:0]      branch_B_20;
1767wire    [SW:0]      branch_A_21;
1768wire    [SW:0]      branch_B_21;
1769wire    [SW:0]      branch_A_22;
1770wire    [SW:0]      branch_B_22;
1771wire    [SW:0]      branch_A_23;
1772wire    [SW:0]      branch_B_23;
1773wire    [SW:0]      branch_A_24;
1774wire    [SW:0]      branch_B_24;
1775wire    [SW:0]      branch_A_25;
1776wire    [SW:0]      branch_B_25;
1777wire    [SW:0]      branch_A_26 ;
1778wire    [SW:0]      branch_B_26 ;
1779wire    [SW:0]      branch_A_27 ;
1780wire    [SW:0]      branch_B_27 ;
1781wire    [SW:0]      branch_A_28 ;
1782wire    [SW:0]      branch_B_28 ;
1783wire    [SW:0]      branch_A_29 ;
1784wire    [SW:0]      branch_B_29 ;
1785wire    [SW:0]      branch_A_30 ;
1786wire    [SW:0]      branch_B_30 ;
1787wire    [SW:0]      branch_A_31 ;
1788wire    [SW:0]      branch_B_31 ;
1789wire    [SW:0]      branch_A_32;
1790wire    [SW:0]      branch_B_32;
1791wire    [SW:0]      branch_A_33;
1792wire    [SW:0]      branch_B_33;
1793wire    [SW:0]      branch_A_34;
1794wire    [SW:0]      branch_B_34;
1795wire    [SW:0]      branch_A_35;
1796wire    [SW:0]      branch_B_35;
1797wire    [SW:0]      branch_A_36;
1798wire    [SW:0]      branch_B_36;
1799wire    [SW:0]      branch_A_37;
1800wire    [SW:0]      branch_B_37;
1801wire    [SW:0]      branch_A_38;
1802wire    [SW:0]      branch_B_38;
1803wire    [SW:0]      branch_A_39;
1804wire    [SW:0]      branch_B_39;
1805wire    [SW:0]      branch_A_40;
1806wire    [SW:0]      branch_B_40;
1807wire    [SW:0]      branch_A_41;
1808wire    [SW:0]      branch_B_41;
1809wire    [SW:0]      branch_A_42 ;
1810wire    [SW:0]      branch_B_42 ;
1811wire    [SW:0]      branch_A_43 ;
1812wire    [SW:0]      branch_B_43 ;
1813wire    [SW:0]      branch_A_44 ;
1814wire    [SW:0]      branch_B_44 ;
1815wire    [SW:0]      branch_A_45 ;
1816wire    [SW:0]      branch_B_45 ;
1817wire    [SW:0]      branch_A_46 ;
1818wire    [SW:0]      branch_B_46 ;
1819wire    [SW:0]      branch_A_47 ;
1820wire    [SW:0]      branch_B_47 ;
1821wire    [SW:0]      branch_A_48;
1822wire    [SW:0]      branch_B_48;
1823wire    [SW:0]      branch_A_49;
1824wire    [SW:0]      branch_B_49;
1825wire    [SW:0]      branch_A_50;
1826wire    [SW:0]      branch_B_50;
1827wire    [SW:0]      branch_A_51;
1828wire    [SW:0]      branch_B_51;
1829wire    [SW:0]      branch_A_52;
1830wire    [SW:0]      branch_B_52;
1831wire    [SW:0]      branch_A_53;
1832wire    [SW:0]      branch_B_53;
1833wire    [SW:0]      branch_A_54;
1834wire    [SW:0]      branch_B_54;
1835wire    [SW:0]      branch_A_55;
1836wire    [SW:0]      branch_B_55;
1837wire    [SW:0]      branch_A_56;
1838wire    [SW:0]      branch_B_56;
1839wire    [SW:0]      branch_A_57;
1840wire    [SW:0]      branch_B_57;
1841wire    [SW:0]      branch_A_58 ;
1842wire    [SW:0]      branch_B_58 ;
1843wire    [SW:0]      branch_A_59 ;
1844wire    [SW:0]      branch_B_59 ;
1845wire    [SW:0]      branch_A_60 ;
1846wire    [SW:0]      branch_B_60 ;
1847wire    [SW:0]      branch_A_61 ;
1848wire    [SW:0]      branch_B_61 ;
1849wire    [SW:0]      branch_A_62 ;
1850wire    [SW:0]      branch_B_62 ;
1851wire    [SW:0]      branch_A_63 ;
1852wire    [SW:0]      branch_B_63 ;
1853
1854reg     [M -1:0]    metric_0 ;
1855reg     [M -1:0]    metric_1 ;
1856reg     [M -1:0]    metric_2 ;
1857reg     [M -1:0]    metric_3 ;
1858reg     [M -1:0]    metric_4 ;
1859reg     [M -1:0]    metric_5 ;
1860reg     [M -1:0]    metric_6 ;
1861reg     [M -1:0]    metric_7 ;
1862reg     [M -1:0]    metric_8 ;
1863reg     [M -1:0]    metric_9 ;
1864reg     [M -1:0]    metric_10 ;
1865reg     [M -1:0]    metric_11 ;
1866reg     [M -1:0]    metric_12 ;
1867reg     [M -1:0]    metric_13 ;
1868reg     [M -1:0]    metric_14 ;
1869reg     [M -1:0]    metric_15 ;
1870reg     [M -1:0]    metric_16;
1871reg     [M -1:0]    metric_17;
1872reg     [M -1:0]    metric_18;
1873reg     [M -1:0]    metric_19;
1874reg     [M -1:0]    metric_20;
1875reg     [M -1:0]    metric_21;
1876reg     [M -1:0]    metric_22;
1877reg     [M -1:0]    metric_23;
1878reg     [M -1:0]    metric_24;
1879reg     [M -1:0]    metric_25;
1880reg     [M -1:0]    metric_26 ;
1881reg     [M -1:0]    metric_27 ;
1882reg     [M -1:0]    metric_28 ;
1883reg     [M -1:0]    metric_29 ;
1884reg     [M -1:0]    metric_30 ;
1885reg     [M -1:0]    metric_31 ;
1886reg     [M -1:0]    metric_32;
1887reg     [M -1:0]    metric_33;
1888reg     [M -1:0]    metric_34;
1889reg     [M -1:0]    metric_35;
1890reg     [M -1:0]    metric_36;
1891reg     [M -1:0]    metric_37;
1892reg     [M -1:0]    metric_38;
1893reg     [M -1:0]    metric_39;
1894reg     [M -1:0]    metric_40;
1895reg     [M -1:0]    metric_41;
1896reg     [M -1:0]    metric_42 ;
1897reg     [M -1:0]    metric_43 ;
1898reg     [M -1:0]    metric_44 ;
1899reg     [M -1:0]    metric_45 ;
1900reg     [M -1:0]    metric_46 ;
1901reg     [M -1:0]    metric_47 ;
1902reg     [M -1:0]    metric_48;
1903reg     [M -1:0]    metric_49;
1904reg     [M -1:0]    metric_50;
1905reg     [M -1:0]    metric_51;
1906reg     [M -1:0]    metric_52;
1907reg     [M -1:0]    metric_53;
1908reg     [M -1:0]    metric_54;
1909reg     [M -1:0]    metric_55;
1910reg     [M -1:0]    metric_56;
1911reg     [M -1:0]    metric_57;
1912reg     [M -1:0]    metric_58 ;
1913reg     [M -1:0]    metric_59 ;
1914reg     [M -1:0]    metric_60 ;
1915reg     [M -1:0]    metric_61 ;
1916reg     [M -1:0]    metric_62 ;
1917reg     [M -1:0]    metric_63 ;
1918
1919wire    [M -1:0]    metric_next_0 ;
1920wire    [M -1:0]    metric_next_1 ;
1921wire    [M -1:0]    metric_next_2 ;
1922wire    [M -1:0]    metric_next_3 ;
1923wire    [M -1:0]    metric_next_4 ;
1924wire    [M -1:0]    metric_next_5 ;
1925wire    [M -1:0]    metric_next_6 ;
1926wire    [M -1:0]    metric_next_7 ;
1927wire    [M -1:0]    metric_next_8 ;
1928wire    [M -1:0]    metric_next_9 ;
1929wire    [M -1:0]    metric_next_10 ;
1930wire    [M -1:0]    metric_next_11 ;
1931wire    [M -1:0]    metric_next_12 ;
1932wire    [M -1:0]    metric_next_13 ;
1933wire    [M -1:0]    metric_next_14 ;
1934wire    [M -1:0]    metric_next_15 ;
1935wire    [M -1:0]    metric_next_16;
1936wire    [M -1:0]    metric_next_17;
1937wire    [M -1:0]    metric_next_18;
1938wire    [M -1:0]    metric_next_19;
1939wire    [M -1:0]    metric_next_20;
1940wire    [M -1:0]    metric_next_21;
1941wire    [M -1:0]    metric_next_22;
1942wire    [M -1:0]    metric_next_23;
1943wire    [M -1:0]    metric_next_24;
1944wire    [M -1:0]    metric_next_25;
1945wire    [M -1:0]    metric_next_26 ;
1946wire    [M -1:0]    metric_next_27 ;
1947wire    [M -1:0]    metric_next_28 ;
1948wire    [M -1:0]    metric_next_29 ;
1949wire    [M -1:0]    metric_next_30 ;
1950wire    [M -1:0]    metric_next_31 ;
1951wire    [M -1:0]    metric_next_32;
1952wire    [M -1:0]    metric_next_33;
1953wire    [M -1:0]    metric_next_34;
1954wire    [M -1:0]    metric_next_35;
1955wire    [M -1:0]    metric_next_36;
1956wire    [M -1:0]    metric_next_37;
1957wire    [M -1:0]    metric_next_38;
1958wire    [M -1:0]    metric_next_39;
1959wire    [M -1:0]    metric_next_40;
1960wire    [M -1:0]    metric_next_41;
1961wire    [M -1:0]    metric_next_42 ;
1962wire    [M -1:0]    metric_next_43 ;
1963wire    [M -1:0]    metric_next_44 ;
1964wire    [M -1:0]    metric_next_45 ;
1965wire    [M -1:0]    metric_next_46 ;
1966wire    [M -1:0]    metric_next_47 ;
1967wire    [M -1:0]    metric_next_48;
1968wire    [M -1:0]    metric_next_49;
1969wire    [M -1:0]    metric_next_50;
1970wire    [M -1:0]    metric_next_51;
1971wire    [M -1:0]    metric_next_52;
1972wire    [M -1:0]    metric_next_53;
1973wire    [M -1:0]    metric_next_54;
1974wire    [M -1:0]    metric_next_55;
1975wire    [M -1:0]    metric_next_56;
1976wire    [M -1:0]    metric_next_57;
1977wire    [M -1:0]    metric_next_58 ;
1978wire    [M -1:0]    metric_next_59 ;
1979wire    [M -1:0]    metric_next_60 ;
1980wire    [M -1:0]    metric_next_61 ;
1981wire    [M -1:0]    metric_next_62 ;
1982wire    [M -1:0]    metric_next_63 ;
1983
1984wire    [M -1:0]    metric_A_0 ;
1985wire    [M -1:0]    metric_B_0 ;
1986wire    [M -1:0]    metric_A_1 ;
1987wire    [M -1:0]    metric_B_1 ;
1988wire    [M -1:0]    metric_A_2 ;
1989wire    [M -1:0]    metric_B_2 ;
1990wire    [M -1:0]    metric_A_3 ;
1991wire    [M -1:0]    metric_B_3 ;
1992wire    [M -1:0]    metric_A_4 ;
1993wire    [M -1:0]    metric_B_4 ;
1994wire    [M -1:0]    metric_A_5 ;
1995wire    [M -1:0]    metric_B_5 ;
1996wire    [M -1:0]    metric_A_6 ;
1997wire    [M -1:0]    metric_B_6 ;
1998wire    [M -1:0]    metric_A_7 ;
1999wire    [M -1:0]    metric_B_7 ;
2000wire    [M -1:0]    metric_A_8 ;
2001wire    [M -1:0]    metric_B_8 ;
2002wire    [M -1:0]    metric_A_9 ;
2003wire    [M -1:0]    metric_B_9 ;
2004wire    [M -1:0]    metric_A_10 ;
2005wire    [M -1:0]    metric_B_10 ;
2006wire    [M -1:0]    metric_A_11 ;
2007wire    [M -1:0]    metric_B_11 ;
2008wire    [M -1:0]    metric_A_12 ;
2009wire    [M -1:0]    metric_B_12 ;
2010wire    [M -1:0]    metric_A_13 ;
2011wire    [M -1:0]    metric_B_13 ;
2012wire    [M -1:0]    metric_A_14 ;
2013wire    [M -1:0]    metric_B_14 ;
2014wire    [M -1:0]    metric_A_15 ;
2015wire    [M -1:0]    metric_B_15 ;
2016wire    [M -1:0]    metric_A_16;
2017wire    [M -1:0]    metric_B_16;
2018wire    [M -1:0]    metric_A_17;
2019wire    [M -1:0]    metric_B_17;
2020wire    [M -1:0]    metric_A_18;
2021wire    [M -1:0]    metric_B_18;
2022wire    [M -1:0]    metric_A_19;
2023wire    [M -1:0]    metric_B_19;
2024wire    [M -1:0]    metric_A_20;
2025wire    [M -1:0]    metric_B_20;
2026wire    [M -1:0]    metric_A_21;
2027wire    [M -1:0]    metric_B_21;
2028wire    [M -1:0]    metric_A_22;
2029wire    [M -1:0]    metric_B_22;
2030wire    [M -1:0]    metric_A_23;
2031wire    [M -1:0]    metric_B_23;
2032wire    [M -1:0]    metric_A_24;
2033wire    [M -1:0]    metric_B_24;
2034wire    [M -1:0]    metric_A_25;
2035wire    [M -1:0]    metric_B_25;
2036wire    [M -1:0]    metric_A_26 ;
2037wire    [M -1:0]    metric_B_26 ;
2038wire    [M -1:0]    metric_A_27 ;
2039wire    [M -1:0]    metric_B_27 ;
2040wire    [M -1:0]    metric_A_28 ;
2041wire    [M -1:0]    metric_B_28 ;
2042wire    [M -1:0]    metric_A_29 ;
2043wire    [M -1:0]    metric_B_29 ;
2044wire    [M -1:0]    metric_A_30 ;
2045wire    [M -1:0]    metric_B_30 ;
2046wire    [M -1:0]    metric_A_31 ;
2047wire    [M -1:0]    metric_B_31 ;
2048wire    [M -1:0]    metric_A_32;
2049wire    [M -1:0]    metric_B_32;
2050wire    [M -1:0]    metric_A_33;
2051wire    [M -1:0]    metric_B_33;
2052wire    [M -1:0]    metric_A_34;
2053wire    [M -1:0]    metric_B_34;
2054wire    [M -1:0]    metric_A_35;
2055wire    [M -1:0]    metric_B_35;
2056wire    [M -1:0]    metric_A_36;
2057wire    [M -1:0]    metric_B_36;
2058wire    [M -1:0]    metric_A_37;
2059wire    [M -1:0]    metric_B_37;
2060wire    [M -1:0]    metric_A_38;
2061wire    [M -1:0]    metric_B_38;
2062wire    [M -1:0]    metric_A_39;
2063wire    [M -1:0]    metric_B_39;
2064wire    [M -1:0]    metric_A_40;
2065wire    [M -1:0]    metric_B_40;
2066wire    [M -1:0]    metric_A_41;
2067wire    [M -1:0]    metric_B_41;
2068wire    [M -1:0]    metric_A_42 ;
2069wire    [M -1:0]    metric_B_42 ;
2070wire    [M -1:0]    metric_A_43 ;
2071wire    [M -1:0]    metric_B_43 ;
2072wire    [M -1:0]    metric_A_44 ;
2073wire    [M -1:0]    metric_B_44 ;
2074wire    [M -1:0]    metric_A_45 ;
2075wire    [M -1:0]    metric_B_45 ;
2076wire    [M -1:0]    metric_A_46 ;
2077wire    [M -1:0]    metric_B_46 ;
2078wire    [M -1:0]    metric_A_47 ;
2079wire    [M -1:0]    metric_B_47 ;
2080wire    [M -1:0]    metric_A_48;
2081wire    [M -1:0]    metric_B_48;
2082wire    [M -1:0]    metric_A_49;
2083wire    [M -1:0]    metric_B_49;
2084wire    [M -1:0]    metric_A_50;
2085wire    [M -1:0]    metric_B_50;
2086wire    [M -1:0]    metric_A_51;
2087wire    [M -1:0]    metric_B_51;
2088wire    [M -1:0]    metric_A_52;
2089wire    [M -1:0]    metric_B_52;
2090wire    [M -1:0]    metric_A_53;
2091wire    [M -1:0]    metric_B_53;
2092wire    [M -1:0]    metric_A_54;
2093wire    [M -1:0]    metric_B_54;
2094wire    [M -1:0]    metric_A_55;
2095wire    [M -1:0]    metric_B_55;
2096wire    [M -1:0]    metric_A_56;
2097wire    [M -1:0]    metric_B_56;
2098wire    [M -1:0]    metric_A_57;
2099wire    [M -1:0]    metric_B_57;
2100wire    [M -1:0]    metric_A_58 ;
2101wire    [M -1:0]    metric_B_58 ;
2102wire    [M -1:0]    metric_A_59 ;
2103wire    [M -1:0]    metric_B_59 ;
2104wire    [M -1:0]    metric_A_60 ;
2105wire    [M -1:0]    metric_B_60 ;
2106wire    [M -1:0]    metric_A_61 ;
2107wire    [M -1:0]    metric_B_61 ;
2108wire    [M -1:0]    metric_A_62 ;
2109wire    [M -1:0]    metric_B_62 ;
2110wire    [M -1:0]    metric_A_63 ;
2111wire    [M -1:0]    metric_B_63 ;
2112                             
2113// 64 state tran             
2114reg     [0:0]       tran_state_0  [TR -1:0] ;
2115reg     [0:0]       tran_state_1  [TR -1:0] ;
2116reg     [0:0]       tran_state_2  [TR -1:0] ;
2117reg     [0:0]       tran_state_3  [TR -1:0] ;
2118reg     [0:0]       tran_state_4  [TR -1:0] ;
2119reg     [0:0]       tran_state_5  [TR -1:0] ;
2120reg     [0:0]       tran_state_6  [TR -1:0] ;
2121reg     [0:0]       tran_state_7  [TR -1:0] ;
2122reg     [0:0]       tran_state_8  [TR -1:0] ;
2123reg     [0:0]       tran_state_9  [TR -1:0] ;
2124reg     [0:0]       tran_state_10 [TR -1:0] ;
2125reg     [0:0]       tran_state_11 [TR -1:0] ;
2126reg     [0:0]       tran_state_12 [TR -1:0] ;
2127reg     [0:0]       tran_state_13 [TR -1:0] ;
2128reg     [0:0]       tran_state_14 [TR -1:0] ;
2129reg     [0:0]       tran_state_15 [TR -1:0] ;
2130reg     [0:0]       tran_state_16 [TR -1:0] ;
2131reg     [0:0]       tran_state_17 [TR -1:0] ;
2132reg     [0:0]       tran_state_18 [TR -1:0] ;
2133reg     [0:0]       tran_state_19 [TR -1:0] ;
2134reg     [0:0]       tran_state_20 [TR -1:0] ;
2135reg     [0:0]       tran_state_21 [TR -1:0] ;
2136reg     [0:0]       tran_state_22 [TR -1:0] ;
2137reg     [0:0]       tran_state_23 [TR -1:0] ;
2138reg     [0:0]       tran_state_24 [TR -1:0] ;
2139reg     [0:0]       tran_state_25 [TR -1:0] ;
2140reg     [0:0]       tran_state_26 [TR -1:0] ;
2141reg     [0:0]       tran_state_27 [TR -1:0] ;
2142reg     [0:0]       tran_state_28 [TR -1:0] ;
2143reg     [0:0]       tran_state_29 [TR -1:0] ;
2144reg     [0:0]       tran_state_30 [TR -1:0] ;
2145reg     [0:0]       tran_state_31 [TR -1:0] ;
2146reg     [0:0]       tran_state_32 [TR -1:0] ;
2147reg     [0:0]       tran_state_33 [TR -1:0] ;
2148reg     [0:0]       tran_state_34 [TR -1:0] ;
2149reg     [0:0]       tran_state_35 [TR -1:0] ;
2150reg     [0:0]       tran_state_36 [TR -1:0] ;
2151reg     [0:0]       tran_state_37 [TR -1:0] ;
2152reg     [0:0]       tran_state_38 [TR -1:0] ;
2153reg     [0:0]       tran_state_39 [TR -1:0] ;
2154reg     [0:0]       tran_state_40 [TR -1:0] ;
2155reg     [0:0]       tran_state_41 [TR -1:0] ;
2156reg     [0:0]       tran_state_42 [TR -1:0] ;
2157reg     [0:0]       tran_state_43 [TR -1:0] ;
2158reg     [0:0]       tran_state_44 [TR -1:0] ;
2159reg     [0:0]       tran_state_45 [TR -1:0] ;
2160reg     [0:0]       tran_state_46 [TR -1:0] ;
2161reg     [0:0]       tran_state_47 [TR -1:0] ;
2162reg     [0:0]       tran_state_48 [TR -1:0] ;
2163reg     [0:0]       tran_state_49 [TR -1:0] ;
2164reg     [0:0]       tran_state_50 [TR -1:0] ;
2165reg     [0:0]       tran_state_51 [TR -1:0] ;
2166reg     [0:0]       tran_state_52 [TR -1:0] ;
2167reg     [0:0]       tran_state_53 [TR -1:0] ;
2168reg     [0:0]       tran_state_54 [TR -1:0] ;
2169reg     [0:0]       tran_state_55 [TR -1:0] ;
2170reg     [0:0]       tran_state_56 [TR -1:0] ;
2171reg     [0:0]       tran_state_57 [TR -1:0] ;
2172reg     [0:0]       tran_state_58 [TR -1:0] ;
2173reg     [0:0]       tran_state_59 [TR -1:0] ;
2174reg     [0:0]       tran_state_60 [TR -1:0] ;
2175reg     [0:0]       tran_state_61 [TR -1:0] ;
2176reg     [0:0]       tran_state_62 [TR -1:0] ;
2177reg     [0:0]       tran_state_63 [TR -1:0] ;
2178                             
2179reg     [TRW -1:0]  wptr ;   
2180reg     [TRW -1:0]  last_wptr ;
2181                             
2182wire    [M -1:0]    diff_0 ; 
2183wire    [M -1:0]    diff_1 ; 
2184wire    [M -1:0]    diff_2 ; 
2185wire    [M -1:0]    diff_3 ; 
2186wire    [M -1:0]    diff_4 ; 
2187wire    [M -1:0]    diff_5 ; 
2188wire    [M -1:0]    diff_6 ; 
2189wire    [M -1:0]    diff_7 ; 
2190wire    [M -1:0]    diff_8 ; 
2191wire    [M -1:0]    diff_9 ;
2192wire    [M -1:0]    diff_10;
2193wire    [M -1:0]    diff_11;
2194wire    [M -1:0]    diff_12;
2195wire    [M -1:0]    diff_13;
2196wire    [M -1:0]    diff_14;
2197wire    [M -1:0]    diff_15;
2198wire    [M -1:0]    diff_16; 
2199wire    [M -1:0]    diff_17; 
2200wire    [M -1:0]    diff_18; 
2201wire    [M -1:0]    diff_19; 
2202wire    [M -1:0]    diff_20; 
2203wire    [M -1:0]    diff_21; 
2204wire    [M -1:0]    diff_22; 
2205wire    [M -1:0]    diff_23; 
2206wire    [M -1:0]    diff_24; 
2207wire    [M -1:0]    diff_25;
2208wire    [M -1:0]    diff_26;
2209wire    [M -1:0]    diff_27;
2210wire    [M -1:0]    diff_28;
2211wire    [M -1:0]    diff_29;
2212wire    [M -1:0]    diff_30;
2213wire    [M -1:0]    diff_31;
2214wire    [M -1:0]    diff_32; 
2215wire    [M -1:0]    diff_33; 
2216wire    [M -1:0]    diff_34; 
2217wire    [M -1:0]    diff_35; 
2218wire    [M -1:0]    diff_36; 
2219wire    [M -1:0]    diff_37; 
2220wire    [M -1:0]    diff_38; 
2221wire    [M -1:0]    diff_39; 
2222wire    [M -1:0]    diff_40; 
2223wire    [M -1:0]    diff_41;
2224wire    [M -1:0]    diff_42;
2225wire    [M -1:0]    diff_43;
2226wire    [M -1:0]    diff_44;
2227wire    [M -1:0]    diff_45;
2228wire    [M -1:0]    diff_46;
2229wire    [M -1:0]    diff_47;
2230wire    [M -1:0]    diff_48; 
2231wire    [M -1:0]    diff_49; 
2232wire    [M -1:0]    diff_50; 
2233wire    [M -1:0]    diff_51; 
2234wire    [M -1:0]    diff_52; 
2235wire    [M -1:0]    diff_53; 
2236wire    [M -1:0]    diff_54; 
2237wire    [M -1:0]    diff_55; 
2238wire    [M -1:0]    diff_56; 
2239wire    [M -1:0]    diff_57;
2240wire    [M -1:0]    diff_58;
2241wire    [M -1:0]    diff_59;
2242wire    [M -1:0]    diff_60;
2243wire    [M -1:0]    diff_61;
2244wire    [M -1:0]    diff_62;
2245wire    [M -1:0]    diff_63;
2246
2247reg                 nd ;
2248reg     [LW -1:0]   cnt ;
2249reg                 trace ;
2250reg                 trace_en ;
2251wire                trace_done ;
2252reg                 trace_done_s0 ;
2253wire                trace_done_pos ;
2254reg     [TRW -1:0]  trace_cnt ;
2255reg     [L-1:0]     res ;
2256wire    [L-1:0]     res_shift ;
2257wire    [K-2: 0]    init_state_i ;
2258reg     [K-2: 0]    trace_state ;
2259
2260wire    [K-2: 0]    cur_state0 ;
2261wire    [K-2: 0]    cur_state1 ;
2262wire    [K-2: 0]    next_state ;
2263reg     [TRW -1:0]  trace_start_wptr ;
2264wire    [TRW -1:0]  trace_start_pos ;
2265wire    [TRW -1:0]  trace_start_pos0 ;
2266wire    [TRW -1:0]  trace_start_pos1 ;
2267reg                 last_trace ;
2268reg                 last_trace_s0 ;
2269wire                trace2 ;
2270reg                 trace2_s1 ;
2271reg                 trace2_s2 ;
2272reg                 trace_pos ;
2273wire                trace_pos_i ;
2274reg     [LW -1:0]   last_trace_num ;
2275reg                 flush_en ;
2276wire                flush ;
2277reg                 done_i ;
2278
2279wire    [K-2: 0]    state_0 ;
2280wire    [K-2: 0]    state_1 ;
2281wire    [K-2: 0]    state_2 ;
2282wire    [K-2: 0]    state_3 ;
2283wire    [K-2: 0]    state_4 ;
2284wire    [K-2: 0]    state_5 ;
2285wire    [K-2: 0]    state_6 ;
2286wire    [K-2: 0]    state_7 ;
2287wire    [K-2: 0]    state_8 ;
2288wire    [K-2: 0]    state_9 ;
2289wire    [K-2: 0]    state_10 ;
2290wire    [K-2: 0]    state_11 ;
2291wire    [K-2: 0]    state_12 ;
2292wire    [K-2: 0]    state_13 ;
2293wire    [K-2: 0]    state_14 ;
2294wire    [K-2: 0]    state_15 ;
2295
2296wire    [K-2: 0]    state_s1_0 ;
2297wire    [K-2: 0]    state_s1_1 ;
2298wire    [K-2: 0]    state_s1_2 ;
2299wire    [K-2: 0]    state_s1_3 ;
2300
2301wire    [K-2: 0]    state_s2_0 ;
2302
2303wire    [M-1: 0]    max_0  ;
2304wire    [M-1: 0]    max_1  ;
2305wire    [M-1: 0]    max_2  ;
2306wire    [M-1: 0]    max_3  ;
2307wire    [M-1: 0]    max_4  ;
2308wire    [M-1: 0]    max_5  ;
2309wire    [M-1: 0]    max_6  ;
2310wire    [M-1: 0]    max_7  ;
2311wire    [M-1: 0]    max_8  ;
2312wire    [M-1: 0]    max_9  ;
2313wire    [M-1: 0]    max_10 ;
2314wire    [M-1: 0]    max_11 ;
2315wire    [M-1: 0]    max_12 ;
2316wire    [M-1: 0]    max_13 ;
2317wire    [M-1: 0]    max_14 ;
2318wire    [M-1: 0]    max_15 ;
2319
2320wire    [M-1: 0]    max_s1_0 ;
2321wire    [M-1: 0]    max_s1_1 ;
2322wire    [M-1: 0]    max_s1_2 ;
2323wire    [M-1: 0]    max_s1_3 ;
2324
2325//reg     [M-1: 0]    max_s1_0_s0 ;
2326//reg     [M-1: 0]    max_s1_1_s0 ;
2327//reg     [M-1: 0]    max_s1_2_s0 ;
2328//reg     [M-1: 0]    max_s1_3_s0 ;
2329
2330//wire    [M-1: 0]    max_s2_0 ;
2331
2332wire    [N -1:0]    tran_all_0 ;
2333wire    [N -1:0]    tran_all_1 ;
2334
2335reg     [6:0]       flush_cnt ;
2336wire    [LW -1:0]   rem_i ;
2337reg     [LW -1:0]   last_cnt ;
2338wire    [LW -1:0]   last_cnt_next ;
2339reg                 last_trace_d0 ;
2340reg                 last_trace_d1 ;
2341reg                 last_trace_d2 ;
2342reg                 last_trace_d3 ;
2343reg                 last_trace_d4 ;
2344reg                 last_trace_d5 ;
2345reg                 one_more_out ;
2346reg                 in_dec ;
2347wire                dv_in_gate ;
2348reg                 trace_pos_s0 ; 
2349
2350//======================================
2351// Main body of code
2352//======================================
2353assign dv_out = trace_done_pos | one_more_out ;
2354assign res_shift = res << last_trace_num ;
2355assign dout = res_shift [L -1:C] ;
2356assign remain = last_trace ? rem_i : 0 ;
2357assign done = dv_out & done_i ;
2358assign rem_i = ~last_cnt_next[LW-1] ? 0 : last_cnt ;
2359
2360//assign dv_in_gate = dv_in & in_dec ;
2361assign dv_in_gate = dv_in & in_dec & (~packet_end) ;
2362
2363always @ (posedge clk or negedge nrst)
2364  if (~nrst)
2365    in_dec <= 1'b0 ;
2366  else if (packet_start)
2367    in_dec <= 1'b1 ;
2368  else if (packet_end)
2369    in_dec <= 1'b0 ;
2370
2371
2372//{coe_0 coe_1}, generated from C model gen_table
2373//0 0;
2374//0 1;
2375//1 0;
2376//1 1;
2377//1 0;
2378//1 1;
2379//0 0;
2380//0 1;
2381
2382assign branch_00 = {llr1[SW -1], llr1} + {llr0[SW -1], llr0} ;  //LLR(bit1) + LLR(bit0)
2383assign branch_01 = {llr1[SW -1], llr1} ;                        //LLR(bit1)
2384assign branch_10 = {llr0[SW -1], llr0} ;                        //LLR(bit0)
2385assign branch_11 = 0  ;                                         //0
2386
2387assign branch_A_0 = branch_00 ;
2388assign branch_B_0 = branch_11 ;
2389assign branch_A_1 = branch_10 ;
2390assign branch_B_1 = branch_01 ;
2391assign branch_A_2 = branch_00 ;
2392assign branch_B_2 = branch_11 ;
2393assign branch_A_3 = branch_10 ;
2394assign branch_B_3 = branch_01 ;
2395assign branch_A_4 = branch_11 ;
2396assign branch_B_4 = branch_00 ;
2397assign branch_A_5 = branch_01 ;
2398assign branch_B_5 = branch_10 ;
2399assign branch_A_6 = branch_11 ;
2400assign branch_B_6 = branch_00 ;
2401assign branch_A_7 = branch_01 ;
2402assign branch_B_7 = branch_10 ;
2403assign branch_A_8 = branch_11 ;
2404assign branch_B_8 = branch_00 ;
2405assign branch_A_9 = branch_01 ;
2406assign branch_B_9 = branch_10 ;
2407assign branch_A_10 = branch_11 ;
2408assign branch_B_10 = branch_00 ;
2409assign branch_A_11 = branch_01 ;
2410assign branch_B_11 = branch_10 ;
2411assign branch_A_12 = branch_00 ;
2412assign branch_B_12 = branch_11 ;
2413assign branch_A_13 = branch_10 ;
2414assign branch_B_13 = branch_01 ;
2415assign branch_A_14 = branch_00 ;
2416assign branch_B_14 = branch_11 ;
2417assign branch_A_15 = branch_10 ;
2418assign branch_B_15 = branch_01 ;
2419assign branch_A_16 = branch_01 ;
2420assign branch_B_16 = branch_10 ;
2421assign branch_A_17 = branch_11 ;
2422assign branch_B_17 = branch_00 ;
2423assign branch_A_18 = branch_01 ;
2424assign branch_B_18 = branch_10 ;
2425assign branch_A_19 = branch_11 ;
2426assign branch_B_19 = branch_00 ;
2427assign branch_A_20 = branch_10 ;
2428assign branch_B_20 = branch_01 ;
2429assign branch_A_21 = branch_00 ;
2430assign branch_B_21 = branch_11 ;
2431assign branch_A_22 = branch_10 ;
2432assign branch_B_22 = branch_01 ;
2433assign branch_A_23 = branch_00 ;
2434assign branch_B_23 = branch_11 ;
2435assign branch_A_24 = branch_10 ;
2436assign branch_B_24 = branch_01 ;
2437assign branch_A_25 = branch_00 ;
2438assign branch_B_25 = branch_11 ;
2439assign branch_A_26 = branch_10 ;
2440assign branch_B_26 = branch_01 ;
2441assign branch_A_27 = branch_00 ;
2442assign branch_B_27 = branch_11 ;
2443assign branch_A_28 = branch_01 ;
2444assign branch_B_28 = branch_10 ;
2445assign branch_A_29 = branch_11 ;
2446assign branch_B_29 = branch_00 ;
2447assign branch_A_30 = branch_01 ;
2448assign branch_B_30 = branch_10 ;
2449assign branch_A_31 = branch_11 ;
2450assign branch_B_31 = branch_00 ;
2451
2452assign branch_A_32 = branch_B_0 ;
2453assign branch_B_32 = branch_A_0 ;
2454assign branch_A_33 = branch_B_1 ;
2455assign branch_B_33 = branch_A_1 ;
2456assign branch_A_34 = branch_B_2 ;
2457assign branch_B_34 = branch_A_2 ;
2458assign branch_A_35 = branch_B_3 ;
2459assign branch_B_35 = branch_A_3 ;
2460assign branch_A_36 = branch_B_4 ;
2461assign branch_B_36 = branch_A_4 ;
2462assign branch_A_37 = branch_B_5 ;
2463assign branch_B_37 = branch_A_5 ;
2464assign branch_A_38 = branch_B_6 ;
2465assign branch_B_38 = branch_A_6 ;
2466assign branch_A_39 = branch_B_7 ;
2467assign branch_B_39 = branch_A_7 ;
2468assign branch_A_40 = branch_B_8 ;
2469assign branch_B_40 = branch_A_8 ;
2470assign branch_A_41 = branch_B_9 ;
2471assign branch_B_41 = branch_A_9 ;
2472assign branch_A_42 = branch_B_10 ;
2473assign branch_B_42 = branch_A_10 ;
2474assign branch_A_43 = branch_B_11 ;
2475assign branch_B_43 = branch_A_11 ;
2476assign branch_A_44 = branch_B_12 ;
2477assign branch_B_44 = branch_A_12 ;
2478assign branch_A_45 = branch_B_13 ;
2479assign branch_B_45 = branch_A_13 ;
2480assign branch_A_46 = branch_B_14 ;
2481assign branch_B_46 = branch_A_14 ;
2482assign branch_A_47 = branch_B_15 ;
2483assign branch_B_47 = branch_A_15 ;
2484assign branch_A_48 = branch_B_16 ;
2485assign branch_B_48 = branch_A_16 ;
2486assign branch_A_49 = branch_B_17 ;
2487assign branch_B_49 = branch_A_17 ;
2488assign branch_A_50 = branch_B_18 ;
2489assign branch_B_50 = branch_A_18 ;
2490assign branch_A_51 = branch_B_19 ;
2491assign branch_B_51 = branch_A_19 ;
2492assign branch_A_52 = branch_B_20 ;
2493assign branch_B_52 = branch_A_20 ;
2494assign branch_A_53 = branch_B_21 ;
2495assign branch_B_53 = branch_A_21 ;
2496assign branch_A_54 = branch_B_22 ;
2497assign branch_B_54 = branch_A_22 ;
2498assign branch_A_55 = branch_B_23 ;
2499assign branch_B_55 = branch_A_23 ;
2500assign branch_A_56 = branch_B_24 ;
2501assign branch_B_56 = branch_A_24 ;
2502assign branch_A_57 = branch_B_25 ;
2503assign branch_B_57 = branch_A_25 ;
2504assign branch_A_58 = branch_B_26 ;
2505assign branch_B_58 = branch_A_26 ;
2506assign branch_A_59 = branch_B_27 ;
2507assign branch_B_59 = branch_A_27 ;
2508assign branch_A_60 = branch_B_28 ;
2509assign branch_B_60 = branch_A_28 ;
2510assign branch_A_61 = branch_B_29 ;
2511assign branch_B_61 = branch_A_29 ;
2512assign branch_A_62 = branch_B_30 ;
2513assign branch_B_62 = branch_A_30 ;
2514assign branch_A_63 = branch_B_31 ;
2515assign branch_B_63 = branch_A_31 ;
2516
2517assign metric_A_0 = metric_0 + {{(M -SW -1){branch_A_0[SW]}}, branch_A_0} ;
2518assign metric_B_0 = metric_1 + {{(M -SW -1){branch_B_0[SW]}}, branch_B_0} ;
2519assign metric_A_1 = metric_2 + {{(M -SW -1){branch_A_1[SW]}}, branch_A_1} ;
2520assign metric_B_1 = metric_3 + {{(M -SW -1){branch_B_1[SW]}}, branch_B_1} ;
2521assign metric_A_2 = metric_4 + {{(M -SW -1){branch_A_2[SW]}}, branch_A_2} ;
2522assign metric_B_2 = metric_5 + {{(M -SW -1){branch_B_2[SW]}}, branch_B_2} ;
2523assign metric_A_3 = metric_6 + {{(M -SW -1){branch_A_3[SW]}}, branch_A_3} ;
2524assign metric_B_3 = metric_7 + {{(M -SW -1){branch_B_3[SW]}}, branch_B_3} ;
2525assign metric_A_4 = metric_8 + {{(M -SW -1){branch_A_4[SW]}}, branch_A_4} ;
2526assign metric_B_4 = metric_9 + {{(M -SW -1){branch_B_4[SW]}}, branch_B_4} ;
2527assign metric_A_5 = metric_10 + {{(M -SW -1){branch_A_5[SW]}}, branch_A_5} ;
2528assign metric_B_5 = metric_11 + {{(M -SW -1){branch_B_5[SW]}}, branch_B_5} ;
2529assign metric_A_6 = metric_12 + {{(M -SW -1){branch_A_6[SW]}}, branch_A_6} ;
2530assign metric_B_6 = metric_13 + {{(M -SW -1){branch_B_6[SW]}}, branch_B_6} ;
2531assign metric_A_7 = metric_14 + {{(M -SW -1){branch_A_7[SW]}}, branch_A_7} ;
2532assign metric_B_7 = metric_15 + {{(M -SW -1){branch_B_7[SW]}}, branch_B_7} ;
2533assign metric_A_8 = metric_16 + {{(M -SW -1){branch_A_8[SW]}}, branch_A_8} ;
2534assign metric_B_8 = metric_17 + {{(M -SW -1){branch_B_8[SW]}}, branch_B_8} ;
2535assign metric_A_9 = metric_18 + {{(M -SW -1){branch_A_9[SW]}}, branch_A_9} ;
2536assign metric_B_9 = metric_19 + {{(M -SW -1){branch_B_9[SW]}}, branch_B_9} ;
2537assign metric_A_10 = metric_20 + {{(M -SW -1){branch_A_10[SW]}}, branch_A_10} ;
2538assign metric_B_10 = metric_21 + {{(M -SW -1){branch_B_10[SW]}}, branch_B_10} ;
2539assign metric_A_11 = metric_22 + {{(M -SW -1){branch_A_11[SW]}}, branch_A_11} ;
2540assign metric_B_11 = metric_23 + {{(M -SW -1){branch_B_11[SW]}}, branch_B_11} ;
2541assign metric_A_12 = metric_24 + {{(M -SW -1){branch_A_12[SW]}}, branch_A_12} ;
2542assign metric_B_12 = metric_25 + {{(M -SW -1){branch_B_12[SW]}}, branch_B_12} ;
2543assign metric_A_13 = metric_26 + {{(M -SW -1){branch_A_13[SW]}}, branch_A_13} ;
2544assign metric_B_13 = metric_27 + {{(M -SW -1){branch_B_13[SW]}}, branch_B_13} ;
2545assign metric_A_14 = metric_28 + {{(M -SW -1){branch_A_14[SW]}}, branch_A_14} ;
2546assign metric_B_14 = metric_29 + {{(M -SW -1){branch_B_14[SW]}}, branch_B_14} ;
2547assign metric_A_15 = metric_30 + {{(M -SW -1){branch_A_15[SW]}}, branch_A_15} ;
2548assign metric_B_15 = metric_31 + {{(M -SW -1){branch_B_15[SW]}}, branch_B_15} ;
2549assign metric_A_16 = metric_32 + {{(M -SW -1){branch_A_16[SW]}}, branch_A_16} ;
2550assign metric_B_16 = metric_33 + {{(M -SW -1){branch_B_16[SW]}}, branch_B_16} ;
2551assign metric_A_17 = metric_34 + {{(M -SW -1){branch_A_17[SW]}}, branch_A_17} ;
2552assign metric_B_17 = metric_35 + {{(M -SW -1){branch_B_17[SW]}}, branch_B_17} ;
2553assign metric_A_18 = metric_36 + {{(M -SW -1){branch_A_18[SW]}}, branch_A_18} ;
2554assign metric_B_18 = metric_37 + {{(M -SW -1){branch_B_18[SW]}}, branch_B_18} ;
2555assign metric_A_19 = metric_38 + {{(M -SW -1){branch_A_19[SW]}}, branch_A_19} ;
2556assign metric_B_19 = metric_39 + {{(M -SW -1){branch_B_19[SW]}}, branch_B_19} ;
2557assign metric_A_20 = metric_40 + {{(M -SW -1){branch_A_20[SW]}}, branch_A_20} ;
2558assign metric_B_20 = metric_41 + {{(M -SW -1){branch_B_20[SW]}}, branch_B_20} ;
2559assign metric_A_21 = metric_42 + {{(M -SW -1){branch_A_21[SW]}}, branch_A_21} ;
2560assign metric_B_21 = metric_43 + {{(M -SW -1){branch_B_21[SW]}}, branch_B_21} ;
2561assign metric_A_22 = metric_44 + {{(M -SW -1){branch_A_22[SW]}}, branch_A_22} ;
2562assign metric_B_22 = metric_45 + {{(M -SW -1){branch_B_22[SW]}}, branch_B_22} ;
2563assign metric_A_23 = metric_46 + {{(M -SW -1){branch_A_23[SW]}}, branch_A_23} ;
2564assign metric_B_23 = metric_47 + {{(M -SW -1){branch_B_23[SW]}}, branch_B_23} ;
2565assign metric_A_24 = metric_48 + {{(M -SW -1){branch_A_24[SW]}}, branch_A_24} ;
2566assign metric_B_24 = metric_49 + {{(M -SW -1){branch_B_24[SW]}}, branch_B_24} ;
2567assign metric_A_25 = metric_50 + {{(M -SW -1){branch_A_25[SW]}}, branch_A_25} ;
2568assign metric_B_25 = metric_51 + {{(M -SW -1){branch_B_25[SW]}}, branch_B_25} ;
2569assign metric_A_26 = metric_52 + {{(M -SW -1){branch_A_26[SW]}}, branch_A_26} ;
2570assign metric_B_26 = metric_53 + {{(M -SW -1){branch_B_26[SW]}}, branch_B_26} ;
2571assign metric_A_27 = metric_54 + {{(M -SW -1){branch_A_27[SW]}}, branch_A_27} ;
2572assign metric_B_27 = metric_55 + {{(M -SW -1){branch_B_27[SW]}}, branch_B_27} ;
2573assign metric_A_28 = metric_56 + {{(M -SW -1){branch_A_28[SW]}}, branch_A_28} ;
2574assign metric_B_28 = metric_57 + {{(M -SW -1){branch_B_28[SW]}}, branch_B_28} ;
2575assign metric_A_29 = metric_58 + {{(M -SW -1){branch_A_29[SW]}}, branch_A_29} ;
2576assign metric_B_29 = metric_59 + {{(M -SW -1){branch_B_29[SW]}}, branch_B_29} ;
2577assign metric_A_30 = metric_60 + {{(M -SW -1){branch_A_30[SW]}}, branch_A_30} ;
2578assign metric_B_30 = metric_61 + {{(M -SW -1){branch_B_30[SW]}}, branch_B_30} ;
2579assign metric_A_31 = metric_62 + {{(M -SW -1){branch_A_31[SW]}}, branch_A_31} ;
2580assign metric_B_31 = metric_63 + {{(M -SW -1){branch_B_31[SW]}}, branch_B_31} ;
2581assign metric_A_32 = metric_0 + {{(M -SW -1){branch_A_32[SW]}}, branch_A_32} ;
2582assign metric_B_32 = metric_1 + {{(M -SW -1){branch_B_32[SW]}}, branch_B_32} ;
2583assign metric_A_33 = metric_2 + {{(M -SW -1){branch_A_33[SW]}}, branch_A_33} ;
2584assign metric_B_33 = metric_3 + {{(M -SW -1){branch_B_33[SW]}}, branch_B_33} ;
2585assign metric_A_34 = metric_4 + {{(M -SW -1){branch_A_34[SW]}}, branch_A_34} ;
2586assign metric_B_34 = metric_5 + {{(M -SW -1){branch_B_34[SW]}}, branch_B_34} ;
2587assign metric_A_35 = metric_6 + {{(M -SW -1){branch_A_35[SW]}}, branch_A_35} ;
2588assign metric_B_35 = metric_7 + {{(M -SW -1){branch_B_35[SW]}}, branch_B_35} ;
2589assign metric_A_36 = metric_8 + {{(M -SW -1){branch_A_36[SW]}}, branch_A_36} ;
2590assign metric_B_36 = metric_9 + {{(M -SW -1){branch_B_36[SW]}}, branch_B_36} ;
2591assign metric_A_37 = metric_10 + {{(M -SW -1){branch_A_37[SW]}}, branch_A_37} ;
2592assign metric_B_37 = metric_11 + {{(M -SW -1){branch_B_37[SW]}}, branch_B_37} ;
2593assign metric_A_38 = metric_12 + {{(M -SW -1){branch_A_38[SW]}}, branch_A_38} ;
2594assign metric_B_38 = metric_13 + {{(M -SW -1){branch_B_38[SW]}}, branch_B_38} ;
2595assign metric_A_39 = metric_14 + {{(M -SW -1){branch_A_39[SW]}}, branch_A_39} ;
2596assign metric_B_39 = metric_15 + {{(M -SW -1){branch_B_39[SW]}}, branch_B_39} ;
2597assign metric_A_40 = metric_16 + {{(M -SW -1){branch_A_40[SW]}}, branch_A_40} ;
2598assign metric_B_40 = metric_17 + {{(M -SW -1){branch_B_40[SW]}}, branch_B_40} ;
2599assign metric_A_41 = metric_18 + {{(M -SW -1){branch_A_41[SW]}}, branch_A_41} ;
2600assign metric_B_41 = metric_19 + {{(M -SW -1){branch_B_41[SW]}}, branch_B_41} ;
2601assign metric_A_42 = metric_20 + {{(M -SW -1){branch_A_42[SW]}}, branch_A_42} ;
2602assign metric_B_42 = metric_21 + {{(M -SW -1){branch_B_42[SW]}}, branch_B_42} ;
2603assign metric_A_43 = metric_22 + {{(M -SW -1){branch_A_43[SW]}}, branch_A_43} ;
2604assign metric_B_43 = metric_23 + {{(M -SW -1){branch_B_43[SW]}}, branch_B_43} ;
2605assign metric_A_44 = metric_24 + {{(M -SW -1){branch_A_44[SW]}}, branch_A_44} ;
2606assign metric_B_44 = metric_25 + {{(M -SW -1){branch_B_44[SW]}}, branch_B_44} ;
2607assign metric_A_45 = metric_26 + {{(M -SW -1){branch_A_45[SW]}}, branch_A_45} ;
2608assign metric_B_45 = metric_27 + {{(M -SW -1){branch_B_45[SW]}}, branch_B_45} ;
2609assign metric_A_46 = metric_28 + {{(M -SW -1){branch_A_46[SW]}}, branch_A_46} ;
2610assign metric_B_46 = metric_29 + {{(M -SW -1){branch_B_46[SW]}}, branch_B_46} ;
2611assign metric_A_47 = metric_30 + {{(M -SW -1){branch_A_47[SW]}}, branch_A_47} ;
2612assign metric_B_47 = metric_31 + {{(M -SW -1){branch_B_47[SW]}}, branch_B_47} ;
2613assign metric_A_48 = metric_32 + {{(M -SW -1){branch_A_48[SW]}}, branch_A_48} ;
2614assign metric_B_48 = metric_33 + {{(M -SW -1){branch_B_48[SW]}}, branch_B_48} ;
2615assign metric_A_49 = metric_34 + {{(M -SW -1){branch_A_49[SW]}}, branch_A_49} ;
2616assign metric_B_49 = metric_35 + {{(M -SW -1){branch_B_49[SW]}}, branch_B_49} ;
2617assign metric_A_50 = metric_36 + {{(M -SW -1){branch_A_50[SW]}}, branch_A_50} ;
2618assign metric_B_50 = metric_37 + {{(M -SW -1){branch_B_50[SW]}}, branch_B_50} ;
2619assign metric_A_51 = metric_38 + {{(M -SW -1){branch_A_51[SW]}}, branch_A_51} ;
2620assign metric_B_51 = metric_39 + {{(M -SW -1){branch_B_51[SW]}}, branch_B_51} ;
2621assign metric_A_52 = metric_40 + {{(M -SW -1){branch_A_52[SW]}}, branch_A_52} ;
2622assign metric_B_52 = metric_41 + {{(M -SW -1){branch_B_52[SW]}}, branch_B_52} ;
2623assign metric_A_53 = metric_42 + {{(M -SW -1){branch_A_53[SW]}}, branch_A_53} ;
2624assign metric_B_53 = metric_43 + {{(M -SW -1){branch_B_53[SW]}}, branch_B_53} ;
2625assign metric_A_54 = metric_44 + {{(M -SW -1){branch_A_54[SW]}}, branch_A_54} ;
2626assign metric_B_54 = metric_45 + {{(M -SW -1){branch_B_54[SW]}}, branch_B_54} ;
2627assign metric_A_55 = metric_46 + {{(M -SW -1){branch_A_55[SW]}}, branch_A_55} ;
2628assign metric_B_55 = metric_47 + {{(M -SW -1){branch_B_55[SW]}}, branch_B_55} ;
2629assign metric_A_56 = metric_48 + {{(M -SW -1){branch_A_56[SW]}}, branch_A_56} ;
2630assign metric_B_56 = metric_49 + {{(M -SW -1){branch_B_56[SW]}}, branch_B_56} ;
2631assign metric_A_57 = metric_50 + {{(M -SW -1){branch_A_57[SW]}}, branch_A_57} ;
2632assign metric_B_57 = metric_51 + {{(M -SW -1){branch_B_57[SW]}}, branch_B_57} ;
2633assign metric_A_58 = metric_52 + {{(M -SW -1){branch_A_58[SW]}}, branch_A_58} ;
2634assign metric_B_58 = metric_53 + {{(M -SW -1){branch_B_58[SW]}}, branch_B_58} ;
2635assign metric_A_59 = metric_54 + {{(M -SW -1){branch_A_59[SW]}}, branch_A_59} ;
2636assign metric_B_59 = metric_55 + {{(M -SW -1){branch_B_59[SW]}}, branch_B_59} ;
2637assign metric_A_60 = metric_56 + {{(M -SW -1){branch_A_60[SW]}}, branch_A_60} ;
2638assign metric_B_60 = metric_57 + {{(M -SW -1){branch_B_60[SW]}}, branch_B_60} ;
2639assign metric_A_61 = metric_58 + {{(M -SW -1){branch_A_61[SW]}}, branch_A_61} ;
2640assign metric_B_61 = metric_59 + {{(M -SW -1){branch_B_61[SW]}}, branch_B_61} ;
2641assign metric_A_62 = metric_60 + {{(M -SW -1){branch_A_62[SW]}}, branch_A_62} ;
2642assign metric_B_62 = metric_61 + {{(M -SW -1){branch_B_62[SW]}}, branch_B_62} ;
2643assign metric_A_63 = metric_62 + {{(M -SW -1){branch_A_63[SW]}}, branch_A_63} ;
2644assign metric_B_63 = metric_63 + {{(M -SW -1){branch_B_63[SW]}}, branch_B_63} ;
2645
2646assign diff_0 = metric_A_0 - metric_B_0 ;
2647assign diff_1 = metric_A_1 - metric_B_1 ;
2648assign diff_2 = metric_A_2 - metric_B_2 ;
2649assign diff_3 = metric_A_3 - metric_B_3 ;
2650assign diff_4 = metric_A_4 - metric_B_4 ;
2651assign diff_5 = metric_A_5 - metric_B_5 ;
2652assign diff_6 = metric_A_6 - metric_B_6 ;
2653assign diff_7 = metric_A_7 - metric_B_7 ;
2654assign diff_8 = metric_A_8 - metric_B_8 ;
2655assign diff_9 = metric_A_9 - metric_B_9 ;
2656assign diff_10 = metric_A_10 - metric_B_10 ;
2657assign diff_11 = metric_A_11 - metric_B_11 ;
2658assign diff_12 = metric_A_12 - metric_B_12 ;
2659assign diff_13 = metric_A_13 - metric_B_13 ;
2660assign diff_14 = metric_A_14 - metric_B_14 ;
2661assign diff_15 = metric_A_15 - metric_B_15 ;
2662assign diff_16 = metric_A_16 - metric_B_16 ;
2663assign diff_17 = metric_A_17 - metric_B_17 ;
2664assign diff_18 = metric_A_18 - metric_B_18 ;
2665assign diff_19 = metric_A_19 - metric_B_19 ;
2666assign diff_20 = metric_A_20 - metric_B_20 ;
2667assign diff_21 = metric_A_21 - metric_B_21 ;
2668assign diff_22 = metric_A_22 - metric_B_22 ;
2669assign diff_23 = metric_A_23 - metric_B_23 ;
2670assign diff_24 = metric_A_24 - metric_B_24 ;
2671assign diff_25 = metric_A_25 - metric_B_25 ;
2672assign diff_26 = metric_A_26 - metric_B_26 ;
2673assign diff_27 = metric_A_27 - metric_B_27 ;
2674assign diff_28 = metric_A_28 - metric_B_28 ;
2675assign diff_29 = metric_A_29 - metric_B_29 ;
2676assign diff_30 = metric_A_30 - metric_B_30 ;
2677assign diff_31 = metric_A_31 - metric_B_31 ;
2678assign diff_32 = metric_A_32 - metric_B_32 ;
2679assign diff_33 = metric_A_33 - metric_B_33 ;
2680assign diff_34 = metric_A_34 - metric_B_34 ;
2681assign diff_35 = metric_A_35 - metric_B_35 ;
2682assign diff_36 = metric_A_36 - metric_B_36 ;
2683assign diff_37 = metric_A_37 - metric_B_37 ;
2684assign diff_38 = metric_A_38 - metric_B_38 ;
2685assign diff_39 = metric_A_39 - metric_B_39 ;
2686assign diff_40 = metric_A_40 - metric_B_40 ;
2687assign diff_41 = metric_A_41 - metric_B_41 ;
2688assign diff_42 = metric_A_42 - metric_B_42 ;
2689assign diff_43 = metric_A_43 - metric_B_43 ;
2690assign diff_44 = metric_A_44 - metric_B_44 ;
2691assign diff_45 = metric_A_45 - metric_B_45 ;
2692assign diff_46 = metric_A_46 - metric_B_46 ;
2693assign diff_47 = metric_A_47 - metric_B_47 ;
2694assign diff_48 = metric_A_48 - metric_B_48 ;
2695assign diff_49 = metric_A_49 - metric_B_49 ;
2696assign diff_50 = metric_A_50 - metric_B_50 ;
2697assign diff_51 = metric_A_51 - metric_B_51 ;
2698assign diff_52 = metric_A_52 - metric_B_52 ;
2699assign diff_53 = metric_A_53 - metric_B_53 ;
2700assign diff_54 = metric_A_54 - metric_B_54 ;
2701assign diff_55 = metric_A_55 - metric_B_55 ;
2702assign diff_56 = metric_A_56 - metric_B_56 ;
2703assign diff_57 = metric_A_57 - metric_B_57 ;
2704assign diff_58 = metric_A_58 - metric_B_58 ;
2705assign diff_59 = metric_A_59 - metric_B_59 ;
2706assign diff_60 = metric_A_60 - metric_B_60 ;
2707assign diff_61 = metric_A_61 - metric_B_61 ;
2708assign diff_62 = metric_A_62 - metric_B_62 ;
2709assign diff_63 = metric_A_63 - metric_B_63 ;
2710
2711assign metric_next_0 = diff_0 [M -1] ? metric_B_0 : metric_A_0 ;
2712assign metric_next_1 = diff_1 [M -1] ? metric_B_1 : metric_A_1 ;
2713assign metric_next_2 = diff_2 [M -1] ? metric_B_2 : metric_A_2 ;
2714assign metric_next_3 = diff_3 [M -1] ? metric_B_3 : metric_A_3 ;
2715assign metric_next_4 = diff_4 [M -1] ? metric_B_4 : metric_A_4 ;
2716assign metric_next_5 = diff_5 [M -1] ? metric_B_5 : metric_A_5 ;
2717assign metric_next_6 = diff_6 [M -1] ? metric_B_6 : metric_A_6 ;
2718assign metric_next_7 = diff_7 [M -1] ? metric_B_7 : metric_A_7 ;
2719assign metric_next_8 = diff_8 [M -1] ? metric_B_8 : metric_A_8 ;
2720assign metric_next_9 = diff_9 [M -1] ? metric_B_9 : metric_A_9 ;
2721assign metric_next_10 = diff_10 [M -1] ? metric_B_10 : metric_A_10 ;
2722assign metric_next_11 = diff_11 [M -1] ? metric_B_11 : metric_A_11 ;
2723assign metric_next_12 = diff_12 [M -1] ? metric_B_12 : metric_A_12 ;
2724assign metric_next_13 = diff_13 [M -1] ? metric_B_13 : metric_A_13 ;
2725assign metric_next_14 = diff_14 [M -1] ? metric_B_14 : metric_A_14 ;
2726assign metric_next_15 = diff_15 [M -1] ? metric_B_15 : metric_A_15 ;
2727assign metric_next_16 = diff_16 [M -1] ? metric_B_16 : metric_A_16 ;
2728assign metric_next_17 = diff_17 [M -1] ? metric_B_17 : metric_A_17 ;
2729assign metric_next_18 = diff_18 [M -1] ? metric_B_18 : metric_A_18 ;
2730assign metric_next_19 = diff_19 [M -1] ? metric_B_19 : metric_A_19 ;
2731assign metric_next_20 = diff_20 [M -1] ? metric_B_20 : metric_A_20 ;
2732assign metric_next_21 = diff_21 [M -1] ? metric_B_21 : metric_A_21 ;
2733assign metric_next_22 = diff_22 [M -1] ? metric_B_22 : metric_A_22 ;
2734assign metric_next_23 = diff_23 [M -1] ? metric_B_23 : metric_A_23 ;
2735assign metric_next_24 = diff_24 [M -1] ? metric_B_24 : metric_A_24 ;
2736assign metric_next_25 = diff_25 [M -1] ? metric_B_25 : metric_A_25 ;
2737assign metric_next_26 = diff_26 [M -1] ? metric_B_26 : metric_A_26 ;
2738assign metric_next_27 = diff_27 [M -1] ? metric_B_27 : metric_A_27 ;
2739assign metric_next_28 = diff_28 [M -1] ? metric_B_28 : metric_A_28 ;
2740assign metric_next_29 = diff_29 [M -1] ? metric_B_29 : metric_A_29 ;
2741assign metric_next_30 = diff_30 [M -1] ? metric_B_30 : metric_A_30 ;
2742assign metric_next_31 = diff_31 [M -1] ? metric_B_31 : metric_A_31 ;
2743assign metric_next_32 = diff_32 [M -1] ? metric_B_32 : metric_A_32 ;
2744assign metric_next_33 = diff_33 [M -1] ? metric_B_33 : metric_A_33 ;
2745assign metric_next_34 = diff_34 [M -1] ? metric_B_34 : metric_A_34 ;
2746assign metric_next_35 = diff_35 [M -1] ? metric_B_35 : metric_A_35 ;
2747assign metric_next_36 = diff_36 [M -1] ? metric_B_36 : metric_A_36 ;
2748assign metric_next_37 = diff_37 [M -1] ? metric_B_37 : metric_A_37 ;
2749assign metric_next_38 = diff_38 [M -1] ? metric_B_38 : metric_A_38 ;
2750assign metric_next_39 = diff_39 [M -1] ? metric_B_39 : metric_A_39 ;
2751assign metric_next_40 = diff_40 [M -1] ? metric_B_40 : metric_A_40 ;
2752assign metric_next_41 = diff_41 [M -1] ? metric_B_41 : metric_A_41 ;
2753assign metric_next_42 = diff_42 [M -1] ? metric_B_42 : metric_A_42 ;
2754assign metric_next_43 = diff_43 [M -1] ? metric_B_43 : metric_A_43 ;
2755assign metric_next_44 = diff_44 [M -1] ? metric_B_44 : metric_A_44 ;
2756assign metric_next_45 = diff_45 [M -1] ? metric_B_45 : metric_A_45 ;
2757assign metric_next_46 = diff_46 [M -1] ? metric_B_46 : metric_A_46 ;
2758assign metric_next_47 = diff_47 [M -1] ? metric_B_47 : metric_A_47 ;
2759assign metric_next_48 = diff_48 [M -1] ? metric_B_48 : metric_A_48 ;
2760assign metric_next_49 = diff_49 [M -1] ? metric_B_49 : metric_A_49 ;
2761assign metric_next_50 = diff_50 [M -1] ? metric_B_50 : metric_A_50 ;
2762assign metric_next_51 = diff_51 [M -1] ? metric_B_51 : metric_A_51 ;
2763assign metric_next_52 = diff_52 [M -1] ? metric_B_52 : metric_A_52 ;
2764assign metric_next_53 = diff_53 [M -1] ? metric_B_53 : metric_A_53 ;
2765assign metric_next_54 = diff_54 [M -1] ? metric_B_54 : metric_A_54 ;
2766assign metric_next_55 = diff_55 [M -1] ? metric_B_55 : metric_A_55 ;
2767assign metric_next_56 = diff_56 [M -1] ? metric_B_56 : metric_A_56 ;
2768assign metric_next_57 = diff_57 [M -1] ? metric_B_57 : metric_A_57 ;
2769assign metric_next_58 = diff_58 [M -1] ? metric_B_58 : metric_A_58 ;
2770assign metric_next_59 = diff_59 [M -1] ? metric_B_59 : metric_A_59 ;
2771assign metric_next_60 = diff_60 [M -1] ? metric_B_60 : metric_A_60 ;
2772assign metric_next_61 = diff_61 [M -1] ? metric_B_61 : metric_A_61 ;
2773assign metric_next_62 = diff_62 [M -1] ? metric_B_62 : metric_A_62 ;
2774assign metric_next_63 = diff_63 [M -1] ? metric_B_63 : metric_A_63 ;
2775
2776always @ (posedge clk or negedge nrst)
2777  if (~nrst)
2778  begin
2779    metric_0  <= 10 ;
2780    metric_1  <= 0 ;
2781    metric_2  <= 0 ;
2782    metric_3  <= 0 ;
2783    metric_4  <= 0 ;
2784    metric_5  <= 0 ;
2785    metric_6  <= 0 ;
2786    metric_7  <= 0 ;
2787    metric_8  <= 0 ;
2788    metric_9  <= 0 ;
2789    metric_10 <= 0 ;
2790    metric_11 <= 0 ;
2791    metric_12 <= 0 ;
2792    metric_13 <= 0 ;
2793    metric_14 <= 0 ;
2794    metric_15 <= 0 ;
2795    metric_16 <= 0 ; 
2796    metric_17 <= 0 ; 
2797    metric_18 <= 0 ; 
2798    metric_19 <= 0 ; 
2799    metric_20 <= 0 ; 
2800    metric_21 <= 0 ; 
2801    metric_22 <= 0 ; 
2802    metric_23 <= 0 ; 
2803    metric_24 <= 0 ; 
2804    metric_25 <= 0 ; 
2805    metric_26 <= 0 ; 
2806    metric_27 <= 0 ; 
2807    metric_28 <= 0 ; 
2808    metric_29 <= 0 ; 
2809    metric_30 <= 0 ; 
2810    metric_31 <= 0 ; 
2811    metric_32 <= 0 ; 
2812    metric_33 <= 0 ; 
2813    metric_34 <= 0 ; 
2814    metric_35 <= 0 ; 
2815    metric_36 <= 0 ; 
2816    metric_37 <= 0 ; 
2817    metric_38 <= 0 ; 
2818    metric_39 <= 0 ; 
2819    metric_40 <= 0 ; 
2820    metric_41 <= 0 ; 
2821    metric_42 <= 0 ; 
2822    metric_43 <= 0 ; 
2823    metric_44 <= 0 ; 
2824    metric_45 <= 0 ; 
2825    metric_46 <= 0 ; 
2826    metric_47 <= 0 ; 
2827    metric_48 <= 0 ; 
2828    metric_49 <= 0 ; 
2829    metric_50 <= 0 ; 
2830    metric_51 <= 0 ; 
2831    metric_52 <= 0 ; 
2832    metric_53 <= 0 ; 
2833    metric_54 <= 0 ; 
2834    metric_55 <= 0 ; 
2835    metric_56 <= 0 ; 
2836    metric_57 <= 0 ; 
2837    metric_58 <= 0 ; 
2838    metric_59 <= 0 ; 
2839    metric_60 <= 0 ; 
2840    metric_61 <= 0 ; 
2841    metric_62 <= 0 ; 
2842    metric_63 <= 0 ; 
2843  end
2844  else if (packet_start)
2845  begin
2846    metric_0  <= 10 ;
2847    metric_1  <= 0 ;
2848    metric_2  <= 0 ;
2849    metric_3  <= 0 ;
2850    metric_4  <= 0 ;
2851    metric_5  <= 0 ;   
2852    metric_6  <= 0 ;
2853    metric_7  <= 0 ;
2854    metric_8  <= 0 ;
2855    metric_9  <= 0 ;
2856    metric_10 <= 0 ;
2857    metric_11 <= 0 ;   
2858    metric_12 <= 0 ;
2859    metric_13 <= 0 ;
2860    metric_14 <= 0 ;
2861    metric_15 <= 0 ;
2862    metric_16 <= 0 ; 
2863    metric_17 <= 0 ; 
2864    metric_18 <= 0 ; 
2865    metric_19 <= 0 ; 
2866    metric_20 <= 0 ; 
2867    metric_21 <= 0 ; 
2868    metric_22 <= 0 ; 
2869    metric_23 <= 0 ; 
2870    metric_24 <= 0 ; 
2871    metric_25 <= 0 ; 
2872    metric_26 <= 0 ; 
2873    metric_27 <= 0 ; 
2874    metric_28 <= 0 ; 
2875    metric_29 <= 0 ; 
2876    metric_30 <= 0 ; 
2877    metric_31 <= 0 ; 
2878    metric_32 <= 0 ; 
2879    metric_33 <= 0 ; 
2880    metric_34 <= 0 ; 
2881    metric_35 <= 0 ; 
2882    metric_36 <= 0 ; 
2883    metric_37 <= 0 ; 
2884    metric_38 <= 0 ; 
2885    metric_39 <= 0 ; 
2886    metric_40 <= 0 ; 
2887    metric_41 <= 0 ; 
2888    metric_42 <= 0 ; 
2889    metric_43 <= 0 ; 
2890    metric_44 <= 0 ; 
2891    metric_45 <= 0 ; 
2892    metric_46 <= 0 ; 
2893    metric_47 <= 0 ; 
2894    metric_48 <= 0 ; 
2895    metric_49 <= 0 ; 
2896    metric_50 <= 0 ; 
2897    metric_51 <= 0 ; 
2898    metric_52 <= 0 ; 
2899    metric_53 <= 0 ; 
2900    metric_54 <= 0 ; 
2901    metric_55 <= 0 ; 
2902    metric_56 <= 0 ; 
2903    metric_57 <= 0 ; 
2904    metric_58 <= 0 ; 
2905    metric_59 <= 0 ; 
2906    metric_60 <= 0 ; 
2907    metric_61 <= 0 ; 
2908    metric_62 <= 0 ; 
2909    metric_63 <= 0 ; 
2910  end
2911  else if (dv_in_gate)
2912  begin
2913    // update metric
2914    metric_0  <= metric_next_0 ;
2915    metric_1  <= metric_next_1 ;
2916    metric_2  <= metric_next_2 ;
2917    metric_3  <= metric_next_3 ;
2918    metric_4  <= metric_next_4 ;
2919    metric_5  <= metric_next_5 ;   
2920    metric_6  <= metric_next_6 ;
2921    metric_7  <= metric_next_7 ;
2922    metric_8  <= metric_next_8 ;
2923    metric_9  <= metric_next_9 ;
2924    metric_10 <= metric_next_10;
2925    metric_11 <= metric_next_11;   
2926    metric_12 <= metric_next_12;
2927    metric_13 <= metric_next_13;
2928    metric_14 <= metric_next_14;
2929    metric_15 <= metric_next_15;
2930    metric_16 <= metric_next_16 ;
2931    metric_17 <= metric_next_17;
2932    metric_18 <= metric_next_18;
2933    metric_19 <= metric_next_19;
2934    metric_20 <= metric_next_20;
2935    metric_21 <= metric_next_21;   
2936    metric_22 <= metric_next_22;
2937    metric_23 <= metric_next_23;
2938    metric_24 <= metric_next_24;
2939    metric_25 <= metric_next_25;
2940    metric_26 <= metric_next_26;
2941    metric_27 <= metric_next_27;   
2942    metric_28 <= metric_next_28;
2943    metric_29 <= metric_next_29;
2944    metric_30 <= metric_next_30;
2945    metric_31 <= metric_next_31;
2946    metric_32 <= metric_next_32 ;
2947    metric_33 <= metric_next_33;
2948    metric_34 <= metric_next_34;
2949    metric_35 <= metric_next_35;
2950    metric_36 <= metric_next_36;
2951    metric_37 <= metric_next_37;   
2952    metric_38 <= metric_next_38;
2953    metric_39 <= metric_next_39;
2954    metric_40 <= metric_next_40;
2955    metric_41 <= metric_next_41;
2956    metric_42 <= metric_next_42;
2957    metric_43 <= metric_next_43;   
2958    metric_44 <= metric_next_44;
2959    metric_45 <= metric_next_45;
2960    metric_46 <= metric_next_46;
2961    metric_47 <= metric_next_47;
2962    metric_48 <= metric_next_48 ;
2963    metric_49 <= metric_next_49;
2964    metric_50 <= metric_next_50;
2965    metric_51 <= metric_next_51;
2966    metric_52 <= metric_next_52;
2967    metric_53 <= metric_next_53;   
2968    metric_54 <= metric_next_54;
2969    metric_55 <= metric_next_55;
2970    metric_56 <= metric_next_56;
2971    metric_57 <= metric_next_57;
2972    metric_58 <= metric_next_58;
2973    metric_59 <= metric_next_59;   
2974    metric_60 <= metric_next_60;
2975    metric_61 <= metric_next_61;
2976    metric_62 <= metric_next_62;
2977    metric_63 <= metric_next_63;
2978  end
2979
2980always @ (posedge clk or negedge nrst)
2981  if (~nrst)
2982    wptr <= 0 ;
2983  else if (packet_start)
2984    wptr <= 0 ;
2985  else if (dv_in_gate)
2986      wptr <= wptr + 1 ;
2987
2988always @ (posedge clk or negedge nrst)
2989  if (~nrst)
2990    last_wptr <= 1'b0 ;
2991  else if (dv_in_gate)
2992    last_wptr <= wptr ;
2993 
2994always @ (posedge clk)
2995  if(dv_in_gate)
2996  begin
2997    // update transition
2998    tran_state_0  [wptr] <= diff_0  [M -1] ;
2999    tran_state_1  [wptr] <= diff_1  [M -1] ;
3000    tran_state_2  [wptr] <= diff_2  [M -1] ;
3001    tran_state_3  [wptr] <= diff_3  [M -1] ;
3002    tran_state_4  [wptr] <= diff_4  [M -1] ;
3003    tran_state_5  [wptr] <= diff_5  [M -1] ;
3004    tran_state_6  [wptr] <= diff_6  [M -1] ;
3005    tran_state_7  [wptr] <= diff_7  [M -1] ;
3006    tran_state_8  [wptr] <= diff_8  [M -1] ;
3007    tran_state_9  [wptr] <= diff_9  [M -1] ;
3008    tran_state_10 [wptr] <= diff_10 [M -1] ;
3009    tran_state_11 [wptr] <= diff_11 [M -1] ;
3010    tran_state_12 [wptr] <= diff_12 [M -1] ;
3011    tran_state_13 [wptr] <= diff_13 [M -1] ;
3012    tran_state_14 [wptr] <= diff_14 [M -1] ;
3013    tran_state_15 [wptr] <= diff_15 [M -1] ;
3014    tran_state_16 [wptr] <= diff_16 [M -1] ;
3015    tran_state_17 [wptr] <= diff_17 [M -1] ;
3016    tran_state_18 [wptr] <= diff_18 [M -1] ;
3017    tran_state_19 [wptr] <= diff_19 [M -1] ;
3018    tran_state_20 [wptr] <= diff_20 [M -1] ;
3019    tran_state_21 [wptr] <= diff_21 [M -1] ;
3020    tran_state_22 [wptr] <= diff_22 [M -1] ;
3021    tran_state_23 [wptr] <= diff_23 [M -1] ;
3022    tran_state_24 [wptr] <= diff_24 [M -1] ;
3023    tran_state_25 [wptr] <= diff_25 [M -1] ;
3024    tran_state_26 [wptr] <= diff_26 [M -1] ;
3025    tran_state_27 [wptr] <= diff_27 [M -1] ;
3026    tran_state_28 [wptr] <= diff_28 [M -1] ;
3027    tran_state_29 [wptr] <= diff_29 [M -1] ;
3028    tran_state_30 [wptr] <= diff_30 [M -1] ;
3029    tran_state_31 [wptr] <= diff_31 [M -1] ;
3030    tran_state_32 [wptr] <= diff_32 [M -1] ;
3031    tran_state_33 [wptr] <= diff_33 [M -1] ;
3032    tran_state_34 [wptr] <= diff_34 [M -1] ;
3033    tran_state_35 [wptr] <= diff_35 [M -1] ;
3034    tran_state_36 [wptr] <= diff_36 [M -1] ;
3035    tran_state_37 [wptr] <= diff_37 [M -1] ;
3036    tran_state_38 [wptr] <= diff_38 [M -1] ;
3037    tran_state_39 [wptr] <= diff_39 [M -1] ;
3038    tran_state_40 [wptr] <= diff_40 [M -1] ;
3039    tran_state_41 [wptr] <= diff_41 [M -1] ;
3040    tran_state_42 [wptr] <= diff_42 [M -1] ;
3041    tran_state_43 [wptr] <= diff_43 [M -1] ;
3042    tran_state_44 [wptr] <= diff_44 [M -1] ;
3043    tran_state_45 [wptr] <= diff_45 [M -1] ;
3044    tran_state_46 [wptr] <= diff_46 [M -1] ;
3045    tran_state_47 [wptr] <= diff_47 [M -1] ;
3046    tran_state_48 [wptr] <= diff_48 [M -1] ;
3047    tran_state_49 [wptr] <= diff_49 [M -1] ;
3048    tran_state_50 [wptr] <= diff_50 [M -1] ;
3049    tran_state_51 [wptr] <= diff_51 [M -1] ;
3050    tran_state_52 [wptr] <= diff_52 [M -1] ;
3051    tran_state_53 [wptr] <= diff_53 [M -1] ;
3052    tran_state_54 [wptr] <= diff_54 [M -1] ;
3053    tran_state_55 [wptr] <= diff_55 [M -1] ;
3054    tran_state_56 [wptr] <= diff_56 [M -1] ;
3055    tran_state_57 [wptr] <= diff_57 [M -1] ;
3056    tran_state_58 [wptr] <= diff_58 [M -1] ;
3057    tran_state_59 [wptr] <= diff_59 [M -1] ;
3058    tran_state_60 [wptr] <= diff_60 [M -1] ;
3059    tran_state_61 [wptr] <= diff_61 [M -1] ;
3060    tran_state_62 [wptr] <= diff_62 [M -1] ;
3061    tran_state_63 [wptr] <= diff_63 [M -1] ;
3062  end
3063 
3064always @ (posedge clk or negedge nrst)
3065  if (~nrst)
3066  begin
3067    trace <= 1'b0 ;
3068    cnt <= 0 ;
3069    trace_start_wptr <= 0 ;
3070  end
3071  else if (packet_start)
3072  begin
3073    trace <= 1'b0 ;
3074    cnt <= 0 ;
3075  end
3076  else if (dv_in_gate)
3077  begin
3078    if (cnt == L-1)
3079    begin
3080      trace <= 1'b1 ;
3081      trace_start_wptr <= wptr ;
3082      cnt <= C ;
3083    end
3084    else
3085    begin
3086      trace <= 1'b0 ;
3087      cnt <= cnt + 1 ;
3088    end
3089  end
3090  else if (trace2)
3091    trace_start_wptr <= last_wptr ;
3092  else
3093    trace <= 1'b0 ;
3094
3095always @ (posedge clk or negedge nrst)
3096  if (~nrst)
3097  begin
3098    flush_en <= 1'b0 ;
3099    flush_cnt <= 0 ;
3100  end
3101  else if (packet_end)
3102  begin
3103    flush_en <= 1 ;
3104    flush_cnt <= 0 ;
3105  end
3106  else if (flush_en)
3107  begin
3108    if (flush_cnt == 63)
3109      flush_en <= 1'b0 ;
3110    else
3111      flush_cnt <= flush_cnt + 1 ; 
3112  end
3113
3114assign flush = flush_cnt == 60 ;
3115
3116always @ (posedge clk or negedge nrst)
3117  if (~nrst)
3118    done_i <= 1'b0 ;
3119  else if (packet_start)
3120    done_i <= 1'b0 ;
3121  //else if (packet_end && cnt == L-1)
3122  else if (trace2 & last_cnt_next[LW-1])
3123    done_i <= 1'b1 ;
3124  else if (last_trace_d3)
3125    done_i <= 1'b1 ;
3126
3127
3128always @ (posedge clk or negedge nrst)
3129  if (~nrst)
3130    last_trace <= 1'b0 ;
3131  else if (packet_start)
3132    last_trace <= 1'b0 ;
3133  else if (flush && trace_done && cnt !=L-1 )
3134    last_trace <= 1'b1 ;
3135
3136always @ (posedge clk)
3137  last_trace_s0 <= last_trace ;
3138
3139assign trace2 = ~last_trace_s0 & last_trace ;
3140assign trace_pos_i = trace | trace2 ;
3141
3142always @ (posedge clk or negedge nrst)
3143  if (~nrst)
3144  begin
3145    trace2_s1 <= 1'b0 ;
3146    trace2_s2 <= 1'b0 ;
3147  end
3148  else
3149  begin
3150    trace2_s1 <= trace2 ;
3151    trace2_s2 <= trace2_s1 ;
3152  end
3153
3154
3155always @ (posedge clk)
3156begin
3157  trace_pos_s0 <= trace_pos_i ;
3158  trace_pos <= trace_pos_s0 ;
3159end
3160
3161always @ (posedge clk or negedge nrst)
3162  if(~nrst)
3163    last_trace_num <= 0 ;
3164  else
3165  begin
3166    if (packet_start)
3167      last_trace_num <= 0 ;
3168    else if (trace2)
3169      last_trace_num <= L -cnt  ;
3170    else if (last_trace_d0)
3171      last_trace_num <= last_trace_num + R ;
3172  end
3173
3174always @ (posedge clk)
3175begin
3176  last_trace_d0 <= last_trace & trace_done_pos ;
3177  last_trace_d1 <= last_trace_d0 ;
3178  last_trace_d2 <= last_trace_d1 ;
3179  last_trace_d3 <= last_trace_d2 ;
3180  last_trace_d4 <= last_trace_d3 ;
3181  last_trace_d5 <= last_trace_d4 ;
3182end
3183
3184assign last_cnt_next = last_cnt - R ;
3185
3186always @ (posedge clk or negedge nrst)
3187  if(~nrst)
3188  begin
3189    one_more_out <= 1'b0 ;
3190    last_cnt <= 0 ;
3191  end
3192  else
3193  begin
3194    if (packet_start)
3195    begin
3196      one_more_out <= 1'b0 ;
3197      last_cnt <= 0 ;
3198    end
3199    else if (flush)
3200      last_cnt <= cnt ;
3201    else if (last_trace_d5)
3202    begin
3203      if (~last_cnt_next[LW-1])
3204      begin
3205        last_cnt <= last_cnt_next ;
3206        one_more_out <= 1'b1 ;
3207      end
3208    end
3209    else
3210      one_more_out <= 1'b0 ;
3211  end
3212
3213always @ (posedge clk or negedge nrst)
3214  if (~nrst)
3215    trace_en <= 0 ;
3216  else if (packet_start)
3217    trace_en <= 0 ;
3218  else if (trace_pos)
3219    trace_en <= 1'b1 ;
3220  else if (trace_cnt == L-2)
3221    trace_en <= 1'b0 ;
3222   
3223always @ (posedge clk or negedge nrst)
3224  if (~nrst)
3225    trace_cnt <= 0 ;
3226  else if (trace_pos)
3227    trace_cnt <= 0 ;
3228  else if (trace_en)
3229    trace_cnt <= trace_cnt + 2; 
3230   
3231assign trace_done = trace_cnt == L ;
3232
3233always @ (posedge clk)
3234  trace_done_s0 <= trace_done ;
3235assign trace_done_pos = ~trace_done_s0 & trace_done ;
3236
3237//==============================
3238// Get max metric
3239//==============================
3240max_metric #(M, K) max_metric_0 (
3241        .clk    (clk        ),
3242        .a      (metric_0   ), 
3243        .b      (metric_1   ), 
3244        .c      (metric_2   ), 
3245        .d      (metric_3   ), 
3246        .sa     (6'd0       ), 
3247        .sb     (6'd1       ), 
3248        .sc     (6'd2       ), 
3249        .sd     (6'd3       ), 
3250        .state  (state_0    ), 
3251        .max    (max_0      )
3252        ) ;
3253       
3254//==============================
3255// Get max metric
3256//==============================
3257max_metric #(M, K) max_metric_1 (
3258        .clk    (clk        ),
3259        .a      (metric_4   ), 
3260        .b      (metric_5   ), 
3261        .c      (metric_6   ), 
3262        .d      (metric_7   ), 
3263        .sa     (6'd4       ), 
3264        .sb     (6'd5       ), 
3265        .sc     (6'd6       ), 
3266        .sd     (6'd7       ), 
3267        .state  (state_1    ), 
3268        .max    (max_1      )
3269        ) ;
3270
3271//==============================
3272// Get max metric
3273//==============================       
3274max_metric #(M, K) max_metric_2 (
3275        .clk    (clk        ),
3276        .a      (metric_8   ), 
3277        .b      (metric_9   ), 
3278        .c      (metric_10  ), 
3279        .d      (metric_11  ), 
3280        .sa     (6'd8       ), 
3281        .sb     (6'd9       ), 
3282        .sc     (6'd10      ), 
3283        .sd     (6'd11      ), 
3284        .state  (state_2    ), 
3285        .max    (max_2      )
3286        ) ;   
3287
3288//==============================
3289// Get max metric
3290//==============================
3291max_metric #(M, K) max_metric_3 (
3292        .clk    (clk        ),
3293        .a      (metric_12  ), 
3294        .b      (metric_13  ), 
3295        .c      (metric_14  ), 
3296        .d      (metric_15  ), 
3297        .sa     (6'd12      ), 
3298        .sb     (6'd13      ), 
3299        .sc     (6'd14      ), 
3300        .sd     (6'd15      ), 
3301        .state  (state_3    ), 
3302        .max    (max_3      )
3303        ) ;
3304
3305//==============================
3306// Get max metric
3307//==============================
3308max_metric #(M, K) max_metric_4 (
3309        .clk    (clk        ),
3310        .a      (metric_16  ), 
3311        .b      (metric_17  ), 
3312        .c      (metric_18  ), 
3313        .d      (metric_19  ), 
3314        .sa     (6'd16      ), 
3315        .sb     (6'd17      ), 
3316        .sc     (6'd18      ), 
3317        .sd     (6'd19      ), 
3318        .state  (state_4    ), 
3319        .max    (max_4      )
3320        ) ;
3321
3322//==============================
3323// Get max metric
3324//==============================
3325max_metric #(M, K) max_metric_5 (
3326        .clk    (clk        ),
3327        .a      (metric_20  ), 
3328        .b      (metric_21  ), 
3329        .c      (metric_22  ), 
3330        .d      (metric_23  ), 
3331        .sa     (6'd20      ), 
3332        .sb     (6'd21      ), 
3333        .sc     (6'd22      ), 
3334        .sd     (6'd23      ), 
3335        .state  (state_5    ), 
3336        .max    (max_5      )
3337        ) ;
3338
3339//==============================
3340// Get max metric
3341//==============================       
3342max_metric #(M, K) max_metric_6 (
3343        .clk    (clk        ),
3344        .a      (metric_24  ), 
3345        .b      (metric_25  ), 
3346        .c      (metric_26  ), 
3347        .d      (metric_27  ), 
3348        .sa     (6'd24      ), 
3349        .sb     (6'd25      ), 
3350        .sc     (6'd26      ), 
3351        .sd     (6'd27      ), 
3352        .state  (state_6    ), 
3353        .max    (max_6      )
3354        ) ;
3355
3356//==============================
3357// Get max metric
3358//==============================       
3359max_metric #(M, K) max_metric_7 (
3360        .clk    (clk        ),
3361        .a      (metric_28  ), 
3362        .b      (metric_29  ), 
3363        .c      (metric_30  ), 
3364        .d      (metric_31  ), 
3365        .sa     (6'd28      ), 
3366        .sb     (6'd29      ), 
3367        .sc     (6'd30      ), 
3368        .sd     (6'd31      ), 
3369        .state  (state_7    ), 
3370        .max    (max_7      )
3371        ) ;
3372
3373//==============================
3374// Get max metric
3375//==============================       
3376max_metric #(M, K) max_metric_8 (
3377        .clk    (clk        ),
3378        .a      (metric_32  ), 
3379        .b      (metric_33  ), 
3380        .c      (metric_34  ), 
3381        .d      (metric_35  ), 
3382        .sa     (6'd32      ), 
3383        .sb     (6'd33      ), 
3384        .sc     (6'd34      ), 
3385        .sd     (6'd35      ), 
3386        .state  (state_8    ), 
3387        .max    (max_8      )
3388        ) ;
3389
3390//==============================
3391// Get max metric
3392//==============================       
3393max_metric #(M, K) max_metric_9 (
3394        .clk    (clk        ),
3395        .a      (metric_36  ), 
3396        .b      (metric_37  ), 
3397        .c      (metric_38  ), 
3398        .d      (metric_39  ), 
3399        .sa     (6'd36      ), 
3400        .sb     (6'd37      ), 
3401        .sc     (6'd38      ), 
3402        .sd     (6'd39      ), 
3403        .state  (state_9    ), 
3404        .max    (max_9      )
3405        ) ;
3406
3407//==============================
3408// Get max metric
3409//==============================       
3410max_metric #(M, K) max_metric_10 (
3411        .clk    (clk        ),
3412        .a      (metric_40  ), 
3413        .b      (metric_41  ), 
3414        .c      (metric_42  ), 
3415        .d      (metric_43  ), 
3416        .sa     (6'd40      ), 
3417        .sb     (6'd41      ), 
3418        .sc     (6'd42      ), 
3419        .sd     (6'd43      ), 
3420        .state  (state_10   ), 
3421        .max    (max_10     )
3422        ) ;
3423
3424//==============================
3425// Get max metric
3426//==============================       
3427max_metric #(M, K) max_metric_11 (
3428        .clk    (clk        ),
3429        .a      (metric_44  ), 
3430        .b      (metric_45  ), 
3431        .c      (metric_46  ), 
3432        .d      (metric_47  ), 
3433        .sa     (6'd44      ), 
3434        .sb     (6'd45      ), 
3435        .sc     (6'd46      ), 
3436        .sd     (6'd47      ), 
3437        .state  (state_11   ), 
3438        .max    (max_11     )
3439        ) ;
3440
3441//==============================
3442// Get max metric
3443//==============================
3444max_metric #(M, K) max_metric_12 (
3445        .clk    (clk        ),
3446        .a      (metric_48  ), 
3447        .b      (metric_49  ), 
3448        .c      (metric_50  ), 
3449        .d      (metric_51  ), 
3450        .sa     (6'd48      ), 
3451        .sb     (6'd49      ), 
3452        .sc     (6'd50      ), 
3453        .sd     (6'd51      ), 
3454        .state  (state_12   ), 
3455        .max    (max_12     )
3456        ) ;
3457
3458//==============================
3459// Get max metric
3460//==============================       
3461max_metric #(M, K) max_metric_13 (
3462        .clk    (clk        ),
3463        .a      (metric_52  ), 
3464        .b      (metric_53  ), 
3465        .c      (metric_54  ), 
3466        .d      (metric_55  ), 
3467        .sa     (6'd52      ), 
3468        .sb     (6'd53      ), 
3469        .sc     (6'd54      ), 
3470        .sd     (6'd55      ), 
3471        .state  (state_13   ), 
3472        .max    (max_13     )
3473        ) ;
3474
3475//==============================
3476// Get max metric
3477//==============================       
3478max_metric #(M, K) max_metric_14 (
3479        .clk    (clk        ),
3480        .a      (metric_56  ), 
3481        .b      (metric_57  ), 
3482        .c      (metric_58  ), 
3483        .d      (metric_59  ), 
3484        .sa     (6'd56      ), 
3485        .sb     (6'd57      ), 
3486        .sc     (6'd58      ), 
3487        .sd     (6'd59      ), 
3488        .state  (state_14   ), 
3489        .max    (max_14     )
3490        ) ;       
3491
3492//==============================
3493// Get max metric
3494//==============================
3495max_metric #(M, K) max_metric_15 (
3496        .clk    (clk        ),
3497        .a      (metric_60  ), 
3498        .b      (metric_61  ), 
3499        .c      (metric_62  ), 
3500        .d      (metric_63  ), 
3501        .sa     (6'd60      ), 
3502        .sb     (6'd61      ), 
3503        .sc     (6'd62      ), 
3504        .sd     (6'd63      ), 
3505        .state  (state_15    ), 
3506        .max    (max_15      )
3507        ) ;
3508       
3509//==============================
3510// Get max metric
3511//==============================
3512max_metric #(M, K) max_metric_s1_0 (
3513        .clk    (clk        ),
3514        .a      (max_0      ), 
3515        .b      (max_1      ), 
3516        .c      (max_2      ), 
3517        .d      (max_3      ), 
3518        .sa     (state_0    ), 
3519        .sb     (state_1    ), 
3520        .sc     (state_2    ), 
3521        .sd     (state_3    ), 
3522        .state  (state_s1_0 ), 
3523        .max    (max_s1_0   )
3524        ) ;
3525
3526//==============================
3527// Get max metric
3528//==============================       
3529max_metric #(M, K) max_metric_s1_1 (
3530        .clk    (clk        ),
3531        .a      (max_4      ), 
3532        .b      (max_5      ), 
3533        .c      (max_6      ), 
3534        .d      (max_7      ), 
3535        .sa     (state_4    ), 
3536        .sb     (state_5    ), 
3537        .sc     (state_6    ), 
3538        .sd     (state_7    ), 
3539        .state  (state_s1_1 ), 
3540        .max    (max_s1_1   )
3541        ) ;
3542
3543//==============================
3544// Get max metric
3545//==============================
3546max_metric #(M, K) max_metric_s1_2 (
3547        .clk    (clk        ),
3548        .a      (max_8      ), 
3549        .b      (max_9      ), 
3550        .c      (max_10     ), 
3551        .d      (max_11     ), 
3552        .sa     (state_8    ), 
3553        .sb     (state_9    ), 
3554        .sc     (state_10   ), 
3555        .sd     (state_11   ), 
3556        .state  (state_s1_2 ), 
3557        .max    (max_s1_2   )
3558        ) ;
3559
3560//==============================
3561// Get max metric
3562//==============================       
3563max_metric #(M, K) max_metric_s1_3 (
3564        .clk    (clk        ),
3565        .a      (max_12     ), 
3566        .b      (max_13     ), 
3567        .c      (max_14     ), 
3568        .d      (max_15     ), 
3569        .sa     (state_12   ), 
3570        .sb     (state_13   ), 
3571        .sc     (state_14   ), 
3572        .sd     (state_15   ), 
3573        .state  (state_s1_3 ), 
3574        .max    (max_s1_3   )
3575        ) ;
3576
3577//==============================
3578// Get max metric
3579//==============================
3580max_metric_logic #(M, K) max_metric_s2_0 (
3581        .a      (max_s1_0    ), 
3582        .b      (max_s1_1    ), 
3583        .c      (max_s1_2    ), 
3584        .d      (max_s1_3    ), 
3585        .sa     (state_s1_0  ), 
3586        .sb     (state_s1_1  ), 
3587        .sc     (state_s1_2  ), 
3588        .sd     (state_s1_3  ), 
3589        .state  (state_s2_0  ), 
3590        .max    (            )
3591        ) ;
3592
3593assign init_state_i = state_s2_0 ;
3594
3595always @ (posedge clk or negedge nrst)
3596  if(~nrst)
3597    trace_state <= 0 ;
3598  else
3599  begin
3600    if (trace_pos)
3601    begin
3602      if(zero_tail & trace2_s2)
3603        trace_state <= 0 ;
3604      else
3605        trace_state <= init_state_i ;
3606    end
3607    else if (trace_en)
3608      trace_state <= next_state ;
3609  end
3610
3611assign  trace_start_pos = trace_start_wptr ;
3612
3613assign trace_start_pos0 = trace_start_pos - trace_cnt ;
3614assign trace_start_pos1 = trace_start_pos - trace_cnt -1 ;
3615
3616assign cur_state0 = trace_state ;
3617
3618assign tran_all_0 = {
3619                     tran_state_63[trace_start_pos0], tran_state_62[trace_start_pos0], tran_state_61[trace_start_pos0], tran_state_60[trace_start_pos0], 
3620                     tran_state_59[trace_start_pos0], tran_state_58[trace_start_pos0], tran_state_57[trace_start_pos0], tran_state_56[trace_start_pos0], 
3621                     tran_state_55[trace_start_pos0], tran_state_54[trace_start_pos0], tran_state_53[trace_start_pos0], tran_state_52[trace_start_pos0], 
3622                     tran_state_51[trace_start_pos0], tran_state_50[trace_start_pos0], tran_state_49[trace_start_pos0], tran_state_48[trace_start_pos0],
3623                     
3624                     tran_state_47[trace_start_pos0], tran_state_46[trace_start_pos0], tran_state_45[trace_start_pos0], tran_state_44[trace_start_pos0], 
3625                     tran_state_43[trace_start_pos0], tran_state_42[trace_start_pos0], tran_state_41[trace_start_pos0], tran_state_40[trace_start_pos0], 
3626                     tran_state_39[trace_start_pos0], tran_state_38[trace_start_pos0], tran_state_37[trace_start_pos0], tran_state_36[trace_start_pos0], 
3627                     tran_state_35[trace_start_pos0], tran_state_34[trace_start_pos0], tran_state_33[trace_start_pos0], tran_state_32[trace_start_pos0],
3628                     
3629                     tran_state_31[trace_start_pos0], tran_state_30[trace_start_pos0], tran_state_29[trace_start_pos0], tran_state_28[trace_start_pos0], 
3630                     tran_state_27[trace_start_pos0], tran_state_26[trace_start_pos0], tran_state_25[trace_start_pos0], tran_state_24[trace_start_pos0], 
3631                     tran_state_23[trace_start_pos0], tran_state_22[trace_start_pos0], tran_state_21[trace_start_pos0], tran_state_20[trace_start_pos0], 
3632                     tran_state_19[trace_start_pos0], tran_state_18[trace_start_pos0], tran_state_17[trace_start_pos0], tran_state_16[trace_start_pos0],
3633
3634                     tran_state_15[trace_start_pos0], tran_state_14[trace_start_pos0], tran_state_13[trace_start_pos0], tran_state_12[trace_start_pos0], 
3635                     tran_state_11[trace_start_pos0], tran_state_10[trace_start_pos0], tran_state_9[trace_start_pos0], tran_state_8[trace_start_pos0], 
3636                     tran_state_7[trace_start_pos0], tran_state_6[trace_start_pos0], tran_state_5[trace_start_pos0], tran_state_4[trace_start_pos0], 
3637                     tran_state_3[trace_start_pos0], tran_state_2[trace_start_pos0], tran_state_1[trace_start_pos0], tran_state_0[trace_start_pos0]} ;
3638
3639assign tran_all_1 =  {
3640                     tran_state_63[trace_start_pos1], tran_state_62[trace_start_pos1], tran_state_61[trace_start_pos1], tran_state_60[trace_start_pos1], 
3641                     tran_state_59[trace_start_pos1], tran_state_58[trace_start_pos1], tran_state_57[trace_start_pos1], tran_state_56[trace_start_pos1], 
3642                     tran_state_55[trace_start_pos1], tran_state_54[trace_start_pos1], tran_state_53[trace_start_pos1], tran_state_52[trace_start_pos1], 
3643                     tran_state_51[trace_start_pos1], tran_state_50[trace_start_pos1], tran_state_49[trace_start_pos1], tran_state_48[trace_start_pos1],
3644                     
3645                     tran_state_47[trace_start_pos1], tran_state_46[trace_start_pos1], tran_state_45[trace_start_pos1], tran_state_44[trace_start_pos1], 
3646                     tran_state_43[trace_start_pos1], tran_state_42[trace_start_pos1], tran_state_41[trace_start_pos1], tran_state_40[trace_start_pos1], 
3647                     tran_state_39[trace_start_pos1], tran_state_38[trace_start_pos1], tran_state_37[trace_start_pos1], tran_state_36[trace_start_pos1], 
3648                     tran_state_35[trace_start_pos1], tran_state_34[trace_start_pos1], tran_state_33[trace_start_pos1], tran_state_32[trace_start_pos1],
3649                     
3650                     tran_state_31[trace_start_pos1], tran_state_30[trace_start_pos1], tran_state_29[trace_start_pos1], tran_state_28[trace_start_pos1], 
3651                     tran_state_27[trace_start_pos1], tran_state_26[trace_start_pos1], tran_state_25[trace_start_pos1], tran_state_24[trace_start_pos1], 
3652                     tran_state_23[trace_start_pos1], tran_state_22[trace_start_pos1], tran_state_21[trace_start_pos1], tran_state_20[trace_start_pos1], 
3653                     tran_state_19[trace_start_pos1], tran_state_18[trace_start_pos1], tran_state_17[trace_start_pos1], tran_state_16[trace_start_pos1],
3654
3655                     tran_state_15[trace_start_pos1], tran_state_14[trace_start_pos1], tran_state_13[trace_start_pos1], tran_state_12[trace_start_pos1], 
3656                     tran_state_11[trace_start_pos1], tran_state_10[trace_start_pos1], tran_state_9[trace_start_pos1], tran_state_8[trace_start_pos1], 
3657                     tran_state_7[trace_start_pos1], tran_state_6[trace_start_pos1], tran_state_5[trace_start_pos1], tran_state_4[trace_start_pos1], 
3658                     tran_state_3[trace_start_pos1], tran_state_2[trace_start_pos1], tran_state_1[trace_start_pos1], tran_state_0[trace_start_pos1]} ;
3659
3660
3661assign cur_state1 = get_next_trace_state (cur_state0, tran_all_0) ;
3662assign next_state = get_next_trace_state (cur_state1, tran_all_1) ;
3663
3664assign state0_bit = cur_state0 [K -2] ;
3665assign state1_bit = cur_state1 [K -2] ;
3666
3667always @ (posedge clk or negedge nrst)
3668  if(~nrst)
3669    res <= 0 ;
3670  else
3671  begin
3672    if (trace_pos)
3673      res <= 0 ;
3674    else if (trace_en)
3675    begin
3676      res <= {state1_bit, state0_bit, res [L-1:2]} ;
3677    end
3678  end
3679
3680//======================================
3681// function: get_next_trace_state
3682//======================================
3683function [K-2:0] get_next_trace_state ;
3684input   [K-2:0]     cur_state_in ;
3685input   [N -1:0]    tran_state_in ;
3686reg     [N -1:0]    tmp ;
3687begin
3688  tmp = tran_state_in >> cur_state_in ;
3689  get_next_trace_state = {cur_state_in [K-3:0], tmp[0]} ;
3690end
3691endfunction
3692
3693endmodule
3694
3695
Note: See TracBrowser for help on using the repository browser.