52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from . import CustomInstruction
|
|
import os
|
|
|
|
class CustomLog(CustomInstruction):
|
|
"""
|
|
CustomLog() logs data
|
|
"""
|
|
|
|
tick_count = 1
|
|
handler = None
|
|
|
|
def __init__(self, call_operation, no_pointer=True):
|
|
super(CustomLog, self).__init__(call_operation)
|
|
|
|
self.no_pointer = no_pointer
|
|
|
|
def run(self, update_program_counter=True):
|
|
if CustomLog.handler is None:
|
|
os.makedirs(self._vmstate.log_dir, exist_ok=True)
|
|
CustomLog.handler = open(os.path.join(self._vmstate.log_dir, 'custom_logs.csv'), "w")
|
|
CustomLog.handler.write("timestamp, program_counter, custom_log_data\n")
|
|
|
|
format_str = self.get_arg(0)
|
|
if self.no_pointer:
|
|
args = tuple([self.get_arg(i) for i in range(1, self.argc())])
|
|
#print(f"CustomLog: {args}")
|
|
else:
|
|
args = tuple([self._vmstate.memory.read_addr(self.get_arg(i)) for i in range(1, self.argc())])
|
|
#print(f"CustomLogDump: {args}")
|
|
|
|
try:
|
|
log_line = "{}".format(format_str % args)
|
|
except TypeError:
|
|
#log_line = "{} {}".format(format_str, args)
|
|
log_line = f"{args}"
|
|
|
|
if ',' in log_line:
|
|
log_line = f"\"{log_line}\""
|
|
|
|
t = self._vmstate.config.analysis.energy.system_model.get_simulation_time()
|
|
pc = f"{self._vmstate.register_file.pc}"
|
|
|
|
CustomLog.handler.write(f"{t}, {pc}, {log_line}\n")
|
|
|
|
super().run(update_program_counter)
|
|
|
|
|
|
class CustomLogDump(CustomLog):
|
|
|
|
def __init__(self, call_operation):
|
|
super(CustomLogDump, self).__init__(call_operation, False)
|