mirror of
https://github.com/siemens-mobile-hacks/web-devtools.git
synced 2026-11-25 23:57:40 +00:00
Add C166 calculator
This commit is contained in:
parent
bee15e6c9c
commit
cac2b1682d
8 changed files with 308 additions and 12 deletions
|
|
@ -9,6 +9,7 @@ const SwilibMergePage = lazy(() => import("@/pages/SwilibMerge/SwilibMergePage")
|
|||
const SwilibMergeEditorPage = lazy(() => import("@/pages/SwilibMerge/SwilibMergeEditorPage"));
|
||||
const ReFilesPage = lazy(() => import("@/pages/ReFiles/ReFilesPage"));
|
||||
const Ptr89Page = lazy(() => import("@/pages/Ptr89/Ptr89Page"));
|
||||
const C166CalculatorPage = lazy(() => import("@/pages/C166Calculator/C166CalculatorPage"));
|
||||
|
||||
export const Root: Component = () => {
|
||||
const showAppError = (err: any) => {
|
||||
|
|
@ -26,6 +27,7 @@ export const Root: Component = () => {
|
|||
<Route path="/swilib/merge" component={SwilibMergePage} />
|
||||
<Route path="/swilib/merge/editor" component={SwilibMergeEditorPage} />
|
||||
<Route path="/re/ptr89" component={Ptr89Page} />
|
||||
<Route path="/re/c166-calculator" component={C166CalculatorPage} />
|
||||
<Route path="/re/symbols" component={ReFilesPage} />
|
||||
|
||||
{/* Legacy routes */}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ export const Sidebar: Component<SidebarProps> = (props) => {
|
|||
title: "Reverse engineering",
|
||||
links: [
|
||||
{ label: "Patterns finder", href: "/re/ptr89", icon: "bi bi-search" },
|
||||
{ label: "C166 calculator", href: "/re/c166-calculator", icon: "bi bi-calculator" },
|
||||
{ label: "Download symbols", href: "/re/symbols", icon: "bi bi-download" }
|
||||
]
|
||||
},
|
||||
|
|
|
|||
9
src/pages/C166Calculator/C166CalculatorPage.scss
Normal file
9
src/pages/C166Calculator/C166CalculatorPage.scss
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
.c166-calculator-input {
|
||||
&--bytes {
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
}
|
||||
|
||||
.c166-calculator-input.c166-calculator-input--source {
|
||||
border-color: rgb(var(--bs-primary-rgb));
|
||||
}
|
||||
96
src/pages/C166Calculator/C166CalculatorPage.tsx
Normal file
96
src/pages/C166Calculator/C166CalculatorPage.tsx
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
import { type Component, For } from "solid-js";
|
||||
import { Form } from "solid-bootstrap";
|
||||
import clsx from "clsx";
|
||||
import {
|
||||
type CalculatorFieldName,
|
||||
useC166CalculatorState,
|
||||
} from "@/pages/C166Calculator/store/c166CalculatorState";
|
||||
import "@/pages/C166Calculator/C166CalculatorPage.scss";
|
||||
|
||||
interface CalculatorInputProps {
|
||||
id: string;
|
||||
value: string;
|
||||
active: boolean;
|
||||
stacked?: boolean;
|
||||
ariaLabel?: string;
|
||||
onInput: (value: string) => void;
|
||||
}
|
||||
|
||||
const columns: Array<{
|
||||
label: string;
|
||||
field: CalculatorFieldName;
|
||||
bytesField: CalculatorFieldName;
|
||||
}> = [
|
||||
{ label: "Physical", field: "physicalAddress", bytesField: "physicalAddressBytes" },
|
||||
{ label: "Offset", field: "fileOffset", bytesField: "fileOffsetBytes" },
|
||||
{ label: "SEG:SOF (CODE)", field: "codePointer", bytesField: "codePointerBytes" },
|
||||
{ label: "PAG:POF (DATA)", field: "dataPointer", bytesField: "dataPointerBytes" },
|
||||
];
|
||||
|
||||
const CalculatorInput: Component<CalculatorInputProps> = (props) => (
|
||||
<input
|
||||
id={props.id}
|
||||
size={12}
|
||||
class={clsx(
|
||||
"form-control font-monospace c166-calculator-input",
|
||||
props.stacked && "c166-calculator-input--bytes",
|
||||
props.active && "c166-calculator-input--source"
|
||||
)}
|
||||
value={props.value}
|
||||
aria-label={props.ariaLabel}
|
||||
autocomplete="off"
|
||||
spellcheck={false}
|
||||
onInput={(e) => props.onInput(e.currentTarget.value)}
|
||||
/>
|
||||
);
|
||||
|
||||
const C166CalculatorPage: Component = () => {
|
||||
const [state, calculator] = useC166CalculatorState();
|
||||
|
||||
return (
|
||||
<>
|
||||
<h5 class="mb-3">C166 calculator</h5>
|
||||
<Form.Group class="d-flex flex-wrap align-items-center gap-2 mb-3">
|
||||
<Form.Label for="c166-fullflash-base" class="mb-0 text-nowrap">
|
||||
Fullflash base
|
||||
</Form.Label>
|
||||
<input
|
||||
id="c166-fullflash-base"
|
||||
size={8}
|
||||
maxLength={8}
|
||||
class="form-control font-monospace"
|
||||
value={state.baseValue}
|
||||
autocomplete="off"
|
||||
spellcheck={false}
|
||||
onInput={(e) => calculator.setBaseValue(e.currentTarget.value)}
|
||||
/>
|
||||
</Form.Group>
|
||||
|
||||
<div class="row g-3">
|
||||
<For each={columns}>{(column) =>
|
||||
<Form.Group class="col-auto">
|
||||
<Form.Label for={`c166-${column.field}`}>
|
||||
{column.label}
|
||||
</Form.Label>
|
||||
<CalculatorInput
|
||||
id={`c166-${column.field}`}
|
||||
value={calculator.getFieldValue(column.field)}
|
||||
active={state.source === column.field}
|
||||
onInput={(value) => calculator.setFieldValue(column.field, value)}
|
||||
/>
|
||||
<CalculatorInput
|
||||
id={`c166-${column.bytesField}`}
|
||||
value={calculator.getFieldValue(column.bytesField)}
|
||||
active={state.source === column.bytesField}
|
||||
stacked
|
||||
ariaLabel={`${column.label} bytes`}
|
||||
onInput={(value) => calculator.setFieldValue(column.bytesField, value)}
|
||||
/>
|
||||
</Form.Group>
|
||||
}</For>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default C166CalculatorPage;
|
||||
151
src/pages/C166Calculator/store/c166CalculatorState.ts
Normal file
151
src/pages/C166Calculator/store/c166CalculatorState.ts
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
import { createStoreWithApi } from "@/utils/createStoreWithApi";
|
||||
import {
|
||||
c166CodePointerToPhysical,
|
||||
c166DataPointerToPhysical,
|
||||
physicalToC166CodePointer,
|
||||
physicalToC166DataPointer,
|
||||
} from "@/utils/c166";
|
||||
import { formatAddress, formatLittleEndian32 } from "@/utils/format";
|
||||
|
||||
const MAX_PHYSICAL_ADDRESS = 0xFFFFFF;
|
||||
|
||||
export type CalculatorFieldName =
|
||||
| "physicalAddress"
|
||||
| "physicalAddressBytes"
|
||||
| "fileOffset"
|
||||
| "fileOffsetBytes"
|
||||
| "codePointer"
|
||||
| "dataPointer"
|
||||
| "codePointerBytes"
|
||||
| "dataPointerBytes";
|
||||
|
||||
type CalculatorValues = Record<CalculatorFieldName, string>;
|
||||
|
||||
const formatHex = (value: number, width: number) => value.toString(16).toUpperCase().padStart(width, "0");
|
||||
|
||||
const parseHex = (value: string, maximum: number) => {
|
||||
const normalized = value.trim().replace(/^0x/i, "");
|
||||
if (!normalized || !/^[0-9A-F]+$/i.test(normalized))
|
||||
return;
|
||||
|
||||
const parsed = Number.parseInt(normalized, 16);
|
||||
return parsed <= maximum ? parsed : undefined;
|
||||
};
|
||||
|
||||
const parseLittleEndian32 = (value: string) => {
|
||||
const normalized = value.replace(/\s/g, "");
|
||||
if (!/^[0-9A-F]{8}$/i.test(normalized))
|
||||
return;
|
||||
|
||||
return [0, 2, 4, 6].reduce((result, offset, index) =>
|
||||
result + Number.parseInt(normalized.slice(offset, offset + 2), 16) * 2 ** (index * 8), 0);
|
||||
};
|
||||
|
||||
const parsePointer = (value: string, maximumPage: number, maximumOffset: number) => {
|
||||
const match = value.trim().match(/^(?:0x)?([0-9A-F]+):(?:0x)?([0-9A-F]+)$/i);
|
||||
if (!match)
|
||||
return;
|
||||
|
||||
const page = Number.parseInt(match[1], 16);
|
||||
const offset = Number.parseInt(match[2], 16);
|
||||
return page <= maximumPage && offset <= maximumOffset ? { page, offset } : undefined;
|
||||
};
|
||||
|
||||
const parsePackedPointer = (value: string, maximumPage: number, maximumOffset: number) => {
|
||||
const packed = parseLittleEndian32(value);
|
||||
if (packed === undefined)
|
||||
return;
|
||||
|
||||
const page = Math.floor(packed / 0x10000);
|
||||
const offset = packed % 0x10000;
|
||||
return page <= maximumPage && offset <= maximumOffset ? { page, offset } : undefined;
|
||||
};
|
||||
|
||||
const getPhysicalAddress = (field: CalculatorFieldName, value: string, base: number): number | undefined => {
|
||||
switch (field) {
|
||||
case "physicalAddress":
|
||||
return parseHex(value, MAX_PHYSICAL_ADDRESS);
|
||||
case "physicalAddressBytes": {
|
||||
const address = parseLittleEndian32(value);
|
||||
return address !== undefined && address <= MAX_PHYSICAL_ADDRESS ? address : undefined;
|
||||
}
|
||||
case "fileOffset": {
|
||||
const offset = parseHex(value, MAX_PHYSICAL_ADDRESS);
|
||||
return offset !== undefined && base + offset <= MAX_PHYSICAL_ADDRESS ? base + offset : undefined;
|
||||
}
|
||||
case "fileOffsetBytes": {
|
||||
const offset = parseLittleEndian32(value);
|
||||
return offset !== undefined && base + offset <= MAX_PHYSICAL_ADDRESS ? base + offset : undefined;
|
||||
}
|
||||
case "codePointer": {
|
||||
const pointer = parsePointer(value, 0xFF, 0xFFFF);
|
||||
return pointer ? c166CodePointerToPhysical(pointer.page, pointer.offset) : undefined;
|
||||
}
|
||||
case "dataPointer": {
|
||||
const pointer = parsePointer(value, 0x3FF, 0x3FFF);
|
||||
return pointer ? c166DataPointerToPhysical(pointer.page, pointer.offset) : undefined;
|
||||
}
|
||||
case "codePointerBytes": {
|
||||
const pointer = parsePackedPointer(value, 0xFF, 0xFFFF);
|
||||
return pointer ? c166CodePointerToPhysical(pointer.page, pointer.offset) : undefined;
|
||||
}
|
||||
case "dataPointerBytes": {
|
||||
const pointer = parsePackedPointer(value, 0x3FF, 0x3FFF);
|
||||
return pointer ? c166DataPointerToPhysical(pointer.page, pointer.offset) : undefined;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const calculateValues = (physicalAddress: number, base: number): CalculatorValues => {
|
||||
const codePointer = physicalToC166CodePointer(physicalAddress);
|
||||
const dataPointer = physicalToC166DataPointer(physicalAddress);
|
||||
const fileOffset = physicalAddress >= base ? physicalAddress - base : undefined;
|
||||
|
||||
return {
|
||||
physicalAddress: formatAddress(physicalAddress),
|
||||
physicalAddressBytes: formatLittleEndian32(physicalAddress),
|
||||
fileOffset: fileOffset === undefined ? "" : formatAddress(fileOffset),
|
||||
fileOffsetBytes: fileOffset === undefined ? "" : formatLittleEndian32(fileOffset),
|
||||
codePointer: `${formatHex(codePointer.segment, 2)}:${formatHex(codePointer.offset, 4)}`,
|
||||
dataPointer: `${formatHex(dataPointer.page, 4)}:${formatHex(dataPointer.offset, 4)}`,
|
||||
codePointerBytes: formatLittleEndian32(codePointer.segment * 0x10000 + codePointer.offset),
|
||||
dataPointerBytes: formatLittleEndian32(dataPointer.page * 0x10000 + dataPointer.offset),
|
||||
};
|
||||
};
|
||||
|
||||
export const useC166CalculatorState = createStoreWithApi(
|
||||
() => ({
|
||||
baseValue: "0x000000",
|
||||
base: 0,
|
||||
physicalAddress: 0,
|
||||
source: "physicalAddress" as CalculatorFieldName,
|
||||
sourceValue: "00000000",
|
||||
}),
|
||||
(state, setState) => ({
|
||||
getFieldValue(field: CalculatorFieldName) {
|
||||
return state.source === field ?
|
||||
state.sourceValue :
|
||||
calculateValues(state.physicalAddress, state.base)[field];
|
||||
},
|
||||
setFieldValue(field: CalculatorFieldName, value: string) {
|
||||
setState({ source: field, sourceValue: value });
|
||||
|
||||
const address = getPhysicalAddress(field, value, state.base);
|
||||
if (address !== undefined)
|
||||
setState("physicalAddress", address);
|
||||
},
|
||||
setBaseValue(value: string) {
|
||||
setState("baseValue", value);
|
||||
|
||||
const base = parseHex(value, MAX_PHYSICAL_ADDRESS);
|
||||
if (base === undefined)
|
||||
return;
|
||||
|
||||
setState("base", base);
|
||||
const address = getPhysicalAddress(state.source, state.sourceValue, base);
|
||||
if (address !== undefined)
|
||||
setState("physicalAddress", address);
|
||||
},
|
||||
}),
|
||||
{ persist: { name: "c166Calculator", debounce: 250 } }
|
||||
);
|
||||
|
|
@ -5,7 +5,6 @@ import {
|
|||
type Ptr89SearchType,
|
||||
type Ptr89XRefType,
|
||||
} from "@sie-js/ptr89";
|
||||
import { formatAddress } from "@/utils/format";
|
||||
import {
|
||||
type AddressSpace,
|
||||
type FullflashMatch,
|
||||
|
|
@ -13,6 +12,11 @@ import {
|
|||
type XRefResult,
|
||||
usePtr89State,
|
||||
} from "@/pages/Ptr89/store/ptr89State";
|
||||
import {
|
||||
physicalToC166CodePointer,
|
||||
physicalToC166DataPointer,
|
||||
} from "@/utils/c166";
|
||||
import { formatAddress, formatLittleEndian32 } from "@/utils/format";
|
||||
|
||||
interface SearchResultsProps {
|
||||
result: SearchResult;
|
||||
|
|
@ -22,18 +26,14 @@ interface XRefResultsProps {
|
|||
result: XRefResult;
|
||||
}
|
||||
|
||||
const formatAddressBytes = (value: number) => [0, 8, 16, 24]
|
||||
.map((shift) => ((value >>> shift) & 0xFF).toString(16).toUpperCase().padStart(2, "0"))
|
||||
.join(" ");
|
||||
|
||||
const formatC166Pointer = (value: number, addressSpace: AddressSpace) => {
|
||||
const pageSize = addressSpace === "data" ? 0x4000 : 0x10000;
|
||||
const pageWidth = addressSpace === "data" ? 4 : 2;
|
||||
const offset = value % pageSize;
|
||||
const page = Math.floor(value / pageSize);
|
||||
const pointer = addressSpace === "data" ?
|
||||
physicalToC166DataPointer(value) :
|
||||
physicalToC166CodePointer(value);
|
||||
const page = "page" in pointer ? pointer.page : pointer.segment;
|
||||
return {
|
||||
pointer: `${page.toString(16).toUpperCase().padStart(pageWidth, "0")}:${offset.toString(16).toUpperCase().padStart(4, "0")}`,
|
||||
bytes: formatAddressBytes((page << 16) | offset),
|
||||
pointer: `${page.toString(16).toUpperCase().padStart(addressSpace === "data" ? 4 : 2, "0")}:${pointer.offset.toString(16).toUpperCase().padStart(4, "0")}`,
|
||||
bytes: formatLittleEndian32(page * 0x10000 + pointer.offset),
|
||||
};
|
||||
};
|
||||
|
||||
|
|
@ -44,7 +44,7 @@ const AddressCell: Component<{ address: number; showBytes: boolean; fullflashNam
|
|||
</Show>
|
||||
<div>{formatAddress(props.address)}</div>
|
||||
<Show when={props.showBytes}>
|
||||
<code class="d-block text-nowrap">{formatAddressBytes(props.address)}</code>
|
||||
<code class="d-block text-nowrap">{formatLittleEndian32(props.address)}</code>
|
||||
</Show>
|
||||
</td>
|
||||
);
|
||||
|
|
|
|||
31
src/utils/c166.ts
Normal file
31
src/utils/c166.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
export interface C166CodePointer {
|
||||
segment: number;
|
||||
offset: number;
|
||||
}
|
||||
|
||||
export interface C166DataPointer {
|
||||
page: number;
|
||||
offset: number;
|
||||
}
|
||||
|
||||
export function physicalToC166CodePointer(address: number): C166CodePointer {
|
||||
return {
|
||||
segment: Math.floor(address / 0x10000),
|
||||
offset: address % 0x10000,
|
||||
};
|
||||
}
|
||||
|
||||
export function c166CodePointerToPhysical(segment: number, offset: number): number {
|
||||
return segment * 0x10000 + offset;
|
||||
}
|
||||
|
||||
export function physicalToC166DataPointer(address: number): C166DataPointer {
|
||||
return {
|
||||
page: Math.floor(address / 0x4000),
|
||||
offset: address % 0x4000,
|
||||
};
|
||||
}
|
||||
|
||||
export function c166DataPointerToPhysical(page: number, offset: number): number {
|
||||
return page * 0x4000 + offset;
|
||||
}
|
||||
|
|
@ -5,3 +5,9 @@ export function formatId(id: number) {
|
|||
export function formatAddress(address: number) {
|
||||
return address.toString(16).padStart(8, '0').toUpperCase();
|
||||
}
|
||||
|
||||
export function formatLittleEndian32(value: number) {
|
||||
return [0, 8, 16, 24]
|
||||
.map((shift) => (Math.floor(value / 2 ** shift) % 0x100).toString(16).toUpperCase().padStart(2, "0"))
|
||||
.join(" ");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue