Skip to content

Commit

Permalink
Fix two bugs in COP0 count
Browse files Browse the repository at this point in the history
First, since the internal register is kept in CPU cycles (not RCP cycles),
we need to double the value written via MTC0/DMTC0.

Second, writing a count equal to compare would cause an infinite loop
because the fault would be triggered while PC was on the instruction
doing MTC0 itself, which would be then re-executed at the end of the
exception. On real hardware, in general, when COUNT==COMPARE, the
interrupt happens a few cycles later, enough for PC to move to other
opcodes. Instead of trying to implement this, I've simply made sure
that the interrupt happened after the opcode was executed rather than
before. Also, since the internal counter is in CPU cycles, we make
sure to only raise the CAUSE bit once.
  • Loading branch information
rasky authored and sp1187 committed Jun 13, 2021
1 parent 6abe0f7 commit a56fa4b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions vr4300/cp0.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ int VR4300_DMTC0(struct vr4300 *vr4300,

switch (dest + VR4300_REGISTER_CP0_0)
{
case VR4300_CP0_REGISTER_COUNT:
rt <<= 1;
break;
case VR4300_CP0_REGISTER_CAUSE:
vr4300->regs[VR4300_CP0_REGISTER_CAUSE] &= ~0x0300;
vr4300->regs[VR4300_CP0_REGISTER_CAUSE] |= rt & 0x0300;
Expand Down Expand Up @@ -193,6 +196,9 @@ int VR4300_MTC0(struct vr4300 *vr4300,

switch (dest + VR4300_REGISTER_CP0_0)
{
case VR4300_CP0_REGISTER_COUNT:
rt <<= 1;
break;
case VR4300_CP0_REGISTER_CAUSE:
vr4300->regs[VR4300_CP0_REGISTER_CAUSE] &= ~0x0300;
vr4300->regs[VR4300_CP0_REGISTER_CAUSE] |= (int32_t)rt & 0x0300;
Expand Down
10 changes: 6 additions & 4 deletions vr4300/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ void vr4300_cycle(struct vr4300 *vr4300) {
// Increment counters.
vr4300->regs[VR4300_CP0_REGISTER_COUNT]++;

if ((uint32_t) (vr4300->regs[VR4300_CP0_REGISTER_COUNT] >> 1) ==
(uint32_t) vr4300->regs[VR4300_CP0_REGISTER_COMPARE])
vr4300->regs[VR4300_CP0_REGISTER_CAUSE] |= 0x8000;

// We're stalling for something...
if (pipeline->cycles_to_stall > 0)
pipeline->cycles_to_stall--;

else
vr4300_cycle_(vr4300);

if ((vr4300->regs[VR4300_CP0_REGISTER_COUNT] & 1) == 1 &&
(uint32_t) (vr4300->regs[VR4300_CP0_REGISTER_COUNT] >> 1) ==
(uint32_t) vr4300->regs[VR4300_CP0_REGISTER_COMPARE]) {
vr4300->regs[VR4300_CP0_REGISTER_CAUSE] |= 0x8000;
}
}

// Sets the opaque pointer used for external accesses.
Expand Down

0 comments on commit a56fa4b

Please sign in to comment.