git-svn-id: http://svn.code.sf.net/p/netboot65/code@144 93682198-c243-4bdb-bd91-e943c89aac3b

This commit is contained in:
jonnosan 2009-06-28 08:36:49 +00:00
parent f8e824a62c
commit 6e76a71992
4 changed files with 243 additions and 30 deletions

View File

@ -11,7 +11,9 @@ op32 =copy_dest ;32 bit operand (pointer)
acc16 =acc32 ;16bit accumulater (value, NOT pointer)
.bss
temp_ax: .res 2
.code
;no 16bit operand as can just use AX
.exportzp acc32
.exportzp op32
@ -20,6 +22,8 @@ acc16 =acc32 ;16bit accumulater (value, NOT pointer)
.export add_32_32
.export add_16_32
.export sub_16_16
.export cmp_32_32
.export cmp_16_16
@ -54,6 +58,19 @@ cmp_16_16:
@exit:
rts
;subtract 2 16 bit numbers
;acc16=acc16-AX
sub_16_16:
stax temp_ax
sec
lda acc16
sbc temp_ax
sta acc16
lda acc16+1
sbc temp_ax+1
sta acc16+1
rts
;add a 32bit operand to the 32 bit accumulater
;acc32=acc32+op32
add_32_32:

View File

@ -9,7 +9,7 @@
.export ip65_init
.export ip65_process
.export ip65_random_word
.export ip65_ctr
.export ip65_ctr_arp
.export ip65_ctr_ip
@ -22,8 +22,10 @@
.import timer_init
.import arp_init
.import ip_init
.import timer_read
.import eth_inp
.import eth_inp
.import eth_outp
.import eth_rx
.import ip_process
@ -31,8 +33,10 @@
.importzp eth_proto_arp
.export ip65_random_word
.bss
ip65_ctr: .res 1 ; incremented for every incoming packet
ip65_ctr_arp: .res 1 ; incremented for every incoming arp packet
@ -41,6 +45,28 @@ ip65_ctr_ip: .res 1 ; incremented for every incoming ip packet
ip65_error: .res 1 ;last error code
.code
;generate a 'random' 16 bit word
;entropy comes from the last ethernet frame, counters, and timer
;inputs: none
;outputs: AX set to a pseudo-random 16 bit number
ip65_random_word:
jsr timer_read ;sets AX
adc $d018 ;on a c64, this is the raster register
pha
adc ip65_ctr_arp
ora #$08 ;make sure we grab at least 8 bytes from eth_inp
tax
:
adc eth_inp,x
adc eth_outp,x
dex
bne :-
tax
pla
adc ip65_ctr
eor ip65_ctr_ip
rts
; initialise the IP stack
; this calls the individual protocol & driver initialisations, so this is

View File

@ -28,6 +28,7 @@ MAX_TCP_PACKETS_SENT=8 ;timeout after sending 8 messages will be about 7 sec
.import check_for_abort_key
.import timer_read
.import ip65_random_word
.importzp acc32
.importzp op32
@ -111,12 +112,11 @@ tcp_send_data_len: .res 2
tcp_callback: .res 2
tcp_flags: .res 1
tcp_connect_sequence_number: .res 4
tcp_connect_expected_sequence_number: .res 4
tcp_connect_ack_number: .res 4
tcp_connect_last_ack: .res 4
tcp_connect_sequence_number: .res 4 ;the seq number we will next send out
tcp_connect_expected_ack_number: .res 4 ;what we expect to see in the next inbound ack
tcp_connect_ack_number: .res 4 ;what we will next ack
tcp_connect_last_received_seq_number: .res 4 ;the seq field in the last inbound packet for this connection
tcp_connect_last_ack: .res 4 ;ack field in the last inbound packet for this connection
tcp_connect_local_port: .res 2
tcp_connect_remote_port: .res 2
tcp_connect_ip: .res 4
@ -125,8 +125,6 @@ tcp_connect_ip: .res 4
tcp_timer: .res 1
tcp_loop_count: .res 1
tcp_packet_sent_count: .res 1
.data
tcp_client_port: .word $0400
.code
@ -143,13 +141,18 @@ tcp_init:
; carry flag is set if an error occured, clear otherwise
tcp_connect:
stax tcp_connect_remote_port
inc tcp_client_port
ldax tcp_client_port
jsr ip65_random_word
stax tcp_connect_local_port
lda #tcp_cxn_state_syn_sent
sta tcp_state
lda #0 ;reset the "packet sent" counter
sta tcp_packet_sent_count
;set the low word of seq number to $0000, high word to something random
sta tcp_connect_sequence_number
sta tcp_connect_sequence_number+1
jsr ip65_random_word
stax tcp_connect_sequence_number+2
@tcp_polling_loop:
@ -164,6 +167,8 @@ tcp_connect:
ldx #3 ;
: lda tcp_connect_ip,x
sta tcp_remote_ip,x
lda tcp_connect_sequence_number,x
sta tcp_sequence_number,x
dex
bpl :-
ldax tcp_connect_local_port
@ -214,6 +219,19 @@ tcp_connect:
sec ;signal an error
rts
@got_a_response:
;inc the sequence number to cover the SYN we have sent
ldax #tcp_connect_sequence_number
stax acc32
ldax #$01
jsr add_16_32
;set the expected ack number with current seq number
ldx #3 ;
: lda tcp_connect_sequence_number,x
sta tcp_connect_expected_ack_number,x
dex
bpl :-
clc
rts
@ -235,7 +253,8 @@ tcp_send:
rts
@connection_established:
ldax #tcp_connect_expected_sequence_number
;increment the expected sequence number
ldax #tcp_connect_expected_ack_number
stax acc32
ldax tcp_send_data_len
jsr add_16_32
@ -255,6 +274,9 @@ tcp_send:
ldx #3 ;
: lda tcp_connect_ip,x
sta tcp_remote_ip,x
lda tcp_connect_sequence_number,x
sta tcp_sequence_number,x
dex
bpl :-
ldax tcp_connect_local_port
@ -262,7 +284,7 @@ tcp_send:
ldax tcp_connect_remote_port
stax tcp_remote_port
jsr tcp_send_packet
lda tcp_packet_sent_count
@ -281,7 +303,8 @@ tcp_send:
@no_abort:
ldax #tcp_connect_last_ack
stax acc32
ldax #tcp_connect_expected_sequence_number
ldax #tcp_connect_expected_ack_number
stax op32
jsr cmp_32_32
beq @got_ack
@ -308,7 +331,13 @@ tcp_send:
sta ip65_error
sec ;signal an error
rts
@got_ack:
@got_ack:
;finished - now we need to advance the sequence number for the data we just sent
ldax #tcp_connect_sequence_number
stax acc32
ldax tcp_send_data_len
jsr add_16_32
clc
rts
@ -499,6 +528,8 @@ tcp_process:
lda tcp_inp+tcp_flags_field
cmp #tcp_flag_SYN+tcp_flag_ACK
bne @not_syn_ack
;it's a SYN/ACK
jsr check_current_connection
bcs @not_current_connection_on_syn_ack
lda tcp_state
@ -521,22 +552,65 @@ tcp_process:
lda #tcp_cxn_state_established
sta tcp_state
@not_expecting_syn_ack:
@not_expecting_syn_ack:
jmp @send_ack
@not_current_connection_on_syn_ack:
;just ignore
rts
@not_syn_ack:
lda tcp_inp+tcp_flags_field
cmp #tcp_flag_ACK
bne @not_ack
;is it an ACK - alone or with PSH/URGENT but not a SYN/ACK?
lda #tcp_flag_ACK
bit tcp_inp+tcp_flags_field
beq @not_ack
;is this the current connection?
jsr check_current_connection
bcs @not_current_connection_on_ack
;if it's an ACK, then record the last ACK (reorder the bytes in the process)
ldx #3 ; copy seq & ack fields (in reverse order)
ldy #0
: lda tcp_inp + tcp_ack,y
sta tcp_connect_last_ack,x
lda tcp_inp + tcp_seq,y
sta tcp_connect_last_received_seq_number,x
iny
dex
bpl :-
;was this the next sequence number we're waiting for?
ldax #tcp_connect_ack_number
stax acc32
ldax #tcp_connect_last_received_seq_number
stax op32
jsr cmp_32_32
bne @not_expected_seq_number
;what is the size of data in this packet?
lda ip_inp+ip_len+1 ;payload length (lo byte)
sta acc16
lda ip_inp+ip_len ;payload length (hi byte)
sta acc16+1
lda
;FIXME
; - move ack ptr along
; - send ack
; - do a callback
.byte $92
@not_expected_seq_number:
@not_current_connection_on_ack:
rts
@not_ack:
lda tcp_inp+tcp_flags_field
cmp #tcp_flag_SYN
bne @not_syn
;for the moment, inbound connections not accepted. so send a RST
@decline_syn_with_reset:
;create a RST packet
ldx #3 ; copy sequence number to ack (in reverse order)
ldy #0

