The Dallas Mavericks and veteran big man Dirk Nowitzki have hammered out the framework of a new contract that sets Nowitzki up to earn $40 million over the next two seasons and play until age 40, according to league sources.

Sources told ESPN that some of the finer contract details are still being worked through, but the broad financial terms have been nailed down in a deal constructed to pay Nowitzki an annual salary in the $20 million range that is more commensurate with his Hall of Fame stature after playing on an ultra-steep discount for the past two seasons.

Before coming to terms with Nowitzki, Dallas rallied from two early disappointments in free agency and used its substantial salary-cap space to make some promising pickups. After being spurned by Hassan Whiteside and Mike Conley, Dallas capitalized on the sudden availability of Harrison Barnes and Andrew Bogut in Golden State -- in the wake of the Warriors' success in the Kevin Durant sweepstakes -- to agree on a four-year, $94.4 million max deal to sign Barnes and a trade to acquire Bogut.

Dirk Nowitzki has spent his entire 18-year career with the Mavericks and is is sixth on the NBA's all-time scoring list with 29,491 career points. AP Photo/Rick Bowmer

The second year of Nowitzki's new deal is likely to be a player option, sources said, which would allow the NBA's sixth all-time leading scorer to decide whether he wants to keep playing. Nowitzki, who turned 38 on June 19, has said numerous times since Dallas' season ended with a first-round exit to Oklahoma City that his ideal scenario is playing two more seasons, but he's also made it clear he wants to re-evaluate things at the end of the 2016-17 season.

Since it emerged in May that Nowitzki would likely decline his $8.7 million player option for next season and return to free agency for the first time since the summer of 2014, it has been widely expected -- both in Dallas and leaguewide -- that the 7-footer from Germany refused to even consider leaving the Mavericks, who acquired him on draft night in 1998 and have made him the focal point of their franchise ever since.

"Dirk gets to do what Dirk wants," Mavericks owner Mark Cuban said during a public appearance in June. "Period. End of story.

"I told him the other day: If Dirk wants to be the head coach, we'll move Rick [Carlisle] over a little bit. Dirk's done so much for this franchise, he's earned that opportunity."

In his 18-year career, Nowitzki has won an MVP award in both the regular season (2007) and NBA Finals (2011) to go with his 13 All-Star selections. With 29,491 career points, Nowitzki is also on pace to surpass Wilt Chamberlain and move into the top five on the league's all-time scoring list during the 2017-18 season as long as he avoids significant injury.

Nowitzki's previous contract was a three-year, $25 million deal, which represented a steep hometown discount to seemingly provide Dallas with the salary-cap flexibility needed to construct a contender during his twilight years. The Mavs, however, have failed to get out of the first round since their 2011 title run, although Nowitzki led them in scoring again this past season.

Nowitzki has acknowledged that his decision to stay in Dallas would be much more difficult if the Mavs hadn't broken through to win their first title in 2011. If he ends up playing two more seasons, Nowitzki would join Kobe Bryant as the only players in league history to spend an entire two-decade career with a single franchise.