Created
August 1, 2021 21:56
-
-
Save raek/c108dcccb452747cd86224d46f9e60ab to your computer and use it in GitHub Desktop.
Mysterious inverter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
demo_blinky.avr.elf: file format elf32-avr | |
Disassembly of section .text: | |
00000000 <__vectors>: | |
0: 0c 94 56 00 jmp 0xac ; 0xac <__ctors_end> | |
4: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
8: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
10: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
14: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
18: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
1c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
20: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
24: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
28: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
2c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
30: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
34: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
38: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
3c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
40: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
44: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
48: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
4c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
50: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
54: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
58: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
5c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
60: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
64: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
68: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
6c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
70: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
74: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
78: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
7c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
80: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
84: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
88: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
8c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
90: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
94: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
98: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
9c: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
a0: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
a4: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
a8: 0c 94 60 00 jmp 0xc0 ; 0xc0 <__bad_interrupt> | |
000000ac <__ctors_end>: | |
ac: 11 24 eor r1, r1 | |
ae: 1f be out 0x3f, r1 ; 63 | |
b0: cf ef ldi r28, 0xFF ; 255 | |
b2: da e0 ldi r29, 0x0A ; 10 | |
b4: de bf out 0x3e, r29 ; 62 | |
b6: cd bf out 0x3d, r28 ; 61 | |
b8: 0e 94 62 00 call 0xc4 ; 0xc4 <main> | |
bc: 0c 94 6b 00 jmp 0xd6 ; 0xd6 <_exit> | |
000000c0 <__bad_interrupt>: | |
c0: 0c 94 00 00 jmp 0 ; 0x0 <__vectors> | |
000000c4 <main>: | |
c4: 51 98 cbi 0x0a, 1 ; 10 | |
c6: 50 9a sbi 0x0a, 0 ; 10 | |
c8: 49 99 sbic 0x09, 1 ; 9 | |
ca: 02 c0 rjmp .+4 ; 0xd0 <main+0xc> | |
cc: 58 9a sbi 0x0b, 0 ; 11 | |
ce: 01 c0 rjmp .+2 ; 0xd2 <main+0xe> | |
d0: 58 98 cbi 0x0b, 0 ; 11 | |
d2: 78 94 sei | |
d4: ff cf rjmp .-2 ; 0xd4 <main+0x10> | |
000000d6 <_exit>: | |
d6: f8 94 cli | |
000000d8 <__stop_program>: | |
d8: ff cf rjmp .-2 ; 0xd8 <__stop_program> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include "pin.h" | |
#include "pin_avr.h" | |
#include <avr/interrupt.h> | |
int main(int argc, char **argv) | |
{ | |
struct pin button_pin = PIN_AVR_MAKE(D, 1); | |
struct pin led_pin = PIN_AVR_MAKE(D, 0); | |
pin_set_dir(&button_pin, PIN_DIR_INPUT); | |
pin_set_dir(&led_pin, PIN_DIR_OUTPUT); | |
pin_write(&led_pin, !pin_read(&button_pin)); | |
sei(); | |
for (;;) { | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdbool.h> | |
#define PIN_DIR_INPUT false | |
#define PIN_DIR_OUTPUT true | |
struct pin; | |
void pin_set_dir(struct pin *pin, bool output); | |
void pin_write(struct pin *pin, bool state); | |
bool pin_read(struct pin *pin); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include <avr/io.h> | |
#include "pin.h" | |
#include "pin_avr.h" | |
__attribute__((always_inline)) | |
inline void pin_set_dir(struct pin *pin, bool output) | |
{ | |
if (output) { | |
*pin->ddr_reg |= pin->bit_pattern; | |
} else { | |
*pin->ddr_reg &= ~(pin->bit_pattern); | |
} | |
} | |
__attribute__((always_inline)) | |
inline void pin_write(struct pin *pin, bool state) | |
{ | |
if (state) { | |
*pin->port_reg |= pin->bit_pattern; | |
} else { | |
*pin->port_reg &= ~(pin->bit_pattern); | |
} | |
} | |
__attribute__((always_inline)) | |
inline bool pin_read(struct pin *pin) | |
{ | |
return (*pin->pin_reg & pin->bit_pattern) != 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdint.h> | |
#include <avr/io.h> | |
#define PIN_AVR_MAKE(letter, bit) \ | |
{ \ | |
.pin_reg = &PIN ## letter, \ | |
.port_reg = &PORT ## letter, \ | |
.ddr_reg = &DDR ## letter, \ | |
.bit_pattern = 1<<(bit), \ | |
} | |
struct pin { | |
volatile uint8_t *pin_reg; | |
volatile uint8_t *port_reg; | |
volatile uint8_t *ddr_reg; | |
uint8_t bit_pattern; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment