# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  # clean:
  #   # The type of runner that the job will run on
  #   runs-on: ubuntu-latest

  #   # Steps represent a sequence of tasks that will be executed as part of the job
    # steps:

    #   - name: cleanCommit
    #     run: |
    #       cd /home/runner/work/RiftCore/
    #       git clone https://github.com/whutddk/RiftCore/
    #       cd RiftCore/
    #       git fetch --all
    #       git checkout gh-pages
    #       git reset --hard 4e5a
 
    #   - name: cleanPush
    #     uses: ad-m/github-push-action@master
    #     with:
    #       github_token: ${{ secrets.GITHUB_TOKEN }}
    #       branch: gh-pages
    #       force: true
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    # needs: clean
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
          
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8

      - name: APT
        run: |
          sudo apt-get update
          sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev libusb-1.0-0-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev device-tree-compiler pkg-config libexpat-dev

      # - name: Access request
      #   run: |
      #     cd /home/runner/work/RiftCore
      #     sudo chmod +777 /home/runner/work/RiftCore/ -R        

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install matplotlib

      - name: Cache Tools Chain
        id: cache-tools-chian
        uses: actions/cache@v2.1.3
        with:
          path: |
            ~/RISCV
            ~/iverilog
            ~/riscv-tests
            ~/lib
            ~/bin
          key: 669



      - name: RISCV GCC
        if: steps.cache-tools-chian.outputs.cache-hit != 'true'
        run: |
          cd ~
          pwd
          mkdir ~/RISCV
          export RISCV=~/RISCV
          wget https://static.dev.sifive.com/dev-tools/riscv64-unknown-elf-gcc-8.3.0-2020.04.0-x86_64-linux-ubuntu14.tar.gz
          tar -x -f riscv64-unknown-elf-gcc-8.3.0-2020.04.0-x86_64-linux-ubuntu14.tar.gz --strip-components=1 -C $RISCV
          export PATH=$PATH:$RISCV/bin


      - name: Iverilog
        if: steps.cache-tools-chian.outputs.cache-hit != 'true'
        run: |
          cd ~
          git clone https://github.com/steveicarus/iverilog.git
          cd iverilog
          git checkout v11_0
          sh autoconf.sh
          ./configure --prefix=/home/runner
          make
          make install
          export PATH=$PATH:~/bin

      # - name: Compile Verilog
      #   run: |
      #     cd /home/runner/work/RiftCore/RiftCore/core/riftCore/tb
      #     iverilog -W all -o ./build/wave.iverilog -y ../ -y ../backend -y ../backend/issue -y ../backend/execute -y ../frontend -y ../element -y ../tb -I ../tb -I ../ ../tb/riftCore_TB.v 
          # git checkout 790640 
      - name: Clone Test Repository
        if: steps.cache-tools-chian.outputs.cache-hit != 'true'
        run: |
          export RISCV=~/RISCV
          export PATH=$PATH:$RISCV/bin
          cd ~
          git clone https://github.com/riscv/riscv-tests.git
          cd ./riscv-tests
          git checkout master
          git submodule update --init --recursive




      - name: Compile ISA
        if: steps.cache-tools-chian.outputs.cache-hit != 'true'
        run: |
          export RISCV=~/RISCV
          export PATH=$PATH:$RISCV/bin
          cd ~/riscv-tests
          cd ./isa
          rm Makefile
          cp /home/runner/work/RiftCore/RiftCore/tb/isa/Makefile ./Makefile
          make

      - name: Compile Dhrystone
        if: steps.cache-tools-chian.outputs.cache-hit != 'true'
        run: |
          export RISCV=~/RISCV
          export PATH=$PATH:$RISCV/bin
          cd ~/riscv-tests/benchmarks
          rm Makefile
          cp /home/runner/work/RiftCore/RiftCore/tb/dhrystone/Makefile ./
          cd ./common
          rm crt.S syscalls.c
          cp /home/runner/work/RiftCore/RiftCore/tb/dhrystone/crt.S ./
          cp /home/runner/work/RiftCore/RiftCore/tb/dhrystone/syscalls.c ./
          cd ../dhrystone
          rm dhrystone_main.c
          cp /home/runner/work/RiftCore/RiftCore/tb/dhrystone/dhrystone_main.c ./
          cd ~/riscv-tests/benchmarks
          make all

      # - name: Test simple
      #   run: |
      #     export PATH=$PATH:~/bin
      #     export RISCV=~/RISCV
      #     export PATH=$PATH:$RISCV/bin
      #     cd ~/riscv-tests/isa
      #     cp *.verilog /home/runner/work/RiftCore/RiftCore/tb/ci/
      #     cd /home/runner/work/RiftCore/RiftCore/tb
      #     export PATH=$PATH:~/bin          
      #     iverilog -Wall -o ./build/wave.iverilog  -y ../RiftChip/ -y ../RiftChip/riftCore/ -y ../RiftChip/riftCore/backend  -y ../RiftChip/riftCore/cache  -y ../RiftChip/riftCore/backend/issue  -y ../RiftChip/riftCore/backend/execute  -y ../RiftChip/riftCore/frontend  -y ../RiftChip/element -y ../RiftChip/SoC -y ../RiftChip/SoC/xilinx_IP/axi_full_Xbar -y ../RiftChip/debug -y ../tb  -I ../tb  -I ../RiftChip/  -I ../RiftChip/SoC/xilinx_IP/axi_full_Xbar ../tb/riftChip_TB.v
      #     vvp -N ./build/wave.iverilog -lxt2


      # - name: Commit
      #   run: |

      #     git checkout -- .
      #     git fetch --all
      #     git checkout gh-pages 
      #     cp ~/riscv-tests/isa/rv64ui-p-simple.verilog /home/runner/work/RiftCore/RiftCore/data/
      #     cp ~/riscv-tests/isa/rv64ui-p-simple.dump /home/runner/work/RiftCore/RiftCore/data/
      #     git config --local user.email "action@github.com"
      #     git config --local user.name "GitHub Action"
      #     git add ./data
      #     git commit -m "update ci result"




      - name: Test ISA
        run: |
          export PATH=$PATH:~/bin
          export RISCV=~/RISCV
          export PATH=$PATH:$RISCV/bin
          cd ~/riscv-tests/isa
          cp *.verilog /home/runner/work/RiftCore/RiftCore/tb/ci/
          cd /home/runner/work/RiftCore/RiftCore/tb
          export PATH=$PATH:~/bin
          python CI.py
  
      - name: Restore Data
        run: |
          cd /home/runner/work/RiftCore/RiftCore/
          git fetch --all
          git checkout origin/gh-pages data/performance.js
          cp data/performance.js tb/ci/
          cp ~/riscv-tests/benchmarks/*.verilog tb/ci/

      - name: Run Dhrystone
        run: |
          export PATH=$PATH:~/bin
          cd /home/runner/work/RiftCore/RiftCore/tb  
          python DS.py    

      - name: Commit
        run: |
          cd ~
          mkdir ./data
          mv /home/runner/work/RiftCore/RiftCore/tb/ci/*.json ~/data/
          mv /home/runner/work/RiftCore/RiftCore/tb/ci/performance.js ~/data/
          cd /home/runner/work/RiftCore/RiftCore
          git checkout -- .
          git fetch --all
          git checkout gh-pages
          rm /home/runner/work/RiftCore/RiftCore/data/*.json
          rm /home/runner/work/RiftCore/RiftCore/data/performance.js
          cp ~/data/* /home/runner/work/RiftCore/RiftCore/data/
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add ./data
          git commit -m "update ci result"
 
      - name: push
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: gh-pages
          force: true




      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.
