⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 picnic.asm

📁 PIC单片机实现的一种网络单片机
💻 ASM
📖 第 1 页 / 共 5 页
字号:
        BCF     STATUS,RP0

        CLRF    PORTC
        MOVLW   b'00100010'
        CALL    assert_wr0

        MOVLW   0x03
        MOVWF   PORTC
        MOVLW   ne_next
        MOVWF   FSR
        MOVF    INDF,W
        CALL    assert_wr0
        GOTO    main99

;-----------------------------------------------------------------------------------
;		ARP协议处理
;-----------------------------------------------------------------------------------
do_arp:
        BCF     STATUS,RP0
        MOVLW   NE_SIZE+PACKET_SIZE
        MOVWF   remote_adr
        MOVLW   ne_cn_l
        MOVWF   FSR
        MOVF    INDF,W
        MOVWF   remote_len
        INCF    FSR,F
        MOVF    INDF,W
        MOVWF   remote_len+1
        MOVLW   NE_SIZE+PACKET_SIZE
        SUBWF   remote_len,F
        MOVLW   0x01
        BTFSS   STATUS,C
        SUBWF   remote_len+1,F
        CALL    remote_read

        MOVLW   ip_header			;读出ip_header以后的数据
        MOVWF   FSR
get_packet10:
        MOVF    remote_len,W
        IORWF   remote_len+1,W
        BTFSC   STATUS,Z
        GOTO    get_packet2

        BCF     PORTE,RE0			; RD
        BTFSS   PORTC,RC5			; RDY - ~Wait
        GOTO    $-1
        MOVF    PORTD,W
        BSF     PORTE,RE0			; RD
        MOVWF   INDF
        INCF    FSR,F

        MOVLW   0x01
        SUBWF   remote_len,F
        BTFSS   STATUS,C
        SUBWF   remote_len+1,F
        BTFSS   FSR,7				; Over flow?
        GOTO    get_packet10
;	80h以后
get_packet20:
        MOVF    remote_len,W
        IORWF   remote_len+1,W
        BTFSC   STATUS,Z
        GOTO    get_packet2			;读入完了

        BCF     PORTE,RE0			;RD
        BTFSS   PORTC,RC5			;RDY - ~Wait
        GOTO    $-1
        BSF     PORTE,RE0

        MOVLW   0x01
        SUBWF   remote_len,F
        BTFSS   STATUS,C
        SUBWF   remote_len+1,F
        GOTO    get_packet20
;
;	分析ARP Header
get_packet2:
        MOVLW   0x00
        SUBWF   arp_ope,W
        BTFSS   STATUS,Z
        GOTO    main9				; arp_code为0001以外则舍弃
        MOVLW   0x01
        SUBWF   arp_ope+1,W
        BTFSC   STATUS,Z
        GOTO    arp_req
        MOVLW   0x02
        SUBWF   arp_ope+1,W
        BTFSC   STATUS,Z
        GOTO    arp_reply

        GOTO    main9
arp_reply:
        ; pagesel ser_arp
        MOVLW   HIGH ser_arp
        MOVWF   PCLATH
        GOTO    ser_arp & 0x7ff
arp_req:
arp:
        MOVLW   this_ip				;this_ip在Bank 1
        MOVWF   FSR

        MOVF    arp_dest_ip,W
        SUBWF   INDF,W
        BTFSS   STATUS,Z
        GOTO    main9
        INCF    FSR,F
        MOVF    arp_dest_ip+1,W
        SUBWF   INDF,W
        BTFSS   STATUS,Z
        GOTO    main9
        INCF    FSR,F
        MOVF    arp_dest_ip+2,W
        SUBWF   INDF,W
        BTFSS   STATUS,Z
        GOTO    main9
        INCF    FSR,F
        MOVF    arp_dest_ip+3,W
        SUBWF   INDF,W
        BTFSS   STATUS,Z
        GOTO    main9
;	是发送给自己的ARP请求
        CALL    prepare_ether2

        MOVLW   COM_PROTO			; 08 Etherframe type
        CALL    assert_wr
        MOVLW   ARP_PROTO			; ARP
        CALL    assert_wr

        MOVLW   HIGH arp1
        MOVWF   PCLATH
        CALL    arp1 & 0x7ff
        CLRF    PCLATH

        CLRF    PORTC
        MOVLW   b'00100010'
        CALL    assert_wr0

        MOVLW   0x04
        MOVWF   PORTC
        MOVLW   PAGE_BEGIN			; transmit page is start page
        CALL    assert_wr0

        MOVLW   0x05
        MOVWF   PORTC
        MOVLW   0x3C				; minimum pakcet = 60
        CALL    assert_wr0

        MOVLW   0x06
        MOVWF   PORTC
        dw	0x100					; adr high
        CALL    assert_wr0

        CALL    transmit			;发送ARP应答
        GOTO    main9

;-----------------------------------------------------------------------------------
;		IP协议处理
;-----------------------------------------------------------------------------------
do_ip:
        BCF     STATUS,RP0
        MOVLW   NE_SIZE+PACKET_SIZE
        MOVWF   remote_adr
        MOVLW   ne_cn_l
        MOVWF   FSR
        MOVF    INDF,W
        MOVWF   remote_len
        INCF    FSR,F
        MOVF    INDF,W
        MOVWF   remote_len+1

        MOVLW   NE_SIZE+PACKET_SIZE
        SUBWF   remote_len,F
        MOVLW   0x01
        BTFSS   STATUS,C
        SUBWF   remote_len+1,F
        CALL    remote_read
;
;	IP Packet受信处理
        MOVLW   ip_header			;将读入的数据写入ip_header地址开始
        MOVWF   FSR
        BCF     PORTE,RE0
        BTFSS   PORTC,RC5
        GOTO    $-1
        MOVF    PORTD,W
        BSF     PORTE,RE0
        MOVWF   INDF				;读入1个byte
        INCF    FSR,F

        MOVLW   0x01
        SUBWF   remote_len,F			;length --
        BTFSS   STATUS,C
        SUBWF   remote_len+1,F

        MOVF    ip_ver_len,W			;计算剩余的Byte数
        MOVWF   gcn1
        MOVLW   0x0F
        ANDWF   gcn1,F
        BCF     STATUS,C
        RLF     gcn1,F
        RLF     gcn1,F
        DECF    gcn1,F				;根据IP Header, 计算IP Header的大小
        CALL    copy_toram			;送到RAM中

ip_get_packet2:
        MOVLW   0x45
        SUBWF   ip_ver_len,W
        BTFSS   STATUS,Z			; IP Version 4?
        GOTO    ip_get_packet9
        MOVLW   TCP_PROTO
        SUBWF   ip_proto,W
        BTFSC   STATUS,Z
        GOTO    tcp				;TCP受信处理
        MOVLW   UDP_PROTO
        SUBWF   ip_proto,W
        BTFSC   STATUS,Z
        GOTO    udp				;UDP受信处理
        MOVLW   ICMP_PROTO
        SUBWF   ip_proto,W
        BTFSC   STATUS,Z
        GOTO    icmp				;ICMP受信处理
ip_get_packet9:
        CALL    abort				;Remote DMA中止
        GOTO    main9

;-----------------------------------------------------------------------------------
;		按照gcn1中设定的数据量读入数据
;-----------------------------------------------------------------------------------
copy_toram:
        BCF     PORTE,RE0
        BTFSS   PORTC,RC5
        GOTO    $-1
        MOVF    PORTD,W
        BSF     PORTE,RE0
        MOVWF   INDF
        INCF    FSR,F

        MOVLW   0x01
        SUBWF   remote_len,F
        BTFSS   STATUS,C
        SUBWF   remote_len+1,F
        DECFSZ  gcn1,F
        GOTO    copy_toram
        RETURN

;-----------------------------------------------------------------------------------
;		将剩余的Packet内容送到Buffer RAM中
;-----------------------------------------------------------------------------------
get_remain:
get_remain0:
        MOVF    remote_len,W
        IORWF   remote_len+1,W
        BTFSC   STATUS,Z
        GOTO    get_remain9			; End ?

        BCF     PORTE,RE0
        BTFSS   PORTC,RC5
        GOTO    $-1
        MOVF    PORTD,W
        BSF     PORTE,RE0
        MOVWF   INDF
        INCF    FSR,F

        MOVLW   0x01
        SUBWF   remote_len,F
        BTFSS   STATUS,C
        SUBWF   remote_len+1,F

        MOVLW   0x90				; 16+128
        ADDWF   FSR,W
        BTFSS   STATUS,C
        GOTO    get_remain0

        MOVLW   0x20
        MOVWF   FSR				;Bank变化后再读入
        BSF     STATUS,IRP

get_remain20:
        MOVF    remote_len,W
        IORWF   remote_len+1,W
        BTFSC   STATUS,Z
        GOTO    get_remain9			;读入完了

        BCF     PORTE,RE0
        BTFSS   PORTC,RC5
        GOTO    $-1
        MOVF    PORTD,W
        BSF     PORTE,RE0
        MOVWF   INDF
        INCF    FSR,F

        MOVLW   0x01
        SUBWF   remote_len,F
        BTFSS   STATUS,C
        SUBWF   remote_len+1,F

        MOVLW   0x90				; 16+128
        ADDWF   FSR,W
        BTFSS   STATUS,C
        GOTO    get_remain20

get_remain30
        CALL    abort

get_remain9:
        BCF     STATUS,IRP
        RETURN

;-----------------------------------------------------------------------------------
;		ICMP协议处理
;-----------------------------------------------------------------------------------
icmp:
        CALL    get_remain			;从RTL8019中读取数据
        MOVLW   0x08
        SUBWF   icmp_type,W
        BTFSS   STATUS,Z
        GOTO    main9				;丢弃type为08以外的数据
        MOVLW   0x00
        SUBWF   icmp_code,W
        BTFSS   STATUS,Z
        GOTO    main9				;丢弃code为0以外的数据

ping
        MOVLW   ICMP_PROTO
        MOVWF   proto				;Protocol=ICMP
        CALL    prepare_ip			;准备IP Packet

        MOVLW   PACKET_SIZE+IP_SIZE
        MOVWF   remote_adr
        MOVLW   PAGE_BEGIN
        MOVWF   remote_adr+1
        MOVF    ip_length+1,W			; ne_cn_l
        MOVWF   remote_len
        MOVLW   IP_SIZE-1
        SUBWF   remote_len,F
        CLRF    remote_len+1
        CALL    remote_write

        CALL    clear_sum			;清除checksum

        MOVLW   0x10
        MOVWF   PORTC
        CALL    assert_wr2times			;type : code
        CALL    assert_wr2times			;sum High : Low

        MOVF    ip_length+1,W
        MOVWF   gcn1
        MOVLW   IP_SIZE+4
        SUBWF   gcn1,F
        MOVLW   icmp_header+4
        MOVWF   FSR
icmp10:
        MOVF    INDF,W
        INCF    FSR,F
        CALL    assert_wr

        MOVLW   0x90
        ADDWF   FSR,W
        BTFSC   STATUS,C
        GOTO    icmp20
icmp19:
        DECFSZ  gcn1,F
        GOTO    icmp10
        BCF     STATUS,IRP

        dw	0x100
        CALL    assert_wr

        MOVLW   PACKET_SIZE+IP_SIZE+2
        MOVWF   remote_adr
        MOVLW   PAGE_BEGIN
        MOVWF   remote_adr+1
        CALL    set_checksum

        MOVLW   0x04
        MOVWF   PORTC
        MOVLW   PAGE_BEGIN
        CALL    assert_wr0

        MOVLW   0x05
        MOVWF   PORTC
        MOVLW   ne_cn_l
        MOVWF   FSR
        MOVF    INDF,W
        CALL    assert_wr0

        MOVLW   0x06
        MOVWF   PORTC
        INCF    FSR,F
        MOVF    INDF,W
        CALL    assert_wr0

        CALL    transmit			;发送ping的应答
        GOTO    main9
