Documentation
¶
Index ¶
- func Disassemble(pc uint32, op uint16) string
- type Bus
- type BusActivity
- type CPU
- func (c *CPU) ClearIRL()
- func (c *CPU) Clock() ClockState
- func (c *CPU) Cycles() uint64
- func (c *CPU) FRTInputCapture()
- func (c *CPU) Halted() bool
- func (c *CPU) LoadResetVectors()
- func (c *CPU) NMI()
- func (c *CPU) Registers() Registers
- func (c *CPU) Reset()
- func (c *CPU) SetIRL(level uint8, vec uint16)
- func (c *CPU) SetIRLAck(f func())
- func (c *CPU) SetPC(pc uint32)
- type ClockState
- type DIVU
- type DMAC
- func (d *DMAC) IRQAsserted(ch int) bool
- func (d *DMAC) Read(addr uint32) uint32
- func (d *DMAC) ReadDRCR(addr uint32) uint8
- func (d *DMAC) Reset()
- func (d *DMAC) Stalling() bool
- func (d *DMAC) Tick() int
- func (d *DMAC) Write(addr uint32, val uint32) int
- func (d *DMAC) WriteDRCR(addr uint32, val uint8)
- type FRT
- type INTC
- type IntSource
- type Registers
- type WDT
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Disassemble ¶
Disassemble decodes a 16-bit SH-2 instruction and returns a human-readable string. The pc parameter is the address of the instruction, used for PC-relative address calculation.
Types ¶
type Bus ¶
type Bus interface {
Read8(addr uint32) uint8
Read16(addr uint32) uint16
Read32(addr uint32) uint32
Write8(addr uint32, val uint8)
Write16(addr uint32, val uint16)
Write32(addr uint32, val uint32)
// AccessCycles returns the CPU state count consumed by a single
// bus transaction of the given size (in bytes: 1, 2, 4, or 16)
// at the given address. Used by the DMAC to accumulate accurate
// bus-occupation stall duration.
AccessCycles(addr uint32, size uint32) uint32
}
Bus provides memory access for the SH-2 CPU. Addresses are 32-bit. The SH-2 uses big-endian byte ordering. Word and longword accesses must be naturally aligned; the CPU raises an address error before reaching the bus on misaligned access.
type BusActivity ¶
type BusActivity uint8
BusActivity describes what bus operation the CPU performed on a clock cycle.
const ( BusNone BusActivity = iota // No bus access BusRead // Memory read BusWrite // Memory write BusHeld // Bus held for atomic operation (TAS.B) )
type CPU ¶
type CPU struct {
// Trace callback: called with (pc, opcode) before each instruction
TraceFunc func(pc uint32, op uint16)
// contains filtered or unexported fields
}
CPU implements the Hitachi SH-2 processor core.
func New ¶
New creates a CPU connected to the given bus in power-on state. master indicates whether this is the master (true) or slave (false) SH-2. SR has all interrupts masked. Call LoadResetVectors() once the bus has valid data to set the initial PC and SP.
func (*CPU) ClearIRL ¶
func (c *CPU) ClearIRL()
ClearIRL deasserts the external IRL interrupt request.
func (*CPU) Clock ¶
func (c *CPU) Clock() ClockState
Clock advances the CPU by exactly one cycle and returns per-cycle state.
func (*CPU) FRTInputCapture ¶
func (c *CPU) FRTInputCapture()
FRTInputCapture triggers input capture on the FRT. Called by the coordinator when it detects a write to MINIT/SINIT addresses. Sync live FRT state before the capture so ICR latches the current FRC value (Section 11.4.5, Figure 11.10).
func (*CPU) LoadResetVectors ¶
func (c *CPU) LoadResetVectors()
LoadResetVectors reads the initial PC from address 0x00000000 and SP from address 0x00000004.
func (*CPU) NMI ¶
func (c *CPU) NMI()
NMI requests a non-maskable interrupt and asserts the NMIL bit in the ICR register so that software can observe the NMI pin state. NMI always has priority level 16 and uses vector 11. Tracked independently of the IPR-level sources via a dedicated flag.
Per manual Sec 9.2.6, an NMI input also sets DMAOR.NMIF (bit 1). transferReady gates new transfers on dmaor&0x07 == 0x01, so setting NMIF here prevents any subsequent CHCR/DMAOR-triggered DMA start until software writes 0 to DMAOR bit 1 to clear NMIF.
func (*CPU) Reset ¶
func (c *CPU) Reset()
Reset returns the CPU to its power-on state, clearing programmer-visible registers and on-chip peripherals, then reloading PC/SP from the reset vectors. This is what real hardware does when the slave receives SSHON (after SSHOFF) - the reset line is pulsed and the CPU re-enters its reset-vector boot sequence. Games rely on this to re-read their game-supplied slave entry pointer after updating it.
func (*CPU) SetIRL ¶
SetIRL updates the external IRL interrupt level driven by the SCU. IRL interrupts are level-triggered: the level reflects the current state of the IRL pins and is sampled each cycle. When the SCU clears the interrupt source, it calls ClearIRL to deassert the request.
type ClockState ¶
type ClockState struct {
Bus BusActivity // Bus activity this clock cycle
LoadUseStall bool // Next instruction stalls on this load result
BranchTaken bool // A branch was resolved (taken) this clock cycle
}
ClockState describes what the CPU did during a Clock() call.
type DIVU ¶
type DIVU struct {
// contains filtered or unexported fields
}
DIVU implements the SH-2 on-chip hardware division unit. It performs 32/32 -> 32 and 64/32 -> 32 signed division in hardware, triggered by writing the dividend register.
func (*DIVU) IRQAsserted ¶
IRQAsserted returns true when the DIVU overflow interrupt line is currently driven to the INTC: OVF latch (bit 0) is set AND OVFIE enable (bit 1) is set. Latch remains asserted until software clears DVCR.OVF.
func (*DIVU) Read ¶
Read reads a DIVU register by full address (0xFFFFFF00-0xFFFFFF14). The SH-2 also exposes non-destructive aliases of DVDNTH and DVDNTL at 0xFFFFFF18 and 0xFFFFFF1C, returning the remainder and quotient of the last division without affecting DIVU state. The SH-7604 manual documents these addresses as reserved, but the real hardware mirrors DVDNTH/DVDNTL there and NiGHTS relies on reading the quotient from 0xFFFFFF1C in its vertex projection code.
type DMAC ¶
type DMAC struct {
// contains filtered or unexported fields
}
DMAC implements the SH-2 on-chip 2-channel DMA controller. Data is moved instantly when triggered, but the CPU stalls for TCR * 20 cycles to simulate bus occupation. TE and the optional interrupt fire when the stall countdown reaches zero.
func (*DMAC) IRQAsserted ¶
IRQAsserted returns true when the DMAC channel's transfer-end interrupt line is currently driven to the INTC: TE latch (bit 1) is set AND IE enable (bit 2) is set. Latch remains asserted until software clears CHCR.TE.
func (*DMAC) Tick ¶
Tick decrements the stall countdown by one cycle. When the countdown reaches zero, TE is set on the completed channel. Returns the channel number that completed, or -1 if no completion occurred.
type FRT ¶
type FRT struct {
// contains filtered or unexported fields
}
FRT implements the SH-2 on-chip free-running timer. The FRT provides a 16-bit free-running counter, output compare, and input capture. Input capture is used for inter-CPU communication in the Saturn (MINIT/SINIT writes).
func (*FRT) IRQFlags ¶
IRQFlags returns the currently asserted FRT interrupt flags masked by their enable bits. Bit positions match FTCSR/TIER: ICF=0x80, OCFA=0x08, OCFB=0x04, OVF=0x02. Non-zero means an interrupt is being asserted to the INTC. Flags remain asserted until software writes 0 to the corresponding FTCSR bit.
func (*FRT) InputCapture ¶
InputCapture latches the current FRC value into ICR and sets the input capture flag. Used by MINIT/SINIT writes. Returns true if the ICF interrupt is enabled.
type INTC ¶
type INTC struct {
// contains filtered or unexported fields
}
INTC implements the SH-2 on-chip interrupt controller. It manages priority levels for internal interrupt sources (FRT, DIVU, etc.) and routes them to the CPU.
func (*INTC) AssertSource ¶
AssertSource marks an interrupt source as possibly asserted. Called by a peripheral's route function when the peripheral's status flag transitions high. processInterrupt reconciles stale-true bits lazily.
func (*INTC) Read ¶
Read reads an INTC register. The addr parameter is the full 32-bit address (0xFFFFFE60-0xFFFFFEE4).
func (*INTC) Reset ¶
func (i *INTC) Reset()
Reset returns the INTC to power-on state. Per manual Sec 5.3.8 the ICR's initial value is H'8000 when the NMI input pin is high and H'0000 when it is low. On the Saturn the NMI pin is pulled high at boot (SMPC asserts it briefly only on reset-button presses), so the NMIL bit (bit 15) is set.
func (*INTC) SetNMIL ¶
SetNMIL sets or clears the NMIL bit (bit 15) of the ICR register. NMIL is a read-only bit that reflects the NMI input pin level.
func (*INTC) Write ¶
Write writes an INTC register. The addr parameter is the full 32-bit address (0xFFFFFE60-0xFFFFFEE4).
Reserved bits are masked to zero per manual Sec 5.3:
- IPRA (0xFFFFFEE2): bits 3-0 reserved -> 0xFFF0
- IPRB (0xFFFFFE60): bits 7-0 reserved -> 0xFF00
- VCRA (0xFFFFFE62): bits 15, 7 reserved -> 0x7F7F
- VCRB (0xFFFFFE64): bits 15, 7 reserved -> 0x7F7F
- VCRC (0xFFFFFE66): bits 15, 7 reserved -> 0x7F7F
- VCRD (0xFFFFFE68): bits 15, 7-0 reserved -> 0x7F00
- VCRWDT(0xFFFFFEE4): bits 15, 7 reserved -> 0x7F7F
- ICR (0xFFFFFEE0): bit 15 NMIL read-only, only bits 8 and 0 writable
type IntSource ¶
type IntSource uint8
IntSource identifies an on-chip interrupt source. Values are ordered by manual Sec 5.2.5 Table 5.4 fixed tie-break priority (high -> low). Iterating the pending bitmask from LSB to MSB picks the higher priority source on equal IPR-level ties.
type Registers ¶
type Registers struct {
R [16]uint32 // General purpose registers (R15 is stack pointer)
PC uint32 // Program counter
PR uint32 // Procedure register (return address for BSR/JSR)
SR uint32 // Status register
GBR uint32 // Global base register
VBR uint32 // Vector base register
MACH uint32 // Multiply-accumulate high
MACL uint32 // Multiply-accumulate low
}
Registers holds the programmer-visible state of the SH-2.
type WDT ¶
type WDT struct {
// contains filtered or unexported fields
}
WDT implements the SH-2 on-chip Watchdog Timer (manual Sec 12). It consists of an 8-bit free-running counter (WTCNT) clocked from the CPU via a prescaler, a control/status register (WTCSR) for enable / mode / CKS / overflow flag, and a reset control register (RSTCSR). Two modes are selected by WTCSR.WT/IT:
- Interval mode (WT/IT=0): overflow sets WTCSR.OVF and raises an interval-interrupt (ITI) through the INTC. The latch is sticky until software clears it via the A5-keyed word-write path.
- Watchdog mode (WT/IT=1): overflow sets RSTCSR.WOVF and would trigger a CPU reset on hardware. Reset is NOT modeled here a log line surfaces the event.
All three registers share a word-write protection scheme (manual Sec 12.2.4): WTCSR/WTCNT live at 0xFFFFFE80 (word-aligned) with high-byte 0xA5 selecting WTCSR and 0x5A selecting WTCNT; RSTCSR at 0xFFFFFE82 accepts 0xA500 (clear WOVF) or 0x5A?? (update RSTE/RSTS). Any other high byte discards the write.
The CPU drives the WDT via deadline scheduling: nextEvent holds the absolute CPU cycle at which the next WTCNT wrap will occur, and tickPeripherals compares against that instead of calling into the WDT every cycle. syncTo reconciles lastSync->now on demand (register access, deadline hit, reset).
func (*WDT) IRQAsserted ¶
IRQAsserted returns true when the WDT is driving an interval interrupt request line: OVF latch set AND interval mode selected. Watchdog-mode overflow does not raise ITI.
func (*WDT) Read ¶
Read reads a WDT register byte. Read addresses per manual Sec 12.1.4 Table 12.2:
- 0xFFFFFE80: WTCSR
- 0xFFFFFE81: WTCNT
- 0xFFFFFE82: undefined; returns 0.
- 0xFFFFFE83: RSTCSR
RSTCSR's read address is offset +1 from its write address because its word-write places the register value in the low byte of the word (big-endian), so byte-wise the register lives at 0xFFFFFE83.