Trying to understand Horizontal and Vertical interrupts

Discussion of development of software for any "obsolete" computer or video game system.
Post Reply
lion2
Posts: 3
Joined: Thu Dec 19, 2019 5:22 pm

Trying to understand Horizontal and Vertical interrupts

Post by lion2 » Thu Dec 19, 2019 5:30 pm

Hi everyone,

I've been researching how classic systems drew graphics. It lead me to research how a CRT screen works. Something I keep reading is that game code was processed during the H&VBlanks. Was code only processed during that time? Was any code processed when the image was being drawn to the screen?

User avatar
dougeff
Posts: 2750
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: Trying to understand Horizontal and Vertical interrupts

Post by dougeff » Thu Dec 19, 2019 6:09 pm

The NES has a CPU, which processes the code, and a PPU, which draws a picture to the TV.

The PPU is busy while drawing, but resting during H and V blank periods (which are very short).

The CPU can only pass data to the PPU, while it is resting, otherwise it will corrupt the visible image.

The CPU is always active, and always processing / executing code. (Later systems had a "wait for interrupt" instruction to reduce electrical usage and heat generated in the CPU... where the CPU essentially does nothing).
nesdoug.com -- blog/tutorial on programming for the NES

lion2
Posts: 3
Joined: Thu Dec 19, 2019 5:22 pm

Re: Trying to understand Horizontal and Vertical interrupts

Post by lion2 » Thu Dec 19, 2019 6:34 pm

I see! I guess I had it confused with how the Atari 2600 work. From what I understand, programmers manually controlled the scanning directly to the screen, so they had to wait for the blanks to occur to execute game code. Thanks for the clarification!

93143
Posts: 1231
Joined: Fri Jul 04, 2014 9:31 pm

Re: Trying to understand Horizontal and Vertical interrupts

Post by 93143 » Thu Dec 19, 2019 6:46 pm

There are, generally speaking, two kinds of interrupts on the NES and SNES and systems like them (barring peripherals or special chips that could use interrupts for other purposes).

The first and by far the most important is the VBlank interrupt. This (at least on NES and SNES) is accomplished with an NMI, or Non-Maskable Interrupt, which fires at the beginning of VBlank. This causes the code to jump to a routine specially written to handle tasks that must be done during VBlank, like video updates, or must be done once per frame regardless of how overloaded with tasks the CPU is, like the music engine.

The other is a raster interrupt that can be triggered on a specified scanline (V-IRQ), at a specified point on each scanline (H-IRQ), or at a specified point on a specified scanline (HV-IRQ). These can be used for raster effects, such as line scroll or split-screen. The NES does not have this capability natively (although I believe some of its mappers provide it), but the SNES and MD do.

Horizontal blanking is too short to get any meaningful work done, aside from quick writes to scroll or palette registers or suchlike, and even those come with a lot of overhead if you use interrupts for them (HDMA is faster if the hardware has it). Generally the CPU code just runs straight through HBlank without caring. Only VBlank is critical, because that's where the CPU has to update the video chip on how to draw the next frame.

...

The above applies to NES-style systems, where the video chip has enough memory to draw a frame without handholding by the CPU.

In the Atari 2600, the video chip had enough memory to draw half a scanline, or a whole scanline with mirroring. This meant that the CPU had to spend essentially the whole of active display feeding the video chip ("racing the beam", they called it). You could perhaps squeeze in some processing in the display kernel, but most of the game logic had to happen during vertical blanking. It was essentially the opposite of the NES/SNES/MD paradigm.

lion2
Posts: 3
Joined: Thu Dec 19, 2019 5:22 pm

Re: Trying to understand Horizontal and Vertical interrupts

Post by lion2 » Fri Dec 20, 2019 8:15 pm

93143 wrote:
Thu Dec 19, 2019 6:46 pm
There are, generally speaking, two kinds of interrupts on the NES and SNES and systems like them (barring peripherals or special chips that could use interrupts for other purposes)...
Thanks for the info! This is all fascinating for me. I grew up with these systems and only really got to know their specs, not how they work. I might even try coding some graphics in their assembly language. Not planning on creating any games, just draw something to the screen.
Again, thanks for all the info. Now to go buy the Racing the Beam book!

Post Reply