#include <stdbool.h>
#include "hal.h"

#define LENGTH 4

typedef struct {
    volatile uint32_t state[LENGTH];
    volatile uint32_t key[LENGTH];
    volatile uint32_t status;
} My_Mem;

#define MY_MEM ((My_Mem*)(0xF0030000))

void writeState(uint32_t * state) {
    for(unsigned i = 0; i < LENGTH; i++) {
        MY_MEM->state[i] = state[i];
    }
}

void readState(uint32_t * state) {
    for(unsigned i = 0; i < LENGTH; i++) {
        state[i] = MY_MEM->state[i];
    }
}

void printState(uint32_t * state) {
    printf("State:\n");
    for(unsigned i = 0; i < LENGTH; i++) {
        printf("%i\n",state[i]);
    }
}

void writeKey(uint32_t * key) {
    for (unsigned i = 0; i < LENGTH; i++) {
        MY_MEM->key[i] = key[i];
    }
}

int main(void) {
    printf("HELLO WORLD\n");

    uint32_t state[LENGTH] = {10000, 3000, 5000, 18156};
    uint32_t key[LENGTH] = {64, 4, 32, 128};

    while (1) {
        printState(state);
        writeState(state);

        writeKey(key);

        MY_MEM->status = 1;
        while (MY_MEM->status != 0);

        readState(state);
        printState(state);

        for(unsigned i = 0; i < LENGTH; i++) {
            state[i] = state[i]+1;
        }
    }
    return 0;
}
