mirror of
https://github.com/enjoy-digital/litex.git
synced 2025-01-04 09:52:26 -05:00
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
# This file is Copyright (c) 2019 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
# License: BSD
|
|
|
|
import os
|
|
import math
|
|
import json
|
|
import time
|
|
import struct
|
|
import datetime
|
|
|
|
from migen import *
|
|
|
|
# Helpers ----------------------------------------------------------------------------------------
|
|
|
|
def get_version(with_time=True):
|
|
fmt = "%Y-%m-%d %H:%M:%S" if with_time else "%Y-%m-%d"
|
|
return datetime.datetime.fromtimestamp(time.time()).strftime(fmt)
|
|
|
|
def get_mem_data(filename_or_regions, endianness="big", mem_size=None):
|
|
# create memory regions
|
|
if isinstance(filename_or_regions, dict):
|
|
regions = filename_or_regions
|
|
else:
|
|
filename = filename_or_regions
|
|
_, ext = os.path.splitext(filename)
|
|
if ext == ".json":
|
|
f = open(filename, "r")
|
|
regions = json.load(f)
|
|
f.close()
|
|
else:
|
|
regions = {filename: "0x00000000"}
|
|
|
|
# determine data_size
|
|
data_size = 0
|
|
for filename, base in regions.items():
|
|
data_size = max(int(base, 16) + os.path.getsize(filename), data_size)
|
|
assert data_size > 0
|
|
if mem_size is not None:
|
|
assert data_size < mem_size, (
|
|
"file is too big: {}/{} bytes".format(
|
|
data_size, mem_size))
|
|
|
|
# fill data
|
|
data = [0]*math.ceil(data_size/4)
|
|
for filename, base in regions.items():
|
|
with open(filename, "rb") as f:
|
|
i = 0
|
|
while True:
|
|
w = f.read(4)
|
|
if not w:
|
|
break
|
|
if len(w) != 4:
|
|
for _ in range(len(w), 4):
|
|
w += b'\x00'
|
|
if endianness == "little":
|
|
data[int(base, 16)//4 + i] = struct.unpack("<I", w)[0]
|
|
else:
|
|
data[int(base, 16)//4 + i] = struct.unpack(">I", w)[0]
|
|
i += 1
|
|
return data
|