簡體   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