150 lines
2.7 KiB
C
150 lines
2.7 KiB
C
#include "menu.h"
|
|
#include "calls.h"
|
|
|
|
static int get_char() {
|
|
int a;
|
|
do {
|
|
a = getchar();
|
|
} while (a < 0);
|
|
return a;
|
|
}
|
|
|
|
static unsigned long get_ulong(void) {
|
|
unsigned long x = 0;
|
|
int ch;
|
|
|
|
do {
|
|
ch = get_char();
|
|
} while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
|
|
|
|
while (ch >= '0' && ch <= '9') {
|
|
x = x * 10 + (unsigned long)(ch - '0');
|
|
ch = getchar();
|
|
if (ch < 0) break;
|
|
}
|
|
|
|
return x;
|
|
}
|
|
|
|
static void print(char* str) {
|
|
while (*str != 0) {
|
|
putchar(*str);
|
|
str++;
|
|
}
|
|
}
|
|
|
|
static void print_new_line(char* str) {
|
|
print(str);
|
|
putchar('\n');
|
|
}
|
|
|
|
static void print_ulong(unsigned long x) {
|
|
char buf[20];
|
|
int i = 0;
|
|
|
|
if (x == 0) {
|
|
putchar('0');
|
|
return;
|
|
}
|
|
|
|
while (x > 0) {
|
|
buf[i++] = '0' + (x % 10);
|
|
x /= 10;
|
|
}
|
|
|
|
while (i--)
|
|
putchar(buf[i]);
|
|
}
|
|
|
|
static void print_menu() {
|
|
print("Enter menu number\n1. Get SBI implementation version\n2. Hart get status\n3. Hart stop\n4. System Shutdown\n5. Help");
|
|
}
|
|
|
|
static void print_sdi_impl() {
|
|
sbiret id = sbi_get_impl_id();
|
|
sbiret spec = sbi_get_spec_version();
|
|
|
|
print("SBI implementation ID ");
|
|
print_ulong(id.value);
|
|
putchar('\n');
|
|
|
|
print("SBI implementation version ");
|
|
print_ulong(spec.value);
|
|
putchar('\n');
|
|
}
|
|
|
|
static const char* hsm_status_to_str(unsigned long st) {
|
|
switch (st) {
|
|
case 0: return "STARTED";
|
|
case 1: return "STOPPED";
|
|
case 2: return "START_PENDING";
|
|
case 3: return "STOP_PENDING";
|
|
case 4: return "SUSPENDED";
|
|
case 5: return "SUSPEND_PENDING";
|
|
case 6: return "RESUME_PENDING";
|
|
default: return "UNKNOWN";
|
|
}
|
|
}
|
|
|
|
static void hart_get_status(void) {
|
|
print("Hart id: ");
|
|
unsigned long hartid = get_ulong();
|
|
print_ulong(hartid);
|
|
putchar('\n');
|
|
|
|
sbiret ret = sbi_hart_get_status(hartid);
|
|
|
|
if (ret.error) {
|
|
print("sbi_hart_get_status error=");
|
|
print_ulong((unsigned long)ret.error);
|
|
putchar('\n');
|
|
return;
|
|
}
|
|
|
|
print("Hart ");
|
|
print_ulong(hartid);
|
|
print(" status=");
|
|
print_ulong((unsigned long)ret.value);
|
|
print(" (");
|
|
print((char*)hsm_status_to_str((unsigned long)ret.value));
|
|
print(")\n");
|
|
}
|
|
|
|
void run_menu() {
|
|
print_menu();
|
|
print("\n> ");
|
|
for (;;) {
|
|
int command = get_char();
|
|
putchar(command);
|
|
putchar('\n');
|
|
switch (command)
|
|
{
|
|
case '1':
|
|
print_sdi_impl();
|
|
break;
|
|
|
|
case '2':
|
|
hart_get_status();
|
|
break;
|
|
|
|
case '3':
|
|
print_new_line("Hart stop");
|
|
sbi_hart_stop();
|
|
break;
|
|
|
|
case '4':
|
|
case 'q':
|
|
print_new_line("Shutting down");
|
|
sbi_shutdown();
|
|
break;
|
|
|
|
case '5':
|
|
print_menu();
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
print("\n> ");
|
|
}
|
|
} |