#define F_CPU 8'000'000UL

#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>


enum MODE {
    PLUS = 0,
    MINUS = 1,
    MULTIPLY = 2,
    DIVIDE = 3
};

int main() {
    // Leds
    DDRB = 0xff;
    PORTB = 0x00;

    // Buttons
    DDRD = 0x00;
    PORTD = 0xff;

    bool change_mode = true;
    enum MODE mode = 0;

    while(true) {
        if((PIND & 0x01) != change_mode) {
            _delay_ms(50);
            if((PIND & 0x01) != change_mode) {
                change_mode = !change_mode;
                if(change_mode) {
                    ++mode;
                    mode %= 4;
                }
            }
        }

        uint64_t inp = ~PIND >> 1;
        int8_t a, b, c;
        a = inp & 0x07;
        b = inp >> 3 & 0x07;

        switch(mode) {
            case PLUS:
                c = a + b;
                break;
            case MINUS:
                c = a - b;
                break;
            case MULTIPLY:
                c = a * b;
                break;
            case DIVIDE:
                c = a / b;
                break;
        }

        PORTB = (1 << (mode + 4)) | (c & 0x0f);
    }
}
