aboutsummaryrefslogtreecommitdiff
path: root/comp1521/smips/instr.c
blob: 645fbaece2963722fb599e02198c4ae3ada22915 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "instr.h"

// Gets the immediate bits, clearing others and shifting right.
uint32_t get_i(const uint32_t instr) { return instr & IMMEDIATE_BITS; }

// Gets the pattern bits, clearing others and shifting right.
uint32_t get_p(const uint32_t instr) { return (instr & PATTERN_BITS) >> 26; }

// Gets the d bits, clearing others and shifting right.
uint32_t get_d(const uint32_t instr) { return (instr & D_BITS) >> 11; }

// Gets the t bits, clearing others and shifting right.
uint32_t get_t(const uint32_t instr) { return (instr & T_BITS) >> 16; }

// Gets the s bits, clearing others and shifting right.
uint32_t get_s(const uint32_t instr) { return (instr & S_BITS) >> 21; }

// $d = $s + $t.
void mips_add(const uint32_t instr, uint32_t registers[32]) {
    int d = (int16_t)get_d(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[d] = registers[s] + registers[t];
}

// $d = $s - $t.
void mips_sub(const uint32_t instr, uint32_t registers[32]) {
    int d = (int16_t)get_d(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[d] = registers[s] - registers[t];
}
// $d = $s & $t.
void mips_and(const uint32_t instr, uint32_t registers[32]) {
    int d = (int16_t)get_d(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[d] = registers[s] & registers[t];
}

// $d = $s | $t.
void mips_or(const uint32_t instr, uint32_t registers[32]) {
    int d = (int16_t)get_d(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[d] = registers[s] | registers[t];
}

// $d = ($s < $t).
void mips_slt(const uint32_t instr, uint32_t registers[32]) {
    int d = (int16_t)get_d(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[d] = registers[s] < registers[t];
}

// $d = $s * $t.
void mips_mul(const uint32_t instr, uint32_t registers[32]) {
    int d = (int16_t)get_d(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[d] = registers[s] * registers[t];
}

// If ($s == $t) PC += i.
void mips_beq(FILE *const fptr, const uint32_t instr, uint32_t registers[32]) {
    int i = (int16_t)get_i(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    if (registers[s] != registers[t]) {
        return;
    }
    if (i >= 0) {
        for (int j = 0; j <= i; ++j) {
            get_next_instruction(fptr);
        }
    } else {
        for (int j = 0; j <= -1 * i; ++j) {
            goto_previous_instruction(fptr);
        }
    }
}

// If ($s != $t) PC += i.
void mips_bne(FILE *const fptr, const uint32_t instr, uint32_t registers[32]) {
    int i = (int16_t)get_i(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    if (registers[s] == registers[t]) {
        return;
    }
    if (i >= 0) {
        for (int j = 0; j <= i; ++j) {
            get_next_instruction(fptr);
        }
    } else {
        for (int j = 0; j <= -1 * i; ++j) {
            goto_previous_instruction(fptr);
        }
    }
}

// $t = $s + i.
void mips_addi(const uint32_t instr, uint32_t registers[32]) {
    int i = (int16_t)get_i(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[t] = registers[s] + (uint32_t)i;
}

// $t = $s < i.
void mips_slti(const uint32_t instr, uint32_t registers[32]) {
    int i = (int16_t)get_i(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[t] = registers[s] < (uint32_t)i;
}

// $t = $s & i.
void mips_andi(const uint32_t instr, uint32_t registers[32]) {
    int i = (int16_t)get_i(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[t] = registers[s] & (uint32_t)i;
}

// $t = $s | i.
void mips_ori(const uint32_t instr, uint32_t registers[32]) {
    int i = (int16_t)get_i(instr);
    int t = (int16_t)get_t(instr);
    int s = (int16_t)get_s(instr);
    registers[t] = registers[s] | (uint32_t)i;
}

// $t = i << 16u.
void mips_lui(const uint32_t instr, uint32_t registers[32]) {
    int i = (int16_t)get_i(instr);
    int t = (int16_t)get_t(instr);
    registers[t] = (uint32_t)i << 16u;
}

// Syscall function which returns non-zero if execution should stop.
int mips_syscall(uint32_t registers[32]) {
    uint32_t v0 = registers[2];
    uint32_t a0 = registers[4];
    switch (v0) {
    case 1:
        printf("%u", a0);
        break;
    case 11:
        printf("%c", a0 & 0xFFu);
        break;
    case 10:
        return 1;
    default:
        printf("Unknown system call: %d\n", v0);
        return 1;
    }
    return 0;
}