// #define       DEBUG */

/*--------------------------------------------------------------

        Command line options:

                D - Generate comment with Address and Data values
                B - Force binary source file
                H - Force Intel (tm) hex source file
                X - Add hex offset to Addresses
                S - Change db and dw strings to defb and defw
                A - Change db/defb to ascii macro for text

----------------------------------------------------------------

        Control codes allowed in the CTL file:

        A - Address     Specifies that the word entry is the Address of something for which a label should be generated.
        B - Byte        Binary Eight bit binary Data (DB).
        C - Code        Executable code.
        F - SFR label   Specify name for SFR.
        I - Ignore      Treat as uninitialized space. Will not be dis-assembled as code or anything else.
        K - SFR bit     Specify name for SFR bit.
        L - Label       Generate a label for an Address.
        R - Register    Specify name for register (instead of RB2R5 for example).
        S - Symbol      Generate a symbol for a value.
        T - Text        ASCII text (DB or ASCII Macro).
        W - Word        Binary Sixteen bit binary Data (DW).

        The difference between labels and symbols is that a label refers to a referenced Address,
        whereas a symbol may be used for 8 or 16 bit immediate Data.
        For some opcodes (eg: MOV R2,#xx) only the symbol table will be searched.
        Other opcodes (eg: mov dptr,#) will search
        the label table first and then search the symbol table only if the
        value is not found in the label table.

----------------------------------------------------------------

        Exit codes:

        0 - No Error (good disassembly)
        1 - Insufficient Memory (shouldn't happen)
        2 - File Error (source file not found, or disk full)
        3 - Error in Control File

----------------------------------------------------------------

        Definition of pflg bits:        pflg = 0xFF if not initialized.
                                        pflg = 0 when initialized by input file.

        bit 7           Set if uninitialized space.
        bit 6           Address Data
        bit 5           Word Binary Data
        bit 4           Byte Binary Data
        bit 3           Clear if Address is Referenced (see bit 0)
        bit 2           ASCII Text
        bit 1           Set if split opcode
        bit 0           Set if Address is Referenced (see bit 3)

        Two bits (3 and 0) are needed to flag a referenced Address because
        such a reference could be to initialized or uninitialized code space.
        If we used a single bit, we could not distinguish between
        a reference and uninitialized space. Bit 3 set with bit 0 clear
        indicates that the control file has specified this area as code.

----------------------------------------------------------------

        Definition of Option Table Bits:

        bit 7           Unconditional transfer instruction (JMP, RET, ETC)
        bit 6           11 bit Addressing mode
        bit 5           Relative Addressing mode
        bit 4           Bit Addressing mode
        bit 3           Direct Addressing mode
        bit 2           Immediate Data
        bit 1           3 byte instruction
        bit 0           2 byte instruction

        Option Table Entry = 0 if Single byte OPCODE with no options.

----------------------------------------------------------------*/

#define verno           2
#define revno           50
#define _binBUFsz       65536                           // Code Space for Target CPU 8051/2      
#define byte            unsigned char
#define word            unsigned int
#define tstop           32                              // TAB Stop   
#define astop           56                              // ASCII Stop 
#define istop           16                              // Instruction
#define ostop           24                              // Opernad    
#define xstop           48                              // Hex Stop   

#define GOOD_EXIT       0
#define MEM_ERROR       1
#define FILE_ERROR      2
#define USER_ERROR      3

#define ASCLINE         32                              // Max length of ascii defb line           
#define ASCLIMIT        512                             // Size of ascii buffer                    
#define BYTELINE        8                               // Max length of binary defb line          
#define BYTELIMIT       512                             // Size of byte binary buffer              
#define WORDLINE        6                               // Max length of defw line                 
#define WORDLIMIT       512                             // Size of word binary buffer              
#define NO_DATA         0xFF                            // Pmem uninitialized value                

// binFLG bits: 

#define bF_INIT         0xFF                            // 11111111  Initial Value                            
#define bF_DATA         0x00                            // ........  Data Valid from File                     
#define bF_NOINIT       0x80                            // 1.......  Set if Uninitialized Space               
#define bF_ADRS         0x40                            // .1......  Address Data                             
#define bF_WORD         0x20                            // ..1.....  Word Binary Data                         
#define bF_BYTE         0x10                            // ...1....  Byte Binary Data                         
#define bF_CLREF        0x08                            // ....1...  Clear if Referenced                      
#define bF_ASCII        0x04                            // .....1..  ASCII Text                               
#define bF_SPLIT        0x02                            // ......1.  Set if Split OPCODE                      
#define bF_REF          0x01                            // .......1  Set if Referenced                        

// EA TBL bits: 

#define EA_XFER        0x80                             //  1....... Unconditional Transfer                   
#define EA_11          0x40                             //  .1...... 11 bit Addressing                        
#define EA_REL         0x20                             //  ..1..... Relative Addressing                      
#define EA_BIT         0x10                             //  ...1.... Bit Addressing                           
#define EA_DIR         0x08                             //  ....1... Direct Addressing                        
#define EA_IMM         0x04                             //  .....1.. Immediate Data                           
#define EA_3           0x02                             //  ......1. 3 byte Instruction                       
#define EA_2           0x01                             //  .......1 2 byte Instruction                       

#define EA_MASK        (~EA_XFER)                       // .1111111  Options Mask                             
#define EA_NONE        0                                // ........  Single Byte, No Options                  
#define EA_SIZE        (EA_3   | EA_2)                  // ......11  [03] 02+01     Additional Byte Count     
#define EA_LREF        (EA_IMM | EA_3)                  // .....1.1  [06] 04+02     JMP, LCALL or MOV DPTR    
#define EA_IMM2        (EA_IMM | EA_2)                  // .....11.  [05] 04+01     Immediate Data            
#define EA_DIR2        (EA_DIR | EA_2)                  // ....1..1  [09] 08+01     Direct                    
#define EA_DIR3        (EA_DIR | EA_3)                  // ....1.1.  [0A] 08+02     Direct                    
#define EA_DMM3        (EA_DIR | EA_IMM | EA_3)         // ....111.  [0E] 08+04+02  Dir & Imm                 
#define EA_BIT2        (EA_BIT | EA_2)                  // ...1...1  [11] 10+01     Bit                       
#define EA_REL2        (EA_REL | EA_2)                  // ..1....1  [21] 20+01     Relative                  
#define EA_IR3         (EA_REL | EA_IMM | EA_3)         // ..1..11.  [26] 20+04+02  Rel & Imm                 
#define EA_DR3         (EA_REL | EA_DIR | EA_3)         // ..1.1.1.  [2A] 20+08+02  Rel & Dir                 
#define EA_RELB        (EA_REL | EA_BIT | EA_3)         // ..11..1.  [32] 20+10+02  Rel & Bit                 
#define EA_112         (EA_11  | EA_2)                  // .1.....1  [41] 40+01     11 bit                    

#define EA_REL3        (EA_REL | EA_3)                  // ..1...1.  [22] 20+02     Rel3                      
#define EA_IMM3        (EA_IMM | EA_3)                  // .....11.  [06] 04+02     Imm 3                     

// Symbol Table and Label Table Entries 

struct sym{
                word    adrs;
                struct  sym     *next;
           unsigned int count;
                byte    attb;
                byte    class;
                char    label[];
};

typedef struct sym *SYM_PTR;