;/*----------------------------------------------------------------------------+
;|                                                                             |
;|       Copyright (c), Raisonance S.A.                                        |
;|                                                                             |
;+-----------------------------------------------------------------------------+
;|                                                                             |
;|       File: calc.st7                                                        |
;|                                                                             |
;|       Author: J. Rennes - Oct 2003                                          |
;|                                                                             |
;+--------------+-------+----------------------------------------------+-------+
;|     DATE     |VERSION| HISTORY/CHANGES                              |   BY  |
;|  (MM/DD/YY)  | VX.Y  |                                              |       |
;+--------------+-------+----------------------------------------------+-------+
;|   10/27/03   |  1.0  | First implementation                         |  --   |
;+--------------+-------+----------------------------------------------+-------+
;|                                                                             |
;| Description:                                                                |
;|                                                                             |
;| Simple 32 bits calculation program.                                         |
;| Implementation of basic SCI handling routines.                              |
;|                                                                             |
;+----------------------------------------------------------------------------*/

$include(st72521.inc)

;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;   Extern used functions or constants
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
   NULL        EQU   0
   BASE_STACK  EQU   100h

   ;SCICR1 bit definitions
   M           EQU   4
   PCE         EQU   2

   ;SCICR2 bit definitions
   TIE         EQU   7
   RIE         EQU   5
   TE          EQU   3
   RE          EQU   2

   ;SCISR bit definitions
   TDRE        EQU   7
   TC          EQU   6
   RDRF        EQU   5

;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;   Segments
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------

   RSG?DATA          segment     DATA INPAGE0
   RSG?BIT           segment     BIT
   RSG?CODE          segment     CODE

;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;   Data
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------

   rseg RSG?DATA
_TX_ptr:
      ds    2
_RX_ptr:
      ds    1
_RX_buf:
      ds    20
_switch_var:
      ds    4
_arg1:
      ds    4
_arg2:
      ds    4
_calc_buf:
      ds    5
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;   Code
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------

   rseg RSG?CODE
   ;-------------------------------------------------------------------------------
   ;   Purpose:        string to be sent
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_string0:
      db    'Raisonance calc sample program',0
_string1:
      db    'Enter 1st number:',0
_string2:
      db    'Enter 2nd number:',0
_string3:
      db    'Enter operator (+,-,*,/):',0
_string4:
      db    'Result of operation is:',0

   ;-------------------------------------------------------------------------------
   ;   Function:       _sci_it_routine
   ;   Purpose:        SCI interrupt handler
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_sci_it_routine:
      ;read status register
      BTJF  SCISR,#RDRF,_trsm

      ;received next character
      LD    A,SCIDR

      ;ensure that end of string is not reach
      JREQ  _rcv_end
      CP    A,#0Ah
      JRNE  _rcv_nxt
      LD    A,#0h

_rcv_end :
      BRES  SCICR2,#RIE

_rcv_nxt:
      LD    X,_RX_ptr
      LD    (_RX_buf,X),A

      JRNE  _rcv_newline
      LD    A,#0Ah      ; write '\n'


_rcv_newline:
      INC   _RX_ptr

      ;wait for end of transmission
      ;end transmit received character
      BTJF  SCISR,#TC,$
      LD    SCIDR,A

      IRET

_trsm:
      ;transmit next character
      LD    A,[_TX_ptr.w]

      ;ensure that end of string is not reach
      JRNE  _trsm_end

      BRES  SCICR2,#TIE
      LD    A,#0Ah      ; write '\n'

_trsm_end:
      LD    SCIDR,A

      ;increment string pointer
      INC   _TX_ptr+1
      JRNE  _iret
      INC   _TX_ptr

_iret:
      IRET

   ;-------------------------------------------------------------------------------
   ;   Function:       _sci_init
   ;   Purpose:        Initializes the SCI in the required mode
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_sci_init:
      ;Load Default value
      CLR   SCICR1
      CLR   SCICR2

      ;Clear all the flags in Status register
      LD    A,SCISR
      LD    A,SCIDR
      CLR   SCIDR

      ;words will be 9 bits long
;     BSET  SCICR1,#M

      ;Parity control
;     BSET  SCICR1,#PCE

      ;set baudrate to 65200 bauds @ 8 MHz
      CLR   SCIETPR
      CLR   SCIERPR
      LD    A,#01Bh
      LD    SCIBRR,A

      ;enable the SCI
      BSET  SCICR2,#TE
      BSET  SCICR2,#RE
      RET

   ;-------------------------------------------------------------------------------
   ;   Function:       _puts
   ;   Purpose:        print the string pointed by (X.A)
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_puts:
      ;wait until Data Transfer Register is empty
      BTJF  SCISR,#TDRE,$

      ;load pointer
      LD    _TX_ptr,X
      LD    _TX_ptr+1,A

      ;enable SCI interrupt
      BSET  SCICR2,#TIE

      RET

   ;-------------------------------------------------------------------------------
   ;   Function:       _gets
   ;   Purpose:        read the string on stdio, return a pointer in (X.A)
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_gets:
      ;load buffer pointer
      CLR   _RX_ptr

      ;enable SCI interrupt
      BSET  SCICR2,#RIE

      ;wait until data has been received
      BTJT  SCICR2,#RIE,$

      ;return pointer
      CLR   X
      LD    A,#LOW(_RX_buf)
      RET

   ;-------------------------------------------------------------------------------
   ;   Function:       _main
   ;   Purpose:        beginning of the program
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_main:
      ;reset stack pointer
      RSP

      ;Configure the SCI
      CALLR _sci_init

      ;Enable interrupts
      RIM

_main_loop:
      LD    X,#HIGH(_string0)
      LD    A,#LOW(_string0)
      CALLR _puts

      ;Send 1st string
      LD    X,#HIGH(_string1)
      LD    A,#LOW(_string1)
      CALLR _puts

      ;Get number
      CALLR _gets
      CALL  _atoi
      LD    A,_arg1+3
      PUSH  A
      LD    A,_arg1+2
      PUSH  A
      LD    A,_arg1+1
      PUSH  A
      LD    A,_arg1
      PUSH  A

      ;Send 2nd string
      LD    X,#HIGH(_string2)
      LD    A,#LOW(_string2)
      CALLR _puts

      ;Get number
      CALLR _gets
      CALL  _atoi

      ;restore 1st arg
      POP   A
      LD    _arg2,A
      POP   A
      LD    _arg2+1,A
      POP   A
      LD    _arg2+2,A
      POP   A
      LD    _arg2+3,A

      ;Send 3rd string
      LD    X,#HIGH(_string3)
      LD    A,#LOW(_string3)
      CALLR _puts

      ;Get operator
      CALLR _gets

      ;Operator is pointed by A
      LD    Y,A
      CLR   X

switch:
      LD    A,(jump_table,X)

      ;compare with operator which is in (Y)
      CP    A,(Y)
      JRNE  _switch_next

      ;Load pointer
      LD    A,(jump_table + 1,X)
      LD    _switch_var,A
      LD    A,(jump_table + 2,X)
      LD    _switch_var+1,A

      CALL  [_switch_var.w]

_call_handler:
      JP _display

;-------------------------------------------------------------------------------
_switch_next:
      INC   X
      INC   X
      INC   X

      ;ensure X is in range
      CP    X,#case_add-jump_table
      JREQ  case_add

      ;continue
      JRA   switch

jump_table:
      db    '+'
      dw    case_add
      db    '-'
      dw    case_sub
      db    '*'
      dw    case_mul
      db    '/'
      dw    case_div

case_add:
      LD    A,_arg1+3
      ADD   A,_arg2+3
      LD    _arg2+3,A

      LD    A,_arg1+2
      ADC   A,_arg2+2
      LD    _arg2+2,A

      LD    A,_arg1+1
      ADC   A,_arg2+1
      LD    _arg2+1,A

      LD    A,_arg1
      ADC   A,_arg2
      LD    _arg2,A
      RET

case_sub:
      LD    A,_arg2+3
      SUB   A,_arg1+3
      LD    _arg2+3,A

      LD    A,_arg2+2
      SBC   A,_arg1+2
      LD    _arg2+2,A

      LD    A,_arg2+1
      SBC   A,_arg1+1
      LD    _arg2+1,A

      LD    A,_arg2
      SBC   A,_arg1
      LD    _arg2,A
      RET

case_mul:
   ;-------------------------------------------------------------------------------
   ;   Function:       case_mul
   ;   Purpose:        multiply two integers
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
      CALL  _signed_to_unsigned

_mul_unsigned:
      ;MSB of parameter 2 * LSB1
      LD    A,_arg2
      LD    _calc_buf,A
      LD    A,_arg2+1
      LD    _calc_buf+1,A

      LD    A,_arg1+2
      LD    _calc_buf+2,A
      LD    A,_arg1+3
      LD    _calc_buf+3,A

      CALLR _mul16_unsigned

      LD    A,_calc_buf+2
      LD    _arg2,A
      LD    A,_calc_buf+3
      LD    _arg2+1,A

      ;LSB of parameter 2 * LSB1
      LD    A,_arg2+2
      LD    _calc_buf,A
      LD    A,_arg2+3
      LD    _calc_buf+1,A

      LD    A,_arg1+2
      LD    _calc_buf+2,A
      LD    A,_arg1+3
      LD    _calc_buf+3,A

      CALLR _mul16_unsigned

      LD    A,_calc_buf+1
      ADD   A,_arg2+1
      LD    _arg2+1,A

      LD    A,_calc_buf
      ADC   A,_arg2
      LD    _arg2,A

      ;LSB of parameter 2 * MSB1
      LD    A,_arg2+2
      LD    _calc_buf,A
      LD    A,_arg2+3
      LD    _calc_buf+1,A

      LD    A,_calc_buf+2
      LD    _arg2+2,A
      LD    A,_calc_buf+3
      LD    _arg2+3,A

      LD    A,_arg1
      LD    _calc_buf+2,A
      LD    A,_arg1+1
      LD    _calc_buf+3,A

      CALLR _mul16_unsigned

      LD    A,_calc_buf+3
      ADD   A,_arg2+1
      LD    _arg2+1,A

      LD    A,_calc_buf+2
      ADC   A,_arg2
      LD    _arg2,A

      JP    _div_compute_sign

   ;-------------------------------------------------------------------------------
   ;   Function:       _mul16_unsigned
   ;   Purpose:        unsigned 16 bits division, result on 32 bits
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_mul16_unsigned:
      ;MSB1 * LSB2
      LD    X,_calc_buf
      LD    A,_calc_buf+3
      MUL   X,A
      PUSH  A

      ;LSB1 * MSB2
      LD    Y,_calc_buf+1
      LD    A,_calc_buf+2
      MUL   Y,A
      PUSH  Y

      ;MSB1 * LSB2 + LSB1 * MSB2
      LD    Y,S
      ADD   A,(BASE_STACK+2,Y)   ;LSB
      LD    (BASE_STACK+2,Y),A

      LD    A,X
      ADC   A,(BASE_STACK+1,Y)   ;MSB
      POP   X
      PUSH  A
      PUSH  CC                   ;keep the carry!

      ;LSB1 * LSB2
      LD    X,_calc_buf+1
      LD    A,_calc_buf+3
      MUL   X,A
      LD    _calc_buf+3,A        ;LSB

      LD    A,X
      ADD   A,(BASE_STACK+2,Y)   ;MSB
      PUSH  CC                   ;keep the carry!

      ;MSB1 * MSB2
      LD    X,_calc_buf+2
      LD    _calc_buf+2,A
      LD    A,_calc_buf
      MUL   X,A
      POP   CC
      ADC   A,(BASE_STACK+1,Y)   ;LSB

      LD    _calc_buf+1,A
      LD    A,X
      POP   CC
      ADC   A,#0                 ;MSB
      LD    _calc_buf,A
      POP   A
      POP   A
      RET

   ;-------------------------------------------------------------------------------
   ;   Function:       case_div
   ;   Purpose:        divide two integers (_arg2 / _arg1)
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
case_div:
      CALLR _signed_to_unsigned

