name: Build interface2

on:
  push:
    paths:
      - interface2/**
      - .github/workflows/interface2_build.yml

# Required, for now, by aws-actions/configure-aws-credentials
permissions:
  id-token: write
  contents: read

jobs:
  test_fpga:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: interface2/fpga
    name: Test FPGA
    steps:
      - uses: actions/checkout@v2
      - name: Install Icarus Verilog
        run: |
          sudo apt-get update
          sudo apt-get install iverilog
      - name: Run tests
        run: make tests

  build_fpga:
    needs: test_fpga
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: interface2/fpga
    env:
      BITSTREAM_CACHE_BUCKET: coax20211004002809282500000001
    name: Build FPGA
    steps:
      - uses: actions/checkout@v2
        with:
          # All history is required to determine the bitstream source SHA, otherwise
          # it may be unnecessarily rebuilt.
          fetch-depth: 0
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@master
        with:
          role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
          aws-region: us-east-1
      - name: Get bitstream source SHA
        run: |
          BITSTREAM_SOURCE_SHA=$(git log -n 1 --format=%H rtl)
          echo Bitstream source SHA is $BITSTREAM_SOURCE_SHA
          echo "BITSTREAM_SOURCE_SHA=$BITSTREAM_SOURCE_SHA" >> $GITHUB_ENV
      - name: Check bitstream cache
        run: |
          BITSTREAM_CACHE_KEY=interface2/fpga/${BITSTREAM_SOURCE_SHA}.zip
          echo "BITSTREAM_CACHE_KEY=$BITSTREAM_CACHE_KEY" >> $GITHUB_ENV
          if aws s3api head-object --bucket $BITSTREAM_CACHE_BUCKET --key $BITSTREAM_CACHE_KEY; then
            echo Cached bitstream exists
            echo "BITSTREAM_CACHE_EXISTS=true" >> $GITHUB_ENV
          else
            echo Cached bitstream does not exist
            echo "BITSTREAM_CACHE_EXISTS=false" >> $GITHUB_ENV
          fi
      - name: Login to AWS ECR
        if: env.BITSTREAM_CACHE_EXISTS == 'false'
        id: aws_ecr_login
        uses: aws-actions/amazon-ecr-login@v1
      - name: Prepare iCEcube2
        if: env.BITSTREAM_CACHE_EXISTS == 'false'
        run: |
          ICECUBE2_IMAGE=$REGISTRY/icecube2:latest
          echo "ICECUBE2_IMAGE=$ICECUBE2_IMAGE" >> $GITHUB_ENV
          docker pull $ICECUBE2_IMAGE
        env:
          REGISTRY: ${{ steps.aws_ecr_login.outputs.registry }}
      - name: Build bitstream
        if: env.BITSTREAM_CACHE_EXISTS == 'false'
        run: make rtl
        env:
          ICECUBE2_MAC_ADDRESS: ${{ secrets.ICECUBE2_MAC_ADDRESS }}
      - name: Cache bitstream
        if: env.BITSTREAM_CACHE_EXISTS == 'false'
        run: |
          cd rtl
          zip cache.zip top.bin top_timing_report.txt
          aws s3 cp cache.zip s3://${BITSTREAM_CACHE_BUCKET}/${BITSTREAM_CACHE_KEY}
          rm cache.zip
      - name: Download cached bitstream
        if: env.BITSTREAM_CACHE_EXISTS == 'true'
        run: |
          cd rtl
          aws s3 cp s3://${BITSTREAM_CACHE_BUCKET}/${BITSTREAM_CACHE_KEY} cache.zip
          unzip cache.zip
          rm cache.zip
      - name: Attach bitstream
        uses: actions/upload-artifact@v2
        with:
          name: fpga_bitstream
          path: |
            interface2/fpga/rtl/top.bin
            interface2/fpga/rtl/top_timing_report.txt

  build_firmware:
    needs: build_fpga
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: interface2/firmware
    name: Build Firmware
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8
      - name: Install PlatformIO
        run: |
          python -m pip install --upgrade pip
          pip install platformio
      - name: Download bitstream
        uses: actions/download-artifact@v2
        with:
          name: fpga_bitstream
          path: interface2/fpga/rtl
      - name: Build firmware
        run: pio run
      - name: Attach firmware
        uses: actions/upload-artifact@v2
        with:
          name: firmware
          path: interface2/firmware/.pio/build/default/firmware.bin
