Skip to content

Instantly share code, notes, and snippets.

@Grazfather
Created November 19, 2016 02:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grazfather/32d7c90322b48a13c4d6134d25cc8e0a to your computer and use it in GitHub Desktop.
Save Grazfather/32d7c90322b48a13c4d6134d25cc8e0a to your computer and use it in GitHub Desktop.
QiwiCTF 2016 RE300_3 simulation code
#
# 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/>.
target= qiwitask
simavr = ../../
IPATH = .
IPATH += ../parts
IPATH += ${simavr}/include
IPATH += ${simavr}/simavr/sim
VPATH = .
VPATH += ../parts
LDFLAGS += -lpthread
all: obj ${target}
include ${simavr}/Makefile.common
board = ${OBJ}/${target}.elf
${board} : ${OBJ}/button.o
${board} : ${OBJ}/uart_pty.o
${board} : ${OBJ}/${target}.o
${target}: ${board}
@echo $@ done
clean: clean-${OBJ}
rm -rf *.a ${target} *.vcd
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include "sim_avr.h"
#include "avr_ioport.h"
#include "sim_elf.h"
#include "sim_gdb.h"
#include "uart_pty.h"
#include "button.h"
button_t buttons[8];
uart_pty_t uart_pty;
avr_t * avr = NULL;
static void * avr_run_thread(void * oaram)
{
char counter = 0;
while (1) {
avr_run(avr);
for (int i = 0; i < 8; i++) {
if (counter & (1 << i))
button_press(&buttons[i], 1000);
}
counter++;
}
return NULL;
}
int main(int argc, char *argv[])
{
elf_firmware_t f;
const char *fname = "task.elf";
const char *mmcu = "atmega328p";
elf_read_firmware(fname, &f);
strcpy(f.mmcu, mmcu);
printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu);
// Create AVR
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);
for (int i = 0; i < 8; i++)
{
button_init(avr, &buttons[i], "button");
// "connect" the output irw of the button to the port pin of the AVR
avr_connect_irq(
buttons[i].irq + IRQ_BUTTON_OUT,
avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), i));
}
// Setup GDB server
avr->gdb_port = 1234;
if (1) {
//avr->state = cpu_Stopped;
avr_gdb_init(avr);
}
// Setup uart
uart_pty_init(avr, &uart_pty);
uart_pty_connect(&uart_pty, '0');
// Run AVR
avr_run_thread(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment