	.module shell.c
	.area text
;          ?temp -> -49,x
;          ?temp -> -47,x
;     cmd_length -> -46,x
;            cmd -> -44,x
;           argc -> -43,x
;           argv -> -41,x
;        cursor1 -> -39,x
;              j -> -37,x
;        cursor2 -> -36,x
;        command -> -34,x
;              i -> -1,x
_shell::
	pshx
	tfr s,x
	leas -60,sp
; // shell.c
; 
; #include <912d60.h>
; #include <stdio.h>
; #include <stdlib.h>
; #include <string.h>
; #include <ctype.h>
; #include "kernel.h"
; #include "semlib.h"
; 
; 
; 
; 
; // FUNCTION PROTOTYPES
; void shellcmd_help(char *argv1);
; void shellcmd_tcb(signed char id);
; void shellcmd_rcb(signed char id);
; void shellcmd_rcbPrint(signed char rid);
; void shellcmd_ticks(void);
; void shellcmd_startTask(unsigned char id);
; void shellcmd_stopTask(unsigned char id);
; void shellcmd_freeMemCheck(void);
; void shellcmd_kdb(int onoff);
; void shellcmd_quitShell(void);
; /*
; void shellcmd_setTaskPriority(unsigned char id, unsigned char priority);
; */
; 
; 
; 
; 
; // GLOBAL VARIABLES
; extern unsigned long int system_tick;
; extern unsigned int current;		   	 	// current task id number
; 
; extern char error_msg[8][25];
; extern char error_src[5][18];
; 
; extern char kdb_trace;
; 
; 
; 
; 
; void shell(void) {
; 	
; 	// LOCAL CONSTANTS
; 	#define cmd_size (32+1)
; 
; 	
; 	
; 	// LOCAL ENUMERATIONS
; 	enum command_type {	help = 1,
; 		 			  	tcb, 
; 		 			  	rcb, 
; 						ticks,
; 						start,
; 						stop,
; 						priority,
; 						freemem,
; 						kdb,
; 						quit
; 	};
; 						
; 		
; 						
; 	// LOCAL VARIABLES
; 	char command[cmd_size], i, j, **argv, *cursor1, *cursor2;
; 	int cmd_length, argc;
; 	enum command_type cmd;
; 	
; 	
; 	
; 	
; 	// INITIALIZE SHELL
; 	sem_get(COM1);		  	   	  // get COM1 semaphore
	ldd #3
	jsr _sem_get
; 	set_task_priority(current, 254);
	ldd #254
	std 0,sp
	ldd _current
	clra
	jsr _set_task_priority
; 	INTR_ON();			  		  // enable interrupts
		cli

	lbra L5
L4:
; 	
; 	
; 	
; 	
; 	
; 	// SHELL COMMAND LINE INTERPRETER
; 	while (1) {
; 	
; 		  i = 0;
	clr -1,x
; 		  cursor1 = command;
	leay -34,x
	sty -39,x
; 		  cursor2 = command;
	leay -34,x
	sty -36,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 THE COMMAND on the console
; 		  while (((command[i]=getchar()) != 0xD) && (i<cmd_size)) {
; 		  		putchar(command[i]);
	leay -34,x
	sty -56,x
	ldab -1,x
	clra
	addd -56,x
	xgdy
	ldab 0,y
	clra
	jsr _putchar
; 				
; 				// allow backspace to correct typing mistakes
; 				if (command[i] == 8) {
	leay -34,x
	sty -56,x
	ldab -1,x
	clra
	addd -56,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 -34,x
	sty -56,x
	ldab -1,x
	clra
	addd -56,x
	xgdy
	clr 0,y
; 					i--;
	ldab -1,x
	clra
	subd #1
	stab -1,x
; 					} 
	bra L11
L10:
; 				else
; 				  	i++;
	ldab -1,x
	clra
	addd #1
	stab -1,x
L11:
L8:
	jsr _getchar
	stab -47,x
	leay -34,x
	sty -56,x
	ldab -1,x
	clra
	addd -56,x
	xgdy
	ldab -47,x
	stab 0,y
	ldab -47,x
	cmpb #13
	beq L12
	ldab -1,x
	cmpb #33
	lblo L7
L12:
; 		  		}
; 		  // terminate the string
; 		  command[i] = NULL;
	leay -34,x
	sty -56,x
	ldab -1,x
	clra
	addd -56,x
	xgdy
	clr 0,y
; 		  putchar('\n');
	ldd #10
	jsr _putchar
; 		  
; 		  
; 		  
; 		  // PUT COMMAND LINE INTO ARGV & ARGC to extract cmd/args		  
; 		  // make argc and argv arrays according to the command line input
; 		  i = 0;
	clr -1,x
; 		  argc = 1;
	ldd #1
	std -43,x
	bra L14
L13:
; 		  
; 		  // count command + args: argc
; 		  while (command[i] != NULL) {
; 		  		if (command[i] == ' ') 
	leay -34,x
	sty -56,x
	ldab -1,x
	clra
	addd -56,x
	xgdy
	ldab 0,y
	cmpb #32
	bne L16
; 				   argc++;
	ldd -43,x
	addd #1
	std -43,x
L16:
	ldab -1,x
	clra
	addd #1
	stab -1,x
L14:
	leay -34,x
	sty -56,x
	ldab -1,x
	clra
	addd -56,x
	xgdy
	tst 0,y
	bne L13
; 				i++;
; 		  		}
; 		  
; 		  
; 		  // allocate memory to argv
; 		  if ((argv = malloc(argc)) == NULL) {
	ldd -43,x
	jsr _malloc
	std -52,x
	std -41,x
	ldd -52,x
	bne L18
; 		  	 puts(error_msg[5]);
	ldd #_error_msg+125
	jsr _puts
; 			 exit(1);
	ldd #1
	jsr _exit
; 		  	 }
L18:
; 	 
;  
; 		  // extract the cmd/args
; 		  i = 0;
	clr -1,x
	clr -37,x
	lbra L24
X0:
; 		  for (j=0; j<argc; j++) {
L25:
	ldab -1,x
	clra
	addd #1
	stab -1,x
	ldd -36,x
	addd #1
	std -36,x
L26:
; 		  	  
; 			  // search command for letters/numbers
; 			  while (isgraph(command[i])) {
	leay -34,x
	sty -56,x
	ldab -1,x
	clra
	addd -56,x
	xgdy
	ldab 0,y
	clra
	jsr _isgraph
	cpd #0
	bne L25
; 			  		i++;
; 					cursor2++;
; 			  		}
; 			  
; 
; 			  // found a cmd/arg, so make a string array for it
; 		  	  if ((argv[j] = malloc(cursor2 - cursor1 + 1)) == NULL) {
	ldd -36,x
	subd -39,x
	addd #1
	jsr _malloc
	std -54,x
	ldab -37,x
	clra
	lsld
	addd -41,x
	xgdy
	ldd -54,x
	std 0,y
	ldd -54,x
	bne L28
; 			  	 puts(error_msg[5]);
	ldd #_error_msg+125
	jsr _puts
; 				 exit(1);
	ldd #1
	jsr _exit
; 			  	 }
L28:
	ldd -36,x
	subd -39,x
	addd #1
	std 2,sp
	ldy -39,x
	sty 0,sp
	ldab -37,x
	clra
	lsld
	addd -41,x
	xgdy
	ldd 0,y
	jsr _memmove
	ldab -37,x
	clra
	lsld
	addd -41,x
	xgdy
	ldab -1,x
	clra
	addd 0,y
	xgdy
	clr 0,y
	leay -33,x
	sty -56,x
	ldab -1,x
	clra
	addd -56,x
	std -39,x
	ldy -39,x
	sty -36,x
	ldab -1,x
	clra
	addd #1
	stab -1,x
L22:
	ldab -37,x
	clra
	addd #1
	stab -37,x
L24:
	ldab -37,x
	clra
	cpd -43,x
	lblt L26
; 			  
; 			  // move finding to argv[j] array, and add NULL char
; 			  memmove(argv[j], cursor1, cursor2 - cursor1 + 1);
; 			  argv[j][i] = NULL;
; 			  
; 			  // update cursors
; 		  	  cursor1 = command + i + 1;
; 			  cursor2 = cursor1;
; 			  i++;
; 			   
; 		  	  }  // end for j
; 		  
; 		  
; 		  
; 		  // CLASSIFY THE COMMAND
; 		  if (command[0] == 0xD);	 // CR, so start over
	ldab -34,x
	cmpb #13
	bne L32
	lbra L33
L32:
; 		  else if (strcmp(argv[0], "help") == 0)
	ldd #L36
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L34
; 		  	   cmd = help;
	ldab #1
	stab -44,x
	lbra L35
L34:
; 		  else if (strcmp(argv[0], "tcb") == 0)
	ldd #L39
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L37
; 		  	   cmd = tcb;
	ldab #2
	stab -44,x
	lbra L38
L37:
; 		  else if (strcmp(argv[0], "rcb") == 0)
	ldd #L42
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L40
; 		  	   cmd = rcb;
	ldab #3
	stab -44,x
	lbra L41
L40:
; 		  else if (strcmp(argv[0], "ticks") == 0)
	ldd #L45
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L43
; 		  	   cmd = ticks;
	ldab #4
	stab -44,x
	lbra L44
L43:
; 		  else if (strcmp(argv[0], "start") == 0)
	ldd #L48
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L46
; 		  	   cmd = start;
	ldab #5
	stab -44,x
	lbra L47
L46:
; 		  else if (strcmp(argv[0], "stop") == 0)
	ldd #L51
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L49
; 		  	   cmd = stop;
	ldab #6
	stab -44,x
	lbra L50
L49:
; 		  else if (strcmp(argv[0], "priority") == 0)
	ldd #L54
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L52
; 		  	   cmd = priority;
	ldab #7
	stab -44,x
	bra L53
L52:
; 		  else if (strcmp(argv[0], "freemem") == 0)
	ldd #L57
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L55
; 		  	   cmd = freemem;
	ldab #8
	stab -44,x
	bra L56
L55:
; 		  else if (strcmp(argv[0], "kdb") == 0)
	ldd #L60
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L58
; 			   cmd = kdb;
	ldab #9
	stab -44,x
	bra L59
L58:
; 		  else if (strcmp(argv[0], "quit") == 0)
	ldd #L63
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L61
; 		  	   cmd = quit;
	ldab #10
	stab -44,x
	bra L62
L61:
; 		  else if (strcmp(argv[0], "") == 0) 
	ldd #L66
	std 0,sp
	ldd [-41,x]
	jsr _strcmp
	cpd #0
	bne L64
; 		  	   cmd = 0;
	clr -44,x
	bra L65
L64:
; 		  else {
; 		  	   puts(error_msg[1]); 
	ldd #_error_msg+25
	jsr _puts
; 			   putchar('\n');
	ldd #10
	jsr _putchar
; 			   cmd = 0;
	clr -44,x
; 		  	   }
L65:
L62:
L59:
L56:
L53:
L50:
L47:
L44:
L41:
L38:
L35:
L33:
; 		
; 		
; 		  // CLASSIFY/VALIDATE THE PARAMETERS and DISPATCH the command
; 		  if (cmd) {
	tst -44,x
	lbeq L68
	ldab -44,x
	clra
	std -49,x
	cpd #1
	beq L73
	ldd -49,x
	cpd #2
	lbeq L91
	ldd -49,x
	cpd #3
	lbeq L99
	ldd -49,x
	cpd #4
	lbeq L110
	ldd -49,x
	cpd #5
	lbeq L114
	ldd -49,x
	cpd #6
	lbeq L119
	ldd -49,x
	cpd #7
	lbeq L124
	ldd -49,x
	cpd #8
	lbeq L125
	ldd -49,x
	cpd #9
	lbeq L126
	ldd -49,x
	cpd #10
	lbeq L130
	lbra L71
X1:
; 		  switch (cmd) {
L73:
; 		   		 case help:
; 				 	  if (argc == 1) {
	ldd -43,x
	cpd #1
	bne L74
; 					  	 puts("---HELP---\n");
	ldd #L76
	jsr _puts
; 					  	 puts("<> -- required arguments");
	ldd #L77
	jsr _puts
; 					  	 puts("[] -- optional arguments\n");
	ldd #L78
	jsr _puts
; 					  	 puts("help [cmd] -- help on a specific command");
	ldd #L79
	jsr _puts
; 					  	 puts("tcb [task_id] -- prints task control block");
	ldd #L80
	jsr _puts
; 					  	 puts("rcb [[task_id] | [-b]] -- prints resource control block");
	ldd #L81
	jsr _puts
; 					 	 puts("ticks -- shows system tick");
	ldd #L82
	jsr _puts
; 					  	 puts("start <task_id> -- start a task");
	ldd #L83
	jsr _puts
; 					  	 puts("stop <task_id>  -- stop a task");
	ldd #L84
	jsr _puts
; 					 	 puts("priority <task_id>, <task_priority>  -- change prio");
	ldd #L85
	jsr _puts
; 						 puts("freemem -- get approximate free RAM space");
	ldd #L86
	jsr _puts
; 					 	 puts("kdb [1|0] -- turn kernel debug on/off");
	ldd #L87
	jsr _puts
; 						puts("quit -- quit the shell (carefull!)");
	ldd #L88
	jsr _puts
; 					 	 putchar('\n');
	ldd #10
	jsr _putchar
; 					  	 }
	lbra L71
L74:
; 					  else if (argc == 2) 
	ldd -43,x
	cpd #2
	lbne L71
; 					  	   shellcmd_help(argv[1]);
	ldy -41,x
	ldd 2,y
	jsr _shellcmd_help
; 				  	  break;
	lbra L71
L91:
; 				 case tcb:
; 				 	  if (argc == 1)
	ldd -43,x
	cpd #1
	bne L92
; 				  	  	 shellcmd_tcb(-1);
	ldd #-1
	jsr _shellcmd_tcb
	lbra L71
L92:
; 					  else if (argc == 2)
	ldd -43,x
	cpd #2
	bne L94
; 					  	 shellcmd_tcb(atoi(argv[1]));
	ldy -41,x
	ldd 2,y
	jsr _atoi
	tfr b,d
	jsr _shellcmd_tcb
	lbra L71
L94:
; 					  else if (argc > 2)
	ldd -43,x
	cpd #2
	lble L71
; 					  	   puts(error_msg[1]);
	ldd #_error_msg+25
	jsr _puts
; 				  	  break;
	lbra L71
L99:
; 				 case rcb:
; 				 	  if (argc == 1)		  		 // print entire rcb
	ldd -43,x
	cpd #1
	bne L100
; 					  	 shellcmd_rcb(-1);
	ldd #-1
	jsr _shellcmd_rcb
	lbra L71
L100:
; 					  else if (argc == 2) {
	ldd -43,x
	cpd #2
	bne L102
; 					  	 if (!strcmp(argv[1], "-b")) {	 // print 'busy' only
	ldd #L106
	std 0,sp
	ldy -41,x
	ldd 2,y
	jsr _strcmp
	cpd #0
	bne L104
; 						 	shellcmd_rcb(-2);
	ldd #-2
	jsr _shellcmd_rcb
; 							}
	lbra L71
L104:
; 						 else	
; 					  	 	shellcmd_rcb(atoi(argv[1]));	  // single
	ldy -41,x
	ldd 2,y
	jsr _atoi
	tfr b,d
	jsr _shellcmd_rcb
; 						 } 
	lbra L71
L102:
; 					  else if (argc > 2)
	ldd -43,x
	cpd #2
	lble L71
; 				 	  	 puts(error_msg[1]);
	ldd #_error_msg+25
	jsr _puts
; 				  	  break;
	lbra L71
L110:
; 				 case ticks:
; 				 	  if (argc > 1)
	ldd -43,x
	cpd #1
	ble L111
; 					  	 puts(error_msg[1]);
	ldd #_error_msg+25
	jsr _puts
	lbra L71
L111:
; 					  else
; 				  	  	  shellcmd_ticks();
	jsr _shellcmd_ticks
; 					  break;
	lbra L71
L114:
; 				 case start:
; 				 	  if ((argc != 2) || 
	ldd -43,x
	cpd #2
	bne L117
	ldy -41,x
	ldy 2,y
	ldab 0,y
	clra
	jsr _isdigit
	cpd #0
	bne L115
L117:
; 					  	 (!isdigit(*argv[1])))
; 					  	 puts(error_msg[1]);
	ldd #_error_msg+25
	jsr _puts
	bra L71
L115:
; 					  else
; 				  	  	shellcmd_startTask(atoi(argv[1]));
	ldy -41,x
	ldd 2,y
	jsr _atoi
	clra
	jsr _shellcmd_startTask
; 					  break;
	bra L71
L119:
; 				 case stop:
; 				 	  if ((argc != 2) || 
	ldd -43,x
	cpd #2
	bne L122
	ldy -41,x
	ldy 2,y
	ldab 0,y
	clra
	jsr _isdigit
	cpd #0
	bne L120
L122:
; 					  	 (!isdigit(*argv[1])))
; 					  	 puts(error_msg[1]);
	ldd #_error_msg+25
	jsr _puts
	bra L71
L120:
; 					  else
; 				  	  	shellcmd_stopTask(atoi(argv[1]));
	ldy -41,x
	ldd 2,y
	jsr _atoi
	clra
	jsr _shellcmd_stopTask
; 				 	  break;
	bra L71
L124:
; 				 case priority:
; 				 	  puts(error_msg[0]);
	ldd #_error_msg
	jsr _puts
; 				 	  break;
	bra L71
L125:
; 				 case freemem:
; 				 	  shellcmd_freeMemCheck();
	jsr _shellcmd_freeMemCheck
; 				 	  break;
	bra L71
L126:
; 				 case kdb:
; 					  if (argc < 2)	
	ldd -43,x
	cpd #2
	bge L127
; 						puts(error_msg[1]);
	ldd #_error_msg+25
	jsr _puts
	bra L71
L127:
; 					  else 
; 					  	shellcmd_kdb(atoi(argv[1]));
	ldy -41,x
	ldd 2,y
	jsr _atoi
	jsr _shellcmd_kdb
; 					  break;
	bra L71
L130:
; 				 case quit:
; 				 	  shellcmd_quitShell();
	jsr _shellcmd_quitShell
; 					  return;
	bra L3
X2:
; 				 default:
; 				  	  break;
L71:
; 					  
; 		  		}}	  // end switch/if		
L68:
; 		  
; 		
; 		
; 		  // free the argc and argv array memory
; 		  for (i=0; i<argc; i++)
	clr -1,x
	bra L134
L131:
	ldab -1,x
	clra
	lsld
	addd -41,x
	xgdy
	ldd 0,y
	jsr _free
L132:
	ldab -1,x
	clra
	addd #1
	stab -1,x
L134:
	ldab -1,x
	clra
	cpd -43,x
	blt L131
	ldd -41,x
	jsr _free
	ldd #10
	jsr _putchar
L5:
	lbra L4
X3:
; 		  	  free(argv[i]);
; 		  free(argv);
; 		  
; 		  putchar('\n');
; 		  
; 	}  // end while(1)
; 	
; 	
; 	INTR_OFF();
		sei

; 	sem_give(COM1);
	ldd #3
	jsr _sem_give
; 	
; }	// end shell()
L3:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rts
;          argv1 -> 2,x
_shellcmd_help::
; 
; 
; 
; 
; 
; void shellcmd_help(char *argv1) {
; 
; 	 puts(error_msg[0]);
	ldd #_error_msg
	jsr _puts
; }
L135:
	.dbline 0 ; func end
	rts
;          start -> -2,x
;            max -> -1,x
;            tid -> 3,x
_shellcmd_tcb::
	pshd
	pshx
	tfr s,x
	leas -22,sp
; 
; 
; 
; void shellcmd_tcb(signed char tid) {
; 	
; 	 // Local Variables
; 	 unsigned char max, start;
; 	 
; 	 if (tid >= MAXTASKS) {
	ldab 3,x
	cmpb #8
	blt L137
; 	 	puts(error_msg[2]);
	ldd #_error_msg+50
	jsr _puts
; 		return;
	lbra L136
L137:
; 		}
; 		
; 	 if (tid < 0) {
	tst 3,x
	bge L140
; 	 	start = 0;
	clr -2,x
; 		max = MAXTASKS;
	ldab #8
	stab -1,x
; 		}	
	bra L141
L140:
; 	 else {
; 	 	start = tid;
	ldab 3,x
	tfr b,d
	stab -2,x
; 	 	max = tid + 1;
	ldab 3,x
	tfr b,d
	addd #1
	stab -1,x
; 		}
L141:
; 	 
; 	 puts("\nTask Control Block:");
	ldd #L142
	jsr _puts
	ldab -2,x
	stab 3,x
	bra L146
L143:
; 	 for (tid=start; tid<max; tid++) {
; 	 
; 	 // don't print a big empty tcb
; 	 if (get_task_name(tid) == NULL)
	ldab 3,x
	tfr b,d
	clra
	jsr _get_task_name
	cpd #0
	bne L147
; 	 	continue;
	bra L144
L147:
	ldd #L149
	jsr _puts
	ldab 3,x
	tfr b,d
	clra
	jsr _get_task_name
	std -4,x
	ldab 3,x
	tfr b,d
	clra
	jsr _get_task_state
	std -6,x
	ldab 3,x
	tfr b,d
	clra
	jsr _get_task_priority
	std -8,x
	ldab 3,x
	tfr b,d
	clra
	jsr _get_task_address
	std -10,x
	ldab 3,x
	tfr b,d
	clra
	jsr _get_task_messages
	std 10,sp
	ldy -10,x
	sty 8,sp
	ldy -8,x
	sty 6,sp
	ldy -6,x
	sty 4,sp
	ldab 3,x
	tfr b,d
	std 2,sp
	ldy -4,x
	sty 0,sp
	ldd #L150
	jsr _printf
L144:
	inc 3,x
L146:
	ldab 3,x
	cmpb -1,x
	blo L143
; 		
; 	 puts  ("---------------+-------+----------+-----------+------------+------------+");
; 	 
; 	 //INTR_OFF();
; 	 printf("Name: %8s | ID: %d | State: %d | Prio: %3d | Addr: %4x | Msgs: 0x%2x |\n", 
; 			get_task_name(tid),
; 			tid,
; 	 		get_task_state(tid),
; 			get_task_priority(tid),
; 			get_task_address(tid),
; 			get_task_messages(tid));
; 	 }		
; 	 //INTR_ON();
; 	 
; 	 puts  ("---------------+-------+----------+-----------+------------+------------+");
	ldd #L149
	jsr _puts
; 	 
; 	 puts("\nState: 0=idle, 1=pending, 2=running, 3=waiting, 4=stopped");
	ldd #L151
	jsr _puts
; 	 puts("Message: [ bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | prio | state ]"); 
	ldd #L152
	jsr _puts
; 	 
; }
L136:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
;            rid -> -1,x
;         option -> 3,x
_shellcmd_rcb::
	pshd
	pshx
	tfr s,x
	leas -2,sp
; 
; 
; 
; 
; void shellcmd_rcb(signed char option) {
; 	 
; 	 // Local Variables
; 	 unsigned char rid;
; 	 
; 
; 	 
; 	 // print all	
; 	 if (option == -1) {
	ldab 3,x
	cmpb #-1
	bne L154
	clr -1,x
	bra L159
L156:
	ldab -1,x
	tfr b,d
	jsr _shellcmd_rcbPrint
L157:
	ldab -1,x
	clra
	addd #1
	stab -1,x
L159:
; 		for (rid=0; rid<NUMRESOURCES; rid++) {
	ldab -1,x
	cmpb #4
	blo L156
; 			shellcmd_rcbPrint(rid);
; 			}
; 		}	
	bra L155
L154:
; 	 
; 	 // only print busy resources
; 	 else if (option == -2) {
	ldab 3,x
	cmpb #-2
	bne L160
	clr -1,x
	bra L165
L162:
; 	 	for (rid=0; rid<NUMRESOURCES; rid++) {
; 			if (get_resource_state(rid) == BUSY)
	ldab -1,x
	clra
	jsr _get_resource_state
	cpd #1
	bne L166
; 			   shellcmd_rcbPrint(rid); 
	ldab -1,x
	tfr b,d
	jsr _shellcmd_rcbPrint
L166:
L163:
	ldab -1,x
	clra
	addd #1
	stab -1,x
L165:
	ldab -1,x
	cmpb #4
	blo L162
; 			}
; 		}
	bra L161
L160:
; 		
; 	 // print only one
; 	 else {
; 	 	  if (option >= NUMRESOURCES) {
	ldab 3,x
	cmpb #4
	blt L168
; 	 	  	 puts(error_msg[6]);
	ldd #_error_msg+150
	jsr _puts
; 			 return;
	bra L153
L168:
; 			 }
; 		  else  
; 	 	  	shellcmd_rcbPrint(option);
	ldab 3,x
	tfr b,d
	jsr _shellcmd_rcbPrint
; 
; 		}	
L161:
L155:
; 
; 	 
; 	 puts  ("-----------+----------+------------+------------+");
	ldd #L171
	jsr _puts
; 	 
; 	 puts("\nState: 0=notbusy, 1=busy");
	ldd #L172
	jsr _puts
; 	 puts("Owner: 0..15=task ID, 255=nobody");
	ldd #L173
	jsr _puts
; 
; }
L153:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
;            rid -> 3,x
_shellcmd_rcbPrint::
	pshd
	pshx
	tfr s,x
	leas -12,sp
; 
; 
; 
; void shellcmd_rcbPrint(signed char rid) {
; 
; 	 puts  ("-----------+----------+------------+------------+");
	ldd #L171
	jsr _puts
; 	 
; 	 INTR_OFF();
		sei

; 	 printf("RsrcID: %2d | State: %x | Owner: %3d | Waiting: %d |\n", 
	ldab 3,x
	tfr b,d
	clra
	jsr _get_resource_state
	std -2,x
	ldab 3,x
	tfr b,d
	clra
	jsr _get_resource_owner
	std -4,x
	ldab 3,x
	tfr b,d
	clra
	jsr _get_resource_queuelen
	std 6,sp
	ldy -4,x
	sty 4,sp
	ldy -2,x
	sty 2,sp
	ldab 3,x
	tfr b,d
	std 0,sp
	ldd #L175
	jsr _printf
; 	 		rid,
; 	 		get_resource_state(rid),
; 			get_resource_owner(rid),
; 			get_resource_queuelen(rid));
; 	INTR_ON();
		cli

; 
; }
L174:
	tfr x,s
	pulx
	leas 2,sp
	.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 #L177
	jsr _printf
; 	 INTR_ON();
		cli

; }
L176:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rts
;             id -> 3,x
_shellcmd_startTask::
	pshd
	pshx
	tfr s,x
	leas -2,sp
; 
; 
; 
; 
; void shellcmd_startTask(unsigned char id) {
; 
; 	 INTR_OFF();
		sei

; 	 if (set_task_state(id, PENDING) == 0)
	ldd #1
	std 0,sp
	ldab 3,x
	clra
	jsr _set_task_state
	cpd #0
	bne L179
; 	  	 printf("task %d started\n", id);
	ldab 3,x
	clra
	std 0,sp
	ldd #L181
	jsr _printf
L179:
; 	 INTR_ON();
		cli

; }
L178:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
;             id -> 3,x
_shellcmd_stopTask::
	pshd
	pshx
	tfr s,x
	leas -2,sp
; 
; 
; 
; 
; void shellcmd_stopTask(unsigned char id) {
; 
; 	 if ((set_task_state(id, STOPPED)) == 0)
	ldd #4
	std 0,sp
	ldab 3,x
	clra
	jsr _set_task_state
	cpd #0
	bne L183
; 	  	 printf("task %d stopped\n", id);
	ldab 3,x
	clra
	std 0,sp
	ldd #L185
	jsr _printf
L183:
; 
; }
L182:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
_shellcmd_freeMemCheck::
	pshx
	tfr s,x
	leas -2,sp
; 
; 
; 
; void shellcmd_freeMemCheck(void) {
; 	 
; 
; 	 INTR_OFF();
		sei

; 	 printf("approximate free heap memory: %d bytes\n", get_free_memory());
	jsr _get_free_memory
	std 0,sp
	ldd #L187
	jsr _printf
; 	 INTR_ON();
		cli

; 	 
; }
L186:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rts
;          onoff -> 2,x
_shellcmd_kdb::
	pshd
	pshx
	tfr s,x
; 
; 
; 
; void shellcmd_kdb(int onoff) {
; 
; 
; 	kdb_trace = onoff;
	ldd 2,x
	stab _kdb_trace
; 
; }
L188:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
_shellcmd_quitShell::
; 
; 
; 
; 
; void shellcmd_quitShell(void) {
; 
; 	 puts("exiting shell");
	ldd #L190
	jsr _puts
; 	 puts("\n");
	ldd #L191
	jsr _puts
; 	 shellcmd_stopTask(get_task_id());
	jsr _get_task_id
	clra
	jsr _shellcmd_stopTask
; 
; }
L189:
	.dbline 0 ; func end
	rts
L191:
	.byte 10,0
L190:
	.byte 'e,'x,'i,'t,'i,'n,'g,32,'s,'h,'e,'l,'l,0
L187:
	.byte 'a,'p,'p,'r,'o,'x,'i,'m,'a,'t,'e,32,'f,'r,'e,'e
	.byte 32,'h,'e,'a,'p,32,'m,'e,'m,'o,'r,'y,58,32,37,'d
	.byte 32,'b,'y,'t,'e,'s,10,0
L185:
	.byte 't,'a,'s,'k,32,37,'d,32,'s,'t,'o,'p,'p,'e,'d,10
	.byte 0
L181:
	.byte 't,'a,'s,'k,32,37,'d,32,'s,'t,'a,'r,'t,'e,'d,10
	.byte 0
L177:
	.byte 't,'i,'c,'k,'s,58,32,37,'l,'d,10,0
L175:
	.byte 'R,'s,'r,'c,'I,'D,58,32,37,50,'d,32,124,32,'S,'t
	.byte 'a,'t,'e,58,32,37,'x,32,124,32,'O,'w,'n,'e,'r,58
	.byte 32,37,51,'d,32,124,32,'W,'a,'i,'t,'i,'n,'g,58,32
	.byte 37,'d,32,124,10,0
L173:
	.byte 'O,'w,'n,'e,'r,58,32,48,46,46,49,53,61,'t,'a,'s
	.byte 'k,32,'I,'D,44,32,50,53,53,61,'n,'o,'b,'o,'d,'y
	.byte 0
L172:
	.byte 10,'S,'t,'a,'t,'e,58,32,48,61,'n,'o,'t,'b,'u,'s
	.byte 'y,44,32,49,61,'b,'u,'s,'y,0
L171:
	.byte 45,45,45,45,45,45,45,45,45,45,45,43,45,45,45,45
	.byte 45,45,45,45,45,45,43,45,45,45,45,45,45,45,45,45
	.byte 45,45,45,43,45,45,45,45,45,45,45,45,45,45,45,45
	.byte 43,0
L152:
	.byte 'M,'e,'s,'s,'a,'g,'e,58,32,91,32,'b,'i,'t,55,32
	.byte 124,32,'b,'i,'t,54,32,124,32,'b,'i,'t,53,32,124,32
	.byte 'b,'i,'t,52,32,124,32,'b,'i,'t,51,32,124,32,'b,'i
	.byte 't,50,32,124,32,'p,'r,'i,'o,32,124,32,'s,'t,'a,'t
	.byte 'e,32,93,0
L151:
	.byte 10,'S,'t,'a,'t,'e,58,32,48,61,'i,'d,'l,'e,44,32
	.byte 49,61,'p,'e,'n,'d,'i,'n,'g,44,32,50,61,'r,'u,'n
	.byte 'n,'i,'n,'g,44,32,51,61,'w,'a,'i,'t,'i,'n,'g,44
	.byte 32,52,61,'s,'t,'o,'p,'p,'e,'d,0
L150:
	.byte 'N,'a,'m,'e,58,32,37,56,'s,32,124,32,'I,'D,58,32
	.byte 37,'d,32,124,32,'S,'t,'a,'t,'e,58,32,37,'d,32,124
	.byte 32,'P,'r,'i,'o,58,32,37,51,'d,32,124,32,'A,'d,'d
	.byte 'r,58,32,37,52,'x,32,124,32,'M,'s,'g,'s,58,32,48
	.byte 'x,37,50,'x,32,124,10,0
L149:
	.byte 45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,43
	.byte 45,45,45,45,45,45,45,43,45,45,45,45,45,45,45,45
	.byte 45,45,43,45,45,45,45,45,45,45,45,45,45,45,43,45
	.byte 45,45,45,45,45,45,45,45,45,45,45,43,45,45,45,45
	.byte 45,45,45,45,45,45,45,45,43,0
L142:
	.byte 10,'T,'a,'s,'k,32,'C,'o,'n,'t,'r,'o,'l,32,'B,'l
	.byte 'o,'c,'k,58,0
L106:
	.byte 45,'b,0
L88:
	.byte 'q,'u,'i,'t,32,45,45,32,'q,'u,'i,'t,32,'t,'h,'e
	.byte 32,'s,'h,'e,'l,'l,32,40,'c,'a,'r,'e,'f,'u,'l,'l
	.byte 33,41,0
L87:
	.byte 'k,'d,'b,32,91,49,124,48,93,32,45,45,32,'t,'u,'r
	.byte 'n,32,'k,'e,'r,'n,'e,'l,32,'d,'e,'b,'u,'g,32,'o
	.byte 'n,47,'o,'f,'f,0
L86:
	.byte 'f,'r,'e,'e,'m,'e,'m,32,45,45,32,'g,'e,'t,32,'a
	.byte 'p,'p,'r,'o,'x,'i,'m,'a,'t,'e,32,'f,'r,'e,'e,32
	.byte 'R,'A,'M,32,'s,'p,'a,'c,'e,0
L85:
	.byte 'p,'r,'i,'o,'r,'i,'t,'y,32,60,'t,'a,'s,'k,95,'i
	.byte 'd,62,44,32,60,'t,'a,'s,'k,95,'p,'r,'i,'o,'r,'i
	.byte 't,'y,62,32,32,45,45,32,'c,'h,'a,'n,'g,'e,32,'p
	.byte 'r,'i,'o,0
L84:
	.byte 's,'t,'o,'p,32,60,'t,'a,'s,'k,95,'i,'d,62,32,32
	.byte 45,45,32,'s,'t,'o,'p,32,'a,32,'t,'a,'s,'k,0
L83:
	.byte 's,'t,'a,'r,'t,32,60,'t,'a,'s,'k,95,'i,'d,62,32
	.byte 45,45,32,'s,'t,'a,'r,'t,32,'a,32,'t,'a,'s,'k,0
L82:
	.byte 't,'i,'c,'k,'s,32,45,45,32,'s,'h,'o,'w,'s,32,'s
	.byte 'y,'s,'t,'e,'m,32,'t,'i,'c,'k,0
L81:
	.byte 'r,'c,'b,32,91,91,'t,'a,'s,'k,95,'i,'d,93,32,124
	.byte 32,91,45,'b,93,93,32,45,45,32,'p,'r,'i,'n,'t,'s
	.byte 32,'r,'e,'s,'o,'u,'r,'c,'e,32,'c,'o,'n,'t,'r,'o
	.byte 'l,32,'b,'l,'o,'c,'k,0
L80:
	.byte 't,'c,'b,32,91,'t,'a,'s,'k,95,'i,'d,93,32,45,45
	.byte 32,'p,'r,'i,'n,'t,'s,32,'t,'a,'s,'k,32,'c,'o,'n
	.byte 't,'r,'o,'l,32,'b,'l,'o,'c,'k,0
L79:
	.byte 'h,'e,'l,'p,32,91,'c,'m,'d,93,32,45,45,32,'h,'e
	.byte 'l,'p,32,'o,'n,32,'a,32,'s,'p,'e,'c,'i,'f,'i,'c
	.byte 32,'c,'o,'m,'m,'a,'n,'d,0
L78:
	.byte 91,93,32,45,45,32,'o,'p,'t,'i,'o,'n,'a,'l,32,'a
	.byte 'r,'g,'u,'m,'e,'n,'t,'s,10,0
L77:
	.byte 60,62,32,45,45,32,'r,'e,'q,'u,'i,'r,'e,'d,32,'a
	.byte 'r,'g,'u,'m,'e,'n,'t,'s,0
L76:
	.byte 45,45,45,'H,'E,'L,'P,45,45,45,10,0
L66:
	.byte 0
L63:
	.byte 'q,'u,'i,'t,0
L60:
	.byte 'k,'d,'b,0
L57:
	.byte 'f,'r,'e,'e,'m,'e,'m,0
L54:
	.byte 'p,'r,'i,'o,'r,'i,'t,'y,0
L51:
	.byte 's,'t,'o,'p,0
L48:
	.byte 's,'t,'a,'r,'t,0
L45:
	.byte 't,'i,'c,'k,'s,0
L42:
	.byte 'r,'c,'b,0
L39:
	.byte 't,'c,'b,0
L36:
	.byte 'h,'e,'l,'p,0

