OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gdb/] [gdb-6.8/] [gdb-6.8.openrisc-2.1/] [sim/] [mips/] [dv-tx3904tmr.c] - Diff between revs 24 and 33

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 24 Rev 33
/*  This file is part of the program GDB, the GNU debugger.
/*  This file is part of the program GDB, the GNU debugger.
 
 
    Copyright (C) 1998, 2007, 2008 Free Software Foundation, Inc.
    Copyright (C) 1998, 2007, 2008 Free Software Foundation, Inc.
    Contributed by Cygnus Solutions.
    Contributed by Cygnus Solutions.
 
 
    This program is free software; you can redistribute it and/or modify
    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
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    (at your option) any later version.
 
 
    This program is distributed in the hope that it will be useful,
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    GNU General Public License for more details.
 
 
    You should have received a copy of the GNU General Public License
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
    */
    */
 
 
 
 
#include "sim-main.h"
#include "sim-main.h"
#include "hw-main.h"
#include "hw-main.h"
 
 
 
 
/* DEVICE
/* DEVICE
 
 
 
 
   tx3904tmr - tx3904 timer
   tx3904tmr - tx3904 timer
 
 
 
 
   DESCRIPTION
   DESCRIPTION
 
 
 
 
   Implements one tx3904 timer/counter described in the tx3904
   Implements one tx3904 timer/counter described in the tx3904
   user guide.  Three instances are required for TMR0, TMR1, and
   user guide.  Three instances are required for TMR0, TMR1, and
   TMR3 within the tx3904, at different base addresses.
   TMR3 within the tx3904, at different base addresses.
 
 
   Both internal and system clocks are synthesized as divided versions
   Both internal and system clocks are synthesized as divided versions
   of the simulator clock.
   of the simulator clock.
 
 
   There is no support for:
   There is no support for:
    - edge sensitivity of external clock
    - edge sensitivity of external clock
    - different mode restrictions for TMR0..2
    - different mode restrictions for TMR0..2
    - level interrupts (interrupts are treated as events that occur at edges)
    - level interrupts (interrupts are treated as events that occur at edges)
 
 
 
 
 
 
   PROPERTIES
   PROPERTIES
 
 
 
 
   reg <base> <length>
   reg <base> <length>
 
 
   Base of TMR control register bank.  <length> must equal 0x100.
   Base of TMR control register bank.  <length> must equal 0x100.
   Register offsets:       0: TCR: timer control  register
   Register offsets:       0: TCR: timer control  register
                           4: TISR: timer interrupt status register
                           4: TISR: timer interrupt status register
                           8: CPRA: compare register A
                           8: CPRA: compare register A
                          12: CPRB: compare register B
                          12: CPRB: compare register B
                          16: ITMR: interval timer mode register
                          16: ITMR: interval timer mode register
                          32: CCDR: divider register
                          32: CCDR: divider register
                          48: PMGR: pulse generator mode register
                          48: PMGR: pulse generator mode register
                          64: WTMR: watchdog timer mode register
                          64: WTMR: watchdog timer mode register
                         240: TRR: timer read register
                         240: TRR: timer read register
 
 
 
 
   clock <ticks>
   clock <ticks>
 
 
   Rate of timer clock signal.  This number is the number of simulator
   Rate of timer clock signal.  This number is the number of simulator
   ticks per clock signal tick.  Default 1.
   ticks per clock signal tick.  Default 1.
 
 
 
 
   ext <ticks>
   ext <ticks>
 
 
   Rate of "external input clock signal", the other clock input of the
   Rate of "external input clock signal", the other clock input of the
   timer.  It uses the same scale as above.  Default 100.
   timer.  It uses the same scale as above.  Default 100.
 
 
 
 
 
 
   PORTS
   PORTS
 
 
 
 
   int (output)
   int (output)
 
 
   Interrupt port.  An event is generated when a timer interrupt
   Interrupt port.  An event is generated when a timer interrupt
   occurs.
   occurs.
 
 
 
 
   ff (output)
   ff (output)
 
 
   Flip-flop output, corresponds to the TMFFOUT port.  An event is
   Flip-flop output, corresponds to the TMFFOUT port.  An event is
   generated when flip-flop changes value.  The integer associated
   generated when flip-flop changes value.  The integer associated
   with the event is 1/0 according to flip-flop value.
   with the event is 1/0 according to flip-flop value.
 
 
 
 
   reset (input)
   reset (input)
 
 
   Reset port.
   Reset port.
 
 
   */
   */
 
 
 
 
 
 
/* static functions */
/* static functions */
 
 
static void deliver_tx3904tmr_tick (struct hw *me, void *data);
static void deliver_tx3904tmr_tick (struct hw *me, void *data);
 
 
 
 
/* register numbers; each is one word long */
/* register numbers; each is one word long */
enum
enum
{
{
  TCR_REG = 0,
  TCR_REG = 0,
  TISR_REG = 1,
  TISR_REG = 1,
  CPRA_REG = 2,
  CPRA_REG = 2,
  CPRB_REG = 3,
  CPRB_REG = 3,
  ITMR_REG = 4,
  ITMR_REG = 4,
  CCDR_REG = 8,
  CCDR_REG = 8,
  PMGR_REG = 12,
  PMGR_REG = 12,
  WTMR_REG = 16,
  WTMR_REG = 16,
  TRR_REG = 60
  TRR_REG = 60
};
};
 
 
 
 
 
 
/* port ID's */
/* port ID's */
 
 
enum
enum
 {
 {
  RESET_PORT,
  RESET_PORT,
  INT_PORT,
  INT_PORT,
  FF_PORT
  FF_PORT
};
};
 
 
 
 
static const struct hw_port_descriptor tx3904tmr_ports[] =
static const struct hw_port_descriptor tx3904tmr_ports[] =
{
{
  { "int", INT_PORT, 0, output_port, },
  { "int", INT_PORT, 0, output_port, },
  { "ff", FF_PORT, 0, output_port, },
  { "ff", FF_PORT, 0, output_port, },
  { "reset", RESET_PORT, 0, input_port, },
  { "reset", RESET_PORT, 0, input_port, },
  { NULL, },
  { NULL, },
};
};
 
 
 
 
 
 
/* The timer/counter register internal state.  Note that we store
/* The timer/counter register internal state.  Note that we store
   state using the control register images, in host endian order. */
   state using the control register images, in host endian order. */
 
 
