*DUE 4/17/17* The Problem Using C programming language write a program that simu
ID: 3817332 • Letter: #
Question
*DUE 4/17/17* The Problem Using C programming language write a program that simulates the Tiny Machine Architecture. Your code must implement the basic instruction set that the Tiny Machine Architecture adheres to: 1 LOAD 2 ADD 3 STORE 4 SUB 5 IN 6 OUT 7 END 8 JMP 9 SKIPZ Each piece of the architecture must be accurately represented in your code (Instruction Register, Program Counter, Memory Address Register, Data Memory, Memory Data Register, and Accumulator). Data Memory will be represented by a 0-9 array. Your Program Counter will begin at 10. For the sake of simplicity Program Memory (IM) and Data Memory (DM) may be implemented as separate arrays. Hint: Implementing a struct for your Instructions and an array of these structs as your Program Memory greatly simplifies this program. Example: typedef struct { int opCode, deviceOrAddress; } Instruction; Instruction programMemory[MAXPROGRAMSIZE]; Note: IM, MDR1, and IR are of type Instruction. All other CPU registers and Data Memory (DM) are of type int. Input Specifications Your simulator must run from the command line with a single input file as a parameter to main. This file will contain a sequence of instructions for your simulator to assemble (store in “program memory”) and then run via the fetch/execute cycle. Example: 5 5 //IN 5 6 7 //OUT 7 3 0 //STORE 0 5 5 //IN 5 6 7 //OUT 7 3 1 //STORE 1 1 0 //LOAD 0 4 1 //SUB 1 3 0 //STORE 0 6 7 //OUT 7 1 1 //LOAD 1 6 7 //OUT 7 7 0 //END Output Specifications Your simulator should provide output according to the input file. Along with this output your program should provide status messages identifying details on the workings of your simulator. Output text does not have to reflect my example word-for-word, but please provide detail on the program as it runs in a readable format that does not conflict with the actual output of your simulator. After each instruction print the current state of the Program Counter, Accumulator, and Data Memory. The INPUT instruction is the only one that should prompt an interaction from the user. Example: Assembling Program… Program Assembled. Run. PC = 10 | A = NULL | MEM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] /* input value */ X PC = 11 | A = X | MEM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] /* outputting accumulator to screen */ X PC = 12 | A = X | MEM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] /* storing accumulator to memory location 0 */ PC = 13 | A = X | MEM = [X, 0, 0, 0, 0, 0, 0, 0, 0, 0] … etc Program complete. Grading Your simulator will be graded on the above criteria. Your program should compile and run from the command line with one input file parameter. Please note that your program will not just be graded on whether or not it runs successfully; accurate simulation and a thorough demonstration of your understanding on the workings of this architecture will constitute a large portion of this grade. As that is the case it is in your best interest to comment your program in a concise and readable way. However, if your program does not run or compile the maximum points possible will be 50 (up to 25 may be recovered by debugging and demonstrating an understanding of your errors during TA’s office hours). For instance, to implement FETCH and instruction LOAD you must implement each step: FETCH MAR PC PC PC + 1 MDR IM [MAR] // IM stands for Instruction Memory (program memory) IR MDR LOAD (Execute cycle) MAR IR.ADDR MDR DM[MAR] DM stands for Data Memory A MDR */ Note: Lecture 1 describes the instruction set architecture of the Tiny Machine. Submission Your program must be submitted as a C file. For example: NameMyProgram.c Please check and double check your submission.
Explanation / Answer
NameMyProgram.c #include #include #define MAXPROGRAMSIZE 100 #define MAXMEMORYSIZE 10 struct instruction{ int opCode, deviceOrAddress; }; struct instruction programMemory[MAXPROGRAMSIZE]; struct instruction instructionRegister; int accumulateor; int memory[MAXMEMORYSIZE]; int programCounter = 10; int memoryAddressRegister; int memoryDataRegister; int decoder = 0; void fetchExecute(); void load(); void store(); void add(); void sub(); void output(); void input(); void skipz(); void jump(); int main(int argc, char *argv[]){ int c; //read input file file.txt FILE *file; if(argc!=2){ printf("please input file first "); exit(1); } file = fopen(argv[1], "r"); if (file) { printf("assembling program "); int programIndex = 0; while ((c = getc(file)) != EOF){ c-='0'; if(c9){ continue; } if(programIndex%2==0){ programMemory[programCounter+programIndex/2].opCode=c; }else{ programMemory[programCounter+programIndex/2].deviceOrAddress=c; } programIndex++; } printf("program assembled Run. "); /*for(i=programCounter;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.