Skip to content

Commit f5bb19c

Browse files
committed
performance hacking
1 parent c35b3a0 commit f5bb19c

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

z80.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ pub trait z80Ctrl {
4040
fn write_byte(&mut self, addr: u16, value: u8);
4141
fn port_in(&self, addr: u16) -> u8;
4242
fn port_out(&mut self, addr: u16, value: u8);
43-
#[cfg(test)]
44-
fn test_finished(&self) -> bool;
4543
}
4644

4745
pub struct z80 {
@@ -69,6 +67,8 @@ pub struct z80 {
6967
halted: bool,
7068
iff1: bool,
7169
iff2: bool,
70+
#[cfg(test)]
71+
pub test_finished: bool,
7272
}
7373

7474
impl z80 {
@@ -115,6 +115,8 @@ impl z80 {
115115
halted: false,
116116
iff1: false,
117117
iff2: false,
118+
#[cfg(test)]
119+
test_finished: false,
118120
}
119121
}
120122
pub fn init(&mut self) {
@@ -141,6 +143,10 @@ impl z80 {
141143
self.irq_pending = 0 as i32 as uint8_t;
142144
self.nmi_pending = 0 as i32 as uint8_t;
143145
self.irq_data = 0 as i32 as uint8_t;
146+
#[cfg(test)]
147+
{
148+
self.test_finished = false;
149+
}
144150
}
145151
pub fn step(&mut self) -> u32 {
146152
unsafe { z80_step_s(self) }
@@ -169,87 +175,91 @@ impl z80 {
169175
self.ctrl.port_in(addr)
170176
}
171177
fn internal_port_out(&mut self, addr: u16, value: u8) {
178+
#[cfg(test)]
179+
{
180+
self.test_finished = true;
181+
}
172182
self.ctrl.port_out(addr, value);
173183
}
174184
}
175185

176186
#[derive(Copy, Clone)]
177-
union C2RustUnnamed_0 {
187+
pub union C2RustUnnamed_0 {
178188
pub c2rust_unnamed: C2RustUnnamed_1,
179189
pub h_l_: uint16_t,
180190
}
181191
#[derive(Copy, Clone)]
182-
struct C2RustUnnamed_1 {
192+
pub struct C2RustUnnamed_1 {
183193
pub l_: uint8_t,
184194
pub h_: uint8_t,
185195
}
186196
#[derive(Copy, Clone)]
187-
union C2RustUnnamed_2 {
197+
pub union C2RustUnnamed_2 {
188198
pub c2rust_unnamed: C2RustUnnamed_3,
189199
pub d_e_: uint16_t,
190200
}
191201
#[derive(Copy, Clone)]
192-
struct C2RustUnnamed_3 {
202+
pub struct C2RustUnnamed_3 {
193203
pub e_: uint8_t,
194204
pub d_: uint8_t,
195205
}
196206
#[derive(Copy, Clone)]
197-
union C2RustUnnamed_4 {
207+
pub union C2RustUnnamed_4 {
198208
pub c2rust_unnamed: C2RustUnnamed_5,
199209
pub b_c_: uint16_t,
200210
}
201211
#[derive(Copy, Clone)]
202-
struct C2RustUnnamed_5 {
212+
pub struct C2RustUnnamed_5 {
203213
pub c_: uint8_t,
204214
pub b_: uint8_t,
205215
}
206216
#[derive(Copy, Clone)]
207-
union C2RustUnnamed_6 {
217+
pub union C2RustUnnamed_6 {
208218
pub c2rust_unnamed: C2RustUnnamed_7,
209219
pub a_f_: uint16_t,
210220
}
211221
#[derive(Copy, Clone)]
212-
struct C2RustUnnamed_7 {
222+
pub struct C2RustUnnamed_7 {
213223
pub f_: uint8_t,
214224
pub a_: uint8_t,
215225
}
216226
#[derive(Copy, Clone)]
217-
union C2RustUnnamed_8 {
227+
pub union C2RustUnnamed_8 {
218228
pub c2rust_unnamed: C2RustUnnamed_9,
219229
pub hl: uint16_t,
220230
}
221231
#[derive(Copy, Clone)]
222-
struct C2RustUnnamed_9 {
232+
pub struct C2RustUnnamed_9 {
223233
pub l: uint8_t,
224234
pub h: uint8_t,
225235
}
226236
#[derive(Copy, Clone)]
227-
union C2RustUnnamed_10 {
237+
pub union C2RustUnnamed_10 {
228238
pub c2rust_unnamed: C2RustUnnamed_11,
229239
pub de: uint16_t,
230240
}
231241
#[derive(Copy, Clone)]
232-
struct C2RustUnnamed_11 {
242+
pub struct C2RustUnnamed_11 {
233243
pub e: uint8_t,
234244
pub d: uint8_t,
235245
}
236246
#[derive(Copy, Clone)]
237-
union C2RustUnnamed_12 {
247+
pub union C2RustUnnamed_12 {
238248
pub c2rust_unnamed: C2RustUnnamed_13,
239249
pub bc: uint16_t,
240250
}
241251
#[derive(Copy, Clone)]
242-
struct C2RustUnnamed_13 {
252+
pub struct C2RustUnnamed_13 {
243253
pub c: uint8_t,
244254
pub b: uint8_t,
245255
}
246256
#[derive(Copy, Clone)]
247-
union C2RustUnnamed_14 {
257+
pub union C2RustUnnamed_14 {
248258
pub c2rust_unnamed: C2RustUnnamed_15,
249259
pub af: uint16_t,
250260
}
251261
#[derive(Copy, Clone)]
252-
struct C2RustUnnamed_15 {
262+
pub struct C2RustUnnamed_15 {
253263
pub f: uint8_t,
254264
pub a: uint8_t,
255265
}

z80_tests.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::z80::{z80, z80Ctrl};
22

33
struct Memory {
44
pub mem: [u8; 0x10000],
5-
pub test_finished: bool,
65
}
76

87
impl z80Ctrl for Memory {
@@ -17,26 +16,17 @@ impl z80Ctrl for Memory {
1716
0xff
1817
}
1918
fn port_out(&mut self, _addr: u16, _value: u8) {
20-
self.test_finished = true;
21-
}
22-
fn test_finished(&self) -> bool {
23-
self.test_finished
2419
}
2520
}
2621

27-
fn run_test(rom: &[u8], cyc_expected: u64) {
28-
let mut memory = Box::new(Memory {
29-
mem: [0; 0x10000],
30-
test_finished: false,
31-
});
22+
fn run_test(cpu: &mut z80, rom: &[u8], cyc_expected: u64) {
3223

3324
for (i, byte) in rom.iter().enumerate() {
34-
memory.mem[0x100 + i] = *byte;
25+
cpu.ctrl.write_byte(0x100 + i as u16, *byte);
3526
}
3627

3728
let mut cyc: u64 = 0;
3829

39-
let mut cpu = z80::new(memory);
4030
cpu.init();
4131
cpu.pc = 0x100;
4232
cpu.ctrl.write_byte(0,0xd3);
@@ -46,7 +36,7 @@ fn run_test(rom: &[u8], cyc_expected: u64) {
4636
cpu.ctrl.write_byte(7,0xc9);
4737

4838
let mut nb_instructions: u64 = 0;
49-
while !cpu.ctrl.test_finished() {
39+
while !cpu.test_finished {
5040
nb_instructions += 1;
5141
cyc = cyc.wrapping_add(cpu.step().into());
5242
}
@@ -64,7 +54,13 @@ fn run_test(rom: &[u8], cyc_expected: u64) {
6454

6555
#[test]
6656
pub fn main() {
67-
run_test(include_bytes!("./roms/prelim.com"), 8721);
68-
run_test(include_bytes!("./roms/zexdoc.cim"), 46734978649);
69-
run_test(include_bytes!("roms/zexall.cim"), 46734978649);
57+
let mut memory = Box::new(Memory {
58+
mem: [0; 0x10000],
59+
});
60+
61+
let mut cpu = z80::new(memory);
62+
63+
run_test(&mut cpu, include_bytes!("./roms/prelim.com"), 8721);
64+
run_test(&mut cpu, include_bytes!("./roms/zexdoc.cim"), 46734978649);
65+
run_test(&mut cpu, include_bytes!("roms/zexall.cim"), 46734978649);
7066
}

0 commit comments

Comments
 (0)