Files
Sylpheed/tools/ppc-manual/control/mtcrf.md
sim dedcf37867 docs(ppc-manual): quote Canary and our own decoder, not the retired xenia-rs
The generator had not been able to run correctly since the manual moved into
`tools/ppc-manual/`: it computed the repository root as `HERE.parent.parent`,
which now names `tools/`, so the XML, Canary's emitters and xenia-rs all stopped
resolving — silently, because both scrapers skipped what they could not find.
Every page's references had been pointing at paths that exist nowhere.

What each source contributed, measured on the 350 pages before this change:

  Operation (pseudocode)  251 pages: fixed boilerplate "derives from the xenia-rs
                          interpreter"; 99 carry real hand-written seeds
  C translation           337 pages: the same kind of boilerplate
  xenia-rs snapshot       336 pages: the interpreter arm, pasted in — the only
                          per-instruction semantics on unseeded pages
  links                   xenia-rs opcode/decoder/interpreter + Canary emitter

Now:

  * semantics come from **Xenia Canary**, the reference emulator, read through
    `git show` at a pinned upstream commit (`origin/canary_experimental`,
    f21ebd49e9). Not our checkout: it carries instrumentation and lacked
    upstream's `mcrf` fix, so it would have published probes and a wrong `mcrf`.
    Each page embeds the emitter (`InstrEmit_<mnem>`), and for the 128 pure
    one-line delegations also the helper that holds the semantics.
  * decode references point at `crates/sylpheed-ppc` — the decoder that
    produces `sylpheed.db` — as in-repo relative links.
  * the boilerplate now says what is true, and the C translation guide maps
    Canary's actual HIR calls, checked against `ppc_hir_builder.h` (including
    that `UpdateCR(n, v)` truncates to 32 bits).
  * `rust_scraper.py` -> `decoder_scraper.py` (interpreter half dropped);
    missing sources are now errors, not empty results.

