Thread: [myhdl-list] strange problem
Brought to you by:
jandecaluwe
From: Neal B. <ndb...@gm...> - 2009-03-17 15:00:04
|
I'm still trying to wrap my head around myhdl. This one is weird. 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? |
From: Christopher F. <chr...@gm...> - 2009-03-17 15:51:47
|
On Tue, Mar 17, 2009 at 9:59 AM, Neal Becker <ndb...@gm...> wrote: > I'm still trying to wrap my head around myhdl. This one is weird. > > 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? > > I am not the most knowledgeable with this issue. But I think it is an issue of scope. In your example the "input_sample_cnt" is part of the testbench and not part of the stimulus generator. I forgot the relationship that the "Signal" introduces. There should be a way to use simple variables as you outline (need to check the cookbooks and examples to refresh my memory). If you make your variable a "Signal+, and use input_sample_cnt.next = ... it will work. As mentioned above I believe there is a method to use variable but I don't know off the top of my head. |
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 |
From: David B. <dav...@ya...> - 2009-03-17 18:35:01
|
Has anyone ever developed a polytopes arithmetic operation in MYHDL, which was also successfully synthesize such a design to VHDL or Verilog? Polytopes are a sort of enumerated integer type operation. Thanks, David Blubaugh |
From: Neal B. <ndb...@gm...> - 2009-03-24 13:25:56
|
Jan Decaluwe wrote: > 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 That worked fine, until I needed to access (read) a variable also in another generator. I find that: x = Signal (0) def gen1... x.next = x + 1 def gen2... print x Works fine, but if x was just a simple variable, say int, we have the referenced before assignment (of course, using x = x+1 instead of x.next = x+1). Other than using a Signal, any other solution? It seems silly to use Signal for something that is just part of my test setup. |
From: Jan D. <ja...@ja...> - 2009-03-24 15:11:45
|
Neal Becker wrote: > That worked fine, until I needed to access (read) a variable also in another > generator. > > I find that: > > x = Signal (0) > > def gen1... > x.next = x + 1 > > def gen2... > print x > > Works fine, but if x was just a simple variable, say int, we have the > referenced before assignment (of course, using x = x+1 instead of x.next = > x+1). > > Other than using a Signal, any other solution? It seems silly to use Signal > for something that is just part of my test setup. You could declare the int x as global inside your generators. Or you could declare a global intbv and use x[:] = x + 1. In both cases though, you loose the determinism that signals give you. In other words, the behavior will depend on the (unspecified) order in which generators are run by the simulator. Jan -- Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com Python as an HDL: http://www.myhdl.org VHDL development, the modern way: http://www.sigasi.com Analog design automation: http://www.mephisto-da.com World-class digital design: http://www.easics.com |