Mouse operation and data packet

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Listen to an AI-generated conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What type of events does a mouse detect?

  • Presses, releases, and movement (correct)
  • Keyboard presses
  • Network connectivity status
  • Screen resolution changes

What is the purpose of the two counters in a mouse?

  • To count the number of clicks
  • To measure movement in the X and Y directions (correct)
  • To store the mouse's serial number
  • To track the time since the last click

How many bytes are in a mouse data packet?

  • 3 (correct)
  • 1
  • 4
  • 2

What is the primary function of the Keyboard Controller (KBC) in the context of mouse operation?

<p>To receive and process mouse events, forwarding them to the system (D)</p>
Signup and view all the answers

In a mouse data packet, what information is contained in the first byte?

<p>States of the middle, right, and left buttons, and overflow flags for X and Y (A)</p>
Signup and view all the answers

What is the purpose of the 'Write Byte to Mouse' command?

<p>To send commands to the mouse through the KBC (A)</p>
Signup and view all the answers

Which KBC port is used to write the actual command or argument byte to the mouse?

<p><code>0x60</code> (B)</p>
Signup and view all the answers

What is the function of the 0xF5 command in mouse operation?

<p>Disables data reporting (in stream mode) (A)</p>
Signup and view all the answers

What is the significance of the interrupt request IRQ12 in mouse operation?

<p>It is triggered by the KBC for each byte received from the mouse (B)</p>
Signup and view all the answers

What is 'stream mode' in the context of mouse operation?

<p>A mode where the mouse continuously sends data packets to the KBC whenever mouse events occur (A)</p>
Signup and view all the answers

How does a program interact with the KBC to interface with the mouse?

<p>By using I/O ports <code>0x60</code> (data) and <code>0x64</code> (status/command) (A)</p>
Signup and view all the answers

In event-driven design, what is the role of an event dispatcher?

<p>To listen for events and ensure the correct event handler is called (A)</p>
Signup and view all the answers

What is the difference between a Mealy machine and a Moore machine in the context of state machines?

<p>Mealy machines depend on both the input event and the process state, while Moore machines depend only on the process state (C)</p>
Signup and view all the answers

Why are function pointers useful in event-driven programming?

<p>They facilitate dynamic linking of events to their respective handlers (B)</p>
Signup and view all the answers

What is the purpose of the Video BIOS Extension (VBE)?

<p>To support higher screen resolutions and more advanced video functionalities (C)</p>
Signup and view all the answers

What is the key difference between text mode and graphic mode in video display?

<p>Text mode uses characters, while graphic mode uses pixels (C)</p>
Signup and view all the answers

In direct-color mode, how is the color of each pixel stored in VRAM?

<p>Using three intensity values: Red, Green, and Blue (RGB) (B)</p>
Signup and view all the answers

What is the role of sys_int86 in Minix 3 when configuring the video card?

<p>It allows user-level programs to make calls to real-mode BIOS (D)</p>
Signup and view all the answers

What is the purpose of vm_map_phys when accessing VRAM?

<p>To map a region of physical memory into the virtual address space of a process (D)</p>
Signup and view all the answers

What is the X Pixmap (XPM) format used for in the context of video display?

<p>For representing pixel colors as strings within a C file (D)</p>
Signup and view all the answers

What determines the speed of animation of sprites?

<p>The number of frames each pixmap is displayed before switching to the next (D)</p>
Signup and view all the answers

What is the key advantage of vector graphics over raster graphics?

<p>Vector graphics can produce smoother, continuous lines (B)</p>
Signup and view all the answers

What is the purpose of Vertical Sync (VSync) in video rendering?

<p>To synchronize buffer swaps with the monitor's refresh rate, preventing tearing (D)</p>
Signup and view all the answers

What is the primary purpose of double buffering in computer graphics?

<p>To prevent tearing by drawing the next frame in a back buffer before copying it to the visible framebuffer (C)</p>
Signup and view all the answers

What is page flipping in the context of video memory management?

<p>Switching the video controller's address pointer between two buffers in VRAM (D)</p>
Signup and view all the answers

In the context of moving sprites, why is resetting pixels important, and what is a drawback of doing it directly in the visible framebuffer?

<p>Resetting pixels removes sprite trails, but it can cause tearing if done directly in the visible framebuffer. (D)</p>
Signup and view all the answers

Triple buffering adds a second back buffer to the rendering pipeline. How does this specifically address limitations encountered even when using VSync with double buffering?

<p>It ensures one buffer is always fully rendered, ready to be displayed as soon as the VSync signal occurs, even if rendering the next frame takes longer than the VSync interval. (D)</p>
Signup and view all the answers

Consider a scenario where a game developer aims to minimize both tearing and input lag. Given the trade-offs between different buffering techniques, which combination would be optimal, and why?

<p>Page flipping combined with VSync, as it offers fast buffer swaps and eliminates tearing by synchronizing with the monitor's refresh rate. (D)</p>
Signup and view all the answers

A developer implements a custom memory manager for sprites in a game, aiming to optimize memory usage and reduce fragmentation. The manager uses a free list and a buddy system. During testing, they observe that, despite sufficient total memory available, new sprite allocations occasionally fail. What is the most likely cause of this issue?

<p>External fragmentation is preventing the allocation of contiguous memory blocks large enough for the new sprites, even though enough total free memory exists. (B)</p>
Signup and view all the answers

Flashcards

Mouse Operation

Detects presses, releases, and movement. Movement uses X and Y counters, reset after communication.

Mouse Communication

The mouse informs the KBC of events by sending a 3-byte data packet via a serial line. Each byte goes to KBC's OUT_BUF; KBC triggers IRQ12.

Data Packet - Byte 1

Contains flags for X/Y overflow, button states (middle, right, left), and MSB of delta X/Y. Bit 3 is always set.

Data Packet - Byte 2

Contains the 8 LSB of the relative displacement on the X axis (delta X). Combined with X MSB from byte 1 forms a 9-bit 2's complement integer.

Signup and view all the flashcards

Data Packet - Byte 3

Contains the 8 LSB of the relative displacement on the Y axis (delta Y). Combined with Y MSB from byte 1 forms a 9-bit 2's complement integer.

Signup and view all the flashcards

Stream Mode

Mouse continuously sends data packets to the KBC whenever mouse events occur.

Signup and view all the flashcards

Remote Mode

Mouse only sends data packets when explicitly requested by the KBC.

Signup and view all the flashcards

Sending Commands to Mouse

Commands are sent to the mouse via the KBC using the 'Write Byte to Mouse' command (0xD4).

Signup and view all the flashcards

Command 0xFF

Resets the mouse.

Signup and view all the flashcards

Command 0xF6

Sets defaults for the default values.

Signup and view all the flashcards

Command 0xF5

Disables data reporting (in stream mode). Must be sent before other commands.

Signup and view all the flashcards

Command 0xF4

Enables data reporting (only in stream mode).

Signup and view all the flashcards

Command 0xF3

Used to define the sample rate of the mouse.

Signup and view all the flashcards

Command 0xF0

Defines Remote Mode.

Signup and view all the flashcards

Command 0xEB

Requests a data packet (in Remote Mode).

Signup and view all the flashcards

Command 0xEA

Defines Stream Mode.

Signup and view all the flashcards

Mouse Acknowledgment

The mouse sends a confirmation byte (ACK, NACK, ERROR) after receiving commands or arguments.

Signup and view all the flashcards

Event Driven Design

The program waits for events and reacts to them.

Signup and view all the flashcards

Events

Signals that something interesting happened (e.g., mouse click, timer, data from network).

Signup and view all the flashcards

Event Handler

Specific code that knows how to handle a specific event.

Signup and view all the flashcards

Dispatchers

Listen for events and ensure the correct event handler is called to respond.

Signup and view all the flashcards

State Machines

Can work with event-driven design. Reaction to an event depends on the current 'mode' or state.

Signup and view all the flashcards

Pointers

Stores the address of a function in memory, allowing functions to be treated as data.

Signup and view all the flashcards

Switch Statement (Events)

Selects the event handler based on the type of event.

Signup and view all the flashcards

Function Pointer Table

An array in which each index corresponds to an event type and stores the pointer to the event handler.

Signup and view all the flashcards

Video Card Components

GPU controls screen hardware, VRAM stores data, BIOS ROM contains firmware.

Signup and view all the flashcards

Text Mode

Matrix/grid of characters

Signup and view all the flashcards

Graphic Mode

Matrix of pixels, where each pixel has a color stored in VRAM.

Signup and view all the flashcards

Direct-color Mode

Color of each pixel is stored directly using RGB values.

Signup and view all the flashcards

Sprite Animation

Arrays of color values

Signup and view all the flashcards

Study Notes

Mouse Operation

  • Detects presses, releases, and movement events.
  • Uses two 9-bit 2's complement counters for X and Y directions, reset after communication.
  • Some touchpads communicate absolute positions
  • Informs the KBC of events by sending a 3-byte data packet via a serial line.
  • Each byte of the packet is placed in the KBC's OUT_BUF.
  • The KBC triggers IRQ12 (which is IRQ4 on PIC 2) for each received byte.

