Question
Programming Project 3 (Tiny Architecture ISA) Code is at the bottom, need help with the output. The Problem Using C programming language write a program
Programming Project 3 (Tiny Architecture ISA)
Code is at the bottom, need help with the output.
The Problem
Using C programming language write a program that simulates a variant of the Tiny Machine
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, Memory Address Registers, Instruction Memory, Data Memory, Memory Data Registers, and
Accumulator). Data Memory will be represented by an integer array. Your Program Counter will begin
pointing to the first instruction of the program.
For the sake of simplicity Instruction 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 Instruction
Memory greatly simplifies this program.
Example:
typedef struct {
int opCode, device Or Address;
}
Instruction
;
Instruction IM[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 store in Instruction Memory and then run
via the fetch/execute cycle. In the input file each instruction is represented with two integers: the first one
represents the opcode and the second one a memory address or a device number depending on the
instruction.
Example:
Input File
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 | DM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/* input value */
X
PC = 11 | A =
X
| DM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/* outputting accumulator to screen */
X
PC = 12 | A =
X
| DM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/* storing accumulator to memory location
0
*/
PC = 13 | A =
X
| DM = [
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 30.
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
Case IR.OP = 1 Load is executed.
LOAD (Execute cycle)
MAR
?
IR.ADDR
MDR2
?
DM[MAR] //DM stands for Data Memory
A
?
MDR2
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.
Note: you can use one or two MARs, MAR for IM and MAR2 for DM.
Tiny Machine ISA:
FETCH
MAR
?
PC
PC
?
PC + 1
MDR
?
IM [MAR]
// IM stands for Instruction Memory (program memory)
IR
?
MDR
Depending on IR.OP one of the following instructions will be executed:
(Execute cycle)
LOAD
MAR
?
IR.ADDR
MDR2
?
DM[MAR]
A
?
MDR2
ADD
MAR
?
IR.ADDR
MDR2
?
DM[MAR]
A
?
A + MDR2
STORE
MAR
?
IR.ADDR
MDR2
?
A
DM[MAR]
?
MDR2
SUB
MAR
?
IR.ADDR
MDR2
?
DM[MAR]
A
?
A - MDR2
IN
A
?
Input value from keyboard
OUT
Screen
?
A
END
Run
?
0 // In your program run must be initialized to 1 to control the instruction cycle.
JMP
PC
?
IR.ADDR
SKIP
IF (A == 0) PC
?
PC + 1
=====================================================================
#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);
void load(void);
void store(void);
void add(void);
void sub(void);
void output(void);
void input(void);
void skipz(void);
void jump(void);
int main(int argc, char *argv[])
{
int c;
//read input file file.txt
FILE *file;
if(argc!=2)
{
printf("Please input the file! ");
exit(1);
}
file = fopen(argv[1], "r");
if (file)
{
printf("Assembling the Program ");
int programIndex = 0;
while ((c = getc(file)) != EOF)
{
c-='0';
if(c<0||c>9)
{
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;i < programCounter+3;i++){
printf(" %d %d",programMemory[i].opCode,programMemory[i].deviceOrAddress);
}*/
while(decoder!=7)
{
switch(decoder)
{
case 0:
fetchExecute();
break;
case 1:
load();
break;
case 2:
add();
break;
case 3:
store();
break;
case 4:
sub();
break;
case 5:
input();
break;
case 6:
output();
break;
case 8:
jump();
break;
case 9:
skipz();
break;
}
}
printf("Program Complete ");
fclose(file);
}else
{
printf("File not found ");
}
system("PAUSE");
return 0;
}
//Fetch an instruction from the memory address pointed to by the PC (Program counter),
//place it in the instruction register (IR) and increment the PC by one
//The OP (Operation code) in the IR tells the computer what is the instruction that must be executed.
void fetchExecute()
{
memoryAddressRegister=programCounter;
++programCounter;
memoryDataRegister = programMemory[memoryAddressRegister].deviceOrAddress;
instructionRegister.deviceOrAddress=memoryDataRegister;
memoryDataRegister = programMemory[memoryAddressRegister].opCode;
instructionRegister.opCode=memoryDataRegister;
decoder = instructionRegister.opCode;
}
//Loads the contents of instructionRegister address into A (A stand for Accumulator).
void load()
{
memoryAddressRegister = instructionRegister.deviceOrAddress;
memoryDataRegister = memory[memoryAddressRegister];
accumulateor = memoryDataRegister;
decoder = 0;
}
//Store the contents of A into instructionResister address
void store()
{
memoryAddressRegister = instructionRegister.deviceOrAddress;
memoryDataRegister = accumulateor;
memory[memoryAddressRegister] = memoryDataRegister;
decoder = 0;
}
//The data value stored at instructionRegister address is added to the A and the result is stored back in the A.
void add()
{
memoryAddressRegister = instructionRegister.deviceOrAddress;
memoryDataRegister = memory[memoryAddressRegister];
accumulateor = accumulateor+memoryDataRegister;
decoder = 0;
}
//Subtracts the value located at instructionRegister address from the A and stored the result back in the A.
void sub()
{
memoryAddressRegister = instructionRegister.deviceOrAddress;
memoryDataRegister = memory[memoryAddressRegister];
accumulateor = accumulateor-memoryDataRegister;
decoder = 0;
}
//Print out the contents of the AC in the output device.
void output()
{
int i = 0;
printf("PC = %d \t A = %d \t MEM = [",programCounter,accumulateor);
for(i=0;i { printf("%d, ",memory[i]); } printf("%d] ",memory[i]); decoder = 0; } //A value from the input device is transferred into the A. void input() { printf("Enter a number: "); scanf("%d",&accumulateor); decoder = 0; } //If the contents of the Accumulator = 0 the next instruction is skipped. void skipz() { if (accumulateor==0) { ++programCounter; } decoder = 0; } //Causes an unconditional branch to instructionRegister address. void jump() { programCounter = instructionRegister.deviceOrAddress; decoder = 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started