avr: initial commit
[z80.git] / avr / monitor / monitor.c
1 /* AVR Monitor */
2
3 #include <avr/pgmspace.h>
4 #include <avr/eeprom.h>
5
6 #include <stdio.h>
7
8 #include "../monitor.h"
9 #include "../uart.h"
10 #include "../bus.h"
11
12 /* variables mirrored in EEPROM */
13 uint8_t ee_slots[8] EEMEM;
14 uint8_t slots[8];
15
16 uint8_t menu_main(void)
17 {
18         uint8_t need_save = 0;
19         char    sel = 0;
20
21         while(sel != '9') {
22                 printf_P(PSTR("\nAVR Monitor\n===========\n"));
23                 printf_P(PSTR("\t(1) Change Slot Configuration\n"));
24                 printf_P(PSTR("\t(2) Load IHEX file to memory\n"));
25                 printf_P(PSTR("\t(3) Reset System\n"));
26                 printf_P(PSTR("\t(9) Exit\n"));
27                 printf_P(PSTR("Select: "));
28                 sel = uart_getc(NULL);
29                 printf_P(PSTR("\n\n"));
30
31                 switch(sel) {
32                 case '1': if(menu_slots() != 0) need_save = 1; break;
33                 case '2': menu_ihex(); break;
34                 case '3': bus_reset(); break;
35                 case '9': break;
36                 default:  printf_P(PSTR("Invalid Entry!\n")); break;
37                 }
38         }
39         return(need_save);
40 }
41
42 void monitor()
43 {
44         /* clear break flag and turn on serial echo */
45         bus_lock();
46         uart_break = 0;
47         uart_echo  = 1;
48
49         /* run main menu and save if changes happened and user wishes */
50         if(menu_main()) {
51                 printf_P(PSTR("Settings have been changed. Save [y/N]? "));
52                 int res = uart_getc(NULL);
53
54                 switch((char)res) {
55                 case 'y': case 'Y':
56                         printf_P(PSTR("\n\tSaving..."));
57                         for(uint8_t i = 0; i < 8; i++)
58                                 eeprom_update_byte(&ee_slots[i], slots[i]);
59                         printf_P(PSTR("done.\n"));
60                         break;
61                 default:
62                         printf_P(PSTR("\n\tSettings have NOT been saved.\n"));
63                 }
64         }
65         /* turn off serial echo */
66         uart_echo = 0;
67
68         printf_P(PSTR("Returning to system.\n"));
69         bus_unlock();
70 }
71
72 void monitor_init()
73 {
74         uint8_t run_monitor = 0;
75
76         /* read variables from EEPROM */
77         for(uint8_t i = 0; i < 8; i++)
78                 slots[i] = eeprom_read_byte(&ee_slots[i]);
79
80         /* check for valid values */
81         for(uint8_t i = 0; i < 8; i++)
82                 if(slots[i] == 0xFF)
83                         run_monitor = 1;
84
85         /* run monitor if necessary */
86         if(run_monitor) {
87                 printf_P(PSTR("\nEEPROM contains invalid entries.\n"));
88                 monitor();
89         }
90         return;
91 }
92