使用键盘和显示器 MMIO 模拟器的 MIPS 无限循环

问题描述 投票:0回答:1

嗨,这是我在这里的第一篇文章。我正在 mips 中制作一个迷宫游戏,它使用位图显示以及键盘和显示 MMIO 模拟器进行移动。我创建了接受字符 WASD 的函数,当我按下任何移动键时,字符会按照我输入的方向无限移动。例如,按一次 D 会使我的角色继续向右移动,直到我的 MARS 崩溃。我只希望它移动一次,然后等待新的输入。下面附上我正在尝试调试的代码。如果可能的话,我想将 jr $ra 寄存器实现到我的代码中。谢谢

.eqv characterPos $t5

gameUpdateLoop:

        lw      $t3, 0xffff0004     # get input from user, then call appropriate movement function
    
        beq    $t3, 100, moveRight      # input d = move right
        beq    $t3, 97, moveLeft        # input a = move left
        beq    $t3, 119, moveUp         # input w = move up
        beq    $t3, 115, moveDown       # input s = move down
        beq    $t3, 120, exitGame       # input x = exit game
    
        jal gameUpdateLoop
    
moveRight:
        ### MOVE RIGHT ###
    
    addi characterPos, characterPos, 4
    sw  $t6, 0($t5)
    
    j gameUpdateLoop
moveLeft:
        ### MOVE LEFT ###

    addi characterPos, characterPos, -4
    sw  $t6, 0($t5)

    j gameUpdateLoop
moveUp:
        ### MOVE UP ###

    addi characterPos, characterPos, -256
    sw  $t6, 0($t5) 

    j gameUpdateLoop

moveDown:
        ### MOVE DOWN ###

    addi characterPos, characterPos, 256
    sw  $t6, 0($t5) 
    
    j gameUpdateLoop
    
exitGame:
    li $v0 10 
    syscall

我尝试使用 jal 而不是 j 来回调 gameUpdateLoop。我不确定还有什么问题

mips
1个回答
0
投票

您没有使用完整的 MMIO 模式。那个图案是,

  • 等待新角色
  • 输入字符
  • 处理它
  • 重复。

你错过了第一步。

在您键入字符之前它不会执行任何操作的原因是,当程序首次启动时,它(可能)输入 0(空字符),并且这会导致循环而不产生任何可见的操作。

但是,一旦您键入一个字符,这就是输入的内容,一遍又一遍,现在相同的无限循环对程序环境产生了明显的作用。


等待新字符的常用方法是在忙等待循环中使用轮询。

busywait:
    lw $t3, 0xffff0000
    andi $t3, $t3, 0x0001
    beq $t3, $zero, busywait

# now a new user-typed character is ready

    lw      $t3, 0xffff0004
    ...
© www.soinside.com 2019 - 2024. All rights reserved.