c-programming

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

sim86.h (2437B)


      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 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <stdint.h>
     16 #include <assert.h>
     17 
     18 typedef uint8_t u8;
     19 typedef uint16_t u16;
     20 typedef uint32_t u32;
     21 typedef uint64_t u64;
     22 
     23 typedef int8_t s8;
     24 typedef int16_t s16;
     25 typedef int32_t s32;
     26 typedef int64_t s64;
     27 
     28 typedef int32_t b32;
     29 
     30 #define ArrayCount(Array) (sizeof(Array) / sizeof((Array)[0]))
     31 
     32 enum operation_type
     33 {
     34     Op_None,
     35 
     36 #define INST(Mnemonic, ...) Op_##Mnemonic,
     37 #define INSTALT(...)
     38 #include "sim86_instruction_table.inl"
     39     
     40     Op_Count,
     41 };
     42 
     43 enum instruction_flag
     44 {
     45     Inst_Lock = (1 << 0),
     46     Inst_Rep = (1 << 1),
     47     Inst_Segment = (1 << 2),
     48     Inst_Wide = (1 << 3),
     49 };
     50 
     51 enum register_index
     52 {
     53     Register_none,
     54     
     55     Register_a,
     56     Register_b,
     57     Register_c,
     58     Register_d,
     59     Register_sp,
     60     Register_bp,
     61     Register_si,
     62     Register_di,
     63     Register_es,
     64     Register_cs,
     65     Register_ss,
     66     Register_ds,
     67     Register_ip,
     68     Register_flags,
     69     
     70     Register_count,
     71 };
     72 
     73 enum effective_address_base
     74 {
     75     EffectiveAddress_direct,
     76     
     77     EffectiveAddress_bx_si,
     78     EffectiveAddress_bx_di,
     79     EffectiveAddress_bp_si,
     80     EffectiveAddress_bp_di,
     81     EffectiveAddress_si,
     82     EffectiveAddress_di,
     83     EffectiveAddress_bp,
     84     EffectiveAddress_bx,
     85     
     86     EffectiveAddress_count,
     87 };
     88 
     89 struct effective_address_expression
     90 {
     91     register_index Segment;
     92     effective_address_base Base;
     93     s32 Displacement;
     94 };
     95 
     96 struct register_access
     97 {
     98     register_index Index;
     99     u8 Offset;
    100     u8 Count;
    101 };
    102 
    103 enum operand_type
    104 {
    105     Operand_None,
    106     Operand_Register,
    107     Operand_Memory,
    108     Operand_Immediate,
    109     Operand_RelativeImmediate,
    110 };
    111 
    112 struct instruction_operand
    113 {
    114     operand_type Type;
    115     union
    116     {
    117         effective_address_expression Address;
    118         register_access Register;
    119         u32 ImmediateU32;
    120         s32 ImmediateS32;
    121     };
    122 };
    123 
    124 struct instruction
    125 {
    126     u32 Address;
    127     u32 Size;
    128     
    129     operation_type Op;
    130     u32 Flags;
    131     
    132     instruction_operand Operands[2];
    133 };