繁体   English   中英

动态实例化:如何在myHDL中动态连接接口

[英]Dynamic Instantiation: How to dynamically wire interfaces in myHDL

我正在尝试制作一个python库,以便使用pySerial和myHDL 1.0dev在PC和FPGA之间动态创建UART接口

它获取数据类型及其属性的名称,并实例化RAM块,并允许访问PC上的读/写命令。 但是,我在动态连接RAM时遇到了问题。

作为一个最小的工作示例,我有这两个类。

class RamBus(object):
    def __init__(self):
        self.clk     = Signal(bool(0))

class UartBus(object):
    def __init__(self):
        self.interfaces = dict()
    def add(self, name, bus):
        self.interfaces[name] = bus
        setattr(self,name,bus)

UartBus用于容纳许多RamBuse。 现在,我将尝试将它们与arbiter模块动态连接。

@block
def arbiter(clk,uartbus):
    modules = []
    for key in uartbus.interfaces:
        print key

        @block
        def electrician(rambus=uartbus.interfaces[key]):
            @always_comb
            def wiring():
                rambus.clk.next = clk
            return wiring
        f = electrician
        modules.append(electrician())
    return modules

如果使用此代码进行转换,则会得到错误的转换

uartbus = UartBus()

uartbus.add('power',RamBus())
uartbus.add('freq',RamBus())

#attempt conversion
clk = Signal(bool(0))
arbiter(clk,uartbus).convert()

这是错误的Verilog。

`timescale 1ns/10ps

module arbiter (
    clk
);


input clk;

wire electrician_0_rambus_clk;
wire electrician_0_rambus_clk;

assign electrician_0_rambus_clk = clk;
assign electrician_0_rambus_clk = clk;

endmodule

而且这两根线的名称相同! 在@always_comb旁边使用字典不起作用,因为到目前为止,任何版本的myHDL都不支持字典进行转换。 如何正确实现动态接线?

因此,我在写这篇文章的时候就找到了答案,因为我认为这是一个有用的窍门,所以我决定还是发布问题。

@block
def arbiter(clk,uartbus):
    modules = []
    for key in uartbus.interfaces:

        #note that there is no @block here!
        def electrician(rambus=uartbus.interfaces[key]):
            @always_comb
            def wiring():
                rambus.clk.next = clk
            return wiring

        #here we can redefine the name that electrician 
        #has so that myHDL converts it with that name.
        electrician.func_name = key
        #then we apply the block decorator
        electrician = block(electrician)

        modules.append(electrician())
        print key

    return modules

这是正确的Verilog。

// File: arbiter.v
// Generated by MyHDL 1.0dev
// Date: Tue Jun 28 14:03:01 2016

`timescale 1ns/10ps

module arbiter (
    clk
);


input clk;

wire freq_0_rambus_clk;
wire power_0_rambus_clk;

assign freq_0_rambus_clk = clk;
assign power_0_rambus_clk = clk;

endmodule

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM