Exemple #1
0
void SAM(C64 *the_c64)
{
	bool done = false;
	char c;

	TheCPU = the_c64->TheCPU;
	TheCPU1541 = the_c64->TheCPU1541;
	TheVIC = the_c64->TheVIC;
	TheSID = the_c64->TheSID;
	TheCIA1 = the_c64->TheCIA1;
	TheCIA2 = the_c64->TheCIA2;

	// Get CPU registers and current memory configuration
	TheCPU->GetState(&R64);
	TheCPU->ExtConfig = (~R64.ddr | R64.pr) & 7;
	TheCPU1541->GetState(&R1541);

#ifdef __riscos__
	Wimp_CommandWindow((int)"SAM");
#endif

#ifdef AMIGA
	if (!(fin = fout = ferr = fopen("CON:0/0/640/480/SAM", "w+")))
		return;
#else
	fin = stdin;
	fout = stdout;
	ferr = stdout;
#endif

	access_1541 = false;
	address = R64.pc;

	fprintf(ferr, "\n *** SAM - Simple Assembler and Monitor ***\n ***         Press 'h' for help         ***\n\n");
	init_abort();
	display_registers();

	while (!done) {
		if (access_1541)
			fprintf(ferr, "1541> ");
		else
			fprintf(ferr, "C64> ");
		fflush(ferr);
		read_line();
		while ((c = get_char()) == ' ') ;

		switch (c) {
			case 'a':		// Assemble
				get_token();
				assemble();
				break;

			case 'b':		// Binary dump
				get_token();
				binary_dump();
				break;

			case 'c':		// Compare
				get_token();
				compare();
				break;

			case 'd':		// Disassemble
				get_token();
				disassemble();
				break;

			case 'e':       // Interrupt vectors
				int_vectors();
				break;

			case 'f':		// Fill
				get_token();
				fill();
				break;

			case 'h':		// Help
				help();
				break;

			case 'i':		// ASCII dump
				get_token();
				ascii_dump();
				break;

			case 'k':		// Memory configuration
				get_token();
				mem_config();
				break;

			case 'l':		// Load data
				get_token();
				load_data();
				break;

			case 'm':		// Memory dump
				get_token();
				memory_dump();
				break;

			case 'n':		// Screen code dump
				get_token();
				screen_dump();
				break;

			case 'o':		// Redirect output
				get_token();
				redir_output();
				break;

			case 'p':		// Sprite dump
				get_token();
				sprite_dump();
				break;

			case 'r':		// Registers
				get_reg_token();
				registers();
				break;

			case 's':		// Save data
				get_token();
				save_data();
				break;

			case 't':		// Transfer
				get_token();
				transfer();
				break;

			case 'v':		// View machine state
				view_state();
				break;

			case 'x':		// Exit
				done = true;
				break;

			case ':':		// Change memory
				get_token();
				modify();
				break;

			case '1':		// Switch to 1541 mode
				access_1541 = true;
				break;

			case '6':		// Switch to C64 mode
				access_1541 = false;
				break;

			case '?':		// Compute expression
				get_token();
				print_expr();
				break;

			case '\n':		// Blank line
				break;

			default:		// Unknown command
				error("Unknown command");
				break;
		}
	}

	exit_abort();

#ifdef AMIGA
	fclose(fin);
#endif
	if (fout != ferr)
		fclose(fout);

#ifdef __riscos__
	Wimp_CommandWindow(-1);
#endif

	// Set CPU registers
	TheCPU->SetState(&R64);
	TheCPU1541->SetState(&R1541);
}
Exemple #2
0
/** Implements the end_interval function of the plugin API */
int corsaro_dos_end_interval(corsaro_t *corsaro, corsaro_interval_t *int_end)
{
  int this_interval = int_end->time-STATE(corsaro)->first_interval;

  khiter_t i;
  attack_vector_t *vector;
  attack_vector_t **attack_arr = NULL;
  int attack_arr_cnt = 0;

  uint8_t gbuf[12];
  uint8_t cntbuf[4];

  if(this_interval < CORSARO_DOS_INTERVAL)
    {
      /* we haven't run for long enough to dump */
      return 0;
    }
  else
    {
      /* we either have hit exactly the right amount of time,
	 or we have gone for too long, dump now and reset the counter */
      STATE(corsaro)->first_interval = int_end->time;
      /* fall through and continue to dump */
    }

  /* this is an interval we care about */

  /* malloc an array big enough to hold the entire hash even though we wont
     need it to be that big */
  if((attack_arr =
      malloc(sizeof(attack_vector_t *)*
	     kh_size(STATE(corsaro)->attack_hash))) == NULL)
    {
      corsaro_log(__func__, corsaro,
		  "could not malloc array for attack vectors");
      return -1;
    }

  /* classify the flows and dump the attack ones */

  for(i = kh_begin(STATE(corsaro)->attack_hash);
      i != kh_end(STATE(corsaro)->attack_hash); ++i)
    {
      if(kh_exist(STATE(corsaro)->attack_hash, i))
	{
	  vector = kh_key(STATE(corsaro)->attack_hash, i);

	  if(attack_vector_is_expired(vector, int_end->time) != 0)
	    {
	      kh_del(av, STATE(corsaro)->attack_hash, i);
	      attack_vector_free(vector);
	      vector = NULL;
	    }
	  else if(attack_vector_is_attack(corsaro, vector, int_end->time) != 0)
	    {
	      /* this is an attack */
	      /* add it to the attack array so we can know how many
		 before we dump it */
	      attack_arr[attack_arr_cnt] = vector;
	      attack_arr_cnt++;
	    }
	  else
	    {
	      attack_vector_reset(vector);
	    }
	}
    }

  corsaro_io_write_interval_start(corsaro, STATE(corsaro)->outfile,
				  &corsaro->interval_start);
  if(corsaro->global_file != NULL)
    {
      corsaro_io_write_plugin_start(corsaro, corsaro->global_file,
				    PLUGIN(corsaro));
    }
  if(CORSARO_FILE_MODE(STATE(corsaro)->outfile) == CORSARO_FILE_MODE_ASCII)
    {
      if(corsaro->global_file != NULL)
	{
	  /* global stats */
	  /* dump the number of mismatched packets and vectors */
	  corsaro_file_printf(corsaro, corsaro->global_file,
			      "mismatch: %"PRIu32"\n"
			      "attack_vectors: %"PRIu32"\n"
			      "non-attack_vectors: %"PRIu32"\n",
			      STATE(corsaro)->number_mismatched_packets,
			      attack_arr_cnt,
			      kh_size(STATE(corsaro)->attack_hash)
			      -attack_arr_cnt);
	}

      /* dump the number of vectors */
      corsaro_file_printf(corsaro, STATE(corsaro)->outfile, "%"PRIu32"\n",
			attack_arr_cnt);
      /* dump the vectors */
      for(i = 0; i < attack_arr_cnt; i++)
	{
	  if(ascii_dump(corsaro, attack_arr[i]) != 0)
	    {
	      corsaro_log(__func__, corsaro, "could not dump hash");
	      return -1;
	    }
	  /* reset the interval stats */
	  attack_vector_reset(attack_arr[i]);
	}
    }
  else if(CORSARO_FILE_MODE(STATE(corsaro)->outfile) == CORSARO_FILE_MODE_BINARY)
      {
	if(corsaro->global_file != NULL)
	  {
	    /* global stats */
	    bytes_htonl(&gbuf[0], STATE(corsaro)->number_mismatched_packets);
	    bytes_htonl(&gbuf[4], attack_arr_cnt);
	    bytes_htonl(&gbuf[8],
			kh_size(STATE(corsaro)->attack_hash)-attack_arr_cnt);
	    if(corsaro_file_write(corsaro, corsaro->global_file,
				  &gbuf[0], 12) != 12)
	      {
		corsaro_log(__func__, corsaro,
			    "could not dump global stats to file");
		return -1;
	      }
	  }

	/* dump the number of vectors */
	bytes_htonl(&cntbuf[0], attack_arr_cnt);
	if(corsaro_file_write(corsaro, STATE(corsaro)->outfile,
			    &cntbuf[0], 4) != 4)
	  {
	    corsaro_log(__func__, corsaro,
			"could not dump vector count to file");
	    return -1;
	  }
	/* dump the vectors */
	for(i = 0; i < attack_arr_cnt; i++)
	  {
	    if(binary_dump(corsaro, attack_arr[i]) != 0)
	      {
		corsaro_log(__func__, corsaro, "could not dump hash");
		return -1;
	      }
	    attack_vector_reset(attack_arr[i]);
	  }
      }
  else
    {
      corsaro_log(__func__, corsaro, "invalid mode");
      return -1;
    }
  if(corsaro->global_file != NULL)
    {
      corsaro_io_write_plugin_end(corsaro, corsaro->global_file,
				  PLUGIN(corsaro));
    }
  corsaro_io_write_interval_end(corsaro, STATE(corsaro)->outfile, int_end);

  STATE(corsaro)->number_mismatched_packets = 0;

  free(attack_arr);

  /* if we are rotating, now is when we should do it */
  if(corsaro_is_rotate_interval(corsaro))
    {
      /* close the current file */
      if(STATE(corsaro)->outfile != NULL)
	{
	  corsaro_file_close(corsaro, STATE(corsaro)->outfile);
	  STATE(corsaro)->outfile = NULL;
	}
    }

  return 0;
}
Exemple #3
0
int main( int argc, char* argv[] )
{
	// =====================================================================
	// Initialization & Command Line Read-In
	// =====================================================================
	int version = 13;
	int mype = 0;
	int max_procs = omp_get_num_procs();
	int i, thread, mat;
	unsigned long seed;
	double omp_start, omp_end, p_energy;
	unsigned long long vhash = 0;
	int nprocs;

	#ifdef MPI
	MPI_Status stat;
	MPI_Init(&argc, &argv);
	MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
	MPI_Comm_rank(MPI_COMM_WORLD, &mype);
	#endif

	// rand() is only used in the serial initialization stages.
	// A custom RNG is used in parallel portions.
	#ifdef VERIFICATION
	srand(26);
	#else
	srand(time(NULL));
	#endif

	// Process CLI Fields -- store in "Inputs" structure
	Inputs in = read_CLI( argc, argv );

	// Set number of OpenMP Threads
	omp_set_num_threads(in.nthreads);

	// Print-out of Input Summary
	if( mype == 0 )
		print_inputs( in, nprocs, version );

	// =====================================================================
	// Prepare Nuclide Energy Grids, Unionized Energy Grid, & Material Data
	// =====================================================================

	// Allocate & fill energy grids
	#ifndef BINARY_READ
	if( mype == 0) printf("Generating Nuclide Energy Grids...\n");
	#endif

	NuclideGridPoint ** nuclide_grids = gpmatrix(in.n_isotopes,in.n_gridpoints);

	#ifdef VERIFICATION
	generate_grids_v( nuclide_grids, in.n_isotopes, in.n_gridpoints );
	#else
	generate_grids( nuclide_grids, in.n_isotopes, in.n_gridpoints );
	#endif

	// Sort grids by energy
	#ifndef BINARY_READ
	if( mype == 0) printf("Sorting Nuclide Energy Grids...\n");
	sort_nuclide_grids( nuclide_grids, in.n_isotopes, in.n_gridpoints );
	#endif

	// Prepare Unionized Energy Grid Framework
	#ifndef BINARY_READ
	GridPoint * energy_grid = generate_energy_grid( in.n_isotopes,
	                          in.n_gridpoints, nuclide_grids );
	#else
	GridPoint * energy_grid = (GridPoint *)malloc( in.n_isotopes *
	                           in.n_gridpoints * sizeof( GridPoint ) );
	int * index_data = (int *) malloc( in.n_isotopes * in.n_gridpoints
	                   * in.n_isotopes * sizeof(int));
	for( i = 0; i < in.n_isotopes*in.n_gridpoints; i++ )
		energy_grid[i].xs_ptrs = &index_data[i*in.n_isotopes];
	#endif

	// Double Indexing. Filling in energy_grid with pointers to the
	// nuclide_energy_grids.
	#ifndef BINARY_READ
	set_grid_ptrs( energy_grid, nuclide_grids, in.n_isotopes, in.n_gridpoints );
	#endif

	#ifdef BINARY_READ
	if( mype == 0 ) printf("Reading data from \"XS_data.dat\" file...\n");
	binary_read(in.n_isotopes, in.n_gridpoints, nuclide_grids, energy_grid);
	#endif

	// Get material data
	if( mype == 0 )
		printf("Loading Mats...\n");
	int *num_nucs  = load_num_nucs(in.n_isotopes);
	int **mats     = load_mats(num_nucs, in.n_isotopes);

	#ifdef VERIFICATION
	double **concs = load_concs_v(num_nucs);
	#else
	double **concs = load_concs(num_nucs);
	#endif

	#ifdef BINARY_DUMP
	if( mype == 0 ) printf("Dumping data to binary file...\n");
	binary_dump(in.n_isotopes, in.n_gridpoints, nuclide_grids, energy_grid);
	if( mype == 0 ) printf("Binary file \"XS_data.dat\" written! Exiting...\n");
	return 0;
	#endif

	// =====================================================================
	// Cross Section (XS) Parallel Lookup Simulation Begins
	// =====================================================================

	// Outer benchmark loop can loop through all possible # of threads
	#ifdef BENCHMARK
	for( int bench_n = 1; bench_n <=omp_get_num_procs(); bench_n++ )
	{
		in.nthreads = bench_n;
		omp_set_num_threads(in.nthreads);
 	#endif

	if( mype == 0 )
	{
		printf("\n");
		border_print();
		center_print("SIMULATION", 79);
		border_print();
	}

	omp_start = omp_get_wtime();

	//initialize papi with one thread (master) here
	#ifdef PAPI
	if ( PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT){
		fprintf(stderr, "PAPI library init error!\n");
		exit(1);
	}
	#endif

	// OpenMP compiler directives - declaring variables as shared or private
	#pragma omp parallel default(none) \
	private(i, thread, p_energy, mat, seed) \
	shared( max_procs, in, energy_grid, nuclide_grids, \
	        mats, concs, num_nucs, mype, vhash)
	{
		// Initialize parallel PAPI counters
		#ifdef PAPI
		int eventset = PAPI_NULL;
		int num_papi_events;
		#pragma omp critical
		{
			counter_init(&eventset, &num_papi_events);
		}
		#endif

		double macro_xs_vector[5];
		double * xs = (double *) calloc(5, sizeof(double));

		// Initialize RNG seeds for threads
		thread = omp_get_thread_num();
		seed   = (thread+1)*19+17;

		// XS Lookup Loop
		#pragma omp for schedule(dynamic)
		for( i = 0; i < in.lookups; i++ )
		{
			// Status text
			if( INFO && mype == 0 && thread == 0 && i % 1000 == 0 )
				printf("\rCalculating XS's... (%.0lf%% completed)",
						(i / ( (double)in.lookups / (double) in.nthreads ))
						/ (double) in.nthreads * 100.0);

			// Randomly pick an energy and material for the particle
			#ifdef VERIFICATION
			#pragma omp critical
			{
				p_energy = rn_v();
				mat      = pick_mat(&seed);
			}
			#else
			p_energy = rn(&seed);
			mat      = pick_mat(&seed);
			#endif

			// debugging
			//printf("E = %lf mat = %d\n", p_energy, mat);

			// This returns the macro_xs_vector, but we're not going
			// to do anything with it in this program, so return value
			// is written over.
			calculate_macro_xs( p_energy, mat, in.n_isotopes,
			                    in.n_gridpoints, num_nucs, concs,
			                    energy_grid, nuclide_grids, mats,
                                macro_xs_vector );

			// Copy results from above function call onto heap
			// so that compiler cannot optimize function out
			// (only occurs if -flto flag is used)
			memcpy(xs, macro_xs_vector, 5*sizeof(double));

			// Verification hash calculation
			// This method provides a consistent hash accross
			// architectures and compilers.
			#ifdef VERIFICATION
			char line[256];
			sprintf(line, "%.5lf %d %.5lf %.5lf %.5lf %.5lf %.5lf",
			       p_energy, mat,
				   macro_xs_vector[0],
				   macro_xs_vector[1],
				   macro_xs_vector[2],
				   macro_xs_vector[3],
				   macro_xs_vector[4]);
			unsigned long long vhash_local = hash(line, 10000);
			#pragma omp atomic
			vhash += vhash_local;
			#endif
		}

		// Prints out thread local PAPI counters
		#ifdef PAPI
		if( mype == 0 && thread == 0 )
		{
			printf("\n");
			border_print();
			center_print("PAPI COUNTER RESULTS", 79);
			border_print();
			printf("Count          \tSmybol      \tDescription\n");
		}
		{
		#pragma omp barrier
		}
		counter_stop(&eventset, num_papi_events);
		#endif

	}

	#ifndef PAPI
	if( mype == 0)
	{
		printf("\n" );
		printf("Simulation complete.\n" );
	}
	#endif

	omp_end = omp_get_wtime();

	// Print / Save Results and Exit
	print_results( in, mype, omp_end-omp_start, nprocs, vhash );

	#ifdef BENCHMARK
	}
	#endif

	#ifdef MPI
	MPI_Finalize();
	#endif

	return 0;
}
static int row_smart_r2l(fsbuf_type buf[FSHEIGHT][FSBUFWIDTH],
                        int bx, int by, int bw, int bh, int flags,
                        int width, int height, int *resultx, int *resulty)
{
    /* handle vertical direction */
    bool t2b = (flags & FSPLACE_TOP_TO_BOTTOM);
    const int y0 =  t2b ? by : by + bh - 1;
    const int y1 =  t2b ? by + bh - height : by + height - 1;
    const int ydir = t2b ? 1 : -1;

    /* get offsets and indices of boundary */
    int x0offset = 0;
    int x1offset = 0;
    int x0index = x_to_index_offset(bx + bw - 1, &x0offset);
    int x1index = x_to_index_offset(bx, &x1offset);

    /* offset and index of furthest point where placement may reach */
    int xwoffset = 0;
    int xwindex = x_to_index_offset(bx + width - 1, &xwoffset);

    /*  note: the index (horizontal) loop must test against the boundary
     *  as the loop is a state-machine with two states:
     *
     *      1) non-scanning: placement is decisivly impossible at the
     *                      location (index, offset). when in this state,
     *                      the loop should break out past (xwindex,
     *                      xwoffset) ie the last position of possible
     *                      placement.
     *
     *      2) scanning:    placement is possible at the location (index,
     *                      offset) and scanning is underway for an area
     *                      width x height. in this case the loop may need
     *                      to go right to the boundary edge (x1index,
     *                      x1offset).
     */

    int y;

    #ifdef FSDEBUG
    DMESSAGE("--------------------------------------------\n"
             "x0 index:%d offset:%d\nx1 index:%d offset:%d\n",
                x0index, x0offset, x1index, x1offset);
    DMESSAGE("\nxw index:%d offset:%d\n", xwindex, xwoffset);
    DMESSAGE("\ny0: %d y1:%d ydir:%d\n",y0,y1,ydir);
    #endif

    for (y = y0; t2b ? y <= y1 : y >= y1; y += ydir)
    {
        int index;
        int offset = 0;
        int bits_remaining = 0;
        int scanning = false;

        #ifdef FSDEBUG
        DMESSAGE("\nline:%d\n",y);
        #endif

        for (index = x0index; index >= x1index; --index)
        {
            fsbuf_type mask = 0;
            int h;

            if (!scanning) /* check enough room remains for area at */
            {              /* (index, offset) (see note above) */
                if (index < xwindex)
                {
                    #ifdef FSDEBUG
                    DMESSAGE("index:%d < xwindex\n", index);
                    #endif
                    break;
                }

                offset = (index == x0index) ? x0offset
                                            : 0;

                if (index == xwindex && offset > xwoffset)
                {
                    #ifdef FSDEBUG
                    DMESSAGE("no room remains at index:%d offset:%d\n",
                                                    index, offset);
                    #endif
                    break;
                }
            }

            if (buf[y][index] == fsbuf_max)
            {
                scanning = false;
                continue;
            }

            if (!scanning)
            {
                scanning = true;
                bits_remaining = width;
                #ifdef FSDEBUG
                DMESSAGE("start scan index:%d offset:%d br:%d\n",
                            index,   offset, bits_remaining);
                #endif

            }
retry:
            if (bits_remaining < FSBUFBITS - offset)
            {   /* ie br = 4, offset = 2: 00111100 */
                mask = (((fsbuf_type)1 << bits_remaining) - 1) << offset;
                #ifdef FSDEBUG
                DMESSAGE("mask... index:%d offset:%d br:%d\n",
                                        index,   offset, bits_remaining);
                binary_dump("br <= FSBITS - offset mask:", mask);
                #endif
            }
            else
            {   /* ie br = 8, offset = 2: 11111100 */
                mask = fsbuf_max << offset;
                #ifdef FSDEBUG
                DMESSAGE("mask... index:%d offset:%d br:%d\n",
                                        index,   offset, bits_remaining);
                binary_dump("br > FSBITS - offset mask:", mask);
                #endif
            }

            for (h = 0; h < height; ++h)
            {
                int v = buf[y + h * ydir][index] & mask;

                if (v)
                {
                    #ifdef FSDEBUG
                    binary_dump("obstructed by v:", v);
                    #endif

                    offset = FSBUFBITS - LEADING_ZEROS(v);

                    if (offset < FSBUFBITS)
                    {
                        if (!(index < xwindex)
                         && !(index == xwindex && offset > xwoffset))
                        {
                            bits_remaining = width;
                            #ifdef FSDEBUG
                            DMESSAGE("rety: index:%d offset:%d br:%d\n",
                                        index,   offset, bits_remaining);
                            #endif
                            goto retry;
                        }
                    }
                    #ifdef FSDEBUG
                    DMESSAGE("breaking vertical scan\n");
                    #endif
                    scanning = false;
                    break; /* break to continue */
                }
            }

            if (!scanning)
                continue;

            if (bits_remaining <= FSBUFBITS - offset) /***** RESULT!! *****/
            {
                #ifdef FSDEBUG
                DMESSAGE("result index:%d offset:%d\n", index,
                                                        offset);
                #endif
                *resultx = index * FSBUFBITS +  (FSBUFBITS - offset)
                                                    - bits_remaining;
                *resulty = t2b ? y : y + 1 - height;
                return true;
            }

            bits_remaining -= FSBUFBITS - offset;
            offset = 0;
        }
    }

    #ifdef FSDEBUG
    DMESSAGE("no free space\n");
    #endif

    return false;
}