Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)

这里有一份GBC模拟器指令处理的代码,其中某些指令的实现不正确,请逐一指出,逐一修复。#include "CPUContext.hpp" namespace GBCEmu {…

  1. User

    这里有一份GBC模拟器指令处理的代码,其中某些指令的实现不正确,请逐一指出,逐一修复。#include "CPUContext.hpp" namespace GBCEmu {

    CPUContext::CPUContext(Bus& bus, CPURegister& reg) : bus_(bus), reg_(reg) { PROCESSOR_[InstType::NOP] = this { this->nop(); }; PROCESSOR_[InstType::LD] = this { this->ld(); }; PROCESSOR_[InstType::ADD] = this { this->add(); }; PROCESSOR_[InstType::ADC] = this { this->adc(); }; PROCESSOR_[InstType::SUB] = this { this->sub(); }; PROCESSOR_[InstType::SBC] = this { this->sbc(); }; PROCESSOR_[InstType::INC] = this { this->inc(); }; PROCESSOR_[InstType::DEC] = this { this->dec(); }; PROCESSOR_[InstType::DI] = this { this->di(); }; PROCESSOR_[InstType::JP] = this { this->jp(); }; PROCESSOR_[InstType::RET] = this { this->ret(); }; PROCESSOR_[InstType::RETI] = this { this->reti(); }; PROCESSOR_[InstType::CALL] = this { this->call(); }; PROCESSOR_[InstType::RST] = this { this->rst(); }; PROCESSOR_[InstType::JR] = this { this->jr(); }; PROCESSOR_[InstType::XOR] = this { this->xor(); }; PROCESSOR_[InstType::POP] = this { this->pop(); }; PROCESSOR_[InstType::PUSH] = this { this->push(); }; };

    // 根据寻址模式准备好数据 bool CPUContext::checkCond() { bool z = reg_.getZFlag(); bool c = reg_.getCFlag();

    switch (curInst_.cond) {
        case CondType::NONE: return true;
        case CondType::C: return c;
        case CondType::NC: return !c;
        case CondType::Z: return z;
        case CondType::NZ: return !z;
    }
    
    return false;
    

    }

    void CPUContext::nop() { return; } void CPUContext::ld() { if (writeToMemo_) { if (is16bitReg(curInst_.reg2)) { bus_.write16(memoDest_, fetchedData_); } else { bus_.write(memoDest_, fetchedData_); } writeToMemo_ = false; return; }

    if (curInst_.mode == AddrMode::HL_SPR) {
        uint8_t hFlag = (reg_.readReg(curInst_.reg1) & 0xF) +
            (reg_.readReg(curInst_.reg2) & 0xF) >= 0x10;
        uint8_t cFlag = (reg_.readReg(curInst_.reg1) & 0xFF) +
            (reg_.readReg(curInst_.reg2) & 0xFF) >= 0x100;
        reg_.setFlags(0, 0, hFlag, cFlag);
        reg_.writeReg(curInst_.reg1, reg_.readReg(curInst_.reg2) +
            static_cast<int8_t>(fetchedData_));
        return;
    }
    reg_.writeReg(curInst_.reg1, fetchedData_);
    

    }

    void CPUContext::inc() { if (is16bitReg(curInst_.reg1)) { // ... emu } uint16_t val = reg_.readReg(curInst_.reg1) + 1; if (writeToMemo_) { val = bus_.read(fetchedData_) & 0xFF + 1; bus_.write(fetchedData_, val); } else { reg_.writeReg(curInst_.reg1, val); }

    if (curOpcode_ & 0x03 == 0x03) {
        return;
    }
    
    reg_.setFlags(val == 0, 0, (val & 0x0F) == 0, -1);
    

    }

    void CPUContext::dec() { if (is16bitReg(curInst_.reg1)) { // ... emu } uint16_t val = reg_.readReg(curInst_.reg1) - 1; if (writeToMemo_) { val = bus_.read(fetchedData_) & 0xFF - 1; bus_.write(fetchedData_, val); } else { reg_.writeReg(curInst_.reg1, val); }

    if (curOpcode_ & 0x0B == 0x0B) {
        return;
    }
    
    reg_.setFlags(val == 0, 1, (val & 0x0F) == 0x0F, -1);
    

    }

    uint8_t CPUContext::stackPop() { return bus_.read(reg_.sp_++); }

    uint16_t CPUContext::stackPop16() { uint16_t lo; lo = stackPop(); uint16_t hi; hi = stackPop(); return (hi << 8) | lo; }

    void CPUContext::stackPush(uint8_t val) { bus_.write(--reg_.sp_, val); }

    void CPUContext::stackPush16(uint16_t val) { stackPush((val >> 8) & 0xFF); stackPush(val & 0xFF); }

    void CPUContext::di() { interruptEnabled_ = false; }

    void CPUContext::go2(uint16_t addr, bool pushPC) { if (checkCond()) { if (pushPC) { stackPush16(reg_.pc_); } reg_.pc_ = addr; // emu_.cycle(1); } }

    void CPUContext::jp() { go2(fetchedData_, false); }

    void CPUContext::ret() { if (curInst_.cond != CondType::NONE) { // emu_cycle }

    if (checkCond()) {
        uint16_t lo;
        lo = stackPop();
        uint16_t hi;
        hi = stackPop();
        uint16_t res = (hi << 8) | lo;
        reg_.pc_ = res;
    }
    

    }

    void CPUContext::reti() { interruptEnabled_ = true; ret(); }

    void CPUContext::call() { go2(fetchedData_, true); }

    void CPUContext::rst() { go2(curInst_.param, true); }

    void CPUContext::jr() { char rel = static_cast(fetchedData_ & 0xFF); uint16_t addr = reg_.pc_ + rel; go2(addr, false); }

    void CPUContext::xor() { reg_.a_ ^= fetchedData_ & 0xFF; reg_.setFlags(reg_.a_, 0, 0, 0); }

    void CPUContext::pop() { uint16_t lo; lo = stackPop(); uint16_t hi; hi = stackPop(); uint16_t res = (hi << 8) | lo; reg_.writeReg(curInst_.reg1, res & (curInst_.reg1 == RegType::AF ? 0xFFF0 : 0xFFFF)); }

    void CPUContext::push() { uint16_t hi; hi = (reg_.readReg(curInst_.reg1) >> 8) & 0xFF; stackPush(hi); uint16_t lo; lo = hi = reg_.readReg(curInst_.reg1) & 0xFF; stackPush(lo); }

    void CPUContext::add() { uint16_t val = reg_.readReg(curInst_.reg1) + fetchedData_; bool is16bit = is16bitReg(curInst_.reg1); if (is16bit) { // emu... }

    if (curInst_.reg1 == RegType::SP) {
        val = reg_.readReg(curInst_.reg1) + static_cast<int8_t>(fetchedData_);
    }
    
    int z = (val & 0xFF) == 0;
    int h = ((reg_.readReg(curInst_.reg1) & 0xF) + (fetchedData_ & 0xF)) >= 0x10;
    int c = (static_cast<int>(reg_.readReg(curInst_.reg1) & 0xFF) + static_cast<int>(fetchedData_ & 0xFF)) >= 0x100;
    if (is16bit) {
        z = -1;
        h = ((reg_.readReg(curInst_.reg1) & 0xFFF) + (static_cast<int8_t>(fetchedData_) & 0xFFF)) >= 0x1000;
        c = (static_cast<uint32_t>(reg_.readReg(curInst_.reg1) & 0xFFFF) + static_cast<uint32_t>(fetchedData_ & 0xFFFF)) >= 0x10000;
    }
    
    if (curInst_.reg1 == RegType::SP) {
        z = 0;
        h = ((reg_.readReg(curInst_.reg1) & 0xF) + (static_cast<int8_t>(fetchedData_) & 0xF)) >= 0x10;
        c = (static_cast<uint16_t>(reg_.readReg(curInst_.reg1) & 0xFF) + static_cast<uint16_t>(fetchedData_ & 0xFF)) >= 0x100;
    }
    
    reg_.writeReg(curInst_.reg1, val & 0xFFFF);
    reg_.setFlags(z, 0, h, c);
    

    }

    void GBCEmu::CPUContext::sub() { uint16_t val = reg_.readReg(curInst_.reg1) - fetchedData_;

    int z = val == 0;
    int h = ((static_cast<int>(reg_.readReg(curInst_.reg1)) & 0xF) - (static_cast<int>(fetchedData_) & 0xF)) < 0;
    int c = (static_cast<int>(reg_.readReg(curInst_.reg1)) - static_cast<int>(fetchedData_)) < 0;
    
    reg_.writeReg(curInst_.reg1, val);
    reg_.setFlags(z, 1, h, c);
    

    }

    void GBCEmu::CPUContext::sbc() { uint8_t val = fetchedData_ + reg_.getCFlag();

    int z = (reg_.readReg(curInst_.reg1) - val) == 0;
    int h = ((static_cast<int>(reg_.readReg(curInst_.reg1)) & 0xF) - (static_cast<int>(fetchedData_) & 0xF) - (static_cast<int>(reg_.getCFlag()))) < 0;
    int c = (static_cast<int>(reg_.readReg(curInst_.reg1)) - static_cast<int>(fetchedData_)) - reg_.getCFlag() < 0;
    
    reg_.writeReg(curInst_.reg1, (reg_.readReg(curInst_.reg1) - val));
    reg_.setFlags(z, 1, h, c);
    

    }

    void GBCEmu::CPUContext::adc() { uint16_t u = fetchedData_; uint16_t a = reg_.a_; uint16_t c = reg_.getCFlag();

    reg_.a_ = (a + u + c) & 0xFF;
    reg_.setFlags(a == 0, 0, (a & 0xF) + (u & 0xF) + c > 0xF, a + u + c > 0xFF);
    

    }

    void CPUContext::process() { auto it = PROCESSOR_.find(curInst_.type); if(it != PROCESSOR_.end()) { (it->second)(); return; } throw std::runtime_error("failed to find processor of instType:" + static_cast(curInst_.type)); }

    void CPUContext::fetchData() { FUNC_TRACE("CPU::fetchData"); uint16_t low, high; switch(curInst_.mode) { case AddrMode::IMP: return; case AddrMode::R: fetchedData_ = reg_.readReg(curInst_.reg1); return; case AddrMode::R_R: fetchedData_ = reg_.readReg(curInst_.reg2); return; case AddrMode::MR_R: fetchedData_ = reg_.readReg(curInst_.reg2); memoDest_ = bus_.read(reg_.readReg(curInst_.reg1)); writeToMemo_ = true; return; case AddrMode::MR: memoDest_ = reg_.readReg(curInst_.reg1); writeToMemo_ = true; fetchedData_ = reg_.readReg(curInst_.reg1); return; case AddrMode::D8: fetchedData_ = bus_.read(reg_.pc_); ++reg_.pc_; return; case AddrMode::MR_D8: fetchedData_ = bus_.read(reg_.pc_); ++reg_.pc_; memoDest_ = reg_.readReg(curInst_.reg1); writeToMemo_ = true; return; case AddrMode::MC_R: fetchedData_ = reg_.readReg(curInst_.reg2); memoDest_ = reg_.readReg(RegType::C) | 0xFF00; writeToMemo_ = true; return; case AddrMode::R_MC: fetchedData_ = bus_.read(reg_.readReg(RegType::C) | 0xFF00); return; case AddrMode::R_HLI: fetchedData_ = bus_.read(reg_.readReg(RegType::HL)); reg_.writeReg(RegType::HL, reg_.readReg(RegType::HL) + 1); return; case AddrMode::R_HLD: fetchedData_ = bus_.read(reg_.readReg(RegType::HL)); reg_.writeReg(RegType::HL, reg_.readReg(RegType::HL) - 1); return; case AddrMode::HLI_R: fetchedData_ = reg_.readReg(curInst_.reg2); memoDest_ = reg_.readReg(RegType::HL); writeToMemo_ = true; reg_.writeReg(RegType::HL,reg_.readReg(RegType::HL) + 1); return; case AddrMode::HLD_R: fetchedData_ = reg_.readReg(curInst_.reg2); memoDest_ = reg_.readReg(RegType::HL); std::cout << "memodest:" << memoDest_ << "\n"; writeToMemo_ = true; reg_.writeReg(RegType::HL, reg_.readReg(RegType::HL) - 1); return; case AddrMode::R_D8: fetchedData_ = bus_.read(reg_.pc_); // emu_.cycle(1); ++reg_.pc_; return; case AddrMode::R_A8: fetchedData_ = bus_.read(bus_.read(reg_.pc_)); // emu_.cycle(1); ++reg_.pc_; return; case AddrMode::A8_R: memoDest_ = bus_.read(reg_.pc_); fetchedData_ = reg_.readReg(curInst_.reg2); writeToMemo_ = true; // emu_.cycle(1); ++reg_.pc_; return; case AddrMode::R_HA8: fetchedData_ = 0xFF00 | bus_.read(bus_.read(reg_.pc_)); // emu_.cycle(1); ++reg_.pc_; return; case AddrMode::HA8_R: memoDest_ = 0xFF00 | bus_.read(reg_.pc_); fetchedData_ = reg_.readReg(curInst_.reg2); writeToMemo_ = true; // emu_.cycle(1); ++reg_.pc_; return; case AddrMode::A16_R: low = bus_.read(reg_.pc_); // emu_.cycle(1); high = bus_.read(reg_.pc_ + 1); // emu_.cycle(1); memoDest_ = low | (high << 8); writeToMemo_ = true; fetchedData_ = reg_.readReg(curInst_.reg2); reg_.pc_ += 2; return; case AddrMode::R_A16: low = bus_.read(reg_.pc_); // emu_.cycle(1); high = bus_.read(reg_.pc_ + 1); // emu_.cycle(1); fetchedData_ = bus_.read(low | (high << 8)); reg_.pc_ += 2; return; case AddrMode::R_D16: case AddrMode::D16: low = bus_.read(reg_.pc_); // emu_.cycle(1); high = bus_.read(reg_.pc_ + 1); // emu_.cycle(1); fetchedData_ = low | (high << 8); reg_.pc_ += 2; return; default: std::cout << "UNKNOWN ADDRESSING MODE \n"; exit(-6); } } }

  2. Assistant

    感谢您提供的代码。经过仔细检查,发现以下指令实现存在一些问题。下面我将逐一指出这些问题并提供相应的修复方案。

    1. ld() 指令

    问题: LD 指令不应该修改任何标志位(Flags),但在处理 AddrMode::HL_SPR 时,当前实现错误地设置了标志位。

    修复方案: 移除 setFlags 调用,以确保 LD 指令不影响标志位。

    修正后的代码:

    void CPUContext::ld()
    {
        if (writeToMemo_) {
            if (is16bitReg(curInst_.reg2)) {
                bus_.write16(memoDest_, fetchedData_);
            } else {
                bus_.write(memoDest_, fetchedData_);
            }
            writeToMemo_ = false;
            return;
        }
    
        if (curInst_.mode == AddrMode::HL_SPR) {
            // 处理 HL_SPR 寻址模式的逻辑,但不设置标志位
            reg_.writeReg(curInst_.reg1, reg_.readReg(curInst_.reg2) + static_cast<int8_t>(fetchedData_));
            return;
        }
        reg_.writeReg(curInst_.reg1, fetchedData_);
    }
    

    2. inc() 指令

    问题:

    • 运算符优先级错误:bus_.read(fetchedData_) & 0xFF + 1 实际上会被解析为 bus_.read(fetchedData_) & (0xFF + 1),导致计算错误。
    • 条件判断中缺少括号,导致运算顺序错误:curOpcode_ & 0x03 == 0x03 实际上被解析为 curOpcode_ & (0x03 == 0x03),结果总是 curOpcode_ & 1。
    • 未实现 16 位寄存器的 INC 操作。

    修复方案:

    • 添加括号以确保正确的运算顺序。
    • 完整实现 16 位寄存器的 INC 操作。

    修正后的代码:

    void CPUContext::inc()
    {
        if (is16bitReg(curInst_.reg1)) {
            uint16_t val = reg_.readReg(curInst_.reg1) + 1;
            reg_.writeReg(curInst_.reg1, val);
            // 16 位寄存器的 INC 操作通常不影响标志位
            return;
        }
    
        uint16_t val = reg_.readReg(curInst_.reg1) + 1;
        if (writeToMemo_) {
            val = (bus_.read(fetchedData_) & 0xFF) + 1;
            bus_.write(fetchedData_, val & 0xFF); // 确保写回的是 8 位
        } else {
            reg_.writeReg(curInst_.reg1, val & 0xFF); // 保持 8 位
        }
    
        if ( (curOpcode_ & 0x03) == 0x03 ) { // 添加括号以确保正确的条件判断
            return;
        }
    
        // 设置标志位:Z, N=0, H, C 不变
        bool z = (val & 0xFF) == 0;
        bool h = ((reg_.readReg(curInst_.reg1) & 0xF) + 1) > 0xF;
        reg_.setFlags(z, 0, h, -1); // C 不改变
    }
    

    3. dec() 指令

    问题:

    • 运算符优先级错误:bus_.read(fetchedData_) & 0xFF - 1 实际上被解析为 bus_.read(fetchedData_) & (0xFF - 1),导致计算错误。
    • 条件判断中缺少括号,导致运算顺序错误:curOpcode_ & 0x0B == 0x0B 实际上被解析为 curOpcode_ & (0x0B == 0x0B),结果总是 curOpcode_ & 1。
    • 未实现 16 位寄存器的 DEC 操作。

    修复方案:

    • 添加括号以确保正确的运算顺序。
    • 完整实现 16 位寄存器的 DEC 操作。

    修正后的代码:

    void CPUContext::dec()
    {
        if (is16bitReg(curInst_.reg1)) {
            uint16_t val = reg_.readReg(curInst_.reg1) - 1;
            reg_.writeReg(curInst_.reg1, val);
            // 16 位寄存器的 DEC 操作通常不影响标志位
            return;
        }
    
        uint16_t val = reg_.readReg(curInst_.reg1) - 1;
        if (writeToMemo_) {
            val = (bus_.read(fetchedData_) & 0xFF) - 1;
            bus_.write(fetchedData_, val & 0xFF); // 确保写回的是 8 位
        } else {
            reg_.writeReg(curInst_.reg1, val & 0xFF); // 保持 8 位
        }
    
        if ( (curOpcode_ & 0x0B) == 0x0B ) { // 添加括号以确保正确的条件判断
            return;
        }
    
        // 设置标志位:Z, N=1, H, C 不变
        bool z = (val & 0xFF) == 0;
        bool h = ((reg_.readReg(curInst_.reg1) & 0xF) - 1) < 0;
        reg_.setFlags(z, 1, h, -1); // C 不改变
    }
    

    4. xor() 指令

    问题: setFlags 方法传递了错误的参数。当前代码使用了 reg_.a_ 作为 Z 标志,而应使用 reg_.a_ == 0。

    修复方案: 将 reg_.a_ 替换为 reg_.a_ == 0 以正确设置 Z 标志。

    修正后的代码:

    void CPUContext::xor_()
    {
        reg_.a_ ^= (fetchedData_ & 0xFF);
        bool z = (reg_.a_ == 0);
        // XOR 指令应重置 N, H, C 标志
        reg_.setFlags(z, 0, 0, 0);
    }
    

    5. push() 指令

    问题: 在 push() 方法中,lo 被错误地赋值为与 hi 相同的值。

    修复方案: 将 lo 赋值为寄存器的低 8 位,而不是重复赋值为 hi。

    修正后的代码:

    void CPUContext::push()
    {
        uint16_t hi = (reg_.readReg(curInst_.reg1) >> 8) & 0xFF;
        stackPush(hi);
        uint16_t lo = reg_.readReg(curInst_.reg1) & 0xFF; // 修正为读取低 8 位
        stackPush(lo);
    }
    

    6. adc() 指令

    问题: 在 adc() 方法中,setFlags 使用了错误的 Z 标志值。当前代码使用了 a == 0,而应该使用 reg_.a_ == 0(即加法后的结果)。

    修复方案: 修改 setFlags 的第一个参数为加法后的结果是否为零。

    修正后的代码:

    void CPUContext::adc()
    {
        uint16_t u = fetchedData_;
        uint16_t a = reg_.a_;
        uint16_t c = reg_.getCFlag();
    
        uint16_t result = a + u + c;
        reg_.a_ = result & 0xFF;
    
        bool z = (reg_.a_ == 0);
        bool h = ((a & 0xF) + (u & 0xF) + c) > 0xF;
        bool carry = result > 0xFF;
    
        reg_.setFlags(z, 0, h, carry);
    }
    

    7. sbc() 指令

    问题: 虽然当前实现逻辑上是正确的,但为了提高代码的可读性和安全性,建议使用明确的类型转换和括号。

    修复方案: 优化代码,增加括号和类型转换以确保操作顺序和类型安全。

    修正后的代码:

    void CPUContext::sbc()
    {
        uint8_t n = fetchedData_;
        uint8_t c = reg_.getCFlag();
        uint16_t reg_val = reg_.readReg(curInst_.reg1);
        uint16_t val = n + c;
    
        uint16_t result = reg_val - val;
    
        bool z = (result & 0xFF) == 0;
        bool h = ((reg_val & 0xF) - (n & 0xF) - c) < 0;
        bool carry = result < val;
    
        reg_.writeReg(curInst_.reg1, result & 0xFF);
        reg_.setFlags(z, 1, h, carry);
    }
    

    8. 其他潜在问题

    虽然上述是主要存在问题的指令,但为了确保代码的整体正确性,还建议检查以下部分:

    • add() 指令的标志位设置: 确保在 ADD 指令中,Z 标志基于加法后的结果而非加法前的值。

      修正后的 add() 部分:

      void CPUContext::add()
      {
          if (is16bitReg(curInst_.reg1)) {
              uint32_t val = reg_.readReg(curInst_.reg1) + fetchedData_;
              bool h = ((reg_.readReg(curInst_.reg1) & 0xFFF) + (fetchedData_ & 0xFFF)) > 0xFFF;
              bool c = (static_cast<uint32_t>(reg_.readReg(curInst_.reg1)) + fetchedData_) > 0xFFFF;
      
              reg_.writeReg(curInst_.reg1, val & 0xFFFF);
              reg_.setFlags(false, 0, h, c); // 16位加法通常不设置Z标志
              return;
          }
      
          if (curInst_.reg1 == RegType::SP) {
              // 处理对SP的加法
              int16_t offset = static_cast<int8_t>(fetchedData_);
              uint32_t val = reg_.readReg(curInst_.reg1) + offset;
      
              bool h = ((reg_.readReg(curInst_.reg1) & 0xF) + (offset & 0xF)) > 0xF;
              bool c = ((reg_.readReg(curInst_.reg1) & 0xFF) + (offset & 0xFF)) > 0xFF;
      
              reg_.writeReg(curInst_.reg1, val & 0xFFFF);
              reg_.setFlags(false, 0, h, c);
              return;
          }
      
          uint16_t a = reg_.readReg(RegType::A);
          uint16_t n = fetchedData_;
          uint16_t result = a + n;
      
          bool z = (result & 0xFF) == 0;
          bool h = ((a & 0xF) + (n & 0xF)) > 0xF;
          bool c = result > 0xFF;
      
          reg_.writeReg(RegType::A, result & 0xFF);
          reg_.setFlags(z, 0, h, c);
      }
      
    • pop() 指令的完整性: 确保在 pop() 指令中,如果弹出的是 AF,正确地分离 A 和 F 寄存器,且对 F 寄存器进行有效位掩码(通常 F 寄存器的低 4 位应为零)。

      修正后的代码:

      void CPUContext::pop()
      {
          uint16_t lo = stackPop();
          uint16_t hi = stackPop();
          uint16_t res = (hi << 8) | lo;
          if (curInst_.reg1 == RegType::AF) {
              reg_.writeReg(RegType::A, (res & 0xFFF0) | (reg_.readReg(RegType::A) & 0x000F));
              // 保持 F 寄存器的低4位为0
              reg_.writeReg(RegType::F, res & 0xF0);
          }
          else {
              reg_.writeReg(curInst_.reg1, res);
          }
      }
      

    9. 总结

    上述修正涵盖了主要存在问题的指令。建议综合测试各指令,确保它们在各种寻址模式和操作数下的正确性。同时,保证对所有寻址模式和寄存器类型的支持,以提高模拟器的准确性。

    如果在修复过程中遇到其他具体问题或需要进一步的优化,请随时提供更多细节。