/*
 * Copyright (c) 2001, Adam Dunkels.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Adam Dunkels.
 * 4. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This file is part of the uIP TCP/IP stack.
 *
 * $Id: arch.c,v 1.1 2001/11/20 20:49:45 adam Exp $
 *
 */


#include "arch.h"
#include "uip.h"

/* This is assembler versions of the uIP support functions for the
   Z80 CPU. Note that the checksum function ("_chksum()", defined
   under uip_ipchksum()) does not support buffer sizes > 255 bytes. */

/*-----------------------------------------------------------------------------------*/
static u16_t chksum_ptr, chksum_len, chksum_tmp;
static u16_t chksum();
/*-----------------------------------------------------------------------------------*/
u16_t
uip_ipchksum()
{
  chksum_tmp = 0;
  chksum_ptr = (u16_t)&uip_buf[UIP_LLH_LEN];
  chksum_len = 20;
  return chksum();
}
/*asm("_chksum:");*/
u16_t chksum() {
#asm
        PUSH    IY
        LD      HL,(CHKSUM_TMP)
        LD      IY,(CHKSUM_PTR)
#endasm
#if UIP_BUFSIZE > 255
#asm
        LD      BC,(CHKSUM_LEN)
        SRL     B
        RR      C
        INC     BC
#endasm
#else
#asm
        LD      A,(CHKSUM_LEN)
        SRL     A
        LD      B,A
        INC     B
#endasm
#endif /* UIP_BUFSIZE > 255 */
#asm
        OR      A
        JR      $2
$1: ;chksum_loop1
        LD      E,(IY)
        INC     IY
        LD      D,(IY)
        INC     IY
        POP     AF
        ADC     HL,DE
$2:
        PUSH    AF
#endasm
#if UIP_BUFSIZE > 255
#asm
        DEC     BC
        LD      A,B
        OR      C
        JR      NZ,$1 ;chksum_loop1
#endasm
#else
#asm
        DJNZ    $1 ;chksum_loop1
#endasm
#endif /* UIP_BUFSIZE > 255 */
#asm
        LD      A,(CHKSUM_LEN)
        SRL     A
        JR      NC,$3 ;chksum_noodd
        LD      E,(IY)
        LD      D,0
        POP     AF
        ADC     HL,DE
        PUSH    AF
$3: ;chksum_noodd
        LD      DE,0
        POP     AF
$4: ;chksum_endloop:
        ADC     HL,DE
        JR      C,$4 ;chksum_endloop
        POP     IY
#endasm
}
/*-----------------------------------------------------------------------------------*/
u16_t
uip_tcpchksum()
{
#if 1
  chksum_tmp = 0x0600-0x1401; /* IP_PROTO_TCP */
#else
  chksum_tmp = (u16_t)uip_buf[9 + UIP_LLH_LEN] << 8;
  chksum_tmp += ~0x1400; /* subtract ip header len */
  if (chksum_tmp < ~0x1400) chksum_tmp++;
#endif
  chksum_ptr = (u16_t)&uip_buf[20 + UIP_LLH_LEN];
  chksum_len = 20;
  chksum_tmp = chksum();

  chksum_ptr = (u16_t) uip_appdata;
  chksum_len = ((u16_t)uip_buf[2 + UIP_LLH_LEN]<<8) + ((u16_t)uip_buf[3 + UIP_LLH_LEN]&0xff) - 40;
  chksum_tmp = chksum();

  chksum_ptr = (u16_t)&uip_buf[12 + UIP_LLH_LEN];
  chksum_len = 8;
  chksum_tmp = chksum();

  chksum_ptr = (u16_t)&uip_buf[2 + UIP_LLH_LEN];
  chksum_len = 2;
  return chksum();
}
/*-----------------------------------------------------------------------------------*/
#if 0
void uip_rcv_nxt(n)
u8_t n;
{
  uip_conn->rcv_nxt[3] += n;
  if(uip_conn->rcv_nxt[3] < n) {
    uip_conn->rcv_nxt[2]++;
    if(uip_conn->rcv_nxt[2] == 0) {
      uip_conn->rcv_nxt[1]++;
      if(uip_conn->rcv_nxt[1] == 0) {
	uip_conn->rcv_nxt[0]++;
      }
    }
  }
}
#else
void uip_rcv_nxt(n)
#if UIP_BUFSIZE > 255
u16_t n;
#else
u8_t n;
#endif /* UIP_BUFSIZE > 255 */
  /*asm("_uip_rcv_nxt:");*/
{
#asm
        PUSH    IX
        LD      IX,2
        ADD     IX,SP
        LD      HL,(UIP_CONN)
        LD      DE,000CH
        ADD     HL,DE
        LD      A,(HL)
        ADD     A,(IX+2)
        LD      (HL),A
#endasm
#if UIP_BUFSIZE > 255
#asm
        DEC     HL
        LD      A,(HL)
        ADC     A,(IX+3)
        LD      (HL),A
#endasm
#else
#asm
        JR      NC,$5
        DEC     HL
        INC     (HL)
#endasm
#endif /* UIP_BUFSIZE > 255 */
#asm
        JR      NZ,$5
        DEC     HL
        INC     (HL)
        JR      NZ,$5
        DEC     HL
        INC     (HL)
$5:
        POP     IX
#endasm
}
#endif
/*-----------------------------------------------------------------------------------*/
#if 0
void uip_ack_nxt(n)
u8_t n;
{
  uip_conn->ack_nxt[3] += n;
  if(uip_conn->ack_nxt[3] < n) {
    uip_conn->ack_nxt[2]++;
    if(uip_conn->ack_nxt[2] == 0) {
      uip_conn->ack_nxt[1]++;
      if(uip_conn->ack_nxt[1] == 0) {
	uip_conn->ack_nxt[0]++;
      }
    }
  }
}
#else
void uip_ack_nxt(n)
#if UIP_BUFSIZE > 255
u16_t n;
#else
u8_t n;
#endif /* UIP_BUFSIZE > 255 */
  /*asm("_uip_ack_nxt:");*/
{
#asm
        PUSH    IX
        LD      IX,2
        ADD     IX,SP
        LD      HL,(UIP_CONN)
        LD      DE,0014H
        ADD     HL,DE
        LD      A,(HL)
        ADD     A,(IX+2)
        LD      (HL),A
#endasm
#if UIP_BUFSIZE > 255
#asm
        DEC     HL
        LD      A,(HL)
        ADC     A,(IX+3)
        LD      (HL),A
#endasm
#else
#asm
        JR      NC,$6
        DEC     HL
        INC     (HL)
#endasm
#endif /* UIP_BUFSIZE > 255 */
#asm
        JR      NZ,$6
        DEC     HL
        INC     (HL)
        JR      NZ,$6
        DEC     HL
        INC     (HL)
$6:
        POP    IX
#endasm
}
#endif
/*-----------------------------------------------------------------------------------*/
