-
-
Save Rabeez/1f87f4dd1d6b37ff853ce6d23d1c6da1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case SC_Open: | |
| { | |
| int filename_ptr = machine->ReadRegister(4); | |
| int len = 150; | |
| char *filename = new char[len]; | |
| int *c = new int; | |
| if (!machine->ReadMem(filename_ptr, 1, c)) // could not read char | |
| return; | |
| // int i = 0; | |
| // while (i < len-1) | |
| for (int i = 0; i < len-1; ++i,++filename_ptr) { | |
| filename[i] = *c; | |
| // ++i; | |
| // ++filename_ptr; | |
| if (!machine->ReadMem(filename_ptr, 1, c)) // could not read char | |
| return; | |
| if ((char) *c == '\0') // string has ended | |
| break; | |
| } | |
| filename[i] = '\0'; | |
| OpenFile *f = fileSystem->Open(filename); | |
| int file_id; | |
| printf("%s file opened", filename); | |
| delete[] filename; | |
| if (f) | |
| file_id = currentThread->space->fileT->addFile(f); | |
| machine->WriteRegister(2, file_id); // return value | |
| int pc; | |
| pc = machine->ReadRegister(PCReg); | |
| machine->WriteRegister(PrevPCReg, pc); | |
| pc = machine->ReadRegister(NextPCReg); | |
| machine->WriteRegister(PCReg, pc); | |
| pc += 4; | |
| machine->WriteRegister(NextPCReg, pc); | |
| break; | |
| } | |
| case SC_Close: | |
| { | |
| int file_id = machine->ReadRegister(4); | |
| OpenFile *f = currentThread->space->fileT->getFile(file_id); | |
| currentThread->space->fileT->removeFile(file_id); | |
| currentThread->space->fileT->printTable(); | |
| printf("%i file closed\n", file_id); | |
| int pc; | |
| pc = machine->ReadRegister(PCReg); | |
| machine->WriteRegister(PrevPCReg, pc); | |
| pc = machine->ReadRegister(NextPCReg); | |
| machine->WriteRegister(PCReg, pc); | |
| pc += 4; | |
| machine->WriteRegister(NextPCReg, pc); | |
| break; | |
| } | |
| case SC_Read: | |
| { | |
| int buff = machine->ReadRegister(4); // write into this | |
| int len = machine->ReadRegister(5); // read this much | |
| int fid = machine->ReadRegister(6); // read from this | |
| int len_read = 0; | |
| if (fid == ConsoleOutput) | |
| return; | |
| char *str = new char[len+1]; | |
| if (fid == ConsoleInput) { | |
| fgets(str, len+1, stdin); | |
| len_read = strlen(str); | |
| } | |
| else { | |
| OpenFile* f = currentThread->space->fileT->getFile(fid); | |
| if (f) | |
| len_read = f->Read(str, len); | |
| else | |
| printf("Bad OpenFileId passed to Read\n"); | |
| } | |
| // int i = 0; | |
| // while (i < len+1) | |
| for (int i = 0; i < len+1; ++i,++buff) { | |
| if (!machine->WriteMem(buff, 1, (int) str[i])) | |
| return; | |
| // i++; | |
| // buff++; | |
| } | |
| machine->WriteRegister(2, len_read); // return value | |
| int pc; | |
| pc = machine->ReadRegister(PCReg); | |
| machine->WriteRegister(PrevPCReg, pc); | |
| pc = machine->ReadRegister(NextPCReg); | |
| machine->WriteRegister(PCReg, pc); | |
| pc += 4; | |
| machine->WriteRegister(NextPCReg, pc); | |
| break; | |
| } | |
| case SC_Write: | |
| { | |
| int buff = machine->ReadRegister(4); // write into this | |
| int len = machine->ReadRegister(5); // read this much | |
| int fid = machine->ReadRegister(6); // read from this | |
| char *str = new char[len+1]; | |
| int *c = new int; | |
| if (!machine->ReadMem(buff, 1, c)) // could not read char | |
| return; | |
| // int i = 0; | |
| // while (i < len) | |
| for (int i = 0; i < len; ++i,++buff) { | |
| str[i] = *c; | |
| // i++; | |
| // buff++; | |
| if (!machine->ReadMem(buff, 1, c)) // could not read char | |
| return; | |
| } | |
| str[i] = '\0'; | |
| if (fid == ConsoleInput) | |
| return; | |
| else if (fid == ConsoleOutput) | |
| printf("%s\n", str); | |
| else { | |
| OpenFile* f = currentThread->space->fileT->getFile(fid); | |
| if (f) | |
| int flag = f->Write(str, len); | |
| else { | |
| printf("%s", "Bad OpenFileId passed to Write\n"); | |
| return; | |
| } | |
| } | |
| int pc; | |
| pc = machine->ReadRegister(PCReg); | |
| machine->WriteRegister(PrevPCReg, pc); | |
| pc = machine->ReadRegister(NextPCReg); | |
| machine->WriteRegister(PCReg, pc); | |
| pc += 4; | |
| machine->WriteRegister(NextPCReg, pc); | |
| break; | |
| } | |
| case SC_Join: | |
| { | |
| int id = machine->ReadRegister(4); | |
| int s = 0; | |
| Thread* t = scheduler->getProcess(id); | |
| if (t) | |
| s = t->join(); | |
| machine->WriteRegister(2, s); // return value | |
| int pc; | |
| pc = machine->ReadRegister(PCReg); | |
| machine->WriteRegister(PrevPCReg, pc); | |
| pc = machine->ReadRegister(NextPCReg); | |
| machine->WriteRegister(PCReg, pc); | |
| pc += 4; | |
| machine->WriteRegister(NextPCReg, pc); | |
| break; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment