mpw/bin/disasm.cpp

245 lines
4.3 KiB
C++
Raw Normal View History

2013-02-10 01:06:03 +00:00
#include <cstdio>
#include <cstdint>
#include <cassert>
2013-07-02 22:48:51 +00:00
#include <cctype>
2013-02-10 01:06:03 +00:00
#include <string>
#include <CoreServices/CoreServices.h>
#include <cpu/defs.h>
#include <cpu/fmem.h>
#include <cpu/CpuModule.h>
2013-07-02 00:21:42 +00:00
#include <macos/traps.h>
#include <macos/sysequ.h>
2013-02-10 01:06:03 +00:00
char strings[4][256];
const uint8_t *Memory = NULL;
uint32_t MemorySize = 0;
void ToolBox(uint32_t pc, uint16_t trap)
{
const char *name;
name = TrapName(trap);
if (name)
{
printf("$%08X %-51s ; %04X\n", pc, name, trap);
}
else
{
printf("$%08X Tool #$%04X ; %04X\n", pc, trap, trap);
}
}
void help()
{
}
void code0(uint32_t data_size)
{
// dump the code 0 jump table.
uint32_t offset = 0;
uint32_t pc;
uint32_t size;
printf("Above A5: $%08X\n", memoryReadLong(0));
printf("Below A5: $%08X\n", memoryReadLong(4));
printf("Jump Table Size: $%08X\n", size = memoryReadLong(8));
printf("Jump Table Offset: $%08X\n", pc = memoryReadLong(12)); // aka CurJTOffset. should be 32.
offset = 16;
while (offset < data_size)
{
uint16_t off = memoryReadWord(offset);
if (memoryReadWord(offset + 2) == 0x3F3C && memoryReadWord(offset + 6) == 0xA9F0)
{
uint16_t seg = memoryReadWord(offset + 4);
// pc +2 since the first byte is the offset, not code.
printf("$%08X %04X : %04X\n", pc + 2, seg, off);
}
else
{
printf("$%08X ???\n", pc + 2);
}
offset += 8;
pc += 8;
}
printf("\n\n");
}
2013-07-02 00:24:49 +00:00
inline char *cc2(uint16_t value, char out[3])
{
uint8_t tmp;
tmp = (value >> 8) & 0xff;
if (tmp & 0x80 || !isprint(tmp)) out[0] = '.';
else out[0] = tmp;
tmp = (value >> 0) & 0xff;
if (tmp & 0x80 || !isprint(tmp)) out[1] = '.';
else out[1] = tmp;
out[2] = 0;
return out;
}
2013-07-02 22:48:51 +00:00
2013-02-10 01:06:03 +00:00
void disasm(const char *name, int segment, uint32_t data_size)
{
if (name && *name) printf("segment %d - %s\n", segment, name);
else printf("segment %d\n", segment);
uint32_t pc = 0;
uint16_t prevOP = 0;
while (pc < data_size)
{
for (unsigned j = 0; j < 4; ++j) strings[j][0] = 0;
uint16_t op = memoryReadWord(pc);
2013-02-21 01:16:50 +00:00
if (prevOP == 0x4E75 || prevOP == 0x4ED0 || prevOP == 0x4E74)
2013-02-10 01:06:03 +00:00
{
2013-02-15 05:00:33 +00:00
if (op > 0x8000)
2013-02-10 01:06:03 +00:00
{
2013-02-15 05:00:33 +00:00
// RTS followed by debug symbol.
unsigned len = (op >> 8) - 0x80;
2013-02-10 01:06:03 +00:00
2013-02-15 05:00:33 +00:00
std::string s;
s.reserve(len);
2013-07-02 00:24:49 +00:00
pc += 1; // skip the length byte.
2013-02-15 05:00:33 +00:00
for (unsigned i = 0; i < len; ++ i)
{
2013-07-02 00:24:49 +00:00
s.push_back(memoryReadByte(pc++));
2013-02-15 05:00:33 +00:00
}
2013-02-10 01:06:03 +00:00
2013-07-02 00:24:49 +00:00
printf("%s\n", s.c_str());
2013-02-15 05:00:33 +00:00
pc = (pc + 1) & ~0x01;
2013-07-02 00:24:49 +00:00
#if 0
if (memoryReadWord(pc) == 0x0000) pc += 2;
#else
// treat the remainder as data until 4E56. [Link A6,#]
while (pc < data_size)
{
char tmp[3];
uint16_t data = memoryReadWord(pc);
if (data == 0x4e56) break;
printf("$%08X $%04X ; '%s'\n", pc, data, cc2(data, tmp));
pc += 2;
}
#endif
printf("\n");
2013-02-15 05:00:33 +00:00
prevOP = 0;
continue;
}
2013-02-10 01:06:03 +00:00
}
if ((op & 0xf000) == 0xa000)
{
// tool call!
ToolBox(pc, op);
pc += 2;
prevOP = op;
continue;
}
pc = cpuDisOpcode(pc, strings[0], strings[1], strings[2], strings[3]);
// address, data, instruction, operand
printf("%s %-10s %-40s ; %s\n", strings[0], strings[2], strings[3], strings[1]);
prevOP = op;
}
printf("\n\n");
}
int main(int argc, char **argv)
{
ResFileRefNum refNum;
FSRef ref;
Handle h;
if (argc != 2)
{
help();
return -1;
}
assert( FSPathMakeRef( (const UInt8 *)argv[1], &ref, NULL) == noErr);
refNum = FSOpenResFile(&ref, fsRdPerm);
assert(refNum != -1 );
//UseResFile(refNum);
cpuSetModel(3, 0); // 68030
int l = Count1Resources('CODE');
for (int i = 0; i < l; ++i)
{
ResAttributes attr;
ResID resID;
ResType resType;
Str255 name;
std::string cname;
h = Get1IndResource('CODE', i + 1);
if (!h) continue;
HLock(h);
attr = GetResAttrs(h);
GetResInfo(h, &resID, &resType, name);
// name is a p-string.
if (name[0])
{
int l = name[0];
cname.assign(name + 1, name + 1 + l);
}
uint32_t size = GetHandleSize(h);
uint8_t *data = *( uint8_t **)h;
if (resID == 0)
{
memorySetMemory(data, size);
code0(size);
}
else
{
// 4-byte header for segment stuff.
data += 4;
size -= 4;
memorySetMemory(data, size);
disasm(cname.c_str(), resID, size);
}
memorySetMemory(nullptr, 0);
ReleaseResource(h);
}
CloseResFile(refNum);
return 0;
}