//              PRODOS
//
// I HOPE YOUR INTRODUCTION TO PLASMA
// HAS INSPIRED YOU TO PLAY AROUND WITH
// PROGRAMMING YOUR APPLE II.
//
// THIS NEXT EXAMPLE SHOWS HOW TO CALL
// PRODOS TO DISPLAY A CATALOG. YOU MAY
// WANT TO CONSULT THE PRODOS TECH REF
// TO UNDERSTAND ALL THE PARAMETERS AND
// STRUCTURE OFFSETS.
//

BYTE PERR = 0
BYTE[512] DATABUFF

DEF GETPFX(PATH)
  BYTE[3] PARAMS

  ^PATH    = 0
  PARAMS.0 = 1
  PARAMS:1 = PATH
  PERR = SYSCALL($C7, @PARAMS)
  RETURN PATH
END

DEF OPEN(PATH, IOBUFF)
  BYTE[6] PARAMS

  PARAMS.0 = 3
  PARAMS:1 = PATH
  PARAMS:3 = IOBUFF
  PARAMS.5 = 0
  SYSCALL($C8, @PARAMS)
  RETURN PARAMS.5
END

DEF READ(REFNUM, BUFF, LEN)
  BYTE[8] PARAMS

  PARAMS.0 = 4
  PARAMS.1 = REFNUM
  PARAMS:2 = BUFF
  PARAMS:4 = LEN
  PARAMS:6 = 0
  PERR = SYSCALL($CA, @PARAMS)
  RETURN PARAMS:6
END

DEF CLOSE(REFNUM)
  BYTE[2] PARAMS

  PARAMS.0 = 1
  PARAMS.1 = REFNUM
  PERR = SYSCALL($CC, @PARAMS)
  RETURN PERR
END

DEF CATALOG
  BYTE[64] PATH
  BYTE REFNUM
  BYTE FIRSTBLK
  BYTE ENTRYLEN, ENTRIESBLK
  BYTE I, TYPE, LEN
  WORD ENTRY, FILECNT

  GETPFX(@PATH)
  PUTS(@PATH)
  PUTC($0D)
  REFNUM = OPEN(@PATH, $0800) // SAFE IO BUFFER LOCATION
  IF PERR; RETURN PERR; FIN
  FIRSTBLK = 1 // FIRST BLOCK IS TREATED SPECIAL
  REPEAT
    IF READ(REFNUM, @DATABUFF, 512) == 512
      ENTRY = @DATABUFF.$04
      IF FIRSTBLK
        ENTRYLEN   = DATABUFF.$23
        ENTRIESBLK = DATABUFF.$24
        FILECNT    = DATABUFF:$25
        ENTRY      = ENTRY + ENTRYLEN
      FIN
      FOR I = FIRSTBLK TO ENTRIESBLK
        TYPE = ^ENTRY
        IF TYPE
          LEN = TYPE & $0F
          ^ENTRY = LEN
          PUTS(ENTRY)
          IF ENTRY->$10 == $D0 // IS IT A DIRECTORY?
            PUTC('/')
            LEN = LEN + 1
          ELSIF ENTRY->$10 == $FF // IS IT A SYSTEM FILE?
            PUTC('*')
            LEN = LEN + 1
          FIN
          FOR LEN = 19 - LEN DOWNTO 0
            PUTC(' ')
          NEXT
          FILECNT = FILECNT - 1
        FIN
        ENTRY = ENTRY + ENTRYLEN
      NEXT
      FIRSTBLK = 0 // DONE WITH FIRST BLOCK
    ELSE
      FILECNT = 0
    FIN
  UNTIL FILECNT == 0
  RETURN CLOSE(REFNUM)
END

CATALOG()
DONE