Verified:

  consistency checks        455 XML entries, 350 families, 598 index keys
  hand-written tails        386/386 byte-identical after regeneration
  xenia-rs in generated     0
  pages with a snapshot     349/350 (was 336) — `dcbi` has no Canary emitter at all
  in-repo decoder links     910/910 resolve to a line holding the identifier
  emitter boundaries        brace counter == column-0 `}` rule on 521/521;
                            preprocessor model unit-tested (#if 0/#else/#elif)
  idempotency               re-run: 0 pages updated, 0 working-tree changes

Hand-written notes (outside the generated regions) are not rewritten here:

  * 110 links into `../../xenia-rs/...` were dead; they now point at the file in
    the archived repository (git.mc02.dev/fabi/xenia-rs @ 8401d4d). Line anchors
    were dropped because the notes predate that commit — 0 of 441 old line
    ranges match it — and a precise-looking wrong anchor is worse than none. The
    link text, which carries the author's line numbers, is unchanged.
  * 140 prose claims about xenia-rs's behaviour remain. 23 are verified to hold
    for Canary too (the 32-bit CR0 truncation, OE left unimplemented); the other
    114 need checking one by one, and some invert — e.g. `divdx` notes a correct
    64-bit CR0 update in xenia-rs where Canary's `UpdateCR` truncates. Left for
    a deliberate pass rather than a blind substitution.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-16 20:34:34 +02:00

6.2 KiB
Raw Blame History

mtcrf — Move to Condition Register Fields

Category: Control / CR / SPR · Form: XFX · Opcode: 0x7c000120

Assembler Mnemonics

Mnemonic XML entry Flags Description
mtcrf mtcrf Move to Condition Register Fields

Syntax

mtcrf [CRM], [RS]

Encoding

mtcrf — form XFX

  • Opcode word: 0x7c000120
  • Primary opcode (bits 05): 31
  • Extended opcode: 144
  • Synchronising: no
Bits Field Meaning
05 OPCD primary opcode (31)
610 RT destination / source GPR
1120 spr/tbr/FXM SPR/TBR number (byte-swapped halves) or CR field mask
2130 XO extended opcode
31 reserved

Operands

Field Role Description
RS mtcrf: read Source GPR (alias for RD in some stores).
CRM mtcrf: write 8-bit CR field mask used by mtcrf — one bit per CR field.

Register Effects

mtcrf

  • Reads (always): RS
  • Reads (conditional): none
  • Writes (always): CRM
  • Writes (conditional): none

Status-Register Effects

No condition-register or status-register effects.

Operation (pseudocode)

for i in 0..7:
    if CRM[i] then CR[i] <- (RS)[32+i*4 : 35+i*4]

C Translation Example

/* No hand-written C yet. Translate the Canary emitter snapshot   */
/* under Implementation References; its HIR maps directly:        */
/*   f.LoadGPR(n) / f.StoreGPR(n, v)  -> r[n] / r[n] = v          */
/*   f.LoadFPR / StoreFPR, f.LoadVR / StoreVR -> f[n], v[n]        */
/*   f.Load(ea, T), f.Store(ea, v) -> raw read / write; emitters   */
/*     wrap them in f.ByteSwap for the big-endian guest value      */
/*   f.UpdateCR(n, v)  -> CR field n from v's LOW 32 BITS vs 0     */
/*   f.LoadCA / f.StoreCA -> xer.CA;  f.StoreSAT -> vscr.SAT       */
/*   i.XO.RA, i.D.DS, ... -> the bit-fields listed under Operands  */
/* The Register Effects and Status-Register Effects tables above  */
/* enumerate every side effect a faithful translation must emit.  */

Implementation References

mtcrf

Canary emitter (frozen snapshot @ f21ebd49e9)
int InstrEmit_mtcrf(PPCHIRBuilder& f, const InstrData& i) {
  // mtocrf FXM,RS
  // count <- 0
  // do i = 0 to 7
  //   if FXMi = 1 then
  //     n <- i
  //     count <- count + 1
  // if count = 1 then
  //   CR4un + 32 : 4un + 35 <- RS4un + 32:4un + 35

  Value* v = f.LoadGPR(i.XFX.RT);
  if (i.XFX.spr & (1 << 9)) {
    uint32_t bits = (i.XFX.spr & 0x1FF) >> 1;
    int count = 0;
    int cri = 0;
    for (int b = 0; b <= 7; ++b) {
      if (bits & (1 << b)) {
        cri = 7 - b;
        ++count;
      }
    }
    if (count == 1) {
      f.StoreCR(cri, v);
    } else {
      // Invalid; store zero to CR.
      f.StoreCR(f.LoadZeroInt64());
    }
  } else {
    uint32_t bits = (i.XFX.spr & 0x1FF) >> 1;
    for (int b = 0; b <= 7; ++b) {
      if (bits & (1 << b)) {
        int cri = 7 - b;
        f.StoreCR(cri, v);
      }
    }
  }
  return 0;
}

Special Cases & Edge Conditions

  • CRM is an 8-bit field-mask, MSB-first. Each bit of CRM corresponds to one CR field: CRM[0] (mask bit 0x80) selects CR0, CRM[1] (0x40) selects CR1, …, CRM[7] (0x01) selects CR7. Each set mask bit causes the corresponding 4-bit slice of RS[32:63] to overwrite that CR field; clear mask bits leave the field untouched.
  • Slice positions inside RS. Big-endian: bits 32..35 of RS map to CR0, bits 36..39 to CR1, …, bits 60..63 to CR7. The high 32 bits of RS are ignored.
  • mtcr RS simplified mnemonic. When CRM = 0xFF, all eight CR fields are written; assemblers fold this into mtcr RS. This is the dominant form (function epilogue restoring the saved CR).
  • mtocrf variant. PowerISA defines mtocrf as the single-field variant — encoded with the high bit of FXM set and exactly one CRM bit set. xenia-rs treats both as the same opcode and processes whatever CRM mask is present, so mtocrf works correctly without special handling.
  • Use case in ABI. Save/restore non-volatile CR fields (CR2, CR3, CR4 on the Xbox 360 ABI). The standard restore is lwz r12, 8(r1); mtcrf 0x38, r120x38 = bits for CR2|CR3|CR4 — preserving the volatile fields the callee may have already updated.
  • No CR0 / XER side effects. mtcrf does not record into CR0; XER is untouched.
  • xenia exact match. xenia-rs decomposes the CR into a u32, applies a per-field mask, and reassembles via set_cr. The 8-bit CRM walk matches the spec exactly.
  • Not synchronising. Reorderable.
  • mfcr — read the entire CR into a GPR.
  • mcrf — copy one CR field to another (no GPR involved).
  • mcrxr, mcrfs — narrower CR-field moves from XER / FPSCR.
  • crand, cror, … — bit-level CR manipulation.

Simplified Mnemonics

Simplified Expansion Notes
mtcr RS mtcrf 0xFF, RS write all eight CR fields from low half of RS

mtocrf RS, FXM is a related encoding handled by the same xenia-rs slot.

IBM Reference