#!/bin/bash

echo "Compile firmware..."

cd tinyblinky
./assemble
cd ..

cd hello_gcc
./compile
cd ..

cp tinyblinky/tinyblinky.hex icestorm/tinyblinky.hex
cp hello_gcc/hello_gcc.hex   icestorm/hello_gcc.hex

mkdir -p build
rm -f build/*

echo "Synthesize design..."
yosys  -q -p "synth_ice40 -top top -json build/nandland.json -blif build/nandland.blif -relut -no-rw-check" icestorm/nandland.v

echo "Place and Route..."
nextpnr-ice40 --freq 50 --hx1k --package vq100 --asc build/nandland.txt --pcf icestorm/nandland.pcf --json build/nandland.json --ignore-loops --pcf-allow-unconstrained

echo "Create Bitstream..."
icepack build/nandland.txt nandland.bin

rm -f build/*

echo "Finished."
