ms_ldst.c 48.1 KB
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 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */


	/*
	 *  ms_ldst  -  Process loads and stores for the MXS simulator
	 *
	 *	Jim Bennett
	 *	1993, 1994, 1995
	 */

#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include "ms.h"
#include "common_cache.h"
#include "simutil.h"
#include "memsys.h"

#ifdef MIPSY_MXS
#include "hw_events.h"
#include "cpu_state.h"
#include "memref.h"
#endif

/* These are needed to make the DCACHE_TAG and DCACHE_INDEXOF macros work */
#define dIndexMask  (DCACHE_INDEX - 1)
#define dTagShift   (log2DCACHE_SIZE - 1)

static int ms_lsop (struct s_cpu_state *st, struct s_lsq *ls,
			struct s_ldst_buffer *ldst_ent, int update_inst);
static int ms_lsop_uncached (struct s_cpu_state *st, struct s_lsq *ls);
static int ms_cache (struct s_cpu_state *st, struct s_lsq *ls,
					struct s_ldst_buffer *entry);

static	int	ldst_buffer_conflict (struct s_cpu_state *st,
					struct s_ldst_buffer *ldst_ent);
static struct s_ldst_buffer *
	ldst_buffer_alloc (struct s_cpu_state *st, int paddr, int lstype);
static	void	ldst_buffer_free (struct s_cpu_state *st,
					struct s_ldst_buffer *entry);
static	void	ldst_buffer_write(struct s_cpu_state *st,
		  struct s_ldst_buffer *entry, int paddr, char *data,int size);
static	int	ldst_buffer_updateRead (struct s_cpu_state *st,
		  struct s_ldst_buffer *entry, int paddr, char *data,int size);

static	int	recurse_unstall (struct s_cpu_state *st, int br_node);
static	void	set_new_excuse (struct s_cpu_state *st, int reg, int excuse);

#ifdef MIPSY_MXS

#define	WouldFault(st, lstype, addr) (0)

#else

#define	WouldFault(st, lstype, addr)					\
	(is_misaligned (lstype, addr) ||				\
    (!((((addr) >= pmap.text_base) && ((addr) < pmap.max_text)) ||	\
       (((addr) >= pmap.data_base) && ((addr) < pmap.data_top)) )))

#endif

/*
 * ldst_init - Initialize the load/store unit data structures. This
 *	     routine should be called after each exception.
 */
void ldst_init( struct s_cpu_state *st)
	{
	int i;

	for (i = 0; i < LDST_BUFFER_SIZE; i++)
		{
		st->ldst_buffer[i].next = &st->ldst_buffer[i+1];
		st->ldst_buffer[i].prev = &st->ldst_buffer[i-1];
		st->ldst_buffer[i].missTag = (void *) -1;
		st->ldst_buffer[i].ls = &st->lsq[i];
		st->lsq[i].ldst_ent = &st->ldst_buffer[i];
		}
	st->ldst_buffer[0].prev = NULL;
	st->ldst_buffer[LDST_BUFFER_SIZE-1].next = NULL;

		/* All entries go on the free list to start	*/
	st->ldst_head = NULL;
	st->ldst_tail = NULL;
	st->ldst_nextReserved = NULL;
	st->ldst_nextAvail = &st->ldst_buffer[0];
	}


	/*
	 *  ms_ldst_dep  -  Update load/store dependencies
	 */

void ms_ldst_dep (struct s_cpu_state *st)
	{

#ifdef ONE_PHASE_LS
		/* For a one phase load/store, update the IWIN_LDST_DEP	*/
		/* flag of instructions at the head of the chain.	*/
	int	inum;

	inum = st->iwin_head_ldst;
	if (inum >= 0)
		{
			/* Disallow speculative stores.		*/
		if ((st->iwin_flags [inum] & (IWIN_STORE | IWIN_SPEC)) ==
						(IWIN_STORE | IWIN_SPEC))
			return;

		st->iwin_flags [inum] &= ~IWIN_LDST_DEP;
		CheckInstAvail (st, inum);
		if (!(st->iwin_flags [inum] & IWIN_STORE))
			{
			inum = st->iwin_ldst [inum];
			while (inum >= 0)
				{
				if (st->iwin_flags [inum] & IWIN_STORE) break;
				st->iwin_flags [inum] &= ~IWIN_LDST_DEP;
				CheckInstAvail (st, inum);
				inum = st->iwin_ldst [inum];
				}
			}

		}
#else
		/* For a two phase load/store, check for instructions	*/
		/* that have completed phase one and are ready to go.	*/
		/* Ready = addr valid + predecessors addr valid		*/
		/*	   and ready to issue.				*/
	int	inum;

	for (inum = st->iwin_head_ldst; inum >= 0;
					inum = st->iwin_ldst [inum] )
		{
		    /* Can't issue or move around a load or store	*/
		    /* until we know the address it will use.		*/
		if (!(st->iwin_flags [inum] & IWIN_ADDR_VALID)) break;

		st->iwin_flags [inum] &= ~IWIN_LDST_DEP;
		CheckInstAvail (st, inum);

		    /* If this instruction didn't issue, then done for	*/
		    /* this cycle, try again next time.			*/
		if (!(st->iwin_flags [inum] & IWIN_AVAIL))
			{
			st->iwin_flags [inum] |= IWIN_LDST_DEP;
			break;
			}
		}
#endif
	}


	/*
	 *  ms_lsq  -  Add a load or store request to the load/store buffer
	 *
	 *	Set the status of the entry according to whether it is
	 *	cached/uncached, load/store, ready, conflicting, etc.
	 */

void ms_lsq (struct s_cpu_state *st, int addr, int reg, int reg2,
						int lstype, int inum)
	{
	struct	s_ldst_buffer *entry;
	struct	s_ldst_buffer *store_entry;
	struct	s_lsq	*ls;

	entry = ldst_buffer_alloc (st, st->iwin_paddr [inum], lstype);
	if (entry == NULL)
		{ st->stall_type = ST_LSQ_FULL; st->stall_issue = 1; return; }

	st->inum2ldst[inum] = entry;
	st->iwin_flags[inum] |= IWIN_LDSTBUF;

	ls = entry->ls;
	ls->status = 0;
	ls->lstype = lstype;
	ls->addr = addr;
	ls->paddr = entry->paddr;
	ls->reg = reg;
	ls->reg2 = reg2;
	ls->inum = inum;

	if (st->iwin_flags [inum] & IWIN_UNCACHED)
		ls->status |= LS_ST_UNCACHED;
	else
		{	/* If it is a cached operation, must check for	*/
			/* conflicts with other cached operations.	*/

		if (!IsPrefetch(lstype) && ldst_buffer_conflict (st, entry))
			ls->status |= LS_ST_CONFLICT;
		}

		/* If it's a store, do the operation once in order to	*/
		/* get the valid bytes information set.  Then check	*/
		/* if the value was really available.			*/

	if (IsStore(lstype))
		{
		int	reg_ix;
		REGSTAT	*rs;

		IncStat (ST_NSTORES);

		if (!IsSC(lstype))
			ms_lsop (st, ls, entry, 0);

		reg_ix = st->iwin[inum].r3 >> 1;
		rs = &st->reg_rstat [reg_ix];
		if (rs->reg_status & REG_IN_WIN)
			ls->status |= LS_ST_PEND;

		}

		/* If it's a load, check if it's also waiting for the	*/
		/* value from a store.					*/
	else if (IsLoad(lstype))
		{
		IncStat (ST_NLOADS);

		if (entry->loadhit)
			{
#ifdef LOAD_BYPASS
			store_entry = entry->loadhit;
			if (store_entry->ls->status & LS_ST_PEND)
				ls->status |= LS_ST_PEND;
#else
			ls->status |= LS_ST_PEND;
#endif
			}
		}
	}



	/*
	 *  ms_memory  -  Memory operations.  All of the memory operations,
	 *	whether cached or uncached, have been stored in the load
	 *	store buffer.  This routine checks each cycle which
	 *	operations can be forwarded to the cache.
	 */

void ms_memory (struct s_cpu_state *st)
	{
	int	cache_ops, check_loads, exit_loop;
	struct	s_ldst_buffer *store_entry;
	struct	s_ldst_buffer *entry, *next_entry;
	struct	s_lsq	*ls;

        next_entry = 0; /* Slience bogus compiler error message */

		/* First update the pending flags for the store ops	*/
	check_loads = 0;
	for (entry = st->ldst_head; (entry); entry = next_entry)
		{
		next_entry = entry->next;
		ls = entry->ls;
		if (IsStore(ls->lstype) && (ls->status & LS_ST_PEND))
			{
			int	reg_ix;
			REGSTAT	*rs;

			reg_ix = st->iwin[ls->inum].r3 >> 1;
			rs = &st->reg_rstat [reg_ix];
			if ((rs->reg_status & REG_IN_WIN) == 0)
				{
					/* Turn off LS_ST_CACHED so	*/
					/* it will be retried in cache	*/
				ls->status &= ~(LS_ST_PEND | LS_ST_CACHED);

					/* Call ms_lsop to write the	*/
					/* correct value in the load	*/
					/* store buffer.		*/
				if (!IsSC(ls->lstype))
					ms_lsop (st, ls, ls->ldst_ent, 0);
				check_loads = 1;
				}
			}
		}

		/* Then if a store became available this cycle, check	*/
		/* for waiting loads as well.				*/
#ifdef LOAD_BYPASS
	if (check_loads)
	    {
	    for (entry = st->ldst_head; (entry); entry = next_entry)
		{
		next_entry = entry->next;
		ls = entry->ls;
		if (IsLoad(ls->lstype) && (ls->status & LS_ST_PEND))
			{
			store_entry = entry->loadhit;
			if ((store_entry->ls->status & LS_ST_PEND) == 0)
					/* Turn off LS_ST_CACHED so	*/
					/* it will be retried in cache	*/
				ls->status &= ~(LS_ST_PEND | LS_ST_CACHED);
			}
		}
	    }
#endif

		/* Update the conflict status of entries	*/
	for (entry = st->ldst_head; (entry); entry = next_entry)
		{
		next_entry = entry->next;
		ls = entry->ls;
		if (ls->status & LS_ST_CONFLICT)
			{
			if (!ldst_buffer_conflict (st, entry))
				ls->status &= ~LS_ST_CONFLICT;
			}
		}

		/* The status is up to date, try to send requests	*/
		/* to the cache.					*/
	cache_ops = 0;
	exit_loop = 0;
	for (entry = st->ldst_head; (entry) && (!exit_loop); entry = next_entry)
		{
		next_entry = entry->next;
		ls = entry->ls;
		if (ls->status & LS_ST_DONE) continue;

			/* Send it to the cache if it isn't uncached	*/
			/* and it's never been accepted by the cache	*/
			/* and it doesn't conflict with prior operations */
		if ((ls->status &
		     (LS_ST_UNCACHED | LS_ST_CACHED | LS_ST_CONFLICT)) == 0)
			{
			if (ms_cache (st, ls, entry) >= 0)
				{
					/* The cache has accepted it.	*/
				cache_ops++;
				if (cache_ops >= CACHE_WIDTH) exit_loop = 1;
				}
			}

			/* Uncached operations are handled when the	*/
			/* data is available, and they are no longer	*/
			/* speculative, in the imprecise model. In the	*/
			/* precise model, are handled at graduation.	*/
		else
		if ((ls->status & (LS_ST_UNCACHED | LS_ST_PEND)) ==
							LS_ST_UNCACHED)
			{
#ifdef	PRECISE
			if (st->iwin_headgrad == ls->inum)
#else
			if ((st->iwin_flags[ls->inum] & IWIN_SPEC) == 0)
#endif /* !PRECISE */
				{
                                   entry->dataPtr = (char *) ls->paddr;  /* Mark as active */
                                   if (ms_lsop_uncached (st, ls) == 0) { 
                                      ls->status |= LS_ST_DONE;
                                   }
                                   exit_loop = 1;  /* One uncached per cycle */
				}
			}

			/* If this request was rejected by the cache,	*/
			/* stick here until it is accepted		*/
		if (ls->status & LS_ST_FAILED) exit_loop = 1;
		}

#ifndef PRECISE
	ldst_retire_stores (st);
#endif /* !PRECISE */
	}


	/*
	 *  ldst_buffer_conflict  -  Check starting at the given entry
	 *	in the load/store buffer if there is a potential cache
	 *	conflict between this operation and all prior entries
	 *	in the buffer.  Return TRUE if so.
	 */

static int ldst_buffer_conflict (struct s_cpu_state *st,
					struct s_ldst_buffer *entry)
	{
	struct	s_ldst_buffer *prev_entry;
	struct	s_lsq	*ls;
	int	matched;
	int	new_paddr, new_mask, new_isload;
	struct s_ldst_buffer *new_loadhit;
	int	cacheindex, mask;
	int	i, cmask;
	int	cache_tags [DCACHE_ASSOC];
	int	next_tag, old_tag;

        prev_entry = 0; /* Slience bogus compiler error message */
	ls = entry->ls;
	new_paddr = ls->paddr;
	new_mask = (IsDouble(ls->lstype) ? ~7 : ~3);
	new_isload = IsLoad (ls->lstype);
	new_loadhit = entry->loadhit;

	cache_tags [0] = DCACHE_TAG(new_paddr);
	next_tag = 1;

	matched = 0;
	for (entry = entry->prev; (entry); entry = prev_entry)
		{
		prev_entry = entry->prev;
		ls = entry->ls;
		if (ls->status & LS_ST_UNCACHED)
			continue;	/* Prior request uncached, so	*/
					/* ignore it			*/

			/*
			 * This previous request does not conflict if:
			 *  1) Was to a different cache index
			 *  2) Can't possibly cause a conflict miss replacement
			 *  3) Was to a different word of the line
			 *  4) Both requests are loads
			 *  5) The new request is a load that hits in the
			 *     store buffer
			 */

		cacheindex = DCACHE_INDEXOF(ls->paddr);
		mask = (IsDouble(ls->lstype) ? ~7 : ~3);
		if (cacheindex != DCACHE_INDEXOF(new_paddr))
			continue;

			/* Potential for conflict miss; report match. */
		old_tag = DCACHE_TAG(ls->paddr);
		for (i=0; i<next_tag; i++)
			if (old_tag == cache_tags[i]) break;
		if (i >= next_tag)
			{
			if (next_tag >= DCACHE_ASSOC)
				{
				matched = 1;
				IncStat(ST_LDSTDEP_CONFLICT);
				break;
				}
			cache_tags [next_tag] = old_tag;
			next_tag++;
			continue;
			}

		if (old_tag !=  DCACHE_TAG(new_paddr))
			continue;

		cmask = mask | new_mask;
		if ((ls->paddr&cmask) != (new_paddr&cmask) )
			continue;	/* Different word of same line */

		if (new_isload &&
		    (IsLoad (ls->lstype) || (new_loadhit)) )
			continue;

		matched = 1;
		IncStat(ST_LDSTDEP_FAIL);
		break;
		}
	return (matched);
	}


	/*
	 *  ms_cache  -  Items are pulled from the load/store queue one
	 *		 at a time and processed by this routine.
	 *
	 *	Return -1 if the operation could not be performed
	 */

