lonestar

0x10c-inspired space game based on the DCPU-16
git clone git://squid-tech.com/lonestar.git
Log | Files | Refs | README

commit dcbdd6e3f8e9f5598ea77848e504c3eae344dd57
parent 123539dff957142f834ce4815127620f084972fa
Author: Josh Moore <jxm5210@rit.edu>
Date:   Mon, 18 Nov 2019 23:00:34 -0500

Fix bug where PC wasn't being set correctly, fix bug where two devices
could be plugged into one port

Diffstat:
Msrc/LoneStar/Assets/Scripts/DCPU16Controller.cs | 41+++++++++++++++++++++--------------------
1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/src/LoneStar/Assets/Scripts/DCPU16Controller.cs b/src/LoneStar/Assets/Scripts/DCPU16Controller.cs @@ -66,6 +66,9 @@ public class DCPU16Controller : NetworkBehaviour // interrupt address ushort REG_IA; + // whether or not to increment the program counter after the completion of the current instruction + bool INC_PC = true; + // current index in the keyboard input buffer byte kb_offs; @@ -137,7 +140,7 @@ public class DCPU16Controller : NetworkBehaviour } else { - if (hit.transform.parent.gameObject.name.Equals("Ports")) + if (hit.transform.parent.gameObject.name.Equals("Ports") && hit.transform.childCount == 0) { // "plug" the device into the port plugging.transform.parent = hit.transform; @@ -145,7 +148,7 @@ public class DCPU16Controller : NetworkBehaviour } else { - // we clicked the device, but did not click a port, so unplug it + // we clicked the device, but did not click an empty port plugging.transform.parent = null; plugging = null; } @@ -204,14 +207,18 @@ public class DCPU16Controller : NetworkBehaviour void processorStep() { + INC_PC = true; + processInstruction(MEM[REG_PC]); - ushort increment = getInstructionSize( - (byte)((MEM[REG_PC] >> 10) & 0x3F), - (byte)((MEM[REG_PC] >> 5) & 0x1F), - (byte)((MEM[REG_PC]) & 0x1F) - ); - REG_PC += increment; + if (INC_PC) + { + REG_PC += getInstructionSize( + (byte)((MEM[REG_PC] >> 10) & 0x3F), + (byte)((MEM[REG_PC] >> 5) & 0x1F), + (byte)((MEM[REG_PC]) & 0x1F) + ); + } } // hold on to our thread and try to get the CLOCK_HZ we want for CPU cycles @@ -326,11 +333,6 @@ public class DCPU16Controller : NetworkBehaviour break; } } - - for (int i = 0; i < MAX_DEVICES; i++) - { - UnityEngine.Debug.Log(PLUGGED_DEVICES[i]); - } } // power on the DCPU, load the rom, and reset it to a default state @@ -426,7 +428,7 @@ public class DCPU16Controller : NetworkBehaviour else if (dest >= 0x10 && dest < 0x18) { // [register + next word] - MEM[GP_REG[dest - 0x10] + MEM[++REG_PC]] = val; + MEM[GP_REG[dest - 0x10] + MEM[REG_PC + 1]] = val; return; } @@ -442,22 +444,21 @@ public class DCPU16Controller : NetworkBehaviour break; case 0x1A: // PICK - MEM[REG_SP + MEM[++REG_PC]] = val; + MEM[REG_SP + MEM[REG_PC + 1]] = val; break; case 0x1B: REG_SP = val; break; case 0x1C: - // the program counter gets incremented after the instruction is processed, - // so we need to make sure it actually gets set to the correct value - REG_PC = (ushort)(val - 1); + REG_PC = val; + INC_PC = false; break; case 0x1D: REG_EX = val; break; case 0x1E: // [next word] - MEM[MEM[++REG_PC]] = val; + MEM[MEM[REG_PC + 1]] = val; break; case 0x1F: // next word (literal) @@ -798,6 +799,7 @@ public class DCPU16Controller : NetworkBehaviour // JSR a MEM[--REG_SP] = REG_PC; REG_PC = READ(a); + INC_PC = false; break; case 0x08: // INT a @@ -839,7 +841,6 @@ public class DCPU16Controller : NetworkBehaviour if (hwiidx < MAX_DEVICES && PLUGGED_DEVICES[hwiidx] != null) { PLUGGED_DEVICES[hwiidx].GetComponent<DeviceScript>().HWI(this.gameObject, GP_REG); - UnityEngine.Debug.Log("Interrupt Device: " + PLUGGED_DEVICES[hwiidx]); } break; default: