simple_disassembler.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* ScummVM Tools
  2. *
  3. * ScummVM Tools is the legal property of its developers, whose
  4. * names are too numerous to list here. Please refer to the
  5. * COPYRIGHT file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include "simple_disassembler.h"
  22. SimpleDisassembler::SimpleDisassembler(InstVec &insts)
  23. : Disassembler(insts)
  24. {
  25. }
  26. static inline unsigned int Nib2(unsigned int v)
  27. {
  28. return (v & 0xF);
  29. }
  30. static inline unsigned int Nib1(unsigned int v)
  31. {
  32. return (v >> 4) & 0xF;
  33. }
  34. // [8s]
  35. // [8u]
  36. // [16s]
  37. // [16u]
  38. // [32s]
  39. // [32u]
  40. // [4,4]
  41. // [8u...8u]
  42. // NBwwB
  43. // NBdB when N == 0
  44. // [ [8u EQ 0], [8u][8s][16s][8u] , [8u][8s][32u][8s] ]
  45. // j prefix = jump/label
  46. void SimpleDisassembler::readParams(InstPtr inst, const char *typeString)
  47. {
  48. // Handle [] blocks as working on an individual element (i.e a BYTE,WORD etc)
  49. // this syntax allows picking of nibbles and bit fields into their own parameters.
  50. while (*typeString)
  51. {
  52. std::string typeStr(typeString, 1);
  53. if (typeStr == "N") // Read nibbles
  54. {
  55. const uint8 byte = mStream->ReadU8();
  56. inst->_params.push_back(new IntValue(Nib1(byte), false));
  57. inst->_params.push_back(new IntValue(Nib2(byte), false));
  58. _address++;
  59. }
  60. else if (typeStr == "U")
  61. {
  62. const uint8 byte = mStream->ReadU8();
  63. inst->_params.push_back( new IntValue((byte >> 5) & 0x7, false));
  64. inst->_params.push_back( new IntValue((byte & 0x1F), false));
  65. _address++;
  66. }
  67. else
  68. {
  69. inst->_params.push_back(readParameter(inst, typeStr));
  70. }
  71. typeString++;
  72. }
  73. }
  74. void SimpleDisassembler::readParams(InstPtr inst, const char *typeString, const std::vector<std::string>& params)
  75. {
  76. while (*typeString)
  77. {
  78. std::string typeStr(typeString, 1);
  79. if (typeStr == "N") // Read nibbles
  80. {
  81. _address++;
  82. }
  83. else if (typeStr == "U")
  84. {
  85. _address++;
  86. }
  87. else
  88. {
  89. // inst->_params.push_back(readParameter(inst, typeStr));
  90. }
  91. typeString++;
  92. }
  93. }
  94. ValuePtr SimpleDisassembler::readParameter(InstPtr inst, std::string type) {
  95. ValuePtr retval = NULL;
  96. if (type == "b") // signed byte
  97. {
  98. retval = new IntValue(mStream->ReadS8(), true);
  99. _address++;
  100. }
  101. else if (type == "B") // unsigned byte
  102. {
  103. retval = new IntValue((uint32)mStream->ReadU8(), false);
  104. _address++;
  105. }
  106. else if (type == "s") // 16-bit signed integer (short), little-endian
  107. {
  108. retval = new IntValue(mStream->ReadS16(), true);
  109. _address += 2;
  110. }
  111. else if (type == "w") // 16-bit unsigned integer (word), little-endian
  112. {
  113. retval = new IntValue((uint32)mStream->ReadU16(), false);
  114. _address += 2;
  115. }
  116. else if (type == "i") // 32-bit signed integer (int), little-endian
  117. {
  118. retval = new IntValue(mStream->ReadS32(), true);
  119. _address += 4;
  120. }
  121. else if (type == "d") // 32-bit unsigned integer (dword), little-endian
  122. {
  123. retval = new IntValue(mStream->ReadU32(), false);
  124. _address += 4;
  125. }
  126. else
  127. {
  128. throw UnknownOpcodeParameterException(type);
  129. }
  130. return retval;
  131. }