static int ms_cache (struct s_cpu_state *st, struct s_lsq *ls,
					struct s_ldst_buffer *entry)
	{
	char *dataPtr = NULL;
	int flavor;
	int ret;
	WorkDecls;

	IncStat(ST_CACHE);

		/* Screen out errors due to speculative loads	*/
	if (WouldFault(st, ls->lstype, ls->addr))
		{
		if (IsLoad (ls->lstype))
			st->reg_rstat[ls->reg >> 1].reg_status |= REG_ERROR;
		st->iwin_flags[ls->inum] |= IWIN_FAULT;
		CheckSquash (st, &st->iwin[ls->inum]);
		ls->status |= LS_ST_CACHED;
		return (0);
		}

#ifdef MIPSY_MXS
	if ((ls->lstype == ST_INTEGER_SC) &&
	    !(((CPUState *) (st->mipsyPtr))->LLbit)) {
	    goto  scfail;
	}
	/* Update Mipsy PC so memstat knows where miss came from. */
	((CPUState *) (st->mipsyPtr))->PC = st->iwin_pc[ls->inum];
#endif

    if (ls->lstype == PREFETCH) {
      int mcmd;
      if (ls->paddr != -1) {
         switch (-ls->reg) {
         case 1: case 5: case 7:
            mcmd = MEMSYS_GETX;
            break;
         case 0: case 4: case 6:
         default:
            mcmd = MEMSYS_GET;
            break;
         }
         SCachePrefetch(CPUNUM(st), ls->addr, ls->paddr, mcmd);
      }
#ifdef PREF_DEBUG
        CPUPrint("\t\tCPU %d EX Prefetch addr = %08x cycle = %d ret = %d\n", 
                 CPUNUM(st), entry->ls->paddr, st->work_cycle, ret);
#endif
      free_inst(st, ls->inum);
      ls->status |= LS_ST_DONE;
      return 0;
    }

	if (ls->status & LS_ST_STALL)
		{
		ret = STALL;
		dataPtr = (char *)entry->missTag;
		}
	else
		{
				/* Set flag for CheckIntervention	*/
		ls->status |= LS_ST_FAILED;
		st->ms_action = ACT_DCACHE_MISS;
		if (IsLoad(ls->lstype)) {
		    IncStat(ST_CACHE_LOAD);
		    flavor = (ls->lstype == LD_INTEGER_LL) ? LL_FLAVOR : 0;
		    ret = DCacheFetchShared(CPUNUM(st), ls->addr, ls->paddr,
						flavor, &dataPtr);
		} else {
		    IncStat(ST_CACHE_STORE);
		    flavor = (ls->lstype == ST_INTEGER_SC) ? SC_FLAVOR : 0;
		    ret = DCacheFetchExclusive(CPUNUM(st), ls->addr, ls->paddr,
						flavor, &dataPtr);
		}
		ls->status &= ~LS_ST_FAILED;
		}
	switch (ret) {
	case SUCCESS: {
		/* Hit in the cache, do the operation (unless the data	*/
		/* is still pending).					*/
	    ls->status |= LS_ST_CACHED;
	    if (ls->status & LS_ST_PEND) return (0);

		/* When the operation is done, clear the invalidated	*/
		/* flag.  If the flag gets set between here and when	*/
		/* this operation is retired, then we have to retry it.	*/
	    entry->dataPtr = dataPtr;
	    entry->invalidated = 0;
	    ms_lsop (st, ls, entry, 1);
#ifdef PREF_DEBUG
        CPUPrint("\t\tCPU %d EX %s addr = %08x cycle = %d\n", CPUNUM(st),
                 IsLoad(ls->lstype) ? "Load" : "Store", ls->paddr, st->work_cycle);
#endif
	    ls->status |= LS_ST_DONE;
	    IncStat(ST_CACHE_HIT);
	    return 0;
	}
	case FAILURE: {
	    /* Cache couldn't accept the request. Return failure to caller
	     * so it will be retried.
	     */
	    IncStat(ST_CACHE_FAILURE);
	    if (IsLoad(ls->lstype))
		set_new_excuse (st, ls->reg, ST_CACHE_LBSY);
	    ls->status |= LS_ST_FAILED;
	    return -1;
	}
	case STALL: {
	    /* Memsystem has it now. Wait for callback */
	    ls->status |= LS_ST_STALL + LS_ST_CACHED;
	    entry->missTag = (void *) dataPtr;
            entry->cacheStallReason = MxsClassifyMiss(CPUNUM(st), 
                                     entry->ls->addr, entry->ls->paddr, FALSE);

	    if (ls->status & LS_ST_PEND) return (0);

	    IncStat(ST_CACHE_MISS);
	    if ((entry->loadhit) ||
		(IsStore(ls->lstype) && (ls->lstype != ST_INTEGER_SC))) {
		/* Actually, stores and loads that hit in the write buffer
		 * can be processed. Special case SC instructions. */
		ms_lsop (st, ls, entry, 1);
		ls->status |= LS_ST_DONE;
		if (entry->loadhit) IncStat(ST_CACHE_LOADHIT);
	    }
	    if (IsLoad(ls->lstype))
		set_new_excuse (st, ls->reg, ST_CACHE_BUS);
	    return 0;
	}
	case SCFAILURE: {
	  scfail:
	    Ireg(ls->reg2) = 0;  /* FAIL the SC */
	    if (ls->reg2 == 0)
		reg0_writeback(st, (void *) (ls->inum));
	    else
		reg_writeback(st, (void *) ls->reg2);

	    entry->size = 0;
	    IncStat(ST_CACHE_SCFAILURE);
	    ls->status |= LS_ST_DONE;
	    return 0;
	    }
	default:
	    if (IsLoad(ls->lstype))
		set_new_excuse (st, ls->reg, ST_CACHE_LBSY);
	    ls->status |= LS_ST_CACHED;
	    return (0);
	}
	}



	/*
	 *  set_new_excuse  -  Update the reason why the register is not
	 *			yet available
	 */

static void set_new_excuse (struct s_cpu_state *st, int reg, int excuse)
	{
	int reg_ix = reg >> 1;

	if (st->new_excuse[reg_ix] == ST_NO_EXCUSE)
		st->reg_excuse[reg_ix] = excuse;
	else
		st->new_excuse[reg_ix] = excuse;
	}




/************************************************************************/
/*									*/
/* Common routine to perform load operations, regardless of whether	*/
/* there is a cache or not.						*/
/*									*/
/************************************************************************/

	/*
	 *  ms_lsop  -  Perform load/store operation
	 *
	 *	Converts virtual address to address in simulator's memory
	 *	and performs the indicated operation.  Always returns 0
	 *	to indicate success.
	 */

static int ms_lsop (struct s_cpu_state *st, struct s_lsq *ls,
				struct s_ldst_buffer *entry, int update_inst)
	{
	int  reg, i, update;
	char *dataPtr;
	uint paddr = ls->paddr;
	static double nulldoubleword = 0;
	WorkDecls;

#ifndef MXS_CACHE
		/* If no cache, Must check for errors here:	*/
		/* Screen out errors due to speculative loads	*/
	if (WouldFault(st, lstype, ls->addr))
		{
		if (IsLoad (ls->lstype))
			st->reg_rstat[ls->reg >> 1].reg_status |= REG_ERROR;
		st->iwin_flags[ls->inum] |= IWIN_FAULT;
		CheckSquash (st, &st->iwin[ls->inum]);
		return (0);
		}
#endif

	reg = ls->reg;
	dataPtr = entry->dataPtr;
	if (dataPtr == NULL) {
	    /* This should only happen when we have a read that
	     * hits in the write buffer but is not in the class. Give
	     * a fake memory location to start with load which will
	     * be filled from the write buffer.
	     */
	    dataPtr = (char *) &nulldoubleword;
	}


#if PC_LATENCY > 1
#define WRITEBACKREG() \
	if (reg == 0) \
    Add_to_worklist (st, PC_LATENCY, reg0_writeback,(void *)(ls->inum)); \
	else \
    Add_to_worklist (st, PC_LATENCY, reg_writeback,(void *)reg);
#else
#define WRITEBACKREG() \
	if (reg == 0) reg0_writeback(st,(void *)(ls->inum)); \
	else reg_writeback(st, (void *) reg)
#endif



	switch (ls->lstype)
		{
		case LD_UNSIGNED_B: {
			unsigned char uc;
			uc = * (unsigned char *)dataPtr;
			update =
			ldst_buffer_updateRead(st,entry,paddr,(char *)&uc,1);
			Ureg(reg) = uc;
			WRITEBACKREG();
			break;
		     }
		case LD_UNSIGNED_H: {
			ushort  us;
			us = *(ushort *)dataPtr;
			update =
			ldst_buffer_updateRead(st,entry,paddr,(char *)&us,2);
			Ureg(reg) = us;
			WRITEBACKREG();
			break;
		     }
		case LD_UNSIGNED_L:
			{
			int reg2 = ls->reg2;
			int shft = ((int)dataPtr & 0x03) * 8;

			dataPtr = (char *)((int)dataPtr&0xfffffffc);
			i = *(unsigned int*) dataPtr;
			update = ldst_buffer_updateRead (st, entry,
						paddr &~0x3, (char *)&i, 4);
			Ureg(reg) = Ureg(reg2) & (~(0xffffffff << shft));
			Ureg(reg) |= i << shft;
			WRITEBACKREG();
			break;
			}

		case LD_UNSIGNED_R:
			{
			int reg2 = ls->reg2;
			int shft = (3 - ((int)dataPtr & 0x03)) * 8;
			int mask = 0xffffff00 << (24 - shft);

			dataPtr = (char *)((int)dataPtr&0xfffffffc);
			i = *(unsigned int*) dataPtr;
			update = ldst_buffer_updateRead (st, entry,
						paddr &~0x3, (char *)&i, 4);
			Ureg(reg) = Ureg(reg2) & mask;
			Ureg(reg) |= (i >> shft) & (~mask);
			WRITEBACKREG();
			break;
			}

		case LD_INTEGER_B: {
			signed char c;
			c = *(signed char *) dataPtr;
			update =
			ldst_buffer_updateRead(st,entry,paddr,(char *)&c,1);
			Ireg(reg) = c;
			WRITEBACKREG();
			break;
		     }
		case LD_INTEGER_H: {
			short s;
			s = *(short *) dataPtr;
			update =
			ldst_buffer_updateRead(st,entry,paddr,(char *)&s,2);
			Ireg(reg) = s;

			WRITEBACKREG();
			break;
		     }
		case LD_INTEGER_W: {
			int w;
			w = *(int *) dataPtr;
			update =
			ldst_buffer_updateRead(st,entry,paddr,(char *)&w,4);
			Ireg(reg) = w;

			WRITEBACKREG();
			break;
		     }
		case LD_INTEGER_LL: {
			int w;
			w = *(int *) dataPtr;
			update =
			ldst_buffer_updateRead(st,entry,paddr,(char *)&w,4);
			Ireg(reg) = w;

			WRITEBACKREG();
#ifdef MIPSY_MXS
			((CPUState *) (st->mipsyPtr))->LLbit = 1;
#endif
			break;
		     }
		case LD_FLOAT:{
			float f;
			f = *(float *) dataPtr;
			update =
			ldst_buffer_updateRead(st,entry,paddr,(char *)&f,4);

			Freg(reg) = f;
			WRITEBACKREG();
			break;
		     }
		case LD_DOUBLE: {
			double d;
			d = *(double *) dataPtr;
			update =
			ldst_buffer_updateRead(st,entry,paddr,(char *)&d,8);

			Dreg(reg) = d;
			WRITEBACKREG();
			break;
		     }
		case ST_INTEGER_B: {
			char b = Ireg(ls->reg);
			ldst_buffer_write(st,entry,paddr,(char *)&b,1);
			break;
		     }
		case ST_INTEGER_H: {
			short s = Ireg(ls->reg);
			ldst_buffer_write(st,entry,paddr,(char *)&s,2);
			break;
		     }
		case ST_INTEGER_W: {
			int i = Ireg(ls->reg);
			ldst_buffer_write(st,entry,paddr,(char *)&i,4);
			break;
		     }

		case ST_INTEGER_L: {
			uint i;
			int byte = (((int)paddr) & 0x3);
			i = Ureg(ls->reg);
			ldst_buffer_write(st,entry,paddr,(char *)&i,4-byte);
			break;
		     }
		case ST_INTEGER_R: {
			 uint i;
			int byte = (((int)paddr) & 0x3);
			i = Ureg(ls->reg) << (24 - byte*8);
			ldst_buffer_write(st,entry,paddr&~0x3,(char *)&i,1 + byte);
			break;
		     }
		case ST_FLOAT: {
			float f = Freg(ls->reg);
			ldst_buffer_write(st,entry,paddr,(char *)&f,4);
			break;
		     }
		case ST_DOUBLE: {
			double d = Dreg(ls->reg);
			ldst_buffer_write(st,entry,paddr, (char *)&d,8);
			break;
		     }
		case ST_INTEGER_SC: {
			int i = Ireg(ls->reg);
			reg = ls->reg2;
			ldst_buffer_write(st,entry,paddr,(char *)&i,4);
			Ireg(reg) = 1;

			WRITEBACKREG();
			break;
		     }
        case PREFETCH: {
          fprintf(stderr, "ms_lsop called on prefetch instruction\r\n");
          ms_break (st, NULL, "ERR");
        }
		}

	if (update_inst)
		{
#ifdef DEBUG_CHECKS
		if (st->iwin_flags[ls->inum] & IWIN_SQUASH)
			{
			fprintf (stderr, "Executing squashed load/store\r\n");
			ms_break (st, NULL, "ERR");
			}
#endif /* DEBUG_CHECKS */

#ifdef PRECISE
		if (st->iwin[ls->inum].r1 < 0)
			free_inst(st, ls->inum);
#else /* PRECISE */
		if (IsLoad (ls->lstype))
			{
			int	ret;

			if (update) entry->dataPtr =
						(char *) &nulldoubleword;
			ret = ldst_buffer_retire (st, ls->inum);
#ifdef DEBUG_CHECKS
			if (ret != 0)
				{
				fprintf (stderr, "Retire failure on read\r\n");
				ms_break (st, NULL, "ERR");
				}
#endif /* DEBUG_CHECKS */
			}
#endif /* PRECISE */
		}

	return (0);
	}

int LdSizeOf(int lstype)
{
    switch (lstype)
		{
		case LD_UNSIGNED_B:
		case LD_INTEGER_B:
			return 1;
		case LD_UNSIGNED_H:
		case LD_INTEGER_H:
			return 2;
		case LD_UNSIGNED_L:
			return 4;
		case LD_UNSIGNED_R:
			return 4;
		case LD_INTEGER_W:
		case LD_INTEGER_LL:
		    return 4;
		case LD_FLOAT:
		    return 4;
		case LD_DOUBLE:
		    return 8;
		}
   return 0;
}


	/*
	 *  ldst_buffer_alloc  -  Allocate an entry in ldst_buffer
	 *
	 *	Also check if this is a load that is satisfied by
	 *	an earlier store, and set a pointer to it if so
	 *
	 *	Returns a NULL pointer if allocation fails
	 */

static struct s_ldst_buffer *
ldst_buffer_alloc(struct s_cpu_state *st, int paddr, int lstype)
	{
	struct s_ldst_buffer *entry, *prev_entry, *store1, *store2;
	char *vs;

        prev_entry = 0; /* Slience bogus compiler error message */

		/* Grab the next entry from the reserved list.		*/
		/* It's an error if this entry hasn't been reserved	*/
	entry = st->ldst_nextReserved;
	if (entry == NULL) return (NULL);

		/* Move entry from reserved list to active load/store	*/
		/* list.  Add at end of list to preserve order.		*/
	st->ldst_nextReserved = entry->next;

	if (st->ldst_tail)
		{
		st->ldst_tail->next = entry;
		entry->prev = st->ldst_tail;
		entry->next = NULL;
		st->ldst_tail = entry;
		}
	else
		{
		st->ldst_head = entry;
		st->ldst_tail = entry;
		entry->next = NULL;
		entry->prev = NULL;
		}

		/* Then fill in this entry				*/
	entry->dataPtr = NULL;
	entry->missTag = (void *) -1;
        entry->cacheStallReason = 0;
	entry->paddr = paddr;
	entry->size = 0;
	entry->invalidated = 0;
	entry->loadhit = NULL;
	vs = entry->validBytes;
	vs[0] = 0; vs[1] = 0; vs[2] = 0; vs[3] = 0;
	vs[4] = 0; vs[5] = 0; vs[6] = 0; vs[7] = 0;

		/* If we are allocating a load see if a previous store	*/
		/* can provide us with a value.				*/
	if (IsLoad(lstype))
		{
		int off = (paddr & 7);
		int size = LdSizeOf(lstype);
		int anyhit = 0;
		int b, hitflag;

		store1 = store2 = NULL;
		for (entry = entry->prev; (entry); entry = prev_entry)
			{
			prev_entry = entry->prev;
			if (!IsStore(entry->ls->lstype)) continue;
			if (((entry->paddr)&~7) == (paddr&~7))
				{
				if (store1 == NULL)
					store1 = entry;
				else
					{
					store2 = entry;
					break;
					}
				}
			}
		entry = st->ldst_tail;

	 /*	JEB:  Address and size could be inconsistent at this point,
	  *	due to speculation.  As a later enhancement, detect this
	  *	case and use this as a hint to stop fetching along this
	  *	thread, and activate a different one.  Put the check earlier
	  *	in the pipeline, where the TLB translation is currently.
	  */
		if ((size+off) > 8) size = 8 - off;
		if (store1)
			{
			hitflag = 1;
			for (b = 0; b < size; b++)
				{
				if (store1->validBytes[b+off])
					anyhit = 1;
				else
					{ hitflag = 0; }
				}
			if (hitflag) entry->loadhit = store1;
			if (anyhit) return (entry);
			}

		if (store2)
			{
			hitflag = 1;
			for (b = 0; b < size; b++)
				if (!store2->validBytes[b+off])
					{ hitflag = 0;  break; }
			if (hitflag) entry->loadhit = store2;
			}
		}
	return (entry);
	}


	/*
	 *  ldst_buffer_reserve  -  Reserve an entry in ldst_buffer
	 *
	 *	Returns -1 if reservation fails
	 */

int ldst_buffer_reserve (struct s_cpu_state *st)
	{
	struct s_ldst_buffer *entry;

	entry = st->ldst_nextAvail;
	if (entry)
		{	/* Found a free entry.  Move it to the reserved	*/
			/* list.					*/
		st->ldst_nextAvail = entry->next;

		entry->next = st->ldst_nextReserved;
		st->ldst_nextReserved = entry;
		return (0);
		}
	return (-1);
	}


	/*
	 *  ldst_buffer_release  -  Release a reserved entry
	 */

void ldst_buffer_release (struct s_cpu_state *st)
	{
	struct s_ldst_buffer *entry = st->ldst_nextReserved;
	if (entry)
		{	/* Move entry from reserved list to free list	*/
		st->ldst_nextReserved = entry->next;

		entry->next = st->ldst_nextAvail;
		st->ldst_nextAvail = entry;
		}
#ifdef DEBUG_CHECKS
	else
		{
		fprintf (stderr, "Illegal release of ldst_buffer entry\r\n");
		ms_break (st, NULL, "ERR");
		}
#endif
	}


	/*
	 *  ldst_buffer_free  -  Free the given load store buffer entry
	 */

static void ldst_buffer_free (struct s_cpu_state *st,
					struct s_ldst_buffer *entry)
	{
	struct s_ldst_buffer *scan;

		/* If it's a store, check if any loads are depending	*/
		/* on this value.  Then let them know it is gone.	*/
	if (IsStore (entry->ls->lstype))
		{
		for (scan = st->ldst_head; (scan); scan = scan->next)
			{
			if ((scan->loadhit == entry) &&
			    IsLoad (scan->ls->lstype) )
				{
				scan->loadhit = NULL;
					/* Turn off LS_ST_CACHED so	*/
					/* it will be retried in cache	*/
				scan->ls->status &= ~(LS_ST_PEND|LS_ST_CACHED);
				}
			}
		}

		/* Move the entry from the active list to the free list	*/
	if (entry->prev == NULL)
		st->ldst_head = entry->next;
	else
		entry->prev->next = entry->next;

	if (entry->next == NULL)
		st->ldst_tail = entry->prev;
	else
		entry->next->prev = entry->prev;

	entry->next = st->ldst_nextAvail;
	st->ldst_nextAvail = entry;
	}


	/*
	 *  ldst_buffer_write  -  Write data into the given entry in the
	 *	load/store buffer and mark the set bytes valid.
	 *
	 *	If invalid arguments are passed, it is due to a speculative
	 *	write.  This is OK, just return silently.
	 *
	 *	JEB: As a later enhancement, could use this as a hint to stop
	 *	fetching along this thread, and activate a different one.
	 */

static void ldst_buffer_write(struct s_cpu_state *st,
	struct s_ldst_buffer *entry, int paddr, char *data,int size)
{
   int lineoffset;

   if (size > 8) fprintf(stderr, "Memory op large\r\n");

   entry->paddr = paddr;
   entry->size = size;
   lineoffset = paddr & 7;
   if ((size+lineoffset) > 8) size = 8 - lineoffset;

   switch(size) {
   case 1:
	*(char *) (entry->data + lineoffset) = *(unsigned char*)data;
	*(char *) (entry->validBytes + lineoffset) = 1;
	break;

   case 2:
	*(char *) (entry->data + lineoffset+0) = *(unsigned char*)data;
	*(char *) (entry->data + lineoffset+1) = *(unsigned char*)(data+1);
	*(char *) (entry->validBytes + lineoffset+0) = 1;
	*(char *) (entry->validBytes + lineoffset+1) = 1;
	break;

   case 3:
	*(char *) (entry->data + lineoffset+0) = *(unsigned char*)data;
	*(char *) (entry->data + lineoffset+1) = *(unsigned char*)(data+1);
	*(char *) (entry->data + lineoffset+2) = *(unsigned char*)(data+2);
	*(char *) (entry->validBytes + lineoffset+0) = 1;
	*(char *) (entry->validBytes + lineoffset+1) = 1;
	*(char *) (entry->validBytes + lineoffset+2) = 1;
	break;

   case 4:
	if ((lineoffset & 0x03) != 0) return;
	*(uint  *) (entry->data + lineoffset) = *(unsigned int *)data;
	*(uint  *) (entry->validBytes + lineoffset) =  0x01010101;
	break;

   case 8:
	if ((lineoffset & 0x07) != 0) return;
	*(uint64  *) (entry->data + lineoffset) =
		*(uint64 *) data;
	*(uint64  *) (entry->validBytes + lineoffset) =
	 ((uint64)0x01010101<<32)| (uint64 )0x01010101;
	break;

   default:
	fprintf(stderr, "Memory op invalid size\r\n");
	break;
   }
   return;
}


	/*
	 *  ldst_buffer_updateRead  -  Read data from the given entry to the
	 *	the data area pointed to, if it hit in the store buffer.
	 *
	 *	Returns 1 when the read is satisfied from the store buffer
	 *	Otherwise returns 0.
	 *
	 *	If invalid arguments are passed, it is due to a speculative
	 *	load.  This is OK, just return silently.
	 *
	 *	JEB: As a later enhancement, could use this as a hint to stop
	 *	fetching along this thread, and activate a different one.
	 */

