*******************************************************************************
*                                                                             *
*	SMC  --  Control program for steppingmotor controller  RB 8904        *
*                                                                             *
*******************************************************************************
*                                                                             *
*       (C) Copyright 1989, 1991, 1994 by Rijksuniversiteit Leiden            *
*                                                                             *
*                                                                             *
*	Assembler        : X6805                                              *
*	Hardware system  : PC                                                 *
*       Operating system : MS-DOS                                             *
*                                                                             *
*       Version : 0.23                                                        *
*       Date    : 18 Feb 1994                                                 *
*       Disk    : SMC                                                         *
*                                                                             *
*******************************************************************************

*******************************************************************************
*                                                                             *
*	Versions history:                                                     *
*                                                                             *
*       SMC  V 0.23 -- 18/02/94                                               *
*       Set time per step validation changed (tps<1 then tps=1)               *
*                                                                             *
*       SMC  V 0.22 -- 18/02/94                                               *
*       Initialize STS_PRT to $FF in subroutine INICOM                        *
*                                                                             *
*       SMC  V 0.21 -- 17/02/94                                               *
*       Set time per step validation changed (tps >= 1 is OK)                 *
*                                                                             *
*       SMC  V 0.20 -- 15/02/94                                               *
*       Calibration on startup removed, Steps Reverse/Forward = 0             *
*                                                                             *
*       SMC  V 0.10 -- 21/06/91                                               *
*       Sweep mode added                                                      *
*									      *
*       SMC  V 0.00 --  3/11/89                                               *
*       Setup of program                                                      *
*                                                                             *
*******************************************************************************

	SUBTITLE [ smc.description ]		Description
	PAGE
*******************************************************************************
*                                                                             *
*	Description                                                           *
*       -----------                                                           *
*                                                                             *
*       This is the control program for the Rulbus dual steppingmotor         *
*       controller card RB 8904.                                              *
*                                                                             *
*       The system consists of a MC68705R3 microcomputer, a Rulbus interface, *
*       two steppingmotor controllers SAA 1027 and four end-stop photo-inter- *
*       ruptors.  The Rulbus interface consists of an adress decoder PAL, a   *
*       databus-buffer, a register used as command-register and a buffer used *
*       to interface the status-register (port of microcomputer) to the data- *
*       bus.                                                                  *
*                                                                             *
*       The motorposition can be controlled via ASCII-coded commands:         *
*       - CLx       : calibrate motor x position,                             *
*       - GPx       : get current position of motor x,                        *
*       - SPx hhhhhh: set destination position of motor x to hhhhhh,          *
*	- GSx       : get time per step of motor x,                           *
*       - SSx 0000hh: set time per step of motor x to hh (0..255 ms),         *
*	- GTx       : get steps per trigger of motor x,                       *
*       - STx hhhhhh: set steps per trigger of motor x to hhhhhh,             *
*       - GIx       : get interrupt status of motor x,                        *
*       - EIx       : enable trigger interrupt of motor x,                    *
*       - DIx       : disable trigger interrupt of motor x.                   *
*   |   - GRx       : get sweep range of motor x                              *
*   |   - SRx hhhhhh: set sweep range of motor x to hhhhhh,                   *
*   |   - GWx       : get sweep status of motor x,                            *
*   |   - EWx       : enable sweep of motor x,                                *
*   |   - DWx       : disable sweep interrupt of motor x.                     *
*                                                                             *
*       When the destination position is approached by reverse movement,      *
*       CMxSRF extra Steps Reverse/Forward are made to eliminate play of      *
*       the spindle.                                                          *
*                                                                             *
*                                                                             *
*       Communication protocol                                                *
*       ----------------------                                                *
*                                                                             *
*       The Rulbus processor - steppingmotor controller is a Master/Slave     *
*       combination.  The master controls the Rulbus/controller communi-      *
*       cation.                                                               *
*                                                                             *
*       Character communication:                                              *
*       The controller reads the command-port and processes the ASCII-        *
*	character or control code as follows:                                 *
*       - COMGET: this is a request sent to the controller to output a        *
*	          character to the status-port; the character is supplied     *
*	          by the command scanner process via the output mailbox,      *
*       - COMRDY: this character is used to synchronize the character         *
*	          communication; it is echoed to the status-port only,        *
*       - other : the character is echoed to the status-port and put in the   *
*                 input mailbox to be processed further by the command-       *
*          	  scanner.                                                    *
*                                                                             *
*       Command communicaton:                                                 *
*    	The control commands sent are short strings with optional argument    *
*       and terminated by the end-of-text character COMEOT.  If a result must *
*       be returned, the processor driving Rulbus can obtain the result by    *
*       repeatedly issuing a COMGET character, reading the status-port and    *
*	issuing a COMRDY character.                                           *
*       Arguments and results consist of six hexadecimal characters.          *
*       Like commandlines, results are terminated with the COMEOT character.  *
*                                                                             *
*******************************************************************************

	PAGE
*******************************************************************************
*                                                                             *
*       Command description                                                   *
*       -------------------                                                   *
*                                                                             *
*                                                                             *
*       Format                                                                *
*                                                                             *
*        means: letters between '<' and '>' are transmitted/received, *
*        means: string of six hexadecimal characters,                 *
*        motor x means: motor 1 or motor 2.                                   *
*                                                                             *
*       Command communication is case sensitive.                              *
*       Capital letters must be used for hexadecimal numbers.                 *
*                                                                             *
*                                                                             *
*       CLx -- Calibrate motor x                                              *
*                                                                             *
*       When the  command is given, the absolute position, destination   *
*   |   (and the sweep range and enable flag) variables are set to zero and   *
*       the motor is turning reverse until the begin-stop is reached.         *
*       There is no return value.                                             *
*                                                                             *
*                                                                             *
*       GPx -- Get current motor position for motor x                         *
*                                                                             *
*       When the  command is given, the current position of motor x      *
*       is returned as , representing 0..2^24 -1.                     *
*                                                                             *
*                                                                             *
*       SPx -- Set destination position for motor x                           *
*                                                                             *
*       When the  command is given, the destination position      *
*	of motor x is set to hhhhhh (0..2^24 -1).  There is no return value.  *
*                                                                             *
*                                                                             *
*       GSx -- Get time per step for motor x                                  *
*                                                                             *
*       When the  command is given, the current time per step of         *
*       motor x is returned as <0000hh>, representing 0..255 [ms/step].       *
*                                                                             *
*                                                                             *
*       SSx -- Set time per step for motor x                                  *
*                                                                             *
*       When the  command is given, the time per step for         *
*       motor x is set to hh (0..255) [ms/step].  Setting the time per        *
*       step to zero (0) causes the time per step to be the smallest          *
*       possible for the motor as specified in the program by the             *
*       constant CMxSPS (Steps Per Second).  There is no return value.        *
*                                                                             *
*                                                                             *
*       GTx -- Get steps per trigger for motor x                              *
*                                                                             *
*       When the  command is given, the number of steps per trigger      *
*       pulse is returned as , representing 0..2^24 -1.               *
*                                                                             *
*                                                                             *
*       STx -- Set steps per trigger for motor x                              *
*                                                                             *
*       When the  command is given, the number of steps per       *
*       trigger is set to hhhhhh (0..2^24 -1).  There is no return value.     *
*                                                                             *
*                                                                             *
*       GIx -- Get interrupt status for motor x                               *
*                                                                             *
*       When the  command is given, the current status of the interrupt  *
*       flag of motor x is returned as <000000> or <000001>, representing     *
*       interrupt flag clear / interrupt flag set respectively.  Reading the  *
*       status clears the flag if it was set.                                 *
*                                                                             *
*                                                                             *
*       EIx -- Enable Rulbus interrupt                                        *
*                                                                             *
*       When the  command is given, the Rulbus interrupt on the trigger  *
*       pulse for motor x is enabled.  The command also clears the interrupt  *
*       flag.  By default theinterrupt is disabled.  There is no return value.*
*                                                                             *
*******************************************************************************
	PAGE
*******************************************************************************
*                                                                             *
*       DIx -- Disable Rulbus interrupt                                       *
*                                                                             *
*       When the  command is given, the the Rulbus interrupt on the      *
*       trigger pulse for motor x is disabled.  The command also clears the   *
*       interrupt flag.  By default the interrupt is disabled.  There is no   *
*       return value.                                                         *
*                                                                             *
*                                                                             *
*   |   GRx -- Get steps per sweep (range) for motor x                        *
*   |                                                                         *
*   |   When the  command is given, the current steps per sweep for      *
*   |   motor x is returned as , representing 0..2^23 -1 steps.       *
*   |                                                                         *
*   |                                                                         *
*   |   SRx -- Set steps per sweep (range) for motor x                        *
*   |                                                                         *
*   |   When the  command is given, the steps per sweep for       *
*   |   motor x is set to hhhhhh (0..2^23 -1). There is no return value.      *
*   |                                                                         *
*   |                                                                         *
*   |   GWx -- Get sweep status for motor x                                   *
*   |                                                                         *
*   |   When the  command is given, the current status of the sweep      *
*   |   flag of motor x is returned as <000000> or <000001>, representing     *
*   |   sweep flag clear / sweep flag set respectively.                       *
*   |                                                                         *
*   |                                                                         *
*   |   EWx -- Enable sweep for motor x                                       *
*   |                                                                         *
*   |   When the  command is given, the sweep for motor x is enabled.    *
*   |   By default the sweep is disabled.  There is no return value.          *
*   |                                                                         *
*   |                                                                         *
*   |   DWx -- Disable sweep for motor x                                      *
*   |                                                                         *
*   |   When the  command is given, the sweep for motor x is disabled.   *
*   |   By default the sweep is disabled.  There is no return value.          *
*                                                                             *
*******************************************************************************

	SUBTITLE [ smc.sys ]				System Guide
	PAGE
*******************************************************************************
*
*
*	System guide
*	============
*
*
*	The following items can be specified on source level:
*
*	- motor number of steps per revolution  		       (CMxSPR)
*	  ( currently not used )
*	- motor number of steps per second      		       (CMxSPS)
*	- number of extra steps reverse/forward after reverse movement (CMxSRF)
*
*******************************************************************************

	SUBTITLE [ smc.usr ]				User Guide
*	PAGE
*******************************************************************************
*
*
*	User guide
*	==========
*
*
*	Rulbus software driver  ( Rulbus.lib )
*
*	For the Stepping Motor Controller there is a Rulbus software driver
*	supporting the available SMC commands. The declaration of the driver-
*	routines is as follows:
*
*	int  open_smc(void);
*	int  smc_cal(int devnum, int mtr);
*	int  smc_getpos(int devnum, int mtr, unsigned long *pos);
*	int  smc_setpos(int devnum, int mtr, unsigned long  pos);
*	int  smc_gettps(int devnum, int mtr, int *tps);
*	int  smc_settps(int devnum, int mtr, int  tps);
*	int  smc_getspt(int devnum, int mtr, unsigned long *spt);
*	int  smc_setspt(int devnum, int mtr, unsigned long  spt);
*	int  smc_gi(int devnum, int mtr, int *istat);
*	int  smc_ei(int devnum, int mtr);
*	int  smc_di(int devnum, int mtr);
*   |   int  smc_getsps(int devnum, int mtr, unsigned long *sps);
*   |   int  smc_setsps(int devnum, int mtr, unsigned long  sps);
*   |   int  smc_getswp(int devnum, int mtr, int *istat);
*   |   int  smc_enaswp(int devnum, int mtr);
*   |   int  smc_disswp(int devnum, int mtr);
*
*	For further detail, see the Rulbus Guide.
*
*******************************************************************************

	SUBTITLE [ smc.spec ]				System specification
	PAGE
