summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2019-11-19 02:57:05 +0300
committerOxore <oxore@protonmail.com>2019-11-19 02:57:05 +0300
commit4ec9b0780e0813e0acd9cfa8e9cb4ca6e9884c5e (patch)
tree0e9359264d1ebacffd4b1ae837a0eac679fa77b8
parent4955030f13c1d79aef005bb3d292224302afcdfa (diff)
Move Operand matching into instruction execution functions
-rw-r--r--src/core.rs86
1 files changed, 33 insertions, 53 deletions
diff --git a/src/core.rs b/src/core.rs
index 74f79b0..4c3202b 100644
--- a/src/core.rs
+++ b/src/core.rs
@@ -390,46 +390,10 @@ impl Core {
Opname::Illegal => (),
Opname::Push => self.push(op.operand1.expect("PUSH has no operand given")),
Opname::Pop => self.pop(op.operand1.expect("POP has no operand given")),
- Opname::Sjmp => {
- if let Some(operand) = op.operand1 {
- match operand {
- Operand::Reladdr(reladdr) => self.sjmp(reladdr),
- _ => panic!("SJMP got incompatible operand"),
- }
- } else {
- panic!("SJMP has no operand given")
- }
- }
- Opname::Ajmp => {
- if let Some(operand) = op.operand1 {
- match operand {
- Operand::Addr11(addr) => self.ajmp(addr),
- _ => panic!("AJMP got incompatible operand"),
- }
- } else {
- panic!("AJMP has no operand given")
- }
- }
- Opname::Ljmp => {
- if let Some(operand) = op.operand1 {
- match operand {
- Operand::Addr16(addr) => self.ljmp(addr),
- _ => panic!("LJMP got incompatible operand"),
- }
- } else {
- panic!("LJMP has no operand given")
- }
- }
- Opname::Jnz => {
- if let Some(operand) = op.operand1 {
- match operand {
- Operand::Reladdr(addr) => self.jnz(addr),
- _ => panic!("JNZ got incompatible operand"),
- }
- } else {
- panic!("JNZ has no operand given")
- }
- }
+ Opname::Sjmp => self.sjmp(op.operand1.expect("SJMP has no operand given")),
+ Opname::Ajmp => self.ajmp(op.operand1.expect("AJMP has no operand given")),
+ Opname::Ljmp => self.ljmp(op.operand1.expect("LJMP has no operand given")),
+ Opname::Jnz => self.jnz(op.operand1.expect("JNZ has no operand given")),
}
}
@@ -462,7 +426,7 @@ impl Core {
Operand::Direct(dir) => dir,
Operand::Indirect(r) => self.ram[r.to_ram_offset() as usize],
Operand::Register(r) => r.to_ram_offset(),
- other => panic!("Mov: got incompatible first operand \"{:?}\"", other),
+ other => panic!("MOV got incompatible first operand \"{:?}\"", other),
};
let value = match o2 {
Operand::Acc => RAM_OFFSET_ACC,
@@ -470,7 +434,7 @@ impl Core {
Operand::Indirect(r) => self.ram[r.to_ram_offset() as usize],
Operand::Data(r) => r,
Operand::Register(r) => r.to_ram_offset(),
- other => panic!("Mov: got incompatible second operand \"{:?}\"", other),
+ other => panic!("MOV got incompatible second operand \"{:?}\"", other),
};
self.ram[dest as usize] = value;
}
@@ -495,23 +459,39 @@ impl Core {
}
}
- fn sjmp(&mut self, reladdr: i8) {
- self.pc = (self.pc as i16 + i16::from(reladdr)) as u16;
+ fn sjmp(&mut self, o: Operand) {
+ match o {
+ Operand::Reladdr(reladdr) => self.pc = (self.pc as i16 + i16::from(reladdr)) as u16,
+ _ => panic!("SJMP got incompatible operand"),
+ }
}
- fn ajmp(&mut self, addr11: u16) {
- assert!(addr11 <= 0x4FFu16);
- // Clear 11 low bits of PC and add address, that is within these 11 bits
- self.pc = (self.pc & !0x4FFu16) & addr11;
+ fn ajmp(&mut self, o: Operand) {
+ match o {
+ Operand::Addr11(addr11) => {
+ assert!(addr11 <= 0x4FFu16);
+ // Clear 11 low bits of PC and add address, that is within these 11 bits
+ self.pc = (self.pc & !0x4FFu16) & addr11;
+ }
+ _ => panic!("AJMP got incompatible operand"),
+ }
}
- fn ljmp(&mut self, addr: u16) {
- self.pc = addr;
+ fn ljmp(&mut self, o: Operand) {
+ match o {
+ Operand::Addr16(addr) => self.pc = addr,
+ _ => panic!("LJMP got incompatible operand"),
+ }
}
- fn jnz(&mut self, reladdr: i8) {
- if self.ram[usize::from(RAM_OFFSET_ACC)] != 0 {
- self.pc = (self.pc as i16 + i16::from(reladdr)) as u16;
+ fn jnz(&mut self, o: Operand) {
+ match o {
+ Operand::Reladdr(reladdr) => {
+ if self.ram[usize::from(RAM_OFFSET_ACC)] != 0 {
+ self.pc = (self.pc as i16 + i16::from(reladdr)) as u16;
+ }
+ }
+ _ => panic!("JNZ got incompatible operand"),
}
}
}