; iigsGsos.s — GS/OS class-1 dispatch wrappers.
;
; PUSH ORDER MATTERS.  Earlier versions used PHA-then-PEA-0, which put
; the bank byte at offset position in the stack layout - broken under
; real GS/OS 6.0.2 (observed as a JSL $E100A8 hang).  The correct order
; matches ORCA-C's PushLong macro: PEA high FIRST, then PHA low.  After
; PEA 0 + PHA, the 4 bytes at (S+1..S+4) are (off_lo, off_hi, bank, pad)
; in little-endian order, which is what the dispatcher reads as a LONG.
;
; Each wrapper takes a 16-bit pointer to a class-1 parm block in A
; (the C ABI).  The GS/OS convention is:
;   PHA  / PEA 0                  ; push 32-bit parm-block pointer
;                                 ; (low 16 = caller's bank-0 ptr,
;                                 ; high 16 = 0 since parm blocks live
;                                 ; in bank 0)
;   LDX #<call-number>            ; class-1 call number ($20xx)
;   JSL $E100A8                   ; dispatcher
;   <pop the 4 pushed bytes>      ; caller-cleans (CALLER's responsibility)
; The dispatcher returns the call status in A (0 = success, non-zero
; = error code).  The dispatcher clobbers X, Y, P; A holds the status.
;
; CRITICAL: GS/OS does NOT pop the parm-block pointer.  The caller
; must clean up the 4 pushed bytes BEFORE its own RTL — otherwise
; the RTL pops parm-pointer bytes as a return address and the CPU
; jumps into garbage (typically $00:0000 = BRK loop).  See the bug
; that motivated this comment.
;
; Each wrapper:
;   1. PHA + PEA 0   (push 4-byte parm-block long ptr)
;   2. LDX #call#
;   3. JSL $E100A8
;   4. Stash A (status) at DP $E4, slide SP up 4 bytes, restore A
;   5. RTL

	.text
	.globl gsosOpen
	.globl gsosRead
	.globl gsosWrite
	.globl gsosClose
	.globl gsosGetEOF
	.globl gsosSetEOF
	.globl gsosSetMark
	.globl gsosGetMark

gsosOpen:
	pea 0
	pha
	ldx #0x2010
	jsl 0xe100a8
	sta 0xe4         ; stash status (A) in DP scratch
	tsc
	clc
	adc #4
	tcs              ; SP += 4 (pop the long ptr)
	lda 0xe4         ; restore status to A
	rtl

gsosRead:
	pea 0
	pha
	ldx #0x2012
	jsl 0xe100a8
	sta 0xe4
	tsc
	clc
	adc #4
	tcs
	lda 0xe4
	rtl

gsosWrite:
	pea 0
	pha
	ldx #0x2013
	jsl 0xe100a8
	sta 0xe4
	tsc
	clc
	adc #4
	tcs
	lda 0xe4
	rtl

gsosClose:
	pea 0
	pha
	ldx #0x2014
	jsl 0xe100a8
	sta 0xe4
	tsc
	clc
	adc #4
	tcs
	lda 0xe4
	rtl

gsosGetEOF:
	pea 0
	pha
	ldx #0x2019
	jsl 0xe100a8
	sta 0xe4
	tsc
	clc
	adc #4
	tcs
	lda 0xe4
	rtl

gsosSetEOF:
	pea 0
	pha
	ldx #0x2018
	jsl 0xe100a8
	sta 0xe4
	tsc
	clc
	adc #4
	tcs
	lda 0xe4
	rtl

gsosSetMark:
	pea 0
	pha
	ldx #0x2016
	jsl 0xe100a8
	sta 0xe4
	tsc
	clc
	adc #4
	tcs
	lda 0xe4
	rtl

gsosGetMark:
	pea 0
	pha
	ldx #0x2017
	jsl 0xe100a8
	sta 0xe4
	tsc
	clc
	adc #4
	tcs
	lda 0xe4
	rtl