View File

@ -9,7 +9,7 @@
.import tcp_listen
.import tcp_callback
; .import ip65_process
.import ip65_random_word
.import ip65_error
.import tcp_connect
@ -32,7 +32,9 @@
.import add_16_32
.import cmp_32_32
.import cmp_16_16
.import sub_16_16
.segment "STARTUP" ;this is what gets put at the start of the file on the C64
.word basicstub ; load address
@ -57,9 +59,29 @@ basicstub:
.code
init:
jsr print_cr
ldax #$1234
stax acc16
ldax #$1235
jsr test_sub_16_16
ldax #$ff34
stax acc16
ldax #$1235
jsr test_sub_16_16
ldax #$100
stax acc16
ldax #$ffff
jsr test_sub_16_16
ldax #$ffff
stax acc16
ldax #$100
jsr test_sub_16_16
rts
ldax #number1
stax acc32
stax op32
@ -171,6 +193,11 @@ init:
init_ip_via_dhcp
jsr print_ip_config
jsr print_cr
jsr print_random_number
;connect to port 81 - should be rejected
ldax #tcp_callback_routine
@ -198,17 +225,46 @@ init:
stax tcp_connect_ip
ldax tcp_dest_ip+2
stax tcp_connect_ip+2
jmp @skip_past_normal_http
ldax #80
jsr tcp_connect
jsr check_for_error
ldax #http_get_length
stax tcp_send_data_len
ldax #http_get_msg
jsr tcp_send
jsr check_for_error
ldax #tcp_callback_routine
stax tcp_callback
ldax tcp_dest_ip
stax tcp_connect_ip
ldax tcp_dest_ip+2
stax tcp_connect_ip+2
@skip_past_normal_http:
; connect to port 80 again, and send GET in 2 parts
ldax #80
jsr tcp_connect
jsr check_for_error
ldax #4
stax tcp_send_data_len
ldax #http_get_msg
jsr tcp_send
jsr check_for_error
ldax #http_get_length-4
stax tcp_send_data_len
ldax #http_get_msg+4
jsr tcp_send
jsr check_for_error
; .byte $92
ldax #looping
@ -235,6 +291,14 @@ check_for_error:
@exit:
rts
print_random_number:
jsr ip65_random_word
stx temp_ax
jsr print_hex
lda temp_ax
jsr print_hex
jsr print_cr
rts
;assumes acc32 & op32 already set
test_add_32_32:
@ -363,6 +427,37 @@ test_add_16_32:
rts
;assumes acc16 & AX already set
test_sub_16_16:
stax temp_ax
lda acc16+1
jsr print_hex
lda acc16
jsr print_hex
lda #'-'
jsr print_a
lda temp_ax+1
jsr print_hex
lda temp_ax
jsr print_hex
lda #'='
jsr print_a
ldax temp_ax
jsr sub_16_16
lda acc16+1
jsr print_hex
lda acc16
jsr print_hex
jsr print_cr
rts
@error:
ldax #failed_msg
jsr print
@ -414,8 +509,9 @@ number15:
number16:
.byte $00,$00,$00,$00
tcp_dest_ip:
.byte 10,5,1,1
; .byte 10,5,1,1
.byte 74,207,242,229
error_code:
.asciiz "ERROR CODE: $"