Skip to content

Commit

Permalink
tweak LinkPatch.js (script for Crossworks users) for improved executi…
Browse files Browse the repository at this point in the history
…on speed
  • Loading branch information
majbthrd committed Nov 29, 2019
1 parent cc14926 commit fcc10ae
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 22 deletions.
44 changes: 33 additions & 11 deletions example-apps/mouseplay/LinkPatch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
CrossStudio script to patch a SAM Dx1 ELF to work with this bootloader:
https://github.com/majbthrd/SAMDx1-USB-DFU-Bootloader
Copyright (C) 2018 Peter Lawrence
Copyright (C) 2018,2019 Peter Lawrence
The bootloader expects an application length and CRC32 to be stored
within the user application (using unused vector table entries). This
Expand Down Expand Up @@ -118,28 +118,50 @@ var high_lookup =
144, 209, 83, 18, 22, 87, 213, 148, 221, 156, 30, 95, 91, 26, 152, 217,
];

/*
ElfFile.peekBytes() is PAINFULLY slow when its second arg is large.
So, it behooves us to incrementally peekBytes at only small chunks.
*/
var chunk_size = 64;

function crc32_calc(crc, address, length)
{
peek = ElfFile.peekBytes(address, length, false, padding_value);

for (i = 0; i < length; i++)
while (length)
{
crc = crc32_table[(crc ^ peek[i]) & 0xff] ^ (crc >>> 8);
chunk = (length > chunk_size) ? chunk_size : length;

peek = ElfFile.peekBytes(address, chunk, false, padding_value);
length -= chunk;

for (i = 0; i < chunk; i++)
{
crc = crc32_table[(crc ^ peek[i]) & 0xff] ^ (crc >>> 8);
}
}

return crc >>> 0;
}

function reverse_crc32_calc(crc, address, length)
{
peek = ElfFile.peekBytes(address, length, false, padding_value);
address += length;

while (length--)
while (length)
{
high_byte = (crc >>> 24) & 0xFF;
crc ^= crc32_table[high_lookup[high_byte]];
crc <<= 8;
crc += high_lookup[high_byte] ^ peek[length];
chunk = (length > chunk_size) ? chunk_size : length;

address -= chunk;
peek = ElfFile.peekBytes(address, chunk, false, padding_value);
length -= chunk;

while (chunk--)
{
high_byte = (crc >>> 24) & 0xFF;
crc ^= crc32_table[high_lookup[high_byte]];
crc <<= 8;
crc += high_lookup[high_byte] ^ peek[chunk];
}

}

return crc >>> 0;
Expand Down
44 changes: 33 additions & 11 deletions example-apps/vcp/LinkPatch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
CrossStudio script to patch a SAM Dx1 ELF to work with this bootloader:
https://github.com/majbthrd/SAMDx1-USB-DFU-Bootloader
Copyright (C) 2018 Peter Lawrence
Copyright (C) 2018,2019 Peter Lawrence
The bootloader expects an application length and CRC32 to be stored
within the user application (using unused vector table entries). This
Expand Down Expand Up @@ -118,28 +118,50 @@ var high_lookup =
144, 209, 83, 18, 22, 87, 213, 148, 221, 156, 30, 95, 91, 26, 152, 217,
];

/*
ElfFile.peekBytes() is PAINFULLY slow when its second arg is large.
So, it behooves us to incrementally peekBytes at only small chunks.
*/
var chunk_size = 64;

function crc32_calc(crc, address, length)
{
peek = ElfFile.peekBytes(address, length, false, padding_value);

for (i = 0; i < length; i++)
while (length)
{
crc = crc32_table[(crc ^ peek[i]) & 0xff] ^ (crc >>> 8);
chunk = (length > chunk_size) ? chunk_size : length;

peek = ElfFile.peekBytes(address, chunk, false, padding_value);
length -= chunk;

for (i = 0; i < chunk; i++)
{
crc = crc32_table[(crc ^ peek[i]) & 0xff] ^ (crc >>> 8);
}
}

return crc >>> 0;
}

function reverse_crc32_calc(crc, address, length)
{
peek = ElfFile.peekBytes(address, length, false, padding_value);
address += length;

while (length--)
while (length)
{
high_byte = (crc >>> 24) & 0xFF;
crc ^= crc32_table[high_lookup[high_byte]];
crc <<= 8;
crc += high_lookup[high_byte] ^ peek[length];
chunk = (length > chunk_size) ? chunk_size : length;

address -= chunk;
peek = ElfFile.peekBytes(address, chunk, false, padding_value);
length -= chunk;

while (chunk--)
{
high_byte = (crc >>> 24) & 0xFF;
crc ^= crc32_table[high_lookup[high_byte]];
crc <<= 8;
crc += high_lookup[high_byte] ^ peek[chunk];
}

}

return crc >>> 0;
Expand Down

0 comments on commit fcc10ae

Please sign in to comment.