#ifndef NMon_Cmd_h
#define NMon_Cmd_h
/*******************************************************************************
NMon - Nohau Monitor Commands

Creator: Sten Larsson
$Header: /NCARM_Misc/Monitor/LdLpc210x/NMon_Cmd.h 65    7/22/03 5:42p Stenl $
--------------------------------------------------------------------------------
DO NOT MODIFY THIS FILE - IT IS A PART OF NMON (Nohau Monitor) AND MAY BE 
UPDATED IN FUTURE RELEASES OF SEEHAU.

This computer software is owned by Nohau Corp, and is protected by U.S. 
copyright laws and other laws and by international treaties. Customers of Nohau
emulators/debuggers are allowed include it in their products for the purpose of 
enhancing the debug capabilities of Nohau products. It is not allowed to use it 
for any other purpose. The source code may not be provided or otherwise made 
available to third parties, except with the purpose of using it with Nohau 
tools. Nohau may elect to grant exceptions to the above statement in a written 
agreement.

Copyright  ©  2002, Nohau Corporation. All Rights Reserved.
Copyright (c) 2002, Nohau Corporation. All Rights Reserved.
*******************************************************************************/
/*
The Nohau Monitor communication is basically master/slave - the debugger is the 
master and will always initate communication, and the target is the slave. 

However, there is a mode - "PUT MODE" - in which the target outputs all data
the the "NPut.c" module wants to send to the debugger. No commands can be sent
while in Put mode, except to continue or exit PUT MODE.

Only one request will be handled at a time - ie all requests received while
processing one command are ignored, except for:

   * NMON_STATUS - return status information.

Message format
--------------
A message is always a multiple of 32 bits.

The command is always 16 bits wide and it has always at least a 16 bit 
parameter. When the parameter is undefined, it shall be sent as 0.

The two MSB bits of the command have a special meaning:
   + 0x8000 - if set, number of bytes following the first 32bits are in the
              lower 16 bits.
   + 

General request format:
   cmd_16         bits: 31-0
   par_16         bits: 15-0
   cnt_14         bits: 13-0   Number of bytes in request following the 32 bits
   parHI_8        bits: 15-8
   parLO_8        bits: 7-0
   
General response format:
   rsp_32         bits: 31-0
   rspHI_8  MSB   bits: 31-24 
   rspMD_8  MSB   bits: 23-16 - bit0 is 1 when NPut is empty
                              - bit1 is 1 when NGet is empty (and enabled)
   rspLO_16 LSB   bits: 15-0
   
Standard Response Format "rspSTD":
   rspHI_8  : status NSTAT_DONE etc
   rspMD_8  : 0
   rspLO_16 : 0
   
General parameters:
   * addr_start = 32 bit address - first (lowest) that shall be affected
   * addr_last  = 32 bit address - last address that shall be affected
                                   For mem read/write set same as addr_start
   * count      = number data bytes that follows the first 32 bits. This is 
                  always a 14 bit value.  Bit 15-14 shall be sent as 0 if not 
                  used.
                  
*/

#define NRSP_NPUT_EMPTY 0x00010000
#define NRSP_NGET_EMPTY 0x00020000

/*##############################################################################
## COMMANDS
##############################################################################*/

#define NCMD32(x) ((x) << 16)

                /*--------------------------------------------------------------
                ** Status operations & General Infomation 
                **------------------------------------------------------------*/
                /* Get current status.
                ** Response: rspLO_16 - current value of NMon_Count. */
#define NMON_STATUS           0x0001        

                /* CPU/Family.
                ** Response: rspLO_16 - 16 bit enum value, NCPU_ARM etc.    */
#define NMON_CPU              0x000A

                /* Implemented features.
                ** Response: rspLO_16 - features as bitmask, NFEAT_NPUT etc.*/
#define NMON_FEATURES         0x000B        
   #define NFEAT_NPUT            0x0001     /* Defs for 16bit response to...  */
   #define NFEAT_NGET            0x0002     /* ...NMON_FEATURES. Each bit...  */
   #define NFEAT_NSRV            0x0004     /* ...represent a feature present.*/

                /* Nohau Monitor version.
                ** Response: rspLO_16: version number as 16 bit value      */
#define NMON_VERSION          0x000C


                /*--------------------------------------------------------------
                ** Deal with NPut & NGet
                **------------------------------------------------------------*/
                /* The target will send printf() output data when in "PUT Mode".
                ** There will not be any response to NMON_PUTMODE_xxx - all 
                ** communication from the will contain data to display.  
                **
                ** NMON_PUTMODE_CHAR1 is same as NMON_PUTMODE, but includes
                ** one data byte for getc() etc in parLO_8.
                ** NMON_PUTMODE_CHAR2 is same, but has next data in parHI_8. 
                **
                ** Any recognized or unrecognized command will cause exit from
                ** PUT MODE.
                */
#define NMON_PUTMODE          0x0020
#define NMON_PUTMODE_CHAR1    0x0021       /* parLO_8 : data  */
#define NMON_PUTMODE_CHAR2    0x0022       /* parLO_8 : 1:st data, parLO_8 : 2:nd. */

                /* End put mode - this command does not cause any response, so
                ** that if there is a received response after sending this, it
                ** will be a put mode message. */
#define NMON_PUTMODE_END      0x0023

                /* Get number of empty positions in the NGet.c buffer
                ** Response: rspLO_16 - number empty positions. */
#define NMON_GETBUF_EMPTYCNT  0x0024

                /*--------------------------------------------------------------
                ** General Set operations
                **------------------------------------------------------------*/
                /* 
                ** Set operations do not respond.    
                ** REMEMBER - HIGHEST BIT SET HAS SPECIAL MEANING
                */
                
                /* Set data space.                                            */
#define NMON_SET_SPACE        0x0101        /* par_16 : space                 */

                /* Set low and high address (in that order).                  */
#define NMON_SET_ADDR         0x8102        /* cnt_14, addr:32                */
#define    NMON_SET_ADDR_SIZE    0x0004     /* Size of command  - 4 bytes     */

                /* Set address limits - both set to zero = don't use          */
#define NMON_SET_LIMIT        0x0103        /* cnt_14, addr:32 addr:32        */
#define    NMON_SET_LIMIT_SIZE    0x0008    /* Size of command  - 8 bytes     */

                /* Set data algorithm addresses.                              */
#define NMON_SET_ALGO_WRITE   0x8105
#define NMON_SET_ALGO_ERASE   0x8106
#define NMON_SET_ALGO_READ    0x8107        /* Currently not used. */

                /* Set base address - address to erase chip for etc.          */
#define NMON_SET_BASE         0x8108 

                /* Set 32-bit target specific parameters.                     */
#define NMON_SET_PARAM_1      0x8109
#define NMON_SET_PARAM_2      0x810A
#define NMON_SET_PARAM_3      0x810B

                /*--------------------------------------------------------------
                ** General Get operations
                **------------------------------------------------------------*/

                /* Get buffer size for mem operations
                ** Response: rspLO_16 - 16 bit enum value.                    */
#define NMON_GET_BUFSIZE      0x0200

                /* Get address for verify error (when occured).
                ** Response: rsp_32 - address value. */
#define NMON_GET_VERIFYADDR   0x0201

                /*--------------------------------------------------------------
                ** Memory operations
                **------------------------------------------------------------*/
                /* Memory operation always operate on current NSPACE and ADDR. 
                */
/* #define NMON_MEM_CS        0x0301        */ /* par:16                      */
/* #define NMON_MEM_FILL      0x0302|0x8000 */ /* cnt:14 data:32              */
#define NMON_MEM_ERASE        0x0303           /* par:16                      */
#define NMON_MEM_READ         0x0304           /* cnt:14                      */
#define NMON_MEM_PRE_ERASE    0x0305           /* par:16                      */
#define NMON_MEM_POST_ERASE   0x0306           /* par:16                      */

                /* Before writing, send PRExx, and after send POSTxx. */
// #define NMON_MEM_PREWRITE     0x030E
// #define NMON_MEM_POSTWRITE    0x030F

                /* For mem WRITE, the cnt value is masked with the command bit. 
                ** The target address is incremented automatically for each data
                ** written. */
#define NMON_MEM_WRITE        0x4000        /* cnt:14 cs:16 data data data... */


/*##############################################################################
## COMMANDS FROM TARGET WHEN IN PUT MODE
##############################################################################*/
/*
In "PUT MODE" the target is free to send data bytes when it so desires. It is 
possible to embed "commands" from the target to the debugger in that data 
stream. The commands are 32 bits wide, however they may not be always be 
aligned on 32 bit boundries. Also, for some commands, parameters are embedded
as a part of the command.
*/

                /* Escape character - when found, and the next character is
                ** also escape, send the escape character. 
                ** However, if the next character is not the escape character,
                ** it is a 32 bit command. */
#define NPUT_ESC_CHAR 0xFF

                /*--------------------------------------------------------------
                ** Deal with the getc() input buffer and the printf() display
                **------------------------------------------------------------*/
                /* The NPut buffer is full - ovverrun has occured. */
