/* Copyright (C) 2009-2010 Parvez Ahmad Written by Parvez Ahmad . 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 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ module BUF (input in, output out); assign out = in; endmodule module TRIBUF(input in, enable, output out); assign out = enable ? in : 1'bz; endmodule module INV(input in, output out); assign out = ~in; endmodule module AND2 #(parameter SIZE = 2) (input [SIZE-1:0] in, output out); assign out = ∈ endmodule module AND3 #(parameter SIZE = 3) (input [SIZE-1:0] in, output out); assign out = ∈ endmodule module AND4 #(parameter SIZE = 4) (input [SIZE-1:0] in, output out); assign out = ∈ endmodule module OR2 #(parameter SIZE = 2) (input [SIZE-1:0] in, output out); assign out = |in; endmodule module OR3 #(parameter SIZE = 3) (input [SIZE-1:0] in, output out); assign out = |in; endmodule module OR4 #(parameter SIZE = 4) (input [SIZE-1:0] in, output out); assign out = |in; endmodule module NAND2 #(parameter SIZE = 2) (input [SIZE-1:0] in, output out); assign out = ~∈ endmodule module NAND3 #(parameter SIZE = 3) (input [SIZE-1:0] in, output out); assign out = ~∈ endmodule module NAND4 #(parameter SIZE = 4) (input [SIZE-1:0] in, output out); assign out = ~∈ endmodule module NOR2 #(parameter SIZE = 2) (input [SIZE-1:0] in, output out); assign out = ~|in; endmodule module NOR3 #(parameter SIZE = 3) (input [SIZE-1:0] in, output out); assign out = ~|in; endmodule module NOR4 #(parameter SIZE = 4) (input [SIZE-1:0] in, output out); assign out = ~|in; endmodule module XOR2 #(parameter SIZE = 2) (input [SIZE-1:0] in, output out); assign out = ^in; endmodule module XOR3 #(parameter SIZE = 3) (input [SIZE-1:0] in, output out); assign out = ^in; endmodule module XOR4 #(parameter SIZE = 4) (input [SIZE-1:0] in, output out); assign out = ^in; endmodule module XNOR2 #(parameter SIZE = 2) (input [SIZE-1:0] in, output out); assign out = ~^in; endmodule module XNOR3 #(parameter SIZE = 3) (input [SIZE-1:0] in, output out); assign out = ~^in; endmodule module XNOR4 #(parameter SIZE = 4) (input [SIZE-1:0] in, output out); assign out = ~^in; endmodule module DEC1 (input in, enable, output reg [1:0] out); always @(in or enable) if(!enable) out = 2'b00; else begin case (in) 1'b0 : out = 2'b01; 1'b1 : out = 2'b10; endcase end endmodule module DEC2 (input [1:0] in, input enable, output reg [3:0] out); always @(in or enable) if(!enable) out = 4'b0000; else begin case (in) 2'b00 : out = 4'b0001; 2'b01 : out = 4'b0010; 2'b10 : out = 4'b0100; 2'b11 : out = 4'b1000; endcase end endmodule module DEC3 (input [2:0] in, input enable, output reg [7:0] out); always @(in or enable) if(!enable) out = 8'b00000000; else begin case (in) 3'b000 : out = 8'b00000001; 3'b001 : out = 8'b00000010; 3'b010 : out = 8'b00000100; 3'b011 : out = 8'b00001000; 3'b100 : out = 8'b00010000; 3'b101 : out = 8'b00100000; 3'b110 : out = 8'b01000000; 3'b111 : out = 8'b10000000; endcase end endmodule module DEC4 (input [3:0] in, input enable, output reg [15:0] out); always @(in or enable) if(!enable) out = 16'b0000000000000000; else begin case (in) 4'b0000 : out = 16'b0000000000000001; 4'b0001 : out = 16'b0000000000000010; 4'b0010 : out = 16'b0000000000000100; 4'b0011 : out = 16'b0000000000001000; 4'b0100 : out = 16'b0000000000010000; 4'b0101 : out = 16'b0000000000100000; 4'b0110 : out = 16'b0000000001000000; 4'b0111 : out = 16'b0000000010000000; 4'b1000 : out = 16'b0000000100000000; 4'b1001 : out = 16'b0000001000000000; 4'b1010 : out = 16'b0000010000000000; 4'b1011 : out = 16'b0000100000000000; 4'b1100 : out = 16'b0001000000000000; 4'b1101 : out = 16'b0010000000000000; 4'b1110 : out = 16'b0100000000000000; 4'b1111 : out = 16'b1000000000000000; endcase end endmodule module DEC5 (input [4:0] in, input enable, output reg [31:0] out); always @(in or enable) if(!enable) out = 32'b00000000000000000000000000000000; else begin case (in) 5'b00000 : out = 32'b00000000000000000000000000000001; 5'b00001 : out = 32'b00000000000000000000000000000010; 5'b00010 : out = 32'b00000000000000000000000000000100; 5'b00011 : out = 32'b00000000000000000000000000001000; 5'b00100 : out = 32'b00000000000000000000000000010000; 5'b00101 : out = 32'b00000000000000000000000000100000; 5'b00110 : out = 32'b00000000000000000000000001000000; 5'b00111 : out = 32'b00000000000000000000000010000000; 5'b01000 : out = 32'b00000000000000000000000100000000; 5'b01001 : out = 32'b00000000000000000000001000000000; 5'b01010 : out = 32'b00000000000000000000010000000000; 5'b01011 : out = 32'b00000000000000000000100000000000; 5'b01100 : out = 32'b00000000000000000001000000000000; 5'b01101 : out = 32'b00000000000000000010000000000000; 5'b01110 : out = 32'b00000000000000000100000000000000; 5'b01111 : out = 32'b00000000000000001000000000000000; 5'b10000 : out = 32'b00000000000000010000000000000000; 5'b10001 : out = 32'b00000000000000100000000000000000; 5'b10010 : out = 32'b00000000000001000000000000000000; 5'b10011 : out = 32'b00000000000010000000000000000000; 5'b10100 : out = 32'b00000000000100000000000000000000; 5'b10101 : out = 32'b00000000001000000000000000000000; 5'b10110 : out = 32'b00000000010000000000000000000000; 5'b10111 : out = 32'b00000000100000000000000000000000; 5'b11000 : out = 32'b00000001000000000000000000000000; 5'b11001 : out = 32'b00000010000000000000000000000000; 5'b11010 : out = 32'b00000100000000000000000000000000; 5'b11011 : out = 32'b00001000000000000000000000000000; 5'b11100 : out = 32'b00010000000000000000000000000000; 5'b11101 : out = 32'b00100000000000000000000000000000; 5'b11110 : out = 32'b01000000000000000000000000000000; 5'b11111 : out = 32'b10000000000000000000000000000000; endcase end endmodule module DEC6 (input [5:0] in, input enable, output reg [63:0] out); always @(in or enable) if(!enable) out = 64'b0000000000000000000000000000000000000000000000000000000000000000; else begin case (in) 6'b000000 : out = 64'b0000000000000000000000000000000000000000000000000000000000000001; 6'b000001 : out = 64'b0000000000000000000000000000000000000000000000000000000000000010; 6'b000010 : out = 64'b0000000000000000000000000000000000000000000000000000000000000100; 6'b000011 : out = 64'b0000000000000000000000000000000000000000000000000000000000001000; 6'b000100 : out = 64'b0000000000000000000000000000000000000000000000000000000000010000; 6'b000101 : out = 64'b0000000000000000000000000000000000000000000000000000000000100000; 6'b000110 : out = 64'b0000000000000000000000000000000000000000000000000000000001000000; 6'b000111 : out = 64'b0000000000000000000000000000000000000000000000000000000010000000; 6'b001000 : out = 64'b0000000000000000000000000000000000000000000000000000000100000000; 6'b001001 : out = 64'b0000000000000000000000000000000000000000000000000000001000000000; 6'b001010 : out = 64'b0000000000000000000000000000000000000000000000000000010000000000; 6'b001011 : out = 64'b0000000000000000000000000000000000000000000000000000100000000000; 6'b001100 : out = 64'b0000000000000000000000000000000000000000000000000001000000000000; 6'b001101 : out = 64'b0000000000000000000000000000000000000000000000000010000000000000; 6'b001110 : out = 64'b0000000000000000000000000000000000000000000000000100000000000000; 6'b001111 : out = 64'b0000000000000000000000000000000000000000000000001000000000000000; 6'b010000 : out = 64'b0000000000000000000000000000000000000000000000010000000000000000; 6'b010001 : out = 64'b0000000000000000000000000000000000000000000000100000000000000000; 6'b010010 : out = 64'b0000000000000000000000000000000000000000000001000000000000000000; 6'b010011 : out = 64'b0000000000000000000000000000000000000000000010000000000000000000; 6'b010100 : out = 64'b0000000000000000000000000000000000000000000100000000000000000000; 6'b010101 : out = 64'b0000000000000000000000000000000000000000001000000000000000000000; 6'b010110 : out = 64'b0000000000000000000000000000000000000000010000000000000000000000; 6'b010111 : out = 64'b0000000000000000000000000000000000000000100000000000000000000000; 6'b011000 : out = 64'b0000000000000000000000000000000000000001000000000000000000000000; 6'b011001 : out = 64'b0000000000000000000000000000000000000010000000000000000000000000; 6'b011010 : out = 64'b0000000000000000000000000000000000000100000000000000000000000000; 6'b011011 : out = 64'b0000000000000000000000000000000000001000000000000000000000000000; 6'b011100 : out = 64'b0000000000000000000000000000000000010000000000000000000000000000; 6'b011101 : out = 64'b0000000000000000000000000000000000100000000000000000000000000000; 6'b011110 : out = 64'b0000000000000000000000000000000001000000000000000000000000000000; 6'b011111 : out = 64'b0000000000000000000000000000000010000000000000000000000000000000; 6'b100000 : out = 64'b0000000000000000000000000000000100000000000000000000000000000000; 6'b100001 : out = 64'b0000000000000000000000000000001000000000000000000000000000000000; 6'b100010 : out = 64'b0000000000000000000000000000010000000000000000000000000000000000; 6'b100011 : out = 64'b0000000000000000000000000000100000000000000000000000000000000000; 6'b100100 : out = 64'b0000000000000000000000000001000000000000000000000000000000000000; 6'b100101 : out = 64'b0000000000000000000000000010000000000000000000000000000000000000; 6'b100110 : out = 64'b0000000000000000000000000100000000000000000000000000000000000000; 6'b100111 : out = 64'b0000000000000000000000001000000000000000000000000000000000000000; 6'b101000 : out = 64'b0000000000000000000000010000000000000000000000000000000000000000; 6'b101001 : out = 64'b0000000000000000000000100000000000000000000000000000000000000000; 6'b101010 : out = 64'b0000000000000000000001000000000000000000000000000000000000000000; 6'b101011 : out = 64'b0000000000000000000010000000000000000000000000000000000000000000; 6'b101100 : out = 64'b0000000000000000000100000000000000000000000000000000000000000000; 6'b101101 : out = 64'b0000000000000000001000000000000000000000000000000000000000000000; 6'b101110 : out = 64'b0000000000000000010000000000000000000000000000000000000000000000; 6'b101111 : out = 64'b0000000000000000100000000000000000000000000000000000000000000000; 6'b110000 : out = 64'b0000000000000001000000000000000000000000000000000000000000000000; 6'b110001 : out = 64'b0000000000000010000000000000000000000000000000000000000000000000; 6'b110010 : out = 64'b0000000000000100000000000000000000000000000000000000000000000000; 6'b110011 : out = 64'b0000000000001000000000000000000000000000000000000000000000000000; 6'b110100 : out = 64'b0000000000010000000000000000000000000000000000000000000000000000; 6'b110101 : out = 64'b0000000000100000000000000000000000000000000000000000000000000000; 6'b110110 : out = 64'b0000000001000000000000000000000000000000000000000000000000000000; 6'b110111 : out = 64'b0000000010000000000000000000000000000000000000000000000000000000; 6'b111000 : out = 64'b0000000100000000000000000000000000000000000000000000000000000000; 6'b111001 : out = 64'b0000001000000000000000000000000000000000000000000000000000000000; 6'b111010 : out = 64'b0000010000000000000000000000000000000000000000000000000000000000; 6'b111011 : out = 64'b0000100000000000000000000000000000000000000000000000000000000000; 6'b111100 : out = 64'b0001000000000000000000000000000000000000000000000000000000000000; 6'b111101 : out = 64'b0010000000000000000000000000000000000000000000000000000000000000; 6'b111110 : out = 64'b0100000000000000000000000000000000000000000000000000000000000000; 6'b111111 : out = 64'b1000000000000000000000000000000000000000000000000000000000000000; endcase end endmodule module MUX2(input [1:0] in, input select, output reg out); always @( in or select) case (select) 0: out = in[0]; 1: out = in[1]; endcase endmodule module MUX4(input [3:0] in, input [1:0] select, output reg out); always @( in or select) case (select) 0: out = in[0]; 1: out = in[1]; 2: out = in[2]; 3: out = in[3]; endcase endmodule module MUX8(input [7:0] in, input [2:0] select, output reg out); always @( in or select) case (select) 0: out = in[0]; 1: out = in[1]; 2: out = in[2]; 3: out = in[3]; 4: out = in[4]; 5: out = in[5]; 6: out = in[6]; 7: out = in[7]; endcase endmodule module MUX16(input [15:0] in, input [3:0] select, output reg out); always @( in or select) case (select) 0: out = in[0]; 1: out = in[1]; 2: out = in[2]; 3: out = in[3]; 4: out = in[4]; 5: out = in[5]; 6: out = in[6]; 7: out = in[7]; 8: out = in[8]; 9: out = in[9]; 10: out = in[10]; 11: out = in[11]; 12: out = in[12]; 13: out = in[13]; 14: out = in[14]; 15: out = in[15]; endcase endmodule module MUX32(input [31:0] in, input [4:0] select, output reg out); always @( in or select) case (select) 0: out = in[0]; 1: out = in[1]; 2: out = in[2]; 3: out = in[3]; 4: out = in[4]; 5: out = in[5]; 6: out = in[6]; 7: out = in[7]; 8: out = in[8]; 9: out = in[9]; 10: out = in[10]; 11: out = in[11]; 12: out = in[12]; 13: out = in[13]; 14: out = in[14]; 15: out = in[15]; 16: out = in[16]; 17: out = in[17]; 18: out = in[18]; 19: out = in[19]; 20: out = in[20]; 21: out = in[21]; 22: out = in[22]; 23: out = in[23]; 24: out = in[24]; 25: out = in[25]; 26: out = in[26]; 27: out = in[27]; 28: out = in[28]; 29: out = in[29]; 30: out = in[30]; 31: out = in[31]; endcase endmodule module MUX64(input [63:0] in, input [5:0] select, output reg out); always @( in or select) case (select) 0: out = in[0]; 1: out = in[1]; 2: out = in[2]; 3: out = in[3]; 4: out = in[4]; 5: out = in[5]; 6: out = in[6]; 7: out = in[7]; 8: out = in[8]; 9: out = in[9]; 10: out = in[10]; 11: out = in[11]; 12: out = in[12]; 13: out = in[13]; 14: out = in[14];
/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011,2012,2013 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT 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 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    chmboxes.c
 * @brief   Mailboxes code.
 *
 * @addtogroup mailboxes
 * @details Asynchronous messages.
 *          <h2>Operation mode</h2>
 *          A mailbox is an asynchronous communication mechanism.<br>
 *          Operations defined for mailboxes:
 *          - <b>Post</b>: Posts a message on the mailbox in FIFO order.
 *          - <b>Post Ahead</b>: Posts a message on the mailbox with urgent
 *            priority.
 *          - <b>Fetch</b>: A message is fetched from the mailbox and removed
 *            from the queue.
 *          - <b>Reset</b>: The mailbox is emptied and all the stored messages
 *            are lost.
 *          .
 *          A message is a variable of type msg_t that is guaranteed to have
 *          the same size of and be compatible with (data) pointers (anyway an
 *          explicit cast is needed).
 *          If larger messages need to be exchanged then a pointer to a
 *          structure can be posted in the mailbox but the posting side has
 *          no predefined way to know when the message has been processed. A
 *          possible approach is to allocate memory (from a memory pool for
 *          example) from the posting side and free it on the fetching side.
 *          Another approach is to set a "done" flag into the structure pointed
 *          by the message.
 * @pre     In order to use the mailboxes APIs the @p CH_USE_MAILBOXES option
 *          must be enabled in @p chconf.h.
 * @{
 */

