Skip to main content

Module bios

Module bios 

Source
Expand description

s9vm : pure PC BIOS service model for a real-mode DOS-class guest.

This module implements the software half of the classic firmware surface: it takes the guest register file captured at a BIOS interrupt, performs the requested service on a guest memory image and a synthetic block device, and returns the updated register values. It touches no hardware: the kernel adapter (BiosMemory / BiosDisk) supplies guest memory and disk sectors, and publishes the resulting CF/ZF through the scratch byte.

§Scratch-byte protocol

The real-mode ROM stubs installed by crate::s9vm::biosrom cannot return values in registers by themselves: they OUT to a debug port, the host services the call, and the stub patches the caller’s stacked FLAGS from a single byte at SCRATCH_GPA. At the end of every call, handled or not, this model writes that byte:

bit 0  CF value ([`SCRATCH_CF`])
bit 1  ZF value ([`SCRATCH_ZF`])
other  zero

BiosOutcome::Unknown writes 0x00 (CF=0, ZF=0) and leaves the BDA alone: the guest is expected to have installed its own handler for that vector. On handled calls the two flags are also mirrored into BiosRegisters::flags, for adapters that derive the scratch byte from the returned register file; the scratch byte stays authoritative.

§Services

VectorFunctionBehaviour
05hprint screenaccepted, no-op (CF=0)
08h, 1Chtimer / user tickBiosOutcome::Unknown
10hvideomode, cursor, scroll, characters, string
11hequipmentAX=0x0021
12hmemory sizeAX=640 KiB
13hdiskreset, status, CHS read, params, type, extensions, DAP read
15hsystem88h only; C0h/E8h rejected
16hkeyboardpolling queue, AH=00/01/02
17hprinteraccepted, status 30h
1Ahtimetick count and RTC time

Any other vector is BiosOutcome::Unknown.

§Failure statuses

Every rejected request sets CF=1 and a documented status:

ConstantValueUsed by
STATUS_INVALID_FUNCTION01hvideo/system/keyboard/disk subfunction
DISK_STATUS_WRITE_PROTECTED03hdisk write requests (synthetic disk)
DISK_STATUS_SECTOR_NOT_FOUND04hbad CHS/LBA, backend error, unmapped buffer
DISK_STATUS_MEDIA_NOT_FOUND0Chunsupported drive number
STATUS_NOT_SUPPORTED86hunsupported INT 15h subfunction

§BIOS data area

initialise_bda fills the fields a DOS boot reads (equipment, memory size, EBDA segment, keyboard head/tail, tick count, video mode, columns, cursor, page, CRTC port, rows and character height). After every handled call BiosState::handle republishes the fields it owns: video mode, cursor, tick count and keyboard head/tail.

The keyboard buffer is modelled as a compact alloc::collections::VecDeque rather than a real ring. The BDA therefore publishes a synthetic ring descriptor: head 0, tail 2 * queued bytes capped at the 32-byte buffer. This keeps the “head equals tail when empty” invariant without storing ring indices in the state.

§Disk geometry

FLOPPY_1440 is the canonical 80 x 2 x 18 x 512 geometry (2880 sectors, 1.44 MiB). DiskGeometry::from_sectors returns it for images of at most 2880 sectors and a documented fixed-disk-ish 1024 x 16 x 63 x 512 geometry above that (1,032,192 sectors, about 504 MiB). The geometry only describes CHS decoding; the authoritative sector count is BiosDisk::sectors, and reads past it fail with status 04h.

CHS sectors are one-based, as on the PC. Cylinder bits 9:8 travel in CL bits 7:6 and are reassembled by the model.

§Documented deviations

  • INT 10h AH=03 returns the cursor shape in CX=0x0607; the prompt also said CH=0, but the two cannot both hold, and 0x0607 wins because the shape is the documented return value. CH is 0x06 (shape start line).
  • INT 11h returns AX=0x0021. In that word bit 0 announces the floppy drive and bits 7:6 are 00b, not 01b: the two fields would make 0x0061, so the authoritative literal 0x0021 is used.
  • INT 13h AH=08 returns DL=2 for drive 00h and DL=3 for drive 80h. Only those two drive numbers are accepted (a stricter reading of the classic “below 0x80 means floppy” shorthand); anything else fails with status 0Ch.
  • Text services write the default attribute 07h where the caller does not supply one (AH=0Eh, scrolling blank lines use BH).
  • Guest buffer writes are attempted byte by byte through BiosMemory. A byte the adapter refuses aborts the call with status 04h; bytes already written stay written (the adapter is free to reject a range that crosses the end of mapped memory).
  • INT 17h accepts output but has no parallel port to deliver it to; the bytes are discarded and only the ready status is reported.

Re-exports§

use alloc::collections::VecDeque; 🔒
use alloc::string::String; 🔒
use alloc::vec::Vec; 🔒

Structs§

BiosRegisters
Guest register file visible to one BIOS call.
BiosState
Architectural state of the BIOS service model.
Completion 🔒
Outcome of one service handler: the pair of flags the scratch byte carries.
DiskGeometry
CHS geometry used to decode and report disk addresses.

Enums§

BiosOutcome
Result of dispatching one interrupt vector.

Constants§

BDA_BASE
Base of the BIOS data area.
BDA_CHAR_HEIGHT
BDA field: character height, in scan lines.
BDA_COLUMNS
BDA field: number of text columns.
BDA_CRTC_PORT
BDA field: CRTC I/O port base.
BDA_CURSOR
BDA field: cursor position of page 0 (row << 8 | column).
BDA_EBDA_SEGMENT
BDA field: extended BIOS data area segment.
BDA_EQUIPMENT
BDA field: installed equipment word.
BDA_KEYBOARD_HEAD
BDA field: keyboard buffer head offset.
BDA_KEYBOARD_TAIL
BDA field: keyboard buffer tail offset.
BDA_KEY_BUFFER
Guest-physical base of the BDA keyboard ring entries.
BDA_MEMORY_SIZE
BDA field: conventional memory size, in KiB.
BDA_PAGE
BDA field: active display page.
BDA_ROWS
BDA field: text rows minus one.
BDA_TICK_COUNT
BDA field: timer tick counter, 32 bits.
BDA_VIDEO_MODE
BDA field: current video mode.
DAP_SIZE
Required size of an extended disk address packet.
DEFAULT_ATTRIBUTE
Attribute used by text services that do not take one from the caller.
DEFAULT_CHAR_HEIGHT
Character height, in scan lines, installed in the BDA.
DEFAULT_COLUMNS
Text columns installed in the BDA.
DEFAULT_CRTC_PORT
CRTC I/O port base installed in the BDA.
DEFAULT_PAGE
Active display page installed in the BDA.
DEFAULT_ROWS_MINUS_ONE
Text rows minus one installed in the BDA.
DEFAULT_VIDEO_MODE
Video mode installed by initialise_bda (80x25 colour text).
DISK_STATUS_INVALID_FUNCTION
Disk status: invalid function or invalid address packet.
DISK_STATUS_MEDIA_NOT_FOUND
Disk status: unsupported drive number.
DISK_STATUS_OK
Disk status: success.
DISK_STATUS_SECTOR_NOT_FOUND
Disk status: bad CHS/LBA, backend error, or unmapped guest buffer.
DISK_STATUS_WRITE_PROTECTED
Disk status: the synthetic disk is read-only.
EBDA_SEGMENT
Extended BIOS data area segment written by initialise_bda.
EQUIPMENT_WORD
BDA equipment word: one floppy drive, 80x25 colour (bit 0 and bits 5:4).
EXTENSIONS_SIGNATURE
Signature returned by the disk extensions presence check.
FLAG_CARRY
FLAGS bit 0: carry.
FLAG_ZERO
FLAGS bit 6: zero.
FLOPPY_1440
Floppy geometry installed by BiosState::new: 80 x 2 x 18 x 512.
FLOPPY_DRIVE
Floppy drive number.
FLOPPY_TOTAL_SECTORS
Total number of sectors of a 1.44 MiB floppy.
HARD_DISK_DRIVE
Fixed-disk drive number.
HARD_DISK_GEOMETRY
Fixed-disk-ish geometry for images larger than a floppy: 1024 x 16 x 63 x 512 (about 504 MiB, 1,032,192 sectors).
KEY_BUFFER_BYTES
Size of the BDA-visible keyboard ring, in bytes.
KEY_BUFFER_CAPACITY
Maximum number of keys the model queues.
KEY_BUFFER_FIRST_OFFSET
First slot offset of the BDA keyboard ring.
MEMORY_SIZE_KIB
Conventional memory size reported by initialise_bda and INT 12h.
PRINTER_STATUS
Status reported by the printer service: selected and ready.
SCRATCH_CF
Scratch bit 0: carry flag.
SCRATCH_GPA
Guest-physical address of the byte the ROM stubs read back.
SCRATCH_ZF
Scratch bit 1: zero flag.
SECTOR_SIZE
Size of one disk sector, in bytes.
SPACE
Character written to blank cells.
STATUS_INVALID_FUNCTION
Invalid function or invalid parameter status.
STATUS_NOT_SUPPORTED
Unsupported system function status (INT 15h).
STATUS_OK
Success status (CF=0).
TEXT_COLUMNS
Text columns of the modelled screen.
TEXT_ROWS
Text rows of the modelled screen.
VIDEO_BASE
Guest-physical base of text-mode video memory.
VIDEO_LEN
Size of the video window cleared when a mode is set.

Traits§

BiosDisk
Synthetic block-device access used by INT 13h.
BiosMemory
Guest memory access used by the service model.

Functions§

bcd 🔒
Encodes a binary byte as two packed BCD digits.
drive_kind 🔒
Accepts only the two documented drive numbers.
fail_ah 🔒
Writes status into AH and reports a failure.
initialise_bda
Installs the BIOS data area fields a DOS boot reads.
linear_address
Computes a real-mode linear address from a segment and an offset.
read_cell 🔒
Reads one character/attribute cell, falling back to a blank cell.
read_sectors 🔒
Reads one sector after another into a guest buffer.
read_text
Renders the 80x25 text screen as trimmed lines.
read_u16 🔒
Reads a little-endian u16 from guest memory.
read_u64 🔒
Reads a little-endian u64 from guest memory.
ring_offset 🔒
Decodes a BDA keyboard ring offset.
scroll_region 🔒
Scrolls a rectangular text window up or down.
set_ah 🔒
Replaces AH without touching AL.
verify_sectors 🔒
Checks that count sectors starting at lba exist on the medium.
video_cell 🔒
Guest-physical address of one text cell.
write_cell 🔒
Writes one character/attribute cell when it is inside the text screen.
write_sectors 🔒
Writes count sectors from a guest buffer, one sector at a time.
write_u16 🔒
Writes a little-endian u16 to guest memory.
write_u32 🔒
Writes a little-endian u32 to guest memory.