Hello All,
I have this asm crc calculation code:
ADDCHECK PUSH ACC ;for 8031 ADD A,CHEKSUM RLC A JC ADCK3 XRL A,#11011101b ;(#0DDh) ADCK3 MOV CHEKSUM,A CLR C ;if CHK >= 244 then CHK= CHK-244 SUBB A,#224 ;(#0E0h) JC ADCK4 MOV CHECKSUM,A ;<CHK> always < 224 !!! ADCK4 POP ACC RET ;============================================================== ADDCHECK: PUSH AX ;for 8086 ADD AL,CHECKSUM RCL AL,1 JC ADCK3 XOR AL,11011101b ADCK3: MOV CHECKSUM,AL SUB AL,224 JC ADCK4 MOV CHECKSUM,AL ADCK4: POP AX RET
I want to adapt for my current MCU C8051F340, as included asm code or "C" source code. (I'm "C" proggramer). Can you help me?
Thank you for Attention!
>>I think it work correct.
Perhaps you have some example data with a sum so you can actually validate it?
Testing it against the x86 code suggests it is good
unsigned char GetCrc2(unsigned char *buffer, size_t bufsize) // sourcer32@gmail.com { unsigned short checksum = 0; while(bufsize--) { checksum += *buffer++; checksum <<= 1; if ((checksum & 0x200) != 0) // ADD carry checksum |= 1; if ((checksum & 0x100) == 0) // RCL carry checksum ^= 0xDD; checksum &= 0xFF; // Modulo 256 if (checksum >= 0xE0) // Modulo 224 checksum -= 0xE0; } return((unsigned char)checksum); } int main(int argc, char **argv) { unsigned char test1[] = { 0x01,0x02,0x03,0x04,0x05,0x06 }; // 0x4A unsigned char test2[] = { 0x11,0x22,0x33,0x44,0x55,0x66 }; // 0x23 unsigned char test3[] = { 0x66,0x55,0x44,0x33,0x22,0x11 }; // 0x59 printf("%02X\n", test(test1, sizeof(test1))); // Assembler printf("%02X\n", test(test2, sizeof(test2))); printf("%02X\n", test(test3, sizeof(test3))); putchar('\n'); printf("%02X\n", GetCrc(test1, sizeof(test1))); // Yours printf("%02X\n", GetCrc(test2, sizeof(test2))); printf("%02X\n", GetCrc(test3, sizeof(test3))); putchar('\n'); printf("%02X\n", GetCrc2(test1, sizeof(test1))); // Mine printf("%02X\n", GetCrc2(test2, sizeof(test2))); printf("%02X\n", GetCrc2(test3, sizeof(test3))); putchar('\n'); return(1); }