Currently our processor is limited to 1M (2 ^ 20) instructions via a static array size. Our project requirements need us to support an arbitrarily long simulation, with numbers in the several million being thrown around.
Given these parameters, eventually we are going to reach the last instruction in our array, and we need to ensure that we do not step out of bounds and cause the simulation to fail.
Implementation
We can get around this by always generating an unconditional jump to the beginning as the last instruction. in assembly this instruction would be j 0x0
This will work as follows:
When we shift left 2 the jump immediate, it goes from 26 bits of 0 to 28 bits of 0.
We only have 2^20 instructions so the top 12 bits of the program counter are never used. This means that the top 4 bits that we concatenate with the 28 bit address immediate is always0000
Altogether this gives us a memory address of 0x00000000 which when we floor divide by 4 gives us the index of the first instruction in memory, 0.
Issue
Currently our processor is limited to 1M (2 ^ 20) instructions via a static array size. Our project requirements need us to support an arbitrarily long simulation, with numbers in the several million being thrown around.
Given these parameters, eventually we are going to reach the last instruction in our array, and we need to ensure that we do not step out of bounds and cause the simulation to fail.
Implementation
We can get around this by always generating an unconditional jump to the beginning as the last instruction. in assembly this instruction would be
j 0x0
This will work as follows:
0
to 28 bits of0
.0000
0x00000000
which when we floor divide by 4 gives us the index of the first instruction in memory, 0.