-
-
Save rti/5115469 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'zlib' | |
require 'intel_hex_record' | |
def main | |
if ARGV.empty? | |
warn "usage: #{File.basename $0} records.hex [records.hex ..]" | |
end | |
ARGV.each do |file| | |
records = IO.readlines(file).map {|x| x.strip!; IntelHexRecord.parse(x) if x[0] == ?: } | |
data = [] | |
records.each do |r| | |
break if r.end? | |
data += r.data if r.data? | |
end | |
puts "0x#{Zlib::crc32(data.to_s).to_s(16)}" | |
end | |
end | |
main if $0 == __FILE__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:10006000000102030405060708090A0B0C0D0E0F18 | |
:10007000101112131415161718191A1B1C1D1E1F08 | |
:10008000202122000000000000000000000000000D | |
:100090000000000000000000000000000000000060 | |
:1000A0000000000000000000000000000000000050 | |
:1000B0000000000000000000000000000000000040 | |
:1000C0000000000000000000000000000000000030 | |
:1000D0000000000000000000000000000000002000 | |
:00000001FF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'intel_hex_record' | |
def calc_dest_filename(name) | |
out = "#{name}.txt" | |
i = 1 | |
while true | |
break out unless File.exist?(out) | |
out = "#{name}.#{i}.txt" | |
i += 1 | |
end | |
end | |
def main | |
if ARGV.empty? | |
warn "usage: #{File.basename $0} records.hex [records.hex ..]" | |
warn ' writes to records.txt' | |
end | |
ARGV.each do |file| | |
records = IO.readlines(file).map {|x| x.strip!; IntelHexRecord.parse(x) if x[0] == ?: } | |
open(calc_dest_filename(file), 'w') do |f| | |
records.each do |r| | |
break if r.end? | |
raise "unexpected record type #{r.type}: #{r.inspect}" unless r.data? | |
f.printf '%08x ', r.offset | |
for c in r.data | |
f.printf '%02x ', c | |
end | |
f.puts | |
end | |
end | |
end | |
end | |
main if $0 == __FILE__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IntelHexRecord | |
def self.parse(str) | |
Parser.new(str).parse | |
end | |
attr_reader :offset, :type, :data | |
include Enumerable | |
def initialize(offset, type, data) | |
@offset = offset | |
@type = type | |
@data = data | |
end | |
def data? | |
type == 0 | |
end | |
def end? | |
type == 1 | |
end | |
def extended_segment_address_record? | |
type == 2 | |
end | |
def size | |
data.size | |
end | |
def each(&blk) | |
data.each(&blk) | |
end | |
class Parser | |
def initialize(str) | |
@str = str | |
@i = 0 | |
@sum = 0 | |
end | |
def parse | |
char == ?: or raise 'expected colon' | |
size = byte | |
offset = word | |
type = byte | |
data = (1..size).map { byte } | |
checksum = byte | |
eof? or raise 'expected eof' | |
(@sum & 0xff) == 0 or raise "checksum doesn't match" | |
IntelHexRecord.new(offset, type, data) | |
end | |
def char | |
c = @str[@i] | |
@i += 1 | |
@sum += c.chr.hex | |
c | |
end | |
def byte | |
b = @str[@i..@i+1].hex | |
@i += 2 | |
@sum += b | |
b | |
end | |
def word | |
(byte << 8) | byte | |
end | |
def eof? | |
@i == @str.size | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment