/*                                       -*- mode:C++; fill-column: 100 -*-
  SFBHWPinsHost.h - Host-specific shims around pin definitions
  Copyright (C) 2008 The Regents of the University of New Mexico.  All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  USA

  $Id$
*/
/**
  \file SFBHWPinsHost.h - Host-specific shims around pin definitions
  \author David H. Ackley.  
  \date (C) 2008 All rights reserved.
  \lgpl
 */

#ifdef SFBHWPINSBOARD_H
#error SFBHWPinsHost.h and SFBHWPinsBoard.h must not be #included together!
#endif

#ifndef SFBHWPINSHOST_H
#define SFBHWPINSHOST_H

#include "SFBTypes.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * An incomplete cut at host-side simulation of the SFB hardware
 * registers.  Not currently in use.
 */
class SFBHWHostRegister {
public:
  SFBHWHostRegister(u32 address, u32 initVal = 0, u32 (*toRead)(u32,u32) = 0, u32 (*toWrite)(u32,u32) = 0) :
    reg(initVal),
    addr(address),
    doRead(toRead),
    doWrite(toWrite)
  { }

  operator u32() { 
    return doRead?doRead(addr,reg):reg; 
  }
  SFBHWHostRegister & operator=(u32 val) {
    if (doWrite) reg = doWrite(addr,val);
    else reg = val;
    return *this;
  }
  SFBHWHostRegister & operator&=(u32 val) {
    if (doWrite) reg = doWrite(addr,reg&val);
    else reg &= val;
    return *this;
  }
  SFBHWHostRegister & operator|=(u32 val) {
    if (doWrite) reg = doWrite(addr,reg|val);
    else  reg |= val;
    return *this;
  }

private:
  u32 reg;
  u32 addr;
  u32 (*doRead)(u32 addr, u32 oldval);
  u32 (*doWrite)(u32 addr, u32 newval);
};

/**
 * An incomplete cut at host-side simulation of the SFB hardware
 * registers.  Not currently in use.
 */
class SFBHWHostRegisterMap {
public:
  SFBHWHostRegister & portInMap(u32 offset, u32 port) ;
  enum { PINMODE_REGISTER_PORT = 0 };
};
extern SFBHWHostRegisterMap hostRegisters;

#define SFB_PORT_IN_MAP(offset, port) hostRegisters.portInMap(offset,port)

#define SFB_PORT_PINMODE_REGISTER(offset) hostRegisters.portInMap(offset,SFBHWHostRegisterMap::PINMODE_REGISTER_PORT)

#define REFLEX_RGB_RED_ON BUG_UNIMPLEMENTED()
#define REFLEX_RGB_RED_OFF BUG_UNIMPLEMENTED()
#define REFLEX_RGB_GREEN_ON BUG_UNIMPLEMENTED()
#define REFLEX_RGB_GREEN_OFF BUG_UNIMPLEMENTED()
#define REFLEX_RGB_BLUE_ON BUG_UNIMPLEMENTED()
#define REFLEX_RGB_BLUE_OFF BUG_UNIMPLEMENTED()

#define SFB_PORT_DIR_REGISTER(port)  SFB_PORT_IN_MAP(0x00,port)
#define SFB_PORT_MASK_REGISTER(port) SFB_PORT_IN_MAP(0x10,port)
#define SFB_PORT_PIN_REGISTER(port)  SFB_PORT_IN_MAP(0x14,port)
#define SFB_PORT_SET_REGISTER(port)  SFB_PORT_IN_MAP(0x18,port)
#define SFB_PORT_CLR_REGISTER(port)  SFB_PORT_IN_MAP(0x1C,port)

#define SFB_PORT_GET_BIT_MASK(port,bit) (SFB_PORT_PIN_REGISTER(port)&(bit))

#define SFB_PORT_SET_BIT_MASK(port,mask) (SFB_PORT_SET_REGISTER(port) |= (mask))
#define SFB_PORT_CLR_BIT_MASK(port,mask) (SFB_PORT_CLR_REGISTER(port) |= (mask))
#define SFB_PORT_WRITE_BIT_MASK(port,mask,value) \
  ((value==LOW)?SFB_PORT_CLR_BIT_MASK(port,mask):SFB_PORT_SET_BIT_MASK(port,mask))

extern void reenterBootloader() __attribute__ ((noreturn));

extern void reenterBrainstem() __attribute__ ((noreturn));

extern void hwpins_startup_initialization();

/* Low-level last ditch routine to set the rbg leds, bypassing (and
   thereby breaking) all the normal pin management stuff.  So don't
   use this unless the situation is dire enough that you don't case
   about that. */
extern void debugRGB(int rgbBits, int wait);

extern void debugRGBNum(int num, int bits, int loops);

extern void _dieOnBoard_(u32 blinkCode, const char * file, int lineno) __attribute__ ((noreturn));

#ifdef __cplusplus
}
#endif

#endif  /* SFBHWPINSHOST_H */
