Update all syscall
And add PutInt() and GetInt()
This commit is contained in:
parent
2b7c7ee780
commit
2a3198c949
7 changed files with 95 additions and 31 deletions
|
@ -41,8 +41,8 @@ CFLAGS += -fsanitize=undefined
|
||||||
LDFLAGS += -fsanitize=undefined
|
LDFLAGS += -fsanitize=undefined
|
||||||
|
|
||||||
# décommenté TD1 - partie V
|
# décommenté TD1 - partie V
|
||||||
#CFLAGS += -fsanitize=address
|
CFLAGS += -fsanitize=address
|
||||||
#LDFLAGS += -fsanitize=address -lpthread
|
LDFLAGS += -fsanitize=address -lpthread
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(NACHOS_SYS),MAC_OS_SYS)
|
ifeq ($(NACHOS_SYS),MAC_OS_SYS)
|
||||||
|
|
|
@ -181,6 +181,24 @@ GetString:
|
||||||
syscall
|
syscall
|
||||||
j $31
|
j $31
|
||||||
.end GetString
|
.end GetString
|
||||||
|
|
||||||
|
.globl PutInt
|
||||||
|
.ent PutInt
|
||||||
|
PutInt:
|
||||||
|
addiu $2,$0,SC_PutInt
|
||||||
|
syscall
|
||||||
|
j $31
|
||||||
|
.end PutInt
|
||||||
|
|
||||||
|
.globl GetInt
|
||||||
|
.ent GetInt
|
||||||
|
GetInt:
|
||||||
|
addiu $2,$0,SC_GetInt
|
||||||
|
syscall
|
||||||
|
j $31
|
||||||
|
.end GetInt
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* dummy function to keep gcc happy */
|
/* dummy function to keep gcc happy */
|
||||||
|
|
|
@ -11,7 +11,6 @@
|
||||||
|
|
||||||
#include "copyright.h"
|
#include "copyright.h"
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
//----------------------------------------------------------------------
|
||||||
// SimpleThread
|
// SimpleThread
|
||||||
// Loop 10 times, yielding the CPU to another ready thread
|
// Loop 10 times, yielding the CPU to another ready thread
|
||||||
|
|
|
@ -37,22 +37,22 @@ int ConsoleDriver::GetChar()
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConsoleDriver::PutString(const char s[MAX_STRING_SIZE])
|
void ConsoleDriver::PutString(const char s[])
|
||||||
{
|
{
|
||||||
int i;
|
int i = 0;
|
||||||
for (i = 0; i < MAX_STRING_SIZE; i++){
|
while (*(s+i) != '\0')
|
||||||
if (*(s+i) =='\0'){
|
{
|
||||||
return;
|
|
||||||
}
|
|
||||||
PutChar(*(s+i));
|
PutChar(*(s+i));
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConsoleDriver::GetString(char * s, int n)
|
void ConsoleDriver::GetString(char * s, int n)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for ( i = 0; i < n - 1; i++){
|
for ( i = 0; i < n; i++){
|
||||||
char c = GetChar();
|
char c = GetChar();
|
||||||
|
DEBUG('s', "consoledriver->getstring keycode:%d, position:%d\n", (int)c, i);
|
||||||
if ( c == '\0' || c == '\n' || c == EOF) {
|
if ( c == '\0' || c == '\n' || c == EOF) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -61,4 +61,22 @@ void ConsoleDriver::GetString(char * s, int n)
|
||||||
*(s+i) = '\0';
|
*(s+i) = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ConsoleDriver::PutInt(int n)
|
||||||
|
{
|
||||||
|
char *buffer = new char[MAX_STRING_SIZE];
|
||||||
|
snprintf(buffer,MAX_STRING_SIZE,"%d",n);
|
||||||
|
DEBUG('s', "consoledriver->PutInt buffer:%s\n", buffer);
|
||||||
|
PutString(buffer);
|
||||||
|
delete [] buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ConsoleDriver::GetInt()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
char *buffer = new char[MAX_STRING_SIZE];
|
||||||
|
GetString(buffer,MAX_STRING_SIZE);
|
||||||
|
sscanf(buffer, "%d", &n);
|
||||||
|
delete [] buffer;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
#endif // CHANGED
|
#endif // CHANGED
|
||||||
|
|
|
@ -18,6 +18,8 @@ class ConsoleDriver:dontcopythis {
|
||||||
|
|
||||||
void PutString(const char *s); // Behaves like fputs(3S)
|
void PutString(const char *s); // Behaves like fputs(3S)
|
||||||
void GetString(char *s, int n); // Behaves like fgets(3S)
|
void GetString(char *s, int n); // Behaves like fgets(3S)
|
||||||
|
void PutInt(int n);
|
||||||
|
int GetInt();
|
||||||
private:
|
private:
|
||||||
Console *console;
|
Console *console;
|
||||||
};
|
};
|
||||||
|
|
|
@ -69,7 +69,8 @@ int copyStringFromMachine(int from, char *to, unsigned size)
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
// Need to read size-1 to put final /0 if needed
|
// Need to read size-1 to put final /0 if needed
|
||||||
while((i<size-1) && (machine->ReadMem(from+i,1,&res))){
|
while((i < size) && (machine->ReadMem(from+i,1,&res))){
|
||||||
|
DEBUG('s', "\n ->copyFromMachine keycode:%d", res);
|
||||||
*(to+i) = (char)res;
|
*(to+i) = (char)res;
|
||||||
if ((char)res == '\0'){
|
if ((char)res == '\0'){
|
||||||
return i;
|
return i;
|
||||||
|
@ -83,7 +84,8 @@ int copyStringFromMachine(int from, char *to, unsigned size)
|
||||||
int copyStringToMachine(int to, char* from, unsigned size)
|
int copyStringToMachine(int to, char* from, unsigned size)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
for (i=0; i < size - 1; i++) {
|
for (i=0; i < size ; i++) {
|
||||||
|
DEBUG('s', "\n ->copyToMachine keycode:%d", (int)from[i]);
|
||||||
if (from[i] == '\0') {
|
if (from[i] == '\0') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -134,14 +136,11 @@ ExceptionHandler (ExceptionType which)
|
||||||
int readsize;
|
int readsize;
|
||||||
int addr_start = machine->ReadRegister(4);
|
int addr_start = machine->ReadRegister(4);
|
||||||
do {
|
do {
|
||||||
char* read = new char[MAX_STRING_SIZE];
|
char *read = new char[MAX_STRING_SIZE + 1];
|
||||||
readsize = copyStringFromMachine( addr_start, read, MAX_STRING_SIZE);
|
readsize = copyStringFromMachine( addr_start, read, MAX_STRING_SIZE);
|
||||||
|
DEBUG('s', "\nRead buffer:%s, size:%d\n", read, readsize);
|
||||||
consoledriver->PutString(read);
|
consoledriver->PutString(read);
|
||||||
// beware, the last element of our buffer was added by
|
addr_start += readsize ;
|
||||||
// copyStringFromMachine(), we need to remove one memory case
|
|
||||||
// or the first char of our next read will be
|
|
||||||
// forgotten
|
|
||||||
addr_start += readsize - 1;
|
|
||||||
delete [] read;
|
delete [] read;
|
||||||
} while(readsize == MAX_STRING_SIZE);
|
} while(readsize == MAX_STRING_SIZE);
|
||||||
break;
|
break;
|
||||||
|
@ -151,32 +150,55 @@ ExceptionHandler (ExceptionType which)
|
||||||
DEBUG ('s', "GetChar.\n");
|
DEBUG ('s', "GetChar.\n");
|
||||||
// Get the char input an write it to the register n.2
|
// Get the char input an write it to the register n.2
|
||||||
int c = consoledriver->GetChar();
|
int c = consoledriver->GetChar();
|
||||||
machine->WriteRegister((2), c);
|
machine->WriteRegister(2, c);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case SC_GetString:
|
case SC_GetString:
|
||||||
{
|
{
|
||||||
DEBUG ('s', "GetString.\n");
|
DEBUG('s', "Begin GetString.\n");
|
||||||
int writesize = 0;
|
int writesize = 0;
|
||||||
int addr = machine->ReadRegister(4);
|
int addr = machine->ReadRegister(4);
|
||||||
int size = machine->ReadRegister(5);
|
int size = machine->ReadRegister(5);
|
||||||
DEBUG ('s', "size:%d\n", size);
|
DEBUG('s', "size:%d\n", size);
|
||||||
do {
|
do {
|
||||||
char* write = new char[MAX_STRING_SIZE];
|
// Do not process char before size
|
||||||
consoledriver->GetString(write, MAX_STRING_SIZE);
|
// but for now we don't have any method to determine if
|
||||||
DEBUG('s', "write:%s:\n", write);
|
// string passed to our function est bigger than expected (size)
|
||||||
writesize = copyStringToMachine(addr, write, MAX_STRING_SIZE);
|
int n_char = ( size < MAX_STRING_SIZE) ? size : MAX_STRING_SIZE;
|
||||||
|
|
||||||
|
// allocate on byte more than buffer to put our \0 if needed
|
||||||
|
char *write = new char[n_char + 1];
|
||||||
|
consoledriver->GetString(write, n_char);
|
||||||
|
writesize = copyStringToMachine(addr, write, n_char);
|
||||||
addr += writesize;
|
addr += writesize;
|
||||||
DEBUG('s', "Writed:%d\n", writesize);
|
size = size - writesize;
|
||||||
|
DEBUG('s', "writed:%s, size:%d, remain:%d\n", write, writesize, size);
|
||||||
delete [] write;
|
delete [] write;
|
||||||
} while( writesize == MAX_STRING_SIZE - 1 );
|
} while( size > 0 && writesize == MAX_STRING_SIZE );
|
||||||
|
DEBUG('s', "End GetString\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case SC_PutInt:
|
||||||
|
{
|
||||||
|
DEBUG('s', "PutInt.\n");
|
||||||
|
int n = machine->ReadRegister(4);
|
||||||
|
DEBUG('s', "PutInt parameter: %d\n", n);
|
||||||
|
consoledriver->PutInt(n);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SC_GetInt:
|
||||||
|
{
|
||||||
|
DEBUG('s', "GetInt\n");
|
||||||
|
int n = consoledriver->GetInt();
|
||||||
|
DEBUG('s', "Number entered: %d\n", n);
|
||||||
|
machine->WriteRegister(2,n);
|
||||||
|
break;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
printf("Unimplemented system call %d\n", type);
|
printf("Unimplemented system call %d\n", type);
|
||||||
ASSERT(FALSE);
|
ASSERT(FALSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,8 @@
|
||||||
#define SC_PutString 12
|
#define SC_PutString 12
|
||||||
#define SC_GetChar 13
|
#define SC_GetChar 13
|
||||||
#define SC_GetString 14
|
#define SC_GetString 14
|
||||||
|
#define SC_PutInt 15
|
||||||
|
#define SC_GetInt 16
|
||||||
#endif
|
#endif
|
||||||
#ifdef IN_USER_MODE
|
#ifdef IN_USER_MODE
|
||||||
|
|
||||||
|
@ -137,9 +139,12 @@ void Yield ();
|
||||||
|
|
||||||
#ifdef CHANGED
|
#ifdef CHANGED
|
||||||
void PutChar (char c);
|
void PutChar (char c);
|
||||||
void PutString (char * s);
|
void PutString (char *s);
|
||||||
void GetChar(char c);
|
char GetChar();
|
||||||
void GetString(char * s, int n );
|
void GetString(char *s, int n );
|
||||||
|
void PutInt(int n);
|
||||||
|
int GetInt();
|
||||||
|
|
||||||
#endif // CHANGED
|
#endif // CHANGED
|
||||||
|
|
||||||
#endif // IN_USER_MODE
|
#endif // IN_USER_MODE
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue