Esempio n. 1
0
void protoss_stop() {
    if(trace_going || watch_going) {
        begin_debug(); // interrupts disabled
        read_debug(197);
        uint32_t dbgdscr = read_debug(34);
        dbgdscr |= 0x8000; // turn on debug
        write_debug(34, dbgdscr);
        for(int i = 0; i < 16; i++) {
            // bcr and wcr
            write_debug(80 + i, 0);
            write_debug(112 + i, 0);
        }

        dbgdscr = read_debug(34);
        dbgdscr &= ~0x8000;
        write_debug(34, dbgdscr);
        end_debug();
    }

    if(trace_going) {
        trace_going = false;
    }
    
    watch_going = false;

#ifdef WATCH{OINTS
    if(ter_patched) {
        memset(debug_stuff, 0, sizeof(debug_stuff));
        old_ie = ml_set_interrupts_enabled(0);

        for(int i = 0; i < 4; i++) ter_patch_loc[i] = ter_orig[i];
    
        flush_cache(ter_patch_loc, sizeof(ter_orig));

        ter_patched = false;

        ml_set_interrupts_enabled(old_ie);
    }
#endif

    if(prefetch_saved) {
        vector_base()[3+8] = prefetch_saved;
        prefetch_saved = NULL;
    }

    if(data_saved) {
        vector_base()[4+8] = data_saved;
        data_saved = NULL;
    }
}
Esempio n. 2
0
static void got_fpe(int z)
{
#ifdef DEBUG_CONTEXT
  write_debug();
#endif
  fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  exit(1);		/* for GCC noreturn */
}
Esempio n. 3
0
int protoss_write_debug_reg(uint32_t reg, uint32_t val) {
    //IOLog("%d %d\n", reg, val);
    begin_debug();
    read_debug(197);
    write_debug(reg, val);
    end_debug();
    return 0;
}
Esempio n. 4
0
/*
 * Function: write_debug_test
 * Description:
 * 	A test routine for the write_debug routine.
 * Scope:       PUBLIC
 * Parameters:  none
 * Return:	none
 * Globals:	none
 * Notes:
 *	Output will look something like:
 *
 * Debug MYNAME -- "common_post.c", line 618
 *
 * Debug MYNAME -- "common_post.c", line 620
 *
 * Debug MYNAME -- "common_post.c", line 622
 *         Entering 33
 *         Leaving 45
 *
 * Debug MYNAME -- "common_post.c", line 626
 *         Leaving 45
 *         x = 32
 *         y = 102
 *
 * Debug MYNAME -- "common_post.c", line 634
 *         - item 0
 *                   item 0
 *                   item 1
 *                   item 2
 *         - item 1
 *                   item 0
 *                   item 1
 *                   item 2
 *         - item 2
 *                   item 0
 *                   item 1
 *                   item 2
 *         - item 3
 *                   item 0
 *                   item 1
 *                   item 2
 *         - item 4
 *                   item 0
 *                   item 1
 *                   item 2
 *
 * Debug MYNAME -- "common_post.c", line 645
 *
 */
void
write_debug_test(void)
{
	int i;
	int j;
	u_char dest;

	dest = LOGSCR;

	/* locations with no text */
	write_debug(dest, 1, "MYNAME", DEBUG_LOC,
		LEVEL1, NULL);
	write_debug(dest, 1, "MYNAME", DEBUG_LOC,
		LEVEL1, NULL);

	/* locations with some text */
	write_debug(dest, 1, "MYNAME", DEBUG_LOC,
		LEVEL1, "%s %d", "Entering", 33);

	/* no location with some text */
	write_debug(dest, 1, DEBUG_LOC_NOHD,
		LEVEL1, "%s %d", "Leaving", 45);

	/* location with text */
	write_debug(dest, 1, "MYNAME", DEBUG_LOC,
		LEVEL1, "%s %d", "Leaving", 45);

	/* no location with text */
	write_debug(dest, 1, NULL, DEBUG_LOC,
		LEVEL1, "x = %d", 32);
	write_debug(dest, 1, NULL, DEBUG_LOC,
		LEVEL1, "y = %d", 102);

	/* test list items */
	write_debug(dest, 1, "MYNAME", DEBUG_LOC,
		LEVEL1, NULL);
	for (i = 0; i < 5; i++) {
		write_debug(dest, 1, DEBUG_LOC_NOHD,
			LEVEL1|LISTITEM, "item %d", i);
		for (j = 0; j < 3; j++) {
			write_debug(dest, 1, DEBUG_LOC_NOHD,
				LEVEL2|LISTITEM|CONTINUE, "item %d", j);
		}
	}
}
Esempio n. 5
0
/* Called from the Assert macro.
 */
void eggAssert(const char *file, int line, const char *module)
{
  write_debug();
  if (!module)
    putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
  else
    putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
  fatal("ASSERT FAILED -- CRASHING!", 1);
}
Esempio n. 6
0
static void got_abort(int z)
{
  signal(SIGABRT, SIG_DFL);
  write_debug();
  fatal("GOT SIGABRT -- CRASHING!", 1);
#ifdef DEBUG
  raise(SIGABRT);
#else
  exit(1);
#endif /* DEBUG */
}
Esempio n. 7
0
static void got_fpe(int z)
{
  signal(SIGFPE, SIG_DFL);
  write_debug();
  fatal("FLOATING POINT ERROR -- CRASHING!", 1);
#ifdef DEBUG
  raise(SIGFPE);
#else
  exit(1);
#endif /* DEBUG */
}
Esempio n. 8
0
static void got_bus(int z)
{
  signal(SIGBUS, SIG_DFL);
  write_debug();
  fatal("BUS ERROR -- CRASHING!", 1);
#ifdef DEBUG
  raise(SIGBUS);
#else
  exit(1);
#endif /* DEBUG */
}
Esempio n. 9
0
static void got_bus(int z)
{
  write_debug();
  fatal("BUS ERROR -- CRASHING!", 1);
#ifdef SA_RESETHAND
  kill(getpid(), SIGBUS);
#else
  bg_send_quit(BG_ABORT);
  exit(1);
#endif
}
Esempio n. 10
0
static void got_segv(int z)
{
  write_debug();
  fatal("SEGMENT VIOLATION -- CRASHING!", 1);
#ifdef SA_RESETHAND
  kill(getpid(), SIGSEGV);
#else
  bg_send_quit(BG_ABORT);
  exit(1);
#endif
}
Esempio n. 11
0
static void got_segv(int z)
{
  signal(SIGSEGV, SIG_DFL);
  write_debug();
  fatal("SEGMENT VIOLATION -- CRASHING!", 1);
#ifdef DEBUG
  raise(SIGSEGV);
#else
  exit(1);
#endif /* DEBUG */
}
Esempio n. 12
0
void memory_write(struct _asm_context *asm_context, uint32_t address, unsigned char data, int line)
{
  if (address >= asm_context->memory.size)
  {
    printf("Warning: Data write address %d overran %d byte boundary at %s:%d\n", address, asm_context->memory.size, asm_context->filename, asm_context->line);
    return;
  }

  write_byte(&asm_context->memory, address, data);
  write_debug(&asm_context->memory, address, line);
}
Esempio n. 13
0
void debug_free(void *block, const char *file, int line)
{
	if (allocated_cnt && mem_mutex) {
		cds_mutex_lock(mem_mutex);
		(*allocated_cnt)--;
		cds_mutex_unlock(mem_mutex);
	}
#ifdef SER
	shm_free(block);
#else
	free(block);
#endif
	write_debug("FREE %p from %s(%d)\n", block, file, line);
}
Esempio n. 14
0
void print_debug(unsigned int flag, const char *messg, ...)
{
  va_list args;

  if (!debug_data[0].state)
    return;

  if (messg == NULL)
    return;

  va_start(args, messg);
  write_debug(flag, messg, args);
  va_end(args);
}
Esempio n. 15
0
static void got_segv(int z)
{
  segfaulted = 1;
  alarm(0);		/* dont let anything jump out of this signal! */
  signal(SIGSEGV, SIG_DFL);
  /* stackdump(0); */
#ifdef DEBUG_CONTEXT
  write_debug();
#endif
  fatal("SEGMENT VIOLATION -- CRASHING!", 1);
#ifdef DEBUG
  raise(SIGSEGV);
#else
  exit(1);
#endif /* DEBUG */
}
Esempio n. 16
0
void *debug_malloc(int size, const char *file, int line)
{
	void *m = NULL;
	if (allocated_cnt && mem_mutex) {
		cds_mutex_lock(mem_mutex);
		(*allocated_cnt)++;
		cds_mutex_unlock(mem_mutex);
	}
#ifdef SER
	m = shm_malloc(size);
#else
	m = malloc(size);
#endif
	write_debug("ALLOC %p size %u from %s(%d)\n", m, size, file, line);
	/* LOG(L_INFO, "%p\n", m); */
	return m;
}
Esempio n. 17
0
/* Got ILL signal -- log context and continue
 */
static void got_ill(int z)
{
  write_debug(0);
}
Esempio n. 18
0
static void end_debug() {
    write_debug(1004, 0);
    //write_debug(192, 0xc5acce55);
    ml_set_interrupts_enabled(old_ie);
}
Esempio n. 19
0
static void begin_debug() {
    //IOLog("begin_debug\n");
    old_ie = ml_set_interrupts_enabled(0);
    //write_debug(192, 0);
    write_debug(1004, 0xc5acce55);
}
Esempio n. 20
0
void memory_debug_line_set_m(struct _memory *memory, uint32_t address, int value)
{
  write_debug(memory, address, value);
}
Esempio n. 21
0
void memory_write_inc(struct _asm_context *asm_context, unsigned char data, int line)
{
  write_byte(&asm_context->memory, asm_context->address, data);
  write_debug(&asm_context->memory, asm_context->address, line);
  asm_context->address++;
}
Esempio n. 22
0
void write_debug(std::string& str)
{
	write_debug(str.c_str());
}
Esempio n. 23
0
int protoss_go() {
    if(trace_going || watch_going) {
        IOLog("protoss_go: already enabled\n");
        return -1;
    }
    
    if(vector_base()[3] != (void *) 0xe59ff018) {
        return -1;
    }
    
    trace_going = true;

    if(!trace_start) trace_start = IOMalloc(num_trace_entries * sizeof(struct trace_entry));
    memset(trace_start, 0, (num_trace_entries - 1) * sizeof(struct trace_entry));
    memset(&trace_start[num_trace_entries - 1], 0xff, sizeof(struct trace_entry));
    trace_ptr = &trace_start[1];

    // We can't ever branch to 80xxxxxx, so overwrite it here
    prefetch_saved = vector_base()[3+8];
    vector_base()[3+8] = (void *) trace_prefetch_handler;

    union dbgbcr dbgbcr5, dbgbcr4;
    dbgbcr5.val = dbgbcr4.val = 0;
    uint32_t dbgbvr5, dbgbvr4;

    dbgbcr5.z1 = 0;
    dbgbcr5.address_range_mask = 0;
    dbgbcr5.z2 = 0;
    dbgbcr5.dbgbvr_match_or_mismatch = 1; // mismatch
    dbgbcr5.dbgbvr_iva_or_context_id = 0; // IVA
    dbgbcr5.dbgbvr_unlinked_or_linked = 1; // linked
    dbgbcr5.linked_brp_num = 4;
    dbgbcr5.security_state_control = 0; // match in either security state
    dbgbcr5.byte_address_select = 0xf; // I don't understand why this exists.
    dbgbcr5.z4 = 0;
    dbgbcr5.privileged_mode_control = 0; // user, system, svc *but not* exception
    dbgbcr5.breakpoint_enable = 1; // woo
    
    dbgbvr5 = 0xdeadbeec; // asm will fill this in for single stepping
    
    dbgbcr4.z1 = 0;
    dbgbcr4.address_range_mask = 0; // exact (but it's step-two for thumb :()
    dbgbcr4.z2 = 0;
    dbgbcr4.dbgbvr_match_or_mismatch = 0; // match
    dbgbcr4.dbgbvr_iva_or_context_id = 1; // Context ID
    dbgbcr4.dbgbvr_unlinked_or_linked = 1;
    dbgbcr4.linked_brp_num = 5;
    dbgbcr4.security_state_control = 0;
    dbgbcr4.byte_address_select = 0xf;
    dbgbcr4.z4 = 0;
    dbgbcr4.privileged_mode_control = 0;
    dbgbcr4.breakpoint_enable = 1;

    IOLog("%08x %08x\n", dbgbcr5.val, dbgbcr4.val);

    // get current context ID
    asm("mrc p15, 0, %0, c13, c0, 1" :"=r"(dbgbvr4) :);
    
    begin_debug(); // interrupts disabled
    read_debug(197);
    uint32_t dbgdscr = read_debug(34);
    dbgdscr |= 0x8000; // turn on debug
    write_debug(34, dbgdscr);
    for(int i = 0; i < 16; i++) {
        write_debug(80 + i, 0);
        write_debug(112 + i, 0);
    }
    for(int i = 0; i < 16; i++) {
        uint32_t bvr = 0, bcr = 0;
        if(i == 4) {
            bvr = dbgbvr4;
            bcr = dbgbcr4.val;
        } else if(i == 5) {
            bvr = dbgbvr5;
            bcr = dbgbcr5.val;
        }
        write_debug(64 + i, bvr);
        write_debug(80 + i, bcr);
        write_debug(112 + i, read_debug(112 + i));
    }
    end_debug();
    
    return 0;
}
Esempio n. 24
0
static void got_fpe(int z)
{
  write_debug();
  fatal("FLOATING POINT ERROR -- CRASHING!", 0);
}
Esempio n. 25
0
void memory_debug_line_set(struct _asm_context *asm_context, uint32_t address, int value)
{
  write_debug(&asm_context->memory, address, value);
}
Esempio n. 26
0
void full_energy(int                  rec,
                 atmos_data_struct   *atmos,
                 soil_con_struct     *soil_con,
                 veg_con_struct      *veg_con,
                 dist_prcp_struct    *prcp,
                 dmy_struct          *dmy,
                 global_param_struct *gp,
                 int                  gridcell,
                 char                 NEWCELL)
/**********************************************************************
	full_energy	Keith Cherkauer		January 8, 1997

  This subroutine controls the model core, it solves both the energy
  and water balance models, as well as frozen soils.  

  modifications:
  07-98 restructured to fix problems with distributed precipitation, 
        and to add the ability to solve the snow model at different 
	elevation bands within a single grid cell.                 KAC
  01-19-00 modified to work with the new atmosphere data structure 
           implemented when the radiation forcing routines were 
	   updated.  Also modified to use the new simplified 
	   soil moisture storage for the frozen soil algorithm.    KAC
  28-Sep-04 Added aero_resist_used to store the aerodynamic resistance
	    actually used in flux calculations.				TJB
  2006-Sep-14 Added computation of soil wetness and root zone soil
	      moisture, as part of ALMA-compliant input/output.		TJB
  2007-Oct-22 Changed ice0 from a scalar to an array.  Previously,
	      when options.SNOW_BAND > 1, the value of ice0 computed
	      for earlier bands was always overwritten by the value
	      of ice0 computed for the final band (even if the final
	      band had 0 area).						JS via TJB

**********************************************************************/
{
  extern veg_lib_struct *veg_lib;
  extern option_struct   options;
#if LINK_DEBUG
  extern debug_struct    debug;
#endif

  char                   overstory;
  char                   SOLVE_SURF_ENERGY;
  int                    i, j, k;
  int                    lidx;
  int                    Ndist;
  int                    dist;
  int                    iveg;
  int                    Nveg;
  int                    veg_class;
  int                    band;
  int                    Nbands;
  int                    hour;
  double                 out_prec[2*MAX_BANDS];
  double                 out_rain[2*MAX_BANDS];
  double                 out_snow[2*MAX_BANDS];
  double                 tmp_surf_temp;
  double                 last_T1;
  double                 out_short=0;
  double                 inshort;
  double                 inlong;
  double                 dp;
  double                 ice0[MAX_BANDS];
  double                 moist;
  double                 surf_atten;
  double                 Tsurf;
  double                 Tgrnd;
  double                 Tend_surf;
  double                 Tend_grnd;
  double                 rainfall[2]; 
  double                 wind_h;
  double                 height;
  double                 displacement;
  double                 roughness;
  double                 ref_height;
  double                 Cv;
  double                 Le;
  double                 Ls;
  double                 Evap;
  double                 Melt[2*MAX_BANDS];
  double                 bare_albedo;
  double                 snow_inflow[MAX_BANDS];
  double                 step_rad;
  double                 step_net_short;
  double                 tmp_aero_resist;
  double                 tmp_throughfall[2][MAX_BANDS];
  double                 tmp_wind[3];
  double                 tmp_melt[MAX_BANDS*2];
  double                 tmp_vapor_flux[MAX_BANDS];
  double                 tmp_canopy_vapor_flux[MAX_BANDS];
  double                 tmp_canopyevap[2][MAX_BANDS];
  double                 tmp_snow_energy;
  double                 tmp_Wdew[2];
  double                 tmp_mu;
  double                 tmp_layerevap[2][MAX_BANDS][MAX_LAYERS];
  double                 tmp_Tmin;
  double                 tmp_total_moist_wet;
  double                 tmp_total_moist_dry;
  double                 gauge_correction[2];
  layer_data_struct     *tmp_layer[2];
  veg_var_struct         tmp_veg_var[2];
  cell_data_struct    ***cell;
  veg_var_struct      ***veg_var;
  energy_bal_struct    **energy;
  energy_bal_struct     *ptr_energy;
  snow_data_struct     **snow;
  snow_data_struct      *tmp_snow;
  veg_var_struct        *tmp_veg[2];
  veg_var_struct        *wet_veg_var;
  veg_var_struct        *dry_veg_var;
  veg_var_struct         empty_veg_var;

  /* set local pointers */
  cell    = prcp->cell;
  veg_var = prcp->veg_var;
  snow    = prcp->snow;
  energy  = prcp->energy;

  /* set variables for distributed precipitation */
  if(options.DIST_PRCP) 
    Ndist = 2;
  else 
    Ndist = 1;
  Nbands = options.SNOW_BAND;

  /* Set number of vegetation types */
  Nveg      = veg_con[0].vegetat_type_num;

  /** Set Damping Depth **/
  dp        = soil_con->dp;

  /* allocate memory for temporary storage structures */
  if(options.FULL_ENERGY || options.FROZEN_SOIL) {
    for(i=0; i<2; i++) {
      tmp_layer[i] = (layer_data_struct *)calloc((options.Nlayer),
						 sizeof(layer_data_struct));
    }
  }
      
  /* Compute gauge undercatch correction factors 
     - this assumes that the gauge is free of vegetation effects, so gauge
     correction is constant for the entire grid cell */
  if( options.CORRPREC && atmos->prec[NR] > 0 ) 
    correct_precip(gauge_correction, atmos->wind[NR], gp->wind_h, 
		   soil_con->rough, soil_con->snow_rough);
  else {
    gauge_correction[0] = 1;
    gauge_correction[1] = 1;
  }
  atmos->out_prec = 0;
  atmos->out_rain = 0;
  atmos->out_snow = 0;

  /**************************************************
    Solve Energy and/or Water Balance for Each
    Vegetation Type
  **************************************************/
  for(iveg = 0; iveg <= Nveg; iveg++){

    /** Solve Veg Type only if Coverage Greater than 0% **/
    if ((iveg <  Nveg && veg_con[iveg].Cv  > 0.) || 
	(iveg == Nveg && veg_con[0].Cv_sum < 1.)) {

      if ( iveg < Nveg ) Cv = veg_con[iveg].Cv;
      else Cv = 1. - veg_con[0].Cv_sum;

      /**************************************************
        Initialize Model Parameters
      **************************************************/

      /* Initialize energy balance variables */
      for(band = 0; band < Nbands; band++) {
	if(soil_con->AreaFract[band] > 0) {
	  energy[iveg][band].shortwave = 0;
	  energy[iveg][band].longwave  = 0.;
	}
      }

      /* Initialize snow variables */
      for(band=0; band<Nbands; band++) {
	if(soil_con->AreaFract[band] > 0) {
	  snow[iveg][band].vapor_flux        = 0.;
	  snow[iveg][band].canopy_vapor_flux = 0.;
	  snow_inflow[band]                  = 0.;
	  Melt[band*2]                       = 0.;
	}
      }

      /* Initialize precipitation storage */
      for ( j = 0; j < 2*MAX_BANDS; j++ ) {
        out_prec[j] = 0;
        out_rain[j] = 0;
        out_snow[j] = 0;
      }
    
      /** Define vegetation class number **/
      if (iveg < Nveg) 
	veg_class = veg_con[iveg].veg_class;
      else 
	veg_class = 0;
      if ( iveg < Nveg ) wind_h = veg_lib[veg_class].wind_h;
      else wind_h = gp->wind_h;

      /** Compute Surface Attenuation due to Vegetation Coverage **/
      if(iveg < Nveg)
	surf_atten = exp(-veg_lib[veg_class].rad_atten 
			 * veg_lib[veg_class].LAI[dmy[rec].month-1]);
      else 
	surf_atten = 1.;
        
      /* Initialize soil thermal properties for the top two layers */
      if(options.FULL_ENERGY || options.FROZEN_SOIL) {
	prepare_full_energy(iveg, Nveg, options.Nnode, prcp, 
			    soil_con, &moist, ice0);
      }

      /** Compute Bare Soil (free of snow) Albedo **/
      if(iveg != Nveg) 
	bare_albedo = veg_lib[veg_class].albedo[dmy[rec].month-1];
      else 
	bare_albedo = BARE_SOIL_ALBEDO;
      
      /*************************************
	Compute the aerodynamic resistance 
      *************************************/

      /* Set surface descriptive variables */
      overstory = FALSE;
      if(iveg < Nveg) {
        displacement = veg_lib[veg_class].displacement[dmy[rec].month-1];
        roughness = veg_lib[veg_class].roughness[dmy[rec].month-1];
        overstory = veg_lib[veg_class].overstory;
      }
      if(iveg == Nveg || roughness == 0) {
        displacement = 0.;
        roughness = soil_con->rough;
        overstory = FALSE;
      }

      /* Initialize wind speeds */
      tmp_wind[0] = atmos->wind[NR];
      tmp_wind[1] = -999.;
      tmp_wind[2] = -999.;
 
      /* Estimate vegetation height */
      height = calc_veg_height(displacement);

      /* Estimate reference height */
      if(displacement < wind_h) 
	ref_height = wind_h;
      else 
	ref_height = displacement + wind_h + roughness;

      /* Compute aerodynamic resistance over various surface types */
      CalcAerodynamic(overstory, iveg, Nveg, veg_lib[veg_class].wind_atten,
                      height, soil_con->rough, soil_con->snow_rough,
                      &displacement, &roughness, &ref_height,
		      veg_lib[veg_class].trunk_ratio,
                      tmp_wind, cell[WET][iveg][0].aero_resist);
  
      /**************************************************
        Store Water Balance Terms for Debugging
      **************************************************/

#if LINK_DEBUG
      if(debug.DEBUG || debug.PRT_MOIST || debug.PRT_BALANCE) {
        /** Compute current total moisture for water balance check **/
	store_moisture_for_debug(iveg, Nveg, prcp->mu, cell,
				 veg_var, snow, soil_con);
	if(debug.PRT_BALANCE) {
	  for(j=0; j<Ndist; j++) {
	    for(band=0; band<Nbands; band++) {
	      if(soil_con->AreaFract[band] > 0) {
		for(i=0; i<options.Nlayer+3; i++) {
		  debug.inflow[j][band][i]  = 0;
		  debug.outflow[j][band][i] = 0;
		}
	      }
	    }
	  }
	}
      }
#endif

      /******************************
        Solve ground surface fluxes 
      ******************************/
  
      for ( band = 0; band < Nbands; band++ ) {
	if( soil_con->AreaFract[band] > 0 ) {

	  if ( iveg < Nveg ) {
	    wet_veg_var = &(veg_var[WET][iveg][band]);
	    dry_veg_var = &(veg_var[DRY][iveg][band]);
	  }
	  else {
	    wet_veg_var = &(empty_veg_var);
	    dry_veg_var = &(empty_veg_var);
	  }

	  surface_fluxes(overstory, rec, band, veg_class, iveg, Nveg, Ndist, 
			 Nbands, options.Nlayer, dp, prcp->mu[iveg], ice0[band], 
			 moist, surf_atten, height, displacement, roughness, 
			 ref_height, bare_albedo, 
			 cell[WET][iveg][0].aero_resist, &(cell[WET][iveg][0].aero_resist_used),
			 &(cell[WET][iveg][band].baseflow), 
			 &(cell[DRY][iveg][band].baseflow), 
			 &(cell[WET][iveg][band].runoff), 
			 &(cell[DRY][iveg][band].runoff), &out_prec[band*2], 
			 &out_rain[band*2], &out_snow[band*2],
			 tmp_wind, &Le, &Ls, &(Melt[band*2]), 
			 &(cell[WET][iveg][band].inflow), 
			 &(cell[DRY][iveg][band].inflow), 
			 &snow_inflow[band], gauge_correction,
			 veg_con[iveg].root, atmos, soil_con, dmy, gp, 
			 &(energy[iveg][band]), &(snow[iveg][band]),
			 cell[WET][iveg][band].layer, 
			 cell[DRY][iveg][band].layer, 
			 wet_veg_var, dry_veg_var);
	  
	  atmos->out_prec += out_prec[band*2] * Cv * soil_con->AreaFract[band];
	  atmos->out_rain += out_rain[band*2] * Cv * soil_con->AreaFract[band];
	  atmos->out_snow += out_snow[band*2] * Cv * soil_con->AreaFract[band];

          /********************************************************
            Compute soil wetness and root zone soil moisture
          ********************************************************/
          cell[WET][iveg][band].rootmoist = 0;
          cell[DRY][iveg][band].rootmoist = 0;
          cell[WET][iveg][band].wetness = 0;
          cell[DRY][iveg][band].wetness = 0;
          for(lidx=0;lidx<options.Nlayer;lidx++) {
            tmp_total_moist_wet = cell[WET][iveg][band].layer[lidx].moist + cell[WET][iveg][band].layer[lidx].ice;
            tmp_total_moist_dry = cell[DRY][iveg][band].layer[lidx].moist + cell[DRY][iveg][band].layer[lidx].ice;
            if (veg_con->root[lidx] > 0) {
              cell[WET][iveg][band].rootmoist += tmp_total_moist_wet;
              cell[DRY][iveg][band].rootmoist += tmp_total_moist_dry;
            } 
            cell[WET][iveg][band].wetness += (tmp_total_moist_wet - soil_con->Wpwp[lidx])/(soil_con->porosity[lidx]*soil_con->depth[lidx]*1000 - soil_con->Wpwp[lidx]);
            cell[DRY][iveg][band].wetness += (tmp_total_moist_dry - soil_con->Wpwp[lidx])/(soil_con->porosity[lidx]*soil_con->depth[lidx]*1000 - soil_con->Wpwp[lidx]);
          }
          cell[WET][iveg][band].wetness /= options.Nlayer;
          cell[DRY][iveg][band].wetness /= options.Nlayer;


	} /** End Loop Through Elevation Bands **/
      } /** End Full Energy Balance Model **/
  
      /****************************
	Controls Debugging Output
      ****************************/

#if LINK_DEBUG
      for(j = 0; j < Ndist; j++) {
	if(iveg < Nveg) 
	  tmp_veg[j] = veg_var[j][iveg];
	else 
	  tmp_veg[j] = NULL;
      }
      ptr_energy = energy[iveg];
      tmp_snow   = snow[iveg];
      for(j = 0; j < Ndist; j++) {
	if(j == 0) 
	  tmp_mu = prcp->mu[iveg]; 
	else 
	  tmp_mu = 1. - prcp->mu[iveg]; 
	/** for debugging water balance: [0] = vegetation, 
	    [1] = ground snow, [2..Nlayer+1] = soil layers **/
	if(debug.PRT_BALANCE) {
	  for(band = 0; band < Nbands; band++) {
	    if(soil_con->AreaFract[band] > 0) {
	      debug.inflow[j][band][options.Nlayer+2] 
		+= out_prec[j+band*2] * soil_con->Pfactor[band];
	      debug.inflow[j][band][0]  = 0.;
	      debug.inflow[j][band][1]  = 0.;
	      debug.outflow[j][band][0] = 0.;
	      debug.outflow[j][band][1] = 0.;
	      if(iveg < Nveg) {
		/** Vegetation Present **/
		debug.inflow[j][band][0] += out_prec[j+band*2] * soil_con->Pfactor[band]; 
		debug.outflow[j][band][0] 
		  += veg_var[j][iveg][band].throughfall;
	      }
	      if(j == 0)
		debug.inflow[j][band][1] += snow_inflow[band];
	      debug.outflow[j][band][1] += Melt[band*2+j];
	    }
	  }  /** End loop through elevation bands **/
	}
         
	if(iveg != Nveg) {
	  write_debug(atmos, soil_con, cell[j][iveg], ptr_energy, 
		      tmp_snow, tmp_veg[j], &(dmy[rec]), gp, out_short, 
		      tmp_mu, Nveg, iveg, rec, gridcell, j, NEWCELL); 
	}
	else {
	  write_debug(atmos, soil_con, cell[j][iveg], ptr_energy, tmp_snow, 
		      tmp_veg[j], &(dmy[rec]), gp, out_short, tmp_mu, Nveg, 
		      iveg, rec, gridcell, j, NEWCELL); 
	}
      }
#endif

    } /** end current vegetation type **/
  } /** end of vegetation loop **/

  /** END Temperature and Moisture Profile Debugging Output **/
  if(options.FULL_ENERGY || options.FROZEN_SOIL) {
    for(j = 0; j < 2; j++) {
      free ((char *)tmp_layer[j]);
    }
  }

}