Fifth - virtual machine opcodes 40 – 49

Table of Contents

1. 40: vidmap

Stack Footprint

addr --

Copies memory from the specified address to the video memory (VGA or VESA mode). Used for displaying images directly on the screen.

Parameters:

  • addr (32-bit): Source memory address containing the image data to copy to video memory.

1.1. Detailed Operation

  1. Video Memory Setup:
    • Set video memory segment to 0xA000 (VGA standard segment for graphics mode).
    • Initialize VESA-related variables (e.g., gra for page number).
  2. VESA Page Mapping:
    • For each VESA page (up to 19 pages for 640x480x8 mode):
      • Set VESA page using interrupt 0x10 with AX = 0x4F05.
      • The page number is stored in gra.
  3. Memory Copying:
    • For each page:
      • Copy 4096 bytes (4KB) from source address to video memory.
      • Use STOSD to write 32-bit words for efficiency.
    • The source address is incremented by 4KB after each page.
  4. Final Cleanup:
    • Restore original segments and clean up stack.

1.2. Example

Copy a 640x480x8 image (307,200 bytes) from memory at 0x10000 to video memory:

0x10000
vidmap

1.3. Notes

  • The operation assumes the video mode is set to VESA 640x480x8 (mode 0x101).
  • The exact number of pages copied depends on the video mode (19 pages for 640x480x8).
  • Requires VESA BIOS support and proper initialization via set_vesa_mode.

2. 41: mouse@

Stack Footprint:

-- x y buttons

Description:

Reads the current mouse position and button states from the mouse driver. Returns three values on the stack:

  • X-coordinate,
  • Y-coordinate,
  • button states.
  • Returns:
    • x (32-bit): Current X-coordinate of the mouse cursor (0-based).
    • y (32-bit): Current Y-coordinate of the mouse cursor (0-based).
    • buttons (32-bit): Bitmask of button states:
      • Bit 0: Left button (1 = pressed)
      • Bit 1: Right button (1 = pressed)
      • Bit 2: Middle button (1 = pressed)

Example:

mouse@

After execution, stack contains buttons (top), y, x (bottom).

3. 42: vidput - put image1 into image2, at location x, y

Stack Footprint:

addr1 addr2 x y --=

Copies a portion of image1 to image2 at the specified (x, y) coordinates with clipping. Unlike tvidput, there is no transparency support - every pixel is copied regardless of value.

Parameters:

  • addr1 (32-bit): Address of the source image structure (width, height, data address).
  • addr2 (32-bit): Address of the destination image structure (width, height, data address).
  • x (32-bit): X-coordinate in the destination image where the copy starts.
  • y (32-bit): Y-coordinate in the destination image where the copy starts.

Image Structure Format:

Identical to tvidput:

  • Offset 0: Image width (32-bit)
  • Offset 4: Image height (32-bit)
  • Offset 8: Base address of image data (32-bit pointer)

Example:

0x1000  ; Source image structure
0x2000  ; Destination image structure
50      ; x
50      ; y
vidput

4. 43: cmove - copy memory array

  • Stack Effect:
addr1 addr2 len --

Copies len bytes from addr1 to addr2. Handles overlapping memory regions correctly.

  • Notes: If addr1 > addr2, copies backwards to avoid data corruption; otherwise copies forwards.
  • Example:

    100  ; Length
    0x2000  ; Destination address
    0x1000  ; Source address
    cmove
    

5. 44: cfill

  • Stack Effect: byte addr len --
  • Description: Fills len bytes of memory starting at addr with the specified byte value.
  • Notes: Simple byte-by-byte filling operation. Used for initializing memory regions.
  • Example:

    0xFF  ; Fill byte
    0x1000  ; Start address
    100  ; Length
    cfill
    

6. 45: tvidput - put image with transparency support

Stack Footprint:

addr1 addr2 x y --

Copies a portion of image1 to image2 at the specified (x, y) coordinates with transparency support. Pixels in image1 with value 255 (0xFF) are treated as transparent and are not copied to the destination.

Parameters:

  • addr1 (32-bit): Address of the source image structure (contains width, height, and data address).
  • addr2 (32-bit): Address of the destination image structure (contains width, height, and data address).
  • x (32-bit): X-coordinate in the destination image where the copy starts.
  • y (32-bit): Y-coordinate in the destination image where the copy starts.

Image Structure Format:

Each image structure is stored in memory as follows:

  • Offset 0: Image width (32-bit integer)
  • Offset 4: Image height (32-bit integer)
  • Offset 8: Base address of image data (32-bit pointer)

Example:

0x1000  ; Source image structure
0x2000  ; Destination image structure
50      ; x
50      ; y
tvidput

Notes:

  • Transparency is fixed at value 255 (0xFF).
  • The operation handles clipping automatically to ensure the copy stays within destination bounds.
  • Source and destination image structures must be properly formatted.

7. 46: depth

  • Stack Effect: -- depth
  • Description: Pushes the current depth (number of items) on the data stack onto the stack.
  • Notes: Calculated by comparing the data stack pointer to the base address of the stack.
  • Example:

    1 2 3
    depth
    

    After execution, stack contains 3 (depth) followed by 1 2 3.

8. 47: charput - draw text character

Stack Footprint: colorfg colorbg addrsrc addrdest x y --

Draws an 8x8 character from a source memory buffer to a destination image buffer at the specified (x, y) coordinates. Each byte in the source buffer represents one row of the character (8 bits per row), where each bit determines whether to use the foreground or background color for that pixel.

Parameters:

  • colorfg (1 byte): Foreground color value (0-255). Typically a grayscale value where 0 is black and 255 is white.
  • colorbg (1 byte): Background color value (0-255).
  • addrsrc (32-bit): Memory address pointing to 8 bytes of character data. Each byte represents one row of the character (8 bits per row).
  • addrdest (32-bit): Base address of the destination image buffer in memory.
  • x (32-bit): X-coordinate (0-based) where the character's left edge starts.
  • y (32-bit): Y-coordinate (0-based) where the character's top edge starts.

Example:

TODO: Create dedicated page that explains this instruction in depth. Link to this new page from current one.

0xFF  ; White foreground
0x00  ; Black background
0x3000  ; Character data address
0x5000  ; Video memory address
20      ; y
10      ; x
charput

Character data for 'A' (ASCII 65):

0x00, 0x18, 0x24, 0x24, 0x24, 0x3C, 0x24, 0x24
  • Each byte represents a row:
    • Row 0: 0x00 → all pixels black
    • Row 1: 0x18 → bits 3-4 set (binary 00011000)
    • Row 2: 0x24 → bits 2 and 5 set (binary 00100100)
    • Row 3: 0x24 → same as row 2
    • Row 4: 0x24 → same as row 2
    • Row 5: 0x3C → bits 2-5 set (binary 00111100)
    • Row 6: 0x24 → same as row 2
    • Row 7: 0x24 → same as row 2

Created: 2026-02-24 Tue 23:35

Validate