; ZuluSCSI™ - Copyright (c) 2022 Rabbit Hole Computing™
; 
; ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
; 
; https://www.gnu.org/licenses/gpl-3.0.html
; ----
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version. 
; 
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details. 
; 
; You should have received a copy of the GNU General Public License
; along with this program.  If not, see <https://www.gnu.org/licenses/>.


; RP2040 PIO program for accelerating SCSI initiator / host function
; Run "pioasm scsi_accel_host.pio scsi_accel_host.pio.h" to regenerate the C header from this.
; GPIO mapping:
; - 0-7: DB0-DB7
; -   8: DBP
; Side set is ACK pin

.define REQ 9
.define ACK 10

; Read from SCSI bus using asynchronous handshake.
; Data is returned as 16-bit words that contain the 8 data bits + 1 parity bit.
; Number of bytes to receive minus 1 should be written to TX fifo.
; Number of bytes to receive must be divisible by 2.
.program scsi_host_async_read
    .side_set 1

    pull block                  side 1  ; Get number of bytes to receive
    mov x, osr                  side 1  ; Store to counter X

start:
    wait 0 gpio REQ             side 1  ; Wait for REQ low
    in pins, 9                  side 0  ; Assert ACK, read GPIO
    in null, 7                  side 0  ; Padding bits
    wait 1 gpio REQ             side 0  ; Wait for REQ high
    jmp x-- start               side 1  ; Deassert ACK, decrement byte count and jump to start
