Thread: Re: [myhdl-list] Setting of enum values (Page 2)
Brought to you by:
jandecaluwe
From: Henry G. <he...@ca...> - 2016-01-21 17:09:39
|
On 21/01/16 17:04, Christopher Felton wrote: >>>> >>> You should be able to use constant values in an object, >>>> >>> this code snip might help: >>> >> >>> >> Is this a relatively recent fix? >>> >> >> > >> > Not sure and it depends on your definition >> > of recent. >> > >> > Yes, there was a fix for constants in an object >> > at some point I don't recall the time frame. >> > > Looks like it was fixed for VHDL (always worked > for Verilog) here: > https://github.com/jandecaluwe/myhdl/pull/64 Ok, great, thanks. I think namespace encapsulation of constants goes a long way to solving this problem. Henry |
From: Marcel H. <1he...@in...> - 2016-01-21 17:19:46
Attachments:
signature.asc
|
On 21.01.2016 17:34, Christopher Felton wrote: > > from myhdl import * > > class MyConstants(object): > def __init__(self): > self.const1 = int(intbv("1111_0001")) > self.const2 = int(intbv("1010_0101")) why the intbv -> int conversion here? Just for using bitwise representation? > > def const_select(sel, x): > const = MyConstants() > > @always_comb > def beh_select(): > if sel == 1: > x.next = const.const1 > elif sel == 2: > x.next = const.const2 > else: > x.next = 0 > > return beh_select > Although I can reproduce your example in some test-cases, I can't use it in my project... I use two approaches here. First is to create a instance of the class in the __init__.py. Then I will get a > Local variable may be referenced before assignment: ProcMode When I create the instance directly in my myhdl-method, I will get > Free variable should be a Signal or an int: ProcMode The advantage of having the enum over, that you can use it directly in a Signal, e.g. > mode = Signal(ProcMode.User) currently, it will complain about not havin the bitsize (I have set it manually, which will work fine) I'm not happy about the solutions yet, altough I really appreciate the help I'm getting here (thanks!), but in my eyes, I just want to solve a simple problem. Has never anyone encountered this and made some thoughts? :/ |
From: Christopher F. <chr...@gm...> - 2016-01-21 18:25:05
|
On 1/21/2016 11:19 AM, Marcel Hellwig wrote: > On 21.01.2016 17:34, Christopher Felton wrote: >> >> from myhdl import * >> >> class MyConstants(object): >> def __init__(self): >> self.const1 = int(intbv("1111_0001")) >> self.const2 = int(intbv("1010_0101")) > > why the intbv -> int conversion here? Just for using bitwise representation? Correct, I could have done int("11110001", 2). The only reason I used `intbv` was so I could use the '_' separator :) Or as you did in your original example, 0b11110001. > > Although I can reproduce your example in some test-cases, I can't use it > in my project... > > I use two approaches here. First is to create a instance of the class in > the __init__.py. Then I will get a > >> Local variable may be referenced before assignment: ProcMode > > When I create the instance directly in my myhdl-method, I will get > >> Free variable should be a Signal or an int: ProcMode Hmm, I am not following what you are trying to do different. From your description, it sounds like the object is not defined when the generator is analyzed? I modified my example slightly to match your original class attribute (instead of instance attribute): https://gist.github.com/cfelton/50cb0fbed5f188fcc1bb > > The advantage of having the enum over, that you can use it directly in a > Signal, e.g. > >> mode = Signal(ProcMode.User) > > currently, it will complain about not havin the bitsize (I have set it > manually, which will work fine) What are you interested in here, using the value or the type? If it is the value you can still use it, if it is the type can create a method in your function mode = Signal(ProcMode.const_type()) Then the method can look at all the constants defined and determine the appropriate bitwidth and return the appropriate type (e.g `intbv(0)[8:]`). Regards, Chris |
From: Christopher F. <chr...@gm...> - 2016-01-21 19:10:10
|
> > I'm not happy about the solutions yet, altough I really appreciate the > help I'm getting here (thanks!), but in my eyes, I just want to solve a > simple problem. Has never anyone encountered this and made some > thoughts? :/ > Given your first example snips, this is one way to generate constants: https://gist.github.com/cfelton/50cb0fbed5f188fcc1bb#file-constant_type-py Regards, Chris |
From: Marcel H. <1he...@in...> - 2016-01-22 18:39:44
Attachments:
signature.asc
|
On 20.01.2016 23:11, Marcel Hellwig wrote: > > Now, myhdl tells me: > >> > myhdl.ConversionError: in file xxx.py, line 63: >> > Unsupported attribute: User > line 63: >> > if mode == CpuMode.User: I finally found out, why this happens. Because I don't like to write the same code twice, I outsourced the if/elif section into a seperate function. > def getRegisterbank(mode): > if mode == ProcMode.User or mode == ProcMode.System: > return 0 > elif mode == ProcMode.Supervisor: > return 1 > elif mode == ProcMode.Abort: > return 2 > elif mode == ProcMode.IRQ: > return 4 > elif mode == ProcMode.FIQ: > return 5 > else: > return 3 > > @always_seq(clk.posedge, reset=reset) > def write(): > if we: > mul = 0 > if mode == ProcMode.User or mode == ProcMode.System: > mul = 0 > # ... > else: > mul = 3 > # mul = getRegisterbank(mode) > re = regbank[mul*18 + rd] > regs[re].next = din > > @always_comb > def read(): > # mul = getRegisterbank(mode) > mul = 0 > re = regbank[mul*18 + rs] > sout.next = regs[re] > > return write, read And here is the problem. When I write it directly in the write/read methods, everything works fine, but as soon as I call the getRegisterbank function, it will complain about unsupported Attribute. Is there a way around this? :/ |
From: Christopher F. <chr...@gm...> - 2016-01-22 21:15:56
|
On 1/22/16 12:39 PM, Marcel Hellwig wrote: > On 20.01.2016 23:11, Marcel Hellwig wrote: >> >> Now, myhdl tells me: >> >>>> myhdl.ConversionError: in file xxx.py, line 63: >>>> Unsupported attribute: User >> line 63: >>>> if mode == CpuMode.User: > > > I finally found out, why this happens. > Because I don't like to write the same code twice, I outsourced the > if/elif section into a seperate function. > Hmmm, yes this doesn't appear to be currently supported, that is accessing attributes in a combinatorial function. Not sure why it is not? Could you use a module instead of a function? rb_inst = register_bank(mode, mul) @always_seq(clk.posedge, reset=reset) def write(): # ... re = regbank[mul*18 + rs] @always_comb def read(): re = regbank[mul*18 + rs] return rb_inst, write, read Regards, Chris |