#compdef blackmagic
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (c) 2022 Rachel Mant
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

_blackmagic_frequency() {
	_numbers -u 'Hz' 'frequency' 'k:kilohertz' 'M:megahertz'
}

_blackmagic_size() {
	_numbers -u 'B' 'size in bytes' 'k:kibibytes' 'M:mebibytes'
}

local arguments

arguments=(
	'-h[show version and help then exit]'
	'-l[list available supported probes]'
	'-v=[set the output verbosity level]:number'
	'(-d -P -s -c)-d=[use a serial device at the given path]:path:_files'
	'(-d -P -s -c)-P=[use the <number>th debug probe found while scanning the system, see the output from list for the order]:number'
	'(-d -P -s -c)-s=[select the debug probe with the given serial number]:serial'
	'(-d -P -s -c)-c=[select the FTDI-based debug probe with of the given type]:type'
	'-n=[select the target device at the given position in the scan chain]:number'
	'(-j -A)-j[use JTAG instead of SWD]'
	'(-j -A)-A[use automatic chain scanning]'
	'-C[connect to target under hardware reset]'
	'(-t -T)-t[perform a chain scan and display information about the conected devices]'
	'(-t -T)-T[perform continues read- or write-back of a value to allow measurement of protocol timing. Aborted by ^C]'
	'-e[assume external resistors for FTDI devices]'
	'(-E -w -V -r)-E[erase the target device Flash]'
	'(-E -w -V -r)-w[write the specified binary file to the target device Flash (the default)]'
	'(-E -w -V -r)-V[verify the target device Flash against the specified binary file]'
	'(-E -w -V -r)-r[read the target device Flash]'
	'-p[power the target from the probe (if possible)]'
	'-R-[reset the device. If followed by "h", this will be done using the hardware reset line instead of over the debug link]:: :(h)'
	'-H[do not use the high level command API (bmp-remote)]'
	'*-M[run target-specific monitor commands]:command'
	'-a=[start address for the given Flash operation (defaults to the start of Flash)]:address:_numbers "address"'
	'-S=[number of bytes to work on in the Flash operation (default is till the operation fails or is complete)]:_blackmagic_size'
	':binary file to use in Flash operations:_files "*.bin"'
)

if ! (( $words[(Ie)-j] )); then
	arguments+=(
		'-f=[set an operating frequency for SWD]:frequency:_blackmagic_frequency'
		'-m=[use the given target ID for selection in SWD multi-drop]:target'
	)
fi

_arguments -s : $arguments
