TRS-80 DOS - NEWDOS/80 v2.0 for the Model I - SYS20/SYS Disassembled
Page Customization
Page Index
SYS20/SYS
Other Navigation
SYS20/SYS - BASIC Disk Statement Executor
SYS20/SYS is the primary resident module during BASIC program execution, handling a comprehensive set of disk-related BASIC statements and functions. This module provides the core infrastructure for file operations, memory management, and BASIC statement execution while programs are running.
Primary Functions:
- CMD Statement Processing: Handles CMD"=" assignments and CMD"F" file operations with sophisticated function dispatching
- Memory Management: RAM testing, buffer allocation, stack validation, and workspace initialization
- File Operations: FRE() function, CLOSE/OPEN validation, KILL statement, FILE statement, SYSTEM statement
- String Functions: INKEY$ implementation with comprehensive input character handling
- Error Handling: Integrated error checking and DOS error translation throughout all operations
Module Architecture:
The module is structured around several key functional areas:
- Entry Points (5200H-5202H): Module validation and call dispatcher entry
- CMD"=" Processor (5203H-529AH): Assignment statement handler with memory testing and stack management
- Memory Test Routines (529BH-52E1H): RAM verification using pattern testing (A5H pattern) with comprehensive validation
- CMD"==" Dispatcher (52E2H-5314H): Function table lookup and execution for file-related commands
- FRE() function (5315H-5323H)
- CLOSE validation (5326H-5341H)
- OPEN validation (5342H-535BH)
- KILL statement (535EH-5364H)
- FILE statement (5367H-536DH)
- SYSTEM statement (5370H-537EH)
- String function handlers (LEN, ASC, STR,VAL,LEFT, VAL, LEFT,VAL,LEFT, RIGHT,CHR, CHR ,CHR) (5381H-53B3H)
- File Operations (5383H-55F9H): CLOSE, OPEN validation, KILL, FILE, SYSTEM implementations
- INKEY$ Handler (55FAH-56ADH): Character input processing with quote handling and delimiter recognition
- INPUT# processing (with detailed state machine explanation) (560EH-56ACH)
- Reserved space (58 NOPs) (56AEH-56E7H)
Technical Highlights:
- Self-Modifying Code: Multiple locations used for temporary storage and dynamic code generation
- Stack Management: Sophisticated SP preservation and workspace allocation at 70FCH
- Memory Protection: Comprehensive bounds checking against MEMSIZ (40FDH) and buffer limits
- Pattern-Based Testing: Uses A5H pattern with XOR verification for RAM validation (64 bytes tested)
- Function Dispatch Tables: CMD"F" uses indexed table lookup at 5952H for command routing
Variables:
| Address | Bytes | Purpose |
|---|---|---|
| 5271H-5273H | 3 | Self-modifying: LD HL,0000H instruction - stores calculated buffer size |
| 5272H-5273H | 2 | Storage: Available memory size (calculated workspace) |
| 5275H-5276H | 2 | Storage: Buffer base address |
| 5278H-5279H | 2 | Storage: Original buffer pointer (for restoration) |
| 528FH-5290H | 2 | Self-modifying: LD SP,0000H instruction - stores stack pointer for restoration |
| 5290H-5291H | 2 | Storage: Saved stack pointer value |
| 52C8H-52CAH | 3 | Self-modifying: LD HL,0000H instruction - stores test buffer address |
| 52C9H-52CAH | 2 | Storage: RAM test workspace pointer (64-byte test area) |
| 5716H | 1 | Error flag: Set to 01H when INKEY$ input error detected (used at 568FH) |
| 5952H+ | Variable | CMD"F" function dispatch table: byte count, function string, jump address triples |
| 56AEH-56E7H | 58 | Reserved/padding: NOP instructions for future expansion or alignment |
Key System Variables Used:
| Address | Bytes | Purpose |
|---|---|---|
| 40A7H | 2 | BASIC work pointer - current position in BASIC text being processed |
| 40B1H | 2 | STREND - end of string storage area |
| 40FDH | 2 | MEMSIZ - highest usable memory address (used for bounds checking) |
| 4318H | 1 | System state flag (related to BASIC execution state) |
| 436AH | 1 | I/O flag byte (bit 7 set during operations to prevent recursion) |
Disassembly:
5200H - Module Entry Point
Entry point for SYS20 module. The CP A instruction sets Z flag if A=0 (module validation check), then immediately returns. This is a standard module signature pattern used by NEWDOS/80 to verify module presence.
5202H - CMD"=" Assignment Statement Processor
Handles CMD"=" assignments that evaluate expressions and potentially store results in memory. This sophisticated routine performs memory bounds checking, RAM testing, and workspace management. The routine validates that sufficient memory exists, tests RAM integrity using pattern verification, and manages stack allocation for expression evaluation.
[STANDARD CMD"=" PATH] The following code handles regular CMD"=" assignments (single equals). First, it sets a recursion guard flag, then calls the BASIC expression evaluator to parse and evaluate what comes after the '='.
[MEMORY VALIDATION SEQUENCE] The expression result is valid (type 38H). Now begins an extensive memory management sequence: save current stack, calculate workspace requirements, verify available memory, test RAM integrity, and set up temporary workspace at 70FCH.
[MEMORY ALLOCATION AND COPY] Memory bounds are validated. Now prepare to copy data to the workspace. This involves calculating the copy length, moving data from the buffer to temporary storage at 5271H area, then to final workspace at 70FCH.
5271H - Self-Modifying Code Area / Staging Data
This section contains a mix of executable instructions and data storage used during the CMD"=" memory operations. The instructions here are modified at runtime to hold calculated values (workspace sizes, addresses, pointers). Some instructions are actual code that executes in special circumstances, while others are purely data storage disguised as instructions.
527FH - Cleanup and Stack Restoration
Restoration routine that executes after CMD"=" processing. Restores the original stack pointer, performs final RAM verification, and returns control to BASIC. This ensures the system state is properly restored after the temporary workspace operations.
529AH - RAM Pattern Fill Test
Fills 64 bytes of RAM with the test pattern A5H (10100101 binary). This pattern is chosen because it alternates bits in a way that helps detect stuck bits, addressing problems, or other RAM failures. The pattern is written to the workspace area calculated earlier.
52A4H - RAM Test Verification Routine
Comprehensive RAM test that verifies memory integrity using XOR pattern testing. Tests two regions: the workspace area (64 bytes at 52C9H) and string storage (from 40B1H/STREND up to 7100H). The test XORs existing memory with new data, writes it back, then verifies. This catches most RAM failures including stuck bits, addressing errors, and data corruption.
52C8H - XOR Memory Test Loop
Core XOR-based memory test engine. This routine reads 64-byte blocks from a source (DE), XORs each byte with the corresponding byte at the destination (HL), writes the result back to (HL), and continues until reaching the upper boundary. The XOR test is particularly effective at detecting stuck bits because any bit that can't change from 0→1 or 1→0 will fail the XOR verification.
52E2H - CMD"==" Function Dispatcher
Handles CMD"==" statements which are followed by a function letter (F, S, K, etc.). This dispatcher uses a function table at 5952H to look up and execute the appropriate handler routine. The table format is: byte_count, function_character(s), address_low, address_high, repeated for each function.
[TABLE LOOKUP SEQUENCE] The character is 'F', so use the dispatch table at 5952H. Table format: each entry is [byte_count][function_chars...][addr_low][addr_high]. The code loops through entries, comparing the function string from the BASIC command with each table entry until a match is found.
[SKIP TO NEXT ENTRY] Byte counts didn't match, so skip over this table entry (function name + address) to reach the next entry.
[CHARACTER-BY-CHARACTER COMPARISON] Byte counts matched at 52F6H. Now compare each character of the function name from BASIC with the table entry's function name.
[MATCH FOUND - EXECUTE] All characters matched! DE now points to the low byte of the jump address. Load the address and jump to the handler routine for this function.
5315H - FRE() Function Handler
Returns the amount of free memory available to BASIC programs. This is accessed via CMD"="FRE, and calculates available memory by subtracting the string storage end (STREND) from the stack pointer, then returns this value as a numeric result to BASIC.
5326H - CLOSE Validation Handler
Validates that all files are properly closed before allowing certain operations. Checks the FCB (File Control Block) table to ensure no files have the "open" status bit set. If any files are open, generates an error.
5342H - OPEN Validation Handler
Validates that at least one file is open before allowing certain operations. This is the inverse of the CLOSE validation - it scans the FCB table looking for any file with the "open" status bit set. If no files are open, generates an error.
535EH - KILL Statement Handler
Executes the BASIC KILL statement to delete a file from disk. Validates that no files are open (calls 5326H), then calls the DOS DELETE function via SYS0 entry point 4D06H to perform the actual file deletion.
5367H - FILE Statement Handler
Executes the BASIC FILE statement which displays a disk directory listing. Validates that no files are open, parses any optional filespec, then calls the DOS directory listing function.
5370H - SYSTEM Statement Handler
Executes the BASIC SYSTEM statement which exits BASIC and returns to the DOS command prompt. Validates all files are closed, ensures proper string termination, then calls the DOS system exit routine.
5381H - Additional String Function Handlers
Collection of handlers for various string and numeric functions accessible through CMD"F" operations. These provide bridges between disk BASIC operations and ROM BASIC function implementations.
55FAH - INKEY$ and INPUT# Functions
Keyboard input and file input processing routines. INKEY$ provides non-blocking keyboard reads, while INPUT# handles complex file parsing with quote and delimiter handling.
5621H - INPUT# Character Processing and Helper Routines
Main character processing loop for INPUT# with state machine handling of quotes, delimiters, and special characters. The loop at 5621H-56ACH reads file input character by character, managing a 255-byte buffer and tracking parsing state in Register D.
Due to the complexity and length of the INPUT# character processing loop (5621H-56ACH), this section implements a sophisticated state machine using Register D bit flags to track: bit 0 (skip spaces), bit 1 (inside quotes), bit 2 (space mode), and bit 3 (quote detected). The loop handles CR-LF sequences, comma delimiters, quoted strings with embedded quotes, and proper buffer management with overflow protection.
56AEH - Reserved Space
Module padding providing expansion space for future functionality.