NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF BIN/ARP
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/ETH.I
				.INB INC/LIBTCPIP.I
*--------------------------------------
TIMEOUT.MAX		.EQ 40					40*100ms = 4 sec.
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPTR1			.BS 2
EntryCount		.BS 1
ZS.END
				.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data Segment Size
				.DA #32					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT		
				.DA	CS.QUIT
L.LIBTCPIP		.DA LIBTCPIP
L.SSCANF.IP		.DA SSCANF.IP
L.SSCANF.MAC	.DA SSCANF.MAC
L.MSG.Usage		.DA MSG.Usage
L.MSG0			.DA MSG0
L.MSG1			.DA MSG1
L.MSG2			.DA MSG2
				.DA 0
*--------------------------------------
CS.INIT			>LDYA L.LIBTCPIP
				>SYSCALL LoadLib
				bcs .9
				sta hLIBTCPIP
.9				rts
*--------------------------------------
CS.RUN			>LIBCALL hLIBTCPIP,LIBTCPIP.GETCFG
				>STYA ZPPTR1

				lda (ZPPTR1)
				bmi .1

				jmp CS.RUN.ERR
.1				ldy #S.PS.ARGC
				lda (pPs),y
				cmp #1
				bcs .2
				jmp CS.RUN.DUMP
			
.2				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>PUSHW L.SSCANF.IP
				>PUSHEA.G DST.IP
				>PUSHEA.G DST.IP+1
				>PUSHEA.G DST.IP+2
				>PUSHEA.G DST.IP+3
				>PUSHBI 8				4 Ptrs
				>SYSCALL sscanf
				bcs CS.RUN.ERR

				ldy #S.PS.ARGC
				lda (pPs),y
				cmp #1
				beq CS.RUN.QUERY
				cmp #3
				bne CS.RUN.ERR
				jmp CS.RUN.ADD
				
CS.RUN.ERR		>PUSHW L.MSG.Usage
				>PUSHBI 0
				>SYSCALL printf
				lda #E.SYN
				sec
				rts
*--------------------------------------
CS.RUN.QUERY	jsr Init.Timeout

.1				>PUSHEA.G DST.MAC
				>PUSHEA.G DST.IP
				>LIBCALL hLIBTCPIP,LIBTCPIP.ARP.QUERY
				bcc .2					success, print & exit
				jsr Wait.TimeOut
				bcs .8
				>SLEEP
				>SYSCALL GetChar
				bcs .1
				cmp #3
				beq .9
				bra .1
				
.2				>PUSHW L.MSG2

				ldx #4
				ldy #DST.IP

.4				>PUSHB (pData),y
				iny
				dex
				bne .4

				ldx #6
				ldy #DST.MAC

.3				>PUSHB (pData),y
				iny
				dex
				bne .3

				>PUSHBI 10
				>SYSCALL printf
.8				lda #0
.9				sec
				rts
*--------------------------------------
CS.RUN.ADD		lda #2
				>SYSCALL ArgV
				>PUSHYA
				>PUSHW L.SSCANF.MAC
				>PUSHEA.G DST.MAC
				>PUSHEA.G DST.MAC+1
				>PUSHEA.G DST.MAC+2
				>PUSHEA.G DST.MAC+3
				>PUSHEA.G DST.MAC+4
				>PUSHEA.G DST.MAC+5
				>PUSHBI 12				6 Ptrs
				>SYSCALL sscanf
				bcs .9

				>PUSHEA.G DST.MAC
				>PUSHEA.G DST.IP
				>LIBCALL hLIBTCPIP,LIBTCPIP.ARP.ADD
				clc
.8				rts
.9				jmp CS.RUN.ERR
*--------------------------------------
CS.RUN.DUMP		>LIBCALL hLIBTCPIP,LIBTCPIP.ARP.GETCACHE
				>STYA ZPPTR1
				>LDYA L.MSG0
				>SYSCALL puts

				lda #K.ARPCACHE.SIZE
				sta EntryCount

.1				lda (ZPPTR1)
				beq .4

				>PUSHW L.MSG1
				
				>PUSHB (ZPPTR1)
				
				ldy #S.ARPCACHE.TTL+1
				>PUSHB (ZPPTR1),y
				dey
				>PUSHB (ZPPTR1),y

				ldy #S.ARPCACHE.MAC

.3				>PUSHB (ZPPTR1),y
				iny
				cpy #S.ARPCACHE.MAC+6
				bne .3

				ldy #S.ARPCACHE.IP

.2				>PUSHB (ZPPTR1),y
				iny
				cpy #S.ARPCACHE.IP+4
				bne .2
				
				>PUSHBI 13
				>SYSCALL printf

.4				lda ZPPTR1
				clc
				adc #S.ARPCACHE
				sta ZPPTR1
				bcc .5

				inc ZPPTR1+1

.5				dec EntryCount
				bne .1

.8				lda #0					tell TSKMGR that all done ok, but 
				sec						we do not want to stay in memory
				rts
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9					is it a TIMER event?

				ldy #TimeOut
				lda (pData),y
				beq .9
				dec 
				sta (pData),y
.9				sec						do not discard TIMER event
				rts
*--------------------------------------
CS.QUIT			lda hLIBTCPIP
				beq .8

				>SYSCALL UnloadLib
.8				clc
				rts
*--------------------------------------
Init.Timeout	ldy #TimeOut
				lda #TIMEOUT.MAX
				sta (pData),y
				rts
*--------------------------------------
Wait.TimeOut	sec
				ldy #TimeOut
				lda (pData),y
				beq .9
				clc
.9				rts
*--------------------------------------
CS.END
LIBTCPIP		.AZ "libtcpip"
SSCANF.IP		.AZ "%d.%d.%d.%d"
SSCANF.MAC		.AZ "%h:%h:%h:%h:%h:%h"
MSG.Usage		.AS "Usage : ARP, display ARP cache\r\n"
				.AS "        ARP ip.ip.ip.ip query cache or network\r\n"
				.AZ "        ARP ip.ip.ip.ip HH:HH:HH:HH:HH:HH add a static ARP record in Cache\r\n"
MSG0			.AZ "STS TTL   MAC Address       IP Address"
MSG1			.AZ "$%h %5D %h:%h:%h:%h:%h:%h %d.%d.%d.%d\r\n"
MSG2			.AZ "%d.%d.%d.%d is at %h:%h:%h:%h:%h:%h\r\n"
hLIBTCPIP		.BS 1
*--------------------------------------
DS.START
DST.IP			.BS 4
DST.MAC			.BS 6
TimeOut			.BS 1
DS.END
*--------------------------------------
MAN
SAVE USR/SRC/BIN/ARP.S
ASM
