Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / lib / site_perl / 5.8.0 / Midas / Command.pm
CommitLineData
86530b38
AT
1# -*- perl -*-
2
3package Midas::Command;
4use strict;
5
6use Getopt::Long;
7use File::Spec;
8use File::Basename;
9use File::Spec;
10use Cwd;
11
12use Midas::Error;
13use Midas::Globals;
14
15use TRELoad 'BitFieldTie';
16
17require Exporter;
18
19our @ISA = qw(Exporter);
20our @EXPORT = qw(run_command chat fatal banner string2bf hexstring2bf
21 verbose opt_n
22 );
23
24our $Prg = basename $0;
25#our %opt = ( verbose => 1 );
26our $Verbose = 1;
27our $N = 0;
28
29##############################################################################
30
31sub verbose {
32 return $$Verbose if ref $Verbose;
33 return $Verbose;
34}
35
36##############################################################################
37
38sub opt_n {
39 return $$N if ref $N;
40 return $N;
41}
42
43##############################################################################
44
45sub fatal {
46 my $msg = shift;
47 my $code = shift;
48
49 my $obj = Midas::Error->throw($msg, $code);
50
51 die $obj;
52}
53
54##############################################################################
55
56sub chat {
57 my $msgs = shift;
58 my $level = shift;
59
60 $level = 1 unless defined $level;
61 return unless verbose >= $level;
62
63 local($_);
64 my @lines = split /\n/, "$msgs";
65 my $string = "$Prg: " . (join "\n$Prg: $_", @lines);
66
67 print "$string\n";
68}
69
70##############################################################################
71
72sub banner {
73 my @lines = @_;
74
75 chat "###########################################################\n", 2;
76
77 foreach my $line (@lines) {
78 print "$Prg: ## $line\n" if verbose > 0;
79 }
80
81 chat "###########################################################\n", 2;
82}
83
84##############################################################################
85
86sub configure {
87 my %args = @_;
88
89 foreach my $key (keys %args) {
90 if($key eq 'verbose') {
91 $Verbose = $args{$key};
92 } elsif($key eq 'n') {
93 $N = $args{$key};
94 } else {
95 fatal "Midas::Command does not recongnize configuration option ".
96 "$key.\n";
97 }
98 }
99
100 return ( verbose => $Verbose,
101 n => $N,
102 );
103
104}
105
106##############################################################################
107
108sub run_command {
109 my $command = shift;
110 my @flags = @_;
111
112 my %cmd_opt = (
113 verbose => 1,
114 fatal => 1,
115 errcode => M_CMDFAIL,
116 pass_errcode => 0,
117 );
118 my @options = qw( verbose=i fatal! errcode=i pass_errcode! );
119 local (@ARGV) = @flags;
120 Getopt::Long::Configure('no_pass_through');
121 GetOptions(\%cmd_opt, @options) or
122 fatal "Cannot parse flags to run_command: @flags\n";
123
124 if(verbose >= $cmd_opt{verbose}) {
125 print "$Prg: $command\n";
126 }
127 my $status = 0;
128 $status = system($command) unless opt_n;
129 if($cmd_opt{fatal} and $status) {
130 my $exit = $status >> 8;
131 my $sig = $status & 127;
132 my $core = $status & 128;
133 my $core_msg = $core ? "Dumped core." : "";
134
135 if($sig) {
136 fatal "Command \"$command\" died via signal $sig. $core_msg\n",
137 $cmd_opt{errcode};
138 }
139
140 if($cmd_opt{pass_errcode} and exists $ERRCODES{$exit}) {
141 fatal "Command \"$command\" failed. $core_msg\n", $exit;
142 } else {
143 fatal "Command \"$command\" failed with status $exit. $core_msg\n",
144 $cmd_opt{errcode};
145 }
146 }
147}
148
149##############################################################################
150
151sub string2bf {
152 my $string = shift;
153 my $width = shift;
154
155 fatal "Cannot run string2bf without a width argument!\n", M_CODE
156 unless defined $width;
157
158 my $ishex = 0;
159 if($string =~ /^\s*0[xX]/ or ref($string) or $CONFIG{default_radix} eq 'hex')
160 {
161 $ishex = 1;
162 # Test for overflow
163 (my $mystring = $string) =~ s/^\s*0[xX]//;
164 $mystring =~ s/\s*$//;
165 my $max_digits = int($width / 4);
166 $max_digits++ if (($max_digits) * 4 != $width);
167
168 if(length $mystring > $max_digits) {
169 my $difference = (length $mystring) - $max_digits;
170 my $leader = substr($mystring, 0, $difference);
171 return "Hex overflow: \"$string\" won't fit in $width bits!\n"
172 unless $leader eq ('0' x $difference);
173 }
174 } else {
175
176 my $max_bf = BitFieldTie->new(65, "1");
177 $max_bf->left_shift($width);
178
179 my $test_bf;
180 eval {
181 $test_bf = BitFieldTie->new_dec(65, $string);
182 };
183 if(not $@) {
184 if($test_bf->ucompare($max_bf) >= 0) {
185 return "Dec Overflow: \"$string\" won't fit in $width bits!\n";
186 }
187 }
188
189 }
190
191
192 my $bf;
193 eval {
194 if($ishex) {
195 $bf = BitFieldTie->new($width, $string);
196 } else {
197 $bf = BitFieldTie->new_dec($width, $string);
198 }
199 };
200 if($@) {
201 return $@;
202 } else {
203 return $bf;
204 }
205}
206
207##############################################################################
208
209sub hexstring2bf {
210 my $string = shift;
211 my $width = shift;
212
213 $string = "0x$string" unless $string =~ /^\s*0[xX]/;
214 return string2bf($string, $width);
215}
216
217##############################################################################
2181;