MMC3 IRQs

OriginalBy Allan Blomquist

iNES Mapper #4 (MMC3) provides a convenient way to change the frame of the PPU to achieve the NES game Split Volumes and “special effects”. Instead of usingSprite-0 HitsI'd rather use IRQ than write PPU hardware. It'll get the CPU to do the job within a frame, not waste time talking.Sprite-0 HitsMarks. MMC3 has four repositories, which were set up by MMC3 for IRQ:

$C000 - IRQ counter - Mapper used it to count down to trigger IRQ. (a) Each scanned line, the value of which is minus one; When it's 0, IRQ triggers.

Note: The background and the elves must be activated.$2001(D3、D4) This is how the countdown will take place.

$C001 - IRQ Count Locker - Write the value you want, that is, the value used to count back; I'm not going straight.IRQ计数寄存器. Inline value.

$E000 - IRQ disabled/confirmer - Write any value here, you can disable IRQ triggers, but you can also disable IRQ.IRQ计数器锁存器, and then the value is copied to the actual valueIRQ计数器. Once IRQ has been confirmed once, the interruption is guaranteed.

$E001 - IRQ Launcher - Write any value here, enable IRQ triggers.

Thus, most business games use these repositories in this way:

When you want to set up IRQ (in your NMI/vblank program)

  1. Write$E000To confirm any currently pending interruptions
  2. Align$C000and$C001Write the number of scans you have to wait for
  3. Write again.$E000Inverted by Lock
  4. Write$E001To EnableIRQ计数器

Once your IRQ happens: (in your IRQ program)

  1. Write$E000To confirm IRQ and disableIRQ计数器

Before you play MMC3's IRQ, there's a few things you have to set. First of all,, you set the IRQ vector in the ROM end-cut vector, pointing to the code you want to be called when IRQ happens. IRQ vector position\(FFFE,在NMI向量和RESET向量的右边。当一个IRQ中断触发时,CPU会跳到的16位地址\)FFFE, and the CPU sign is automatically stored and automatically recovered upon return of the IRQ program. But CPU's depositors will not be saved, so you have to ensure that at the end of the IRQ program they will be restored to their original condition (it should start with the AXY register, end with the YXA, in order).

Second What you have to do, using the MC3 IRQ, is to stop the other IRQs that are created on the NES. NES has a frame counter, which by default produces an IRQ break at the end of each frame, very much like the NMI. If you don't disable this, your IRQ code will be more than you want, and you'll ruin your game. Disable IRQ interruption of frame counter, just at the start of your program.$40Write to$4017.

Finally. In order to be able to use IRQ, you have to tell the CPU of NES that you actually confirm them, rather than ignore them, because it's the IRQ that will be activated by default (and that's why you don't have to worry about the occurrence of IRQ in a simple game, which is actually ignored by CPU). You have to clear the CPU's stop sign with the "CLI" command. At some point, if you want to ban them again, use "SEI".

The following is the smallest example of the use of MMC3 IRQ interruptions in nbasic. It uses an IRQ break to change half of the background color on the screen, without using it.Sprite-0-HitsMake sure you're in position.

Note: The timing of MMC3 IRQ is what most simulators do not really do (e.g., an inverse X may actually trigger an IRQ interruption on a scanner X-1 or X+1, depending on the simulator you use). If you use IRQ, please test your game several times in various simulators.

;//nesasm文件头
asm
    .inesprg 1              ;//1个PRG段
    .ineschr 0              ;//0个CHR段
    .inesmir 0              ;//镜像类型0
    .inesmap 4              ;//内存使用mapper 4 (MMC3)
    .org $C000
    .bank 0
endasm

start:

    gosub vwait
    set $2000 $80           ;// 打开NMI
    set $2001 $08           ;// 打开BG使MMC3能够计数

    set $4017 $40           ;// 禁用帧计数器IRQ
asm
    cli                     ;// 启用IRQ处理过程
endasm

gameloop:
    goto gameloop

;// NMI程序
nmi:
    set $2006 $3F
    set $2006 $00           ;// 设置背景色为红色
    set $2007 $06

    set $E000 $01
    set $C000 $78           ;// 设置MMC3让IRQ中断发生在
    set $C001 $78           ;// 120条扫描线之后
    set $E000 $01
    set $E001 $01

    set $2006 $00
    set $2006 $00

    set $2001 $08           ;// 打开BG使MMC3能够计数

    resume

;// IRQ程序
irq:

asm
    pha
    txa
    pha                     ;// 保存寄存器A,X和Y。你必须这么做,因为
    tya                     ;// IRQ中断了你的主线程代码。改变你的
    pha                     ;// 寄存器值,对主线程来说,这是可是
                            ;// 不太好的...
endasm

    set $2001 $00           ;// 强制关闭背景,迫使愚蠢的模拟器意识到我在改变背景色

    set $2006 $3F
    set $2006 $00           ;// 设置背景色为绿色
    set $2007 $0A

    set $E000 $01           ;// 确认IRQ并禁止进一步产生IRQ

    set $2006 $00
    set $2006 $00

asm
    pla
    tay                     ;// 取回你的寄存器值
    pla
    tax
    pla
endasm

    resume


;//等待屏幕刷新
vwait:
    asm
        lda $2002
        bpl vwait           ;//等待回溯开始
    vwait_1:
        lda $2002
        bmi vwait_1         ;//等待回溯结束
    endasm
    return

;//文件脚部
asm
    ;//跳转表指向NMI,Reset和IRQ开始位置
    .bank 1
    .org $fffa
    .dw nmi, start, irq
endasm