#include "msp430x14x.h"
#include "netbug.equ"


;------------------------------------------------------------------------------
;           Rezervaciq na pamet za danni
;------------------------------------------------------------------------------ 
            
            org         0x200
BEGA        DS         2
ENDA        DS         2

;
;           Sledvastite baitove se inicializirat s 0
;           sled startirane
;

CHSUM       DS        1
TRCNT       DS        1
SOHCNT      equ       $
CKSM        equ       $
TEMP        equ       $
            DS        1
            
BYTECT      equ       $            
MCONT       equ       $ 
            DS        1

XADD        DS        1
BRKTRC      DS        1

BRKADD      DS        2
XSAVE       equ       $
TW          equ       $
            DS        2
            
BRKSIN      DS        1

;
;           Tezi baitove se inicializirat sled startirane
;

SP          DS        1

;           do tuk se inicializirat

OFFSET      equ       $
TEMP21      DS        2
TEMP22      DS        2
TEMP2       DS        2
TEMP1       DS        1

RT_MODE     DS        1
BUFPNT      DS        2
            DS        4
BUFF        DS        150
BUF_END     equ       $

MODE        equ       $
            DS        1
            DS        1 ; ZA DA MOJE STACK-A DA NA CHETEN NOMER
STACK_ST    equ       $
            DS        74
STACK_      equ       $

;           Bufer za tochkite na prekyswane
;
NBRBPT      EQU       3
BRKADR      DS        NBRBPT*6
BRKINS      EQU       $




;------------------------------------------------------------------------------
;
;          Deklarirane na ASCII simvoli i konstanti
;
;------------------------------------------------------------------------------

SELFAD      equ       'C'
MAINAD      equ       'A'
SOH         equ       0x01
STX         equ       0X02
ETX         equ       0x03
EOT         equ       0X04
ACK         equ       0x06
SPC         EQU       ' '

C_MODE      equ       0xE1
D_MODE      equ       0XE2


            ;RSEG CODE
            org 0x8000



;RESET
;------------------------------------------------------------------------------ 
RESET 
;------------------------------------------------------------------------------ 
Init_Sys;   Initalize MSP430 system
;------------------------------------------------------------------------------ 

StopWDT     mov.w   #WDTPW+WDTHOLD,&WDTCTL  ; Stop WDT
            MOV.w   #STACK_, SP
SetupUART1  mov.b   #CHAR,&UCTL1            ; 8-bit characters
            mov.b   #SSEL0,&UTCTL1          ; UCLK = ACLK
            mov.b   #00Dh,&UBR01            ; 32k/2400 - 13.65
            mov.b   #000h,&UBR11            ; 32k 2400
            mov.b   #06Bh,&UMCTL1           ; 32k 2400 modulation 
            bis.b   #UTXE1+URXE1,&ME2       ; Enable USART1 TXD/RXD
SetupP3     bis.b   #0C0h,&P3SEL            ; P3.6,7 = USART1 option select
            bis.b   #040h,&P3DIR            ; P3.6 = output direction 
            dint                            ; General disable interrupts
            
            MOV       #STACK_, SP

            ;MOV       #0x82, R12
            ;BR        #BRKSUB
            ;MOV       #0x0450, TEMP21
            ;MOV       #0x02B0, TEMP22
            ;MOV       #0x000f, TEMP2
            ;CALL      #MOVTORAM

;
;           Izprastane na nachalno syobstenie
;
            mov.b   #SOH, BUFF-4          ;start na syobstenie
            mov.b   #SELFAD, BUFF-2       ;sobstven adres
            mov.b   #MAINAD, BUFF-3       ;adres na podatel
            mov.b   #80h, BUFF-1          ;cvetnost
            mov     #IDENT, R13
            call    #MOVTXT
            call    #PDATABF
;------------------------------------------------------------------------------
;           Glaven komanden cikyl
;------------------------------------------------------------------------------ 

CONTRL      EQU     $
;------------------------------------------------------------------------------
;           Pechat na prompta
;           i inicializaciq na ukazatelq na steka
;------------------------------------------------------------------------------
            MOV.w   #STACK_, SP
            mov     #PROMPT, R13
            call    #MOVTXT
            call    #PDATABF
;
;           Poemane na syobstenie v bufera
;            
            call    #PSHBUF
            jmp     COMMAND
;
;           Poemane na komandata ot Bufera
;            
COMMAND     EQU     $
            mov     #BUFF+1, BUFPNT
            call    #INCH  
;
;           R12 sydirja komandata vyvedena ot potrebitelq,
;           komandata se izpylnqva posredstvom jmp tablica
;            
            mov     #FCTABL, R13
NXTCH       cmp.b   @R13+, r12
            jz      GOODCH
            cmp     #FCTBEN, R13
            jne     NXTCH
;
;           Reakciq pri nepoznata komanda
;            
            mov     #NOCMD, R12
            call    #MOVTXT
            call    #PDATABF
            jmp     CONTRL
;
;           Reakciq pri pravilna komanda
;
GOODCH      sub     #FCTABL+1, r13
            rla     r13
            add     #FCADDR, r13
            BR      @r13
            
FCTABL      EQU     $       ;must be even

            DB      'A'
            DB      'D'
            DB      'F'
            DB      'G'
            DB      'H'
            DB      'I'
            DB      'Z'
            DB      'S'
            DB      'K'
            DB      'L'
            DB      'M'
            DB      'P'
            DB      'R'
            DB      'V'
            DB      'W'
FCTBEN      EQU     $
            DB      ' '
FCADDR      EQU     $       ;must be even

            DW      MOVEDT
            DW      RSTBRK
            DW      13;#FWORD
            DW      GOTO
            DW      HPRINT
            DW      INIMEM
            DW      17;#DELSEC
            DW      18;#SECT
            DW      COMPAR
            DW      LOAD
            DW      21;#CHANGE
            DW      22;#PUNCH
            DW      CNGREG
            DW      SETBRK
            DW      25;#WTEST

            
            jmp     CONTRL

;------------------------------------------------------------------------------
;
;         KOMANDA R - PROMQNA I PECHAT SYDYRJANIETO NA REGISTRITE
;         R RN DDDD
;         R
;
;------------------------------------------------------------------------------
CNGREG      EQU     $
            CMP.B   #1, BUFF
            JNE     CHNR1
            JMP     CHNPRN
CHNR1       CALL    #INCHS
            CMP.B   #'R', R12
            JNE     CHNPRN
            CALL    #INHEX
            MOV     R12, TEMP1     ; TMP1 SYDYRVA NOMERA NA REGISTURA ZA SMQNA
            CALL    #BADDRS
            MOV     R12, TEMP2            
CR0         CMP.B   #00h, TEMP1      ; R0
            JNE     CR1
            MOV     TEMP2, R0
CR1         CMP.B   #01h, TEMP1      ; R1
            JNE     CR2
            MOV     TEMP2, R1
CR2         CMP.B   #02h, TEMP1      ; R2
            JNE     CR3
            MOV     TEMP2, R2
CR3         CMP.B   #03h, TEMP1      ; R3
            JNE     CR4
            ;MOV     TEMP2, R3
CR4         CMP.B   #04h, TEMP1      ; R4
            JNE     CR5
            MOV     TEMP2, R4
CR5         CMP.B   #05h, TEMP1      ; R5
            JNE     CR6
            MOV     TEMP2, R5
CR6         CMP.B   #06h, TEMP1      ; R6
            JNE     CR7
            MOV     TEMP2, R6
CR7         CMP.B   #07h, TEMP1      ; R7
            JNE     CR8
            MOV     TEMP2, R7
CR8         CMP.B   #08h, TEMP1      ; R8
            JNE     CR9
            MOV     TEMP2, R8
CR9         CMP.B   #09h, TEMP1      ; R9
            JNE     CR10
            MOV     TEMP2, R9
CR10        CMP.B   #0Ah, TEMP1      ; R10
            JNE     CR11
            MOV     TEMP2, R10
CR11        CMP.B   #0Bh, TEMP1      ; R11
            JNE     CR12
            MOV     TEMP2, R11
CR12        CMP.B   #0Ch, TEMP1      ; R12
            JNE     CR13
            MOV     TEMP2, R12
CR13        CMP.B   #0Dh, TEMP1      ; R13
            JNE     CR14
            MOV     TEMP2, R13
CR14        CMP.B   #0Eh, TEMP1      ; R14
            JNE     CR15
            MOV     TEMP2, R14
CR15        CMP.B   #0Fh, TEMP1      ; R15
            JNE     CHNPRN
            MOV     TEMP2, R15
            MOV     R0, TEMP2                       
CHNPRN      CALL    #PRINT
CHNEND      JMP     CONTRL
;------------------------------------------------------------------------------
;
;         KOMANDA A - PREMESTVANE NA MASIV OT DANNI
;         A BBBB EEEE NNNN
;
;------------------------------------------------------------------------------
MOVEDT      EQU $
;VYVEJDANE NACHALEN, KRAEN ADRES I NOVIQ NACHALEN ADRES
            CALL    #BADDRS
            MOV     R12, BEGA       ;NACHALEN ADRES
            CALL    #BADDRS
            MOV     R12, ENDA       ;KRAEN ADRES
            CALL    #BADDRS
            MOV     R12, TEMP2      ;TEMP2 - NOV NACHALEN ADRES
            MOV     ENDA, R12
            SUB     BEGA, R12       ;V R12 E DYLJINATA NA MASIVA
            INC     R12
            ; PREDAVANE NA ARGULEMTI NA FUNC SECTORWR
            MOV     BEGA, TEMP21
            MOV     TEMP2, TEMP22
            MOV     R12, TEMP2
            CALL    #SECTORWR
            JMP     CONTRL
;------------------------------------------------------------------------------
;
;           KOMANDA I - INICIALIZIRANE NA PAMETTA
;           
;           I BBBB EEEE DD
;
;------------------------------------------------------------------------------
INIMEM      EQU     $
            CALL    #BADDRS
            MOV     R12, BEGA         ;NACHALEN ADRES
            CALL    #BADDRS
            MOV     R12, ENDA         ;NACHALEN ADRES
            CALL    #BYTES
            MOV     BEGA, R13
NXTINI      MOV.B   R12, 0(R13)
            CMP     R13, ENDA
            JEQ     ENDINI
            INC     R13
            JMP     NXTINI
ENDINI      JMP     CONTRL                        
;------------------------------------------------------------------------------
;
;         KOMANDA H - RAZPECHATVANE SYDYRJANIETO NA PAMETTA
;         H BBBB EEEE
;
;------------------------------------------------------------------------------
;         FULLY TESTED
;------------------------------------------------------------------------------
HPRINT      EQU     $
            CALL    #BADDRS
            MOV     R12, BEGA         ;NACHALEN ADRES
            CALL    #BADDRS
            MOV     R12, ENDA         ;NACHALEN ADRES
;ZAKRYLNQNE NA NACHALNIQ ADRES            
            AND.B   #0xF0, BEGA       ;NA BEGA E MALDSHIQ BAIT
;PODGOTOVKA ZA CIKYLA
            MOV     &BEGA, TEMP2
; CIKYL NA RAZPECHATVANE
            MOV     ENDA, BUFF        ; dyljinata na izhodniq bufer e 3*(ENDA-BEGA+1)+4
            SUB     BEGA, BUFF
            INC     BUFF
            MOV     BUFF, R12
            RLA     R12
            ADD     #0x04, R12
            ADD     R12, BUFF
            ;MOV.B   #71, BUFF
ONELIN      EQU     $
            MOV     #BUFF+1, BUFPNT
            MOV     #TEMP2+1, R13
            CALL    #OUT2H
            MOV     #TEMP2, R13
            CALL    #OUT2H
            CALL    #OUTSPC
            MOV     TEMP2, R13        ;TEKUST ADRES
            MOV.B   #10H, TEMP1       ;BROJ BAJTA NA RED
; IZVEJDANE NA EDIN RED            
NEXTBT      CALL    #OUT2HS           ;IZVEJDANE NA CHISLOTO
            DEC.B   TEMP1             ;MODIFICIRANE NA BROQCHA
            JNE     NEXTBT            ;OSTE SIMVOLI V REDA
; IZVEJDANE NA ASCII-KODOVETE            
            ;CALL    #OUTSPC                    ;\
            ;MOV     TEMP2, R13                 ; \
            ;MOV.B   #10H, TEMP1                ;  \
NXTSYM      ;MOV.B   @R13, R12                  ;   |
            ;AND.B   #01111111B, R12            ;   |
            ;CMP.B   #SPC, R12                  ;   |
            ;JGE     POINT                      ;   |
            ;CMP.B   #'~', R12                  ;   | NE ZNAM ZA 
            ;JGE     OUTCH4                     ;   | KAKVO E OSTE !!??
POINT       ;MOV.B   #'.', R12                  ;   |
OUTCH4      ;CALL    #OUTCHBF                   ;   |      
            ;INC     R13                        ;  /
            ;DEC.B   TEMP1                      ; /
            ;JNE     NXTSYM                     ;/
; PRIKLJUCHVANE NA CIKYLA ZA RED I PROVERKA
            MOV     R13, TEMP2
            CALL    #PDATABF
            MOV     TEMP2, R13
            CMP     #0000H, R13
            JEQ     HPEND
            CMP     R13, ENDA
            JN      HPEND
            JMP     ONELIN
HPEND       JMP     CONTRL                        
;------------------------------------------------------------------------------
;
;         KOMANDA K - SRAVNQVANE NA MASIV OT DANNI
;         K BBBB EEEE NNNN
;
;------------------------------------------------------------------------------
COMPAR      EQU $
;VYVEJDANE NACHALEN, KRAEN ADRES I DRUGIQ NACHALEN ADRES
            CALL    #BADDRS
            MOV     R12, BEGA       ;NACHALEN ADRES
            MOV     BEGA, R13
            CALL    #BADDRS
            MOV     R12, ENDA       ;KRAEN ADRES
            MOV     ENDA, R14
            INC     R14
            CALL    #BADDRS
            MOV     R12, TEMP2      ;TEMP2 - DRUGIQ NACHALEN ADRES
COMPBYTE    CMP.B   @R13+, 0(R12)
            JNE     NOTEQUAL
            INC     R12
            CMP     R13, R14
            JEQ     EQUAL    
            JMP     COMPBYTE
NOTEQUAL    mov     #NOTEQ, R13
            call    #MOVTXT
            call    #PDATABF
            JMP     CONTRL
EQUAL       mov     #EQUALS, R13
            call    #MOVTXT
            call    #PDATABF
            JMP     CONTRL

;------------------------------------------------------------------------------
;
;         KOMANDA L - VYVEJDANE NA MASIV OT DANNI
;         L BBBB DD DD DD .. ..
;
;------------------------------------------------------------------------------
LOAD        EQU $
            ; ENDA E UKAZATEL KYM KRAQ NA MASIVA ZA PRIEMANE
            MOV     #BUFF, ENDA
            MOV.B   BUFF, R12
            ADD     R12, ENDA
            INC     ENDA
            ; R13 E UKAZATEL ZA ZAPISVANE NA HEX DANNITE
            MOV     #BUFF, R13
            CALL    #BADDRS
            MOV     R12, TEMP22       ;ADRES
READDATA    CMP     BUFPNT, ENDA
            JEQ     LTOFLASH
            INC     R13
            CALL    #BYTES
            MOV.B   R12, 0(R13)
            JMP     READDATA
LTOFLASH    MOV     #BUFF+1, TEMP21    
            ;DEC     R13        
            SUB     #BUFF, R13
            MOV     R13, TEMP2        ;DYLJINA NA MASIVA
            CALL    #SECTORWR
            JMP     CONTRL
;------------------------------------------------------------------------------
;
;         KOMANDA G - STARTIRANE PO ADRES
;         G AAAA
;
;------------------------------------------------------------------------------
GOTO        EQU     $
            CALL    #BADDRS
            MOV     R12, BRKADD
            MOV     R12, STACK_ST
            MOV     #BK$INS, R12
            CALL    #BRKSUB
            BR      BRKADD
;------------------------------------------------------------------------------
;
;         KOMANDA D - NULIRANE NA TOCHKI NA PREKYSWANE
;         D
;         D AAAA
;
;------------------------------------------------------------------------------
RSTBRK      EQU     $
            CMP.B   #1, BUFF
            JEQ     DELBRK
            CALL    #BADDRS
            MOV     R12, BRKADD
            MOV     #BK$PG1, R12
            JMP     SETB2
; NULIRANE NA VSICHKI TOCHKI PRI D BEZ ADRES
DELBRK      EQU     $
            MOV     #BK$PGA, R12
            JMP     SETB2
;------------------------------------------------------------------------------
;
;         KOMANDA V - POSTAWQ/RAZPECHATVA TOCHKI NA PREKYSVANE
;         V
;         V AAAA
;
;------------------------------------------------------------------------------
PNTBRK      EQU     $
            MOV     #BK$PRT, R12
            JMP     SETB2
SETBRK      EQU     $
            CMP.B   #6, BUFF
            JNE     PNTBRK
            CALL    #BADDRS
            MOV     R12, BRKADD
            MOV     #BK$PG1, R12
            CALL    #BRKSUB
            MOV     #BK$RCD, R12
SETB2       CALL    #BRKSUB
BRKEND      JMP     CONTRL                                           
;------------------------------------------------------------------------------
;
;           BRKSUB
;  
;  TAZI PODPROGRAMA IZWYRSHVA OPERACII S TOCHKITE NA PREKYSVANE
;  SYDYRJANIETO NA R12 OPREDELQ OPERACIQTA
;  R12 = BK$INS => VMYKVA V POTREBITELSKIQ KOD
;  R12 = BK$PG1 => PREMAHVA TOCHKATA V BRKADD, OSTANALITE
;                  SE OTSRANQVAT
;  R12 = BK$PGA => PREMAHVA VSICHKI TOCHKI
;  R12 = BK$PRT => PECHATA VSICHKI TOCHKI I GI OTSTRANQVA
;  R12 = BK$RMV => OTSTRANQVA VSICHKI TOCHKI
;  R12 = BK$RCD => SYZDAVA NOVA TOCHKA V TABLICATA OT BRKADD
;                  OSTANALITE OTSTRANQVA
BK$RMV      EQU     00000001B
BK$INS      EQU     10000010B
BK$RCD      EQU     00000101B
BK$PG1      EQU     00001001B
BK$PGA      EQU     00010001B
BK$PRT      EQU     00100001B
;
;------------------------------------------------------------------------------
BRKSUB      EQU     $
            PUSH    R12
            MOV     #BRKADR, R13          ;R13 VMESTO INDEX REGISTER X
BRK_0       MOV     R13, XSAVE
            POP     R14                   ; komandata se pazi v r14
            PUSH    R14
            MOV     2(R13), R12
            MOV     4(R13), R11
            MOV     @R13, R13 
            TST     R13
            JEQ     BRK_01
            AND.B   #0xFB, R14            ; R14 VMESTO REG B
            JMP     BRK_011
BRK_01      AND.B   #0xE4, R14
BRK_011     TST.B   BRKSIN
            JEQ     BRK_02
            ;AND.B   #0xFD, R14
            JMP     BRK_021
BRK_02      ;AND.B   #0xFE, R14
;
;  PREZAPIS NA POTREBITELSKIQ KOD OT
;  TABLICATA S TOCHKITE NA PREKYSVANE                             
;
; 0000 0001
BRK_021     RRA.B   R14
            JNC     BRK_2
            PUSH    R14
            MOV     XSAVE, R14
            CMP     0(R13), 2(R14)
            JNE     BRK_022
            CMP     2(R13), 4(R14)
            JNE     BRK_022
            JMP     BRK_023
BRK_022     MOV     XSAVE, R12
            ADD     #2, R12
            MOV     R12, TEMP21
            MOV     R13, TEMP22
            MOV     #4, TEMP2
            CALL    #SECTORWR
BRK_023     POP     R14       
;
;  ZAPIS NA SWI V POTREBITELSKIQ KOD
;
; 0000 0010
BRK_2       RRA.B   R14
            JNC     BRK_3
;-------------------------------------
            CMP     R13, STACK_ST
            JEQ     BRK_21
;-------------------------------------
            MOV     XSAVE, R13
            ;MOV     2(R13), 0(R12)            
            ;MOV     4(R13), 2(R12)
            MOV     #SWI, TEMP21
            MOV     @R13, TEMP22
            MOV     #4, TEMP2
            CALL    #SECTORWR
BRK_21
;
; ZAPIS NA TOCHKITE V TABLICATA
;
; 0000 0100
BRK_3       MOV     XSAVE, R13
            RRA.B   R14
            JNC     BRK_4
            MOV     BRKADD, 0(R13)
            PUSH    R14
            MOV     @R13, R14
            MOV     0(R14), 2(R13)
            MOV     2(R14), 4(R13)
            POP     R14
            AND.B   #11111011b, 0(SP)

