💾 Archived View for librehacker.com › gemlog › tech › 20221011-0.gmi captured on 2024-07-09 at 00:37:00. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-05-10)

-=-=-=-=-=-=-

Forth Progress: ARRAY and CRC32

I've been a bit distracted lately with my astronomy studies, but I have still been working with Mecrisp Stellaris Forth here and there. I asked in the #mecrisp IRC channel if anybody had a software implementation of the CRC32 checksum algorithm available for Mecrisp Stellaris. They told me no, but that it would be a great idea for me to make one and contribute it. So, I did.

The main appeal of CRC32 is that it is very simple to implement, especially if you store the polynomial lookup table in memory. Here are links to my code:

https://codeberg.org/infrared/mf-rp2040/src/commit/82ff68e023d84e563fa33c4b54ec329595fd492e/ARRAY.FS

https://codeberg.org/infrared/mf-rp2040/src/commit/82ff68e023d84e563fa33c4b54ec329595fd492e/CRC32.FS

https://codeberg.org/infrared/mf-rp2040/src/commit/82ff68e023d84e563fa33c4b54ec329595fd492e/CRC32_DEMO.FS

ARRAY.FS is just a few helper words to make storing arrays of data look nicer. CRC32.FS is just the polynomial lookup table (256 32-bit values) and these two words:

\ ******************************************************************
\ PARAMETERS:
\   W1: CURRENT CHECKSUM (32 BITS)
\   C: NEXT BYTE OF INPUT DATA
\ OUTPUT:
\   W2: NEXT CHECKSUM
\ ******************************************************************
: CRC32_STEP ( W1 C -- W2 )
    OVER $FF AND XOR CELLS LOOKUP + @ SWAP 8 RSHIFT XOR ;

\ ******************************************************************
\ PARAMETERS:
\   U: NUMBER OF BYTES
\   A: START ADDRESS OF DATA
\ OUTPUT:
\   W: CHECKSUM
\ ******************************************************************
: CRC32 ( U A -- W )
    $FFFFFFFF -ROT DUP ROT + SWAP
    DO I C@ CRC32_STEP LOOP $FFFFFFFF XOR ;

The first word is a helper word. CRC32 is the loop which takes a byte count and a starting memory address, and then outputs the checksum for the data in that memory area.

As a word of caution, the code just uses C@ to fetch each byte, so I'm not sure if that works for all use cases. More specifically, I don't know if all Stellaris targets support reading firmware with C@.