;***************************************************************************************************
;
; DWORD MakeCRC16Helper_asm(const void *pBuffer, DWORD crc, DWORD len);
; DWORD MakeCRC32Helper_asm(const void *pBuffer, DWORD crc, DWORD len);
;
;***************************************************************************************************

.586
.model flat, c


;***************************************************************************************************
; CRCテーブルはMakeCRC.cppで定義してあるのを参照

.data
extern c crc16table:dword
extern c crc32table:dword


;***************************************************************************************************
; MACRO

CRC_ROUND       MACRO   num, crctable
    mov     ebx, [edi + num]

    mov     edx, ebx
    xor     edx, eax
    and     edx, 0ffh
    mov     esi, [edx*4+crctable]
    shr     eax, 8
    xor     eax, esi

    shr     ebx, 8
    mov     edx, ebx
    xor     edx, eax
    and     edx, 0ffh
    mov     esi, [edx*4+crctable]
    shr     eax, 8
    xor     eax, esi

    shr     ebx, 8
    mov     edx, ebx
    xor     edx, eax
    and     edx, 0ffh
    mov     esi, [edx*4+crctable]
    shr     eax, 8
    xor     eax, esi

    shr     ebx, 8
    mov     edx, ebx
    xor     edx, eax
    and     edx, 0ffh
    mov     esi, [edx*4+crctable]
    shr     eax, 8
    xor     eax, esi
ENDM


CRC16_ROUND       MACRO   num
    CRC_ROUND   num+ 0, crc16table
    CRC_ROUND   num+ 4, crc16table
    CRC_ROUND   num+ 8, crc16table
    CRC_ROUND   num+12, crc16table
ENDM


CRC32_ROUND       MACRO   num
    CRC_ROUND   num+ 0, crc32table
    CRC_ROUND   num+ 4, crc32table
    CRC_ROUND   num+ 8, crc32table
    CRC_ROUND   num+12, crc32table
ENDM



;***************************************************************************************************
; CODE

.code

MakeCRC16Helper_asm     proc   uses ebx ecx esi edi,   pBuffer:dword, crc:dword, len:dword

    mov     ecx, len
    mov     eax, crc
    mov     edi, pBuffer
    shr     ecx, 6
    jnz     MAINLOOP
    ret

  MAINLOOP:
    CRC16_ROUND( 0)
    CRC16_ROUND(16)
    CRC16_ROUND(32)
    CRC16_ROUND(48)

    add     edi, 64
    sub     ecx, 1
    jnz     MAINLOOP
    ret

MakeCRC16Helper_asm      endp


MakeCRC32Helper_asm     proc   uses ebx ecx esi edi,   pBuffer:dword, crc:dword, len:dword

    mov     ecx, len
    mov     eax, crc
    mov     edi, pBuffer
    shr     ecx, 6
    jnz     MAINLOOP
    ret

  MAINLOOP:
    CRC32_ROUND( 0)
    CRC32_ROUND(16)
    CRC32_ROUND(32)
    CRC32_ROUND(48)

    add     edi, 64
    sub     ecx, 1
    jnz     MAINLOOP
    ret

MakeCRC32Helper_asm      endp

end
