avr: initial commit
[z80.git] / avr / monitor / monitor.c
diff --git a/avr/monitor/monitor.c b/avr/monitor/monitor.c
new file mode 100644 (file)
index 0000000..737998e
--- /dev/null
@@ -0,0 +1,92 @@
+/* AVR Monitor */
+
+#include <avr/pgmspace.h>
+#include <avr/eeprom.h>
+
+#include <stdio.h>
+
+#include "../monitor.h"
+#include "../uart.h"
+#include "../bus.h"
+
+/* variables mirrored in EEPROM */
+uint8_t ee_slots[8] EEMEM;
+uint8_t slots[8];
+
+uint8_t menu_main(void)
+{
+       uint8_t need_save = 0;
+       char    sel = 0;
+
+       while(sel != '9') {
+               printf_P(PSTR("\nAVR Monitor\n===========\n"));
+               printf_P(PSTR("\t(1) Change Slot Configuration\n"));
+               printf_P(PSTR("\t(2) Load IHEX file to memory\n"));
+               printf_P(PSTR("\t(3) Reset System\n"));
+               printf_P(PSTR("\t(9) Exit\n"));
+               printf_P(PSTR("Select: "));
+               sel = uart_getc(NULL);
+               printf_P(PSTR("\n\n"));
+
+               switch(sel) {
+               case '1': if(menu_slots() != 0) need_save = 1; break;
+               case '2': menu_ihex(); break;
+               case '3': bus_reset(); break;
+               case '9': break;
+               default:  printf_P(PSTR("Invalid Entry!\n")); break;
+               }
+       }
+       return(need_save);
+}
+
+void monitor()
+{
+       /* clear break flag and turn on serial echo */
+       bus_lock();
+       uart_break = 0;
+       uart_echo  = 1;
+
+       /* run main menu and save if changes happened and user wishes */
+       if(menu_main()) {
+               printf_P(PSTR("Settings have been changed. Save [y/N]? "));
+               int res = uart_getc(NULL);
+
+               switch((char)res) {
+               case 'y': case 'Y':
+                       printf_P(PSTR("\n\tSaving..."));
+                       for(uint8_t i = 0; i < 8; i++)
+                               eeprom_update_byte(&ee_slots[i], slots[i]);
+                       printf_P(PSTR("done.\n"));
+                       break;
+               default:
+                       printf_P(PSTR("\n\tSettings have NOT been saved.\n"));
+               }
+       }
+       /* turn off serial echo */
+       uart_echo = 0;
+
+       printf_P(PSTR("Returning to system.\n"));
+       bus_unlock();
+}
+
+void monitor_init()
+{
+       uint8_t run_monitor = 0;
+
+       /* read variables from EEPROM */
+       for(uint8_t i = 0; i < 8; i++)
+               slots[i] = eeprom_read_byte(&ee_slots[i]);
+
+       /* check for valid values */
+       for(uint8_t i = 0; i < 8; i++)
+               if(slots[i] == 0xFF)
+                       run_monitor = 1;
+
+       /* run monitor if necessary */
+       if(run_monitor) {
+               printf_P(PSTR("\nEEPROM contains invalid entries.\n"));
+               monitor();
+       }
+       return;
+}
+