#include "ch.h"

#if CH_USE_MAILBOXES || defined(__DOXYGEN__)
/**
 * @brief   Initializes a Mailbox object.
 *
 * @param[out] mbp      the pointer to the Mailbox structure to be initialized
 * @param[in] buf       pointer to the messages buffer as an array of @p msg_t
 * @param[in] n         number of elements in the buffer array
 *
 * @init
 */
void chMBInit(Mailbox *mbp, msg_t *buf, cnt_t n) {

  chDbgCheck((mbp != NULL) && (buf != NULL) && (n > 0), "chMBInit");

  mbp->mb_buffer = mbp->mb_wrptr = mbp->mb_rdptr = buf;
  mbp->mb_top = &buf[n];
  chSemInit(&mbp->mb_emptysem, n);
  chSemInit(&mbp->mb_fullsem, 0);
}

/**
 * @brief   Resets a Mailbox object.
 * @details All the waiting threads are resumed with status @p RDY_RESET and
 *          the queued messages are lost.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 *
 * @api
 */
void chMBReset(Mailbox *mbp) {

  chDbgCheck(mbp != NULL, "chMBReset");

  chSysLock();
  mbp->mb_wrptr = mbp->mb_rdptr = mbp->mb_buffer;
  chSemResetI(&mbp->mb_emptysem, mbp->mb_top - mbp->mb_buffer);
  chSemResetI(&mbp->mb_fullsem, 0);
  chSchRescheduleS();
  chSysUnlock();
}

