C++ Machine Language and RTN Instructions

Hello,

I need some assistance. I'm trying to simulate how machine language works using C++ coding rather than assembly. My goal is to load a program into simulated RAM and simulate RTN instructions. The instructionsn should apply one at a time. I can get my code to compile and input the data file, however, when I actually attempt to run the RTN instructions, my code crashes. I've tried to comment out the switch and only run the "fetch" but the program still crashes. This is a grey area as the book I have only explains the machine language function and not how to simulate the language in C++ which is why I'm reaching out here. Any assistance would be appreciative. Here's my work thus far:

Data Input:
000 1AAA
100 2AAA
AEF 30FC

Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cmath>

using namespace std;

int RAM [4095];
int R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, RA, RB, RC, RD, RE, RF; //Registers
int IR, PC, MBR;

typedef long WordSize;
typedef int AddressRange;
typedef int CodeSize;
WordSize Memory [4095], A, B, MDR; // Machine architecture

void into_Memory( string );
void print_Memory();
void run_Program();

void into_Memory( string filename ) {
int memory = 0;
fstream program;
program.open( filename.c_str() );
if( !program.is_open() ) {
cout << filename <<" does not exist!\n";
system ("pause");
exit(1);
}

while( program >> hex >> memory ) {
program >> hex >> RAM[memory];
}
cout << "Program loaded successfully!\n\n";
}

void print_Memory() {
for( int i = 0; i < 4096; i++ )
if( !RAM[i] == 0 )
cout << hex << i << " " << RAM[i] << endl;

cout << "Program Count: " << PC << endl;
cout << "Instruction Register: " << IR << endl;
}

void run_Program() { //fetch

int operand;
int isRunning = 1;
AddressRange PC, MAR;
WordSize Temp;
CodeSize OP;
AddressRange Addr;

while(isRunning) {
IR = RAM[PC];
PC = PC + 1;
OP = IR/0xFFF;
operand = IR%0xFFF;
switch (OP) { // Execute Opcode
case 01 : MAR = Addr; // LDA Addr
MDR = Memory[ MAR ];
A = MDR;
break;
case 02 : MAR = Addr; // STA Addr
MDR = A;
Memory[ MAR ] = MDR;
break;
case 03 : Temp = A; // XAB
A = B;
B = Temp;
break;
case 04 : A = 0; // CLA
break;
case 20 : MAR = Addr; // ADD Addr
MDR = Memory[ MAR ];
A = A + MDR;
break;
case 21 : MAR = Addr; // SUB Addr
MDR = Memory[ MAR ];
A = A - MDR;
break;
case 30 : PC = Addr; // JMP
// break;
case 99 : exit(0); // HLT
}
}
}

int main( int argc, char *argv[] ) {

string filename;
fstream program;
int isRunning = 1;
char choice;
PC = 0;
MBR = 0;
IR = 0;
cout << "Please enter the name of the data file: ";
cin >> filename;

into_Memory( filename );
while( isRunning ) {
cout << "\nPlease make a selection:\nR = Run the Program \""
<< filename << "\"\nP = Print Memory\nQ = Quit\n::";
cin >> choice;
if( choice == 'R' || choice == 'r' )
run_Program();
else if( choice == 'P' || choice =='p' )
print_Memory();
else if( choice == 'Q' || choice == 'q' )
isRunning = 0;

}
cout << "\n\n\nPress enter to exit...";
cin.ignore();
cin.get();

return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
void run_Program() 
{
    int operand;
    int isRunning = 1;
    AddressRange PC, MAR;
    WordSize Temp;
    CodeSize OP;
    AddressRange Addr;

    while (isRunning) 
    {
        IR = RAM[PC];
   ... etc.


At line 5, PC is defined but is not initialised. It could contain any random garbage value. Thus at line 12, RAM[PC] is accessing some random location in memory. From here on, results are unpredictable.

There is also a global variable named PC. You need to decide which one to use, don't have both local and global PC.

Also, you allocate a couple of arrays of 4095 elements, but the loop for ( int i = 0; i < 4096; i++ ) is accessing a total of 4096 elements, not 4095.
Hi Chervil,

Thank you for the heads up on the Global decloration for PC. I ended up removing the Address Range decloration and kept the global decloration. The reason that I'm using "RAM [PC]" is I'm trying to mimic an RTN instruction. This may not work since C++ might not read it the same. I guess what I'm trying to do is translate:
MAR <-- PC
MAR <-- IR[11..0]
PC <-- PC + 2
RA <-- Mem[MAR]
if IR[15..12] == 1 do ...
etc.
Topic archived. No new replies allowed.