import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

x = np.arange(0, 100)

def square(x):
    return (x % 50) < 25

plt.plot(x, square(x))

"""
Explanation: Square Wave Generator
A square wave is a periodic waveform that alternates between two discrete values. 
Here's an example square wave that is generated using a simple Python function.
End of explanation
"""


import magma as m
m.set_mantle_target("ice40")
from loam.boards.icestick import IceStick

icestick = IceStick()
icestick.Clock.on()
icestick.J3[0].output().on()

"""
Explanation: To implement our square wave in magma, we start by importing the IceStick module from loam. We instance the IceStick and turn on the Clock and J3[0] (configured as an output).
End of explanation
"""


import magma as m
import mantle
main = icestick.main()

# TODO: Define main

m.compile('build/square', main)

%%bash
cd build
cat square.pcf
yosys -q -p 'synth_ice40 -top main -blif square.blif' square.v
arachne-pnr -q -d 1k -o square.txt -p square.pcf square.blif 
icepack square.txt square.bin
iceprog square.bin

"""
Explanation: Create a magma circuit that will produce a digital square wave on the J3[0] GPIO pin.
End of explanation
"""


