generated from cc3-ug/proj01-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.c
146 lines (134 loc) · 5.36 KB
/
part2.c
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
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
#include "utils.h"
#include "riscv.h"
void execute_instruction(Instruction instruction, Processor *processor, Byte *memory);
void execute_itype_except_load(Instruction instruction, Processor *processor) {
switch(instruction.itype.funct3) {
case 0b000:
// Corrected to FUNCT3_ADD
processor->R[instruction.itype.rd] = processor->R[instruction.itype.rs1] + ((int32_t)instruction.itype.imm << 20 >> 20);
break;
case 0b010:
// Corrected to FUNCT3_SLT
processor->R[instruction.rtype.rd] = (processor->R[instruction.rtype.rs1] < processor->R[instruction.rtype.rs2]) ? 1 : 0;
break;
case 0b011:
// Corrected to FUNCT3_SLTU
processor->R[instruction.rtype.rd] = (processor->R[instruction.rtype.rs1] < processor->R[instruction.rtype.rs2]) ? 1 : 0;
break;
case 0b100:
// Corrected to FUNCT3_XOR
processor->R[instruction.rtype.rd] = processor->R[instruction.rtype.rs1] ^ processor->R[instruction.rtype.rs2];
break;
case 0b101:
// Corrected to FUNCT3_SRL
processor->R[instruction.rtype.rd] = processor->R[instruction.rtype.rs1] >> processor->R[instruction.rtype.rs2];
break;
case 0b110:
// Corrected to FUNCT3_OR
processor->R[instruction.rtype.rd] = processor->R[instruction.rtype.rs1] | processor->R[instruction.rtype.rs2];
break;
case 0b111:
// Corrected to FUNCT3_AND
processor->R[instruction.rtype.rd] = processor->R[instruction.rtype.rs1] & processor->R[instruction.rtype.rs2];
break;
default:
handle_invalid_instruction(instruction);
exit(-1);
break;
}
}
void execute_itype_except_load(Instruction instruction, Processor *processor) {
switch(instruction.itype.funct3) {
case 0b000:
// Corrected to FUNCT3_ADDI
processor->R[instruction.itype.rd] = processor->R[instruction.itype.rs1] + sign_extend(instruction.itype.imm, 12);
break;
default:
handle_invalid_instruction(instruction);
exit(-1);
break;
}
}
void execute_ecall(Processor *p, Byte *memory) {
switch(p->R[10]) { // Cambiado de REG_A7 a 10
case 1: // Cambiado de ECALL_PRINT_INT a 1
printf("Print integer: %d\n", p->R[11]); // Cambiado de REG_A0 a 11
break;
case 4: // Cambiado de ECALL_PRINT_STRING a 4
for (int i = p->R[11]; memory[i] != '\0'; ++i) { // Cambiado de REG_A0 a 11
printf("%c", memory[i]);
}
printf("\n");
break;
case 10: // Cambiado de ECALL_EXIT a 10
exit(p->R[11]); // Cambiado de REG_A0 a 11
break;
default:
printf("Illegal ecall number %d\n", -1);
exit(-1);
break;
}
}
void execute_branch(Instruction instruction, Processor *processor) {
Word offset = sign_extend(((instruction.btype.imm7 << 5) | instruction.btype.imm5) << 1, 12);
switch(instruction.btype.funct3) {
case 0b000:
// Corrected to FUNCT3_BEQ
if (processor->R[instruction.rtype.rs1] == processor->R[instruction.rtype.rs2]) {
processor->PC += offset;
} else {
processor->PC += 4;
}
break;
default:
handle_invalid_instruction(instruction);
exit(-1);
break;
}
}
void execute_load(Instruction instruction, Processor *processor, Byte *memory) {
Word address = processor->R[instruction.itype.rs1] + sign_extend(instruction.itype.imm, 12);
switch(instruction.itype.funct3) {
case 0b000:
// Corrected to FUNCT3_LB
processor->R[instruction.itype.rd] = sign_extend(memory[address], 8);
break;
default:
handle_invalid_instruction(instruction);
exit(-1);
break;
}
}
void execute_store(Instruction instruction, Processor *processor, Byte *memory) {
Word address = processor->R[instruction.stype.rs1] + sign_extend(instruction.stype.imm7, 12);
Word value = processor->R[instruction.stype.rs2];
switch(instruction.stype.funct3) {
case 0b000:
// Corrected to FUNCT3_SB
memory[address] = value & 0xFF;
break;
default:
handle_invalid_instruction(instruction);
exit(-1);
break;
}
}
void execute_jalr(Instruction instruction, Processor *processor) {
Word return_address = processor->PC + 4;
processor->PC = (processor->R[instruction.itype.rs1] + sign_extend(instruction.itype.imm, 12)) & ~1;
processor->R[instruction.itype.rd] = return_address;
}
void execute_jal(Instruction instruction, Processor *processor) {
Word return_address = processor->PC + 4;
processor->PC += sign_extend(instruction.jtype.imm << 1, 21);
processor->R[instruction.jtype.rd] = return_address;
}
void execute_auipc(Instruction instruction, Processor *processor) {
processor->R[instruction.utype.rd] = processor->PC + sign_extend(instruction.utype.imm << 12, 32);
}
void execute_lui(Instruction instruction, Processor *processor) {
processor->R[instruction.utype.rd] = instruction.utype.imm << 12;
}