Skip to content

Commit

Permalink
added more util tag functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cnvogelg committed Jan 11, 2025
1 parent 3992f32 commit f259d35
Show file tree
Hide file tree
Showing 23 changed files with 760 additions and 288 deletions.
4 changes: 2 additions & 2 deletions amitools/vamos/astructs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .access import AccessStruct
from .astruct import AmigaStruct, AmigaStructTypes, APTR_SELF, BPTR_SELF
from .astructdef import AmigaStructDef, AmigaClassDef
from .scalar import ULONG, LONG, UWORD, WORD, UBYTE, BYTE
from .pointer import BPTR, APTR, BPTR_VOID, APTR_VOID, PointerType
from .scalar import ULONG, LONG, UWORD, WORD, UBYTE, BYTE, ScalarType
from .pointer import BPTR, APTR, BPTR_VOID, APTR_VOID, PointerType, make_aptr, make_bptr
from .array import ARRAY, ArrayIter, ArrayType
from .string import CSTR, BSTR, CStringType, BStringType
from .dump import TypeDumper
Expand Down
43 changes: 37 additions & 6 deletions amitools/vamos/astructs/pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ def get_ref_type(cls):
def get_signature(cls):
return "{}*".format(cls._ref_type.get_signature())

def __init__(self, mem=None, addr=None, cpu=None, reg=None, **kwargs):
def __init__(
self, mem=None, addr=None, cpu=None, reg=None, ref=None, ref_addr=0, **kwargs
):
"""create pointer type with referenced object"""
super(PointerType, self).__init__(mem, addr, cpu, reg, **kwargs)
self._ref = None
self._ref_addr = None
self._ref = ref
if ref:
self._ref_addr = ref.get_addr()
else:
self._ref_addr = ref_addr

def setup(self, val, alloc=None, free_refs=None):
if val is None:
Expand Down Expand Up @@ -70,7 +75,9 @@ def set_ref(self, ref):
if not ref:
self._ref_addr = 0
else:
assert isinstance(ref, self._ref_type)
# ignore VOID
if not issubclass(self._ref_type, VOID):
assert isinstance(ref, self._ref_type)
self._ref_addr = ref.get_addr()
self._write_pointer(self._ref_addr)

Expand All @@ -91,17 +98,23 @@ def set(self, ref_addr):
def _read_pointer(self):
if self._addr is not None:
store_addr = self._mem.r32(self._addr)
else:
elif self._reg is not None:
store_addr = self._cpu.r_reg(self._reg)
else:
# fake read for unbound pointer (re-use value)
store_addr = self._ref_to_store_addr(self._ref_addr)
ref_addr = self._store_to_ref_addr(store_addr)
return ref_addr

def _write_pointer(self, ref_addr):
store_addr = self._ref_to_store_addr(ref_addr)
if self._addr is not None:
self._mem.w32(self._addr, store_addr)
else:
elif self._reg is not None:
self._cpu.w_reg(self._reg, store_addr)
else:
# fake write for unbound pointers (set value)
self._ref_addr = ref_addr

