Fifth - virtual machine opcodes 40 – 47

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 of the 19 VESA pages (hardcoded 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 16384 bytes (16 KB, 4096 dwords) from source address to video memory.
      • Use STOSD to write 32-bit words for efficiency.
    • The source address is incremented by 16 KB 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), which the emulator sets once at startup (there is no separate set_vesa_mode routine).
  • The page count is hardcoded to 19. Total transfer is 19 x 16384 = 311,296 bytes — that is 4,096 bytes PAST the end of a 307,200-byte 640x480x8 image, so the source buffer must be padded accordingly.

2. 41: mouse@

Stack Footprint:

-- dx dy buttons

Description:

Reads mouse motion and button states from the DOS mouse driver (int 33h). Returns three values on the stack:

  • horizontal motion delta,
  • vertical motion delta,
  • button states.
  • Returns:
    • dx (32-bit): Horizontal motion since the last call, in mickeys (signed; negative = moved left).
    • dy (32-bit): Vertical motion since the last call, in mickeys (signed; negative = moved up).
    • 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)

Note: the values are motion DELTAS (read via mouse function 0Bh), not absolute cursor coordinates. Accumulate them to track a cursor position.

Example:

mouse@

After execution, stack contains buttons (top), dy, dx (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, inline pixel data).
  • addr2 (32-bit): Address of the destination image structure (width, height, inline pixel data).
  • 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: Pixel data, stored INLINE — one byte per pixel, row-major, starting immediately after the 8-byte header (this is NOT a 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: Copy direction is chosen for overlap safety: if addr1 > addr2 (source above destination) it copies FORWARDS; otherwise it copies BACKWARDS.
  • Example:

    0x1000  ; Source address
    0x2000  ; Destination address
    100  ; Length
    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 inline pixel data).
  • addr2 (32-bit): Address of the destination image structure (contains width, height, and inline pixel data).
  • 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: Pixel data, stored INLINE — one byte per pixel, row-major, starting immediately after the 8-byte header (this is NOT a 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. Bit 7 (the MSB) of each byte is the leftmost pixel of the row; glyphs are fixed at 8x8 pixels.

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): Address of the destination image STRUCTURE — i.e. width/height header followed by inline pixel data, as used by vidput. NOT a raw framebuffer address: the width field is read as the row stride.
  • 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:

0xFF  ; White foreground
0x00  ; Black background
0x3000  ; Character data address
0x5000  ; Destination image structure
10      ; x
20      ; y
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-10-11 su 10:18

Validate