Data Packet Structure

  • Byte 1: Contains overflow flags for X and Y deltas and button states.
    • Includes the MSB of the Y delta.
    • Includes the MSB of the X delta.
    • Indicates middle, right, and left button states (1 if pressed).
    • Bit 3 is always set to 1.
  • Byte 2: Contains the 8 LSB of the X-axis relative displacement (delta X).
    • Combined with the X MSB from Byte 1, forms a 9-bit 2's complement integer.
  • Byte 3: Contains the 8 LSB of the Y-axis relative displacement (delta Y).
    • Combined with the Y MSB from Byte 1, forms a 9-bit 2's complement integer.

Mouse Operation Modes

  • Stream Mode (Default): Continuously sends data packets to the KBC at a fixed rate whenever mouse events occur.
  • Remote Mode: Sends data packets only when explicitly requested by the KBC.

Mouse Commands

  • Sent to the mouse via the KBC using the "Write Byte to Mouse" command (0xD4), written to the KBC's command port (0x64).
  • The byte to be sent (command or argument) is written to the KBC's data port (0x60).
  • 0xFF: Resets the mouse.
  • 0xF6: Sets default values.
  • 0xF5: Disables data reporting (used in stream mode before other commands).
  • 0xF4: Enables data reporting (stream mode only).
  • 0xF3: Sets the sample rate.
  • 0xF0: Sets remote mode.
  • 0xEB: Reads data (requests a data packet in remote mode).
  • 0xEA: Sets stream mode.
  • 0xE9: Requests status (mouse configuration).
  • 0xE8: Sets the resolution.
  • 0xE7/0xE6: Sets the scale (2:1 or 1:1).
  • The mouse sends an acknowledgment byte (0xFA for ACK, 0xFE for NACK, 0xFC for ERROR) in response to most received bytes.
  • The ACK byte is placed in the KBC's OUT_BUF and must be read from port 0x60.
  • For commands that trigger a data response, the mouse sends the response after acknowledging the last byte of the command.
  • The mouse must be first disabled using the command 0xF5 before sending other commands in stream mode after enabling data report.

Mouse Implementation Details

  • Interacting with the mouse requires interfacing with the KBC (i8042) using I/O ports 0x60 (data) and 0x64 (status/command).
  • Mouse interrupts (IRQ12) are subscribed using sys_irqsetpolicy().
    • IRQ_EXCLUSIVE is important to prevent interference from Minix's default mouse handler.
  • The interrupt handler does not need to explicitly check OBF or AUX bits, as the KBC uses different IRQ lines for the keyboard and mouse.
  • Since the mouse data packet has 3 bytes, these bytes must be assembled in the driver.
    • A buffer and counter are used. The 1st byte has bit 3 set, as a hint for synchronization. All 3 bytes must belong to the same package, requiring checking.

Event Driven Design

  • Programs wait for events to occur and react to them, instead of executing top-down.
  • They wait for clients to signal an event and then act.
  • Events: Signals that something interesting has happened, such as mouse clicks, timer triggers, or data arrival from the network.
  • Event handler: Specific code sections that handle each event type.
  • Dispatchers: Manage event handlers by listening for events and calling the correct handler.

State Machines

  • State machines can function with event driven design.
  • The program can be in different "modes". How it responds to an event may change based on the current "mode" or state.
  • Useful when the event handler depends on input event and process state (Mealy machine), or when it depends only on process state (Moore machine).
  • Example states: INIT, DRAW, COMP with events like RDOWN (right button pressed), RUP (right button released).

Event Processing

  • Application Dependent: IH performs more processing and may decide what the event means for the program.
  • Application Independent: IH only gets raw data from the hardware, and other parts of the program decide what to do with it.
  • Device drivers in Minix 3 generally use an application-independent approach, processing interrupt notifications.

Function Pointers

  • Store the address of a function in memory, treating functions as data.
  • Allow assigning functions to variables.
  • Allow storing functions in arrays, structs, or unions.
  • Functions can be passed as arguments to other functions.
  • Functions can be returned from other functions.
  • Example: int (*fp)(int); declares a pointer to a function that takes an int and returns an int.

Function Pointers in Events

  • Useful for linking events to their handlers.
  • Events
    • Switch Statement: Selects the event handler based on event type.
    • Table of Function Pointers: An array where each index corresponds to an event type and stores the pointer to the handler.
  • State Machines: The event handler depends on the current state and the event.
    • Two-Dimensional Table: Indexed by state and event.
  • Menus: Each menu item executes a specific action.
    • Each menu entry stores a pointer to the function.
  • Device Drivers
    • Generic Operations: Defines a common interface for different devices.
    • Flexibility: allows replacing operations to adapt to different hardware

Video Card

  • Interfaces between the computer and the monitor.
  • GPU: Controls the screen hardware and performs rendering, reducing CPU usage.
  • VRAM: Memory storing data to be rendered on the screen.
  • BIOS ROM/Flash: Non-volatile memory containing firmware and code for basic video I/O operations, such as VBE.

x86 Registers

Register Size Parts Typical Use
AL 8 bits Low of AX 8-bit data/operations
AH 8 bits High of AX BIOS functions, MSB operations
AX 16 bits AH + AL Real mode (16-bit)
EAX 32 bits Extends AX Protected/64 bit mode

Video Modes

  • Text Mode: Grid of characters (e.g., 80x25).
  • Graphic Mode: Matrix of pixels, each with a color stored in VRAM.
  • Direct-color mode: the color of each pixel is stored directly in VRAM, using 3 intensity values (Red, Green, Blue - RGB)
    • 8 bits / color / pixel
  • Indexed mode: stores an index of a table (palette) of colors in VRAM. The palette contains the definition (RGB intensities) of each color.
    • 8 bit index -> 256 colors

Memory Model

  • Determines how video memory is organized.
  • Linear Mode: Pixels are stored contiguously in memory in a linear framebuffer.
    • Requires knowing the framebuffer base address (vbe_get_mode_info()), pixel coordinates, and number of bytes used to encode the color.

Video Card Configuration

  • Minix 3 typically starts in text mode.
  • The GPU is configured using standardized interfaces through BIOS and VBE.
    • BIOS services are provided through interrupts (INT xx).
    • VBE (Video BIOS Extension): Supports higher resolutions using INT 0x10, differentiated by the AH register value.
  • Calls to real-mode BIOS can be made from user level in Minix 3 using the sys_int86 kernel call, which temporarily switches from 32-bit protected mode to 16-bit real mode.
  • Arguments for BIOS calls are passed through CPU registers represented by the reg86_t struct.

VRAM Access

  • Images can be drawn on the screen from graphic mode. Obtain the physical memory address of the framebuffer using vbe_get_mode_info() (or implement your own using VBE function 0x01). The vbe_mode_info_t struct contains:
    • Screen dimensions.
    • Color depth.
    • Physical base address of the framebuffer (PhysBasePtr).
  • Mapping the region of physical memory into the process's virtual address space using kernel calls. Process user-level cannot directly access the physical memory, hence sys_privtcl (using SYS_PRIV_ADD_MEM) and vm_map_phys are used for mapping. Returns virtual address (void *video_mem).
  • After mapping VRAM, it is accessed using pointers. The pointer type dictates the number of bytes per pixel.
    • 3 bytes per pixel: Use memcpy or a struct. Pointer arithmetic automatically handles pixel traversal based on the pointer type.
  • reg86_t struct: is the union of anonymous structs that allow the access of IA32 registers: uint32_t *, uint16_t *, uint8_t *

Image Display (XPM)

  • Images represented as a pixmap (array of pixel color values).
  • X Pixmap (XPM): Format where pixel colors are represented by strings in a C file.
  • xpm_load(): Converts XPM to raw pixmap, reads the XPM, then creates a pixel with the specified color. The image (img.bytes) is then copied to VRAM.

Raster Graphics

  • Image stored directly in a framebuffer
  • Simple Hardware
  • Constant time to redraw
  • Images often have jagged edges

Vector Graphics

  • Allow to draw smooth lines
  • Complex Harware
  • Update rate depends on number of paths in the image
  • May flicker if image becomes to large

Sprites

  • Sprites are separate overlay images that do not alter the background with position, dimensions, speed, and a pointer to its pixmap.
  • The pixmap includes a transparent color which should not be copied to the VRAM, allowing background to be visible.
  • A collision can be detected is a non-transparent pixel overlaps a non-background pixel.
  • create_sprite() and destroy_sprite() manages allocated and the deallocation of the sprite.

Moving Sprites

  • Sprite movement is obtained by changing a sprite in an order to make different pixmaps.
  • Pixels have to be redefined to there original starting point so that the Sprite appears to move correctly.

Vertical Sync

  • To solve screen tearing sync screen refreseh swaps during the interval vetween frames to ensure they do not tear.

Double Buffering

  • Draw the back frame, write everything on the background from the back buffer and transfer write the result back to VRAM. This reduces time and is faster, but may cause tearing.

Page Flipping

  • instant background swap to another one to save on performance issues.
Technique Eliminates Tearing? Reduces Trails? Overhead Complexity
Reset of Pixels No Yes Low Low
Double Buffering Partial Yes Moderate Moderate
Page Flipping Partial Yes Low High
VSync + Double Buffer Yes Yes Moderate Moderate
Triple Buffering Yes Yes High High

Animating Sprites

  • Involves changing the image of the sprite over time.
  • Animation speed is measured by the number of frames each image is shown for before changing the next.
  • An animated sprite can be created using create_animSprite(), which takes an array of XPMs as input.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

Mouse Operations Quiz
18 questions
Mouse Genetics Flashcards
20 questions
Mouse-in-Hole Problem and Recursion Concepts
48 questions
Use Quizgecko on...
Browser
Browser