#! /bin/bash
if [ "$1" = "-h" ]; then
	echo "Copies and loads a bitfile to the spec7 over PCIe. Must be run as root"
	echo "usage: 
	$0 NAME_OF_BITFILE.bit"
	exit 1
fi
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

ID=$(lspci | grep "Xilinx Corporation Device 7022" | awk '{print $1}')
echo "removing Pcie entry."
echo 1 > /sys/bus/pci/devices/0000\:$ID/remove
sleep 0.2
echo "rescanning Pcie entries."
echo 1 > /sys/bus/pci/rescan
sleep 0.2
echo "Pcie entry loaded."
sleep 1
echo "dcache off" >/dev/ttyUSB1 
echo "copying $1 to Spec7."
cp $1 /dev/spec7_bar4
echo "loading bitfile into pCap."
echo "fpga loadb 0 0x1000000 0x1000000" >/dev/ttyUSB1
echo "done"