/**
 * @brief   Posts a message into a mailbox.
 * @details The invoking thread waits until a empty slot in the mailbox becomes
 *          available or the specified time runs out.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 * @param[in] msg       the message to be posted on the mailbox
 * @param[in] time      the number of ticks before the operation timeouts,
 *                      the following special values are allowed:
 *                      - @a TIME_IMMEDIATE immediate timeout.
 *                      - @a TIME_INFINITE no timeout.
 *                      .
 * @return              The operation status.
 * @retval RDY_OK       if a message has been correctly posted.
 * @retval RDY_RESET    if the mailbox has been reset while waiting.
 * @retval RDY_TIMEOUT  if the operation has timed out.
 *
 * @api
 */
msg_t chMBPost(Mailbox *mbp, msg_t msg, systime_t time) {
  msg_t rdymsg;

  chSysLock();
  rdymsg = chMBPostS(mbp, msg, time);
  chSysUnlock();
  return rdymsg;
}

/**
 * @brief   Posts a message into a mailbox.
 * @details The invoking thread waits until a empty slot in the mailbox becomes
 *          available or the specified time runs out.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 * @param[in] msg       the message to be posted on the mailbox
 * @param[in] time      the number of ticks before the operation timeouts,
 *                      the following special values are allowed:
 *                      - @a TIME_IMMEDIATE immediate timeout.
 *                      - @a TIME_INFINITE no timeout.
 *                      .
 * @return              The operation status.
 * @retval RDY_OK       if a message has been correctly posted.
 * @retval RDY_RESET    if the mailbox has been reset while waiting.
 * @retval RDY_TIMEOUT  if the operation has timed out.
 *
 * @sclass
 */
