代码拉取完成,页面将自动刷新
# Copyright 2018 SiFive, Inc
# SPDX-License-Identifier: Apache-2.0
import argparse
import sys
try:
# Python 3
from itertools import zip_longest
except ImportError:
# Python 2
from itertools import izip_longest as zip_longest
# Copied from https://docs.python.org/3/library/itertools.html
def grouper(iterable, n, fillvalue=None):
"""Collect data into fixed-length chunks or blocks"""
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def convert(bit_width, infile, outfile):
byte_width = bit_width // 8
if sys.version_info >= (3, 0):
for row in grouper(infile.read(), byte_width, fillvalue=0):
# Reverse because in Verilog most-significant bit of vectors is first.
hex_row = ''.join('{:02x}'.format(b) for b in reversed(row))
outfile.write(hex_row + '\n')
else:
for row in grouper(infile.read(), byte_width, fillvalue='\x00'):
# Reverse because in Verilog most-significant bit of vectors is first.
hex_row = ''.join('{:02x}'.format(ord(b)) for b in reversed(row))
outfile.write(hex_row + '\n')
def main():
parser = argparse.ArgumentParser(
description='Convert a binary file to a format that can be read in '
'verilog via $readmemh(). By default read from stdin '
'and write to stdout.'
)
if sys.version_info >= (3, 0):
parser.add_argument('infile',
nargs='?',
type=argparse.FileType('rb'),
default=sys.stdin.buffer)
else:
parser.add_argument('infile',
nargs='?',
type=argparse.FileType('rb'),
default=sys.stdin)
parser.add_argument('outfile',
nargs='?',
type=argparse.FileType('w'),
default=sys.stdout)
parser.add_argument('--bit-width', '-w',
type=int,
required=True,
help='How many bits per row.')
args = parser.parse_args()
if args.bit_width % 8 != 0:
sys.exit("Cannot handle non-multiple-of-8 bit width yet.")
convert(args.bit_width, args.infile, args.outfile)
if __name__ == '__main__':
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。