summaryrefslogtreecommitdiff
path: root/src/sim/id.cc
blob: c6e42d52de70e508f593c7e314bb3fb6eaf3e87b (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "id.h"
#include "accessor.h"
#include "instr.h"
#include "instrDTO.h"
#include "logger.h"
#include "response.h"
#include "stage.h"
#include "utils.h"

static Logger *global_log = Logger::getInstance();

ID::ID(Stage *stage) : Stage(stage) { this->id = DCDE; }

Response ID::advance(InstrDTO &i) { Response r; }

// TODO this function is ugly
void ID::get_instr_fields(
	signed int &s1, signed int &s2, signed int &s3, Mnemonic &m)
{
	unsigned int type, s0b, s1b, s2b;
	this->split_instr(s1, type, m);

	// define the parsing bounds
	s0b = REG_SIZE;
	switch (type) {
	case 0b00:
		// R-TYPE
		s1b = s0b + REG_SIZE;
		s2b = s1b + REG_SIZE;
		break;
	case 0b01:
		// I-TYPE
		s1b = s0b + REG_SIZE;
		s2b = WORD_SPEC;
		break;
	case 0b10:
		// J-TYPE
		s1b = WORD_SPEC;
	}

	if (type != 0b10)
		s3 = GET_MID_BITS(s1, s1b, s2b);

	s2 = GET_MID_BITS(s1, s0b, s1b);
	s1 = GET_LS_BITS(s1, s0b);
}

void ID::split_instr(signed int &raw, unsigned int &type, Mnemonic &m)
{
	unsigned int opcode, opcode_size;

	type = GET_LS_BITS(raw, TYPE_SIZE);
	opcode_size = (type == 0b0) ? R_OPCODE_SIZE : OPCODE_SIZE;
	opcode = GET_MID_BITS(raw, TYPE_SIZE, TYPE_SIZE + opcode_size);
	try {
		m = instr::mnemonic_map.at((opcode << 2) + type);
	} catch (std::out_of_range const &) {
		m = NOP;
	}

	raw = (unsigned int)raw >> (TYPE_SIZE + opcode_size);
}

Response ID::dereference_register(signed int &v)
{
	Response r;
	r = OK;

	if (v < 0 || v >= GPR_NUM + V_NUM) {
		global_log->log(
			ERROR, string_format(
					   "instruction tried to access register %d, which does "
					   "not exist",
					   v));
		exit(EXIT_FAILURE);
	} else if (v >= GPR_NUM)
		v = this->vrs[v % GPR_NUM];
	else
		v = this->gprs[v];

	return r;
}