*******************************************************************************
*
*
*	System specification
*	====================
*	
*
*	Inputs
*	------
*	
*	o Command-register					( CMD_PRT )
*	  - 8 bit input
*	
*	o End-stops motor 1					( PE1_PRT )
*	  - 1 bit reverse end-stop				( PE1_MIN )
*	  - 1 bit forward end-stop				( PE1_MAX )
*	
*	o End-stops motor 2					( PE2_PRT )
*	  - 1 bit reverse end-stop				( PE2_MIN )
*	  - 1 bit forward end-stop				( PE2_MAX )
*	
*	
*	Outputs
*	-------
*	
*	o Status-register					( STS_PRT )
*	  - 8 bit output
*	
*	o 4-phase steppingmotor controller 1 (SAA 1027)		( PM1_PRT )
*	  - 1 bit forward/reverse				( PM1_DIR )
*	  - 1 bit step clocksignal				( PM1_CLK )
*	
*	o 4-phase steppingmotor controller 2 (SAA 1027)		( PM2_PRT )
*	  - 1 bit forward/reverse				( PM2_DIR )
*	  - 1 bit step clocksignal				( PM2_CLK )
*	
*	o Trigger output Motor 1				( PT1_PRT )
*	  - 1 bit trigger output (trigger/steps)		( PT1_TRG )
*
*	o Trigger output Motor 2				( PT2_PRT )
*	  - 1 bit trigger output (trigger/steps)		( PT2_TRG )
*
*	o Rulbus Interrupt output				( PRB_PRT )
*	  - 1 bit interrupt on trigger from motor 1/2		( PRB_INT )
*
*******************************************************************************

	SUBTITLE [ smc.dsgn ]				Program design
	PAGE
*******************************************************************************
*	
*
*	Program design
*	==============
*	
*
*	o User stack
*	
*	This module  creates an user stack for the MC68705  single 
*	chip   microcomputer.   It  simulates   some   MC6802-like 
*	instructions (LDS, STS, TSX, TXS, DECS, INCS, PSHA,  PULA) 
*	and  provides  a couple of extra instructions  like:  TSA, 
*	TAS, PSHX, PULX, PSHAX, PULAX.
*	Some basic differences exist between the MC6802 stack  and 
*	the MC68705 user stack: 
*	- the user stackpointer is only 8-bits wide
*	- the user stackpointer points to the last pushed item
*	- the  user  stackpointer  exists in RAM, not  in  a  real 
*	  register
*	- the user stackpointer must be initialized (INISTK).
*	
*
*	o Runtime test facility
*	
*	Two testpins configured as output are available for  test-
*	purposes.   One  pin  is used to track  execution  of  the 
*	foreground  program  (TPF) and one pin is  used  to  track 
*	execution  of the background program (TPB). There are  two 
*	macro's  for  each pin to set (SETTPF,  SETTPB)  or  clear 
*	(CLRTPF,  CLRTPB)  it.  Place these macro's  anywhere  you 
*	like to test execution of the program.
*	
*
*	o Main control
*	
*	This module contains the infinite loop of user processes.
*	First the stackpointer is reset and the initialization is 
*	performed, then the infinite loop is entered.
*	
*
*	o Initialize
*	
*	This  module contains all code to initialize  the  system.  
*	It executes as follows:
*	- clear RAM
*	- set input/output lines direction
*	- initialize stack-module
*	- initialize interrupt-module
*	- initialize communication/scanner module
*	- initialize motor 1 and 2 processes
*	- enable interrupt
*	
*
*	o Communication process
*	
*	The  communication process handles the character  exchange 
*	between controller and Rulbus.
*	
*
*	o Scanner process
*	
*	The scanner process reads the commandline and executes the 
*	proper routine.
*	
*
*	o Motor 1 and 2 processes
*	
*	The   motor processes do the motor  position  calibration, 
*	calculate the moter movement and trigger moment. The motor 
*	processes  signal the interrupt routine to do  the  actual 
*	motor stepping, triggering and issuing Rulbus interrupts.
*	
*
*	o Steppingmotor controller
*	
*	This  module contains the routines to control both of  the 
*	steppingmotor controllers.
*	
*******************************************************************************
	PAGE
