【问题标题】:Myhdl: assigning a bitslice to a signed variable fails with negative valuesMyhdl:将位片分配给有符号变量失败并显示负值
【发布时间】:2021-03-20 14:51:46
【问题描述】:

添加的代码因 ValueError 而失败,我不知道出了什么问题。这是我想要做的: 在我的 fpga 中,我通过 spi 接收数据。 数据是到达 16 位寄存器 rxdata 的双极性信号(在测量信号的意义上)。

任务是将这个信号解释为有符号,并且只需要高 12 位(包括符号)。因此,变量 bipolar 为 12 位宽且有符号,即像在代码中一样声明。然后我分配:

    bipolar.next=rxdata[:4].signed() 

我的问题是,一旦数据变为负数(即最高有效位变为 1),12 位切片的分配就会失败。

以数据 0x8fff 为例,在运行时我得到:

'ValueError: intbv 值 2303 >= 最大值 2048'

我没想到会这样,因为双方都被声明为有符号并且数据适合变量 bipolar。

还有其他方法吗?

(顺便说一句:bipolar[:].next=rxdata[:4].signed() 我得到 0 作为结果,我也不期望)

#testcase.py sk 09.12.2020
#assign a  slice to a signed signal (bipolar) fails at runtime with negative numbers
from myhdl import *

nspi=16
n=12
tend=1e-6

@block
def testcase():
    CLK = Signal(bool(0))
    RESET = ResetSignal(1,active = 0, isasync=True)
    bipolar=Signal(intbv(0,min=-2**(n-1),max=2**(n-1)))
    rxdata = Signal(intbv(0)[nspi:0])   #received data is bipolar, transferred via spi into rxdata

    ''' Clock driver 16MHz'''
    @always(delay(31))
    def driver():
        CLK.next = not CLK

    @instance
    def init():
        rxdata.next=0x8fff   #0x7fff i.e. positive passes, 0x8fff     i.e negative fails runtime check
        yield delay(100)

    @always_seq(CLK.negedge,reset=RESET)
    def assign():
        #bipolar[:].next=rxdata[:(nspi-n)].signed()  #this passes - but result is 0! (unexpected for me)
        bipolar.next=rxdata[:(nspi-n)].signed()  #this fails with negative numbers (unexpected for me)
        print(bipolar, 'bipolar=', int(str(bipolar),16))


    return instances()

tc = testcase()
tc.run_sim(tend*1e9)

print('Simulated to tend='+str(tend))

【问题讨论】:

    标签: slice valueerror signed myhdl


    【解决方案1】:

    刚刚找到了方法:使用阴影信号!即使用圆括号 () 而不是方括号 ..[]

    #testcase.py sk 12.12.2020
    #assign a  slice to a signed signal (bipolar) fails at runtime with negative numbers
    #-> use shadow signals instead!     #bipolar[:].next=rxdata[:(nspi-n)].signed()  #this passes - but result is 0! (unexpected for me)
    
    from myhdl import *
    
    nspi=16
    n=12
    tend=1e-6
    
    @block
    def testcase():
        CLK = Signal(bool(0))
        RESET = ResetSignal(1,active = 0, isasync=True)
        bipolar=Signal(intbv(0,min=-2**(n-1),max=2**(n-1)))
        #rxdata = Signal(intbv(0)[nspi:0])   #received data is bipolar, transferred via spi into rxdata
        rxdata = Signal(intbv(0,min=0,max=2**nspi))
    
    
        ''' Clock driver 16MHz'''
        @always(delay(31))
        def driver():
            CLK.next = not CLK
    
        @instance
        def init():
            rxdata.next=0xffff   #0x7fff i.e. positive passes, 0x8fff i.e negative fails runtime check when not using shadow
            yield delay(100)
    
        @always_seq(CLK.negedge,reset=RESET)
        def assign():
            #bipolar.next=rxdata[:(nspi-n)].signed()     #this fails in runtime check -> number too big
            #bipolar[:].next=rxdata[:(nspi-n)].signed()  #this passes - but result is 0! (unexpected for me)
            bipolar.next=rxdata(nspi,(nspi-n)).signed()  #this is the way: use shadow signal !
            print(bipolar, 'bipolar=', int(str(bipolar),16))
    
    
        return instances()
    
    tc = testcase()
    tc.run_sim(tend*1e9)
    
    print('Simulated to tend='+str(tend))
    

    【讨论】:

    • 我只在尝试翻译、编译和运行时才意识到: .signed() 和 shadow 信号都不能使用。我只是尝试使用没有 .signed() 扩展的方括号来编译和运行 - 但感觉迷失了......
    • 模拟和目标编程的解决方案:bipolar 不能声明为 intbv 而是 modbv!
    猜你喜欢
    • 2013-03-15
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2011-10-22
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多