Documentation
¶
Index ¶
- func Disassemble(pc uint32, op uint16) string
- func IsDelayedBranch(op uint16) bool
- type Bus
- type CPU
- func (c *CPU) CachePurge()
- func (c *CPU) CacheStoreHit8(addr uint32, val uint8)
- func (c *CPU) ClearFRTInputCapture()
- func (c *CPU) ClearIRL()
- func (c *CPU) Cycles() uint64
- func (c *CPU) FRT() *FRT
- func (c *CPU) FRTInputCapture()
- func (c *CPU) Halted() bool
- func (c *CPU) INTC() *INTC
- func (c *CPU) IRL() uint32
- func (c *CPU) LoadResetVectors()
- func (c *CPU) NMI()
- func (c *CPU) NMIRequest()
- func (c *CPU) Read8(addr uint32) uint8
- func (c *CPU) Read16(addr uint32) uint16
- func (c *CPU) Read32(addr uint32) uint32
- func (c *CPU) RefreshOnChipInterrupts()
- func (c *CPU) Registers() Registers
- func (c *CPU) Reset()
- func (c *CPU) RunUntil(target uint64)
- func (c *CPU) SetCCR(v uint8)
- func (c *CPU) SetGBR(v uint32)
- func (c *CPU) SetIRL(level uint8, vec uint16)
- func (c *CPU) SetIRLAck(f func(vec uint16))
- func (c *CPU) SetPC(pc uint32)
- func (c *CPU) SetPR(v uint32)
- func (c *CPU) SetReg(n int, v uint32)
- func (c *CPU) SetSR(v uint32)
- func (c *CPU) SetState(s *State)
- func (c *CPU) SetVBR(v uint32)
- func (c *CPU) State() State
- func (c *CPU) Step() bool
- func (c *CPU) SyncClock(to uint64)
- func (c *CPU) Write8(addr uint32, val uint8)
- func (c *CPU) Write16(addr uint32, val uint16)
- func (c *CPU) Write32(addr uint32, val uint32)
- type CacheState
- type DIVU
- type DIVUState
- type DMAC
- func (d *DMAC) Active() bool
- func (d *DMAC) IRQAsserted(ch int) bool
- func (d *DMAC) NextEvent() uint64
- 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) Write(addr uint32, val uint32, now uint64) int
- func (d *DMAC) WriteDRCR(addr uint32, val uint8)
- type DMACChanState
- type DMACState
- type ExecState
- type FRT
- type FRTState
- type INTC
- type INTCState
- type IntSource
- type Registers
- type State
- type WDT
- type WDTState
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.
func IsDelayedBranch ¶ added in v0.2.0
IsDelayedBranch reports whether op is an SH-2 delayed branch, i.e. the following instruction (the delay slot) executes before the branch takes effect. The decode patterns mirror those used by Disassemble above. This is a disassembly aid; the execution core handles delay slots via runtime state (CPU.inDelay/delayPC) rather than opcode classification.
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)
// The SH2* methods are the instruction-execution access path: each
// folds the full bus-access stall (region cost + slave arbitration +
// inter-CPU contention) into the data access and returns the cycles to
// add to the CPU's wait-state debt (reads also return the value). Width
// is in the method name, matching the plain Read*/Write* above, which
// are the non-timed path (on-chip DMAC, exceptions). now is the
// accessing CPU's cycle counter, the system clock both SH-2s keep
// aligned within a sync chunk; slave selects the arbitration penalty.
SH2Read8(addr uint32, now int64, slave bool) (uint8, uint32)
SH2Read16(addr uint32, now int64, slave bool) (uint16, uint32)
SH2Read32(addr uint32, now int64, slave bool) (uint32, uint32)
SH2Write8(addr uint32, val uint8, now int64, slave bool) uint32
SH2Write16(addr uint32, val uint16, now int64, slave bool) uint32
SH2Write32(addr uint32, val uint32, now int64, slave bool) uint32
// AccessCycles returns the CPU state count consumed by a single
// read transaction of the given size (in bytes: 1, 2, 4, or 16)
// at the given address; WriteAccessCycles the same for a write
// (some devices cost differently per direction - SDRAM reads run
// a full burst pipeline while writes are single). Used by the
// DMAC to accumulate accurate bus-occupation stall duration.
AccessCycles(addr uint32, size uint32) uint32
WriteAccessCycles(addr uint32, size uint32) uint32
// ReadCacheLine fills dst from the 16-byte-aligned line at base in
// a single bus tenure, so another bus master cannot interleave a
// write between the four longwords. Per SH7604 Sec 8.4.1 a cache
// line fill reads four longwords consecutively, and for Work RAM-H
// that is one SDRAM burst (Sec 7.5.3-7.5.4) - an indivisible
// transaction the other CPU/DMAC cannot break into. base is the
// big-endian line; dst[0] is base's high byte.
ReadCacheLine(base uint32, dst *[16]byte)
// SH2FillLine is the timed 16-byte line-fill variant (returns the
// access stall). See SH2Read32 for now/slave.
SH2FillLine(base uint32, dst *[16]byte, now int64, slave bool) uint32
// ReadRMW8 begins a TAS.B read-modify-write: it claims the
// address's bus area and returns with the claim held. WriteRMW8
// completes the sequence and releases it. Per SH7604 Sec 7.10 the
// bus is not released between the read and write cycles of TAS.
ReadRMW8(addr uint32) uint8
WriteRMW8(addr uint32, val uint8)
// Timed TAS.B pair (return the access stall). The read half charges
// cost+arbitration+contention and holds the bus; the write half extends
// the busy window and charges only the write cost.
SH2RMWRead(addr uint32, now int64, slave bool) (uint8, uint32)
SH2RMWWrite(addr uint32, val uint8, now int64) 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 CPU ¶
type CPU struct {
// Trace callback: called with (pc, opcode) before each instruction
TraceFunc func(pc uint32, op uint16)
// HLEHook, if non-nil, is invoked from the execute loop when the
// fetched PC falls within the HLE BIOS magic address range. The
// hook is responsible for performing the emulated service in Go.
// The bus returns RTS (+ NOP delay slot) at these addresses, so
// the CPU unwinds to the caller normally after the hook returns.
// Set by HLEBIOS.Boot when the emulator is started without a real
// BIOS image; nil otherwise.
HLEHook func(pc uint32)
// 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) CachePurge ¶ added in v0.3.0
func (c *CPU) CachePurge()
CachePurge purges the cache on behalf of emulated BIOS code running in Go (HLE): on hardware that code executed on this CPU through the cache, so its memory writes were coherent and its footprint evicted resident lines. A full purge at the service boundary restores both properties. Must be called on the CPU's own thread, between instructions.
func (*CPU) CacheStoreHit8 ¶ added in v1.1.0
CacheStoreHit8 applies a byte store at addr to this CPU's cache as the CPU's own store would: in the cache area with the cache enabled, a resident line holding addr is updated (write hit, Section 8.4.2) and nothing is allocated. It does not write memory; the caller does. Used for external memory writes that stand in for a store by this CPU. Must be called with the CPU's thread parked, between instructions.
func (*CPU) ClearFRTInputCapture ¶ added in v0.3.0
func (c *CPU) ClearFRTInputCapture()
ClearFRTInputCapture clears the FRT input-capture latch and re-evaluates the on-chip interrupt line. Used on a system reset (CKCHG/SYSRES): the peer SH-2 that drives this CPU's cross-CPU input capture is turned off, so a SINIT/MINIT capture latched just before the reset must not leave ICF asserted (the request line is the AND of ICF and TIER.ICIE and is otherwise held until software clears ICF).
func (*CPU) ClearIRL ¶
func (c *CPU) ClearIRL()
ClearIRL deasserts the external IRL interrupt request.
func (*CPU) FRT ¶ added in v0.2.0
FRT returns the CPU's free-running timer module. Used by HLE BIOS services that need to configure FRT registers on a CPU whose own setup code we're replacing with Go (e.g., enabling slave FRT input-capture interrupts when the real-BIOS slave-init code that would normally configure them is being skipped).
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) INTC ¶ added in v0.2.0
INTC returns the CPU's interrupt controller. Used by HLE BIOS services that need to set vector / priority registers on a CPU whose own setup code we're replacing with Go.
func (*CPU) IRL ¶ added in v0.3.0
IRL returns the raw external interrupt-request word currently driven by the SCU (level<<16 | vector). Counterpart to SetIRL/ClearIRL.
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) NMIRequest ¶ added in v0.3.0
func (c *CPU) NMIRequest()
NMIRequest asserts an NMI from another goroutine. The CPU applies it on its own thread in Clock, so the register/INTC state NMI touches is only ever mutated by the owning thread.
func (*CPU) Read8 ¶ added in v0.3.0
read8 reads a byte, checking for on-chip peripheral addresses first. Partition routing per SH7604 manual Section 8.3 Table 8.2.
func (*CPU) Read16 ¶ added in v0.3.0
Read8/Read16/Read32 and Write8/Write16/Write32 perform a data access exactly as a MOV would: partition routing per SH7604 manual Section 8.3 Table 8.2, cache fill/update on the cache-area path, inter-CPU bus contention and stall charged to this CPU, and an address error raised on misalignment. They are side-effecting, not neutral peeks.
Read16 reads a 16-bit value with alignment check.
func (*CPU) Read32 ¶ added in v0.3.0
read32 reads a 32-bit value with alignment check. Partition routing per SH7604 manual Section 8.3 Table 8.2.
func (*CPU) RefreshOnChipInterrupts ¶ added in v0.2.0
func (c *CPU) RefreshOnChipInterrupts()
RefreshOnChipInterrupts re-evaluates the level-sensitive request line of every implemented on-chip interrupt source and marks the INTC pending bit for those currently asserting with a non-zero priority.
SH7604 on-chip interrupt requests are level-sensitive: each is the logical AND of a status flag and its enable bit (FRT Sec 11.5, WDT Sec 12.3.2, DIVU Sec 10, DMAC Sec 9), held until software clears the flag, and the INTC samples it continuously (Sec 5). A request can become active with no fresh flag-set edge: by enabling an already-set flag, by a mode change exposing a held flag, or by raising the module priority while a flag is pending. The peripherals' edge callbacks (route*Interrupt) only cover the flag-set edge, so this is called after any write to an interrupt control/enable/mode/ priority register to recover those level transitions. Over-marking is safe: processInterrupt lazily clears a pending bit whose source is no longer asserted.
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) RunUntil ¶ added in v0.4.2
RunUntil runs the CPU until its cycle counter reaches target, stopping at the first instruction boundary at or after it, so the counter may end up to one instruction's cost past target. While the CPU is halted or held off the bus by a burst DMA transfer, the clock jumps to the nearer of the next peripheral deadline and target and fires what is due, so a wake-up interrupt or the transfer end is seen at its cycle.
func (*CPU) SetCCR ¶ added in v0.3.0
SetCCR seeds the cache control register without going through an on-chip register write. Used by the HLE BIOS to reproduce the real BIOS handoff state (docs/bios/handoff_state.md: CCR = $01, cache enabled with all four ways purged). The CP semantics of a real CCR write apply: a set CP bit purges and self-clears (SH7604 manual Section 8.4.6).
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.
func (*CPU) SetIRLAck ¶
SetIRLAck sets the callback invoked when the SH-2 accepts an IRL interrupt. The callback receives the vector the CPU dispatched.
func (*CPU) SetPR ¶ added in v0.2.0
SetPR sets the procedure register. Used by HLE BIOS services that need a bus-served RTS at a magic address to land at a specific PC when the magic address was reached without a preceding JSR (which would have set PR for them).
func (*CPU) SetReg ¶ added in v0.2.0
SetReg sets general-purpose register Rn (n = 0..15). Used by external code (HLE BIOS, boot setup) to seed register state without going through an SH-2 instruction.
func (*CPU) SetSR ¶ added in v0.2.0
SetSR sets the status register. Reserved bits are masked off so no LDC-equivalent invariant is broken.
func (*CPU) SetState ¶ added in v0.3.2
SetState restores a snapshot taken by State. Pure field assignment: the snapshot is internally consistent by construction, so nothing is re-derived.
func (*CPU) State ¶ added in v0.3.2
State returns a complete snapshot of the CPU's serializable state.
func (*CPU) Step ¶ added in v0.4.2
Step runs the CPU for one unit of work: one instruction, charged its full cycle cost with wait states included, or one interrupt acceptance. It returns false without doing anything when the CPU is halted or held off the bus by a burst DMA transfer; RunUntil advances the clock instead.
func (*CPU) SyncClock ¶ added in v0.4.2
SyncClock advances the cycle counter to the shared system clock after time the CPU spent held in reset, without executing anything. The on-chip timers did not count during that time, so they are resynced to the new instant. The emulator calls this when it enables the slave so both CPUs' counters express the same clock to the bus.
func (*CPU) Write8 ¶ added in v0.3.0
write8 writes a byte, checking for on-chip peripheral addresses first.
type CacheState ¶ added in v0.3.2
type CacheState struct {
CCR uint8
Data [4096]byte
Tag [4][64]uint32
Valid [4][64]bool
LRU [64]uint8
}
CacheState holds the cache control register and all four cache arrays. The arrays are one unit: data restored without tags, valid bits, and LRU state is an incoherent cache.
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 DIVUState ¶ added in v0.3.2
type DIVUState struct {
DVSR uint32
DVDNT uint32
DVDNTH uint32
DVDNTL uint32
DVCR uint32
VCRDIV uint32
}
DIVUState holds the division unit registers.
type DMAC ¶
type DMAC struct {
// contains filtered or unexported fields
}
DMAC implements the SH-2 on-chip 2-channel DMA controller.
func (*DMAC) Active ¶ added in v0.4.1
Active returns true while any channel's bus-occupation window is open.
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) NextEvent ¶ added in v0.4.2
NextEvent returns the earliest open window's completion cycle, or math.MaxUint64 when no transfer is in progress.
func (*DMAC) Stalling ¶
Stalling returns true if the DMAC is locking the CPU off the bus. Only burst-mode transfers (CHCR TB, bit 4) hold the bus until the transfer end condition is satisfied.
type DMACChanState ¶ added in v0.3.2
DMACChanState holds one DMA channel's registers.
type DMACState ¶ added in v0.3.2
type DMACState struct {
Ch [2]DMACChanState
DMAOR uint16
DRCR [2]uint8
NextCh int
CompletesAt [2]uint64
}
DMACState holds the on-chip DMA controller state.
type ExecState ¶ added in v0.3.2
type ExecState struct {
PrevPC uint32
DelayPC uint32
InDelay bool
NMIPending bool
// NMIReq is the cross-goroutine NMI request not yet folded into
// NMIPending; it can be set-but-unfolded at a frame boundary.
NMIReq bool
IntInhibit bool
LastLoadReg uint8
MultiplierBusyUntil uint64
NextPeripheralEvent uint64
// Fetch-line memo. Captured verbatim: it stays coherent because
// the cache it resolves into is restored byte-for-byte.
FetchLineAddr uint32
FetchLineWay int
FetchLineOff uint32
SBYCR uint8
BCR1 uint16
}
ExecState holds the pipeline and execution-engine state.
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) ClearInputCapture ¶ added in v0.3.0
func (f *FRT) ClearInputCapture()
ClearInputCapture clears the input-capture flag (ICF) and its read latch. Used on a system reset (CKCHG/SYSRES), which turns off the peer SH-2 that drives cross-CPU input capture, so a capture latched just before the reset must not survive it.
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 FRTState ¶ added in v0.3.2
type FRTState struct {
FRC uint16
OCRA uint16
OCRB uint16
ICR uint16
TIER uint8
FTCSR uint8
ReadFlags uint8
TCR uint8
TOCR uint8
Prescaler uint16
Temp uint8
LastSync uint64
NextEvent uint64
}
FRTState holds the free-running timer state, including the TEMP register for the 8-bit byte-pair access protocol and the FTCSR read-then-write-0 clear tracking, both of which span instructions.
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 INTCState ¶ added in v0.3.2
type INTCState struct {
IPRA uint16
IPRB uint16
VCRA uint16
VCRB uint16
VCRC uint16
VCRD uint16
ICR uint16
VCRWDT uint16
Pending uint16
}
INTCState holds the interrupt controller registers and the pending source bitmask.
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 State ¶ added in v0.3.2
type State struct {
Reg Registers
Cycles uint64
Halted bool
IRL uint32 // packed level<<16 | vector, the SCU-driven line latch
Exec ExecState
Cache CacheState
INTC INTCState
FRT FRTState
DIVU DIVUState
DMAC DMACState
WDT WDTState
}
State is a complete snapshot of the CPU's serializable state for save states. State() and SetState() are pure field copies: nothing is re-derived, invalidated, or recomputed on restore, so a round-trip covers every field with no carve-outs. Excluded are only scratch set fresh before any read (ir, addrError) and construction wiring (bus, callbacks, isMaster).
The consumer serializes these fields itself (the sh2 package exposes state, it does not serialize); field encodings and their stability rules live with the serializer.
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: timer enabled, OVF latch set, and interval mode selected. Watchdog-mode overflow does not raise ITI. TME gates the request: TME is the WDT's only enable bit, and a latched OVF with the timer disabled must not request 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.