Implement NOP

This commit is contained in:
n loewen 2023-08-03 15:05:50 +01:00
parent 18f0a12ef5
commit a0892109a6
4 changed files with 30 additions and 17 deletions

View File

@ -13,7 +13,7 @@ exports.assemble = (str, debug = false) => {
return decodeInstructions(str);
}
const mnemonicsWithOptionalArgs = ['end', 'cfc', 'chp'];
const mnemonicsWithOptionalArgs = ['end', 'cfc', 'chp', 'nop'];
const mnemonics2opcodes = {
end: { direct: 0, indirect: 0 },
sto: { direct: 1, indirect: 2 },
@ -24,6 +24,7 @@ const mnemonics2opcodes = {
jmp: { direct: 11, indirect: 12 },
cfc: { direct: 13, indirect: 13 },
chp: { direct: 14, indirect: 14 },
nop: { direct: 15, indirect: 15 },
};
function decodeInstructions(str) {

View File

@ -38,22 +38,22 @@ Assemble and run, with debug output:
## Instruction set
0 END
1 STO lit# ; store ... mem[lit#] <- A
2 STO addr ; store ... mem[mem[addr]] <- A
3 LDA lit# ; load ... A <- lit#
4 LDA addr ; load ... A <- mem[addr]
5 ADD lit# ; add ... A <- A + lit# ... and un/set carry flag
6 ADD addr ; add ... A <- A + mem[addr] ... and un/set carry flag
7 SUB lit# ; sub ... A <- A - lit# ... and un/set carry flag
8 SUB addr ; sub ... A <- A - mem[addr] ... and un/set carry flag
9 HOP lit# ; hop ... skip next instruction if A == lit# ... when true: IP <- PC + 2
A HOP addr ; hop ... skip next instruction if A == addr ... when true: IP <- PC + 2
B JMP lit# ; jump ... IP <- lit#
C JMP addr ; jump ... IP <- addr
D CCF ———— ; clear Carry Flag ... CF = 0
E CHP ———— ; carry hop ... skip next instruction if Carry Flag is set ... when true: IP <- PC + 2
F
00 END
01 STO lit# ; store ... mem[lit#] <- A
02 STO addr ; store ... mem[mem[addr]] <- A
03 LDA lit# ; load ... A <- lit#
04 LDA addr ; load ... A <- mem[addr]
05 ADD lit# ; add ... A <- A + lit# ... and un/set carry flag
06 ADD addr ; add ... A <- A + mem[addr] ... and un/set carry flag
07 SUB lit# ; sub ... A <- A - lit# ... and un/set carry flag
08 SUB addr ; sub ... A <- A - mem[addr] ... and un/set carry flag
09 HOP lit# ; hop ... skip next instruction if A == lit# ... when true: IP <- PC + 2
0A HOP addr ; hop ... skip next instruction if A == addr ... when true: IP <- PC + 2
0B JMP lit# ; jump ... IP <- lit#
0C JMP addr ; jump ... IP <- addr
0D CCF ———— ; clear Carry Flag ... CF = 0
0E CHP ———— ; carry hop ... skip next instruction if Carry Flag is set ... when true: IP <- PC + 2
0F NOP ———— ; no operation
- Instructions are two bytes long:
one byte for the opcode, one for the argument

View File

@ -147,6 +147,11 @@ const Instructions = {
CPU.IP += 2;
}
},
no_op: () => {
CPU.currentInstruction.mnemonic = `NOP`;
CPU.IP += 2;
},
}
const opcodes2mnemonics = {
@ -165,6 +170,7 @@ const opcodes2mnemonics = {
12: (arg) => Instructions.jump_addr(arg),
13: (arg) => Instructions.carry_clear(arg),
14: (arg) => Instructions.carry_hop(arg),
15: (arg) => Instructions.no_op(arg),
};
function startCPU(code) {

6
test-programs/nop.asm Normal file
View File

@ -0,0 +1,6 @@
;; test NOP
ADD $01
NOP
NOP
NOP
END