	.module shell.c
	.area text
;          ?temp -> -37,x
;              j -> -36,x
;            cmd -> -35,x
;        command -> -33,x
;              i -> -1,x
_shell::
	pshx
	tfr s,x
	leas -42,sp
; // shell.c
; 
; 
; #include <912d60.h>
; #include <stdio.h>
; #include <string.h>
; #include "kernel.h"
; #include "semlib.h"
; 
; 
; 
; 
; // FUNCTION PROTOTYPES
; void shellcmd_tcb(void);
; void shellcmd_rcb(void);
; void shellcmd_ticks(void);
; void shellcmd_start_task(unsigned char id);
; 
; /*void shellcmd_stop_task(unsigned char id);
; void shellcmd_set_task_priority(unsigned char id, unsigned char priority);
; void shellcmd_quit_shell(void);*/
; 
; 
; 
; 
; // GLOBAL VARIABLES
; extern unsigned long int system_tick;
; extern unsigned int current;		   	 	// current task id number
; 
; 
; 
; 
; 
; void shell(void) {
; 	
; 	// LOCAL VARIABLES
; 	char command[32], i, j;
; 	int cmd;
; 	
; 	
; 	
; 	// INITIALIZE
; 	sem_get(COM1);		  // get comm port 1 semaphore
	ldd #0
	jsr _sem_get
; 	INTR_ON();			  // enable interrupts
		cli

	lbra L5
L4:
; 	
; 	
; 	
; 	// COMMAND SHELL 
; 	while (1) {
; 	
; 		  i = 0;
	clr -1,x
; 		  
; 		  // put a prompt
; 		  putchar('>'); putchar('>'); putchar(' ');
	ldd #62
	jsr _putchar
	ldd #62
	jsr _putchar
	ldd #32
	jsr _putchar
	bra L8
L7:
; 		  
; 		  
; 		  // get and echo a command
; 		  while (((command[i]=getchar()) != 0xD) && (i<7)) {
; 		  		putchar(command[i]);
	leay -33,x
	sty -40,x
	ldab -1,x
	clra
	addd -40,x
	xgdy
	ldab 0,y
	clra
	jsr _putchar
; 				
; 				if (command[i] == 8) {
	leay -33,x
	sty -40,x
	ldab -1,x
	clra
	addd -40,x
	xgdy
	ldab 0,y
	cmpb #8
	bne L10
; 				    putchar(32);  		 // space
	ldd #32
	jsr _putchar
; 					putchar(8);			 // backspace
	ldd #8
	jsr _putchar
; 					command[i] = '\0';	 // null
	leay -33,x
	sty -40,x
	ldab -1,x
	clra
	addd -40,x
	xgdy
	clr 0,y
; 					i--;
	ldab -1,x
	clra
	subd #1
	stab -1,x
; 				} else
	bra L11
L10:
; 				  	i++;
	ldab -1,x
	clra
	addd #1
	stab -1,x
L11:
L8:
	jsr _getchar
	stab -37,x
	leay -33,x
	sty -40,x
	ldab -1,x
	clra
	addd -40,x
	xgdy
	ldab -37,x
	stab 0,y
	ldab -37,x
	cmpb #13
	beq L12
	ldab -1,x
	cmpb #7
	lblo L7
L12:
; 		  }
; 		  command[i] = '\0';
	leay -33,x
	sty -40,x
	ldab -1,x
	clra
	addd -40,x
	xgdy
	clr 0,y
; 		  putchar('\n');
	ldd #10
	jsr _putchar
; 
; 		  
; 		  
; 		  // determine/execute the command
; 		  if (command[0] == 0xD)	 // CR
	ldab -33,x
	cmpb #13
	bne L13
; 		  	 cmd = 0;
	ldd #0
	std -35,x
	bra L14
L13:
; 		  else if (strcmp(command, "tcb") == 0)
	ldd #L17
	std 0,sp
	leay -33,x
	xgdy
	jsr _strcmp
	cpd #0
	bne L15
; 		  	 shellcmd_tcb();
	jsr _shellcmd_tcb
	bra L16
L15:
; 		  else if (strcmp(command, "ticks") == 0)
	ldd #L20
	std 0,sp
	leay -33,x
	xgdy
	jsr _strcmp
	cpd #0
	bne L18
; 		  	 shellcmd_ticks();
	jsr _shellcmd_ticks
	bra L19
L18:
; 		  else if (strcmp(command, "start") == 0)
	ldd #L23
	std 0,sp
	leay -33,x
	xgdy
	jsr _strcmp
	cpd #0
	bne L21
; 		  	 shellcmd_start_task(2);
	ldd #2
	jsr _shellcmd_start_task
	bra L22
L21:
; 		  else if (strcmp(command, "") == 0);
	ldd #L26
	std 0,sp
	leay -33,x
	xgdy
	jsr _strcmp
	cpd #0
	bne L24
	bra L25
L24:
; 		  else
; 		  	  puts("syntax error"); putchar('\n');
	ldd #L27
	jsr _puts
L25:
L22:
L19:
L16:
L14:
	ldd #10
	jsr _putchar
L5:
	lbra L4
X0:
; 
; 
; 	}  // end while(1)
; 	
; 	INTR_OFF();
		sei

; 	sem_give(COM1);
	ldd #0
	jsr _sem_give
; 	
; }
L3:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rts
;             id -> -2,x
_shellcmd_tcb::
	pshx
	tfr s,x
	leas -10,sp
; 
; 
; 
; 
; void shellcmd_tcb(void) {
	ldd #0
	std -2,x
L29:
	ldd #L33
	jsr _puts
		sei

	ldd -2,x
	clra
	jsr _get_task_state
	std -4,x
	ldd -2,x
	clra
	jsr _get_task_priority
	clra
	std 4,sp
	ldd -4,x
	clra
	std 2,sp
	movw -2,x,0,sp
	ldd #L34
	jsr _printf
L30:
; 
; 	 // LOCAL VARIABLES
; 	 int id;
; 
; 	 
; 	 for (id=0; id<3; id++) {
	ldd -2,x
	addd #1
	std -2,x
	ldd -2,x
	cpd #3
	blt L29
; 	 puts  ("----------+----------+---------+");
; 	 
; 	 /* this takes a while, so treat as a critical section in order
; 	 	to get reliable data  */
; 	 INTR_OFF();
; 	 
; 	 printf("TaskID: %d | State: %d | Prio: %d\n", id,
; 	 				 	  		 	  			   get_task_state(id),
; 												   get_task_priority(id));
; 	 } // end for loop
; 	 
; 	 // finished with critical section
; 	 INTR_ON();
		cli

; 	 
; 	 puts  ("----------+----------+---------+");
	ldd #L33
	jsr _puts
; 	 puts("Idle=0, Pending=1, Running=2, Waiting=3, Finished=4");
	ldd #L35
	jsr _puts
; 	 putchar('\n');
	ldd #10
	jsr _putchar
; 	 
; 	 
; }
L28:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rts
;  lreg1 -> -4,x
;  lreg2 -> -8,x
_shellcmd_ticks::
	pshx
	tfr s,x
	leas -12,sp
; 
; 
; 
; 
; void shellcmd_ticks(void) {
; 	 
; 	 INTR_OFF();
		sei

; 	 printf("Ticks: %ld\n", system_tick);
	ldy #_system_tick
	movw 0,y,-4,x
	movw 2,y,-2,x
	leay 0,sp
	movw -4,x,0,y
	movw -2,x,2,y
	ldd #L37
	jsr _printf
; 	 INTR_ON();
		cli

; 	 putchar('\n');
	ldd #10
	jsr _putchar
; }
L36:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rts
;             id -> 3,x
_shellcmd_start_task::
	pshd
	pshx
	tfr s,x
	leas -2,sp
; 
; 
; 
; 
; void shellcmd_start_task(unsigned char id) {
; 
; 	 INTR_OFF();
		sei

; 	 set_task_state(id, 1);
	ldd #1
	std 0,sp
	ldab 3,x
	clra
	jsr _set_task_state
; 	 INTR_ON();
		cli

; 	 puts("task started");
	ldd #L39
	jsr _puts
; }
L38:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
L39:
	.byte 't,'a,'s,'k,32,'s,'t,'a,'r,'t,'e,'d,0
L37:
	.byte 'T,'i,'c,'k,'s,58,32,37,'l,'d,10,0
L35:
	.byte 'I,'d,'l,'e,61,48,44,32,'P,'e,'n,'d,'i,'n,'g,61
	.byte 49,44,32,'R,'u,'n,'n,'i,'n,'g,61,50,44,32,'W,'a
	.byte 'i,'t,'i,'n,'g,61,51,44,32,'F,'i,'n,'i,'s,'h,'e
	.byte 'd,61,52,0
L34:
	.byte 'T,'a,'s,'k,'I,'D,58,32,37,'d,32,124,32,'S,'t,'a
	.byte 't,'e,58,32,37,'d,32,124,32,'P,'r,'i,'o,58,32,37
	.byte 'd,10,0
L33:
	.byte 45,45,45,45,45,45,45,45,45,45,43,45,45,45,45,45
	.byte 45,45,45,45,45,43,45,45,45,45,45,45,45,45,45,43
	.byte 0
L27:
	.byte 's,'y,'n,'t,'a,'x,32,'e,'r,'r,'o,'r,0
L26:
	.byte 0
L23:
	.byte 's,'t,'a,'r,'t,0
L20:
	.byte 't,'i,'c,'k,'s,0
L17:
	.byte 't,'c,'b,0

