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 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
|
/* Supports instruction memory.
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
James Bowman, Scott Dattalo
Copyright (C) 2013 Borut Razem
Copyright (C) 2014-2016 Molnar Karoly
This file is part of gputils.
gputils 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, or (at your option)
any later version.
gputils 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 gputils; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <limits.h>
#include "stdhdr.h"
#include "libgputils.h"
typedef union
{
uint8_t b[2];
uint16_t w;
} bword_t;
/**************************************************************************************************
gpmemory.c
This file provides the functions used to manipulate the PIC memory.
The memory is stored in 'memory blocks' which are implemented
with the 'MemBlock_t' structure:
typedef struct MemArg {
const char *arg;
int val; The value of the first argument.
int offs; If the argument is area then this the offset of the address.
} MemArg_t;
typedef struct MemArgList {
MemArg_t first;
MemArg_t second;
} MemArgList_t;
typedef union __attribute__ ((packed)) MemData {
unsigned int all;
struct __attribute__ ((packed)) {
unsigned byte : 8; [0-7] The data byte.
unsigned is_addr_branch_src: 1; [ 8] W_ADDR_T_BRANCH_SRC
unsigned is_addr_label : 1; [ 9] W_ADDR_T_LABEL
unsigned is_addr_func : 1; [10] W_ADDR_T_FUNC
unsigned is_arg_first : 1; [11] W_ARG_T_FIRST
unsigned is_arg_second : 1; [12] W_ARG_T_SECOND
unsigned is_second_word : 1; [13] W_SECOND_WORD
unsigned is_const_data : 1; [14] W_CONST_DATA
unsigned is_byte_listed : 1; [15] BYTE_LISTED_MASK
unsigned is_byte_used : 1; [16] BYTE_USED_MASK
};
struct __attribute__ ((packed)) {
unsigned : 8;
unsigned addr_t: 3;
unsigned arg_t : 2;
unsigned : 2;
unsigned attr_t: 2;
};
struct __attribute__ ((packed)) {
unsigned : 8;
unsigned all_attr: 24;
};
} MemData_t;
typedef struct MemByte {
MemData_t data; The data byte and the attributes of.
char *section_name; During assembly or linking shows the name of section.
char *symbol_name; During assembly or linking shows the name of symbol.
After disassembly shows the name of function or label.
unsigned int dest_byte_addr; After disassembly shows the target byte-address (not org) of a branch.
MemArgList_t args;
} MemByte_t;
typedef struct MemBlock {
unsigned int base;
MemByte_t *memory;
struct MemBlock_t *next;
} MemBlock_t;
Each MemBlock_t can hold up to 'I_MEM_MAX' (64kB currently) bytes. The 'base'
is the base address of the memory block. If the instruction memory spans
more than 64kB, then additional memory blocks can be allocated and linked
together in a singly linked list ('next'). The last memory block in a
linked list of blocks has its next ptr set to NULL. 64kB is left over
from when it was number of two byte instructions and it corresponded
to 64k bytes which is the upper limit on inhx8m files.
**************************************************************************************************/
/**************************************************************************************************
* _memory_new
*
* Create memory for a new memory block.
*
* Inputs:
* m - start of the instruction memory
* mpb - pointer to the memory block structure (MemBlock_t)
* base_address - where this new block of memory is based
*
**************************************************************************************************/
static MemBlock_t *
_memory_new(MemBlock_t *M, MemBlock_t *Mbp, unsigned int Base_address)
{
unsigned int block = IMemBaseFromAddr(Base_address);
Mbp->base = block;
Mbp->memory = (MemByte_t *)GP_Calloc(I_MEM_MAX, sizeof(MemByte_t));
do {
if ((M->next == NULL) || (M->next->base > block)) {
/* Insert after this block. */
Mbp->next = M->next;
M->next = Mbp;
return Mbp;
}
M = M->next;
} while (M != NULL);
assert(0);
return NULL;
}
/*------------------------------------------------------------------------------------------------*/
static void
_store_section_name(MemByte_t *Mb, const char *Name)
{
if ((Name != NULL) && (*Name != '\0')) {
Mb->section_name = GP_Strdup(Name);
}
}
/*------------------------------------------------------------------------------------------------*/
static void
_store_symbol_name(MemByte_t *Mb, const char *Name)
{
if ((Name != NULL) && (*Name != '\0')) {
Mb->symbol_name = GP_Strdup(Name);
}
}
/*------------------------------------------------------------------------------------------------*/
MemBlock_t *
gp_mem_i_create(void)
{
return (MemBlock_t *)GP_Calloc(1, sizeof(MemBlock_t));
}
/*------------------------------------------------------------------------------------------------*/
void
gp_mem_i_free(MemBlock_t *M)
{
MemBlock_t *next;
MemByte_t *b;
unsigned int i;
if (M == NULL) {
return;
}
do {
if (M->memory != NULL) {
b = M->memory;
for (i = I_MEM_MAX; i; ++b, --i) {
if (b->section_name != NULL) {
free(b->section_name);
}
if (b->symbol_name != NULL) {
free(b->symbol_name);
}
}
free(M->memory);
}
next = M->next;
free(M);
M = next;
} while (M != NULL);
}
/**************************************************************************************************
* gp_mem_b_is_used
*
* Check if byte at address is used. This function will traverse through
* the linked list of memory blocks searching for the address from the
* word will be fetched.
*
* Inputs:
* M - start of the instruction memory
* Byte_address -
* Returns
* true if byte at byte_address is used, false if not
*
**************************************************************************************************/
gp_boolean
gp_mem_b_is_used(MemBlock_t *M, unsigned int Byte_address)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
do {
if (M->base == block) {
return ((M->memory != NULL) && M->memory[offset].data.is_byte_used);
}
M = M->next;
} while (M != NULL);
return false;
}
/*------------------------------------------------------------------------------------------------*/
gp_boolean
gp_mem_b_offset_is_used(MemBlock_t *M, unsigned int Byte_offset)
{
if ((M == NULL) || (M->memory == NULL)) {
return false;
}
return M->memory[Byte_offset].data.is_byte_used;
}
/**************************************************************************************************
* gp_mem_b_get
*
* Fetch a byte from the pic memory. This function will traverse through
* the linked list of memory blocks searching for the address from the
* word will be fetched. If the address is not found, then `0' will be
* returned.
*
* Inputs:
* M - start of the instruction memory
* Byte_address -
* Returns
* If is used the address the byte at address and true.
* Otherwise 0 and false.
*
**************************************************************************************************/
gp_boolean
gp_mem_b_get(const MemBlock_t *M, unsigned int Byte_address, uint8_t *Byte,
const char **Section_name, const char **Symbol_name)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemByte_t *b;
while (M != NULL) {
if (M->base == block) {
if (M->memory != NULL) {
b = &M->memory[offset];
*Byte = b->data.byte;
if (Section_name != NULL) {
*Section_name = b->section_name;
}
if (Symbol_name != NULL) {
*Symbol_name = b->symbol_name;
}
return b->data.is_byte_used;
}
else {
*Byte = 0;
if (Section_name != NULL) {
*Section_name = NULL;
}
if (Symbol_name != NULL) {
*Symbol_name = NULL;
}
return false;
}
}
M = M->next;
}
if (Section_name != NULL) {
*Section_name = NULL;
}
if (Symbol_name != NULL) {
*Symbol_name = NULL;
}
*Byte = 0;
return false;
}
/**************************************************************************************************
* gp_mem_b_put
*
* This function will write one byte to a pic memory address. If the
* destination memory block is non-existant, a new one will be created.
*
* inputs:
* M - start of the instruction memory
* Byte_address - destination address of the write
* Value - the value to be written at that address
* Section_name - section_name of the memory block
* Symbol_name - symbol_name in the memory block
* returns:
* none
*
**************************************************************************************************/
void
gp_mem_b_put(MemBlock_t *I_memory, unsigned int Byte_address, uint8_t Value,
const char *Section_name, const char *Symbol_name)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemBlock_t *m = I_memory;
MemByte_t *b;
while (m != NULL) {
if (m->base == block) {
if (m->memory == NULL) {
m->memory = (MemByte_t *)GP_Calloc(I_MEM_MAX, sizeof(MemByte_t));
}
b = &m->memory[offset];
if (b->section_name == NULL) {
_store_section_name(b, Section_name);
}
if (b->symbol_name == NULL) {
_store_symbol_name(b, Symbol_name);
}
b->data.byte = Value;
b->data.is_byte_used = true;
return;
}
m = m->next;
}
/* Couldn't find an address to write this value. This must be
the first time we've tried to write to high memory some place. */
m = _memory_new(I_memory, (MemBlock_t *)GP_Malloc(sizeof(MemBlock_t)), Byte_address);
b = &m->memory[offset];
b->data.byte = Value;
b->data.is_byte_used = true;
_store_section_name(b, Section_name);
_store_symbol_name(b, Symbol_name);
}
/**************************************************************************************************
* gp_mem_b_clear
*
* This function will clear one byte of a pic memory address.
*
* inputs:
* M - start of the instruction memory
* Byte_address - destination address of the clear
* returns:
* none
*
**************************************************************************************************/
void
gp_mem_b_clear(MemBlock_t *M, unsigned int Byte_address)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemByte_t *b;
while (M != NULL) {
if (M->base == block) {
if (M->memory != NULL) {
b = &M->memory[offset];
b->data.all = 0;
if (b->section_name != NULL) {
free(b->section_name);
b->section_name = NULL;
}
if (b->symbol_name != NULL) {
free(b->symbol_name);
b->symbol_name = NULL;
}
}
return;
}
M = M->next;
}
}
/*------------------------------------------------------------------------------------------------*/
void
gp_mem_b_move(MemBlock_t *M, unsigned int From_byte_address, unsigned int To_byte_address,
unsigned int Byte_size)
{
unsigned int from_block = IMemBaseFromAddr(From_byte_address);
unsigned int from_offset = IMemOffsFromAddr(From_byte_address);
unsigned int to_block = IMemBaseFromAddr(To_byte_address);
unsigned int to_offset = IMemOffsFromAddr(To_byte_address);
size_t size;
if ((From_byte_address == To_byte_address) || (Byte_size == 0)) {
return;
}
assert(from_block == to_block);
assert((from_offset + Byte_size) <= I_MEM_MAX);
assert((to_offset + Byte_size) <= I_MEM_MAX);
while (M != NULL) {
if (M->base == from_block) {
size = Byte_size * sizeof(MemByte_t);
memmove(&M->memory[to_offset], &M->memory[from_offset], size);
/* Clear the unused area. */
if (from_offset > to_offset) {
/*
* from_offset
* |
* v
* +-------+
* | |
* +-------+
*
* move direction
* <<-------
*
* to_offset unused area
* | |
* v v
* +-------+----+
* | |XXXX|
* +-------+----+
*/
size = (from_offset - to_offset) * sizeof(MemByte_t);
memset(&M->memory[to_offset + Byte_size], 0, size);
}
else {
/*
* from_offset
* |
* v
* +-------+
* | |
* +-------+
*
* move direction
* ------->>
*
* unused area
* |
* | to_offset
* | |
* v v
* +----+-------+
* |XXXX| |
* +----+-------+
*/
size = (to_offset - from_offset) * sizeof(MemByte_t);
memset(&M->memory[from_offset], 0, size);
}
return;
}
M = M->next;
}
}
/*------------------------------------------------------------------------------------------------*/
void
gp_mem_b_delete(MemBlock_t *M, unsigned int Byte_address)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemByte_t *b;
size_t size;
while (M != NULL) {
if (M->base == block) {
if (M->memory != NULL) {
b = &M->memory[offset];
if (b->section_name != NULL) {
free(b->section_name);
}
if (b->symbol_name != NULL) {
free(b->symbol_name);
}
size = (I_MEM_MAX - offset) * sizeof(MemByte_t);
if (size != 0) {
memmove(b, &M->memory[offset + 1], size);
}
memset(&M->memory[I_MEM_MAX], 0, sizeof(MemByte_t));
}
return;
}
M = M->next;
}
}
/*------------------------------------------------------------------------------------------------*/
void
gp_mem_b_delete_area(MemBlock_t *M, unsigned int Byte_address, unsigned int Byte_number)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemByte_t *b;
unsigned int remnant_byte_num;
unsigned int i;
if (Byte_number == 0) {
return;
}
while (M != NULL) {
if (M->base == block) {
if (M->memory != NULL) {
remnant_byte_num = I_MEM_MAX - offset;
assert(Byte_number <= remnant_byte_num);
remnant_byte_num -= Byte_number;
for (b = &M->memory[offset], i = Byte_number; i > 0; ++b, --i) {
if (b->section_name != NULL) {
free(b->section_name);
}
if (b->symbol_name != NULL) {
free(b->symbol_name);
}
}
/*
* Before the deleting.
*
* block (from byte_address)
* | offset (from byte_address)
* | | byte_number
* v v | remnant_byte_num
* +----+---|----+---------|---------+
* | |<--^--->|<--------^-------->|
* +----+--------+-------------------+
* <---------------v--------------->
* |
* I_MEM_MAX
*/
if (remnant_byte_num != 0) {
/* Delete the designated area. */
memmove(&M->memory[offset], &M->memory[offset + Byte_number], remnant_byte_num * sizeof(MemByte_t));
}
/*
* After the deleting.
*
* block (from byte_address)
* | offset (from byte_address)
* | | remnant_byte_num
* v v | empty_area = byte_number
* +----+---------|---------+---|----+
* | |<--------^-------->|<--^--->|
* +----+-------------------+--------+
* <---------------v--------------->
* |
* I_MEM_MAX
*/
/* Clear the empty area. */
memset(&M->memory[offset + remnant_byte_num], 0, Byte_number * sizeof(MemByte_t));
}
return;
}
M = M->next;
}
}
/*------------------------------------------------------------------------------------------------*/
unsigned int
b_range_memory_used(const MemBlock_t *M, unsigned int From_byte_address, unsigned int To_byte_address)
{
unsigned int i;
unsigned int j;
unsigned int starting_block;
unsigned int block;
unsigned int n_bytes;
j = 0;
block = 0;
starting_block = IMemBaseFromAddr(From_byte_address);
/* find the starting block */
while ((M != NULL) && (block < starting_block)) {
j += I_MEM_MAX;
M = M->next;
++block;
}
n_bytes = 0;
/* count used bytes */
while ((M != NULL) && (j < To_byte_address)) {
for (i = 0; (i < I_MEM_MAX) && (j < To_byte_address); ++i) {
if ((M->memory != NULL) && M->memory[i].data.is_byte_used) {
++n_bytes;
}
++j;
}
M = M->next;
}
return n_bytes;
}
/*------------------------------------------------------------------------------------------------*/
unsigned int
gp_mem_b_used(const MemBlock_t *M)
{
return b_range_memory_used(M, 0, UINT_MAX);
}
/**************************************************************************************************
*
*
* These functions are used to read and write instruction memory.
*
*
**************************************************************************************************/
unsigned int
gp_mem_i_offset_is_used_le(MemBlock_t *M, unsigned int Byte_offset)
{
unsigned int ret;
ret = (gp_mem_b_offset_is_used(M, Byte_offset)) ? W_USED_L : 0;
ret |= (gp_mem_b_offset_is_used(M, Byte_offset + 1)) ? W_USED_H : 0;
return ret;
}
/*------------------------------------------------------------------------------------------------*/
unsigned int
gp_mem_i_get_le(const MemBlock_t *M, unsigned int Byte_address, uint16_t *Word,
const char **Section_name, const char **Symbol_name)
{
unsigned int ret;
bword_t bw;
ret = (gp_mem_b_get(M, Byte_address, &bw.b[0], Section_name, Symbol_name)) ? W_USED_L : 0;
ret |= (gp_mem_b_get(M, Byte_address + 1, &bw.b[1], NULL, NULL)) ? W_USED_H : 0;
*Word = bw.w;
return ret;
}
/*------------------------------------------------------------------------------------------------*/
void
gp_mem_i_put_le(MemBlock_t *M, unsigned int Byte_address, uint16_t Word,
const char *Section_name, const char *Symbol_name)
{
gp_mem_b_put(M, Byte_address, Word & 0xff, Section_name, Symbol_name);
gp_mem_b_put(M, Byte_address + 1, Word >> 8, Section_name, Symbol_name);
}
/*------------------------------------------------------------------------------------------------*/
unsigned int
gp_mem_i_offset_is_used_be(MemBlock_t *M, unsigned int Byte_offset)
{
unsigned int ret;
ret = (gp_mem_b_offset_is_used(M, Byte_offset)) ? W_USED_H : 0;
ret |= (gp_mem_b_offset_is_used(M, Byte_offset + 1)) ? W_USED_L : 0;
return ret;
}
/*------------------------------------------------------------------------------------------------*/
unsigned int
gp_mem_i_get_be(const MemBlock_t *M, unsigned int Byte_address, uint16_t *Word,
const char **Section_name, const char **Symbol_name)
{
unsigned int ret;
bword_t bw;
ret = (gp_mem_b_get(M, Byte_address, &bw.b[1], Section_name, Symbol_name)) ? W_USED_H : 0;
ret |= (gp_mem_b_get(M, Byte_address + 1, &bw.b[0], NULL, NULL)) ? W_USED_L : 0;
*Word = bw.w;
return ret;
}
/*------------------------------------------------------------------------------------------------*/
void
gp_mem_i_put_be(MemBlock_t *M, unsigned int Byte_address, uint16_t Word,
const char *Section_name, const char *Symbol_name)
{
gp_mem_b_put(M, Byte_address, Word >> 8, Section_name, Symbol_name);
gp_mem_b_put(M, Byte_address + 1, Word & 0xff, Section_name, Symbol_name);
}
/*------------------------------------------------------------------------------------------------*/
void
gp_mem_i_delete(MemBlock_t *m, unsigned int byte_address)
{
gp_mem_b_delete(m, byte_address);
gp_mem_b_delete(m, byte_address);
}
/*------------------------------------------------------------------------------------------------*/
void
gp_mem_i_print(const MemBlock_t *M, pic_processor_t Processor)
{
proc_class_t class;
unsigned int byte_addr;
unsigned int i;
unsigned int j;
unsigned int org;
gp_boolean row_used;
unsigned int w_used;
bword_t data;
uint8_t c;
#define WORDS_IN_ROW 8
class = Processor->class;
while (M != NULL) {
if (M->memory != NULL) {
byte_addr = IMemAddrFromBase(M->base);
for (i = 0; i < I_MEM_MAX; i += 2 * WORDS_IN_ROW) {
row_used = false;
for (j = 0; j < (2 * WORDS_IN_ROW); j++) {
if (M->memory[i + j].data.all != 0) {
row_used = true;
break;
}
}
if (row_used) {
org = gp_processor_insn_from_byte_p(Processor, byte_addr + i);
printf("%08X ", org);
if ((gp_processor_is_eeprom_org(Processor, org) >= 0) ||
((class == PROC_CLASS_PIC16E) &&
((gp_processor_is_idlocs_org(Processor, org) >= 0) ||
(gp_processor_is_config_org(Processor, org) >= 0)))) {
/* The row should be shown byte by byte. */
for (j = 0; j < (2 * WORDS_IN_ROW); j++) {
if (gp_mem_b_get(M, byte_addr + i + j, &data.b[0], NULL, NULL)) {
printf("%02X ", data.b[0]);
}
else {
printf("-- ");
}
}
for (j = 0; j < (2 * WORDS_IN_ROW); j++) {
c = M->memory[i + j].data.byte;
putchar(isprint(c) ? c : '.');
}
}
else {
/* The row should be shown word by word. */
for (j = 0; j < WORDS_IN_ROW; j++) {
w_used = class->i_memory_get(M, byte_addr + i + (j * 2), &data.w, NULL, NULL);
switch (w_used & W_USED_ALL) {
case W_USED_ALL:
printf("%04X ", data.w);
break;
case W_USED_H:
printf("%02X-- ", data.b[1]);
break;
case W_USED_L:
printf("--%02X ", data.b[0]);
break;
default:
printf("---- ");
}
}
for (j = 0; j < (2 * WORDS_IN_ROW); j++) {
c = M->memory[i + j].data.byte;
putchar(isprint(c) ? c : '.');
}
}
putchar('\n');
}
}
}
M = M->next;
}
}
/**************************************************************************************************
*
*
* These functions are used to mark memory as listed.
*
*
**************************************************************************************************/
void
gp_mem_b_set_listed(MemBlock_t *M, unsigned int Byte_address, unsigned int N_bytes)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
while (N_bytes--) {
while (M != NULL) {
if (M->base == block) {
if (M->memory == NULL) {
M->memory = (MemByte_t *)GP_Calloc(I_MEM_MAX, sizeof(MemByte_t));
}
M->memory[offset].data.is_byte_listed = true;
break;
}
M = M->next;
}
++Byte_address;
}
}
/*------------------------------------------------------------------------------------------------*/
unsigned int
gp_mem_b_get_unlisted_size(const MemBlock_t *M, unsigned int Byte_address)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int n_bytes = 0;
if ((M != NULL) && (M->memory != NULL)) {
while (n_bytes < 4) {
/* find memory block belonging to the byte_address */
while (block != M->base) {
M = M->next;
if (M == NULL) {
return n_bytes;
}
}
if ((M->memory != NULL) && (!M->memory[IMemOffsFromAddr(Byte_address)].data.is_byte_listed)) {
/* byte at byte_address not listed */
++Byte_address;
++n_bytes;
}
else {
/* byte at byte_address already listed */
break;
}
}
}
return n_bytes;
}
/*------------------------------------------------------------------------------------------------*/
gp_boolean
gp_mem_b_set_addr_type(MemBlock_t *M, unsigned int Byte_address, unsigned int Type,
unsigned int Dest_byte_addr)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemByte_t *b;
while (M != NULL) {
if ((M->base == block) && (M->memory != NULL)) {
b = &M->memory[offset];
if (b->data.is_byte_used) {
b->data.all |= Type & W_ADDR_T_MASK;
if (Type & W_ADDR_T_BRANCH_SRC) {
b->dest_byte_addr = Dest_byte_addr;
}
return true;
}
break;
}
M = M->next;
}
return false;
}
/*------------------------------------------------------------------------------------------------*/
unsigned int
gp_mem_b_get_addr_type(const MemBlock_t *M, unsigned int Byte_address, const char **Label_name,
unsigned int *Dest_byte_addr)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemByte_t *b;
while (M != NULL) {
if ((M->base == block) && (M->memory != NULL)) {
b = &M->memory[offset];
if (Label_name != NULL) {
*Label_name = (b->data.all & (W_ADDR_T_FUNC | W_ADDR_T_LABEL)) ? b->symbol_name : NULL;
}
if (Dest_byte_addr != NULL) {
*Dest_byte_addr = (b->data.all & W_ADDR_T_BRANCH_SRC) ? b->dest_byte_addr : 0;
}
return (b->data.all & W_ADDR_T_MASK);
}
M = M->next;
}
if (Label_name != NULL) {
*Label_name = NULL;
}
if (Dest_byte_addr != NULL) {
*Dest_byte_addr = 0;
}
return 0;
}
/*------------------------------------------------------------------------------------------------*/
gp_boolean
gp_mem_b_set_addr_name(MemBlock_t *M, unsigned int Byte_address, const char *Name)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemByte_t *b;
while (M != NULL) {
if ((M->base == block) && (M->memory != NULL)) {
b = &M->memory[offset];
if (b->symbol_name == NULL) {
_store_symbol_name(b, Name);
}
return true;
}
M = M->next;
}
return false;
}
/*------------------------------------------------------------------------------------------------*/
gp_boolean
gp_mem_b_set_args(MemBlock_t *M, unsigned int Byte_address, unsigned int Type, const MemArgList_t *Args)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemByte_t *b;
while (M != NULL) {
if ((M->base == block) && (M->memory != NULL)) {
b = &M->memory[offset];
if (b->data.is_byte_used) {
b->data.all |= Type & W_ARG_T_MASK;
if (Type & W_ARG_T_FIRST) {
b->args.first.arg = Args->first.arg;
b->args.first.val = Args->first.val;
b->args.first.offs = Args->first.offs;
}
if (Type & W_ARG_T_SECOND) {
b->args.second.arg = Args->second.arg;
b->args.second.val = Args->second.val;
b->args.second.offs = Args->second.offs;
}
return true;
}
break;
}
M = M->next;
}
return false;
}
/*------------------------------------------------------------------------------------------------*/
unsigned int
gp_mem_b_get_args(const MemBlock_t *M, unsigned int Byte_address, MemArgList_t *Args)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
MemByte_t *b;
while (M != NULL) {
if ((M->base == block) && (M->memory != NULL)) {
b = &M->memory[offset];
if (b->data.is_byte_used) {
if (Args != NULL) {
if (b->data.is_arg_first) {
Args->first.arg = b->args.first.arg;
Args->first.val = b->args.first.val;
Args->first.offs = b->args.first.offs;
}
else {
Args->first.arg = NULL;
Args->first.val = 0;
Args->first.offs = 0;
}
if (b->data.is_arg_second) {
Args->second.arg = b->args.second.arg;
Args->second.val = b->args.second.val;
Args->second.offs = b->args.second.offs;
}
else {
Args->second.arg = NULL;
Args->second.val = 0;
Args->second.offs = 0;
}
}
return (b->data.all & W_ARG_T_MASK);
}
}
M = M->next;
}
if (Args != NULL) {
Args->first.arg = NULL;
Args->first.val = 0;
Args->first.offs = 0;
Args->second.arg = NULL;
Args->second.val = 0;
Args->second.offs = 0;
}
return 0;
}
/*------------------------------------------------------------------------------------------------*/
gp_boolean
gp_mem_b_set_type(MemBlock_t *M, unsigned int Byte_address, unsigned int Type)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
while (M != NULL) {
if ((M->base == block) && (M->memory != NULL)) {
M->memory[offset].data.all |= Type & W_TYPE_MASK;
return true;
}
M = M->next;
}
return false;
}
/*------------------------------------------------------------------------------------------------*/
gp_boolean
gp_mem_b_clear_type(MemBlock_t *M, unsigned int Byte_address, unsigned int Type)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
while (M != NULL) {
if ((M->base == block) && (M->memory != NULL)) {
M->memory[offset].data.all &= ~(Type & W_TYPE_MASK);
return true;
}
M = M->next;
}
return false;
}
/*------------------------------------------------------------------------------------------------*/
unsigned int
gp_mem_b_get_type(const MemBlock_t *M, unsigned int Byte_address)
{
unsigned int block = IMemBaseFromAddr(Byte_address);
unsigned int offset = IMemOffsFromAddr(Byte_address);
while (M != NULL) {
if ((M->base == block) && (M->memory != NULL)) {
return (M->memory[offset].data.all & W_TYPE_MASK);
}
M = M->next;
}
return 0;
}
|