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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
|
/* 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"
// local
#include "r2kcl.h"
#include "z80mac.h"
unsigned word_parity( u16_t x ) {
// bitcount(x) performed by shift-and-add
u16_t tmp = (x & 0x5555) + ((x & 0xAAAA) >> 1);
tmp = (tmp & 0x3333) + ((tmp & 0xCCCC) >> 2);
tmp = (tmp & 0x0F0F) + ((tmp & 0xF0F0) >> 4);
tmp = (tmp & 0x000F) + ((tmp & 0x0F00) >> 8);
// parity determined by count being odd or even
return 0x01 ^ (tmp & 1);
}
/******** rabbit 2000 memory access helper functions *****************/
u32_t rabbit_mmu::logical_addr_to_phys( u16_t logical_addr ) {
u32_t phys_addr = logical_addr;
unsigned segnib = logical_addr >> 12;
if (segnib >= 0xE000)
{
phys_addr += ((u32_t)xpc) << 12;
}
else if (segnib >= ((segsize >> 4) & 0x0F))
{
phys_addr += ((u32_t)stackseg) << 12;
}
else if (segnib >= (segsize & 0x0F))
{
phys_addr += ((u32_t)dataseg) << 12;
}
return phys_addr;
}
void cl_r2k::store1( u16_t addr, t_mem val ) {
u32_t phys_addr;
if (mmu.io_flag == IOI) {
if ((mmu.mmidr ^ 0x80) & 0x80)
/* bit 7 = 0 --> use only 8-bits for internal I/O addresses */
addr = addr & 0x0ff;
if (addr == MMIDR) {
mmu.mmidr = val;
return;
}
if (addr == SADR) {
/* serial A (console when using the rabbit programming cable) */
putchar(val);
fflush(stdout);
}
return;
}
if (mmu.io_flag == IOE) {
/* I/O operation for external device (such as an ethernet controller) */
return;
}
phys_addr = mmu.logical_addr_to_phys( addr );
ram->write(phys_addr, val);
}
void cl_r2k::store2( u16_t addr, u16_t val ) {
u32_t phys_addr;
if (mmu.io_flag == IOI) {
/* I/O operation for on-chip device (serial ports, timers, etc) */
return;
}
if (mmu.io_flag == IOE) {
/* I/O operation for external device (such as an ethernet controller) */
return;
}
phys_addr = mmu.logical_addr_to_phys( addr );
ram->write(phys_addr, val & 0xff);
ram->write(phys_addr+1, (val >> 8) & 0xff);
}
u8_t cl_r2k::get1( u16_t addr ) {
u32_t phys_addr = mmu.logical_addr_to_phys( addr );
if (mmu.io_flag == IOI) {
/* stub for on-chip device I/O */
return 0;
}
if (mmu.io_flag == IOE) {
/* stub for external device I/O */
return 0;
}
return ram->read(phys_addr);
}
u16_t cl_r2k::get2( u16_t addr ) {
u32_t phys_addr = mmu.logical_addr_to_phys( addr );
u16_t l, h;
if (mmu.io_flag == IOI) {
/* stub for on-chip device I/O */
return 0;
}
if (mmu.io_flag == IOE) {
/* stub for external device I/O */
return 0;
}
l = ram->read(phys_addr );
h = ram->read(phys_addr+1);
return (h << 8) | l;
}
t_mem cl_r2k::fetch1( void ) {
return fetch( );
}
u16_t cl_r2k::fetch2( void ) {
u16_t c1, c2;
c1 = fetch( );
c2 = fetch( );
return (c2 << 8) | c1;
}
t_mem cl_r2k::fetch(void) {
/*
* Fetch without checking for breakpoint hit
*
* Used by bool cl_uc::fetch(t_mem *code) in sim.src/uc.cc
* which does check for a breakpoint hit
*/
u32_t phys_addr = mmu.logical_addr_to_phys( PC );
ulong code;
if (!rom)
return(0);
code= rom->read(phys_addr);
PC = (PC + 1) & 0xffffUL;
vc.fetch++;
return(code);
}
/******** start rabbit 2000 specific codes *****************/
int cl_r2k::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.SP = (regs.SP + d) & 0xffff;
return(resGO);
}
int cl_r2k::inst_altd(t_mem code) {
// stub
return(resGO);
}
int
cl_r2k::inst_r2k_ld(t_mem code)
{
/* 0xC4 ld hl,(sp+n)
* 0xD4 ld (sp+n),hl
* 0xE4 ld hl,(ix+d)
* DD E4 = ld hl,(hl+d) [note: (hl+d) breaks the normal prefix pattern]
* FD E4 = ld hl,(iy+d)
* 0xF4 ld (ix+d),hl
* DD F4 = ld (hl+d),hl
* FD F4 = ld (iy+d),hl
*/
switch(code) {
case 0xC4: regs.HL = get2( add_u16_disp(regs.SP, fetch()) ); vc.rd+= 2; break;
case 0xD4: store2( add_u16_disp(regs.SP, fetch()), regs.HL ); vc.wr+= 2; break;
case 0xE4: regs.HL = get2( add_u16_disp(regs.IX, fetch()) ); vc.rd+= 2; break;
case 0xF4: store2( add_u16_disp(regs.IX, fetch()), regs.HL ); vc.wr+= 2; break;
default:
return(resINV_INST);
}
return(resGO);
}
int cl_r2k::inst_r2k_ex (t_mem code) {
u16_t tempw;
switch(code) {
case 0xE3:
// EX DE', HL on rabbit processors
tempw = regs.aDE;
regs.aDE = regs.HL;
regs.HL = tempw;
return(resGO);
default:
return(resINV_INST);
}
}
int cl_r2k::inst_ljp(t_mem code) {
u16_t mn;
mn = fetch2(); /* don't clobber PC before the fetch for xmem page */
mmu.xpc = fetch1();
PC = mn;
return(resGO);
}
int cl_r2k::inst_lcall(t_mem code) {
u16_t mn;
push1(mmu.xpc);
push2(PC+2);
vc.wr+= 2;
mn = fetch2(); /* don't clobber PC before the fetch for xmem page */
mmu.xpc = fetch1();
PC = mn;
return(resGO);
}
int cl_r2k::inst_bool(t_mem code) {
regs.raf.F &= ~BIT_ALL;
if (regs.HL)
regs.HL = 1;
else
regs.raf.F |= BIT_Z;
return(resGO);
}
int cl_r2k::inst_r2k_and(t_mem code) { // AND HL,DE
regs.HL &= regs.DE;
regs.raf.F &= ~BIT_ALL;
if (regs.DE & 0x8000)
regs.raf.F |= BIT_S;
if (regs.DE == 0)
regs.raf.F |= BIT_Z;
if (word_parity(regs.DE))
regs.raf.F |= BIT_P;
return(resGO);
}
int cl_r2k::inst_r2k_or (t_mem code) { // OR HL,DE
regs.HL |= regs.DE;
regs.raf.F &= ~BIT_ALL;
if (regs.DE & 0x8000)
regs.raf.F |= BIT_S;
if (regs.DE == 0)
regs.raf.F |= BIT_Z;
if (word_parity(regs.DE))
regs.raf.F |= BIT_P;
return(resGO);
}
int cl_r2k::inst_mul(t_mem code) {
long m;
long m1 = (long)(regs.BC & 0x7fff);
long m2 = (long)(regs.DE & 0x7fff);
if (regs.BC & 0x8000)
m1 -= (1 << 15);
if (regs.DE & 0x8000)
m2 -= (1 << 15);
m = m1 * m2;
regs.BC = ((unsigned long)(m) & 0xffff);
regs.HL = ((unsigned long)(m) >> 16) & 0xffff;
return(resGO);
}
int cl_r2k::inst_rl_de(t_mem code) {
unsigned int oldcarry = (regs.raf.F & BIT_C);
regs.raf.F &= ~BIT_ALL;
regs.raf.F |= (((regs.DE >> 15) & 1U) << BITPOS_C);
regs.DE = (regs.DE << 1) | (oldcarry >> BITPOS_C);
if (regs.DE & 0x8000)
regs.raf.F |= BIT_S;
if (regs.DE == 0)
regs.raf.F |= BIT_Z;
if (word_parity(regs.DE))
regs.raf.F |= BIT_P;
return(resGO);
}
int cl_r2k::inst_rr_de(t_mem code) {
unsigned int oldcarry = (regs.raf.F & BIT_C);
regs.raf.F &= ~BIT_ALL;
regs.raf.F |= ((regs.DE & 1) << BITPOS_C);
regs.DE = (regs.DE >> 1) | (oldcarry << (15 - BITPOS_C));
if (regs.DE & 0x8000)
regs.raf.F |= BIT_S;
if (regs.DE == 0)
regs.raf.F |= BIT_Z;
if (word_parity(regs.DE))
regs.raf.F |= BIT_P;
return(resGO);
}
int cl_r2k::inst_rr_hl(t_mem code) // RR HL
{
unsigned int oldcarry = (regs.raf.F & BIT_C);
regs.raf.F &= ~BIT_ALL;
regs.raf.F |= ((regs.HL & 1) << BITPOS_C);
regs.HL = (regs.HL >> 1) | (oldcarry << (15 - BITPOS_C));
if (regs.HL & 0x8000)
regs.raf.F |= BIT_S;
if (regs.HL == 0)
regs.raf.F |= BIT_Z;
if (word_parity(regs.HL))
regs.raf.F |= BIT_P;
return(resGO);
}
int
cl_r2k::inst_rst(t_mem code)
{
switch(code) {
case 0xC7: // RST 0
push2(PC+2);
PC = 0x0;
vc.wr+= 2;
break;
case 0xCF: // RST 8
return(resINV_INST);
case 0xD7: // RST 10H
push2(PC+2);
PC = 0x10;
vc.wr+= 2;
break;
case 0xDF: // RST 18H
push2(PC+2);
PC = 0x18;
vc.wr+= 2;
break;
case 0xE7: // RST 20H
push2(PC+2);
PC = 0x20;
vc.wr+= 2;
break;
case 0xEF: // RST 28H
//PC = 0x28;
switch (regs.raf.A) {
case 0:
return(resBREAKPOINT);
// ::exit(0);
break;
case 1:
//printf("PUTCHAR-----> %xH\n", regs.hl.l);
putchar(regs.hl.l);
fflush(stdout);
break;
}
break;
case 0xF7: // RST 30H
return(resINV_INST); // opcode is used for MUL on rabbit 2000+
break;
case 0xFF: // RST 38H
push2(PC+2);
PC = 0x38;
vc.wr+= 2;
break;
default:
return(resINV_INST);
break;
}
return(resGO);
}
int cl_r2k::inst_xd(t_mem prefix)
{
u16_t *regs_IX_OR_IY = (prefix==0xdd)?(®s.IX):(®s.IY);
t_mem code;
if (fetch(&code))
return(resBREAKPOINT);
switch (code) {
// 0x06 LD A,(IX+A) is r4k+ instruction
case 0x21: // LD IX,nnnn
case 0x22: // LD (nnnn),IX
case 0x2A: // LD IX,(nnnn)
case 0x2E: // LD LX,nn
case 0x36: // LD (IX+dd),nn
case 0x46: // LD B,(IX+dd)
case 0x4E: // LD C,(IX+dd)
case 0x56: // LD D,(IX+dd)
case 0x5E: // LD E,(IX+dd)
case 0x66: // LD H,(IX+dd)
case 0x6E: // LD L,(IX+dd)
case 0x70: // LD (IX+dd),B
case 0x71: // LD (IX+dd),C
case 0x72: // LD (IX+dd),D
case 0x73: // LD (IX+dd),E
case 0x74: // LD (IX+dd),H
case 0x75: // LD (IX+dd),L
case 0x77: // LD (IX+dd),A
case 0x7E: // LD A,(IX+dd)
case 0xF9: // LD SP,IX
if (prefix == 0xdd)
return(inst_dd_ld(code));
else
return(inst_fd_ld(code));
case 0x7C: // LD HL,IX
regs.HL = *regs_IX_OR_IY; // LD HL, IX|IY for rabbit processors
return(resGO);
case 0x7D: // LD IX,HL
*regs_IX_OR_IY = regs.HL; // LD IX|IY,HL for rabbit processors
return(resGO);
case 0x23: // INC IX
case 0x34: // INC (IX+dd)
if (prefix == 0xdd)
return(inst_dd_inc(code));
else
return(inst_fd_inc(code));
case 0x09: // ADD IX,BC
case 0x19: // ADD IX,DE
case 0x29: // ADD IX,IX
case 0x39: // ADD IX,SP
case 0x86: // ADD A,(IX)
if (prefix == 0xdd)
return(inst_dd_add(code));
else
return(inst_fd_add(code));
case 0x2B: // DEC IX
case 0x35: // DEC (IX+dd)
if (prefix == 0xdd)
return(inst_dd_dec(code));
else
return(inst_fd_dec(code));
// 0x4C TEST IX is r4k+
case 0x8E: // ADC A,(IX)
case 0x96: // SUB (IX+dd)
case 0x9E: // SBC A,(IX+dd)
case 0xA6: // AND (IX+dd)
case 0xAE: // XOR (IX+dd)
case 0xB6: // OR (IX+dd)
case 0xBE: // CP (IX+dd)
if (prefix == 0xdd)
return(inst_dd_misc(code));
else
return(inst_fd_misc(code));
case 0xC4: // LD IX,(SP+n)
*regs_IX_OR_IY = get2( add_u16_disp(regs.SP, fetch()) );
vc.rd+= 2;
return(resGO);
case 0xCB: // escape, IX prefix to CB commands
// fixme: limit the opcodes passed through to those officially
// documented as present on the rabbit processors
if (prefix == 0xdd)
return(inst_ddcb()); /* see inst_ddcb.cc */
else
return(inst_fdcb()); /* see inst_fdcb.cc */
case 0xCC: // BOOL IX|IY
if (*regs_IX_OR_IY)
*regs_IX_OR_IY = 1;
// update flags
regs.raf.F &= ~BIT_ALL;
// bit 15 will never be set, so S<=0
if (*regs_IX_OR_IY == 0)
regs.raf.F |= BIT_Z;
// L/V and C are always cleared
return(resGO);
case 0xD4: // LD (SP+n),IX|IY
store2( add_u16_disp(regs.SP, fetch()), *regs_IX_OR_IY );
vc.wr+= 2;
return(resGO);
case 0xE1: // POP IX
*regs_IX_OR_IY = get2(regs.SP);
regs.SP+=2;
vc.rd+= 2;
return(resGO);
case 0xE3: // EX (SP),IX
{
u16_t tempw;
tempw = *regs_IX_OR_IY;
*regs_IX_OR_IY = get2(regs.SP);
store2(regs.SP, tempw);
vc.rd+= 2;
vc.wr+= 2;
}
return(resGO);
case 0xE4:
if (prefix == 0xDD)
regs.HL = get2( add_u16_disp(regs.HL, fetch()) );
else
regs.HL = get2( add_u16_disp(regs.IY, fetch()) );
vc.rd+= 2;
return(resGO);
case 0xE5: // PUSH IX
push2(*regs_IX_OR_IY);
vc.wr+= 2;
return(resGO);
case 0xE9: // JP (IX)
PC = *regs_IX_OR_IY;
return(resGO);
case 0xEA:
push2(PC);
PC = *regs_IX_OR_IY;
vc.wr+= 2;
return(resGO);
case 0xDC: // AND IX|IY,DE for rabbit processors
case 0xEC: // OR IX|IY,DE for rabbit processors
if (code == 0xDC)
*regs_IX_OR_IY &= regs.DE;
else
*regs_IX_OR_IY |= regs.DE;
// update flags
regs.raf.F &= ~BIT_ALL;
if (*regs_IX_OR_IY & 0x8000)
regs.raf.F |= BIT_S;
if (regs_IX_OR_IY == 0)
regs.raf.F |= BIT_Z;
if (word_parity(*regs_IX_OR_IY))
regs.raf.F |= BIT_P;
return(resGO);
case 0xF4: // LD (HL|IY+d),HL
if (prefix == 0xDD)
store2( add_u16_disp(regs.HL, fetch()), regs.HL );
else
store2( add_u16_disp(regs.IY, fetch()), regs.HL );
vc.wr+= 2;
return(resGO);
case 0xFC: // RR IX|IY
{
u16_t tmp = (regs.raf.F & BIT_C) << (15 - BITPOS_C);
tmp |= (*regs_IX_OR_IY >> 1);
regs.raf.F = (regs.raf.F & ~BIT_C) | ((*regs_IX_OR_IY & 1) << BITPOS_C);
if (*regs_IX_OR_IY & 0x8000)
regs.raf.F |= BIT_S;
if (*regs_IX_OR_IY == 0)
regs.raf.F |= BIT_Z;
if (word_parity(*regs_IX_OR_IY))
regs.raf.F |= BIT_P;
return(resGO);
}
default:
return(resINV_INST);
}
return(resINV_INST);
}
|