static int
ldst_buffer_updateRead(struct s_cpu_state *st, struct s_ldst_buffer *entry,
			int paddr, char *data, int size)
{
   int lineoffset, i;
   char vb[8];
   struct s_ldst_buffer *store_entry;

   if (entry->loadhit == NULL) return (0);

   vb[0] = vb[1] = vb[2] = vb[3] = vb[4] = vb[5] = vb[6] = vb[7] = 0;
   lineoffset = paddr & 7;
   if ((size+lineoffset) > 8) size = 8 - lineoffset;

				/* Update this entry from the store	*/

   store_entry = entry->loadhit;

   for (i = 0; i < size; i++) {
    if (*(char *) (store_entry->validBytes + lineoffset+i) == 1) {
      data[i] =  *(char *) (store_entry->data + lineoffset+i);
      vb[i+lineoffset] = 1;
      }
   }

   for (i = 0; i < size; i++) {
	lineoffset = paddr & 7;
	if (vb[i+lineoffset] == 0) {
	    /* Didn't update it. */
	    return 0;
	}
   }
   return 1;
}



	/*
	 *  ldst_retire_stores  -  Retire unspeculative stores (for imprecise
	 *		CPU model)
	 */

static void ldst_retire_stores (struct s_cpu_state *st)
	{
	struct	s_ldst_buffer *entry, *next_entry;
	struct	s_lsq	*ls;
	int	inum, ret;
	INST	*ip;

        next_entry = 0; /* Slience bogus compiler error message */
	for (entry = st->ldst_head; (entry); entry = next_entry)
		{
		next_entry = entry->next;
		ls = entry->ls;
		if (IsLoad (ls->lstype) || IsPrefetch(ls->lstype)) continue;
		if ((ls->status & LS_ST_DONE) == 0) continue;

		inum = ls->inum;
		ip = &st->iwin[inum];

			/* Uncached loads don't need any more		*/
			/* processing, just release the entry.		*/
		if (ls->status & LS_ST_UNCACHED)
			{
			if (st->iwin_flags[inum] & IWIN_LDSTBUF)
				{
				st->iwin_flags [inum] &= ~IWIN_LDSTBUF;
				ldst_buffer_free (st, entry);
				}

#ifdef DEBUG_CHECKS
			if (st->iwin_flags[inum] & IWIN_SQUASH)
				{
				fprintf (stderr,
					"Executing squashed load/store\r\n");
				ms_break (st, NULL, "ERR");
				}
#endif /* DEBUG_CHECKS */
			if (ip->r1 > 0)
				reg_writeback ((void *)st, (void *)ip->r1);
			else
				free_inst (st, inum);
			continue;
			}

		if ((st->iwin_flags[inum] & (IWIN_STORE | IWIN_SPEC)) ==
								IWIN_STORE)
			{
			/* Found a store that is no longer speculative,	*/
			/* so process it.				*/
#ifdef DEBUG_CHECKS
			if (st->iwin_flags[inum] & IWIN_SQUASH)
				{
				fprintf (stderr,
					"Executing squashed load/store\r\n");
				ms_break (st, NULL, "ERR");
				}
#endif /* DEBUG_CHECKS */
			ret = ldst_buffer_retire (st, inum);
			if (ret < 0)
					/* Retry the store if the	*/
					/* line has been evicted.	*/
				ls->status &= ~(LS_ST_CACHED | LS_ST_DONE);
			else if (ret == 0)
				{	/* It worked, can free this	*/
					/* instruction.			*/
				if (ip->r1 > 0)
					reg_writeback ((void *)st,
							(void *)ip->r1);
				else
					free_inst (st, inum);
				}
			else
				;	/* Still waiting for the line	*/
					/* to come in, try again next	*/
					/* cycle.			*/
			}
		}
	}


	/*
	 *  ldst_buffer_retire  -  Do actual writes to cache as stores
	 *		graduate.
	 *
	 *	Returns 0 if successful
	 *	Returns 1 if the line hasn't come into the cache yet
	 *	Returns -1 if the line has been evicted (=> need to retry)
	 */

int ldst_buffer_retire(struct s_cpu_state *st, int inum)
{
	struct s_ldst_buffer *entry = st->inum2ldst[inum];
#ifdef DEBUG_CHECKS
	if (st->iwin_flags[inum] & IWIN_SQUASH)
		{
		fprintf (stderr, "Executing squashed load/store\r\n");
		ms_break (st, NULL, "ERR");
		}
#endif /* DEBUG_CHECKS */

   if (entry->ls->inum != inum) {
	fprintf(stderr, "retire failure in ldst_buffer_retire\r\n");
	ms_break (st, NULL, "ERR");
   }
   if (entry->ls->status & LS_ST_UNCACHED) goto retire_exit;
   if (IsPrefetch(entry->ls->lstype)) goto retire_exit;

   if (entry->invalidated) {     /* Line was nuked from the cache */
	char *dataPtr;
	int ret;

	if (IsLoad(entry->ls->lstype) || (entry->ls->lstype == ST_INTEGER_SC)) {
	    IncStat(ST_LDST_INVALIDLOAD);
	    return -1;  /* Fail loads or SCs */
	}
	/* Refetch line for stores. */
	if (entry->dataPtr == NULL) {
	    IncStat(ST_LDST_WAIT);
	    /* Outstanding memory op - must wait for it to finish */
	    return 1;
	}
	st->ms_action = ACT_DCACHE_MISS;
	/* Update Mipsy PC so memstat knows where miss came from. */
	((CPUState *) (st->mipsyPtr))->PC = st->iwin_pc[entry->ls->inum];
	ret = DCacheFetchExclusive(CPUNUM(st), entry->ls->addr,
				entry->ls->paddr,0, &dataPtr);
	switch (ret) {
	case SUCCESS:
	    entry->dataPtr = dataPtr; /* Still in cache */
	    IncStat(ST_LDST_REFETCH);
	    break;
	case FAILURE:
            /* If we got invalidated then most likley we are not in the
             * second level cache.  Make this report as second level Dcache
             * miss. 
             */
            entry->cacheStallReason = E_L2 | E_D; 
	    /* Cache couldn't accept the request */
	    IncStat(ST_LDST_WAIT);
	    return 1;  /* Retry latter. */
	case STALL:
	    /* Memory system has it down */
	    entry->missTag = (void *) dataPtr;
            entry->cacheStallReason = MxsClassifyMiss(CPUNUM(st), 
                                     entry->ls->addr, entry->ls->paddr, FALSE);
	    entry->dataPtr = NULL;
	    entry->invalidated = 0;
	    IncStat(ST_LDST_REFETCH);
	    return 1;
	}
   }
   if ((entry->dataPtr == NULL) &&	/* Data not here yet. */
	(entry->ls->lstype != ST_INTEGER_SC)) { /* OK for failed SCs */
	  IncStat(ST_LDST_WAIT);
	  return 1;
   }

   if (IsStore(entry->ls->lstype)) {
	int off = (entry->paddr&7);
	char *dst = (char *)((uint)entry->dataPtr&~7) + off;
	char *src = entry->data + off;
	IncStat(ST_LDST_RETIRE_STORE);
#ifdef TRACE
	if (tracefile)
		{
		struct	s_trace	trc;

		trc.addr = entry->ls->addr;
		trc.lstype = entry->ls->lstype;
		trc.u.dreg = 0.0;
		bcopy (src, (char *)&trc.u.dreg, entry->size);
		fwrite (&trc, sizeof (struct s_trace), 1, tracefile);
		fflush (tracefile);
		}
#endif
	switch (entry->size) {
	case 0:
	   break;
	case 1:
	  dst[0] = src[0]; break;
	case 2:
	  dst[0] = src[0]; dst[1] = src[1]; break;
	case 3:
	  dst[0] = src[0]; dst[1] = src[1];  dst[2] = src[2]; break;
	case 4:
	  ((int *)dst)[0] = ((int *)src)[0]; break;
	case 8:
	  ((double *)dst)[0] = ((double *)src)[0]; break;
	default:
	   bcopy(src,dst, entry->size);
	}
   } else
	{
	IncStat(ST_LDST_RETIRE_LOAD);
#ifdef TRACE
	if (tracefile)
		{
		struct	s_trace	trc;

		trc.addr = entry->ls->addr;
		trc.lstype = entry->ls->lstype;
		trc.u.dreg = 0.0;
		trc.u.ireg = entry->ls->reg;
		if (!trace_writes)
			{
			fwrite (&trc, sizeof (struct s_trace), 1, tracefile);
			fflush (tracefile);
			}
		}
#endif
	}

retire_exit:
   st->iwin_flags[inum] &= ~IWIN_LDSTBUF;
   ldst_buffer_free (st, entry);
   return (0);
}

