; -----------------------------------------------
; PShell - a runtime environment for    
; sharp pocket computers                 
; file     : stdprogs.asm                   
; author   : puehringer edgar           
; date     : 06.06.2004                 
; version  : 1.0.0                      
; assembler: as61860                    
;                                       
; A collection of pshell standard programs:
;   - shutdown
;   - phell program delete
;   - renew
; -----------------------------------------------

.area pshell (REL)
        
.include "pshell.h"

; --8x------------------------------------------------
; Start of third applikation (Sound)
; ----------------------------------------------------
app3st:
        .dw  app3end-app3st	; size of the programm
.if __PC_HAS_LOWERCASE_CHARS__
        .ascii "Sound"   	; name of the programm
.else
        .ascii "SOUND"   	; name of the programm
.endif
        .db  0x00       	; end of string 
        .db  0x05          	; number of graphic 
                   		; icon bytes
        .dw  0x607c		; icon pattern
	.dw  0x0261
	.db  0x3f


        CALL ps_clrscr
	LIA  <(app315-app3st)
	LIB  >(app315-app3st)
	CALL ps_rel_ab2abs_x
	CALL ps_putstr
	CALL ps_toggle_sound	; does not destroy X
	JRCP app3l2
app311:	IXL
	CPIA 0
	JRNZM app311
app3l2: CALL ps_putstr
	CALL ps_beep
;app3l3:	CALL    ps_kbd_raw
;	JRNCM   app3l3
;app3l4:	CALL    ps_kbd_raw
;	JRCM   app3l4
	LIA 0xff
	CALL ps_sleep
	RTN


app315:	
.if __PC_HAS_LOWERCASE_CHARS__
	.ascii "Sound is "
	.db  0x00
	.ascii "on"
	.db  0x00
	.ascii "off"
	.db  0x00
.else
	.ascii "SOUND IS "
	.db  0x00
	.ascii "ON"
	.db  0x00
	.ascii "OFF"
	.db  0x00
.endif


app3end: 
;         .dw  0x0000          ; trailing 0 to indicate
                              ; the end of the pshell
                              ; program series; this
                              ; may be overwritten by
                              ; the next program in
                              ; memory

; --8x------------------------------------------------
; Start of first applikation (Shutdown)
; ----------------------------------------------------
app1st:
	.dw  app1end-app1st	; size of the programm
.if __PC_HAS_LOWERCASE_CHARS__
	.ascii "Shutdown" 	; name of the programm
.else
	.ascii "SHUTDOWN" 	; name of the programm
.endif
	.db  0x00         	; end of string 
        .db  0x05        	; number of graphic 
                         	; icon bytes
        .dw  0x2208		; icon pattern
	.dw  0x5d08
	.db  0x22
         
        CALL ps_clrscr
	LIA  0xff
	CALL ps_exit

app1end: 
;         .dw  0x0000          ; trailing 0 to indicate
                              ; the end of the pshell
                              ; program series; this
                              ; may be overwritten by
                              ; the next program in
                              ; memory


; --8x------------------------------------------------
; Start of second applikation (Renew)
; ----------------------------------------------------
app2st:
        .dw  app2end-app2st	; size of the programm
.if __PC_HAS_LOWERCASE_CHARS__
        .ascii "Renew"   	; name of the programm
.else
        .ascii "RENEW"   	; name of the programm
.endif
        .db  0x00      		; string 
        .db  0x05      		; number of graphic 
                          	; icon bytes
        .dw  0x207e		; icon pattern
	.dw  0x2101
	.db  0x06

	LIDP BASIC__START_L
	LP   REG_XL
	MVBD

	IX
	ANID 0x00
app200:	IX
	IXL
	LIB 0x00
	LP REG_XL
	ADB
	IXL
	CPIA 0xFF
	JRNZM app200
	
	LIDP BASIC__END_L
	
	LP REG_XL
	EXBD
	RTN		

app2end: 
;         .dw  0x0000          ; trailing 0 to indicate
                              ; the end of the pshell
                              ; program series; this
                              ; may be overwritten by
                              ; the next program in
                              ; memory

; --8x------------------------------------------------
; Start of 4. applikation (Delete)
; ----------------------------------------------------
app4st:
         .dw  app4end-app4st  	; size of the programm
.if __PC_HAS_LOWERCASE_CHARS__
        .ascii "Delete"      	; name of the programm
.else
        .ascii "DELETE"         ; name of the programm
.endif
        .db  0x00       	; string 
        .db  0x05       	; number of graphic 
                        	; icon bytes
        .dw  0x2214		; icon pattern
	.dw  0x0814
	.db  0x22
         
        CALL ps_clrscr
	LIA  <(app415-app4st)
	LIB  >(app415-app4st)
	CALL ps_rel_ab2abs_x
	CALL ps_putstr

	LIDP psm_length		; load number of progs-1
	LDD
	DECA
	PUSH
	CALL ps_addr
	IXL
	IXL
	CALL ps_putstr
	LIA  <(app416-app4st)
	LIB  >(app416-app4st)
	CALL ps_rel_ab2abs_x
	CALL ps_putstr
app413: CALL ps_kbd_raw
	CPIA KEY_N
	JRZP app414
	CPIA KEY_Y
	JRNZM app413

	POP
	CALL ps_addr
	RA
	IX
	STD
	IX
	STD
	CALL psm_init
	RTN

app414:	POP
	RTN


	

	LIA  0x0e
	CALL ps_exit

app415:	
.if __PC_HAS_LOWERCASE_CHARS__
	.ascii 'Delete "'
	.db  0x00
app416:	
	.ascii '" (Y/N)?'
	.db  0x00
.else
	.ascii 'DELETE "'
	.db  0x00
app416:	
	.ascii '" (Y/N)?'
	.db  0x00
.endif

app4end: 
;         .dw  0x0000          ; trailing 0 to indicate
                              ; the end of the pshell
                              ; program series; this
                              ; may be overwritten by
                              ; the next program in
                              ; memory

; --8x------------------------------------------------
; Start of 5. applikation (FDISK)
; ----------------------------------------------------
app5st:
         .dw  app5end-app5st	; size of the programm
         .ascii "FDISK"     	; name of the programm
         .db  0x00        	; string 
         .db  0x05         	; number of graphic 
                           	; icon bytes
        .dw  0x1c3e		; icon pattern
	.dw  0x363e
	.db  0x1c
         
	CALL ps_clrscr
	LIA  <(app517-app5st)
	LIB  >(app517-app5st)
	CALL ps_rel_ab2abs_x
	CALL ps_putstr
        LIA  <(app5buf-app5st)
	LIB  >(app5buf-app5st)
	CALL ps_rel_ab2abs_y
	; Get the address of the double zero after the
	; last program
	LIA  0xff
	CALL ps_addr
	IX			; skip the -1 gap
	LP    REG_XH
	EXAM
	EXAB
	LP    REG_XL
	EXAM
        CALL ps_word2str
        RA
        IYS
        LIA  <(app5buf-app5st)
	LIB  >(app5buf-app5st)
	CALL ps_rel_ab2abs_x
	CALL ps_putstr
.if 	(LCD_ROWS - 1)		; if PC has multiple lines
        RA			; cur_x = 0
        LIDP ps_cur_x
        STD
        LIA 1			; cur_y = 1
        LIDP ps_cur_y
        STD
.else
	CALL ps_kbd_raw
        CALL ps_clrscr
.endif
	LIA  <(app518-app5st)
	LIB  >(app518-app5st)
	CALL ps_rel_ab2abs_x
	CALL ps_putstr

	LIDP BASIC__START_L
	LP  REG_A
	MVBD
	LIDP ps_tmp
	LP  REG_A
	EXBD
	
app509:	LIA 13			; cur_x = 13
        LIDP ps_cur_x
        STD

	LIA  <(app5buf-app5st)
	LIB  >(app5buf-app5st)
	CALL ps_rel_ab2abs_x
	CALL ps_cp_x_to_y
	LIDP ps_tmp
	LP  REG_A
	MVBD
        CALL ps_word2str
        LIA  32
        IYS
        RA
        IYS
	CALL ps_putstr

	
	CALL ps_kbd_raw
	LIDP  ps_tmp+1
	CPIA KEY_DOWN
	JRNZP app502
	LDD
	DECA
	STD
	JRM   app509
app502:	CPIA KEY_UP
	JRNZP app503
	LDD
	INCA
	STD
	JRM   app509
app503:	CPIA KEY_RETURN
	JRNZM app509
	
	; Ask if modification ok
	CALL ps_clrscr
app566:		LIA  <(app516-app5st)
	LIB  >(app516-app5st)
	CALL ps_rel_ab2abs_x
	CALL ps_putstr
app500: CALL ps_kbd_raw
	CPIA KEY_N
	JRZP app501
	CPIA KEY_Y
	JRZP app520
	JRM  app566
	; modify and make backup of basic pointer
app520:	LIDP ps_tmp
	LP  REG_A
	MVBD
	LIDP BASIC__START_L
	LP  REG_A
	EXBD
	; FIXME make backup of basic pointer
app501:	RTN

app515:	
.if __PC_HAS_LOWERCASE_CHARS__
	.ascii " Bytes"
.else
	.ascii " BYTES"
.endif
	.db  0x00
app516:	
.if __PC_HAS_LOWERCASE_CHARS__
	.ascii "Apply settings (Y/N)?"
	.db  0x00
app517:	.ascii "Next Prog  : "
	.db  0x00
app518:	.ascii "Basic Start:"
.else
	.ascii "APPLY SETTINGS (Y/N)?"
	.db  0x00
app517:	.ascii "NEXT PROG  : "
	.db  0x00
app518:	.ascii "BASIC START:"
.endif
	.db  0x00
app5buf:
	.ascii "XXXXXX"
        .db  0x00
	
app5end: 
;	.dw  0x0000      	; trailing 0 to indicate
                          	; the end of the pshell
                           	; program series; this
                           	; may be overwritten by
                           	; the next program in
                          	; memory

; --8x------------------------------------------------
; Start of sixth applikation - Enable/disable
; program mode (only for setup)
; if ps_ci_param == 42, the program only checks if
; it could change the mode
; ----------------------------------------------------
app6st:
        .dw  app6end-app6st	; size of the programm
        .ascii "PROMODE"   	; name of the programm
        .db  0x00       	; end of string 
        .db  0x05          	; number of graphic 
                   		; icon bytes
        .dw  0x1C22		; icon pattern
	.dw  0x7722
	.db  0x00

	LIDP ps_ci_param
	LDD
	CPIA 42
	JRNZP app603
.if __PC_HAS_MODE_BTN__
	LIA 1
.else
	RA
.endif	
	STD
	RTN
	
app603:		
.if __PC_HAS_MODE_BTN__
.error (MEM_LCDA_PRO - MEM_LCDA_RUN)	; assert RUN and PRO are on the
	LIDP MEM_LCDA_PRO		; same byte in RAM
	LDD
	ANIA MEM_LCDM_PRO
	JRNZP app601
				; PRO mode ->RUN mode 
	ANIA MEM_LCDM_PRO
	ORIA MEM_LCDF_RUN
	STD
	JRP  app602
app601:				; RUN mode -> PRO mode
	ANIA MEM_LCDM_RUN
	ORIA MEM_LCDF_PRO
	STD

app602:	LIA 1
.else
	RA
.endif
	LIDP ps_ci_param
	STD
	RTN

app6end: 
;         .dw  0x0000          ; trailing 0 to indicate
                              ; the end of the pshell
                              ; program series; this
                              ; may be overwritten by
                              ; the next program in
                              ; memory

; --8x------------------------------------------------
; Start of seventh application - Hello World
; ----------------------------------------------------
app7st:
	.dw  app7end-app7st	; size of the programm
.if __PC_HAS_LOWERCASE_CHARS__
	.ascii "HelloW" 	; name of the programm
.else
	.ascii "HELLOW" 	; name of the programm
.endif
	.db  0x00         	; end of string 
        .db  0x05        	; number of graphic 
                         	; icon bytes
        .dw  0x1455		; icon pattern
	.dw  0x5555
	.db  0x14
         
        CALL ps_clrscr
	LIA  <(app701-app7st)
	LIB  >(app701-app7st)
	CALL ps_rel_ab2abs_x
	CALL ps_putstr
	LIA 0xff
	CALL ps_sleep
	RTN


app701:	
.if __PC_HAS_LOWERCASE_CHARS__
	.ascii "Hello World!"
	.db  0x00
.else
	.ascii "HELLO WORLD!"
	.db  0x00
.endif

app7end: 
;        .dw  0x0000           ; trailing 0 to indicate
                              ; the end of the pshell
                              ; program series; this
                              ; may be overwritten by
                              ; the next program in
                              ; memory


; --8x------------------------------------------------
; Start of eight application - CPU speed
; ----------------------------------------------------
app8st:
	.dw  app8end-app8st	; size of the programm
.if __PC_HAS_LOWERCASE_CHARS__
	.ascii "CPU Speed" 	; name of the programm
.else
	.ascii "CPU SPEED" 	; name of the programm
.endif
	.db  0x00         	; end of string 
        .db  0x05        	; number of graphic 
                         	; icon bytes
        .dw  0x2a7f		; icon pattern
	.dw  0x7e7f
	.db  0x2a
        
        CALL ps_clrscr
	LIA  <(app810-app8st)
	LIB  >(app810-app8st)
	CALL ps_rel_ab2abs_x
	CALL ps_putstr

	RA
	LP REG_XL
	EXAM
	RA
	LP REG_XH
	EXAM
	LIA <ps_tmp_str
	LP REG_YL
	EXAM
	LIA >ps_tmp_str
	LP REG_YH
	EXAM

app801:	TEST TEST_CNT_512	; wait until 512 ms timer=1
	JRZM app801		;  ==> read a pending timer event

app802: TEST TEST_CNT_512	; wait until 512 ms timer=1
	JRZM app802		;  ==> wait until incomplete timer
				;      period is gone 
	
	; -- start of count loop
app803:	IX			; 6
	DX			; 6
	IX			; 6
	DX			; 6
	IX			; 6
	NOPT			; 3
	NOPT			; 3
	NOPW			; 2
	NOPW			; 2	
	TEST TEST_CNT_512	; 4
	JRZM app803		;_7__
	; -- end of count loop   51
      
	LP REG_XH
	EXAM
	EXAB
	LP REG_XH
	EXAM
        CALL ps_word2str
        RA
        IYS

	LIA <ps_tmp_str
	LP REG_XL
	EXAM
	LIA >ps_tmp_str
	LP REG_XH
	EXAM
	CALL ps_putstr
	CALL ps_kbd_raw
	RTN

app810:	
.if __PC_HAS_LOWERCASE_CHARS__
	.ascii "kHz*10 "
	.db  0x00
.else
	.ascii "KHZ*10 "
	.db  0x00
.endif

app8end: 
        .dw  0x0000           ; trailing 0 to indicate
                              ; the end of the pshell
                              ; program series; this
                              ; may be overwritten by
                              ; the next program in
                              ; memory


         .ascii "Fehler"
