# Maintainer: bill-auger <bill-auger@programmer.net>


pkgbase=neo6502
pkgname=(neo6502-docs neo6502-emulator neo6502-firmware)
_upstream_name=neo6502-firmware
pkgver=0.16.5
_pico_sdk_ver=1.5.1
_tinyusb_ver=0.16.0
_picodvi_rev=a248d72b8cc95a08b43ef4307cd954eb801272b2 # no stable release available
_pico_fatfs_ver=1.0.8
pkgrel=1
pkgdesc="Emulator and firmware for the Neo6502 retro-computer"
arch=(x86_64) # armv7h i686 are both lacking some makedepends
license=(custom:MIT)
url=http://www.neo6502.com/

depends=()
makedepends=(64tass arm-none-eabi-gcc arm-none-eabi-newlib cmake
             python-gitpython python-pillow sdl2 zip)
makedepends+=(pandoc texlive-latex texlive-latexextra texlive-latexrecommended)
source=(neo6502-firmware-${pkgver}.tar.gz::https://github.com/paulscottrobson/${_upstream_name}/archive/refs/tags/v${pkgver}.tar.gz
        pico-sdk-${_pico_sdk_ver}.tar.gz::https://github.com/raspberrypi/pico-sdk/archive/refs/tags/${_pico_sdk_ver}.tar.gz
        tinyusb-${_tinyusb_ver}.tar.gz::https://github.com/hathach/tinyusb/archive/refs/tags/${_tinyusb_ver}.tar.gz
        PicoDVI-${_picodvi_rev}.tar.gz::https://github.com/Wren6991/PicoDVI/archive/${_picodvi_rev}.tar.gz
        pico_fatfs-${_pico_fatfs_ver}.tar.gz::https://github.com/carlk3/no-OS-FatFS-SD-SPI-RPi-Pico/archive/refs/tags/v${_pico_fatfs_ver}.tar.gz)
source+=(separate_emulators.patch)
source+=(offline_build.patch) # includes dep_checks.patch
source+=(no_harmless_exit_fails.patch)
sha256sums=('efea50fc3355ed98e6faf20ed48a6ae5ac2012181cbfeabbf2efd476dcc61dbe'
            '95f5e522be3919e36a47975ffd3b208c38880c14468bd489ac672cfe3cec803c'
            '5b24ed6d7732d07bb25a2013b3ad65fdb8752da3e9faac6372ff0574b26a0447'
            'efd85ffe21335004f8ebfe6d07c2a6482c4dd96bc6f0132fa4adb21464845a88'
            '68ffa9f157b94df969d3bc34f32acb4d4fdf2bdf5391952cf242fec20f53b0b8')
sha256sums+=(4b20bfc8c5098a8e9c3d2a9ba8f1be26a70026ae418f86526d90deeeb4f5a7b3)
sha256sums+=(5ebdea1d7dc5eb534b4fbecc1f70a193e6354506cffbfaf97829bdb494fff953)
sha256sums+=(e8d3d24dae1fdc502180f4f79f931689569c7ea08ed7f9dba5270af4ed28f24d)


prepare()
{
  cd "${srcdir}"/pico-sdk-${_pico_sdk_ver}

  # configure cross-compiler for pico firmware
  sed -i 's|ARM_TOOLCHAIN_COMMON_FLAGS "|& -march=armv6-m -mtune=cortex-m0plus -fcf-protection=none -fstack-check=generic -fno-stack-clash-protection |' cmake/preload/toolchains/pico_arm_gcc.cmake


  cd "${srcdir}"/${_upstream_name}-${pkgver}

  # avoid building the windows emulator
  echo "applying separate_emulators.patch"
  patch -Np1 < "${srcdir}"/separate_emulators.patch

  # avoid downloading sources at build-time
  echo "applying offline_build.patch"
  patch -Np1 < "${srcdir}"/offline_build.patch

  # prevent exit failure when sourcing cvimg.py
  echo "applying no_harmless_exit_fails.patch"
  patch -Np1 < "${srcdir}"/no_harmless_exit_fails.patch
}

build()
{
  cd "${srcdir}"/${_upstream_name}-${pkgver}

  # avoid downloading sources at build-time
  export PICO_SDK_FETCH_FROM_GIT=OFF
  export PICO_SDK_PATH="${srcdir}"/pico-sdk-${_pico_sdk_ver}
  export PICO_TINYUSB_PATH="${srcdir}"/tinyusb-${_tinyusb_ver}
  export PICO_DVI_PATH="${srcdir}"/PicoDVI-${_picodvi_rev}
  export PICO_FATFS_PATH="${srcdir}"/no-OS-FatFS-SD-SPI-RPi-Pico-${_pico_fatfs_ver}

  make firmware
  make linux
}

package_neo6502-docs()
{
  pkgdesc="Documentation for the Neo6502 retro-computer"
  cd "${srcdir}"/${_upstream_name}-${pkgver}

  install -Dm644 documents/release/*.odt -t "${pkgdir}"/usr/share/doc/${pkgbase}/
  install -Dm644 documents/release/*.pdf -t "${pkgdir}"/usr/share/doc/${pkgbase}/
  install -Dm644 documents/*.pdf         -t "${pkgdir}"/usr/share/doc/${pkgbase}/
  install -Dm644 LICENSE                 -t "${pkgdir}"/usr/share/licenses/${pkgbase}/
}

package_neo6502-emulator()
{
  pkgdesc="Emulator for the Neo6502 retro-computer"
  optdepends=("neo6502-docs: Neo6502 documentation")

  cd "${srcdir}"/${_upstream_name}-${pkgver}

  install -Dm755 bin/neo            "${pkgdir}"/usr/bin/neo
  install -Dm644 bin/*.zip       -t "${pkgdir}"/usr/share/${pkgbase}/utils/
  install -d                        "${pkgdir}"/usr/share/${pkgbase}/examples/
  cp -a examples/{assembly,basic,C} "${pkgdir}"/usr/share/${pkgbase}/examples/
  chmod -R 644                      "${pkgdir}"/usr/share/${pkgbase}/examples/
  install -Dm644 LICENSE         -t "${pkgdir}"/usr/share/licenses/${pkgbase}/
}

package_neo6502-firmware()
{
  pkgdesc="Firmware for the Neo6502 retro-computer"
  optdepends=("neo6502-docs: Neo6502 documentation")

  cd "${srcdir}"/${_upstream_name}-${pkgver}

  install -Dm644 bin/*.bin -t "${pkgdir}"/usr/share/${pkgbase}/firmware/
  install -Dm644 LICENSE   -t "${pkgdir}"/usr/share/licenses/${pkgbase}/
}
