summaryrefslogtreecommitdiff
path: root/src/sim/if.cc
blob: fb49749ca58473bc254cdfcb600b68c214079bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "if.h"
#include "accessor.h"
#include "instrDTO.h"
#include "response.h"
#include "stage.h"

IF::IF(Stage *stage) : Stage(stage) { this->id = FETCH; }

InstrDTO *IF::advance(Response p)
{
	InstrDTO *r = nullptr;

	this->advance_helper();
	if (this->curr_instr != nullptr && p == WAIT) {
		// mutual consent
		++this->pc;
		this->curr_instr->set_time_of(this->id, this->clock_cycle);
		r = new InstrDTO(*this->curr_instr);
		delete curr_instr;
		curr_instr = nullptr;
	}

	return r;
}

std::vector<int> IF::stage_info() { 
	std::vector<int> info;
	if(this->curr_instr){
		info.push_back(this->curr_instr->get_pc());
		info.push_back(this->curr_instr->get_instr_bits());
	} 
	return info;
}

void IF::advance_helper()
{
	Response r;
	int i;
	signed int bits;

	if (this->curr_instr == nullptr) {
		i = this->storage->read_word(this, this->pc, bits);
		r = i ? OK : STALLED;
		if (r == OK) {
			this->curr_instr = new InstrDTO();
			this->curr_instr->set_instr_bits(bits);
			this->curr_instr->set_pc(this->pc);
		}
	}
}