-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
298 lines (268 loc) · 6.33 KB
/
main.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* A simple user interface for this project
Copyright (C) 2016 Arnie97
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <hpstdio.h>
#include <hpconio.h>
#include "s3c2410.h"
#include "hp39kbd.h"
#include "display.h"
#include "main.h"
const char title[] = "Clock Tuner for HP 39g+";
int
event_handler(unsigned row, unsigned col)
{
// [APLET]
if (row == 0 && col == 7) {
// exit immediately
return 5;
} else {
// wait until the key is released
set_indicator(indicator_wait, TRUE);
while (any_key_pressed);
set_indicator(indicator_wait, FALSE);
}
// [UP]: 0, [DOWN]: 2, [PLOT]: 4, [HOME]: 6
if (col == 6 && !(row & 1)) {
return (row >> 1) + 1;
} else if (row <= 6 && col <= 4) {
int ch = row * 5 - col + 'D'; // letter keys
if (ch == 'D') {
return 0; // [DEL]
} else if (ch < 'D') {
ch++; // skip the [DEL] key after [D]
} else if (ch >= 'T') {
ch--; // skip the [ALPHA] key before [T]
if (ch >= 'X') {
ch--; // skip the [SHIFT] key before [X]
if (row == 6 && col == 0) {
return 6; // [ENTER]
} else if (ch > 'Z') {
return 0;
}
}
}
return ch;
} else if (row == 6 && col == 5) {
return 7; // [SYMB]
}
// unhandled keys
return 0;
}
int
show_system_info(void)
{
clear_screen();
printf(
"%s\n\n"
"v1.2 build 20160903 by Arnie97\n\n",
title
);
delay(50000);
for (int i = 0; i < 6; i++) {
set_indicator((i + 1) % 6, TRUE);
set_indicator(i, FALSE);
delay(400000);
}
int status;
float fclk = clkslow_to_freq(CLKSLOW, FIN);
if (fclk) {
// slow mode
status = 4;
} else {
// normal mode
fclk = mpllcon_to_freq(MPLLCON, FIN);
status = is_valid_mpllcon(MPLLCON);
}
const char *msg[] = {
"UNKNOWN", "DEFAULT", "CHANGED", "PROFILE",
"SLOW MODE", "ENABLED"
};
printf(
"MPLLCON CLKSLOW BANKCON0\n"
"%08x %08x %08x\n"
" (%.2f MHz, %s %s)\n",
*MPLLCON, *CLKSLOW, *BANKCON0,
fclk, msg[status], msg[status == 4? 5: 3]
);
delay(500000);
set_indicator(0, FALSE);
gotoxy(0, 8);
printf(
"Config: [PLOT] Exit: [APLET]"
"Refresh: [HOME] About: [SYMB]"
);
for (;;) {
int k = get_key();
if (k == 3) // [PLOT]
return show_freq_config(0);
else if (k == 4) // [HOME]
return show_system_info();
else if (k == 5) // [APLET]
return 0; // exit program
else if (k == 7) // [SYMB]
return show_credits();
}
}
int
show_freq_config(int page)
{
clear_screen();
printf(
"Select your desired frequency:\n\n"
);
int beg, end;
char c = 'A';
if (page == 0) {
for (int i = 6; i >= 0; i--) {
unsigned clkslow = 0x10 | i;
printf(
"[%c] %-7.2f",
c++, clkslow_to_freq(&clkslow, FIN)
);
}
beg = 0, end = 8;
} else {
beg = 8, end = 23;
}
for (int i = beg; i < end; i++) {
printf(
"[%c] %-7.2f",
c++, mpllcon_to_freq(valid_mpllcon_values + i, FIN)
);
}
gotoxy(0, 8);
printf(
"Back: [HOME] Exit: [APLET]"
"About: [SYMB] %s: [%s]",
page? "Prev": "Next", page? "UP": "DOWN"
);
for (;;) {
int k = get_key();
if (k == 0) // unknown keys
continue;
else if (k == 1 || k == 3) // [UP], [PLOT]
return show_freq_config(0);
else if (k == 2) // [DOWN]
return show_freq_config(1);
else if (k == 4) // [HOME]
return show_system_info();
else if (k == 5) // [APLET]
return 0; // exit program
else if (k == 7) // [SYMB]
return show_credits();
else if ('A' <= k && k < c) // letter keys
return show_freq_confirm(end - c + k);
}
}
int
show_freq_confirm(int choice)
{
clear_screen();
// get register config for the selected frequency
unsigned mpllcon = valid_mpllcon_values[choice < 0? 7: choice];
unsigned clkslow = choice < 0? 0x10 | (-choice - 1): 0x4;
// calculate human readable frequencies
float fclk1 = clkslow_to_freq(CLKSLOW, FIN);
if (!fclk1) {
fclk1 = mpllcon_to_freq(MPLLCON, FIN);
}
float fclk2 = clkslow_to_freq(&clkslow, FIN);
if (!fclk2) {
fclk2 = mpllcon_to_freq(&mpllcon, FIN);
}
printf(
"WARNING!\n"
"THIS OPERATION MAY DAMAGE YOUR\n"
"DEVICE. USE AT YOUR OWN RISK.\n"
"\n"
"MPLLCON: %08x -> %08x\n"
"CLKSLOW: %08x -> %08x\n"
"(Clock: %.2f Mhz -> %.2f MHz)\n",
*MPLLCON, mpllcon, *CLKSLOW, clkslow,
fclk1, fclk2
);
gotoxy(0, 8);
printf(
"Back: [HOME] Exit: [APLET]"
"OK: [ENTER] Cancel: [PLOT]"
);
for (;;) {
int k = get_key();
if (k == 3) // [PLOT]
return show_freq_config(0);
else if (k == 4) // [HOME]
return show_system_info();
else if (k == 5) // [APLET]
return 0; // exit program
else if (k == 6) // [ENTER]
return show_freq_change(mpllcon, clkslow, fclk2);
}
}
int
show_freq_change(unsigned mpllcon, unsigned clkslow, unsigned freq)
{
clear_screen();
printf("Working hard, please hold on...\n\n");
sys_LCDSynch();
BANKCON0->tacc = freq_to_access_cycle(freq);
*MPLLCON = mpllcon;
*CLKSLOW = clkslow;
sys_lcdfix();
delay(100000);
printf("Well done.");
gotoxy(0, 8);
printf(
"Back: [HOME] Exit: [APLET]"
"Config: [PLOT] About: [SYMB]"
);
for (;;) {
int k = get_key();
if (k == 3) // [PLOT]
return show_freq_config(0);
else if (k == 4) // [HOME]
return show_system_info();
else if (k == 5) // [APLET]
return 0; // exit program
else if (k == 7) // [SYMB]
return show_credits();
}
}
int
show_credits(void)
{
clear_screen();
printf(
"%s\n\n"
"Copyright (C) 2005 HP-GCC Team\n"
"Copyright (C) 2016 Arnie97\n"
"\n"
"Clock Tuner is free software; it "
"comes with absolutely no warranty"
"See file 'LICENSE' for details.",
title
);
gotoxy(0, 9);
printf(
"Back: [HOME] Config: [PLOT]"
);
for (;;) {
int k = get_key();
if (k == 3) // [PLOT]
return show_freq_config(0);
else if (k == 4) // [HOME]
return show_system_info();
else if (k == 5) // [APLET]
return 0; // exit program
}
}