_div_unsigned:
      LD    X,#32
      CLR   _calc_buf+3
      CLR   _calc_buf+2
      CLR   _calc_buf+1
      CLR   _calc_buf

_div_loop:
      SLL   _arg2+3
      RLC   _arg2+2
      RLC   _arg2+1
      RLC   _arg2
      RLC   _calc_buf+3
      RLC   _calc_buf+2
      RLC   _calc_buf+1
      RLC   _calc_buf

      ;compare the two operands
      LD    A,_calc_buf
      CP    A,_arg1
      JRNE  _div_compare
      LD    A,_calc_buf+1
      CP    A,_arg1+1
      JRNE  _div_compare
      LD    A,_calc_buf+2
      CP    A,_arg1+2
      JRNE  _div_compare
      LD    A,_calc_buf+3
      CP    A,_arg1+3

      ;Here if N = 1,_calc_buf < _arg1
_div_compare:
      JRULT _div_endloop

      ;evaluate _calc_buf - _arg1
      LD    A,_calc_buf+3
      SUB   A,_arg1+3
      LD    _calc_buf+3,A

      LD    A,_calc_buf+2
      SBC   A,_arg1+2
      LD    _calc_buf+2,A

      LD    A,_calc_buf+1
      SBC   A,_arg1+1
      LD    _calc_buf+1,A

      LD    A,_calc_buf
      SBC   A,_arg1
      LD    _calc_buf,A

_div_nxt:
      INC   _arg2+3

_div_endloop:
      DEC   X
      JRNE  _div_loop

_div_compute_sign:
      ;result sign
      BTJF  _calc_buf+4,#7,_div_end

      ;negate result
      CPL   _arg2
      LD    X,_arg2+1
      CPL   X
      LD    A,_arg2+2
      CPL   A
      NEG   _arg2+3
      SBC   A,#0FFh
      LD    _arg2+2,A
      LD    A,X
      SBC   A,#0FFh
      LD    _arg2+1,A
      JRC   _div_end
      INC   _arg2

_div_end:
      RET

   ;-------------------------------------------------------------------------------
   ;   Function:       _signed_to_unsigned
   ;   Purpose:        compute absolute value of arg1 and arg2, store result sign in _calc_buf+2
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_signed_to_unsigned:
      ;result sign
      LD    A,_arg1
      XOR   A,_arg2
      LD    _calc_buf+4,A

      ;if _arg1 < 0, invert it now
      BTJF  _arg1,#7,_signed_to_unsigned_nxt
      CPL   _arg1
      LD    X,_arg1+1
      CPL   X
      LD    A,_arg1+2
      CPL   A
      NEG   _arg1+3
      SBC   A,#0FFh
      LD    _arg1+2,A
      LD    A,X
      SBC   A,#0FFh
      LD    _arg1+1,A
      JRC   _signed_to_unsigned_nxt
      INC   _arg1

_signed_to_unsigned_nxt:
      ;if _arg2 < 0, invert it now
      BTJF  _arg2,#7,_signed_to_unsigned_end
      CPL   _arg2
      LD    X,_arg2+1
      CPL   X
      LD    A,_arg2+2
      CPL   A
      NEG   _arg2+3
      SBC   A,#0FFh
      LD    _arg2+2,A
      LD    A,X
      SBC   A,#0FFh
      LD    _arg2+1,A
      JRC   _signed_to_unsigned_nxt
      INC   _arg2

_signed_to_unsigned_end:
      RET

   ;-------------------------------------------------------------------------------
   ;   Function:       _display
   ;   Purpose:        display _arg1
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_display:
      ;put zero at the end of string
      CLR   _switch_var-1
      LD    X,#_switch_var-1
      PUSH  X

      ;prepare unsigned division
      CLR   _calc_buf+4
      CLR   _arg1
      CLR   _arg1+1
      CLR   _arg1+2
      LD    A,#10
      LD    _arg1+3,A

      ;Keep the sign
      LD    A,_arg2
      LD    _switch_var,A
      BTJF  _arg2,#7,_display_loop

      ;negate result
      CPL   _arg2
      LD    X,_arg2+1
      CPL   X
      LD    A,_arg2+2
      CPL   A
      NEG   _arg2+3
      SBC   A,#0FFh
      LD    _arg2+2,A
      LD    A,X
      SBC   A,#0FFh
      LD    _arg2+1,A
      JRC   _display_loop
      INC   _arg2

