Add Syscall: Exit, PutChar, GetCar, PutString, GetString

This commit is contained in:
Yorick Barbanneau 2021-10-11 22:41:17 +02:00
parent 6f405265a5
commit 80fc250109
15 changed files with 359 additions and 17 deletions

View file

@ -63,6 +63,37 @@ UpdatePC ()
// "which" is the kind of exception. The list of possible exceptions
// are in machine.h.
//----------------------------------------------------------------------
int copyStringFromMachine(int from, char *to, unsigned size)
{
unsigned i = 0;
int res;
// Need to read size-1 to put final /0 if needed
while((i<size-1) && (machine->ReadMem(from+i,1,&res))){
*(to+i) = (char)res;
if ((char)res == '\0'){
return i;
}
i++;
}
*(to+i)='\0';
return size;
}
int copyStringToMachine(int to, char* from, unsigned size)
{
unsigned i;
for (i=0; i < size - 1; i++) {
if (from[i] == '\0') {
break;
}
machine->WriteMem(to+i,1,(int)from[i]);
}
// Write the last /0
machine->WriteMem(to+i,1,'\0');
return i;
}
void
ExceptionHandler (ExceptionType which)
@ -82,6 +113,67 @@ ExceptionHandler (ExceptionType which)
interrupt->Powerdown ();
break;
}
#ifdef CHANGED
case SC_Exit:
{
int ret = machine->ReadRegister(4);
printf("Exit code %d\n", ret);
interrupt->Powerdown();
break;
}
case SC_PutChar:
{
DEBUG ('s', "PutChar.\n");
consoledriver->PutChar((char)machine->ReadRegister(4));
break;
}
case SC_PutString:
{
DEBUG ('s', "PutString.\n");
int readsize;
int addr_start = machine->ReadRegister(4);
do {
char* read = new char[MAX_STRING_SIZE];
readsize = copyStringFromMachine( addr_start, read, MAX_STRING_SIZE);
consoledriver->PutString(read);
// beware, the last element of our buffer was added by
// 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;
} while(readsize == MAX_STRING_SIZE);
break;
}
case SC_GetChar:
{
DEBUG ('s', "GetChar.\n");
// Get the char input an write it to the register n.2
int c = consoledriver->GetChar();
machine->WriteRegister((2), c);
break;
}
case SC_GetString:
{
DEBUG ('s', "GetString.\n");
int writesize = 0;
int addr = machine->ReadRegister(4);
int size = machine->ReadRegister(5);
DEBUG ('s', "size:%d\n", size);
do {
char* write = new char[MAX_STRING_SIZE];
consoledriver->GetString(write, MAX_STRING_SIZE);
printf("write:%s:\n", write);
writesize = copyStringToMachine(addr, write, MAX_STRING_SIZE);
addr += writesize;
printf("Writed:%d\n", writesize);
delete [] write;
} while( writesize == MAX_STRING_SIZE - 1 );
break;
}
#endif
default:
{
printf("Unimplemented system call %d\n", type);