USA Income and Taxes:

To look at the US economy, we have to first look at the total income of all the people living here, as well as their wealth.

US income/wealth percentile:

This data is for around 2020-2025. 

There's a neat income percentile on this link: https://dqydj.com/income-percentile-calculator/

There's also a wealth percentile here: https://dqydj.com/millionaires-in-america/

We saw in other USA sections, that there are about 130M households and about 150M working population in USA. Income percentile refers to percentile in these 150M workers, i.e top 5% means in the top 7.5M earners. Wealth percentile refers to these 130M households, i.e top 10% means you are in the top 13M households. Wealthier households are usually middle age or old age household as it takes 20 years or more to accumulate enough wealth to get to top percentile with a day job.

  • 0%-70%: We see that every $1K in income gets you 1 percentile higher upto $70K in income. That means if you have $1K in income, you are in bottom 1%, while if your income is $70K, you are in 70 percentile. So, 0% to 70% goes up linearly with income. After this starts slight non linear curve.
  • 71%-80%: Here, income goes up by $2K for every 1%. 70% starts at $70K in income, and 80% ends at $92K in income.
  • 81%-90%: Here, income goes up by $4K for every 1%. 80% starts at $92K in income, and 90% ends at $132K in income.
  • 90%-95%: Here income start going exponentially higher, and you need to make $10K more to get up by 1%. Income start at $132K and end at $186K. About 15M or 12% households are millionaires, meaning most of them are in this income group starting at 90% and above.
  • 96%-99%. Here income go even more exponentially higher, and to make it to the top end of this income group, you need to make at least $500K. About 5M or 4% households have $4M or more in assets, and they are usually earners in this group.
  • Top 1%: These are the richest people, who not only make > $500K/yr, but also have wealth > $10M. About 1M households have wealth > $10M, with 100K households having wealth > $50M, and 30K households having assets > $100M.  These 30K huseholds are who's who in America, and are generally well known. This is the chart showing top 1% of households income wise per state. As can be seen, in richer states as Connecticut, Masschusetts, California, New york, your household needs to being in close to $1M, while in poorer states as West Virginia, Mississippi, New Mexico, etc, just $0.5M will get you there.

Bottom 90% of the people are so poor they they live and die as slaves. More on this in another article. "For the bottom 60% of U.S. households, a "minimal quality of life" is out of reach, according to the group, a research organization focused on improving lower earners' economic well-being. " Link => https://www.cbsnews.com/news/cost-of-living-income-quality-of-life

 

 


 

IRS income data

A lot of tax and income data available on IRS website. We want to just get an idea of how income of the population lines up.

IRS data taken for years, 2017, 2011 and 2007 (taken from here: https://www.irs.gov/statistics):

Types of returns filed: 3 major kind of tax returns filed, which account for 80% of tax revenue:

A. Individual tax returns: Filed by individuals like you and me. These are the 1040 forms filed. I'm showing data for 3 years: 2007, 2011 and 2017.

2017:

Selected data pulled from IRS stats file here: https://www.irs.gov/pub/irs-soi/17in14ar.xls

This is the 1040 form which the above table refers to: https://www.irs.gov/pub/irs-prior/f1040--2017.pdf

152M individual tax returns (form 1040)  filed.  73M were returns of single persons, 55M were returns of married filing jointly and 22M were returns of heads of households, and 3M were married filing separately.

AGI (line 37 in form 1040): AGI is Adjusted gross income and is line 37 in form 1040. Total AGI was $11.1T.

Number of returns with AGI > $500K was 1.5M or about 1% of total returns. 1M of these returns had AGI < $1M. So, once you start hitting AGI of $1M, you get in top 0.3% of population. Total cumulative AGI for this group is $2.3T (avg AGI = $1.5M). Only $0.9T of this income came from wages/salaries. $0.6T came from sale of capital assets, $0.1T from dividends, $0.6T from partnerships and S corps. In fact, as you go higher up in AGI, higher percentage of income comes from passive assets and not from wages (for people with AGI > $10M, income from assets is 6X their wages. So, decline in price of stocks/equities will hurt this top 1% very disproportionately as 25% of their AGI will evaporate in absence of capital gain (as they can't sell stocks for profit when markets are down for the year).

Number of returns with $200K < AGI < $500K were 6M or 4% of returns. So, an AGI of $200K or more places you in top 5% of working US population. Total cumulative AGI for this group is $1.8T (avg AGI = $300K).

Number of returns with $100K < AGI < $200K were 20M or 14% of returns. So, an AGI of $100K or more places you in top 20% of working US population. Total AGI for this group is $2.7T (avg AGI = $130K).

Number of returns with AGI < $100K: Remaining 125M returns fell in this category. This represents bottom 80% of the population. These are the poorest people with very little savings, and little assets. Their combined AGI is $4.5T (avg AGI = $36K), which is about half of tthe total AGI of entire US workforce. Their AGI is mostly comprised of wages, and SS/pensions. 80% of AGI, or about $3.5T comes from wages. Remaining $0.5T comes from retirement accounts, and $0.5T from Social security benefits. So, most of the retired people fall in this category. Also, most of the youngest workers under 26 years of age fall here too, as they are just starting. 2M returns are for people under 18 years of age, totalling $0.01T while 23M returns are for people between age of 18 and 26 years of age, totaling $0.45T. So, 25M returns are from people under 26 years of age, who mostly fall in AGI < $100K. When you hear about unemployment in news, this is the group that is most vulnerable, and comprises of most unemployed people. This bottom 80% drives the so called economy, as whatever they make gets spent, with negligible savings. About 50M people have non taxable returns, meaning they owe $0 in federal income taxes. Looking at table about 54M returns had AGI < $25K (or income below poverty line for a family of 4, with 2 kids and 2 adults. For an individual, poverty threshold is at $13K), so most likely these are the people having non taxable returns, since std deduction and many other credits can wipe out taxes. US census.gov website shows 11% of people below poverty line, while here we see 35% of population with AGI below $25K. Reason is many individuals filing return as single person are above poverty line, since poverty threshold for households with single person is at $13K.

Social security website shows that about 6% of returns filed have earnings over maximum taxable limit for social security for each year since 1980. Congress adjusts the Social Security limit every year to keep this 6% number constant. For 2017, the limit was $127K. This limit has doubled from $68K in 1998 to $138K in 2020, implying that wages/salaries for top 6% are rising at an annual rate of 3.5%.

https://www.ssa.gov/policy/docs/population-profiles/tax-max-earners.html

Bottomline: So, to be in top 5% and to stay there, you need wages > $130K and AGI > $200K for year 2017, and both of these need to go up by about 4% every year starting from year 2017. Also, you need to have extra income from dividends, interest, stocks sale, comprising at least 1/2 of what you get in wages/salaries. So, that way your AGI comes out to 150% of your wages.

AGI componenets: Various components of AGI are as follows:

wages/salaries (line 7 in form 1040) = $7.6T (126M returns), So, that leaves 26M returns (since total returns=152M) with no wages/salaries, which is mostly older retired people and disabled people. 29M returns had Social security benefits amounting to $0.6T. 15M returns had IRA distributions totaling $0.3T. These are mostly older people not in workforce. This implies that 26M returns are filed by people not employed (i.e old people living off social security, disabled people, etc). Looking at other table, we see that 26M returns were for people 65 and older, with collective AGI of $2.1T, out of which only 9M had salaries and wages totaling $0.5T.

interest (line 8 in form 1040) = 0.16T : taxable interest = $0.1T (44M returns), tax exempt interest = $0.06 (6M returns), => about 35% of returns have money earned thru bank account CD, savings a/c, etc. So, 2/3 of workforce don't even have bank accounts or don't earn any interest. Of the bottom 80% with AGI < $100K, 26M had taxable interest income totaling $0.025T. So, 80% of the interest income is attributed to top 20% of people, since they have savings. However, total deposits in US banks is over $10T, so $0.1T interest implies 1% interest rate, which is the highest rate banks offered in the last decade. So, basically income from interest has evaporated.

dividends (line 9 in form 1040) = 0.28T : ordinary dividends = $0.28T (28M returns), qualified dividends = 0.22T (26M returns), => about 20% of all returns have money earned thru stocks, since qualified dividends mostly comes thru stocks. That's a very high percentage of people invested in stock market thru their personal ccounts, considering that only 35% have bank accounts. Of the bottom 80% with AGI < $100K, 16M had ordinary dividend income totaling $0.05T. Here, just as in interest income above, 80% of the dividend income is attributed to top 20% of people. People with AGI in range $100K < AGI < $500K made about $0.1T in dividend. Assuming most of the dividend comes from US equities, it implies, that top 20% of the people have a lot of money invested in equities/stocks. Looks like all of America owns equities, with top 1% owning 50% of equities. Also, let's assume that $500B paid in dividends was paid by US companies in 2017 (no reliable source found for this but that's the range dividends have been around 2015-2020 ? FIXME?). Since ordinary dividends are $0.28T, then this implies that about 50% of US equities are owned by people in their personal accounts, while remaining 50% is held in their retirement accounts. Note that most of the equities held in personal accounts is for people in top 10%.

sale of capital assets (line 13 in form 1040, schedule D) = $0.8T (17M returns), Of these, about 6M returns filed for net cummulative loss of $13B. Reason for such low net loss is because losses are limited to $3K/return, so net loss could not exceed 6M*$3K = $18B in any given year. Again, people with AGI <$100K (or bottom 80%) of people had just $0.05T in cummulative capital gain, which is just 5% of all capital gains. Top 1% had capital gains totaling $0.6T or 75% of all capital gain. Sale of capital assets include house, stocks, and many other assets. Looking at tables on irs website for "capital gain/loss by asset type for year 2012" didn't give a clear picture on how much of this is from stocks and how much from other assets.

partnership and S corp income (line 17 in form 1040) = $0.7T (7M returns). Many individuals also form partnerships to open corporations. The profit from these corporations pass thru their individual income tax return,

rental and royalty income (line 17 in form 1040) = $0.05T (11M returns)

Of 26M returns that didn't have wages (152M-126M), most of them are either old/disabled people collecting SS/IRA/401K, or people with their own business

business income (line 12 in form 1040) = $0.35T (26M returns). Surprising that 20% of workforce have their pwn business. Probably a lot of mom/pop shops or franchises. What's interesting is that about 6.5M or 25% of these business returns had net loss. If that's true then a lot of these individual business would go bankrupt every year.

pension/annuities (line 16 in form 1040) = $1.2T (30M returns). distributions from 401K plan are reported under this on line 16a, 16b of form 1040. 18M returns were from people over age of 65, totaling $0.7T, while 6M returns were from people between age of 55 to 65, totaling $0.3T. As expected, 80% of this income is attributable to people in retirement.

taxable IRA distributions (line 15 in form 1040) =$0.26T (12M returns), here distributions from personal IRA accounts (not thru work) are reported on line 15a, 15b of form 1040. 11M returns were from people over age of 65, totaling $0.21T, while 2M returns were from people between age of 55 to 65, totaling $0.05T. As expected, 80% of this income is attributable to people in retirement.

Social security benefits (line 20 in form 1040) = $0.6T (29M returns),

unemployment compensation (line 19 in form 1040) = 0.1T (13M returns) and others were misc items.

Of this AGI, 100M returns applied for std deduction of $0.75T, while 45M returns applied for itemized deduction of $1.2T. So, Taxable income was $5.7T after accounting for deductions, exemptions, tax credits, etc. IRS collected a tax of $1.05T (~20% of taxable income).

2011: 145M individual tax returns (form 1040)  filed.  67M were returns of single persons, 53M were returns of married filing jointly and 22M were returns of heads of households, and 3M were married filing separately.

Total AGI was $8.4T. Various components of this are:

wages = $6.0T (120M returns),

interest = 0.2T : taxable interest = $0.12T (52M returns), tax exempt interest = $0.07T (6M returns), => about 35% of returns have money earned thru bank account CD, savings a/c, etc.

dividends = 0.35T : ordinary dividends = $0.2T (28M returns), qualified dividends = 0.15T (25M returns), => about 20% of returns have money earned thru stocks

capital gains = $0.4T (22M returns),

partnership and S corp income = $0.4T (8M returns),

rental and royalty income = $0.05T (11M returns)

Of 25M returns that didn't have wages (145M-120M), most of them are either old/disabled people collecting SS/IRA, or people with their own business

business income = $0.3T (23M returns),

pension/annuities= $0.6T (27M returns),

Social security benefits = $0.2T (17M returns),

taxable IRA distributions=$0.2T (13M returns)

unemployment compensation = 0.1T (13M returns) and others were misc items.

Of this AGI, 100M returns applied for std deduction of $0.75T, while 45M returns applied for itemized deduction of $1.2T. So, Taxable income was $5.7T after accounting for deductions, exemptions, tax credits, etc. IRS collected a tax of $1.05T (~20% of taxable income).

2007: 142M individual tax returns (form 1040)  filed.  Total AGI was $8.7T. Of this wages = $5.8T, capital gains = $0.9T, pension/annuities= $0.5T, partnership and S corp income = $0.4T,  business income = $o.3T, taxable interest = $0.25T, ordinary dividends = $0.25T, Social security benefits = $0.2T, taxable IRA distributions=$0.2T and others were misc items. Taxable income was $6T after accounting for deductions, tax credits, etc. IRS collected a tax of $1.1T (~20% of taxable income).

B. Employment tax return: This tax return has to be filed by employers to pay social security and medicare tax to the government. Note that each employed person in USA is supposed to pay 12.4% SS tax and 2.9% Medicare tax. If you are self employed, you pay all of this, but if you are employed by someone, you pay one half and your employer pays the other half. Most of the people are employed by someone else, so their employer files tax form 940, and pays this tax to IRS. We as employees never see this tax return being filed, as money is already taken out of our paycheck by the employer. Employer then files this separate tax return paying his share (6.2%+1.45%)as well as paying the money that he took out of our paycheck (6.2%+1.45%). Self employed people file this return themselves. There were 30M such tax returns filed for 2007. Employers don't file separate tax return for each employee, but combine it in one for all their employees. So, this tells us that there were 15M employers (as 15M are self employed people. If we assume that each employed person paid about $6K in SS+Medicare tax (assuming median income of $50K), then for 150M employees (for 2008, workforce was about 155M), that would be about $1T in employment taxes.

C. Corporations: 7M corporate tax returns were filed for 2008.

D. Partnership: 3M partnership returns were filed for 2008.

There are 2 kinds of business: unincorporated and incorporated.

A. Unincorporated business: The revenue, expenses and income of business flows thru personal tax return. It has unlimited liability, is unable to defer taxes, and must file income taxes based on calender year. It's simple to set up these kind of business, and they have no payroll. These business are also called non employer or self-employed firms, and are genrally excluded from all business statistics from census bureau, as they account for only 3% of all business receipts in USA.

UnInc business, are further subdivided into 3 types as per IRS classifications:

1. Sole propertierships: someone who owns an unincorporated business by himself or herself. Generally non-farm sole propertiership are included in all stats.

2. Partership: relationship existing between two or more persons who join to carry on a trade or business. Each person contributes money, property, labor or skill, and expects to share in the profits and losses of the business. Partners may be individuals, corporations, other partnerships, tax-exempt organizations, nominees, or other legal entities. Partners are not employees and should not be issued a Form W-2. The partnership must furnish copies of Schedule K-1 (Form 1065) to the partners.

3. S-corporations: These are an alternative to Inc corp, but they should have no more than 100 shareholders, and have one class of stock. S corporations avoid double taxation on the corporate income, as income is passed thru tax on individual's income tax return.

B. Incorporated business: Usually represented by Inc at the name of business. The revenue, expenses and income of business flows thru corporate tax return, at corporate tax rates (usually 35%). The profit is again taxed when it's distributed to shareholders as dividends. It has unlimited liability, credit proofs its shareholders, is able to defer taxes, and file income taxes based on their own fiscal year which may or may not coincide with the calender year. It's complex and expensive to setup these kind of business, and it has payroll. There is only 1 type of Inc business, known as C corp.

LLC: A Limited Liability Company (LLC) is a business structure allowed by state statute. LLCs are popular because, similar to a corporation, owners have limited personal liability for the debts and actions of the LLC. Other features of LLCs are more like a partnership, providing management flexibility and the benefit of pass-through taxation. The federal government does not recognize an LLC as a classification for federal tax purposes. An LLC business entity must file a corporation (C-corp or S-corp), partnership or sole proprietorship tax return.

Note that sole proprietorship, partners, S-corp are included above in individual income tax returns as the income from these flow through individual tax returns of owners and not through corporate tax on business (as in C-corp) itself.

Workforce

Workforce is comprised of people hired by government or by private companies. Total population of USA is 315M, of which 50% don't go for work. Of this 160M who don't go for work, 45M are elderly (65+), 75M are kids (<18) and 50M are stay at home mom/dads and disabled people. Remaining 155M want to work and are either working or looking for work. They are called the civilian workforce (Total number of people in the non-civilian workforce is 1.5M who are hired by the military). Since unemployment or underemployment is about 10%, we can assume that only 140M people are employed. Lets break this number further down:

I. Federal government: employs about 2M civilian workforce (on top of 1.5M military workforce), and is the largest employer in USA. It excludes postal service employees of usps. avg salary is about $100K.

II. State and local government: employs about 17M civilian workforce. 7M are employed in schools as teachers and support staff, 2.5M in protective services (including police officers, fire fighters, and correctional officers), 2.0M in higher education, 1.4M in health care (including nurses and other workers at public hospitals and clinics), 1M in libraries, housing, others, 0.8M in transportation (including road maintenance workers and bus drivers) and remaining 2M in all other professions. Link is:   http://www.cbpp.org/cms/index.cfm?fa=view&id=3410. about 35% of state and local government spending is on wages of these employees, excluding healthcare and retirement benefits. Including these benefits, about 45% of the spending is on wages.

so, about 15% of the civilian workforce is employed by Government (public employees), while remaining 85% is employed by private companies or are self employed. About 15M (10% of civilian workforce) is self employed, with 10M unincorporated self-employed (often work by themselves without employees) and 5M incorporated self-employed (often have employees).

In year 2007, there were total of 27M private non-farm business in USA, out of which 21M were unincorporated businesses (no employees), and 6M incorporated business (with employees). UnInc business brought in $1T in business sale receipts, while Inc business brought in $30T in sales receipts. This is the link from census bureau: http://www.census.gov/econ/smallbus.html

These 21M business which had no employees include the self-employed, freelancers, independent contractors, sole proprietorships, family-owned businesses, LLCs, corporations, S-corporations or partnerships. So, the number of unincorporated self employed above implies that out of 21M businesses with no employees, only 10M are actually a day to day business with an owner. The remaining 11M are just corporations, partnerships built for tax/liability advantages.

Ap per IRS data from 2007 (http://www.irs.gov/pub/irs-soi/07ot3naics.xls), there were 32M business returns filed (instead of 27M that's expected). These had business receipts of $30T, with net income of $3T. Of these, 23M returns were for no-farm sole proprietorship, which had $1T in business receipts and 0.3T in net income. 3M returns were filed for partnerships (Form 1065). The number of partners on these returns was 18M. Most of these partnerships are in Finance, Insurance, Real estate, construction, retail and other services. Net revenue from these partnerships was $5.9T (business receipts = $3.9T, portfolio income from interest/dividend/capital_gains=$1T, others = $1T) while net income was $0.7T ($0.3T was ordinary income while $0.4T was from dividends, interest, royalties, rental income, etc). total assets of these partnerships was $20T. About 6M corporations in USA (includes both C-corp and S-corp), but 4M of these are are S-corp or other pass through entities (like REITs, RICs, etc) which pay no corporate tax. These S-corp had business receipts of $6T, with net income of $0.4T. About 2M were C-corp, for which corporate income taxes were filed, which paid tax of $0.3T on taxable income of $1T (real income was $1.5T, but taxable income came down with deductions/credits. corporate tax rate is 35%). Total assets was $82T. Total revenue was $22T, of which business receipts were $18T. Total revenue is more than GDP as corporations sell not only to consumers, but also to each other.

So, if we take out these 15M self employed from the civilian workforce, we are left with 120M people, or about 75% of the workforce. This 75% of the workforce is hired equally by small business (Business having less than 500 employees) and large business (Business having more than 500 employees). Of the 6M businesses with employees, only 20K businesses had more than 500 employees.   These small businesses employed about 60M people, while large businesses (about 20K in number) employed the remaining 60M ppl. So, these small businesses had about 10 employees on avg, while large businesses had about 3K employees on avg. Large business are usually all public or large private companies, which have chains throughout the country. Small business are the ones run by a owner, hire few employees, and typically include all mom-pop stores that you see around, gas-stations, restaurants, motels, personal care, house maintenance contractors, or small internet companies.

Among the large businesses, Walmart is the largest private employer at 1.8M employees, followed by Mcdonalds and UPS each at 0.5M employees. USPS employes about 0.6M people and is the second largest cvilian employer behind walmart, but is generally not counted as a private employer. It has the backing of federal government. The top 50 largest employers, excluding usps, employed about 12M people. link: http://nyjobsource.com/largestemployers.html . Most of these companies are retail companies, and as such the salaries of majority of their employees are < $50K/year.

Employment taxes (FICA and medicare tax from both employees and employers): Total tax of $0.9T was paid to IRS.

Total money earned by individuals, corporations and Government:

individuals income=$7.6T(money kept in pocket) + $1.1T (federal tax) + $1.4T (approx state/local tax incl property taxes, as state/local tax for 2012 was $1.4T) + $0.9T( assumed contributions of 10% to IRAs, health insurance premiums,etc which are excluded from AGI)=$11T.

Corporations income=$1.1T(money kept in corp reserves) + $0.3T (federal tax) + $0.1T(dividends paid).

Federal Government income: total taxes collected by IRS = $2.5T amounting to 18% of GDP.  individual income tax=$1.1T, Corporate income tax=$0.3T, Employment tax=$0.9T, Excise/gift/estate tax=$0.1T, others=$0.1T.

sum total of all money = $11T(individual income) + $1.5T(corporate income)+ $1T(federal government income from Employment tax + other tax, which weren't included in individual or corporate income) = $13.5T (while GDP was $14T)

individual expense:

corporate expense:

Federal Government Expenses: total = $3T. defense = $0.7T, healthcare = $0.7T($0.4T to medicare for 45M elderly/disabled and $0.3T to medicaid/CHIP for 60M low income people),   pensions=$0.7T(mostly Social security avg check of $1.2K/month to 35M retired workers and 10M spouses/children of retired/dead workers, and 10M disabled workers), welfare=$0.4T (unemployment assistance, food stamps, low income assistance,etc) interest=$0.2T(on debt of $9T) , education=$0.1T, transportation=$0.1T and others=$0.1T.

GDP:

USA nominal GDP was approx $?T in 2011 ($14T in year 2007).

GDP components: GDP can be measured using income approach (sum total of income of all individuals living in a country during 1 year) or using production approach (Market value of all final goods and services calculated during 1 year) or using expenditure approach (All expenditure incurred by individual during 1 year ). Mostly expenditure approach is used, components of which are shown below:

A. personal consumption: = 70% of GDP. money spent by individuals on various items and services.

1. services: 40% of GDP. The two largest components are real estate (10%) and health care (12%)

2. non-durable goods: 20% of GDP. The three largest components are food (10%), clothing (2.7%) and fuel (2.4%)

3. durable goods: 8% of GDP. autos (3.6%) and furniture (3%).

B. Private/Business investment: 16% of GDP

1. Non-residential: 12%

2. Residential: 4%

C. Government investment: 19% of GDP

1. Federal Govt: 7% of GDP. defense spending=5%, non-defense spending=2%

2. state and local Govt: 12% of GDP

D. Import/export: -5% of GDP.

1. exports: 12% of GDP, with goods being 9% of GDP and services comprising 3%.

2. imports: 17% of GDP

 

 

 

Population:

Population in USA is about 310 Million as per 2010 census. ( https://www.census.gov/data/tables/time-series/demo/families/households.html ) There are 113 Million households (house, apartment, or any separate living quarter in which any number of people can live). 230M people are white (of these 50M people are of hispanic origin), 42M are black/african, 15M Asian (Chinese=3.5M, Indians=2.9M, Filipino=2.6M, Vietnamese=1.7M, Korean=1.5M) and 3M American Indians. Note that these refer to origin or race, not citizenship. So, a baby born to chinese citizens here will be a considered of chinese race, even though he and his parents are citizens here. About 60M are households with married couples, while 20M are other family households. About 40M are non-family households (people from different families sharing an apartment, or a person living by himself in a dorm, apt etc but who doesn't consider himself a family. Ex: kids in dorms, unmarried people with new jobs sharing apt, etc). Number of households increase by nearly 1M per year.

Population wise, California, Texas and Florida have been top 3 states (all with relatively warmer weather than rest of the country, so they have also been top states for retirement). 25% of US population lives in these 3 states.

Population growth:

world population is growing by 1.2% yearly as of 2010 (2.2% is birth rate, while 1% is death rate, resulting in net growth rate of 1.2%). India=1.4%, USA=0.8%, China=0.5% population growth. In year 2016 in USA, there were 4M births a year (40% to unmarried women) and 2.7M deaths (0.6M due to heart disease, 0.6M due to cancer, 0.2M accidents, life expectancy=79 years) resulting in net birth rate of 1.3M or 0.4% per year. Net immigration is 1.2M or 0.4% per year (green card=0.5M, other visas/illegal=0.7M), which causes US population to grow by 0.8% (2.5M people per year). By 2058, US population is expected to cross 400M. Population growth is expected to slow down to 1.5M people per year (0.4M will be natural increase, while 1.1M would be due to net immigration). So, net immigration is going to be the driving force for population growth in USA. If not for population growth from immigration, GDP growth would be less than half of what it is. So, USA does need people from other countries to keep on coming in for it's own good.

One noteworthy data from deaths based on age shows that 80% of the deaths occurred due to age age. For 2017, 0.9M deaths occurred at age of 85+. Other 0.3M for ages 80-84. In fact, 2.1M or 75% of the deaths occurred for people aged 65+, Since 60M people are age > 60 yrs in US, this implies that 5% of the people in this age group die every year. Deaths in age groups < 60 years is less than 0.2%.

https://www.cdc.gov/nchs/data/nvsr/nvsr68/nvsr68_09-508.pdf

Population growth via immigration based on country of birth.

This link gives important stats for year 2009: https://www.migrationpolicy.org/article/frequently-requested-statistics-immigrants-and-immigration-united-states-1/. US immigrant (foreign born irrespective of current citizenship) population as of 2009 was 39M (mexicans=10M, Phillipines=1.8M, India=1.7M, China=1.6M, Vietnam=1.2M). It increases by 0.5M or 1.5% every year (In 2016, it was 44M). In 2016, 1.5M new immigrants came to USA (Indians=180K, Chinese=160K, Mexicans=150K, Filipino=50K). Till 1970, there were only 10M immigrants, but started increasing rapidly since then, because immigration related strict laws (immigration and nationality act) were amended to more favorable laws in 1965 (immigration capped to 0.3M per year), particularly for immigration from Asia. Of the 39M immigrants, 17M became US citizens, 13M permanent residents, 11M unauthorized immigrants (not sure?) and remainder on various visas. Of these 39M, 90% of them came to USA on business/tourist visa, while 2M came on work-visa, while 1M on student visa.  Nearly 6M visa were issued in 2009, of which 4M were business/tourist visa, 0.4M were student visa(F-1, F-2, F-3) and their family, and 0.4M were exchange visitors (J-1, J-2) and their families. As can be seen that most of the people who came to USA on visa went back, since immigrant population on average goes up by 1M every year. About 0.5M new immigrants come thru green card and others thru illegal immigration, so only very few of these 6M people actually stay in US permananetly. These 39M immigrants make up 26M households. In 2009, nearly 1.2M immigrants become permananet residents of which 0.7M are existing immigrants, while 0.5M are new arrivals to USA. Also, 0.8M immigrants became US citizens (Mexico=112K, India=52K, Phillipines=39K, China=37K, Vietnam=31K)

Asian Indians in USA:

basically 1 out of 100 people is USA is an Asian Indian (defined as one of Indian origin => He could either be one who is born here, but has ancestors from India, or one who has been born in Indian, but is currently in USA).

Following wikipedia link gives useful info: http://en.wikipedia.org/wiki/Indian_American#Statistics_on_Indians_in_the_U.S.

Of these 2.9 million indians in USA, 1.7M are India born who either became naturalized citizens, permanent residents (0.5M) or just temporary residents on various student or work visas. Remaining 1.2M are USA born, who are USA citizens by birth.

As of 2012, There were total 13.3M permanent residents in USA, of which 0.5M were Indians (Mexico=3.3M, China=0.6M, Phillipines=0.6M). Also, there were 0.4M H1B Visa holders in USA of which 0.3M were Indians (China=50K, no ther country more than 5K)

Cities:

There are only 10 cities in USA with population > 1M (i.e 10 lakh people). In contrast, there are 50 cities in India with population > 1M

Link: https://en.wikipedia.org/wiki/List_of_United_States_cities_by_population

1M + : 10 cities

400K+ : 50 cities

200K+: 100 cities

100K+: 300 cities (of these about 70 are from california, 40 from texas and 20 from Florida. None of other states have more than 10 cities in top 300 cities)

About 50M lived in top 50 cities. Cities by themselves do not give an idea of size of that area, as any city area is fixed, so as people start moving into the city, the city expands, but the expanded areas become new cities. As the original city itself doesn't grow in area, it's population kind of maxes out after a while, and adjoining cities keep on taking extra influx of people. A better stat is MSA (metropolitan statistical area). It gives better picture how the city population and area is growing. Link below shows that just 5 MSA comprised of 50M people. Top 15 MSA comprised of over 100M people, while more than half of US population is in top 35 MSA. About 70% of population is in top 100 MSA, and 85% in all 383 MSA.

Link: https://en.wikipedia.org/wiki/List_of_metropolitan_statistical_areas

Historically, New York, Los Angeles and Chicago has always been the top 3 most populated cities as well as MSA in USA. These MSA comprise about 15% of USA population (while these top 3 cities comprise only about 5% of population, so we can see that MSA are more relevant). Area wise also these are huge MSA. Since top 100 MSA are where most of the high paying jobs and industries are, these are essentially where people mostly live. These are the MSA where housing prices matter, since these area are restricted by available space to build housing which is close by to work (within 30 min of commute time). Outside of these 100 MSA, house prices should pretty much be same as the cost to build house, since there's abundant land. Land as a percentage of house sale price is less than 10% for MSA which are not in top 100.

Employment:

Population and employment figures from year 2010: (taken from www.census.gov, and www.bls.gov)

USA population is 310M, out of which 155M are in the civilian non-farm workforce (1.5M are in military workforce, while 3M farm for a living. If we include total workforce is 160M). About 10M out of this civilian workforce is unemployed, so 145M people are employed. Simple calculation for population is that there are roughly 20M people in each age group of 5 year increment, starting from 0yrs upto the age of 60 yrs. So, population in 0-60yrs is 20M*60/5=240M ppl (In reality, it's 250M). Remaining 60M are over the age of 60 yrs. Population breakup by age is:

< 5yrs: 20M. Out of these, 6M goto some form of preschool.

5-17yrs: 54M. Out of these 55M are enrolled in kindergarten, elementary or high school (50M in public and 5M in private). So, all of the population is getting primary education. That is reflected in the workforce number, where 87% of the workforce has a high school degree. That implies, about 10% of these 55M kids drop out of high school or even before, without getting a high school degree. More details here: https://nces.ed.gov/programs/digest/d17/tables/dt17_105.20.asp?current=yes . Total student enrollment is 76M, of which 55M are in schools (38M in elementary/middle school, 16M in high school), 21M are in colleges (18M undergrads, and 3M grads). 14M high school kids are in this age group, while none of college kids are in this age group.

18-24yrs: 30M. Out of these, 14M are enrolled in high school or college (school=1.5M, undergrad/grad college=12.5M). So, more than half of this age group people are not getting any college education. Of the ones that are enrolled, half of them drop out without geting a 4 year degree. So, only 25% of the workforce has a 4 year college degree or higher, which comes close with the workforce numbers of 30% workforce having a 4 yr college degree . Note, many of the people in higher age group, also get a 4 yr college degree later in their life, so the workforce has a slightly higher number for people with 4 yr college degree. Also, out of the remaining 75%, about 2/3rd of them enter the workforce. So, out of 22M who don't complete college, about 15M get into workforce and start earning money.

25-59yrs: 145M. 8M of people here are still students in colleges. Most of the people in this group comprise the workforce of 155M. BLS figures show a partcipation rate of 85%, so about 115M of the workforce is comprised of this group. Looking at this number, only 20M (145M-8M-115M) of this age group is not employed. Some of them are stay at home mom/dad, some may be involved in farming, while some may be out of labor force all together because of disabilities, etc, while some are simply unemployed because no one is hiring them.

> 60yrs: 60M. about 25% of the people in this age group still work, implying about 15M of the workforce comes from this age group. Note that full retirement benefits (social security benefits) don't start until the age of 67yrs, so many people keep on working till age 65. In fact, in the age group of 60-65yrs, of a population of 17M, 11M keep on working. Remaining 4M of the workforce comes from people over age 65.

So, based on this, we see that abou 80% of the workforce of 155M comes from age group 25-60yrs, and only 30% of the workforce has a 4 yr college degree. Kids+students are about 100M, while old retired people are about 45M. Out of remaining 165M people who are eligible to work, 150M people are working (including military and farm jobs). Only 15M are not working (5M due to choice or disability, while 10M due to not able to find a job). One interesting fact is that females in colleges outnumber males (12M vs 9M).

Salary:

Now, lets see at the occupation of this workforce and their median salary. This is a good link at bls website: http://www.bls.gov/cps/cpsaat39.htm (NOTE: this only shows earnings for people making more than certain threshold/year, since for lower earnings, data may get unreliable). Unemployment rate for people with bachelor's or higher degree is very low (2.5% in 2017), while those who didn't complete high school was much higher at 13%.

Now, we look at what profession are these people employed in.

Highest paying occupations: about 17M people are in this group making > $100K/year. These are top 10% of working population. Highest paid jobs are doctors, lawyers and CEOs most of whom are in top 1%.

  1. Medical Professions: These include anyone working in Medical field, as doctors, nurses, therapists, etc. NOTE that doctors are the only ones in this field who make salaries in top 1%. Others make much lower salaries.
    • Doctors: Total 1M physicians, surgeons, dentists, pharmicists, dietician or related occupation. Doctors are the highest paid of any profession in USA. Median salary=$200K for salaried doctors, while self employed or those working in speciality fields earn about $400K in median salary. In fact, 9 of top 10 of the highest paying jobs in the USA are all in the field of health care profession (i.e doctors).
    • Therapists: 0.5M therapists, as speech therapist, physical therapist, respiratory therapist, etc. Median salary=$100K.
    • Nurses: 2.5M. Salary=$100K
    • Technologists, technicians, vocational nurses: 2M. Decent salary > $70K.
  2. CEOs: 1M CEOs, including those who have their own business. median salary = $200K. If we only look at large public or private companies, there might be only 20K CEOs, and their median salary is >$500K.
  3. Legal Professions: This includes lawyers and paralegals, as well as people who have their own law firm. They are usually very highly paid, and lawyers in top companies as well in good legal firms usually end up in top 1% of the population earning wise.
    • Lawyers: 1.2M lawyers (according to American Bar Association). Median salary=$200K.
    • Paralegal, legal assistants and legal support workers: Total=0.5M.
  4. Technology Profession: Here, we include all tech field profession mostly Engineers and computer programmers, etc. Salaries for these people is also pretty high, and they usually start in the top 10%-20% of earners, and by mid career are usually in top 3%-5% of earners.
    • Computers: 3M in the field of software programming, IT, DB, Web developer, etc.
    • Engineering: Total = 2M. Electrical engineers, Mechanical engineers and civil engineers are 0.3M each, while other fields as aerospace, chemical, nuclear, materials, industrial, etc comprise remaining 2M. Engineering jobs are the highest paid jobs coming out of college across all disciplines.
    • Engineering Technicians/drafters: 0.5M
  5. Life, physical, and social science: 1M. Surprisingly salaries are at par with engineering jobs, at about $100K. These mostly include phychologist, scientists, economists, Miscellaneous life, physical, and social science technicians, etc.
  6. Arts, design, entertainment, sports, and media occupations: 1.5M. Mostly includes artists, actors, writers, directors, dancers, sportsman, entertainers, photographers, media workers, etc. avg salary is about $100K.

Lowest paying occupations: about 15M people are in this group making < $40K/year ($20/hr *40 hrs/week *50 weeks = $40K). When you hear headlines of people losing jobs, these are the people who lose their jobs. So, unempoyment rate is mostly affected by people in this category. They are bottom 10% of working population.

  1. Agricultural/farm workers: Total=1M. lowest pay of all occupations at < $10/hr. Doesn't require any education. They usually work on farms of other people.
  2. Food preparation and serving related occupations: Total=4M. Mostly employed in small businesses, as waiters, cooks, attendants, etc.
  3. Personal care and service occupations: Total=2M. Mostly includes hair dressers, child care, animal trainers, fitness workers, etc.
  4. Building and grounds cleaning and maintenance occupations: Total=4M. Mostly includes janitors, maids, ground maintainence, etc. Very low pay at < $10/hr.
  5. Protective services: Total=1M (excludes govt employees as police, fire fighters, etc). Includes security guards, life guards, etc.
  6. Healthcare support occupations: Total=2M. Includes all support staff, assistants at hospitals, etc.

Middle paying occupations: remaining 120M people are in this group making $40K - $100K. These are the middle 80% of the working population and comprise everyone else. This is WIP? FIXME?

  1. Teachers: We include anyone in Teaching profession here. They are teaxhers in schools, 2 yr colleges, as well as professors in universities. Most of the professors in top universities get paid pretty well to be in the top 10% of population. Howver, they are still significantly under paid compared to their colleagues at private corporations.
    • Schools: 3.7M teachers in elementary/high schools (3.2M in public schools and 0.5M in private schools). Assuming 50M students in public schools, that's a ratio of 1:16 for teacher:student. For private schools, it's 0.5M teachers for 5M students implying a ratio of 1:10. About 100K public schools (there are about 14K public school district, meaning avg of 7 schools per ISD (independent school district) and 35K private schools. About $650B was spent in public schools in 2018, implying per student expenditure of $12K/year. If we add up private schools, about $1T is spent in schools every year.
    • Colleges: Assuming avg college fees of $25K/year, and 20M students, about $0.5T is spent in colleges every year.  There are about 3000 4 yr institutions, and 1700 2 year institutions. https://nces.ed.gov/fastfacts/display.asp?id=372.
  2. Misc: Many more professions here. I'll list some more common ones here later

 

 

 

Theatres in USA

Theatres in USA are very expensive. Average Ticket price is $8/person (incl tax). Tickets are cheaper for movies before 6PM. However, since this is a country, where deals abound, you can save atleast half the money while enjoying movies at these theatres. Many of these theatres put up Bollywood movies also, if there are significant number of Indians living in that area. There are dedicated Indian theatres also in some of the big cities like Dallas, Houston, Atlanta, etc. Most of the theatres are digital theatres, while there are some IMAX theatres too. If you are here, you should try a real IMAX theatre atleast once. There are lot of IMAX theatres, but the real ones are the ones with huge screens in a dedicated theatre. If you are seeing an IMAX in multiplex, it's not a real IMAX. The sad part is that these "IMAX certified" thatres end up charging you a lot of money, while giving you the experience of something just slightly better than a regular 3D experience. You can find more details of American movie theatres on this Wiki Link

Different Theatres

  1. Cinemark. Cinemark is the 2nd largest movie theatre chain in USA. They have Normal theatres, Digital and 3D theatres, as well as IMAX theatres. Their tickets are usually $8, but they have so called discount theatres, which offer you tickets for $1 or $2. The discount theatres are exactly the same as normal theatres, so there is nothing that you miss. Only catch is that these discount theatres do not have latest release, but movies which are atleast couple of months old. Cinemark tries to have atleast 1 discount theatre in big cities, so it's the cheapest way to watch movies anywhere in USA. These discount theatres are usually referred to as "Movies 6", "Movies 8", "Movies 10", "Movies 14", etc  or "dollar cinema" theatres on the cinemark website. Look through the "theatre and showtime" page on the website, to find a theatre near you. I'm listing some theatres below (movies 8, etc), which are "dollar" theatres (<$3/ticket). There are other "movies 14" etc. theatres also, which have slightly higher prices (<$6/ticket), but they have newer releases. You should not have to pay more than $6 for any movie show, since you will usually find these higher priced movie theatres in almost all cities with latest releases.
    • Austin, TX: Cinemark Movies 8 (Round Rock, TX) => PERMANENTLY CLOSED (as of 2021) regular movies = $2.50/ticket, 3D = $4.25/ticket (discounted to $3.75 on wednessdays), mostly 6 month old movies
    • Austin, TX: Cinemark Movies 14 (Round Rock, TX) => regular movies = $5.75/ticket, mostly newer movies
    • Dallas, TX: Cinemark Hollywood USA Movies 15 (Garland, TX) => regular movies = $2/ticket, 3D = $4/ticket (discounted on Tuesdays), mostly 6 month old movies
  2. AMC Theatres. AMC is the largest movie theatre chain in USA (almost twice the size of AMC). This theatre chain gives you the best value for money. They also have similar movie halls as Cinemark theatres. Although their normal movie charges are also about $8, but they charge $4 (some places they charge $5 or $6) for movies on Friday, Saturday and Sunday that start before 12PM noon. This is the cheapest you can watch movies at places where you don't have cinemark discount theatres.
  3. Bollywood Theatres: There are a lot of Bollywood theatres operated by Indians, which exclusivly show desi movies. However, these are only in big cities. This website is the best I was able to find, which lists all the bollywood cinemas in USA, as well as has links to all the bollywood movies that are released. However, most of the theatres charge $8/ticket. Some of these theatres have deals for 1 day in a week. Still too expensive, and not worth the money. Most of these movies you can watch online for free or at a very low cost. See "online movie serial" link on left.
  4. IMAX: IMAX theatres are the ones that are worth watching a movie outside. A decent projector (<$500) can give you an experience similar to what you get in regular movie theatres, but real IMAX theatres, give you an experience which is impossible to capture via a home projector. Typical home projector screens are 10ftx10ft=100 sq ft, which is just 5% of typical IMAX screen.

    Probably best to try the world's three largest IMAX screens:

    1. Sydney IMAX, Sydney, Australia - Held world record for 15 years and has recently been upgraded
    Dimensions: 97x117ft (29.7x35.7m) - 11,350sq. ft. , 1,060m2

    2. Melbourne IMAX, Melbourne, Australia - Used to have the world's largest 3D screen prior to Sydney upgrade
    Dimensions: 75x104ft (23x32m) - 7,800sq. ft, 736m2

    3. Prasad IMAX, Hyperabad, India - The world's busiest and 3rd largest screen
    Dimensions: 72x95ft (22x29m) - 6,840sq ft., 638m2

    To put these screens into perspective, most IMAX screens are 2,500sq ft. or less that less than 22% of the Sydney screen size

     

     


 

How to watch Hollywood movies for free in Theatres

Probably you have heard about movie screenings. These are the first shows of the movie that are shown to journalists, reporters and selected guests. This is from where these people get to write reviews about a movie, before it gets released to the mass public. But here in US, these movie screenings are available to the general public also. There are lot of ways you can find out about these free movie screenings. One is to ask the theatre, and see when they have it. Other is to look in newspapers. However, the best and the most reliable source is the wild about movies website. This link https://www.wildaboutmovies.com/free-movie-screenings/ takes you to movies that are available for free screening. Click on the movie you are interested and see if it is available in your city. Most of the big US cities are covered here. Fill in the form. You will get confirmation in email. Print your pass and goto the theatre on designated date. Remember that not everyone gets passes. Only a few selected random entries get confirmation email with a pass.

One thing to note is that seating is on a first come and first serve basis. The number of passes issued to general public are more than the number of seats. So, you have to go to the theatre atleast half an hour before the scheduled movie time. If the movie is a very hyped movie, you might have to go hours before the movie starts. You will have to stand in the queue. Don't be surprised if you see a long queue. Unless the theatre administrator comes and informs that NOT everyone will be admitted, you will most likely be admitted even if you are at the end of the queue. Come full, since popcorn/drinks are not cheap in theatres cry

 


 

Kids Summer Movies

A lot of theatres in USA have movie shows during the summer for very cheap. The movies are usually kids movies that are old. They are the first show during the day, when the theatres are anyway empty. Also, they avoid weekends, and have these shows on weekdays to minimize their losses. Very few theatres participate, so check on the website to see which theatre is closest to you. In spite of all of these limitations, it's a nice way to get kids to watch movies on the big screen for a dollar or two. AMC, Cinemark, Regal all have their own version of "Kids Summer Movie Club"

  1. Cinemark: They call it the "Summer Movie Clubhouse". For about 10 weeks in summer, they show a different movie every week. The movies are shown from Monday-Wednesday @10am or before.
    1. 2024 => June 10-Aug 15 (10 weeks). Link => https://www.cinemark.com/series-events-info-pages/summer-movie-clubhouse/

 

 

******************************************
For running synthesis in Cadence RC (RTL Compiler):
-------------------------------------------------------------------

RC does global opt which isolates timing critical and non-timing critical paths before mapping them to gates. This results in better design than tools which do local/incremental opt in which design is mapped to gates first and then timing is opt.

RC:
---
Create a dir: /db/NOZOMI_NEXT_OA/design1p0/HDL/RCompiler/digtop/

NOTE: everything in RC is stored in virtual dir starting at / . So, we see / after many of the cmd which specifies that the cmd applies to all of the design (/ implies the top level dir of design. It's NOT for continuation on next line)

cp .synth_init file from some other dir. It is similar to .synopsys_dc.setup and has search paths, lib path and other variables setup. RC searches for this file first in installation dir as master.synth_init file, then in home dir for .cadence/.synth_init file, and finally in current dir for .synth_init file. It has the following and many more settings:

#set_attribute <attr_name> <attr_value> <object> => sets value of an attribute. In RC, there are predefined attr associated with objects. We can set only those attr on objects, which are read-write. Also, some attr can only be set at root (/) level, while some can be set on "designs" objects only.
ex: set_attribute lp_clock_gating_exclude true /designs/digtop => setting attr on designs/digtop object

#we can also create our own attribute:
set_user_attribute <attr_name> <attr_value> <object>

#get_attribute  <attr_name> <object> => gets attr value on single object only.
ex: get_attr load /libraries/slow/inx1/A

#set library paths to max delay lib. When we set this attr, RC scans these .lib files and reports errors/warnings in these files such as usupported constructs, etc. It also reports unusable cells (marked as "dont_use" in these lib files. usually all CTS cells and dly cells are marked as dont_use). Then it sets attribute of root "/": library = PML30_W_150_1.65_CORE.lib PML30_W_150_1.65_CTS.lib
set_attribute lib_search_path {/db/pdkoa/lbc8/2011.06.26/diglib/pml30/synopsys/src} /
set_attribute library {"PML30_W_150_1.65_CORE.lib" "PML30_W_150_1.65_CTS.lib"} / => max library. library attr is a root attr and so it's applied at root dir. This cmd populates the /libraries virtual dir.

#WLM, PLE, spatial or Physical RC can be used for wire modeling. WLM is worst and Physical is best. WLM is default.
#RC-WLM: info in .lib file. has WLM models. look in liberty.txt for details. WLM provides same res/cap for all layers (which is Res=0, cap=1pf/unit_length). In reality, res=0.2ohm/um and cap=0.2ff/um for LBC7 process. so, WLM is overly optimistic for net delays, and effectively treats net delays as 0.
set attr interconnect_mode wireload /
set attr wireload_mode top /

#RC-PLE (physical layout estimation), RC-spatial, RC-physical(RCP): needs tech_lef and std_cell_lef files in addition to .lib files. cap_table and floorplan def files are optional for PLE and spatial, but floorplan .def file is required for physical as it has pin location, macro placement, etc. PLE does good job modeling local interconnects since physical cell size as well as various metal layer info is present. Providing cap table info gives better estimate of cap/res as actual cap/res taken for each layer. spatial models longer wires better, as it does coarse placement under the hood. providing floorplan def helps a lot in RC-spatial.

set attr interconnect_mode ple / => not needed as specifying lef files applies PLE.
#when setting attr for lef lib, RC scans these files and reports number of routing layers, number of logic/seq cells, and any warnings/errors etc. It alos looks for consistency b/w tech lef and cap table for width of layers, etc. Then it sets attr "lef_library", "cap_table" for root "/" to named files below.
set_attribute lef_library {"/db/pdk/lbc7/rev1/diglib/msl270/r3.0.0/vdio/lef/msl270_lbc7_core_iso_2pin.lef" "/db/pdk/lbc7/rev1/diglib/msl270/r3.0.0/vdio/lef/msl270_lbc7_tech_3layer.lef" } / => both tech and std cell lef files provided. stored in compiler memory at /libraries
set_attribute cap_table_file {/db/pdk/lbc7/rev1/diglib/msl270/r3.0.0/vdio/captabl/3lm_maxC_maxvia.capTbl} / => helpful. This shows res and cap for various width and spacing for each layer and vias.

For running spatial or physical, include it in "synthesize" cmd as follows when running rc:
synthesize -to_mapped -spatial -effort [low|medium|high] => spatial
synthesize -to_placed => physical. It runs First encounter (FE) placeDesign, trialroute, extractRC, buffers long wires, brings in physical timing and performs inc opt. Then we can do: write_encounter digtop. We can output a def file which is fully placed legal design pre-CTS. We can then start from the CTS step in FE.


##Default undriven/unconnected setting is 'none'.  These connect each i/p, o/p or internal undriven signal (wire/reg) to specified value. none implies undriven signal remains undriven. post elaboration netlist will have appr gates and assign stmt to support driven value.
#set_attribute hdl_unconnected_input_port_value 0 | 1 | x | none /
#set_attribute hdl_undriven_output_port_value   0 | 1 | x | none /
#set_attribute hdl_undriven_signal_value        0 | 1 | x | none /

#naming style in verilog netlist generated. %s is variable name, %d is individual bit
set_attribute hdl_array_naming_style %s_%d /  
set_attribute bus_naming_style %s_%d /

#Selects the Verilog style for unconnected instance pins. default is to write out dummy wires for unconnected instance pins. ex: for this line in original RTL: DELAY1 DL (.A(A2)); //DL module has 1 i/p port and 1 o/p port which is not coded in RTL.
#full => Put UNCONNECTED for nets connecting unconnected instance pins in gate netlist. ex: DELAY1 DL(.A (A2), .Z (UNCONNECTED));
#partial => Put the unconnected instance pins in gate netlist, but no wire to connect to it. ex: DELAY1 DL(.A (A2), .Z ());
#none => do nothing. ex: DELAY1 DL (.A(A2));
set_attribute write_vlog_unconnected_port_style  partial / => remove  UNCONNECTED nets from pins.

#set_attribute tns_opto true / => turn ON TNS
 
##set_attribute wireload_mode <value> /
set_attribute information_level 7 /

set_attribute hdl_track_filename_row_col true / => To include the RTL file name and line number  at  which  the  DFT violation occurred in the messages produced by check_dft_rules

#clk gating set for 3 or more flops
set_attribute lp_insert_clock_gating true /
set_attribute lp_clock_gating_min_flops 3 /
set_attribute lp_clock_gating_prefix CLK_GATE /

#do not merge equiv flops and latches
set_attribute optimize_merge_flops false /
set_attribute optimize_merge_latches false /

#optimize const flops. By default, set to true so that const 0/1 can be propagated thru flops, thus allowing removal of flops.
#set_attr optimize_constant_0_flops false
#set_attr optimize_constant_1_flops false

#use_tiehilo_for_const: const are tied to hi/lo cells. This doesn't connect all 1'b1/1'b0 to tiehi/lo cells , so we use another cmd after synthesize to fix remaining 1'b1/1'b0 problem. options:
#duplicate => Allows each constant assignment to be replaced with a tie cell.
#unique => Allows  only  one unique tie cell in the netlist. Treatment of the remaining constant assignments depends on settings of the remove_assigns and set_remove_assign_options
#none => Prevents the  replacement  of constants in the netlist with tie cells
set_attr use_tiehilo_for_const unique => only 1 unique tie cell should be added.

vaious other attr can be set in .synth_init file, before running rc cmds.

------------------
run RC: script run_rc
rc -10.1-s202 -over -f ./tcl/top.tcl -logfile ./logs/top.log -cmdfile ./logs/top.cmd

#IMP: for getting help with cmds on rc,
rc:/> cdnshelp => brings up cdns help browser for that rev of tool
rc:/> man or help <cmd_name>. Tab key shows all possible completions.
rc:/> man lib_serach_path => this will show man page for attr "lib_serach_path"

# write_template => template script can be generated by running write_template with various options
write_template -outfile run.tcl -full => creates script with all basic cmd, dft, power, retiming. -simple creates a simple script.

#running scripts within RC: do a source or include with script file name.

top.tcl:
-------
#initial setup: (it has set SCAN_EXISTS 0 => choose b/w scan vs non-scan design)
#include/source other files
include tcl/setup.tcl => set varaibles as DESIGN, SYN_EFF, lib path, dir, etc. All lib path, cap_table etc are put in .synth_init file, but can also be put here.
#source tcl/setup.tcl => we can also use source to include the file

#source tcl/analyze.tcl
#read verilog/vhdl/systemVerilog files, elaborate, check design, uniqify and then check for uniquufy
#read_hdl <-v1995 | -v2001 | -sv | -vhdl> [list "$RTL_DIR/global.v"  ... " " ] => default lang attr is one specified by hdl_language  attribute.  The  default value for the hdl_language attribute is -v1995. For -vhdl, hdl_vhdl_read_version root attribute apecifies vhdl version, by default it's set to VHDL-1993.
read_hdl -v2001 [list "$RTL_DIR/global.v"  ... "$RTL_DIR/digtop.v" ]

#read_netlist design_struct.v => to read gate level netlist

elaborate $DIG_TOP_LEVEL => $DIG_TOP_LEVEL is set to digtop above. This elaborates top level design and all its references. We only specify the top level. It builds data structures, infers registers,performs HDL opt, identifies clk gating and operand isolation candidates.

check_design -unresolved => checks for design problems as unresolved references. Using -all checks for  undriven/multidriven ports/pins, unloaded ports/pins, constant connected ports/pins and any assign stmt.

#uniquify not needed as design is uniquified by default.
/*
uniquify $DIG_TOP_LEVEL
#task to make sure design is uniquified
proc is_design_uniquified {} {
    foreach subd [find /des*/* -subdesign *] { => look in designs dir for all sub designs
        if {[llength [get_attr instance $subd]] > 1 } {
            puts "ERROR: design is NOT uniquified"
            return
        } else { return "design is uniquified" }
    }
}
is_design_uniquified => calling the actual procedure
*/

#provide constraints in SDC: 2 options: read sdc file directly by using read_sdc or enter constraints as in DC. eg.
#option 1: read_sdc ./tcl/env_constraints.tcl => reads all DC sdc cmds directly without any prefixing. Useful as same file can be used in EDI/Dc, etc. IMP: we've to add ./tcl and not tcl/, since virtual dir structure is assumed for RC, so it looks for tcl dir in virtual dir, which is not there, so it complains. By changing it to ./tcl, it looks in unix tcl dir in current dir
#option 2: replace all dc cmds with dc::, or change them to RC equiv cmd. ex: dc::set_load ..... We can read these cmds anytime within RC shell or put it in file and source it: source tcl/env_constraints.tcl. However, same file can't be used in synopsys tools as "dc::" is not sdc cmd.

#env constraints: (see in sdc.txt for cmd details: some cmds in DC sdc file aren't std sdc cmd, so they have to be replaced with appr RC cmds).
option 1: read_sdc ./tcl/env_constraints.tcl => same file can be used in EDI
option 2: prepend sdc cmds with dc::.

env_constraints.tcl file: op_cond (PVT), load (both i/p and o/p), drive (only on i/p), fanout(only on o/p) and WLM. Of these, op_cond and WLM are already specified in .synth_init file. dont_touch, dont_use directives also provided here.
------
#i/p driver: use "set_driving_cell" as it's std sdc cmd
#set_attribute external_driver [find [find "MSL270_W_125_2.5_CORE.db" -libcell IV110] -libpin Y] [all_inputs] => DC cmd
set_driving_cell -lib_cell IV110 [all_inputs] => sdc cmd. use this for both RC/DC

#o/p load: use "set_load" as it's std sdc cmd. However, to automatically use i/p cap for IV110 as load cap for o/p ports, we need to use diff cmd in DC vs RC. Then we can use set_load.
if {$RUN_PNR ==1} {
set output_load 0.005
} else {
#set output_load [get_attribute capacitance "MSL270_W_125_2.5_CORE.db/IV110/A"] => get_attribute is native cmd for both RC/DC with different syntax, so it gives an error in RC. Also, it can't be used in EDI. For RC, we use "get_liberty_attribute" which is simpler.
set output_load [get_liberty_attribute capacitance [find [find "MSL270_W_125_2.5_CORE.db" -libcell IV110] -libpin A]] => get_liberty_attribute isn't supported in EDI. use this in RC only.
#set output_load  [get_attribute max_capacitance [find [find / -libcell MSL270_W_125_2.5_CORE/IV110] -libpin A]] => here get_attribute is used and full path of libcell is provided since we start search from top level virtual dir "/".
}

set output_load_4x [expr 4 * $output_load]
set_load -pin_load $output_load_4x [all_outputs]

write_set_load > ${_OUTPUTS_PATH}/net_cap.txt => shows load values for all the nets in design in set_load format. since set_load is sdc cmd, values are shown in pf. Run this in RC to make sure units are correctly shown.

#set_dont_use
read_sdc ./tcl/dont_use.tcl

#set_dont_touch
read_sdc ./tcl/dont_touch.tcl

#write out HDL in cadence primitives, before doing synthesis
write_hdl    > ./netlist/${DESIGN}.PreSyn.v

#initial synthesis
synthesize -to_generic -eff low -no_incr => opt mux and datapath and stops before mapping. It contains tech independent components. It does const propagation, resource sharing, logic speculation, mux opt, CSA (carry save adder) opt. -no_incr allows it to opt logic from scratch.
synthesize -to_mapped  -eff low -no_incr => maps design to cells in tech lib and optimizes it. It evaluates every cell in design and resizes to improve area and power. If -incr option is used, then it runs DRC, timing, area cleanup and critical region resynthesis to meet timing. -incr preserves current impl and performs opt only if there is an improvement in overall cost of design. -to_mapped is default option.

#when we synthesize with map, we see "global mapping target info" on screen and in log file. In each cost group, RC will estimate a target slack number based on the design structure, libraries, and design constraints. This slack number is the estimated slack on the worst path of a cost group seen before mapping. During mapping, RC will try to structure logic, and select cells to bring this target slack number close to 0.

#puts "Runtime & Memory after initial synthesize"
#timestat MAPPED

generate_reports -outdir $_REPORTS_PATH -tag ${DESIGN}.initial => reports area, gate, timing in separate files.
#report area > $_REPORTS_PATH/${DESIGN}.initial_area.rpt => no need of this cmd, as area already reported by above cmd

write_hdl  > ${_NETLIST_PATH}/${DESIGN}_initial.v

#### design constraints => case_analysis, i/p,o/p delays, clocks/generated clocks, false/multicycle paths
if {$SCAN_EXISTS} {
read_sdc ./tcl/case_analysis.tcl => set_case_analysis only if scan exists to force part in functional mode. We want to have simple functional timing paths, and not have paths for scan_mode too. strictly speaking, this stmt is not required.
#case_analysis.tcl
#set_case_analysis 0 scan_mode_in => force scan_mode to 0 so that we see timimg paths b/w diff clocks. We are not interested in timing when part is in scan mode.
}
read_sdc ./tcl/constraints.tcl => has i/p, o/p delays
#constraints.tcl
set_input_delay  0.2 -clock clk1 [all_inputs]
set_output_delay 0.4 -clock clk1 [all_outputs]
}

#clocks (set_drive and create_clock/create_generated_clock for all clks).
read_sdc ./tcl/clocks.tcl

#we don't set uncertainty in clocks.tcl, since we use that file in EDI, where we want to use real clk delays)
set_clock_uncertainty $SPI_SCK_skew SPI_SCK

#turn off clk gating if not wanted (in .synth_init we set clk gating to true)
set_attribute lp_insert_clock_gating false /

#read false_paths/multi-cycle paths
read_sdc ./tcl/false_paths.tcl
read_sdc ./tcl/multicycle_paths.tcl

#to prevent any logic changes on instances of specified cells.
#map_size_ok => Allows  resizing, unmapping, and remapping of a mapped sequential inst during opt,  but not renaming or deleting it.
#size_ok     => Allows resizing a mapped inst during  opt, but not deleting, renaming, or remapping it.
    
set_attr preserve map_size_ok [find I_S1_CONTROL -instance  instances_seq/sm_reg*]
#set_attr preserve true => Prevents logic changes to any object in the design during opt. needed

# Incremental Compile with high effort
source tcl/compile.tcl

###compile.tcl has following cmds.
#report worst case timing by setting this variable:
set_attribute map_timing true /

## Define cost groups (clock-clock, clock-output, input-clock, input-output)
if {[llength [all::all_seqs]] > 0} {
  define_cost_group -name I2C -design $DESIGN
  define_cost_group -name C2O -design $DESIGN
  define_cost_group -name C2C -design $DESIGN
  path_group -from [all::all_seqs] -to [all::all_seqs] -group C2C -name C2C
  path_group -from [all::all_seqs] -to [all::all_outs] -group C2O -name C2O
  path_group -from [all::all_inps]  -to [all::all_seqs] -group I2C -name I2C
}

define_cost_group -name I2O -design $DESIGN
path_group -from [all::all_inps]  -to [all::all_outs] -group I2O -name I2O

#report all failed cmds when reading sdc
echo "failed sdc cmds" > $_REPORTS_PATH/${DESIGN}.after_constrain.rpt
echo $::dc::sdc_failed_commands >> $_REPORTS_PATH/${DESIGN}.after_constrain.rpt

echo "The number of exceptions is [llength [find /designs/$DESIGN -exception *]]" >> $_REPORTS_PATH/${DESIGN}.after_constrain.rpt

report timing -lint -verbose >> $_REPORTS_PATH/${DESIGN}.after_constrain.rpt => reports possible timing problems in the design, such as ports that have no external delays (unclocked primary I/O), unclocked flops, multiple clocks propagating to the same clock pin, timing exceptions that cannot be satisfied, timing exceptions overwriting other timing exceptions, constraints that may have no impact on the design, and so on.

#incremental synthesis
synthesize -to_mapped -eff high -incr

#IMP: we might have 1'b0 and 1'b1 in logic at this time. To connect them to tiehi/tielo cells, run this:
insert_tiehilo -all -hilo TO020L -verbose [find -design *] => for both hi/lo connections, same cell used. verbose shows info on screen, as to which 1'b1/1'b0 are still not tied. -all does it for all including scan cells. If we put "-hi TO020 -lo TO020", then tool connects hi connections to one instance of TO020 (to HI pin. LO pin is left floating) and lo connections to another instance of TO020 (to LO pin. HI pin is left floating). So, this results in 2 copies of TO020 cells. By using "-hilo TO020", we use same instance for hi and lo connections.

#reports
generate_reports -outdir $_REPORTS_PATH -tag ${DESIGN}.incremental
summary_table -outdir $_REPORTS_PATH

report timing  -num_paths 500 >> $_REPORTS_PATH/${DESIGN}.all_timing.rpt
foreach cg [find / -cost_group -null_ok *] {
  report timing -cost_group [list $cg] -num_paths 100 > $_REPORTS_PATH/${DESIGN}.[basename $cg]_timing.rpt
}

report area > $_REPORTS_PATH/${DESIGN}.compile.rpt
report design_rules >> $_REPORTS_PATH/${DESIGN}.compile.rpt
report summary >> $_REPORTS_PATH/${DESIGN}.compile.rpt => reports area, timing and design rules.

#optional reports
report messages >> $_REPORTS_PATH/${DESIGN}.compile.rpt => reports summary of error msg
report qor     >> $_REPORTS_PATH/${DESIGN}.compile.rpt
report gates -power >> $_REPORTS_PATH/${DESIGN}.compile.rpt => reports libcells used, total area and instance count
report clock_gating >> $_REPORTS_PATH/${DESIGN}.compile.rpt
report power -depth 0 >> $_REPORTS_PATH/${DESIGN}.compile.rpt
report datapath >> $_REPORTS_PATH/${DESIGN}.compile.rpt => datapath resource report

#write results
write_design -basename ${_OUTPUTS_PATH}/${DESIGN}
write_script > ${_OUTPUTS_PATH}/${DESIGN}.script
write_hdl  > ${_NETLIST_PATH}/${DESIGN}.v => final non-scan netlist

####### Insert Scan
if {$SCAN_EXISTS} { => see synthesis_DC.txt for details on this
set_ideal_network [get_ports scan_en_in]
set_false_path -from scan_en_in

source tcl/insert_dft.tcl
}

#insert_dft.tcl has following
source ./tcl/scan_constraints.tcl

#scan_constraints has following:
set_attribute dft_dont_scan true [ list Idigcore/IResetGen/nReset_meta1_reg \
                                        Idigcore/IResetGen/nReset_meta2_reg ]

set_attr dft_scan_style muxed_scan / => mux_scan style
set_attribute dft_prefix DFT_ / => prefix dft with DFT_

# For VDIO customers, it is recommended to set the value of the next two attributes to false.
set_attribute dft_identify_top_level_test_clocks false /
set_attribute dft_identify_test_signals false /

set_attribute dft_identify_internal_test_clocks false /
set_attribute use_scan_seqs_for_non_dft false /

set_attribute dft_scan_map_mode tdrc_pass "/designs/$DESIGN"
set_attribute dft_connect_shift_enable_during_mapping tie_off "/designs/$DESIGN"
set_attribute dft_connect_scan_data_pins_during_mapping loopback "/designs/$DESIGN"
set_attribute dft_scan_output_preference auto "/designs/$DESIGN"
set_attribute dft_lockup_element_type preferred_level_sensitive "/designs/$DESIGN"
#set_attribute dft_mix_clock_edges_in_scan_chains true "/designs/$DESIGN"

---
### define clocks, async set/reset, SDI, SDO, SCAN_EN and SCAN_MODE.
##all dft cmds have these common options:
#define_dft <test_mode | test_clock | shift_enable | scan_chain> -name <testObject> <port or pin name> -create_port -hookup_pin <pin_name> -hookup_polarity <inverted|non_inverted> -shared_in -shared_out

#<port or pin name>: we provide the driving port_or_pin_name. However, that will work only if we code the RTL in a way where the top level port can directly be used as SE, SCLK, SDI, SDO. In many cases, functional pins are used as scan pins by multiplexing them, so if we directly use the port name, that will be incorrect. For ex spi_cs_n being used as scan_shift_en (during scan_mode) has spi_cs_n anded with scan_mode to generate scan_shift_en which is then connected to SE pin of all flops. In this case, internal scan_shift_en needs to be used for SE, so we add option "-hookup_pin B/scan_shift_en_int" so that tool connects this pin to SE of all flops. When you specify this option, the RC-DFT engine does not validate the controllability of any logic between the top-level test-mode signal and its designated hookup pin under test-mode setup (i.e if the hookup pin can be changed to desired value by toggling i/p port or not). The way RTL is coded in our group is we get the pin driven out and then driven back in as dedicated pin for scan purpose (for ex scan_enable_out and scan_enable_in pins). Then we don't need -hookup_pin option. Look in DFT compiler notes (pg 1 back).

#-shared_in is used to indicate that i/p port is used for functional port also. similarly -shared_out is used to indicate that o/p port is used for functional port also. By default, the signal applied to the specified driving pin or port is considered to be a dedicated test signal. By specifying these, we ensure that these test signals will not get constrained in the write_do_lec dofile. Not specifying this option for a shared test signal will result in overconstraining the write_do_lec dofile (by forcing that input port to inactive state) which can lead to false EQs.

#-no_ideal marks the test signal as non-ideal which allows buffering in RC. By default, it's treated as ideal.

----
#force pins for test mode: i.e async set/reset need to be in inactive state, while SCAN_MODE needs to be high.
#define_dft test_mode -name <testModeObject> -active <high|low> -no_ideal -scan_shift <port_or_pin_name> [-create_port] [-shared_in] -hookup_pin <pin_name> -hookup_polarity <inverted|non_inverted>
define_dft test_mode -name scan_mode -active high scan_mode_in
#define_dft test_mode -name scan_reset -active high n_reset => we don't define async set/reset as we force them to 0, when scan_mode=1 (in RTL itself). If we need to toggle n_reset during scan test to have more coverage, then we need to use -scan_shift option which holds the scan signal to its test-mode active value during the scan shift operation of the tester cycle, but is otherwise allowed to pulse during capture cycle (test signal will be treated as a non-scan clock signal by the ATPG tool). -scan_shift option is also needed to generate correct lec.do file, else n_reset pin will get get constrained which will lead to false EQs.

#now define scan_clk, scan_shift_en, scan_data_in and scan_data_out for each chain. Note that these scan_pins are multiplexed with normal functional pins, so -hoopup_pin option is used.
#define_dft test_clock -name <testClockObject> -domain <testClockDomain> -period <delay in pico sec, default 50000> -rise <integer> -fall <integer> <portOrpin> -hookup_pin <pin_name> -controllable => Defines  a  test  clock  and  associates a test clock waveform with the clock. If  you  do  not define test clocks, the DFT rule checker automatically analyzes the test clocks and creates these objects with a default waveform. -hookup_pin specifies the core-side hookup pin to be used for the top-level test clock during DFT synthesis.
#-controllable => when specifying an internal pin for a test clock, this option indicates that the internal clock pin is controllable in test mode (for example, Built-in-Self-Test (BIST)). If you do not specify this option, the rule checker must be able to trace back from the internal pin to a controllable top-level clock pin. If you specify an internal pin as being controllable, you need to ensure that this pin can be controlled for the duration of the test cycle. The tool will not validate your assumption.
#-domain => pecifies the DFT clock domain associated with the test clock.Clocks belonging to the same domain can be mixed in a chain. If you omit this option, a new DFT clock domain is created and associated with the test clock. Flip-flops belonging to different test clocks in the same domain can be mixed in a chain. Lockup elements can be added between the flip-flops belonging to different test clocks.

define_dft test_clock -name scan_clk -domain scan_clk -period 100000 -rise 40 -fall 80 SCLK => scan_clk defined at port SCLK with period of 100ns (10 Mhz). rise happens at 40% from start of clk period while fall happens at 80%. So, rise happens at 40ns, while fall happens at 80 ns, assuming clk starts at 0ns. This test clk can be referred to as scan_clk from now on (name is helpful to search for the test clk, or look it in reports, etc). We don't specify hookup_pin as in RTL, we force the i/p clk pin to goto all flops in scan_mode (by using mux).

#define_dft shift_enable -name <shiftEnableObject> -active <high|low> <portOrpin_name> -hookup_pin <pin_name> [-create_port] => It specifies name and active value for shift_en signal. Active value is propagated during dft rule checking. The input signal can be defined on a top-level port or an internal driving pin. hookup_pin is internal pin which is the actual scan_en that should goto all flops.
define_dft shift_enable -name scan_enable  -active high SCAN_EN_IN => SCAN_EN_IN is defined as shift_enable and referred to as "scan_enable". In this RTL is coded so that scan_en_out comes back in as input port with name SCAN_EN_IN, so no need of hookup_pin.

#define_dft scan_chain -name <ChainName> -sdi <topLeveLSDIPort> -sdo <topLevelSDOPort> [-hookup_pin_sdi <coreSideSDIDrivingPin>] [-hookup_pin_sdo <coreSideSDOLoadPin>] [-shift_enable <ShiftEnableObject>] [-shared_output | -non_shared_output] [-terminal_lockup <level | edge>] => -hookup_pin_sdi/sdo specs core side hookup pin to be used for the scan data input/output signal during scan chain connection. -shift_enable designates chain specific SE signal, else default shift_enable signal used. -shared_output specs that a mux be inserted in the scan data path by the connect_scan_chains cmd since functional o/p port is being used as SDO port.

define_dft scan_chain -name chain1 -sdi spi_mosi  -sdo spi_miso -shared_output => sdi and sdo defined

###end of scan_constraints.tcl file

# DFT DRC Checking
check_dft_rules       > $_REPORTS_PATH/${DESIGN}_dft.rpt => look at hdl_track_filename_row_col attr.

report dft_registers >> $_REPORTS_PATH/${DESIGN}_dft.rpt
report dft_setup     >> $_REPORTS_PATH/${DESIGN}_dft.rpt

check_design -multidriven
check_dft_rules -advanced                                     >> $_REPORTS_PATH/${DESIGN}_dft.rpt
report dft_violations -tristate -xsource -xsource_by_instance >> $_REPORTS_PATH/${DESIGN}_dft.rpt

#fix dft violations before proceeding (either by modifying RTL or using auto fixing)
fix_dft_violations

# To turn off sequential merging on the design uncomment & use the following attributes.
set_attribute optimize_merge_flops false /
set_attribute optimize_merge_latches false /

#synthesize to map regular FF to scan FF (define_dft above makes forces synthesize cmd to include scan FF instead of non-scan FF. There is no separate scan option to synthesize with scan
synthesize -to_map -no_incr -auto_identify_shift_register => shift reg auto identified so that they are not replaced by scan cells

#Build the full scan chanins.
connect_scan_chains -preview => It shows how scan chain will be connected but makes no changes yet to the netlist.  
connect_scan_chains -auto_create_chain => connects scan FF which pass DFT into scan_chain. -auto_create_chain option allows the tool to create more chains, if needed, than what has been specified thru define_dft cmd.
report dft_chains > $_REPORTS_PATH/${DESIGN}_SCAN_Chain.txt

delete_unloaded_undriven -force_bit_blast -all digtop => remove unconnected ports in the design
set_attribute remove_assigns true => remove assigns & insert tiehilo cells during Incremental synthesis
set_attribute use_tiehilo_for_const duplicate

#incremental synthesis only if needed
#synthesize -to_mapped -eff low -incr

#IMP: we might have 1'b0 and 1'b1 in logic after scan synth. To connect them to tiehi/tielo cells, run this:
insert_tiehilo -all -hilo TO020L -verbose [find -design *]

#reports after scan insertion
report dft_setup > $_REPORTS_PATH/${DESIGN}-DFTsetup_final
write_scandef > ${DESIGN}-scanDEF
#write_atpg [-stil|mentor|cadence] > ${DESIGN}-ATPG
write_atpg -stil > ${DESIGN}-ATPG
write_dft_abstract_model > ${DESIGN}-scanAbstract
write_hdl -abstract > ${DESIGN}-logicAbstract
write_script -analyze_all_scan_chains > ${DESIGN}-writeScript-analyzeAllScanChains
## check_atpg_rules -library <Verilog simulation library files> -compression -directory <Encounter Test workdir directory>
## write_et_bsv -library <Verilog structural library files> -directory $ET_WORKDIR
## write_et_mbist -library <Verilog structural library files> -directory $ET_WORKDIR -bsv  -mbist_interface_file_di
r <string> -mbist_interface_file_list <string>
## write_et_atpg -library <Verilog structural library files> -compression -directory $ET_WORKDIR
write_et_atpg -library  /db/pdk/lbc7/rev1/diglib/msl270/r3.0.0/verilog/models/*.v  -directory $ET_WORKDIR

#final reports
generate_reports -outdir $_REPORTS_PATH -tag ${DESIGN}.scan
summary_table -outdir $_REPORTS_PATH

report timing  -num_paths 500 >> $_REPORTS_PATH/${DESIGN}.all_timing.scan.rpt
foreach cg [find / -cost_group -null_ok *] {
  report timing -cost_group [list $cg] -num_paths 100 > $_REPORTS_PATH/${DESIGN}_scan.[basename $cg]_timing.rpt
}

report area > $_REPORTS_PATH/${DESIGN}.scan.compile.rpt
report design_rules >> $_REPORTS_PATH/${DESIGN}.scan.compile.rpt
report summary >> $_REPORTS_PATH/${DESIGN}.scan.compile.rpt => reports area, timing and design rules.

#optional reports
report messages > $_REPORTS_PATH/${DESIGN}.scan.compile.rpt
report qor >> $_REPORTS_PATH/${DESIGN}.scan.compile.rpt
report gates -power >> $_REPORTS_PATH/${DESIGN}.scan.compile.rpt
report clock_gating >> $_REPORTS_PATH/${DESIGN}.scan.compile.rpt
report power -depth 0 >> $_REPORTS_PATH/${DESIGN}.scan.compile.rpt
report datapath > $_REPORTS_PATH/${DESIGN}.scan.compile.rpt

write_design -basename ${_OUTPUTS_PATH}/${DESIGN}_scan
write_script > ${_OUTPUTS_PATH}/${DESIGN}_scan.script
write_hdl  > ${_NETLIST_PATH}/${DESIGN}_scan.v => final scan netlist

-- end of insert_dft.tcl

#write sdc and do files
write_sdc > sdc/constraints.sdc

#Write do file for RTL is to be compared with the final netlist. only revised is specified since RTL is taken as golden. Otherwise we need to specify "-golden_design <RTL_files>"
if {$SCAN_EXISTS} {
write_do_lec -revised_design ${_NETLIST_PATH}/${DESIGN}_scan.v -logfile ${_LOG_PATH}/rtl2final.lec.log > ${_OUTPUTS_PATH}/rtl2final.lec.do
write_et_atpg -library /db/pdkoa/1533c035/current/diglib/pml48/verilog/models => write Encounter Test ATPG scripts in et_scripts dir to generate patterns
} else {
write_do_lec -revised_design ${_NETLIST_PATH}/${DESIGN}.v -logfile ${_LOG_PATH}/rtl2final.lec.log > ${_OUTPUTS_PATH}/rtl2final.lec.do
}

puts "Final Runtime & Memory."
timestat FINAL
puts "============================"
puts "Synthesis Finished ........."
puts "============================"

#################################
#for scan mapping, use this section
#################################
define_dft test_mode -shared_in -active high $TESTSCANMODE
set_attribute dft_dont_scan true [find / -inst I_WRAPPER/scanmode_r*]
set_attribute dft_dont_scan true [find / -inst I_WRAPPER/clked_nt_result*]

define_dft shift_enable  -name SE \
                         -active high \
                         -hookup_pin [find / -pin I_WRAPPER/SCANEN]\
                         [find / -port I_GPIO_Y[1]]
define_dft test_clock    -name SCANCLOCK \
                         -period 100000 -fall 40 -rise 60 \
                         [find / -port I_GPIO_Y[0]]
#define_dft test_mode     -scan_shift -name RESET -active high \
#                         [find / -port I_XRESET]

define_dft scan_chain    -name chain1 \
                         -sdi [find / -port I_GPIO_Y[2]] \
                         -sdo [find / -port O_GPIO_A[3]] \
                         -hookup_pin_sdi [find / -pin I_WRAPPER/SI1] \
                         -hookup_pin_sdo [find / -pin I_WRAPPER/SO1] \
                         -shared_out

define_dft scan_chain    -name chain2 \
                         -sdi [find / -port I_GPIO_Y[4]] \
                         -sdo [find / -port O_GPIO_A[5]] \
                         -hookup_pin_sdi [find / -pin I_WRAPPER/SI2] \
                         -hookup_pin_sdo [find / -pin I_WRAPPER/SO2] \
                         -shared_out


set_attribute dft_min_number_of_scan_chains 2 [find / -design $DIGTOPLEVEL]
#set_attribute dft_mix_clock_edges_in_scan_chains true [find / -design $DIGTOPLEVEL]
################################################################################
## dft_drc is used instead of check_test command
################################################################################
check_dft_rules > ./reports/check_dft_rules.rpt

############################################33



Scan mapping: converting flip-flops that pass TDRC.
Scan mapping: bypassed.  You have to either
1) set attribute 'dft_scan_map_mode' to 'tdrc_pass' and run 'check_dft_rules' or
2) set attribute 'dft_scan_map_mode' to 'force_all'.

Scan mapping bypassed because no TDRC data is available: either command 'check_dft_rules' has not been run or TDRC data has been subsequently invalidated.

#for scan
connect_scan_chains

---------------------------------------------------------------------------

For synthesis which involves multiple power domains:
----------

read_power_intent -module TOP -cpf "../TOP.cpf"
redirect chk.cpf.detailed.rpt "check_cpf -detail"
commit_power_intent
verify_power_structure -lp_only -pre_synthesis -detail > $_REPORTS_PATH/digtop_verify_power.rpt

write_cpf -output_dir ${_OUTPUTS_PATH} -prefix ${DESIGN}_
write_power_intent -base_name ${_OUTPUTS_PATH}/TOP_m -cpf -design TOP


DC (Design Compiler):  This is the synthesis tool from Synopsys, which takes RTL as input and generates a synthesized netlist.


For running synthesis in Design Compiler:
-------------------------------------------------------------------
In synthesis, clk and scan_enable are set as ideal network, so they don't get buffered (they get buffered in PnR). Reset and all other pins are buffered as needed to meet DRC. This reset tree built in DC is again rebuilt in PnR during placement to make sure it meets recovery/removal checks.

steps in DC synthesis are as follows:


1. RTL opt: HDL-Compiler compiles HDL (performs translation and arch opt of design). DC translates HDL desc to components extracted from GTECH(generic tech) and DW(Design Ware) lib called as RTL opt. GTECH consists of basic logic gates and flops, while DW contains complex cells as adder, comparators, etc. these are tech independent.
2. Logic opt: DC then does logic opt. first, it does structuring which adds intermediate variables and logic structures to GTECH netlist. then it does flattening which converts combo logic paths into 2 level SOP rep. At this stage, all intermediate variables and it's associated logic structure are removed.
3. Gate opt: it optimizes and maps GTECH design to specific tech lib (known as target lib). It's constraints driven. It does delay opt, design rule fixing and area opt. Power Compiler used if static/dynamic power opt done.
4. Add DFT: Next Test synthesis is done using DFT Compiler, which integrates test logic into design.
5. Place and Route (PnR): PnR is done next, from which delays can be back annotated to design. DC can then resynthesize to generate better netlist.

Operating condition for any chip is defined via 3 conditions: Process (P), Voltage (V) and Temperature (T). Since these 3 uniquely determine the speed of transistor, we choose a particular PVT corner for running Synthesis. Usually we define 3 PVT corners (below ex is for a design in 250nm). The term max, min, etc refers to delay, so max corner means corner with maximum gate delay, i.e slowest corner.

NOM: P=TYP, V=1.8V, T=25C (TYP) => This is the typical or nominal corner where chip is supposed to run at nominal speed. Here PVT is specified as 1.8V, room temperature and nominal process.
MAX: P=WEAK, V=1.65V, T=150C (WC) => This is the worst case corner, where chip is supposed to run at the slowest speed. Here PVT is specified as 1.65V, high temperature and weak (slow) process. MAX implies this PVT gives you maximum delay (i.e slowest speed). You will note that voltage is -10% below typ. This is generally a safe voltage to choose as voltage is not supposed to fluctuate by more than +/- 10% even in worst case scenarios (as voltages are usually controlled by PMU, which hold voltage levels very tight. Most of the voltage fluctuations happen due to IR drop on and off chip).
MIN: P=STRONG, V=1.95V, T=-40C (BC) => This is the best case corner, where chip is supposed to run at the slowest speed. Here PVT is specified as 1.95V, low temperature and strong (fast) process. MIN implies this PVT gives you minimum delay (i.e fastest speed). Voltage here is usually +10% above typ

Since we want our design to be able to run in worst possible scenario, we choose WC (MAX) corner to synthesize our design. Then, our design is guaranteed to work across all OP conditions.

run DC:


dc_shell-t -2010.03-SP5 => brings up DC shell. dc_shell is shell mode (dc own shell), while dc_shell-t is tcl mode (dc shell which can accept tcl cmd too). dc_shell-xg-t is XG mode, which uses opt mem mgmt to reduce run time.
dc_shell-t -2010.03-SP5 -f ./tcl/top.tcl | tee logs/my.log => runs with cmds in top.tcl and keeps all info printing on screen to my.log
dc_shell-t -2010.03-SP5 -t topo -f ./tcl/top.tcl => To bring dc-shell-t in topo mode. This requires MilkyWay (MW) db. See section in synopsys ICC (PnR_ICC.txt)

When we run in DC shell above, it's a text based shell. We can also have GUI.
Design Vision is GUI for synopsys synthesis env. symbol lib is needed to generate design schematic. To start gui, either run "dc_shell -gui", or from within dc_shell, run "gui_start".
DC family:
1. DC Expert (compile cmd used).
2. DC Ultra(compile cmd used).

Help in DC: type "help" or "cmd_name -help" or "man cmd_name"


setup file for DC:


We have a setup file for DC that DC reads before invoking DC shell. This file is .synopsys_dc.setup and is usually put in the dir from where DC is invoked. This file has search paths, lib path and other variables setup. Note this file can be copied from some other project by using: cp dir1/.synopsys_dc.setup to dir2/.

.synopsys_dc.setup => This file can have all common settings that you want to apply to your design. It can source other tcl files or set parameters for DC. At a minimum, it needs to set search_path, target_library and link library.


set search_path "$search_path /db/pdk/tech45nm/.../synopsys/bin" => adds this path to default path to search for design and lib files

set target_library TECH_W_125_1.6_STDCELLS.db => this lib, which should be present in search path above, is used during compile to gen gate level netlist. worst case (wc) lib chosen, as we try to meet setup for wc corner. taget_library is used by opt engine to map design to, so it should have all stdcells that are required for mapping.

set link_library {* TECH_W_125_1.6_STDCELL.db }  => link_library (or link_path) is a superset of target_library. resolves references. First looks in DC mem (* means DC mem which has design files), then in specified .db (same as target_library files) for matching lib cell name and then any other libraries which are not target for opt, but may be present in design (as Macro, RAM cells). In DC, we don't need Clock cells (i.e buffers, inverters specifically made for clk tree), so in many companies, clk cells are all put in a separate library, so that we don't have to load unnecessary library cells during synthesis.

link library are synopsys .db files (liberty files in db format) and our design are *.ddc/*.db files. We put *, so that on top of liberty files, DC searches in all the designs already loaded in mem (i.e for module named A in top.db, it searches in A.db, before it looks for A in .lib files). If we omit *, it will cause link failures, as hier designs have modules, which it won't be able to find any more.

NOTE: Most lib/db files have file name same as library name within that file. i.e "TECH_W_125_1.6_STDCELL.db" is defined as library within the file "/db/tech45/.../TECH_W_125_1.6_STDCELL.db". "target_library" and "link_library" refers to file names ?? FIXME ??. Also, we can also provide the full path name of the file so that search_path is not needed for finding target and link libraries.

ex: set target_library "/db/tech/.../TECH_W_125_1.6_STDCELLS.db"


NOTE: In PT, we use PnR netlist which has Clk cells, so we add db for clk cells also when running PT.

NOTE: if we have hard IP blocks, then db files for those blocks should be included in link_library, and paths for those should be in search path. That way, we don't have to provide RTL code for that IP. DC sees that cell name in the db file present in any of target and link lib, and on finding them there, it doesn't complain about missing cell.

Ex: sram2048x32 (sram cell). We instantiate "sram2048x32" in RTL file digtop.v and also have a rtl file (sram2048x32.v) for this module. Then, when running DC, we don't analyze and synthesize rtl file "sram2048x32.v" (i.e this verilog file is not provided in list of RTL files). DC looks at module name "sram2048x32" and tries to find this cell in link_library. It finds this "cell (sram2048x32)" stmt in "sram2048x32_W_125_1.65.db" file, which is present in link library.At this point, tool is happy, otherwise it would search for "sram2048x32" module in any of the other rtl files. This is similar to what happens if we instantiate a latch (LATCH10) directly in RTL, then DC looks for that cell in target_library and link_library. It finds them in "TECH_W_125_1.6_STDCELL.db" file as "cell (LATCH01)" and hence doesn't complain, otherwise it would look for LATCH10 module in any of the RTL files being analyzed, and on not finding the module, it would complain.


#symbol_library => defines symbols for schematic viewing of design.
#synthetic_library => to specify any specially licensed DW lib. Std. DW lib are included by default.

NOTE: only .db library can be read in DC. If we have library in .lib format, then we need to convert it to .db using cmds below and then use those.
#read_lib /db/.../rom_W_150_1.65.lib => This file will be read and stored as *.db file in mem. list_libs will now show this lib too as .db
write_lib rom_W_150_1.65.db -f db -o /db/.../rom_W_150_1.65.db => optional. This saves file in path specified so that next time .db file are directly available to be read by DC (saves run time??).

#if we want to do max/min timing using max/min lib, then we need to do as explained in create_views of PnR_ICC.txt.
#list_libs => lists which lib are used as max lib (denoted by M), and which for min lib (denoted by m). We should see all db library, and in which db file they are. dw_foundation.sldb, gtech and standard.sldb lib are also shown with their paths.
#report_lib => reports contents of lib as units, op cond, WLM and cells. Use this to see library units for cap, resistance, etc present in the library.
#which abc.db => shows absoulte path for this .db file that is being used currently.

Difference between target library and link library, and why do we need both?

Target lib are lib that you target for mapping your RTL to gates. These are std cells which are provided as target. DC chooses from amongst this set, a subset of cells to optimize the final mapped design. On the other hand, link lib resolves references in the design by linking the instances, references in the RTL with the link libraries. So, in link lib, we provide target lib plus any IP as memory, PLL, analog blocks etc, which are needed strictly for linking. These IP lib are not needed for optimizing but just for linking (as they contain just 1 lib that we force to link). So, link lib contain target lib + extra macro libs.

So, the question is why do we need both, when we are specifying same libraries in target and link? Reason might be that it's easier for the tool to have different lib settings for "OPTIMIZATION-MAPPING" & "LINKING".That way it knows what to pick for optimizing and mapping, and what to use for strick one to one mapping.


DC script:

Below is a sample DC script that can be used to run synthesis. We start with the top most file known as top.tcl.

top.tcl: this is the main tcl file that is sourced by the DC tool from cmd line. All DC cmds are in this file, and DC starts running cmds from this file until it reaches end of this file. These are the various sections of this script in tcl:


1. Read all RTL files, and link the library of cells/IP.


#source some other files
#NOTE: for source to work, file path has to start with ./ so that it looks for that file in unix dir, else DC will look for that file in it's memory which doesn't have that file, so it will error out.
source ./setup.tcl => In this file set some variables, i.e "set RTL_DIR /db/dir" "set DIG_TOP_LEVEL  digtop" or any other settings

#this is to suppress warnings during analyze/elaborate
suppress_message {"LINT-1" "LINT-2" "LINT-10" "LINT-33" "LINT-8" "LINT-45" "VER-130" }

#read verilog/vhdl/systemVerilog files. DC can also rd in .ddc & .db (snps internal format, .ddc recommended), equation (snps equation format), pla (berkeley espresso PLA format) and st (snps state table format). 2 ways:
1. Analyze and elaborate => analyzes (compiles, checks for erros and creates an intermediate format) and elaborates HDL design, and stores it in snps lib format file for reuse. All subdesigns below the current design are analyzed, and then elaboration performed only at top level. During elaboration, RTL compiler builds data structures, infers registers in design, performs high level HDL optimization, and checks semantics. It translates the design into a technology-independent design (GTECH) from the intermediate files produced during analysis. It replaces  HDL arithmetic operators in the code with DesignWare components and automatically executes the link command, which resolves design references
After elaboration, RTL compiler has internally created data structure for the whole design on which it can perform operations. cmds:

analyze -format verilog|vhdl [list a.v b.v] => on doing analyze, WORK dir created which has .pvl, .syn and .mr file for each verilog module. Runs PRESTO HDL Compiler for RTL files, and then loads all .lib files.
analyze -autoread [list a.v b.v c.vhd] => to auto analyze mix of verilog and vhdl files

elaborate <top level verilog module name, VHDL entity or VHDL configuration> => ex: elaborate digtop => loads gtech.db and standard.sldb from synopsys lib, and link library *_CORE.db and *_CTS.db from user defined lib, and then builds all modules. It infers memory devices (flip flops, and appends _reg to the net name storing the value i.e net <= value), analyzes case stmt (full case[all possible branches specified so combinatorial logic synthesized, else latch synthesized for non full case], parallel case[case items don't overlap, so mux synthesized, else for non-parallel case, priority checking logic synthesized])

2. read_file -f <verilog|vhdl|db/edif> filename => we can also use read_verilog, read_vhdl, read_db and read_edif, instead of specifying file type in read_file. this can be used to read in gate level netlists also that are mapped to a specific tech. This also performs analysis and elaboration on HDL designs written in RTL format, but it elaborates every design read, which is unnecessary. Only top level design needs to be elaborated. read_file is useful if I want to reuse previously synthesized logic in my design.

#We use 1st way shown above: do analyze and elaborate and then set current_design
analyze -format verilog [list "/db/.../global.v" "/db/.../utils.v" ... "/db/.../digtop.v" ]
elaborate      digtop => since digtop is top level module
current_design digtop => current design always needs to be set to top level

#for design references during linking, DC uses the system variables link_library and search_path along with the design attribute local_link_library to resolve design references. link library has library cells (from .lib) as well as subdesigns(modules inside top level module) that the link cmd uses.
link => resolves references. and connects the located references to the design.

#To see the reference names, use the following command:
#get_references AN* => returns coll of instances that refer to AN2, AN3 etc. ex o/p = {U2 U3 U4}
dc_shell> report_cell [get_references AN*]  => shows references for AN2, AN3, etc for cells and the library to which it's linked. At this stage, lib is GTECH and all references are from this GTECH library. so, use * to see all references.
dc_shell> report_cell [get_references *]  => this shows all ref for cells present in top level design. If there is any logic stmt (i.e assign = A&B; etc) in top level, then it gets mapped to GTECH cells as GTECH_OR, GTECH_AND, etc and gets reported too.
Cell                      Reference       Library             Area  Attributes
--------------------------------------------------------------------------------
B_0                       GTECH_BUF       gtech           0.000000  c, u
C29                       *SELECT_OP_2.1_2.1_1            0.000000  s, u
C54                       GTECH_AND2      gtech           0.000000  u
ccd_top                   ccd_top                         4.000000  b, h, n, u
revid_tieoff              TO010           PML30_W_150_1.65_CORE.db  1.750000
--------------------------------------------------------------------------------
Total 42 cells                                            172.500000


2. specify constraints: env constraints (PVT), design constraints(area/max_fanout) & timing constraints(clks/false_paths)

constraints: IMP: all constraints are specified in sdc format. see sdc.txt for details of constraints. 2 set of constraints:
1. env_constraints = i/p driver, o/p load, i/p delay, o/p delay, dont_touch, dont_use
2. design constraints:
   A. design rule const: max_fanout, max_transition, max_cap
   B. optimization constraints:
      I. timing constraints = clks, generated clk, false path, multicycle paths, (if false_paths refer to gate level netlist, then initial mapped netlist needed)
      II. power contraints = max_power
      III. area constraints = max_area


A. environment constraints: as op cond (PVT), load (both i/p and o/p), drive (only on i/p), fanout(only on o/p) and WLM.

#set_operating_conditions: see in PT OCV section for details of this cmd.
set_operating_conditions -max W_150_1.65 -library STD_W_150_1.65_CELL.db (Instead of set_operating_conditions we can also use "set_max_library STD_W_150_1.65_CELL.db) => Here, we are using our max delay library for both setup/hold runs. We can check this by looking in reports/digtop.min_timing.rpt.

FIXME # LBC8/PML30 lib uses 1.8V PCH_D_1 and NCH_D_1 (Lmin=0.6um drawn), cell height=13.6um, 8routing tracks available, with 3,4,5 Layer for metal routing. 1X inv has i/p cap of 6ff. Power is about 0.1uW/gate/Mhz (CV^2f= 6ff*1.8^2*10^6/MHz = 0.15uW/MHz for inx1) FIXME

#WLM: wire load model: used only when design is not in physical mode.
set auto_wire_load_selection true
#set_wire_load_model "6K_3LM" => sets wire load model on current design to something other than the default one set in .lib file. Usually for larger designs, we set WLM manually, since the default WLM may be smaller designs, and so too optimistic.

# Setting enclosed wire load mode. mode may be top|enclosed|segmented
set_wire_load_mode enclosed => Here, multiple WLM are specified for various sub-modules, so for a net which traverses multiple sub-modules, WLM of that higher level module used which completely encompasses the net. When mode is "top", then WLM of top level module used for all nets in design. Since WLM is defined only for top level design above, WLM for lower level sub-modules are chosen as default when mode=enclosed or segmented.

report_design => see in PT OCV section for details of this cmd. shows all libs used, op cond used (PVT from WLM used, etc.

##### DC TOPO flow starts: see in PnR_ICC.txt for details. comment out the WLM portion above for DC-TOPO.

#create MW lib if one doesn't exist already. From next time, we can just open created desgin lib.
create_mw_lib -technology /db/DAYSTAR/design1p0/HDL/Milkyway/gs40.6lm.tf \
    -mw_reference_library "/db/DAYSTAR/design1p0/HDL/Milkyway/pml48MwRefLibs/CORE /db/DAYSTAR/design1p0/HDL/Milkyway/pml48ChamMwRefLibs/CORE" \
    -open my_mw_design_lib
open_mw_lib my_mw_design_lib
set_check_library_options -cell_area -cell_footprint
check_library

#set TLU+ files instead of WLM.
set_tlu_plus_files \
    -max_tluplus /db/DAYSTAR/design1p0/HDL/Milkyway/tlu+/gs40.6lm.maxc_maxvia.wb2tcr.metalfill.spb.nlr.tlup \
    -min_tluplus /db/DAYSTAR/design1p0/HDL/Milkyway/tlu+/gs40.6lm.minc_minvia.wb2tcr.metalfill.spb.nlr.tlup \
    -tech2itf    /db/DAYSTAR/design1p0/HDL/Milkyway/mapping.file
check_tlu_plus_files

######DC-TOPO flow ends

#naming convention for lib objects varies b/w vendors, but for SNPS, it's "[file:]library/cell/[Pin]" (file and pin are optional). Ex: to access AND2 cell: set_dont_touch /usr/designs/Count_16.ddc:Count_16/U1/U5.

#i/p drives
set_driving_cell -lib_cell IV110 [all_inputs] => all i/p ports driven by IV110
#set_drive/set_input_transition

#i/p and o/p loads. (i/p load needed when there is extra load due to wire or extra fanout not captured in input gate cap)
set output_load    [get_attribute [get_lib_pins {"PML30_W_150_1.65_CORE.db/IV110/A"}] capacitance]
set output_load_4x [expr 4 * $output_load]
set_load $output_load_4x [all_outputs] => setting FO=4 load on all o/p pins. (set_load can be used on any net, port)
#NOTE: If we set o/p load to be very high (i.e 1pf), then all o/p ports will get driven by largest INV/BUF as any other logic gates don't have that drive capability to drive such a high load. So, on such ports Isolation buffers may not be needed in PnR flow, as buffers are already there from synthesized netlist (if we do put buffers in PnR flow, then we will have 2 buffers back to back for each port, resulting in area wastage)

global constraints:
-----------
#set_dont_use
set_dont_use PML30_W_150_1.65_CORE.db/LA* => don't use latches from lib
set_dont_use PML30_W_150_1.65_CORE.db/DTB* => don't use D-flops with preseet and clr

#set_dont_touch => prevents specified object (net,instance,etc) from being modified duing optimization.
Ex: set_dont_touch [get_cells {TWA/FF1}] => prevents the specified instance from being modified
Ex: set_dont_touch [get_nets -of_objects [get_cells {TWA/FF1}]] => i/o preserved for that cell.
set_dont_touch scan_inp_iso => prevents module instance from being modified.

B. design constraints: design rule and optimization constraints. For initial synthesis, we only provide env_constraint and not design constraints (as we just need gate mapping for RTL to write our false path file)
----------------
1. design rule constraints: usually provided in .lib. typical constraints are set_max_transition, set_max_fanout, set_max_capacitance. These cosntraints are associated with pins of the cells in lib, but eventually end up constraining nets of design. DC prioritizes these over opt constraints, and tries not to violate them. clk nets and constant nets have design rule fixing disabled by default, scan nets do not. i/p ports of design have max_cap figured out by cells driving i/p port (using set_driving_cell in sdc file), while o/p ports have max_cap figured out by cells driving o/p port (size of cells driving o/p port is picked up based on load on o/p port (using set_load in sdc file). o/p port max_cap is seldom violated because DC picks up right size gate to drive the o/p load. However, i/p port max_cap may be violated if we didn't pick right size buffer to drive heavily loaded pins (as i/p clk pin, reset pin, etc).
NOTE: For bidir pins, it's treated as both i/p and o/p pin, so it has a driver as well as a load. That makes it harder to meet max_cap requirement of external driver if external driving buffer is not chosen properly, while a large cap load is placed on the pin (It may easily meet internal driver max_cap requirement as the tool can size the internal driver appr). It may also fail max_transition, as if max_cap gets violated, then depending on how bad it failed, the external driving buffer may need to extrapolate timing for excess cap load, resulting in max_transition violation. To avoid this, choose appr external driver for bidir pin.
###design rule const:  We don't set any DRC as all these are picked as per .lib

2. opt constraints: opt const for timing provided later during incremental compile.
set_max_area       0

----
#set_fix_multiple_port_nets: sets "fix_multiple_port_nets" attr on the design specified.
#This attribute controls whether compile inserts extra logic into the design to ensure that there are no feedthroughs, or that there are no two output ports connected to the same net at any level of hierarchy. The default is not to add any extra logic into the design to fix such cases. Certain three-state nets cannot be buffered, because this changes the logic functionality of the design.
#-all: insert buffers for o/p directly connected to i/p(-feedthrough), inserts buffers if a driver drives multiple output ports(-outputs) and duplicate logic constants so that constants drive only 1 o/p port
# -buffer_constants: buffers logic constants instead of duplicating them.
set_fix_multiple_port_nets -all -buffer_constants [get_designs *]

#set_isolate_ports: Specifies the ports that are to be isolated from internal fanouts of their driver nets.
#-driver BU140 => BU140 or other size buffer used to isolate. By using -force, we force the driver to be the size specified (i.e BU140 only, no other size allowed), and also force isolation to be done on all ports specified, even if they don't need isolation.
#we don't put isolation cells during synthesis, as we do it during PnR.
#set_isolate_ports -driver BU140 -force [all_inputs]
#set_isolate_ports -driver BU140 -force [all_outputs]

----------------------------
# Uniquify after applying constraints
current_design $DIG_TOP_LEVEL
link
uniquify => NOT necessary, since this step is done as part of compile. Removes multiple-instantiated hierarchy in the current design by creating a unique design for each cell instance. So, if you do get_designs * => it now shows multiple instances of clk_mux with clk_mux_1, clk_mux_2, etc. So, each of these clk_mux_* have the same rtl, but they can now be optimized separately.

#Provide physical info (area, placement, keepout, routing tracks, etc) abt floorplan if in DC-TOPO mode. 3 ways:
1. write_def within ICC, and then import it into DC by using extract_physical_constraints cmd.
ex: extract_physical_constraints {design1.def ... design2.def}
2. write_floorplan cmd in ICC which generates a tcl script, and then read that file using read_floorplan.
ex: read_floorplan -echo criTop.all.fp.tcl => this tcl file is generated by write_floorplan cmd in ICC, and used here in DC.
3. Manually provide physical info. Put these constraints(die area, port locations, macro, keepout, etc) in a tcl file and source it. these constraints are the one that we use in ICC to force the tool to generate desired placement.

##### opt const (speed): clk related info here (set_input_delay, set_output_delay provided during incremental compile)
create_clock -name "spi_clk" -period 50 -waveform     { 2 27 } [get_ports spi_clk] => 20M clk, rising edge at 2ns and falling edge at 27ns.

set_clock_uncertainty 0.5 spi_slk => adds 0.5 units skew to clk to model skew during CTS in PnR.

# generated Clock: NOTE: this cmd sometimes requires the presence of synthesized netlist, as the target pin list may be o/p of flops, etc so, we use this cmd after the initial compile.
create_generated_clock -name "reg_clk" -divide_by 1  -source [get_ports clock_12m] [get_pins clk_rst_gen/pin1] => apply waveform on pin "clk_rst_gen/pin1"

#optional ideal attr => not needed for DC. clk nets are ideal nets by default.
#set_ideal_network -no_propagate {clk1 clk2} => marks a set of ports or pins  in  the  current  design  as sources  of an ideal network. compile command treats all nets, cells, and pins on the transitive fanout of these objects  as ideal (i.e no delay). transition time of the driver is set to 0ns. Propagation  traverses through combinational cells but stops at sequential cells. In  addition  to disabling timing updates and timing optimizations, all cells and nets in the ideal network have the dont_touch attribute  set. "-no_propagate" indicates that the ideal network is not propagated through logic gates (i.e logic gates encountered are treated as non-ideal with non-zero delay). By default, ideal property is propagated thru gates. NOTE: during report_timing, we see transition time on these ports/pins as 0ns, resulting in no "max_transition_time" violations.  
#set_ideal_latency 2 clk1
#set_dont_touch_network [get_clocks *]
#set_propagated_clock [all_clocks]

#set ideal n/w for scan_enable, so that they don't get buffered in DC, will be buffered in PnR
#set_ideal_network -no_propagate {POR_N I_CLK_GEN/POR_N_SYNCED} => NOT needed. POR_N port only goes to 2-3 flops as it gets synced first, then the synced version goes to all flops. We don't set any of these ports to ideal as that will prevent tool from putting buffers on these paths. These paths result in max_cap, max_trnasition viol (not timing viol as async paths aren't checked for timing in DC), so DC will buffer these to prevent those viol. If we do not want to buffer the reset tree in DC, we can use this cmd to prevent buffering in DC, and then buffer it during PnR. However, the sdc file exported to PnR tool should have this cmd removed so that PnR tool can buffer it. Also, no false_path should be set starting from "POR_N_SYNCED" pin as it's a real path. We can set false path starting from "POR_N", but even that's not required

#set_ideal_network -no_propagate I_CLK_GEN/SCANRESET => NOT needed as it feeds in same reset tree.
#set_ideal_network -no_propagate scan_en => this done during scan stitching. Here, scan_en not set to ideal is OK, as this net is not connected to any flop (it's a floating net at this stage, and scan_enable/scan_data pin of all flops is tied to 0 or 1). So, no opt takes place on this net. Later during dft scan stitching step, scan_en gets tied to pin of all flops, that is where we set it to ideal, so that it doesn't get buffered.

# Specify clock gating style => Sets  the  clock-gating  style  for the clock-gate insertion and replacement
#-sequential_cell none | latch => 2 styles. A. latch free ( no latch, just and/or gate, specify none). B. latch based (latch followed by and/or, default)
#-positive_edge_logic {cell list | integrated} => for gating +ve FF inferred from RTL. For latch based, cell list must be AND/NAND (can also specify latch in cell list). For latch-free, cell list must be OR/NOR. integrated => Uses a single special integrated clk gating cell from lib instead  of  the  clock-gating circuitry (i.e latch followed by and/nand). With integrated option, we can say whether enable signal is active low and if clk is inverted within the integrated cell. when using integrated clk gaing cells, setup/hold are specified in lib, so separate -setup/-hold options are not required. Tool identifies clk gating cells in lib by looking for clock_gating_integrated_cell.
For CGP, it's "latch_posedge", and for CGPT, it's "latch_posedge_precontrol".
For CGN, it's "latch_negedge", and for CGNT, it's "latch_negedge_precontrol".
#-negative_edge_logic {cell list} => same as above except that for latch based, cell list must be OR/NOR (can also specify latch in cell list). For latch-free, cell list must be AND/NAND.
#-control_point none | before | after => Final_En = (En | Scan_en). Before or after determines whether to put the OR gate before or after the latch. The  tool  creates  a new  input  port to provide the test signal.  The control points must be hooked up to the design level test_mode  or  scan_enable port using the insert_dft command.
#-control_signal scan_enable | test_mode => Specifies  the test control signal.  If an input port is created and the argument is  scan_enable, the name of the port is determined by the test_scan_enable_port_naming_style variable, while for test_mode, the name of the port is determined  by  the  test_mode_port_naming_style  variable. test_mode signal is the one that is asserted throughout scan testing, while scan_enable signal is asserted only during scan shifting (All FFs have scan_enable  the select line of their internal mux). Ususally it's set to scan_enable.

set_clock_gating_style -control_point       before \
                       -control_signal      scan_enable \
                       -positive_edge_logic integrated \
                       -negative_edge_logic integrated

3. synthesize/compile design (initial stage):


#2 types of compile strategy:
A. top-down: top level design and all it's subdesigns are compiled together. Takes care of interblock dependencies, but not practical for large designs, since all designs must reside in memory at same time.
B. Bottom-up: individual subdesigns are constrained and compiled separately. After successful compilation, the designs are assigned the dont_touch attribute to prevent further changes to them during subsequent compile phases. Then the compiled subdesigns are assembled to compose the designs of the next higher level of the hierarchy, and those designs are compiled iteratively until the top level design is synthesized.

# Initial Compile
#-scan: replaces normal flops with scan version. connects scan pins to tiehi or tielo (doesn't do actual stitching of scan pins here)
#DC uses design rule cost and opt cost to determine cost fn. use -no_design_rule to disable design rule cost (max_tran, max_fo, etc) and -only_design_rule to disable opt rule cost (delay, power, area, etc). hold violations are fixed only if set_fix_hold and set_min_delay is specified for design. otherwise, only max_delay (not min_delay) is part of cost fn. We can reorder priority of design/opt constraints to get new cost fn by using set_cost_priority.
#-gate_clock: enables clk gating opt as per options set by set_clock_gating_style cmd. clk gates inserted are wrapped inside a clk_gating module which has CG* cell.
#-no_autoungroup: all user hier are preserved (i.e ungrouping is disabled). Required, else ungrouping removes hier boundaries and flattens the netlist to optimize across modules. Without this, some hierarchies were being flattened to implement clock gating
compile_ultra -scan -no_design_rule -gate_clock -no_autoungroup

#check_design => checks synthesized design for consistency.
#check_timing
#report_clocks

# reports after compile
set rptfilename [format "%s%s" $mspd_rpt_path $DIG_TOP_LEVEL.initial_area.rpt ]
#redirect => Redirects the output of a command to a file. -append appends o/p to target
redirect $rptfilename {echo "digtop compile.tcl run : [date]"}
redirect -append $rptfilename {report_area -hier} => reports total area for combo & non-combo logic. also reports total no of cells at top level of design (in module digtop, counting 1 for each sub-module and 1 for each stdcell), no. of I/O ports and no of nets (total no. of wire in digtop). -hier reports it for all hier modules. area is taken from area in .lib file (RAM/ROM IP usually have incorrect area, as they aren' scaled in terms of NAND2 equiv size)
redirect -append $rptfilename {report_reference -hier} => reports all references in current instance (if current inst set) or current design (default). It reports all instances in top level module of current design, which has subdesigns(as other modules), as well as some std cells connecting these modules together). -hierarchy option goes thru the hier of sub modules and reports all leaf cell refrences.

#NOTE: we can use above 2 cmds for any netlist to report total number of gates. For ex, to find out total gates in routed netlist, do:
read_verilog ./DIGTOP_routed.v
current_design DIG_TOP  
report_area -hierarchy

#NOTE: no constraints of any sort (i/p, o/p delay, false paths, etc) are applied above, as we just want to get a verilog netlist mapped from RTL. Even clk wasn't required to be declared, as we aren't running any timing on this netlist.
# Initial Compile
-----------------------------------------------------------------------------
# Clean up
# removes unconnected ports from a list of cells or instances, perform link and uniquify before this command
# -blast_buses -> if a bus has an unconnected port, the bus is removed.
#find => Finds a design or library object. -hierarrchy means at any hierarchy of design. Ex: remove_unconnected_ports -blast_buses find( -hierarchy cell, "*")
remove_unconnected_ports -blast_buses [find -hierarchy cell *]

#to ensure name consisitency b/w netlist and other layout tools => define_name_rules and change_names cmd used to convert names. define_name_rules defines our own rules, and change_names applies the change to the netlist for the particular rule. There are already std rules for verilog/vhdl. Sometimes, we keep these cmds in .synopsys_dc.setup, so that they are always applied.
# change_names of ports, cells, and nets in a design. -hierarchy Specifies that all names in the design hierarchy are to be modified. (report_name_rules shows rules_names are sverilog,verilog,verilog_1995 and vhdl). This cmd should always be applied before writing netlist, as naming in the design database file is not Verilog or VHDL compliant.
#report_names => shows effects of change_names w/o making the changes.
change_names -rules verilog -hierarchy => std verilog rule applied to all hier of netlist.

#define_name_rules <rule_name> -map { {{string_to_be_replaced, new_replaced_string}} } -type cell
define_name_rules     reduce_underscores   -map { {{"_$", ""}, {"^_", ""}, {"__", "_"}} } => names a rule which removes trailing underscore, starting underscore and replaces double underscore with a single underscore.
change_names -rules   reduce_underscores   -hierarchy => rule applied

define_name_rules    reduce_case_sensitive   -case_insensitive
change_names -rules  reduce_case_sensitive   -hierarchy -verbose

#not sure ???
apply_mspd_name_rules_noam

------------------------------------------------------------------------------
#DC doesn't automatically saves designs loaded in memory. So, save design before exiting.
#save design using write: saves in .ddc, .v, .vhdl format
#save design using write_milkyway: writes to a milkyway database.
write -format ddc     -hierarchy -output ./netlist/${DIG_TOP_LEVEL}_initial.ddc \ => preferred, .ddc is internal database format
write -format verilog -hierarchy -output ./netlist/${DIG_TOP_LEVEL}_initial.v => verilog format (also supports systemverilog (svim) and VHDL format o/p.

4. synthesize/compile design (incremental stage):


# Apply constraints for func mode, when scan exists
source tcl/case_analysis.tcl => specify which scan related pins need to be tied for func mode. It has these stmt:
#set_case_analysis => Sets  constant  or transitional values to a list of pins or ports and prop thru logic for use by the timing engine. The specified constants or transitional values are valid only during timing analysis and do not alter the  netlist.

set_case_analysis 0 scan_mode_in => we force Scan_mode to 0, as we want to see timing paths b/w diff clocks. false paths take care of bogus paths b/w clock domains. forcing it to 1 will cause all clocks to be the same clock (i.e scan_clk), so, we won't be able to see inter clock paths. If we don't force scan_mode at all, then both scan_mode=0 and scan_mode=1 timing analysis is run.
#set_case_analysis 0 scan_en_in => we should NOT force this to 0, as we want timing for scan shift paths also.

# all constraints for all i/o pins here (opt constraints)
source tcl/constraints.tcl => Put all i/p o/p delays here

#we may want to leave setting i/p delays, so that in PT we can see them as "endpoints not constrained" warning. This helps us to see which all are pins which are going thru meta flop.
set_input_delay 0.2 -clock clk1 [remove_from_collection [all_inputs] [get_port {clk1}]] => sets 0.2 unit delay on all i/p pins (except clk1 port) relative to clk1.
sET_output_delay 0.4 -clock clk1 [remove_from_collection [all_outputs] [get_port {spi_miso}]] => 0.4 delay for all o/p ports except spi_miso

#create generated clocks here since it may refer to pins of flops, etc which may only be present in synthesized netlist
source tcl/gen_clocks.tcl
#all clks treated as div by 4 clk, since very large divided clks will cause longer run time.
#we don't have long delay paths, so even if we define very fast clks as div by 4 clk, we should not see any failing paths for setup. It will screw up hold time calc, as PT treats hold time based on no of clk edges that lies in b/w one setup path.
# Div 4 clk
create_generated_clock -name "clk_1600k"        -divide_by 4  -source [get_ports clkosc] [get_pins Iclk_rst_gen/clk_count_reg_1/Q]
create_generated_clock -name "clk_100k"         -divide_by 4  -source [get_ports clkosc] [get_pins Iclk_rst_gen/clk_count_reg_5/Q]

#ram latch clock (since clk signal, generated as a pulse, may be o/p of flop). We can do div by 1 also.
create_generated_clock -name "clk_latch_reg"    -divide_by 2  -source [get_ports clkosc] [get_pins Iregfile/wr_strobe_spi_sync_reg/Q]

#gated clocks
#create_generated_clock -name "spi_clk_gated"   -divide_by 1  -source [get_ports spi_clk]   [get_pins spi/spi_clk_gate/Q]

# Propagate clocks. NOTE: we don't propagate clk, since we don't have any buffers in DC netlist. clks treated as ideal
#set_propagated_clock [all_clocks]

# Apply false-paths: In false paths, we define false paths
# NOTE: false paths only related to setup timing are checked here. If log report indicates an ERROR in any line, it doesn't take any false path from that line into consideration (i.e it doesn't expand the wildcards to choose paths that match and drop paths that don't exist, however PnR tool does expand the wildcards and choose paths that match and drop paths that don't exist, without reporting any error, so be careful). Hold, async, clk-gating paths aren't checked (unconstrained paths) durng synthesis, however they are checked during PnR. so, we might have to add extra false paths when running PnR. These added paths may give ERROR when synthesis is re run, however we can just ignore such errors. Or instead of adding these extra false paths in DC, we can create a new false path file in PnR, and add this file to existing false path file from DC.
#set_disable_timing [get_cells {test_mode_dmux/*}]

source -echo tcl/false_paths.tcl
#set_false_path -from {POR_N POR_N_SYNCED SCAN_RESET} => Not needed as these cause recovery/removal violations which DC doesn't check for. Only scan_en pin needs to be set to false_path as it causes real timing violation due to large transition time. However if scan_en pin is set to ideal_network, then fasle path not needed for scan_en as transition time=0ns.

# Apply multi-cycle paths
source tcl/multicycle_paths.tcl

# Incremental Compile with high effort
source tcl/compile.tcl
This has:
#here we do design rule and opt rule fixing
#-incremental performs only gate level opt and not logic level opt. Resulting design is same or better than original.
#-map_effort high => default effort is medium. high effort causes restructuring and remapping the logic around critical paths. It changes the starting point, so that local minima problem is reduced. It goes to the extreme, so is very CPU intensive.
compile_ultra  -incremental -scan -area_high_effort_script -gate_clock -no_autoungroup

5. generate reports:


#generate reports:
report_area, report_reference => in area.rpt
report_timing -delay max -max_paths 500 => report setup in max_timing.rpt (this has timing with scan_mode=0, so has all interclock paths)
report_timing -delay min -max_paths 500 => report hold in min_timing.rpt => this report should be clean if no clk_uncertainty is defined. This is because c2q delay for flops is greater than hold requirement of flops, so with ideal clock (no clk delays/buffering anywhere), all flops will pass holdtime req.
check_design, report_clock_gating, report_clocks, check_timing, report_disable_timing => in compile.rpt
report_clock_gating => reports no of registers clk gated vs non-clk gated. It also shows how many CG* cells got added to do clk gating.
report_constraint => lists each constraint, and whether met/violated, also max delay and min delay cost for all path groups. -all_violators only reports violating constraints.

#cleanup netlist and then write netlist
cleanup as done in compile_initial (remove_unonnected_ports, define_name_rules, change_names)
write -format verilog -hierarchy -output ./netlist/digtop.v

6. Insert Scan:


#SCAN: DFT compiler (separate license) is invoked for scan. DFTMAX license is needed for scan compression.
#Insert Scan
set_ideal_network [get_ports scan_en_in] => scan_en_in pin is used during scan shifting (defined in setup.tcl). By setting it as ideal, DFT compiler doesn't buffer the signal as all cells/net on this n/w have "dont_touch" attr set. To buffer it, put driving cell using set_driving_cell on this port, so that DFT Compiler can buffer it appr. In DC, we don't buffer this signal, as we buffer it during PnR. NOTE: if scan_en_in is internal net, we should do this:
set_ideal_network -no_propagate {u_DIA_DIG/u_DMUX_DIG/U5/Y} => We should choose gate o/p pin and not the o/p scan_en net ({u_DIA_DIG/u_DMUX_DIG/scan_en}). Else ideal network is not applied to gate o/p, so that gate has large transition time, so tons of viols.

#set_false_path -from scan_en_in => setting any path flowing thru Scan_en and ending at clk as false. Since we have set scan_en as ideal n/w, we don't need false path for this as transition time=0ns. However if ideal n/w is not set for scan_en, then this takes care of cases where scan_en i/p delay causes any setup/hold violation at clk. If we don't do this, we see paths with scan_en failing as they have high FO and high cap, so large delay and large transition time.  We can NOT take care of this by setting small max_delay for setup and large min_delay for hold, because the high FO will cause it to fail timing nonetheless. NOTE: this is not equiv to setting case analysis for scan_en=0, as that removes scan paths (flop to flop) from any timing analysis. We want to see timing for both scan/non-scan paths.
#IMP NOTE: both false_path and ideal_network for scan_en pin should be removed in EDI (if using sdc constraints generated from DC), since scan_en path is real, as we have clk toggling within a cycle of scan_en toggling in Tetramax patterns, so it better meet timing. We need to run gate sims on teramax patterns to make sure we meet timing.

source tcl/insert_dft.tcl => this file has following lines in it:
#sets all scan configuration details as shown below:
#set test timing variables. leave these at default values if Tetramax is used to gen test patterns.
#set test_default_delay 0
#set test_default_bidir_delay 0
#set test_default_strobe 40
#set test_default_period 100 => default scan period is 10MHz.

######define test protocol using set_dft_signal cmd.
#set_dft_signal=>Specifies the DFT signal types for DRC and DFT insertion.
#-view existing_dft | spec => existing_dft implies that the  specification  refers  to  the existing  usage  of  a port, while spec (the default value) implies that the specification refers to ports that the tool must use during DFT insertion. spec view is prescriptive and specifies actions that must be taken. It indicates that signal n/w doesn't yet exist and insert_dft cmd must add it. An example of this is ScanEn signal (even though ScanEn port exists, the signal n/w is not there, so it's prescriptive). existing_dft view is descriptive and describes an existing signal n/w. An example is system clk that is used as Scan Clk (here clk n/w already exists since system clk is used as scan clk, so descriptive). So, scan_clk, reset, scan_mode are existing_dft as n/w is already there, but SDI, SDO, ScanEn are spec as that n/w needs to be built. view is used for many DFT cmds as set_dft_signal and set_scan_path.
Ex: set_dft_signal -view existing_dft -port A -type ScanEnable => when working with dft inserted design, indicates that port A is used as scan enable. indicates that ScanEnable n/w does exist and should be used. This is NOT true in most designs as n/w never exists for scan_en pin. In this case, tool will create a new port "A" and connect it to scan_en pin of all flops
Ex: set_dft_signal -view spec -port A -type ScanEnable => when preparing a design for dft insertion, specifies that port A  is used as scan enable. indicates that ScanEnable n/w doesn't exist yet. This is true for most designs, so use this for scan_en pin.
#-type specifies signal type, as Reset, constant, SDI, SDO, ScanEn, TestData, TestMode. Constant is a continuously applied value to a port.
#-active_state => Specifies the active states for the following signal types: ScanEnable, Reset, constant, TestMode, etc. active sense high or low

#define clocks, async set/reset, SDI, SDO, SCAN_EN and SCAN_MODE. We don't need to define SCAN_CLK as SCAN_MODE forces clock to scan clk (mux chooses b/w SCAN_CLK or FUNC_CLK), and it is traced all the way to the i/p port (as later during create_test_protocol, we say -infer_clock). We don't define async set/reset as we force them to 0, when scan_mode=1 (in RTL itself).
set_dft_signal -view existing_dft -port scan_mode_in -type Constant -active_state 1  => scan_mode_in pin is used during scan mode and is high throughout scan (defined in setup.tcl). existing_dft states that scan_mode n/w exists so the tool doesn't need to do anything to add the n/w.
set_dft_signal -view spec  -port  spi_mosi       -type ScanDataIn => SDI is spi_mosi
set_dft_signal -view spec  -port  spi_miso       -type ScanDataOut => SDO is spi_miso
set_dft_signal -view spec  -port  scan_en_in     -type ScanEnable  -active_state 1 => SE (for shifting during scan) is scan_en_in, and it needs to be "1" for shifting to take place. If scan_en is an internal pin, then we do:
 set_dft_signal -view spec   -hookup_pin {u_DIG_sub/scan_enable} -type ScanEnable  -active_state 1 => pin may be port of sub-module or o/p pin of a gate as "u_DIG_sub/u_sub2/U5/Y". Preferred to use port of sub-module as gate name may change every time tool is run.
#NOTE: in all cmds above, if above scan ports don't exist, then tool creates new ports.

#if needed, set_dft_signal for scan clk, scan reset and scanmode => NOTE these are "exist" view and not "spec" view
set_dft_signal -view exist -type ScanClock   -port SPI_SCK -timing [list 10 [expr ($SCANCLK_PERIOD/2)+10]] => changed freq of scan clk, so that design runs slower
set_dft_signal -view exist -type Constant    -port SCANRESET      -active_state 1
set_dft_signal -view exist -type Constant    -hookup_pin {u_SPT_DIG/auto/Scan_Mode_reg/Q}  -active_state 1 =>  Preferred to use port of sub-module for hookup_pin as in extreme case, flop name may change every time tool is run.

####do all scan related configuration.
#set_scan_element: excludes seq cells from scan insertion, reducing fault coverage
set_scan_element false [find cell test_mode/scan_mode_out_reg] => default is true which means all  nonviolated  sequential  cells  are replaced with equivalent scan cells. when false, no scan replacement done on objects (objects may be cells[as FF/LAT], hier cells, lib cells, ref, design). Sequential cells violated by dft_drc are not replaced by equivalent scan cells, regardless of their scan_element attribute values.

# Specify scan style: 4 styles: multiplexed_flip_flop, clocked_scan, lssd, scan_enabled_lssd. set_scan_configuration or test_default_scan_style can be used to set scan style.
#set_scan_configuration -style [multiplexed_flip_flop | clocked_scan | lssd |  aux_clock_lssd  | combinational | none] => By default, insert_dft uses the scan style value specified by environment variable test_default_scan_style in your .synopsys_dc.setup file
set_scan_configuration -style multiplexed_flip_flop

#count
set_scan_configuration -chain_count 1 => number of chains that insert_dft is to build. Here's it's 1. If not specified, insert_dft builds the minimum  number of scan chains consistent with clock mixing constraints.

#set_scan_configuration -clock_mixing [no_mix | mix_edges | mix_clocks | mix_clocks_not_edges] => Specifies  whether  insert_dft  can include cells from different clock domains in the same scan chain.
no_mix                 The default; cells must be clocked by the same edge of the same clock.
mix_edges              Cells must be clocked by the same clock, but the clock edges can be different.
mix_clocks_not_edges   Cells must be clocked by the same clock edge, but the clocks can be different.
mix_clocks             Cells can be clocked by different clocks and different clock edges.

set_scan_configuration -clock_mixing mix_clocks => we use mix_clocks even though during scan_mode, we have only 1 clk. Reason is that lockup element can only be added if mix_clocks option is used.

#lockup
set_scan_configuration -add_lockup true => Inserts  lockup  latches (synchronization element) between clock domain  boundaries  on  scan  chains,  when  set  to  true  (the default). If the scan specification  does  not  mix clocks on chains, insert_dft ignores this option.

#lockup_type [latch | flip_flop] => The default lock-up type is a level-sensitive latch.  If you  choose  flip_flop  as  the  lock-up type, an edge-triggered flip-flop is used as  the  synchronization  element.

#set_scan_configuration -internal_clocks [single|none|multi] => An internal clock is defined as an internal signal driven  by  a multiplexer (or multiple input gate) output pin (excludes clk gating cells). Applies  only  to  the  multiplexed flip-flop scan style, and is ignored for other scan styles.  It's used to avoid problems when placing gating logic on the clock lines (which might result in hold issues).
none (the default) - insert_dft does not treat internal clocks as  separate  clocks.   This  is  the default value for internal_clocks option.
single  -  insert_dft treats any internal clocks in the design as separate clocks for the purpose of scan chain architecting.The single value stops at the first buffer or inverter driving the flip-flops clock.
multi - insert_dft treats any internal clocks in the design as separate  clocks  for  the purpose of scan chain architecting. The multi value jumps over any buffers and inverters, stopping at the first multi-input gate driving the flip-flops clock.

set_scan_configuration -internal_clocks multi => for our design, we set it to multi.

#set_scan_link scan_link_name [Wire | Lockup] => Declares  a  scan link for the current design.  Scan links connect scan cells, scan segments, and scan ports within scan chains.  DFT  Compiler supports scan links that are implemented as wires (type Wire) and scanout lock-up latches (type Lockup).
set_scan_link LOCKUP Lockup => we name the scanlink LOCKUP

#set_scan_path specifies scan path.
set_scan_path chain1 => Specifies a name for the scan chain, here it's called chain1

#set_scan_state [unknown|test_ready|scan_existing] => sets the scan state status for the current design. Use this command only on a design that has  been  scan-replaced  using,for example, compile -scan, so that the Q ouputs of scan flip-flops are connected to the scan inputs and the scan enable pins are connected  to logic  zero.   If  there  are  nonscan  elements  in  the  design,  use set_scan_element false to identify them.
unknown=>the  scan  state  of  the  design  is  unknown,
test_ready=>the design is scan-replaced,
scan_existing=>the design is scan-inserted.

set_scan_state test_ready

--- End of scan_constriants.tcl file.

###### configure your design for scan testing by generating test protocol using create_test_protocol. Test protocol files are written in spf (STIL procedure file) which are then input to pattern generation tools as TetraMax, Encounter Test to generate pattern file in STIL format.
#create_test_protocol [-infer_asynch, -infer_clock, -capture_procedure single_clock | multi_clock] => creates a test protocol for the current design based on user specifications which were issued prior to  running  this command.  The   specifications   were  made  using  commands  such  as set_dft_signal, etc. The  create_test_protocol command should be executed before running the dft_drc command because design rule checking requires a test  protocol.
-infer_asynch => Infers asynchronous set and reset signals in the design, and places them at off state  during scan shifting.
-infer_clock => Infers test clock pins from the design, and pulses them during scan shifting.
-capture_procedure [single_clock | multi_clock] => Specifies the capture procedure type.  The multi_clock type creates a protocol file that uses generic  capture  procedures  for all  capture  clocks.   The single_clock type creates a protocol file that uses the legacy 3-vector capture  procedures  for  all capture clocks. The default value is multi_clock.

create_test_protocol -infer_clock => -infer_clock not needed if scan_clock defined above.

####DFT DRC Checking
dft_drc checks for these 3 violations:
--
1. Violations That Prevent Scan Insertion: caused due to 3 cond:
 A. FF clk is uncontrollable. => clk at FF should toggle due to test clk toggling, and clk at FF shoudl be in known state at time=0 (sometimes clk gating causes this issue)
 B. latch is enabled at the beginning of the clock cycle C.
 C. async controls of registers are uncontrollable or are held active. => if set/reset of FF/latch can't be disabled by PI of design.

2. Violations That Prevent Data Capture: caused due to these:
 A. clk used as data i/p to FF,
 B. o/p of black box feeds in clk of reg (clk may or may not fire depending on logic),
 C. src reg launch before dest reg capture,
 D. registered clk gating ckt (caused due to clk gating implemented wrongly)
 E. 3 state contention
 F. clk feeding multiple i/p of same reg (i.e. clk signal feeding into clk pin and async set/reset)

3. Violations That Reduce Fault Coverage:
 A. combo feedback loops (i.e if loops are used as a latch, replace them with a latch)
 B. Clocks That Interact With Register Input
 C. Multiple Clocks That Feed Into Latches and FF. Latches should be transparent, and latches must be enabled by one clock or by a clock ANDed with data derived from sources other than that clock.
 D. black boxes: logic surrounding BB is unobservable or uncontrollable.
 ---
#dft_drc [-pre_dft|-verbose|-coverage_estimate|-sample percentage] => checks the current design against the test design rules of the scan test implementation specified  by  the  set_scan_configuration -style  command. If design rule violations are found, the appropriate messages are  generated. Perform  test  design  rule  checking on a design before performing any other DFT Compiler operations, such as insert_dft, and after creating a valid test protocol.
-pre_dft => Specifies  that  only  pre-DFT  rules (D rules) are checked. By default, for scan-routed designs, post-DFT  rules  are checked; otherwise pre-DFT rules are checked.
-verbose => Controls  the  amount  of detail when displaying violations. every violation instance is displayed.
-coverage_estimate => Generates a test coverage estimate at the  end  of  design  rule checking.
-sample percentage =>  Specifies a sample percent of faults to be considered when estimating test coverage.

dft_drc -verbose => check for violations here, before proceeding. shows black box violations for macro, non-scan flop violation because of set_scan_element being set to false, non-scan flops present in design (flops didn't get replaced by their scan equiv because of set_scan_element being set to false or other issues ). It shows final scan flops and non-scan flops.

#preview_dft => Previews,  but does not implement, scan style, the test points, scan chains, and on-chip clocking control logic to be added  to  the  current design. The command first generates information on the scan  architecture  that  will be implemented.  In the case of a DFTMAX insertion, preview_dft provides information about the  compressor  being  created,and for basic scan, the specific chain information for the design. Next,  the command generates and displays a scan chain design that satisfies scan specifications on the current  design. This design is exactly the scan chain design that is presented to the insert_dft command for synthesis.
preview_dft -show all => Reports  information  for all objects in scan chain design.
preview_dft -test_points all => Reports all test points information, in addition to the  summary report  the preview_dft command produces by default.  The information displayed includes names assigned  to  the  test  points,locations  of  violations  being fixed, names of test mode tags, logic states that enable  the  test  mode,  and  names  of  data sources or sinks for the test points.

# Insert Scan and build the scan chain.
insert_dft => adds internal-scan or boundary-scan circuitry to the  current design. First, the  command  populates  a  flattened  representation  of  the  entire  design. By default, insert_dft performs only scan insertion and routing. Next,  insert_dft architects scan chains into the design. By default, insert_dft constructs as many scan chains as there are clocks  and  edges.   By   setting   the -clock_mixing option, we can control scan chains created. Scan cells are ordered on scan chains based on some criteria. Then the command applies a scan-equivalence process to all cells. The insert_dft command then adds generic disabling logic  where  necessary.   It  finds  and  gates all pins that do not hold the values they require during scan shift.Having  disabled  three-state buses and configured bidirectional ports,insert_dft builds the scan chains. It identifies scan_in and scan_out ports. Finally,  insert_dft  routes  global  signals (including either or both scan enable and test clocks) and applies a default  technology  mapping to  all new generic logic (including disabling logic and multiplexers). It  introduces  dedicated  test  clocks  for  clocked_scan,  lssd,  and aux_clock_lssd scan styles. Typically, at this point, the insert_dft command has  violated  compile design  rules  and  constraints  and now begins minimizing these violations, and optimizing the design.
The insert_dft command automatically updates the test protocol after inserting scan circuitry into the design, and dft_drc can be executed  afterward without rerunning create_test_protocol.

# DFT DRC Checking after insertion
write_test_protocol -output digtop_scan.spf => Writes a test protocol file to file_name specified.
dft_drc -verbose -coverage_estimate => verbose rpt with coverage post scan to make sure no violations. Coverage reported here is inferior than one reported by TetraMax/ET as those are more accurate. Also, scan_reset pin if present is not considered in coverage here, as we never provided scan_reset pin info to the tool. It's just tied to inactive state here.
#test coverage = detected_faults / (total_faults - undetectable_faults) => This is the important one to look at
#fault coverage = detected_faults / (total_faults) => this is always lower than test_coverage as UD faults not included. Not so important one to look at.

#report on scan structure:
report_scan_chain > reports/scan.rpt
report_scan_path -view existing_dft -chain all >> reports/scan.rpt
report_scan_path -view existing_dft -cell all >> reports/scan.rpt

# reports after scan insertion => redirect cmd used to create scan.area/max_timing/min_timing/compile rpts in reports/scan.compile.rpt/timing.rpt files. Then cleanup done, and verilog file wriiten in *_scan.v file.

#we don't run inc compile after scan, as scan cells are only getting stitched, so each Q pin sees a little bit of extra load due to SD pin of next flop. That little extra load causes <0.1ns timing change, so scan timing and func timing are almost same (scan mode is set to 0 for both, scan_enable/shift_enable is not forced). Sometimes, wire from Q to SDI may be significant because may be the next flop in chain is in another block very far away, resulting in large timing violation on such paths in DC (due to large transition time). When we see such paths in DC, we should ignore them as PnR tool will buffer and fix it. NOTE: this scan timing is different that scan timing in PT, as scan timing in PT reflects timing in scan_mode=1. We can run such timing in DC too (by setting scan_mode=1, defining single scan_clk, and setting all i/o delays wrt scan_clk). However, we'll only see hold violations here mostly related to scan_shift_en paths (setup path failures would mostly be same as those of functional paths).

#NOTE: dft compiler adds a mux whenever a functional pin is used as SDO pin. Select pin of mux is tied to ScanEnable pin. "0" input is functional o/p, while "1" i/p is connected to o/p pin of last scan chain. That's why after routing scan chains, we see extra mux in digtop.scan.v compared to digtop.v. We need this as functional flop and SDO flop may not be same flop. compiler may decide to have sdo_out from a different flop than func pin flop. If SDO pin of last scan chain is connected directly to func o/p port, then this mux is not reqd.

-----------------------------------------------------------------
# Write out SDC (synopsys design constraints) script file in functional mode. This script  contains  commands  that can  be  used  with  PrimeTime  or  with  Design Compiler. This sdc file combines all constraints file (user or auto generated) in func mode and so is used in AutoRoute during func mode.
write_sdc sdc/func_constraints.sdc

#count total instances in DC netlist
/home/kagrawal/scripts/count_instances_EDI.tcl => reports all gates in reports/instance_count.rpt

#use exit or quit
exit

#Final log file is in logs/top.log. Lok in this file for any errors/warning.
#Final reports: are in reports dir. Look in
#digtop.after_constrain.rpt => all false path, other constraint errors.
#digtop.compile/area/max/min for reports with no scan.
#digtop.scan.compile/area/max/min for reports with scan.

*******************************************
path groups: Look in PT notes (manually written ones) for more details.
----------
by default, DC/PT group paths based on the clock controlling the endpoint (all paths not associated with a clock are in the default path group). We'll see Path Group with "clock_name" in timing reports.

#control opt of paths: We can create our own path groups so that DC can optimize chosen critical paths.
group_path -name group3 -from in3 -to FF1/D -weight 2.5 => creates group3 path from i/p in3 to FF, and assigns a weight of 2.5 to this path group. default weight is 1 for all paths in a path group. weight can range from 0 to 100.

#opt near critical path: by default, only path with WNS is opt. but by specifying critical range, DC can opt all paths that are within that range.
group_path -critical_range 3 => opt all paths within 3 units of WNS (i.e if WNS = -15, then paths with -12ns and worse are all opt). can also use "set_critical_range 3.0 $current_design".

#opt all paths: create path group for each end point. then DC opt each path group.
set endpoints [add_to_collection [all_outputs] [all_registers -data_pins]] => all o/p ports and D pins of all FF added.
foreach_in_collection endpt $endpoints {
 set pin [get_object_name $endpt]
 group_path -name $pin -to $pin
}

---------------------------
#useful cmds:

#To remove design from DC mem, we can do this (instead of closing and opening DC)
dc_shell> remove_design -all

#Retarting dc shell again. We can read in previous .ddc file generated by using read_ddc cmd. This is helpful when we close dc-shell, but want to open previous design again.
dc_shell> read_ddc netlist/digtop_scan.ddc

#to remove design from dc_shell. can be used once design has been saved, so that we can start a new run without exiting dc_shell
dc_shell> remove_design -all => removes current design as well as all subdesigns from memory

#reporting any net connectivity
report_net -conn -v -nosplit net1 => reports (v for verbose, nosplit to get all in one line) all pins connected to the net and detailed report of cap. useful for High FO nets.

#reporting Fanout for all nets above a certain threhold
report_net_fanout -v -threshold 100 => reports (v for verbose) all nets with FO > 100

-------------------