/*
 * ldst_record_stall - Record the stall generated by an entry 
 */
void
record_ldst_stall(struct s_cpu_state *st, int inum, int stall)
{
   struct s_ldst_buffer *entry = st->inum2ldst[inum];

   if (entry->ls->inum != inum) {
	fprintf(stderr, "retire failure in ldst_record_stall\r\n");
	ms_break (st, NULL, "ERR");
   }
   MS_STALL_EVENT(CPUNUM(st), entry->ls->addr, st->iwin_pc[inum], 
                  stall, entry->cacheStallReason);
}

void
record_icache_stall(struct s_cpu_state *st, int pc, int stall)
{
   MS_STALL_EVENT(CPUNUM(st), pc, pc, stall, 
                  st->active_thread->icache_stall_reason);

}

void
record_pipeline_stall(struct s_cpu_state *st, int pc, int stall)
{
   MS_STALL_EVENT(CPUNUM(st), pc, pc, stall, E_PIPELINE);
}
	/*
	 *  ldst_buffer_squash
	 */

void ldst_buffer_squash(struct s_cpu_state *st, int inum)

{
   struct s_ldst_buffer *entry = st->inum2ldst[inum];

   if (entry->ls->inum != inum) {
	fprintf(stderr, "retire failure in ldst_buffer_squash\r\n");
	ms_break (st, NULL, "ERR");
   }
   IncStat(ST_LDST_BUF_SQUASH);
   if (entry->dataPtr == NULL)
	IncStat(ST_LDST_BUF_SQUASH_ACT);
   st->iwin_flags[inum] &= ~IWIN_LDSTBUF;
   ldst_buffer_free (st, entry);
   return;
}


/*
 * MxsApproveImiss - Return TRUE if it is safe to process an instruction
 *		   cache miss to this address. This routine will disallow
 *		   any instruction cache miss that might invalidate an active
 *		   entry in the ldst buffer.
 */
int
MxsApproveImiss(struct s_cpu_state *st,int paddr)
{

   int sind;
   struct s_ldst_buffer *entry;

   sind = SCACHE_INDEXOF(paddr);
   for (entry = st->ldst_head; (entry); entry = entry->next)
	{
	if (SCACHE_INDEXOF(entry->paddr) != sind)
	 continue;  /* Skip if not an active entry at same Scache index */
	return 0;
   }
   return 1;
}


	/*
	 *  DoMxsIntervention  -  When a cache line is thrown out, notify
	 *			  the load/store buffer by calling this
	 *			  routine.
	 */
void
DoMxsIntervention(struct s_cpu_state *st, int paddr, int size, int writebackonly)
{
    struct s_ldst_buffer *entry;
    int paddr_end = paddr + size;

    for (entry = st->ldst_head; (entry); entry = entry->next)
	{
	if (entry->dataPtr == NULL) continue; /* Only a problem if we have
						 returned data */

	if ((entry->paddr >= paddr) && (entry->paddr < paddr_end)) {
	    if (!writebackonly || IsStore(entry->ls->lstype)) {
		entry->invalidated = 1;
	    }
	}
    }
}


	/*
	 *  CheckIntervention  -  Check if recycling the cache line might cause
	 *			  an intervention, and if so return TRUE.
	 *
	 *	When mtag is specified, stop the check when the entry
	 *	containing that miss tag is reached.
	 */

int CheckIntervention (struct s_cpu_state *st, void *mtag, int paddr, int size)
	{
	struct s_ldst_buffer *entry;
	int paddr_end = paddr + size;
	int	ignore_null = 0;

	for (entry = st->ldst_head; (entry); entry = entry->next)
		{
			/* After the first failed entry is encountered,	*/
			/* just need to worry about entries with non-	*/
			/* null data pointers.				*/
		if (entry->ls->status & LS_ST_FAILED) ignore_null = 1;
		if (ignore_null && (entry->dataPtr == NULL)) continue;

		if (mtag && (entry->missTag == mtag)) return (0);
		if ((entry->paddr >= paddr) && (entry->paddr < paddr_end))
			return (1);
		}
	return (0);
	}



	/*
	 *  GetMxsAction  -  Pass the saved action to caller
	 */

int GetMxsAction (struct s_cpu_state *st)
	{
	return (st->ms_action);
	}


	/*
	 *  DoMxsAction  -  Perform the requested action in response to
	 *		    a memory system request.
	 */

void DoMxsAction (struct s_cpu_state *st, void *missTag, int ms_action)
	{
	struct	s_ldst_buffer *entry, *next_entry = 0;

	switch (ms_action)
		{
		case ACT_UNSTALL_ICACHE:
			recurse_unstall (st, 0);
			return;
		case ACT_DCACHE_MISS:
			{
			for (entry = st->ldst_head; (entry); entry = next_entry)
			    {
			    next_entry = entry->next;
			    if (entry->missTag == missTag)
				{    /* Found the corresponding entry	*/
				     /* in the load/store buffer	*/
				struct s_lsq *ls = entry->ls;

				entry->missTag = (void *) -1;
				ls->status &= ~LS_ST_STALL;
				if (ls->status & LS_ST_DONE)
				    {	/* In this case, the operation	*/
					/* has been performed in the	*/
					/* load/store buffer already.	*/
				    Result ret;
				    char *dataPtr;

				     /* Update Mipsy PC so memstat knows 
                                      * where miss came from. */
				     ((CPUState *) (st->mipsyPtr))->PC = 
                                        st->iwin_pc[ls->inum];
				    if (IsLoad(ls->lstype))
					ret = DCacheFetchShared(CPUNUM(st),
					     ls->addr, ls->paddr,0, &dataPtr);
				    else
					ret = DCacheFetchExclusive(CPUNUM(st),
					     ls->addr, ls->paddr,0, &dataPtr);
				    if (ret != SUCCESS)
					{
					fprintf(stderr,
					  "Cache callback messup\r\n");
					ms_break (st, NULL, "ERR");
					}
				    entry->dataPtr = dataPtr;
				    }
				else
				    ls->status &= ~LS_ST_CACHED;
				}
			    }
			recurse_unstall (st, 0);  /* Just in case the icache
						   * miss merged with a dcache
			return;			   * miss */
			}
		default:
			return;
		}

	}

