whitequark / libfx2

Chip support package for Cypress EZ-USB FX2 series microcontrollers
BSD Zero Clause License
73 stars 26 forks source link

Support IHEX record type 0x04 in `input_data`. #1

Closed cr1901 closed 6 years ago

cr1901 commented 6 years ago

Verified this works fine on Microchip PIC IHEX files previously.

I did not include an equivalent function for emitting record 0x04 because I didn't need it/have time to add it, and I'm not certain in how emitting 0x04 should work.

*output_data function only works with contiguous byte arrays, whereas IHEX would be perfectly fine w/ gaps. The easiest way is to simply delineate 64kb boundaries and add a 0x04 record every time a 64kb boundary is crossed, but this is wasteful. I'd been using the following conversion function in my own scripts to test IHEX gaps. Feel free to adjust to your needs:

# Assumes: Every range of data provided has some discontiguous bytes between
# the regions; input_data should've taken care of this.
def offset_dat_to_list(dat, offset, trim, fill):
    out = []
    raw_prefix = [fill for i in range(offset)] if not trim else []

    prev_o = 0
    prev_l = 0
    first = True
    rec_prefix = []
    rec_buf = []

    for o, b in sorted(dat, key=lambda d: d[0]):
        if first:
            # First record will trivially pass assert, and we don't want
            # fill bytes- that's offset/trim's job.
            first = False
        else:
            start_fill = prev_o + prev_l
            assert o > start_fill, "Contiguous region was marked as discontiguous."
            rec_prefix = [fill for i in range(o - start_fill)]

        if
            rec_buf = rec_prefix + b
            out += rec_buf

        prev_o = o
        prev_l = len(b)

    return bytes(raw_prefix + out)