msg_t chMBPostS(Mailbox *mbp, msg_t msg, systime_t time) {
  msg_t rdymsg;

  chDbgCheckClassS();
  chDbgCheck(mbp != NULL, "chMBPostS");

  rdymsg = chSemWaitTimeoutS(&mbp->mb_emptysem, time);
  if (rdymsg == RDY_OK) {
    *mbp->mb_wrptr++ = msg;
    if (mbp->mb_wrptr >= mbp->mb_top)
      mbp->mb_wrptr = mbp->mb_buffer;
    chSemSignalI(&mbp->mb_fullsem);
    chSchRescheduleS();
  }
  return rdymsg;
}

/**
 * @brief   Posts a message into a mailbox.
 * @details This variant is non-blocking, the function returns a timeout
 *          condition if the queue is full.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 * @param[in] msg       the message to be posted on the mailbox
 * @return              The operation status.
 * @retval RDY_OK       if a message has been correctly posted.
 * @retval RDY_TIMEOUT  if the mailbox is full and the message cannot be
 *                      posted.
 *
 * @iclass
 */
msg_t chMBPostI(Mailbox *mbp, msg_t msg) {

  chDbgCheckClassI();
  chDbgCheck(mbp != NULL, "chMBPostI");

  if (chSemGetCounterI(&mbp->mb_emptysem) <= 0)
    return RDY_TIMEOUT;
  chSemFastWaitI(&mbp->mb_emptysem);
  *mbp->mb_wrptr++ = msg;
  if (mbp->mb_wrptr >= mbp->mb_top)
    mbp->mb_wrptr = mbp->mb_buffer;
  chSemSignalI(&mbp->mb_fullsem);
  return RDY_OK;
}

