| 1 |
struct VRC3 : Chip { |
| 2 |
|
| 3 |
uint4 prg_bank; |
| 4 |
bool irq_mode; |
| 5 |
bool irq_enable; |
| 6 |
bool irq_acknowledge; |
| 7 |
uint16 irq_latch; |
| 8 |
struct { |
| 9 |
union { |
| 10 |
uint16 w; |
| 11 |
struct { uint8 order_lsb2(l, h); }; |
| 12 |
}; |
| 13 |
} irq_counter; |
| 14 |
bool irq_line; |
| 15 |
|
| 16 |
void main() { |
| 17 |
while(true) { |
| 18 |
if(scheduler.sync == Scheduler::SynchronizeMode::All) { |
| 19 |
scheduler.exit(Scheduler::ExitReason::SynchronizeEvent); |
| 20 |
} |
| 21 |
|
| 22 |
if(irq_enable) { |
| 23 |
if(irq_mode == 0) { //16-bit |
| 24 |
if(++irq_counter.w == 0) { |
| 25 |
irq_line = 1; |
| 26 |
irq_enable = irq_acknowledge; |
| 27 |
irq_counter.w = irq_latch; |
| 28 |
} |
| 29 |
} |
| 30 |
if(irq_mode == 1) { //8-bit |
| 31 |
if(++irq_counter.l == 0) { |
| 32 |
irq_line = 1; |
| 33 |
irq_enable = irq_acknowledge; |
| 34 |
irq_counter.l = irq_latch; |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
cpu.set_irq_line(irq_line); |
| 40 |
tick(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
unsigned prg_addr(unsigned addr) const { |
| 45 |
unsigned bank = (addr < 0xc000 ? (unsigned)prg_bank : 0x0f); |
| 46 |
return (bank * 0x4000) + (addr & 0x3fff); |
| 47 |
} |
| 48 |
|
| 49 |
void reg_write(unsigned addr, uint8 data) { |
| 50 |
switch(addr & 0xf000) { |
| 51 |
case 0x8000: irq_latch = (irq_latch & 0xfff0) | ((data & 0x0f) << 0); break; |
| 52 |
case 0x9000: irq_latch = (irq_latch & 0xff0f) | ((data & 0x0f) << 4); break; |
| 53 |
case 0xa000: irq_latch = (irq_latch & 0xf0ff) | ((data & 0x0f) << 8); break; |
| 54 |
case 0xb000: irq_latch = (irq_latch & 0x0fff) | ((data & 0x0f) << 12); break; |
| 55 |
|
| 56 |
case 0xc000: |
| 57 |
irq_mode = data & 0x04; |
| 58 |
irq_enable = data & 0x02; |
| 59 |
irq_acknowledge = data & 0x01; |
| 60 |
if(irq_enable) irq_counter.w = irq_latch; |
| 61 |
break; |
| 62 |
|
| 63 |
case 0xd000: |
| 64 |
irq_line = 0; |
| 65 |
irq_enable = irq_acknowledge; |
| 66 |
break; |
| 67 |
|
| 68 |
case 0xf000: |
| 69 |
prg_bank = data & 0x0f; |
| 70 |
break; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void power() { |
| 75 |
} |
| 76 |
|
| 77 |
void reset() { |
| 78 |
prg_bank = 0; |
| 79 |
irq_mode = 0; |
| 80 |
irq_enable = 0; |
| 81 |
irq_acknowledge = 0; |
| 82 |
irq_latch = 0; |
| 83 |
irq_counter.w = 0; |
| 84 |
irq_line = 0; |
| 85 |
} |
| 86 |
|
| 87 |
void serialize(serializer& s) { |
| 88 |
s.integer(prg_bank); |
| 89 |
s.integer(irq_mode); |
| 90 |
s.integer(irq_enable); |
| 91 |
s.integer(irq_acknowledge); |
| 92 |
s.integer(irq_latch); |
| 93 |
s.integer(irq_counter.w); |
| 94 |
s.integer(irq_line); |
| 95 |
} |
| 96 |
|
| 97 |
VRC3(Board& board) : Chip(board) { |
| 98 |
} |
| 99 |
|
| 100 |
}; |