struct tx3904tmr {
struct tx3904tmr {
  address_word base_address; /* control register base */
  address_word base_address; /* control register base */
  unsigned_4 clock_ticks, ext_ticks; /* clock frequencies */
  unsigned_4 clock_ticks, ext_ticks; /* clock frequencies */
  signed_8 last_ticks; /* time at last deliver_*_tick call */
  signed_8 last_ticks; /* time at last deliver_*_tick call */
  signed_8 roundoff_ticks; /* sim ticks unprocessed during last tick call */
  signed_8 roundoff_ticks; /* sim ticks unprocessed during last tick call */
  int ff; /* pulse generator flip-flop value: 1/0 */
  int ff; /* pulse generator flip-flop value: 1/0 */
  struct hw_event* event; /* last scheduled event */
  struct hw_event* event; /* last scheduled event */
 
 
  unsigned_4 tcr;
  unsigned_4 tcr;
#define GET_TCR_TCE(c)      (((c)->tcr & 0x80) >> 7)
#define GET_TCR_TCE(c)      (((c)->tcr & 0x80) >> 7)
#define GET_TCR_CCDE(c)     (((c)->tcr & 0x40) >> 6)
#define GET_TCR_CCDE(c)     (((c)->tcr & 0x40) >> 6)
#define GET_TCR_CRE(c)      (((c)->tcr & 0x20) >> 5)
#define GET_TCR_CRE(c)      (((c)->tcr & 0x20) >> 5)
#define GET_TCR_CCS(c)      (((c)->tcr & 0x04) >> 2)
#define GET_TCR_CCS(c)      (((c)->tcr & 0x04) >> 2)
#define GET_TCR_TMODE(c)    (((c)->tcr & 0x03) >> 0)
#define GET_TCR_TMODE(c)    (((c)->tcr & 0x03) >> 0)
  unsigned_4 tisr;
  unsigned_4 tisr;
#define SET_TISR_TWIS(c)    ((c)->tisr |= 0x08)
#define SET_TISR_TWIS(c)    ((c)->tisr |= 0x08)
#define SET_TISR_TPIBS(c)   ((c)->tisr |= 0x04)
#define SET_TISR_TPIBS(c)   ((c)->tisr |= 0x04)
#define SET_TISR_TPIAS(c)   ((c)->tisr |= 0x02)
#define SET_TISR_TPIAS(c)   ((c)->tisr |= 0x02)
#define SET_TISR_TIIS(c)    ((c)->tisr |= 0x01)
#define SET_TISR_TIIS(c)    ((c)->tisr |= 0x01)
  unsigned_4 cpra;
  unsigned_4 cpra;
  unsigned_4 cprb;
  unsigned_4 cprb;
  unsigned_4 itmr;
  unsigned_4 itmr;
#define GET_ITMR_TIIE(c)    (((c)->itmr & 0x8000) >> 15)
#define GET_ITMR_TIIE(c)    (((c)->itmr & 0x8000) >> 15)
#define SET_ITMR_TIIE(c,v)  BLIT32((c)->itmr, 15, (v) ? 1 : 0)
#define SET_ITMR_TIIE(c,v)  BLIT32((c)->itmr, 15, (v) ? 1 : 0)
#define GET_ITMR_TZCE(c)    (((c)->itmr & 0x0001) >> 0)
#define GET_ITMR_TZCE(c)    (((c)->itmr & 0x0001) >> 0)
#define SET_ITMR_TZCE(c,v)  BLIT32((c)->itmr, 0, (v) ? 1 : 0)
#define SET_ITMR_TZCE(c,v)  BLIT32((c)->itmr, 0, (v) ? 1 : 0)
  unsigned_4 ccdr;
  unsigned_4 ccdr;
#define GET_CCDR_CDR(c)     (((c)->ccdr & 0x07) >> 0)
#define GET_CCDR_CDR(c)     (((c)->ccdr & 0x07) >> 0)
  unsigned_4 pmgr;
  unsigned_4 pmgr;
#define GET_PMGR_TPIBE(c)   (((c)->pmgr & 0x8000) >> 15)
#define GET_PMGR_TPIBE(c)   (((c)->pmgr & 0x8000) >> 15)
#define SET_PMGR_TPIBE(c,v) BLIT32((c)->pmgr, 15, (v) ? 1 : 0)
#define SET_PMGR_TPIBE(c,v) BLIT32((c)->pmgr, 15, (v) ? 1 : 0)
#define GET_PMGR_TPIAE(c)   (((c)->pmgr & 0x4000) >> 14)
#define GET_PMGR_TPIAE(c)   (((c)->pmgr & 0x4000) >> 14)
#define SET_PMGR_TPIAE(c,v) BLIT32((c)->pmgr, 14, (v) ? 1 : 0)
#define SET_PMGR_TPIAE(c,v) BLIT32((c)->pmgr, 14, (v) ? 1 : 0)
#define GET_PMGR_FFI(c)     (((c)->pmgr & 0x0001) >> 0)
#define GET_PMGR_FFI(c)     (((c)->pmgr & 0x0001) >> 0)
#define SET_PMGR_FFI(c,v)   BLIT32((c)->pmgr, 0, (v) ? 1 : 0)
#define SET_PMGR_FFI(c,v)   BLIT32((c)->pmgr, 0, (v) ? 1 : 0)
  unsigned_4 wtmr;
  unsigned_4 wtmr;
#define GET_WTMR_TWIE(c)    (((c)->wtmr & 0x8000) >> 15)
#define GET_WTMR_TWIE(c)    (((c)->wtmr & 0x8000) >> 15)
#define SET_WTMR_TWIE(c,v)  BLIT32((c)->wtmr, 15, (v) ? 1 : 0)
#define SET_WTMR_TWIE(c,v)  BLIT32((c)->wtmr, 15, (v) ? 1 : 0)
#define GET_WTMR_WDIS(c)    (((c)->wtmr & 0x0080) >> 7)
#define GET_WTMR_WDIS(c)    (((c)->wtmr & 0x0080) >> 7)
#define SET_WTMR_WDIS(c,v)  BLIT32((c)->wtmr, 7, (v) ? 1 : 0)
#define SET_WTMR_WDIS(c,v)  BLIT32((c)->wtmr, 7, (v) ? 1 : 0)
#define GET_WTMR_TWC(c)     (((c)->wtmr & 0x0001) >> 0)
#define GET_WTMR_TWC(c)     (((c)->wtmr & 0x0001) >> 0)
#define SET_WTMR_TWC(c,v)   BLIT32((c)->wtmr, 0, (v) ? 1 : 0)
#define SET_WTMR_TWC(c,v)   BLIT32((c)->wtmr, 0, (v) ? 1 : 0)
  unsigned_4 trr;
  unsigned_4 trr;
};
};
 
 
 
 
 
 
/* Finish off the partially created hw device.  Attach our local
/* Finish off the partially created hw device.  Attach our local
   callbacks.  Wire up our port names etc */
   callbacks.  Wire up our port names etc */
 
 
static hw_io_read_buffer_method tx3904tmr_io_read_buffer;
static hw_io_read_buffer_method tx3904tmr_io_read_buffer;
static hw_io_write_buffer_method tx3904tmr_io_write_buffer;
static hw_io_write_buffer_method tx3904tmr_io_write_buffer;
static hw_port_event_method tx3904tmr_port_event;
static hw_port_event_method tx3904tmr_port_event;
 
 
static void
static void
attach_tx3904tmr_regs (struct hw *me,
attach_tx3904tmr_regs (struct hw *me,
                      struct tx3904tmr *controller)
                      struct tx3904tmr *controller)
{
{
  unsigned_word attach_address;
  unsigned_word attach_address;
  int attach_space;
  int attach_space;
  unsigned attach_size;
  unsigned attach_size;
  reg_property_spec reg;
  reg_property_spec reg;
 
 
  if (hw_find_property (me, "reg") == NULL)
  if (hw_find_property (me, "reg") == NULL)
    hw_abort (me, "Missing \"reg\" property");
    hw_abort (me, "Missing \"reg\" property");
 
 
  if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  if (!hw_find_reg_array_property (me, "reg", 0, &reg))
    hw_abort (me, "\"reg\" property must contain one addr/size entry");
    hw_abort (me, "\"reg\" property must contain one addr/size entry");
 
 
  hw_unit_address_to_attach_address (hw_parent (me),
  hw_unit_address_to_attach_address (hw_parent (me),
                                     &reg.address,
                                     &reg.address,
                                     &attach_space,
                                     &attach_space,
                                     &attach_address,
                                     &attach_address,
                                     me);
                                     me);
  hw_unit_size_to_attach_size (hw_parent (me),
  hw_unit_size_to_attach_size (hw_parent (me),
                               &reg.size,
                               &reg.size,
                               &attach_size, me);
                               &attach_size, me);
 
 
  hw_attach_address (hw_parent (me), 0,
  hw_attach_address (hw_parent (me), 0,
                     attach_space, attach_address, attach_size,
                     attach_space, attach_address, attach_size,
                     me);
                     me);
 
 
  if(hw_find_property(me, "clock") != NULL)
  if(hw_find_property(me, "clock") != NULL)
    controller->clock_ticks = (unsigned_4) hw_find_integer_property(me, "clock");
    controller->clock_ticks = (unsigned_4) hw_find_integer_property(me, "clock");
 
 
  if(hw_find_property(me, "ext") != NULL)
  if(hw_find_property(me, "ext") != NULL)
    controller->ext_ticks = (unsigned_4) hw_find_integer_property(me, "ext");
    controller->ext_ticks = (unsigned_4) hw_find_integer_property(me, "ext");
 
 
  controller->base_address = attach_address;
  controller->base_address = attach_address;
}
}
 
 
 
 
static void
static void
tx3904tmr_finish (struct hw *me)
tx3904tmr_finish (struct hw *me)
{
{
  struct tx3904tmr *controller;
  struct tx3904tmr *controller;
 
 
  controller = HW_ZALLOC (me, struct tx3904tmr);
  controller = HW_ZALLOC (me, struct tx3904tmr);
  set_hw_data (me, controller);
  set_hw_data (me, controller);
  set_hw_io_read_buffer (me, tx3904tmr_io_read_buffer);
  set_hw_io_read_buffer (me, tx3904tmr_io_read_buffer);
  set_hw_io_write_buffer (me, tx3904tmr_io_write_buffer);
  set_hw_io_write_buffer (me, tx3904tmr_io_write_buffer);
  set_hw_ports (me, tx3904tmr_ports);
  set_hw_ports (me, tx3904tmr_ports);
  set_hw_port_event (me, tx3904tmr_port_event);
  set_hw_port_event (me, tx3904tmr_port_event);
 
 
  /* Preset clock dividers */
  /* Preset clock dividers */
  controller->clock_ticks = 1;
  controller->clock_ticks = 1;
  controller->ext_ticks = 100;
  controller->ext_ticks = 100;
 
 
  /* Attach ourself to our parent bus */
  /* Attach ourself to our parent bus */
  attach_tx3904tmr_regs (me, controller);
  attach_tx3904tmr_regs (me, controller);
 
 
  /* Initialize to reset state */
  /* Initialize to reset state */
  controller->tcr =
  controller->tcr =
    controller->itmr =
    controller->itmr =
    controller->ccdr =
    controller->ccdr =
    controller->pmgr =
    controller->pmgr =
    controller->wtmr =
    controller->wtmr =
    controller->tisr =
    controller->tisr =
    controller->trr = 0;
    controller->trr = 0;
  controller->cpra = controller->cprb = 0x00FFFFFF;
  controller->cpra = controller->cprb = 0x00FFFFFF;
  controller->ff = 0;
  controller->ff = 0;
  controller->last_ticks = controller->roundoff_ticks = 0;
  controller->last_ticks = controller->roundoff_ticks = 0;
  controller->event = NULL;
  controller->event = NULL;
}
}
 
 
 
 
 
 
/* An event arrives on an interrupt port */
/* An event arrives on an interrupt port */
 
 
static void
static void
tx3904tmr_port_event (struct hw *me,
tx3904tmr_port_event (struct hw *me,
                     int my_port,
                     int my_port,
                     struct hw *source,
                     struct hw *source,
                     int source_port,
                     int source_port,
                     int level)
                     int level)
{
{
  struct tx3904tmr *controller = hw_data (me);
  struct tx3904tmr *controller = hw_data (me);
 
 
  switch (my_port)
  switch (my_port)
    {
    {
    case RESET_PORT:
    case RESET_PORT:
      {
      {
        HW_TRACE ((me, "reset"));
        HW_TRACE ((me, "reset"));
 
 
        /* preset flip-flop to FFI value */
        /* preset flip-flop to FFI value */
        controller->ff = GET_PMGR_FFI(controller);
        controller->ff = GET_PMGR_FFI(controller);
 
 
        controller->tcr =
        controller->tcr =
          controller->itmr =
          controller->itmr =
          controller->ccdr =
          controller->ccdr =
          controller->pmgr =
          controller->pmgr =
          controller->wtmr =
          controller->wtmr =
          controller->tisr =
          controller->tisr =
          controller->trr = 0;
          controller->trr = 0;
        controller->cpra = controller->cprb = 0x00FFFFFF;
        controller->cpra = controller->cprb = 0x00FFFFFF;
        controller->last_ticks = controller->roundoff_ticks = 0;
        controller->last_ticks = controller->roundoff_ticks = 0;
        if(controller->event != NULL)
        if(controller->event != NULL)
          hw_event_queue_deschedule(me, controller->event);
          hw_event_queue_deschedule(me, controller->event);
        controller->event = NULL;
        controller->event = NULL;
        break;
        break;
      }
      }
 
 
    default:
    default:
      hw_abort (me, "Event on unknown port %d", my_port);
      hw_abort (me, "Event on unknown port %d", my_port);
      break;
      break;
    }
    }
}
}
 
 
 
 
/* generic read/write */
/* generic read/write */
 
 
static unsigned
static unsigned
tx3904tmr_io_read_buffer (struct hw *me,
tx3904tmr_io_read_buffer (struct hw *me,
                         void *dest,
                         void *dest,
                         int space,
                         int space,
                         unsigned_word base,
                         unsigned_word base,
                         unsigned nr_bytes)
                         unsigned nr_bytes)
{
{
  struct tx3904tmr *controller = hw_data (me);
  struct tx3904tmr *controller = hw_data (me);
  unsigned byte;
  unsigned byte;
 
 
  HW_TRACE ((me, "read 0x%08lx %d", (long) base, (int) nr_bytes));
  HW_TRACE ((me, "read 0x%08lx %d", (long) base, (int) nr_bytes));
  for (byte = 0; byte < nr_bytes; byte++)
  for (byte = 0; byte < nr_bytes; byte++)
    {
    {
      address_word address = base + byte;
      address_word address = base + byte;
      int reg_number = (address - controller->base_address) / 4;
      int reg_number = (address - controller->base_address) / 4;
      int reg_offset = 3 - (address - controller->base_address) % 4;
      int reg_offset = 3 - (address - controller->base_address) % 4;
      unsigned_4 register_value; /* in target byte order */
      unsigned_4 register_value; /* in target byte order */
 
 
      /* fill in entire register_value word */
      /* fill in entire register_value word */
      switch (reg_number)
      switch (reg_number)
        {
        {
        case TCR_REG: register_value = controller->tcr; break;
        case TCR_REG: register_value = controller->tcr; break;
        case TISR_REG: register_value = controller->tisr; break;
        case TISR_REG: register_value = controller->tisr; break;
        case CPRA_REG: register_value = controller->cpra; break;
        case CPRA_REG: register_value = controller->cpra; break;
        case CPRB_REG: register_value = controller->cprb; break;
        case CPRB_REG: register_value = controller->cprb; break;
        case ITMR_REG: register_value = controller->itmr; break;
        case ITMR_REG: register_value = controller->itmr; break;
        case CCDR_REG: register_value = controller->ccdr; break;
        case CCDR_REG: register_value = controller->ccdr; break;
        case PMGR_REG: register_value = controller->pmgr; break;
        case PMGR_REG: register_value = controller->pmgr; break;
        case WTMR_REG: register_value = controller->wtmr; break;
        case WTMR_REG: register_value = controller->wtmr; break;
        case TRR_REG: register_value = controller->trr; break;
        case TRR_REG: register_value = controller->trr; break;
        default: register_value = 0;
        default: register_value = 0;
        }
        }
 
 
      /* write requested byte out */
      /* write requested byte out */
      memcpy ((char*) dest + byte, ((char*)& register_value)+reg_offset, 1);
      memcpy ((char*) dest + byte, ((char*)& register_value)+reg_offset, 1);
    }
    }
 
 
  return nr_bytes;
  return nr_bytes;
}
}
 
 
 
 
 
 
static unsigned
static unsigned
tx3904tmr_io_write_buffer (struct hw *me,
tx3904tmr_io_write_buffer (struct hw *me,
                          const void *source,
                          const void *source,
                          int space,
                          int space,
                          unsigned_word base,
                          unsigned_word base,
                          unsigned nr_bytes)
                          unsigned nr_bytes)
{
{
  struct tx3904tmr *controller = hw_data (me);
  struct tx3904tmr *controller = hw_data (me);
  unsigned byte;
  unsigned byte;
 
 
  HW_TRACE ((me, "write 0x%08lx %d", (long) base, (int) nr_bytes));
  HW_TRACE ((me, "write 0x%08lx %d", (long) base, (int) nr_bytes));
  for (byte = 0; byte < nr_bytes; byte++)
  for (byte = 0; byte < nr_bytes; byte++)
    {
    {
      address_word address = base + byte;
      address_word address = base + byte;
      unsigned_1 write_byte = ((const char*) source)[byte];
      unsigned_1 write_byte = ((const char*) source)[byte];
      int reg_number = (address - controller->base_address) / 4;
      int reg_number = (address - controller->base_address) / 4;
      int reg_offset = 3 - (address - controller->base_address) % 4;
      int reg_offset = 3 - (address - controller->base_address) % 4;
 
 
      /* fill in entire register_value word */
      /* fill in entire register_value word */
      switch (reg_number)
      switch (reg_number)
        {
        {
        case TCR_REG:
        case TCR_REG:
          if(reg_offset == 0) /* first byte */
          if(reg_offset == 0) /* first byte */
            {
            {
              /* update register, but mask out NOP bits */
              /* update register, but mask out NOP bits */
              controller->tcr = (unsigned_4) (write_byte & 0xef);
              controller->tcr = (unsigned_4) (write_byte & 0xef);
 
 
              /* Reset counter value if timer suspended and CRE is set. */
              /* Reset counter value if timer suspended and CRE is set. */
              if(GET_TCR_TCE(controller) == 0 &&
              if(GET_TCR_TCE(controller) == 0 &&
                 GET_TCR_CRE(controller) == 1)
                 GET_TCR_CRE(controller) == 1)
                controller->trr = 0;
                controller->trr = 0;
            }
            }
          /* HW_TRACE ((me, "tcr: %08lx", (long) controller->tcr)); */
          /* HW_TRACE ((me, "tcr: %08lx", (long) controller->tcr)); */
          break;
          break;
 
 
        case ITMR_REG:
        case ITMR_REG:
          if(reg_offset == 1) /* second byte */
          if(reg_offset == 1) /* second byte */
            {
            {
              SET_ITMR_TIIE(controller, write_byte & 0x80);
              SET_ITMR_TIIE(controller, write_byte & 0x80);
            }
            }
          else if(reg_offset == 0) /* first byte */
          else if(reg_offset == 0) /* first byte */
            {
            {
              SET_ITMR_TZCE(controller, write_byte & 0x01);
              SET_ITMR_TZCE(controller, write_byte & 0x01);
            }
            }
          /* HW_TRACE ((me, "itmr: %08lx", (long) controller->itmr)); */
          /* HW_TRACE ((me, "itmr: %08lx", (long) controller->itmr)); */
          break;
          break;
 
 
        case CCDR_REG:
        case CCDR_REG:
          if(reg_offset == 0) /* first byte */
          if(reg_offset == 0) /* first byte */
            {
            {
              controller->ccdr = write_byte & 0x07;
              controller->ccdr = write_byte & 0x07;
            }
            }
          /* HW_TRACE ((me, "ccdr: %08lx", (long) controller->ccdr)); */
          /* HW_TRACE ((me, "ccdr: %08lx", (long) controller->ccdr)); */
          break;
          break;
 
 
        case PMGR_REG:
        case PMGR_REG:
          if(reg_offset == 1) /* second byte */
          if(reg_offset == 1) /* second byte */
            {
            {
              SET_PMGR_TPIBE(controller, write_byte & 0x80);
              SET_PMGR_TPIBE(controller, write_byte & 0x80);
              SET_PMGR_TPIAE(controller, write_byte & 0x40);
              SET_PMGR_TPIAE(controller, write_byte & 0x40);
            }
            }
          else if(reg_offset == 0) /* first byte */
          else if(reg_offset == 0) /* first byte */
            {
            {
              SET_PMGR_FFI(controller, write_byte & 0x01);
              SET_PMGR_FFI(controller, write_byte & 0x01);
            }
            }
          /* HW_TRACE ((me, "pmgr: %08lx", (long) controller->pmgr)); */
          /* HW_TRACE ((me, "pmgr: %08lx", (long) controller->pmgr)); */
          break;
          break;
 
 
        case WTMR_REG:
        case WTMR_REG:
          if(reg_offset == 1) /* second byte */
          if(reg_offset == 1) /* second byte */
            {
            {
              SET_WTMR_TWIE(controller, write_byte & 0x80);
              SET_WTMR_TWIE(controller, write_byte & 0x80);
            }
            }
          else if(reg_offset == 0) /* first byte */
          else if(reg_offset == 0) /* first byte */
            {
            {
              SET_WTMR_WDIS(controller, write_byte & 0x80);
              SET_WTMR_WDIS(controller, write_byte & 0x80);
              SET_WTMR_TWC(controller, write_byte & 0x01);
              SET_WTMR_TWC(controller, write_byte & 0x01);
            }
            }
          /* HW_TRACE ((me, "wtmr: %08lx", (long) controller->wtmr)); */
          /* HW_TRACE ((me, "wtmr: %08lx", (long) controller->wtmr)); */
          break;
          break;
 
 
        case TISR_REG:
        case TISR_REG:
          if(reg_offset == 0) /* first byte */
          if(reg_offset == 0) /* first byte */
            {
            {
              /* All bits must be zero in given byte, according to
              /* All bits must be zero in given byte, according to
                 spec. */
                 spec. */
 
 
              /* Send an "interrupt off" event on the interrupt port */
              /* Send an "interrupt off" event on the interrupt port */
              if(controller->tisr != 0) /* any interrupts active? */
              if(controller->tisr != 0) /* any interrupts active? */
                {
                {
                  hw_port_event(me, INT_PORT, 0);
                  hw_port_event(me, INT_PORT, 0);
                }
                }
 
 
              /* clear interrupt status register */
              /* clear interrupt status register */
              controller->tisr = 0;
              controller->tisr = 0;
            }
            }
          /* HW_TRACE ((me, "tisr: %08lx", (long) controller->tisr)); */
          /* HW_TRACE ((me, "tisr: %08lx", (long) controller->tisr)); */
          break;
          break;
 
 
        case CPRA_REG:
        case CPRA_REG:
          if(reg_offset < 3) /* first, second, or third byte */
          if(reg_offset < 3) /* first, second, or third byte */
            {
            {
              MBLIT32(controller->cpra, (reg_offset*8)+7, (reg_offset*8), write_byte);
              MBLIT32(controller->cpra, (reg_offset*8)+7, (reg_offset*8), write_byte);
            }
            }
          /* HW_TRACE ((me, "cpra: %08lx", (long) controller->cpra)); */
          /* HW_TRACE ((me, "cpra: %08lx", (long) controller->cpra)); */
          break;
          break;
 
 
        case CPRB_REG:
        case CPRB_REG:
          if(reg_offset < 3) /* first, second, or third byte */
          if(reg_offset < 3) /* first, second, or third byte */
            {
            {
              MBLIT32(controller->cprb, (reg_offset*8)+7, (reg_offset*8), write_byte);
              MBLIT32(controller->cprb, (reg_offset*8)+7, (reg_offset*8), write_byte);
            }
            }
          /* HW_TRACE ((me, "cprb: %08lx", (long) controller->cprb)); */
          /* HW_TRACE ((me, "cprb: %08lx", (long) controller->cprb)); */
          break;
          break;
 
 
        default:
        default:
          HW_TRACE ((me, "write to illegal register %d", reg_number));
          HW_TRACE ((me, "write to illegal register %d", reg_number));
        }
        }
    } /* loop over bytes */
    } /* loop over bytes */
 
 
  /* Schedule a timer event in near future, so we can increment or
  /* Schedule a timer event in near future, so we can increment or
     stop the counter, to respond to register updates. */
     stop the counter, to respond to register updates. */
  hw_event_queue_schedule(me, 1, deliver_tx3904tmr_tick, NULL);
  hw_event_queue_schedule(me, 1, deliver_tx3904tmr_tick, NULL);
 
 
  return nr_bytes;
  return nr_bytes;
}
}
 
 
 
 
 
 
/* Deliver a clock tick to the counter. */
/* Deliver a clock tick to the counter. */
static void
static void
deliver_tx3904tmr_tick (struct hw *me,
deliver_tx3904tmr_tick (struct hw *me,
                        void *data)
                        void *data)
{
{
  struct tx3904tmr *controller = hw_data (me);
  struct tx3904tmr *controller = hw_data (me);
  SIM_DESC sd = hw_system (me);
  SIM_DESC sd = hw_system (me);
  signed_8 this_ticks = sim_events_time(sd);
  signed_8 this_ticks = sim_events_time(sd);
 
 
  signed_8 warp;
  signed_8 warp;
  signed_8 divisor;
  signed_8 divisor;
  signed_8 quotient, remainder;
  signed_8 quotient, remainder;
 
 
  /* compute simulation ticks between last tick and this tick */
  /* compute simulation ticks between last tick and this tick */
  if(controller->last_ticks != 0)
  if(controller->last_ticks != 0)
    warp = this_ticks - controller->last_ticks + controller->roundoff_ticks;
    warp = this_ticks - controller->last_ticks + controller->roundoff_ticks;
  else
  else
    {
    {
      controller->last_ticks = this_ticks; /* initialize */
      controller->last_ticks = this_ticks; /* initialize */
      warp = controller->roundoff_ticks;
      warp = controller->roundoff_ticks;
    }
    }
 
 
  if(controller->event != NULL)
  if(controller->event != NULL)
    hw_event_queue_deschedule(me, controller->event);
    hw_event_queue_deschedule(me, controller->event);
  controller->event = NULL;
  controller->event = NULL;
 
 
  /* Check whether the timer ticking is enabled at this moment.  This
  /* Check whether the timer ticking is enabled at this moment.  This
     largely a function of the TCE bit, but is also slightly
     largely a function of the TCE bit, but is also slightly
     mode-dependent. */
     mode-dependent. */
  switch((int) GET_TCR_TMODE(controller))
  switch((int) GET_TCR_TMODE(controller))
    {
    {
    case 0: /* interval */
    case 0: /* interval */
      /* do not advance counter if TCE = 0 or if holding at count = CPRA */
      /* do not advance counter if TCE = 0 or if holding at count = CPRA */
      if(GET_TCR_TCE(controller) == 0 ||
      if(GET_TCR_TCE(controller) == 0 ||
         controller->trr == controller->cpra)
         controller->trr == controller->cpra)
        return;
        return;
      break;
      break;
 
 
    case 1: /* pulse generator */
    case 1: /* pulse generator */
      /* do not advance counter if TCE = 0 */
      /* do not advance counter if TCE = 0 */
      if(GET_TCR_TCE(controller) == 0)
      if(GET_TCR_TCE(controller) == 0)
        return;
        return;
      break;
      break;
 
 
    case 2: /* watchdog */
    case 2: /* watchdog */
      /* do not advance counter if TCE = 0 and WDIS = 1 */
      /* do not advance counter if TCE = 0 and WDIS = 1 */
      if(GET_TCR_TCE(controller) == 0 &&
      if(GET_TCR_TCE(controller) == 0 &&
         GET_WTMR_WDIS(controller) == 1)
         GET_WTMR_WDIS(controller) == 1)
        return;
        return;
      break;
      break;
 
 
    case 3: /* disabled */
    case 3: /* disabled */
      /* regardless of TCE, do not advance counter */
      /* regardless of TCE, do not advance counter */
      return;
      return;
    }
    }
 
 
  /* In any of the above cases that return, a subsequent register
  /* In any of the above cases that return, a subsequent register
     write will be needed to restart the timer.  A tick event is
     write will be needed to restart the timer.  A tick event is
     scheduled by any register write, so it is more efficient not to
     scheduled by any register write, so it is more efficient not to
     reschedule dummy events here. */
     reschedule dummy events here. */
 
 
 
 
  /* find appropriate divisor etc. */
  /* find appropriate divisor etc. */
  if(GET_TCR_CCS(controller) == 0) /* internal system clock */
  if(GET_TCR_CCS(controller) == 0) /* internal system clock */
    {
    {
      /* apply internal clock divider */
      /* apply internal clock divider */
      if(GET_TCR_CCDE(controller)) /* divisor circuit enabled? */
      if(GET_TCR_CCDE(controller)) /* divisor circuit enabled? */
        divisor = controller->clock_ticks * (1 << (1 + GET_CCDR_CDR(controller)));
        divisor = controller->clock_ticks * (1 << (1 + GET_CCDR_CDR(controller)));
      else
      else
        divisor = controller->clock_ticks;
        divisor = controller->clock_ticks;
    }
    }
  else
  else
    {
    {
      divisor = controller->ext_ticks;
      divisor = controller->ext_ticks;
    }
    }
 
 
  /* how many times to increase counter? */
  /* how many times to increase counter? */
  quotient = warp / divisor;
  quotient = warp / divisor;
  remainder = warp % divisor;
  remainder = warp % divisor;
 
 
  /* NOTE: If the event rescheduling code works properly, the quotient
  /* NOTE: If the event rescheduling code works properly, the quotient
     should never be larger than 1.  That is, we should receive events
     should never be larger than 1.  That is, we should receive events
     here at least as frequently as the simulated counter is supposed
     here at least as frequently as the simulated counter is supposed
     to decrement.  So the remainder (-> roundoff_ticks) will slowly
     to decrement.  So the remainder (-> roundoff_ticks) will slowly
     accumulate, with the quotient == 0.  Once in a while, quotient
     accumulate, with the quotient == 0.  Once in a while, quotient
     will equal 1. */
     will equal 1. */
 
 
  controller->roundoff_ticks = remainder;
  controller->roundoff_ticks = remainder;
  controller->last_ticks = this_ticks;
  controller->last_ticks = this_ticks;
  while(quotient > 0) /* Is it time to increment counter? */
  while(quotient > 0) /* Is it time to increment counter? */
    {
    {
      /* next 24-bit counter value */
      /* next 24-bit counter value */
      unsigned_4 next_trr = (controller->trr + 1) % (1 << 24);
      unsigned_4 next_trr = (controller->trr + 1) % (1 << 24);
      quotient --;
      quotient --;
 
 
      switch((int) GET_TCR_TMODE(controller))
      switch((int) GET_TCR_TMODE(controller))
        {
        {
        case 0: /* interval timer mode */
        case 0: /* interval timer mode */
          {
          {
            /* Current or next counter value matches CPRA value?  The
            /* Current or next counter value matches CPRA value?  The
               first case covers counter holding at maximum before
               first case covers counter holding at maximum before
               reset.  The second case covers normal counting
               reset.  The second case covers normal counting
               behavior. */
               behavior. */
            if(controller->trr == controller->cpra ||
            if(controller->trr == controller->cpra ||
               next_trr == controller->cpra)
               next_trr == controller->cpra)
              {
              {
                /* likely hold CPRA value */
                /* likely hold CPRA value */
                if(controller->trr == controller->cpra)
                if(controller->trr == controller->cpra)
                  next_trr = controller->cpra;
                  next_trr = controller->cpra;
 
 
                SET_TISR_TIIS(controller);
                SET_TISR_TIIS(controller);
 
 
                /* Signal an interrupt if it is enabled with TIIE,
                /* Signal an interrupt if it is enabled with TIIE,
                   and if we just arrived at CPRA.  Don't repeatedly
                   and if we just arrived at CPRA.  Don't repeatedly
                   interrupt if holding due to TZCE=0 */
                   interrupt if holding due to TZCE=0 */
                if(GET_ITMR_TIIE(controller) &&
                if(GET_ITMR_TIIE(controller) &&
                   next_trr != controller->trr)
                   next_trr != controller->trr)
                  {
                  {
                    hw_port_event(me, INT_PORT, 1);
                    hw_port_event(me, INT_PORT, 1);
                  }
                  }
 
 
                /* Reset counter? */
                /* Reset counter? */
                if(GET_ITMR_TZCE(controller))
                if(GET_ITMR_TZCE(controller))
                  {
                  {
                    next_trr = 0;
                    next_trr = 0;
                  }
                  }
              }
              }
          }
          }
        break;
        break;
 
 
        case 1: /* pulse generator mode */
        case 1: /* pulse generator mode */
          {
          {
            /* first trip point */
            /* first trip point */
            if(next_trr == controller->cpra)
            if(next_trr == controller->cpra)
              {
              {
                /* flip flip-flop & report */
                /* flip flip-flop & report */
                controller->ff ^= 1;
                controller->ff ^= 1;
                hw_port_event(me, FF_PORT, controller->ff);
                hw_port_event(me, FF_PORT, controller->ff);
                SET_TISR_TPIAS(controller);
                SET_TISR_TPIAS(controller);
 
 
                /* signal interrupt */
                /* signal interrupt */
                if(GET_PMGR_TPIAE(controller))
                if(GET_PMGR_TPIAE(controller))
                  {
                  {
                    hw_port_event(me, INT_PORT, 1);
                    hw_port_event(me, INT_PORT, 1);
                  }
                  }
 
 
              }
              }
            /* second trip point */
            /* second trip point */
            else if(next_trr == controller->cprb)
            else if(next_trr == controller->cprb)
              {
              {
                /* flip flip-flop & report */
                /* flip flip-flop & report */
                controller->ff ^= 1;
                controller->ff ^= 1;
                hw_port_event(me, FF_PORT, controller->ff);
                hw_port_event(me, FF_PORT, controller->ff);
                SET_TISR_TPIBS(controller);
                SET_TISR_TPIBS(controller);
 
 
                /* signal interrupt */
                /* signal interrupt */
                if(GET_PMGR_TPIBE(controller))
                if(GET_PMGR_TPIBE(controller))
                  {
                  {
                    hw_port_event(me, INT_PORT, 1);
                    hw_port_event(me, INT_PORT, 1);
                  }
                  }
 
 
                /* clear counter */
                /* clear counter */
                next_trr = 0;
                next_trr = 0;
              }
              }
          }
          }
        break;
        break;
 
 
        case 2: /* watchdog timer mode */
        case 2: /* watchdog timer mode */
          {
          {
            /* watchdog timer expiry */
            /* watchdog timer expiry */
            if(next_trr == controller->cpra)
            if(next_trr == controller->cpra)
              {
              {
                SET_TISR_TWIS(controller);
                SET_TISR_TWIS(controller);
 
 
                /* signal interrupt */
                /* signal interrupt */
                if(GET_WTMR_TWIE(controller))
                if(GET_WTMR_TWIE(controller))
                  {
                  {
                    hw_port_event(me, INT_PORT, 1);
                    hw_port_event(me, INT_PORT, 1);
                  }
                  }
 
 
                /* clear counter */
                /* clear counter */
                next_trr = 0;
                next_trr = 0;
              }
              }
          }
          }
        break;
        break;
 
 
        case 3: /* disabled */
        case 3: /* disabled */
        default:
        default:
          break;
          break;
        }
        }
 
 
      /* update counter and report */
      /* update counter and report */
      controller->trr = next_trr;
      controller->trr = next_trr;
      /* HW_TRACE ((me, "counter trr %ld tisr %lx",
      /* HW_TRACE ((me, "counter trr %ld tisr %lx",
         (long) controller->trr, (long) controller->tisr)); */
         (long) controller->trr, (long) controller->tisr)); */
    } /* end quotient loop */
    } /* end quotient loop */
 
 
  /* Reschedule a timer event in near future, so we can increment the
  /* Reschedule a timer event in near future, so we can increment the
     counter again.  Set the event about 75% of divisor time away, so
     counter again.  Set the event about 75% of divisor time away, so
     we will experience roughly 1.3 events per counter increment. */
     we will experience roughly 1.3 events per counter increment. */
  controller->event = hw_event_queue_schedule(me, divisor*3/4, deliver_tx3904tmr_tick, NULL);
  controller->event = hw_event_queue_schedule(me, divisor*3/4, deliver_tx3904tmr_tick, NULL);
}
}
 
 
 
 
 
 
 
 
const struct hw_descriptor dv_tx3904tmr_descriptor[] = {
const struct hw_descriptor dv_tx3904tmr_descriptor[] = {
  { "tx3904tmr", tx3904tmr_finish, },
  { "tx3904tmr", tx3904tmr_finish, },
  { NULL },
  { NULL },
};
};
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.