#define NPUT_TARG_OVERRUN   0xFF010000  
#define NPUT_TARG_OVERDONE  0xFF020000 

                /* Move the cursor in the Target Console. New position X,Y 
                ** in lower two bytes. */
#define NPUT_TARG_MOVE      0xFF030000  

                /* Input characters from the user are buffered in Seehau. When
                ** PUT Mode, sending NPUT_GET_CLEAR will clear all characters 
                ** in the debugger. */
#define NPUT_TARG_CLRGET    0xFF040000

                /* Clear the screen in the Target Console. */
#define NPUT_TARG_CLRSCR    0xFF050000

                /* Clear the rest of current line in the Target Console. */
#define NPUT_TARG_CLREOL    0xFF060000

                /* Clear the rest of page in the Target Console. */
#define NPUT_TARG_CLREOP    0xFF070000

                /* Set echo on/off for putc() functionality - default is on. 
                ** Lower byte 0=off, otherwise on. */
#define NPUT_TARG_ECHO      0xFF0B0000

                /*--------------------------------------------------------------
                ** Commands used in emulator software
                **------------------------------------------------------------*/
                /* Lower 8 bits: 0=not present, 1=present. */
#define NMON_PUT_PRESENT   0x00010000
#define NMON_GET_PRESENT   0x00020000

                /*--------------------------------------------------------------
                ** Deal with the errors in the TARGET system.
                **------------------------------------------------------------*/
                /* Send this command before error message put in buffer. The 
                ** lower byte can have an error descriptor.*/
#define NPUT_TARG_ERRSTART 0xFF080000
#define   NERR_GENERAL          0x01
#define   NERR_INTERNAL         0x02  /* Internal error in NMon, NPut etc */
#define   NERR_ASSERT           0x03
#define   NERR_PRECONDITION     0x04
#define   NERR_POSTCONDITION    0x05
#define   NERR_NEVER_BE_HERE    0x06
#define   NERR_NOT_IMPLEMENTED  0x07

                /* Send this command after error message put in buffer. */
#define NPUT_TARG_ERREND   0xFF090000

                /* Transfer a line number in the lower 16 bits. */
#define NPUT_TARG_LINE     0xFF0A0000


/*##############################################################################
## MASKS and OTHER MISC
##############################################################################*/

                /* The lower 16 bits of the 32 bit word contains a parameter.
                ** If the MSB bit is set, the lower 16 bits contain message
                ** size - number of bytes to follow. */
#define NMON_CNT14_BIT   0x8000        /* cnt:14 cs:16 data data data .... */
#define NMON_CNT14_MASK  0x3FFF

#define NMON_CMD_MASK32  0xFFFF0000
#define NMON_PAR_MASK32  0x0000FFFF
#define NMON_PARH_MASK32 0x0000FF00
#define NMON_PARL_MASK32 0x000000FF

                /* Status commands are dealt with specially, so they all have 
                ** the lowest numbers available. NMON_HIGHEST_STATUS indicates
                ** the highest command number for a status type message. */
#define NMON_HIGHEST_STATUS NSTAT_FAIL_TIMEOUT

/*##############################################################################
## COMMANDS
##############################################################################*/

/* 
** Defs for 8 MSB bits in response.
*/
#define NSTAT_BUSY           0x10   /* BUSY, OK = Not yet completed        */
#define NSTAT_OK             0x20   /* OK = Completed.                     */
#define NSTAT_FAIL           0x30   /* FAIL=done, but not ok.              */
#define NSTAT_FAIL_CS        0x40   /* FAILED - checksum not correct.      */
#define NSTAT_FAIL_VERIFY    0x50   /* FAILED - when verifying.            */
#define NSTAT_FAIL_NOIMPL    0x60   /* FAILED - not implemented.           */
#define NSTAT_FAIL_LIMRANGE  0x70   /*          */
#define NSTAT_FAIL_TIMEOUT   0x80

/*##############################################################################
## PREDEFINED MEMORY SPACES
##############################################################################*/

#define NSPC_RAM     0x00FF
#define NSPC_INVALID 0x00FE
#define NSPC_FLASH_0 0x0000
#define NSPC_FLASH_1 0x0001
#define NSPC_FLASH_2 0x0002
#define NSPC_FLASH_3 0x0003
#define NSPC_FLASH_4 0x0004

#define NSPC_COUNT   5       // We have currently up to 5 data spaces, not 
                             // counting RAM.

/*##############################################################################
## SUPPORTED CPUs
##############################################################################*/

#define NCPU_ARM   1 
#define NCPU_BLAZE 2

#endif /* NMon_Cmd_h */