def __repr__(self):
return "{}(ref={}, addr={})".format(
Expand Down Expand Up @@ -214,6 +227,15 @@ def APTR(ref_type):
return new_type


def make_aptr(ref_obj, **kw_args):
"""create a pointer instance in memory or as reg around given object"""
ref_type = type(ref_obj)
aptr_type = APTR(ref_type)
ptr_obj = aptr_type(**kw_args)
ptr_obj.ref = ref_obj
return ptr_obj


# pointer type cache
BPTRTypes = {}

Expand All @@ -229,5 +251,14 @@ def BPTR(ref_type):
return new_type


def make_bptr(ref_obj, **kw_args):
"""create a pointer instance in memory or as reg around given object"""
ref_type = type(ref_obj)
bptr_type = BPTR(ref_type)
ptr_obj = bptr_type(**kw_args)
ptr_obj.ref = ref_obj
return ptr_obj


APTR_VOID = APTR(VOID)
BPTR_VOID = BPTR(VOID)
27 changes: 13 additions & 14 deletions amitools/vamos/lib/DosLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from amitools.vamos.machine.regs import *
from amitools.vamos.libcore import LibImpl
from amitools.vamos.astructs import AccessStruct
from amitools.vamos.astructs import AccessStruct, CSTR
from amitools.vamos.libstructs import (
DosLibraryStruct,
DosInfoStruct,
Expand All @@ -25,14 +25,13 @@
DosPacketStruct,
PathStruct,
)
from amitools.vamos.libtypes import TagList, DosTag
from amitools.vamos.error import *
from amitools.vamos.log import log_dos
from .dos.Args import *
from .dos.Error import *
from .dos.AmiTime import *
from .util.TagList import *
from .dos import Printf, PathPart
from .dos.DosTags import DosTags
from .dos.PatternMatch import Pattern, pattern_parse, pattern_match
from .dos.MatchFirstNext import MatchFirstNext
from .dos.CommandLine import CommandLine
Expand Down Expand Up @@ -1510,12 +1509,9 @@ def ReadItem(self, ctx):

# ----- System/Execute -----

def SystemTagList(self, ctx):
cmd_ptr = ctx.cpu.r_reg(REG_D1)
tagitem_ptr = ctx.cpu.r_reg(REG_D2)
cmd = ctx.mem.r_cstr(cmd_ptr)
tag_list = taglist_parse_tagitem_ptr(ctx.mem, tagitem_ptr, DosTags)
log_dos.info("SystemTagList: cmd='%s' tags=%s", cmd, tag_list)
def SystemTagList(self, ctx, cmd: CSTR, tag_list: TagList):
cmd = cmd.get_str()
log_dos.info("SystemTagList: cmd=%s tags=%s ctx=%s", cmd, tag_list, ctx)
# cmd is at this point a full string of commands to execute.
# If we're running from the Amiga shell, forward this to the shell
# anyhow.
Expand All @@ -1530,17 +1526,20 @@ def SystemTagList(self, ctx):
)
return 0xFFFFFFFF
# Push-back the commands into the input buffer.
new_input.setbuf(cmd)
if cmd:
new_input.setbuf(cmd)
new_stdin = self.file_mgr.open(None, "*", "rw")
outtag = tag_list.find_tag("SYS_Output")
outtag = tag_list.find_tag(DosTag.SYS_Output)
# print "setting new input to %s" % new_input
# and install this as current input. The shell will read from that
# instead until it hits the EOF
input_fhsi = cli.r_s("cli_StandardInput")
input_fhci = cli.r_s("cli_CurrentInput")
if outtag != None and outtag.data != 0:
output_fhci = cli.r_s("cli_StandardOutput")
cli.w_s("cli_StandardOutput", outtag.data << 2)
if outtag:
data = outtag.get_data()
if data != 0:
output_fhci = cli.r_s("cli_StandardOutput")
cli.w_s("cli_StandardOutput", data << 2)
else:
output_fhci = None
cli.w_s("cli_CurrentInput", new_input.mem.addr)
Expand Down
1 change: 0 additions & 1 deletion amitools/vamos/lib/LocaleLibrary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from amitools.vamos.machine.regs import *
from amitools.vamos.libcore import LibImpl
from amitools.vamos.lib.util.TagList import *
from amitools.vamos.lib.util.AmiDate import *
from amitools.vamos.log import *

Expand Down
117 changes: 86 additions & 31 deletions amitools/vamos/lib/UtilityLibrary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from amitools.vamos.machine.regs import *
from amitools.vamos.machine.regs import REG_D0, REG_D1, REG_A0, REG_A1
from amitools.vamos.astructs import APTR
from amitools.vamos.libcore import LibImpl
from amitools.vamos.lib.util.TagList import *
from amitools.vamos.libtypes import TagList, TagItem
from amitools.vamos.lib.util.AmiDate import *
from amitools.vamos.log import *

Expand Down Expand Up @@ -93,38 +94,92 @@ def Strnicmp(self, ctx):

# Tags

def NextTagItem(self, ctx):
ti_ptr_addr = ctx.cpu.r_reg(REG_A0)
ti_addr = ctx.mem.r32(tr_ptr_addr)
ti_addr = next_tag_item(ctx, ti_addr)
if ti_addr is None:
next_addr = 0
def FindTagItem(self, ctx, tag_val, tag_list: TagList) -> TagItem:
if not tag_list:
return 0
log_utility.info("FindTagItem(tag=%08x, list=%s)", tag_val, tag_list)
tag = tag_list.find_tag(tag_val)
if tag:
log_utility.info("found tag: %r", tag)
return tag
else:
next_addr = ti_addr + 8
ctx.mem.w32(tr_ptr_addr, next_addr)
return ti_addr

def FindTagItem(self, ctx):
tagValue = ctx.cpu.r_reg(REG_D0)
ti_addr = ctx.cpu.r_reg(REG_A0)
if ti_addr == 0:
log_utility.info("no tag!")
return 0

def GetTagData(self, ctx, tag_val, default_val, tag_list_addr):
if tag_list_addr == 0:
return default_val
log_utility.info(
"GetTagData(tag=%08x, def=%08x, list=%08x)",
tag_val,
default_val,
tag_list_addr,
)
tl = TagList(ctx.mem, tag_list_addr)
data = tl.get_tag_data(tag_val, default_val)
log_utility.info("result data=%08x", data)
return data

def PackBoolTags(self, ctx, init_flags, tag_list_addr, bool_map_addr):
if tag_list_addr == 0 or bool_map_addr == 0:
return 0
log_utility.info(
"PackBoolTags(flags=%08x, list=%08x, map=%08x)",
init_flags,
tag_list_addr,
bool_map_addr,
)
tag_list = TagList(ctx.mem, tag_list_addr)
bool_map = TagList(ctx.mem, bool_map_addr)
result = init_flags
for tag in tag_list:
on_flag = tag.get_data()
bool_tag = bool_map.find_tag(tag)
if bool_tag:
mask_val = bool_tag.get_data()
if on_flag:
result |= mask_val
else:
result &= ~mask_val
log_utility.info("result mask=%08x", result)
return result

def NextTagItem(self, ctx, ti_ptr: APTR(APTR(TagItem))):
if ti_ptr is None:
return 0
tag_ptr = ti_ptr.ref
log_utility.info("NextTagItem(ti_ptr=%s) -> tag=%s", ti_ptr, tag_ptr)
if tag_ptr is None:
return 0
tag = tag_ptr.ref
real_tag = tag.next_real_tag()
if real_tag is None:
log_utility.info("no real tag!")
tag_ptr.ref = None
return 0
while True:
ti_addr = next_tag_item(ctx, ti_addr)
if ti_addr is None:
return 0
tag, _ = get_tag(ctx, ti_addr)
if tag == tagValue:
return ti_addr
ti_addr += 8

def GetTagData(self, ctx):
defaultValue = ctx.cpu.r_reg(REG_D1)
ti_addr = self.FindTagItem(ctx)
if ti_addr != 0:
return get_tag(ctx, ti_addr)[1]
else:
return defaultValue
succ_tag = tag.succ_tag()
log_utility.info("real tag=%r, succ_tag=%r", tag, succ_tag)
tag_ptr.ref = succ_tag
return tag.get_addr()

def FilterTagChanges(self, ctx, change_list: TagList, orig_list: TagList, apply):
if change_list is None or orig_list is None:
return 0
log_utility.info(
"FilterTagChanges(change=%s, orig=%s, apply=%s)",
change_list,
orig_list,
apply,
)
for tag in change_list:
orig_tag = orig_list.find_tag(tag)
if orig_tag:
tag_data = tag.get_data()
if tag_data == orig_tag.get_data():
tag.remove()
elif apply:
orig_tag.set_data(tag_data)

# ---- Date -----

Expand Down
7 changes: 7 additions & 0 deletions amitools/vamos/lib/dos/DosLibCtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ def __init__(
self.exec_lib = None
self.dos_lib = None

def __str__(self):
return "[DosLibCtx:cpu=%s,mem=%s,process=%s]" % (
self.cpu,
self.mem,
self.process,
)

def set_exec_lib(self, exec_lib):
self.exec_lib = exec_lib

Expand Down
40 changes: 0 additions & 40 deletions amitools/vamos/lib/dos/DosTags.py

This file was deleted.

8 changes: 8 additions & 0 deletions amitools/vamos/lib/lexec/ExecLibCtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ def __init__(self, machine, alloc, runner, seg_loader, path_mgr, lib_mgr):
def set_cur_task_process(self, task, process):
self.task = task
self.process = process

def __str__(self):
return "[DosLibCtx:cpu=%s,mem=%s,task=%s,process=%s]" % (
self.cpu,
self.mem,
self.task,
self.process,
)
Loading

0 comments on commit f259d35

Please sign in to comment.