;
; PREMAHVANE NA TOCHKATA, CHIJTO ADRES E V BRKADD
;
; 0000 1000                  
BRK_4       RRA.B   R14
            JNC     BRK_5
            CMP     BRKADD, 0(R13)
            JEQ     BRK_52
;
; PREMAHVANE NA VSICHKI TOCHKI
;
; 0001 0000 
BRK_5       RRA.B   R14
            JNC     BRK_6
BRK_52      CLR     0(R13)
            CLR     2(R13)
            CLR     4(R13)
;
; PECHAT NA TOCHKITE NA PREKYSWANE
;
; 0010 0000
BRK_6       RRA.B   R14
            JNC     BRK_E
            MOV     #BUFF+1, BUFPNT
            MOV     @R13, R12
            PUSH    R13
            CALL    #OUT4HS
            POP     R13
            MOV.B   #5, BUFF
            CALL    #PDATABF
;
; ZATVARQNE NA CIKYLA
;
BRK_E       ADD     #6, R13
            CMP     #BRKINS, R13
            JEQ     BRK_09
            JMP     BRK_0
;
; PRIKLJUCHVANE NA IZPYLNENIETO I IZHOD
;
BRK_09      ;MOV 
            POP     R12 ; !!??
            RET
                        
BRK_DO      PUSH    R2
            MOV     2(SP), TEMP2
            SUB     #04h, TEMP2
            POP     R2
            CALL    #PRINT
            BR      #CONTRL
SWI         DW      0x12B0 ; OPCODE OF CALL(IMM)
            DW      BRK_DO
;------------------------------------------------------------------------------
; PODPROGRAMA ZA RAZPECHATVANE NA SYDYRJANIETO NA REGISTRITE
;------------------------------------------------------------------------------
PRINT       EQU     $
            PUSH    R15
            PUSH    R14
            PUSH    R13
            PUSH    R12
            PUSH    R11
            PUSH    R10
            PUSH    R9
            PUSH    R8
            PUSH    R7
            PUSH    R6
            PUSH    R5
            PUSH    R4
            PUSH    R3
            PUSH    R2
            PUSH    R1
            PUSH    TEMP2
            MOV     #0, R15
            MOV     #BUFF+1, BUFPNT            
PRNTREG     MOV     #2, R14
            mov     #REGS+1, R13
            MOV     BUFPNT, R12
            ;DEC     R12
            ;DEC     R12
            call    #MVTX2
            MOV     R15, R12
            CALL    #OUTHR
            CALL    #OUTSPC
            MOV     SP, R13
            RLA     R15
            ADD     R15, R13
            RRA     R15
            INC     R15
            INC     R13
            CALL    #OUT2H
            DEC     R13
            DEC     R13
            CALL    #OUT2H
            CMP     #10h, R15
            JNE     PRNTREG
            MOV.B   #80h, BUFF
            CALL    #PDATABF
            POP     R12           ; R0
            POP     R12           ; R1
            POP     R12           ; R2
            POP     R12           ; R3
            POP     R4
            POP     R5
            POP     R6
            POP     R7
            POP     R8
            POP     R9
            POP     R10
            POP     R11
            POP     R12
            POP     R13
            POP     R14
            POP     R15
            RET
;------------------------------------------------------------------------------
;           Bazisna sistema za in/out
;------------------------------------------------------------------------------ 


;------------------------------------------------------------------------------
;           Bazisna sistema za input
;------------------------------------------------------------------------------
;
;           Priemane na syobstenie v bufera
;

PSHBUF      ;CLR.B     &SOHCNT
RX_HEADER   CLR.B     &CHSUM
            CALL      #INCHBF
            JC        PSHBUF
            CMP.B     #SOH, R12          ; START NA SYOBSTENIE
            JNE       PSHBUF
            MOV.B     R12, &(BUFF-4)
            CALL      #INCHBF            ; ADRES NA POLUCHATEL
            JC        PSHBUF
            CMP.B     #SELFAD, R12       ; SOBSTVEN ADRES
            JNE       PSHBUF
            MOV       #BUFF+1, BUFPNT
            MOV.B     R12, &(BUFF-2)
            CALL      #INCHBF            ; ADRES NA PODATEL
            JC        PSHBUF
            MOV.B     R12, &(BUFF-3)
            CALL      #INCHBF            ; CVETNOST
            JC        PSHBUF
            MOV.B     R12, &(BUFF-1)
            
            CALL      #INCHBF            ; DYLJINA /MLADSHA/
            JC        PSHBUF         
            MOV.B     R12, BUFF
            MOV.B     R12, TRCNT

RX_DATA     TST.B     &TRCNT
            JEQ       REC_CH
            CALL      #INCHBF
            JC        PSHBUF
            PUSH      R13
            MOV       BUFPNT, R13 
            MOV.B     R12, 0(R13)
            CMP       #BUF_END, R13
            JEQ       RX_DATA
            INC       R13
            MOV       R13, BUFPNT
            POP       R13
            DEC.B     TRCNT
            JMP       RX_DATA
REC_CH      MOV.B     &CHSUM, &TEMP
            CALL      #INCHBF            ; CHECKSUM /MLADSHA/
            JC        PSHBUF
            CMP.B     R12, &TEMP
            ;JNE       PSHBUF
            CALL      #INCHBF            ; PRIEMANE NA #OET
            JNC       SENDACK
            RET
            
            ; IZPRASTANE NA POTVYRJDENIE
            
SENDACK     equ       $             
            MOV       #ACK, R12
            ;CALL      #SENDSYM
            RET
            
            
            ;
            ; VYVEJDANE NA EDIN SIMVOL
            ;

INCHBF      BIT.B   #URXIFG1,&IFG2          ; USART1 RX buffer ready?
            JZ      INCHBF                  ; Jump if TX buffer not ready  
            MOV.B   &RXBUF1, R12            ; Move character to R12
            ADD.B   R12, &CHSUM             ; Calculate Check Sum
            BIT.B   #PE, &U1RCTL            ; Check for parity error
            CLRC
            RET            

            ;
            ; PRMESTVANE NA SYOBSTENIE V BUFERA
            ; R13 SYDYRVA ADRESA NA SYOBSTENIETO
            ;
; TESTED            
MOVTXT      MOV     #BUFF, R12
MVTX1       MOV.B   @R13, R14
            INC.B   R14
MVTX2       MOV.B   @R13+, 0(R12)
            INC     R12
            DEC.B   R14
            JNE     MVTX2
            MOV     R12, BUFPNT
            RET
            
            ;
            ; PRIEMANE NA SPACE OT BUFERA
            ;
; TESTED
INSPC       PUSH    R13
            PUSH    R12
            MOV     BUFPNT, R13
            CMP     #BUF_END, R13
            JNE     INSPC1
INSPC2      JMP     NOSPC
INSPC1      MOV.B   #SPC, R12
            CMP.B   @R13+, R12 
            JNE     NOSPC
            MOV     R13, BUFPNT
            POP     R12   
            POP     R13            
            RET                                          
NOSPC       POP     R12   
            POP     R13            
            BR      #CONTRL
;            
INCHS       CALL    #INSPC
;
; POEMANE NA SIMVOL OT BUFERA
; R12 VRYSTA SIMVOLA
;
INCH        PUSH    R13
            MOV     BUFPNT, R13
            CMP     #BUF_END, R13
            JNE     INCHB1
            JMP     NOCH
INCHB1      MOV.B   @R13+, R12
            MOV     R13, BUFPNT
; PREMAHVANE NA RAZLIKATA MEJDU MALKI I GOLEMI BUKVI            
            AND.B   #01111111B, R12
            CMP.B   #'a', R12
            JL      NOSMALL
            CMP.B   #'z', R12
            JGE     NOSMALL
            AND     #11011111b, R12
NOSMALL     POP     R13            
            RET            
NOCH        POP     R13            
            BR      #CONTRL
            
;
; PODPROGRAMA ZA VYVEJDANE NA DVE DVUBAJTOVI CHISLA
; POSLEDOVATELNO V KLETKI BEGA I ENDA
;
;TESTED, VSICHKO RABOTI (BADDRS)
INBADD      CALL    #BADDRS
            MOV     R12, BEGA
            CALL    #BADDRS
            MOV     R12, ENDA
            RET
            
;
; VYVEJDANE NA INTERVAL I NA DVUBAJTOVO CHISLO OT BUFF
;            
BADDRS      CALL    #INSPC
;
; VYVEJDANE NA DVUBAJTOVO CHISLO
; /CHETIRI SHESTNAJSETICHNI CIFRI/
; V R12 
;            
BADDR       PUSH    R13
            CALL    #BYTE_
            MOV.B   R12, R13  
            SWPB    R13
            CALL    #BYTE_
            ADD     R13, R12
            POP     R13
            RET
            
;
BYTES       CALL    #INSPC
;
; VYVEJDANE NA BYTE
; /DVE SHESTNAJSETICHNI CIFRI/
; OT BUFF V R12
;
BYTE_       CALL    #INHEX
BYTE2       PUSH    R13
            RLA     R12
            RLA     R12
            RLA     R12
            RLA     R12
            MOV     R12, R13
            CALL    #INHEX
            ADD     R13, R12      ; IMA OSTE NESTO NAPISANO V KODA !!???     
            POP     R13
            RET ; NE E DOVYRSHENO
            
;
; VYVEJDANE NA EDNA HEX CIFRA
; V R12
;
INHEX       CALL    #INCH
INHEX2      SUB.B   #'0', R12
            JN      NOINHEX
            CMP.B   #10, R12
            JN      IN1HG
            CMP.B   #'A'-'0', R12
            JN      NOINHEX
            CMP.B   #'F'-'0'+1, R12 ;!!??
            JGE     NOINHEX
            SUB.B   #'A'-'9'-1, R12
IN1HG       RET
NOINHEX     CLR     R12
            RET            

ASC_BIN            ; NE E DOPISANO, KATO RAZBERA ZA KAKWO E, TOGAVA

;------------------------------------------------------------------------------
; BAZISNA SISTEMA ZA IZHOD
;------------------------------------------------------------------------------
;
; IZVEJDANE NA BUFERA ZAVYRSHVAST S OET ILI ETX
;
;
PDATABF     EQU     $
; PREDAVANE NA BUFERA
TRHEAD      EQU     $
            CLR.B   SOHCNT
            MOV.B   #C_MODE, RT_MODE
            CLR.B   CHSUM
            MOV     #TRBYTE, R14        ; V R14 SE SYHRANQVA ADRESA NA TRBYTE (PESTI SE MQSTO)
            MOV     #BUFF-4, R15        ; V R15 E POINTERA ZA BUFFERA
            MOV.B   @R15+, R12      ; R15=BUFF-4
            CALL    R14             ; START NA TEKST
            MOV.B   @R15+, R12      ; R15=BUFF-3
            CALL    R14             ; POLUCHATEL
            MOV.B   @R15+, R12      ; R15=BUFF-2
            CALL    R14             ; PODATEL
            MOV.B   @R15+, R12      ; R15=BUFF-1
            CALL    R14             ; CVETNOST
            
            IF      FLAG5=BIN 
            MOV.B   #D_MODE, RT_MODE
            ENDIF
            
            MOV.B   @R15+, R12      ; R15=BUFF
            MOV.B   R12, TRCNT      ;
            CALL    R14
TRDATA      EQU     $

TRDT3       TST.B   TRCNT
            JEQ     TRCHSM
            
TRDT4       DEC.B     TRCNT
            MOV.B   @R15+, R12
            CALL    R14             ; DATA
            JMP     TRDATA                             
            
TRCHSM      MOV.B   CHSUM, R12
            CALL    #SENDSYM        ; CHECKSUM
TRENDT      EQU     $
            
            IF      FLAG5=BIN
            MOV.B   #C_MODE, RT_MODE
            ENDIF                          
            
            MOV     #EOT, R12
            CALL    #SENDSYM
; PRIEMANE NA POTVYRJDENIE
ACKRCV      PUSH    R12             
            CALL    #INCHBF
            POP     R12  
            RET
                                               
TRBYTE      ADD.B   R12, CHSUM
            CALL    #SENDSYM
TRDRET      RET

;
; IZVEJDANE NA SIMVOLEN NIZ
; UKAZAN OT R13 I ZAVYRSHVAST
; S EOT ILI ETX
;
PDATA       MOV.B   @R13+, R12
PDATA1      CALL    #OUTCH
            CMP.B   #EOT, R12
            JEQ     PDATA2
            CMP.B   #ETX, R12
            JEQ     PDATA2
            JMP     PDATA
PDATA2      RET
;
; IZVEJDANE NA DVA BAJTA, UKAZANI OT R13 S INTERVAL
; SLED NEGO, MODIFIKACIQ NA UKAZALECA
;            
OUT4HS      ADD     #01h, R13
            CALL    #OUT2H
            SUB     #02h, R13
            CALL    #OUT2H
            ADD     #01h, R13
            MOV.B   #SPC, R12
            JMP     OUTCHBF
            
;
; IZVEJDANE NA BAJTA, UKAZAN OT R13 S INTERVAL
; SLED NEGO, MODIFIKACIQ NA UKAZALECA
;
OUT2HS      CALL    #OUT2H
;
; IZVEJDANE NA EDIN INTERVAL
;
OUTSPC      MOV.B   #SPC, R12
            JMP     OUTCHBF

; VHODEN PARAM E R12
OUHEX       AND.B   #00001111B, R12
            ADD.B   #'0', R12
            CMP.B   #'9'+1, R12
            JN      OUHEXE
            ADD.B   #'A'-'9'-1, R12
OUHEXE      RET

;
BIN_ASC     ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NE SE ZNAE OSTE

;
; IZVEJDANE NA DVE SHESTNAJSETICHNI CIFRI - BAJT
; UKAZAN OT R13 S MODIFICIRANE NA UKAZALECA
;            
OUT2H       MOV.B   @R13+, R12

; IZVEJDANE NA DVE SHESTNAJSETICHNI CIFRI - SYDYRJANIETO NA R12
OUT2HA      PUSH    R12
            CALL    #OUTHL
            POP     R12
            JMP     OUTHR
;
; IZVEJDANE NA STARSHATA TETRADA KATO HEX CIFRA
;            
OUTHL       RRA.B   R12
            RRA.B   R12
            RRA.B   R12
            RRA.B   R12
;
; IZVEJDANE NA MLADSHATA TETRADA KATO HEX CIFRA
;              
OUTHR
            CALL    #OUHEX
;
; IZVEJDANE NA EDIN SIMVOL V BUFF
;                        
OUTCHBF
            PUSH    R13
            MOV     BUFPNT, R13
            MOV.B   R12, 0(R13)
            INC     R13
            MOV     R13, BUFPNT
            POP     R13
            RET
            
; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
; ZA NUJDITE NA SIMULACIQTA ZA SEGA
;
SENDSYM     bit.b   #UTXIFG1,&IFG2          ; USART1 RX buffer ready?
            jz      SENDSYM
            mov.b   R12, &TXBUF1
            ;bis.b   #UTXIFG1,&IFG2
            RET
OUTCH       
            ;MOV       R12, R4
            ;bit.b   #UTXIFG1,&IFG2          ; USART1 RX buffer ready?
            ;jz      OUTCH
            mov.b   R12, &TXBUF1
            RET                                                                                
;------------------------------------------------------------------------------
;           Textove na syobsteniq
;------------------------------------------------------------------------------

