Skip to content
Snippets Groups Projects
Commit 77e73f29 authored by Bernard Blackham's avatar Bernard Blackham
Browse files

Use a precomputed CRC table for xmodem

parent db71d970
No related merge requests found
...@@ -41,7 +41,7 @@ rom2.elf: $(OBJS) memory.x ...@@ -41,7 +41,7 @@ rom2.elf: $(OBJS) memory.x
$(SIZE) $@ $(SIZE) $@
clean: clean:
rm -f *.o *.elf *.s19 *.b *.a rom.tar.bz2 romsrc.c rm -f *.o *.elf *.s19 *.b *.a rom.tar.bz2 romsrc.c crctab.h
# #
# Some useful rules # Some useful rules
...@@ -53,12 +53,20 @@ size: rom2.s19 ...@@ -53,12 +53,20 @@ size: rom2.s19
$(SIZE) $< $(SIZE) $<
rom.tar.bz2: rom.tar.bz2:
rm -f romsrc.c rm -f romsrc.c crctab.h
tar cjf rom.tar.bz2 README Makefile *.c *.h *.s *.x tar cjf rom.tar.bz2 README Makefile *.c *.h *.s *.x
romsrc.c: rom.tar.bz2 romsrc.c: rom.tar.bz2
perl -w src2c.pl < $< > $@ perl -w src2c.pl < $< > $@
xmodem.c: crctab.h
gencrctab: gencrctab.c
gcc -o $@ $<
crctab.h: gencrctab
./gencrctab > $@
# #
# Implicit rules # Implicit rules
# #
......
#include <stdio.h>
#define CRC16 0x1021 /* Generator polynomial (X^16 + X^12 + X^5 + 1) */
int main() {
int val;
printf("const u16 crctab[] = {\n\t");
for (val = 0; val <= 255; val++) {
int i;
unsigned int crc;
crc = val << 8;
for (i = 0; i < 8; i++) {
crc <<= 1;
if (crc & 0x10000)
crc ^= CRC16;
}
printf("0x%04x,", crc&0xffff);
if ((val+1)%8 == 0) printf("\n\t");
}
printf("};\n");
return 0;
}
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "chime.h" #include "chime.h"
#include "sci.h" #include "sci.h"
#include "xmodem.h" #include "xmodem.h"
#include "crctab.h"
/* These definitions are for xmodem protocol. */ /* These definitions are for xmodem protocol. */
...@@ -51,28 +52,6 @@ readchar (int timeout) ...@@ -51,28 +52,6 @@ readchar (int timeout)
return 0; return 0;
} }
#define CRC16 0x1021 /* Generator polynomial (X^16 + X^12 + X^5 + 1) */
/* Call this to init the fast CRC-16 calculation table. */
static short crctab(u8 val) {
int i;
unsigned int crc;
crc = val << 8;
for (i = 0; i < 8; ++i)
{
crc <<= 1;
if (crc & 0x10000)
crc ^= CRC16;
}
return crc;
}
/* Calculate a CRC-16 for the LEN byte message pointed at by P. */ /* Calculate a CRC-16 for the LEN byte message pointed at by P. */
/* Pads with ^Z if necessary */ /* Pads with ^Z if necessary */
...@@ -83,11 +62,11 @@ docrc (unsigned char *p, int len) ...@@ -83,11 +62,11 @@ docrc (unsigned char *p, int len)
unsigned short crc = 0; unsigned short crc = 0;
while (len-- > 0) while (len-- > 0)
crc = (crc << 8) ^ crctab((crc >> 8) ^ *p++); crc = (crc << 8) ^ crctab[(crc >> 8) ^ *p++];
if (len2 < 128) { if (len2 < 128) {
len = 128-len; len = 128-len;
while (len-- > 0) while (len-- > 0)
crc = (crc << 8) ^ crctab((crc >> 8) ^ 0x1a); crc = (crc << 8) ^ crctab[(crc >> 8) ^ 0x1a];
} }
return crc; return crc;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment