79354124

Date: 2025-01-14 06:22:02
Score: 2
Natty:
Report link

How this question is answered depends upon whether it means,

  1. "As a beginning Apple II 6502 programmer, how do I access files on the floppy?"
    or
  2. "As an advanced programmer, how can I read/write bytes without using any Disk Operating System?"

@supercat has already given a superlative answer for Question #2, so this answer is aimed at Question #1, for the beginning Apple II assembly language programmer.

Short answer

Call the DOS file manager routines for handling files. (Or see the ProDOS routines, if you prefer).

The file manager routines provide assembly language access to the commands you are already familiar with from BASIC:

OPEN, CLOSE, READ, WRITE, DELETE, CATALOG, LOCK, UNLOCK, RENAME, POSITION, INIT, VERIFY

Longer answer

All the routines are called by JSR $3D6 with the Y and A registers pointing to an 18 byte "parameter list" you've filled in. Here's the quick reference table:

FILE MANAGER PARAMETER LIST (required input for each command)

00 01 02, 03 04 05 06 07 08, 09 0A 0B 0C, 0D 0E, 0F 10, 11
Open 01 Rec. Len. V D S Type Name⁰ RC¹ Work Buff.² T/S List³
Close 02 RC Work Buff. T/S List Data Buff.⁴
Read 03 Sub-code Rec. Num. BOL⁵ BOH⁵ RLL⁶ RLH⁶ Range⁷ RC Work Buff. T/S List Data Buff.
Write 04 Sub-code Rec. Num. BOL BOH RLL RLH Range RC Work Buff. T/S List Data Buff.
Delete 05 V D S Name RC Work Buff. T/S List
Catalog 06 D S RC Work Buff.
Lock 07 V D S Name RC Work Buff. T/S List
Unlock 08 V D S Name RC Work Buff. T/S List
Rename 09 New Name V D S Name RC Work Buff. T/S List
Position 0A Rec. Num. BOL BOH RC Work Buff.
Init 0B DOS Page Num. V D S RC Work Buff.
Verify 0C V D S Name RC Work Buff. T/S List Data Buff.

More details

The easiest way to make a parameter list is to modify DOS's existing parameter list. JSR $3DC, puts the low byte of the address in Y and the high byte in A.

Even more details

The parameter list contains pointers to memory regions which need to be reserved by the programmer by borrowing a "file buffer" from DOS. File buffers are in a linked list with the first one pointed to by the first two bytes of DOS. If the first character of a buffer's filename is a NULL ($00), then the buffer is not in use and you can reserve it by changing that byte. If it is in use, you'll have to follow the link to the next buffer. If the link points to the $0000, then you've hit the end of the list. (By default DOS starts with MAXFILES 3).

The DOS buffers are in the following format, but note that the pointers to them are actually the address of the filename field (offset $22D).

Start End Description Length
$000 $0FF Data sector buffer 256 bytes
$100 $1FF T/S List sector buffer 256 bytes
$200 $22C File manager workarea buffer 45 bytes
$22D $24A File name buffer 30 bytes
$24B $24C Address of file manager workarea buffer 2 bytes
$24D $24E Address of T/S List sector buffer 2 bytes
$24F $250 Address of the data sector buffer 2 bytes
$251 $252 Address of the filename field of the next buffer in the linked list 2 bytes

Example Algorithms from Beneath Apple DOS

  1. Check if DOS is even loaded since it is required before using the file manager routines.
  LDA $3D0    GET VECTOR JMP
  CMP #$4C    IS IT A JUMP?
  BNE NODOS   NO, DOS NOT LOADED
  1. Locate a free DOS buffer
  FBUFF   LDA     $3D2    Locate DOS load point
          STA     $1
          LDY     #0
          STY     $0
  *
  GBUF0   LDA     ($0),Y  Locate next DOS buffer  
          PHA
          INY
          LDA     $(0),Y
          STA     $1
          PLA
          STA     $0
          BNE     GBUF    Got one
          LDA     $1
          BEQ     NBUF    No buffers free
  *
  GBUF    LDY     #0      Get filename
          LDA     $(0),Y
          BEQ     GOTBUF  It's free
          LDY     #36     It's not free
          BNE     GBUF0   Go get next buffer
  *
  GOTBUF  CLC             Indicate: Got a free buffer
          RTS             Return to caller
  NBUF    SEC             Indicate: No free buffers       
          RTS             Return to caller

Further reading

This answer is wholly based on Don Worth and Pieter Lechner's excellent Beneath Apple DOS, published by Quality Software in 1982. All the nitty gritty omitted here, including an example assembly language program that uses the file manager routines can be found in that most worthy tome.

Reasons:
  • Blacklisted phrase (0.5): how can I
  • Blacklisted phrase (1): how do I
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @supercat
  • Starts with a question (0.5): How this
Posted by: hackerb9