| 1 | package Verilog::Readmem; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | require Exporter; | |||||||||
| 8 | 1 | our @ISA = qw(Exporter); | |||||||||
| 9 | 1 | our @EXPORT_OK = qw(parse_readmem); | |||||||||
| 10 | 1 | our @EXPORT; | |||||||||
| 11 | |||||||||||
| 12 | 1 | our $VERSION = '0.05'; | |||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | 8 | ▶ | sub bin2dec { | ||||||||
|
Subroutine
bin2dec: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 16 | 8 | return unpack("N", pack("B32", substr("0" x 32 . shift, -32))); | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 30 | ▶ | sub parse_readmem { | ||||||||
|
Subroutine
parse_readmem: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||
| 20 | 30 | my ($arg_ref) = @_; | |||||||||
| 21 | 30 | my $file; | |||||||||
| 22 | my $hex_mode; | ||||||||||
| 23 | 30 | my $numeric; | |||||||||
| 24 | |||||||||||
| 25 | # Check inputs. | ||||||||||
| 26 | 30 | ▶ | if (exists $arg_ref->{filename}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 29 | $file = $arg_ref->{filename}; | |||||||||
| 28 | } | ||||||||||
| 29 | else { | ||||||||||
| 30 | 1 | croak "Error: filename is required.\n"; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 29 | ▶ | if (exists $arg_ref->{binary}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 8 | ▶ | $hex_mode = ($arg_ref->{binary} eq 1) ? 0 : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | } | ||||||||||
| 36 | else { | ||||||||||
| 37 | 21 | $hex_mode = 1; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 29 | ▶ | if (exists $arg_ref->{string}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 9 | ▶ | $numeric = ($arg_ref->{string} eq 1) ? 0 : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | } | ||||||||||
| 43 | else { | ||||||||||
| 44 | 20 | $numeric = 1; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | # Remove comments from input file. | ||||||||||
| 48 | 29 | my $lines = remove_comments($file); | |||||||||
| 49 | 28 | $lines =~ s/^\s+//m; # Remove any leading whitespace prior to split | |||||||||
| 50 | 28 | my @tokens = split /\s+/, $lines; | |||||||||
| 51 | |||||||||||
| 52 | # Create array-of-arrays corresponding to all address blocks. | ||||||||||
| 53 | 28 | my @all_blocks; | |||||||||
| 54 | my @block; | ||||||||||
| 55 | 28 | push @block, '0'; | |||||||||
| 56 | 28 | for (@tokens) { | |||||||||
| 57 | 131236 | $_ = lc; | |||||||||
| 58 | 131236 | ▶ | if (/^@/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 25 | my $addr = check_addr($_, $numeric); | |||||||||
| 60 | 20 | ▶ | if (@block > 1) {push @all_blocks, [@block]} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 20 | @block = (); | |||||||||
| 62 | 20 | push @block, $addr; | |||||||||
| 63 | } | ||||||||||
| 64 | else { | ||||||||||
| 65 | 131211 | push @block, check_data($_, $hex_mode, $numeric); | |||||||||
| 66 | } | ||||||||||
| 67 | } | ||||||||||
| 68 | 16 | ▶ | if (@block > 1) {push @all_blocks, [@block]} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | |||||||||||
| 70 | 16 | return \@all_blocks; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 131211 | ▶ | sub check_data { | ||||||||
|
Subroutine
check_data: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||
| 74 | # Check for proper syntax of a data token. | ||||||||||
| 75 | # Return transformed data, if there are no errors. | ||||||||||
| 76 | 131211 | my ($dat, $hex_mode, $numeric) = @_; | |||||||||
| 77 | 131211 | ▶ | if ($dat =~ /^_/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 1 | croak "Error: illegal leading underscore for data '$dat'.\n"; | |||||||||
| 79 | } | ||||||||||
| 80 | 131210 | ▶ | if ($numeric) { # Convert to numeric | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 131169 | $dat =~ s/_//g; | |||||||||
| 82 | 131169 | ▶ | if ($hex_mode) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 131159 | ▶ | croak "Error: unsupported characters in 2-state readmemh input '$dat'.\n" if ($dat =~ /[^\da-f]/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 131158 | ▶ | croak "Error: Hex value exceeds 32-bits '$dat'.\n" if (length($dat) > 8); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 131157 | $dat = hex $dat; | |||||||||
| 86 | } | ||||||||||
| 87 | else { | ||||||||||
| 88 | 10 | ▶ | croak "Error: unsupported characters in 2-state readmemb input '$dat'.\n" if ($dat =~ /[^01]/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 9 | ▶ | croak "Error: Binary value exceeds 32-bits '$dat'.\n" if (length($dat) > 32); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 8 | $dat = bin2dec($dat); | |||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | else { # String mode | ||||||||||
| 94 | 41 | ▶ | if ($hex_mode) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 26 | ▶ | croak "Error: unsupported characters in 4-state readmemh input '$dat'.\n" if ($dat =~ /[^\da-fxz_]/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | } | ||||||||||
| 97 | else { | ||||||||||
| 98 | 15 | ▶ | croak "Error: unsupported characters in 4-state readmemb input '$dat'.\n" if ($dat =~ /[^01xz_]/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | 131204 | return $dat; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 25 | ▶ | sub check_addr { | ||||||||
|
Subroutine
check_addr: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||
| 105 | # Check for proper syntax of an address token. | ||||||||||
| 106 | # Return transformed address, if there are no errors. | ||||||||||
| 107 | 25 | my ($addr, $numeric) = @_; | |||||||||
| 108 | 25 | $addr =~ s/^@//; | |||||||||
| 109 | 25 | ▶ | return 0 unless length $addr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 23 | ▶ | if ($numeric) { # Convert to numeric | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 20 | $addr =~ s/_//g; | |||||||||
| 112 | 20 | ▶ | return 0 unless length $addr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 19 | ▶ | croak "Error: unsupported characters in 2-state address '$addr'.\n" if ($addr =~ /[^\da-f]/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 16 | ▶ | croak "Error: Hex address exceeds 32-bits '$addr'.\n" if (length($addr) > 8); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 15 | $addr = hex $addr; | |||||||||
| 116 | } | ||||||||||
| 117 | else { # String mode | ||||||||||
| 118 | 3 | ▶ | croak "Error: unsupported characters in 2-state string address '$addr'.\n" if ($addr =~ /[^\da-f_]/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | } | ||||||||||
| 120 | 17 | return $addr; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 29 | ▶ | sub remove_comments { | ||||||||
|
Subroutine
remove_comments: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 124 | # Remove C++ and Verilog comments from input file and return all | ||||||||||
| 125 | # lines as a string. | ||||||||||
| 126 | # Removes block comments (/**/) and single-line comments (//). | ||||||||||
| 127 | |||||||||||
| 128 | # Slurp file into $lines variable. | ||||||||||
| 129 | 29 | my $file = shift; | |||||||||
| 130 | 29 | local $/ = undef; | |||||||||
| 131 | 29 | ▶ | open my $IN_FH, '<', $file or croak "Error: Can not open file $file: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 28 | my $lines = <$IN_FH>; | |||||||||
| 133 | 28 | close $IN_FH; | |||||||||
| 134 | |||||||||||
| 135 | # 1st, insert space before all /* | ||||||||||
| 136 | # This handles corner case not handled by perlfaq6 regex below. | ||||||||||
| 137 | # If the input file contains "123/*456*/789", the perlfaq6 regex | ||||||||||
| 138 | # will remove the comments, but leave a single value: 123789. | ||||||||||
| 139 | # But, ncverilog and vcs will replace the comment with a space, | ||||||||||
| 140 | # leaving 2 values: 123 and 789. This is the desired behavior. | ||||||||||
| 141 | # Wait... before we do that, we have to account for the other | ||||||||||
| 142 | # corner case of "//*": this is really a single-line comment, | ||||||||||
| 143 | # not a multi-line comment. | ||||||||||
| 144 | 28 | $lines =~ s{//\*}{// \*}g; | |||||||||
| 145 | 28 | $lines =~ s{/\*}{ /\*}g; | |||||||||
| 146 | |||||||||||
| 147 | # Use regex from perlfaq6 (C++ comments). | ||||||||||
| 148 | 28 | ▶ | $lines =~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | |||||||||||
| 150 | # Returns all lines as a string | ||||||||||
| 151 | 28 | return $lines; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | |||||||||||
| 155 | =head1 NAME | ||||||||||
| 156 | |||||||||||
| 157 | Verilog::Readmem - Parse Verilog $readmemh or $readmemb text file | ||||||||||
| 158 | |||||||||||
| 159 | =head1 VERSION | ||||||||||
| 160 | |||||||||||
| 161 | This document refers to Verilog::Readmem version 0.05. | ||||||||||
| 162 | |||||||||||
| 163 | =head1 SYNOPSIS | ||||||||||
| 164 | |||||||||||
| 165 | use Verilog::Readmem qw(parse_readmem); | ||||||||||
| 166 | |||||||||||
| 167 | # Read memory file into Array-Of-Arrays data structure: | ||||||||||
| 168 | my $mem_ref = parse_readmem({filename => 'memory.hex'}); | ||||||||||
| 169 | |||||||||||
| 170 | my $num_blocks = scalar @{$mem_ref}; | ||||||||||
| 171 | print "num_blocks = $num_blocks\n"; | ||||||||||
| 172 | |||||||||||
| 173 | # It is typical to have only one data block. | ||||||||||
| 174 | # Sum up all data values. | ||||||||||
| 175 | if ($num_blocks == 1) { | ||||||||||
| 176 | my ($addr, @data) = @{ $mem_ref->[0] }; | ||||||||||
| 177 | my $sum = 0; | ||||||||||
| 178 | for (@data) { $sum += $_ } | ||||||||||
| 179 | print "addr = $addr, data sum = $sum\n"; | ||||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | =head1 DESCRIPTION | ||||||||||
| 183 | |||||||||||
| 184 | The Verilog Hardware Description Language (HDL) provides a convenient | ||||||||||
| 185 | way to load a memory during logic simulation. The C<$readmemh()> and | ||||||||||
| 186 | C<$readmemb()> system tasks are used in the HDL source code to import | ||||||||||
| 187 | the contents of a text file into a memory variable. | ||||||||||
| 188 | |||||||||||
| 189 | In addition to having the simulator software read in these memory files, | ||||||||||
| 190 | it is also useful to analyze the contents of the file outside | ||||||||||
| 191 | of the simulator. For example, it may be useful to derive some | ||||||||||
| 192 | simulation parameters from the memory file prior to running the | ||||||||||
| 193 | simulation. Data stored at different addresses may be combined | ||||||||||
| 194 | arithmetically to produce other meaningful values. In some cases, | ||||||||||
| 195 | it is simpler to perform these calculations outside of the simulator. | ||||||||||
| 196 | |||||||||||
| 197 | C<Verilog::Readmem> emulates the Verilog C<$readmemh()> and C<$readmemb()> | ||||||||||
| 198 | system tasks. The same memory file which is read in by the | ||||||||||
| 199 | simulator can also be read into a Perl program, potentially easing | ||||||||||
| 200 | the burden of having the HDL code perform numeric calculations | ||||||||||
| 201 | or string manipulations. | ||||||||||
| 202 | |||||||||||
| 203 | =head2 Input File Syntax | ||||||||||
| 204 | |||||||||||
| 205 | The syntax of the text file is described in the documentation of | ||||||||||
| 206 | the IEEE standard for Verilog. Briefly, the file contains two types | ||||||||||
| 207 | of tokens: data and optional addresses. The tokens are separated by | ||||||||||
| 208 | whitespace and comments. Comments may be single-line (//) or | ||||||||||
| 209 | multi-line (/**/), similar to C++. Addresses are specified by a leading | ||||||||||
| 210 | "at" character (@) and are always hexadecimal strings. Data values | ||||||||||
| 211 | are either hexadecimal strings (C<$readmemh>) or binary strings (C<$readmemb>). | ||||||||||
| 212 | Data and addresses may contain underscore (_) characters. The syntax | ||||||||||
| 213 | supports 4-state logic for data values (0, 1, x, z), where x represents | ||||||||||
| 214 | an unknown value and z represents the high impedance value. | ||||||||||
| 215 | |||||||||||
| 216 | If no address is specified, the data is assumed to start at address 0. | ||||||||||
| 217 | Similarly, if data exists before the first specified address, then that data | ||||||||||
| 218 | is assumed to start at address 0. | ||||||||||
| 219 | |||||||||||
| 220 | There are many corner cases which are not explicitly mentioned in the | ||||||||||
| 221 | Verilog document. In each instance, this module was designed to behave | ||||||||||
| 222 | the same as two widely-known, commercially-available simulators. | ||||||||||
| 223 | |||||||||||
| 224 | =head1 SUBROUTINES | ||||||||||
| 225 | |||||||||||
| 226 | |||||||||||
| 227 | =over 4 | ||||||||||
| 228 | |||||||||||
| 229 | =item parse_readmem | ||||||||||
| 230 | |||||||||||
| 231 | Read in a Verilog $readmem format text file and return the addresses | ||||||||||
| 232 | and data as a reference to an array of arrays. All comments are | ||||||||||
| 233 | stripped out. All options to the C<parse_readmem> function must | ||||||||||
| 234 | be passed as a single B<hash>. | ||||||||||
| 235 | |||||||||||
| 236 | =back | ||||||||||
| 237 | |||||||||||
| 238 | =head2 OPTIONS | ||||||||||
| 239 | |||||||||||
| 240 | =over 4 | ||||||||||
| 241 | |||||||||||
| 242 | =item filename | ||||||||||
| 243 | |||||||||||
| 244 | A filename must be provided. | ||||||||||
| 245 | |||||||||||
| 246 | my $mem_ref = parse_readmem({filename => 'memory.hex'}); | ||||||||||
| 247 | |||||||||||
| 248 | =item binary | ||||||||||
| 249 | |||||||||||
| 250 | By default, the input file format is hexadecimal, consistent with | ||||||||||
| 251 | the Verilog C<$readmemh()> system task. To read in a binary format, | ||||||||||
| 252 | consistent with the Verilog C<$readmemb()> system task, | ||||||||||
| 253 | use C<< binary=>1 >>. | ||||||||||
| 254 | |||||||||||
| 255 | my $mem_ref = parse_readmem({filename=>$file, binary=>1}); | ||||||||||
| 256 | |||||||||||
| 257 | =item string | ||||||||||
| 258 | |||||||||||
| 259 | By default, all addresses and data values will be converted | ||||||||||
| 260 | to numeric (decimal) values. If numeric conversion is not | ||||||||||
| 261 | desired, use C<< string=>1 >>. | ||||||||||
| 262 | |||||||||||
| 263 | my $mem_ref = parse_readmem({filename=>$file, string=>1}); | ||||||||||
| 264 | |||||||||||
| 265 | In numeric conversion mode, data must represent 2-state | ||||||||||
| 266 | logic (0 and 1). If an application requires 4-state logic (0, 1, x, z), | ||||||||||
| 267 | numeric conversion must be disabled using C<< string=>1 >>. | ||||||||||
| 268 | |||||||||||
| 269 | To parse a binary format file using string mode: | ||||||||||
| 270 | |||||||||||
| 271 | my $mem_ref = parse_readmem( | ||||||||||
| 272 | { | ||||||||||
| 273 | string => 1, | ||||||||||
| 274 | binary => 1, | ||||||||||
| 275 | filename => '/path/to/file.bin' | ||||||||||
| 276 | } | ||||||||||
| 277 | ); | ||||||||||
| 278 | |||||||||||
| 279 | =back | ||||||||||
| 280 | |||||||||||
| 281 | =head2 EXAMPLE | ||||||||||
| 282 | |||||||||||
| 283 | The returned array-of-arrays has the following structure: | ||||||||||
| 284 | |||||||||||
| 285 | [a0, d01, d02, d03], | ||||||||||
| 286 | [a1, d11, d12, d13, d14, d15], | ||||||||||
| 287 | [a2, d21, d22] | ||||||||||
| 288 | |||||||||||
| 289 | Each array corresponds to a block of memory. The first item in | ||||||||||
| 290 | each array is the start address of the block. All subsequent | ||||||||||
| 291 | items are data values. In the example above, there are 3 memory | ||||||||||
| 292 | blocks. The 1st block starts at address a0 and has 3 data values. | ||||||||||
| 293 | The 2nd block starts at address a1 and has 5 data values. | ||||||||||
| 294 | The 3rd block starts at address a2 and has 2 data values. | ||||||||||
| 295 | |||||||||||
| 296 | =head1 EXPORT | ||||||||||
| 297 | |||||||||||
| 298 | None by default. | ||||||||||
| 299 | |||||||||||
| 300 | =head1 DIAGNOSTICS | ||||||||||
| 301 | |||||||||||
| 302 | Error conditions cause the program to die using C<croak> from the | ||||||||||
| 303 | standard C<Carp> module. | ||||||||||
| 304 | |||||||||||
| 305 | =head1 LIMITATIONS | ||||||||||
| 306 | |||||||||||
| 307 | In the default numeric conversion mode, address and data values | ||||||||||
| 308 | may not be larger than 32-bit. If an application requires larger values, | ||||||||||
| 309 | numeric conversion must be disabled using C<< string=>1 >>. This allows | ||||||||||
| 310 | for post-processing of strings in either hexadecimal or binary format. | ||||||||||
| 311 | |||||||||||
| 312 | =head1 SEE ALSO | ||||||||||
| 313 | |||||||||||
| 314 | Refer to the following Verilog documentation: | ||||||||||
| 315 | |||||||||||
| 316 | IEEE Standard Verilog (c) Hardware Description Language | ||||||||||
| 317 | IEEE Std 1364-2001 | ||||||||||
| 318 | Version C | ||||||||||
| 319 | Section 17.2.8, "Loading memory data from a file" | ||||||||||
| 320 | |||||||||||
| 321 | =head1 AUTHOR | ||||||||||
| 322 | |||||||||||
| 323 | Gene Sullivan (gsullivan@cpan.org) | ||||||||||
| 324 | |||||||||||
| 325 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 326 | |||||||||||
| 327 | Copyright (c) 2008 Gene Sullivan. All rights reserved. | ||||||||||
| 328 | |||||||||||
| 329 | This module is free software; you can redistribute it and/or modify | ||||||||||
| 330 | it under the same terms as Perl itself. See L<perlartistic>. | ||||||||||
| 331 | |||||||||||
| 332 | =cut | ||||||||||
| 333 | |||||||||||
| 334 | 1 | 1; | |||||||||
| 335 | |||||||||||
| 336 | __END__ | ||||||||||