Tatantyler

Cyclic Redundancy Checks - DCPU-16 (w/ test program)

Jan 23rd, 2013
556
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; CRC Implementation
  2. ; By KillaVanilla
  3. ; Yes, this is different from the one in UnnamedAPI.
  4.  
  5. ; Test program
  6. SET X, 0x8408
  7. SET Y, data
  8. SET I, 79
  9. JSR CRC_Calculate
  10. DAT 0
  11.  
  12. ; **********************************************************************************************
  13. ; CRC_Calculate - Calculate a 16-bit CRC based on a given polynomial and block of data.
  14. ; This function caluclates a CRC checksum.
  15. ; Arguments:
  16. ;   X - The generator polynomial. (Least significant bit first)
  17. ;   Y - A pointer to the block of data to run the CRC on.
  18. ;   I - The length of the data to be processed (in words)
  19. ; Returns:
  20. ;   Z - The checksum
  21.  
  22. :CRC_Calculate
  23.     SET PUSH, I
  24.     SET PUSH, J
  25.     SET PUSH, X
  26.     SET PUSH, Y
  27.     SET PUSH, A
  28.     SET PUSH, B
  29.     SET PUSH, C
  30.    
  31.     SET B, 0x0001 ; holds the bit we're going to shift in next. Should be between 0x1 and 0x8. (0001 - 1000)
  32.     SET Z, [Y] ; Initalize the remainder register
  33.     SET J, Y
  34.     ADD J, I ; We work backwards on this one
  35.     SET Z, [J] ; Hold the remainder here
  36.    
  37.     :CRC_Calculate_loop
  38.         JSR CRC_Calculate_loop_perform_calc
  39.         SHR Z, 1 ; Shift the register to the right
  40.         SET C, [J] ; Shift in the next bit
  41.         AND C, B
  42.         IFG C, 0
  43.             JSR CRC_Calculate_loop_shift
  44.         XOR Z, C
  45.         SHL B, 1
  46.         IFN B, 0
  47.             SET PC, CRC_Calculate_loop
  48.         SET B, 0x0001
  49.         SUB J, 1
  50.         IFL J, Y
  51.             SET PC, CRC_Calculate_loop_end
  52.         SET PC, CRC_Calculate_loop
  53.         :CRC_Calculate_loop_shift ; Shift C until it equals 1
  54.             IFE C, 1
  55.                 SET PC, POP
  56.             SHR C, 1
  57.             SET PC, CRC_Calculate_loop_shift
  58.     :CRC_Calculate_loop_perform_calc
  59.         SET A, Z
  60.         AND A, 0x0001
  61.         IFG A, 0
  62.             XOR Z, X
  63.         SET PC, POP
  64.     :CRC_Calculate_loop_end
  65.     SET C, POP
  66.     SET B, POP
  67.     SET A, POP
  68.     SET Y, POP
  69.     SET X, POP
  70.     SET J, POP
  71.     SET I, POP
  72.     SET PC, POP
  73.  
  74. ; Test Data
  75. :data
  76. ;DAT "Test tseT Test tseT Test tseT Test tseT Test tseT Test tseT Test tseT Test tseT" ; CRC is 0x12b6 (with CRC-16-CCITT)
  77. DAT "Test tseT Test tseT Test tseT tseT tseT Test tseT Test tseT Test tseT Test tseT" ; CRC is 0x749b (with CRC-16-CCITT)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×