Skip to content

Commit

Permalink
Ensure opcodes with FD and FF prefix are executed atomically
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanizag committed Mar 20, 2022
1 parent 102a67e commit 5a4703c
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 48 deletions.
34 changes: 17 additions & 17 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'z80'",
"name": "Debug unit tests in library 'iz80'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=z80"
"--package=iz80"
],
"filter": {
"name": "z80",
"name": "iz80",
"kind": "lib"
}
},
Expand All @@ -26,15 +26,15 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'z80'",
"name": "Debug executable 'iz80'",
"cargo": {
"args": [
"build",
"--bin=z80",
"--package=z80"
"--bin=iz80",
"--package=iz80"
],
"filter": {
"name": "z80",
"name": "iz80",
"kind": "bin"
}
},
Expand All @@ -44,16 +44,16 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'z80'",
"name": "Debug unit tests in executable 'iz80'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=z80",
"--package=z80"
"--bin=iz80",
"--package=iz80"
],
"filter": {
"name": "z80",
"name": "iz80",
"kind": "bin"
}
},
Expand All @@ -69,7 +69,7 @@
"test",
"--no-run",
"--test=opcodes_arith",
"--package=z80"
"--package=iz80"
],
"filter": {
"name": "opcodes_arith",
Expand All @@ -88,7 +88,7 @@
"test",
"--no-run",
"--test=opcodes_bits",
"--package=z80"
"--package=iz80"
],
"filter": {
"name": "opcodes_bits",
Expand All @@ -107,7 +107,7 @@
"test",
"--no-run",
"--test=opcodes_jumps",
"--package=z80"
"--packagei=z80"
],
"filter": {
"name": "opcodes_jumps",
Expand All @@ -126,7 +126,7 @@
"test",
"--no-run",
"--test=opcodes_io",
"--package=z80"
"--package=iz80"
],
"filter": {
"name": "opcodes_io",
Expand All @@ -145,7 +145,7 @@
"test",
"--no-run",
"--test=opcodes_ld",
"--package=z80"
"--package=iz80"
],
"filter": {
"name": "opcodes_ld",
Expand All @@ -164,7 +164,7 @@
"test",
"--no-run",
"--test=opcodes",
"--package=z80"
"--package=iz80"
],
"filter": {
"name": "opcodes",
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "iz80"
version = "0.3.4"
version = "0.3.5"
authors = ["Ivan Izaguirre <[email protected]>"]
edition = "2018"
license = "BSD-3-Clause"
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,13 @@ sta nnnn / lda nnnn........... OK
stax <b,d>.................... OK
Tests complete
```

### Pre-commit tests

Note that some of the Zexall tests takes very long and is disabled for continuouos integrations. To run it, execute:

```
cargo test --release -- --nocapture --ignored --test zexall
```

2 changes: 1 addition & 1 deletion publishing.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Reminder:
- Change the version in Cargo.toml, rebuild and commit
- Create a tag (git tag v0.3.4) and push (git push --tags)
- Publish (cargo Publish)
- Publish (cargo publish)
3 changes: 1 addition & 2 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ impl Cpu {
env.subroutine_call(NMI_ADDRESS);
}


let pc = env.state.reg.pc();
let opcode = self.decoder.decode(&mut env);
if self.trace {
print!("==> {:04x}: {:20}", pc, opcode.disasm(&mut env));
}
opcode.execute(&mut env);
env.step();
env.clear_index();

if self.trace {
print!(" PC:{:04x} AF:{:04x} BC:{:04x} DE:{:04x} HL:{:04x} SP:{:04x} IX:{:04x} IY:{:04x} Flags:{:08b}",
Expand Down
20 changes: 17 additions & 3 deletions src/decoder_z80.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ pub struct DecoderZ80 {

impl Decoder for DecoderZ80 {
fn decode(&self, env: &mut Environment) -> &Opcode {
let b0 = env.advance_pc();
let mut b0 = env.advance_pc();

// Process prefixes even if reapeated
while b0 == 0xdd || b0 == 0xfd {
if b0 == 0xdd {
// DD prefix
env.set_index(Reg16::IX);
b0 = env.advance_pc()
} else {
// FD prefix
env.set_index(Reg16::IY);
b0 = env.advance_pc()
}
}

let opcode = match b0 {
0xcb => {
if env.is_alt_index() {
Expand Down Expand Up @@ -222,9 +236,9 @@ impl DecoderZ80 {
0 => Some(build_push_rr(RP2[p.p])), // PUSH rr
1 => match p.p {
0 => Some(build_call()), // Call nn
1 => Some(build_prefix(Reg16::IX)), // DD prefix
1 => None, // DD prefix
2 => None, // ED prefix
3 => Some(build_prefix(Reg16::IY)), // FD prefix
3 => None, // FD prefix
_ => panic!("Unreachable")
},
_ => panic!("Unreachable")
Expand Down
10 changes: 0 additions & 10 deletions src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,11 @@ impl <'a> Environment<'_> {

pub fn set_index(&mut self, index: Reg16) {
self.state.index = index;
self.state.index_changed = true;
}

pub fn step(&mut self) {
if self.state.index_changed {
self.state.index_changed = false;
} else {
self.clear_index();
}
}

pub fn clear_index(&mut self) {
self.state.index = Reg16::HL;
self.state.displacement_loaded = false;
self.state.index_changed = false;
}

pub fn index_description(&self) -> String {
Expand Down
11 changes: 0 additions & 11 deletions src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ impl Opcode {
}
}

pub fn build_prefix(index: Reg16) -> Opcode {
Opcode {
name: format!("PREFIX {:?}", index),
action: Box::new(move |env: &mut Environment| {
// Change the index mode to IX or IY
//let d = env.advance_pc() as i8;
env.set_index(index /*, d*/);
})
}
}

pub fn build_nop() -> Opcode {
Opcode {
name: "NOP".to_string(),
Expand Down
2 changes: 0 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct State {
pub index: Reg16, // Using HL, IX or IY
pub displacement: i8, // Used for (IX+d) and (iY+d)
pub displacement_loaded: bool, // TODO: remove
pub index_changed: bool, // Use the index change for the next opcode, reset afterwards
}

impl State {
Expand All @@ -28,7 +27,6 @@ impl State {
index: Reg16::HL,
displacement: 0,
displacement_loaded: false,
index_changed: false
}
}
}

0 comments on commit 5a4703c

Please sign in to comment.