Skip to content
Snippets Groups Projects
Commit 6fe9fa3f authored by James, Ben's avatar James, Ben Committed by Sella, Iftach
Browse files

Step/main/branch

parent 1f28301d
No related branches found
No related tags found
2 merge requests!2fixed a bug,!1Step/main/branch
......@@ -308,6 +308,89 @@ void dataProcessingInstr(struct ProcessorState *processor, int address)
}
}
void branchInstr(struct ProcessorState *processor, uint32_t address)
{
// Check if address is valid
verifyAddress(address);
uint32_t instr = processor->memory[address];
// Check if the instruction should be executed according to its condition codes
if(!validCond(getBits(instr, 28, 31), processor)){
return;
}
// Get offset and sign
int32_t offset = getBits(instr, 0, 23);
int sign = getBits(offset, 23, 23);
// New address calculated with left shift and sign extension (and offset of 8)
uint32_t branchAddr = processor->registers[14] + ((offset * 4) | ((sign) ? 0xFC000000 : 0)) + 8;
// New address verified
verifyAddress(branchAddr);
// New address loaded to PC
processor->registers[14] = branchAddr;
return;
}
// Steps through pipeline calling appropriate instruction method.
// Sets teminate if instruction is all-0.
void step(struct ProcessorState *processor, bool *terminate)
{
// Fetches next instruction location (including offset)
uint32_t nextAddr = processor->registers[14] + 8;
// Fetches next instruction to be executed
uint32_t nextInstr = processor->memory[nextAddr];
// Checks for all-0
if (nextInstr == 0)
{
*terminate = 1;
return;
}
// Check instruction type and call appropriate method
switch (getBits(nextInstr, 26, 27))
{
case 0:
if ((getBits(nextInstr, 25, 25) == 0) && (getBits(nextInstr, 4, 8) == 9)) {
multiplyInstr(processor, nextAddr);
} else {
dataProcessingInstr(processor, nextAddr);
}
case 1:
dataTransferInstr(processor, nextAddr);
case 2:
branchInstr(processor, nextAddr);
}
return;
}
int main(int argc, char *argv[])
{
struct ProcessorState *processor;
// Load file into processor state
if (argc == 2) {
processor = loadBinary(argv[1]);
} else if (argc > 2) {
printf("Too many arguments");
return 0;
} else {
printf("No file given");
return 0;
}
bool terminate = 0;
// Loop through instructions until all-0 met
while (!terminate) {
step(processor, &terminate);
}
}
//*** TESTING FUNCTIONS ***//
......@@ -389,10 +472,12 @@ struct ProcessorState *makeProcessor(void)
return processor;
}
/*
int main(void)
{
testBitGetters();
printf("\n");
testBarrelShifter();
return 0;
}
\ No newline at end of file
}
*/
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment