0

I'm working on creating a simulator for RISC.

Below are a different part of codes relevant to my question.

main.cpp

memory* main_memory;
processor* cpu;
main_memory = new memory (verbose);    //memory classs implemented correctly
cpu = new processor (main_memory, verbose);

interpret_commands(main_memory, cpu);// takes to commands class

In processor.cpp, I have to access a function of memory class that is public.

memory.cpp:


uint32_t memory::read_word (uint32_t address) {
            // some calculation and returns a hex word at address
}

processor.cpp:

#include "memory.h"
void processor::execute(unsigned int num, bool breakpoint_check){

    if(some condition){

           //call read_word at address
    }
}

I do have a parameterized constructor in processor.cpp that has memory class pointer:

processor::processor(memory* main_memory, bool verbose){

}

The question is, how can I call read_word function(of class memory) from the execute function (of class processor)?
Any help would be highly appreciated :)

  • 1
    You forgot to ask a question. – John Zwinck Jun 16 '19 at 02:05
  • Here's the thing about bugs. If you don't know what they are, how do you know what is the relevant code? Heck, what is the relevant problem? – user4581301 Jun 16 '19 at 02:05
  • Side note: you probably don't need any dynamic allocations, and if you do, [avoid `new`](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). [Use a smart pointer.](https://en.cppreference.com/w/cpp/memory/unique_ptr) – user4581301 Jun 16 '19 at 02:09
  • lol @JohnZwinck. Sorry have been working on simulator whole night :) Updated it now. – Rick Williams Jun 16 '19 at 02:10
  • @user4581301, I updated the question. I need to access read_word() from execute (). – Rick Williams Jun 16 '19 at 02:11
  • If the `processor` constructor stored the `main_memory` pointer it was given in `somePointer` and you have a computed `address`, `somePointer->read_word(address);` ought to do it. – user4581301 Jun 16 '19 at 02:13
  • Have you peeked at [RISC-V simulator for x86-64](https://github.com/rv8-io/rv8)? – Ted Lyngmo Jun 16 '19 at 02:13
  • @user4581301, so, should I store the address of main_memory in the processor constructor and then use this address in the execute() to call read_word from there? – Rick Williams Jun 16 '19 at 02:23
  • I mean this as politely as possible: That question says you're not ready to write a CPU simulator in C++. Get yourself [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn the language with simpler problems before attempting this. Trying to learn to program with a task like this is going to leave you frustrated and burned out. – user4581301 Jun 16 '19 at 02:33

1 Answers1

1

You just need to store the main_memory* in the constructor as a member variable, so you can use it later in another member function:

processor::processor(memory* main_memory, bool verbose)
    : _main_memory(main_memory)
{}

void processor::execute(unsigned int num, bool breakpoint_check){
    if(some condition){
        _main_memory->read_word(...);
    }
}

And add memory* _main_memory = nullptr inside class processor.

John Zwinck
  • 207,363
  • 31
  • 261
  • 371