/**
 * @brief   Posts an high priority message into a mailbox.
 * @details The invoking thread waits until a empty slot in the mailbox becomes
 *          available or the specified time runs out.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 * @param[in] msg       the message to be posted on the mailbox
 * @param[in] time      the number of ticks before the operation timeouts,
 *                      the following special values are allowed:
 *                      - @a TIME_IMMEDIATE immediate timeout.
 *                      - @a TIME_INFINITE no timeout.
 *                      .
 * @return              The operation status.
 * @retval RDY_OK       if a message has been correctly posted.
 * @retval RDY_RESET    if the mailbox has been reset while waiting.
 * @retval RDY_TIMEOUT  if the operation has timed out.
 *
 * @api
 */
msg_t chMBPostAhead(Mailbox *mbp, msg_t msg, systime_t time) {
  msg_t rdymsg;

  chSysLock();
  rdymsg = chMBPostAheadS(mbp, msg, time);
  chSysUnlock();
  return rdymsg;
}

/**
 * @brief   Posts an high priority message into a mailbox.
 * @details The invoking thread waits until a empty slot in the mailbox becomes
 *          available or the specified time runs out.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 * @param[in] msg       the message to be posted on the mailbox
 * @param[in] time      the number of ticks before the operation timeouts,
 *                      the following special values are allowed:
 *                      - @a TIME_IMMEDIATE immediate timeout.
 *                      - @a TIME_INFINITE no timeout.
 *                      .
 * @return              The operation status.
 * @retval RDY_OK       if a message has been correctly posted.
 * @retval RDY_RESET    if the mailbox has been reset while waiting.
 * @retval RDY_TIMEOUT  if the operation has timed out.
 *
 * @sclass
 */
