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

Subversion Repositories dmt_tx

[/] [dmt_tx/] [trunk/] [myhdl/] [rtl/] [cmath.py] - Blame information for rev 31

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

Line No. Rev Author Line
1 27 dannori
 
2
from myhdl import *
3
 
4
def cadd(a_re, a_im, b_re, b_im, y_re, y_im, overflow, width=8):
5
  '''Complex add
6
 
7
  I/O pins:
8
  =========
9
  a         : input a
10
  b         : input b
11
  y         : output a + b
12
  overflow  : signal overflow
13
 
14
  parameter:
15
  ==========
16
  width : data width for input and output
17
  '''
18
  @always_comb
19
  def logic():
20
    m = 2**(width-1)
21
 
22
    #
23
    # Real value calculation
24
    if (a_re + b_re) >= m:
25
      y_re.next = m-1
26
      ovfl_re = True
27
 
28
    elif (a_re + b_re) < -m:
29
      y_re.next = -m
30
      ovfl_re = True
31
 
32
    else:
33
      y_re.next = a_re + b_re
34
      ovfl_re = False
35
 
36
    #
37
    # Imaginary add
38
    if (a_im + b_im) >= m:
39
      y_im.next = m-1
40
      ovfl_im = True
41
 
42
    elif (a_im + b_im) < -m:
43
      y_im.next = -m
44
      ovfl_im = True
45
 
46
    else:
47
      y_im.next = a_im + b_im
48
      ovfl_im = False
49
 
50
    overflow.next = ovfl_re or ovfl_im
51
 
52
  return instances()
53
 
54
 
55
def csub(a_re, a_im, b_re, b_im, y_re, y_im, overflow, width=8):
56
 
57
  @always_comb
58
  def logic():
59
    m = 2**(width-1)
60
 
61
    #
62
    # Real value calculation
63
    if (a_re - b_re) >= m:
64
      y_re.next = m-1
65
      ovfl_re = True
66
 
67
    elif (a_re - b_re) < -m:
68
      y_re.next = -m
69
      ovfl_re = True
70
 
71
    else:
72
      y_re.next = a_re - b_re
73
      ovfl_re = False
74
 
75
    #
76
    # Imaginary add
77
    if (a_im - b_im) >= m:
78
      y_im.next = m-1
79
      ovfl_im = True
80
 
81
    elif (a_im - b_im) < -m:
82
      y_im.next = -m
83
      ovfl_im = True
84
 
85
    else:
86
      y_im.next = a_im - b_im
87
      ovfl_im = False
88
 
89
    overflow.next = ovfl_re or ovfl_im
90
 
91
  return instances()
92 31 dannori
 
93
 
94
def cmult(a_re, a_im, b_re, b_im, y_re, y_im, overflow):
95
  ''' Perform the complex multiplication of a * b = y
96
 
97
  This turns out to be:
98
 
99
  y_re = a_re * b_re - a_im * b_im
100
  y_im = a_re * b_im + a_im * b_re
101
 
102
 
103
  I/O pins:
104
  =========
105
  a         : input a
106
  b         : input b
107
  y         : output a * b
108
  overflow  : signal overflow
109
 
110
  '''
111
 
112
  @always_comb
113
  def logic():
114
 
115
    # use input width of a_re, assume a_im, b_re and b_im are the same
116
    width = len(a_re)
117
 
118
    # calculate min and max value range
119
    smin = -2**(width-1)
120
    smax = 2**(width-1)-1
121
 
122
    #print 'cmult input width: ', width
123
 
124
    prod_a = a_re * b_re
125
    prod_b = a_im * b_im
126
    prod_c = a_re * b_im
127
    prod_d = a_im * b_re
128
 
129
    # scaling back the product to stay on input width
130
    prod_a = prod_a >> width
131
    prod_b = prod_b >> width
132
    prod_c = prod_c >> width
133
    prod_d = prod_d >> width
134
 
135
    #print 'cmult in: ', a_re, a_im, b_re, b_im
136
    #print 'cmult prod: ', prod_a, prod_b, prod_c, prod_d
137
 
138
    prod_diff = prod_a - prod_b
139
    prod_sum = prod_c + prod_d
140
 
141
    ovfl = False
142
 
143
    if prod_sum >= smax:
144
      prod_sum = smax-1
145
      ovfl = True
146
 
147
    elif prod_sum < smin:
148
      prod_sum = smin
149
      ovfl = True
150
 
151
    if prod_diff >= smax:
152
      prod_diff = smax-1
153
      ovfl = True
154
    elif prod_diff < smin:
155
      prod_diff = smin
156
      ovfl = True
157
 
158
     # here we would need a bit growth, but only signal it as overflow
159
    y_re.next = prod_diff
160
    y_im.next = prod_sum
161
    overflow.next = ovfl
162
 
163
    #print 'cmult: a_re: %d, a_im: %d, b_re: %d, b_im: %d'%(
164
    #        a_re, a_im, b_re, b_im)
165
    #print 'cmult: y_re: %d, y_im: %d, overflow: %d'%(y_re, y_im,
166
    #    overflow)
167
 
168
  return instances()

powered by: WebSVN 2.1.0

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