<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#
# sim_run.py
#
# Gdb-like python functions for running the simulator.
#
# Only nexti and finish works currently, the other ones
# need to be updated to the improved symtable.
#
#

# Uses the following external functions:
#    SIM_get_program_counter()      (sim_cpu)
#    SIM_get_stack_pointer()        (sim_cpu)
#    SIM_instruction_info()         (sim_cpu)
#    SIM_current_processor()        (sim_system)
#    SIM_continue()                 (sim_control)
#    SIM_source_line_by_address()   (ext_symtable)
#    SIM_source_file_by_address()   (ext_symtable)
#    SIM_instructions_from_source() (ext_symtable)
#    SIM_address_by_source()        (ext_symtable)

from sim_core import *
from cli import *
from sim_commands import local_print_disassemble_line

# logical_2_large_address - Convert address from logical to large
# TODO: Fix for 32 bit targets
def logical_2_large_address(cpu, addr):
	return addr;

# nexti - Step one instruction, with function calls counting as one instruction
def nexti():
	executing_call = 0
	while 1:
		pc = SIM_get_program_counter(SIM_current_processor())
		instr = SIM_instruction_info(SIM_current_processor(), pc)
		if (instr.type &amp; It_Call) and executing_call==0:
			executing_call = 1
			return_point = instr.return_pc
			return_stack = SIM_get_stack_pointer(SIM_current_processor())
		elif executing_call:
			if pc == return_point and return_stack == SIM_get_stack_pointer(SIM_current_processor()):
				break
		else:
			SIM_continue(1)
			break
		SIM_continue(1)
	# TODO call some standard function for this
	local_print_disassemble_line(SIM_current_processor(), SIM_get_program_counter(SIM_current_processor()), 1)

# stepi - Step one instruction
def stepi():
	SIM_continue(1)

def symbolic_info():
	cpu = SIM_current_processor()
	pc = SIM_get_program_counter(cpu)
        large_addr_pc = logical_2_large_address(cpu, pc)
	source_line = SIM_source_line_by_address(large_addr_pc, 0)
	source_file = SIM_source_file_by_address(large_addr_pc, 0)
	num_instr = SIM_instructions_from_source(source_file, source_line)
	#print "pc=%s file=%s line=%d num_instr=%d" % (large_addr_pc, source_file, source_line, num_instr)
	return (large_addr_pc, source_line, source_file, num_instr)

# next - Step one source line, with functions calls counting as one line
def next(step_over=1):
	(pc, source_line, source_file, num_instr) = symbolic_info()
	line_pcs = []
	for i in range(0, num_instr):
		line_pcs.append(SIM_address_by_source(source_file, source_line, i))
		#print "instr[%d] -- %s" % (i, line_pcs[i])
	executing_call = 0
        next_print = 100
	while 1:
		cpu = SIM_current_processor()
		pc = SIM_get_program_counter(cpu)
		large_addr_pc = logical_2_large_address(cpu, pc)
		#print "pc=%s" % large_addr_pc
		instr = SIM_instruction_info(SIM_current_processor(), pc)
		if step_over:
			if instr_info_t_is_call_get(instr):
				executing_call = 1
				return_point = instr_info_t_return_pc_get(instr)
				return_stack = SIM_get_stack_pointer(SIM_current_processor())
			elif executing_call == 0 and instr_info_t_is_return_get(instr):
				for foo in range(1 + instr_info_t_delay_slots_get(instr)):
					SIM_continue(1)
				return
		SIM_continue(1)
		next_print = next_print - 1
		if next_print == 0:
			print ".",
			next_print = 100
		if step_over and executing_call and pc == return_point and return_stack == SIM_get_stack_pointer(SIM_current_processor()):
			return
		elif large_addr_pc not in line_pcs:
			return

# step - Step one source line
def step():
	next(step_over=0)

# finish - Run until end of function
def finish():
	stack_pointer = SIM_get_stack_pointer(SIM_current_processor())
	next_print = 100
	while 1:
		pc = SIM_get_program_counter(SIM_current_processor())
		instr = SIM_instruction_info(SIM_current_processor(), pc)
		if instr.type &amp; It_Return:
			for foo in range(1 + instr.delay_slots):
				SIM_continue(1)
			break
		SIM_continue(1)
		next_print = next_print - 1
		if next_print == 0:
			print ".",
			next_print = 100
	# TODO call some standard function for this
	local_print_disassemble_line(SIM_current_processor(), SIM_get_program_counter(SIM_current_processor()), 1)

new_command("nexti", nexti,
	    [],
	    alias = "ni",
	    type  = "general commands",
	    short = "run until the next sequential instruction",
	    doc_items = [('SEE ALSO', 'run, stepi, finish')],
            repeat = nexti,
	    doc = """
Run until the next sequential instruction in the program ignoring
function calls.
Note: Does not yet ignore traps and exceptions.
""", filename="/home/mp/simics-2.0.23/src/extensions/apps-python/sim_run.py", linenumber="122")

new_command("finish", finish,
	    [],
	    alias = "",
	    type  = "general commands",
	    short = "run to the end of the current function",
	    doc_items = [('SEE ALSO', 'run, stepi, nexti')],
	    doc = """
Continue the simulation until the end of the current function.
Note: Does not yet ignore traps and exceptions.
""", filename="/home/mp/simics-2.0.23/src/extensions/apps-python/sim_run.py", linenumber="135")
</pre></body></html>