# Timing: the 2 MHz CPU, the 1 MHz bus and cycle stretching The single most important timing fact of the machine, and the one the detectors model least well. Citations as in [start-here.md](start-here.md). ## The clocks - 16 MHz crystal, divided by the Video ULA into 8, 4, 2 and 1 MHz (DOC: SM85-1 3.1; SMBP 5.2). - The system 1 MHz clock **1MHzE** is made by a D-type (half of IC34 on the Model B) dividing the 2 MHz clock; the Video ULA's own 1 MHz output goes only to the SAA5050 (DOC: SM85-1 3.1). On the B+ the system clock is **1E**, made in half of IC25 "from the 1MHz and 2MHz outputs of the video processor" (DOC: SMBP 5.2). - Every 1 MHz peripheral (the VIAs, the 6845's bus side, the ACIA, the ADC, FRED and JIM devices) is clocked by 1MHzE, "chosen to allow chips such as 6522 VIAs to use their internal timing elements correctly" (DOC: AUG 28.5). The VIA timers therefore count at 1 MHz (DOC: AUG 22, "decrement at 1MHz"). ## Which accesses are slow | Range | Device | Speed | Source | | --- | --- | --- | --- | | &0000-&7FFF | RAM | 2 MHz | DOC: SM85-1 3.1 ("including accesses to the RAM and ROM") | | &8000-&FBFF, &FF00-&FFFF | paged ROM/RAM, MOS ROM | 2 MHz | same | | &FC00-&FDFF | FRED, JIM (1 MHz bus) | **1 MHz** | DOC: SM85-1 3.1, 3.13; SMBP 5.9 | | &FE00-&FE07 | 6845 CRTC | **1 MHz** | DOC: SM85-1 3.1 ("the 6845 CRT controller") | | &FE08-&FE0F | 6850 ACIA | **1 MHz** | DOC: SM85-1 3.1 | | &FE10-&FE1F | serial ULA | **1 MHz** | DOC: SM85-1 3.1 ("the serial processor") | | &FE20-&FE2F | Video ULA | 2 MHz | not in SM's list of slow devices: DOC by omission | | &FE30-&FE3F | ROM select latch | 2 MHz | same | | &FE40-&FE7F | system and user VIAs | **1 MHz** | DOC: SM85-1 3.1 ("the two VIA's") | | &FE80-&FE9F | 8271 FDC | 2 MHz | DOC by omission; SMBP 5.5.1: "8271 interface timing is controlled by not2M" | | &FEA0-&FEBF | 68B54 ADLC | 2 MHz | DOC by omission (MRM: Econet is a "2MHz External I/O" device) | | &FEC0-&FEDF | uPD7002 ADC | **1 MHz** | DOC: SM85-1 3.1 | | &FEE0-&FEFF | Tube | 2 MHz | DOC: SM85-1 section 2 ("the TUBE works at the full 2 MHz") | "IC23 detects when a slow 1 MHz device is being addressed and it calls for the 6502 to execute a slow clock cycle" (DOC: SM85-1 3.2). On the B+, "each 1MHz peripheral select line is connected to an input of NAND gate IC41" (DOC: SMBP 5.2). The board's decoder holds this table as data, one row per range, with its source. ## How a slow cycle is made Model B (DOC: SM85-1 3.1 and its figure 1.2, "1 MHz stretching"): a D-type (half of IC31) remembers that a 1 MHz cycle was requested; at the right moment "one of the 2 MHz clock cycles is masked off" and the request is cleared. "Depending on the phase relationship between the 1 and 2 MHz clocks at the time of the request, the delay on the 2 MHzE clock is different": the figure draws two cases, **case A**, in which the stretched CPU cycle spans two 2 MHz periods, and **case B**, in which it spans three. The manual gives the program that shows both on an oscilloscope (two `STA &FC00` in a loop). B+ (DOC: SMBP 5.2): "For 1MHz cycles phi2 is held at logic 1 until the 1E signal is synchronised. The cycle ends with both phi2 and 1E falling together"; "there are two ways in which the transition from 2MHz to 1MHz takes place depending on which phase 1E was on when the request was received", figure 2. So, for the specification: - A CPU bus cycle addressing a slow device lasts **2 or 3** 2 MHz periods (1 or 2 extra), and **ends on a falling edge of 1MHzE** (DOC: both manuals). - Which of the two depends only on the phase of 1MHzE when the cycle starts (DOC). The mapping read off the figures is: if the cycle starts with 1MHzE such that a whole 1MHzE high phase can fit before the next falling edge reached within two periods, case A (2 periods); otherwise case B (3 periods). **HYPOTHESIS** (read from a drawing, not measured): to be fixed by the first tier-1 judge, Tom Seddon's `T.TIMINGS` test, whose output `O.TIMINGS` was saved from a real BBC Micro ([validation.md](validation.md)), before any timing-sensitive milestone. - The CPU sees a slow cycle as one long cycle: phi2 is held high (DOC: SMBP 5.2). It is still one bus cycle to the 6502 core; the board charges the extra time and steps the 1 MHz devices and the video through it. **No core change is needed** for this, provided the core's bus callback for each cycle lets the board advance time before it returns (6502-python's and w65c02-python's per-cycle callbacks do: [cpu-and-reuse.md](cpu-and-reuse.md)). - **Double access**: an AUG note warns that a 1 MHz bus device accessed "during a period when the 1MHzE clock is high ... will be accessed immediately" and then again when 1MHzE is next high (DOC: AUG 28.5.2, about devices on the external bus that decode without the cleaned-up page selects). For the internal VIAs, whose chip selects are clocked by 1MHzE, a single access per stretched cycle is assumed (HYPOTHESIS); it matters for read-sensitive registers (a VIA's T1C-L read clears the T1 flag). Unknown U3 in the brief. ## RAM sharing with the video circuit The CPU and the 6845 (or the SAA5050's address path in mode 7) use the RAM on alternate phases of the 2 MHz clock, so **the video never stalls the CPU** (DOC: AUG 18.1 "The 6845 does not interfere with processor access to the memory since the processor and 6845 operate on alternate phases"; SMBP 5.1). The 6845's accesses also refresh the DRAM. There is no contention model to write; there is a phase to keep (the video fetch reads RAM half a 2 MHz cycle away from the CPU's write). ## The Master The Master's CPU is also stretched for "the 1MHz Extension Bus, the Analogue to Digital converter and the Versatile Interface" (DOC: MRM ch. 2), but the same manual's "1MHz Internal I/O" section says "Only one internal I/O component works at this speed - the system VIA", and MSM's parts list marks the 6845 and 6850 "1MHZ" parts. Which Master devices stretch is **conflict C2** in the brief; it is settled for row 3 only, by a test run on a real Master (none in hand yet) or by the MSM schematic's decode. ## What decides timing claims | Claim | Judge | Tier | | --- | --- | --- | | a slow cycle lasts 2 or 3 periods and ends on 1MHzE's fall | SM85-1 figure 1.2, SMBP 5.2 | 2 (manufacturer) | | the phase-to-case mapping | `O.TIMINGS`, `O.VTIMERS`, `O.VPOLL` and the VIA tests' outputs from a real BBC B | 1 (outputs captured on a real unit) | | interrupt-poll timing through stretched cycles | `O.VPOLL`, `O.VIA.I1`, `O.VIA.I2` | 1 | | anything MAME says about it | MAME 0.285 does not stretch in its cycle count (OBSERVED) | 5, and not usable here |