From patchwork Mon Aug 6 02:16:23 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Peter A. G. Crosthwaite" X-Patchwork-Id: 175245 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 475142C0092 for ; Mon, 6 Aug 2012 12:55:51 +1000 (EST) Received: from localhost ([::1]:54499 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyCtl-0000gV-K3 for incoming@patchwork.ozlabs.org; Sun, 05 Aug 2012 22:18:25 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54853) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyCtZ-0000J1-5R for qemu-devel@nongnu.org; Sun, 05 Aug 2012 22:18:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SyCtX-0001fd-MA for qemu-devel@nongnu.org; Sun, 05 Aug 2012 22:18:13 -0400 Received: from mail-pb0-f45.google.com ([209.85.160.45]:49361) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyCtX-0001ae-Fw for qemu-devel@nongnu.org; Sun, 05 Aug 2012 22:18:11 -0400 Received: by mail-pb0-f45.google.com with SMTP id ro12so4514247pbb.4 for ; Sun, 05 Aug 2012 19:18:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :in-reply-to:references:x-gm-message-state; bh=6rnUGSI/5bYFhlzZlHIt2T7nenX3TWPwWlG2aPLOQEg=; b=FO0q+9ZquIUN5giE/Lbt2V5XXJhT8Qd5kFTqhCftwjOuSqPnsZ+WP8zX+qW9VOHKO3 57HDM04ZlMwXWU5x6EBrh0IVpfD3TB/6syOZJyS5Q/Df6h2gPGkYE0krZyiiKFTpqaG8 YoopfX0qmpZMlKtyX61GWN5gQJBw7gWox4OaJ8e3CMf7Mucj8gv7eNlLVhTWFNlRdH17 Sky43hEuTIbMTBJ36vyclkQAFvQdh4D/AZHnRf/9BLU8SbVzpkdCW0pigeyAq5ZJC6iD fOKdQixzaaCp/LiaBNqC37y3k9GwIy9MCKQTwe7mqfJkUfc57jnxlhRqI5zfUxLU5xiL T+9A== Received: by 10.68.200.8 with SMTP id jo8mr15254596pbc.148.1344219491012; Sun, 05 Aug 2012 19:18:11 -0700 (PDT) Received: from localhost ([124.148.20.9]) by mx.google.com with ESMTPS id og4sm7865727pbb.48.2012.08.05.19.18.07 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 05 Aug 2012 19:18:10 -0700 (PDT) From: "Peter A. G. Crosthwaite" To: qemu-devel@nongnu.org, paul@codesourcery.com, edgar.iglesias@gmail.com, peter.maydell@linaro.org, stefanha@gmail.com Date: Mon, 6 Aug 2012 12:16:23 +1000 Message-Id: <81b9e406e0b0292ccd3168385ab6a73a78a0ada5.1344218410.git.peter.crosthwaite@petalogix.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: References: In-Reply-To: References: X-Gm-Message-State: ALoCoQnu5RI9ctYVXZ0JnF8StVYBjVZzV4SSpMf6waIA8jnDWFEi0o+f4b8dT916bmQThEK8bN5O X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.160.45 Cc: peter.crosthwaite@petalogix.com, i.mitsyanko@samsung.com, john.williams@petalogix.com Subject: [Qemu-devel] [PATCH v5 09/15] hw: Added generic FIFO API. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Added a FIFO API that can be used to create and operate byte FIFOs. Signed-off-by: Peter A. G. Crosthwaite --- hw/Makefile.objs | 1 + hw/fifo.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/fifo.h | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 0 deletions(-) create mode 100644 hw/fifo.c create mode 100644 hw/fifo.h diff --git a/hw/Makefile.objs b/hw/Makefile.objs index 8327e55..6ba570e 100644 --- a/hw/Makefile.objs +++ b/hw/Makefile.objs @@ -15,6 +15,7 @@ hw-obj-$(CONFIG_ECC) += ecc.o hw-obj-$(CONFIG_NAND) += nand.o hw-obj-$(CONFIG_PFLASH_CFI01) += pflash_cfi01.o hw-obj-$(CONFIG_PFLASH_CFI02) += pflash_cfi02.o +hw-obj-y += fifo.o hw-obj-$(CONFIG_M48T59) += m48t59.o hw-obj-$(CONFIG_ESCC) += escc.o diff --git a/hw/fifo.c b/hw/fifo.c new file mode 100644 index 0000000..5e14e1e --- /dev/null +++ b/hw/fifo.c @@ -0,0 +1,79 @@ +/* + * Generic FIFO component, implemented as a circular buffer. + * + * Copyright (c) 2012 Peter A. G. Crosthwaite + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#include "fifo.h" + +void fifo8_create(Fifo8 *fifo, uint32_t capacity) +{ + fifo->data = g_new(uint8_t, capacity); + fifo->capacity = capacity; + fifo->head = 0; + fifo->num = 0; +} + +void fifo8_destroy(Fifo8 *fifo) +{ + g_free(fifo->data); +} + +void fifo8_push(Fifo8 *fifo, uint8_t data) +{ + if (fifo->num == fifo->capacity) { + abort(); + } + fifo->data[(fifo->head + fifo->num) % fifo->capacity] = data; + fifo->num++; +} + +uint8_t fifo8_pop(Fifo8 *fifo) +{ + uint8_t ret; + + if (fifo->num == 0) { + abort(); + } + ret = fifo->data[fifo->head++]; + fifo->head %= fifo->capacity; + fifo->num--; + return ret; +} + +void fifo8_reset(Fifo8 *fifo) +{ + fifo->num = 0; +} + +bool fifo8_is_empty(Fifo8 *fifo) +{ + return (fifo->num == 0); +} + +bool fifo8_is_full(Fifo8 *fifo) +{ + return (fifo->num == fifo->capacity); +} + +const VMStateDescription vmstate_fifo8 = { + .name = "SSISlave", + .version_id = 1, + .minimum_version_id = 1, + .minimum_version_id_old = 1, + .fields = (VMStateField[]) { + VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, 0, capacity), + VMSTATE_UINT32(head, Fifo8), + VMSTATE_UINT32(num, Fifo8), + VMSTATE_END_OF_LIST() + } +}; + diff --git a/hw/fifo.h b/hw/fifo.h new file mode 100644 index 0000000..3fb09ff --- /dev/null +++ b/hw/fifo.h @@ -0,0 +1,47 @@ +#ifndef FIFO_H +#define FIFO_H + +#include "hw.h" + +typedef struct { + /* All fields are private */ + uint8_t *data; + uint32_t capacity; + uint32_t head; + uint32_t num; +} Fifo8; + +/* create a fifo of the specified size */ + +void fifo8_create(Fifo8 *, uint32_t); + +/* cleanup a fifo */ + +void fifo8_destroy(Fifo8 *); + +/* push a data byte to the fifo. Behaviour is undefined if the fifo is full */ + +void fifo8_push(Fifo8 *, uint8_t); + +/* pop a data byte from the fifo. Behviour is undefined if the fifo is empty */ + +uint8_t fifo8_pop(Fifo8 *); + +/* reset (empty) the fifo */ + +void fifo8_reset(Fifo8 *); + +bool fifo8_is_empty(Fifo8 *); +bool fifo8_is_full(Fifo8 *); + +extern const VMStateDescription vmstate_fifo8; + +#define VMSTATE_FIFO8(_field, _state) { \ + .name = (stringify(_field)), \ + .size = sizeof(Fifo8), \ + .vmsd = &vmstate_fifo8, \ + .flags = VMS_STRUCT, \ + .offset = vmstate_offset_value(_state, _field, Fifo8), \ +} + +#endif /* FIFO_H */