NOCMD       DB      22
            DB      'NOT RECOGNIZED COMMAND'
IDENT       DB      22
            DB      'MSP430 SERVICE PROGRAM'            
PROMPT      DB      1
            DB      '>'    
EQUALS      DB      5
            DB      'EQUAL'
NOTEQ       DB      9
            DB      'NOT EQUAL'
REGS        DB      2
            DB      ' ';0Dh ; CR
            DB      'R'
            DB      ' '                         
;------------------------------------------------------------------------------
;           Bazisna sistema za rabota s FLASH
;           Fully tested
;------------------------------------------------------------------------------           
;           Iztrivane na sektor
;           r13 sydyrja adresa na kletka ot sectora za iztrivane
DELSEC      EQU     $
T_Busy1     BIT     #BUSY,&FCTL3
            JNZ     T_Busy1
Timing      mov.w   #FWKEY+FSSEL0+FN4,&FCTL2  ; *Timing generator = MCLK/33
Erase_Seg   mov.w   #FWKEY,&FCTL3             ; Lock = 0
            mov.w   #FWKEY+ERASE,&FCTL1       ; Erase bit = 1
            CLR     0(R13)                    ; Dummy write to SegA to erase
T_Busy2     BIT     #BUSY,&FCTL3
            JNZ     T_Busy2
            XOR     #(FXKEY+LOCK),&FCTL3
            RET

                        
;     TEMP21 - adresa na nachaloto na dannite
;     TEMP22 - noviq adres na dannite
;     TEMP2  - dyljina na dannite
SECTORWR    EQU     $
MOVTORAM    EQU     $
            PUSH    R12
            PUSH    R13
            PUSH    R14
            mov.w   #FWKEY+FSSEL0+FN4,&FCTL2  ; *Timing generator = MCLK/33
            ;MOV     TEMP21, R13
            MOV     TEMP22, R14
            MOV     R14, R13
            ;AND     #0x01FF, R14
            AND     #0xFE00, R13
            MOV     #0x0400, R12
MOV1        CMP     R13, R14
            JEQ     MOV2            
            MOV.B   @R13+, 0(R12)
            INC     R12
            JMP     MOV1
MOV2        MOV     TEMP2, R13 ; V R13 SE SYHRANQVA DYLJINATA NA MASIVA
            MOV     TEMP21, R14
MOV2_1      CMP     #0, R13
            JEQ     MOV3
            MOV.B   @R14+, 0(R12)
            DEC     R13
            INC     R12
            JMP     MOV2_1
MOV3        MOV     TEMP22, R13
            ADD     TEMP2, R13
MOV3_1      MOV.B   @R13+, 0(R12)
            INC     R12
            CMP     #0X0600, R12
            JNE     MOV3_1
DELFLASH    MOV     TEMP22, R13
            CALL    #DELSEC
MOVTOFLASH  mov.w   #FWKEY,&FCTL3                         ; Lock = 0
            mov.w   #FWKEY+WRT,&FCTL1
            AND     #0xFE00, R13
            MOV     #0x0400, R12
MOV4        BIT     #BUSY,&FCTL3
            JNZ     MOV4
            MOV.B   @R12+, 0(R13)
            INC     R13   
            CMP     #0x0600, R12
            JNE     MOV4
            mov.w   #FWKEY+LOCK,&FCTL3      ; Lock = 1
            POP     R14     
            POP     R13
            POP     R12
            RET

;------------------------------------------------------------------------------ 
;  User Program
;------------------------------------------------------------------------------  
            ORG      04000h
User        mov.w   #WDTPW+WDTHOLD, &WDTCTL  ; Stop WDT
            dint                            ; General disable interrupts
            
SetupP4     bis.b   #000h,&P4SEL            ; 
            bis.b   #00Fh,&P4DIR            ;
            mov.b   #001h,&P4OUT            ;
SetupP5     bis.b   #000h,&P5SEL            ; 
            bis.b   #0FFh,&P5DIR            ; 
            ;mov.b   DIGIT,&P5OUT            ;
            mov.b   COUNT, R13
Cycle       mov.b   DELAY, R12
            dec     R13
            jnz     Display
            mov.b   COUNT, R13
Display     mov.b   R13, R10
            add     #DIG_TABLE, R10
            mov.b   @R10, &P5OUT
Delay       dec     R12
            jz      Cycle
            mov.w   #00FFFh, R11
Delay1      dec     R11
            jnz     Delay1
            jmp     Delay

DIG_TABLE   DB      03Fh
            DB      006h
            DB      05Bh
            DB      04Fh
            DB      066h
            DB      06Dh
            DB      07Dh
            DB      007h
            DB      07Fh
            DB      07Fh
            DB      06Fh
            DB      077h
            
            ORG     04100h
DELAY       DB      06h
COUNT       DB      09h
;------------------------------------------------------------------------------ 
;           Interrupt Vectors Used MSP430x13x/14x        
;------------------------------------------------------------------------------ 
            ORG     0FFFEh                  ; 
            DW      RESET                   ; POR, ext. Reset, Watchdog
            end
                       
;------------------------------------------------------------------------------ 
;           Interrupt Vectors Used MSP430x13x/14x         
;------------------------------------------------------------------------------ 
;            ORG     0FFFEh                  ; MSP430 RESET Vector
;            DW      RESET                   ; 
;            END     