msg_t chMBPostAheadS(Mailbox *mbp, msg_t msg, systime_t time) {
  msg_t rdymsg;

  chDbgCheckClassS();
  chDbgCheck(mbp != NULL, "chMBPostAheadS");

  rdymsg = chSemWaitTimeoutS(&mbp->mb_emptysem, time);
  if (rdymsg == RDY_OK) {
    if (--mbp->mb_rdptr < mbp->mb_buffer)
      mbp->mb_rdptr = mbp->mb_top - 1;
    *mbp->mb_rdptr = msg;
    chSemSignalI(&mbp->mb_fullsem);
    chSchRescheduleS();
  }
  return rdymsg;
}

/**
 * @brief   Posts an high priority message into a mailbox.
 * @details This variant is non-blocking, the function returns a timeout
 *          condition if the queue is full.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 * @param[in] msg       the message to be posted on the mailbox
 * @return              The operation status.
 * @retval RDY_OK       if a message has been correctly posted.
 * @retval RDY_TIMEOUT  if the mailbox is full and the message cannot be
 *                      posted.
 *
 * @iclass
 */
msg_t chMBPostAheadI(Mailbox *mbp, msg_t msg) {

  chDbgCheckClassI();
  chDbgCheck(mbp != NULL, "chMBPostAheadI");

  if (chSemGetCounterI(&mbp->mb_emptysem) <= 0)
    return RDY_TIMEOUT;
  chSemFastWaitI(&mbp->mb_emptysem);
  if (--mbp->mb_rdptr < mbp->mb_buffer)
    mbp->mb_rdptr = mbp->mb_top - 1;
  *mbp->mb_rdptr = msg;
  chSemSignalI(&mbp->mb_fullsem);
  return RDY_OK;
}

