romcalls.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Hey EMACS -*- linux-c -*- */
  2. /* $Id: romcalls.c 2268 2006-11-06 17:18:51Z roms $ */
  3. /* TiEmu - Tiemu Is an EMUlator
  4. *
  5. * Copyright (c) 2000-2001, Thomas Corvazier, Romain Liévin
  6. * Copyright (c) 2001-2003, Romain Liévin
  7. * Copyright (c) 2003, Julien Blache
  8. * Copyright (c) 2004, Romain Liévin
  9. * Copyright (c) 2005, Romain Liévin
  10. * Copyright (c) 2006-2007, Kevin Kofler
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  25. */
  26. /*
  27. Symbols (ROM calls address and names)
  28. */
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include "romcalls.h"
  34. #include "images.h"
  35. #include "ti68k_def.h"
  36. #include "timem.h"
  37. #include "main.h"
  38. static int loaded = 0; // loaded
  39. static int addr_loaded = 0; // addresses loaded
  40. static ROM_CALL table[NMAX_ROMCALLS] = {
  41. /* Generated from romcalls.txt by replacing:
  42. ([^:]*):(.*)
  43. with:
  44. {0x\1,0,"\2"},
  45. in Kate. */
  46. #include "romcalls.inc"
  47. }; // list by id
  48. static int size;
  49. /* =========== */
  50. /*
  51. Retrieve base address of ROM calls table and size.
  52. */
  53. void romcalls_get_table_infos(uint32_t *base, uint32_t *size)
  54. {
  55. *base = *size = 0;
  56. if(calc_type == TI92)
  57. return;
  58. *base = rd_long(&rom[0x12000 + 0x88 + 0xC8]);
  59. *size = rd_long(&rom[((*base-4) & 0x0fffff)]);
  60. if (*size > sizeof(table)/sizeof(table[0]))
  61. {
  62. fprintf(stderr,"WARNING: this ROM_CALL table has more entries than those of all known AMS versions !\nWARNING: clamping the number of entries.\n");
  63. *size = sizeof(table)/sizeof(table[0]);
  64. }
  65. }
  66. /*
  67. Given a ROM call ID, retrieve address of ROM call.
  68. */
  69. void romcalls_get_symbol_address(int id, uint32_t *addr)
  70. {
  71. uint32_t base;
  72. base = rd_long(&rom[0x12000 + 0x88 + 0xC8]);
  73. *addr = rd_long(&rom[(base & 0x0fffff) + 4*id]);
  74. }
  75. /* =========== */
  76. /*
  77. Fill the addr field from ROM calls located in FLASH. Don't touch other fields !
  78. And construct list from ROM call table.
  79. */
  80. static int merge_from_flash(void)
  81. {
  82. uint32_t addr;
  83. int i;
  84. romcalls_get_table_infos(&addr, (uint32_t *)&size);
  85. if(size == 0)
  86. return -1;
  87. for(i = 0; i < size; i++)
  88. {
  89. if(table[i].name == NULL)
  90. table[i].name = strdup("unknown");
  91. table[i].addr = rd_long(&rom[(addr & 0x0fffff) + (i << 2)]);
  92. }
  93. addr_loaded = 1;
  94. return 0;
  95. }
  96. /*
  97. Load ROM calls from file and FLASH and merge.
  98. Return value:
  99. 0 if successful
  100. -1 if error
  101. -3 if TI92
  102. */
  103. int romcalls_load(int noflash)
  104. {
  105. // check whether parsing is possible
  106. if(calc_type == TI92) return -3;
  107. if(!noflash && merge_from_flash())
  108. return -1;
  109. loaded = !0;
  110. return 0;
  111. }
  112. int romcalls_is_loaded(void)
  113. {
  114. return loaded;
  115. }
  116. /* =========== */
  117. // cache last search (disasm)
  118. static int last_id = 0;
  119. // returns id or -1
  120. int romcalls_is_addr(uint32_t addr)
  121. {
  122. int i;
  123. if(!addr_loaded) return -1;
  124. for(i = 0; i < size; i++)
  125. {
  126. if(addr == table[i].addr)
  127. return last_id = i;
  128. }
  129. return -1;
  130. }
  131. const char* romcalls_get_name(int id)
  132. {
  133. int maxid = size;
  134. if (!loaded) return "not loaded";
  135. if (id >= 0)
  136. {
  137. // Assume maximum number of ROM_CALLs if we're not disassembling an OS.
  138. if (maxid == 0)
  139. {
  140. maxid = sizeof(table)/sizeof(table[0]);
  141. }
  142. if (id < maxid)
  143. {
  144. return table[id].name;
  145. }
  146. }
  147. return NULL;
  148. }
  149. // No need to check the parameter: the only caller of romcalls_get_addr() calls
  150. // romcalls_get_table_infos() before calling romcalls_get_addr().
  151. uint32_t romcalls_get_addr(int id)
  152. {
  153. return table[id].addr;
  154. }