\section{Application Framework and Snake}

The final stage integrated the individual OS components into a simple application framework. Instead of booting into a single test routine, the kernel now presents a terminal menu where the user can choose between the music player and Snake.

\subsection{Menu-Based Control Flow}
The kernel initialization path now sets up the terminal, GDT, IDT, PIT, keyboard interrupt handler, kernel memory, and paging. After that initialization, the kernel repeatedly clears the terminal, asks for an application number, and dispatches to either the music player or Snake.

\begin{listing}[H]
\begin{minted}{c}
while (1) {
    TerminalClear();
    TerminalWriteString("Enter application number (0 for music, 1 for snake): ");
    char input = TerminalGetChar();

    switch (input) {
        case '0':
            PlayMusic();
            break;
        case '1':
            PlayGame();
            break;
    }
}
\end{minted}
\caption{Application menu dispatch in the kernel main loop.}
\end{listing}

This demonstrates that the terminal, keyboard, interrupt, timer, memory, and application code can cooperate through a single kernel control flow.
\clearpage

\subsection{Snake Game State}
Snake uses a dynamically allocated game state. The game state contains the board, snake, food, score, and pseudo-random state. Creating and destroying the game therefore exercises the kernel's \texttt{malloc()} and \texttt{free()} implementation in a more realistic setting than isolated allocation tests.

\begin{listing}[H]
\begin{minted}{c}
struct GameState* CreateGame(void) {
    struct GameState* game = (struct GameState*)malloc(sizeof(struct GameState));
    if (!game) return 0;

    game->snake = (struct Snake*)malloc(sizeof(struct Snake));
    if (!game->snake) {
        free(game);
        return 0;
    }

    game->food = (struct Food*)malloc(sizeof(struct Food));
    if (!game->food) {
        free(game->snake);
        free(game);
        return 0;
    }

    game->score = 0;
    game->rngState = GetCurrentTick();
    return game;
}
\end{minted}
\caption{Snake creates its game objects using the kernel heap allocator.}
\end{listing}
\clearpage

\subsection{Input, Timing, and Feedback}
The Snake loop reads the last key pressed by the keyboard handler, updates the snake direction, moves the snake, checks collisions, redraws the board, and then sleeps using the PIT. The game uses \texttt{GAME\_SPEED\_MS} to control pacing.

Sound is also integrated into the game. Eating food, dying, and winning each trigger short PC speaker effects. This connects the application layer back to the PIT and PC speaker support implemented earlier.

\begin{listing}[H]
\begin{minted}{c}
input = GetLastKeyPressed();
HandleInput(game, input);
tail = MoveSnake(game->snake);

collisionType = CheckCollision(game->snake, game->food);
if (collisionType == FOOD) {
    game->score++;
    PlayFoodSound();
    AddSegment(game->snake, tail.x, tail.y);
    SpawnFood(game);
}

DrawBoard(game);
SleepInterrupt(GAME_SPEED_MS);
\end{minted}
\caption{Main Snake loop combining keyboard input, game state, sound, drawing, and PIT timing.}
\end{listing}
\clearpage

\section{Final State of the Project}

At the end of the documented work, the kernel contains:
\begin{itemize}
  \item early GDT setup and segment register reloading,
  \item IDT, ISR, IRQ, PIC, and keyboard interrupt support,
  \item kernel heap initialization and simple dynamic allocation,
  \item identity-mapped paging for the early kernel address space,
  \item PIT-based timer ticks and sleep functions,
  \item PC speaker sound generation through PIT channel 2,
  \item a menu-driven application flow,
  \item a music player application,
  \item an interactive Snake game using memory allocation, keyboard input, timing, terminal drawing, and sound feedback.
\end{itemize}

The project therefore moved from a minimal booting kernel that printed static text into a small interactive operating-system environment with hardware interrupts, timing, memory management, terminal input, sound, and application-level control flow.

