| #!/usr/bin/env python3 |
| # Copyright 2023 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| """ |
| Generate 256-bitwidth vmem file from 32-bitwidth vmem file |
| """ |
| |
| import argparse |
| import re |
| |
| def main(): |
| """ |
| Args: |
| input: The input 32-bitwidth vmem file name. |
| output: The output 256-bitwidth vmem file name. |
| """ |
| parser = argparse.ArgumentParser(description='Generate 256-bitwidth vmem file.') |
| parser.add_argument("--input", |
| "-i", |
| action='store', |
| required=True, |
| help="Input 32-bitwidth vmem file") |
| parser.add_argument("--output", |
| "-o", |
| required=True, |
| help="Output 256-bitwidth vmem file") |
| parser.add_argument("--entry_addr", |
| default = 0x0, |
| help="Entry address of kelvin program") |
| args = parser.parse_args() |
| |
| # Open input/output files |
| infile = open(args.input, 'r') |
| outfile = open(args.output, 'w') |
| |
| # Fetch each line from 32-bitwidth vmem file and dump each data line for |
| # 256-bitwidth vmem file. |
| addr_base = args.entry_addr |
| mem_data_line = "" |
| word_per_line = 8 |
| for line in infile.readlines(): |
| # Ignore comment |
| if re.match(r"\/\*(.)*\*\/", line): |
| continue |
| tokens = line.split() |
| m = re.match(r"@([0-9a-fA-F]{8})", tokens[0]) |
| addr = int(m[1], 16) |
| for token in tokens[1:]: |
| mem_data_line = token + mem_data_line |
| addr = addr + 1 |
| # Write out a mem data line when it is ready |
| if (addr % word_per_line) == 0: |
| outfile.write(f"@{addr_base:08X} {mem_data_line}\n") |
| addr_base = int((addr + args.entry_addr) / word_per_line) |
| mem_data_line = "" |
| if mem_data_line: |
| outfile.write(f"@{addr_base:08X} {mem_data_line}\n") |
| infile.close() |
| outfile.close() |
| |
| if __name__ == "__main__": |
| main() |