+;this is some very quick and dirty glue to make the most useful IP65 functions available via a single entry point.
+;this allows user applications to be developed that don't link ip65 in directly, rather they use an instance of ip65 that is preloaded (or in a cartridge/ROM)
+;this whole file could (and should) be greatly optimised by making it all table driven, but since this file is probably only going to be used in a bankswitched ROM where
+;space is not at such a premium, I'll go with the gross hack for now.
+
+;Modified by ShadowM for use with GEOS.
+;The include of commonprint.i is commented out, as well as the routines for
+;printing to the screen:
+;KPR_PRINT_ASCIIZ
+;KPR_PRINT_HEX
+;KPR_PRINT_DOTTED_QUAD
+;KPR_PRINT_IP_CONFIG
+;KPR_PRINT_INTEGER
+
+;Includes for input routines (filter_dns, get_filtered_input, filter_number)
+;have been commented out, along with the routines that call them. Same for
+;disk I/O, TFTP and HTTP.
+
+;GEOS IRQ vector (there are two, top and bottom)
+intBotVector = $849F
+
+.ifndef KPR_API_VERSION_NUMBER
+ .define EQU =
+ .include "../inc/kipper_constants.i"
+.endif
+.include "../inc/common.i"
+; .include "../inc/commonprint.i"
+.export kipper_dispatcher
+
+.import ip65_init
+.import dhcp_init
+.import cfg_get_configuration_ptr
+.import ip65_error
+.import dns_ip
+.import dns_resolve
+.import dns_set_hostname
+.import udp_callback
+.import udp_add_listener
+.import udp_remove_listener
+.import ip_inp
+.import udp_inp
+.import udp_send
+.import udp_send_src
+.import udp_send_src_port
+.import udp_send_dest
+.import udp_send_dest_port
+.import udp_send_len
+
+.import copymem
+.import cfg_mac
+.importzp copy_src
+.importzp copy_dest
+
+;imports added by ShadowM for GEOS version:
+.import arp_init
+.import eth_init
+.import icmp_add_listener
+.import icmp_callback
+.import icmp_echo_cnt
+.import icmp_remove_listener
+.import icmp_send_echo
+.import ip_init
+.import ip65_process
+.import timer_init
+.import timer_vbl_handler
+
+;reuse the copy_src zero page location
+kipper_params = copy_src
+buffer_ptr= copy_dest
+
+.code
+
+jmp kipper_dispatcher ;vector for GEOS
+
+.data
+
+old_bot_vector: ;hold area for GEOS intBotVector
+ .word 0
+irq_handler_installed_flag:
+ .byte 0
+ip_configured_flag:
+ .byte 0
+
+.code
+
+install_irq_handler:
+ ldax intBotVector
+ stax old_bot_vector
+ php
+ sei
+ ldax #timer_vbl_handler
+ stax intBotVector
+ lda #$ff
+ sta irq_handler_installed_flag
+ plp
+ rts
+
+kipper_dispatcher:
+ stax kipper_params
+
+
+; not supported under GEOS, must be more granular
+; cpy #KPR_INITIALIZE
+; bne :+
+; lda ip_configured_flag
+; bne ip_configured
+; jsr ip65_init
+; bcs init_failed
+; jsr dhcp_init
+; bcc dhcp_ok
+; jsr ip65_init ;if DHCP failed, then reinit the IP stack (which will reset IP address etc that DHCP messed with to cartridge default values)
+;dhcp_ok:
+; lda #1
+; sta ip_configured_flag
+; clc
+;init_failed:
+; rts
+;
+;ip_configured:
+; clc
+; rts
+;new GEOS init code:
+ cpy #KPR_INITIALIZE_CARD
+ bne :+
+ jsr eth_init
+ bcc @ok
+ lda #KPR_ERROR_DEVICE_FAILURE
+ sta ip65_error
+@ok:
+ rts
+:
+
+ cpy #KPR_INITIALIZE_STACK
+ bne :+
+ jsr arp_init
+ jsr ip_init
+ rts
+:
+
+ cpy #KPR_INSTALL_IRQ_HANDLER
+ bne :+
+ jsr timer_init
+ jsr install_irq_handler
+ rts
+:
+
+ cpy #KPR_INITIALIZE_DHCP
+ bne :+
+ jsr dhcp_init
+ rts ;call KPR_GET_LAST_ERROR if carry set on return
+:
+
+ cpy #KPR_POLL
+ bne :+
+ jsr ip65_process
+:
+
+ cpy #KPR_GET_IP_CONFIG
+ bne :+
+ ldax #cfg_mac
+ clc
+ rts
+:
+
+ cpy #KPR_DNS_RESOLVE
+ bne :+
+ phax
+ ldy #KPR_DNS_HOSTNAME+1
+ lda (kipper_params),y
+ tax
+ dey
+ lda (kipper_params),y
+ jsr dns_set_hostname
+ bcs @dns_error
+ jsr dns_resolve
+ bcs @dns_error
+
+ ldy #KPR_DNS_HOSTNAME_IP
+ plax
+ stax kipper_params
+ ldx #4
+@copy_dns_ip:
+ lda dns_ip,y
+ sta (kipper_params),y
+ iny
+ dex
+ bne @copy_dns_ip
+ rts
+@dns_error:
+ plax
+ rts
+
+:
+
+ cpy #KPR_UDP_ADD_LISTENER
+ bne :+
+ ldy #KPR_UDP_LISTENER_CALLBACK
+ lda (kipper_params),y
+ sta udp_callback
+ iny
+ lda (kipper_params),y
+ sta udp_callback+1
+ ldy #KPR_UDP_LISTENER_PORT+1
+ lda (kipper_params),y
+ tax
+ dey
+ lda (kipper_params),y
+
+ jmp udp_add_listener
+:
+
+ cpy #KPR_GET_INPUT_PACKET_INFO
+ bne :+
+ ldy #3
+@copy_src_ip:
+ lda ip_inp+12,y ;src IP
+ sta (kipper_params),y
+ dey
+ bpl @copy_src_ip
+
+ ldy #KPR_REMOTE_PORT
+ lda udp_inp+1 ;src port (lo byte)
+ sta (kipper_params),y
+ iny
+ lda udp_inp+0 ;src port (high byte)
+ sta (kipper_params),y
+ iny
+ lda udp_inp+3 ;dest port (lo byte)
+ sta (kipper_params),y
+ iny
+ lda udp_inp+2 ;dest port (high byte)
+ sta (kipper_params),y
+
+ iny
+ sec
+ lda udp_inp+5 ;payload length (lo byte)
+ sbc #8 ;to remove length of header
+ sta (kipper_params),y
+
+ iny
+ lda udp_inp+4 ;payload length (hi byte)
+ sbc #0 ;in case there was a carry from the lo byte
+ sta (kipper_params),y
+
+ iny
+ lda #<(udp_inp+8) ;payload ptr (lo byte)
+ sta (kipper_params),y
+
+ iny
+ lda #>(udp_inp+8) ;payload ptr (hi byte)
+ sta (kipper_params),y
+
+.import tcp_inbound_data_ptr
+.import tcp_inbound_data_length
+
+ lda ip_inp+9 ;proto number
+ cmp #6 ;TCP
+ bne @not_tcp
+ ldy #KPR_PAYLOAD_LENGTH
+ lda tcp_inbound_data_length
+ sta (kipper_params),y
+ iny
+ lda tcp_inbound_data_length+1
+ sta (kipper_params),y
+
+ ldy #KPR_PAYLOAD_POINTER
+ lda tcp_inbound_data_ptr
+ sta (kipper_params),y
+ iny
+ lda tcp_inbound_data_ptr+1
+ sta (kipper_params),y
+@not_tcp:
+
+ clc
+ rts
+:
+
+ cpy #KPR_SEND_UDP_PACKET
+ bne :+
+ ldy #3
+@copy_dest_ip:
+ lda (kipper_params),y
+ sta udp_send_dest,y
+ dey
+ bpl @copy_dest_ip
+
+ ldy #KPR_REMOTE_PORT
+ lda (kipper_params),y
+ sta udp_send_dest_port
+ iny
+ lda (kipper_params),y
+ sta udp_send_dest_port+1
+ iny
+
+ lda (kipper_params),y
+ sta udp_send_src_port
+ iny
+ lda (kipper_params),y
+ sta udp_send_src_port+1
+ iny
+
+
+ lda (kipper_params),y
+ sta udp_send_len
+ iny
+ lda (kipper_params),y
+ sta udp_send_len+1
+ iny
+
+ ;AX should point at data to send
+ lda (kipper_params),y
+ pha
+ iny
+ lda (kipper_params),y
+ tax
+ pla
+ jmp udp_send
+:
+
+ cpy #KPR_UDP_REMOVE_LISTENER
+ bne :+
+ jmp udp_remove_listener
+:
+
+
+ cpy #KPR_DEACTIVATE
+ ;nothing to do now we don't use IRQ
+ bne :+
+ clc
+ rts
+
+:
+ cpy #KPR_PRINT_ASCIIZ
+ bne :+
+; jsr print
+; clc
+; not supported under GEOS
+ sec
+ rts
+:
+
+ cpy #KPR_PRINT_HEX
+ bne :+
+; jsr print_hex
+; clc
+; not supported under GEOS
+ sec
+ rts
+:
+
+ cpy #KPR_PRINT_DOTTED_QUAD
+ bne :+
+; jsr print_dotted_quad
+; clc
+; not supported under GEOS
+ sec
+ rts
+:
+
+ cpy #KPR_PRINT_IP_CONFIG
+ bne :+
+; jsr print_ip_config
+; clc
+; not supported under GEOS
+ sec
+ rts
+:
+
+ cpy #KPR_PRINT_INTEGER
+ bne :+
+; jsr print_integer
+; clc
+; not supported under GEOS
+ sec
+ rts
+:
+
+
+ .segment "TCP_VARS"
+ port_number: .res 2
+ nonzero_octets: .res 1
+ .code
+
+ cpy #KPR_DOWNLOAD_RESOURCE
+ bne :+
+.import url_download
+.import url_download_buffer
+.import url_download_buffer_length
+
+
+ ldy #KPR_URL_DOWNLOAD_BUFFER
+ lda (kipper_params),y
+ sta url_download_buffer
+ iny
+ lda (kipper_params),y
+ sta url_download_buffer+1
+
+ ldy #KPR_URL_DOWNLOAD_BUFFER_LENGTH
+ lda (kipper_params),y
+ sta url_download_buffer_length
+ iny
+ lda (kipper_params),y
+ sta url_download_buffer_length+1
+
+ ldy #KPR_URL+1
+ lda (kipper_params),y
+ tax
+ dey
+ lda (kipper_params),y
+ jmp url_download
+:
+
+ cpy #KPR_FILE_LOAD
+bne :+
+;.import io_device_no
+;.import io_read_file
+;.import io_filename
+;.import io_filesize
+;.import io_load_address
+; phax
+; ldy #KPR_FILE_ACCESS_FILENAME
+; lda (kipper_params),y
+; sta io_filename
+; iny
+; lda (kipper_params),y
+; sta io_filename+1
+;
+; ldy #KPR_FILE_ACCESS_DEVICE
+; lda (kipper_params),y
+; sta io_device_no
+;
+; ldy #KPR_FILE_ACCESS_POINTER+1
+; lda (kipper_params),y
+; tax
+; dey
+; lda (kipper_params),y
+; jsr io_read_file
+; plax
+; bcc @read_file_ok
+; rts
+;
+;@read_file_ok:
+; stax kipper_params
+;
+; ldy #KPR_FILE_ACCESS_POINTER
+; lda io_load_address
+; sta (kipper_params),y
+; iny
+; lda io_load_address+1
+; sta (kipper_params),y
+;
+; ldy #KPR_FILE_ACCESS_FILESIZE
+; lda io_filesize
+; sta (kipper_params),y
+; iny
+; lda io_filesize+1
+; sta (kipper_params),y
+; not supported under GEOS
+ sec
+ rts
+:
+
+ cpy #KPR_PING_HOST
+ .import icmp_echo_ip
+ .import icmp_ping
+ bne :+
+ ldy #3
+@copy_ping_ip_loop:
+ lda (kipper_params),y
+ sta icmp_echo_ip,y
+ dey
+ bpl @copy_ping_ip_loop
+ jmp icmp_ping
+
+:
+ cpy #KPR_ICMP_ADD_LISTENER
+ bne :+
+ ldy #KPR_ICMP_LISTENER_CALLBACK
+ lda (kipper_params),y
+ sta icmp_callback
+ iny
+ lda (kipper_params),y
+ sta icmp_callback+1
+ ldy #KPR_ICMP_LISTENER_TYPE
+ lda (kipper_params),y
+ jmp icmp_add_listener
+:
+
+ cpy #KPR_ICMP_REMOVE_LISTENER
+ bne :+
+ ;A contains ICMP type to remove listener for
+ jmp icmp_remove_listener
+:
+
+ cpy #KPR_ICMP_SEND_ECHO_REQ ;send ping
+ bne not_echo_request
+ ;AX contains pointer to destination IP address
+ ldy #3
+: lda (kipper_params),y
+ sta icmp_echo_ip,y
+ dey
+ bpl :-
+ lda #0
+ sta icmp_echo_cnt
+ sta icmp_echo_cnt + 1 ;initialize ping sequence
+ jmp icmp_send_echo
+not_echo_request:
+
+:
+ cpy #KPR_TCP_CONNECT
+ bne :+
+ .import tcp_connect
+ .import tcp_callback
+ .import tcp_connect_ip
+ .import tcp_listen
+ ldy #3
+ lda #0
+ sta nonzero_octets
+@copy_dest_ip:
+ lda (kipper_params),y
+ beq @octet_was_zero
+ inc nonzero_octets
+@octet_was_zero:
+ sta tcp_connect_ip,y
+ dey
+ bpl @copy_dest_ip
+
+ ldy #KPR_TCP_CALLBACK
+ lda (kipper_params),y
+ sta tcp_callback
+ iny
+ lda (kipper_params),y
+ sta tcp_callback+1
+
+ ldy #KPR_TCP_PORT+1
+ lda (kipper_params),y
+ tax
+ dey
+ lda (kipper_params),y
+ ldy nonzero_octets
+ bne @outbound_tcp_connection
+ jmp tcp_listen
+
+@outbound_tcp_connection:
+ jmp tcp_connect
+
+:
+
+ .import tcp_send
+ .import tcp_send_data_len
+ cpy #KPR_SEND_TCP_PACKET
+ bne :+
+ ldy #KPR_TCP_PAYLOAD_LENGTH
+ lda (kipper_params),y
+ sta tcp_send_data_len
+ iny
+ lda (kipper_params),y
+ sta tcp_send_data_len+1
+ ldy #KPR_TCP_PAYLOAD_POINTER+1
+ lda (kipper_params),y
+ tax
+ dey
+ lda (kipper_params),y
+ jmp tcp_send
+
+:
+
+
+.import tcp_close
+ cpy #KPR_TCP_CLOSE_CONNECTION
+ bne :+
+ jmp tcp_close
+:
+
+
+; .import filter_dns
+; .import get_filtered_input
+; .import filter_number
+
+ cpy #KPR_INPUT_STRING
+ bne :+
+; ldy #40 ;max chars
+; ldax #$0000
+; jmp get_filtered_input
+; not supported under GEOS:
+ sec
+ rts
+:
+
+ cpy #KPR_INPUT_HOSTNAME
+ bne :+
+; ldy #40 ;max chars
+; ldax #filter_dns
+; jmp get_filtered_input
+; not supported under GEOS
+ sec
+ rts
+:
+
+cpy #KPR_INPUT_PORT_NUMBER
+ bne :+
+; ldy #5 ;max chars
+; ldax #filter_number
+; jsr get_filtered_input
+; bcs @no_port_entered
+
+ ;AX now points a string containing port number
+; .import parse_integer
+; jmp parse_integer
+; not supported under GEOS
+ sec
+@no_port_entered:
+ rts
+:
+
+cpy #KPR_BLOCK_COPY
+ bne :+
+ ;this is where we pay the price for trying to save a few 'zero page' pointers
+ ;by reusing the 'copy_src' and 'copy_dest' addresses!
+.segment "TCP_VARS"
+ tmp_copy_src: .res 2
+ tmp_copy_dest: .res 2
+ tmp_copy_length: .res 2
+.code
+
+ ldy #KPR_BLOCK_SRC
+ lda (kipper_params),y
+ sta tmp_copy_src
+ iny
+ lda (kipper_params),y
+ sta tmp_copy_src+1
+
+ ldy #KPR_BLOCK_DEST
+ lda (kipper_params),y
+ sta tmp_copy_dest
+ iny
+ lda (kipper_params),y
+ sta tmp_copy_dest+1
+
+ ldy #KPR_BLOCK_SIZE
+ lda (kipper_params),y
+ sta tmp_copy_length
+ iny
+ lda (kipper_params),y
+ sta tmp_copy_length+1
+
+ ldax tmp_copy_src
+ stax copy_src
+ ldax tmp_copy_dest
+ stax copy_dest
+ ldax tmp_copy_length
+ jmp copymem
+:
+
+ cpy #KPR_PARSER_INIT
+ bne :+
+ .import parser_init
+ jmp parser_init
+:
+
+ cpy #KPR_PARSER_SKIP_NEXT
+ bne :+
+ .import parser_skip_next
+ jmp parser_skip_next
+:
+
+
+
+ cpy #KPR_GET_LAST_ERROR
+ bne :+
+ lda ip65_error
+ clc
+ rts
+:
+
+
+;default function handler
+ lda #KPR_ERROR_FUNCTION_NOT_SUPPORTED
+ sta ip65_error
+ sec ;carry flag set = error
+ rts
+
+
+;-- LICENSE FOR function_dispatcher.s --
+; The contents of this file are subject to the Mozilla Public License
+; Version 1.1 (the "License"); you may not use this file except in
+; compliance with the License. You may obtain a copy of the License at
+; http://www.mozilla.org/MPL/
+;
+; Software distributed under the License is distributed on an "AS IS"
+; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+; License for the specific language governing rights and limitations
+; under the License.
+;
+; The Original Code is ip65.
+;
+; The Initial Developer of the Original Code is Jonno Downes,
+; jonno@jamtronix.com.
+; Portions created by the Initial Developer are Copyright (C) 2009