Skip to content

Commit

Permalink
[#15] Combine on_pop_rp().
Browse files Browse the repository at this point in the history
  • Loading branch information
kosarev committed Aug 27, 2021
1 parent ed206d8 commit 363e569
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 87 deletions.
132 changes: 68 additions & 64 deletions z80.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,24 @@ class root {
// Always set the low byte first.
self().on_set_f(get_low8(n));
self().on_set_a(get_high8(n)); }
fast_u16 on_get_ix() {
// Always get the low byte first.
fast_u8 l = self().on_get_ixl();
fast_u8 h = self().on_get_ixh();
return make16(h, l); }
void on_set_ix(fast_u16 ix) {
// Always set the low byte first.
self().on_set_ixl(get_low8(ix));
self().on_set_ixh(get_high8(ix)); }
fast_u16 on_get_iy() {
// Always get the low byte first.
fast_u8 l = self().on_get_iyl();
fast_u8 h = self().on_get_iyh();
return make16(h, l); }
void on_set_iy(fast_u16 iy) {
// Always set the low byte first.
self().on_set_iyl(get_low8(iy));
self().on_set_iyh(get_high8(iy)); }
fast_u16 on_get_ir() {
// Always get the low byte first.
fast_u8 l = self().on_get_i();
Expand Down Expand Up @@ -1312,6 +1330,13 @@ class internals::disasm_base : public B {
self().on_format("ei"); }
void on_nop() {
self().on_format("nop"); }
void on_pop_rp(regp2 rp) {
// TODO: Unify handling the G specifier.
if(!self().on_is_z80()) {
self().on_format("pop G", rp);
} else {
iregp irp = get_iregp_kind_or_hl(rp);
self().on_format("pop G", rp, irp); } }
void on_ret() {
self().on_format("ret"); }
void on_ret_cc(condition cc) {
Expand Down Expand Up @@ -1422,6 +1447,26 @@ class internals::disasm_base : public B {
++args;
return value;
}

static bool is_indexable(reg r) {
return r == reg::at_hl || r == reg::h || r == reg::l;
}

iregp get_iregp_kind_or_hl(bool indexable) {
return indexable ? self().on_get_iregp_kind() : iregp::hl;
}

iregp get_iregp_kind_or_hl(reg r) {
return get_iregp_kind_or_hl(is_indexable(r));
}

iregp get_iregp_kind_or_hl(regp rp) {
return get_iregp_kind_or_hl(rp == regp::hl);
}

iregp get_iregp_kind_or_hl(regp2 rp) {
return get_iregp_kind_or_hl(rp == regp2::hl);
}
};

// TODO: Split to a instructions verbalizer and a disassembler.
Expand Down Expand Up @@ -1514,8 +1559,6 @@ class i8080_disasm : public internals::disasm_base<i8080_decoder<root<D>>> {
self().on_format("shld W", nn); }
void on_out_n_a(fast_u8 n) {
self().on_format("out N", n); }
void on_pop_rp(regp2 rp) {
self().on_format("pop G", rp); }
void on_push_rp(regp2 rp) {
self().on_format("push G", rp); }
void on_xcall_nn(fast_u8 op, fast_u16 nn) {
Expand Down Expand Up @@ -1809,9 +1852,6 @@ class z80_disasm
self().on_format("out (c), R", r, iregp::hl, 0); }
void on_out_n_a(fast_u8 n) {
self().on_format("out (N), a", n); }
void on_pop_rp(regp2 rp) {
iregp irp = get_iregp_kind_or_hl(rp);
self().on_format("pop G", rp, irp); }
void on_push_rp(regp2 rp) {
iregp irp = get_iregp_kind_or_hl(rp);
self().on_format("push G", rp, irp); }
Expand Down Expand Up @@ -1955,29 +1995,10 @@ class z80_disasm
unreachable("Unknown block output operation.");
}

private:
static bool is_indexable(reg r) {
return r == reg::at_hl || r == reg::h || r == reg::l;
}

iregp get_iregp_kind_or_hl(bool indexable) {
return indexable ? self().on_get_iregp_kind() : iregp::hl;
}

iregp get_iregp_kind_or_hl(reg r) {
return get_iregp_kind_or_hl(is_indexable(r));
}

iregp get_iregp_kind_or_hl(regp rp) {
return get_iregp_kind_or_hl(rp == regp::hl);
}

iregp get_iregp_kind_or_hl(regp2 rp) {
return get_iregp_kind_or_hl(rp == regp2::hl);
}

protected:
using base::self;
using base::is_indexable;
using base::get_iregp_kind_or_hl;

template<typename T>
static T get_arg(const void **&args) {
Expand Down Expand Up @@ -2703,6 +2724,27 @@ class internals::executor_base : public B {
sp = inc16(sp);
self().on_set_sp(sp);
return make16(hi, lo); }
void on_pop_rp(regp2 rp) {
if(self().on_is_z80()) {
iregp irp = self().on_get_iregp_kind();
self().on_set_regp2(rp, irp, self().on_pop());
return;
}

fast_u16 nn = self().on_pop();
if(rp == regp2::af) {
// Not all flags are updated on pop psw.
nn = (nn & ~(xf_mask | yf_mask | nf_mask));
}
if(rp == regp2::af && self().on_is_to_use_lazy_flags()) {
flag_set flags(/* is_lazy= */ true);
flags.set(get_low8(nn), 1);
set_flags(flags);

self().on_set_a(get_high8(nn));
} else {
self().on_set_regp2(rp, nn);
} }
void on_call(fast_u16 nn) {
self().on_push(self().on_get_pc());
self().on_set_wz(nn);
Expand Down Expand Up @@ -3329,21 +3371,6 @@ class i8080_executor : public internals::executor_base<B> {
nn |= nf_mask;
}
self().on_push(nn); }
void on_pop_rp(regp2 rp) {
fast_u16 nn = self().on_pop();
if(rp == regp2::af) {
// Not all flags are updated on pop psw.
nn = (nn & ~(xf_mask | yf_mask | nf_mask));
}
if(rp == regp2::af && self().on_is_to_use_lazy_flags()) {
flag_set flags(/* is_lazy= */ true);
flags.set(get_low8(nn), 1);
set_flags(flags);

self().on_set_a(get_high8(nn));
} else {
self().on_set_regp2(rp, nn);
} }

protected:
using base::self;
Expand Down Expand Up @@ -3390,26 +3417,6 @@ class z80_executor : public internals::executor_base<B> {

void set_i_on_ld(fast_u8 i) { self().on_set_i(i); }

fast_u16 on_get_ix() {
// Always get the low byte first.
fast_u8 l = self().on_get_ixl();
fast_u8 h = self().on_get_ixh();
return make16(h, l); }
void on_set_ix(fast_u16 ix) {
// Always set the low byte first.
self().on_set_ixl(get_low8(ix));
self().on_set_ixh(get_high8(ix)); }

fast_u16 on_get_iy() {
// Always get the low byte first.
fast_u8 l = self().on_get_iyl();
fast_u8 h = self().on_get_iyh();
return make16(h, l); }
void on_set_iy(fast_u16 iy) {
// Always set the low byte first.
self().on_set_iyl(get_low8(iy));
self().on_set_iyh(get_high8(iy)); }

fast_u16 get_pc_on_disp_read() { return self().on_get_pc(); }
void set_pc_on_disp_read(fast_u16 pc) { self().on_set_pc(pc); }

Expand Down Expand Up @@ -3954,9 +3961,6 @@ class z80_executor : public internals::executor_base<B> {
void on_push_rp(regp2 rp) {
iregp irp = self().on_get_iregp_kind();
self().on_push(self().on_get_regp2(rp, irp)); }
void on_pop_rp(regp2 rp) {
iregp irp = self().on_get_iregp_kind();
self().on_set_regp2(rp, irp, self().on_pop()); }

protected:
using base::self;
Expand Down
23 changes: 0 additions & 23 deletions z80/_z80module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,6 @@ class machine : public B {
fast_u8 on_get_f() const { return state.f; }
void on_set_f(fast_u8 n) { state.f = n; }

fast_u8 on_get_ixh() const { return state.ixh; }
void on_set_ixh(fast_u8 n) { state.ixh = n; }

fast_u8 on_get_ixl() const { return state.ixl; }
void on_set_ixl(fast_u8 n) { state.ixl = n; }

fast_u8 on_get_iyh() const { return state.iyh; }
void on_set_iyh(fast_u8 n) { state.iyh = n; }

fast_u8 on_get_iyl() const { return state.iyl; }
void on_set_iyl(fast_u8 n) { state.iyl = n; }

fast_u16 on_get_bc() const { return make16(state.b, state.c); }
void on_set_bc(fast_u16 n) { split16(state.b, state.c, n); }

Expand All @@ -160,12 +148,6 @@ class machine : public B {
fast_u16 on_get_af() const { return make16(state.a, state.f); }
void on_set_af(fast_u16 n) { split16(state.a, state.f, n); }

fast_u16 on_get_ix() const { return make16(state.ixh, state.ixl); }
void on_set_ix(fast_u16 n) { split16(state.ixh, state.ixl, n); }

fast_u16 on_get_iy() const { return make16(state.iyh, state.iyl); }
void on_set_iy(fast_u16 n) { split16(state.iyh, state.iyl, n); }

fast_u16 on_get_pc() const { return state.pc; }
void on_set_pc(fast_u16 n) { state.pc = n; }

Expand All @@ -178,11 +160,6 @@ class machine : public B {
fast_u16 on_get_last_read_addr() const { return state.last_read_addr; }
void on_set_last_read_addr(fast_u16 n) { state.last_read_addr = n; }

iregp on_get_iregp_kind() const {
return static_cast<iregp>(state.irp_kind); }
void on_set_iregp_kind(iregp irp) {
state.irp_kind = static_cast<least_u8>(irp); }

bool on_is_int_disabled() const { return state.int_disabled; }
void on_set_is_int_disabled(bool f) { state.int_disabled = f; }

Expand Down
23 changes: 23 additions & 0 deletions z80/machine.inc
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,35 @@ class machine_object
z80::z80_decoder<z80::root<machine_object>>>,
object_state>> {
public:
iregp on_get_iregp_kind() const {
return static_cast<iregp>(state.irp_kind); }
void on_set_iregp_kind(iregp irp) {
state.irp_kind = static_cast<least_u8>(irp); }

fast_u8 on_get_ixh() const { return state.ixh; }
void on_set_ixh(fast_u8 n) { state.ixh = n; }

fast_u8 on_get_ixl() const { return state.ixl; }
void on_set_ixl(fast_u8 n) { state.ixl = n; }

fast_u8 on_get_iyh() const { return state.iyh; }
void on_set_iyh(fast_u8 n) { state.iyh = n; }

fast_u8 on_get_iyl() const { return state.iyl; }
void on_set_iyl(fast_u8 n) { state.iyl = n; }

fast_u8 on_get_i() const { return state.i; }
void on_set_i(fast_u8 n) { state.i = n; }

fast_u8 on_get_r() const { return state.r; }
void on_set_r(fast_u8 n) { state.r = n; }

fast_u16 on_get_ix() const { return make16(state.ixh, state.ixl); }
void on_set_ix(fast_u16 n) { split16(state.ixh, state.ixl, n); }

fast_u16 on_get_iy() const { return make16(state.iyh, state.iyl); }
void on_set_iy(fast_u16 n) { split16(state.iyh, state.iyl, n); }

fast_u16 on_get_ir() const { return make16(state.i, state.r); }
};

Expand Down

0 comments on commit 363e569

Please sign in to comment.