icmp20:
        MOVLW   0x20
        MOVWF   FSR
        BSF     STATUS,IRP
        GOTO    icmp19

;-----------------------------------------------------------------------------------
;		UDP处理
;-----------------------------------------------------------------------------------
udp:
        MOVLW   0x08
        MOVWF   gcn1
        CALL    copy_toram

; BOOTP Port的检查
        MOVLW   0x00
        SUBWF   tcp_tar_port,W
        BTFSS   STATUS,Z
        GOTO    udp_1
        MOVLW   BOOTPC_PORT & 0xff
        SUBWF   tcp_tar_port+1,W
        BTFSC   STATUS,Z
        GOTO    bootp_res			; 进入BOOTP处理

        MOVLW   this_ip
        MOVWF   FSR
        MOVF    INDF,W
        SUBWF   ip_dest,W
        BTFSS   STATUS,Z
        GOTO    main9
        INCF    FSR,F
        MOVF    INDF,W
        SUBWF   ip_dest+1,W
        BTFSS   STATUS,Z
        GOTO    main9
        INCF    FSR,F
        MOVF    INDF,W
        SUBWF   ip_dest+2,W
        BTFSS   STATUS,Z
        GOTO    main9
        INCF    FSR,F
        MOVF    INDF,W
        SUBWF   ip_dest+3,W
        BTFSS   STATUS,Z
        GOTO    main9
; LCD Port检查
udp_1:
        MOVLW   lcd_port & 0xff
        MOVWF   FSR
        MOVF    INDF,W
        SUBWF   tcp_tar_port,W
        BTFSS   STATUS,Z
        GOTO    udp_2
        INCF    FSR,F
        MOVF    INDF,W
        SUBWF   tcp_tar_port+1,W
        BTFSC   STATUS,Z
        GOTO    udp_lcd_tag
udp_2:
        MOVLW   para_port & 0xff
        MOVWF   FSR
        MOVF    INDF,W
        SUBWF   tcp_tar_port,W
        BTFSS   STATUS,Z
        GOTO    udp_3
        INCF    FSR,F
        MOVF    INDF,W
        SUBWF   tcp_tar_port+1,W
        BTFSC   STATUS,Z
        GOTO    udp_parallel_tag
udp_3:
        MOVLW   serial_port & 0xff
        MOVWF   FSR
        MOVF    INDF,W
        SUBWF   tcp_tar_port,W
        BTFSS   STATUS,Z
        GOTO    udp_4
        INCF    FSR,F
        MOVF    INDF,W
        SUBWF   tcp_tar_port+1,W
        BTFSC   STATUS,Z
        GOTO    udp_serial_tag
udp_4:
;
; add new function here
;
        GOTO    main9

udp_lcd_tag:
        MOVLW   HIGH udp_lcd
        MOVWF   PCLATH
        GOTO    udp_lcd & 0x7ff

;
; Parallel I/O 处理(UDP)
udp_parallel_tag:
        MOVLW   udp_data
        MOVWF   FSR
        CALL    get_remain
        MOVLW   HIGH udp_parallel
        MOVWF   PCLATH
        GOTO    udp_parallel & 0x7ff

udp_serial_tag:
        MOVLW   HIGH udp_serial
        MOVWF   PCLATH
        GOTO    udp_serial & 0x7ff

;-----------------------------------------------------------------------------------
get_dgram:
        MOVF    remote_len,W
        IORWF   remote_len+1,W
        BTFSC   STATUS,Z
        GOTO    get_dgram9

        BCF     PORTE,RE0
        BTFSS   PORTC,RC5
        GOTO    $-1
        MOVF    PORTD,W
        BSF     PORTE,RE0
        MOVWF   mdata

        MOVLW   0x01
        SUBWF   remote_len,F
        BTFSS   STATUS,C
        SUBWF   remote_len+1,F
        BCF     STATUS,C			; OK
        RETURN
get_dgram9:
        BSF     STATUS,C			; EOT

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -