// SPDX-License-Identifier: MIT //! The pipeline of PM section 7: its classes, and the stage scheduler that times every //! step. //! //! sh2-python's `pipeline.py`. Every decode-table form names one of the classes below //! (``decode.rs``). A class is PM table 7.2's row for it: the stages one instruction //! occupies in the five-stage pipeline (IF ID EX MA WB, plus ``mm`` for the multiplier's //! busy states), the execution states without contention, how many slots the *next* //! instruction's ID stalls, and the figure in PM 7.7 that draws it. The SH-2 numbers are //! used throughout (PM table 7.2 note 1: the SH-1's multiplier stages differ, and PM //! figures 7.14-7.22 and 7.45-7.52 are the SH-1's). //! //! **The model** (PM 7.1-7.5, read off the figures; docs/timing.md "The scheduler"). The //! pipeline is lockstep: time is a sequence of *slots*, one stage of each kind per slot //! at most, and every in-flight instruction moves on at a slot boundary. Which slot each //! stage falls in is structural: //! //! - the instruction after instruction *k* is decoded (ID) in the slot of *k*'s EX, plus //! the ID stall of *k*'s class (MAC.W 1, LDC.L 2, TAS 3, RTE 3, ...; a taken delayed //! branch stalls its slot 1); EX follows ID, and the stages after EX follow in //! consecutive slots as the class draws them (``EX MA EX MA`` for the memory logic //! operations, ``EX EX MA MA EX MA EX EX`` for an interrupt); //! - instruction *k*'s fetch (IF) is in the slot of the previous instruction's ID. A //! fetch is a longword (PM 7.4.2): an instruction at 4n + 2 whose longword the //! previous fetch brought takes no bus cycle (``if``, one state); every other fetch is //! an ``IF`` on the bus (or on the cache bus). //! //! How long a slot lasts depends on what is in it (PM 7.2.3, 7.4, 7.5): //! //! - the longest stage; IF and MA last their memory cycles, the others one state; //! - an MA and an ``IF`` in the same slot split it: the MA first, then the rest, and the //! slot lasts MA + IF (PM 7.4.1); an MA with an ``if`` does not split (PM 7.4.3); //! - a load's MA and the next instruction's EX that uses the loaded register split the //! slot the same way (PM 7.5; not when that instruction is a load to the same register //! or a ``MAC @Rm+,@Rn+``); //! - the multiplier runs ``mm`` states after a multiply's last MA, regardless of slots; //! a later instruction's multiplier MA (the MA of MULS.W, the second MA of MAC.W, //! MAC.L, MUL.L, DMULS.L, DMULU.L, the MA of LDS/STS to and from MACH/MACL, CLRMAC) is //! extended until that ``mm`` ends, as one slot (PM 7.7.2's ``M--A``). //! //! Branches redirect the fetch stream (PM 7.7.5): a taken ``BT``/``BF`` fetches the two //! instructions after it and discards them, and the target's IF is in the slot after //! the branch's EX (three states); a taken delayed branch fetches its slot, stalls the //! slot's ID one slot (three for RTE, PM figure 7.92) and fetches the target in the slot //! of the slot's ID; a taken ``BT/S``/``BF/S`` also fetches and discards the instruction //! after the slot (PM figure 7.80; figure 7.82 draws no such fetch for the unconditional //! ones: C13). TRAPA and the illegal instruction exception fetch the two instructions //! after them and discard them; an interrupt or an address error replaces the //! instruction in its ID, whose fetch and the next one's are discarded; the handler's IF //! is in the slot of the sequence's last stage (PM 7.7.7). These discarded fetches are //! the overrun fetches of U11: real bus cycles (or cache fills), always made. //! //! **The machinery** (plain integers and lists, transcribable). The scheduler keeps a //! cursor: the first slot not yet *finalized*, with its start state. A slot is //! finalized (its fetch made, its length fixed) only when everything in it is known, //! which is when the cursor passes it. ``issue`` gives an instruction its slots before //! its handler runs; each data access the handler makes opens its next MA slot //! (``ma_next``), which moves the cursor there first, so ``chip.clock`` inside a bus //! callback is the state that cycle starts on; ``retire`` closes the MAs, places the //! fetches a branch redirects, and leaves the cursor at the instruction's last MA (or its //! EX). The order of bus cycles is the order of the slots: in a slot, the MA, then the //! fetch. ``chip.clock`` between steps is the start of the cursor's slot, so a step //! returns the states between its cursor and the last one; over a run they add up to //! PM's count (the interval between EX stages, PM 7.3). //! //! ``chip.pipeline_log``, when ``Some`` (default ``None``, no cost but a test), receives //! the scheduler's events: ``If`` for every fetch placed, ``Issue`` for every //! instruction or exception sequence, ``Ma`` for every MA, ``Mm`` for every multiplier //! operation, ``Slot`` for every slot finalized (the reference's tuples, [`Event`]). //! //! What Rust forces: the reference's tests replace ``chip._place`` to make every fetch an //! ``if`` (their "as drawn" figures); a Rust method cannot be replaced, so the chip has //! a ``free_fetch`` flag that does the same after ``place`` (a test seam, off by default). use std::sync::OnceLock; use crate::bus::Bus; use crate::cpu::Sh7604; /// Slots in flight are at most a dozen ahead of the cursor (an interrupt's handler /// fetch, PM figure 7.95); the ring holds the slots from the cursor on. pub const RING: usize = 64; pub const RING_MASK: i64 = RING as i64 - 1; // How an instruction's end redirects the fetch stream (``retire``). pub const KIND_PLAIN: u32 = 0; // the next fetch is sequential pub const KIND_COND: u32 = 1; // BT, BF: taken, two fetches discarded, target IF after EX pub const KIND_COND_DELAYED: u32 = 2; // BT/S, BF/S: taken, a delayed branch plus one discarded fetch pub const KIND_BRANCH: u32 = 3; // BRA, BRAF, BSR, BSRF, JMP, JSR, RTS, RTE: delayed pub const KIND_TRAP: u32 = 4; // TRAPA, illegal instructions: two discarded fetches, then the handler pub const KIND_SLEEP: u32 = 5; // SLEEP: no further fetch pub const KIND_INTERRUPT: u32 = 6; // interrupts, address errors: the replaced instruction's fetches only /// Register bits for the load-use rule: R0-R15 are bits 0-15, PR bit 16. pub const PR_BIT: u32 = 1 << 16; /// One row of PM table 7.2, SH-2 column. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct PipelineClass { pub name: &'static str, pub category: &'static str, // PM table 7.2's "Category" text pub stages: &'static str, // the instruction's own stages, as the figure draws them pub states: i64, // execution states with no contention (table 7.2 "State") pub next_id_stall: i64, // slots the following instruction's ID waits (the figure) pub mm: i64, // multiplier states after the last MA (PM 7.7.2) pub figure: &'static str, } const fn c( name: &'static str, category: &'static str, stages: &'static str, states: i64, stall: i64, mm: i64, figure: &'static str, ) -> PipelineClass { PipelineClass { name, category, stages, states, next_id_stall: stall, mm, figure } } #[rustfmt::skip] pub const CLASSES: [PipelineClass; 27] = [ c("transfer", "Register-register transfer", "IF ID EX", 1, 0, 0, "PM figure 7.10"), c("load", "Memory load", "IF ID EX MA WB", 1, 0, 0, "PM figure 7.11"), c("store", "Memory store", "IF ID EX MA", 1, 0, 0, "PM figure 7.12"), c("arith", "Arithmetic between registers", "IF ID EX", 1, 0, 0, "PM figure 7.13"), c("mac_w", "Multiply/accumulate", "IF ID EX MA MA mm mm", 3, 1, 2, "PM figure 7.23"), c("mac_l", "Double-length multiply/accumulate", "IF ID EX MA MA mm mm mm mm", 3, 1, 4, "PM figure 7.34"), c("mul_w", "Multiplication", "IF ID EX MA mm mm", 1, 0, 2, "PM figure 7.53"), c("mul_l", "Double-length multiplication", "IF ID EX MA MA mm mm mm mm", 2, 1, 4, "PM figure 7.63"), c("logic", "Register-register logic operation", "IF ID EX", 1, 0, 0, "PM figure 7.74"), c("logic_mem", "Memory logic operation", "IF ID EX MA EX MA", 3, 2, 0, "PM figure 7.75"), c("tas", "TAS", "IF ID EX MA EX MA", 4, 3, 0, "PM figure 7.76"), c("shift", "Shift", "IF ID EX", 1, 0, 0, "PM figure 7.77"), c("branch_cond", "Conditional branch", "IF ID EX", 1, 0, 0, "PM figures 7.78, 7.79"), c("branch_cond_delayed", "Delayed conditional branch", "IF ID EX", 1, 0, 0, "PM figures 7.80, 7.81"), c("branch", "Unconditional branch", "IF ID EX", 2, 1, 0, "PM figure 7.82"), c("system", "System control ALU", "IF ID EX", 1, 0, 0, "PM figure 7.83"), c("ldc_l", "LDC.L", "IF ID EX MA EX", 3, 2, 0, "PM figure 7.84"), c("stc_l", "STC.L", "IF ID EX MA", 2, 1, 0, "PM figure 7.85"), c("lds_l_pr", "LDS.L (PR)", "IF ID EX MA WB", 1, 0, 0, "PM figure 7.86"), c("sts_l_pr", "STS.L (PR)", "IF ID EX MA", 1, 0, 0, "PM figure 7.87"), c("reg_to_mac", "Register to MAC transfer", "IF ID EX MA", 1, 0, 0, "PM figure 7.88"), c("mem_to_mac", "Memory to MAC transfer", "IF ID EX MA", 1, 0, 0, "PM figure 7.89"), c("mac_to_reg", "MAC to register transfer", "IF ID EX MA WB", 1, 0, 0, "PM figure 7.90"), c("mac_to_mem", "MAC to memory transfer", "IF ID EX MA", 1, 0, 0, "PM figure 7.91"), c("rte", "RTE", "IF ID EX MA MA", 4, 3, 0, "PM figure 7.92"), c("trap", "TRAP", "IF ID EX EX MA MA MA EX EX", 8, 0, 0, "PM figure 7.93"), c("sleep", "SLEEP", "IF ID EX", 3, 0, 0, "PM figure 7.94"), ]; /// The exception sequences of PM 7.7.7. ``states`` is not in PM table 7.2: it is the /// interval from the sequence's first EX to the handler's first EX in PM figures /// 7.95-7.97 (the handler's IF in the slot of the last stage), which the scheduler /// reproduces (the reference's tests/test_pipeline.py). #[rustfmt::skip] pub const EXCEPTIONS: [PipelineClass; 3] = [ c("interrupt", "Interrupt exception processing", "IF ID EX EX MA MA EX MA EX EX", 9, 0, 0, "PM figure 7.95"), c("address_error", "Address error exception processing", "IF ID EX EX MA MA EX MA EX EX", 9, 0, 0, "PM figure 7.96"), c("illegal", "Illegal instruction exception processing", "IF ID EX EX MA MA MA EX EX", 8, 0, 0, "PM figure 7.97"), ]; /// Extra states of a taken conditional branch over its not-taken 1 (PM table A.50: /// BT/BF 3/1, BT/S/BF/S 2/1). The scheduler produces them; the table is for rung 0. pub const TAKEN_EXTRA: [(&str, i64); 2] = [("branch_cond", 2), ("branch_cond_delayed", 1)]; /// The MA that waits for the multiplier (PM 7.7.2), by class: its index among the /// class's MAs. MULS.W's only MA; the second MA of the MACs and the double-length /// multiplies ("the second MA reads the memory and accesses the multiplier"); the MA /// the MAC transfers add "for accessing the multiplier". fn multiplier_ma(name: &str) -> i64 { match name { "mul_w" => 0, "mac_w" => 1, "mac_l" => 1, "mul_l" => 1, "reg_to_mac" => 0, "mem_to_mac" => 0, "mac_to_reg" => 0, "mac_to_mem" => 0, _ => -1, } } fn kind_of(name: &str) -> u32 { match name { "branch_cond" => KIND_COND, "branch_cond_delayed" => KIND_COND_DELAYED, "branch" => KIND_BRANCH, "rte" => KIND_BRANCH, "trap" => KIND_TRAP, "illegal" => KIND_TRAP, "sleep" => KIND_SLEEP, "interrupt" => KIND_INTERRUPT, "address_error" => KIND_INTERRUPT, _ => KIND_PLAIN, } } /// What the scheduler needs of one class: built once per class by [`plan`]. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Plan { pub mas: Vec, // each MA's slot, counted from the (first) EX pub stall: i64, // the next instruction's ID stall pub mul: i64, // index in ``mas`` of the MA that waits for the multiplier, or -1 pub mm: i64, // multiplier states it starts after that MA pub kind: u32, // KIND_*: how it redirects the fetch stream pub tail: i64, // slots from EX to the last stage (the handler's IF, KIND_TRAP/INTERRUPT) pub name: &'static str, } /// The scheduler's view of a class: the slots of its stages after ID. pub fn plan(pclass: &PipelineClass) -> Plan { let after_id: Vec<&str> = pclass.stages.split_whitespace().skip(2).collect(); let mas: Vec = after_id.iter().enumerate().filter(|(_, s)| **s == "MA").map(|(i, _)| i as i64).collect(); let tail = after_id.iter().enumerate().filter(|(_, s)| **s != "mm").map(|(i, _)| i as i64).max().unwrap(); let stall = if pclass.name == "sleep" { 2 } else { pclass.next_id_stall }; // SLEEP: 3 states Plan { mas, stall, mul: multiplier_ma(pclass.name), mm: pclass.mm, kind: kind_of(pclass.name), tail, name: pclass.name, } } /// ``PLANS``: every class's plan, CLASSES then EXCEPTIONS, in that order. pub fn plans() -> &'static [Plan] { static PLANS: OnceLock> = OnceLock::new(); PLANS.get_or_init(|| CLASSES.iter().chain(EXCEPTIONS.iter()).map(plan).collect()) } /// The index in [`plans`] of the class called ``name``. pub fn plan_index(name: &str) -> usize { plans().iter().position(|p| p.name == name).unwrap_or_else(|| panic!("no pipeline class {name}")) } /// One event of ``chip.pipeline_log`` (the reference's tuples). #[derive(Clone, Debug, PartialEq, Eq)] pub enum Event { /// ``("if", slot, address, bus)``: a fetch placed. If(i64, u32, bool), /// ``("issue", ex_slot, class_name, address)``. Issue(i64, &'static str, u32), /// ``("ma", slot, states)``. Ma(i64, i64), /// ``("mm", state, states)``. Mm(i64, i64), /// ``("slot", slot, states, split)``. Slot(i64, i64, bool), } impl Event { /// The reference's tuple, as Python's ``repr`` writes it. pub fn text(&self) -> String { let b = |v: bool| if v { "True" } else { "False" }; match self { Event::If(slot, address, bus) => format!("('if', {slot}, {address}, {})", b(*bus)), Event::Issue(ex, name, pc) => format!("('issue', {ex}, '{name}', {pc})"), Event::Ma(slot, states) => format!("('ma', {slot}, {states})"), Event::Mm(state, states) => format!("('mm', {state}, {states})"), Event::Slot(slot, states, split) => format!("('slot', {slot}, {states}, {})", b(*split)), } } } fn ring(slot: i64) -> usize { (slot & RING_MASK) as usize } impl Sh7604 { /// An empty pipeline at ``self.clock``; the first fetch is at ``self.pc``. pub(crate) fn pipeline_reset(&mut self) { self.p_if = [-1; RING]; // per slot: the line a bus fetch reads, -2 'if', -1 none self.p_ma = [0; RING]; // per slot: the MA's states, 0 = no MA self.p_lu = [0; RING]; // per slot: 1 when a load-use split falls in it self.p_slot = 0; self.p_start = self.clock; self.p_next_ex = 2; self.p_ex = 0; self.p_fetch_pc = self.pc; self.p_fetch_line = -1; self.p_mm_end = 0; self.p_lu_dest = 0; self.p_lu_slot = -1; // The instruction's MAs while its handler runs. self.p_plan = None; self.p_ma_i = 0; self.p_ma_left = 0; self.p_ma_open = -1; self.p_ma_t0 = 0; self.p_mul = -1; self.p_mm = 0; self.p_ma_is_mul = false; // The two most recently fetched longwords (line address, value). self.fl0 = -1; self.fl1 = -1; self.fv0 = 0; self.fv1 = 0; // The BSC's write buffer (core.rs ``bus_write``): the state at which the // buffered write's external cycles end, and a CCR write waiting behind it (U9: // its state, or 0, and its value). self.wb_end = 0; self.ccr_at = 0; self.ccr_value = 0; // The BSC's refresh (core.rs ``refresh_before``): where the last refresh cycle // ends, and the clock from which an access must look at the BSC first. self.refresh_end = 0; self.bsc_due = self.bsc.refresh_at; self.bsc_due_exists = true; self.pipeline_restart(); } /// Refetch from ``self.pc``: a reset, a loaded state without a pipeline, a host's /// resync. /// /// The fetch at ``pc`` goes in the cursor's slot, the next in the slot after (its /// ID), and the instruction's EX two slots on, as after a branch (PM figure 7.7). pub fn pipeline_restart(&mut self) { for i in 0..RING { self.p_if[i] = -1; self.p_ma[i] = 0; self.p_lu[i] = 0; } let slot = self.p_slot; self.p_start = self.clock; self.p_fetch_line = -1; self.p_lu_dest = 0; self.fl0 = -1; self.fl1 = -1; let target = self.delay_target; self.p_pc = self.pc; self.p_fetch_pc = self.pc; self.place(slot); if target >= 0 { self.p_fetch_pc = target as u32; } self.place(slot + 1); self.p_next_ex = slot + 2; } /// Forget the fetched longwords, so the next opcodes are read again (replay hosts). /// /// Timing is unchanged; the opcode of the next instruction comes from a fresh read /// of the bus. The rung-2 replay uses it because it fills its memory one instruction /// ahead of the core's fetches. pub fn drop_fetched(&mut self) { self.fl0 = -1; self.fl1 = -1; } // -- the fetch stream ---------------------------------------------------------------- /// Put the fetch of the next instruction of the stream in ``slot``. pub(crate) fn place(&mut self, slot: i64) { let address = self.p_fetch_pc; let line = (address & 0xFFFFFFFC) as i64; let request = if address & 2 != 0 && line == self.p_fetch_line { -2 // 'if': the previous fetch brought this longword (PM 7.4.2) } else { line }; self.p_fetch_line = line; self.p_if[ring(slot)] = request; self.p_fetch_pc = address.wrapping_add(2); if let Some(log) = self.pipeline_log.as_mut() { log.push(Event::If(slot, address, request >= 0)); } if self.free_fetch { // The reference's test seam: every fetch an 'if' (its tests' ``place_free``). self.p_if[ring(slot)] = -2; } } /// One IF: read the longword at ``line`` and keep it beside the previous one. fn fetch_into(&mut self, line: i64) { let value = self.read_fetch(line as u32); self.fl1 = self.fl0; self.fv1 = self.fv0; self.fl0 = line; self.fv0 = value; } /// The instruction at ``pc``, from the longword its IF fetched. pub(crate) fn opcode(&mut self, pc: u32) -> u32 { let line = (pc & 0xFFFFFFFC) as i64; let value; if line == self.fl0 { value = self.fv0; } else if line == self.fl1 { value = self.fv1; } else { // Not fetched by the pipeline (a host replaced the PC or dropped the // fetches): read it now, outside the slots' timing. let clock = self.clock; value = self.read_fetch(line as u32); self.clock = clock; self.fl1 = self.fl0; self.fv1 = self.fv0; self.fl0 = line; self.fv0 = value; } if pc & 2 != 0 { value & 0xFFFF } else { value >> 16 } } // -- slots --------------------------------------------------------------------------- /// Finalize every slot before ``target``; ``self.clock`` = the start of ``target``. /// /// Finalizing a slot makes its fetch (after its MA: the MA has priority, PM 7.4.1) /// and fixes its length: MA + IF when an MA meets an ``IF`` or a load-use split, /// else the longest stage (PM 7.2.3, 7.4, 7.5). pub(crate) fn advance(&mut self, target: i64) { let mut slot = self.p_slot; let mut start = self.p_start; if slot < target { while slot < target { let i = ring(slot); let ma = self.p_ma[i]; let request = self.p_if[i]; let length; let split; if request >= 0 { let fetch = start + ma; self.clock = fetch; self.fetch_into(request); let fetched = self.clock - fetch; length = ma + if fetched > 1 { fetched } else { 1 }; split = ma > 0; } else if ma != 0 && self.p_lu[i] != 0 { length = ma + 1; split = true; } else { length = if ma > 1 { ma } else { 1 }; split = false; } self.p_if[i] = -1; self.p_ma[i] = 0; self.p_lu[i] = 0; if let Some(log) = self.pipeline_log.as_mut() { log.push(Event::Slot(slot, length, split)); } start += length; slot += 1; } self.p_slot = slot; self.p_start = start; } self.clock = start; } // -- one instruction ----------------------------------------------------------------- /// Give the next instruction (or sequence) its slots, before its handler runs. /// /// ``dest`` is the register a load writes in WB (bits as ``PR_BIT``), ``uses`` the /// registers the instruction reads or writes for the load-use rule, ``pc`` its /// address (for ``pipeline_log``; an exception sequence's is the instruction it /// replaces). pub(crate) fn issue(&mut self, p: &'static Plan, dest: u32, uses: u32, pc: u32) { let ex = self.p_next_ex; if uses & self.p_lu_dest != 0 && self.p_lu_slot == ex { self.p_lu[ring(ex)] = 1; // PM 7.5: the load's MA and this EX split } self.p_lu_dest = dest; self.p_lu_slot = ex + 1; self.p_ex = ex; self.p_plan = Some(p); self.p_ma_i = 0; self.p_ma_left = p.mas.len(); self.p_mul = p.mul; self.p_mm = p.mm; if p.kind == KIND_PLAIN { // The fetch two instructions on, in the next instruction's ID slot. self.place(ex + p.stall); self.p_next_ex = ex + 1 + p.stall; } if let Some(log) = self.pipeline_log.as_mut() { log.push(Event::Issue(ex, p.name, pc)); } } /// Open the instruction's next MA: move the cursor to its slot (one data access). pub(crate) fn ma_next(&mut self) { if self.p_ma_open >= 0 { self.ma_close(); } let i = self.p_ma_i; let slot = self.p_ex + self.p_plan.unwrap().mas[i]; self.p_ma_i = i + 1; self.p_ma_left -= 1; self.advance(slot); self.p_ma_open = slot; self.p_ma_t0 = self.clock; self.p_ma_is_mul = i as i64 == self.p_mul; } /// Fix the open MA's states: its accesses' cycles, at least one, and the multiplier. /// /// A multiplier MA lasts until the running ``mm`` ends (PM 7.7.2's ``M--A``); a /// multiply then starts its own ``mm`` when its MA ends. fn ma_close(&mut self) { let slot = self.p_ma_open; let t0 = self.p_ma_t0; let mut length = self.clock - t0; if length < 1 { length = 1; } if self.p_ma_is_mul { if self.p_mm_end - t0 > length { length = self.p_mm_end - t0; } if self.p_mm != 0 { self.p_mm_end = t0 + length + self.p_mm; let mm = self.p_mm; if let Some(log) = self.pipeline_log.as_mut() { log.push(Event::Mm(t0 + length, mm)); } } } self.p_ma[ring(slot)] = length; self.p_ma_open = -1; if let Some(log) = self.pipeline_log.as_mut() { log.push(Event::Ma(slot, length)); } } /// After the handler: the MAs it did not use, the redirected fetches, the cursor. /// /// ``sequential`` is the address the next instruction would have without a branch /// (a ``BT``/``BF`` that moved ``self.pc`` elsewhere was taken). pub(crate) fn retire(&mut self, p: &'static Plan, sequential: i64) { while self.p_ma_left != 0 { self.ma_next(); // an MA with no data access: the multiplier's, or a fault's } if self.p_ma_open >= 0 { self.ma_close(); } let kind = p.kind; if kind != 0 { let ex = self.p_ex; if kind == KIND_BRANCH { // PM figures 7.82, 7.92: the slot's ID stalls; the target's IF is in it. self.p_fetch_pc = self.delay_target as u32; self.place(ex + p.stall); self.p_next_ex = ex + 1 + p.stall; } else if kind == KIND_COND_DELAYED { self.place(ex); // PM figure 7.80: fetched and discarded when taken if self.delay_target >= 0 { self.p_fetch_pc = self.delay_target as u32; self.place(ex + 1); self.p_next_ex = ex + 2; } else { self.p_next_ex = ex + 1; } } else if kind == KIND_COND { self.place(ex); if self.pc as i64 != sequential { // PM figure 7.78: two fetches discarded, the target's IF after EX. self.p_fetch_pc = self.pc; self.place(ex + 1); self.place(ex + 2); self.p_next_ex = ex + 3; } else { self.p_next_ex = ex + 1; } } else if kind == KIND_SLEEP { self.p_next_ex = ex + 1 + p.stall; // PM figure 7.94: no further fetch } else { if kind == KIND_TRAP { self.place(ex); // PM figures 7.93, 7.97: the third instruction's IF } // PM 7.7.7: the handler's IF in the slot of the sequence's last stage. self.p_fetch_pc = self.pc; self.place(ex + p.tail); self.place(ex + p.tail + 1); self.p_next_ex = ex + p.tail + 2; } } let mas = &p.mas; let last = if mas.is_empty() { self.p_ex } else { self.p_ex + mas[mas.len() - 1] }; self.advance(last); } /// The scheduler's in-flight state at a step boundary, for ``ChipState``. pub fn pipeline_state(&self) -> Vec { let base = self.p_slot; let mut out = vec![ base, self.p_start, self.p_next_ex, self.p_ex, self.p_fetch_pc as i64, self.p_fetch_line, self.p_mm_end, self.p_lu_dest as i64, self.p_lu_slot, self.fl0, self.fv0 as i64, self.fl1, self.fv1 as i64, self.wb_end, self.ccr_at, self.ccr_value as i64, ]; for slot in base..base + 16 { let i = ring(slot); out.push(self.p_if[i]); out.push(self.p_ma[i]); out.push(self.p_lu[i] as i64); } out } pub(crate) fn load_pipeline_state(&mut self, values: &[i64]) { let base = values[0]; self.p_start = values[1]; self.p_next_ex = values[2]; self.p_ex = values[3]; self.p_fetch_pc = values[4] as u32; self.p_fetch_line = values[5]; self.p_mm_end = values[6]; self.p_lu_dest = values[7] as u32; self.p_lu_slot = values[8]; self.fl0 = values[9]; self.fv0 = values[10] as u32; self.fl1 = values[11]; self.fv1 = values[12] as u32; self.wb_end = values[13]; self.ccr_at = values[14]; self.ccr_value = values[15] as u32; self.p_slot = base; let ring_values = &values[16..]; for i in 0..RING { self.p_if[i] = -1; self.p_ma[i] = 0; self.p_lu[i] = 0; } for k in 0..16 { let i = ring(base + k as i64); self.p_if[i] = ring_values[3 * k]; self.p_ma[i] = ring_values[3 * k + 1]; self.p_lu[i] = ring_values[3 * k + 2] as u8; } self.p_ma_left = 0; self.p_ma_open = -1; self.p_pc = self.pc; } }