Virtual Expo 2024

GAMEBOY EMULATOR

Year Long Project
CompSoc

The Nintendo Game Boy is a handheld video game console released by Nintendo in 1989. It was well known for its compact size, monochrome display, and long battery life, making it highly portable and popular among gamers. It featured a cartridge-based system allowing players to insert game cartridges to play various titles. Its most iconic game is arguably "Tetris," which became synonymous with the Game Boy. The success of the Game Boy led to several iterations and successors, including the Game Boy Color, Game Boy Advance, and Game Boy Advance SP, cementing its legacy as one of the most influential gaming platforms of all time.                                                   The Original Gameboy

The primary objective of this project is to recreate the Gameboy's central processing unit (CPU), memory, graphics processing unit (GPU), sound chip, and other essential components. By achieving this objective, the project aims to provide an accurate emulation of the GameBoy hardware, allowing users to play their favorite games while preserving the nostalgic charm of the original console.


The following is a breakdown of the Gameboy's key components:

1. Central Processing Unit (CPU):

The CPU in the Game Boy is a modified version of the Zilog Z80 processor running at approximately 4.19 MHz. It's an 8-bit processor, meaning it operates on data in 8-bit chunks. The Z80 is a popular choice for embedded systems and low-cost computers due to its simplicity and versatility. It features a set of 16-bit registers: AF, BC, DE, HL, SP - stack pointer, and PC - program counter. The CPU executes instructions fetched from memory, performs arithmetic and logical operations, and controls the flow of program execution.

                                                      Gameboy Architecture

The Game Boy's CPU has various registers serving different purposes.

-- The accumulator (A) is used for arithmetic operations and as a general-purpose register.
-- The program counter (PC) holds the memory address of the next instruction to be executed.
-- The stack pointer (SP) points to the top of the stack, used for managing subroutine calls and interrupts.

-- Other registers include the flags register (F), used to store status information such as carry, zero, negative, and overflow flags.


2. Memory:

The Game Boy has a total of 64 KB of addressable memory space. It features 8 KB of internal RAM (Random Access Memory) used for storing variables, stack space, and other runtime data. Additionally, there's 8 KB of Video RAM (VRAM) dedicated to storing graphics data for display. External to the CPU, the Game Boy cartridges contain ROM (Read-Only Memory) chips ranging in size from 32 KB to 8 MB, which hold the game code and assets.


3. Bus:

The bus is the communication pathway through which data travels between the CPU, memory, and peripherals. The CPU communicates with memory and peripherals by sending addresses and data over the bus. The bus architecture facilitates data transfer and control between different components of the system.


4. Pixel Processing Unit (PPU):

The PPU is a specialized chip designed to handle graphical operations specifically tailored for the Game Boy's display.
It operates independently of the main CPU, allowing it to handle graphics processing while the CPU executes game logic and other tasks.

                                                                   Gameboy Display

The primary functions of the PPU include rendering tiles, sprites, scrolling backgrounds, and managing the display output.

The Game Boy's display consists of a grid of tiles, each measuring 8x8 pixels. The PPU accesses tile data stored in VRAM (Video RAM) to render the screen. Tiles are small graphical elements that can be combined to form backgrounds, characters, and objects within the game world. Developers can create custom tile sets to design unique visuals for their games.

Sprites are movable objects that can be overlaid on top of the background layer. The PPU supports up to 40 sprites per frame, with a maximum of 10 sprites per scanline. Developers can manipulate sprite attributes such as size, priority, and palette selection to create visually rich scenes.

The PPU follows a rendering pipeline to process graphical data and generate the display output. It fetches tile data and attributes from VRAM, performs palette lookups, and composes the final image for each scanline. The PPU synchronizes its operations with the LCD refresh cycle to ensure smooth and flicker-free rendering.

The Game Boy supports four shades of gray, which are mapped to a monochrome palette. Developers can define custom palettes to add color variation and depth to their games. The PPU maps tile data to the appropriate palette entries during rendering, allowing for diverse visual effects.

The PPU generates interrupts to signal the CPU when certain rendering tasks are completed. These interrupts allow the CPU to synchronize with the PPU's rendering cycle and update game state accordingly. Interrupts are also generated when the user presses buttons during gameplay.


5. Audio Processing Unit (APU):

The APU is an integral part of the Game Boy's hardware, providing audio capabilities to complement the visual aspects of gameplay. It consists of four independent sound channels, each with its unique characteristics and capabilities. The APU operates concurrently with the CPU and PPU, allowing developers to create immersive audio experiences in their games.

                                                           Gameboy Audio

The Game Boy's APU features four sound channels:
-- Square Wave Channel 1: Produces a square wave sound with variable frequency and duty cycle.
-- Square Wave Channel 2: Similar to Channel 1 but with additional features such as frequency sweep and envelope control.
-- Wave Channel: Generates complex waveforms using a programmable wave pattern stored in memory.
-- Noise Channel: Generates randomized noise with adjustable frequency and envelope control.
Each channel can be individually configured and controlled to produce a wide range of sounds and effects.

The APU supports envelope and sweep functionality for the square wave channels. Envelope control allows developers to adjust the volume of a sound over time, creating effects such as fade-in, fade-out, and pulsating rhythms. Sweep control modifies the frequency of a sound at a specified rate, creating pitch modulation effects and dynamic changes in tone.

The APU mixes the output of the four sound channels to generate the final audio signal. The APU generates interrupts to signal the CPU when sound playback is complete or when specific events occur. These interrupts allow the CPU to synchronize with the APU's timing and update game state accordingly. Timing precision is crucial for maintaining synchronization between audio and visual elements, ensuring a seamless gaming experience.


Resources:

- Gameboy Pandocs documentation.
- Gameboy development Wiki.
- Tonc, a tutorial on GBA programming by Jasper Vijn
- The Ultimate Game Boy Talk.
Emulation of Nintendo Game Boy
- Why did I spend 1.5 months creating a Gameboy emulator?

METADATA

Report prepared on March 21, 2024, 10:19 p.m. by:

  • Vignaraj Pai [CompSoc]
  • Calvin Dominic Fernandes [CompSoc]
  • Siddharth Bhat [CompSoc]
  • Apoorva Agrawal [CompSoc]
  • Aditya Pandia [CompSoc]
  • Prabhanjan Bolanthur Prabhu [CompSoc]
  • Madhav Dhingra [CompSoc]
  • Tarun Dinesh Joshi [CompSoc]

Report reviewed and approved by None on None.

Check out more projects!