; CRC-32 = x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 (used in Ethernet)

; ===========================================================================
create_ethframe proc far
; Creates ETHERNET Type 1 frame 
; ES:SI = Buffer
; CX    = Size
; ===========================================================================
       xor  ax, ax
       mov  framesize, ax

       push cs
       pop  ds
       lea  di, framebuffer
       mov  bp, di
       push di
       push cx
       mov  di, si
       mov  bl, 0
       call fillarray
       pop  cx
       pop  di

       cmp  cx, 46
       jge  smin_ok
       ; diff = 46 - blockl
       mov  bx, 46
       mov  ax, cx
       xor  dx, dx
       sub  bx, ax
       mov  cx, ax
smin_ok:
       cmp  cx, 1500
       jbe  make_ethframe
       mov  cx, 1500
make_ethframe:
       xor  bx, bx
set_dest_mac:
       mov  al, byte ptr cs:dst_addr[bx]
       mov  byte ptr es:[di], al
       inc  di
       inc  bx
       cmp  bx, 5
       jbe  set_dest_mac
       xor  bx, bx
set_src_mac:
       mov  al, byte ptr cs:mac_addr[bx]
       mov  byte ptr es:[di], al
       inc  di
       inc  bx
       cmp  bx, 5
       jbe  set_src_mac
set_etype:
       mov  ax, ethtype
       ; do I need to swap this ? mar-20-2006
       mov  word ptr es:[di], ax
       inc  di
       inc  di
; elen is for 802.3 w/ 802.2 header and data
;set_elen:
;       mov  word ptr es:[di], cx
;       inc  di
;       inc  di

       push cx
eth_loadpayload:
       mov  al, byte ptr es:[si]
       mov  byte ptr ds:[di], al
       inc  di
       inc  si
       loop eth_loadpayload
       mov  di, bp
       pop  cx
       push cx
       mov  ax, 14
       add  ax, cx
       mov  cx, ax
       xor  bx, bx
eth_crcloop:
       mov  al, byte ptr ds:[di]
       push bx
       call ethercrc32
       pop  bx
       mov  bx, ax
       loop eth_crcloop
       pop  cx
       mov  ax, cx
       mov  bx, 18
       add  ax, bx
       mov  cx, ax
       mov  framesize, cx
       ret
create_ethframe endp

; receiver process algoritm
; if carrier do
; receive byte
; if not start of frame do wait for start of frame lock (loop to receive byte)
; receive block (read loop)
; check crc and size
;       len => 64   and <= 1518 and CRC passes (len-4)
; tally runts and giants for diagnostic use
; if error increment error count, and discard else
; match destination mac to our mac address or bcast = true forword
; (inform higher levels)

mac_addr    db 6 dup(0)
dst_addr    db 6 dup(0)
eth_bcast   db 6 dup(0ffh)
def_addr    db 0ceh,27h,5bh,03,20,06   ; Chris's Packets!
ethtype     dw 0800h
IP_ETH      EQU 0800h
framesize   dw 1500
framebuffer db 1500 dup(0)
