aboutsummaryrefslogtreecommitdiff
path: root/qtmips_machine/instruction.cpp
blob: 159d4437d53df49ca4b0cce56153c85332c520e7 (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
#include "instruction.h"
#include <sstream>
#include <iostream>

InstructionR::InstructionR(std::uint8_t rs, std::uint8_t rd, std::uint8_t rt, std::uint8_t sa) {
    this->rs = rs;
    this->rd = rd;
    this->rt = rt;
    this->sa = sa;
}

// TODO for registers output as register ($0)!

std::vector<std::string> InstructionR::to_strs() {
    std::vector<std::string> str;
    // Instruction name
    str.push_back("unknown"); // unknown instruction, should be replaced by child

    std::stringstream ss;
    // Source register
    ss << std::hex << (unsigned) this->rs;
    str.push_back(ss.str());
    ss.str("");
    // Target register
    ss << std::hex << (unsigned) this->rt;
    str.push_back(ss.str());
    ss.str("");
    // Destination register
    ss << std::hex << (unsigned) this->rd;
    str.push_back(ss.str());
    ss.str("");
    // Shift amount
    ss << std::hex << (unsigned) this->sa;
    str.push_back(ss.str());

    return str;
}

InstructionI::InstructionI(std::uint8_t rs, std::uint8_t rt, std::uint16_t immediate) {
    this->rs = rs;
    this->rt = rt;
    this->immediage = immediate;
}

std::vector<std::string> InstructionI::to_strs() {
    std::vector<std::string> str;
    // Instruction name
    str.push_back("unknown"); // unknown instruction, should be replaced by child

    std::stringstream ss;
    // Source register
    ss << std::hex << (unsigned) this->rs;
    str.push_back(ss.str());
    ss.str("");
    // Destination register
    ss << std::hex << (unsigned) this->rt;
    str.push_back(ss.str());
    ss.str("");
    // Immediate value
    ss << std::hex << (unsigned) this->immediage;
    str.push_back(ss.str());

    return str;
}

InstructionJ::InstructionJ(std::uint32_t address) {
    this->address = address;
}

std::vector<std::string> InstructionJ::to_strs() {
    std::vector<std::string> str;
    // Instruction name
    str.push_back("unknown"); // unknown instruction, should be replaced by child

    std::stringstream ss;
    // Source register
    ss << std::hex << (unsigned) this->address;
    str.push_back(ss.str());

    return str;
}