1

我正在尝试使用 pySerial 使用myHDL 1.0dev创建一个 python 库,用于在 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 用于容纳许多 RamBus。现在我将尝试将它们与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 都不支持字典进行转换。如何正确实现动态布线?

4

1 回答 1

2

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

@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
于 2016-06-28T18:04:07.113 回答