aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: fe3da0e7032e7ff372c7a079adf2577f87b2d3d2 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <tft.h>
#include <tty.h>
#include <i2c.h>
#include <zeta.h>
#include <input.h>

void
uart_putchar(char c);

static inline void
enable_input_fifo(void)
{
}

static inline void
disable_input_fifo(void)
{
}

static inline void
init_pio(void)
{
	// Control mode - i.e. no parallel port communication
	port_b_ctrl = PIO_MODE_3;
	// A, B and five keys as inputs
	port_b_ctrl = 0x7F;
	// Load interrupt vector
	//  port_b_ctrl = ISR_ADDRESS(port_b_isr_ptr);
	// Interrupt word - interrupt if any pin goes low, mask follows
    //  port_b_ctrl = PIO_INT_CTRL(PIO_INT_EN | PIO_OR | PIO_LOW | PIO_MASK);
	// Mask - 0 means to check line
	//  port_b_ctrl = 0x80;

	/* port_a_ctrl = PIO_MODE_3; */

	/* port_a_ctrl = 0x7C; */

	/* // Load interrupt vector */
	/* port_a_ctrl = ISR_ADDRESS(port_a_isr_ptr); */

	/* port_a_ctrl = PIO_INT_CTRL(PIO_INT_EN | PIO_OR | PIO_HIGH | PIO_MASK); */
	/* port_a_ctrl = 0x83; */

	/* // SDA and SCL high */
	/* port_a_data = 0x03; */
}

static inline void
init_ctc(void)
{
	/* 200Hz clock */
	ctc_channel_1 = CTC_CTRL(CTC_INT_BIT | CTC_PRESCALER_BIT
							 | CTC_TIME_CONST_BIT | CTC_RST_BIT);
	ctc_channel_1 = 0;

	/* 200Hz clock */
	ctc_channel_2 = CTC_CTRL(CTC_INT_BIT | CTC_PRESCALER_BIT
							 | CTC_TIME_CONST_BIT | CTC_RST_BIT);
	ctc_channel_2 = 0;

    /* ctc_channel_3 = (CPU_FREQ / 256 / 144); */

	ctc_channel_3 = CTC_CTRL(CTC_INT_BIT | CTC_PRESCALER_BIT
							 | CTC_TIME_CONST_BIT | CTC_RST_BIT);
	ctc_channel_3 = 0; // 256

	// Interrupt table for CTC
	// Final address is (Ireg << 8) | ctc_isr_ptr | {00/01/10/11} | 0
	ctc_channel_0 = ISR_ADDRESS(ctc0_isr_ptr);
}

static inline void
init_sio(void)
{
	static const u8 sio_a_cfg[] = {
		0b00011000,                      // Reset channel
		4         ,                      // wr4
		0b01000100,                      // X16 clock (115200), one stop bit, no parity
		1         ,                      // wr1
		SIO_RX_INT_MD0 | SIO_RX_INT_MD1, // interrupt on every Rx, no wait function
		3         ,                      // wr3
		0b11000001,                      // enable Rx - 8 bit char
		5         ,                      // wr5
		0b01101000                       // enable Tx - 8 bit char
	};

	static const u8 sio_b_cfg[] = {
		0b00011000,                     // Reset channel
		2         ,                     // load interrupt vector
		ISR_ADDRESS(rx_isr_ptr)         // int_table_rx
	};

    for (u8 i = 0; i < LENGTH(sio_a_cfg); ++i)
        sio_a_ctrl = sio_a_cfg[i];

    for (u8 i = 0; i < LENGTH(sio_b_cfg); ++i)
        sio_b_ctrl = sio_b_cfg[i];
}

// copy code to ram, so function pointer is modifyable

static uint8_t tick = 0;

void
timer(void) __critical __interrupt(5)
{
	static int prescale = 0;

	if (++prescale == 150) {
		prescale = 0;
		tick = 1;
	}
}

/* i2c_start_condition(); */
/* i2c_send(0xA0); */
/* i2c_send(0x00); */
/* i2c_send(0x00); */
/* i2c_restart_condition(); */
/* i2c_send(0xA1); */
/* b = i2c_recv(NACK); */
/* i2c_stop_condition(); */

void
main(void)
{
	tft_init();

	init_ctc();
	init_pio();
	init_sio();

    // Interrupt mode 2
    IM(2);
    // Enable interrupts
    EI;

	// Boot menu
	addstr("1) Programming mode\r\n");
	addstr("2) Normal boot (default)\r\n");
	addstr("3) Boot address 0xC000\r\n");

	for (int i = 5; i > 0; --i) {
		addch('\r');
		addch(i + '0');

		while (!tick) {
			u8 keys = poll_keys();

			if (!(keys & KEY2)) {
				addch('\r');
				bootloader();
			}

			if (!(keys & KEY3)) {
				goto boot;
			}

			if (!(keys & KEY4)) {
				// Definitely safe looking C code ...
				void (*ptr)(void) = (void (*)(void))0xC000;
				ptr();
			}
		}

		DI;
		tick = 0;
		EI;
	}

	addch('\r');

boot:
	addstr("Starting system ...\r\n");
	// Load interrupt vector
	port_b_ctrl = ISR_ADDRESS(port_b_isr_ptr);
	// Interrupt word - interrupt if any pin goes low, mask follows
    port_b_ctrl = PIO_INT_CTRL(PIO_INT_EN | PIO_OR | PIO_LOW | PIO_MASK);
	// Mask - 0 means to check line
	port_b_ctrl = 0x80;
	_menu();

	while (1)
		;
}