Ones Complement Script  Go To Script Pool
# Ones-complement add a list of 16 bit numbers

$sum = 0;
while($theline = <>) {
if($theline =~ /^#/) {
# Do nothing - comment
} else {
chop($theline);
$sum = $sum + &ReadHex($theline);
if($sum > 65535) {
$sum = $sum - 65536;
$sum = $sum + 1;
}
printf("%s\tsum=%s\n",$theline,&WriteHex($sum));
}
}

sub ReadHex {
local($theline) = @_;
# Chop off any leading 0x indicator
$theline =~ s/0x//;
# Convert to lowercase
$theline =~ tr/A-Z/a-z/;
@digits = split(//,$theline);
$value = 0;
for($i=0; $i<=$#digits; $i=$i+1) {
$value = $value * 16 + HexDigitToInt($digits[$i]);
}
return $value;
}

sub WriteHex {
local($val) = @_;
$string = '';
while($val > 0) {
$rem = $val % 16;
$string = IntToHexDigit($rem) . $string;
$val = ($val - $rem) / 16;
}
return $string;
}

sub HexDigitToInt {
local($digit) = @_;
if($digit =~ /[0-9]/) {
return $digit;
} elsif ($digit eq 'a') {
return 10;
} elsif ($digit eq 'b') {
return 11;
} elsif ($digit eq 'c') {
return 12;
} elsif ($digit eq 'd') {
return 13;
} elsif ($digit eq 'e') {
return 14;
} elsif ($digit eq 'f') {
return 15;
} else {
printf("Error - non-hex digit in string: %s\n",$digit);
}
}

sub IntToHexDigit {
local($int) = @_;
if($int < 10) {
return $int;
} elsif ($int == 10) {
return 'a';
} elsif ($int == 11) {
return 'b';
} elsif ($int == 12) {
return 'c';
} elsif ($int == 13) {
return 'd';
} elsif ($int == 14) {
return 'e';
} elsif ($int == 15) {
return 'f';
} else {
printf("Error - non-hex digit in integer: %d\n",$int);
}
}
 

Other Scripts:

Batch Simulation
Compare Files
Convert
Def2Verilog
Gen XOR Tree
GetValue
IHEX2VHEX
nand tree
Name Instance
OnesCompSum
Preprocessor
Verilog Tag

Verilog Tag2
Verilog2DEF
VHDL2HTML
VRename
XNF2VHDL

 

 

Products:

Undertow Suite
Undertow
Interactive_tool
Optimizing_tool
VeriPower
Power_tool
Toggle_tool
Express_VCT
Personal_VCT
VBIT«
verilog2vhdl™
vhdl2verilogmkt
Script Pool


 

 Company    Products     News    Assistance     Download     Contact Us    Sales

Veritools, Inc.
459 Hamilton Avenue, Suite 200, Palo Alto, California 94301
(650) 462 - 5590 Main    (650) 462 - 5593 Fax
inquiry@veritools.com

For Web site information please email Webmaster