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

Subversion Repositories dmt_tx

[/] [dmt_tx/] [trunk/] [myhdl/] [test/] [test_bit_order.py] - Blame information for rev 30

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 dannori
#!/usr/bin/env python
2
 
3
import unittest
4
import os
5
from myhdl import *
6
 
7
import random
8
 
9
 
10
if __name__ == '__main__':
11
  import sys
12
  sys.path.append('../')
13
 
14
from rtl.bit_order import bit_order
15
 
16
 
17
class TestBitOrder(unittest.TestCase):
18
 
19
  def test_bit_order_simple(self):
20
 
21
    def bench(fp_dataL, ip_dataL, fip_bitL, expOutL):
22
      '''Verify the proper function of the bit ordering block.
23
 
24
      Function gets passed the input data for the fast and the
25
      inteleaved path, a list with bits to load from the fast and/or
26
      interleaved path and the expected output word.
27
      '''
28
 
29
      DWIDTH = 8
30
 
31
      # setup signals 
32
      clk, reset, \
33
          fast_data_avail_i, fast_rden_o, \
34
          inter_data_avail_i, inter_rden_o, \
35
          ready_o, enable_i, \
36
          data_out_valid_o \
37
          = [Signal(bool(0)) for i in range(9)]
38
 
39
      fast_data_i, inter_data_i = [Signal(intbv(0)[DWIDTH:]) for i in range(2)]
40
 
41
      fast_bits_i, inter_bits_i = [Signal(intbv(0)[4:]) for i in range(2)]
42
      data_out_o = Signal(intbv(0)[15:])
43
 
44
      #
45
      # instantiate DUT
46
      #
47
      bit_order_inst = bit_order(clk, reset,
48
                                  fast_data_avail_i, fast_rden_o, fast_data_i,
49
                                  inter_data_avail_i, inter_rden_o, inter_data_i,
50
                                  ready_o, enable_i, fast_bits_i, inter_bits_i,
51
                                  data_out_valid_o, data_out_o)
52
 
53
      @always(delay(10))
54
      def clkgen():
55
        clk.next = not clk
56
 
57
      @instance
58
      def sendInputData():
59
 
60
        yield reset.negedge
61
        #print "Got the reset.negedge at %d"%now()
62
        yield join( fifo(clk, fast_data_avail_i, fast_rden_o, fast_data_i, fp_dataL),
63
                    fifo(clk, inter_data_avail_i, inter_rden_o, inter_data_i, ip_dataL)
64
                    )
65
 
66
      @instance
67
      def ctrlBitOrdering():
68
 
69
        # apply reset
70
        yield clk.negedge
71
        reset.next = 1
72
        yield clk.negedge
73
        reset.next = 0
74
 
75
        yield applyBitValues(clk, ready_o, enable_i,
76
                              fast_bits_i, inter_bits_i,
77
                              fip_bitL)
78
 
79
 
80
      @instance
81
      def verify():
82
 
83
        yield verifyOutput(clk, data_out_valid_o, data_out_o, expOutL)
84
 
85
        raise StopSimulation
86
 
87
      return instances()
88
 
89
    #####################################
90
 
91
    # generate test data
92
    fp_dataL, ip_dataL, fip_bitL = genData()
93
 
94
    # calculate expected output
95
    expOutL = calcExpectedOutput(fp_dataL, ip_dataL, fip_bitL)
96
 
97
 
98
    tb = bench(fp_dataL, ip_dataL, fip_bitL, expOutL)
99
    #tb = traceSignals(bench, fp_dataL, ip_dataL, fip_bitL, expOutL)
100
    sim = Simulation(tb)
101
    sim.run()
102
 
103
##########################################################################
104
# bus functional models
105
def verifyOutput(clk, data_out_valid_o, data_out_o, expOutL,
106
                  TIMEOUT=10):
107
  '''Bus function model for the bit order block output
108
 
109
  The expected output is provided with expOutL as a list of integers.
110
  The function will wait for an active data_out_valid_o signal and
111
  compare the output with a sample of the list.
112
  '''
113
  #print "Expected values: ", expOutL
114
 
115
  for exp_value in expOutL:
116
    to_ctr = 0
117
    while data_out_valid_o == 0:
118
      yield clk.negedge
119
      to_ctr += 1
120
      if to_ctr > TIMEOUT:
121
        raise StopSimulation, "verifyOutput timeout ERROR"
122
 
123
    #print "data_out_valid_o: %d at %d"%(data_out_valid_o, now())
124
    #print "data_out_o: 0x%x expected: 0x%x"%(data_out_o, exp_value)
125
 
126
    yield data_out_valid_o.negedge
127
 
128
 
129
def applyBitValues(clk, ready_o, enable_i, fast_bits_i, inter_bits_i, fip_bitL):
130
  '''BFM for setting the fast and interleaved bits and enabling
131
  processing
132
 
133
  Waits on an active ready_o and applies the fast_bits_i and
134
  inter_bits_i data. Then sets enable_i active. Feeds all data from
135
  fip_bitL to the module this way.
136
  '''
137
  #print "Bit list: ", fip_bitL
