summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-04-27 23:26:40 -0400
committerbd <bdunahu@operationnull.com>2025-04-27 23:26:40 -0400
commita5366c56469bdec480c7eb463f9f71d7a3e3b2d2 (patch)
tree0e66c059b00e66297d4bd06a553b4721ff9eacc0
parentb50cb7048275f5b9255dea7f3ac1362aaa09fe83 (diff)
Fix push/pop instruction
-rw-r--r--inc/stage.h4
-rw-r--r--src/controller.cc1
-rw-r--r--src/ex.cc374
-rw-r--r--src/id.cc4
4 files changed, 194 insertions, 189 deletions
diff --git a/inc/stage.h b/inc/stage.h
index a5d7fe6..4e0c252 100644
--- a/inc/stage.h
+++ b/inc/stage.h
@@ -136,8 +136,6 @@ class Stage
if (v < 0 || v >= GPR_NUM) {
throw std::out_of_range("Invalid GPR index");
}
- cout << "dereference_register: " << v << endl;
- cout << "gprs[v]: " << gprs[v] << endl;
return gprs[v];
}
else if constexpr (std::is_same_v<T, std::array<signed int, V_R_LIMIT>>) {
@@ -190,4 +188,4 @@ class Stage
Response status;
};
-#endif /* STAGE_H_INCLUDED */ \ No newline at end of file
+#endif /* STAGE_H_INCLUDED */
diff --git a/src/controller.cc b/src/controller.cc
index a84126d..a5c6691 100644
--- a/src/controller.cc
+++ b/src/controller.cc
@@ -31,6 +31,7 @@ Controller::Controller(Stage *stage, Storage *storage, bool is_pipelined)
this->pc = 0x0;
this->checked_out = {};
this->gprs = {0};
+ this->vrs.fill({0});
this->gprs.at(2) = MEM_WORDS; // set the stack pointer
}
diff --git a/src/ex.cc b/src/ex.cc
index 6dfe44f..3f86ad4 100644
--- a/src/ex.cc
+++ b/src/ex.cc
@@ -22,7 +22,6 @@
#include "stage.h"
#include <unordered_map>
-
// Switch statements for each instruction
void EX::advance_helper()
{
@@ -32,212 +31,217 @@ void EX::advance_helper()
unsigned int pc;
Mnemonic m;
+ s1 = 0, s2 = 0, s3 = 0;
m = this->curr_instr->mnemonic;
pc = this->curr_instr->slot_B;
- if(this->is_vector_type(m)) {
- if(this->curr_instr->mnemonic != LOADV && this->curr_instr->mnemonic != STOREV){
+ if (this->is_vector_type(m)) {
+ if (this->curr_instr->mnemonic != LOADV &&
+ this->curr_instr->mnemonic != STOREV) {
v1 = this->curr_instr->operands.vector.slot_one;
v2 = this->curr_instr->operands.vector.slot_two;
- v3 = this->curr_instr->operands.vector.slot_three;
+ v3 = this->curr_instr->operands.vector.slot_three;
} else {
- v_immediate = this->curr_instr->operands.load_store_vector.immediate;
- v_base_addr = this->curr_instr->operands.load_store_vector.base_addr;
+ v_immediate =
+ this->curr_instr->operands.load_store_vector.immediate;
+ v_base_addr =
+ this->curr_instr->operands.load_store_vector.base_addr;
}
v_len = this->curr_instr->slot_A;
- if(v_len == 0){
- //clear destination vector reg
+ if (v_len == 0) {
+ // clear destination vector reg
v1.fill(0);
- }
- } else{
+ }
+ } else {
s1 = this->curr_instr->operands.integer.slot_one;
s2 = this->curr_instr->operands.integer.slot_two;
s3 = this->curr_instr->operands.integer.slot_three;
}
- if(this->is_logical(m)) {
+ if (this->is_logical(m)) {
this->set_condition(OF, false);
this->set_condition(UF, false);
}
- switch(m) {
- case ADD:
- this->set_condition(OF, ADDITION_OF_GUARD(s1, s2));
- this->set_condition(UF, ADDITION_UF_GUARD(s1, s2));
- s1 = s1 + s2;
- break;
-
- case SUB:
- this->set_condition(OF, SUBTRACTION_OF_GUARD(s1, s2));
- this->set_condition(UF, SUBTRACTION_UF_GUARD(s1, s2));
- s1 = s1 - s2;
- break;
-
- case MUL:
- this->set_condition(OF, MULTIPLICATION_OF_GUARD(s1, s2));
- this->set_condition(UF, MULTIPLICATION_UF_GUARD(s1, s2));
- s1 = s1 * s2;
- break;
-
- case QUOT:
- this->handle_divide(s1, s2, false);
- break;
-
- case REM:
- this->handle_divide(s1, s2, true);
- break;
-
- case SFTR:
- s1 = s1 >> s2;
- break;
-
- case SFTL:
- s1 = s1 << s2;
- break;
-
- case AND:
- s1 = s1 & s2;
- break;
-
- case OR:
- s1 = s1 | s2;
- break;
-
- case XOR:
- s1 = s1 ^ s2;
- break;
-
- case NOT:
- s1 = ~s1;
- break;
-
- case CMP:
- (s1 > s2) ? this->set_condition(GT, true)
- : this->set_condition(GT, false);
- (s1 == s2) ? this->set_condition(EQ, true)
- : this->set_condition(EQ, false);
+ switch (m) {
+ case ADD:
+ this->set_condition(OF, ADDITION_OF_GUARD(s1, s2));
+ this->set_condition(UF, ADDITION_UF_GUARD(s1, s2));
+ s1 = s1 + s2;
+ break;
+
+ case SUB:
+ this->set_condition(OF, SUBTRACTION_OF_GUARD(s1, s2));
+ this->set_condition(UF, SUBTRACTION_UF_GUARD(s1, s2));
+ s1 = s1 - s2;
+ break;
+
+ case MUL:
+ this->set_condition(OF, MULTIPLICATION_OF_GUARD(s1, s2));
+ this->set_condition(UF, MULTIPLICATION_UF_GUARD(s1, s2));
+ s1 = s1 * s2;
+ break;
+
+ case QUOT:
+ this->handle_divide(s1, s2, false);
+ break;
+
+ case REM:
+ this->handle_divide(s1, s2, true);
+ break;
+
+ case SFTR:
+ s1 = s1 >> s2;
+ break;
+
+ case SFTL:
+ s1 = s1 << s2;
+ break;
+
+ case AND:
+ s1 = s1 & s2;
+ break;
+
+ case OR:
+ s1 = s1 | s2;
+ break;
+
+ case XOR:
+ s1 = s1 ^ s2;
+ break;
+
+ case NOT:
+ s1 = ~s1;
+ break;
+
+ case CMP:
+ (s1 > s2) ? this->set_condition(GT, true)
+ : this->set_condition(GT, false);
+ (s1 == s2) ? this->set_condition(EQ, true)
+ : this->set_condition(EQ, false);
+ break;
+
+ case ADDI:
+ this->set_condition(OF, ADDITION_OF_GUARD(s1, s3));
+ this->set_condition(UF, ADDITION_UF_GUARD(s1, s3));
+ s1 = s1 + s3;
+ break;
+
+ case SUBI:
+ this->set_condition(OF, SUBTRACTION_OF_GUARD(s1, s3));
+ this->set_condition(UF, SUBTRACTION_UF_GUARD(s1, s3));
+ s1 = s1 - s3;
+ break;
+
+ case SFTRI:
+ s1 = s1 >> s3;
+ break;
+
+ case SFTLI:
+ s1 = s1 << s3;
+ break;
+
+ case ANDI:
+ s1 = s1 & s3;
+ break;
+
+ case ORI:
+ s1 = s1 | s3;
+ break;
+
+ case XORI:
+ s1 = s1 ^ s3;
+ break;
+
+ case LOAD:
+ case STORE:
+ case PUSH:
+ case POP:
+ s1 = s1 + s3;
+ break;
+
+ case JMP:
+ case JAL:
+ s1 = s1 + s2;
+ break;
+
+ case JRL:
+ s1 = pc + s2;
+ break;
+
+ case BEQ:
+ (this->get_condition(EQ)) ? s1 = pc + s2 : s1 = -1;
+ break;
+
+ case BGT:
+ (this->get_condition(GT)) ? s1 = pc + s2 : s1 = -1;
+ break;
+
+ case BUF:
+ (this->get_condition(UF)) ? s1 = pc + s2 : s1 = -1;
+ break;
+
+ case BOF:
+ (this->get_condition(OF)) ? s1 = pc + s2 : s1 = -1;
+ break;
+
+ case ADDV:
+ for (int i = 0; i < v_len; i++) {
+ this->set_condition(OF, ADDITION_OF_GUARD(v1[i], v2[i]));
+ this->set_condition(UF, ADDITION_UF_GUARD(v1[i], v2[i]));
+ v1[i] = v1[i] + v2[i];
+ }
+ break;
+ case SUBV:
+ for (int i = 0; i < v_len; i++) {
+ this->set_condition(OF, SUBTRACTION_OF_GUARD(v1[i], v2[i]));
+ this->set_condition(UF, SUBTRACTION_UF_GUARD(v1[i], v2[i]));
+ v1[i] = v1[i] - v2[i];
+ }
+ break;
+ case MULV:
+ for (int i = 0; i < v_len; i++) {
+ this->set_condition(OF, MULTIPLICATION_OF_GUARD(v1[i], v2[i]));
+ this->set_condition(UF, MULTIPLICATION_UF_GUARD(v1[i], v2[i]));
+ v1[i] = v1[i] * v2[i];
+ }
+ break;
+ case DIVV:
+ for (int i = 0; i < v_len; i++) {
+ this->handle_divide(v1[i], v2[i], false);
+ }
+ break;
+ case CEV:
+ int i;
+ for (i = 0; i < v_len; i++) {
+ if (v1[i] != v2[i]) {
break;
-
- case ADDI:
- this->set_condition(OF, ADDITION_OF_GUARD(s1, s3));
- this->set_condition(UF, ADDITION_UF_GUARD(s1, s3));
- s1 = s1 + s3;
- break;
-
- case SUBI:
- this->set_condition(OF, SUBTRACTION_OF_GUARD(s1, s3));
- this->set_condition(UF, SUBTRACTION_UF_GUARD(s1, s3));
- s1 = s1 - s3;
- break;
-
- case SFTRI:
- s1 = s1 >> s3;
- break;
-
- case SFTLI:
- s1 = s1 << s3;
- break;
-
- case ANDI:
- s1 = s1 & s3;
- break;
-
- case ORI:
- s1 = s1 | s3;
- break;
-
- case XORI:
- s1 = s1 ^ s3;
- break;
-
- case LOAD:
- case STORE:
- case PUSH:
- case POP:
- s1 = s1 + s3;
- break;
-
- case JMP:
- case JAL:
- s1 = s1 + s2;
- break;
-
- case JRL:
- s1 = pc + s2;
- break;
-
- case BEQ:
- (this->get_condition(EQ)) ? s1 = pc + s2 : s1 = -1;
- break;
-
- case BGT:
- (this->get_condition(GT)) ? s1 = pc + s2 : s1 = -1;
- break;
-
- case BUF:
- (this->get_condition(UF)) ? s1 = pc + s2 : s1 = -1;
- break;
-
- case BOF:
- (this->get_condition(OF)) ? s1 = pc + s2 : s1 = -1;
- break;
-
- case ADDV:
- for(int i=0;i<v_len;i++){
- this->set_condition(OF, ADDITION_OF_GUARD(v1[i],v2[i]));
- this->set_condition(UF, ADDITION_UF_GUARD(v1[i],v2[i]));
- v1[i] = v1[i] + v2[i];
}
- break;
- case SUBV:
- for(int i=0;i<v_len;i++){
- this->set_condition(OF, SUBTRACTION_OF_GUARD(v1[i],v2[i]));
- this->set_condition(UF, SUBTRACTION_UF_GUARD(v1[i],v2[i]));
- v1[i] = v1[i] - v2[i];
- }
- break;
- case MULV:
- for(int i=0;i<v_len;i++){
- this->set_condition(OF, MULTIPLICATION_OF_GUARD(v1[i],v2[i]));
- this->set_condition(UF, MULTIPLICATION_UF_GUARD(v1[i],v2[i]));
- v1[i] = v1[i] * v2[i];
- }
- break;
- case DIVV:
- for(int i=0;i<v_len;i++){
- this->handle_divide(v1[i],v2[i],false);
- }
- break;
- case CEV:
- int i;
- for(i=0;i<v_len;i++){
- if(v1[i] != v2[i]){
- break;
- }
- }
- if(i == v_len){
- this->set_condition(EQ, true);
- } else {
- this->set_condition(EQ, false);
- }
- break;
- case LOADV:
- case STOREV:
- v_base_addr = v_base_addr + v_immediate;
- break;
-
- case RET:
- case NOP:
- break;
-
+ }
+ if (i == v_len) {
+ this->set_condition(EQ, true);
+ } else {
+ this->set_condition(EQ, false);
+ }
+ break;
+ case LOADV:
+ case STOREV:
+ v_base_addr = v_base_addr + v_immediate;
+ break;
+
+ case RET:
+ case NOP:
+ break;
}
- if(this->is_vector_type(m)) {
- if(this->curr_instr->mnemonic != LOADV && this->curr_instr->mnemonic != STOREV){
- this->curr_instr->operands.vector.slot_one = v1;
+ if (this->is_vector_type(m)) {
+ if (this->curr_instr->mnemonic != LOADV &&
+ this->curr_instr->mnemonic != STOREV) {
+ this->curr_instr->operands.vector.slot_one = v1;
} else {
- this->curr_instr->operands.load_store_vector.base_addr = v_base_addr;
- }
- } else{
+ this->curr_instr->operands.load_store_vector.base_addr =
+ v_base_addr;
+ }
+ } else {
this->curr_instr->operands.integer.slot_one = s1;
}
this->status = OK;
diff --git a/src/id.cc b/src/id.cc
index 4358150..e4790ef 100644
--- a/src/id.cc
+++ b/src/id.cc
@@ -285,6 +285,7 @@ void ID::decode_J_type(signed int &s1)
s3 = 2; // stack pointer
s1 = -1; // increment amount
r1 = this->read_guard(s2);
+ this->curr_instr->operands.integer.slot_one = s1;
this->curr_instr->operands.integer.slot_two = s2;
r2 = (this->is_checked_out(s3)) ? STALLED
: OK; // we read the stack pointer
@@ -298,6 +299,7 @@ void ID::decode_J_type(signed int &s1)
s2 = s1; // destination
s3 = 2; // stack pointer
s1 = 1; // increment amount
+ this->curr_instr->operands.integer.slot_one = s1;
r1 = (this->is_checked_out(s3)) ? STALLED
: OK; // we read the stack pointer
if (r1 == OK) {
@@ -316,7 +318,7 @@ void ID::decode_J_type(signed int &s1)
if(this->status == OK){
this->curr_instr->operands.integer.slot_one = s1;
this->curr_instr->operands.integer.slot_two = s2;
- this->curr_instr->operands.integer.slot_three = s3;
+ this->curr_instr->operands.integer.slot_three = s3;
}
}
}