#! /usr/bin/perl
# MIPS I system
# Copyright 2011-2012 Stefan Schuermans <stefan@blinkenarea.org>
# Copyleft GNU public license V2 or later
# http://www.gnu.org/copyleft/gpl.html
use strict;
use warnings;
require("crc32.pl");
# parse C data
my @bytes = ();
while (my $line = <>) {
chomp $line;
# tcpdump -XX
if ($line =~ /^ *[0-9A-FXa-fx]+: +([0-9A-Fa-f ]+)/) {
my $data = $1;
$data =~ s/ .*$//; # remove ASCII in case the begin was [0-9A-Fa-f]+
$data =~ s/[^0-9A-Fa-f]//g;
$data =~ s/(..)/$1,/g;
for my $part (split(/,/, $data)) {
if ($part =~ /^([0-9A-Fa-f]{2})/) {
my $val = $1;
push (@bytes, hex($val));
}
}
}
# C data
elsif ($line =~ /^ *([0-9A-FXa-fx, ]+)/) {
my $data = $1;
for my $part (split(/,/, $data)) {
if ($part =~ /^ *0[xX]([0-9A-Fa-f]{1,2})/) {
my $val = $1;
push (@bytes, hex($val));
}
}
}
}
# add CRC
if (@bytes >= 4) {
my @bytes_no_crc = @bytes;
my @crc_bytes = ();
unshift (@crc_bytes, pop(@bytes_no_crc));
unshift (@crc_bytes, pop(@bytes_no_crc));
unshift (@crc_bytes, pop(@bytes_no_crc));
unshift (@crc_bytes, pop(@bytes_no_crc));
my $crc_calc = crc32([@bytes_no_crc]);
my $mismatch = 0;