Skip to content
Snippets Groups Projects
Commit 314d9910 authored by iliya's avatar iliya
Browse files

feat: ex3 completely done, moving to UART

parent 259f274a
No related branches found
No related tags found
No related merge requests found
...@@ -37,7 +37,6 @@ volatile static unsigned idx_error; ...@@ -37,7 +37,6 @@ volatile static unsigned idx_error;
extern bool is_supervisor_mode; extern bool is_supervisor_mode;
void reset_user_stack(); void reset_user_stack();
void switch_to_user_mode();
void asm_test_fault(); void asm_test_fault();
void change_ret(void *addr); void change_ret(void *addr);
...@@ -62,23 +61,35 @@ void test_supervisor_mode() { ...@@ -62,23 +61,35 @@ void test_supervisor_mode() {
a = *p_out_of_mem; // not OK (out of regions) a = *p_out_of_mem; // not OK (out of regions)
} }
void permute_mode(void) { void ask_user_input(void) {
char buff[64] = { 0 }; char buff[64] = { 0 };
printf("Current: %s\nType 'switch' to toggle between USER and SUPERVISOR: ", char switch_cmd[] = "switch";
char status_cmd[] = "status";
printf("(%s) Would you like to `switch` or display `status` errors : ",
is_supervisor_mode ? "Supervisor" : "User"); is_supervisor_mode ? "Supervisor" : "User");
fflush(stdin); fflush(stdin);
fscanf(stdin, "%s", buff); fscanf(stdin, "%s", buff);
char cmd[] = "switch"; if (strncmp(buff, switch_cmd, strlen(switch_cmd)) == 0) {
if (strncmp(buff, cmd, strlen(cmd)) == 0) { // permute through svc depending on flag
if (!is_supervisor_mode) { // If user mode __asm volatile ("svc 0x69");
__asm volatile ("svc 0x32"); return;
} else { // If supervisor }
switch_to_user_mode();
if (strncmp(buff, status_cmd, strlen(status_cmd)) == 0) {
if (idx_error == 0) {
printf("No errors have occurred yet\n");
return;
}
for (int i = 0; i < idx_error; i++) {
if (i < BUF_SIZE) {
printf("Address that shat the bed : 0x%x\tError code : 0x%x\n",
arr_addr[i], arr_err_code[i]);
}
} }
is_supervisor_mode = !is_supervisor_mode;
} }
printf("Current: %s\n", is_supervisor_mode ? "Supervisor" : "User");
} }
void test_user_mode() { void test_user_mode() {
...@@ -144,7 +155,7 @@ int main(void) { ...@@ -144,7 +155,7 @@ int main(void) {
//test_user_mode(); // to be removed after checking //test_user_mode(); // to be removed after checking
while (1) { while (1) {
permute_mode(); ask_user_input();
exec_user_read_write(); exec_user_read_write();
} }
......
...@@ -15,63 +15,58 @@ ...@@ -15,63 +15,58 @@
#define PRINT_SYNTAX_ERROR() PRINT("Syntax error! Expected syntax is either <8 characters hexadecimal address> for reading\n or " \ #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"); "<8 characters hexadecimal address>=<8 characters hexadecimal value> for writing\n");
bool is_supervisor_mode=false; bool is_supervisor_mode = false;
void switch_to_user_mode();
void switch_to_supervisor_mode(void); void switch_to_supervisor_mode(void);
void SVC_Handler() void SVC_Handler() {
{ if (!is_supervisor_mode) { // If user mode
switch_to_supervisor_mode(); switch_to_supervisor_mode();
} else { // If supervisor
switch_to_user_mode();
}
is_supervisor_mode = !is_supervisor_mode;
} }
void exec_user_read_write() {
void exec_user_read_write()
{
char str[200]; char str[200];
int i=0, coma_nb=0, value_idx; int i = 0, coma_nb = 0, value_idx;
unsigned addr, value; unsigned addr, value;
PRINT("Write an hexadecimal address for reading <addr> or <addr>,<value> for writing (%s):\n", is_supervisor_mode?"Supervisor":"User"); PRINT(
"Write an hexadecimal address for reading <addr> or <addr>,<value> for writing (%s):\n",
is_supervisor_mode ? "Supervisor" : "User");
fflush(stdin); fflush(stdin);
fscanf(stdin, "%s",str); fscanf(stdin, "%s", str);
for (i=0; i<strlen(str); i++) for (i = 0; i < strlen(str); i++) {
{ if (str[i] == ',') {
if (str[i] == ',') if (i == 0 || ++coma_nb > 1) {
{
if (i==0 || ++coma_nb>1)
{
PRINT_SYNTAX_ERROR(); PRINT_SYNTAX_ERROR();
return; return;
} }
str[i]=0; str[i] = 0;
value_idx=i+1; value_idx = i + 1;
} else if (!isxdigit(str[i])) } else if (!isxdigit(str[i])) {
{
PRINT_SYNTAX_ERROR(); PRINT_SYNTAX_ERROR();
return; return;
} }
} }
if ((coma_nb&(i<2)) || i>17) if ((coma_nb & (i < 2)) || i > 17) {
{
PRINT_SYNTAX_ERROR(); PRINT_SYNTAX_ERROR();
PRINT("(Bad length!)"); PRINT("(Bad length!)");
return; return;
} }
sscanf(str, "%x", &addr); sscanf(str, "%x", &addr);
if (!coma_nb) // if read if (!coma_nb) { // if read
{
PRINT("reading address: 0x%08x\n", addr); PRINT("reading address: 0x%08x\n", addr);
PRINT("value read: 0x%08x\n", *(unsigned *)addr); PRINT("value read: 0x%08x\n", *(unsigned*) addr);
} } else { // write
else // write sscanf(str + value_idx, "%x", &value);
{
sscanf(str+value_idx, "%x", &value);
PRINT("writing address: 0x%08x with 0x%08x\n", addr, value); PRINT("writing address: 0x%08x with 0x%08x\n", addr, value);
*(unsigned *)addr=value; *(unsigned*) addr = value;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment