GWRAM.SYSTEM/ram2gs.c

187 lines
4.6 KiB
C
Raw Normal View History

2020-07-22 04:47:25 +00:00
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>
#include "util.h"
2020-09-06 02:45:23 +00:00
#include "gwconio.h"
2023-09-21 08:25:33 +00:00
#include "ram2gs_hal.h"
2023-08-16 07:53:59 +00:00
static void menu()
2020-07-22 04:47:25 +00:00
{
clrscr(); // Clear screen
2020-09-06 02:45:23 +00:00
gwcputsxy(5, 1, "-- RAM2GS Capacity Settings --");
gwcputsxy(4, 3, "Current RAM2GS capacity: ...");
2020-07-22 04:47:25 +00:00
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 6, "Select desired memory capacity:");
2020-07-22 04:47:25 +00:00
2020-09-06 02:45:23 +00:00
gwcputsxy(4, 8, "1. 4 megabytes");
gwcputsxy(4, 10, "2. 8 megabytes");
2020-07-22 04:47:25 +00:00
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 18, "Capacity will be saved until power-off.");
2020-07-22 04:47:25 +00:00
gwcputsxy(1, 20, "To remember capacity and LED setting in");
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 21, "nonvolatile memory, press Apple+number.");
2020-07-22 04:47:25 +00:00
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 23, "Press [Q] to quit without saving.");
2023-09-21 08:25:33 +00:00
}
2023-09-21 08:25:33 +00:00
static void menu_size(uint16_t bankcount) {
gotoxy(29, 3);
printf("%d", bankcount * 64);
gwcputs(" kB");
}
2023-09-21 08:25:33 +00:00
static void menu_led(char enled) {
if (enled) { gwcputsxy(1, 15, "LED enabled. Press [L] to disable LED."); }
else { gwcputsxy(1, 15, "LED disabled. Press [L] to enable LED."); }
}
static void loading_screen()
{
clrscr(); // Clear screen
gwcputsxy(8, 1, "Loading RAM2GS settings...");
2020-07-22 04:47:25 +00:00
}
int ram2gs_main(void)
{
2023-09-21 08:25:33 +00:00
char type;
2023-09-30 00:55:19 +00:00
uint8_t bankcount;
2023-09-21 08:25:33 +00:00
char en8meg = true;
char hasled = true;
char enled = false;
2023-09-21 08:25:33 +00:00
char nvm = false;
2023-09-21 08:25:33 +00:00
int reset_count = 0;
loading_screen();
2023-09-21 08:25:33 +00:00
if (ram2gs_detect(0x00)) { // Altera MAX II / V
type = 0x00;
2023-08-16 07:53:59 +00:00
hasled = !ram2gs_detect(0x04);
2023-09-21 08:25:33 +00:00
} else if (ram2gs_detect(0x04)) { // Lattice MachXO / iCE40 / AGM AG256
type = 0x04;
hasled = true;
} else if (ram2gs_detect(0x08)) { // Lattice MachXO2
type = 0x08;
hasled = true;
2023-08-16 07:53:59 +00:00
} else {
#ifndef SKIP_RAM2GS_DETECT
2020-07-22 04:47:25 +00:00
// If no RAM2GS, show an error message and quit
2023-09-21 08:25:33 +00:00
gwcputsxy(0, 8, " No RAM2GS II detected.");
2020-09-06 02:45:23 +00:00
gwcputsxy(0, 10, " Press any key to quit.");
2020-07-22 04:47:25 +00:00
cgetc(); // Wait for key
clrscr(); // Clear screen before quitting
return EXIT_SUCCESS;
2023-09-21 08:25:33 +00:00
#else
hasled = true;
2023-08-16 07:53:59 +00:00
#endif
2020-07-22 04:47:25 +00:00
}
2023-09-21 08:25:33 +00:00
// Set chip type
ram2gs_hal_set_type(type);
// Print menu
2023-08-16 07:53:59 +00:00
menu();
2023-09-21 08:25:33 +00:00
// Detect and print current capacity
bankcount = ram2gs_getsize();
2023-09-30 00:55:19 +00:00
en8meg = bankcount >= 128;
2023-09-21 08:25:33 +00:00
menu_size(bankcount);
// Detect and print LED menu
#ifndef SKIP_RAM2GS_DETECT
if (hasled) {
enled = !ram2gs_detect(type | 0x02);
menu_led(enled);
}
#endif
2020-07-22 04:47:25 +00:00
// Get user choice from menu
while (true) {
// Set capacity or quit according to keypress.
switch (toupper(cgetc() & 0x7F)) {
case 'Q' : {
clrscr();
return EXIT_SUCCESS;
}
case '1': en8meg = false; ram2gs_set(en8meg, enled); break;
case '2': en8meg = true; ram2gs_set(en8meg, enled); break;
2023-08-16 07:53:59 +00:00
case 'L': {
enled = !enled;
ram2gs_set(en8meg, enled);
2023-09-21 08:25:33 +00:00
if (hasled) {
menu_led(enled);
if (enled) {
wait(1);
ram2gs_flashled(10);
wait(10);
ram2gs_flashled(10);
}
};
2023-08-16 07:53:59 +00:00
continue;
} case 'R': {
2020-07-22 04:47:25 +00:00
reset_count++;
2020-09-06 03:48:51 +00:00
if (reset_count >= 25) {
// Show message about resetting.
2020-07-22 04:47:25 +00:00
clrscr(); // Clear screen
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 8, "Resetting RAM2GS settings.");
gwcputsxy(1, 9, "Do not turn off your Apple.");
2020-07-22 04:47:25 +00:00
2023-09-21 08:25:33 +00:00
ram2gs_erase(); // Erase RAM2GS settings memory
2023-08-16 07:53:59 +00:00
ram2gs_set(1, 0); // Enable 8 megabytes and disable LED
// Wait for >= 500ms on even the fastest systems.
spin(32, 8);
2020-07-22 04:47:25 +00:00
// Show success message and quit
clrscr(); // Clear screen
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 8, "RAM2GS settings reset successfully.");
nvm = true;
2020-07-22 04:47:25 +00:00
goto end;
}
continue;
2020-09-06 03:48:51 +00:00
} default: reset_count = 0; continue;
2020-07-22 04:47:25 +00:00
}
// Check if pressed with apple key. If so, save to nonvolatile memory.
if (read_applekey()) { nvm = true; }
break;
}
// Clear screen in preparation to show saving or success message.
clrscr();
if (nvm) { // Save in NVM if requested.
// Show message about saving.
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 8, "Saving RAM2GS capacity setting.");
gwcputsxy(1, 9, "Do not turn off your Apple.");
2020-07-22 04:47:25 +00:00
// Save capacity in nonvolatile memory.
2023-09-21 08:25:33 +00:00
ram2gs_save_start(en8meg, enled);
// Wait for >= 500ms on even the fastest systems.
spin(33, 8);
2023-09-21 08:25:33 +00:00
// Finish saving
ram2gs_save_end(en8meg, enled);
2020-07-22 04:47:25 +00:00
// Print success message
clrscr(); // Clear screen
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 8, "RAM2GS capacity saved successfully.");
2020-07-22 04:47:25 +00:00
} else { // Print success message if not saving in NVM.
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 8, "RAM2GS capacity set successfully.");
2020-07-22 04:47:25 +00:00
}
end:
if (nvm) { // Show end message for nonvolatile save
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 10, "You may now turn off your Apple.");
gwcputsxy(1, 12, "You may also reset your Apple for");
gwcputsxy(1, 13, "the setting change to take effect.");
2020-07-22 04:47:25 +00:00
} else { // Show end message for volatile save
2020-09-06 02:45:23 +00:00
gwcputsxy(1, 10, "Please reset your Apple for");
gwcputsxy(1, 11, "the setting change to take effect.");
2020-07-22 04:47:25 +00:00
}
// Don't quit. Instead leave prompt asking user to reset.
while(1) { cgetc(); }
}