name: Build interface2

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

# Required by aws-actions/configure-aws-credentials for GitHub OIDC.
permissions:
  id-token: write
  contents: read

jobs:
  test_fpga:
    name: Test FPGA
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: interface2/fpga

    steps:
      - uses: actions/checkout@v4

      - name: Install Icarus Verilog
        run: |
          sudo apt-get update
          sudo apt-get install iverilog

      - name: Run tests
        run: make tests

  build_fpga:
    name: Build FPGA
    needs: test_fpga
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: interface2/fpga

    steps:
      - uses: actions/checkout@v4
        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@v4
        with:
          role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
          aws-region: us-east-1

      - name: Get bitstream cache key
        run: |
          BITSTREAM_SOURCE_SHA=$(git log -n 1 --format=%H rtl)
          BITSTREAM_CACHE_KEY=interface2/fpga/$BITSTREAM_SOURCE_SHA.zip
          echo Bitstream source SHA is $BITSTREAM_SOURCE_SHA
          echo "BITSTREAM_SOURCE_SHA=$BITSTREAM_SOURCE_SHA" >> $GITHUB_ENV
          echo "BITSTREAM_CACHE_KEY=$BITSTREAM_CACHE_KEY" >> $GITHUB_ENV

      - name: Check bitstream cache
        run: |
          if aws s3api head-object --bucket ${{ vars.BITSTREAM_CACHE_BUCKET }} --key ${{ env.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@v2
        with:
          mask-password: 'true'

      - 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://${{ vars.BITSTREAM_CACHE_BUCKET }}/${{ env.BITSTREAM_CACHE_KEY }}
          rm cache.zip

      - name: Download cached bitstream
        if: env.BITSTREAM_CACHE_EXISTS == 'true'
        run: |
          cd rtl
          aws s3 cp s3://${{ vars.BITSTREAM_CACHE_BUCKET }}/${{ env.BITSTREAM_CACHE_KEY }} cache.zip
          unzip cache.zip
          rm cache.zip

      - name: Attach bitstream
        uses: actions/upload-artifact@v4
        with:
          name: fpga_bitstream
          path: |
            interface2/fpga/rtl/top.bin
            interface2/fpga/rtl/top_timing_report.txt

  build_firmware:
    name: Build Firmware
    needs: build_fpga
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: interface2/firmware

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python 3.8
        uses: actions/setup-python@v5
        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@v4
        with:
          name: fpga_bitstream
          path: interface2/fpga/rtl

      - name: Build firmware
        run: pio run

      - name: Attach firmware
        uses: actions/upload-artifact@v4
        with:
          name: firmware
          path: interface2/firmware/.pio/build/default/firmware.bin
