Question: Create a program that simulates a variant of the Tiny Harvard Architecture. In this implementation, memory ( RAM ) is split into Instruction Memory (

Create a program that simulates a variant of the Tiny Harvard Architecture. In this implementation, memory (RAM) is split into Instruction Memory (IM) and Data Memory (DM). Your code must implement the basic instruction set architecture (ISA) of the Tiny Machine Architecture:
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, Instruction Memory (IM), MAR -1, MDR -1(MAR -1 and MDR -1 are connected to the IM). Data Memory, MAR -2, MDR -2(MAR -2 and MDR -2 are connected to the DM), and Accumulator. Instruction Memory will be represented by an integer array, and each instruction will use 2 elements of the array (one for OP and the other one for the address). Data Memory will be represented by an integer array, and each data value uses an element of the DM array. Your Program Counter will begin pointing to the first instruction of the program (PC =0) The virtual machine (VM) you are implementing 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 VM. 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:
Reading Program...
Program Loaded.
Run.
PC =10| A = NULL | DM =[0,0,0,0,0,0,0,0,0,0]/* input value /
X
PC =12| A = X | DM =[0,0,0,0,0,0,0,0,0,0]/ outputting accumulator to screen /
X
PC =14| A = X | DM =[0,0,0,0,0,0,0,0,0,0]/ storing accumulator to memory location 0*/
PC =16| A = X | DM =[X,0,0,0,0,0,0,0,0,0]...
etc
Program complete.
The code (text) must be loaded starting at location "0", and therefore the PC must be initialized to zero (PC =0). Any program that is loaded somewhere else (for example loaded at memory location 10), will get a zero. The PC should start from 0, and Data is in Data Memory which is another array. You must run a program to multiply 2 numbers. We will test your HW 4 running a program to multiply 2 numbers; numbers will not be validated. you can use two integers as inputs 3 and 4.
This is my code. Check to ensure that it meets the requiremtns and works.
#include
// Constants for instruction set architecture
#define LOAD 1
#define ADD 2
#define STORE 3
#define SUB 4
#define IN 5
#define OUT 6
#define END 7
#define JMP 8
#define SKIPZ 9
// Memory sizes
#define IM_SIZE 250
#define DM_SIZE 10
// Function prototypes
void load_program(const char *filename);
void fetch_execute_cycle();
void print_state();
// Registers
int PC =0; // Program counter
int MAR1, MDR1; // Instruction Memory Registers
int MAR2, MDR2; // Data Memory Registers
int IR; // Instruction Register
int A =0; // Accumulator
// Memory arrays
int IM[IM_SIZE][2]={0}; // Instruction Memory
int DM[DM_SIZE]={0}; // Data Memory
int main(int argc, char *argv[]){
if (argc !=2){
printf("Usage: %s
", argv[0]);
return 1;
}
// Load program from file
load_program(argv[1]);
// Run fetch/execute cycle
printf("Program Loaded. Run.
");
fetch_execute_cycle();
printf("Program complete.
");
return 0;
}
void load_program(const char *filename){
FILE *file = fopen(filename,"r");
if (!file){
printf("Error: Unable to open file %s
", filename);
return;
}
int opcode, address;
int i =0;
while (fscanf(file,"%d %d", &opcode, &address)!= EOF && i < IM_SIZE){
IM[i][0]= opcode;
IM[i][1]= address;
i++;
}
fclose(file);
}
void fetch_execute_cycle(){
int run =1;
while (run){
// Fetch
MAR1= PC;
PC +=2;
MDR1= IM[MAR1][0];
IR = MDR1;
// Execute
switch (IR){
case LOAD:
// LOAD
MAR2= IM[MAR1][1];
MDR2= DM[MAR2];
A = MDR2;
break;
case ADD:
// ADD
MAR2= IM[MAR1][1];
MDR2= DM[MAR2];
A += MDR2;
break;
case STORE:
// STORE
MAR2= IM[MAR1][1];
DM[MAR2]= A;
break;
case SUB:
// SUB
MAR2= IM[MAR1][1];
MDR2= DM[MAR2];
A -= MDR2;
break;
case IN:
// IN

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!