Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement LD, ADD #8

Merged
merged 2 commits into from
Jun 5, 2024

Conversation

Kelvinyu1117
Copy link
Contributor

Implemented Instructions:

  • LD Vx, byte
  • ADD Vx, byte
  • LD Vx, Vy
  • ADD Vx, Vy

@Kelvinyu1117 Kelvinyu1117 force-pushed the feature/kelvin-support-arith branch from cd08257 to 241bde6 Compare June 4, 2024 17:19
@@ -1,13 +1,13 @@
#include "op_code.hpp"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have already included op_code.hpp in <arabica/cpu/cpu.hpp>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks.

@@ -37,6 +55,26 @@ void CPU::run(const Memory& memory) {
// ToDo: raise interrupt? how to test the failed case?
};
} break;
case OP_CODE::LD_Vx_byte: {
uint8_t byte_v = instruction & 0x00FF;
registers[(instruction & 0x0F00) >> 8] = byte_v;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instruction execution should advance PC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, fixed, thanks.

#include <arabica/cpu/cpu.hpp>
#include <fmt/core.h>

namespace arabica {

void CPU::run(const Memory& memory) {
instruction = memory[pc] << 8 | memory[pc + 1];
uint16_t prefix = instruction & 0xF000;
uint16_t prefix = (instruction & 0xF000) >> 12;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it doesn’t need to shift left.

uint16_t prefix = instruction & 0xF000;
opcode          = static_cast<OP_CODE>(prefix);

If the prefix doesn't repeat, we already get the opcode.
Therefore, there's no need to handle the cases for 0x1, 0x2, 0x6, and 0x7.

case 0x1: {
  opcode = OP_CODE::JP_addr;
} break;
case 0x2: {
  opcode = OP_CODE::CALL_addr;
} break;
case 0x6: {
  opcode = OP_CODE::LD_Vx_byte;
} break;
case 0x7: {
  opcode = OP_CODE::ADD_Vx_byte;
} break;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, we should only handle the special cases, fixed, thanks.

@gapry gapry requested a review from ceciliachan1979 June 4, 2024 17:44
Copy link
Contributor

@ceciliachan1979 ceciliachan1979 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gapry
Copy link
Owner

gapry commented Jun 5, 2024

LGTM

@gapry gapry merged commit f3f8445 into gapry:main Jun 5, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants