1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//! Shared CPU-cycle IRQ counter used by multiple mappers.
//!
//! Handles the common pattern of a u16 counter ticked on each CPU cycle,
//! with configurable counting direction, threshold, and post-fire behavior.
/// Defines how the IRQ counter ticks and fires.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CpuCycleIrqMode {
/// Count up with mask, fire when counter >= threshold (level-sensitive).
/// Sets `pending = (counter >= threshold)` every tick.
/// Used by: Mapper 42
UpLevel { threshold: u16, mask: u16 },
/// Count up, fire when counter >= threshold, reset counter to 0 on fire.
/// Used by: Mapper 43
UpReset { threshold: u16 },
/// Count up, fire when counter == threshold, auto-disable on fire.
/// Used by: Mapper 50
UpAutoDisable { threshold: u16 },
/// Count up, fire at `fire_count`, self-acknowledge at `ack_count`.
/// Used by: NTDEC 2722 (Mapper 40)
UpSelfAck { fire_count: u16, ack_count: u16 },
/// Count down, fire when counter reaches 0. Counter stops at 0.
/// Used by: Mapper 65, Bandai FCG
DownToZero,
/// Count down, check for 0 before decrement. Auto-disable on fire.
/// Used by: Mapper 67
DownAutoDisable,
/// Count down with wrapping. Fire on underflow (0 → 0xFFFF).
/// `enabled` only gates IRQ assertion, not counting.
/// Used by: Sunsoft FME-7
DownUnderflow,
}
/// A configurable CPU-cycle IRQ counter shared by many mappers.
pub struct CpuCycleIrq {
counter: u16,
enabled: bool,
pending: bool,
reload: u16,
mode: CpuCycleIrqMode,
}
impl CpuCycleIrq {
pub fn new(mode: CpuCycleIrqMode) -> Self {
Self {
counter: 0,
enabled: false,
pending: false,
reload: 0,
mode,
}
}
/// Tick the IRQ counter for one CPU cycle.
pub fn tick(&mut self) {
match self.mode {
CpuCycleIrqMode::UpLevel { threshold, mask } => {
if !self.enabled {
return;
}
self.counter = self.counter.wrapping_add(1) & mask;
self.pending = self.counter >= threshold;
}
CpuCycleIrqMode::UpReset { threshold } => {
if !self.enabled {
return;
}
self.counter = self.counter.wrapping_add(1);
if self.counter >= threshold {
self.counter = 0;
self.pending = true;
}
}
CpuCycleIrqMode::UpAutoDisable { threshold } => {
if !self.enabled {
return;
}
self.counter = self.counter.wrapping_add(1);
if self.counter == threshold {
self.pending = true;
self.enabled = false;
}
}
CpuCycleIrqMode::UpSelfAck {
fire_count,
ack_count,
} => {
if !self.enabled {
return;
}
self.counter = self.counter.wrapping_add(1);
if self.counter == fire_count {
self.pending = true;
} else if self.counter == ack_count {
self.pending = false;
}
}
CpuCycleIrqMode::DownToZero => {
if !self.enabled || self.counter == 0 {
return;
}
self.counter -= 1;
if self.counter == 0 {
self.pending = true;
}
}
CpuCycleIrqMode::DownAutoDisable => {
if !self.enabled {
return;
}
if self.counter == 0 {
self.pending = true;
self.enabled = false;
return;
}
self.counter -= 1;
}
CpuCycleIrqMode::DownUnderflow => {
// enabled only gates IRQ assertion, not counting
self.counter = self.counter.wrapping_sub(1);
if self.counter == 0xFFFF && self.enabled {
self.pending = true;
}
}
}
}
/// Returns whether an IRQ is pending.
pub fn is_pending(&self) -> bool {
self.pending
}
/// Acknowledge (clear) a pending IRQ.
pub fn acknowledge(&mut self) {
self.pending = false;
}
pub fn counter(&self) -> u16 {
self.counter
}
pub fn set_counter(&mut self, value: u16) {
self.counter = value;
}
pub fn enabled(&self) -> bool {
self.enabled
}
pub fn set_enabled(&mut self, value: bool) {
self.enabled = value;
}
pub fn set_pending(&mut self, value: bool) {
self.pending = value;
}
pub fn reload(&self) -> u16 {
self.reload
}
pub fn set_reload(&mut self, value: u16) {
self.reload = value;
}
/// Load counter from reload register.
pub fn reload_counter(&mut self) {
self.counter = self.reload;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_count_up_level_fires_at_threshold_and_clears_below() {
// Mapper 42 style: level-sensitive, 15-bit counter, threshold 0x6000
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::UpLevel {
threshold: 0x6000,
mask: 0x7FFF,
});
irq.set_enabled(true);
// Tick up to threshold - 1: should not be pending
for _ in 0..0x5FFF {
irq.tick();
}
assert!(!irq.is_pending(), "should not be pending below threshold");
// One more tick reaches threshold: should be pending
irq.tick();
assert!(irq.is_pending(), "should be pending at threshold");
// Counter wraps around via mask back below threshold → no longer pending
// 0x7FFF - 0x6000 + 1 = 0x2000 more ticks to wrap around
for _ in 0..0x2000 {
irq.tick();
}
assert!(
!irq.is_pending(),
"should not be pending after wrapping below threshold"
);
}
#[test]
fn test_count_up_reset_fires_and_resets_counter() {
// Mapper 43 style: fire at 0x1000, reset counter
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::UpReset { threshold: 0x1000 });
irq.set_enabled(true);
// Tick to threshold
for _ in 0..0x1000 {
irq.tick();
}
assert!(irq.is_pending(), "should fire at threshold");
assert_eq!(irq.counter(), 0, "counter should reset to 0 on fire");
// Acknowledge and tick again to next threshold
irq.acknowledge();
for _ in 0..0x1000 {
irq.tick();
}
assert!(
irq.is_pending(),
"should fire again after reset and re-counting"
);
assert_eq!(irq.counter(), 0, "counter should reset again");
}
#[test]
fn test_count_up_auto_disable_fires_once() {
// Mapper 50 style: fire at 0x1000, auto-disable
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::UpAutoDisable { threshold: 0x1000 });
irq.set_enabled(true);
for _ in 0..0x1000 {
irq.tick();
}
assert!(irq.is_pending(), "should fire at threshold");
assert!(!irq.enabled(), "should auto-disable on fire");
// Further ticks should not change state (disabled)
irq.acknowledge();
for _ in 0..0x1000 {
irq.tick();
}
assert!(
!irq.is_pending(),
"should not fire again while auto-disabled"
);
}
#[test]
fn test_count_up_self_ack_fires_and_self_acks() {
// NTDEC 2722 style: fire at 4096, self-ack at 8192
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::UpSelfAck {
fire_count: 4096,
ack_count: 8192,
});
irq.set_enabled(true);
// Tick to fire count
for _ in 0..4096 {
irq.tick();
}
assert!(irq.is_pending(), "should fire at fire_count");
// Continue ticking to self-ack count
for _ in 0..4096 {
irq.tick();
}
assert!(!irq.is_pending(), "should self-acknowledge at ack_count");
}
#[test]
fn test_count_down_to_zero_fires_at_zero() {
// Mapper 65 / Bandai FCG style: count down, fire at 0, stop at 0
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::DownToZero);
irq.set_enabled(true);
irq.set_counter(100);
// Tick 100 times to reach 0
for _ in 0..100 {
irq.tick();
}
assert!(irq.is_pending(), "should fire when counter reaches 0");
assert_eq!(irq.counter(), 0, "counter should be 0");
// Further ticks should not change counter (stopped at 0)
irq.acknowledge();
irq.tick();
assert!(!irq.is_pending(), "should not fire again when stopped at 0");
assert_eq!(irq.counter(), 0, "counter should remain 0");
}
#[test]
fn test_count_down_auto_disable_fires_and_disables() {
// Mapper 67 style: check 0 before decrement, auto-disable
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::DownAutoDisable);
irq.set_enabled(true);
irq.set_counter(5);
// Tick 5 times to decrement to 0
for _ in 0..5 {
irq.tick();
}
assert_eq!(
irq.counter(),
0,
"counter should reach 0 after 5 decrements"
);
// Next tick sees counter==0 and fires + auto-disables
irq.tick();
assert!(irq.is_pending(), "should fire when counter is 0");
assert!(!irq.enabled(), "should auto-disable on fire");
}
#[test]
fn test_count_down_underflow_fires_on_wrap() {
// Sunsoft FME-7 style: wrapping decrement, fire on 0→0xFFFF
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::DownUnderflow);
irq.set_enabled(true);
irq.set_counter(2);
// Tick 1: counter goes 2→1
irq.tick();
assert!(!irq.is_pending());
assert_eq!(irq.counter(), 1);
// Tick 2: counter goes 1→0
irq.tick();
assert!(!irq.is_pending());
assert_eq!(irq.counter(), 0);
// Tick 3: counter goes 0→0xFFFF (underflow) → fire
irq.tick();
assert!(irq.is_pending(), "should fire on underflow");
assert_eq!(irq.counter(), 0xFFFF);
}
#[test]
fn test_count_down_underflow_does_not_fire_when_disabled() {
// FME-7: enabled gates IRQ assertion but NOT counting
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::DownUnderflow);
irq.set_enabled(false);
irq.set_counter(1);
// Tick 1: counter 1→0 (counting happens even when disabled)
irq.tick();
assert_eq!(
irq.counter(),
0,
"counter should still decrement when disabled"
);
// Tick 2: counter 0→0xFFFF (underflow, but disabled so no IRQ)
irq.tick();
assert_eq!(irq.counter(), 0xFFFF);
assert!(
!irq.is_pending(),
"should NOT fire on underflow when disabled"
);
}
#[test]
fn test_acknowledge_clears_pending() {
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::DownToZero);
irq.set_enabled(true);
irq.set_counter(1);
irq.tick();
assert!(irq.is_pending());
irq.acknowledge();
assert!(!irq.is_pending(), "acknowledge should clear pending");
}
#[test]
fn test_enabled_gate_prevents_counting() {
// For modes where enabled gates counting (not DownUnderflow)
let mut irq = CpuCycleIrq::new(CpuCycleIrqMode::DownToZero);
irq.set_enabled(false);
irq.set_counter(5);
for _ in 0..10 {
irq.tick();
}
assert_eq!(irq.counter(), 5, "counter should not change when disabled");
assert!(!irq.is_pending(), "should not fire when disabled");
}
}