开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

how to create a number of BRAMs ? (problem with function-based approach)


Sergey Smolov
 

Is it correct for BSV to use functions to create a number of BRAMs?
I've wrote the following code:

typedef 1024 MaxVarNum;
typedef 8192 MaxClsNum;
typedef 32 MaxVarInCls;
typedef TLog#(MaxVarNum) VarIdxWidth;
typedef TLog#(TAdd#(MaxVarInCls, 1)) ClsSizeWidth;
typedef TAdd#(VarIdxWidth, 1) LiteralWidth;
typedef TAdd#(ClsSizeWidth, TMul#(MaxVarInCls, LiteralWidth)) ClsSize;
typedef UInt#(ClsSize) ClauseUInt;
typedef Bit#(ClsAddrWidth) ClsAddrWidthBit;
typedef TDiv#(MaxClsNum, 4) ClsBankSize;
typedef TLog#(ClsBankSize) ClsAddrWidth;
typedef struct { ClauseUInt data; } Clause deriving (Bits, Eq);
typedef BRAM2Port#(ClsAddrWidthBit, Clause) ClauseBRAM;

function ClauseBRAM mkClauseBRAM(Integer blockNum);
??? BRAM_Configure cfg = defaultValue;
??? String fileName = "cls_mem" + fromInteger(blockNum) + "_data.txt";
??? cfg.loadFormat = tagged Hex fileName;
??? cfg.memorySize = valueOf(ClsBankSize);
??? ClauseBRAM bram = mkBRAM2Server(cfg);
??? return bram;
endfunction

and compiler gave me the following:

Type error at the use of the following function:
??? mkBRAM2Server

? The expected return type of the function:
??? Clause::ClauseBRAM

? The return type according to the use:
??? a__#(BRAM::BRAM2Port#(b__, c__))


If it is the wrong way, then how is it possible to create a number of BRAMs inside BSV module? I suspect that some kind of "generate" is needed here.


 

The error message was saying that you're returning the module generator (Module#(ClauseBRAM)) and not the module's interface (ClauseBRAM). You probably want to make this function a module.This is mostly just swapping out function/endfunction with module/endmodule (and make the minor syntax changes to the type signature).

Note that:

? ClauseBRAM bram = mkBRAM2Server(cfg);

should be?

? ClauseBRAM bram <- mkBRAM2Server(cfg);??

since as part of instantiating the mkClauseBRAM, you'll want to instantiate the mkBRAM2Server.?

-Nirav









On Fri, Apr 16, 2021 at 8:51 AM Sergey Smolov <ssedai@...> wrote:
Is it correct for BSV to use functions to create a number of BRAMs?
I've wrote the following code:

typedef 1024 MaxVarNum;
typedef 8192 MaxClsNum;
typedef 32 MaxVarInCls;
typedef TLog#(MaxVarNum) VarIdxWidth;
typedef TLog#(TAdd#(MaxVarInCls, 1)) ClsSizeWidth;
typedef TAdd#(VarIdxWidth, 1) LiteralWidth;
typedef TAdd#(ClsSizeWidth, TMul#(MaxVarInCls, LiteralWidth)) ClsSize;
typedef UInt#(ClsSize) ClauseUInt;
typedef Bit#(ClsAddrWidth) ClsAddrWidthBit;
typedef TDiv#(MaxClsNum, 4) ClsBankSize;
typedef TLog#(ClsBankSize) ClsAddrWidth;
typedef struct { ClauseUInt data; } Clause deriving (Bits, Eq);
typedef BRAM2Port#(ClsAddrWidthBit, Clause) ClauseBRAM;

function ClauseBRAM mkClauseBRAM(Integer blockNum);
??? BRAM_Configure cfg = defaultValue;
??? String fileName = "cls_mem" + fromInteger(blockNum) + "_data.txt";
??? cfg.loadFormat = tagged Hex fileName;
??? cfg.memorySize = valueOf(ClsBankSize);
??? ClauseBRAM bram = mkBRAM2Server(cfg);
??? return bram;
endfunction

and compiler gave me the following:

Type error at the use of the following function:
??? mkBRAM2Server

? The expected return type of the function:
??? Clause::ClauseBRAM

? The return type according to the use:
??? a__#(BRAM::BRAM2Port#(b__, c__))


If it is the wrong way, then how is it possible to create a number of BRAMs inside BSV module? I suspect that some kind of "generate" is needed here.