Select Git revision
user_cmd.c 2.18 KiB
/*
* user_interpreter.c
*
* Created on: 12 nov. 2023
* Author: Vincent
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdarg.h>
#include "uart.h"
#include "user_cmd.h"
#include "macro.h"
#define PRINT_SYNTAX_ERROR() PRINT("Syntax error! Expected syntax is either <8 characters hexadecimal address> for reading\n or " \
"<8 characters hexadecimal address>=<8 characters hexadecimal value> for writing\n");
bool is_supervisor_mode = false;
//void switch_to_user_mode();
void switch_mode(int control_value);
//void switch_to_supervisor_mode(void);
void SVC_Handler() {
if (is_supervisor_mode) {
switch_mode(USER_CONTROL_VALUE);
} else {
switch_mode(SUPERVISOR_CONTROL_VALUE);
}
is_supervisor_mode = !is_supervisor_mode;
}
void uart_scanf(char *buff) {
char user_input[USER_INPUT_SIZE] = { 0 };
char current = 0;
size_t idx = 0;
while (current != '\r') {
current = uart0_rec_byte_ref();
user_input[idx] = current;
idx++;
}
user_input[idx - 1] = '\0';
memcpy(buff, user_input, USER_INPUT_SIZE);
}
void exec_user_read_write() {
char str[USER_INPUT_SIZE];
int i = 0, coma_nb = 0, value_idx;
unsigned addr, value;
PRINT("(%s) Write an hexadecimal address for reading <addr> or <addr>,<value> for writing :\r\n",
is_supervisor_mode ? "Supervisor" : "User")
;
#if UART_ENABLE
uart_scanf(str);
#else
fflush(stdin);
fscanf(stdin, "%s", str);
#endif
for (i = 0; i < strlen(str); i++) {
if (str[i] == ',') {
if (i == 0 || ++coma_nb > 1) {
PRINT_SYNTAX_ERROR();
return;
}
str[i] = 0;
value_idx = i + 1;
} else if (!isxdigit(str[i])) {
PRINT_SYNTAX_ERROR();
return;
}
}
if ((coma_nb & (i < 2)) || i > 17) {
PRINT_SYNTAX_ERROR();
PRINT("(Bad length!)")
;
return;
}
sscanf(str, "%x", &addr);
if (!coma_nb) { // if read
PRINT("reading address: 0x%08x\n", addr)
;
PRINT("value read: 0x%08x\n", *(unsigned*) addr)
;
} else { // write
sscanf(str + value_idx, "%x", &value);
PRINT("writing address: 0x%08x with 0x%08x\n", addr, value)
;
*(unsigned*) addr = value;
}
}