NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/ps
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPSPtr			.BS 2
ZPSessionPtr	.BS 2
ZPArgV			.BS 2
Index			.BS 1
hSession		.BS 1
ZS.END
				.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #0					S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #32					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT		
				.DA	CS.QUIT
L.MSG0			.DA MSG0
L.MSG1			.DA MSG1
L.MSG2			.DA MSG2
L.MSG3			.DA MSG3
L.STATUS		.DA MSG.INIT
				.DA MSG.RUN
				.DA MSG.EVENT
				.DA MSG.QUIT
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			lda #1
				>SYSCALL ArgV
				bcs .10

				>SYSCALL AToI
				bcs .90

				tax
				bne .90

				tya

*				ldx #0

.11				cmp PSs.PID,x	
				beq .12

				inx
				cpx #K.PS.MAX
				bne .11

				lda #E.NSP
*				sec
				rts

.12				lda PSs.hPS,x
				jsr CS.RUN.DUMP
				bcs .9

				lda #0
				sec
				rts

.10				>LDYA L.MSG0
				>SYSCALL PutS
				ldx Index

.1				lda PSs.hPS,x
				beq .7

				jsr CS.RUN.DUMP
				>SLEEP

.7				inc Index
				ldx Index
				cpx #K.PS.MAX
				bne .1

.8				lda #0					tell Kernel that all done ok, but 
				sec						we do not want to stay in memory
.9				rts

.90				lda #E.SYN
				sec
				rts
*--------------------------------------
CS.RUN.DUMP		>SYSCALL GetMemPtr
				>STYA ZPPSPtr

				>PUSHW L.MSG1
				
				ldx Index
				lda PSs.hPS,x
				>PUSHA

				ldy #S.PS.PPID

.1				lda (ZPPSPtr),y
				>PUSHA
				iny
				cpy #S.PS.CPID+1
				bne .1
				
				lda PSs.Stats,x
				>PUSHA					CPU%

				ldy #S.PS.S
				lda (ZPPSPtr),y
				tax

				>PUSHW L.STATUS,x

				jsr CS.RUN.DecodeFlags
				>PUSHEA.G PS.FLAGS

				ldy #S.PS.hSession
				lda (ZPPSPtr),y
				tax
				lda USRs.hSID-1,x
				>SYSCALL GetStkObj
				bcs .9
				
				>STYA ZPSessionPtr
				stx hSession
				
				ldy #S.SESSION.UID
				lda (ZPSessionPtr),y
				>PUSHA
				
				>PUSHBI 10
			
				>SYSCALL PrintF

				lda hSession
				>SYSCALL FreeMem

				jmp CS.RUN.PrintArgs
.9				rts
*--------------------------------------
CS.RUN.DecodeFlags
				lda (ZPPSPtr)			Get flags
				ldy #PS.FLAGS+7
				ldx #7

.2				lsr
				pha
				lda #'-'
				bcc .3

				lda MSG.FLAGS,x

.3				sta (pData),y
				pla
				dey
				dex
				bpl .2	

				rts
*--------------------------------------
CS.RUN.PrintArgs
				ldy #S.PS.hARGV
				lda (ZPPSPtr),y
				beq .8					NO ARGV in PS0

				>SYSCALL GetMemPtr
				>STYA ZPArgV

.1				lda (ZPArgV)
				beq .8

				>PUSHW L.MSG2
				>PUSHW ZPArgV
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9

.2				inc ZPArgV
				bne .3

				inc ZPArgV+1
.3				lda (ZPArgV)
				bne .2

				inc ZPArgV
				bne .1

				inc ZPArgV+1
				bra .1

.8				>LDYA L.MSG3
				>SYSCALL PutS
.9				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			clc
				rts
*--------------------------------------
CS.END			
MSG0			.CZ "\e[?7lhPS pPID  PID cPID CPU% Status Flags    UID Args"
MSG1			.CZ "%3d  %3d  %3d  %3d %3d\% %6s %s %3d"
MSG2			.CS " %s"
MSG3			.DA #0
MSG.FLAGS		.AS "HS???XEN"
MSG.INIT		.AZ "Init"
MSG.RUN			.AZ "Run"
MSG.EVENT		.AZ "Event"
MSG.QUIT		.AZ "Quit"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
PS.FLAGS		.BS 9
DS.END
				.ED
*--------------------------------------
MAN
SAVE usr/src/bin/ps.s
ASM
