import React, { useId, useState } from "react"; import { ArrowRight, Info, BookOpen, Lightbulb, Warning, } from "@phosphor-icons/react"; import { Markdown } from "./Markdown.jsx"; import { parseLearningBlock, translateAddress, } from "./shared/learning-blocks.js"; function AddressTranslation({ block }) { const [address, setAddress] = useState(block.address), [draft, setDraft] = useState(String(block.address)), [activeRole, setActiveRole] = useState(null); const uid = useId(), max = block.pageSize * block.mappings.length - 1; const result = translateAddress(address, block.pageSize, block.mappings); const change = (value) => { const n = Number(value); setDraft(value); if (value !== "" && Number.isSafeInteger(n) && n >= 0 && n <= max) setAddress(n); }; const update = (n) => { setAddress(n); setDraft(String(n)); }; const part = (role) => ({ onMouseEnter: () => setActiveRole(role), onMouseLeave: () => setActiveRole(null), onFocus: () => setActiveRole(role), onBlur: () => setActiveRole(null), }); return (
Figure Address translation

Virtual address

{result.page .toString(2) .padStart( Math.max(1, Math.ceil(Math.log2(block.mappings.length))), "0", )} {result.offset .toString(2) .padStart(Math.log2(block.pageSize), "0")}

{block.mappings.length} virtual pages
{block.pageSize.toLocaleString("en-US")} bytes per page

Page table

Page Frame
{block.mappings.map((frame, page) => ( ))}

Physical address

{result.frame === null ? (
Not mapped The operating system must handle this access.
) : ( <>
Frame {result.frame}
0 {block.pageSize - 1}

Byte {result.offset}{" "} within the frame

Physical address {result.physical.toLocaleString("en-US")} )}
{result.frame === null ? ( "No physical address for an unmapped page" ) : ( <> {result.frame} × {block.pageSize} + {result.offset} = {result.physical} )}
{block.caption}
); } export function LessonBlock({ code, materialId, sources }) { const block = parseLearningBlock(code); if (!block) return (

This learning block has invalid data.

{code}
); if (block.type === "address-translation") return ; const Icon = { note: Info, definition: BookOpen, example: Lightbulb, warning: Warning, }[block.type]; return ( ); }