예제 #1
0
void sig_int(int sign)
{
	printf("signal caught, simavr terminating\n");
	if (avr)
		avr_terminate(avr);
	exit(0);
}
예제 #2
0
파일: sim_avr.c 프로젝트: vencax/pysimavr
void avr_callback_run_gdb(avr_t * avr)
{
	avr_gdb_processor(avr, avr->state == cpu_Stopped);

	if (avr->state == cpu_Stopped)
		return ;

	// if we are stepping one instruction, we "run" for one..
	int step = avr->state == cpu_Step;
	if (step)
		avr->state = cpu_Running;
	
	uint16_t new_pc = avr->pc;

	if (avr->state == cpu_Running) {
		new_pc = avr_run_one(avr);
#if CONFIG_SIMAVR_TRACE
		avr_dump_state(avr);
#endif
	}

	// if we just re-enabled the interrupts...
	// double buffer the I flag, to detect that edge
	if (avr->sreg[S_I] && !avr->i_shadow)
		avr->pending_wait++;
	avr->i_shadow = avr->sreg[S_I];

	// run the cycle timers, get the suggested sleep time
	// until the next timer is due
	avr_cycle_count_t sleep = avr_cycle_timer_process(avr);

	avr->pc = new_pc;

	if (avr->state == cpu_Sleeping) {
		if (!avr->sreg[S_I]) {
			if (avr->log)
				printf("simavr: sleeping with interrupts off, quitting gracefully\n");
			avr_terminate(avr);
			avr->state = cpu_Done;
			return;
		}
		/*
		 * try to sleep for as long as we can (?)
		 */
		avr->sleep(avr, sleep);
		avr->cycle += 1 + sleep;
	}
	// Interrupt servicing might change the PC too, during 'sleep'
	if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
		avr_service_interrupts(avr);
	
	// if we were stepping, use this state to inform remote gdb
	if (step)
		avr->state = cpu_StepDone;

}
예제 #3
0
static void *
avr_run_thread (void *oaram)
{
  while (1)
    {
      int state = avr_run (avr);
      if (state == cpu_Done || state == cpu_Crashed)
        break;
    }
  avr_terminate (avr);
  
  return NULL;
}
예제 #4
0
파일: drumfish.c 프로젝트: cardoe/drumfish
static void
handler(int sig)
{
    switch (sig) {
        case SIGINT:
        case SIGTERM:
            avr_terminate(avr);
            exit(EXIT_FAILURE);
            break;

        case SIGHUP:
            avr_reset(avr);
            break;
    }
}
예제 #5
0
파일: tests.c 프로젝트: 33d/gbsim-win
static int my_avr_run(avr_t * avr)
{
	if (avr->state == cpu_Stopped)
		return avr->state;

	uint16_t new_pc = avr->pc;

	if (avr->state == cpu_Running)
		new_pc = avr_run_one(avr);

	// if we just re-enabled the interrupts...
	// double buffer the I flag, to detect that edge
	if (avr->sreg[S_I] && !avr->i_shadow)
		avr->interrupts.pending_wait++;
	avr->i_shadow = avr->sreg[S_I];

	// run the cycle timers, get the suggested sleep time
	// until the next timer is due
	avr_cycle_count_t sleep = avr_cycle_timer_process(avr);

	avr->pc = new_pc;

	if (avr->state == cpu_Sleeping) {
		if (!avr->sreg[S_I]) {
			printf("simavr: sleeping with interrupts off, quitting gracefully\n");
			avr_terminate(avr);
			fail("Test case error: special_deinit() returned?");
			exit(0);
		}
		/*
		 * try to sleep for as long as we can (?)
		 */
		// uint32_t usec = avr_cycles_to_usec(avr, sleep);
		// printf("sleep usec %d cycles %d\n", usec, sleep);
		// usleep(usec);
		avr->cycle += 1 + sleep;
	}
	// Interrupt servicing might change the PC too, during 'sleep'
	if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
		avr_service_interrupts(avr);
	
	// if we were stepping, use this state to inform remote gdb

	return avr->state;
}
예제 #6
0
파일: run_avr.c 프로젝트: bsekisser/simavr
int main(int argc, char *argv[])
{
	elf_firmware_t f = {{0}};
	long f_cpu = 0;
	int trace = 0;
	int gdb = 0;
	int log = 1;
	char name[16] = "";
	uint32_t loadBase = AVR_SEGMENT_OFFSET_FLASH;
	int trace_vectors[8] = {0};
	int trace_vectors_count = 0;

	if (argc == 1)
		display_usage(basename(argv[0]));

	for (int pi = 1; pi < argc; pi++) {
		if (!strcmp(argv[pi], "--list-cores")) {
			list_cores();
		} else if (!strcmp(argv[pi], "-h") || !strcmp(argv[pi], "--help")) {
			display_usage(basename(argv[0]));
		} else if (!strcmp(argv[pi], "-m") || !strcmp(argv[pi], "-mcu")) {
			if (pi < argc-1)
				strcpy(name, argv[++pi]);
			else
				display_usage(basename(argv[0]));
		} else if (!strcmp(argv[pi], "-f") || !strcmp(argv[pi], "-freq")) {
			if (pi < argc-1)
				f_cpu = atoi(argv[++pi]);
			else
				display_usage(basename(argv[0]));
		} else if (!strcmp(argv[pi], "-t") || !strcmp(argv[pi], "-trace")) {
			trace++;
		} else if (!strcmp(argv[pi], "-ti")) {
			if (pi < argc-1)
				trace_vectors[trace_vectors_count++] = atoi(argv[++pi]);
		} else if (!strcmp(argv[pi], "-g") || !strcmp(argv[pi], "-gdb")) {
			gdb++;
		} else if (!strcmp(argv[pi], "-v")) {
			log++;
		} else if (!strcmp(argv[pi], "-ee")) {
			loadBase = AVR_SEGMENT_OFFSET_EEPROM;
		} else if (!strcmp(argv[pi], "-ff")) {
			loadBase = AVR_SEGMENT_OFFSET_FLASH;			
		} else if (argv[pi][0] != '-') {
			char * filename = argv[pi];
			char * suffix = strrchr(filename, '.');
			if (suffix && !strcasecmp(suffix, ".hex")) {
				if (!name[0] || !f_cpu) {
					fprintf(stderr, "%s: -mcu and -freq are mandatory to load .hex files\n", argv[0]);
					exit(1);
				}
				ihex_chunk_p chunk = NULL;
				int cnt = read_ihex_chunks(filename, &chunk);
				if (cnt <= 0) {
					fprintf(stderr, "%s: Unable to load IHEX file %s\n", 
						argv[0], argv[pi]);
					exit(1);
				}
				printf("Loaded %d section of ihex\n", cnt);
				for (int ci = 0; ci < cnt; ci++) {
					if (chunk[ci].baseaddr < (1*1024*1024)) {
						f.flash = chunk[ci].data;
						f.flashsize = chunk[ci].size;
						f.flashbase = chunk[ci].baseaddr;
						printf("Load HEX flash %08x, %d\n", f.flashbase, f.flashsize);
					} else if (chunk[ci].baseaddr >= AVR_SEGMENT_OFFSET_EEPROM ||
							chunk[ci].baseaddr + loadBase >= AVR_SEGMENT_OFFSET_EEPROM) {
						// eeprom!
						f.eeprom = chunk[ci].data;
						f.eesize = chunk[ci].size;
						printf("Load HEX eeprom %08x, %d\n", chunk[ci].baseaddr, f.eesize);
					}
				}
			} else {
				if (elf_read_firmware(filename, &f) == -1) {
					fprintf(stderr, "%s: Unable to load firmware from file %s\n",
							argv[0], filename);
					exit(1);
				}
			}
		}
	}

	if (strlen(name))
		strcpy(f.mmcu, name);
	if (f_cpu)
		f.frequency = f_cpu;

	avr = avr_make_mcu_by_name(f.mmcu);
	if (!avr) {
		fprintf(stderr, "%s: AVR '%s' not known\n", argv[0], f.mmcu);
		exit(1);
	}
	avr_init(avr);
	avr_load_firmware(avr, &f);
	if (f.flashbase) {
		printf("Attempted to load a bootloader at %04x\n", f.flashbase);
		avr->pc = f.flashbase;
	}
	avr->log = (log > LOG_TRACE ? LOG_TRACE : log);
	avr->trace = trace;
	for (int ti = 0; ti < trace_vectors_count; ti++) {
		for (int vi = 0; vi < avr->interrupts.vector_count; vi++)
			if (avr->interrupts.vector[vi]->vector == trace_vectors[ti])
				avr->interrupts.vector[vi]->trace = 1;
	}

	// even if not setup at startup, activate gdb if crashing
	avr->gdb_port = 1234;
	if (gdb) {
		avr->state = cpu_Stopped;
		avr_gdb_init(avr);
	}

	signal(SIGINT, sig_int);
	signal(SIGTERM, sig_int);

	for (;;) {
		int state = avr_run(avr);
		if ( state == cpu_Done || state == cpu_Crashed)
			break;
	}
	
	avr_terminate(avr);
}
예제 #7
0
파일: drumfish.c 프로젝트: cardoe/drumfish
int
main(int argc, char *argv[])
{
    const char *argv0 = argv[0];
    int exit_state = EXIT_FAILURE;
    char *env;
    struct drumfish_cfg config;
    struct sigaction act;
    int state = cpu_Limbo;
    int opt;
    char **flash_file = NULL;
    size_t flash_file_len = 0;
    long  port;

    config.mac = NULL;
    config.pflash = NULL;
    config.foreground = 1;
    config.verbose = 0;
    config.gdb = 0;
    config.erase_pflash = 0;
    config.peripherals[DF_PERIPHERAL_UART0] = strdup("off");
    config.peripherals[DF_PERIPHERAL_UART1] = strdup("on");

    while ((opt = getopt(argc, argv, "ef:p:m:vg:s:h")) != -1) {
        switch (opt) {
            case 'e':
                config.erase_pflash = 1;
                break;
            case 'f':
                /* Increment how many file names we need to keep track of */
                flash_file_len++;

                /* Set a reasonable max which is less than 2^30 to avoid
                 * overflowing realloc().
                 */
                if (flash_file_len > MAX_FLASH_FILES) {
                    fprintf(stderr, "Unable to load more than %d "
                            "firmware images at once.\n", MAX_FLASH_FILES);
                    exit(EXIT_FAILURE);
                }

                /* Make room for our next pointer */
                flash_file = realloc(flash_file,
                        sizeof(void *) * flash_file_len);
                if (!flash_file) {
                    fprintf(stderr, "Failed to allocate memory for "
                            "flash file list.\n");
                    exit(EXIT_FAILURE);
                }

                /* Now let's store our file name */
                flash_file[flash_file_len - 1] = strdup(optarg);
                if (!flash_file[flash_file_len - 1]) {
                    fprintf(stderr, "Failed to allocate memory for "
                            "flash file.\n");
                    exit(EXIT_FAILURE);
                }
                break;
            case 's':
                /* path to storage for flash */
                config.pflash = strdup(optarg);
                if (!config.pflash) {
                    fprintf(stderr, "Failed to allocate memory for "
                            "programmable flashh.\n");
                    exit(EXIT_FAILURE);
                }
                break;
            case 'm':
                config.mac = strdup(optarg);
                if (!config.mac) {
                    fprintf(stderr, "Failed to allocate memory for MAC.\n");
                    exit(EXIT_FAILURE);
                }
                break;
            case 'v':
               config.verbose++;
               break;
            case 'g':
               errno = 0;
               port = strtol(optarg, NULL, 10);
               if (errno != 0) {
                   fprintf(stderr, "Invalid supplied GDB port '%s': %s\n",
                           optarg, strerror(errno));
                   exit(EXIT_FAILURE);
               }

               if (port <= 1024 && port > UINT16_MAX) {
                   fprintf(stderr, "Invalid supplied GDB port %ld. "
                           "Must be 1024 < port <= %d\n", port, UINT16_MAX);
                   exit(EXIT_FAILURE);
               }

               config.gdb = port;
               break;
            case 'p':
               /* store requested port (UART) path */
               df_peripheral_parse(&config, optarg);
               break;
            case 'V':
               /* print version */
               break;
            case 'h':
               usage(argv0);
               exit(EXIT_SUCCESS);
               break;
            default: /* '?' */
                usage(argv0);
                exit(EXIT_FAILURE);
        }
    }

    /* Initialize our logging support */
    df_log_init(&config);

    /* If the user did not override the default location of the
     * programmable flash storage, then set the default
     */
    env = getenv("HOME");
    if (!env || !env[0]) {
        fprintf(stderr, "Unable to determine your HOME.\n");
        exit(EXIT_FAILURE);
    }

    if (asprintf(&config.pflash, "%s%s", env, DEFAULT_PFLASH_PATH) < 0) {
        fprintf(stderr, "Failed to allocate memory for pflash filename.\n");
        exit(EXIT_FAILURE);
    }

    printf("Programmable Flash Storage: %s\n", config.pflash);

    /* Handle the bare minimum signals */
    /* Yes I should use sigset_t here and use sigemptyset() */
    memset(&act, 0, sizeof(act));

    act.sa_handler = handler;
    if (sigaction(SIGHUP, &act, NULL) < 0) {
        fprintf(stderr, "Failed to install SIGHUP handler\n");
        exit(EXIT_FAILURE);
    }
    if (sigaction(SIGINT, &act, NULL) < 0) {
        fprintf(stderr, "Failed to install SIGINT handler\n");
        exit(EXIT_FAILURE);
    }
    if (sigaction(SIGTERM, &act, NULL) < 0) {
        fprintf(stderr, "Failed to install SIGTERM handler\n");
        exit(EXIT_FAILURE);
    }

    avr = m128rfa1_create(&config);
    if (!avr) {
        fprintf(stderr, "Unable to initialize requested board.\n");
        exit(EXIT_FAILURE);
    }

    /* Flash in any requested firmware, while cleaning up our memory */
    for (size_t i = 0; i < flash_file_len; i++) {
        if (flash_load(flash_file[i], avr->flash, avr->flashend + 1)) {
            fprintf(stderr, "Failed to load '%s' into flash.\n", flash_file[i]);
            exit(EXIT_FAILURE);
        }
        /* Don't need this memory anymore */
        free(flash_file[i]);
    }
    free(flash_file);

    /* Ensure the instruction we're about to execute is legit */
    if (avr->flash[avr->pc] == 0xff) {
        fprintf(stderr, "No firmware loaded in programmable flash, unable "
                "to boot.\n");
        fprintf(stderr, "Try using '-f firmware.hex' to supply one.\n");
        exit(EXIT_FAILURE);
    }

    /* If the user wants to run the core with GDB server enabled,
     * set that up.
     */
    if (config.gdb) {
        avr->gdb_port = config.gdb;
        /* Normally starting the CPU should be in limbo, but the
         * GDB code of simavr wants it to be stopped.
         */
        state = cpu_Stopped;

        avr_gdb_init(avr);
    }

    /* Capture the current time to be used as when our CPU started */
    df_log_start_time();

    df_log_msg(DF_LOG_INFO, "Booting CPU from 0x%x.\n", avr->pc);

    /* Our main event loop */
    for (;;) {
        state = avr_run(avr);

        if (state == cpu_Done) {
            exit_state = EXIT_SUCCESS;
            break;
        } else if (state == cpu_Crashed) {
            /* many firmwares disable interrupts and enable the watchdog
             * to cause the MCU to reboot. simavr treats that state as
             * cpu_Crashed
             */
            df_log_msg(DF_LOG_INFO, "CPU rebooted\n");
            avr_reset(avr);
        }
    }

    avr_terminate(avr);

    df_log_msg(DF_LOG_INFO, "Terminated.\n");

    free(config.pflash);

    return exit_state;
}
예제 #8
0
int main(int argc, char *argv[])
{
	int state;
	elf_firmware_t f;
	const char *fname = "../BasicMotorControl.elf";
	const char *mmcu = "atmega328p";

	if (elf_read_firmware(fname, &f) != 0)
	{
		exit(1);
	}

	f.frequency = 16000000;

	printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, mmcu);

	avr = avr_make_mcu_by_name(mmcu);
	if (!avr)
	{
		fprintf(stderr, "%s: AVR '%s' not known\n", argv[0], mmcu);
		exit(1);
	}

	avr_init(avr);
	avr_load_firmware(avr, &f);

#if 0
	/* even if not setup at startup, activate gdb if crashing */
	avr->gdb_port = 1234;
	avr->state = cpu_Stopped;
	avr_gdb_init(avr);
#endif

	/* VCD file initialization */
	avr_vcd_init(avr, "wave.vcd", &vcd_file, 10000 /* usec */);
	avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 0), 1, "B0" ); 
	avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 1), 1, "B1" ); 
	avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 2), 1, "B2" ); 
	avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 3), 1, "B3" ); 
	avr_vcd_start(&vcd_file);

	/* IRQ callback hooks */
	avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 0), pin_changed_hook, NULL); 
	avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 1), pin_changed_hook, NULL); 
	avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 2), pin_changed_hook, NULL); 
	avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 3), pin_changed_hook, NULL); 

	/* show some info */
	show_ports(avr);

	/* install signal handlers */
	signal(SIGINT, sig_int);
	signal(SIGTERM, sig_int);

	/* main loop */
	printf("*** Entering main loop ***\n");
	while (1) 
	{
		state = avr_run(avr);
		if (state == cpu_Done || state == cpu_Crashed)
		{
			printf("CPU State %d\n", state);
			break;
		}
	}

	avr_vcd_stop(&vcd_file);
	avr_terminate(avr);

	return 0;
}