static int ms_lsop_uncached (struct s_cpu_state *st, struct s_lsq *ls)
	{
	uint	data_address;
	int	i;
        int err;
#undef WRITEBACKREG
#if UC_LATENCY > 1
#define WRITEBACKREG() \
	if (ls->reg == 0) \
    Add_to_worklist (st, UC_LATENCY, reg0_writeback,(void *)(ls->inum)); \
	else \
    Add_to_worklist (st, UC_LATENCY, reg_writeback,(void *)ls->reg);
#else
#define WRITEBACKREG() \
	if (ls->reg == 0) reg0_writeback(st,(void *)(ls->inum)); \
	else reg_writeback(st, (void *) ls->reg)
#endif

        err = 0;
	switch (ls->lstype)
		{
		case LD_UNSIGNED_B: {
                        unsigned char c;
                        err = DoUncachedRead(st,ls->addr, ls->paddr, 1, &c);
                        if (err == 0) { 
                           Ureg(ls->reg) = c;
                           WRITEBACKREG();
                        } 
			break;
                }
		case LD_UNSIGNED_H: {
                        unsigned short h;
                        err = DoUncachedRead(st,ls->addr,ls->paddr, 2, &h);
                        if (err == 0) { 
                           Ureg(ls->reg) = h;
                           WRITEBACKREG();
                        } 
			break;
                }
		case LD_UNSIGNED_L:
			{
			int shft = ((int)ls->paddr & 0x03) * 8;
			data_address = ((uint)ls->paddr&0xfffffffc);
                        err = DoUncachedRead(st,ls->addr,data_address, 4, &i);
                        if (err == 0) { 
                           Ureg(ls->reg) = Ureg(ls->reg2) &
						(~(0xffffffff << shft));
                           Ureg(ls->reg) |= i << shft;
                           WRITEBACKREG();
                        }
			break;
			}

		case LD_UNSIGNED_R:
			{
			int shft = (3 - ((int)ls->paddr & 0x03)) * 8;
			int mask = 0xffffff00 << (24 - shft);
			data_address = ((uint)ls->paddr&0xfffffffc);
                        err = DoUncachedRead(st,ls->addr,data_address, 4, &i);
                        if (err == 0) { 
                           Ureg(ls->reg) = Ureg(ls->reg2) & mask;
                           Ureg(ls->reg) |= (i >> shft) & (~mask);
                           WRITEBACKREG();
                        }
			break;
			}

		case LD_INTEGER_B: {
                        signed char b;
                        err = DoUncachedRead(st,ls->addr,ls->paddr, 1, &b);
                        if (err == 0) { 
                           Ireg(ls->reg) = b;
                           WRITEBACKREG();
                        } 
			break;
                }
		case LD_INTEGER_H: {
                        short h;
                        err = DoUncachedRead(st,ls->addr,ls->paddr, 2, &h);
                        if (err == 0) { 
                           Ireg(ls->reg) = h;
                           WRITEBACKREG();
                        } 
			break;
                }
		case LD_INTEGER_W:
			if (ls->addr & 1) {
			    int retval;
			    int addr = ls->addr & ~1;

			    /* Turn the lw t0, 1(a0) instruction into a conditional
			     * store */
			    if ((*(int *)addr) & 1) {
				retval = 0;
			    }  else {
				(*(int *)addr) = (int) 1;
				retval = 1;
			    }
			    Ireg(ls->reg) = retval;
			    WRITEBACKREG();
                            err = 0;
			    break;
			}
                        err = DoUncachedRead(st,ls->addr,ls->paddr, 4, &i);

                        if (err == 0) { 
                           Ireg(ls->reg) = i;
                           WRITEBACKREG();
                        } 
			break;

		case LD_FLOAT: {
                        float f;
                        err = DoUncachedRead(st,ls->addr,ls->paddr, 4, &f);
                        if (err == 0) { 
                           Freg(ls->reg) = f;
                           WRITEBACKREG();
                        } 
			break;
                }
		case LD_DOUBLE: {
                        double d;
                        err = DoUncachedRead(st,ls->addr,ls->paddr, 8, &d);
                        if (err == 0) { 
                           Dreg(ls->reg) = d;
                           WRITEBACKREG();
                        } 
			break;
                }
		case ST_INTEGER_B: {
                        char b = (char) Ireg(ls->reg);
                        err = DoUncachedWrite(st,ls->addr,ls->paddr, 1, 0, &b);
			break;
                }
                case ST_INTEGER_H: {
                        short h = (short) Ireg(ls->reg);
                        err = DoUncachedWrite(st,ls->addr,ls->paddr, 2, 0, &h);
			break;
                }

		case ST_INTEGER_W:
                        err = DoUncachedWrite(st,ls->addr,ls->paddr, 4, 0, &Ireg(ls->reg));
			break;

		case ST_FLOAT:
                        err = DoUncachedWrite(st,ls->addr,ls->paddr, 4, 0, &Freg(ls->reg));
			break;

		case ST_DOUBLE:
                        err = DoUncachedWrite(st,ls->addr,ls->paddr, 8, 0, &Dreg(ls->reg));
			break;

		case ST_INTEGER_L:
		case ST_INTEGER_R:
                   /* Can't handle these yet */
		default:
			fprintf(stderr,
				"Bad lstype passed to ms_lsop_uncached\r\n");
			ms_break (st, NULL, "ERR");
			break;
		}

        if (err != 0) {
           return err;
        }
	if (IsStore(ls->lstype)) {
	    IncStat(ST_LSOP_UNCACHED_STORE);
#ifdef PRECISE
#ifdef DEBUG_CHECKS
	    if (st->iwin_flags[ls->inum] & IWIN_FREED)
		{
		fprintf(stderr, "Freed store instruction being executed!\r\n");
		ms_break (st, NULL, "ERR");
		}
#endif /* DEBUG_CHECKS */
#endif
	    free_inst(st, ls->inum);
	} else
	    IncStat(ST_LSOP_UNCACHED_LOAD);
        return err;
	}



	/*
	 *  recurse_unstall  -  Unstall the fetch unit after a cache miss
	 *			has been satisfied.
	 *
	 *	Return TRUE if a thread was unstalled.
	 */

static int recurse_unstall (struct s_cpu_state *st, int br_node)
	{
	BrTREE	*br;
	THREAD	*th;
	int ret = 0;
	if (br_node < 0) return (0);

	br = &st->branch_tree[br_node];
	th = &st->threads[br->thread];
	if (th->stall_icache)
		{
		th->stall_icache = 0;
		UpdateStallFetch (th);
		ret = 1;
		}

	return (ret + recurse_unstall (st, br->lchild) +
			recurse_unstall (st, br->rchild));
	}


#ifndef MIPSY_MXS
	/*
	 *  is_misaligned  -  Return TRUE if the load/store instruction
	 *			will cause an alignment error.
	 */

static int is_misaligned (int lstype, int addr)
	{
	switch (lstype)
		{
		case LD_UNSIGNED_B:
			return (0);

		case LD_UNSIGNED_H:
			return (addr & 0x01);

		case LD_UNSIGNED_L:
			return (0);

		case LD_UNSIGNED_R:
			return (0);

		case LD_INTEGER_B:
			return (0);

		case LD_INTEGER_H:
			return (addr & 0x01);

		case LD_INTEGER_W:
			return (addr & 0x03 ? 1 : 0);

		case LD_FLOAT:
			return (addr & 0x03 ? 1 : 0);

		case LD_DOUBLE:
			return (addr & 0x07 ? 1 : 0);

		case ST_INTEGER_B:
			return (0);

		case ST_INTEGER_H:
			return (addr & 0x01);

		case ST_INTEGER_W:
			return (addr & 0x03 ? 1 : 0);

		case ST_INTEGER_L:
			return (0);

		case ST_INTEGER_R:
			return (0);

		case ST_FLOAT:
			return (addr & 0x03 ? 1 : 0);

		case ST_DOUBLE:
			return (addr & 0x07 ? 1 : 0);
                default:
                        break;
		}
	return (1);
	}
#endif /* !MIPSY_MXS */