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

Subversion Repositories mkjpeg

[/] [mkjpeg/] [trunk/] [design/] [mdct/] [FinitePrecRndNrst.v] - Blame information for rev 67

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 67 mikel262
//-----------------------------------------------------------------------------
2
// Title         : Finite Precision Symmetric Reduction module
3
 
4
// Introduces 2 clock cycles of latency
5
 
6
`timescale  1 ns / 100 ps
7
 
8
module FinitePrecRndNrst
9
 
10
#(
11
  parameter   C_IN_SZ=37,
12
              C_OUT_SZ=16,
13
              C_FRAC_SZ=15
14
)
15
(   input wire                         CLK,
16
    input wire                         RST,
17
 
18
    input wire  signed [C_IN_SZ-1:0]   datain,
19
    input wire                         dataval,
20
    output wire signed [C_OUT_SZ-1:0]  dataout,
21
 
22
    output reg                         clip_inc,
23
    output reg                         dval_out
24
 );
25
 
26
   wire                                 sign;
27
   wire signed [C_IN_SZ-1:0]            rc_val;
28
   reg  signed [C_IN_SZ-1:0]            data_round_f;
29
   wire signed [C_IN_SZ-C_FRAC_SZ-1:0]  data_round;
30
   reg  signed [C_OUT_SZ-1:0]           data_rs;
31
   reg                                  dataval_d1;
32
   reg                                  sign_d1;
33
 
34
   assign sign = datain[C_IN_SZ-1];
35
   assign rc_val = { {(C_IN_SZ-C_FRAC_SZ){1'b0}}, 1'b1, {(C_FRAC_SZ-1){1'b0}} };
36
 
37
   always @(posedge CLK or posedge RST)
38
    if(RST)
39
      begin
40
        data_round_f <= 'b0;
41
        dataval_d1   <= 1'b0;
42
        sign_d1      <= 1'b0;
43
        dval_out     <= 1'b0;
44
      end
45
    else
46
      begin
47
        data_round_f <= datain + rc_val;
48
 
49
        dataval_d1   <= dataval;
50
        dval_out     <= dataval_d1;
51
        sign_d1      <= sign;
52
      end
53
 
54
   assign data_round = data_round_f[C_IN_SZ-1:C_FRAC_SZ];
55
 
56
   // saturation / clipping
57
   always @(posedge CLK or posedge RST)
58
    if(RST)
59
      begin
60
        data_rs  <= 'b0;
61
        clip_inc <= 1'b0;
62
      end
63
    else
64
      begin
65
         clip_inc <= 1'b0;
66
 
67
         // clipping condition
68
         if(
69
             (
70
               (C_IN_SZ-C_FRAC_SZ != C_OUT_SZ) &&
71
               (~(&data_round[C_IN_SZ-C_FRAC_SZ-1 : C_OUT_SZ-1])) ==
72
               (|(data_round[C_IN_SZ-C_FRAC_SZ-1 : C_OUT_SZ-1]))
73
             )
74
           || // special case
75
             (
76
               (C_IN_SZ-C_FRAC_SZ == C_OUT_SZ) &&
77
               (data_round[C_IN_SZ-C_FRAC_SZ-1] != sign_d1) &&
78
               data_round != {C_OUT_SZ{1'b0}}
79
             )
80
           )
81
           begin
82
             // clipping counter
83
             if(dataval_d1)
84
               clip_inc <= 1'b1;
85
 
86
             if(sign_d1)
87
               // do saturation
88
               data_rs  <= -(2**(C_OUT_SZ)/2)+1;
89
             else
90
               // do saturation
91
               data_rs  <= (2**(C_OUT_SZ)/2)-1;
92
           end
93
         else
94
           data_rs <= data_round[C_OUT_SZ-1:0];
95
      end
96
 
97
   assign dataout = data_rs;
98
 
99
   //always @(posedge CLK or posedge RST)
100
   // if(RST)
101
   //   begin
102
   //     dataout <= 0;
103
   //   end
104
   // else
105
   //   begin
106
   //     dataout <= data_rs;
107
   //   end
108
 
109
 
110
endmodule
111
 

powered by: WebSVN 2.1.0

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