Advertisement
Dimension

ExportStructure.java

Dec 26th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.66 KB | None | 0 0
  1. package emulator.editor;
  2.  
  3. import emulator.editor.Export.MemoryImage;
  4.  
  5. public abstract class ExportStructure {
  6.     // TODO access:
  7.     // fat pointer, string pointer (start,size)
  8.     // array list
  9.     //
  10.    
  11.     public static interface IField {
  12.        
  13.     }
  14.    
  15.     public final byte[] structure;
  16.     public final int size;
  17.    
  18.     public ExportStructure(int size) {
  19.         this.size = size;
  20.         this.structure = new byte[size];
  21.     }
  22.    
  23.     public static class InterruptDescriptorTableRegister extends ExportStructure {
  24.         public enum Field implements IField {
  25.             LIMIT, // in bytes
  26.             BASE,
  27.         }
  28.  
  29.         public InterruptDescriptorTableRegister() {
  30.             super(6); // 48 bits
  31.            
  32.             //write(Field.LIMIT, 0x800); // 8 * 256
  33.         }
  34.        
  35.         @Override
  36.         public Object read(IField field, int start) {
  37.             switch((Field) field) {
  38.             case LIMIT:
  39.                 {
  40.                     int value = 0;
  41.                     value |= structure[0] << 0; // 0
  42.                     value |= structure[1] << 8; // 8
  43.                     return value;
  44.                 }
  45.             case BASE:
  46.                 {
  47.                     int value = 0;
  48.                     value |= structure[2] << 0; // 16
  49.                     value |= structure[3] << 8; // 24
  50.                     value |= structure[4] << 16; // 32
  51.                     value |= structure[5] << 24; // 40
  52.                     return value;
  53.                 }
  54.             default:
  55.                 assert false;
  56.                 return null;
  57.             }
  58.         }
  59.         @Override
  60.         public void write(IField field, Object object) {
  61.             switch((Field) field) {
  62.             case LIMIT:
  63.                 {
  64.                     int value = (Integer) object;
  65.                     structure[0] = (byte) ((value & 0x000000FF) >>> 0); // 0
  66.                     structure[1] = (byte) ((value & 0x0000FF00) >>> 8); // 8
  67.                 }
  68.                 break;
  69.             case BASE:
  70.                 {
  71.                     int value = (Integer) object;
  72.                     structure[2] = (byte) ((value & 0x000000FF) >>> 0); // 16
  73.                     structure[3] = (byte) ((value & 0x0000FF00) >>> 8); // 24
  74.                     structure[4] = (byte) ((value & 0x00FF0000) >>> 16); // 32
  75.                     structure[5] = (byte) ((value & 0xFF000000) >>> 24); // 40
  76.                 }
  77.                 break;
  78.             default:
  79.                 assert false;
  80.             }
  81.         }
  82.     }
  83.    
  84.     public static class InterruptDescriptor extends ExportStructure {
  85.         public enum Field implements IField {
  86.             SEGMENT,
  87.             OFFSET,
  88.             TYPE_ATTR,
  89.         }
  90.        
  91.         public static class TypeAttr {
  92.             public int type = 0; // constant of Type
  93.             public boolean storageSegment = false;
  94.             public int privilege = 0; // 0 .. 3
  95.             public boolean present = true;
  96.         }
  97.        
  98.         public static class Type { // only 32-bit
  99.             public static final int TASK_GATE       = Integer.parseInt("00000101", 2); // 0x5
  100.             public static final int INTERRUPT_GATE  = Integer.parseInt("00001110", 2); // 0xE, interrupt gate, disable interrupts (fixed stack size)
  101.             public static final int TRAP_GATE       = Integer.parseInt("00001111", 2); // 0xF
  102.         }
  103.        
  104.         public InterruptDescriptor() {
  105.             super(8); // 64 bits
  106.            
  107.             structure[4] = 0; // 32
  108.         }
  109.        
  110.         @Override
  111.         public Object read(IField field, int start) {
  112.             switch((Field) field) {
  113.             case SEGMENT:
  114.                 {
  115.                     int value = 0;
  116.                     value |= structure[2] << 0; // 16
  117.                     value |= structure[3] << 8; // 24
  118.                     return value;
  119.                 }
  120.             case OFFSET:
  121.                 {
  122.                     int value = 0;
  123.                     value |= structure[0] << 0; // 0: low offset
  124.                     value |= structure[1] << 8; // 8
  125.                     value |= structure[6] << 16; // 48: high offset
  126.                     value |= structure[7] << 24; // 56
  127.                     return value;
  128.                 }
  129.             case TYPE_ATTR:
  130.                 {
  131.                     TypeAttr value = new TypeAttr();
  132.                     value.type              = (structure[5] & (byte) Integer.parseInt("00001111", 2)) >>> 0; // 40 .. 43
  133.                     value.storageSegment    = (structure[5] & (byte) Integer.parseInt("00010000", 2)) != 0; // 44
  134.                     value.privilege         = (structure[5] & (byte) Integer.parseInt("01100000", 2)) >>> 5; // 45 .. 46
  135.                     value.present           = (structure[5] & (byte) Integer.parseInt("10000000", 2)) != 0; // 47
  136.                     return value;
  137.                 }
  138.             default:
  139.                 assert false;
  140.                 return null;
  141.             }
  142.         }
  143.         @Override
  144.         public void write(IField field, Object object) {
  145.             switch((Field) field) {
  146.             case SEGMENT:
  147.                 {
  148.                     int value = (Integer) object;
  149.                     structure[2] = (byte) ((value & 0x000000FF) >>> 0); // 16
  150.                     structure[3] = (byte) ((value & 0x0000FF00) >>> 8); // 24
  151.                 }
  152.                 break;
  153.             case OFFSET:
  154.                 {
  155.                     int value = (Integer) object;
  156.                     structure[0] = (byte) ((value & 0x000000FF) >>> 0); // 0: low offset
  157.                     structure[1] = (byte) ((value & 0x0000FF00) >>> 8); // 8
  158.                     structure[6] = (byte) ((value & 0x00FF0000) >>> 16); // 48: high offset
  159.                     structure[7] = (byte) ((value & 0xFF000000) >>> 24); // 56
  160.                 }
  161.                 break;
  162.             case TYPE_ATTR:
  163.                 {
  164.                     TypeAttr value = (TypeAttr) object;
  165.                     structure[5] = 0;
  166.                     structure[5] |= (value.type         & 0x0000000F)   << 0; // 40 .. 43
  167.                     structure[5] |= (value.storageSegment   ? 1 : 0)    << 4; // 44
  168.                     structure[5] |= (value.privilege    & 0x00000003)   << 5; // 45 .. 46
  169.                     structure[5] |= (value.present          ? 1 : 0)    << 7; // 47
  170.                 }
  171.                 break;
  172.             default:
  173.                 assert false;
  174.             }
  175.         }
  176.     }
  177.    
  178.     public static class GlobalDescriptorTableRegister extends ExportStructure {
  179.         public enum Field implements IField {
  180.             SIZE, // entry count
  181.             LINEAR_ADDRESS,
  182.         }
  183.  
  184.         public GlobalDescriptorTableRegister() {
  185.             super(6); // 48 bits
  186.         }
  187.        
  188.         @Override
  189.         public Object read(IField field, int start) {
  190.             switch((Field) field) {
  191.             case SIZE:
  192.                 {
  193.                     int value = 0;
  194.                     value |= structure[0] << 0; // 0
  195.                     value |= structure[1] << 8; // 8
  196.                     value++;
  197.                     return value;
  198.                 }
  199.             case LINEAR_ADDRESS:
  200.                 {
  201.                     int value = 0;
  202.                     value |= structure[2] << 0; // 16
  203.                     value |= structure[3] << 8; // 24
  204.                     value |= structure[4] << 16; // 32
  205.                     value |= structure[5] << 24; // 40
  206.                     return value;
  207.                 }
  208.             default:
  209.                 assert false;
  210.                 return null;
  211.             }
  212.         }
  213.         @Override
  214.         public void write(IField field, Object object) {
  215.             switch((Field) field) {
  216.             case SIZE:
  217.                 {
  218.                     int value = (Integer) object;
  219.                     assert value > 0;
  220.                     value--;
  221.                     structure[0] = (byte) ((value & 0x000000FF) >>> 0); // 0
  222.                     structure[1] = (byte) ((value & 0x0000FF00) >>> 8); // 8
  223.                 }
  224.                 break;
  225.             case LINEAR_ADDRESS:
  226.                 {
  227.                     int value = (Integer) object;
  228.                     structure[2] = (byte) ((value & 0x000000FF) >>> 0); // 16
  229.                     structure[3] = (byte) ((value & 0x0000FF00) >>> 8); // 24
  230.                     structure[4] = (byte) ((value & 0x00FF0000) >>> 16); // 32
  231.                     structure[5] = (byte) ((value & 0xFF000000) >>> 24); // 40
  232.                 }
  233.                 break;
  234.             default:
  235.                 assert false;
  236.             }
  237.         }
  238.     }
  239.    
  240.     public static class GlobalDescriptor extends ExportStructure {
  241.         public enum Field implements IField {
  242.             LIMIT,
  243.             BASE,
  244.             ACCESS_BYTE,
  245.             FLAGS,
  246.         }
  247.        
  248.         public static class AccessByte {
  249.             public boolean accessed = false;
  250.             public boolean readable_writable = false; // code can read (write disallowed regardless), data can write (read allowed regardless)
  251.             public boolean direction_conforming = false; // data grows up, code specified privilege only
  252.             public boolean executable = false;
  253.             public int privilege = 3; // 0 .. 3
  254.             public boolean present = true;
  255.         }
  256.        
  257.         public static class Flags {
  258.             public boolean size = true;
  259.             public boolean granularity = true;
  260.         }
  261.  
  262.         public GlobalDescriptor() {
  263.             super(8); // 64 bits
  264.         }
  265.        
  266.         @Override
  267.         public Object read(IField field, int start) {
  268.             switch((Field) field) {
  269.             case LIMIT:
  270.                 {
  271.                     int value = 0;
  272.                     value |= structure[0] << 0; // 0: I
  273.                     value |= structure[1] << 8; // 8
  274.                     value |= (structure[6] & 0xF) << 16; // 48 .. 51: II
  275.                     return value;
  276.                 }
  277.             case BASE:
  278.                 {
  279.                     int value = 0;
  280.                     value |= structure[2] << 0; // 16: I
  281.                     value |= structure[3] << 8; // 24
  282.                     value |= structure[4] << 16; // 32: II
  283.                     value |= structure[7] << 24; // 56: III
  284.                     return value;
  285.                 }
  286.             case ACCESS_BYTE:
  287.                 {
  288.                     AccessByte value = new AccessByte();
  289.                     value.accessed              = (structure[5] & (byte) Integer.parseInt("00000001", 2)) != 0; // 40
  290.                     value.readable_writable     = (structure[5] & (byte) Integer.parseInt("00000010", 2)) != 0; // 41
  291.                     value.direction_conforming  = (structure[5] & (byte) Integer.parseInt("00000100", 2)) != 0; // 42
  292.                     value.executable            = (structure[5] & (byte) Integer.parseInt("00001000", 2)) != 0; // 43
  293.                     value.privilege             = (structure[5] & (byte) Integer.parseInt("01100000", 2)) >>> 5; // 45 .. 46
  294.                     value.present               = (structure[5] & (byte) Integer.parseInt("10000000", 2)) != 0; // 47
  295.                     return value;
  296.                 }
  297.             case FLAGS:
  298.                 {
  299.                     Flags value = new Flags();
  300.                     value.size          = (structure[6] & (byte) Integer.parseInt("01000000", 2)) != 0; // 54
  301.                     value.granularity   = (structure[6] & (byte) Integer.parseInt("10000000", 2)) != 0; // 55
  302.                     return value;
  303.                 }
  304.             default:
  305.                 assert false;
  306.                 return null;
  307.             }
  308.         }
  309.         @Override
  310.         public void write(IField field, Object object) {
  311.             switch((Field) field) {
  312.             case LIMIT:
  313.                 {
  314.                     int value = (Integer) object;
  315.                     structure[0] = (byte) ((value & 0x000000FF) >>> 0); // 0: I
  316.                     structure[1] = (byte) ((value & 0x0000FF00) >>> 8); // 8
  317.                     structure[6] &= 0xF0;
  318.                     structure[6] |= (byte) ((value & 0x000F0000) >>> 16); // 48 .. 51: II
  319.                 }
  320.                 break;
  321.             case BASE:
  322.                 {
  323.                     int value = (Integer) object;
  324.                     structure[2] = (byte) ((value & 0x000000FF) >>> 0); // 16: I
  325.                     structure[3] = (byte) ((value & 0x0000FF00) >>> 8); // 24
  326.                     structure[4] = (byte) ((value & 0x00FF0000) >>> 16);// 32: II
  327.                     structure[7] = (byte) ((value & 0xFF000000) >>> 24); // 56: III
  328.                 }
  329.                 break;
  330.             case ACCESS_BYTE:
  331.                 {
  332.                     AccessByte value = (AccessByte) object;
  333.                     structure[5] = 0;
  334.                     structure[5] |= (value.accessed             ? 1 : 0)    << 0; // 40
  335.                     structure[5] |= (value.readable_writable    ? 1 : 0)    << 1; // 41
  336.                     structure[5] |= (value.direction_conforming ? 1 : 0)    << 2; // 42
  337.                     structure[5] |= (value.executable           ? 1 : 0)    << 3; // 43
  338.                     structure[5] |= 1                                       << 4; // 44
  339.                     structure[5] |= (value.privilege        & 0x00000003)   << 5; // 45 .. 46
  340.                     structure[5] |= (value.present              ? 1 : 0)    << 7; // 47
  341.                 }
  342.                 break;
  343.             case FLAGS:
  344.                 {
  345.                     Flags value = (Flags) object;
  346.                     structure[6] &= 0x0F;
  347.                     structure[6] |= (value.size         ? 1 : 0) << 6; // 54
  348.                     structure[6] |= (value.granularity  ? 1 : 0) << 7; // 55
  349.                 }
  350.                 break;
  351.             default:
  352.                 assert false;
  353.             }
  354.         }
  355.     }
  356.    
  357.     public static class PageDirectoryEntry extends ExportStructure {
  358.         public enum Field implements IField {
  359.             FLAGS,
  360.             CUSTOM,
  361.             PHYSICAL_ADDRESS,
  362.         }
  363.        
  364.         public static class Flags {
  365.             public boolean present = true;
  366.             public boolean read_write = false;
  367.             public boolean user_supervisor = false;
  368.             public boolean writeThrough = false;
  369.             public boolean cacheDisabled = true;
  370.             public boolean accessed = false;
  371.             public boolean dirty = false;
  372.             public boolean pageSize = false;
  373.             public boolean global = false; // ignored for 4kb
  374.         }
  375.        
  376.         public PageDirectoryEntry() {
  377.             super(4); // 32 bits
  378.         }
  379.        
  380.         @Override
  381.         public Object read(IField field, int start) {
  382.             switch((Field) field) {
  383.             case FLAGS:
  384.                 {
  385.                     Flags value = new Flags();
  386.                     value.present           = (structure[0] & (byte) Integer.parseInt("00000001", 2)) != 0; // 0
  387.                     value.read_write        = (structure[0] & (byte) Integer.parseInt("00000010", 2)) != 0; // 1
  388.                     value.user_supervisor   = (structure[0] & (byte) Integer.parseInt("00000100", 2)) != 0; // 2
  389.                     value.writeThrough      = (structure[0] & (byte) Integer.parseInt("00001000", 2)) != 0; // 3
  390.                     value.cacheDisabled     = (structure[0] & (byte) Integer.parseInt("00010000", 2)) != 0; // 4
  391.                     value.accessed          = (structure[0] & (byte) Integer.parseInt("00100000", 2)) != 0; // 5
  392.                     value.dirty             = (structure[0] & (byte) Integer.parseInt("01000000", 2)) != 0; // 6
  393.                     value.pageSize          = (structure[0] & (byte) Integer.parseInt("10000000", 2)) != 0; // 7
  394.                     value.global            = (structure[1] & (byte) Integer.parseInt("00000001", 2)) != 0; // 8
  395.                     return value;
  396.                 }
  397.             case CUSTOM:
  398.                 {
  399.                     int value = 0;
  400.                     value |= (structure[1] & (byte) Integer.parseInt("00001110", 2)) >>> 1; // 9 .. 11
  401.                     return value;
  402.                 }
  403.             case PHYSICAL_ADDRESS:
  404.                 {
  405.                     int value = 0;
  406.                     value |= (structure[1] & 0xF0) << 8; // 12 .. 15
  407.                     value |= structure[2] << 16; // 16
  408.                     value |= structure[3] << 24; // 24
  409.                     return value;
  410.                 }
  411.             default:
  412.                 assert false;
  413.                 return null;
  414.             }
  415.         }
  416.         @Override
  417.         public void write(IField field, Object object) {
  418.             switch((Field) field) {
  419.             case FLAGS:
  420.                 {
  421.                     Flags value = (Flags) object;
  422.                     structure[0] = 0;
  423.                     structure[0] |= (value.present          ? 1 : 0) << 0; // 0
  424.                     structure[0] |= (value.read_write       ? 1 : 0) << 1; // 1
  425.                     structure[0] |= (value.user_supervisor  ? 1 : 0) << 2; // 2
  426.                     structure[0] |= (value.writeThrough     ? 1 : 0) << 3; // 3
  427.                     structure[0] |= (value.cacheDisabled    ? 1 : 0) << 4; // 4
  428.                     structure[0] |= (value.accessed         ? 1 : 0) << 5; // 5
  429.                     structure[0] |= (value.dirty            ? 1 : 0) << 6; // 6
  430.                     structure[0] |= (value.pageSize         ? 1 : 0) << 7; // 7
  431.                     structure[1] &= (byte) Integer.parseInt("11111110", 2);
  432.                     structure[1] |= (value.global           ? 1 : 0) << 0; // 8
  433.                 }
  434.                 break;
  435.             case CUSTOM:
  436.                 {
  437.                     int value = (Integer) object;
  438.                     structure[1] &= (byte) Integer.parseInt("11110001", 2);
  439.                     structure[1] |= (byte) ((value & (byte) Integer.parseInt("00000111", 2)) << 1); // 9 .. 11
  440.                 }
  441.                 break;
  442.             case PHYSICAL_ADDRESS:
  443.                 {
  444.                     int value = (Integer) object;
  445.                     structure[1] &= 0x0F;
  446.                     structure[1] |= (byte) ((value & 0x0000F000) >>> 8); // 12 .. 15
  447.                     structure[2] = (byte) ((value & 0x00FF0000) >>> 16); // 16
  448.                     structure[3] = (byte) ((value & 0xFF000000) >>> 24); // 24
  449.                 }
  450.                 break;
  451.             default:
  452.                 assert false;
  453.             }
  454.         }
  455.     }
  456.    
  457.     /*public static class TaskSegmentDescriptor extends ExportStructure {
  458.         public enum Field implements IField {
  459.             LIMIT,
  460.             BASE,
  461.             FLAGS,
  462.            
  463.         }
  464.        
  465.         public TaskSegmentDescriptor(int size) {
  466.             super(8);
  467.         }
  468.        
  469.         @Override
  470.         public Object read(IField field, int start) {
  471.             return null;
  472.         }
  473.         @Override
  474.         public void write(IField field, Object object) {
  475.            
  476.         }
  477.     }
  478.    
  479.     public static class TaskSegment extends ExportStructure {
  480.         public enum Field implements IField {
  481.             PREVIOUS_TASK,
  482.             ESP0,
  483.             ESP1,
  484.             ESP2,
  485.             SS0,
  486.             SS1,
  487.             SS2,
  488.             CR3,
  489.             EIP,
  490.             EFLAGS,
  491.             EAX,
  492.             ECX,
  493.             EDX,
  494.             EBX,
  495.             ESP,
  496.             EBP,
  497.             ESI,
  498.             EDI,
  499.             ES,
  500.             CS,
  501.             SS,
  502.             DS,
  503.             FS,
  504.             GS,
  505.             LDT_SELECTOR,
  506.             IO_MAP_BASE,
  507.         }
  508.        
  509.         public TaskSegment(int size) {
  510.             super(1234);
  511.         }
  512.        
  513.         @Override
  514.         public Object read(IField field, int start) {
  515.             assert false;
  516.             return null;
  517.         }
  518.         @Override
  519.         public void write(IField field, Object object) {
  520.             assert false;
  521.         }
  522.     }*/
  523.  
  524.     public abstract Object read(IField field, int start); // load structure field from memory
  525.     public abstract void write(IField field, Object object); // write structure field to memory
  526. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement