Skip to content

extended instruction set: ADDC - Add with carry, SUBC - Subtract with… #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions instruction-set.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ <h4>Math operations</h4>
SUB reg, reg
SUB reg, address
SUB reg, constant
</pre>
<b>Addition with carry and Subtraction with borrow</b>b>
<p>Adds two numbers or subtracts one number from another. If the carry flag is set, this will add or subtract one more. This is useful for adding and subtracting numbers which are wider than 8 bits. These operations will modify the carry and zero flag. SP can be used as operand with ADDC and SUBC.</p>
<pre>
ADDC reg, reg
ADDC reg, address
ADDC reg, constant
SUBC reg, reg
SUBC reg, address
SUBC reg, constant
</pre>
<b>Increment and Decrement</b>
<p>Increments or decrements a register by one. This operations will modify the carry and zero flag. SP can be used as operand with INC and DEC.</p>
Expand Down
34 changes: 34 additions & 0 deletions src/assembler/asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ app.service('assembler', ['opcodes', function (opcodes) {
else
throw "ADD does not support this operands";

code.push(opCode, p1.value, p2.value);
break;
case 'ADDC':
p1 = getValue(match[op1_group]);
p2 = getValue(match[op2_group]);

if (p1.type === "register" && p2.type === "register")
opCode = opcodes.ADDC_REG_TO_REG;
else if (p1.type === "register" && p2.type === "regaddress")
opCode = opcodes.ADDC_REGADDRESS_TO_REG;
else if (p1.type === "register" && p2.type === "address")
opCode = opcodes.ADDC_ADDRESS_TO_REG;
else if (p1.type === "register" && p2.type === "number")
opCode = opcodes.ADDC_NUMBER_TO_REG;
else
throw "ADDC does not support this operands";

code.push(opCode, p1.value, p2.value);
break;
case 'SUB':
Expand All @@ -279,6 +296,23 @@ app.service('assembler', ['opcodes', function (opcodes) {
else
throw "SUB does not support this operands";

code.push(opCode, p1.value, p2.value);
break;
case 'SUBC':
p1 = getValue(match[op1_group]);
p2 = getValue(match[op2_group]);

if (p1.type === "register" && p2.type === "register")
opCode = opcodes.SUBC_REG_FROM_REG;
else if (p1.type === "register" && p2.type === "regaddress")
opCode = opcodes.SUBC_REGADDRESS_FROM_REG;
else if (p1.type === "register" && p2.type === "address")
opCode = opcodes.SUBC_ADDRESS_FROM_REG;
else if (p1.type === "register" && p2.type === "number")
opCode = opcodes.SUBC_NUMBER_FROM_REG;
else
throw "SUBC does not support this operands";

code.push(opCode, p1.value, p2.value);
break;
case 'INC':
Expand Down
58 changes: 57 additions & 1 deletion src/emulator/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
throw "Instruction pointer is outside of memory";
}

var regTo, regFrom, memFrom, memTo, number;
var regTo, regFrom, memFrom, memTo, number, carryVal;
var instr = memory.load(self.ip);
switch(instr) {
case opcodes.NONE:
Expand Down Expand Up @@ -201,6 +201,34 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) + number));
self.ip++;
break;
case opcodes.ADDC_REG_TO_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
regFrom = checkGPR_SP(memory.load(++self.ip));
carryVal = self.carry ? 1 : 0;
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) + getGPR_SP(regFrom) + carryVal ));
self.ip++;
break;
case opcodes.ADDC_REGADDRESS_TO_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
regFrom = memory.load(++self.ip);
carryVal = self.carry ? 1 : 0;
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) + memory.load(indirectRegisterAddress(regFrom)) + carryVal));
self.ip++;
break;
case opcodes.ADDC_ADDRESS_TO_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
memFrom = memory.load(++self.ip);
carryVal = self.carry ? 1 : 0;
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) + memory.load(memFrom) + carryVal));
self.ip++;
break;
case opcodes.ADDC_NUMBER_TO_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
number = memory.load(++self.ip);
carryVal = self.carry ? 1 : 0;
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) + number + carryVal));
self.ip++;
break;
case opcodes.SUB_REG_FROM_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
regFrom = checkGPR_SP(memory.load(++self.ip));
Expand All @@ -225,6 +253,34 @@ app.service('cpu', ['opcodes', 'memory', function(opcodes, memory) {
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) - number));
self.ip++;
break;
case opcodes.SUBC_REG_FROM_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
regFrom = checkGPR_SP(memory.load(++self.ip));
carryVal = self.carry ? 1 : 0;
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) - self.gpr[regFrom] - carryVal));
self.ip++;
break;
case opcodes.SUBC_REGADDRESS_FROM_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
regFrom = memory.load(++self.ip);
carryVal = self.carry ? 1 : 0;
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) - memory.load(indirectRegisterAddress(regFrom)) - carryVal));
self.ip++;
break;
case opcodes.SUBC_ADDRESS_FROM_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
memFrom = memory.load(++self.ip);
carryVal = self.carry ? 1 : 0;
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) - memory.load(memFrom) - carryVal));
self.ip++;
break;
case opcodes.SUBC_NUMBER_FROM_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
number = memory.load(++self.ip);
carryVal = self.carry ? 1 : 0;
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) - number - carryVal));
self.ip++;
break;
case opcodes.INC_REG:
regTo = checkGPR_SP(memory.load(++self.ip));
setGPR_SP(regTo,checkOperation(getGPR_SP(regTo) + 1));
Expand Down
10 changes: 9 additions & 1 deletion src/emulator/opcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ app.service('opcodes', [function() {
SHR_REG_WITH_REG: 94,
SHR_REGADDRESS_WITH_REG: 95,
SHR_ADDRESS_WITH_REG: 96,
SHR_NUMBER_WITH_REG: 97
SHR_NUMBER_WITH_REG: 97,
ADDC_REG_TO_REG: 98,
ADDC_REGADDRESS_TO_REG: 99,
ADDC_ADDRESS_TO_REG: 100,
ADDC_NUMBER_TO_REG: 101,
SUBC_REG_FROM_REG: 102,
SUBC_REGADDRESS_FROM_REG: 103,
SUBC_ADDRESS_FROM_REG: 104,
SUBC_NUMBER_FROM_REG: 105
};

return opcodes;
Expand Down