1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
* Simulated instructions specific to the LR35902, the Z-80 derivative used
* in the gameboy.
*
* 2011-12-21 created by Leland Morrison
*
*
This file is part of microcontroller simulator: ucsim.
UCSIM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
UCSIM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UCSIM; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
/*@1@*/
#include "ddconfig.h"
#include "lr35902cl.h"
static u8_t swap_nibbles(u8_t val) {
return ((val >> 4) & 0x0f) | ((val << 4) & 0xf0);
}
int cl_lr35902::inst_cb(void) {
u8_t result;
t_mem code;
if ( (peek1( ) & 0xf8) != 0x30 )
return cl_z80::inst_cb( );
code = fetch1();
/* perform SWAP instead of slia */
switch(code) {
case 0x30: result = regs.bc.h = swap_nibbles(regs.bc.h); break; /* b */
case 0x31: result = regs.bc.l = swap_nibbles(regs.bc.l); break; /* c */
case 0x32: result = regs.de.h = swap_nibbles(regs.de.h); break; /* d */
case 0x33: result = regs.de.l = swap_nibbles(regs.de.l); break; /* e */
case 0x34: result = regs.hl.l = swap_nibbles(regs.hl.h); break; /* h */
case 0x35: result = regs.hl.h = swap_nibbles(regs.hl.l); break; /* l */
case 0x36: /* SWAP (HL) */
{
result = swap_nibbles(get1(regs.HL));
store1(regs.HL, result);
vc.rd++;
vc.wr++;
}
break;
case 0x37: result = regs.raf.A = swap_nibbles(regs.raf.A); break; /* swap a */
default: return resINV_INST;
}
regs.raf.F = (result)?0:0x80; // all except zero are simply cleared
return(resGO);
}
int cl_lr35902::inst_st_sp_abs(t_mem code) {
if (code == 0x08) {
u16_t addr = fetch2( );
store2( addr, regs.SP );
vc.wr+= 2;
return(resGO);
}
return resINV_INST;
}
int cl_lr35902::inst_stop0 (t_mem code) {
// TODO: change to wait for a signal for simulated hardware
return resHALT;
}
int cl_lr35902::inst_ldi (t_mem code) {
if (code == 0x22) {
store1( regs.HL, regs.raf.A );
regs.HL ++;
vc.wr++;
return resGO;
} else if (code == 0x2A) {
regs.raf.A = get1( regs.HL );
regs.HL ++;
vc.rd++;
return resGO;
}
return resINV_INST;
}
int cl_lr35902::inst_ldd (t_mem code) {
if (code == 0x32) {
store1( regs.HL, regs.raf.A );
regs.HL --;
vc.wr++;
return resGO;
} else if (code == 0x3A) {
regs.raf.A = get1( regs.HL );
regs.HL --;
vc.rd++;
return resGO;
}
return resINV_INST;
}
int cl_lr35902::inst_ldh (t_mem code) {
u16_t addr = 0xFF00 + fetch1( );
if (code == 0xE0) {
store1( addr, regs.raf.A );
vc.wr++;
return resGO;
} else if (code == 0xF0) {
regs.raf.A = get1( addr );
vc.rd++;
return resGO;
}
return resINV_INST;
}
int cl_lr35902::inst_reti (t_mem code) {
/* enable interrupts */
cl_z80::inst_ei(0xFB);
/* pop2(PC); */
PC=get2(regs.SP);
regs.SP+=2;
vc.rd+= 2;
return resGO;
}
int cl_lr35902::inst_add_sp_d(t_mem code) {
u16_t d = fetch( );
/* sign-extend d from 8-bits to 16-bits */
d |= (d>>7)*0xFF00;
regs.raf.F &= ~(BIT_ALL); /* clear these */
if ((regs.SP & 0x0FFF) + (d & 0x0FFF) > 0x0FFF)
regs.raf.F |= BIT_A;
if (regs.SP + (int)(d) > 0xffff)
regs.raf.F |= BIT_C;
regs.SP = (regs.SP + d) & 0xffff;
return(resGO);
}
int cl_lr35902::inst_ld16 (t_mem code) {
u16_t addr = fetch2( );
if (code == 0xEA) {
store1( addr, regs.raf.A );
vc.wr++;
return resGO;
} else if (code == 0xFA) {
regs.raf.A = get1( addr );
vc.rd++;
return resGO;
}
return resINV_INST;
}
int cl_lr35902::inst_ldhl_sp (t_mem code) {
u16_t d = fetch( );
/* sign-extend d from 8-bits to 16-bits */
d |= (d>>7)*0xFF00;
regs.raf.F &= ~(BIT_ALL); /* clear these */
if ((regs.SP & 0x0FFF) + (d & 0x0FFF) > 0x0FFF)
regs.raf.F |= BIT_A;
if (regs.SP + (int)(d) > 0xffff)
regs.raf.F |= BIT_C;
regs.HL = (regs.SP + d) & 0xffff;
return resGO;
}
|