138
  for fbits, ibits in fip_bitL:
139
 
140
    # wait until ready is active
141
    while ready_o == 0:
142
      yield clk.negedge
143
 
144
    fast_bits_i.next = fbits
145
    inter_bits_i.next = ibits
146
    enable_i.next = 1
147
    #print "applying %d, %d at %d"%(fbits, ibits, now())
148
 
149
    yield clk.negedge
150
    enable_i.next = 0
151
 
152
 
153
def fifo(clk, data_avail_i, rden_o, data_i, dataL):
154
  '''BFM for sending the fast and interleaved data over the FIFO
155
  interface
156
 
157
  Waits on a request from the bit order block and sends a data word out.
158
  '''
159
  data_avail_i.next = 1
160
  #print "FIFO data: ", dataL
161
 
162
  for data in dataL:
163
    data_i.next = data
164
    while rden_o == 0:
165
      yield clk.posedge
166
 
167
 
168
##########################################################################
169
# functions for data generation and calculating expected output
170
def genData():
171
  '''Generate test data for the bit order block verification
172
  Return fp_dataL, ip_dataL, fip_bitL
173
 
174
          fp_dataL  : contains the data for the fast path. One byte per
175
                      entry. Number of bytes (sum of data bits) matches
176
                      with the sum of bits in fip_bitL for the fast path
177
          ip_dataL  : contains the data for the interleaved path. One
178
                      byte per entry. Number of bytes (sum of data bits)
179
                      matches with the sum of bits in fip_bitL for the
180
                      interleaved path
181
          fip_bitL  : list of tuples, with tuples of size 2. Index 0 is
182
                      the number of bits from the fast path and index 1
183
                      is the number of bits from the interlaved path.
184
  '''
185
 
186
  #fp_bitL = [i for i in range(2,16)]
187
  fp_bitL = [2,8]
188
  #random.shuffle(fp_bitL)
189
  bit_sum = sum(fp_bitL)
190
  rem_bit_sum = bit_sum % 8
191
  # there is no carrier load of 1 bit, 
192
  # so in case we need to add 1, add 9
193
  if rem_bit_sum == 1:
194
    fp_bitL.append(9)
195
  elif rem_bit_sum > 1:
196
    fp_bitL.append(8-rem_bit_sum)
197
 
198
  #print "fp_bitL: ", fp_bitL
199
 
200
  ip_bitL = [0]*len(fp_bitL)
201
 
202
  fip_bitL = []
203
  for i, fp_bits in enumerate(fp_bitL):
204
    fip_bitL.append((fp_bits, ip_bitL[i]))
205
 
206
  fp_bit_sum = sum(fp_bitL)
207
  ip_bit_sum = sum(ip_bitL)
208
 
209
  # finished setup the bit list tables
210
 
211
  fp_dataL = []
212
  ip_dataL = []
213
  byte_num = fp_bit_sum / 8
214
  for i in range(byte_num):
215
    value = random.randrange(256)
216
    #fp_dataL.append(value)
217
    fp_dataL.append(16+ i%256)
218
    #print i, i%256
219
 
220
  return fp_dataL, ip_dataL, fip_bitL
221
 
222
 
223
def calcExpectedOutput(fp_dataL, ip_dataL, fip_bitL):
224
  '''Calculate the expected output of the bit order block
225
  Return a list with the expected out data words
226
  '''
227
  expOutL = []
228
 
229
  # construct a list of bool values from the data. Data for each path
230
  # come in as one byte per list entry
231
  fpL = []
232
  for byte in fp_dataL:
233
    fpL.extend(byteToBoolList(byte))
234
 
235
  ipL = []
236
  for byte in ip_dataL:
237
    ipL.extend(byteToBoolList(byte))
238
 
239
 
240
  # reverse the lists, so that the lsb is index -1, as the pop() gets
241
  fpL.reverse()
242
  ipL.reverse()
243
  # the data from the end of the list
244
  for fp_bits, ip_bits in fip_bitL:
245
    data_out = 0
246
    # get first the bits from the fast path
247
    if fpL:
248
      for i in range(fp_bits):
249
        bit = fpL.pop()
250
        data_out |= bit << i
251
 
252
    # now go on with the interleaved path
253
    if ipL:
254
      for i in range(ip_bits):
255
        bit = ipL.pop()
256
        data_out |= bit << i
257
 
258
    expOutL.append(data_out)
259
 
260
 
261
  return expOutL
262
 
263
 
264
 
265
def byteToBoolList(byte):
266
  '''Convert an integer byte value to a list of bool types
267
  index 0 of the list is lsb
268
  '''
269
  retL = []
270
  for i in range(8):
271
    bit = (byte >> i) & 0x1
272
    if bit:
273
      retL.append(1)
274
    else:
275
      retL.append(0)
276
 
277
  return retL
278
 
279
########################################################################
280
# main
281
if __name__ == '__main__':
282
  suite = unittest.TestLoader().loadTestsFromTestCase(TestBitOrder)
283
  unittest.TextTestRunner(verbosity=2).run(suite)

powered by: WebSVN 2.1.0

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