loop: ;the main game loop jsr readKeys ;jump to subroutine readKeys jsr checkCollision ;jump to subroutine checkCollision jsr updateSnake ;jump to subroutine updateSnake jsr drawApple ;jump to subroutine drawApple jsr drawSnake ;jump to subroutine drawSnake jsr spinWheels ;jump to subroutine spinWheels jmp loop ;jump to loop (this is what makes it loop)
up: lda $10 ;put value stored at address $10 (the least significant byte, meaning the ;position in a 8x32 strip) in register A sec ;set carry flag sbc #$20 ;Subtract with Carry: subtract hex $20 (dec 32) together with the NOT of the ;carry bit from value in register A. If overflow occurs the carry bit is clear. ;This moves the snake up one row in its strip and checks for overflow sta $10 ;store value of register A at address $10 (the least significant byte ;of the head's position) bcc upup ;If the carry flag is clear, we had an overflow because of the subtraction, ;so we need to move to the strip above the current one rts ;return
发现在sta之前会先执行sbc操作(Substract with Borrow,带借位减法)对寄存器a进行更新,检查一下自己sbc操作的代码果然有问题!问题在于每次计算完成后调用了一个helper function还会再对carry bit进行一次计算导致寄存器a的值异常。改好后就能成功运行啦!蛇的运行也正常了,苹果也都能显示出来了,玩了一会一切正常!
改后长这样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
voidCPU::SBC(const AddressingMode &mode) { // A - M - C̅ -> A uint16_t addr = get_operand_address(mode); uint8_t data = read(addr);
uint16_t value = static_cast<uint16_t>(data); uint16_t carry_in = get_flag(C) ? 0 : 1; uint16_t result = static_cast<uint16_t>(registers.a) - value - carry_in;