Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- /*These variables are associated with the implementation of the VM*/
- int fp ;
- int i ;
- int j, k ;
- char input_line [7] ;
- /*These are variables representing the VM itself*/
- char IR[6] ;
- int PC = 0 ;
- int P[4]; //these are the pointer registers
- int R[4] ; //GP regs
- int ACC ;
- int PSW[2] ;
- char memory [100][6] ; //this is the program memory for first program
- short int opcode ; //nice to know what we are doing
- int program_line = 0 ;
- /* Provide implementation of the helper functions. You are free to provide as
- many additional helper functions as desired.
- */
- //This function returns the integer value of operand 1
- //when this operand is an immediate two-byte integer.
- int ParseOp1 (char *IR) {
- int parseVal;
- int val1 = (int)((IR[2]) - 48);
- int val2 = (int)((IR[3]) - 48);
- parseVal = (10 * val1) + val2;
- return parseVal;
- }
- // returns the integer value of operand 2 when this operand is a two-byte integer.
- int ParseOp2 (char *IR) {
- int parseVal;
- int val1 = (int)((IR[4]) - 48);
- int val2 = (int)((IR[5]) - 48);
- parseVal = (10 * val1) + val2;
- return parseVal;
- }
- //returns the integer value of operands 1 and 2 combined to form a 4-byte integer.
- int ParseOP1andOP2Imm(char *IR)
- {
- int parseVal;
- parseVal = ParseOp1(IR) * 100;
- parseVal += ParseOp2(IR);
- return parseVal;
- }
- // returns the register number of the register used as operand 1 of an instruction.
- // Can be either Pointer or General-Purpose register.
- int ParseOp1Reg (char *IR)
- {
- int val = (int)((IR[3]) - 48);
- return val;
- }
- // returns the register number of a register used as operand 2 of an instruction.
- // Can be either a Pointer or General-Purpose register.
- int ParseOp2Reg (char *IR)
- {
- int val = (int)((IR[5]) - 48);
- return val;
- }
- // returns the data stored at memory location Memory_Location
- int FetchData(int Memory_Location)
- {
- int parseVal;
- parseVal = (memory[Memory_Location][2] - 48) * 1000;
- parseVal += (memory[Memory_Location][3] - 48) * 100;
- parseVal += (memory[Memory_Location][4] - 48) * 10;
- parseVal += (memory[Memory_Location][5] - 48);
- return parseVal;
- }
- //Prints out the contents of the IR on the same line.
- void PrintIR(char *IR)
- {
- printf("IR:");
- for (int i = 0; i < 6; ++i)
- {
- printf("%c",IR[i]);
- }
- }
- void printMEM(int upto){
- for(int i=0; i<upto; i++){
- printf("%c %c %c %c %c %c \n", memory[i][0],memory[i][1],
- memory[i][2],memory[i][3],
- memory[i][4],memory[i][5]);
- }
- }
- //prints out the contents of memory from row 0 to row upto.
- //This should print out all instructions and data stored in memory.
- //Now its time for the instruction execution functions.
- // Here is the first.
- int OP0(char *IR)
- { int PREG, VAL ;
- printf("Opcode = 00. Load Pointer Immediate\n");
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR) ;
- VAL = ParseOp2 (IR) ;
- P[PREG] = VAL;
- printf("%d has been loaded into P%d \n", PREG, VAL);
- return 1;
- }
- int OP1(char *IR)
- { int PREG, VAL ;
- printf("Opcode = 01. Add to Pointer Immediate\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR) ;
- VAL = ParseOp2 (IR) ;
- P[PREG] += VAL;
- printf("%d has been added to P%d \n", PREG, VAL);
- return 1;
- }
- int OP2(char *IR)
- { int PREG, VAL ;
- printf("Opcode = 02. Subtract From Pointer Immediate\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR) ;
- VAL = ParseOp2 (IR) ;
- P[PREG] -= VAL;
- printf("%d has been subtracted from P%d \n", PREG, VAL);
- return 1;
- }
- int OP3(char *IR)
- {
- printf("Opcode = 03. Load Accumulator Immediate\n") ;
- PrintIR(IR) ;
- ACC = ParseOP1andOP2Imm(IR);
- printf("%d has been loaded into the accumulator \n", ACC);
- return 1;
- }
- int OP4(char *IR)
- { int VAL;
- printf("Opcode = 04. Load Accumulator Register Addressing\n") ;
- PrintIR(IR) ;
- VAL = ParseOp1Reg(IR);
- ACC = P[VAL];
- printf("%d has been loaded into the accumulator \n", ACC);
- return 1;
- }
- int OP5(char *IR)
- {
- int VAL;
- printf("Opcode = 05. Load Accumulator Direct Addressing\n") ;
- PrintIR(IR) ;
- VAL = ParseOp1(IR);
- ACC = FetchData(VAL);
- printf("%d has been loaded into the accumulator \n", ACC);
- return 1;
- }
- int OP6(char *I)
- { int VAL, tmpACC;
- printf("Opcode = 06. Store Accumulator Register Addressing\n") ;
- PrintIR(IR) ;
- tmpACC = ACC;
- VAL = ParseOp1Reg(IR);
- memory[P[VAL]][5] = tmpACC%10;
- tmpACC /= 10;
- memory[P[VAL]][4] = tmpACC%10;
- tmpACC /= 10;
- memory[P[VAL]][3] = tmpACC%10;
- tmpACC /= 10;
- memory[P[VAL]][2] = tmpACC%10;
- memory[P[VAL]][1] = 90;
- memory[P[VAL]][0] = 90;
- printf("%d has been saved in memory location %d\n", ACC,P[VAL]);
- return 1;
- }
- int OP7(char *IR)
- { int VAL, tmpACC;
- printf("Opcode = 07. Store Accumulator Direct Addressing\n");
- PrintIR(IR) ;
- tmpACC = ACC;
- VAL = ParseOp1(IR);
- memory[VAL][5] = tmpACC%10;
- tmpACC /= 10;
- memory[VAL][4] = tmpACC%10;
- tmpACC /= 10;
- memory[VAL][3] = tmpACC%10;
- tmpACC /= 10;
- memory[VAL][2] = tmpACC%10;
- memory[VAL][1] = 90;
- memory[VAL][0] = 90;
- printf("%d has been saved in memory location %d\n", ACC,VAL);
- return 1;
- }
- int OP8(char *IR) {
- int PREG, VAL, tmp ;
- printf("Opcode = 08. Store Register to memory: Register Addressing\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR) ;
- VAL = ParseOp2Reg (IR) ;
- tmp = R[PREG];
- memory[P[VAL]][5] = tmp%10;
- tmp /= 10;
- memory[P[VAL]][4] = tmp%10;
- tmp /= 10;
- memory[P[VAL]][3] = tmp%10;
- tmp /= 10;
- memory[P[VAL]][2] = tmp%10;
- tmp /= 10;
- memory[P[VAL]][1] = 90;
- memory[P[VAL]][0] = 90;
- printf("%d has been save in memory location %d \n", R[PREG], P[VAL]);
- return 1;
- }
- int OP9(char *IR) {
- int PREG, VAL, tmp ;
- printf("Opcode = 09. Store Register to memory: Direct Addressing\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR) ;
- VAL = ParseOp2(IR) ;
- tmp = R[PREG];
- memory[VAL][5] = tmp%10;
- tmp /= 10;
- memory[VAL][4] = tmp%10;
- tmp /= 10;
- memory[VAL][3] = tmp%10;
- tmp /= 10;
- memory[VAL][2] = tmp%10;
- tmp /= 10;
- memory[VAL][1] = 90;
- memory[VAL][0] = 90;
- printf("%d has been save in memory location %d \n", R[PREG], VAL);
- return 1;
- }
- int OP10(char *IR) {
- int PREG, VAL;
- printf("Opcode = 10. Load Register from memory: Register Addressing \n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR) ;
- VAL = ParseOp2Reg(IR) ;
- R[PREG] = FetchData(P[VAL]);
- printf("R%d has been loaded with value %d \n", PREG, R[PREG]);
- return 1;
- }
- int OP11(char *IR) {
- int PREG, VAL, tmp ;
- printf("Opcode = 11. Load Register from memory: Direct Addressing \n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR) ;
- VAL = ParseOp2(IR) ;
- R[PREG] = FetchData(VAL);
- printf("R%d has been loaded with value %d \n", PREG, R[PREG]);
- return 1;
- }
- int OP12(char *IR) {
- int VAL;
- printf("Opcode = 12. Load Register R0 Immediate\n") ;
- PrintIR(IR) ;
- R[0] = ParseOP1andOP2Imm(IR);
- printf("R0 has been loaded with value %d \n", R[0]);
- return 1;
- }
- int OP13(char *IR) {
- int PREG, VAL;
- printf("Opcode = 13. Register to Register Transfer\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR) ;
- VAL = ParseOp2Reg(IR) ;
- R[PREG] = R[VAL];
- printf("R%d has been loaded with value %d \n", PREG, R[PREG]);
- return 1;
- }
- int OP14(char *IR) {
- int PREG;
- printf("Opcode = 14. Load Accumulator from Register\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR);
- ACC = R[PREG];
- printf("ACC has been loaded with value %d \n", ACC);
- return 1;
- }
- int OP15(char *IR) {
- int PREG;
- printf("Opcode = 15. Load Register from Accumulator\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR);
- R[PREG] = ACC;
- printf("R%d has been loaded with value %d \n", PREG, R[PREG]);
- return 1;
- }
- int OP16(char *IR) {
- int VAL;
- printf("Opcode = 16. Add Accumulator Immediate\n") ;
- PrintIR(IR) ;
- VAL= ParseOP1andOP2Imm(IR);
- ACC += VAL;
- printf("%d has been added to the accumulator \n", VAL );
- return 1;
- }
- int OP17(char *IR) {
- int VAL;
- printf("Opcode = 17. Subtract Accumulator Immediate\n") ;
- PrintIR(IR) ;
- VAL= ParseOP1andOP2Imm(IR);
- ACC -= VAL;
- printf("%d has been subtracted from the accumulator \n", VAL );
- return 1;
- }
- int OP18(char *IR) {
- int PREG;
- printf("Opcode = 18. Add contents of Register to Accumulator\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR);
- ACC += R[PREG];
- printf("R%d has been added to the accumulator \n", R[PREG]);
- return 1;
- }
- int OP19(char *IR) {
- int PREG;
- printf("Opcode = 19. Subtract contents of Register from Accumulator\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR);
- ACC -= R[PREG];
- printf("R%d has been subtracted from the accumulator \n", R[PREG]);
- return 1;
- }
- int OP20(char *IR) {
- int PREG, VAL;
- printf("Opcode = 20. Add Accumulator Register Addressing\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR);
- VAL = FetchData(P[PREG]);
- ACC += VAL;
- printf("%d has been added to the accumulator \n", P[PREG]);
- return 1;
- }
- int OP21(char *IR) {
- int PREG, VAL;
- printf("Opcode = 21. Add Accumulator Direct Addressing\n") ;
- PrintIR(IR);
- PREG = ParseOp1(IR);
- VAL = FetchData(PREG);
- ACC += VAL;
- printf("%d has been added to the accumulator \n", VAL);
- return 1;
- }
- int OP22(char *IR) {
- int PREG, VAL;
- printf("Opcode = 22. Subtract Accumulator Register Addressing\n") ;
- PrintIR(IR) ;
- PREG = ParseOp1Reg(IR);
- VAL = FetchData(P[PREG]);
- ACC -= VAL;
- printf("%d has been subtracted from the accumulator \n", P[PREG]);
- return 1;
- }
- int OP23(char *IR) {
- int PREG, VAL;
- printf("Opcode = 23. Subtract Accumulator Direct Addressing\n") ;
- PrintIR(IR);
- PREG = ParseOp1(IR);
- VAL = FetchData(PREG);
- ACC -= VAL;
- printf("%d has been subtracted from the accumulator \n", VAL);
- return 1;
- }
- int OP24(char *IR, int *PSW) {
- int PREG;
- printf("Opcode = 24. Compare Equal Register Addressing\n") ;
- PrintIR(IR);
- PREG = ParseOp1Reg(IR);
- if(FetchData(P[PREG]) == ACC) {
- PSW[0] = 1;
- printf("PSW[0] has been set to true\n");
- }
- else {
- PSW[0] = 0;
- printf("PSW[0] has been set to false\n");
- }
- return 1;
- }
- int OP25(char *IR, int *PSW) {
- int PREG, VAL;
- printf("Opcode = 25. Compare Less Register Addressing\n") ;
- PrintIR(IR);
- PREG = ParseOp1Reg(IR);
- if(FetchData(P[PREG]) > ACC) {
- PSW[0] = 1;
- printf("PSW[0] has been set to true\n");
- }
- else {
- PSW[0] = 0;
- printf("PSW[0] has been set to false\n");
- }
- return 1;
- }
- int OP26(char *IR, int *PSW) {
- int PREG, VAL;
- printf("Opcode = 26. Compare Greater Register Addressing\n") ;
- PrintIR(IR);
- PREG = ParseOp1Reg(IR);
- if(FetchData(P[PREG]) < ACC) {
- PSW[0] = 1;
- printf("PSW[0] has been set to true\n");
- }
- else {
- PSW[0] = 0;
- printf("PSW[0] has been set to false\n");
- }
- return 1;
- }
- int OP27(char *IR, int *PSW) {
- printf("Opcode = 27. Compare Greater Immediate\n") ;
- PrintIR(IR);
- if(ParseOP1andOP2Imm(IR) < ACC) {
- PSW[0] = 1;
- printf("PSW[0] has been set to true\n");
- }
- else {
- PSW[0] = 0;
- printf("PSW[0] has been set to false\n");
- }
- return 1;
- }
- int OP28(char *IR, int *PSW) {
- printf("Opcode = 28. Compare Equal Immediate\n") ;
- PrintIR(IR);
- if(ParseOP1andOP2Imm(IR) == ACC) {
- PSW[0] = 1;
- printf("PSW[0] has been set to true\n");
- }
- else {
- PSW[0] = 0;
- printf("PSW[0] has been set to false\n");
- }
- return 1;
- }
- int OP29(char *IR, int *PSW) {
- printf("Opcode = 29. Compare Less Immediate\n") ;
- PrintIR(IR);
- if(ParseOP1andOP2Imm(IR) > ACC) {
- PSW[0] = 1;
- printf("PSW[0] has been set to true\n");
- }
- else {
- PSW[0] = 0;
- printf("PSW[0] has been set to false\n");
- }
- return 1;
- }
- int OP30(char *IR, int *PSW) {
- int PREG;
- printf("Opcode = 30. Compare Register Equal\n");
- PrintIR(IR);
- PREG = ParseOp1Reg(IR);
- if(FetchData(R[PREG] == ACC)) {
- PSW[0] = 1;
- printf("PSW[0] has been set to true\n");
- }
- else {
- PSW[0] = 0;
- printf("PSW[0] has been set to false\n");
- }
- return 1;
- }
- int OP31(char *IR, int *PSW) {
- int PREG;
- printf("Opcode = 31. Compare Register Less\n") ;
- PrintIR(IR);
- PREG = ParseOp1Reg(IR);
- if(FetchData(R[PREG] > ACC)) {
- PSW[0] = 1;
- printf("PSW[0] has been set to true\n");
- }
- else {
- PSW[0] = 0;
- printf("PSW[0] has been set to false\n");
- }
- return 1;
- }
- int OP32(char *IR, int *PSW) {
- int PREG;
- printf("Opcode = 32. Compare Register Greater\n") ;
- PrintIR(IR);
- PREG = ParseOp1Reg(IR);
- if(FetchData(R[PREG] < ACC)) {
- PSW[0] = 1;
- printf("PSW[0] has been set to true\n");
- }
- else {
- PSW[0] = 0;
- printf("PSW[0] has been set to false\n");
- }
- return 1;
- }
- int OP33(char *IR, int *PSW, int *PC) {
- int VAL;
- printf("Opcode = 33. Branch Conditional True\n") ;
- PrintIR(IR);
- VAL = ParseOp1(IR);
- if(PSW[0] == 1) {
- *PC = VAL;
- printf("Program counter set to %d \n", VAL);
- }
- else {
- PC += 1;
- printf("Branch condiction failed\n");
- }
- return 1;
- }
- int OP34(char *IR, int *PSW, int *PC) {
- int VAL;
- printf("Opcode = 34. Branch Conditional False\n") ;
- PrintIR(IR);
- VAL = ParseOp1(IR);
- if(PSW[0] == 0) {
- *PC = VAL;
- printf("Program counter set to %d \n", VAL);
- }
- else {
- PC += 1;
- printf("Branch condiction failed\n");
- }
- return 1;
- }
- int OP35(char *IR, int *PSW, int *PC) {
- int VAL;
- printf("Opcode = 35. Branch Unconditional\n") ;
- PrintIR(IR);
- VAL = ParseOp1(IR);
- *PC = VAL;
- printf("Program counter set to %d \n", VAL);
- return 1;
- }
- int OP99(char *IR) {
- printf("Opcode = 99. Halt\n") ;
- return 1;
- }
- /* Now its your turn! Provide the prototype and implementation for the remaining opcodes. */
- main(int argc, char *argv[])
- {
- //Step 1 Read file into VM memory. Assume the file name is program2.
- fp = open("program2", O_RDONLY) ; //always check the return value.
- printf("Open is %d\n", fp) ;
- if (fp < 0) //error in read
- {
- printf("Could not open file\n");
- exit(0) ;
- }
- //read in the first line of the program
- int ret = read (fp, input_line, 7 ) ; //returns number of characters read`
- while (1)
- {
- if (ret <= 0) //indicates end of file or error
- break ; //breaks out of infinite loop
- //copy from input line into program memory
- printf("Copying Program line %d into memory\n", program_line) ;
- for (i = 0; i < 6 ; i++)
- {
- memory[program_line][i] = input_line[i] ;
- printf("%c ", memory[program_line][i]) ;
- }
- printf("\n") ;
- //read in next line of code
- ret = read (fp, input_line, 7 ) ;
- //if the firat character is a 'Z' then you are reading data.
- //No more program code so break out of loop
- if(input_line[0] == 'Z')
- break ;
- program_line++ ; //now at a new line in the prog
- }
- printf("PROGRAM COPIED INTO VM MEMORY!!\n") ;
- int Done = 0 ;
- PC = 0;
- while (!Done)
- {for (i = 0; i < 6 ; i++)
- IR[i] = memory[PC][i] ;
- opcode = ((int) (IR[0])- 48) * 10 ;
- opcode += ((int) (IR[1])- 48) ;
- printf("\n In Program Execution Loop: New PC is %d OPCODE IS %d\n\n", PC, opcode) ;
- /* You need to put in the case statements for the remaining opcodes */
- switch(opcode) {
- case 0: OP0(IR) ; PC++; break ;
- case 1: OP1(IR) ; PC++; break ;
- case 2: OP2(IR) ; PC++; break ;
- case 3: OP3(IR) ; PC++ ; break ;
- case 4: OP4(IR) ; PC++ ; break;
- case 5: OP5(IR) ; PC++; break ;
- case 6: OP6(IR) ; PC++ ; break ;
- case 7: OP7(IR) ; PC++; break ;
- case 8: OP8(IR) ; PC++; break ;
- case 9: OP9(IR) ; PC++; break ;
- case 10: OP10(IR) ; PC++; break ;
- case 11: OP11(IR) ; PC++; break ;
- case 12: OP12(IR) ; PC++; break ;
- case 13: OP13(IR) ; PC++; break ;
- case 14: OP14(IR) ; PC++; break ;
- case 15: OP15(IR) ; PC++; break ;
- case 16: OP16(IR) ; PC++; break ;
- case 17: OP17(IR) ; PC++; break ;
- case 18: OP18(IR) ; PC++; break ;
- case 19: OP19(IR) ; PC++; break ;
- case 20: OP20(IR) ; PC++; break ;
- case 21: OP21(IR) ; PC++; break ;
- case 22: OP22(IR) ; PC++; break ;
- case 23: OP23(IR) ; PC++; break ;
- case 24: OP24(IR, PSW) ; PC++; break ;
- case 25: OP25(IR, PSW) ; PC++; break ;
- case 26: OP26(IR, PSW) ; PC++ ; break;
- case 27: OP27(IR, PSW) ; PC++; break ;
- case 28: OP28(IR, PSW) ; PC++; break ;
- case 29: OP29(IR, PSW); PC++ ; break ;
- case 30: OP30(IR, PSW) ; PC++; break ;
- case 31: OP31(IR, PSW) ; PC++; break ;
- case 32: OP32(IR, PSW) ; PC++; break ;
- case 33: OP33(IR, PSW, &PC) ; break ;
- case 34: OP34(IR, PSW, &PC) ; break ;
- case 35: OP35(IR, PSW, &PC) ; break ;
- case 99: printf("ALL DONE\n") ; Done = 1 ;
- default: printf("Instruction %d not found!~\n", opcode) ;
- exit(0) ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement