@@ -2255,9 +2255,6 @@ def __init__(self, m, name, clk, rst, datawidth=32, addrwidth=32,
22552255 self .wresp .bvalid (1 )
22562256 )
22572257
2258- self .mem = self .m .Reg (
2259- '_' .join (['' , self .name , 'mem' ]), 8 , vtypes .Int (2 ) ** self .mem_addrwidth )
2260-
22612258 if memimg is None :
22622259 if memimg_name is None :
22632260 memimg_name = '_' .join (['' , self .name , 'memimg' , '.out' ])
@@ -2268,12 +2265,24 @@ def __init__(self, m, name, clk, rst, datawidth=32, addrwidth=32,
22682265 elif isinstance (memimg , str ):
22692266 memimg_name = memimg
22702267
2268+ num_words = sum (1 for line in open (memimg , 'r' ))
2269+ # resize mem_addrwidth according to the memimg size
2270+ self .mem_addrwidth = max (self .mem_addrwidth ,
2271+ int (math .ceil (math .log (num_words , 2 ))))
2272+
22712273 else :
22722274 if memimg_datawidth is None :
22732275 memimg_datawidth = mem_datawidth
22742276 if memimg_name is None :
22752277 memimg_name = '_' .join (['' , self .name , 'memimg' , '.out' ])
2276- to_memory_image (memimg_name , memimg , datawidth = memimg_datawidth )
2278+
2279+ num_words = to_memory_image (memimg_name , memimg , datawidth = memimg_datawidth )
2280+ # resize mem_addrwidth according to the memimg size
2281+ self .mem_addrwidth = max (self .mem_addrwidth ,
2282+ int (math .ceil (math .log (num_words , 2 ))))
2283+
2284+ self .mem = self .m .Reg (
2285+ '_' .join (['' , self .name , 'mem' ]), 8 , vtypes .Int (2 ) ** self .mem_addrwidth )
22772286
22782287 self .m .Initial (
22792288 vtypes .Systask ('readmemh' , memimg_name , self .mem )
@@ -2582,6 +2591,9 @@ def to_memory_image(filename, array, length=None,
25822591 for v in values :
25832592 f .write (fmt % v )
25842593
2594+ num_lines = len (array ) * num
2595+ return num_lines
2596+
25852597 else :
25862598 num = int (math .ceil (wordwidth / datawidth ))
25872599 mask = (2 ** datawidth ) - 1
@@ -2602,6 +2614,9 @@ def to_memory_image(filename, array, length=None,
26022614 f .write (fmt % cat )
26032615 values = []
26042616
2617+ num_lines = len (array ) // num
2618+ return num_lines
2619+
26052620
26062621def aligned_shape (shape , datawidth , mem_datawidth ):
26072622 aligned_shape = list (shape [:])
0 commit comments