def mk_counter(context, name, typ, limit, init=None, increment=None, enable=None, reset=None):
    """
    Counts from init to limit by increment. Returns the
    value of the counter and whether the limit has been
    reached or not. An enable signal and a reset signal
    may also be specified.

    Args:
        context: the context to use
        name: the unique name of this counter
        type: the type of the data stored in the counter
        limit: the limit after which the counter saturates
        init (=0): the initial value of the counter
        increment (=1): the increment value
        enable (=true): the counter is incremented only if enable is true
        reset (=false): resets the counter to the initial value

    Returns:
        counter: the current value of the counter
        Q: true iff the counter has reached saturation
    """
    if init is None:
        init = context.mk_number("0", typ)
    if increment is None:
        increment = context.mk_number("1", typ)
    if enable is None:
        enable = context.mk_true()
    if reset is None:
        reset = context.mk_false()

    counter = context.mk_latch(name, typ)
    not_q = context.mk_lt(counter, limit)
    nex = context.mk_ite(reset,\
                         init,\
                         context.mk_ite(context.mk_and(enable, not_q),\
                                        context.mk_add(counter, increment),\
                                        counter))
    context.set_latch_init_next(counter, init, nex)
    q = context.mk_not(not_q, name + '.Q')
    return counter, q