;.global SendFrame
; Waits for a message reads it to the struct passed as an argument
; First argument is the pointer to the message struct
    .global WaitForMessage
    .global ResetRxL1Fifo

    .asg    27, RX_EOF_ERROR
    .asg    24, R31_ERROR_CRC
    .asg    23, R31_ERROR_NIBBLE
    .asg    22, R31_RX_SOF
    .asg    21, R31_RX_SFD
    .asg    20, R31_RX_EOF
    .asg    19, R31_RX_ERROR
    .asg    18, R31_WORD_RDY
    .asg    17, R31_BYTE_RDY
    .asg    16, R31_DATA_RDY
    

    .asg    31, R31_TX_CRC_ERR
    .asg    30, R31_TX_RESET
    .asg    29, R31_TX_EOF
    .asg    28, R31_TX_ERROR_NIBBLE
    .asg    27, R31_TX_CRC_HIGH
    .asg    26, R31_TX_CRC_LOW
    .asg    25, R31_TX_PUSH16
    .asg    24, R31_TX_PUSH8
    .asg    23, R31_RX_ERROR_CLR
    .asg    22, R31_RX_EOF_CLR
    .asg    21, R31_RX_SFD_CLR
    .asg    20, R31_RX_SOF_CLR
    .asg    18, R31_RX_RESET
    .asg    17, R31_RX_POP16
    .asg    16, R31_RX_POP8

ResetRxL1Fifo:
    ldi R31.w2, 0xf4         ; clear rx errror, clear eof, cler sfd, cler sof, reset
    jmp R3.w2

WaitForMessage:
    ; the first argument is the pointer to the message struct and is passed in R14
    ; the first 32 bits of the message struct are the length of the message
    ; after that there is the body
    ;    - 6 bytes for the destination mac address
    ;    - 6 bytes for the source mac address
    ;    - 2 bytes for the ethertype
    ;    - n bytes for the payload
    ;------------------------------------------------------------------------------
    .asg R16, len
    .asg R14, rx_msg
    .asg R15, buffer
    .asg R17, status
    .asg R18, tmpreg1
    .asg R19, tmpreg2
    .asg R20, tmpreg3
    .asg R21, tmpreg4
    .asg R29, ifstatus
    .asg "set R31, R31, R31_RX_POP16", DO_POP_16
    add  buffer, rx_msg, 4  ; take the pointer to the buffer

restart:
        ldi len, 0           ; initialize length to zero
        ldi R31.w2, 0xf4     ; clear rx errror, clear eof, cler sfd, cler sof, reset
        qba wait_sof
    
wait_sof:
    ; wait for sof and data read
    ldi tmpreg2.w0, 0x0000
    ldi tmpreg2.w2, 0x0004            ; wait for word ready
wait_sof_2:
    and tmpreg1, R31, tmpreg2
    qbeq wait_sof_2, tmpreg1, 0

; start of main cycle
cycle:
        and ifstatus, R31, R31
        ; check errors
        ;and tmpreg1, ifstatus.b2, 0x90 ; error nibble and rxeof
        ;qbne restart, tmpreg1, 0

no_error:
        qbbc keep_copying, ifstatus, R31_RX_EOF    ; if rx_eof
        	qbbs restart, ifstatus, R31_ERROR_CRC		       ; if crc error goto restart
        	qbbs restart, ifstatus, R31_ERROR_NIBBLE		   ; if nibble error goto restart
            qbbc return, ifstatus, R31_DATA_RDY                ; if no data ready terminate
keep_copying:
        ; else (no eof) if there is no data ready then let's repeat
        qbbc cycle, ifstatus, R31_DATA_RDY

copy_data:
        ; load a word
        qbbc copy_byte, ifstatus, R31_WORD_RDY ; jump if not word ready
        sbbo &ifstatus.w0, buffer, len, 2
        DO_POP_16
        add len, len, 2
        qba cycle

copy_byte:
        ; load a byte
        qbbc cycle, ifstatus, R31_BYTE_RDY
        sbbo &ifstatus.b0, buffer, len, 1
        DO_POP_16
        add len, len, 1

return:
    ; copy length
    sbbo &len, rx_msg, 0, 4
    jmp R3.w2





