summaryrefslogtreecommitdiffstats
path: root/sw-avr/rom-loader/main.c
blob: de1dbeea519244978475aaa401053ea56d099bfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>


#include "usart.h"
#include "pinmap.h"

inline void io_init(void)
{
    ADDRL_DDR |= (_BV(ADDRL_bitE) | _BV(ADDRL_bitD));
    ADDRL_DDR |= (0x7<<ADDRL_bitA0); // 3 bits

    ADDRH_DDR |= (_BV(ADDRH_bitE) | _BV(ADDRH_bitD));
    ADDRH_DDR |= (0x7<<ADDRH_bitA0);

    // all output
    DATA_DDR = 0xFF;
}

inline void set_addr(uint16_t addr)
{
    uint8_t i;

    // disable 74LS259 chips
    ADDRL_PORT &= ~(_BV(ADDRL_bitE));
    ADDRH_PORT &= ~(_BV(ADDRH_bitE));

    for (i = 0; i < 8; i++) {
        ADDRL_PORT &= ~(0x7<<ADDRL_bitA0);
        ADDRL_PORT |= (i<<ADDRL_bitA0);

        if (bit_is_set(addr, i))
            ADDRL_PORT |= _BV(ADDRL_bitD);
        else
            ADDRL_PORT &= ~(_BV(ADDRL_bitD));

        if (bit_is_set(addr, i+8))
            ADDRH_PORT |= _BV(ADDRH_bitD);
        else
            ADDRH_PORT &= ~(_BV(ADDRH_bitD));
    }
    
    // enable 74LS259 chips
    ADDRL_PORT |= _BV(ADDRL_bitE);
    ADDRH_PORT |= _BV(ADDRH_bitE);
}

int main(void)
{
    uint16_t addr = 0;

    io_init();
    usart_init();

    while (1) {
        DATA_PORT = usart_recv();
        set_addr(addr);

        _delay_ms(1);

        addr++;
    }

    return 0;
}