Re: [myhdl-list] strange problem
Brought to you by:
jandecaluwe
From: Jan D. <ja...@ja...> - 2009-03-17 16:02:23
|
Neal Becker wrote: > I'm still trying to wrap my head around myhdl. This one is weird. It is purely a Python scope issue. Consider: def f(): n = 0 def g(): n += 1 print n g() f() Running this gives: > python tmp.py Traceback (most recent call last): File "tmp.py", line 8, in <module> f() File "tmp.py", line 6, in f g() File "tmp.py", line 4, in g n += 1 UnboundLocalError: local variable 'n' referenced before assignment To understand what happens, consider that 'n += 1' is equivalent to: n = n + 1 When you *assign* to n somewhere locally, Python considers it a local variable. As the n in the rhs as not been locally assigned yet, you get the error. Note that as long you don't assign to n, you would have read access to the n in the enclosing scope. For your case, put the initialization in the generator before the while loop. Jan > > In my testbench/monitor, which was working, I just added a variable > 'input_sample_cnt': > > input_sample_cnt = 0 > > @instance > def stimulus(): > while (1): > yield clock.negedge > if (en): > in_i.next = in_i_iter.next() > in_q.next = in_q_iter.next() > input_sample_cnt += 1 > > reset.next = 0 > > I get: > input_sample_cnt += 1 > UnboundLocalError: local variable 'input_sample_cnt' referenced before > assignment > > But I've had this all along: > result_fd = file ('result', 'w') > count_fd = file ('count', 'w') > reset_fd = file ('reset', 'w') > x_fd = file ('x', 'w') > en_fd = file ('en', 'w') > oe_fd = file ('oe', 'w') > > @instance > def monitor(): > while 1: > yield clock.posedge > ##print 'reset:', reset, 'en:', en, 'x:', x, 'count:', > to_hex(count), 'result:', to_hex(result) > if (oe): > print >> result_fd, to_hex (result_i), to_hex (result_q) > print >> count_fd, to_hex (count) > print >> x_fd, to_hex (in_i), to_hex (in_q) > print >> reset_fd, to_hex (reset) > print >> en_fd, to_hex (en) > print >> oe_fd, to_hex (oe) > > This seems functionally the same thing (other than using +=). Why does this > latter work, but the former not? What should I be doing instead? > > > > ------------------------------------------------------------------------------ > Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are > powering Web 2.0 with engaging, cross-platform capabilities. Quickly and > easily build your RIAs with Flex Builder, the Eclipse(TM)based development > software that enables intelligent coding and step-through debugging. > Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com > _______________________________________________ > myhdl-list mailing list > myh...@li... > https://lists.sourceforge.net/lists/listinfo/myhdl-list > -- Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com Python as a hardware description language: http://www.myhdl.org |