|
| 1 | +if tastudio then -- bizhawk |
| 2 | + shift_right = bit.rshift |
| 3 | + bitwise_and = bit.band |
| 4 | + bitwise_xor = bit.bxor |
| 5 | + read_byte = memory.read_u8 |
| 6 | + read_word = memory.read_u16_le |
| 7 | + get_cycles = emu.totalexecutedcycles |
| 8 | + function get_program_counter() |
| 9 | + return emu.getregister('PC') |
| 10 | + end |
| 11 | + function get_stack_pointer() |
| 12 | + return emu.getregister('S') |
| 13 | + end |
| 14 | + function register_callback_execute(function_) |
| 15 | + event.onmemoryexecuteany(function_) |
| 16 | + end |
| 17 | + function script_suffix() |
| 18 | + event.onexit(function() io.close(log_file) end) |
| 19 | + while true do |
| 20 | + emu.frameadvance() |
| 21 | + end |
| 22 | + end |
| 23 | +else -- mesen (assumed) |
| 24 | + function read_byte(address) |
| 25 | + return emu.read(address, emu.memType.cpuDebug) |
| 26 | + end |
| 27 | + function read_word(address) |
| 28 | + return emu.readWord(address, emu.memType.cpuDebug) |
| 29 | + end |
| 30 | + function get_cycles() |
| 31 | + return emu.getState().cpu.cycleCount |
| 32 | + end |
| 33 | + function get_program_counter() |
| 34 | + return emu.getState().cpu.pc |
| 35 | + end |
| 36 | + function get_stack_pointer() |
| 37 | + return emu.getState().cpu.sp |
| 38 | + end |
| 39 | + print = emu.log |
| 40 | + function register_callback_execute(function_) |
| 41 | + emu.addMemoryCallback(function_, emu.memCallbackType.cpuExec, 0x0, 0xffff) |
| 42 | + end |
| 43 | + function script_suffix() end |
| 44 | +end |
| 45 | + |
| 46 | +log_file = io.open('call_log.txt', 'w') |
| 47 | + |
| 48 | +returns = {} |
| 49 | +last_cycles = 0 |
| 50 | + |
| 51 | +register_callback_execute(function() |
| 52 | + pc = get_program_counter() |
| 53 | + opcode = read_byte(pc) |
| 54 | + new_cycles = get_cycles() - last_cycles |
| 55 | + |
| 56 | + if opcode == 0x20 then -- JSR |
| 57 | + operand = read_word(pc + 1) |
| 58 | + |
| 59 | + log_file:write(string.format('jsr to %04x (pc: %04x, cycles: +%d)\n', operand, pc, new_cycles)) |
| 60 | + returns[pc + 2] = operand |
| 61 | + last_cycles = get_cycles() |
| 62 | + elseif opcode == 0x60 then -- RTS |
| 63 | + return_address = read_word(0x100 + get_stack_pointer() + 1) |
| 64 | + |
| 65 | + return_string = returns[return_address] and |
| 66 | + string.format('%04x', returns[return_address]) or |
| 67 | + 'unknown' |
| 68 | + |
| 69 | + log_file:write(string.format('rts from %s (pc: %04x, cycles: +%d)\n', return_string, pc, new_cycles)) |
| 70 | + returns[return_address] = nil |
| 71 | + last_cycles = get_cycles() |
| 72 | + end |
| 73 | +end) |
| 74 | + |
| 75 | +script_suffix() |
0 commit comments