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 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
|
/*
* 16c84.cc -- contains all 16c84-specific simulator routines
*/
#include "picsim.hh"
#include <ctype.h>
#include <stdlib.h>
info_reg pic16c84_info[]={
{"STATUS",{"IRP","RP1","RP0","TO","PD","Z","DC","C"},0,0},
{"OPTION",{"RBPU","INTE","RTS","RTE","PSA","PS2","PS1","PS0"},0,0},
{"INTCON",{"GIE","EEIE","RTIE","INTE","RBIE","RTIF","INTF","RBIF"},0,0},
{NULL,{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},0,0}
};
pin_info pic16c84_pin[18] = {
{ "RA2", 0, 0 },
{ "RA3", 0, 0 },
{ "RA4", 0, 0 },
{ "MCLR", 0, 0 },
{ "Vss", 2, 0 },
{ "RB0", 0, 0 },
{ "RB1", 0, 0 },
{ "RB2", 0, 0 },
{ "RB3", 0, 0 },
{ "RB4", 0, 0 },
{ "RB5", 0, 0 },
{ "RB6", 0, 0 },
{ "RB7", 0, 0 },
{ "Vdd", 2, 0 },
{ "OSC2", 2, 0 },
{ "OSC1", 2, 0 },
{ "RA0", 0, 0 },
{ "RA1", 0, 0 }
} ;
pins_info pic16c84_pins = {
18,
pic16c84_pin
} ;
typedef struct {
u_int mask, dont_care, discriminator;
u_int literal;
unsigned d : 1,
f : 1,
b : 1;
char *name;
void (*execute)( void );
} pic16c84_instr;
#define C 0001 // Carry
#define DC 0002 // Digit carry
#define Z 0004 // Zero
#define PD 0010 // Power Down
#define TO 0020 // Time-out
#define RP0 0040 // Page select bit 0
#define RP1 0100 // Page select bit 1 (not used)
#define IRP 0200 // Indirect addressing page select (not used)
extern pic16c84_instr PIC16C84_INSTR[];
static int match_instr( u_int instr );
static void use_a_cycle( Boolean no_ints = False );
static void sleep_a_cycle( void );
static char get_f( u_int instr, int *p = NULL, int *r = NULL );
static char get_w( void );
static void set_f( int p, int r, char val, Boolean set_z );
static void set_w( char val, Boolean set_z );
static void set_status( int set, int clear );
static void init_pic16c84( void );
static void reset_pic16c84( void );
static char *disasm_pic16c84( u_int instr );
static char status_hook( reg *status, Boolean write, char v = 0 );
static char port_hook( reg *port, Boolean write, char v = 0 );
/*
* This is the entry point to the Pic16C84 simulator. All calls from the
* generic simulator come through here.
*/
SimAR
pic16c84( SimFunc func, SimAR arg )
{
SimAR result;
int i;
static char buf[64];
switch (func) {
case Init:
init_pic16c84( );
return result;
case Reset:
reset_pic16c84( );
return result;
case GetPage:
result.i = (Pic.reg_file[0][3].value >> 4) & 3;
return result;
case GetInfo:
result.p = pic16c84_info;
pic16c84_info[0].value = Pic.reg_file[0][3].value;
pic16c84_info[1].value = Pic.reg_file[1][1].value;
pic16c84_info[2].value = Pic.reg_file[0][11].value;
return result;
case GetPins:
result.p = &pic16c84_pins;
pic16c84_pin[0].io = !(Pic.reg_file[1][5].value & 4);
pic16c84_pin[0].hi = (Pic.reg_file[0][5].value & 4) != 0;
pic16c84_pin[1].io = !(Pic.reg_file[1][5].value & 8);
pic16c84_pin[1].hi = (Pic.reg_file[0][5].value & 8) != 0;
pic16c84_pin[2].io = !(Pic.reg_file[1][5].value & 16);
pic16c84_pin[2].hi = (Pic.reg_file[0][5].value & 16) != 0;
pic16c84_pin[16].io = !(Pic.reg_file[1][5].value & 1);
pic16c84_pin[16].hi = (Pic.reg_file[0][5].value & 1) != 0;
pic16c84_pin[17].io = !(Pic.reg_file[1][5].value & 2);
pic16c84_pin[17].hi = (Pic.reg_file[0][5].value & 2) != 0;
for (i = 0; i < 8; i++) {
pic16c84_pin[i + 5].io = !(Pic.reg_file[1][6].value & (1 << i));
pic16c84_pin[i + 5].hi = (Pic.reg_file[0][6].value & (1 << i))!=0;
}
return result;
case Disassemble:
result.p = buf;
sprintf( buf, "%04X %s",
arg.i, disasm_pic16c84( Pic.uinfo.picmemmap[arg.i] ) );
return result;
case Step:
if (!Pic.sleeping) {
if ((i = match_instr( Pic.uinfo.picmemmap[Pic.pc] )) == -1) {
fprintf( stderr,
"executed invalid opcode 0x%04X at PC 0x%04X as NOP\n",
Pic.uinfo.picmemmap[Pic.pc],
Pic.pc );
use_a_cycle( );
} else {
PIC16C84_INSTR[i].execute( );
use_a_cycle( );
}
} else {
sleep_a_cycle( );
}
return result;
default:
exit( 1 );
}
}
/*
* PIC16C84 initialization
*/
static void
init_pic16c84( void )
{
int i, j;
Pic.isize = 0x400;
Pic.reg_pages = 2;
Pic.nstack = 8;
for (i = 0; i < Pic.reg_pages; i++) {
for (j = 0; j < 128; j++) {
Pic.reg_file[i][j].redirect_page = i; /* redirect to self */
Pic.reg_file[i][j].redirect_reg = j;
Pic.reg_file[i][j].implemented = (j <= 0x2f) ? 0xff : 0;
Pic.reg_file[i][j].modified = 0;
Pic.reg_file[i][j].modified_last = 0;
Pic.reg_file[i][j].value = 0;
Pic.reg_file[i][j].hook = 0;
switch (j) {
case 0:
Pic.reg_file[i][j].name = " W";
Pic.reg_file[i][j].redirect_page = 0;
break;
case 1:
if (!i)
Pic.reg_file[i][j].name = " RTCC";
else {
Pic.reg_file[i][j].name = "OPTION";
Pic.reg_file[i][j].value = 0xff;
}
break;
case 2:
Pic.reg_file[i][j].name = " PCL";
Pic.reg_file[i][j].redirect_page = 0;
break;
case 3:
Pic.reg_file[i][j].name = "STATUS";
Pic.reg_file[i][j].value = 0x18;
Pic.reg_file[i][j].redirect_page = 0;
Pic.reg_file[0][j].hook = status_hook;
break;
case 4:
Pic.reg_file[i][j].name = " FSR";
Pic.reg_file[i][j].redirect_page = 0;
break;
case 5:
Pic.reg_file[i][j].name = (!i) ? " PORTA" : " TRISA";
Pic.reg_file[i][j].implemented = 0x1f;
if (i) Pic.reg_file[i][j].value = 0x1f;
Pic.reg_file[i][j].hook = port_hook;
break;
case 6:
Pic.reg_file[i][j].name = (!i) ? " PORTB" : " TRISB";
if (i) Pic.reg_file[i][j].value = 0xff;
Pic.reg_file[i][j].hook = port_hook;
break;
case 7:
Pic.reg_file[i][j].name = "------";
Pic.reg_file[i][j].implemented = 0;
break;
case 8:
Pic.reg_file[i][j].name = (!i) ? "EEDATA" : "EECON1"; break;
case 9:
Pic.reg_file[i][j].name = (!i) ? " EEADR" : "EECON2"; break;
case 10:
Pic.reg_file[i][j].name = "PCLATH";
Pic.reg_file[i][j].implemented = 0x1f;
Pic.reg_file[i][j].redirect_page = 0;
break;
case 11:
Pic.reg_file[i][j].name = "INTCON"; break;
default:
Pic.reg_file[i][j].name = NULL;
if (i) Pic.reg_file[i][j].redirect_page = 0;
break;
}
}
}
Pic.wdt_left = (u_long)(Pic.uinfo.clock * Pic.wdt_max / 4.0);
Pic.prescaler = 128;
}
static void
reset_pic16c84( void )
{
if (Pic.sleeping) {
set_status( TO, IRP | RP1 | RP0 | PD );
set_f( 0, 11, 0, False ); // INTCON = 0
} else {
set_f( 0, 11, get_f( 11, NULL, NULL ) & 1, False ); // INTCON = 0
set_status( 0, IRP | RP1 | RP0 );
}
Pic.sleeping = 0;
Pic.pc = 0;
Pic.wdt_left = (u_long)(Pic.uinfo.clock * Pic.wdt_max / 4.0);
Pic.prescaler = 128;
set_f( 1, 1, ~0, False ); // OPTION = ~0
set_f( 1, 5, ~0, False ); // Set TRIS-A & TRIS-B to 1's
set_f( 1, 6, ~0, False );
set_f( 0, 10, 0, False ); // PCLATH = 0
}
static char
status_hook( reg *status, Boolean write, char v )
{
if (write) {
Boolean switch_page = ((v ^ status->value) & RP0);
status->value = v;
if (switch_page) {
String s = "next";
Cardinal n = 1;
reg_switch( NULL, NULL, &s, &n );
instr_refresh( NULL, NULL, NULL, NULL );
}
}
return status->value;
}
static char
port_hook( reg *port, Boolean write, char v )
{
if (write) {
Boolean twiddle_bits = (v ^ port->value);
port->value = v & port->implemented;
if (twiddle_bits) ic_update( );
}
return port->value;
}
/*
* Common stuff that must be done every time the processor executes a
* cycle:
* Increment the Ticks counter
* Increment the PC & set the PCL register
* Decrement the WDT, check for timeout
* Decrement the RTCC, check for interrupt
*
* Functions that are going to diddle the PC and just want to call this
* to get the necessary extra cycle delay pass no_ints == True to avoid
* getting a WDT timeout or interrupt in the middle of the instruction.
*/
static void
use_a_cycle( Boolean no_ints )
{
Boolean wdt_tick = True;
Boolean rtcc_tick = !(Pic.reg_file[1][1].value & 0x20);
Pic.ticks += 4;
if (--Pic.millisec_left <= 0) {
Pic.millisec += 1;
Pic.millisec_left = (int)(Pic.uinfo.clock / 4000.0);
}
Pic.pc += 1;
if (Pic.pc >= Pic.isize) Pic.pc = 0;
/*
* The prescaler will tick if it's assigned to the WDT or if
* the RTCC is running off the internal clock:
*/
if (Pic.reg_file[1][1].value & 0x28) {
if (!no_ints || Pic.prescaler != 1) Pic.prescaler -= 1;
if (Pic.prescaler > 0) {
/*
* Prescaler hasn't timed out yet ... no tick on whatever
* it's connected to.
*/
if (Pic.reg_file[1][1].value & 8)
wdt_tick = False;
else
rtcc_tick = False;
} else {
/*
* Prescaler has timed out ... tick on whatever it's
* connected to.
*/
if (Pic.reg_file[1][1].value & 8)
wdt_tick = True;
else
rtcc_tick = True;
/*
* Also, the prescaler gets reset.
*/
Pic.prescaler = (((Pic.reg_file[1][1].value & 8) ? 1 : 2) <<
(Pic.reg_file[1][1].value & 7));
}
}
if (wdt_tick && (Pic.wdt_left || !no_ints)) {
if (Pic.wdt_left) {
Pic.wdt_left -= 1;
} else {
if (Pic.uinfo.wd_fuse) {
Pic.pc = 0; // WDT timeout!
Pic.sleeping = 0;
Pic.wdt_left = (u_long)(Pic.uinfo.clock * Pic.wdt_max / 4.0);
Pic.prescaler = 128;
set_f( 1, 1, ~0, False ); // OPTION = ~0
set_f( 1, 5, ~0, False ); // Set TRIS-A & TRIS-B to 1's
set_f( 1, 6, ~0, False );
set_f( 0, 10, 0, False ); // PCLATH = 0
set_status( PD, IRP | RP1 | RP0 | TO );
info_refresh( NULL, NULL, NULL, NULL );
if (Pic.running)
sim_toggle_run( NULL, NULL, NULL, NULL );
} else {
Pic.wdt_left = (u_long)(Pic.uinfo.clock * Pic.wdt_max / 4.0);
if (Pic.reg_file[1][1].value & 8) {
Pic.prescaler = 1 << (Pic.reg_file[1][1].value & 7);
}
}
}
}
if (rtcc_tick && (!no_ints || Pic.reg_file[0][1].value != 1)) {
Pic.reg_file[0][1].value -= 1; // RTCC decrements
Pic.reg_file[0][1].modified = 1;
// fixme: Check here for RTCC interrupt
}
set_f( 0, 2, Pic.pc & 0xff, False );
}
/*
* Common stuff that must be done every time the processor sleeps a cycle:
* Decrement the WDT & check for wakeup
*/
static void
sleep_a_cycle( void )
{
if (--Pic.millisec_left <= 0) {
Pic.millisec += 1;
Pic.millisec_left = (int)(Pic.uinfo.clock / 4000.0);
}
if (Pic.reg_file[1][1].value & 8) {
// Prescaler is assigned to WDT
Pic.prescaler -= 1;
if (Pic.prescaler > 0) return;
Pic.prescaler = 1 << (Pic.reg_file[1][1].value & 7);
}
if (Pic.wdt_left) {
Pic.wdt_left -= 1;
} else {
// WDT timeout during SLEEP
Pic.sleeping = 0;
Pic.wdt_left = (u_long)(Pic.uinfo.clock * Pic.wdt_max / 4.0);
if (Pic.reg_file[1][1].value & 8) {
Pic.prescaler = 1 << (Pic.reg_file[1][1].value & 7);
}
info_refresh( NULL, NULL, NULL, NULL );
set_status( 0, PD | TO );
Pic.pc += 1;
set_f( 0, 2, Pic.pc & 0xff, False );
}
}
static char
get_f( u_int instr, int *p, int *r )
{
int a, b, c, d;
a = (Pic.reg_file[0][3].value >> 5) & 1;
b = instr & 0x7f;
if (!b) { // use contents of FSR
c = (Pic.reg_file[0][4].value >> 7) & 1;
d = Pic.reg_file[0][4].value;
} else {
c = Pic.reg_file[a][b].redirect_page;
d = Pic.reg_file[a][b].redirect_reg;
}
if (p != NULL) *p = c;
if (r != NULL) *r = d;
if (d) {
if (Pic.reg_file[c][d].hook) {
return Pic.reg_file[c][d].hook( &Pic.reg_file[c][d], False, 0 );
} else {
return Pic.reg_file[c][d].value & 0xff;
}
} else {
return 0; // indirect access of F0 returns 0
}
return (!d) ? 0 : Pic.reg_file[c][d].value & 0xff;
}
static char
get_w( void )
{
return Pic.reg_file[0][0].value;
}
/*
* Construct a 13-bit value from the low 11 bits of the instr and bits <4:3>
* of PCLATH. This value is then truncated to fit within the address space.
*/
static int
get_addr( u_int instr )
{
return (((((int)Pic.reg_file[0][10].value << 8) & 0x1800)
| instr & 0x7ff) - 1) & 0x3ff;
}
static void
set_f( int p, int r, char val, Boolean set_z )
{
if (r) {
if (Pic.reg_file[p][r].hook) {
val = Pic.reg_file[p][r].hook( &Pic.reg_file[p][r], True, val );
} else {
val =
Pic.reg_file[p][r].value = val & Pic.reg_file[p][r].implemented;
Pic.reg_file[p][r].modified = 1;
}
}
// fixme: execute any hooks on this register!
if (set_z)
set_status( (!val) ? Z : 0, (val) ? Z : 0 );
}
static void
set_w( char val, Boolean set_z )
{
Pic.reg_file[0][0].value = val;
Pic.reg_file[0][0].modified = 1;
if (set_z)
set_status( (!val) ? Z : 0, (val) ? Z : 0 );
}
static void
set_status( int set, int clear )
{
Pic.reg_file[0][3].value |= set;
Pic.reg_file[0][3].value &= ~clear;
Pic.reg_file[0][3].modified = 1;
}
static char *
disasm_pic16c84( u_int instr )
{
static char dscr[32];
int i;
pic16c84_instr *tmpl;
if ((i = match_instr( instr )) == -1) return "??????";
tmpl = PIC16C84_INSTR + i;
if (tmpl->f) {
char reg[16];
int a;
int b;
int p;
int r;
a = (Pic.reg_file[0][3].value >> 5) & 1;
b = instr & 0x7f;
p = Pic.reg_file[a][b].redirect_page;
r = Pic.reg_file[a][b].redirect_reg;
if (!p && !r) {
strcpy( reg, "@FSR" );
} else
if (Pic.reg_file[p][r].name) {
const char *cp = Pic.reg_file[p][r].name;
while (isspace(*cp)) ++cp;
strcpy( reg, cp );
} else
sprintf( reg, "F%02X", r );
if (tmpl->d)
sprintf( dscr, "%s %s,%c",
tmpl->name, reg, (instr & 0x80) ? 'F' : 'W' );
else
if (tmpl->b)
sprintf( dscr, "%s %s,%d",
tmpl->name, reg, (instr & 0x380) >> 7 );
else
sprintf( dscr, "%s %s", tmpl->name, reg );
} else
if (tmpl->literal)
sprintf( dscr,
(tmpl->literal & 0xff00) ? "%s 0x%04x" : "%s 0x%02x",
tmpl->name, instr & tmpl->literal );
else
strcpy( dscr, tmpl->name );
if (instr & tmpl->dont_care) {
char *cp;
for (cp = dscr; *cp; cp++)
if (isalpha( *cp )) *cp = tolower( *cp );
}
return dscr;
}
/*
* These routines simulate each individual instruction
*/
static void
exADDWF( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
int w, f, sum;
int c, dc;
w = get_w( ) & 0xff;
f = get_f( instr, &p, &r ) & 0xff;
sum = w + f;
if (instr & 0x80)
set_f( p, r, sum, True );
else
set_w( sum, True );
c = (sum & 0x100) ? C : 0;
dc = (((w & 0x0f) + (f & 0x0f)) & 0x10) ? DC : 0;
set_status( c | dc, (c^C) | (dc^DC) );
}
static void
exANDWF( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
int w, f, nitpic_and;
int d, dc;
w = get_w( );
f = get_f( instr, &p, &r );
nitpic_and = w & f;
if (instr & 0x80)
set_f( p, r, nitpic_and, True );
else
set_w( nitpic_and, True );
}
static void
exCLRF( void )
{
int p, r;
get_f( Pic.uinfo.picmemmap[ Pic.pc ], &p, &r );
set_f( p, r, 0, True );
}
static void
exCLRW( void )
{
set_w( 0, True );
}
static void
exCOMF( void )
{
u_int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
char v;
v = get_f( instr, &p, &r );
if (instr & 0x80)
set_f( p, r, ~v, True );
else
set_w( ~v, True );
}
static void
exDECF( void )
{
u_int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
char v;
v = get_f( instr, &p, &r );
v -= 1;
if (instr & 0x80)
set_f( p, r, v, True );
else
set_w( v, True );
}
static void
exDECFSZ( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
char v;
v = get_f( instr, &p, &r );
v -= 1;
if (instr & 0x80)
set_f( p, r, v, False );
else
set_w( v, False );
if (v == 0) use_a_cycle( True );
}
static void
exINCF( void )
{
u_int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
char v;
v = get_f( instr, &p, &r );
v += 1;
if (instr & 0x80)
set_f( p, r, v, True );
else
set_w( v, True );
}
static void
exINCFSZ( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
char v;
v = get_f( instr, &p, &r );
v += 1;
if (instr & 0x80)
set_f( p, r, v, False );
else
set_w( v, False );
if (v == 0) use_a_cycle( True );
}
static void
exIORWF( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
int w, f, nitpic_or;
int c, dc;
w = get_w( );
f = get_f( instr, &p, &r );
nitpic_or = w | f;
if (instr & 0x80)
set_f( p, r, nitpic_or, True );
else
set_w( nitpic_or, True );
}
static void
exMOVF( void )
{
u_int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
char v;
v = get_f( instr, &p, &r );
if (instr & 0x80)
set_f( p, r, v, True );
else
set_w( v, True );
}
static void
exMOVWF( void )
{
u_int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
get_f( instr, &p, &r );
set_f( p, r, get_w(), False );
}
static void
exNOP( void )
{
}
static void
exRLF( void )
{
u_int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
char v;
int c;
v = get_f( instr, &p, &r );
c = (v & 0x80) ? C : 0;
v = (v << 1) | ((Pic.reg_file[0][3].value & C) ? 1 : 0);
if (instr & 0x80)
set_f( p, r, v, False );
else
set_w( v, False );
set_status( c, c ^ C );
}
static void
exRRF( void )
{
u_int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
char v;
int c;
v = get_f( instr, &p, &r );
c = (v & 1) ? C : 0;
v = (v >> 1) | ((Pic.reg_file[0][3].value & C) ? 0x80 : 0);
if (instr & 0x80)
set_f( p, r, v, False );
else
set_w( v, False );
set_status( c, c ^ C );
}
static void
exSUBWF( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
int w, f, dif;
int c, dc;
w = get_w( ) & 0xff;
f = get_f( instr, &p, &r ) & 0xff;
dif = f - w;
if (instr & 0x80)
set_f( p, r, dif, True );
else
set_w( dif, True );
c = (dif & 0x100) ? C : 0;
dc = (((f & 0x0f) - (w & 0x0f)) & 0x10) ? DC : 0;
set_status( c | dc, (c^C) | (dc^DC) );
}
static void
exSWAPF( void )
{
u_int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
char v;
v = get_f( instr, &p, &r );
v = ((v >> 4) & 0x0f) | (v << 4);
if (instr & 0x80)
set_f( p, r, v, False );
else
set_w( v, False );
}
static void
exXORWF( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int p, r;
int w, f, nitpic_xor;
w = get_w( );
f = get_f( instr, &p, &r );
nitpic_xor = w ^ f;
if (instr & 0x80)
set_f( p, r, nitpic_xor, True );
else
set_w( nitpic_xor, True );
}
static void
exBCF( void )
{
u_int instr = Pic.uinfo.picmemmap[Pic.pc];
int bit = (instr >> 7) & 7;
int p, r;
char v;
v = get_f( instr, &p, &r ) & ~(1 << bit);
set_f( p, r, v, False );
}
static void
exBSF( void )
{
u_int instr = Pic.uinfo.picmemmap[Pic.pc];
int bit = (instr >> 7) & 7;
int p, r;
char v;
v = get_f( instr, &p, &r ) | (1 << bit);
set_f( p, r, v, False );
}
static void
exBTFSC( void )
{
u_int instr = Pic.uinfo.picmemmap[Pic.pc];
int bit = (instr >> 7) & 7;
if (!(get_f( instr ) & (1 << bit))) use_a_cycle( True );
}
static void
exBTFSS( void )
{
u_int instr = Pic.uinfo.picmemmap[Pic.pc];
int bit = (instr >> 7) & 7;
if (get_f( instr ) & (1 << bit)) use_a_cycle( True );
}
static void
exADDLW( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int w, l, sum;
int c, dc;
w = get_w( ) & 0xff;
l = instr & 0xff;
sum = w + l;
set_w( sum, True );
c = (sum & 0x100) ? C : 0;
dc = (((w & 0x0f) + (l & 0x0f)) & 0x10) ? DC : 0;
set_status( c | dc, (c^C) | (dc^DC) );
}
static void
exANDLW( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int w, l, nitpic_and;
w = get_w( );
l = instr & 0xff;
nitpic_and = w & l;
set_w( nitpic_and, True );
}
static void
exCALL( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
Pic.stack[Pic.sp] = Pic.pc;
Pic.sp += 1;
if (Pic.sp >= Pic.nstack) Pic.sp = 0;
use_a_cycle( True );
Pic.pc = get_addr( instr );
}
static void
exCLRWDT( void )
{
Pic.wdt_left = (u_long)(Pic.uinfo.clock * Pic.wdt_max / 4.0);
if (Pic.reg_file[1][1].value & 8) {
Pic.prescaler = 1 << (Pic.reg_file[1][1].value & 7);
}
set_status( TO | PD, 0 );
info_refresh( NULL, NULL, NULL, NULL );
}
static void
exGOTO( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
use_a_cycle( True );
Pic.pc = get_addr( instr );
}
static void
exIORLW( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int w, l, nitpic_or;
w = get_w( );
l = instr & 0xff;
nitpic_or = w & l;
set_w( nitpic_or, True );
}
static void
exMOVLW( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
set_w( instr & 0xff, False );
}
static void
exRETFIE( void )
{
int gie = get_f( 0x0b );
set_f( 0, 0x0b, gie | 0x80, False );
use_a_cycle( True );
Pic.sp -= 1;
if (Pic.sp < 0) Pic.sp = Pic.nstack - 1;
Pic.pc = Pic.stack[Pic.sp];
}
static void
exRETLW( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
set_w( instr & 0xff, False );
use_a_cycle( True );
Pic.sp -= 1;
if (Pic.sp < 0) Pic.sp = Pic.nstack - 1;
Pic.pc = Pic.stack[Pic.sp];
}
static void
exRETURN( void )
{
use_a_cycle( True );
Pic.sp -= 1;
if (Pic.sp < 0) Pic.sp = Pic.nstack - 1;
Pic.pc = Pic.stack[Pic.sp];
}
static void
exSLEEP( void )
{
exCLRWDT( );
set_status( TO, PD );
Pic.sleeping = 1;
Pic.pc = Pic.pc - 1;
}
static void
exSUBLW( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int w, l, dif;
int c, dc;
w = get_w( ) & 0xff;
l = instr & 0xff;
dif = l - w;
set_w( dif, True );
c = (dif & 0x100) ? C : 0;
dc = (((l & 0x0f) - (w & 0x0f)) & 0x10) ? DC : 0;
set_status( c | dc, (c^C) | (dc^DC) );
}
static void
exXORLW( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
int w, l, nitpic_xor;
w = get_w( );
l = instr & 0xff;
nitpic_xor = w ^ l;
set_w( nitpic_xor, True );
}
static void
exOPTION( void )
{
set_f( 1, 1, get_w(), False );
}
static void
exTRIS( void )
{
int instr = Pic.uinfo.picmemmap[ Pic.pc ];
switch (instr & 7) {
case 5:
case 6:
set_f( 1, instr & 7, get_w(), False );
break;
default:
// fixme! what does this do?
break;
}
}
pic16c84_instr
PIC16C84_INSTR[] = {
{ 0x3f00, 0x0000, 0x0700, 0x0000, 1, 1, 0, "ADDWF ", exADDWF },
{ 0x3f00, 0x0000, 0x0500, 0x0000, 1, 1, 0, "ANDWF ", exANDWF },
{ 0x3f80, 0x0000, 0x0180, 0x0000, 0, 1, 0, "CLRF ", exCLRF },
{ 0x3f80, 0x007f, 0x0100, 0x0000, 0, 0, 0, "CLRW ", exCLRW },
{ 0x3f00, 0x0000, 0x0900, 0x0000, 1, 1, 0, "COMF ", exCOMF },
{ 0x3f00, 0x0000, 0x0300, 0x0000, 1, 1, 0, "DECF ", exDECF },
{ 0x3f00, 0x0000, 0x0b00, 0x0000, 1, 1, 0, "DECFSZ", exDECFSZ },
{ 0x3f00, 0x0000, 0x0a00, 0x0000, 1, 1, 0, "INCF ", exINCF },
{ 0x3f00, 0x0000, 0x0f00, 0x0000, 1, 1, 0, "INCFSZ", exINCFSZ },
{ 0x3f00, 0x0000, 0x0400, 0x0000, 1, 1, 0, "IORWF ", exIORWF },
{ 0x3f00, 0x0000, 0x0800, 0x0000, 1, 1, 0, "MOVF ", exMOVF },
{ 0x3f80, 0x0000, 0x0080, 0x0000, 0, 1, 0, "MOVWF ", exMOVWF },
{ 0x3f9f, 0x0060, 0x0000, 0x0000, 0, 0, 0, "NOP ", exNOP },
{ 0x3f00, 0x0000, 0x0d00, 0x0000, 1, 1, 0, "RLF ", exRLF },
{ 0x3f00, 0x0000, 0x0c00, 0x0000, 1, 1, 0, "RRF ", exRRF },
{ 0x3f00, 0x0000, 0x0200, 0x0000, 1, 1, 0, "SUBWF ", exSUBWF },
{ 0x3f00, 0x0000, 0x0e00, 0x0000, 1, 1, 0, "SWAPF ", exSWAPF },
{ 0x3f00, 0x0000, 0x0600, 0x0000, 1, 1, 0, "XORWF ", exXORWF },
{ 0x3c00, 0x0000, 0x1000, 0x0000, 0, 1, 1, "BCF ", exBCF },
{ 0x3c00, 0x0000, 0x1400, 0x0000, 0, 1, 1, "BSF ", exBSF },
{ 0x3c00, 0x0000, 0x1800, 0x0000, 0, 1, 1, "BTFSC ", exBTFSC },
{ 0x3c00, 0x0000, 0x1c00, 0x0000, 0, 1, 1, "BTFSS ", exBTFSS },
{ 0x3e00, 0x0100, 0x3e00, 0x00ff, 0, 0, 0, "ADDLW ", exADDLW },
{ 0x3F00, 0x0000, 0x3900, 0x00ff, 0, 0, 0, "ANDLW ", exANDLW },
{ 0x3800, 0x0000, 0x2000, 0x07ff, 0, 0, 0, "CALL ", exCALL },
{ 0x3fff, 0x0000, 0x0064, 0x0000, 0, 0, 0, "CLRWDT", exCLRWDT },
{ 0x3800, 0x0000, 0x2800, 0x07ff, 0, 0, 0, "GOTO ", exGOTO },
{ 0x3f00, 0x0000, 0x3800, 0x00ff, 0, 0, 0, "IORLW ", exIORLW },
{ 0x3c00, 0x0300, 0x3000, 0x00ff, 0, 0, 0, "MOVLW ", exMOVLW },
{ 0x3fff, 0x0000, 0x0009, 0x0000, 0, 0, 0, "RETFIE", exRETFIE },
{ 0x3c00, 0x0300, 0x3400, 0x00ff, 0, 0, 0, "RETLW ", exRETLW },
{ 0x3fff, 0x0000, 0x0008, 0x0000, 0, 0, 0, "RETURN", exRETURN },
{ 0x3fff, 0x0000, 0x0063, 0x0000, 0, 0, 0, "SLEEP ", exSLEEP },
{ 0x3e00, 0x0100, 0x3c00, 0x00ff, 0, 0, 0, "SUBLW ", exSUBLW },
{ 0x3f00, 0x0000, 0x3a00, 0x00ff, 0, 0, 0, "XORLW ", exXORLW },
{ 0x3fff, 0x0000, 0x0062, 0x0000, 0, 0, 0, "OPTION", exOPTION },
{ 0x3ff8, 0x0000, 0x0060, 0x0007, 0, 0, 0, "TRIS ", exTRIS }
} ;
static int
match_instr( u_int instr )
{
const int ninstr = sizeof(PIC16C84_INSTR)/sizeof(PIC16C84_INSTR[0]);
int i;
pic16c84_instr *tmpl;
for (i = 0; i < ninstr; i++) {
tmpl = PIC16C84_INSTR + i;
if ((instr & ~tmpl->dont_care & tmpl->mask) == tmpl->discriminator)
break;
}
return ((i >= ninstr) ? -1 : i);
}
|