from pymoku.sdk import (
    RMSynth, generate_slot_bitstreams, Manifest, BarFile, CocoTB, PythonZip,
    PythonManifest, get_platform, include_support
)

ID = '{{namespace}}:{{name}}'

# include required support library
include_support()

# define local sources
vhdl = source('vhdl/{{top_entity}}.vhd')
vhdl_test = source('tests/Test{{top_entity}}.vhd')
python = source('python/*.py')
python_test = source('tests/*.py') | source('python/{{name}}.py')


# custom implementation of RMInstance to be instantiated once for each slot
# bitstreams being built.  Each instance is customised based on context and
# designer configuration.
def create_{{name}}(platform, name=None, **kwargs):
    _id = ID
    config_name = platform.name

    if name is not None:
        _id = f"{_id}:{name}"
        config_name = f"{name}-{config_name}"

    synth = RMSynth(top="{{top_entity}}",
                    vhdl=vhdl,
                    fpga_part=platform.part,
                    config_name=config_name,
                    generics=dict(
                       NUM_AIN=platform.slot_ains,
                       NUM_AOUT=platform.slot_aouts,
                       NUM_CBUFS=platform.slot_cbufs,
                       **kwargs
                       # add custom generics here, they must match {{top_entity}}.vhd
                    ))
    return Manifest(_id, generate_slot_bitstreams(platform, synth))


# define the python module to package for this slot
pyzip = PythonZip('{{name}}.zip')
pyzip.depends_on(python)

alias('python', publish(
    BarFile('{{name}}-python.bar',
        PythonManifest(ID, pyzip),
)))


# define the simulation for the Slot entity.  This should be customized to match
# the generics for the slot.  More CocoTBs can be defined to test specific
# entities.
CocoTB('test-slot',
       top='Test{{top_entity}}',
       python='test_{{name}}',
       sources=vhdl | vhdl_test | python_test,
       generics=dict(
           NUM_AIN=4,
           NUM_AOUT=4,
           NUM_CBUFS=4,
       ))
alias('tests', ['test-slot'])


# hardware/platform specific configuraitons are listed below.  Each instance of
# {{top_entity}} will create a bitstream.  Multiple bitstreams can be defined
# for each slot as long as they have a unique id.