/**
 * @brief   Retrieves a message from a mailbox.
 * @details The invoking thread waits until a message is posted in the mailbox
 *          or the specified time runs out.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 * @param[out] msgp     pointer to a message variable for the received message
 * @param[in] time      the number of ticks before the operation timeouts,
 *                      the following special values are allowed:
 *                      - @a TIME_IMMEDIATE immediate timeout.
 *                      - @a TIME_INFINITE no timeout.
 *                      .
 * @return              The operation status.
 * @retval RDY_OK       if a message has been correctly fetched.
 * @retval RDY_RESET    if the mailbox has been reset while waiting.
 * @retval RDY_TIMEOUT  if the operation has timed out.
 *
 * @api
 */
msg_t chMBFetch(Mailbox *mbp, msg_t *msgp, systime_t time) {
  msg_t rdymsg;

  chSysLock();
  rdymsg = chMBFetchS(mbp, msgp, time);
  chSysUnlock();
  return rdymsg;
}

/**
 * @brief   Retrieves a message from a mailbox.
 * @details The invoking thread waits until a message is posted in the mailbox
 *          or the specified time runs out.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 * @param[out] msgp     pointer to a message variable for the received message
 * @param[in] time      the number of ticks before the operation timeouts,
 *                      the following special values are allowed:
 *                      - @a TIME_IMMEDIATE immediate timeout.
 *                      - @a TIME_INFINITE no timeout.
 *                      .
 * @return              The operation status.
 * @retval RDY_OK       if a message has been correctly fetched.
 * @retval RDY_RESET    if the mailbox has been reset while waiting.
 * @retval RDY_TIMEOUT  if the operation has timed out.
 *
 * @sclass
 */
msg_t chMBFetchS(Mailbox *mbp, msg_t *msgp, systime_t time) {
  msg_t rdymsg;

  chDbgCheckClassS();
  chDbgCheck((mbp != NULL) && (msgp != NULL), "chMBFetchS");

  rdymsg = chSemWaitTimeoutS(&mbp->mb_fullsem, time);
  if (rdymsg == RDY_OK) {
    *msgp = *mbp->mb_rdptr++;
    if (mbp->mb_rdptr >= mbp->mb_top)
      mbp->mb_rdptr = mbp->mb_buffer;
    chSemSignalI(&mbp->mb_emptysem);
    chSchRescheduleS();
  }
  return rdymsg;
}

/**
 * @brief   Retrieves a message from a mailbox.
 * @details This variant is non-blocking, the function returns a timeout
 *          condition if the queue is empty.
 *
 * @param[in] mbp       the pointer to an initialized Mailbox object
 * @param[out] msgp     pointer to a message variable for the received message
 * @return              The operation status.
 * @retval RDY_OK       if a message has been correctly fetched.
 * @retval RDY_TIMEOUT  if the mailbox is empty and a message cannot be
 *                      fetched.
 *
 * @iclass
 */
msg_t chMBFetchI(Mailbox *mbp, msg_t *msgp) {

  chDbgCheckClassI();
  chDbgCheck((mbp != NULL) && (msgp != NULL), "chMBFetchI");

  if (chSemGetCounterI(&mbp->mb_fullsem) <= 0)
    return RDY_TIMEOUT;
  chSemFastWaitI(&mbp->mb_fullsem);
  *msgp = *mbp->mb_rdptr++;
  if (mbp->mb_rdptr >= mbp->mb_top)
    mbp->mb_rdptr = mbp->mb_buffer;
  chSemSignalI(&mbp->mb_emptysem);
  return RDY_OK;
}
#endif /* CH_USE_MAILBOXES */

/** @} */