Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Build a Y86 Architectue Simulator in C For this assignment, you are going to use C to implement a simulator for the Sequential Y86 architecture.

Build a Y86 Architectue Simulator in C

For this assignment, you are going to use C to implement a simulator for the Sequential Y86 architecture. Your input will be a text file that contains first the program counter followed by the memory. A front end is already written that will read this file into an array where each byte of memory is a char. Starting at the memory address given to you as the star ting program counter, you will read in bytes and execute the program. This is very similar to what we did in class where we disassembled the input and looked at what happens in the different stages for each type of instruction.

For example, consider t he partial input given in the box below. The first number (0x0) is the starting program counter PC . Other values can be used if the program execution can start elsewhere. The rest of the input is the contents of memory starting at address 0x0. This is what you will be executing. You start by examining the first byte and then all needed additional bytes for that instruction. After execution (which may modify the PC), you execute the instruction at PC, Start by understanding what the below program should do if simulated.

image text in transcribed

You will need to implement all of the given Y86 plus some additional instruction s to allow you to dump the c ontents of registers and memory:

? printall Single byte instruction 0xC0 prints out the values stored in all 15 registers

? printreg reg Two byte instruction 0xD0, 0xrA:F print out the value of the register indicated by rA

? printmem disp(reg) 10 byte instruction 0xE0, 0xrA:F, 8 byte displacement print out the 8 byte constant stored at address contents(rA) + displacement.

There are examples of all three of these new instructions in the above code. Implement these first they will help you debug to real Y86 instructions.

Starting the Assignment

The starting handout, available on blackboard, contains several files:

? simulate.c this is the file you will be editing

? simulate_main.c reads the input into the data structure you will be using.

? Makefile - creates executable simulate

? Two examples to initially try: test1.m and test2.m Try running simulate test1.m. Iniitally doesnt do much but eventually will execute the Y86 program test1.Y. There is also a test2.Y. Use the give tools to create other test files.

Implementation Notes

? You will need to use 64 - bit (long int) for your registers and other 64 - bit values. To print a 64 - bit value in hex, use %lx (rather than %x).

? You must use char type (single byte) and bit - level operators to do your work. Use masks and shifting to get to the parts of the byte you need to use at a given time. Be careful about shif ting bytes since arithmetic right shifts are used in C. Casting can also cause problems since sign extension is going to be used.

INCLUDED FILES:

(simulate_main.c) - don't edit this:

#include #include

#define MAX_MEMORY 9000

char convert(char v1, char v2) ; void get_input(FILE*); extern void execute(int);

char memory[MAX_MEMORY] ; int PC = 0;

int main(int argc, char **argv) { FILE *fp; if (argc > 1) { fp = fopen(argv[1],"r"); if (!fp) {printf("Can't open file %s ",argv[1]); exit(1); } } else { printf("Usage: %s ",argv[0]); exit(1); } get_input(fp); execute(PC); }

void get_input(FILE *fp) { char c1,c2; char pc[32]; int pos = 0; fscanf(fp,"%s",pc); PC = (long int)strtol(pc,NULL,16);

fscanf(fp,"%c",&c1); while (fscanf(fp,"%c%c",&c1,&c2) > 1) { if (pos == MAX_MEMORY) { printf("Input too large Execution will continue but results may be unreliable "); return; } memory[pos++] = convert(c1,c2); }

for (;pos }

char convert(char v1, char v2) { int rval = 0; if (v1 >= '0' && v1 = 'a' && v1 = 'A' && v1 = '0' && v2 = 'a' && v2 = 'A' && v2

(similate.c) - edit this:

#include #include

int Pop(int), Push(int), Call(int), Ret(int), Jump(int); int IRmov(int), RRmov(int), RMmov(int), MRmov(int); int OPx(int); int printall(int), printreg(int),printmem(int);

char SF = 0x00; char ZF = 0x00;

extern char memory[] ; extern int PC ;

long int regs[16];

char *regname[15] = {"%rax","%rcx","%rdx","%rbx","%rsp","%rbp", "%rsi","%rdi","%r8","%r9","%r10","%r11","%r12","%r13","%r14"};

void execute(int PC) { int done = 0; char byte; char opcode ;

while (!done) { byte = memory[PC]; opcode = (byte >> 4)&0xf; switch (opcode) { case 0: printf("Halting at instruction 0x%x ",PC); done = 1; break; //halt case 1: PC++; break; // nop case 2: PC = RRmov(PC); break; case 3: PC = IRmov(PC); break; case 4: PC = RMmov(PC); break; case 5: PC = MRmov(PC); break; case 6: PC = OPx(PC); break; case 7: PC = Jump(PC); break; case 8: PC = Call(PC); break; case 9: PC = Ret(PC); break; case 10: PC = Push(PC); break; case 11: PC = Pop(PC); break; case 12: PC = printall(PC); break; case 13: PC = printreg(PC); break; case 14: PC = printmem(PC); break; default: printf("# unknown op at PC=0x%x ",PC); PC++; } } }

int printreg(int PC) { return PC+1; }

int printall (int PC) { return PC+1; }

int printmem(int PC) { return PC + 1; }

int IRmov(int PC) { return PC+1; }

int RRmov(int PC) { return PC+1; }

int RMmov(int PC) { return PC+1; }

int MRmov(int PC) { return PC+1; }

int Jump(int PC ) { return PC+1; }

int Call(int PC) { return PC + 1; }

int Ret(int PC) { return PC+1; }

int Push(int PC) { return PC+1; }

int Pop(int PC) { return PC+1; }

int OPx(int PC) { return PC+1; }

0x0 30F4001000000000000030F6FECA0000 00000000802B00000000000000c06F80 3600000000000000c00F0030F6EFBE00 000000000090A06FB00F900000000000 0x0 30F4001000000000000030F6FECA0000 00000000802B00000000000000c06F80 3600000000000000c00F0030F6EFBE00 000000000090A06FB00F900000000000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions