1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/*
simduino.c
Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
This file is part of simavr.
simavr is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
simavr is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with simavr. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <libgen.h>
#if __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <pthread.h>
#include "sim_avr.h"
#include "avr_ioport.h"
#include "sim_elf.h"
#include "sim_hex.h"
#include "sim_gdb.h"
#include "uart_pty.h"
#include "sim_vcd_file.h"
uart_pty_t uart_pty;
avr_t * avr = NULL;
avr_vcd_t vcd_file;
struct avr_flash {
char avr_flash_path[1024];
int avr_flash_fd;
};
// avr special flash initalization
// here: open and map a file to enable a persistent storage for the flash memory
void avr_special_init( avr_t * avr, void * data)
{
struct avr_flash *flash_data = (struct avr_flash *)data;
printf("%s\n", __func__);
// open the file
flash_data->avr_flash_fd = open(flash_data->avr_flash_path,
O_RDWR|O_CREAT, 0644);
if (flash_data->avr_flash_fd < 0) {
perror(flash_data->avr_flash_path);
exit(1);
}
// resize and map the file the file
(void)ftruncate(flash_data->avr_flash_fd, avr->flashend + 1);
ssize_t r = read(flash_data->avr_flash_fd, avr->flash, avr->flashend + 1);
if (r != avr->flashend + 1) {
fprintf(stderr, "unable to load flash memory\n");
perror(flash_data->avr_flash_path);
exit(1);
}
}
// avr special flash deinitalization
// here: cleanup the persistent storage
void avr_special_deinit( avr_t* avr, void * data)
{
struct avr_flash *flash_data = (struct avr_flash *)data;
printf("%s\n", __func__);
lseek(flash_data->avr_flash_fd, SEEK_SET, 0);
ssize_t r = write(flash_data->avr_flash_fd, avr->flash, avr->flashend + 1);
if (r != avr->flashend + 1) {
fprintf(stderr, "unable to load flash memory\n");
perror(flash_data->avr_flash_path);
}
close(flash_data->avr_flash_fd);
uart_pty_stop(&uart_pty);
}
int main(int argc, char *argv[])
{
struct avr_flash flash_data;
char boot_path[1024] = "ATmegaBOOT_168_atmega328.ihex";
uint32_t boot_base, boot_size;
char * mmcu = "atmega328p";
uint32_t freq = 16000000;
int debug = 0;
int verbose = 0;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i] + strlen(argv[i]) - 4, ".hex"))
snprintf(boot_path, sizeof(boot_path), "%s", argv[i]);
else if (!strcmp(argv[i], "-d"))
debug++;
else if (!strcmp(argv[i], "-v"))
verbose++;
else {
fprintf(stderr, "%s: invalid argument %s\n", argv[0], argv[i]);
exit(1);
}
}
avr = avr_make_mcu_by_name(mmcu);
if (!avr) {
fprintf(stderr, "%s: Error creating the AVR core\n", argv[0]);
exit(1);
}
uint8_t * boot = read_ihex_file(boot_path, &boot_size, &boot_base);
if (!boot) {
fprintf(stderr, "%s: Unable to load %s\n", argv[0], boot_path);
exit(1);
}
if (boot_base > 32*1024*1024) {
mmcu = "atmega2560";
freq = 20000000;
}
printf("%s booloader 0x%05x: %d bytes\n", mmcu, boot_base, boot_size);
snprintf(flash_data.avr_flash_path, sizeof(flash_data.avr_flash_path),
"simduino_%s_flash.bin", mmcu);
flash_data.avr_flash_fd = 0;
// register our own functions
avr->custom.init = avr_special_init;
avr->custom.deinit = avr_special_deinit;
avr->custom.data = &flash_data;
avr_init(avr);
avr->frequency = freq;
memcpy(avr->flash + boot_base, boot, boot_size);
free(boot);
avr->pc = boot_base;
/* end of flash, remember we are writing /code/ */
avr->codeend = avr->flashend;
avr->log = 1 + verbose;
// even if not setup at startup, activate gdb if crashing
avr->gdb_port = 1234;
if (debug) {
avr->state = cpu_Stopped;
avr_gdb_init(avr);
}
uart_pty_init(avr, &uart_pty);
uart_pty_connect(&uart_pty, '0');
while (1) {
int state = avr_run(avr);
if ( state == cpu_Done || state == cpu_Crashed)
break;
}
}
|