2024-02-09 23:30:47 +00:00
|
|
|
<?php
|
|
|
|
|
namespace Targets\TargetDescriptionFiles;
|
|
|
|
|
|
2024-12-27 01:53:02 +00:00
|
|
|
require_once __DIR__ . "/AddressRange.php";
|
|
|
|
|
|
2024-02-09 23:30:47 +00:00
|
|
|
class MemorySegmentSection
|
|
|
|
|
{
|
|
|
|
|
public ?string $key = null;
|
|
|
|
|
public ?string $name = null;
|
2024-12-27 01:53:02 +00:00
|
|
|
public ?AddressRange $addressRange = null;
|
|
|
|
|
public ?int $addressSpaceUnitSize = null;
|
2024-02-09 23:30:47 +00:00
|
|
|
|
|
|
|
|
/** @var MemorySegmentSection[] */
|
|
|
|
|
public array $subSections = [];
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
?string $key,
|
|
|
|
|
?string $name,
|
|
|
|
|
?int $startAddress,
|
|
|
|
|
?int $size,
|
2024-12-27 01:53:02 +00:00
|
|
|
?int $addressSpaceUnitSize,
|
2024-02-09 23:30:47 +00:00
|
|
|
array $subSections
|
|
|
|
|
) {
|
|
|
|
|
$this->key = $key;
|
|
|
|
|
$this->name = $name;
|
2024-12-27 01:53:02 +00:00
|
|
|
$this->addressRange = is_numeric($startAddress) && is_numeric($size)
|
|
|
|
|
? new AddressRange(
|
|
|
|
|
$startAddress,
|
|
|
|
|
$startAddress + ($size / ($addressSpaceUnitSize ?? 1)) - 1
|
|
|
|
|
)
|
|
|
|
|
: null;
|
|
|
|
|
$this->addressSpaceUnitSize = $addressSpaceUnitSize;
|
2024-02-09 23:30:47 +00:00
|
|
|
$this->subSections = $subSections;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-27 01:53:02 +00:00
|
|
|
public function size(): ?int
|
|
|
|
|
{
|
|
|
|
|
return $this->addressRange instanceof AddressRange
|
|
|
|
|
? $this->addressRange->size() * ($this->addressSpaceUnitSize ?? 1)
|
|
|
|
|
: null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getInnermostSubSectionContainingAddressRange(AddressRange $range)
|
2024-02-22 20:54:48 +00:00
|
|
|
: ?MemorySegmentSection {
|
2024-12-27 01:53:02 +00:00
|
|
|
if (!$this->containsAddressRange($range)) {
|
2024-02-22 20:54:48 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($this->subSections as $section) {
|
2024-12-27 01:53:02 +00:00
|
|
|
if ($section->containsAddressRange($range)) {
|
|
|
|
|
return $section->getInnermostSubSectionContainingAddressRange($range);
|
2024-02-22 20:54:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-27 01:53:02 +00:00
|
|
|
public function contains(MemorySegmentSection $other): bool
|
2024-02-09 23:30:47 +00:00
|
|
|
{
|
2024-12-27 01:53:02 +00:00
|
|
|
return $this->addressRange instanceof AddressRange && $this->addressRange->contains($other->addressRange);
|
2024-02-09 23:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-27 01:53:02 +00:00
|
|
|
public function containsAddressRange(AddressRange $range): bool
|
2024-02-22 20:54:48 +00:00
|
|
|
{
|
2024-12-27 01:53:02 +00:00
|
|
|
return $this->addressRange instanceof AddressRange && $this->addressRange->contains($range);
|
2024-02-22 20:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-09 23:30:47 +00:00
|
|
|
public function intersectsWith(MemorySegmentSection $other): bool
|
|
|
|
|
{
|
|
|
|
|
return
|
2024-12-27 01:53:02 +00:00
|
|
|
$this->addressRange instanceof AddressRange
|
|
|
|
|
&& $other->addressRange instanceof AddressRange
|
|
|
|
|
&& $this->addressRange->intersectsWith($other->addressRange)
|
2024-02-09 23:30:47 +00:00
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
}
|