Skip to content

Commit

Permalink
removed variable from get/set_register
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed Jun 24, 2024
1 parent bd1228b commit a4f7539
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
58 changes: 32 additions & 26 deletions cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,39 +143,31 @@ void cpu::reset()
init_interrupt_queue();
}

uint16_t cpu::get_register(const int nr, const rm_selection_t mode_selection) const
uint16_t cpu::get_register(const int nr) const
{
if (nr < 6) {
int set = get_register_set();

return regs0_5[set][nr];
}

if (nr == 6) {
if (mode_selection == rm_prev)
return sp[getPSW_prev_runmode()];

if (nr == 6)
return sp[getPSW_runmode()];
}

assert(nr == 7);

return pc;
}

void cpu::set_register(const int nr, const uint16_t value, const rm_selection_t mode_selection)
void cpu::set_register(const int nr, const uint16_t value)
{
if (nr < 6) {
int set = get_register_set();

regs0_5[set][nr] = value;
}
else if (nr == 6) {
if (mode_selection == rm_prev)
sp[getPSW_prev_runmode()] = value;
else
sp[getPSW_runmode()] = value;
}
else if (nr == 6)
sp[getPSW_runmode()] = value;
else {
assert(nr == 7);
pc = value;
Expand Down Expand Up @@ -491,22 +483,22 @@ gam_rc_t cpu::getGAM(const uint8_t mode, const uint8_t reg, const word_mode_t wo
switch(mode) {
case 0: // Rn
g.reg = reg;
g.value = get_register(reg, rm_cur) & (word_mode == wm_byte ? 0xff : 0xffff);
g.value = get_register(reg) & (word_mode == wm_byte ? 0xff : 0xffff);
break;
case 1: // (Rn)
g.addr = get_register(reg, rm_cur);
g.addr = get_register(reg);
if (read_value)
g.value = b->read(g.addr.value(), word_mode, rm_cur, isR7_space);
break;
case 2: // (Rn)+ / #n
g.addr = get_register(reg, rm_cur);
g.addr = get_register(reg);
if (read_value)
g.value = b->read(g.addr.value(), word_mode, rm_cur, isR7_space);
addRegister(reg, rm_cur, word_mode == wm_word || reg == 7 || reg == 6 ? 2 : 1);
g.mmr1_update = { word_mode == wm_word || reg == 7 || reg == 6 ? 2 : 1, reg };
break;
case 3: // @(Rn)+ / @#a
g.addr = b->read(get_register(reg, rm_cur), wm_word, rm_cur, isR7_space);
g.addr = b->read(get_register(reg), wm_word, rm_cur, isR7_space);
// might be wrong: the adds should happen when the read is really performed, because of traps
addRegister(reg, rm_cur, 2);
g.mmr1_update = { 2, reg };
Expand All @@ -518,30 +510,30 @@ gam_rc_t cpu::getGAM(const uint8_t mode, const uint8_t reg, const word_mode_t wo
addRegister(reg, rm_cur, word_mode == wm_word || reg == 7 || reg == 6 ? -2 : -1);
g.mmr1_update = { word_mode == wm_word || reg == 7 || reg == 6 ? -2 : -1, reg };
g.space = d_space;
g.addr = get_register(reg, rm_cur);
g.addr = get_register(reg);
if (read_value)
g.value = b->read(g.addr.value(), word_mode, rm_cur, isR7_space);
break;
case 5: // @-(Rn)
addRegister(reg, rm_cur, -2);
g.mmr1_update = { -2, reg };
g.addr = b->read(get_register(reg, rm_cur), wm_word, rm_cur, isR7_space);
g.addr = b->read(get_register(reg), wm_word, rm_cur, isR7_space);
g.space = d_space;
if (read_value)
g.value = b->read(g.addr.value(), word_mode, rm_cur, g.space);
break;
case 6: // x(Rn) / a
next_word = b->read(getPC(), wm_word, rm_cur, i_space);
addRegister(7, rm_cur, + 2);
g.addr = get_register(reg, rm_cur) + next_word;
g.addr = get_register(reg) + next_word;
g.space = d_space;
if (read_value)
g.value = b->read(g.addr.value(), word_mode, rm_cur, g.space);
break;
case 7: // @x(Rn) / @a
next_word = b->read(getPC(), wm_word, rm_cur, i_space);
addRegister(7, rm_cur, + 2);
g.addr = b->read(get_register(reg, rm_cur) + next_word, wm_word, rm_cur, d_space);
g.addr = b->read(get_register(reg) + next_word, wm_word, rm_cur, d_space);
g.space = d_space;
if (read_value)
g.value = b->read(g.addr.value(), word_mode, rm_cur, g.space);
Expand All @@ -563,7 +555,13 @@ bool cpu::putGAM(const gam_rc_t & g, const uint16_t value)
return rc.is_psw == false;
}

set_register(g.reg.value(), value, g.mode_selection);
if (g.mode_selection == rm_prev) {
assert(g.reg.value() == 6);
sp[getPSW_prev_runmode()] = value;
}
else {
set_register(g.reg.value(), value);
}

return true;
}
Expand Down Expand Up @@ -1419,8 +1417,12 @@ bool cpu::single_operand_instructions(const uint16_t instr)
// always words: word_mode-bit is to select between MFPI and MFPD
uint16_t v = 0xffff;

if (dst_mode == 0)
v = get_register(dst_reg, rm_prev);
if (dst_mode == 0) {
if (dst_reg == 6)
v = sp[getPSW_prev_runmode()];
else
v = get_register(dst_reg);
}
else {
// calculate address in current address space
auto a = getGAMAddress(dst_mode, dst_reg, wm_word);
Expand All @@ -1444,8 +1446,12 @@ bool cpu::single_operand_instructions(const uint16_t instr)
uint16_t v = popStack();
bool set_flags = true;

if (dst_mode == 0)
set_register(dst_reg, v, rm_prev);
if (dst_mode == 0) {
if (dst_reg == 6)
sp[getPSW_prev_runmode()] = v;
else
set_register(dst_reg, v);
}
else {
auto a = getGAMAddress(dst_mode, dst_reg, wm_word);
addToMMR1(a);
Expand Down
4 changes: 2 additions & 2 deletions cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class cpu
uint16_t getStackPointer(const int which) const { assert(which >= 0 && which < 4); return sp[which]; }
uint16_t getPC() const { return pc; }

void set_register(const int nr, const uint16_t value, const rm_selection_t mode_selection = rm_cur);
void set_register(const int nr, const uint16_t value);
void set_registerLowByte(const int nr, const word_mode_t word_mode, const uint16_t value);
// used by 'main' for json-validation
void lowlevel_register_set(const uint8_t set, const uint8_t reg, const uint16_t value);
Expand All @@ -193,7 +193,7 @@ class cpu
void setStackPointer(const int which, const uint16_t value) { assert(which >= 0 && which < 4); sp[which] = value; }
void setPC(const uint16_t value) { pc = value; }

uint16_t get_register(const int nr, const rm_selection_t mode_selection = rm_cur) const;
uint16_t get_register(const int nr) const;

bool put_result(const gam_rc_t & g, const uint16_t value);
};

0 comments on commit a4f7539

Please sign in to comment.