Skip to content

Commit

Permalink
Machine runtime parameters (#32)
Browse files Browse the repository at this point in the history
* Refactored all fetch, decode and execute to machines available.

* Renamed functions
  • Loading branch information
TakatJJ authored Oct 20, 2022
1 parent a9b80b5 commit a46bb7f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 44 deletions.
51 changes: 28 additions & 23 deletions src/core/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void Machine::decodeInstruction(int fetchedValue, Instruction *&instruction, Add
void Machine::executeInstruction(Instruction *&instruction, AddressingMode::AddressingModeCode addressingModeCode, QString registerName, int immediateAddress)
{
int value1, value2, result;
int immediateAddress = decodedImmediateAddress;
Instruction::InstructionCode instructionCode;
instructionCode = (instruction) ? instruction->getInstructionCode() : Instruction::NOP;
bool isImmediate = (addressingModeCode == AddressingMode::IMMEDIATE); // Used to invalidate immediate jumps
Expand All @@ -92,14 +93,14 @@ void Machine::executeInstruction(Instruction *&instruction, AddressingMode::Addr
//////////////////////////////////////////////////

case Instruction::LDR:
result = memoryGetOperandValue(immediateAddress, addressingModeCode);
result = GetCurrentOperandValue();
setRegisterValue(registerName, result);
updateFlags(result);
break;

case Instruction::STR:
result = getRegisterValue(registerName);
memoryWrite(memoryGetOperandAddress(immediateAddress, addressingModeCode), result);
memoryWrite(GetCurrentOperandAddress(), result);
break;


Expand All @@ -110,7 +111,7 @@ void Machine::executeInstruction(Instruction *&instruction, AddressingMode::Addr

case Instruction::ADD:
value1 = getRegisterValue(registerName);
value2 = memoryGetOperandValue(immediateAddress, addressingModeCode);
value2 = GetCurrentOperandValue();
result = (value1 + value2) & 0xFF;

setRegisterValue(registerName, result);
Expand All @@ -121,7 +122,7 @@ void Machine::executeInstruction(Instruction *&instruction, AddressingMode::Addr

case Instruction::OR:
value1 = getRegisterValue(registerName);
value2 = memoryGetOperandValue(immediateAddress, addressingModeCode);
value2 = GetCurrentOperandValue();
result = (value1 | value2);

setRegisterValue(registerName, result);
Expand All @@ -130,7 +131,7 @@ void Machine::executeInstruction(Instruction *&instruction, AddressingMode::Addr

case Instruction::AND:
value1 = getRegisterValue(registerName);
value2 = memoryGetOperandValue(immediateAddress, addressingModeCode);
value2 = GetCurrentOperandValue();
result = (value1 & value2);

setRegisterValue(registerName, result);
Expand All @@ -147,7 +148,7 @@ void Machine::executeInstruction(Instruction *&instruction, AddressingMode::Addr

case Instruction::SUB:
value1 = getRegisterValue(registerName);
value2 = memoryGetOperandValue(immediateAddress, addressingModeCode);
value2 = GetCurrentOperandValue();
result = (value1 - value2) & 0xFF;

setRegisterValue(registerName, result);
Expand Down Expand Up @@ -216,63 +217,63 @@ void Machine::executeInstruction(Instruction *&instruction, AddressingMode::Addr

case Instruction::JMP:
if (!isImmediate) // Invalidate immediate jumps
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JN:
if (getFlagValue("N") == true && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JP:
if (getFlagValue("N") == false && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JV:
if (getFlagValue("V") == true && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JNV:
if (getFlagValue("V") == false && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JZ:
if (getFlagValue("Z") == true && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JNZ:
if (getFlagValue("Z") == false && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JC:
if (getFlagValue("C") == true && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JNC:
if (getFlagValue("C") == false && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JB:
if (getFlagValue("B") == true && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JNB:
if (getFlagValue("B") == false && !isImmediate)
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::JSR:
if (!isImmediate)
{
int jumpAddress = memoryGetJumpAddress(immediateAddress, addressingModeCode);
int jumpAddress = GetCurrentJumpAddress();
memoryWrite(jumpAddress, getPCValue());
setPCValue(jumpAddress+1);
}
Expand Down Expand Up @@ -1026,8 +1027,12 @@ int Machine::memoryReadNext()
return value;
}

int Machine::memoryGetOperandAddress(int immediateAddress, AddressingMode::AddressingModeCode addressingModeCode)
int Machine::GetCurrentOperandAddress()
{

int immediateAddress = decodedImmediateAddress;
AddressingMode::AddressingModeCode addressingModeCode = decodedAdressingModeCode1;

switch (addressingModeCode)
{
case AddressingMode::DIRECT:
Expand All @@ -1050,14 +1055,14 @@ int Machine::memoryGetOperandAddress(int immediateAddress, AddressingMode::Addre
}
}

int Machine::memoryGetOperandValue(int immediateAddress, AddressingMode::AddressingModeCode addressingModeCode)
int Machine::GetCurrentOperandValue()
{
return memoryRead(memoryGetOperandAddress(immediateAddress, addressingModeCode)); // Return 1-byte value
return memoryRead(GetCurrentOperandAddress()); // Return 1-byte value
}

int Machine::memoryGetJumpAddress(int immediateAddress, AddressingMode::AddressingModeCode addressingModeCode)
int Machine::GetCurrentJumpAddress()
{
return memoryGetOperandAddress(immediateAddress, addressingModeCode);
return GetCurrentOperandAddress();
}


Expand Down
6 changes: 3 additions & 3 deletions src/core/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ class Machine : public QObject
void memoryWrite(int address, int value); // Increments accessCount
int memoryReadNext(); // Returns value pointed to by PC, then increments PC; Increments accessCount

virtual int memoryGetOperandAddress(int immediateAddress, AddressingMode::AddressingModeCode addressingModeCode); // increments accessCount
int memoryGetOperandValue(int immediateAddress, AddressingMode::AddressingModeCode addressingModeCode); // increments accessCount
int memoryGetJumpAddress( int immediateAddress, AddressingMode::AddressingModeCode addressingModeCode); // increments accessCount
virtual int GetCurrentOperandAddress(); // increments accessCount
int GetCurrentOperandValue(); // increments accessCount
int GetCurrentJumpAddress(); // increments accessCount



Expand Down
20 changes: 12 additions & 8 deletions src/machines/periclesmachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,28 @@ int PericlesMachine::calculateBytesToReserve(QString addressArgument)
return (addressingModeCode == AddressingMode::IMMEDIATE) ? 2 : 3; // Immediate requires only 2 bytes
}

void PericlesMachine::decodeInstruction(int fetchedValue, Instruction *&instruction, AddressingMode::AddressingModeCode &addressingModeCode, QString &registerName, int &immediateAddress)
{
addressingModeCode = extractAddressingModeCode(fetchedValue);
registerName = extractRegisterName(fetchedValue);
void PericlesMachine::decodeInstruction(){
decodedAdressingModeCode1 = extractAddressingModeCode(fetchedValue);
decodedRegisterName1 = extractRegisterName(fetchedValue);

if (instruction && instruction->getNumBytes() == 0) // If instruction has variable number of bytes
if (currentInstruction && currentInstruction->getNumBytes() == 0) // If instruction has variable number of bytes
{
immediateAddress = getPCValue(); // Address that contains first argument byte
decodedImmediateAddress = getPCValue(); // Address that contains first argument byte

// Skip argument bytes
incrementPCValue();
if (addressingModeCode != AddressingMode::IMMEDIATE) // Immediate argument has only 1 byte
if (decodedAdressingModeCode1 != AddressingMode::IMMEDIATE) // Immediate argument has only 1 byte
incrementPCValue();
}
}

int PericlesMachine::memoryGetOperandAddress(int immediateAddress, AddressingMode::AddressingModeCode addressingModeCode)
int PericlesMachine::GetCurrentOperandAddress()
{
int immediateAddress = decodedImmediateAddress;
AddressingMode::AddressingModeCode addressingModeCode = decodedAdressingModeCode1;


switch (addressingModeCode)
{
case AddressingMode::DIRECT:
Expand Down
4 changes: 2 additions & 2 deletions src/machines/periclesmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class PericlesMachine : public Machine
public:
PericlesMachine();

virtual void decodeInstruction(int fetchedValue, Instruction *&instruction, AddressingMode::AddressingModeCode &addressingMode, QString &registerId, int &immediateAddress);
void decodeInstruction();
virtual int calculateBytesToReserve(QString addressArgument);
virtual int memoryGetOperandAddress(int immediateAddress, AddressingMode::AddressingModeCode addressingModeCode); // increments accessCount
virtual int GetCurrentOperandAddress(); // increments accessCount
virtual void getNextOperandAddress(int &intermediateAddress, int &intermediateAddress2, int &finalOperandAddress);
virtual QString generateArgumentsString(int address, Instruction *instruction, AddressingMode::AddressingModeCode addressingModeCode, int &argumentsSize);

Expand Down
15 changes: 9 additions & 6 deletions src/machines/voltamachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ VoltaMachine::VoltaMachine()
addressingModes.append(new AddressingMode("......11", AddressingMode::INDEXED_BY_PC, "(.*),pc"));
}

void VoltaMachine::executeInstruction(Instruction *&instruction, AddressingMode::AddressingModeCode addressingModeCode, QString, int immediateAddress)
void VoltaMachine::executeInstruction()
{
int value1, value2, result, bit;
Instruction::InstructionCode instructionCode;
instructionCode = (instruction) ? instruction->getInstructionCode() : Instruction::NOP;
instructionCode = (currentInstruction) ? currentInstruction->getInstructionCode() : Instruction::NOP;

int immediateAddress = decodedImmediateAddress;
AddressingMode::AddressingModeCode addressingModeCode = decodedAdressingModeCode1;

switch (instructionCode)
{
Expand Down Expand Up @@ -279,22 +282,22 @@ void VoltaMachine::executeInstruction(Instruction *&instruction, AddressingMode:
break;

case Instruction::VOLTA_PSH:
value1 = memoryGetOperandValue(immediateAddress, addressingModeCode);
value1 = GetCurrentOperandValue();
stackPush(value1);
break;

case Instruction::VOLTA_POP:
value1 = stackPop();
memoryWrite(memoryGetOperandAddress(immediateAddress, addressingModeCode), value1);
memoryWrite(GetCurrentOperandAddress(), value1);
break;

case Instruction::VOLTA_JMP:
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::VOLTA_JSR:
stackPush(getPCValue());
setPCValue(memoryGetJumpAddress(immediateAddress, addressingModeCode));
setPCValue(GetCurrentJumpAddress());
break;

case Instruction::VOLTA_HLT:
Expand Down
4 changes: 2 additions & 2 deletions src/machines/voltamachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class VoltaMachine : public Machine
{
public:
VoltaMachine();

virtual void executeInstruction(Instruction *&instruction, AddressingMode::AddressingModeCode addressingModeCode, QString, int immediateAddress);
void executeInstruction();
void skipNextInstruction();

virtual void generateDescriptions();
Expand Down

0 comments on commit a46bb7f

Please sign in to comment.