Ok so am writing a program that simulates a computer system.The cpu and memory will be forked making memory a child process and cpu athe parent and i will be using pipes to let them exchange informatino. I am really lost tough on what to do next, i have loaded my program.txt to memory and set up a basic cpu skeleton but not sure how to implement this,
two operations: which are memory operations
read(address) returns the value at the address
write(address, data) writes the data to the address

heres what i have so far please disregard my forking and pipes at the moment, not sure where to go from here. If need more info i will give it , just not sure how go on about it. This program will runa program named program.txt that contains instructions and data that my simulator will use.

/* 
 * File:   project1.cpp
 * Author: alejandro
 *
 * Created on February 25, 2013, 2:00 PM
 */

#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <sys/wait.h>

using namespace std;
int memory(int adress,int data);
int cpu(int number, int data);
/*
 * 
 */
int main(int argc, char** argv) 
{



    int pipefd[2];
    pid_t cpid;
    int buff;

    if(pipe(pipefd)==-1)
    {
        perror("pipe");
        exit(EXIT_FAILURE);
    }
    cpid = fork();
    if(cpid == -1)
    {
        perror("fork");
        exit(EXIT_FAILURE);

    }
    if(cpid==0)
    {
        int x;
        int y;
        read(pipefd[0],&x,5);
        memory(x,y); 
        write(pipefd[1],&x,5);
        waitpid(NULL);
    }
    else
    {
        int data;
        int numbers;
        read(pipefd[0],&data,5); 
        cpu(numbers,data);
        write(pipefd[1],&data,5);
        cpid= waitpid(-1,NULL,0);
    }





    return 0;
}
int cpu(int numbers, int data)
{
    int number=numbers;
    int PC=0;
    int SP=0;
    int IR=0;
    int AC=0;
    int X=0;
    int Y=0;
    switch(IR)
    {
        case 1:
            AC=data;break;
        case 2:
            //AC = loadAdress(PC);break;
        case 3:
            //storeAC(PC);break;
        case 4:
            X+AC;break;
        case 5:
            Y+AC;break;
        case 6:
            AC-X;break;
        case 7:
            AC-Y;break;
        case 8:
            //getPort();break;
        case 9:
            //putPort();break;
        case 10:
            X=AC;break;
        case 11:
            Y=AC;break;
        case 12:
            AC=X;break;
        case 13:
            AC=Y;break;
        case 14:
            //jumpAddress(PC);break;
        case 15:
            //if(AC==0)jumpAdress(PC);break;
        case 16:
            //if(AC1=0)jumpAdress(PC);break;
        case 17:
            //??;break;
        case 18:
            //??;break;
        case 19:
            ++X;break;
        case 20:
            --X;break;
        case 30:
            cout<<"Finished execution"<<endl;exit(EXIT_SUCCESS);break;
    }
      return number;  
}

int memory(int adress,int data)
{

    int memArray[1000];
    int memAdress = adress;
    int numberInstructions = 0;//this indicates the number of instructions in memory
    ifstream infile;
    infile.open("program.txt");
    if(infile.eof())
    {
        cout << "error" << endl;
        return 1;
    }
    while(!infile.eof())
    {
        infile >> memArray[numberInstructions];
        ++numberInstructions;
    }
    cout << "number of lines: " << numberInstructions;
    cout <<endl<<"The array: "<<endl;
    for(int i = 0; i < numberInstructions;i++)
    {
        cout << memArray[i] <<endl;
    }
    infile.close();

    return memArray[adress];
}
     // read(address);// returns the value at the address
     // write(address, data);// writes the data to the address

You have a couple of problems. Mostly the variables that you pass to the cpu() and memory functions are being used as though they are being updated inside the functions, yet you don't update them there. Also, if that was your intention, then you need to alter the signature of these functions to this:

int memory(int adress&,int& data);
int cpu(int& number, int& data);

so you are passing the values by reference, then when you do this:

memory(x,y); 
write(pipefd[1],&x,5);

The value of x will be properly set and passed to the write() function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.