diff --git a/Makefile.common b/Makefile.common index 103005afe..703c6f8d7 100644 --- a/Makefile.common +++ b/Makefile.common @@ -191,7 +191,7 @@ endif -Wl,--relax,--gc-sections \ -Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \ -I$(SIMAVR)/sim/avr -I../simavr/sim/avr -I../../simavr/sim/avr \ - ${filter-out %.h, ${^}} -o ${@} + ${AVR_LDFLAGS} ${filter-out %.h, ${^}} -o ${@} @${AVR}size ${@}|sed '1d' # this rule has precedence diff --git a/examples/board_simduino/simduino.c b/examples/board_simduino/simduino.c index dfaa5cd4a..48a5695ad 100644 --- a/examples/board_simduino/simduino.c +++ b/examples/board_simduino/simduino.c @@ -115,7 +115,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "%s: Unable to load %s\n", argv[0], boot_path); exit(1); } - if (boot_base > 32*1024*1024) { + if (boot_base >= 32*1024) { // past an ATmega328's flash mmcu = "atmega2560"; freq = 20000000; } @@ -134,12 +134,15 @@ int main(int argc, char *argv[]) avr->custom.init = avr_special_init; avr->custom.deinit = avr_special_deinit; avr->custom.data = &flash_data; + uint8_t * fuse = &avr->fuse[avr->boot_fuse]; + *fuse &= ~((1 << AVR_FUSE_BOOTRST) | AVR_FUSE_BOOTSZ_MASK); + if (strcmp(mmcu, "atmega2560")) + *fuse |= 1 << AVR_FUSE_BOOTSZ0; 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; diff --git a/simavr/cores/sim_core_declare.h b/simavr/cores/sim_core_declare.h index 7d6dac7bc..1b6e54d2e 100644 --- a/simavr/cores/sim_core_declare.h +++ b/simavr/cores/sim_core_declare.h @@ -32,7 +32,7 @@ #define _SFR_IO8(v) ((v)+32) #define _SFR_IO16(v) ((v)+32) #define _SFR_MEM8(v) (v) -#define _BV(v) (v) +#define _BV(v) (1 << (v)) #define _VECTOR(v) (v) /* @@ -62,7 +62,7 @@ # define _FUSE_HELPER { FUSE1_DEFAULT, FUSE1_DEFAULT, FUSE2_DEFAULT, \ FUSE3_DEFAULT, FUSE4_DEFAULT, FUSE5_DEFAULT } #elif FUSE_MEMORY_SIZE == 3 -# define _FUSE_HELPER { LFUSE_DEFAULT, HFUSE_DEFAULT, EFUSE_DEFAULT } +# define _FUSE_HELPER { (uint8_t)LFUSE_DEFAULT, (uint8_t)HFUSE_DEFAULT, (uint8_t)EFUSE_DEFAULT } #elif FUSE_MEMORY_SIZE == 2 # define _FUSE_HELPER { LFUSE_DEFAULT, HFUSE_DEFAULT } #elif FUSE_MEMORY_SIZE == 1 @@ -71,6 +71,14 @@ # define _FUSE_HELPER { 0 } #endif +#if defined(IVSEL) && defined(GICR) +# define _IVSEL_HELPER .ivsel = AVR_IO_REGBIT(GICR, IVSEL), +#elif defined(IVSEL) +# define _IVSEL_HELPER .ivsel = AVR_IO_REGBIT(MCUCR, IVSEL), +#else +# define _IVSEL_HELPER +#endif + #ifdef MCUCSR # define MCU_STATUS_REG MCUCSR #else @@ -85,6 +93,7 @@ .e2end = E2END, \ .vector_size = _vector_size, \ .fuse = _FUSE_HELPER, \ + _IVSEL_HELPER \ .signature = { SIGNATURE_0,SIGNATURE_1,SIGNATURE_2 }, \ .lockbits = 0xFF, \ .reset_flags = {\ diff --git a/simavr/cores/sim_mega128.c b/simavr/cores/sim_mega128.c index 4797f8700..2e144891f 100644 --- a/simavr/cores/sim_mega128.c +++ b/simavr/cores/sim_mega128.c @@ -61,6 +61,8 @@ const struct mcu_t { .core = { .mmcu = "atmega128", DEFAULT_CORE(4), + .boot_min_words = 512, + .boot_fuse = AVR_FUSE_HIGH, .init = m128_init, .reset = m128_reset, diff --git a/simavr/cores/sim_mega1280.c b/simavr/cores/sim_mega1280.c index 2fa0cb912..7cc3fbc8b 100644 --- a/simavr/cores/sim_mega1280.c +++ b/simavr/cores/sim_mega1280.c @@ -66,6 +66,8 @@ const struct mcu_t { .core = { .mmcu = "atmega1280", DEFAULT_CORE(4), + .boot_min_words = 512, + .boot_fuse = AVR_FUSE_HIGH, .init = m1280_init, .reset = m1280_reset, diff --git a/simavr/cores/sim_mega1281.c b/simavr/cores/sim_mega1281.c index 1dd2677c6..0942c2ab9 100644 --- a/simavr/cores/sim_mega1281.c +++ b/simavr/cores/sim_mega1281.c @@ -61,6 +61,8 @@ const struct mcu_t { .core = { .mmcu = "atmega1281", DEFAULT_CORE(4), + .boot_min_words = 512, + .boot_fuse = AVR_FUSE_HIGH, .init = m1281_init, .reset = m1281_reset, diff --git a/simavr/cores/sim_mega128rfa1.c b/simavr/cores/sim_mega128rfa1.c index 49bd444ea..b0e30e350 100644 --- a/simavr/cores/sim_mega128rfa1.c +++ b/simavr/cores/sim_mega128rfa1.c @@ -63,6 +63,8 @@ const struct mcu_t { .core = { .mmcu = "atmega128rfa1", DEFAULT_CORE(4), + .boot_min_words = 512, + .boot_fuse = AVR_FUSE_HIGH, .init = m128rfa1_init, .reset = m128rfa1_reset, diff --git a/simavr/cores/sim_mega128rfr2.c b/simavr/cores/sim_mega128rfr2.c index a584d26c8..7a47fe0fb 100644 --- a/simavr/cores/sim_mega128rfr2.c +++ b/simavr/cores/sim_mega128rfr2.c @@ -89,6 +89,8 @@ const struct mcu_t { .core = { .mmcu = "atmega128rfr2", DEFAULT_CORE(4), + .boot_min_words = 512, + .boot_fuse = AVR_FUSE_HIGH, .init = m128rfr2_init, .reset = m128rfr2_reset, diff --git a/simavr/cores/sim_mega169.c b/simavr/cores/sim_mega169.c index 3ac4f7429..1ee6a6ed9 100644 --- a/simavr/cores/sim_mega169.c +++ b/simavr/cores/sim_mega169.c @@ -48,6 +48,8 @@ const struct mcu_t { .core = { .mmcu = "atmega169p", DEFAULT_CORE(4), + .boot_min_words = 128, + .boot_fuse = AVR_FUSE_HIGH, .init = m169p_init, .reset = m169p_reset, diff --git a/simavr/cores/sim_mega2560.h b/simavr/cores/sim_mega2560.h index f3d2df650..3dc46443d 100644 --- a/simavr/cores/sim_mega2560.h +++ b/simavr/cores/sim_mega2560.h @@ -66,6 +66,8 @@ const struct mcu_t { .core = { .mmcu = "atmega2560", DEFAULT_CORE(4), + .boot_min_words = 512, + .boot_fuse = AVR_FUSE_HIGH, .init = m2560_init, .reset = m2560_reset, diff --git a/simavr/cores/sim_mega328pb.c b/simavr/cores/sim_mega328pb.c index 6b68e370e..28208b345 100644 --- a/simavr/cores/sim_mega328pb.c +++ b/simavr/cores/sim_mega328pb.c @@ -61,6 +61,8 @@ const struct mcu_t { .core = { .mmcu = "atmega328pb", DEFAULT_CORE(4), + .boot_min_words = 256, + .boot_fuse = AVR_FUSE_HIGH, .init = m328pb_init, .reset = m328pb_reset, diff --git a/simavr/cores/sim_megax.h b/simavr/cores/sim_megax.h index 57a0e1e2b..4dda8bc20 100644 --- a/simavr/cores/sim_megax.h +++ b/simavr/cores/sim_megax.h @@ -79,6 +79,12 @@ const struct mcu_t SIM_CORENAME = { .core = { .mmcu = SIM_MMCU, DEFAULT_CORE(SIM_VECTOR_SIZE), +#if FLASHEND == 0x7fff // ATmega32 + .boot_min_words = 256, +#else // ATmega8/16 + .boot_min_words = 128, +#endif + .boot_fuse = AVR_FUSE_HIGH, .init = mx_init, .reset = mx_reset, diff --git a/simavr/cores/sim_megax4.h b/simavr/cores/sim_megax4.h index f00496ea9..451c243cb 100644 --- a/simavr/cores/sim_megax4.h +++ b/simavr/cores/sim_megax4.h @@ -78,6 +78,14 @@ const struct mcu_t SIM_CORENAME = { .core = { .mmcu = SIM_MMCU, DEFAULT_CORE(4), +#if FLASHEND == 0x3fff // ATmega164 + .boot_min_words = 128, +#elif FLASHEND == 0x7fff // ATmega324 + .boot_min_words = 256, +#else // ATmega644/1284 + .boot_min_words = 512, +#endif + .boot_fuse = AVR_FUSE_HIGH, .init = mx4_init, .reset = mx4_reset, diff --git a/simavr/cores/sim_megax8.h b/simavr/cores/sim_megax8.h index a11d223f3..a23a9e491 100644 --- a/simavr/cores/sim_megax8.h +++ b/simavr/cores/sim_megax8.h @@ -76,6 +76,13 @@ const struct mcu_t SIM_CORENAME = { .core = { .mmcu = SIM_MMCU, DEFAULT_CORE(SIM_VECTOR_SIZE), +#if FLASHEND == 0x7fff // ATmega328 + .boot_min_words = 256, + .boot_fuse = AVR_FUSE_HIGH, +#elif FLASHEND > 0xfff // ATmega88/168 + .boot_min_words = 128, + .boot_fuse = AVR_FUSE_EXT, +#endif .init = mx8_init, .reset = mx8_reset, diff --git a/simavr/cores/sim_megaxm1.h b/simavr/cores/sim_megaxm1.h index ce8725835..5479b6e74 100644 --- a/simavr/cores/sim_megaxm1.h +++ b/simavr/cores/sim_megaxm1.h @@ -67,6 +67,12 @@ const struct mcu_t SIM_CORENAME = { .core = { .mmcu = SIM_MMCU, DEFAULT_CORE(SIM_VECTOR_SIZE), +#if FLASHEND == 0x3fff // ATmega16M1 + .boot_min_words = 256, +#else // ATmega64M1 + .boot_min_words = 512, +#endif + .boot_fuse = AVR_FUSE_HIGH, .init = mxm1_init, .reset = mxm1_reset, diff --git a/simavr/cores/sim_megaxu2.h b/simavr/cores/sim_megaxu2.h index cdad6e721..4da1ee604 100644 --- a/simavr/cores/sim_megaxu2.h +++ b/simavr/cores/sim_megaxu2.h @@ -72,6 +72,8 @@ const struct mcu_t SIM_CORENAME = { .core = { .mmcu = SIM_MMCU, DEFAULT_CORE(4), + .boot_min_words = 256, + .boot_fuse = AVR_FUSE_HIGH, .init = mxu2_init, .reset = mxu2_reset, diff --git a/simavr/cores/sim_megaxu4.h b/simavr/cores/sim_megaxu4.h index d7f90c3f3..bcee306b1 100644 --- a/simavr/cores/sim_megaxu4.h +++ b/simavr/cores/sim_megaxu4.h @@ -85,6 +85,8 @@ const struct mcu_t SIM_CORENAME = { .core = { .mmcu = SIM_MMCU, DEFAULT_CORE(4), + .boot_min_words = 256, + .boot_fuse = AVR_FUSE_HIGH, .init = mxu4_init, .reset = mxu4_reset, diff --git a/simavr/sim/avr_watchdog.c b/simavr/sim/avr_watchdog.c index 5166c5464..121ad758d 100644 --- a/simavr/sim/avr_watchdog.c +++ b/simavr/sim/avr_watchdog.c @@ -196,7 +196,8 @@ static void avr_watchdog_reset(avr_io_t * port) } /* TODO could now use the two pending/running IRQs to do the same * as before */ - avr_irq_register_notify(p->watchdog.irq, avr_watchdog_irq_notify, p); + if (p->watchdog.vector) + avr_irq_register_notify(p->watchdog.irq, avr_watchdog_irq_notify, p); } static avr_io_t _io = { diff --git a/simavr/sim/sim_avr.c b/simavr/sim/sim_avr.c index 9ac18caa5..e093c5d2d 100644 --- a/simavr/sim/sim_avr.c +++ b/simavr/sim/sim_avr.c @@ -213,6 +213,23 @@ avr_terminate( avr->flash = avr->data = NULL; } +void +avr_update_reset_vector( + avr_t * avr) +{ + if (avr->boot_min_words) { + uint8_t fuse = avr->fuse[avr->boot_fuse]; + // inverted as fuses read 0 when programmed, 0 is the smallest section + uint8_t bootsz = (~fuse & AVR_FUSE_BOOTSZ_MASK) >> AVR_FUSE_BOOTSZ0; + uint32_t boot_bytes = (avr->boot_min_words * 2) << bootsz; + avr->boot_start = avr->flashend + 1 - boot_bytes; + if (!(fuse & (1 << AVR_FUSE_BOOTRST))) { + avr->reset_pc = avr->boot_start; + avr->pc = avr->reset_pc; + } + } +} + void avr_reset( avr_t * avr) @@ -234,6 +251,7 @@ avr_reset( for(int i = 0x20; i <= avr->ioend; i++) avr->data[i] = 0; _avr_sp_set(avr, avr->ramend); + avr_update_reset_vector(avr); avr->pc = avr->reset_pc; // Likely to be zero for (int i = 0; i < 8; i++) avr->sreg[i] = 0; diff --git a/simavr/sim/sim_avr.h b/simavr/sim/sim_avr.h index d3075038d..328827c1d 100644 --- a/simavr/sim/sim_avr.h +++ b/simavr/sim/sim_avr.h @@ -190,6 +190,11 @@ typedef void (*avr_run_t)( #define AVR_FUSE_HIGH 1 #define AVR_FUSE_EXT 2 +#define AVR_FUSE_BOOTRST 0 +#define AVR_FUSE_BOOTSZ0 1 +#define AVR_FUSE_BOOTSZ1 2 +#define AVR_FUSE_BOOTSZ_MASK ((1 << AVR_FUSE_BOOTSZ1) | (1 << AVR_FUSE_BOOTSZ0)) + /* * Main AVR instance. Some of these fields are set by the AVR "Core" definition files * the rest is runtime data (as little as possible) @@ -308,6 +313,11 @@ typedef struct avr_t { * allow support for bootloaders */ avr_flashaddr_t reset_pc; + // Boot section from the fuses, IVSEL moves the vectors there + uint8_t boot_fuse; + uint16_t boot_min_words; + avr_flashaddr_t boot_start; + avr_regbit_t ivsel; /* Callback for single-bit instructions (SBI, CBI) on the low 32 * I/O registers. If not set, or the function returns zero, a normal @@ -449,6 +459,11 @@ avr_core_allocate( void avr_reset( avr_t * avr); +// sets the boot section from BOOTSZ, and reset_pc and the PC to it when +// BOOTRST is programmed. Otherwise reset_pc and the PC are left alone. +void +avr_update_reset_vector( + avr_t * avr); // Get a pointer to a core IRQ. diff --git a/simavr/sim/sim_elf.c b/simavr/sim/sim_elf.c index 37023bad2..419e09188 100644 --- a/simavr/sim/sim_elf.c +++ b/simavr/sim/sim_elf.c @@ -134,6 +134,7 @@ avr_load_firmware( elf_firmware_t * firmware) { fw_chunk_t *chunk, *ochunk; + int fuses = 0; if (firmware->frequency) avr->frequency = firmware->frequency; @@ -275,6 +276,7 @@ avr_load_firmware( break; case FUSES: do_chunk(chunk, avr->fuse, sizeof avr->fuse, NULL); + fuses = 1; break; case LOCK: do_chunk(chunk, &avr->lockbits, sizeof avr->lockbits, NULL); @@ -288,6 +290,16 @@ avr_load_firmware( chunk = chunk->next; free(ochunk); } + if (fuses) { + /* + * avr_init() set the boot section and the reset vector from the + * default fuses of the part, before the firmware's were loaded + * here. Update them, as on a part programmed before power-on, so + * that BOOTRST and BOOTSZ give the reset vector and the boot + * section the firmware asked for. + */ + avr_update_reset_vector(avr); + } // load the default pull up/down values for ports for (int i = 0; i < 8 && firmware->external_state[i].port; i++) { avr_ioport_external_t e = { diff --git a/simavr/sim/sim_interrupts.c b/simavr/sim/sim_interrupts.c index 4e66ae1ad..601c7143b 100644 --- a/simavr/sim/sim_interrupts.c +++ b/simavr/sim/sim_interrupts.c @@ -293,6 +293,8 @@ avr_service_interrupts( _avr_push_addr(avr, avr->pc); avr_sreg_set(avr, S_I, 0); avr->pc = vector->vector * avr->vector_size; + if (avr_regbit_get(avr, avr->ivsel)) + avr->pc += avr->boot_start; avr_raise_irq(vector->irq + AVR_INT_IRQ_RUNNING, 1); avr_raise_irq(table->irq + AVR_INT_IRQ_RUNNING, vector->vector); diff --git a/tests/Makefile b/tests/Makefile index 3e27043fa..2ac937874 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -38,6 +38,7 @@ axf: ${sources:.c=.axf} hex: atmega88_example.hex at90usb162_usb.axf atmega32u2_usb.axf atmega32u4_usb.axf: usb_test_fw.h +atmega328p_ivsel_boot.axf: AVR_LDFLAGS = -Wl,--section-start=.text=0x7800 ${OBJ}/%.tst: tests.c %.c ifneq ($(E),) diff --git a/tests/atmega328p_ivsel.c b/tests/atmega328p_ivsel.c new file mode 100644 index 000000000..0d39541cf --- /dev/null +++ b/tests/atmega328p_ivsel.c @@ -0,0 +1,65 @@ +/* + atmega328p_ivsel.c + + The application of test_atmega328p_ivsel: it prints A, starts the + bootloader (atmega328p_ivsel_boot.c) unless the bootloader started + it, then takes a Timer0 interrupt through its own vectors (a). + + 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 . + */ + +#include +#include +#include +#include "avr_mcu_section.h" + +AVR_MCU(F_CPU, "atmega328p"); + +#define BOOT_DONE (1 << 1) +#define APP_IRQ (1 << 2) + +static void +put(char ch) +{ + UCSR0B = (1 << TXEN0); + while (!(UCSR0A & (1 << UDRE0))) + ; + UDR0 = ch; +} + +ISR(TIMER0_OVF_vect) +{ + TIMSK0 = 0; + put('a'); + GPIOR0 |= APP_IRQ; +} + +int +main(void) +{ + put('A'); + if (!(GPIOR0 & BOOT_DONE)) + __asm__ volatile ("jmp 0x7800"); + TIMSK0 = (1 << TOIE0); + TCCR0B = (1 << CS00); + sei(); + while (!(GPIOR0 & APP_IRQ)) + ; + cli(); + put('\n'); + sleep_enable(); + sleep_cpu(); +} diff --git a/tests/atmega328p_ivsel_boot.c b/tests/atmega328p_ivsel_boot.c new file mode 100644 index 000000000..3ecaf95e2 --- /dev/null +++ b/tests/atmega328p_ivsel_boot.c @@ -0,0 +1,72 @@ +/* + atmega328p_ivsel_boot.c + + The bootloader of test_atmega328p_ivsel, linked at 0x7800, the boot + section these fuses give: it prints B, takes a Timer0 interrupt with + IVSEL set (b), clears IVSEL and starts the application. + + 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 . + */ + +#include +#include +#include "avr_mcu_section.h" + +AVR_MCU(F_CPU, "atmega328p"); + +FUSES = { + .low = LFUSE_DEFAULT, + .high = FUSE_SPIEN & FUSE_BOOTSZ1 & FUSE_BOOTRST, + .extended = EFUSE_DEFAULT, +}; + +#define BOOT_IRQ (1 << 0) +#define BOOT_DONE (1 << 1) + +static void +put(char ch) +{ + UCSR0B = (1 << TXEN0); + while (!(UCSR0A & (1 << UDRE0))) + ; + UDR0 = ch; +} + +ISR(TIMER0_OVF_vect) +{ + TIMSK0 = 0; + put('b'); + GPIOR0 |= BOOT_IRQ; +} + +int +main(void) +{ + put('B'); + MCUCR = (1 << IVCE); + MCUCR = (1 << IVSEL); + TIMSK0 = (1 << TOIE0); + TCCR0B = (1 << CS00); + sei(); + while (!(GPIOR0 & BOOT_IRQ)) + ; + cli(); + TCCR0B = 0; + MCUCR = (1 << IVCE); + MCUCR = 0; + GPIOR0 |= BOOT_DONE; + __asm__ volatile ("jmp 0"); +} diff --git a/tests/test_atmega328p_ivsel.c b/tests/test_atmega328p_ivsel.c new file mode 100644 index 000000000..308130c49 --- /dev/null +++ b/tests/test_atmega328p_ivsel.c @@ -0,0 +1,58 @@ +/* + test_atmega328p_ivsel.c + + IVSEL and the boot section: the bootloader's fuses give its reset + vector, and its interrupt goes through its own vectors, whether the + reset (BOOTRST) or the application started it. + + 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 . + */ + +#include "tests.h" +#include "sim_avr.h" +#include "sim_elf.h" + +static avr_t * +load(void) +{ + avr_t * avr = tests_init_avr("atmega328p_ivsel.axf"); + elf_firmware_t fw = { 0 }; + + if (elf_read_firmware("atmega328p_ivsel_boot.axf", &fw)) + fail("can't load the bootloader"); + avr_load_firmware(avr, &fw); + return avr; +} + +int main(int argc, char **argv) { + avr_t * avr; + + tests_init(argc, argv); + + avr = load(); + if (avr->pc != 0x7800) + fail("reset at %04x, the bootloader's fuses give 7800", avr->pc); + tests_assert_uart_receive_avr(avr, 100000, "BbAa\n", '0'); + + avr = load(); + avr->fuse[AVR_FUSE_HIGH] |= 1 << AVR_FUSE_BOOTRST; + avr->reset_pc = 0; + avr_reset(avr); + tests_assert_uart_receive_avr(avr, 100000, "ABbAa\n", '0'); + + tests_success(); + return 0; +} diff --git a/tests/test_boot_section.c b/tests/test_boot_section.c new file mode 100644 index 000000000..ff54fe575 --- /dev/null +++ b/tests/test_boot_section.c @@ -0,0 +1,88 @@ +/* + test_boot_section.c + + Boot section start and reset vector from BOOTSZ and BOOTRST, against + the datasheets' "Boot Size Configuration" tables. + + 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 . + */ + +#include "tests.h" +#include "sim_avr.h" + +static const struct { + const char * mmcu; + uint8_t fuse; + uint32_t start[4]; +} parts[] = { + { "atmega88", AVR_FUSE_EXT, { 0x1800, 0x1c00, 0x1e00, 0x1f00 } }, + { "atmega168", AVR_FUSE_EXT, { 0x3800, 0x3c00, 0x3e00, 0x3f00 } }, + { "atmega328p", AVR_FUSE_HIGH, { 0x7000, 0x7800, 0x7c00, 0x7e00 } }, + { "atmega328pb", AVR_FUSE_HIGH, { 0x7000, 0x7800, 0x7c00, 0x7e00 } }, + { "at90usb82", AVR_FUSE_HIGH, { 0x1000, 0x1800, 0x1c00, 0x1e00 } }, + { "atmega8u2", AVR_FUSE_HIGH, { 0x1000, 0x1800, 0x1c00, 0x1e00 } }, + { "at90usb162", AVR_FUSE_HIGH, { 0x3000, 0x3800, 0x3c00, 0x3e00 } }, + { "atmega16u2", AVR_FUSE_HIGH, { 0x3000, 0x3800, 0x3c00, 0x3e00 } }, + { "atmega32u2", AVR_FUSE_HIGH, { 0x7000, 0x7800, 0x7c00, 0x7e00 } }, + { "atmega16u4", AVR_FUSE_HIGH, { 0x3000, 0x3800, 0x3c00, 0x3e00 } }, + { "atmega32u4", AVR_FUSE_HIGH, { 0x7000, 0x7800, 0x7c00, 0x7e00 } }, + { "atmega2560", AVR_FUSE_HIGH, { 0x3e000, 0x3f000, 0x3f800, 0x3fc00 } }, + { "atmega8", AVR_FUSE_HIGH, { 0x1800, 0x1c00, 0x1e00, 0x1f00 } }, + { "atmega16", AVR_FUSE_HIGH, { 0x3800, 0x3c00, 0x3e00, 0x3f00 } }, + { "atmega32", AVR_FUSE_HIGH, { 0x7000, 0x7800, 0x7c00, 0x7e00 } }, + { "atmega164", AVR_FUSE_HIGH, { 0x3800, 0x3c00, 0x3e00, 0x3f00 } }, + { "atmega324", AVR_FUSE_HIGH, { 0x7000, 0x7800, 0x7c00, 0x7e00 } }, + { "atmega644", AVR_FUSE_HIGH, { 0xe000, 0xf000, 0xf800, 0xfc00 } }, + { "atmega1284", AVR_FUSE_HIGH, { 0x1e000, 0x1f000, 0x1f800, 0x1fc00 } }, + { "atmega128", AVR_FUSE_HIGH, { 0x1e000, 0x1f000, 0x1f800, 0x1fc00 } }, + { "atmega1280", AVR_FUSE_HIGH, { 0x1e000, 0x1f000, 0x1f800, 0x1fc00 } }, + { "atmega1281", AVR_FUSE_HIGH, { 0x1e000, 0x1f000, 0x1f800, 0x1fc00 } }, + { "atmega128rfa1", AVR_FUSE_HIGH, { 0x1e000, 0x1f000, 0x1f800, 0x1fc00 } }, + { "atmega128rfr2", AVR_FUSE_HIGH, { 0x1e000, 0x1f000, 0x1f800, 0x1fc00 } }, + { "atmega16m1", AVR_FUSE_HIGH, { 0x3000, 0x3800, 0x3c00, 0x3e00 } }, + { "atmega64m1", AVR_FUSE_HIGH, { 0xe000, 0xf000, 0xf800, 0xfc00 } }, +}; + +int main(int argc, char **argv) { + tests_init(argc, argv); + for (int i = 0; i < sizeof(parts) / sizeof(parts[0]); i++) { + avr_t * avr = avr_make_mcu_by_name(parts[i].mmcu); + avr_init(avr); + if (avr->pc) + fail("%s: reset at %04x", parts[i].mmcu, avr->pc); + for (int bootsz = 0; bootsz < 4; bootsz++) + for (int bootrst = 0; bootrst < 2; bootrst++) { + uint8_t * fuse = &avr->fuse[parts[i].fuse]; + *fuse &= ~((1 << AVR_FUSE_BOOTRST) | AVR_FUSE_BOOTSZ_MASK); + *fuse |= bootsz << AVR_FUSE_BOOTSZ0; + if (bootrst) + *fuse |= 1 << AVR_FUSE_BOOTRST; + avr->reset_pc = 0; + avr_reset(avr); + uint32_t start = parts[i].start[bootsz]; + if (avr->boot_start != start || avr->pc != (bootrst ? 0 : start)) + fail("%s: BOOTSZ %d BOOTRST %d, boot section %05x, reset at %05x", + parts[i].mmcu, bootsz, bootrst, avr->boot_start, avr->pc); + } + avr->reset_pc = 0x100; + avr_reset(avr); + if (avr->pc != 0x100) + fail("%s: reset at %05x, not at reset_pc", parts[i].mmcu, avr->pc); + avr_terminate(avr); + } + tests_success(); + return 0; +}