ali1234 / vhs-teletext

Software to recover teletext data from VHS recordings.
GNU General Public License v3.0
179 stars 21 forks source link

Implement checksum generation/verification #38

Closed ali1234 closed 4 years ago

ali1234 commented 4 years ago

We can now decode the checksums from packet X/27. We need to be able to verify it and generate for new pages.

Javascript implementation of the CRC from @ZXGuesser:

    function calculatePageCRC(){
        /* calculate the CRC checksum of a page per ETS 300 706 9.6.1 */

        var checksum = 0;

        for (var c=8; c<32; c++){
            checksum = calculateCRCByte(setParity(renderer.level1PageArray[0][c]), checksum);
        }

        for (var r=1; r<25; r++){
            for (var c=0; c<40; c++){
                checksum = calculateCRCByte(setParity(renderer.level1PageArray[r][c]), checksum);
            }
        }

        // TODO: use a real packet 25
        for (var c=0; c<40; c++){
            checksum = calculateCRCByte(setParity(0x20), checksum);
        }

        return checksum;
    }

    function calculateCRCByte(pageByte, checksum){
        for (var b=7; b>-1; b--){
            var outBit = ((pageByte>>b)&1) ^ ((checksum>>6)&1) ^ ((checksum>>8)&1) ^ ((checksum>>11)&1) ^ ((checksum>>15)&1);
            checksum = outBit | ((checksum&0x7FFF)<<1);
        }

        return checksum;
    }

Note that there is a bug in the teletext spec regarding checksum calculation. It says:

During a sequence of 8 192 clockpulses bytes 14 to 37 from packet X/0 and the following character bytes (bytes 14 to 45) of packets X/1 upto X/25, in ascending address order, form the input.

"(bytes 14 to 45)" appears to be incorrect - it does not cover the displayed bytes of rows X/1-25. The javascript code above appears to be a correct implementation as far as we know.