├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.md ├── bin ├── csv2vcd ├── csv2vcd.real └── csv2vcd.vector ├── examples ├── ex_bus_hex.csv ├── ex_integer.7z └── ex_real.csv └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | __pycache__ 3 | 4 | # Packages 5 | *.egg 6 | *.egg-info 7 | dist 8 | build 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.md 2 | include LICENSE 3 | graft examples 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | csv2vcd 2 | ======= 3 | 4 | Signal analyzer CSV to IEEE 1364-2001 VCD file format converter. 5 | 6 | This small scripts allows to convert CSV sampling outputs from several signal 7 | analyzers to IEEE 1364-2001 VCD. Value Change Dump is the format used by 8 | GtkWave http://gtkwave.sourceforge.net/. 9 | 10 | Note: Currently there is 3 executables, the main supports integer variables, 11 | another supports real variables, and the last one supports vector 12 | variables. 13 | 14 | Usage 15 | ----- 16 | 17 | csv2vcd [input csv] [output vcd] 18 | 19 | Example files can be found in the ``examples`` folder. 20 | 21 | License 22 | ------- 23 | 24 | Licensed under the Apache License, Version 2.0 (the "License"); 25 | you may not use this file except in compliance with the License. 26 | You may obtain a copy of the License at: 27 | 28 | http://www.apache.org/licenses/LICENSE-2.0 29 | 30 | Unless required by applicable law or agreed to in writing, 31 | software distributed under the License is distributed on an 32 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 33 | KIND, either express or implied. See the License for the 34 | specific language governing permissions and limitations 35 | under the License. 36 | 37 | Improvements 38 | ------------ 39 | 40 | - Merge all executables to support integer, real and vector variables with the 41 | same codebase. 42 | - Allow to setup CSV input clock resolution in the command line. 43 | -------------------------------------------------------------------------------- /bin/csv2vcd: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # csv2vcd - Signal analyzer CSV to IEEE 1364-2001 VCD file format converter 5 | # 6 | # Copyright (C) 2014 Carlos Jenkins 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, 15 | # software distributed under the License is distributed on an 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | # KIND, either express or implied. See the License for the 18 | # specific language governing permissions and limitations 19 | # under the License. 20 | 21 | from email.Utils import formatdate 22 | from os.path import isfile, exists, basename, splitext 23 | import sys 24 | 25 | 26 | """ 27 | Symbols available for signals. 28 | """ 29 | symbols = [chr(i) for i in range(33, 127)] 30 | 31 | """ 32 | Template for signal specification on VCD file format. 33 | """ 34 | signal_tpl = '$var wire 1 {symbol} {name} $end' 35 | 36 | """ 37 | Template for signal change on VCD file format. 38 | """ 39 | changed_tpl = '{}{}' 40 | 41 | """ 42 | Header template for VCD file format. 43 | """ 44 | header_tpl = '''\ 45 | $date 46 | {date} 47 | $end 48 | $version 49 | 1.0 50 | $end 51 | $timescale 52 | 1ps 53 | $end 54 | $scope module {module} $end 55 | {signals} 56 | $upscope $end 57 | $enddefinitions $end 58 | $dumpvars 59 | #0 60 | {initial} 61 | $end 62 | ''' 63 | 64 | """ 65 | Time unit conversion table. 66 | """ 67 | units = { 68 | 'ps': 1, 69 | 'ns': 1000, 70 | 'us': 1000000, 71 | 'ms': 1000000000, 72 | 's': 1000000000000 73 | } 74 | 75 | 76 | def get_parts(line): 77 | """ 78 | Parse a line of the form: 79 | 80 | "{signal_a},{signal_b},{...},{signal_x},{float} {time_unit}" 81 | 82 | For example: 83 | 84 | 0,1,0,0,-52.415000 us 85 | 86 | And return a tuple of time in pico seconds and a list of signals: 87 | 88 | ({time_ps}, [{signal_a}, {signal_b}, {...}, {signal_x}]) 89 | 90 | For example: 91 | 92 | (-52415000, [0, 1, 0, 0]) 93 | """ 94 | parts = line.strip().split(',') 95 | time, unit = parts[-1].split() 96 | del(parts[-1]) 97 | 98 | ctime = int(float(time) * units[unit]) 99 | cparts = [int(p) for p in parts] 100 | # or... cparts = map(int, parts) 101 | return (ctime, cparts) 102 | 103 | 104 | def convert(module, infile, outfile): 105 | """ 106 | Given an input CSV and an output file perform the conversion of the first 107 | and write the result to the second. 108 | """ 109 | 110 | # Get signals 111 | fline = infile.readline() 112 | # -> List of signals names 113 | psignals = [h[1:-1] for h in fline.strip().split(',')[:-1]] 114 | # -> Check symbol capacity 115 | if len(psignals) > len(symbols): 116 | sys.stderr.write('ERROR: Not enough symbols available.\n') 117 | return False 118 | # -> VCD formatted signals descriptors 119 | fsignals = [ 120 | signal_tpl.format(symbol=symbols[i], name=psignals[i]) 121 | for i in range(len(psignals)) 122 | ] 123 | 124 | # Skip garbage lines 125 | found = False 126 | for i, l in enumerate(infile, 1): 127 | if l.strip().endswith('0 s'): 128 | sys.stdout.write('Breakpoint at line #{}.\n'.format(i)) 129 | found = True 130 | break 131 | 132 | # Check that breakpoint was found 133 | if not found: 134 | sys.stdout.write('ERROR: Breakpoint "0 s" not found.\n') 135 | return False 136 | 137 | # Initial state 138 | tinit, pinit = get_parts(l) 139 | 140 | finit = [ 141 | changed_tpl.format(pinit[i], symbols[i]) for i in range(len(pinit)) 142 | ] 143 | 144 | # Write VCD header 145 | outfile.write(header_tpl.format( 146 | date=formatdate(), 147 | module=module, 148 | signals='\n'.join(fsignals), 149 | initial='\n'.join(finit) 150 | )) 151 | 152 | # Convert lines 153 | previous = pinit 154 | changed = [] 155 | for l in infile: 156 | timestamp, signals = get_parts(l) 157 | for p, n, s in zip(previous, signals, symbols): 158 | if p != n: 159 | changed.append(changed_tpl.format(n, s)) 160 | 161 | if changed: 162 | outfile.write('#') 163 | outfile.write(str(timestamp)) 164 | outfile.write('\n') 165 | for c in changed: 166 | outfile.write(c) 167 | outfile.write('\n') 168 | del(changed[:]) 169 | previous = signals 170 | 171 | return True 172 | 173 | 174 | def main(): 175 | """ 176 | Main function. Handle arguments and files. 177 | """ 178 | 179 | # Check arguments 180 | if len(sys.argv) not in (2, 3): 181 | sys.stderr.write('USAGE: csv2vcd [input csv] [output vcd]\n') 182 | sys.exit(-1) 183 | 184 | # Determine files 185 | input_file = sys.argv[1] 186 | module = splitext(basename(input_file))[0] 187 | if len(sys.argv) != 3: 188 | output_file = module + '.vcd' 189 | else: 190 | output_file = sys.argv[2] 191 | 192 | # Check files 193 | if not isfile(input_file): 194 | sys.stderr.write( 195 | 'ERROR: Input file {} doesn\'t exists.\n'.format( 196 | input_file 197 | )) 198 | sys.exit(-1) 199 | 200 | if exists(output_file) and not isfile(output_file): 201 | sys.stderr.write( 202 | 'ERROR: Output file {} exists and is not a file.\n'.format( 203 | output_file 204 | )) 205 | sys.exit(-1) 206 | 207 | with open(input_file, 'r') as infile: 208 | with open(output_file, 'w') as outfile: 209 | if not convert(module, infile, outfile): 210 | sys.exit(-1) 211 | sys.exit(0) 212 | 213 | 214 | if __name__ == '__main__': 215 | main() 216 | -------------------------------------------------------------------------------- /bin/csv2vcd.real: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # csv2vcd - Signal analyzer CSV to IEEE 1364-2001 VCD file format converter 5 | # 6 | # Copyright (C) 2014 Carlos Jenkins 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, 15 | # software distributed under the License is distributed on an 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | # KIND, either express or implied. See the License for the 18 | # specific language governing permissions and limitations 19 | # under the License. 20 | 21 | from email.Utils import formatdate 22 | from os.path import isfile, exists, basename, splitext 23 | import sys 24 | 25 | 26 | """ 27 | Symbols available for signals. 28 | """ 29 | symbols = [chr(i) for i in range(33, 127)] 30 | 31 | """ 32 | Template for signal specification on VCD file format. 33 | """ 34 | signal_tpl = '$var real 1 {symbol} {name} $end' 35 | 36 | """ 37 | Template for signal change on VCD file format. 38 | """ 39 | changed_tpl = 'r{:.16f} {}' 40 | 41 | """ 42 | Header template for VCD file format. 43 | """ 44 | header_tpl = '''\ 45 | $date 46 | {date} 47 | $end 48 | $version 49 | 1.0 50 | $end 51 | $timescale 52 | 1ps 53 | $end 54 | $scope module {module} $end 55 | {signals} 56 | $upscope $end 57 | $enddefinitions $end 58 | $dumpvars 59 | #0 60 | {initial} 61 | $end 62 | ''' 63 | 64 | """ 65 | Time unit conversion table. 66 | """ 67 | units = { 68 | 'ps': 1, 69 | 'ns': 1000, 70 | 'us': 1000000, 71 | 'ms': 1000000000, 72 | 's': 1000000000000 73 | } 74 | 75 | 76 | def get_parts(line): 77 | """ 78 | Parse a line of the form: 79 | 80 | "{signal_a},{signal_b},{...},{signal_x},{float} {time_unit}" 81 | 82 | For example: 83 | 84 | 0.5,1.0,0.0,0.0,-52.415000 us 85 | 86 | And return a tuple of time in pico seconds and a list of signals: 87 | 88 | ({time_ps}, [{signal_a}, {signal_b}, {...}, {signal_x}]) 89 | 90 | For example: 91 | 92 | (-52415000, [0.5, 1.0, 0.0, 0.0]) 93 | """ 94 | parts = line.strip().split(',') 95 | time, unit = parts[-1].split() 96 | del(parts[-1]) 97 | 98 | ctime = int(float(time) * units[unit]) 99 | cparts = [float(p) for p in parts] 100 | # or... cparts = map(int, parts) 101 | return (ctime, cparts) 102 | 103 | 104 | def convert(module, infile, outfile): 105 | """ 106 | Given an input CSV and an output file perform the conversion of the first 107 | and write the result to the second. 108 | """ 109 | 110 | # Get signals 111 | fline = infile.readline() 112 | # -> List of signals names 113 | psignals = [h[1:-1] for h in fline.strip().split(',')[:-1]] 114 | # -> Check symbol capacity 115 | if len(psignals) > len(symbols): 116 | sys.stderr.write('ERROR: Not enough symbols available.\n') 117 | return False 118 | # -> VCD formatted signals descriptors 119 | fsignals = [ 120 | signal_tpl.format(symbol=symbols[i], name=psignals[i]) 121 | for i in range(len(psignals)) 122 | ] 123 | 124 | # Skip garbage lines 125 | found = False 126 | for i, l in enumerate(infile, 1): 127 | if l.strip().endswith('0 s'): 128 | sys.stdout.write('Breakpoint at line #{}.\n'.format(i)) 129 | found = True 130 | break 131 | 132 | # Check that breakpoint was found 133 | if not found: 134 | sys.stdout.write('ERROR: Breakpoint "0 s" not found.\n') 135 | return False 136 | 137 | # Initial state 138 | tinit, pinit = get_parts(l) 139 | 140 | finit = [ 141 | changed_tpl.format(pinit[i], symbols[i]) for i in range(len(pinit)) 142 | ] 143 | 144 | # Write VCD header 145 | outfile.write(header_tpl.format( 146 | date=formatdate(), 147 | module=module, 148 | signals='\n'.join(fsignals), 149 | initial='\n'.join(finit) 150 | )) 151 | 152 | # Convert lines 153 | previous = pinit 154 | changed = [] 155 | for l in infile: 156 | timestamp, signals = get_parts(l) 157 | for p, n, s in zip(previous, signals, symbols): 158 | if p != n: 159 | changed.append(changed_tpl.format(n, s)) 160 | 161 | if changed: 162 | outfile.write('#') 163 | outfile.write(str(timestamp)) 164 | outfile.write('\n') 165 | for c in changed: 166 | outfile.write(c) 167 | outfile.write('\n') 168 | del(changed[:]) 169 | previous = signals 170 | 171 | return True 172 | 173 | 174 | def main(): 175 | """ 176 | Main function. Handle arguments and files. 177 | """ 178 | 179 | # Check arguments 180 | if len(sys.argv) not in (2, 3): 181 | sys.stderr.write('USAGE: csv2vcd [input csv] [output vcd]\n') 182 | sys.exit(-1) 183 | 184 | # Determine files 185 | input_file = sys.argv[1] 186 | module = splitext(basename(input_file))[0] 187 | if len(sys.argv) != 3: 188 | output_file = module + '.vcd' 189 | else: 190 | output_file = sys.argv[2] 191 | 192 | # Check files 193 | if not isfile(input_file): 194 | sys.stderr.write( 195 | 'ERROR: Input file {} doesn\'t exists.\n'.format( 196 | input_file 197 | )) 198 | sys.exit(-1) 199 | 200 | if exists(output_file) and not isfile(output_file): 201 | sys.stderr.write( 202 | 'ERROR: Output file {} exists and is not a file.\n'.format( 203 | output_file 204 | )) 205 | sys.exit(-1) 206 | 207 | with open(input_file, 'r') as infile: 208 | with open(output_file, 'w') as outfile: 209 | if not convert(module, infile, outfile): 210 | sys.exit(-1) 211 | sys.exit(0) 212 | 213 | 214 | if __name__ == '__main__': 215 | main() 216 | -------------------------------------------------------------------------------- /bin/csv2vcd.vector: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # csv2vcd - Signal analyzer CSV to IEEE 1364-2001 VCD file format converter 5 | # 6 | # Copyright (C) 2014 Carlos Jenkins 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, 15 | # software distributed under the License is distributed on an 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | # KIND, either express or implied. See the License for the 18 | # specific language governing permissions and limitations 19 | # under the License. 20 | 21 | from email.Utils import formatdate 22 | from os.path import isfile, exists, basename, splitext 23 | import sys 24 | 25 | 26 | """ 27 | Symbols available for signals. 28 | """ 29 | symbols = [chr(i) for i in range(33, 127)] 30 | 31 | """ 32 | Template for signal specification on VCD file format. 33 | """ 34 | signal_tpl = '$var wire {vector} {symbol} {name} $end' 35 | 36 | """ 37 | Template for signal change on VCD file format. 38 | """ 39 | changed_tpl = '{}{}' 40 | 41 | """ 42 | Header template for VCD file format. 43 | """ 44 | header_tpl = '''\ 45 | $date 46 | {date} 47 | $end 48 | $version 49 | 1.0 50 | $end 51 | $timescale 52 | 1ns 53 | $end 54 | $scope module {module} $end 55 | {signals} 56 | $upscope $end 57 | $enddefinitions $end 58 | $dumpvars 59 | #0 60 | {initial} 61 | $end 62 | ''' 63 | 64 | """ 65 | Time unit conversion table. 66 | """ 67 | units = { 68 | 'ns': 1, 69 | 'us': 1000, 70 | 'ms': 1000000, 71 | 's': 1000000000 72 | } 73 | 74 | # This list stores the multi-bit signal position in the line and its 75 | # corresponding width 76 | bus_index = [] 77 | 78 | 79 | def get_parts(line): 80 | """ 81 | Parse a line of the form: 82 | 83 | "{signal_a},{signal_b},{...},{signal_x},{float} {time_unit}" 84 | 85 | For example: 86 | 87 | 0,1,0,0,-52.415000 us 88 | 89 | And return a tuple of time in pico seconds and a list of signals: 90 | 91 | ({time_ps}, [{signal_a}, {signal_b}, {...}, {signal_x}]) 92 | 93 | For example: 94 | 95 | (-52415000, [0, 1, 0, 0]) 96 | """ 97 | parts = line.strip().split(',') 98 | time, unit = parts[-1].split() 99 | del(parts[-1]) 100 | 101 | ctime = int(float(time) * units[unit]) 102 | for i, p in enumerate(parts): 103 | for x in bus_index: 104 | # If an item in the list parts is a multi-bit signal 105 | if x[0] == i: 106 | # For each multi-bit signal, convert it to binary format with 107 | # zero-padding add a white-space in between the signal value 108 | # and the symbol assigned to it in VCD 109 | parts[i] = 'b' + p.zfill(int(x[1])) + ' ' 110 | 111 | # cparts = [int(p) for p in parts] 112 | # or... cparts = map(int, parts) 113 | return (ctime, parts) 114 | 115 | 116 | def convert(module, infile, outfile): 117 | """ 118 | Given an input CSV and an output file perform the conversion of the first 119 | and write the result to the second. 120 | """ 121 | 122 | # Get signals 123 | fline = infile.readline() 124 | # -> List of signals names 125 | psignals = [h[1:-1] for h in fline.strip().split(',')[:-1]] 126 | # -> Check symbol capacity 127 | if len(psignals) > len(symbols): 128 | sys.stderr.write('ERROR: Not enough symbols available.\n') 129 | return False 130 | # -> VCD formatted signals descriptors 131 | 132 | # List to store multi-bit signal width 133 | vectors = [] 134 | for i in range(len(psignals)): 135 | # In the csv file the multi-bit signal should be declared in the given 136 | # format: signal_name [width-1:0] 137 | if '[' in psignals[i]: 138 | width = psignals[i][ 139 | (psignals[i].index('[') + 1):psignals[i].index(':') 140 | ] 141 | vectors.append(str(int(width) + 1)) 142 | 143 | # For multi-bit signal example: Data [31:0], signal width is 32 144 | bus_index.append([i, vectors[i]]) 145 | else: 146 | vectors.append('1') # Width for single-bit signal should be 1 147 | 148 | # Added vectors to the signal tuple to show signal width in VCD 149 | fsignals = [ 150 | signal_tpl.format( 151 | vector=vectors[i], 152 | symbol=symbols[i], 153 | name=psignals[i] 154 | ) 155 | for i in range(len(psignals)) 156 | ] 157 | 158 | # Skip garbage lines 159 | found = False 160 | for i, l in enumerate(infile, 1): 161 | if l.strip().endswith('0 s'): 162 | sys.stdout.write('Breakpoint at line #{}.\n'.format(i)) 163 | found = True 164 | break 165 | 166 | # Check that breakpoint was found 167 | if not found: 168 | sys.stdout.write('ERROR: Breakpoint "0 s" not found.\n') 169 | return False 170 | 171 | # Initial state 172 | tinit, pinit = get_parts(l) 173 | 174 | finit = [ 175 | changed_tpl.format(pinit[i], symbols[i]) for i in range(len(pinit)) 176 | ] 177 | 178 | # Write VCD header 179 | outfile.write(header_tpl.format( 180 | date=formatdate(), 181 | module=module, 182 | signals='\n'.join(fsignals), 183 | initial='\n'.join(finit) 184 | )) 185 | 186 | # Convert lines 187 | previous = pinit 188 | changed = [] 189 | for l in infile: 190 | timestamp, signals = get_parts(l) 191 | for p, n, s in zip(previous, signals, symbols): 192 | if p != n: 193 | changed.append(changed_tpl.format(n, s)) 194 | 195 | if changed: 196 | outfile.write('#') 197 | outfile.write(str(timestamp)) 198 | outfile.write('\n') 199 | for c in changed: 200 | outfile.write(c) 201 | outfile.write('\n') 202 | del(changed[:]) 203 | previous = signals 204 | 205 | return True 206 | 207 | 208 | def main(): 209 | """ 210 | Main function. Handle arguments and files. 211 | """ 212 | 213 | # Check arguments 214 | if len(sys.argv) not in (2, 3): 215 | sys.stderr.write('USAGE: csv2vcd [input csv] [output vcd]\n') 216 | sys.exit(-1) 217 | 218 | # Determine files 219 | input_file = sys.argv[1] 220 | module = splitext(basename(input_file))[0] 221 | if len(sys.argv) != 3: 222 | output_file = module + '.vcd' 223 | else: 224 | output_file = sys.argv[2] 225 | 226 | # Check files 227 | if not isfile(input_file): 228 | sys.stderr.write( 229 | 'ERROR: Input file {} doesn\'t exists.\n'.format( 230 | input_file 231 | )) 232 | sys.exit(-1) 233 | 234 | if exists(output_file) and not isfile(output_file): 235 | sys.stderr.write( 236 | 'ERROR: Output file {} exists and is not a file.\n'.format( 237 | output_file 238 | )) 239 | sys.exit(-1) 240 | 241 | with open(input_file, 'r') as infile: 242 | with open(output_file, 'w') as outfile: 243 | if not convert(module, infile, outfile): 244 | sys.exit(-1) 245 | sys.exit(0) 246 | 247 | 248 | if __name__ == '__main__': 249 | main() 250 | -------------------------------------------------------------------------------- /examples/ex_bus_hex.csv: -------------------------------------------------------------------------------- 1 | "My_Bus_3[7:0]","My_Bus_1[15:0]","Reset","CS","WR","RS","d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","My_Bus_2[7:0]","Time" 2 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.715100 ms 3 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.715000 ms 4 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714900 ms 5 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714800 ms 6 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714700 ms 7 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714600 ms 8 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714500 ms 9 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714400 ms 10 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714300 ms 11 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714200 ms 12 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714100 ms 13 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.714000 ms 14 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713900 ms 15 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713800 ms 16 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713700 ms 17 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713600 ms 18 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713500 ms 19 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713400 ms 20 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713300 ms 21 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713200 ms 22 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713100 ms 23 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.713000 ms 24 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712900 ms 25 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712800 ms 26 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712700 ms 27 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712600 ms 28 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712500 ms 29 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712400 ms 30 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712300 ms 31 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712200 ms 32 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712100 ms 33 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.712000 ms 34 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711900 ms 35 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711800 ms 36 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711700 ms 37 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711600 ms 38 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711500 ms 39 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711400 ms 40 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711300 ms 41 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711200 ms 42 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711100 ms 43 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.711000 ms 44 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.710900 ms 45 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.710800 ms 46 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.710700 ms 47 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.710600 ms 48 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.710500 ms 49 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.710400 ms 50 | 00,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00,-209.710300 ms 51 | -------------------------------------------------------------------------------- /examples/ex_integer.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/carlos-jenkins/csv2vcd/5e7d6272e6e0d54a42d51f443198d6ff77296977/examples/ex_integer.7z -------------------------------------------------------------------------------- /examples/ex_real.csv: -------------------------------------------------------------------------------- 1 | "CLK","CS","DOUT","DIN","Time" 2 | 0.0,0.0,0.0,0.0,-10.000 ns 3 | 0.0,0.0,0.0,0.0,-8.333 ns 4 | 0.0,0.0,0.0,0.0,-6.667 ns 5 | 0.0,0.0,0.0,0.0,-5.000 ns 6 | 0.0,0.0,0.0,0.0,-3.333 ns 7 | 0.0,0.0,0.0,0.0,-1.667 ns 8 | 1.0,0.0,0.0,0.0,0 s 9 | 2.0,0.0,0.0,0.0,1.667 ns 10 | 3.0,1.2,0.0,0.0,3.333 ns 11 | 3.5,3.4,0.0,0.0,5.000 ns 12 | 4.1,2.2,0.0,0.0,6.667 ns 13 | 2.3,2.1,0.0,0.0,8.333 ns 14 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from setuptools import setup 4 | 5 | setup( 6 | name='csv2vcd', 7 | version='1.0', 8 | description= 9 | 'Signal analyzer CSV to IEEE 1364-2001 VCD file format converter', 10 | author='Carlos Jenkins', 11 | author_email='carlos@jenkins.co.cr', 12 | url='https://github.com/carlos-jenkins/csv2vcd', 13 | #packages=['csv2vcd'], 14 | #package_dir={'': 'lib'}, 15 | scripts=['bin/csv2vcd'], 16 | classifiers=[ 17 | 'License :: OSI Approved :: Apache Software License', 18 | ], 19 | #test_suite='test', 20 | setup_requires=[ 21 | 'flake8' 22 | ] 23 | ) 24 | --------------------------------------------------------------------------------