【问题标题】:Why is the NES init code like this (6502 assembly)?为什么NES初始化代码是这样的(6502汇编)?
【发布时间】:2013-08-05 21:37:49
【问题描述】:
; We now have about 30,000 cycles to burn before the PPU stabilizes.
; One thing we can do with this time is put RAM in a known state.
; Here we fill it with $00, which matches what (say) a C compiler
; expects for BSS.  Conveniently, X is still 0.
txa
@clrmem:
    sta $000,x
    sta $100,x
    sta $300,x
    sta $400,x
    sta $500,x
    sta $600,x
    sta $700,x  ; Remove this if you're storing reset-persistent data

; We skipped $200,x on purpose.  Usually, RAM page 2 is used for the
; display list to be copied to OAM.  OAM needs to be initialized to
; $EF-$FF, not 0, or you'll get a bunch of garbage sprites at (0, 0).

inx
bne @clrmem

基本上它似乎做的是用 0 初始化所有上述地址,递增 x,跳回标签的开头,用 1 填充所有地址,再次递增 x,它会一遍又一遍地发生,直到 BNE假(所以如果零标志为 1)。所以基本上当 X 为 0xFF 时发生 INX 时,X 将为 0(对吗?),BNE 将为假,它会停止分支并继续执行程序。我理解其余的大部分内容,但为什么它会使用看似随机的内存地址呢?为什么是 0x000、0x100、0x200 等?为什么这个循环会发生 256 次?之后的代码显示程序等待第二个 VBLANK(NES PPU 相关),我有点明白,但是 256 时间循环是怎么回事?它声明它需要燃烧大约 30k 个周期,但为什么要这样呢?

注意:原来我在阅读代码时没有注意,当我问这个问题时,我忘记了 STA 指令做了什么。以上部分信息不正确。

【问题讨论】:

    标签: assembly initialization reset 6502


    【解决方案1】:

    仔细查看STA (Store Accumulator) instruction。给定

    sta $300,x
    

    它将累加器存储到$300+xx 正在递增..

    因此,在您执行的连续迭代中:

    Accumulator -> $300+00
    Accumulator -> $300+01
    Accumulator -> $300+02
    ...
    Accumulator -> $300+FF
    

    为什么需要这样做:既然需要等待时间,为什么不将内存置于“已知状态”。这有助于消除以后由于错误而导致的不可预测的行为。 例如,在 C 语言中:始终具有值 0(null)的未初始化指针可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2021-08-30
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      相关资源
      最近更新 更多