Class: GdbFlasher::IHex

Inherits:
Object
  • Object
show all
Defined in:
lib/gdbflasher/ihex.rb

Defined Under Namespace

Classes: Record, Segment

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIHex

Returns a new instance of IHex.



113
114
115
# File 'lib/gdbflasher/ihex.rb', line 113

def initialize
  @segments = []
end

Instance Attribute Details

#segmentsObject

Returns the value of attribute segments.



111
112
113
# File 'lib/gdbflasher/ihex.rb', line 111

def segments
  @segments
end

Class Method Details

.load(stream) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gdbflasher/ihex.rb', line 117

def self.load(stream)
  high_address = 0
  lines = {}

  stream.each_line do |line|
    line.rstrip!

    record = Record.parse line

    case record.type
    when :eof
      break

    when :extended_address
      high_address, = record.data.unpack "n"

    when :data
      lines[(high_address << 16) | record.address] = record.data
    end
  end

  segment = nil
  ihex = self.new

  lines.sort_by { |k1,v1,k2,b2| k1 <=> k2 }.each do |address, data|
    if segment.nil? || segment.base + segment.size != address
      segment = Segment.new
      segment.base = address
      segment.data = data

      ihex.segments << segment
    else
      segment.data += data
    end
  end

  ihex
end

Instance Method Details

#pad_segments!(size, fill_byte = 0x00) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/gdbflasher/ihex.rb', line 181

def pad_segments!(size, fill_byte = 0x00)
  @segments.each do |segment|
    segment.pad_segment! size, fill_byte
  end

  self
end

#split_segments!(size, fill_byte = 0x00) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/gdbflasher/ihex.rb', line 156

def split_segments!(size, fill_byte = 0x00)
  pad_segments! size, fill_byte

  i = 0

  while i < @segments.length
    segment = @segments[i]
    parts = segment.size / size

    for j in 1...parts do
      new_segment = Segment.new
      new_segment.base = segment.base + size * j
      new_segment.data = segment.data[j * size...(j + 1) * size]

      @segments.insert i + j, new_segment
    end

    segment.data = segment.data[0...size]

    i += parts
  end

  self
end