Skip to content

Commit

Permalink
Tweaked error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
breakintoprogram authored Oct 20, 2022
1 parent 13d00ae commit e21d008
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 56 deletions.
98 changes: 47 additions & 51 deletions src/mos.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Title: AGON MOS - MOS code
* Author: Dean Belfield
* Created: 10/07/2022
* Last Updated: 13/10/2022
* Last Updated: 20/10/2022
*
* Modinfo:
* 11/07/2022: Added mos_cmdDIR, mos_cmdLOAD, removed mos_cmdBYE
Expand All @@ -16,6 +16,7 @@
* 25/09/2022: Added mos_GETERROR, mos_MKDIR; mos_input now sets first byte of buffer to 0
* 03/10/2022: Added mos_cmdSET
* 13/10/2022: Added mos_OSCLI and supporting code
* 20/10/2022: Tweaked error handling
*/

#include <eZ80.h>
Expand Down Expand Up @@ -58,7 +59,7 @@ static t_mosCommand mosCommands[] = {

// Array of file errors; mapped by index to the error numbers returned by FatFS
//
static char * mos_fileErrors[] = {
static char * mos_errors[] = {
"OK",
"Error accessing SD card",
"Assertion failed",
Expand All @@ -78,15 +79,16 @@ static char * mos_fileErrors[] = {
"Volume locked",
"LFN working buffer could not be allocated",
"Too many open files",
"Invalid parameter"
"Invalid parameter",
"Invalid command",
};

// Output a file error
// Parameters:
// - error: The FatFS error number
//
void mos_fileError(int error) {
printf("\n\r%s\n\r", mos_fileErrors[error]);
void mos_error(int error) {
printf("\n\r%s\n\r", mos_errors[error]);
}

// Wait for a keycode character from the VPD
Expand Down Expand Up @@ -185,46 +187,42 @@ BOOL mos_parseString(char * ptr, char ** p_Value) {
// Execute a MOS command
// Parameters:
// - buffer: Pointer to a zero terminated string that contains the MOS command with arguments
// Returns:
// - MOS error code
//
void mos_exec(char * buffer) {
int mos_exec(char * buffer) {
char * ptr;
int status;
int fr = 20;
int (*func)(char * ptr);

ptr = strtok(buffer, " ");
if(ptr != NULL) {
func = mos_getCommand(ptr);
if(func != 0) {
status = func(ptr);
if(status != 0) {
printf("Bad Parameters\n\r");
}
}
else {
printf("%cInvalid Command\n\r", MOS_prompt);
fr = func(ptr);
}
}
return fr;
}

// DIR command
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdDIR(char * ptr) {
FRESULT fr;

fr = mos_DIR();
mos_fileError(fr);
return 0;
return fr;
}

// LOAD <filename> <addr> command
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdLOAD(char * ptr) {
FRESULT fr;
Expand All @@ -234,19 +232,18 @@ int mos_cmdLOAD(char * ptr) {
if(
!mos_parseString(NULL, &filename)
) {
return 1;
return 19; // Bad Parameter
}
if(!mos_parseNumber(NULL, &addr)) addr = MOS_defaultLoadAddress;
fr = mos_LOAD(filename, addr, 0);
mos_fileError(fr);
return 0;
return fr;
}

// SAVE <filename> <addr> <len> command
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdSAVE(char * ptr) {
FRESULT fr;
Expand All @@ -259,18 +256,17 @@ int mos_cmdSAVE(char * ptr) {
!mos_parseNumber(NULL, &addr) ||
!mos_parseNumber(NULL, &size)
) {
return 1;
return 19; // Bad Parameter
}
fr = mos_SAVE(filename, addr, size);
mos_fileError(fr);
return 0;
return fr;
}

// DEL <filename> command
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdDEL(char * ptr) {
char * filename;
Expand All @@ -280,24 +276,23 @@ int mos_cmdDEL(char * ptr) {
if(
!mos_parseString(NULL, &filename)
) {
return 1;
return 19; // Bad Parameter
}
fr = mos_DEL(filename);
mos_fileError(fr);
return 0;
return fr;
}

// JMP <addr> command
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdJMP(char *ptr) {
UINT24 addr;
void (* dest)(void) = 0;
if(!mos_parseNumber(NULL, &addr)) {
return 1;
return 19; // Bad Parameter
};
dest = (void *)addr;
dest();
Expand All @@ -308,7 +303,7 @@ int mos_cmdJMP(char *ptr) {
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdRUN(char *ptr) {
UINT24 addr;
Expand All @@ -322,7 +317,7 @@ int mos_cmdRUN(char *ptr) {
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdCD(char * ptr) {
char * path;
Expand All @@ -332,18 +327,17 @@ int mos_cmdCD(char * ptr) {
if(
!mos_parseString(NULL, &path)
) {
return 1;
return 19; // Bad Parameter
}
fr = f_chdir(path);
mos_fileError(fr);
return 0;
return fr;
}

// REN <filename1> <filename2> command
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdREN(char *ptr) {
FRESULT fr;
Expand All @@ -354,18 +348,17 @@ int mos_cmdREN(char *ptr) {
!mos_parseString(NULL, &filename1) ||
!mos_parseString(NULL, &filename2)
) {
return 1;
return 19; // Bad Parameter
}
fr = mos_REN(filename1, filename2);
mos_fileError(fr);
return 0;
return fr;
}

// MKDIR <filename> command
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdMKDIR(char * ptr) {
char * filename;
Expand All @@ -375,18 +368,17 @@ int mos_cmdMKDIR(char * ptr) {
if(
!mos_parseString(NULL, &filename)
) {
return 1;
return 19; // Bad Parameter
}
fr = mos_MKDIR(filename);
mos_fileError(fr);
return 0;
return fr;
}

// SET <option> <value> command
// Parameters:
// - ptr: Pointer to the argument string in the line edit buffer
// Returns:
// - true if the function succeeded, otherwise false
// - MOS error code
//
int mos_cmdSET(char * ptr) {
char * command;
Expand All @@ -396,7 +388,7 @@ int mos_cmdSET(char * ptr) {
!mos_parseString(NULL, &command) ||
!mos_parseNumber(NULL, &value)
) {
return 1;
return 19; // Bad Parameter
}
if(strcmp(command, "KEYBOARD") == 0 && value < 2) {
putch(0x17);
Expand All @@ -405,7 +397,7 @@ int mos_cmdSET(char * ptr) {
putch(value & 0xFF);
return 0;
}
return 1;
return 19; // Bad Parameter
}

// Load a file from SD card to memory
Expand Down Expand Up @@ -680,14 +672,18 @@ char mos_FEOF(UINT8 fh) {
// - size: Size of buffer
//
void mos_GETERROR(UINT8 errno, INT24 address, INT24 size) {
strncpy((char *)address, mos_fileErrors[errno], size - 1);
strncpy((char *)address, mos_errors[errno], size - 1);
}

// OSCLI
// Parameters
// - cmd: Address of the command entered
// Returns:
// - MOS error code
//
void mos_OSCLI(char * cmd) {
mos_exec(cmd);
UINT24 mos_OSCLI(char * cmd) {
UINT24 fr;
fr = mos_exec(cmd);
return fr;
}

9 changes: 5 additions & 4 deletions src/mos.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Title: AGON MOS - MOS code
* Author: Dean Belfield
* Created: 10/07/2022
* Last Updated: 25/09/2022
* Last Updated: 20/10/2022
*
* Modinfo:
* 11/07/2022: Removed mos_cmdBYE, Added mos_cmdLOAD
Expand All @@ -15,6 +15,7 @@
* 05/09/2022: Added mos_cmdREN, mos_cmdBOOT; moved mos_EDITLINE into mos_editline.c
* 25/09/2022: Added mos_GETERROR, mos_MKDIR
* 13/10/2022: Added mos_OSCLI and supporting code
* 20/10/2022: Tweaked error handling
*/

#ifndef MOS_H
Expand All @@ -32,12 +33,12 @@ typedef struct {
FIL fileObject;
} t_mosFileObject;

void mos_fileError(int error);
void mos_error(int error);

char mos_getkey(void);
UINT24 mos_input(char * buffer, int bufferLength);
void * mos_getCommand(char * ptr);
void mos_exec(char * buffer);
int mos_exec(char * buffer);

BOOL mos_parseNumber(char * ptr, UINT24 * p_Value);
BOOL mos_parseString(char * ptr, char ** p_Value);
Expand Down Expand Up @@ -70,6 +71,6 @@ void mos_FPUTC(UINT8 fh, char c);
char mos_FEOF(UINT8 fh);

void mos_GETERROR(UINT8 errno, INT24 address, INT24 size);
void mos_OSCLI(char * cmd);
UINT24 mos_OSCLI(char * cmd);

#endif MOS_H
6 changes: 5 additions & 1 deletion src/mos_api.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; Title: AGON MOS - API code
; Author: Dean Belfield
; Created: 24/07/2022
; Last Updated: 13/10/2022
; Last Updated: 20/10/2022
;
; Modinfo:
; 03/08/2022: Added a handful of MOS API calls and stubbed FatFS calls
Expand All @@ -11,6 +11,7 @@
; 05/09/2022: Added mos_REN
; 24/09/2022: Error codes returned for MOS commands
; 13/10/2022: Added mos_OSCLI and supporting code
; 20/10/2022: Tweaked error handling

.ASSUME ADL = 1
Expand Down Expand Up @@ -473,6 +474,8 @@ mos_api_getError: LD A, MB ; Check if MBASE is 0
; HLU: Pointer the the MOS command string
; DEU: Pointer to additional command structure
; BCU: Number of additional commands
; Returns:
; A: MOS error code
;
mos_api_oscli: LD A, MB ; Check if MBASE is 0
OR A, A
Expand All @@ -485,6 +488,7 @@ mos_api_oscli: LD A, MB ; Check if MBASE is 0
;
$$: PUSH HL ; char * buffer
CALL _mos_OSCLI
LD A, L ; Return vaue in HLU, put in A
POP HL
RET
Expand Down

0 comments on commit e21d008

Please sign in to comment.