*******************************************************************************
*
*	o Interrupt
*	
*	The  interrupt routine performs the timing for  the  motor 
*	processes and timer processes and perfoms the actual motor 
*	stepping, triggering and issuing Rulbus interrupts.
*
*******************************************************************************

*------------------------------------------------------------------------------
*  End of X2REG.DES
*------------------------------------------------------------------------------

	SUBTITLE	[ smc.cfg ]			Hardware configuration
	PAGE
*------------------------------------  M O D U L E   hardware configuration ---
*
*  Module hardware configuration
*
*
*  Version: 0.00
*  Date   : 22 Sep 1989
*
*------------------------------------------------------------------------------
*
*  Routines in this module:
*
*  - none
*
*------------------------------------------------------------------------------

*------------------------------------------------------------------------------
*
*  Front layout
*  ------------
*
*
*  +------------------+ 
*  |    +             |
*  |  STEPPING MOTOR  |
*  |    CONTROLLER    |
*  |                  |
*  |                  |
*  |      MOTOR 1     |
*  |     +-------+    |
*  |     |       |    |
*  |     |       |    |
*  |     |       |    |
*  |     |       |    |
*  |     |       |    |
*  |     |       |    |
*  |     +-------+    |
*  |                  |
*  |                  |
*  |      MOTOR 2     |
*  |     +-------+    |
*  |     |       |    |
*  |     |       |    |
*  |     |       |    |
*  |     |       |    |
*  |     |       |    |
*  |     |       |    |
*  |     +-------+    |
*  |                  |
*  |     R U L B U S  |
*  |    +             |
*  +------------------+
*  
*------------------------------------------------------------------------------
	PAGE
*------------------------------------------------------------------------------
*
*  Simplified schematic of the logic controller
*  --------------------------------------------
*
*
*   Rulbus                                     MC68705R3 
*  +------+                                   +---------+          SAA 1027 
*  |      |    ADDRESS                        |         | PM1_PRT +--------+
*  |      |   +------+       +------+ STS_PRT |         |---------| PM1CLK |
*  |   A7 |---|      |  +----|      |---------|         |---------| PM1DIR |
*  |      | : |      |  |    |      |         |         |         +--------+
*  |   A0 |---| PAL  |  |    | HCT  |         |         |
*  |      |   | 16L8 |  |    | 245  |         |         |          TIL 139   
*  |   E  |---|      |  |    |      |         |         | PE1_PRT +--------+  
*  |   RW |---|      |  | +--|      |---------|         |---------| PE1MIN |
*  |      |   +------+  | |  +------+         |         |---------| PE1MAX |
*  |      |             | |                   |         |         +--------+ 
*  |      |             | |                   |         |
*  |      |             | |                   |      __ | PT2_PRT            
*  |      |             | |                   | |_| |   |---------- PT1_TRG
*  |      |             | |                   | |   |__ |
*  |      |             | |                   |         |          SAA 1027  
*  |      |             | |                   |         | PM2_PRT +--------+ 
*  |      |     DATA    | |  REGISTER         |         |---------| PM2CLK |
*  |      |   +------+  | |  +------+ CMD_PRT |         |---------| PM2DIR |
*  |   D7 |---|      |--+.|--|      |---------|         |         +--------+ 
*  |      | : |      |    |  |      |         |         |
*  |      | : | HCT  |    |  | HCT  |         |         |          TIL 139  
*  |      | : | 245  |    |  | 574  |         |         | PE2_PRT +--------+
*  |      | : |      |    |  |      |         |         |---------| PE2MIN | 
*  |   D0 |---|      |----+--|      |---------|         |---------| PE2MAX |
*  |      |   +------+       +------+         |         |         +--------+
*  |      |                                   |         |
*  |      |                           PRB_PRT |         | PT2_PRT            
*  |  INT |-----------------------------------|         |---------- PT2_TRG
*  +------+                           PRB_INT +---------+ 
*                                                        
*------------------------------------------------------------------------------