_display_loop:
      ;divide by 10
      CALL  _div_unsigned
      POP   X

      ;grab and display the rest
      LD    A,_calc_buf+3
      ADD   A,#30h
      DEC   X
      LD    (X),A
      PUSH  X

      LD    A,_arg2
      OR    A,_arg2+1
      OR    A,_arg2+2
      OR    A,_arg2+3

      ;compute next character if _arg2 != 0
      JRNE  _display_loop

      POP   X
      BTJF  _switch_var,#7,_display_unsigned

      ;print the sign
      LD    A,#2Dh
      DEC   X
      LD    (X),A

_display_unsigned:
      LD    A,X
      CLR   X
      CALL  _puts

      JP _main_loop

   ;-------------------------------------------------------------------------------
   ;   Function:       _atoi
   ;   Purpose:        convert a string into a int
   ;   Date:           10/28/2003
   ;-------------------------------------------------------------------------------
_atoi:
      ;pointer to ASCII string
      LD    X,A

      ;result
      CLR   _arg2
      CLR   _arg2+1
      CLR   _arg2+2
      CLR   _arg2+3

      ;prepare multiplication
      CLR   _calc_buf+4
      CLR   _arg1
      CLR   _arg1+1
      CLR   _arg1+2
      LD    A,#10
      LD    _arg1+3,A

      ;sign
      LD    A,(X)
      XOR   A,#2Dh      ;'-'
      PUSH  A
      JRNE  _atoi_loop
      INC   X

_atoi_loop:
      LD    A,(X)

      ;ensure it is a number
      CP    A,#30h      ;compare with '0'
      JRULT _atoi_end
      CP    A,#39h      ;compare with '9'
      JRUGT _atoi_end

      ;Multiplicate by 10
      PUSH  X
      CALL  _mul_unsigned
      POP   X

      ;add converted character
      LD    A,(X)
      SUB   A,#30h
      ADD   A,_arg2+3
      LD    _arg2+3,A
      CLR   A
      ADC   A,_arg2+2
      LD    _arg2+2,A
      CLR   A
      ADC   A,_arg2+1
      LD    _arg2+1,A
      CLR   A
      ADC   A,_arg2
      LD    _arg2,A

      ;increment pointer and loop
      INC   X
      JRA   _atoi_loop

_atoi_end:
      ;don't forget the sign
      LD    Y,_arg2
      LD    X,_arg2+1

      POP   A
      TNZ   A
      JRNE  _atoi_ret

      ;complement
      CPL   Y
      CPL   X
      LD    A,_arg2+2
      CPL   A
      NEG   _arg2+3

      SBC   A,#0FFh
      LD    _arg2+2,A

      JRNC  _atoi_ret
      LD    A,X
      SBC   A,#0FFh
      LD    X,A

      JRC   _atoi_ret
      INC   Y

_atoi_ret:
      LD    _arg1,Y
      LD    _arg1+1,X

      LD    A,_arg2+2
      LD    _arg1+2,A

      LD    A,_arg2+3
      LD    _arg1+3,A
      RET

;-------------------------------------------------------------------------------

   cseg at 0ffE0h

_vectab:
   dw    NULL           ;PWMART
   dw    NULL           ;I2C
   dw    NULL           ;NOT USED
   dw    _sci_it_routine;SCI
   dw    NULL           ;TIMER B
   dw    NULL           ;TIMER A
   dw    NULL           ;SPI
   dw    NULL           ;CAN
   dw    NULL           ;EI3
   dw    NULL           ;EI2
   dw    NULL           ;EI1
   dw    NULL           ;EI0
   dw    NULL           ;MCC
   dw    NULL           ;TLI
   dw    NULL           ;TRAP
   dw    _main          ;RESET

END












