Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. // version 0.01
  2. // Author: Pavel Iakovlev
  3.  
  4.  
  5. #pragma option OST
  6. #pragma option ON
  7. #pragma option cri-
  8. #pragma option -CPA
  9. #initallvar 0
  10. #jumptomain FALSE
  11.  
  12. #startaddress 0x10000
  13.  
  14. #code32 TRUE
  15.  
  16. char   os_name[8]   = {'M','E','N','U','E','T','0','1'};
  17. dword  os_version   = 0x00000001;
  18. dword  start_addr   = #main;
  19. dword  final_addr   = #______STOP______+32;
  20. dword  alloc_mem    = 20000;
  21. dword  x86esp_reg   = 20000;
  22. dword  I_Param      = #param;
  23. dword  I_Path       = #program_path;
  24. char param[4096] ={0};
  25. char program_path[4096] = {0};
  26.  
  27. dword test_bytecode = "\x05\x10\x82\xe2\x07\x30\x82\xe2\x03\x20\x81\xe0"; // test opcode arm
  28.  
  29. struct _reg // registers arm
  30. {
  31.         dword r0;
  32.         dword r1;
  33.         dword r2;
  34.         dword r3;
  35.         dword r4;
  36.         dword r5;
  37.         dword r6;
  38.         dword r7;
  39.         dword r8;
  40.         dword r9;
  41.         dword r10;
  42.         dword r11;
  43.         dword r12; // (Intra-Procedure-call scratch register)
  44.         dword r13; // (Stack Pointer)
  45.         dword r14; // (Link Register)
  46.         dword r15; // PC (Program Counter)
  47. };
  48.  
  49. _reg reg = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // clear and init registers
  50. dword REG = #reg;
  51.  
  52. struct _flags
  53. {
  54.         byte negative;
  55.         byte zero;
  56.         byte carry;
  57.         byte overflow;
  58. };
  59.  
  60. _flags flags = {0,0,0,0}; // clear and init flags
  61.  
  62. struct _mode
  63. {
  64.         byte User;
  65.         byte FastInterrupt;
  66.         byte Interrupt;
  67.         byte Supervisor;
  68. };
  69.  
  70. _mode mode = {0,0,0,0}; // processor mode
  71.  
  72. struct _mask
  73. {
  74.         byte IRQ;
  75.         byte FIRQ;
  76. };
  77.  
  78. _mask mask = {0,0}; // processor mask
  79.  
  80. void main()
  81. {
  82.  
  83.         callOpcode(#test_bytecode,3);
  84.        
  85.         EAX = -1;
  86.         $int 0x40;
  87. }
  88.  
  89. dword callOpcode(dword binary, lengthInstruction)
  90. {
  91.         dword command = 0;
  92.         dword PC = 0;
  93.         byte flag = 0;
  94.         byte pMask = 0;
  95.         byte pMode = 0;
  96.         while(lengthInstruction)
  97.         {
  98.                 PC    = reg.r15 >> 2 & 0xFFFFFF;
  99.                 flag  = reg.r15 >> 28;
  100.                 pMask = reg.r15 >> 26;
  101.                
  102.                 flags.negative = flag & 0x8;
  103.                 flags.zero     = flag & 0x4;
  104.                 flags.carry    = flag & 0x2;
  105.                 flags.overflow = flag & 0x1;
  106.                
  107.                 mask.IRQ  = pMask & 0x2;
  108.                 mask.FIRQ = pMask & 0x1;
  109.                
  110.                 switch(reg.r15 & 3)
  111.                 {
  112.                         case 0:
  113.                                 DSDWORD[#mode] = 0x000000FF;
  114.                         break;
  115.                         case 1:
  116.                                 DSDWORD[#mode] = 0x0000FF00;
  117.                         break;
  118.                         case 2:
  119.                                 DSDWORD[#mode] = 0x00FF0000;
  120.                         break;
  121.                         case 3:
  122.                                 DSDWORD[#mode] = 0xFF000000;
  123.                         break;
  124.                 }
  125.                
  126.                 command = DSDWORD[binary + PC]; // generation PC instruction
  127.                 //EAX = DSDWORD[command >> 28 << 2 + #opcodeExec]; // get opcodeExecition call instruction
  128.                 //EAX(command); // call opcodeExecition
  129.                 //IF (command & 0xC000000 == 0) opcodeExec0(command);
  130.                 IF (command & 0x0FFFFFF0 == 0x12FFF10) BranchExchange(command);
  131.                 ELSE IF (command & 0x0FF00FF0 == 0x1000090) SingleDataSwap(command);
  132.                 ELSE IF (command & 0x0FC000F0 == 0x0000090) Multiply(command);
  133.                 ELSE IF (command & 0x0FC000F0 == 0x0800090) MultiplyLong(command);
  134.                 ELSE IF (command & 0x0C000000 == 0x0000000) DataProcessing(command);
  135.                
  136.                 PC += 4; // addition 4 for reg15 or PC instruction
  137.                 PC <<= 2;
  138.                
  139.                 flag = 0;
  140.                 IF (flags.negative) flag |= 0x8;
  141.                 IF (flags.zero)     flag |= 0x4;
  142.                 IF (flags.carry)    flag |= 0x2;
  143.                 IF (flags.overflow) flag |= 0x1;
  144.                
  145.                 pMask = 0;
  146.                 IF (mask.IRQ)  pMask |= 0x2;
  147.                 IF (mask.FIRQ) pMask |= 0x1;
  148.                
  149.                 IF (mode.User)               pMode = 0;
  150.                 ELSE IF (mode.FastInterrupt) pMode = 1;
  151.                 ELSE IF (mode.Interrupt)     pMode = 2;
  152.                 ELSE IF (mode.Supervisor)    pMode = 3;
  153.                
  154.                 reg.r15 = flag << 28 | PC | pMode;
  155.                 lengthInstruction--;
  156.         }
  157. }
  158.  
  159. dword Multiply(dword command)
  160. {
  161.        
  162. }
  163.  
  164. dword MultiplyLong(dword command)
  165. {
  166.        
  167. }
  168.  
  169. dword SingleDataSwap(dword command)
  170. {
  171.        
  172. }
  173.  
  174. dword BranchExchange(dword command)
  175. {
  176.        
  177. }
  178.  
  179. dword DataProcessing(dword command) // Data Processing / PSR Transfer
  180. {
  181.         dword opcode = 0;
  182.         dword Rd = #reg;
  183.         dword Rn = #reg;
  184.         dword operand = 0;
  185.         opcode = command >> 21 & 0xF;
  186.         Rd += command >> 12 & 0xF << 2;
  187.         Rn += command >> 16 & 0xF << 2;
  188.         operand = command & 0xFFF;
  189.  
  190.         IF (command & 0x2000000 == 0)
  191.         {
  192.                 operand = DSDWORD[operand << 2 + #reg];
  193.         }
  194.        
  195.         switch (opcode)
  196.         {
  197.                 case 0: // and
  198.                         DSDWORD[Rd] = DSDWORD[Rn] & operand;
  199.                 break;
  200.                 case 1: // eor
  201.                         DSDWORD[Rd] = DSDWORD[Rn] | operand;
  202.                 break;
  203.                 case 2: // sub
  204.                         DSDWORD[Rd] = DSDWORD[Rn] - operand;
  205.                 break;
  206.                 case 3: // rsb
  207.                         DSDWORD[Rd] = operand - DSDWORD[Rn];
  208.                 break;
  209.                 case 4: // add
  210.                         DSDWORD[Rd] = DSDWORD[Rn] + operand;
  211.                 break;
  212.         }
  213.         IF(reg.r2 == 12) while(1);
  214. }
  215.  
  216.  
  217.  
  218. ______STOP______: