async def initial_hal_test(dut, debug=True):
    """Example of using the software HAL against cosim testbench"""

    cocotb.fork(Clock(dut.clk, 5, units='ns').start())
    await reset(dut)

    # Create the avalon master and direct our HAL calls to that
    master = AvalonMaster(dut, "csr", dut.clk)
    if debug:
        master.log.setLevel(logging.DEBUG)

    @cocotb.function
    async def read(address):
        master.log.debug("External source: reading address 0x%08X" % address)
        value = await master.read(address)
        master.log.debug("Reading complete: got value 0x%08x" % value)
        return value

    @cocotb.function
    async def write(address, value):
        master.log.debug("Write called for 0x%08X -> %d" % (address, value))
        await master.write(address, value)
        master.log.debug("Write complete")

    io_module.set_write_function(write)
    io_module.set_read_function(read)

    dut._log.info("READ/WRITE functions set up, initialising HAL")

    state = hal.endian_swapper_init(0)

    # Check the actual value
    if dut.byteswapping.value:
        raise TestFailure("Byteswapping is enabled but haven't configured DUT")

    await cocotb.external(hal.endian_swapper_enable)(state)

    await ReadOnly()

    if not dut.byteswapping.value:
        raise TestFailure("Byteswapping wasn't enabled after calling "
                          "endian_swapper_enable")

    dut._log.info(
        "HAL call endian_swapper_enable successfully enabled the DUT")
def initial_hal_test(dut, debug=True):
    """Example of using the software HAL against cosim testbench"""

    cocotb.fork(Clock(dut.clk, 5000).start())
    yield reset(dut)

    # Create the avalon master and direct our HAL calls to that
    master = AvalonMaster(dut, "csr", dut.clk)
    if debug:
        master.log.setLevel(logging.DEBUG)

    @cocotb.function
    def read(address):
        master.log.debug("External source: reading address 0x%08X" % address)
        value = yield master.read(address)
        master.log.debug("Reading complete: got value 0x%08x" % value)
        raise ReturnValue(value)

    @cocotb.function
    def write(address, value):
        master.log.debug("Write called for 0x%08X -> %d" % (address, value))
        yield master.write(address, value)
        master.log.debug("Write complete")

    io_module.set_write_function(write)
    io_module.set_read_function(read)

    dut._log.info("READ/WRITE functions set up, initialising HAL")

    state = hal.endian_swapper_init(0)

    # Check the actual value
    if dut.byteswapping.value:
        raise TestFailure("Byteswapping is enabled but haven't configured DUT")

    yield cocotb.external(hal.endian_swapper_enable)(state)

    if not dut.byteswapping.value:
        raise TestFailure("Byteswapping wasn't enabled after calling "
                          "endian_swapper_enable")

    dut._log.info("HAL call endian_swapper_enable successfully enabled the DUT")