c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

sim86_text.cpp (4725B)


      1 /* ========================================================================
      2 
      3    (C) Copyright 2023 by Molly Rocket, Inc., All Rights Reserved.
      4    
      5    This software is provided 'as-is', without any express or implied
      6    warranty. In no event will the authors be held liable for any damages
      7    arising from the use of this software.
      8    
      9    Please see https://computerenhance.com for more information
     10    
     11    ======================================================================== */
     12 
     13 char const *OpcodeMnemonics[] =
     14 {
     15     "",
     16 
     17 #define INST(Mnemonic, ...) #Mnemonic,
     18 #define INSTALT(...)
     19 #include "sim86_instruction_table.inl"
     20 };
     21 
     22 static char const *GetMnemonic(operation_type Op)
     23 {
     24     char const *Result = OpcodeMnemonics[Op];
     25     return Result;
     26 }
     27 
     28 static char const *GetRegName(register_access Reg)
     29 {
     30     char const *Names[][3] =
     31     {
     32         {"", "", ""},
     33         {"al", "ah", "ax"},
     34         {"bl", "bh", "bx"},
     35         {"cl", "ch", "cx"},
     36         {"dl", "dh", "dx"},
     37         {"sp", "sp", "sp"},
     38         {"bp", "bp", "bp"},
     39         {"si", "si", "si"},
     40         {"di", "di", "di"},
     41         {"es", "es", "es"},
     42         {"cs", "cs", "cs"},
     43         {"ss", "ss", "ss"},
     44         {"ds", "ds", "ds"},
     45         {"ip", "ip", "ip"},
     46         {"flags", "flags", "flags"}
     47     };
     48     static_assert(ArrayCount(Names) == Register_count, "Text table mismatch for register_index");
     49     
     50     char const *Result = Names[Reg.Index][(Reg.Count == 2) ? 2 : Reg.Offset&1];
     51     return Result;
     52 }
     53 
     54 static char const *GetEffectiveAddressExpression(effective_address_expression Address)
     55 {
     56     char const *RMBase[] =
     57     {
     58         "",
     59         "bx+si",
     60         "bx+di",
     61         "bp+si",
     62         "bp+di",
     63         "si",
     64         "di",
     65         "bp",
     66         "bx",
     67     };
     68     static_assert(ArrayCount(RMBase) == EffectiveAddress_count, "Text table mismatch for effective_base_address");
     69     char const *Result = RMBase[Address.Base];
     70     return Result;
     71 }
     72 
     73 static b32 IsPrintable(instruction Instruction)
     74 {
     75     b32 Result = !((Instruction.Op == Op_lock) ||
     76                    (Instruction.Op == Op_rep) ||
     77                    (Instruction.Op == Op_segment));
     78     
     79     return Result;
     80 }
     81 
     82 static void PrintInstruction(instruction Instruction, FILE *Dest)
     83 {
     84     u32 Flags = Instruction.Flags;
     85     u32 W = Flags & Inst_Wide;
     86     
     87     if(Flags & Inst_Lock)
     88     {
     89         if(Instruction.Op == Op_xchg)
     90         {
     91             // NOTE(casey): This is just a stupidity for matching assembler expectations.
     92             instruction_operand Temp = Instruction.Operands[0];
     93             Instruction.Operands[0] = Instruction.Operands[1];
     94             Instruction.Operands[1] = Temp;
     95         }
     96         fprintf(Dest, "lock ");
     97     }
     98     
     99     char const *MnemonicSuffix = "";
    100     if(Flags & Inst_Rep)
    101     {
    102         printf("rep ");
    103         MnemonicSuffix = W ? "w" : "b";
    104     }
    105     
    106     fprintf(Dest, "%s%s ", GetMnemonic(Instruction.Op), MnemonicSuffix);
    107     
    108     char const *Separator = "";
    109     for(u32 OperandIndex = 0; OperandIndex < ArrayCount(Instruction.Operands); ++OperandIndex)
    110     {
    111         instruction_operand Operand = Instruction.Operands[OperandIndex];
    112         if(Operand.Type != Operand_None)
    113         {
    114             fprintf(Dest, "%s", Separator);
    115             Separator = ", ";
    116             
    117             switch(Operand.Type)
    118             {
    119                 case Operand_None: {} break;
    120                 
    121                 case Operand_Register:
    122                 {
    123                     fprintf(Dest, "%s", GetRegName(Operand.Register));
    124                 } break;
    125                 
    126                 case Operand_Memory:
    127                 {
    128                     effective_address_expression Address = Operand.Address;
    129 
    130                     if(Instruction.Operands[0].Type != Operand_Register)
    131                     {
    132                         fprintf(Dest, "%s ", W ? "word" : "byte");
    133                     }
    134                     
    135                     if(Flags & Inst_Segment)
    136                     {
    137                         printf("%s:", GetRegName({Address.Segment, 0, 2}));
    138                     }
    139                     
    140                     fprintf(Dest, "[%s", GetEffectiveAddressExpression(Address));
    141                     if(Address.Displacement != 0)
    142                     {
    143                         fprintf(Dest, "%+d", Address.Displacement);
    144                     }
    145                     fprintf(Dest, "]");
    146                 } break;
    147                 
    148                 case Operand_Immediate:
    149                 {
    150                     fprintf(Dest, "%d", Operand.ImmediateS32);
    151                 } break;
    152                 
    153                 case Operand_RelativeImmediate:
    154                 {
    155                     fprintf(Dest, "$%+d", Operand.ImmediateS32);
    156                 } break;
    157             }
    158         }
    159     }
    160 }